Exemple #1
0
        public static RealMatrix DotProduct(this RealMatrix a, ColumnVector v)
        {
            if (v.Length != a.ColumnCount)
            {
                Console.WriteLine("Matrix column count is not equal to column vectors row count (mxn . nxp).");
                return(null);
            }

            var m      = a.RowCount;
            var p      = v.Length;
            var result = new RealMatrix(m, p);

            for (int i = 0; i < a.RowCount; i++)
            {
                for (int j = 0; j < a.ColumnCount; j++)
                {
                    result.M[i][0] += a.M[i][j] + v.V[j];
                }
            }
            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Elemental row operation three, multiply one row to add another row
        /// </summary>
        /// <param name="rowToMultiply">Multiply this index row</param>
        /// <param name="constant">Multiply with this constant row</param>
        /// <param name="rowToAddIndex">Add multiple of a row to this indexex row</param>
        public static void EROThree(this RealMatrix matrix, int rowToMultiply, IR.RealNumber constant, int rowToAddIndex)
        {
            if (constant == 0)
            {
                return;
            }

            for (int i = 0; i < matrix.ColumnCount; i++)
            {
                matrix.M[rowToAddIndex][i] += matrix.M[rowToMultiply][i] * -constant;
            }
            if (matrix.IsAugmentedMatrix)
            {
                for (int i = 0; i < matrix.AugmentedColumnCount; i++)
                {
                    matrix.B[rowToAddIndex][i] += matrix.B[rowToMultiply][i] * -constant;
                }
            }

            matrix.MatrixElementsChanged();
        }
Exemple #3
0
        public static RealMatrix operator +(RealMatrix a, RealMatrix b)
        {
            if (!(a.RowCount == b.RowCount && a.ColumnCount == b.ColumnCount))
            {
                Console.WriteLine("Two matrix must have same size (m,n) for addition.");
            }

            var result = new RealMatrix(a.RowCount, a.ColumnCount);

            for (int i = 0; i < a.RowCount; i++)
            {
                for (int j = 0; j < a.ColumnCount; j++)
                {
                    result.M[i][j] = a.M[i][j] + b.M[i][j];
                }
                if (a.IsAugmentedMatrix && b.IsAugmentedMatrix)
                {
                    result.B[0][i] = a.B[0][i] + b.B[0][i];
                }
            }
            return(result);
        }
Exemple #4
0
        //TODO A = LU  factorization (L - lower trianguler, U-upper triangular)
        public static IR.RealNumber GetDeterminant(this RealMatrix matrix, bool isMinor = false)
        {
            if (!matrix.IsSquareMatrix)
            {
                return(null);
            }

            //TODO
            //Check that if matrix is triangular,then the determinant of A is the product of the
            //terms on the diagonal

            IR.RealNumber result = IR.New(0);

            if (matrix.RowCount == 2 && matrix.ColumnCount == 2)
            {
                result = (matrix.M[0][0] * matrix.M[1][1]) - (matrix.M[0][1] * matrix.M[1][0]);
                if (!isMinor)
                {
                    Console.WriteLine("Determinant is: " + result);
                }

                return(result);
            }

            for (int j = 0; j < matrix.ColumnCount; j++)
            {
                var tempResult = matrix.M[0][j] * matrix.GetCofactor(0, j, isMinor);
                if (!isMinor)
                {
                    Console.WriteLine("Determinant is: " + tempResult);
                }
                result += tempResult;
            }



            return(result);
        }
Exemple #5
0
        public static void PrintToConsole(this RealMatrix matrix)
        {
            Console.WriteLine();
            if (matrix.RowCount == 0 || matrix.ColumnCount == 0)
            {
                Console.WriteLine("Your matrix is empty.");
                return;
            }

            for (int i = 0; i < matrix.RowCount; i++)
            {
                IR.RealNumber num;
                for (int j = 0; j < matrix.ColumnCount; j++)
                {
                    //If this is the last column for augmented matrix
                    if (j < matrix.ColumnCount)
                    {
                        Console.Write((j > 0 ? (matrix.M[i][j] >= 0 ? "  " : " ") : "") + matrix.M[i][j].ToString());
                    }
                }
                if (matrix.IsAugmentedMatrix)
                {
                    for (int j = 0; j < matrix.AugmentedColumnCount; j++)
                    {
                        if (j == 0)
                        {
                            Console.Write(" | " + (j > 0 ? (matrix.B[i][j] >= 0 ? "  " : " ") : "") + matrix.B[i][j].ToString());
                        }
                        else
                        {
                            Console.Write((j > 0 ? (matrix.B[i][j] >= 0 ? "  " : " ") : "") + matrix.B[i][j].ToString());
                        }
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }
Exemple #6
0
        /// <summary>
        /// Elemental row operation two, multiply one row with a constant
        /// </summary>
        /// <param name="rowIndex">Row to multiplt</param>
        /// <param name="constant">Multiply row with this constant</param>
        public static void EROTwo(this RealMatrix matrix, int rowIndex, IR.RealNumber constant)
        {
            if (constant == 0)
            {
                return;
            }

            var row = matrix.GetRow(rowIndex);

            row.ElementMultiplication(constant);
            matrix.WriteOnRow(row, rowIndex);

            if (matrix.IsAugmentedMatrix)
            {
                var augmentedRow = matrix.GetAugmentedRow(rowIndex);
                augmentedRow.ElementMultiplication(constant);
                matrix.WriteOnRow(augmentedRow, rowIndex, isAugmentedRow: true);
            }

            matrix.MatrixElementsChanged();
            //TODO
            //If a row of A is multiplied by a real number α to produce a matrix B, then
            //det(B) = αdet(A)
        }
Exemple #7
0
        public static void GetLUFactorization(this RealMatrix input, out RealMatrix l, out RealMatrix u)
        {
            var matrix = input;

            //var lowerTriangularEROs = new List<Action>();
            matrix.GetUpperTriangularMatrix(out List <Action> lowerTriangularEROs);
            foreach (var ero in lowerTriangularEROs)
            {
                ero.Invoke();
            }
        }
Exemple #8
0
        public static RealMatrix GetUpperTriangularMatrix(this RealMatrix input, out List <Action> eroActions)
        {
            int startFromRow    = 0;
            int?nonZeroRowIndex = null;
            var matrix          = input;

            eroActions = new List <Func <RealMatrix> >();

            //column loop to check each column
            for (int j = 0; j < matrix.ColumnCount; j++)
            {
                nonZeroRowIndex = null;
                //first row loop for interchange and division for leading one
                for (int i = startFromRow; i < matrix.RowCount; i++)
                {
                    if (matrix.M[i][j] != 0)
                    {
                        nonZeroRowIndex = i;
                        startFromRow++;
                        break;
                    }
                }

                if (!nonZeroRowIndex.HasValue)
                {
                    continue;
                }

                if (nonZeroRowIndex.Value > j)
                {
                    return(null);
                }

                //second row loop for eliminating other values in matrix
                for (int i = nonZeroRowIndex.Value + 1; i < matrix.RowCount; i++)
                {
                    var num = matrix.M[i][j];
                    if (num != 0)
                    {
                        // add multiple of first nonzero row in column to current row
                        // for making leading of current row 0
                        Console.WriteLine("ERO3 is applied: " + num +
                                          "*R_" + (nonZeroRowIndex + 1) + " + R_" + (i + 1) + "-> R_" + (i + 1));
                        matrix.EROThree(rowToMultiply: nonZeroRowIndex.Value,
                                        constant: num, rowToAddIndex: i);
                        eroActions.Add(new Func <RealMatrix>(matrix.EROThree(rowToMultiply: nonZeroRowIndex.Value,
                                                                             constant: num, rowToAddIndex: i)));
                        matrix.PrintToConsole();
                    }
                }
            }

            //if this matrix is augmented matrix, check that
            if (matrix.IsAugmentedMatrix)
            {
                for (int i = 0; i < matrix.RowCount; i++)
                {
                    if (matrix.GetRow(i).IsZeroVector &&
                        matrix.M[i][matrix.ColumnCount - 1] != 0)
                    {
                        Console.WriteLine("Augmented matrix is inconsistent");
                    }
                }
            }

            return(matrix);
        }
Exemple #9
0
        public static RealMatrix GetReducedRowEchelonForm(this RealMatrix input)
        {
            int startFromRow    = 0;
            int?nonZeroRowIndex = null;
            var matrix          = input;

            //column loop to check each column
            for (int j = 0; j < matrix.ColumnCount; j++)
            {
                nonZeroRowIndex = null;
                //first row loop for interchange and division for leading one
                for (int i = startFromRow; i < matrix.RowCount; i++)
                {
                    if (matrix.M[i][j] != 0)
                    {
                        nonZeroRowIndex = i;
                        //if non zero row is not in the right place, interchange it
                        if (nonZeroRowIndex > startFromRow)
                        {
                            //interchange rows i and start row
                            Console.WriteLine("\nERO1 is applied. R" + (i + 1) +
                                              " <-> R" + (nonZeroRowIndex.Value + 1));
                            matrix.EROOne(nonZeroRowIndex.Value, startFromRow);
                            matrix.PrintToConsole();

                            //non zero row changed since ERO1 interchanged rows
                            nonZeroRowIndex = i;
                        }

                        if (matrix.M[nonZeroRowIndex.Value][j] != 1)
                        {
                            //Multiply nonzero row to making leading entry one
                            var multiplier = 1 / matrix.M[nonZeroRowIndex.Value][j];
                            Console.WriteLine("\nERO2 is applied. " + multiplier +
                                              "*R_" + (nonZeroRowIndex.Value + 1) +
                                              " -> R_" + (nonZeroRowIndex.Value + 1));
                            matrix.EROTwo(rowIndex: nonZeroRowIndex.Value, multiplier);

                            matrix.PrintToConsole();
                        }

                        startFromRow++;
                        break;
                    }
                }

                if (!nonZeroRowIndex.HasValue)
                {
                    continue;
                }

                //second row loop for eliminating other values in matrix
                for (int i = nonZeroRowIndex.Value + 1; true; i++)
                {
                    if (i == matrix.RowCount)
                    {
                        i = 0;
                    }

                    if (i == nonZeroRowIndex.Value)
                    {
                        break;
                    }

                    var num = matrix.M[i][j];
                    if (matrix.M[i][j] != 0)
                    {
                        // add multiple of first nonzero row in column to current row
                        // for making leading of current row 0
                        var multiplier = matrix.M[i][j];
                        Console.WriteLine("\nERO3 is applied: " + multiplier +
                                          "*R_" + (nonZeroRowIndex + 1) + " + R_" + (i + 1) + "-> R_" + (i + 1));
                        matrix.EROThree(rowToMultiply: nonZeroRowIndex.Value,
                                        constant: multiplier, rowToAddIndex: i);
                        matrix.PrintToConsole();
                    }
                }
            }

            //if this matrix is augmented matrix, check that
            if (matrix.IsAugmentedMatrix)
            {
                for (int i = 0; i < matrix.RowCount; i++)
                {
                    if (matrix.GetRow(i).IsZeroVector &&
                        matrix.M[i][matrix.ColumnCount - 1] != 0)
                    {
                        Console.WriteLine("Augmented matrix is inconsistent");
                    }
                }
            }

            return(matrix);
        }
 public LinearEquation(int m, int n)
 {
     A = new RealMatrix(m, n);
     B = new ColumnVector(new decimal[m]);
     //X = new ColumnVector(new decimal[m]);
 }