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
17 changes: 17 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,23 @@ A not-equal written `<>` is read as `not (a = b)`, and printed that way.

## Unreleased — since 2.6.0

### A polynomial system is not answered with some of its solutions missing

**Silent: a polynomial system that lost solutions has all of them.** `EquationSystem.Solve` triangularises a
polynomial system over the rationals with a Gröbner basis, and two defects made that path decline systems it should
have answered. The conversion to lexicographic order recorded each new row of its echelon form without the rows that
had reduced it, so `x * y, x + y^2 - 1` was triangularised with `x + y^2` in place of `x + y^2 - 1`. And an element
whose leading coefficient a root makes zero was not read as zero: `x y + 3 x - 5 y - 15` at `y = -3` stayed
`x * (-3) + 3 * x` and was solved for `x` as every number. A declined system went to the eliminator, which solves
`x * y = 0` for `y` as `0` alone and lost solutions. Both are fixed. A system the triangularisation now takes lists
its rows in the order it finds them, which can differ from the eliminator's
([#2126](https://github.com/asc-community/AngouriMath/issues/2126)).

| Input | Was (2.6.0) | Now |
|---|---|---|
| `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 square system is not answered with rows that are no solution

**Silent: a row that is no solution is gone, and a system with a row that fails for some values is `null`.**
Expand Down
8 changes: 7 additions & 1 deletion Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,16 @@ internal static class Fglm
continue;
}

// The row stored is this monomial's normal form less the rows that reduced it,
// so the combination it stands for is the monomial less theirs. Recording the
// monomial alone gave every later element that leans on this row the wrong
// coefficients: x * y, x + y^2 - 1 came back as x + y^2, whose root (0, 0)
// is no solution, and the system fell back to an eliminator that lost two of
// its three solutions.
staircase.Add(monomial);
var unit = new ERational[staircase.Count];
for (var i = 0; i < unit.Length; i++)
unit[i] = ERational.Zero;
unit[i] = i < staircase.Count - 1 && i < combination.Length ? combination[i].Negate() : ERational.Zero;
unit[staircase.Count - 1] = ERational.One;

for (var row = 0; row < rowCombinations.Count; row++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,49 @@ static bool BackSubstitute(
if (univariate.SolveEquation(variable).InnerSimplified is not Set.FiniteSet roots)
return budget.Allow(false, "unenumerable roots");

var remaining = new Dictionary<Variable, int>(at);
for (var i = 0; i < at; i++)
remaining[variables[i]] = i;
var still = variables.Take(at).ToList();
foreach (var root in roots)
{
assignment[at] = root;
var narrowed = new List<Entity>(equations.Count);
var consistent = true;
foreach (var equation in equations)
{
var substituted = equation.Substitute(variable, root).InnerSimplified;
var substituted = Canonical(equation.Substitute(variable, root).InnerSimplified, remaining, still);
if (substituted.Vars.Any())
narrowed.Add(substituted);
else if (substituted is Number.Rational { IsZero: false })
consistent = false;
}
// A root that leaves an equation a nonzero number extends to no solution.
if (!consistent)
continue;
if (!BackSubstitute(narrowed, variables, at - 1, assignment, found, budget))
return false;
}
return true;
}

/// <summary>
/// <paramref name="substituted"/> as a polynomial over <c>Q</c> in the variables still to
/// be assigned, where it is one, so that a term the root has made worth nothing is gone.
/// </summary>
/// <remarks>
/// A basis that is not in shape position has elements like <c>x y + 3 x - 5 y - 15</c>,
/// whose leading coefficient vanishes at a root: at <c>y = -3</c> it is <c>0</c>, and
/// says nothing about <c>x</c>. One structural pass leaves it <c>x * (-3) + 3 * x</c>,
/// which still mentions <c>x</c>, so it was taken as the equation to solve <c>x</c>
/// from, solved to every number, and the system was declined. Where a root is a
/// radical the coefficients are not rational, and it is left as it is.
/// </remarks>
static Entity Canonical(Entity substituted, IReadOnlyDictionary<Variable, int> remaining, IReadOnlyList<Variable> still)
=> MultivariatePolynomial.TryParse(substituted, remaining) is { } polynomial
? polynomial.ToEntity(still)
: substituted;

/// <summary>
/// Substitutes a candidate into the original equations and insists they come out
/// exactly zero.
Expand Down
74 changes: 74 additions & 0 deletions Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
using AngouriMath;
using AngouriMath.Core;
using AngouriMath.Core.Exceptions;
using AngouriMath.Extensions;
using AngouriMath.Functions;
using AngouriMath.Functions.Algebra.Groebner;
using Xunit;
using static AngouriMath.Entity;

Expand Down Expand Up @@ -76,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 (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 @@ -94,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 (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 @@ -118,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 (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 @@ -132,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 (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 (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.

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 @@ -152,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 (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.

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 @@ -173,10 +176,81 @@
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 (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 (macos-latest)

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

/// <summary>
/// The basis of <c>x * y, x + y^2 - 1</c> in degree-reverse-lexicographic order has
/// <c>1</c>, <c>y</c> and <c>x</c> under its staircase, and converting it reaches
/// <c>y^2 = 1 - x</c> with the row for <c>1</c> already standing. The row kept for
/// <c>y^2</c> is then <c>-x</c>, and recording it as <c>y^2</c> alone made the element
/// for <c>x</c> come out as <c>x + y^2</c>, whose root <c>(0, 0)</c> is no solution.
/// </summary>
[Fact]
public void TheLexicographicBasisKeepsWhatARowWasReducedBy()
{
Variable[] variables = { "x", "y" };
var index = new Dictionary<Variable, int> { [variables[0]] = 0, [variables[1]] = 1 };
var generators = new[] { "x * y", "x + y^2 - 1" }
.Select(text => MultivariatePolynomial.TryParse(text.ToEntity(), index)!)
.ToList();
var budget = new GroebnerBudget();
var basis = Buchberger.Compute(generators, MonomialOrder.DegreeReverseLexicographic, budget);
Assert.NotNull(basis);
var lexicographic = Fglm.ToLexicographic(basis!, variables.Length, budget);
Assert.NotNull(lexicographic);

var elements = lexicographic!.Select(element => element.ToEntity(variables)).ToList();
Assert.Equal(2, elements.Count);
foreach (var expected in new[] { "y^3 - y", "x + y^2 - 1" })
Assert.Contains(elements, element => (element - expected.ToEntity()).Simplify() == 0);
}

/// <summary>
/// The system the basis above belongs to. With the wrong element the triangularisation
/// was declined, and the eliminator that answered instead solved <c>x * y = 0</c> for
/// <c>y</c> as <c>0</c> alone, which kept <c>(1, 0)</c> and lost <c>(0, 1)</c> and
/// <c>(0, -1)</c>.
/// </summary>
[Fact]
public void ASystemWhoseBasisLeansOnAReducedRowIsSolvedWhole()
{
Entity[] equations = { "x * y", "x + y^2 - 1" };
Variable[] variables = { "x", "y" };

var solutions = MathS.Equations(equations).Solve(variables);

Assert.NotNull(solutions);
Assert.Equal(3, solutions!.RowCount);
AssertEverySolutionSatisfies(equations, variables, solutions);
foreach (var (x, y) in new[] { (1, 0), (0, 1), (0, -1) })
Assert.Contains(Enumerable.Range(0, solutions.RowCount),
row => solutions[row, 0] == Number.Integer.Create(x) && solutions[row, 1] == Number.Integer.Create(y));
}

/// <summary>
/// A basis that is not in shape position. Its element <c>x y + 3 x - 5 y - 15</c> is
/// <c>0</c> at <c>y = -3</c> but was left <c>x * (-3) + 3 * x</c>, which still mentions
/// <c>x</c>, so it was the equation <c>x</c> was solved from there. That solved to every
/// number, the triangularisation was declined, and the eliminator lost <c>(5, 0)</c>.
/// </summary>
[Fact]
public void ARootThatZeroesALeadingCoefficientUsesTheNextElement()
{
Entity[] equations = { "x * y + 3 * x - 5 * y - 15", "x^2 - 3 * x - 4 * y - 10" };
Variable[] variables = { "x", "y" };

var solutions = MathS.Equations(equations).Solve(variables);

Assert.NotNull(solutions);
Assert.Equal(3, solutions!.RowCount);
AssertEverySolutionSatisfies(equations, variables, solutions);
foreach (var (x, y) in new[] { (5, 0), (1, -3), (2, -3) })
Assert.Contains(Enumerable.Range(0, solutions.RowCount),
row => solutions[row, 0] == Number.Integer.Create(x) && solutions[row, 1] == Number.Integer.Create(y));
}

/// <summary>
/// The answers stay exact rather than being handed back as decimals — which is the
/// point of triangularising rather than evaluating.
Expand All @@ -186,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.
for (var column = 0; column < solutions.ColumnCount; column++)
Assert.DoesNotContain(
solutions[row, column].Nodes,
Expand All @@ -204,7 +278,7 @@
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.
}

/// <summary>
Expand Down Expand Up @@ -249,7 +323,7 @@
}
var solutions = MathS.Equations(equations).Solve(variables);
Assert.NotNull(solutions);
Assert.Equal(1, solutions.RowCount);

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

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Dereference of a possibly null reference.
}
}
}
Loading