コード例 #1
0
        public static SquareRealMatrix operator *(SquareRealMatrix a, SquareRealMatrix b)
        {
            SquareRealMatrix retVal = new SquareRealMatrix(a.Rows, a.Columns);

            for (int rowCount = 0; rowCount < retVal.Rows; rowCount++)
            {
                for (int colCount = 0; colCount < retVal.Columns; colCount++)
                {
                    for (int retColCount = 0; retColCount < retVal.Columns; retColCount++)
                    {
                        retVal.InternalRep[rowCount, colCount] += a.InternalRep[rowCount, retColCount] * b.InternalRep[retColCount, colCount];
                        double sens = retVal.InternalRep[rowCount, colCount];

                        if (Math.Abs(sens) < 1.0e-8d) //f**ked up value set to zero  // TODO: tell us how you really feel about it, Brad.  GPG
                        {
                            retVal.InternalRep[rowCount, colCount] = 0;
                        }
                    }
                }
            }

            StringBuilder sb = new StringBuilder();

            sb.Append("$$");
            sb.Append(a.ToLatex());
            sb.Append(" \\cdot ");
            sb.Append(b.ToLatex());
            sb.Append(" = ");
            sb.Append(retVal.ToLatex());
            sb.Append("$$");

            retVal.m_FullRep = sb.ToString();

            return(retVal);
        }
コード例 #2
0
        public static SquareRealMatrix operator +(SquareRealMatrix a, SquareRealMatrix b)
        {
            SquareRealMatrix retVal = new SquareRealMatrix(a.Rows, a.Columns);

            for (int i = 0; i < retVal.Rows; i++)
            {
                for (int j = 0; j < retVal.Columns; j++)
                {
                    retVal.InternalRep[i, j] = a.InternalRep[i, j] + b.InternalRep[i, j];
                }
            }

            StringBuilder sb = new StringBuilder();

            sb.Append("$$");
            sb.Append(a.ToLatex());
            sb.Append(" + ");
            sb.Append(b.ToLatex());
            sb.Append(" = ");
            sb.Append(retVal.ToLatex());
            sb.Append("$$");

            retVal.m_FullRep = sb.ToString();
            return(retVal);
        }
コード例 #3
0
        public static SquareRealMatrix operator -(double b, SquareRealMatrix a)
        {
            SquareRealMatrix retVal = new SquareRealMatrix(a.Rows, a.Columns);

            for (int rowCount = 0; rowCount < retVal.Rows; rowCount++)
            {
                for (int colCount = 0; colCount < retVal.Columns; colCount++)
                {
                    retVal.InternalRep[rowCount, colCount] = b - a.InternalRep[rowCount, colCount];
                }
            }

            StringBuilder sb = new StringBuilder();

            sb.Append("$$");
            sb.Append(b.ToString());
            sb.Append(" - ");
            sb.Append(a.ToLatex());
            sb.Append(" = ");
            sb.Append(retVal.ToLatex());
            sb.Append("$$");

            retVal.m_FullRep = sb.ToString();
            return(retVal);
        }
コード例 #4
0
        public static int Test_KroneckerSum()
        {
            RealFactory      rf = new RealFactory();
            SquareRealMatrix A  = rf[2, 2,
                                     1, -1,
                                     0, 2
                                  ];

            SquareRealMatrix B = rf[2, 2,
                                    1, 0,
                                    2, -1
                                 ];

            StringBuilder sb = new StringBuilder();//Start building latex

            sb.Append(@"\begin{aligned}");

            sb.AppendFormat(@"&A = {0}\;B = {1}", A.ToLatex(), B.ToLatex() + @" \\ \\");
            sb.AppendFormat(@"&C = A\;\oplus\;B = {0}", SquareRealMatrix.KroneckerSum(A, B).ToLatex());

            sb.Append(@"\end{aligned}");

            HtmlOutputMethods.WriteLatexEqToHtmlAndLaunch(sb.ToString(), "Test_KroneckerSum.html"); //display Latex via mathjax

            return(0);
        }
