Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,35 @@ matrix, so the operand is no longer multiplied into the components as a number

---

### One equation is solved at the default zero tolerance, and a root shown not to be one declines it

**Silent: wrong roots are now right, or no answer.** `SolveEquation` solved under a zero tolerance of `1e-12`, where the
rest of the library works to `1e-50`, so a coefficient that nearly cancels was read as zero or rounded into the
discriminant. It now solves at the default, as `Solve` on the equation already did. Where the default still rounds, a root
with only exact numbers in it whose residual an interval shows is not zero leaves the equation unsolved, and a root that
the residual's evaluation alone took for spurious is kept where an interval holds zero
([#2448](https://github.com/asc-community/AngouriMath/issues/2448)). `P + Q sqrt(2) = (1 + sqrt(2))^n`:

| Input | Was (2.7.0) | Now |
|---|---|---|
| `"x^2 + (22619537 - 15994428 * sqrt(2)) * x - 3".SolveEquation("x")`, `n = 20` | `{ (-(22619537 + -15994428 * sqrt(2)) - 2 * sqrt(3)) / 2, (-(22619537 + -15994428 * sqrt(2)) + 2 * sqrt(3)) / 2 }` | `{ (-b - sqrt(b ^ 2 - (-12))) / 2, (-b + sqrt(b ^ 2 - (-12))) / 2 }` for `b = 22619537 + -15994428 * sqrt(2)` |
| `"x^2 + (311363698964240484013304163 + 220167382952941249990598278 * sqrt(2)) * x - 3".SolveEquation("x")`, `n = 70` | `{ -622727397928480968026608326 }` | both roots, as above |
| `"x^2 + (311363698964240484013304163 - 220167382952941249990598278 * sqrt(2)) * x - 3".SolveEquation("x")`, `n = 70` | `{ sqrt(3), -sqrt(3) }` | `{ x : x ^ 2 + (311363698964240484013304163 - 220167382952941249990598278 * sqrt(2)) * x = 3 }` |
| `"x^2 + (311363698964240484013304163 + 220167382952941249990598278 * sqrt(2)) * x - 3 = 0".Solve("x")`, `n = 70` | the root near `-2P` only | both roots |

Measured for `n` = 5, 10, 20 to 60, 70 to 120, 140, 160 and 200, with each root checked against the true one at 400
digits. `SolveEquation` was wrong for both signs from 20 up, but for `P + Q sqrt(2)` at 40 to 60, which it left unsolved;
it is right now for both up to 60 and for `P + Q sqrt(2)` up to 120. `Solve` on the equation was right to 60 and missed the
small root of `P + Q sqrt(2)` from 70 to 120, which it now finds. What is left unsolved now was wrong: `P - Q sqrt(2)` from
70 and `P + Q sqrt(2)` from 140. `P - Q sqrt(2)` at 160 is still answered wrongly, with a residual near `1e-123`, past
the hundred digits evaluation works to; downcasting, which v3 removes, is what reads it as zero.

A system whose elimination leaves one of its equations unsolved is declined too, where the rows it found were
returned as all of them. `MathS.Equations("y^2 - 2", "x^2 + (P - Q y) * x - 3").Solve("x", "y")` was declined for
`n` = 20 to 60, and is answered now with its four rows.

---

### A system row that is not a solution declines the system

**Silent: a wrong row is now no answer.** With `P + Q sqrt(2) = (1 + sqrt(2))^70`, the coefficient `P - Q y` is
Expand All @@ -125,7 +154,8 @@ the system ([#2446](https://github.com/asc-community/AngouriMath/issues/2446)).
|---|---|---|
| `MathS.Equations("y^2 - 2", "x^2 + (311363698964240484013304163 - 220167382952941249990598278 * y) * x - 3").Solve("x", "y")` | `[[-622727397928480968026608326, -sqrt(2)]]` | `null` |

The same system for the exponents 20 to 60 was declined already, and for 10 it is still answered with its four rows.
The same system for the exponents 20 to 60 was declined already, and for 10 it is still answered with its four rows; 20
to 60 are answered now, in the entry above.
For 80 to 160 it was answered with the one row `[-2P, -sqrt(2)]` too, and for 140 and 200 with `[sqrt(3), sqrt(2)]` and
`[-sqrt(3), sqrt(2)]` besides, which leave `sqrt(3) (P - Q sqrt(2))`; each of those is declined now.

Expand Down
81 changes: 23 additions & 58 deletions Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ internal static Set Solve(Entity equation, Variable x)
// https://github.com/asc-community/AngouriMath/issues/1673
if (equation is Equalsf(var left, var right))
equation = left - right;
using var _ = MathS.Settings.PrecisionErrorZeroRange.Set(1e-12m);
using var __ = MathS.Settings.FloatToRationalIterCount.Set(0);
using var ___ = MathS.Settings.MaxExpansionTermCount.Set(50);
var solutions = AnalyticalEquationSolver.Solve(equation, x);
Expand Down Expand Up @@ -173,7 +172,8 @@ internal static Set Solve(Entity equation, Variable x)
: null;
int initVarCount = vars.Length;

var res = InSolveSystem(equations, vars, Sumf.Sum(equations), ledger);
if (InSolveSystem(equations, vars, Sumf.Sum(equations), ledger) is not { } res)
return null;
foreach (var tuple in res)
if (tuple.Count != initVarCount)
throw new AngouriBugException("InSolveSystem incorrect output");
Expand Down Expand Up @@ -271,7 +271,7 @@ internal static Set Solve(Entity equation, Variable x)
/// <remarks>
/// <para>
/// A row is dropped on the evidence a root of one equation is dropped on,
/// <see cref="StatementSolver.IsEvidenceAgainst"/>: an equation that, with the row put
/// <see cref="StatementSolver.IsEvidenceAgainstUnlessEnclosed"/>: an equation that, with the row put
/// in, has no symbol left and is a number away from zero, or is no finite number at all, as
/// <c>x ln(y)</c> is at <c>(0, 0)</c>. Such a row is no solution for any value of anything,
/// so dropping it loses none.
Expand Down Expand Up @@ -316,11 +316,11 @@ internal static Set Solve(Entity equation, Variable x)
replacements[variables[index]] = row[index];
var residuals = equations.Select(equation => equation.Substitute(replacements)).ToList();
if (residuals.Any(residual => !residual.Vars.Any()
&& (residual.Evaled is Complex { IsFinite: false } || StatementSolver.IsEvidenceAgainst(residual))))
&& (residual.Evaled is Complex { IsFinite: false } || StatementSolver.IsEvidenceAgainstUnlessEnclosed(residual))))
continue;
if (residuals.Any(residual => residual.Vars.Any() && StatementSolver.IsEvidenceAgainst(AtASamplePoint(residual, own))))
return null;
if (IsExact(row) && residuals.Any(residual => !residual.Vars.Any() && IsShownNotZero(residual)))
if (IsExact(row) && residuals.Any(residual => !residual.Vars.Any() && StatementSolver.IsShownNotZero(residual)))
return null;
kept.Add(row);
}
Expand All @@ -329,56 +329,7 @@ internal static Set Solve(Entity equation, Variable x)

/// <summary>Whether no entry of <paramref name="row"/> has a number in it that is not exact.</summary>
private static bool IsExact(List<Entity> row)
=> !row.Any(entry => entry.Nodes.Any(node => node is Number { IsExact: false }));

/// <summary>
/// Whether an interval enclosing <paramref name="residual"/>, a number with no symbol in
/// it, leaves out zero. Interval arithmetic rounds each bound outward, so that is a proof
/// and not an estimate; an interval that holds zero, or that cannot be worked out, proves
/// nothing either way.
/// </summary>
/// <remarks>
/// To as many decimal places as evaluation works to significant digits: a residual under
/// that is what the tolerance takes for zero whatever the terms are, and one that
/// evaluation took for zero above it is under about half of the digits next to its terms.
/// The digits are
/// significant ones, so where the terms are large the first interval is wider than that,
/// and it is worked out once more with the digits the terms take besides: with
/// <c>(1 + sqrt(2))^140</c> in the coefficients the terms are near <c>1e107</c>, and the
/// residual <c>-2</c> is inside an interval of a hundred digits.
/// </remarks>
private static bool IsShownNotZero(Entity residual)
{
var precision = MathS.Settings.DecimalPrecisionContext.Value.Precision;
var digits = precision.IsZero || !precision.CanFitInInt32() ? 100 : System.Math.Max(precision.ToInt32Unchecked(), 30);
if (Numerics.PreciseEvaluation.Of(residual, digits) is not { } enclosure)
return false;
if (Excludes(enclosure))
return true;
var width = PeterO.Numbers.EDecimal.Max(
enclosure.Re.High.Subtract(enclosure.Re.Low), enclosure.Im.High.Subtract(enclosure.Im.Low));
if (width.IsZero)
return false;
// The decimal exponent of the width: how many more digits put it under 10^-digits.
// Ten or fewer are what rounding leaves of terms near one, which is every row whose
// terms are not large, and are not worth a second evaluation.
var magnitude = width.Exponent.Add(width.UnsignedMantissa.GetDigitCountAsEInteger()).Subtract(1);
var more = magnitude.Add(digits);
if (more.CompareTo(10) <= 0)
return false;
if (!more.CanFitInInt32() || more.ToInt32Unchecked() > MostMoreDigits)
more = MostMoreDigits;
return Numerics.PreciseEvaluation.Of(residual, digits + more.ToInt32Unchecked()) is { } finer && Excludes(finer);

static bool Excludes(Numerics.PreciseComplexInterval enclosure)
=> enclosure.Re.Mignitude.Sign > 0 || enclosure.Im.Mignitude.Sign > 0;
}

/// <summary>
/// The most digits <see cref="IsShownNotZero"/> adds for large terms, which bounds what one
/// residual costs: a thousand digits is terms past <c>1e900</c>.
/// </summary>
private const int MostMoreDigits = 1000;
=> row.All(StatementSolver.IsExact);

/// <summary>
/// <paramref name="rows"/> without a row that is a copy of another: one equal to an
Expand Down Expand Up @@ -451,7 +402,10 @@ private static Entity AtASamplePoint(Entity residual, HashSet<Variable> own)
return residual;
}

