Beispiel #1
0
        public IList <double> Solve()  //Returns a list of coefficients for the variables in the same order they were entered
        {
            solution = new double[rows[0].Coefficients.Count()];

            for (int pivotM = 0; pivotM < rows.Count() - 1; pivotM++)
            {
                int pivotN = rows[pivotM].IndexOfFirstNonZero;

                for (int i = pivotN + 1; i < rows.Count(); i++)
                {
                    LinearEquation rowToReduce = rows[i];
                    double         pivotFactor = rowToReduce[pivotN] / -rows[pivotM][pivotN];
                    rowToReduce.AddCoefficients(rows[pivotM], pivotFactor);
                }
            }

            while (rows.Any(r => r.Result != 0))
            {
                LinearEquation row = rows.FirstOrDefault(r => r.NonZeroCount == 1);
                if (row == null)
                {
                    break;
                }

                int    solvedIndex = row.IndexOfFirstNonZero;
                double newSolution = row.Result / row[solvedIndex];

                AddToSolution(solvedIndex, newSolution);
            }



            return(solution);
        }