コード例 #5
0
        public static int Test_UnitVector_Col_Row_Accessors()
        {
            List <double> initer = new List <double> {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
            };                                                       //init matrix vector
            SquareRealMatrix A = new SquareRealMatrix(4, 4, initer); //create 4X4 matrix

            A.Name = "A";                                            //give it a name

            UnitVectorSpace uvs  = new UnitVectorSpace(4);           //Create unit vector space to use
            UnitVector      e2   = new UnitVector("e2", 4, RowColumn.Column);
            UnitVector      e2_p = new UnitVector("e2", 4, RowColumn.Row);

            RealVector    rv = A * e2;
            StringBuilder sb = new StringBuilder();//Start building latex

            sb.Append(@"\begin{aligned}");
            sb.AppendFormat(@"&A = &{0} \\ \\", A.ToLatex()); //Display Original A matrix
            sb.Append(@"&A_{.2} = &" + rv.ToLatex() + @" \\ \\");
            RealVector rvRow = e2_p * A;

            sb.Append(@"&A_{2.}' = &" + rvRow.ToLatex());

            sb.Append(@"\end{aligned}");
            HtmlOutputMethods.WriteLatexEqToHtmlAndLaunch(sb.ToString(), "Test_UnitVector_Col_Row_Accessors.html"); //display Latex via mathjax
            return(0);
        }
コード例 #6
0
        public static int Test_Partioned_Matrix()
        {
            List <double> initer = new List <double> {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
            };                                                       //init matrix vector
            SquareRealMatrix A = new SquareRealMatrix(4, 4, initer); //create 4X4 matrix

            A.Name = "A";                                            //give it a name
            SquareRealMatrix AColumns = new SquareRealMatrix(new List <RealVector> {
                A[".1"], A[".2"], A[".3"], A[".4"]
            });                                                                                                            //Create partioned matrix from columns
            SquareRealMatrix ARows = new SquareRealMatrix(new List <RealVector> {
                A["1."], A["2."], A["3."], A["4."]
            });                                     //Create partioned matrix from rows

            StringBuilder sb = new StringBuilder(); //Start building latex

            sb.Append(@"\begin{aligned}");
            sb.AppendFormat(@"&A = &{0} \\ \\", A.ToLatex());                                                                         //Display Original A matrix
            sb.Append(@"&A = \left[ A_{.1}\;A_{.2}\;A_{.3}\;A_{.4} \right] = \;\;" + "&" + AColumns.ToLatex() + @" \\ \\");           //partioned matrix via columns of A
            sb.Append(@"&\left[ A_{1.}\;A_{2.}\;A_{3.}\;A_{4.} \right] = \;\;" + "&" + ARows.ToLatex() + @" \\ \\");                  //partioned matrix via rows of A
            sb.Append(@"&A = \left[ A_{1.}\;A_{2.}\;A_{3.}\;A_{4.} \right]' = \;\;" + "&" + ARows.Transpose().ToLatex() + @" \\ \\"); //transpose to get A
            sb.Append(@"\end{aligned}");
            HtmlOutputMethods.WriteLatexEqToHtmlAndLaunch(sb.ToString(), "Test_Partioned_Matrix.html");                               //display Latex via mathjax
            return(0);
        }
コード例 #7
0
        public static SquareRealMatrix KroneckerSum(SquareRealMatrix a, SquareRealMatrix b)
        {
            SquareRealMatrix retVal = null;
            SquareRealMatrix id     = IdentityMatrix(a.Rows);

            retVal = KroneckerProduct(a, id) + KroneckerProduct(id, b);

            retVal.FullRep = a.ToLatex() + @"\;\oplus\;" + b.ToLatex() + " = " + retVal.ToLatex(); //produce latex string

            return(retVal);
        }
コード例 #8
0
        public static string MultipliedVectorLatex(SquareRealMatrix A, double[] VectorIn, double[] VectorOut)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(A.ToLatex());
            sb.Append(VectorToLatex(VectorIn));
            sb.Append(" = ");
            sb.Append(VectorToLatex(VectorOut));

            return(sb.ToString());
        }
コード例 #9
0
        public static int Test_Rows_Of_Matrix()
        {
            List <double> initer = new List <double> {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
            };                                                                                                                   //init matrix vector
            SquareRealMatrix A = new SquareRealMatrix(4, 4, initer);                                                             //create 4X4 matrix

            A.Name = "A";                                                                                                        //give it a name
            RealVector rvRow = A["2."];                                                                                          //use accessor to get row 2 of matrix
            string     outR  = @"\begin{aligned}&A  = " + A.ToLatex() + @" \\ \\" + "&" + rvRow.ToLatex("F") + @"\end{aligned}"; //use row accessor A2. which returns row 2.

            HtmlOutputMethods.WriteLatexEqToHtmlAndLaunch(outR, "TestRows_Of_Matrix.html");                                      //display Latex via mathjax
            return(0);
        }
