Example #1
0
        /// <summary>
        /// Operator overload which subtracts matrix2 from matrix1
        /// </summary>
        /// <param name="mat1"></param>
        /// <param name="mat2"></param>
        /// <returns>New matrix which needs to be stored</returns>
        public static MatrixN operator -(MatrixN mat1, MatrixN mat2)
        {
            MatrixN result = new MatrixN(mat1.Row, mat1.Column);

            for (int iRow = 0; iRow < mat1.Row; iRow++)
            {
                for (int iColumn = 0; iColumn < mat1.Column; iColumn++)
                {
                    result[iRow, iColumn] = mat1[iRow, iColumn] - mat2[iRow, iColumn];
                }
            }
            return(result);
        }
Example #2
0
        /// <summary>
        /// Adds two matrices
        /// </summary>
        /// <param name="mat1"></param>
        /// <param name="mat2"></param>
        /// <returns></returns>
        public static MatrixN Add(MatrixN mat1, MatrixN mat2)
        {
            MatrixN result;

            if (mat1.Row == mat2.Row && mat1.Column == mat2.Column)
            {
                result = mat1 + mat2;
            }
            else
            {
                throw new Exception("Matrices need to have same dimensions in order to be added");
            }
            return(result);
        }
Example #3
0
        /// <summary>
        /// Multiplies two matrices if possible and throws an exception for invalid operations
        /// </summary>
        /// <param name="mat1"></param>
        /// <param name="mat2"></param>
        /// <returns></returns>
        public static MatrixN Times(MatrixN mat1, MatrixN mat2)
        {
            MatrixN result;

            if (mat1.Column == mat2.Row)
            {
                result = mat1 * mat2;
            }
            else
            {
                throw new Exception("Invalid matrix multiplication operation, row and column dont match");
            }
            return(result);
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="mat1"></param>
        /// <param name="mat2"></param>
        /// <returns></returns>
        public static MatrixN Subtract(MatrixN mat1, MatrixN mat2)
        {
            MatrixN result;

            if (mat1.Row == mat2.Row && mat1.Column == mat2.Column)
            {
                result = mat1 - mat2;
            }
            else
            {
                throw new Exception("Matrices need to have same dimensions in order to perform subtraction");
            }
            return(result);
        }
Example #5
0
        /// <summary>
        /// Operator overload for two matrix N objects
        /// </summary>
        /// <param name="mat1"></param>
        /// <param name="mat2"></param>
        /// <returns></returns>
        public static MatrixN operator *(MatrixN mat1, MatrixN mat2)
        {
            MatrixN result = new MatrixN(mat1.Row, mat1.Column);

            for (int iRow = 0; iRow < mat1.Row; iRow++)
            {
                for (int iColumn = 0; iColumn < mat2.Column; iColumn++)
                {
                    for (int counter = 0; counter < mat2.Column; counter++)
                    {
                        result[iRow, iColumn] += mat1[iRow, counter] * mat2[counter, iColumn];
                    }
                }
            }
            return(result);
        }
Example #6
0
        /// <summary>
        /// Reduces a matrix using gaussian elimination into row reduced form
        /// </summary>
        /// <param name="mat"></param>
        /// <returns></returns>
        public static MatrixN Elimination(MatrixN mat)
        {
            MatrixN result = mat;

            for (int k = 0; k < (Math.Min(result.Row, result.Column) - 1); k++)
            {
                for (int iundPivot = k + 1; iundPivot < result.Row; iundPivot++)
                {
                    double l = result[iundPivot, k] / result[k, k];
                    for (int iremRow = k + 1; iremRow < result.Column; iremRow++)
                    {
                        result[iundPivot, iremRow] = result[iundPivot, iremRow] - result[k, iremRow] * l;
                    }

                    result[iundPivot, k] = 0;
                }
            }

            return(result);
        }