예제 #1
0
        private static SimpleMatrix Add(SimpleMatrix SimpleMatrix1, SimpleMatrix SimpleMatrix2)
        {
            if (SimpleMatrix1.Rows != SimpleMatrix2.Rows || SimpleMatrix1.Cols != SimpleMatrix2.Cols)
            {
                throw new SimpleMatrixException("Operation not possible");
            }
            SimpleMatrix result = new SimpleMatrix(SimpleMatrix1.Rows, SimpleMatrix1.Cols);

            for (int i = 0; i < result.Rows; i++)
            {
                for (int j = 0; j < result.Cols; j++)
                {
                    result[i, j] = SimpleMatrix1[i, j] + SimpleMatrix2[i, j];
                }
            }
            return(result);
        }
예제 #2
0
        /// <summary>
        /// The function returns the adjoint of the current SimpleMatrix
        /// </summary>
        public SimpleMatrix Adjoint()
        {
            if (this.Rows != this.Cols)
            {
                throw new SimpleMatrixException("Adjoint of a non-square SimpleMatrix does not exists");
            }
            SimpleMatrix AdjointSimpleMatrix = new SimpleMatrix(this.Rows, this.Cols);

            for (int i = 0; i < this.Rows; i++)
            {
                for (int j = 0; j < this.Cols; j++)
                {
                    AdjointSimpleMatrix[i, j] = Math.Pow(-1, i + j) * (Minor(this, i, j).Determinent());
                }
            }
            AdjointSimpleMatrix = AdjointSimpleMatrix.Transpose();
            return(AdjointSimpleMatrix);
        }
예제 #3
0
        /// <summary>
        /// The helper function for the above Determinent() method
        /// it calls itself recursively and computes determinent using minors
        /// </summary>
        private double Determinent(SimpleMatrix SimpleMatrix)
        {
            double det = 0;

            if (SimpleMatrix.Rows != SimpleMatrix.Cols)
            {
                throw new SimpleMatrixException("Determinent of a non-square SimpleMatrix doesn't exist");
            }
            if (SimpleMatrix.Rows == 1)
            {
                return(SimpleMatrix[0, 0]);
            }
            for (int j = 0; j < SimpleMatrix.Cols; j++)
            {
                det += (SimpleMatrix[0, j] * Determinent(SimpleMatrix.Minor(SimpleMatrix, 0, j)) * (int)System.Math.Pow(-1, 0 + j));
            }
            return(det);
        }
예제 #4
0
        /// <summary>
        /// The function returns the inverse of the current SimpleMatrix using Reduced Echelon Form method
        /// The function is very fast and efficient but may raise overflow exceptions in some cases.
        /// In such cases use the Inverse() method which computes inverse in the traditional way(using adjoint).
        /// </summary>
        public SimpleMatrix InverseFast()
        {
            if (this.DeterminentFast() == 0)
            {
                throw new SimpleMatrixException("Inverse of a singular SimpleMatrix is not possible");
            }
            try
            {
                SimpleMatrix IdentitySimpleMatrix       = SimpleMatrix.IdentitySimpleMatrix(this.Rows, this.Cols);
                SimpleMatrix ReducedEchelonSimpleMatrix = this.Duplicate();
                for (int i = 0; i < this.Rows; i++)
                {
                    if (ReducedEchelonSimpleMatrix[i, i] == 0)  // if diagonal entry is zero,
                    {
                        for (int j = i + 1; j < ReducedEchelonSimpleMatrix.Rows; j++)
                        {
                            if (ReducedEchelonSimpleMatrix[j, i] != 0)           //check if some below entry is non-zero
                            {
                                ReducedEchelonSimpleMatrix.InterchangeRow(i, j); // then interchange the two rows
                                IdentitySimpleMatrix.InterchangeRow(i, j);       // then interchange the two rows
                            }
                        }
                    }
                    IdentitySimpleMatrix.MultiplyRow(i, (double)(1.0 / (ReducedEchelonSimpleMatrix[i, i])));
                    ReducedEchelonSimpleMatrix.MultiplyRow(i, (double)(1.0 / (ReducedEchelonSimpleMatrix[i, i])));

                    for (int j = i + 1; j < ReducedEchelonSimpleMatrix.Rows; j++)
                    {
                        IdentitySimpleMatrix.AddRow(j, i, -ReducedEchelonSimpleMatrix[j, i]);
                        ReducedEchelonSimpleMatrix.AddRow(j, i, -ReducedEchelonSimpleMatrix[j, i]);
                    }
                    for (int j = i - 1; j >= 0; j--)
                    {
                        IdentitySimpleMatrix.AddRow(j, i, -ReducedEchelonSimpleMatrix[j, i]);
                        ReducedEchelonSimpleMatrix.AddRow(j, i, -ReducedEchelonSimpleMatrix[j, i]);
                    }
                }
                return(IdentitySimpleMatrix);
            }
            catch (Exception)
            {
                throw new SimpleMatrixException("Inverse of the given SimpleMatrix could not be calculated");
            }
        }
