Пример #1
0
        public static NMatrix Minors(NMatrix matrix)
        {
            NMatrix minors = new NMatrix(new decimal[matrix.Rows, matrix.Columns]);

            for (int i = 0; i < matrix.Rows; i++)
            {
                for (int j = 0; j < matrix.Columns; j++)
                {
                    if (matrix.Rows - 1 > 0 && matrix.Columns - 1 > 0)
                    {
                        NMatrix minorCalc = new NMatrix(new decimal[matrix.Rows - 1, matrix.Columns - 1]);

                        int x0 = 0;
                        for (int x = 0; x < matrix.Rows; x++)
                        {
                            if (x == i)
                            {
                                x++;
                            }
                            if (x == matrix.Rows)
                            {
                                break;
                            }

                            int y0 = 0;
                            for (int y = 0; y < matrix.Columns; y++)
                            {
                                if (y == j)
                                {
                                    y++;
                                }
                                if (y == matrix.Columns)
                                {
                                    break;
                                }

                                minorCalc.Data[x0, y0] = matrix.Data[x, y];

                                y0++;
                            }
                            x0++;
                        }


                        minors.Data[i, j] = NMatrix.Determinant(minorCalc);
                    }
                    else if (matrix.Rows == 1 && matrix.Columns == 1)
                    {
                        minors.Data[i, j] = matrix.Data[i, j];
                    }
                    else
                    {
                        throw new ArgumentException("A minor can only be calculated for a square matrix.");
                    }
                }
            }

            return(minors);
        }
Пример #2
0
        public static NMatrix Cofactors(NMatrix matrix)
        {
            NMatrix minors = NMatrix.Minors(matrix);

            for (int i = 0; i < matrix.Rows; i++)
            {
                for (int j = 0; j < matrix.Columns; j++)
                {
                    minors.Data[i, j] *= (Tools.Validators.IsEven(i + j)) ? 1 : -1;
                }
            }

            return(minors);// Now holds cofactors
        }
Пример #3
0
        public static decimal Determinant(NMatrix matrix)
        {
            if (matrix.Rows != matrix.Columns)
            {
                throw new ArgumentException("Determinant calculation failed - the matrix wasn't square.");
            }

            decimal result = 0;

            if (matrix.Rows == 0)
            {
            }
            else if (matrix.Rows == 1)
            {
                result = matrix.Data[0, 0];
            }
            else
            {
                NMatrix cfs;
                try
                {
                    cfs = NMatrix.Cofactors(matrix);
                }
                catch (ArgumentException)
                {
                    // IS THIS CORRECT?!
                    throw new ArgumentException("Determinant calculation failed - this was likely because its determinant was undifined.");
                }

                for (int i = 0; i < matrix.Columns; i++)
                {
                    result += matrix.Data[0, i] * cfs.Data[0, i];
                }
            }

            return(result);
        }
Пример #4
0
 public IMatrix AdjointMatrix()
 {
     return(NMatrix.Adjoint(this));
 }
Пример #5
0
 public NMatrix Adjoint()
 {
     return(NMatrix.Adjoint(this));
 }
Пример #6
0
 public static NMatrix Adjoint(NMatrix matrix)
 {
     return(matrix.Cofactors().T());
 }
Пример #7
0
 public decimal Determinant()
 {
     return(NMatrix.Determinant(this));
 }
Пример #8
0
 public IMatrix CofactorsMatrix()
 {
     return(NMatrix.Cofactors(this));
 }
Пример #9
0
 public IMatrix MinorsMatrix()
 {
     return(NMatrix.Minors(this));
 }
Пример #10
0
 public NMatrix Minors()
 {
     return(NMatrix.Minors(this));
 }