From 61b5502cdd6e0bbe2a6cd2441ec9ff4caafd2d7f Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Tue, 29 Sep 2026 04:53:23 +0000 Subject: [PATCH] An equation between moduli is left unsolved over the complex numbers, and solved by cases over the reals An expression real for every complex x, x only ever inside a modulus, is one real condition on the two real unknowns x is made of, so a curve solves it: abs(x - 2) = abs(x - 3) holds on the line Re x = 5/2. Newton's method found points of such a curve and answered them as the set, and abs(x - 1) + abs(x - 2) = 5 missed its real root 4 for points of an ellipse; it is now left as the set of the x satisfying it, decided by structure before the numerical search. With the codomain RR, an equation in moduli of linear functions is solved by cases between the kinks, where every modulus has one sign: abs(x - 2) = abs(x - 3) is { 5/2 }, Sullivan and Mackey's Prob 1.5.16, and a piece on which the equation holds identically is a whole interval of solutions. Closes #1573. Part of #1409. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 17 +++ Sources/.editorconfig | 6 + .../AnalyticalEquationSolver.cs | 14 ++ .../Solvers/EquationSolver/ModulusSolver.cs | 125 ++++++++++++++++++ .../Algebra/SolveTest/ModulusEquationTest.cs | 55 ++++++++ 5 files changed, 217 insertions(+) create mode 100644 Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/ModulusSolver.cs create mode 100644 Sources/Tests/UnitTests/Algebra/SolveTest/ModulusEquationTest.cs diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 1877c36da..20ea5d90a 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -767,6 +767,23 @@ emptiness as a proof: `forall x in RR : x^2 in ZZ` was `True` | `"x^2 in {1, 4}".Solve("x")` | `{}` — wrong | `{ 1, -1, 2, -2 }` | | `"x^2 in ZZ".Solve("x")` | `{}` — wrong | `{ x : x ^ 2 in ZZ }`, left as written | | `forall x in RR : x^2 in ZZ` | `UnhandledParseException` (quantifiers are new since) | `False` | +### An equation between moduli is left unsolved over the complex numbers, and solved by cases over the reals + +An expression real for every complex `x`, with `x` only ever inside a modulus, is one real condition +on the two real unknowns `x` is made of, so what solves it is a curve: `abs(x - 2) = abs(x - 3)` holds +on the whole line `Re x = 5/2`. **Wrong answer fixed**: Newton's method found points of such a curve +and answered them as the solution set, and `abs(x - 1) + abs(x - 2) = 5` missed its real root `4` +for points of an ellipse. It is now left as the set of the `x` satisfying it. With the codomain set +to `RR`, an equation in moduli of linear functions is solved by cases between the kinks, where each +modulus has one sign: `abs(x - 2) = abs(x - 3)` is `{ 5/2 }` (Sullivan and Mackey's Prob 1.5.16), and +`abs(x - 1) + abs(x - 2) = 1` is `[1; 2]` ([#1573](https://github.com/asc-community/AngouriMath/issues/1573)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"abs(x - 2) = abs(x - 3)".ToEntity().Solve("x")` | `{ 2.4999… - 53.004…i, 2.4999… + 53.004…i, … }` | `{ x : abs(x - 2) = abs(x - 3) }` | +| the same, with `MathS.Settings.Codomain` set to `RR` | `{ 2.4999… - 53.004…i, 2.4999… + 53.004…i, … }` | `{ 5/2 }` | +| `"abs(x - 1) + abs(x - 2) = 5".ToEntity().Solve("x")`, codomain `RR` | `{ -1, 3.9997… - 0.0345…i, 3.9997… + 0.0345…i, … }` | `{ -1, 4 }` | + ### `...` is the pattern operator `{1, 2, ..., n}`, `{2, 4, ..., 2 n}`, `{5, 10, 15, ...}`, `{..., -1, 0}`, `1 + 2 + ... + n` and diff --git a/Sources/.editorconfig b/Sources/.editorconfig index d19f61600..72eb57efa 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -193,6 +193,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [AngouriMath/Core/Budgets/*.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n +[Tests/UnitTests/Algebra/SolveTest/ModulusEquationTest.cs] +file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n + [Tests/UnitTests/Core/Budgets/*.cs] [Tests/UnitTests/Core/ExceptionMessageNamesTheExpressionTest.cs] [Tests/UnitTests/Core/UnknownDomainIsNotABugTest.cs] @@ -240,6 +243,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [AngouriMath/Functions/Continuous/Solvers/EquationSolver/LinearSystemSolver.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n +[AngouriMath/Functions/Continuous/Solvers/EquationSolver/ModulusSolver.cs] +file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n + [Tests/UnitTests/Algebra/SolveTest/ParametricSystemTest.cs] file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed under MIT.\nDetails: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.\nWebsite: https://am.angouri.org.\n diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs index 1f1cbc662..a4282ea4b 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs @@ -124,6 +124,12 @@ internal static Set Solve(Entity expr, Variable x, bool compensateSolving = fals return new ConditionalSet(x, residual.Equalizes(0)); } + // Over the reals an equation in moduli of linear functions is solved by cases, between + // the kinks: |x - 2| = |x - 3| is x = 5/2, Sullivan and Mackey's Prob 1.5.16. + // https://github.com/asc-community/AngouriMath/issues/1573 + if (MathS.Settings.Codomain.Value == Core.Domain.Real && ModulusSolver.SolveOverTheReals(expr, x) is { } overTheReals) + return overTheReals; + // Whether a candidate root really is one. The loose tolerance that guesses the // rational must not also be what decides this: at 1e-7, x^41 + 6x + 1 accepted // -1/6, whose residual is -1.25e-32 -- small, but the difference between an @@ -367,6 +373,14 @@ bool ProductIsDefinedAt(Entity root) if (expr.Vars.Count == 1 && expr.Simplify() is Integer { IsZero: true }) return MathS.Sets.C; + // An expression real for every complex x -- x only ever inside a modulus -- is one + // real condition on the two real unknowns x is made of, so what solves it is a + // curve: |x - 2| = |x - 3| is the line Re x = 5/2. Newton's method would find points + // of it and answer them as the set. Left unsolved; over the reals it is solved by + // cases, above. https://github.com/asc-community/AngouriMath/issues/1573 + if (ModulusSolver.IsRealForEveryComplex(expr, x)) + return Unsolved(expr, x); + // A numerical search that comes back with nothing has not shown there is // nothing to find: Newton's method is started from finitely many points inside // a bounded region, so an empty result is a fact about the search rather than diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/ModulusSolver.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/ModulusSolver.cs new file mode 100644 index 000000000..b6113e2ee --- /dev/null +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/ModulusSolver.cs @@ -0,0 +1,125 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using System.Collections.Generic; +using System.Linq; +using static AngouriMath.Entity; +using static AngouriMath.Entity.Number; +using static AngouriMath.Entity.Set; + +namespace AngouriMath.Functions.Algebra.AnalyticalSolving +{ + /// + /// Equations in moduli: where one is real for every complex x, and how one in moduli of + /// linear functions is solved over the reals. + /// + /// + /// Over the complex numbers |x - 2| = |x - 3| holds on the whole line + /// Re x = 5/2: both sides are real whatever x is, so the equation is one real + /// condition on the two real unknowns x is made of, and what solves it is a curve. A + /// numerical search finds points of it and would answer them as the set. Over the reals the + /// same equation is x = 5/2, found by cases: between two neighbouring kinks every + /// modulus has one sign, and the equation there has no moduli. + /// https://github.com/asc-community/AngouriMath/issues/1573 + /// + internal static class ModulusSolver + { + /// + /// Whether is real for every complex , read off + /// its structure: only ever inside a modulus, and around the moduli + /// arithmetic with real numbers. A numerical probe could not tell this from a function + /// that is real at the points it tried. + /// + internal static bool IsRealForEveryComplex(Entity expr, Variable x) + => expr.ContainsNode(x) && RealWhereverItIs(expr, x); + + private static bool RealWhereverItIs(Entity expr, Variable x) + => expr switch + { + _ when !expr.ContainsNode(x) => expr.Evaled is Real || expr.Evaled is Complex { ImaginaryPart.IsZero: true }, + Absf => true, + Sumf(var augend, var addend) => RealWhereverItIs(augend, x) && RealWhereverItIs(addend, x), + Minusf(var minuend, var subtrahend) => RealWhereverItIs(minuend, x) && RealWhereverItIs(subtrahend, x), + Mulf(var multiplier, var multiplicand) => RealWhereverItIs(multiplier, x) && RealWhereverItIs(multiplicand, x), + Divf(var dividend, var divisor) => RealWhereverItIs(dividend, x) && RealWhereverItIs(divisor, x), + Powf(var @base, Integer) => RealWhereverItIs(@base, x), + _ => false, + }; + + /// + /// The real at which is zero, where every + /// modulus in it with inside is of a linear function with real + /// coefficients; otherwise, or where a piece is not solved. + /// + internal static Set? SolveOverTheReals(Entity expr, Variable x) + { + var moduli = new List(); + var kinks = new List(); + foreach (var node in expr.Nodes) + { + if (node is not Absf(var argument) || !argument.ContainsNode(x)) + continue; + if (!TreeAnalyzer.TryGetPolyLinear(argument, x, out var slope, out var intercept) + || intercept.ContainsNode(x) || slope.Evaled is not Real { IsZero: false } + || (-intercept / slope).InnerSimplified is not Real kink) + return null; + moduli.Add(argument); + kinks.Add(kink); + } + if (moduli.Count == 0) + return null; + var ends = kinks.Distinct().OrderBy(kink => kink.EDecimal).ToList(); + + // The pieces between neighbouring kinks, closed where they end at one, and on each the + // sign every modulus takes, read at a point inside it. + Set answer = Set.Empty; + for (var i = 0; i <= ends.Count; i++) + { + Entity left = i == 0 ? Real.NegativeInfinity : ends[i - 1]; + Entity right = i == ends.Count ? Real.PositiveInfinity : ends[i]; + Entity inside = i == 0 ? (Entity)ends[0] - 1 : i == ends.Count ? (Entity)ends[^1] + 1 : ((Entity)ends[i - 1] + ends[i]) / 2; + var signs = new Dictionary(); + foreach (var argument in moduli) + if (!signs.ContainsKey(argument)) + { + if (argument.Substitute(x, inside).Evaled is not Real value) + return null; + signs[argument] = !value.IsNegative; + } + var withoutModuli = expr.Replace(node => node is Absf(var argument) && signs.TryGetValue(argument, out var nonNegative) + ? (nonNegative ? argument : -argument) + : node).InnerSimplified; + Set piece = new Interval(left, i > 0, right, i < ends.Count); + if (!withoutModuli.ContainsNode(x)) + { + if (withoutModuli.Evaled is Complex { IsZero: true }) + answer = answer.Unite(piece); + continue; + } + var solved = AnalyticalEquationSolver.Solve(withoutModuli, x); + // An equation that holds everywhere on the piece has the whole piece for solutions: + // |x - 1| + |x - 2| = 1 is every x from 1 to 2, the solver answering such an + // identity with every number. + if (solved == MathS.Sets.C) + { + answer = answer.Unite(piece); + continue; + } + if (solved is not FiniteSet roots) + return null; + foreach (var root in roots) + if (root.Evaled is Real value && Within(value, left, right)) + answer = answer.Unite(new FiniteSet(root)); + } + return (Set)answer.InnerSimplified; + } + + private static bool Within(Real value, Entity left, Entity right) + => left.Evaled is Real low && right.Evaled is Real high + && low.EDecimal.CompareTo(value.EDecimal) <= 0 && value.EDecimal.CompareTo(high.EDecimal) <= 0; + } +} diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/ModulusEquationTest.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/ModulusEquationTest.cs new file mode 100644 index 000000000..8a8539861 --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/ModulusEquationTest.cs @@ -0,0 +1,55 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using AngouriMath; +using AngouriMath.Core; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity.Set; + +namespace AngouriMath.Tests.Algebra +{ + /// + /// An equation in moduli. Over the complex numbers |x - 2| = |x - 3| holds on the line + /// Re x = 5/2, and Newton's method answered it with three points of that line. Over the + /// reals it is x = 5/2, found by cases between the kinks. + /// #1573 + /// + [Trait("Area", "Algebra")] + public sealed class ModulusEquationTest + { + /// + /// Real for every complex x, so a curve solves it, and no list of points is its + /// solution set: left as the set of the x satisfying it. + /// + [Theory] + [InlineData("abs(x - 2) = abs(x - 3)")] + [InlineData("abs(x - 1) + abs(x - 2) = 5")] + [InlineData("abs(2 x - 1) - abs(x) = 3")] + public void OverTheComplexNumbersACurveIsNotAListOfPoints(string equation) + => Assert.IsType(equation.ToEntity().Solve("x")); + + /// + /// Sullivan and Mackey's Prob 1.5.16, and moduli between which a whole interval solves. + /// + [Theory] + [InlineData("abs(x - 2) = abs(x - 3)", "{ 5/2 }")] + [InlineData("abs(2 x - 1) = abs(2 x - 3)", "{ 1 }")] + [InlineData("abs(2 x - 2) = abs(3 x - 3)", "{ 1 }")] + [InlineData("abs(x + 1) = abs(x - 5)", "{ 2 }")] + [InlineData("abs(x - 1) + abs(x - 2) = abs(x - 3)", "{ 0, 2 }")] + [InlineData("abs(x - 1) + abs(x - 2) = 5", "{ -1, 4 }")] + [InlineData("abs(x) = 2", "{ -2, 2 }")] + [InlineData("abs(x) = -1", "{ }")] + [InlineData("abs(x - 1) + abs(x - 2) = 1", "[1; 2]")] + public void OverTheRealsTheKinksCutItIntoCases(string equation, string expected) + { + using var _ = MathS.Settings.Codomain.Set(Domain.Real); + Assert.Equal(expected.ToEntity(), equation.ToEntity().Solve("x")); + } + } +}