44 size_t n = Coefficients.
GetRows();
46 if ((ConstantTerms.
GetRows() != n) || (ConstantTerms.
GetCols() != 1))
49 "const SquareMatrixDouble *Coefficients, const MatrixDouble *ConstantTerms): ") +
50 _(
"invalid or incompatible matrix dimensions"));
58 throw ExceptionRuntime(
_(
"Equation has either no solution or an infinity of solutions."));
63 for (
size_t k = 0; k < n; k++)
67 for (
size_t r = 0; r < n; r++)
69 Mk.
At(r, k) = ConstantTerms.
At(r, 0);
74 Solutions.
At(k, 0) = Dk / D;
94 size_t n = Coefficients.
GetRows();
96 if (ConstantTerms.
GetRows() != n)
99 "const SquareMatrixDouble *Coefficients, const MatrixDouble *ConstantTerms): ") +
100 _(
"invalid or incompatible matrix dimensions"));
104 USquareMatrixDouble CopyCoefficients = CloneAs<SquareMatrixDouble>(Coefficients);
105 UMatrixDouble CopyConstantTerms = CloneAs<MatrixDouble>(ConstantTerms);
107 for (
size_t c = 0; c < n - 1; c++)
111 double Pivot = CopyCoefficients->At(c, c);
112 double AbsMaxPivot = fabs(Pivot);
115 for (
size_t r = c + 1 ; r < n; r++)
117 double Candidate = CopyCoefficients->At(r, c);
119 if (fabs(Candidate) > AbsMaxPivot)
122 AbsMaxPivot = fabs(Pivot);
131 throw ExceptionRuntime(
_(
"Equation has either no solution or an infinity of solutions."));
136 CopyCoefficients->SwapRows(c, RowIndex);
137 CopyConstantTerms->SwapRows(c, RowIndex);
141 for (
size_t r = c + 1; r < n; r++)
143 double Coeff = CopyCoefficients->At(r, c);
147 double Scale = - Coeff / Pivot;
149 for (
size_t k = c; k < n; k++)
151 CopyCoefficients->IncreaseElement(r, k, CopyCoefficients->At(c, k) *
Scale);
154 CopyConstantTerms->IncreaseElement(r, 0, CopyConstantTerms->At(c, 0) *
Scale);
162 Solutions.
At(n - 1, 0) = CopyConstantTerms->At(n - 1, 0) / CopyCoefficients->At(n - 1, n - 1);
164 for (
auto r =
int(n) - 2; r >= 0; --r)
168 for (
auto c =
int(n) - 1; c > r; --c)
170 Cumul += CopyCoefficients->At(r, c) * Solutions.
At(c, 0);
173 Solutions.
At(r, 0) = (CopyConstantTerms->At(r, 0) - Cumul) / CopyCoefficients->At(r, r);
191 return (b * b - 4.0 * a * c);
207 std::set<double> roots;
211 roots.insert( - c / b);
215 double Inv2a = 1.0 / (2.0 * a);
219 roots.insert(- b * Inv2a);
225 double SqrtDelta = sqrt(Delta);
227 roots.insert((- b - SqrtDelta) * Inv2a);
228 roots.insert((- b + SqrtDelta) * Inv2a);
size_t GetRows() const noexcept
Returns the number of rows.
double Discriminant(double a, double b, double c) noexcept
Discriminant of trinom .
size_t GetCols() const noexcept
Returns the number of columns.
MatrixDouble GaussJordan(const SquareMatrixDouble &Coefficients, const MatrixDouble &ConstantTerms)
Resolution of linear equation system by the pivot method.
void Scale(ITER it_begin, ITER it_end, const double s)
Scale a collection of numbers.
std::set< double > RealRoots(double a, double b, double c) noexcept
Real roots of trinom .
const T & At(size_t pos) const noexcept
MatrixDouble Cramer(const SquareMatrixDouble &Coefficients, const MatrixDouble &ConstantTerms)
Resolution of linear equation system by the determinant method.
Square double matrix class.
A character string class.
double Determinant() const
Determinant.