示例#1
0
        private void SwapRows(Equation equation, ref bool[] hasZeroRows, Pivot pivot)
        {
            for (int i = 0; i < equation.RowsCount; i++)
            {
                (equation.Coefficients[pivot.Row, i], equation.Coefficients[pivot.Column, i]) =
                    (equation.Coefficients[pivot.Column, i], equation.Coefficients[pivot.Row, i]);
            }

            (equation.Intercepts[pivot.Row], equation.Intercepts[pivot.Column]) =
                (equation.Intercepts[pivot.Column], equation.Intercepts[pivot.Row]);

            (hasZeroRows[pivot.Row], hasZeroRows[pivot.Column]) = (hasZeroRows[pivot.Column], hasZeroRows[pivot.Row]);

            pivot.Row = pivot.Column;
        }
示例#2
0
        private double[] RowReduction(Equation equation)
        {
            bool[] hasZeroRows = new bool[equation.RowsCount];
            bool[] hasZeroCols = new bool[equation.RowsCount];

            //convert the matrix into an upper triangular matrix
            for (int i = 0; i < equation.RowsCount; i++)
            {
                Pivot pivot = SelectPivot(equation, hasZeroRows, hasZeroCols);
                SwapRows(equation, ref hasZeroRows, pivot);
                ForwardElimination(equation, pivot, ref hasZeroRows, ref hasZeroCols);
            }

            //reduction to row echelon form
            BackSubstitution(equation);

            return(equation.Intercepts);
        }
        private void RowReduction(double[] row, Equation equation)
        {
            while (true)
            {
                Pivot pivot = SelectPivot(row, equation);

                if (pivot.Row == -1 || pivot.Column == -1 || CurrentSolution == Solution.Infinity)
                {
                    break;
                }

                ResultVariables[pivot.Column] = pivot.Row;


                double divisor = equation.Coefficients[pivot.Row, pivot.Column];
                equation.Intercepts[pivot.Row] /= divisor;
                for (int i = 0; i < equation.ColsCount; i++)
                {
                    equation.Coefficients[pivot.Row, i] /= divisor;
                }

                double multiple = 0;
                for (int i = 0; i < equation.RowsCount; i++)
                {
                    if (pivot.Row != i && !EqualToZero(equation.Coefficients[i, pivot.Column]))
                    {
                        multiple = equation.Coefficients[i, pivot.Column];

                        equation.Intercepts[i] -= equation.Intercepts[pivot.Row] * multiple;

                        for (int j = 0; j < equation.ColsCount; j++)
                        {
                            equation.Coefficients[i, j] -=
                                equation.Coefficients[pivot.Row, j] * multiple;
                        }
                    }
                }

                int size = equation.Intercepts.Length;
                if (CurrentPhase == Phase.One)
                {
                    equation.Intercepts[size - 2] -=
                        equation.Intercepts[pivot.Row] * equation.Function[pivot.Column];

                    equation.Intercepts[size - 1] -=
                        equation.Intercepts[pivot.Row] * row[pivot.Column];

                    double functionCol = equation.Function[pivot.Column];
                    double rowCol      = row[pivot.Column];
                    for (int i = 0; i < equation.ColsCount; i++)
                    {
                        equation.Function[i] -=
                            equation.Coefficients[pivot.Row, i] * functionCol;

                        row[i] -=
                            equation.Coefficients[pivot.Row, i] * rowCol;
                    }

                    SecondaryRow = row;
                }
                else
                {
                    equation.Intercepts[size - 1] -=
                        equation.Intercepts[pivot.Row] * equation.Function[pivot.Column];

                    multiple = equation.Function[pivot.Column];
                    for (int i = 0; i < equation.ColsCount; i++)
                    {
                        equation.Function[i] -=
                            equation.Coefficients[pivot.Row, i] * multiple;
                    }


                    equation.Function = row;
                }
            }
        }