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
15 changes: 15 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,21 @@ its rows in the order it finds them, which can differ from the eliminator's
| `MathS.Equations("x*y", "x + y^2 - 1").Solve("x", "y")` | `[[1, 0]]` | `[[1, 0], [0, 1], [0, -1]]` |
| `MathS.Equations("x*y + 3*x - 5*y - 15", "x^2 - 3*x - 4*y - 10").Solve("x", "y")` | `[[1, -3], [2, -3]]` | `[[5, 0], [1, -3], [2, -3]]` |

### A polynomial system with roots in decimals keeps its exact solutions

**Silent: a polynomial system whose elimination has roots with no closed form has all its solutions, the rational ones
exactly.** The Gröbner triangularisation puts every tuple it finds back into the system and keeps it only where each
equation is exactly zero, which a root found numerically never is. One such root sent the whole system to the
eliminator, which answered in decimals and could lose solutions: `x^2 + y^2 = 2, x^3 - y = -2` eliminates to
`(x + 1)` times a quintic with no roots in radicals, and both of its real solutions were missing. A tuple in decimals is
now kept where each equation's residual is under `1E-10` of the largest term it adds up to. A tuple that is no solution
misses by whole quantities. An exact tuple is still checked exactly. No issue was filed; a check of systems built from
known solutions found it.

| Input | Was (2.6.0) | Now |
|---|---|---|
| `MathS.Equations("x^2 + y^2 - 2", "x^3 - y + 2").Solve("x", "y")` | the four complex solutions, in decimals | `[-1, 1]`, `[-1.3455, -0.4356]` and the four complex solutions |

### `round` is exact, and an infinity is its own nearest integer

**Silent for a long rational, not silent for an infinity.** `round` of a rational with more digits than the precision was
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using AngouriMath.Core;
using AngouriMath.Core.Budgets;
using PeterO.Numbers;
using static AngouriMath.Entity;

namespace AngouriMath.Functions.Algebra.Groebner
Expand Down Expand Up @@ -41,9 +42,11 @@ namespace AngouriMath.Functions.Algebra.Groebner
/// <c>(sqrt(2), sqrt(2))</c> and <c>(-sqrt(2), -sqrt(2))</c> — because one structural
/// pass is enough to prove a radical identity. Where a root is a decimal the check cannot
/// be made at all, and rather than accept a tuple on a tolerance, which is how a root
/// that is merely close becomes a reported solution, the whole system goes back to the
/// existing solver. So this takes what it can prove and declines the rest without
/// changing what those did before.
/// that is merely close becomes a reported solution, the whole system went back to the
/// existing solver, which answers it in decimals too and can leave out what this would have
/// found exactly: x^2 + y^2 = 2, x^3 - y = -2 lost (-1, 1). So a candidate in decimals is
/// checked to the precision it has instead, a negligible share of the largest term, which a
/// tuple that is not a solution misses by whole quantities.
/// </para>
/// </remarks>
internal static class GroebnerSystemSolver
Expand Down Expand Up @@ -151,7 +154,9 @@ static bool Attempt(

var builder = new MatrixBuilder(variables.Count);
foreach (var candidate in found)
builder.Add(candidate);
// An entry already in decimals is the number it evaluates to, rather than the
// polynomial in decimals that back-substitution wrote it as.
builder.Add(candidate.Select(entry => entry.Nodes.Any(IsADecimal) && entry.Evaled is Number.Complex value ? value : entry).ToArray());
solutions = builder.ToMatrix();
return true;
}
Expand Down Expand Up @@ -274,14 +279,48 @@ static bool Satisfies(
for (var i = 0; i < variables.Count; i++)
substitutions[variables[i]] = candidate[i];

// A candidate with a decimal in it is a root of a factor of a univariate polynomial whose
// roots have no closed form, found numerically, and no structural pass makes its residual
// exactly zero. Such a candidate is checked to the precision it has: each equation's
// residual is to be a negligible part of the largest term the equation adds up to. A
// tuple that does not satisfy the system, as a basis out of shape position can give, is
// off by whole quantities and fails this as it fails the exact test. Declining every
// decimal sent the system to elimination, which answered x^2 + y^2 = 2, x^3 - y = -2 in
// decimals and without its rational solution (-1, 1).
bool? inDecimals = null;
foreach (var equation in equations)
{
if (!budget.Spend())
return false;
if (equation.Substitute(substitutions).InnerSimplified is not Number.Integer { IsZero: true })
var substituted = equation.Substitute(substitutions);
if (substituted.InnerSimplified is Number.Integer { IsZero: true })
continue;
inDecimals ??= candidate.Any(entry => entry.Nodes.Any(IsADecimal));
if (inDecimals is false || !IsNegligible(substituted))
return budget.Allow(false, "unverifiable root");
}
return true;
}

