Skip to content

Commit 850b00a

Browse files
An equation between moduli is left unsolved over the complex numbers, and solved by cases over the reals (#1578)
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. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 12d7371 commit 850b00a

5 files changed

Lines changed: 217 additions & 0 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,23 @@ emptiness as a proof: `forall x in RR : x^2 in ZZ` was `True`
802802
| `"x^2 in {1, 4}".Solve("x")` | `{}` — wrong | `{ 1, -1, 2, -2 }` |
803803
| `"x^2 in ZZ".Solve("x")` | `{}` — wrong | `{ x : x ^ 2 in ZZ }`, left as written |
804804
| `forall x in RR : x^2 in ZZ` | `UnhandledParseException` (quantifiers are new since) | `False` |
805+
### An equation between moduli is left unsolved over the complex numbers, and solved by cases over the reals
806+
807+
An expression real for every complex `x`, with `x` only ever inside a modulus, is one real condition
808+
on the two real unknowns `x` is made of, so what solves it is a curve: `abs(x - 2) = abs(x - 3)` holds
809+
on the whole line `Re x = 5/2`. **Wrong answer fixed**: Newton's method found points of such a curve
810+
and answered them as the solution set, and `abs(x - 1) + abs(x - 2) = 5` missed its real root `4`
811+
for points of an ellipse. It is now left as the set of the `x` satisfying it. With the codomain set
812+
to `RR`, an equation in moduli of linear functions is solved by cases between the kinks, where each
813+
modulus has one sign: `abs(x - 2) = abs(x - 3)` is `{ 5/2 }` (Sullivan and Mackey's Prob 1.5.16), and
814+
`abs(x - 1) + abs(x - 2) = 1` is `[1; 2]` ([#1573](https://github.com/asc-community/AngouriMath/issues/1573)).
815+
816+
| Input | Was (2.5.0) | Now |
817+
|---|---|---|
818+
| `"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) }` |
819+
| the same, with `MathS.Settings.Codomain` set to `RR` | `{ 2.4999… - 53.004…i, 2.4999… + 53.004…i, … }` | `{ 5/2 }` |
820+
| `"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 }` |
821+
805822
### `...` is the pattern operator
806823

807824
`{1, 2, ..., n}`, `{2, 4, ..., 2 n}`, `{5, 10, 15, ...}`, `{..., -1, 0}`, `1 + 2 + ... + n` and

‎Sources/.editorconfig‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
193193
[AngouriMath/Core/Budgets/*.cs]
194194
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
195195

196+
[Tests/UnitTests/Algebra/SolveTest/ModulusEquationTest.cs]
197+
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
198+
196199
[Tests/UnitTests/Core/Budgets/*.cs]
197200
[Tests/UnitTests/Core/ExceptionMessageNamesTheExpressionTest.cs]
198201
[Tests/UnitTests/Core/UnknownDomainIsNotABugTest.cs]
@@ -240,6 +243,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
240243
[AngouriMath/Functions/Continuous/Solvers/EquationSolver/LinearSystemSolver.cs]
241244
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
242245

246+
[AngouriMath/Functions/Continuous/Solvers/EquationSolver/ModulusSolver.cs]
247+
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
248+
243249
[Tests/UnitTests/Algebra/SolveTest/ParametricSystemTest.cs]
244250
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
245251

‎Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/AnalyticalEquationSolver.cs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ internal static Set Solve(Entity expr, Variable x, bool compensateSolving = fals
124124
return new ConditionalSet(x, residual.Equalizes(0));
125125
}
126126

127+
// Over the reals an equation in moduli of linear functions is solved by cases, between
128+
// the kinks: |x - 2| = |x - 3| is x = 5/2, Sullivan and Mackey's Prob 1.5.16.
129+
// https://github.com/asc-community/AngouriMath/issues/1573
130+
if (MathS.Settings.Codomain.Value == Core.Domain.Real && ModulusSolver.SolveOverTheReals(expr, x) is { } overTheReals)
131+
return overTheReals;
132+
127133
// Whether a candidate root really is one. The loose tolerance that guesses the
128134
// rational must not also be what decides this: at 1e-7, x^41 + 6x + 1 accepted
129135
// -1/6, whose residual is -1.25e-32 -- small, but the difference between an
@@ -367,6 +373,14 @@ bool ProductIsDefinedAt(Entity root)
367373
if (expr.Vars.Count == 1 && expr.Simplify() is Integer { IsZero: true })
368374
return MathS.Sets.C;
369375

376+
// An expression real for every complex x -- x only ever inside a modulus -- is one
377+
// real condition on the two real unknowns x is made of, so what solves it is a
378+
// curve: |x - 2| = |x - 3| is the line Re x = 5/2. Newton's method would find points
379+
// of it and answer them as the set. Left unsolved; over the reals it is solved by
380+
// cases, above. https://github.com/asc-community/AngouriMath/issues/1573
381+
if (ModulusSolver.IsRealForEveryComplex(expr, x))
382+
return Unsolved(expr, x);
383+
370384
// A numerical search that comes back with nothing has not shown there is
371385
// nothing to find: Newton's method is started from finitely many points inside
372386
// a bounded region, so an empty result is a fact about the search rather than
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using static AngouriMath.Entity;
11+
using static AngouriMath.Entity.Number;
12+
using static AngouriMath.Entity.Set;
13+
14+
namespace AngouriMath.Functions.Algebra.AnalyticalSolving
15+
{
16+
/// <summary>
17+
/// Equations in moduli: where one is real for every complex <c>x</c>, and how one in moduli of
18+
/// linear functions is solved over the reals.
19+
/// </summary>
20+
/// <remarks>
21+
/// Over the complex numbers <c>|x - 2| = |x - 3|</c> holds on the whole line
22+
/// <c>Re x = 5/2</c>: both sides are real whatever <c>x</c> is, so the equation is one real
23+
/// condition on the two real unknowns <c>x</c> is made of, and what solves it is a curve. A
24+
/// numerical search finds points of it and would answer them as the set. Over the reals the
25+
/// same equation is <c>x = 5/2</c>, found by cases: between two neighbouring kinks every
26+
/// modulus has one sign, and the equation there has no moduli.
27+
/// https://github.com/asc-community/AngouriMath/issues/1573
28+
/// </remarks>
29+
internal static class ModulusSolver
30+
{
31+
/// <summary>
32+
/// Whether <paramref name="expr"/> is real for every complex <paramref name="x"/>, read off
33+
/// its structure: <paramref name="x"/> only ever inside a modulus, and around the moduli
34+
/// arithmetic with real numbers. A numerical probe could not tell this from a function
35+
/// that is real at the points it tried.
36+
/// </summary>
37+
internal static bool IsRealForEveryComplex(Entity expr, Variable x)
38+
=> expr.ContainsNode(x) && RealWhereverItIs(expr, x);
39+
40+
private static bool RealWhereverItIs(Entity expr, Variable x)
41+
=> expr switch
42+
{
43+
_ when !expr.ContainsNode(x) => expr.Evaled is Real || expr.Evaled is Complex { ImaginaryPart.IsZero: true },
44+
Absf => true,
45+
Sumf(var augend, var addend) => RealWhereverItIs(augend, x) && RealWhereverItIs(addend, x),
46+
Minusf(var minuend, var subtrahend) => RealWhereverItIs(minuend, x) && RealWhereverItIs(subtrahend, x),
47+
Mulf(var multiplier, var multiplicand) => RealWhereverItIs(multiplier, x) && RealWhereverItIs(multiplicand, x),
48+
Divf(var dividend, var divisor) => RealWhereverItIs(dividend, x) && RealWhereverItIs(divisor, x),
49+
Powf(var @base, Integer) => RealWhereverItIs(@base, x),
50+
_ => false,
51+
};
52+
53+
/// <summary>
54+
/// The real <paramref name="x"/> at which <paramref name="expr"/> is zero, where every
55+
/// modulus in it with <paramref name="x"/> inside is of a linear function with real
56+
/// coefficients; <see langword="null"/> otherwise, or where a piece is not solved.
57+
/// </summary>
58+
internal static Set? SolveOverTheReals(Entity expr, Variable x)
59+
{
60+
var moduli = new List<Entity>();
61+
var kinks = new List<Real>();
62+
foreach (var node in expr.Nodes)
63+
{
64+
if (node is not Absf(var argument) || !argument.ContainsNode(x))
65+
continue;
66+
if (!TreeAnalyzer.TryGetPolyLinear(argument, x, out var slope, out var intercept)
67+
|| intercept.ContainsNode(x) || slope.Evaled is not Real { IsZero: false }
68+
|| (-intercept / slope).InnerSimplified is not Real kink)
69+
return null;
70+
moduli.Add(argument);
71+
kinks.Add(kink);
72+
}
73+
if (moduli.Count == 0)
74+
return null;
75+
var ends = kinks.Distinct().OrderBy(kink => kink.EDecimal).ToList();
76+
77+
// The pieces between neighbouring kinks, closed where they end at one, and on each the
78+
// sign every modulus takes, read at a point inside it.
79+
Set answer = Set.Empty;
80+
for (var i = 0; i <= ends.Count; i++)
81+
{
82+
Entity left = i == 0 ? Real.NegativeInfinity : ends[i - 1];
83+
Entity right = i == ends.Count ? Real.PositiveInfinity : ends[i];
84+
Entity inside = i == 0 ? (Entity)ends[0] - 1 : i == ends.Count ? (Entity)ends[^1] + 1 : ((Entity)ends[i - 1] + ends[i]) / 2;
85+
var signs = new Dictionary<Entity, bool>();
86+
foreach (var argument in moduli)
87+
if (!signs.ContainsKey(argument))
88+
{
89+
if (argument.Substitute(x, inside).Evaled is not Real value)
90+
return null;
91+
signs[argument] = !value.IsNegative;
92+
}
93+
var withoutModuli = expr.Replace(node => node is Absf(var argument) && signs.TryGetValue(argument, out var nonNegative)
94+
? (nonNegative ? argument : -argument)
95+
: node).InnerSimplified;
96+
Set piece = new Interval(left, i > 0, right, i < ends.Count);
97+
if (!withoutModuli.ContainsNode(x))
98+
{
99+
if (withoutModuli.Evaled is Complex { IsZero: true })
100+
answer = answer.Unite(piece);
101+
continue;
102+
}
103+
var solved = AnalyticalEquationSolver.Solve(withoutModuli, x);
104+
// An equation that holds everywhere on the piece has the whole piece for solutions:
105+
// |x - 1| + |x - 2| = 1 is every x from 1 to 2, the solver answering such an
106+
// identity with every number.
107+
if (solved == MathS.Sets.C)
108+
{
109+
answer = answer.Unite(piece);
110+
continue;
111+
}
112+
if (solved is not FiniteSet roots)
113+
return null;
114+
foreach (var root in roots)
115+
if (root.Evaled is Real value && Within(value, left, right))
116+
answer = answer.Unite(new FiniteSet(root));
117+
}
118+
return (Set)answer.InnerSimplified;
119+
}
120+
121+
private static bool Within(Real value, Entity left, Entity right)
122+
=> left.Evaled is Real low && right.Evaled is Real high
123+
&& low.EDecimal.CompareTo(value.EDecimal) <= 0 && value.EDecimal.CompareTo(high.EDecimal) <= 0;
124+
}
125+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using AngouriMath;
9+
using AngouriMath.Core;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
using static AngouriMath.Entity.Set;
13+
14+
namespace AngouriMath.Tests.Algebra
15+
{
16+
/// <summary>
17+
/// An equation in moduli. Over the complex numbers <c>|x - 2| = |x - 3|</c> holds on the line
18+
/// <c>Re x = 5/2</c>, and Newton's method answered it with three points of that line. Over the
19+
/// reals it is <c>x = 5/2</c>, found by cases between the kinks.
20+
/// <a href="https://github.com/asc-community/AngouriMath/issues/1573">#1573</a>
21+
/// </summary>
22+
[Trait("Area", "Algebra")]
23+
public sealed class ModulusEquationTest
24+
{
25+
/// <summary>
26+
/// Real for every complex <c>x</c>, so a curve solves it, and no list of points is its
27+
/// solution set: left as the set of the <c>x</c> satisfying it.
28+
/// </summary>
29+
[Theory]
30+
[InlineData("abs(x - 2) = abs(x - 3)")]
31+
[InlineData("abs(x - 1) + abs(x - 2) = 5")]
32+
[InlineData("abs(2 x - 1) - abs(x) = 3")]
33+
public void OverTheComplexNumbersACurveIsNotAListOfPoints(string equation)
34+
=> Assert.IsType<ConditionalSet>(equation.ToEntity().Solve("x"));
35+
36+
/// <summary>
37+
/// Sullivan and Mackey's Prob 1.5.16, and moduli between which a whole interval solves.
38+
/// </summary>
39+
[Theory]
40+
[InlineData("abs(x - 2) = abs(x - 3)", "{ 5/2 }")]
41+
[InlineData("abs(2 x - 1) = abs(2 x - 3)", "{ 1 }")]
42+
[InlineData("abs(2 x - 2) = abs(3 x - 3)", "{ 1 }")]
43+
[InlineData("abs(x + 1) = abs(x - 5)", "{ 2 }")]
44+
[InlineData("abs(x - 1) + abs(x - 2) = abs(x - 3)", "{ 0, 2 }")]
45+
[InlineData("abs(x - 1) + abs(x - 2) = 5", "{ -1, 4 }")]
46+
[InlineData("abs(x) = 2", "{ -2, 2 }")]
47+
[InlineData("abs(x) = -1", "{ }")]
48+
[InlineData("abs(x - 1) + abs(x - 2) = 1", "[1; 2]")]
49+
public void OverTheRealsTheKinksCutItIntoCases(string equation, string expected)
50+
{
51+
using var _ = MathS.Settings.Codomain.Set(Domain.Real);
52+
Assert.Equal(expected.ToEntity(), equation.ToEntity().Solve("x"));
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)