Skip to content

Commit b00a06f

Browse files
committed
Merge remote-tracking branch 'origin/master' into a-product-equation-is-solved-branch-by-branch
2 parents ceaee37 + 4b0a97e commit b00a06f

4 files changed

Lines changed: 126 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ A not-equal written `<>` is read as `not (a = b)`, and printed that way.
4242

4343
## Unreleased — since 2.6.0
4444

45+
### A polynomial system is not answered with some of its solutions missing
46+
47+
**Silent: a polynomial system that lost solutions has all of them.** `EquationSystem.Solve` triangularises a
48+
polynomial system over the rationals with a Gröbner basis, and two defects made that path decline systems it should
49+
have answered. The conversion to lexicographic order recorded each new row of its echelon form without the rows that
50+
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
51+
whose leading coefficient a root makes zero was not read as zero: `x y + 3 x - 5 y - 15` at `y = -3` stayed
52+
`x * (-3) + 3 * x` and was solved for `x` as every number. A declined system went to the eliminator, which solves
53+
`x * y = 0` for `y` as `0` alone and lost solutions. Both are fixed. A system the triangularisation now takes lists
54+
its rows in the order it finds them, which can differ from the eliminator's
55+
([#2126](https://github.com/asc-community/AngouriMath/issues/2126)).
56+
57+
| Input | Was (2.6.0) | Now |
58+
|---|---|---|
59+
| `MathS.Equations("x*y", "x + y^2 - 1").Solve("x", "y")` | `[[1, 0]]` | `[[1, 0], [0, 1], [0, -1]]` |
60+
| `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]]` |
61+
4562
### A square system is not answered with rows that are no solution
4663

4764
**Silent: a row that is no solution is gone, and a system with a row that fails for some values is `null`.**

‎Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,16 @@ internal static class Fglm
203203
continue;
204204
}
205205

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

212218
for (var row = 0; row < rowCombinations.Count; row++)

‎Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,49 @@ static bool BackSubstitute(
189189
if (univariate.SolveEquation(variable).InnerSimplified is not Set.FiniteSet roots)
190190
return budget.Allow(false, "unenumerable roots");
191191

192+
var remaining = new Dictionary<Variable, int>(at);
193+
for (var i = 0; i < at; i++)
194+
remaining[variables[i]] = i;
195+
var still = variables.Take(at).ToList();
192196
foreach (var root in roots)
193197
{
194198
assignment[at] = root;
195199
var narrowed = new List<Entity>(equations.Count);
200+
var consistent = true;
196201
foreach (var equation in equations)
197202
{
198-
var substituted = equation.Substitute(variable, root).InnerSimplified;
203+
var substituted = Canonical(equation.Substitute(variable, root).InnerSimplified, remaining, still);
199204
if (substituted.Vars.Any())
200205
narrowed.Add(substituted);
206+
else if (substituted is Number.Rational { IsZero: false })
207+
consistent = false;
201208
}
209+
// A root that leaves an equation a nonzero number extends to no solution.
210+
if (!consistent)
211+
continue;
202212
if (!BackSubstitute(narrowed, variables, at - 1, assignment, found, budget))
203213
return false;
204214
}
205215
return true;
206216
}
207217

218+
/// <summary>
219+
/// <paramref name="substituted"/> as a polynomial over <c>Q</c> in the variables still to
220+
/// be assigned, where it is one, so that a term the root has made worth nothing is gone.
221+
/// </summary>
222+
/// <remarks>
223+
/// A basis that is not in shape position has elements like <c>x y + 3 x - 5 y - 15</c>,
224+
/// whose leading coefficient vanishes at a root: at <c>y = -3</c> it is <c>0</c>, and
225+
/// says nothing about <c>x</c>. One structural pass leaves it <c>x * (-3) + 3 * x</c>,
226+
/// which still mentions <c>x</c>, so it was taken as the equation to solve <c>x</c>
227+
/// from, solved to every number, and the system was declined. Where a root is a
228+
/// radical the coefficients are not rational, and it is left as it is.
229+
/// </remarks>
230+
static Entity Canonical(Entity substituted, IReadOnlyDictionary<Variable, int> remaining, IReadOnlyList<Variable> still)
231+
=> MultivariatePolynomial.TryParse(substituted, remaining) is { } polynomial
232+
? polynomial.ToEntity(still)
233+
: substituted;
234+
208235
/// <summary>
209236
/// Substitutes a candidate into the original equations and insists they come out
210237
/// exactly zero.

‎Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
using AngouriMath;
1111
using AngouriMath.Core;
1212
using AngouriMath.Core.Exceptions;
13+
using AngouriMath.Extensions;
14+
using AngouriMath.Functions;
15+
using AngouriMath.Functions.Algebra.Groebner;
1316
using Xunit;
1417
using static AngouriMath.Entity;
1518

@@ -177,6 +180,77 @@ public void SystemsWithIrrationalSolutionsAreSolved(string first, string second,
177180
AssertEverySolutionSatisfies(equations, variables, solutions);
178181
}
179182

183+
/// <summary>
184+
/// The basis of <c>x * y, x + y^2 - 1</c> in degree-reverse-lexicographic order has
185+
/// <c>1</c>, <c>y</c> and <c>x</c> under its staircase, and converting it reaches
186+
/// <c>y^2 = 1 - x</c> with the row for <c>1</c> already standing. The row kept for
187+
/// <c>y^2</c> is then <c>-x</c>, and recording it as <c>y^2</c> alone made the element
188+
/// for <c>x</c> come out as <c>x + y^2</c>, whose root <c>(0, 0)</c> is no solution.
189+
/// </summary>
190+
[Fact]
191+
public void TheLexicographicBasisKeepsWhatARowWasReducedBy()
192+
{
193+
Variable[] variables = { "x", "y" };
194+
var index = new Dictionary<Variable, int> { [variables[0]] = 0, [variables[1]] = 1 };
195+
var generators = new[] { "x * y", "x + y^2 - 1" }
196+
.Select(text => MultivariatePolynomial.TryParse(text.ToEntity(), index)!)
197+
.ToList();
198+
var budget = new GroebnerBudget();
199+
var basis = Buchberger.Compute(generators, MonomialOrder.DegreeReverseLexicographic, budget);
200+
Assert.NotNull(basis);
201+
var lexicographic = Fglm.ToLexicographic(basis!, variables.Length, budget);
202+
Assert.NotNull(lexicographic);
203+
204+
var elements = lexicographic!.Select(element => element.ToEntity(variables)).ToList();
205+
Assert.Equal(2, elements.Count);
206+
foreach (var expected in new[] { "y^3 - y", "x + y^2 - 1" })
207+
Assert.Contains(elements, element => (element - expected.ToEntity()).Simplify() == 0);
208+
}
209+
210+
/// <summary>
211+
/// The system the basis above belongs to. With the wrong element the triangularisation
212+
/// was declined, and the eliminator that answered instead solved <c>x * y = 0</c> for
213+
/// <c>y</c> as <c>0</c> alone, which kept <c>(1, 0)</c> and lost <c>(0, 1)</c> and
214+
/// <c>(0, -1)</c>.
215+
/// </summary>
216+
[Fact]
217+
public void ASystemWhoseBasisLeansOnAReducedRowIsSolvedWhole()
218+
{
219+
Entity[] equations = { "x * y", "x + y^2 - 1" };
220+
Variable[] variables = { "x", "y" };
221+
222+
var solutions = MathS.Equations(equations).Solve(variables);
223+
224+
Assert.NotNull(solutions);
225+
Assert.Equal(3, solutions!.RowCount);
226+
AssertEverySolutionSatisfies(equations, variables, solutions);
227+
foreach (var (x, y) in new[] { (1, 0), (0, 1), (0, -1) })
228+
Assert.Contains(Enumerable.Range(0, solutions.RowCount),
229+
row => solutions[row, 0] == Number.Integer.Create(x) && solutions[row, 1] == Number.Integer.Create(y));
230+
}
231+
232+
/// <summary>
233+
/// A basis that is not in shape position. Its element <c>x y + 3 x - 5 y - 15</c> is
234+
/// <c>0</c> at <c>y = -3</c> but was left <c>x * (-3) + 3 * x</c>, which still mentions
235+
/// <c>x</c>, so it was the equation <c>x</c> was solved from there. That solved to every
236+
/// number, the triangularisation was declined, and the eliminator lost <c>(5, 0)</c>.
237+
/// </summary>
238+
[Fact]
239+
public void ARootThatZeroesALeadingCoefficientUsesTheNextElement()
240+
{
241+
Entity[] equations = { "x * y + 3 * x - 5 * y - 15", "x^2 - 3 * x - 4 * y - 10" };
242+
Variable[] variables = { "x", "y" };
243+
244+
var solutions = MathS.Equations(equations).Solve(variables);
245+
246+
Assert.NotNull(solutions);
247+
Assert.Equal(3, solutions!.RowCount);
248+
AssertEverySolutionSatisfies(equations, variables, solutions);
249+
foreach (var (x, y) in new[] { (5, 0), (1, -3), (2, -3) })
250+
Assert.Contains(Enumerable.Range(0, solutions.RowCount),
251+
row => solutions[row, 0] == Number.Integer.Create(x) && solutions[row, 1] == Number.Integer.Create(y));
252+
}
253+
180254
/// <summary>
181255
/// The answers stay exact rather than being handed back as decimals — which is the
182256
/// point of triangularising rather than evaluating.

0 commit comments

Comments
 (0)