private static bool IsADecimal(Entity node)
=> node is Number.Complex number && (number.RealPart is not Number.Rational || number.ImaginaryPart is not Number.Rational);

/// <summary>How small a residual is next to the largest term, to be what rounding leaves of a zero.</summary>
[ConstantField] private static readonly EDecimal NegligibleShare = EDecimal.FromString("1E-10");

/// <summary>
/// Whether <paramref name="substituted"/>, an equation with numbers for its unknowns,
/// evaluates to a residual negligible next to the largest of the terms it adds up to.
/// </summary>
private static bool IsNegligible(Entity substituted)
{
if (substituted.Evaled is not Number.Complex { IsFinite: true } residual)
return false;
var largest = EDecimal.One;
foreach (var term in Sumf.LinearChildren(substituted))
if (term.Evaled is Number.Complex { IsFinite: true } value)
largest = EDecimal.Max(largest, value.Abs().EDecimal);
return residual.Abs().EDecimal.CompareTo(largest.Multiply(NegligibleShare)) <= 0;
}
}
}
34 changes: 31 additions & 3 deletions Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
var solutions = MathS.Equations(equations).Solve(variables);

Assert.NotNull(solutions);
Assert.Equal(expectedSolutions, solutions.RowCount);

Check warning on line 82 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 82 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 82 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 82 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 82 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Dereference of a possibly null reference.

Check warning on line 82 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Dereference of a possibly null reference.
Assert.Equal(variableCount, solutions.ColumnCount);
AssertEverySolutionSatisfies(equations, variables, solutions);
}
Expand All @@ -97,7 +97,7 @@
var solutions = MathS.Equations(equations).Solve(variables);

Assert.NotNull(solutions);
Assert.Equal(2, solutions.RowCount);

Check warning on line 100 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 100 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 100 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 100 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 100 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Dereference of a possibly null reference.

Check warning on line 100 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Dereference of a possibly null reference.
AssertEverySolutionSatisfies(equations, variables, solutions);
}

Expand All @@ -121,7 +121,7 @@
var solutions = MathS.Equations(equations).Solve(variables);

Assert.NotNull(solutions);
Assert.Equal(2, solutions.RowCount);

Check warning on line 124 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 124 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 124 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 124 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 124 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Dereference of a possibly null reference.

Check warning on line 124 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Dereference of a possibly null reference.
AssertEverySolutionSatisfies(equations, variables, solutions);
}

Expand All @@ -135,13 +135,13 @@
Entity[] equations = { "x - 1", "y - 2" };
var solutions = MathS.Equations(equations).Solve("x", "y");
Assert.NotNull(solutions);
Assert.Equal(1, solutions.RowCount);

Check warning on line 138 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 138 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 138 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 138 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 138 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Dereference of a possibly null reference.

Check warning on line 138 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Dereference of a possibly null reference.
Assert.Equal(1, solutions[0, 0].EvalNumerical());
Assert.Equal(2, solutions[0, 1].EvalNumerical());

var swapped = MathS.Equations(equations).Solve("y", "x");
Assert.NotNull(swapped);
Assert.Equal(2, swapped[0, 0].EvalNumerical());

