/// <summary>
        /// The function return the Minor of element[Row,Col] of a Matrix object
        /// </summary>
        public static MATRIX Minor(MATRIX matrix, int iRow, int iCol)
        {
            MATRIX minor = new MATRIX(matrix.Rows - 1, matrix.Cols - 1);
            int    m = 0, n = 0;

            for (int i = 0; i < matrix.Rows; i++)
            {
                if (i == iRow)
                {
                    continue;
                }
                n = 0;
                for (int j = 0; j < matrix.Cols; j++)
                {
                    if (j == iCol)
                    {
                        continue;
                    }
                    minor[m, n] = matrix[i, j];
                    n++;
                }
                m++;
            }
            return(minor);
        }
 /// <summary>
 /// The function returns the inverse of a given matrix
 /// </summary>
 public static MATRIX Inverse(MATRIX matrix)
 {
     if (Determinent(matrix) == 0)
     {
         throw new MatrixException("Inverse of a singular matrix is not possible");
     }
     return(Adjoint(matrix) / Determinent(matrix));
 }
        private static MATRIX Multiply(MATRIX matrix, Fraction frac)
        {
            MATRIX result = new MATRIX(matrix.Rows, matrix.Cols);

            for (int i = 0; i < matrix.Rows; i++)
            {
                for (int j = 0; j < matrix.Cols; j++)
                {
                    result[i, j] = matrix[i, j] * frac;
                }
            }
            return(result);
        }
Пример #4
0
        public static MATRIX operator +(MATRIX m1, MATRIX m2)
        {
            MATRIX temp = new MATRIX(m1.Rows, m1.Cols);

            for (int k = 0; k < m1.Rows; k++)
            {
                for (int h = 0; h < m1.Cols; h++)
                {
                    temp[k, h] = m1[k, h] + m2[k, h];
                }
            }
            return(temp);
        }
        /// <summary>
        /// The function duplicates the current Matrix object
        /// </summary>
        public MATRIX Duplicate()
        {
            MATRIX matrix = new MATRIX(Rows, Cols);

            for (int i = 0; i < Rows; i++)
            {
                for (int j = 0; j < Cols; j++)
                {
                    matrix[i, j] = this[i, j];
                }
            }
            return(matrix);
        }
        /// <summary>
        /// The function returns the transpose of a given matrix
        /// </summary>
        public static MATRIX Transpose(MATRIX matrix)
        {
            MATRIX TransposeMatrix = new MATRIX(matrix.Cols, matrix.Rows);

            for (int i = 0; i < TransposeMatrix.Rows; i++)
            {
                for (int j = 0; j < TransposeMatrix.Cols; j++)
                {
                    TransposeMatrix[i, j] = matrix[j, i];
                }
            }
            return(TransposeMatrix);
        }
