示例#1
0
文件: Class1.cs 项目: LoysMOBA/Lab3
            public static double Determinant(RMatrix mat)
            {
                double result = 0.0;

                if (!mat.IsSquared())
                {
                    throw new Exception("The matrix must be squared!");
                }
                if (mat.GetnRows == 1)
                {
                    result = mat[0, 0];
                }
                else
                {
                    for (int i = 0; i < mat.GetnRows; i++)
                    {
                        result += Math.Pow(-1, i) * mat[0, i] * Determinant(RMatrix.Minor(mat, 0, i));
                    }
                }
                return(result);
            }
示例#2
0
文件: Class1.cs 项目: LoysMOBA/Lab3
            public static RMatrix Inverse(RMatrix m, uint round = 0)
            {
                if (m.GetnCols != m.GetnRows)
                {
                    throw new ArgumentException("Обратная матрица существует только для квадратных, невырожденных, матриц.");
                }
                RMatrix matrix      = new RMatrix(m.GetnRows, m.GetnCols); //Делаем копию исходной матрицы
                double  determinant = Determinant(m);                      //Находим детерминант

                if (determinant == 0)
                {
                    return(matrix);                  //Если определитель == 0 - матрица вырожденная
                }
                for (int i = 0; i < m.GetnRows; i++)
                {
                    for (int t = 0; t < m.GetnCols; t++)
                    {
                        RMatrix tmp = RMatrix.Minor(m, i, t);  //получаем матрицу без строки i и столбца t
                        //(1 / determinant) * Determinant(tmp) - формула поределения элемента обратной матрицы
                        matrix[t, i] = round == 0 ? (1 / determinant) * Math.Pow(-1, i + t) * Determinant(tmp) : Math.Round(((1 / determinant) * Math.Pow(-1, i + t) * Determinant(tmp)), (int)round, MidpointRounding.ToEven);
                    }
                }
                return(matrix);
            }