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 @@ -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
Expand Down
6 changes: 6 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Equations in moduli: where one is real for every complex <c>x</c>, and how one in moduli of
/// linear functions is solved over the reals.
/// </summary>
/// <remarks>
/// Over the complex numbers <c>|x - 2| = |x - 3|</c> holds on the whole line
/// <c>Re x = 5/2</c>: both sides are real whatever <c>x</c> is, so the equation is one real
/// condition on the two real unknowns <c>x</c> 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 <c>x = 5/2</c>, 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
/// </remarks>
internal static class ModulusSolver
{
/// <summary>
/// Whether <paramref name="expr"/> is real for every complex <paramref name="x"/>, read off
/// its structure: <paramref name="x"/> 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.
/// </summary>
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,
};

/// <summary>
/// The real <paramref name="x"/> at which <paramref name="expr"/> is zero, where every
/// modulus in it with <paramref name="x"/> inside is of a linear function with real
/// coefficients; <see langword="null"/> otherwise, or where a piece is not solved.
/// </summary>
internal static Set? SolveOverTheReals(Entity expr, Variable x)
{
var moduli = new List<Entity>();
var kinks = new List<Real>();
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<Entity, bool>();
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;
}
}
55 changes: 55 additions & 0 deletions Sources/Tests/UnitTests/Algebra/SolveTest/ModulusEquationTest.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// An equation in moduli. Over the complex numbers <c>|x - 2| = |x - 3|</c> holds on the line
/// <c>Re x = 5/2</c>, and Newton's method answered it with three points of that line. Over the
/// reals it is <c>x = 5/2</c>, found by cases between the kinks.
/// <a href="https://github.com/asc-community/AngouriMath/issues/1573">#1573</a>
/// </summary>
[Trait("Area", "Algebra")]
public sealed class ModulusEquationTest
{
/// <summary>
/// Real for every complex <c>x</c>, so a curve solves it, and no list of points is its
/// solution set: left as the set of the <c>x</c> satisfying it.
/// </summary>
[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<ConditionalSet>(equation.ToEntity().Solve("x"));

/// <summary>
/// Sullivan and Mackey's Prob 1.5.16, and moduli between which a whole interval solves.
/// </summary>
[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"));
}
}
}
Loading