コード例 #10
0
        public static SquareRealMatrix KroneckerProduct(SquareRealMatrix a, SquareRealMatrix b)
        {
            int Rows                = a.Rows * b.Rows;    //calculate number of rows.
            int Columns             = a.Columns * b.Rows; // calculate number of columns
            int incC                = 0;                  //increment variable for column of b matrix
            int incR                = 0;                  //increment variable for row of b matrix
            int incAMC              = 0;                  //increment variable for column of a matrix
            int incAMR              = 0;                  //increment variable for row of a matrix
            SquareRealMatrix retVal = new SquareRealMatrix(Rows, Columns);
            int    rowCount         = 0;
            int    colCount         = 0;
            double exp              = 0;

            for (rowCount = 0; rowCount < retVal.Rows; rowCount++)
            {
                if (incR > b.Rows - 1)//reached end of rows of b matrix
                {
                    incR = 0;
                    incAMR++;
                }
                incAMC = 0;
                for (colCount = 0; colCount < retVal.Columns; colCount++)
                {
                    exp = a[incAMR, incAMC] * b[incR, incC];
                    incC++;
                    if (incC > b.Columns - 1)////reached end of columns of b matrix
                    {
                        incC = 0;
                        incAMC++;
                    }

                    retVal[rowCount, colCount] = exp;
                }
                incR++;
            }

            retVal.FullRep = a.ToLatex() + @"\;\otimes\;" + b.ToLatex() + " = " + retVal.ToLatex(); //produce latex string
            return(retVal);
        }
コード例 #11
0
        public static int Test_VecOperator()
        {
            RealFactory rf = new RealFactory(); //create a real factory

            SquareRealMatrix A = rf[2, 2,       //create a 2 X 2 real matrix
                                    1, 2,
                                    3, 4
                                 ];

            RealVector    VecA = A.Vec("A");          //use Vec operator giving the matrix a name
            StringBuilder sb   = new StringBuilder(); //Start building latex

            sb.Append(@"\begin{aligned}");

            sb.AppendFormat(@"&A = {0}", A.ToLatex() + @" \\ \\"); //display A matrix
            sb.AppendFormat(@"&{0}", VecA.ToLatex("F"));           //display Vec A
            sb.Append(@"\end{aligned}");

            HtmlOutputMethods.WriteLatexEqToHtmlAndLaunch(sb.ToString(), "Test_VecOperator.html"); //display Latex via mathjax

            return(0);
        }
コード例 #12
0
        public static int Test_TraceFunction()
        {
            RealFactory rf = new RealFactory(); //create a real factory

            SquareRealMatrix A = rf[3, 3,       //create a 3 X 3 real matrix
                                    1, 2, 3,
                                    7, 8, 9,
                                    6, 5, 4
                                 ];

            StringBuilder sb = new StringBuilder();//Start building latex

            sb.Append(@"\begin{aligned}");

            sb.AppendFormat(@"&A = {0}", A.ToLatex() + @" \\ \\");  //display A matrix
            sb.AppendFormat(@"&tr A = {0}", A.Trace() + @" \\ \\"); //display trace
            sb.Append(@"\end{aligned}");

            HtmlOutputMethods.WriteLatexEqToHtmlAndLaunch(sb.ToString(), "Test_TraceFunction.html"); //display Latex via mathjax

            return(0);
        }
コード例 #13
0
        public static SquareRealMatrix ElementaryMatrix(int rows, int columns, string Name)
        {
            SquareRealMatrix retVal = new SquareRealMatrix(rows, columns);

            retVal.Name = Name;

            if (retVal.Name[0] != 'E')
            {
                throw new Exception("Name of ElementaryMatrix must begin with a capital E followed by two number indices");
            }

            int oI1 = 0;
            int oI2 = 0;

            try
            {
                if (!int.TryParse(retVal.Name[1].ToString(), out oI1))
                {
                    throw new Exception("Name of ElementaryMatrix must begin with a capital E followed by two number indices, index 1 bad.");
                }

                if (!int.TryParse(retVal.Name[2].ToString(), out oI2))
                {
                    throw new Exception("Name of ElementaryMatrix must begin with a capital E followed by two number indices, index 2 bad");
                }

                retVal[oI1 - 1, oI2 - 1] = 1;
                retVal.LatexName         = @"E_{" + (oI1).ToString() + (oI2).ToString() + "}";
            }
            catch (Exception)
            {
                throw new Exception("Name of ElementaryMatrix must begin with a capital E followed by two number indices. Could not parse indices.");
            }

            retVal.FullRep = retVal.LatexName + @"\;=\;" + retVal.ToLatex();
            return(retVal);
        }