Пример #7
0
        public static MATRIX operator /(MATRIX m1, double d)
        {
            MATRIX temp = new MATRIX(m1.Rows, m1.Cols);

            for (int k = 0; k < m1.Rows; k++)
            {
                for (int h = 0; h < m1.Cols; h++)
                {
                    temp[k, h] = m1[k, h] / d;
                }
            }
            return(temp);
        }
        /// <summary>
        /// The function returns a Null Matrix of dimension ( Row x Col )
        /// </summary>
        public static MATRIX NullMatrix(int iRows, int iCols)
        {
            Fraction temp = new Fraction(0);
            MATRIX matrix = new MATRIX(iRows, iCols);

            for (int i = 0; i < iRows; i++)
            {
                for (int j = 0; j < iCols; j++)
                {
                    matrix[i, j] = temp;
                }
            }
            return(matrix);
        }
        /// <summary>
        /// The function takes a Matrix object and returns it as a string
        /// </summary>
        public static string ConvertToString(MATRIX matrix)
        {
            string str = "";

            for (int i = 0; i < matrix.Rows; i++)
            {
                for (int j = 0; j < matrix.Cols; j++)
                {
                    str += matrix[i, j].ConvertToString() + "\t";
                }
                str += "\n";
            }
            return(str);
        }
 /// <summary>
 /// The function returns the reduced echelon form of a given matrix
 /// </summary>
 public static MATRIX ReducedEchelonForm(MATRIX matrix)
 {
     try
     {
         MATRIX ReducedEchelonMatrix = matrix.Duplicate();
         for (int i = 0; i < matrix.Rows; i++)
         {
             if (ReducedEchelonMatrix[i, i] == 0)                        // if diagonal entry is zero,
             {
                 for (int j = i + 1; j < ReducedEchelonMatrix.Rows; j++)
                 {
                     if (ReducedEchelonMatrix[j, i] != 0)                                 //check if some below entry is non-zero
                     {
                         ReducedEchelonMatrix.InterchangeRow(i, j);                       // then interchange the two rows
                     }
                 }
             }
             if (ReducedEchelonMatrix[i, i] == 0)        // if not found any non-zero diagonal entry
             {
                 continue;                               // increment i;
             }
             if (ReducedEchelonMatrix[i, i] != 1)        // if diagonal entry is not 1 ,
             {
                 for (int j = i + 1; j < ReducedEchelonMatrix.Rows; j++)
                 {
                     if (ReducedEchelonMatrix[j, i] == 1)                                 //check if some below entry is 1
                     {
                         ReducedEchelonMatrix.InterchangeRow(i, j);                       // then interchange the two rows
                     }
                 }
             }
             ReducedEchelonMatrix.MultiplyRow(i, Fraction.Inverse(ReducedEchelonMatrix[i, i]));
             for (int j = i + 1; j < ReducedEchelonMatrix.Rows; j++)
             {
                 ReducedEchelonMatrix.AddRow(j, i, -ReducedEchelonMatrix[j, i]);
             }
             for (int j = i - 1; j >= 0; j--)
             {
                 ReducedEchelonMatrix.AddRow(j, i, -ReducedEchelonMatrix[j, i]);
             }
         }
         return(ReducedEchelonMatrix);
     }
     catch (Exception)
     {
         throw new MatrixException("Matrix can not be reduced to Echelon form");
     }
 }
        private static MATRIX Add(MATRIX matrix1, MATRIX matrix2)
        {
            if (matrix1.Rows != matrix2.Rows || matrix1.Cols != matrix2.Cols)
            {
                throw new MatrixException("Operation not possible");
            }
            MATRIX result = new MATRIX(matrix1.Rows, matrix1.Cols);

            for (int i = 0; i < result.Rows; i++)
            {
                for (int j = 0; j < result.Cols; j++)
                {
                    result[i, j] = matrix1[i, j] + matrix2[i, j];
                }
            }
            return(result);
        }
        /// <summary>
        /// The function returns the determinent of a Matrix object as Fraction
        /// </summary>
        public static Fraction Determinent(MATRIX matrix)
        {
            Fraction det = new Fraction(0);

            if (matrix.Rows != matrix.Cols)
            {
                throw new MatrixException("Determinent of a non-square matrix doesn't exist");
            }
            if (matrix.Rows == 1)
            {
                return(matrix[0, 0]);
            }
            for (int j = 0; j < matrix.Cols; j++)
            {
                det += (matrix[0, j] * Determinent(MATRIX.Minor(matrix, 0, j)) * (int)System.Math.Pow(-1, 0 + j));
            }
            return(det);
        }
        /// <summary>
        /// The function returns the adjoint of a given matrix
        /// </summary>
        public static MATRIX Adjoint(MATRIX matrix)
        {
            if (matrix.Rows != matrix.Cols)
            {
                throw new MatrixException("Adjoint of a non-square matrix does not exists");
            }
            MATRIX AdjointMatrix = new MATRIX(matrix.Rows, matrix.Cols);

            for (int i = 0; i < matrix.Rows; i++)
            {
                for (int j = 0; j < matrix.Cols; j++)
                {
                    AdjointMatrix[i, j] = Math.Pow(-1, i + j) * Determinent(Minor(matrix, i, j));
                }
            }
            AdjointMatrix = Transpose(AdjointMatrix);
            return(AdjointMatrix);
        }
        private static MATRIX Multiply(MATRIX matrix1, MATRIX matrix2)
        {
            if (matrix1.Cols != matrix2.Rows)
            {
                throw new MatrixException("Operation not possible");
            }
            MATRIX result = MATRIX.NullMatrix(matrix1.Rows, matrix2.Cols);

            for (int i = 0; i < result.Rows; i++)
            {
                for (int j = 0; j < result.Cols; j++)
                {
                    for (int k = 0; k < matrix1.Cols; k++)
                    {
                        result[i, j] += matrix1[i, k] * matrix2[k, j];
                    }
                }
            }
            return(result);
        }
        /// <summary>
        /// The function returns a Scalar Matrix of dimension ( Row x Col ) and scalar K
        /// </summary>
        public static MATRIX ScalarMatrix(int iRows, int iCols, int K)
        {
            Fraction zero   = new Fraction(0);
            Fraction scalar = new Fraction(K);
            MATRIX matrix   = new MATRIX(iRows, iCols);

            for (int i = 0; i < iRows; i++)
            {
                for (int j = 0; j < iCols; j++)
                {
                    if (i == j)
                    {
                        matrix[i, j] = scalar;
                    }
                    else
                    {
                        matrix[i, j] = zero;
                    }
                }
            }
            return(matrix);
        }
        /// <summary>
        /// The function concatenates the two given matrices column-wise
        /// </summary>
        public static MATRIX Concatenate(MATRIX matrix1, MATRIX matrix2)
        {
            if (matrix1.Rows != matrix2.Rows)
            {
                throw new MatrixException("Concatenation not possible");
            }
            MATRIX matrix = new MATRIX(matrix1.Rows, matrix1.Cols + matrix2.Cols);

            for (int i = 0; i < matrix.Rows; i++)
            {
                for (int j = 0; j < matrix.Cols; j++)
                {
                    if (j < matrix1.Cols)
                    {
                        matrix[i, j] = matrix1[i, j];
                    }
                    else
                    {
                        matrix[i, j] = matrix2[i, j - matrix1.Cols];
                    }
                }
            }
            return(matrix);
        }
 /// <summary>
 /// Internal Fucntions for the above operators
 /// </summary>
 private static MATRIX Negate(MATRIX matrix)
 {
     return(MATRIX.Multiply(matrix, -1));
 }
 public static MATRIX operator /(MATRIX matrix1, Fraction frac)
 {
     return(MATRIX.Multiply(matrix1, Fraction.Inverse(frac)));
 }
 public static MATRIX operator /(MATRIX matrix1, double dbl)
 {
     return(MATRIX.Multiply(matrix1, Fraction.Inverse(Fraction.ConvertToFraction(dbl))));
 }
 public static MATRIX operator /(MATRIX matrix1, int iNo)
 {
     return(MATRIX.Multiply(matrix1, Fraction.Inverse(new Fraction(iNo))));
 }
 public static MATRIX operator *(Fraction frac, MATRIX matrix1)
 {
     return(MATRIX.Multiply(matrix1, frac));
 }
 /// <summary>
 /// The function returns the current Matrix object as a string
 /// </summary>
 public string ConvertToString()
 {
     return(MATRIX.ConvertToString(this));
 }
 public static MATRIX operator *(int iNo, MATRIX matrix1)
 {
     return(MATRIX.Multiply(matrix1, iNo));
 }
 public static MATRIX operator *(MATRIX matrix1, MATRIX matrix2)
 {
     return(MATRIX.Multiply(matrix1, matrix2));
 }
 /// <summary>
 /// Operators for the Matrix object
 /// includes -(unary), and binary opertors such as +,-,*,/
 /// </summary>
 public static MATRIX operator -(MATRIX matrix)
 {
     return(MATRIX.Negate(matrix));
 }
 public static MATRIX operator -(MATRIX matrix1, MATRIX matrix2)
 {
     return(MATRIX.Add(matrix1, -matrix2));
 }
 public static MATRIX operator *(double dbl, MATRIX matrix1)
 {
     return(MATRIX.Multiply(matrix1, Fraction.ConvertToFraction(dbl)));
 }