/// <summary>
        /// Reduces matrix to row-echelon (REF/Gauss) or reduced row-echelon (RREF/Gauss-Jordan) form and solves for augmented columns (if any.)
        /// </summary>
        public static MatrixEliminationResult Eliminate(double[,] input, MatrixReductionForm form, int augmentedCols = 0)
        {
            int totalRowCount = input.GetLength(0);
            int totalColCount = input.GetLength(1);

            if (augmentedCols >= totalColCount)
            {
                throw new ArgumentException(nameof(augmentedCols), Properties.Resources.Exception_TooMuchAugmentedColumns);
            }

            MatrixEliminationResult result = new MatrixEliminationResult();

            double[,] output = CreateCopy(input);

            // number of pivots found
            int numPivots = 0;

            // loop through columns, exclude augmented columns
            for (int col = 0; col < totalColCount - augmentedCols; col++)
            {
                int?pivotRow = FindPivot(output, numPivots, col, totalRowCount);

                if (pivotRow == null)
                {
                    continue; // no pivots, go to another column
                }
                ReduceRow(output, pivotRow.Value, col, totalColCount);

                SwitchRows(output, pivotRow.Value, numPivots, totalColCount);

                pivotRow = numPivots;
                numPivots++;

                // Eliminate Previous Rows
                if (form == MatrixReductionForm.ReducedRowEchelonForm)
                {
                    for (int tmpRow = 0; tmpRow < pivotRow; tmpRow++)
                    {
                        EliminateRow(output, tmpRow, pivotRow.Value, col, totalColCount);
                    }
                }

                // Eliminate Next Rows
                for (int tmpRow = pivotRow.Value; tmpRow < totalRowCount; tmpRow++)
                {
                    EliminateRow(output, tmpRow, pivotRow.Value, col, totalColCount);
                }
            }

            result.Rank                 = numPivots;
            result.FullMatrix           = output;
            result.AugmentedColumnCount = augmentedCols;
            if (augmentedCols > 0 && form == MatrixReductionForm.ReducedRowEchelonForm) // matrix has solution
            {
                result.Solution = ExtractColumns(output, totalColCount - augmentedCols, totalColCount - 1);
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Reduces matrix to row-echelon (REF/Gauss) or reduced row-echelon (RREF/Gauss-Jordan) form.
        /// Accepts the number of augmeted columns. If the number specified is null, the default number specified in the matrix is used.
        /// </summary>
        /// <remarks>
        /// If <param name="augmentedColCount">augmentedColCount</param> is null, <seealso cref="Elsheimy.Components.Linears.Matrix.AugmentedColumnCount"/> is used.
        /// </remarks>
        public virtual Matrix Reduce(MatrixReductionForm form, int?augmentedColCount = null)
        {
            int augmentedCols = augmentedColCount ?? this.AugmentedColumnCount;

            return(new Matrix(MatrixFunctions.Eliminate(this.InnerMatrix, form, augmentedCols).FullMatrix));
        }
 /// <summary>
 /// Reduces matrix to row-echelon (REF/Gauss) or reduced row-echelon (RREF/Gauss-Jordan) form.
 /// </summary>
 public static double[,] Reduce(double[,] input, MatrixReductionForm form)
 {
     return(Eliminate(input, form, 0).FullMatrix);
 }