예제 #5
0
 /// <summary>
 /// The function returns the Echelon form of the current SimpleMatrix
 /// </summary>
 public SimpleMatrix EchelonForm()
 {
     try
     {
         SimpleMatrix EchelonSimpleMatrix = this.Duplicate();
         for (int i = 0; i < this.Rows; i++)
         {
             if (EchelonSimpleMatrix[i, i] == 0) // if diagonal entry is zero,
             {
                 for (int j = i + 1; j < EchelonSimpleMatrix.Rows; j++)
                 {
                     if (EchelonSimpleMatrix[j, i] != 0)           //check if some below entry is non-zero
                     {
                         EchelonSimpleMatrix.InterchangeRow(i, j); // then interchange the two rows
                     }
                 }
             }
             if (EchelonSimpleMatrix[i, i] == 0) // if not found any non-zero diagonal entry
             {
                 continue;                       // increment i;
             }
             if (EchelonSimpleMatrix[i, i] != 1) // if diagonal entry is not 1 ,
             {
                 for (int j = i + 1; j < EchelonSimpleMatrix.Rows; j++)
                 {
                     if (EchelonSimpleMatrix[j, i] == 1)           //check if some below entry is 1
                     {
                         EchelonSimpleMatrix.InterchangeRow(i, j); // then interchange the two rows
                     }
                 }
             }
             EchelonSimpleMatrix.MultiplyRow(i, (double)(1.0 / (EchelonSimpleMatrix[i, i])));
             for (int j = i + 1; j < EchelonSimpleMatrix.Rows; j++)
             {
                 EchelonSimpleMatrix.AddRow(j, i, -EchelonSimpleMatrix[j, i]);
             }
         }
         return(EchelonSimpleMatrix);
     }
     catch (Exception)
     {
         throw new SimpleMatrixException("SimpleMatrix can not be reduced to Echelon form");
     }
 }
예제 #6
0
        /// <summary>
        /// The function returns the determinent of the current SimpleMatrix object as double
        /// It computes the determinent by reducing the SimpleMatrix to reduced echelon form using row operations
        /// The function is very fast and efficient but may raise overflow exceptions in some cases.
        /// In such cases use the Determinent() function which computes determinent in the traditional
        /// manner(by using minors)
        /// </summary>
        public double DeterminentFast()
        {
            if (this.Rows != this.Cols)
            {
                throw new SimpleMatrixException("Determinent of a non-square SimpleMatrix doesn't exist");
            }
            double det = 1;

            try
            {
                SimpleMatrix ReducedEchelonSimpleMatrix = this.Duplicate();
                for (int i = 0; i < this.Rows; i++)
                {
                    if (ReducedEchelonSimpleMatrix[i, i] == 0)  // if diagonal entry is zero,
                    {
                        for (int j = i + 1; j < ReducedEchelonSimpleMatrix.Rows; j++)
                        {
                            if (ReducedEchelonSimpleMatrix[j, i] != 0)           //check if some below entry is non-zero
                            {
                                ReducedEchelonSimpleMatrix.InterchangeRow(i, j); // then interchange the two rows
                                det *= -1;                                       //interchanging two rows negates the determinent
                            }
                        }
                    }

                    det *= ReducedEchelonSimpleMatrix[i, i];
                    ReducedEchelonSimpleMatrix.MultiplyRow(i, (double)(1.0 / (ReducedEchelonSimpleMatrix[i, i])));

                    for (int j = i + 1; j < ReducedEchelonSimpleMatrix.Rows; j++)
                    {
                        ReducedEchelonSimpleMatrix.AddRow(j, i, -ReducedEchelonSimpleMatrix[j, i]);
                    }
                    for (int j = i - 1; j >= 0; j--)
                    {
                        ReducedEchelonSimpleMatrix.AddRow(j, i, -ReducedEchelonSimpleMatrix[j, i]);
                    }
                }
                return(det);
            }
            catch (Exception)
            {
                throw new SimpleMatrixException("Determinent of the given SimpleMatrix could not be calculated");
            }
        }
