diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 74fe3f736..fa097778d 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -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`.** diff --git a/Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs b/Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs index f3c99b250..369b98324 100644 --- a/Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs +++ b/Sources/AngouriMath/Functions/Algebra/Groebner/Fglm.cs @@ -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++) diff --git a/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs b/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs index 44044cf00..a836c2d45 100644 --- a/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs +++ b/Sources/AngouriMath/Functions/Algebra/Groebner/GroebnerSystemSolver.cs @@ -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(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(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; } + /// + /// as a polynomial over Q in the variables still to + /// be assigned, where it is one, so that a term the root has made worth nothing is gone. + /// + /// + /// A basis that is not in shape position has elements like x y + 3 x - 5 y - 15, + /// whose leading coefficient vanishes at a root: at y = -3 it is 0, and + /// says nothing about x. One structural pass leaves it x * (-3) + 3 * x, + /// which still mentions x, so it was taken as the equation to solve x + /// 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. + /// + static Entity Canonical(Entity substituted, IReadOnlyDictionary remaining, IReadOnlyList still) + => MultivariatePolynomial.TryParse(substituted, remaining) is { } polynomial + ? polynomial.ToEntity(still) + : substituted; + /// /// Substitutes a candidate into the original equations and insists they come out /// exactly zero. diff --git a/Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs b/Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs index d04792acc..f241d6f95 100644 --- a/Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs +++ b/Sources/Tests/UnitTests/Algebra/GroebnerSystemTest.cs @@ -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; @@ -177,6 +180,77 @@ public void SystemsWithIrrationalSolutionsAreSolved(string first, string second, AssertEverySolutionSatisfies(equations, variables, solutions); } + /// + /// The basis of x * y, x + y^2 - 1 in degree-reverse-lexicographic order has + /// 1, y and x under its staircase, and converting it reaches + /// y^2 = 1 - x with the row for 1 already standing. The row kept for + /// y^2 is then -x, and recording it as y^2 alone made the element + /// for x come out as x + y^2, whose root (0, 0) is no solution. + /// + [Fact] + public void TheLexicographicBasisKeepsWhatARowWasReducedBy() + { + Variable[] variables = { "x", "y" }; + var index = new Dictionary { [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); + } + + /// + /// The system the basis above belongs to. With the wrong element the triangularisation + /// was declined, and the eliminator that answered instead solved x * y = 0 for + /// y as 0 alone, which kept (1, 0) and lost (0, 1) and + /// (0, -1). + /// + [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)); + } + + /// + /// A basis that is not in shape position. Its element x y + 3 x - 5 y - 15 is + /// 0 at y = -3 but was left x * (-3) + 3 * x, which still mentions + /// x, so it was the equation x was solved from there. That solved to every + /// number, the triangularisation was declined, and the eliminator lost (5, 0). + /// + [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)); + } + /// /// The answers stay exact rather than being handed back as decimals — which is the /// point of triangularising rather than evaluating.