示例#1
0
        public MyMatrix Clone()
        {
            MyMatrix newmatrix = new MyMatrix(n, m);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    newmatrix[i, j] = values[i, j].Clone();
                }
            }
            return(newmatrix);
        }
示例#2
0
        public static MyMatrix operator *(Rational a, MyMatrix B)
        {
            MyMatrix aB = B.Clone();

            for (int i = 0; i < B.n; i++)
            {
                for (int j = 0; j < B.m; j++)
                {
                    aB[i, j] *= a;
                }
            }
            return(aB);
        }
示例#3
0
        public static MyMatrix operator -(MyMatrix A, MyMatrix B)
        {
            if (A.n != B.n || A.m != B.m)
            {
                throw new InvalidOperationException("Dimension mismatch!");
            }
            MyMatrix subt = new MyMatrix(A.n, A.m);

            for (int i = 0; i < subt.n; i++)
            {
                for (int j = 0; j < subt.m; j++)
                {
                    subt[i, j] = A[i, j] - B[i, j];
                }
            }
            return(subt);
        }
示例#4
0
        public MyMatrix Adjunct()
        {
            if (n != m)
            {
                throw new InvalidOperationException("Dimension mismatch!");
            }
            MyMatrix adjunct = new MyMatrix(n);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    adjunct[i, j] = Adjunct(i, j);
                }
            }
            return(adjunct);
        }
示例#5
0
        public MyMatrix Apply(MyMatrix vector)
        {
            MyMatrix output = new MyMatrix(n, 1);

            if (vector.N != n || vector.M != 1)
            {
                throw new InvalidOperationException("Dimension mismatch!");
            }
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    output[i, 0] += values[i, j] * vector[j, 0];
                }
            }
            return(output);
        }
示例#6
0
        public static MyMatrix FromWolfram(string input)
        {
            string[] lines = input.Replace("\n", "").Replace("\r", "").Replace(" ", "").Split(new string[] { "},{" }, StringSplitOptions.None);
            lines[0] = lines[0].Replace("{", "");
            lines[lines.Length - 1] = lines.Last().Replace("}", "");
            MyMatrix tmp = new MyMatrix(lines.Length, lines[0].Split(',').Length);

            for (int i = 0; i < lines.Length; i++)
            {
                string[] args = lines[i].Split(',');
                for (int j = 0; j < args.Length; j++)
                {
                    tmp[i, j] = new Rational(args[j]);
                }
            }
            return(tmp);
        }
示例#7
0
 public bool Equals(MyMatrix other)
 {
     if (n != other.n || m != other.m)
     {
         return(false);
     }
     for (int i = 0; i < n; i++)
     {
         for (int j = 0; j < m; j++)
         {
             if (values[i, j] != other[i, j])
             {
                 return(false);
             }
         }
     }
     return(true);
 }
示例#8
0
        public string KerBasis()
        {
            MyMatrix    row_echelon = Clone();
            string      output      = row_echelon.GaussLaTeX() + '\n';
            LaTeXExport export      = new LaTeXExport(1, ",", 1, false, "$$\\ker\\ \\text{basis}:\\ ");
            List <int>  pivots      = new List <int>();

            for (int i = 0; i < n; i++)
            {
                int j_s = 0;
                for (; j_s < m && row_echelon[i, j_s] == 0; j_s++)
                {
                    ;
                }
                if (j_s >= m)
                {
                    break;
                }
                pivots.Add(j_s);
            }
            int j_free = 0;

            for (int j = 0; j < m - pivots.Count; j++, j_free++)
            {
                for (; pivots.Contains(j_free); j_free++)
                {
                    ;
                }
                MyMatrix basis_el = new MyMatrix(m, 1);
                basis_el[j_free, 0] = 1;
                for (int i = 0; i < pivots.Count; i++)
                {
                    basis_el[pivots[i], 0] = -row_echelon[i, j_free];
                }
                export.Add(basis_el.GetLaTeX());
            }
            if (export.Empty)
            {
                export.Add(new MyMatrix(m, 1).GetLaTeX());
            }
            return(output + export.Result);
        }
示例#9
0
        public static MyMatrix operator *(MyMatrix A, MyMatrix B)
        {
            if (A.m != B.n)
            {
                throw new InvalidOperationException("Dimension mismatch!");
            }
            MyMatrix AB = new MyMatrix(A.n, B.m);

            for (int i = 0; i < AB.n; i++)
            {
                for (int j = 0; j < AB.m; j++)
                {
                    for (int l = 0; l < A.m; l++)
                    {
                        AB[i, j] += A[i, l] * B[l, j];
                    }
                }
            }
            return(AB);
        }
示例#10
0
        public static MyMatrix FromLaTeX(string input)
        {
            string[] lines = input.Replace("\n", "").Replace("\r", "").Replace(" ", "").Split(new string[] { "\\\\" }, StringSplitOptions.None);
            if (lines[0].Contains("begin"))
            {
                lines[0] = lines[0].Substring(lines[0].IndexOf('}') + 1);
                if (lines[0][0] == '{')
                {
                    lines[0] = lines[0].Substring(lines[0].IndexOf('}') + 1);
                }
            }
            if (lines.Last().Contains("end"))
            {
                lines[lines.Length - 1] = lines.Last().Substring(0, lines.Last().IndexOf("\\end"));
            }
            int end_empty = 0;

            if (lines.Last().Length == 0)
            {
                end_empty = 1;
            }
            MyMatrix tmp = new MyMatrix(lines.Length - end_empty, lines[0].Split('&').Length);

            for (int i = 0; i < tmp.N; i++)
            {
                string[] args = lines[i].Split('&');
                for (int j = 0; j < args.Length; j++)
                {
                    if (args[j].Contains("frac"))
                    {
                        tmp[i, j] = ParseFrac(args[j]);
                    }
                    else
                    {
                        tmp[i, j] = new Rational(args[j]);
                    }
                }
            }
            return(tmp);
        }
示例#11
0
        public static Rational ScalarProduct(MyMatrix a, MyMatrix b)
        {
            MyMatrix a_n, b_n;

            if (a.m == 1)
            {
                a_n = a.Transposed();
            }
            else
            {
                a_n = a;
            }
            if (b.m == 1)
            {
                b_n = b;
            }
            else
            {
                b_n = b.Transposed();
            }
            return((a_n * b_n).Sum());
        }
示例#12
0
        public string InverseLaTeX()
        {
            if (!IsSquare)
            {
                throw new InvalidOperationException("Matrix is not square!");
            }
            if (Determinant == 0)
            {
                throw new DivideByZeroException("Determinant zero!");
            }
            MyMatrix tmp = new MyMatrix(n, n * 2);

            tmp.Set(this);
            for (int i = 0; i < n; i++)
            {
                tmp[i, i + n] = 1;
            }
            string output = tmp.GaussLaTeX(n);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    tmp[i, j] = tmp[i, j + n];
                }
            }
            tmp.M = n;

            /*try
             * {
             *  if (!(tmp * this).Equals(Id))
             *      throw new Exception();
             * }
             * catch { throw new ArithmeticException("Cannot find inverse matrix!"); }*/
            values = tmp.values;
            return(output);
        }