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
18 changes: 18 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ among the arguments.

---

### 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
`1.6e-27` at `y = sqrt(2)`, and solving one equation reads values that small as zero. The elimination then found the row
`[-2P, -sqrt(2)]`, which leaves `-2` in the second equation, and lost all four solutions. A row with only exact numbers
in it is now put back into the equations by interval arithmetic, and a residual whose interval leaves out zero declines
the system ([#2446](https://github.com/asc-community/AngouriMath/issues/2446)).

| Input | Was (2.7.0) | Now |
|---|---|---|
| `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.
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.

---

## 2.7.0 — since 2.6.0

**Read these first: names that read differently.** Each of these was read as something else on 2.6.0. Code that used
Expand Down
70 changes: 70 additions & 0 deletions Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,21 @@ internal static Set Solve(Entity equation, Variable x)
/// Anything else is kept, as a root of one equation is: a residual that cannot be
/// evaluated, or one too small next to its terms to be told from rounding.
/// </para>
/// <para>
/// Except where the row is exact and an interval enclosing a residual leaves out zero,
/// which declines the system. An exact row is a solution only where every residual is
/// exactly zero, so such a row is none, however small the residual is next to its terms;
/// and that the tolerance took it for one means rounding reached the elimination, which
/// can have lost true rows as well as made this one. In
/// <c>y^2 - 2, x^2 + (P - Q y) x - 3</c>, with <c>(1 + sqrt(2))^70 = P + Q sqrt(2)</c>,
/// <c>P - Q sqrt(2)</c> is <c>1.6e-27</c>, and the zero tolerance of one equation's solve
/// reads it, and terms of that size, as nothing: the row <c>[-2P, -sqrt(2)]</c> leaves
/// <c>-2</c> against terms of <c>4e53</c>, under a tolerance relative to them, and all
/// four solutions were gone. Rounding reaches an exact elimination through downcasting,
/// which v3 removes; the decline is to be reconsidered then, and the check kept.
/// <a href="https://github.com/asc-community/AngouriMath/issues/2446">#2446</a>,
/// <a href="https://github.com/asc-community/AngouriMath/issues/1019#issuecomment-5754734225">#1019</a>
/// </para>
/// </remarks>
private static List<List<Entity>>? RowsThatHold(List<Entity> equations, Variable[] variables, List<List<Entity>> rows)
{
Expand All @@ -305,11 +320,66 @@ internal static Set Solve(Entity equation, Variable x)
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)))
return null;
kept.Add(row);
}
return WithoutDecimalCopies(kept);
}

/// <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;

/// <summary>
/// <paramref name="rows"/> without a row that is a copy of another: one equal to an
/// earlier row, and one with a number in it that is not exact, each of whose entries is
Expand Down
48 changes: 48 additions & 0 deletions Sources/Tests/UnitTests/Common/SolverRegressionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,54 @@ public void Issue2425_AnExponentialEquationOverTheRealsHasOnlyRealRoots(string e
}
}

// https://github.com/asc-community/AngouriMath/issues/2446
// The declines here come of downcasting, which v3 removes:
// https://github.com/asc-community/AngouriMath/issues/1019#issuecomment-5754734225
// P and Q are the Pell numbers with (1 + sqrt(2))^n = P + Q sqrt(2), so at y = sqrt(2)
// the coefficient P - Q y is (1 - sqrt(2))^n, which is 1.6e-27 for n = 70. The system was
// answered there with the one row [-2P, -sqrt(2)], which leaves exactly -2 + 2P (P - Q sqrt(2))
// in the second equation, about -2, and its four solutions were lost. Each equation is a
// quadratic in x for each y, so every answer has four rows; each row is put back into
// both equations and enclosed, by interval arithmetic, within 1e-80 of zero. Declining
// is allowed where the coefficients are too close to rounding to settle, and n = 10 is
// answered today and must stay answered. At n = 140 the terms are near 1e107, past the
// hundred digits evaluation works to, and the rows [sqrt(3), sqrt(2)] and
// [-sqrt(3), sqrt(2)] were answered too.
[Theory]
[InlineData(10, true)]
[InlineData(20, false)]
[InlineData(30, false)]
[InlineData(40, false)]
[InlineData(50, false)]
[InlineData(60, false)]
[InlineData(70, false)]
[InlineData(140, false)]
[InlineData(200, false)]
public void Issue2446_EveryRowOfASystemWithACancellingCoefficientIsASolution(int n, bool answered)
{
System.Numerics.BigInteger p = 1, q = 0;
for (var i = 0; i < n; i++)
(p, q) = (p + 2 * q, p + q);
var equations = new[] { "y^2 - 2".ToEntity(), $"x^2 + ({p} - {q} * y) * x - 3".ToEntity() };
var solutions = MathS.Equations(equations).Solve("x", "y");
if (solutions is null)
{
Assert.False(answered, "declined a system that is answered");
return;
}
Assert.Equal(4, solutions.RowCount);
for (var row = 0; row < solutions.RowCount; row++)
foreach (var equation in equations)
{
var residual = equation.Substitute("x", solutions[row, 0]).Substitute("y", solutions[row, 1]);
var enclosure = AngouriMath.Numerics.PreciseEvaluation.Of(residual, 200);
Assert.True(enclosure is { } value
&& value.Re.Magnitude.CompareTo(PeterO.Numbers.EDecimal.FromString("1E-80")) <= 0
&& value.Im.Magnitude.CompareTo(PeterO.Numbers.EDecimal.FromString("1E-80")) <= 0,
$"[{solutions[row, 0]}, {solutions[row, 1]}] leaves {residual.Evaled} in {equation}");
}
}

// The complex reading is untouched: e^x + 1 = 0 has the root i pi there.
[Fact]
public void Issue2425_OverTheComplexNumbersTheRootIsKept()
Expand Down
Loading