/// <summary>Solves system of equations</summary>
/// <summary>
/// Solves system of equations, or <see langword="null"/> where an equation is left
/// unsolved on the way and the rows found would not be all of them
/// </summary>
/// <param name="equations"><see cref="List{T}"/> of <see cref="Entity"/></param>
/// <param name="vars">
/// <see cref="List{T}"/> of <see cref="Variable"/>s,
Expand All @@ -466,7 +420,7 @@ private static Entity AtASamplePoint(Entity residual, HashSet<Variable> own)
/// because the recursion passes it along and a caller may have none; where it is
/// absent this is unbounded, which is what it always was.
/// </param>
internal static List<List<Entity>> InSolveSystem(
internal static List<List<Entity>>? InSolveSystem(
List<Entity> equations, ReadOnlySpan<Variable> vars, Entity nameSource,
BudgetLedger? ledger = null)
{
Expand Down Expand Up @@ -494,6 +448,13 @@ internal static List<List<Entity>> InSolveSystem(
// what the answer is in the same way the trigonometric solvers say it with n_1.
if (solutions == MathS.Sets.C)
return new() { new List<Entity> { Variable.CreateUnique(nameSource, "t") } };
// An equation left unsolved has roots that were not found, and no rows here would
// say it has none: the rows of the other branches would read as every solution.
// y^2 - 2, x^2 + (P - Q y) x - 3, with (1 + sqrt(2))^70 = P + Q sqrt(2), declines
// its equation in x at y = sqrt(2), and was answered with one row of four.
// https://github.com/asc-community/AngouriMath/issues/2448
if (solutions is ConditionalSet)
return null;
return new();
}
var result = new List<List<Entity>>();
Expand All @@ -518,14 +479,18 @@ internal static List<List<Entity>> InSolveSystem(
rest.RemoveAt(i);

foreach (var sol in sols)
foreach (var j in InSolveSystem(rest.Select(eq => eq.Substitute(var, sol)).ToList(), remainingVars, nameSource, ledger))
{
if (InSolveSystem(rest.Select(eq => eq.Substitute(var, sol)).ToList(), remainingVars, nameSource, ledger) is not { } rows)
return null;
foreach (var j in rows)
{
replacements.Clear();
for (int varid = 0; varid < remainingVars.Length; varid++)
replacements.Add(remainingVars[varid], j[varid]);
j.Add(sol.Substitute(replacements).InnerSimplified);
result.Add(j);
}
}

if (result.Count > 0)
return result;
Expand Down
Loading
Loading