예제 #7
0
        private static SimpleMatrix Multiply(SimpleMatrix SimpleMatrix1, SimpleMatrix SimpleMatrix2)
        {
            if (SimpleMatrix1.Cols != SimpleMatrix2.Rows)
            {
                throw new SimpleMatrixException("Operation not possible");
            }
            SimpleMatrix result = SimpleMatrix.NullSimpleMatrix(SimpleMatrix1.Rows, SimpleMatrix2.Cols);

            for (int i = 0; i < result.Rows; i++)
            {
                for (int j = 0; j < result.Cols; j++)
                {
                    for (int k = 0; k < SimpleMatrix1.Cols; k++)
                    {
                        result[i, j] += SimpleMatrix1[i, k] * SimpleMatrix2[k, j];
                    }
                }
            }
            return(result);
        }
예제 #8
0
        /// <summary>
        /// The function returns a Scalar SimpleMatrix of dimension ( Row x Col ) and scalar K
        /// </summary>
        public static SimpleMatrix ScalarSimpleMatrix(int iRows, int iCols, int K)
        {
            double zero               = 0;
            double scalar             = K;
            SimpleMatrix SimpleMatrix = new SimpleMatrix(iRows, iCols);

            for (int i = 0; i < iRows; i++)
            {
                for (int j = 0; j < iCols; j++)
                {
                    if (i == j)
                    {
                        SimpleMatrix[i, j] = scalar;
                    }
                    else
                    {
                        SimpleMatrix[i, j] = zero;
                    }
                }
            }
            return(SimpleMatrix);
        }
예제 #9
0
        /// <summary>
        /// The function concatenates the two given matrices column-wise
        /// it can be helpful in a equation solver class where the augmented SimpleMatrix is obtained by concatenation
        /// </summary>
        public static SimpleMatrix Concatenate(SimpleMatrix SimpleMatrix1, SimpleMatrix SimpleMatrix2)
        {
            if (SimpleMatrix1.Rows != SimpleMatrix2.Rows)
            {
                throw new SimpleMatrixException("Concatenation not possible");
            }
            SimpleMatrix SimpleMatrix = new SimpleMatrix(SimpleMatrix1.Rows, SimpleMatrix1.Cols + SimpleMatrix2.Cols);

            for (int i = 0; i < SimpleMatrix.Rows; i++)
            {
                for (int j = 0; j < SimpleMatrix.Cols; j++)
                {
                    if (j < SimpleMatrix1.Cols)
                    {
                        SimpleMatrix[i, j] = SimpleMatrix1[i, j];
                    }
                    else
                    {
                        SimpleMatrix[i, j] = SimpleMatrix2[i, j - SimpleMatrix1.Cols];
                    }
                }
            }
            return(SimpleMatrix);
        }
예제 #10
0
 public static SimpleMatrix operator -(SimpleMatrix SimpleMatrix1, SimpleMatrix SimpleMatrix2)
 {
     return(SimpleMatrix.Add(SimpleMatrix1, -SimpleMatrix2));
 }
예제 #11
0
 /// <summary>
 /// Internal Fucntions for the above operators
 /// </summary>
 private static SimpleMatrix Negate(SimpleMatrix SimpleMatrix)
 {
     return(SimpleMatrix.Multiply(SimpleMatrix, -1));
 }
예제 #12
0
 /// <summary>
 /// Operators for the SimpleMatrix object
 /// includes -(unary), and binary opertors such as +,-,*,/
 /// </summary>
 public static SimpleMatrix operator -(SimpleMatrix SimpleMatrix)
 {
     return(SimpleMatrix.Negate(SimpleMatrix));
 }
예제 #13
0
 public static SimpleMatrix operator /(SimpleMatrix SimpleMatrix1, double frac)
 {
     return(SimpleMatrix.Multiply(SimpleMatrix1, (double)(1.0 / frac)));
 }
예제 #14
0
 public static SimpleMatrix operator /(SimpleMatrix SimpleMatrix1, int iNo)
 {
     return(SimpleMatrix.Multiply(SimpleMatrix1, (double)(1.0 / iNo)));
 }
예제 #15
0
 public static SimpleMatrix operator *(double frac, SimpleMatrix SimpleMatrix1)
 {
     return(SimpleMatrix.Multiply(SimpleMatrix1, frac));
 }
예제 #16
0
 public static SimpleMatrix operator *(int iNo, SimpleMatrix SimpleMatrix1)
 {
     return(SimpleMatrix.Multiply(SimpleMatrix1, iNo));
 }
예제 #17
0
 public static SimpleMatrix operator *(SimpleMatrix SimpleMatrix1, SimpleMatrix SimpleMatrix2)
 {
     return(SimpleMatrix.Multiply(SimpleMatrix1, SimpleMatrix2));
 }