Check warning on line 144 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 144 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 144 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 144 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 144 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Dereference of a possibly null reference.
Assert.Equal(1, swapped[0, 1].EvalNumerical());
}

Expand All @@ -155,7 +155,7 @@
Entity[] equations = { "cos(x2 + 1)^2 + 3y", "y * (-1) + 4cos(x2 + 1)" };
var solutions = MathS.Equations(equations).Solve("x", "y");
Assert.NotNull(solutions);
Assert.Equal(8, solutions.RowCount);

Check warning on line 158 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 158 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 158 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 158 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 158 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Dereference of a possibly null reference.
}

/// <summary>
Expand All @@ -176,7 +176,7 @@
var solutions = MathS.Equations(equations).Solve(variables);

Assert.NotNull(solutions);
Assert.Equal(expected, solutions.RowCount);

Check warning on line 179 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 179 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 179 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 179 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.
AssertEverySolutionSatisfies(equations, variables, solutions);
}

Expand Down Expand Up @@ -260,7 +260,7 @@
{
var solutions = MathS.Equations(new Entity[] { "x^2 - 2", "y - x" }).Solve("x", "y");
Assert.NotNull(solutions);
for (var row = 0; row < solutions.RowCount; row++)

Check warning on line 263 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 263 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.
for (var column = 0; column < solutions.ColumnCount; column++)
Assert.DoesNotContain(
solutions[row, column].Nodes,
Expand All @@ -268,9 +268,9 @@
}

/// <summary>
/// Where the univariate's roots are decimals there is nothing to prove an identity
/// with, so the system is declined — and the declining has to be quick, because an
/// earlier version proved nothing slowly and cost more than it saved.
/// Where the univariate's roots are decimals there is nothing to prove an identity with,
/// so a candidate in decimals is checked to the precision it has, its residual a
/// negligible share of the largest term of each equation.
/// </summary>
[Fact]
public void APolynomialSystemWithDecimalRootsIsStillAnswered()
Expand All @@ -278,9 +278,37 @@
Entity[] equations = { "x3 + 9 x2 y - 10", "y3 + x y2 - 2" };
var solutions = MathS.Equations(equations).Solve("x", "y");
Assert.NotNull(solutions);
Assert.Equal(9, solutions.RowCount);

Check warning on line 281 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 281 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.
}

/// <summary>
/// A rational solution beside roots that are decimals is kept exactly. The univariate of
/// x^2 + y^2 = 2, x^3 - y = -2 is (x + 1) times a quintic with no roots in radicals; the
/// path declined every candidate for the quintic's decimals, and the elimination it fell
/// back to answered in decimals without (-1, 1). Found by a check of systems built from
/// known solutions; no issue was filed.
/// </summary>
[Fact]
public void ARationalSolutionBesideDecimalOnesIsKept()
{
var solutions = MathS.Equations("x^2 + y^2 - 2", "x^3 - y + 2").Solve("x", "y");
Assert.NotNull(solutions);
Assert.Equal(6, solutions.RowCount);

Check warning on line 296 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.

Check warning on line 296 in Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.
var exact = false;
for (var row = 0; row < solutions.RowCount; row++)
{
if (solutions[row, 0] == (Entity)(-1) && solutions[row, 1] == (Entity)1)
exact = true;
foreach (var equation in new Entity[] { "x^2 + y^2 - 2", "x^3 - y + 2" })
{
var residual = equation.Substitute("x", solutions[row, 0]).Substitute("y", solutions[row, 1]).EvalNumerical();
Assert.True(Number.Abs(residual).EDecimal.CompareTo(PeterO.Numbers.EDecimal.FromString("1e-10")) < 0,
$"row {row} leaves {residual} in {equation}");
}
}
Assert.True(exact, $"(-1, 1) is not a row of {solutions}");
}

/// <summary>
/// A free variable means infinitely many solutions, which is not something a
/// triangular basis enumerates — the ideal is not zero-dimensional, so there is no
Expand Down
Loading