Skip to content

Commit 3cfecdd

Browse files
An equation in a remainder is solved to its whole family (#1630)
f mod a = r has one solution per period. It is now inverted the way the trigonometric functions are, with a whole parameter: f = r + |a| n where r is a value the floored remainder takes -- [0, a) for a positive a, (a, 0] for a negative one -- and no solution where it is not. (x mod 4) + 1 = 3 is { 2 + 4 n_1 }, where 2.5.0 answered { }; (2x + 1) mod 4 = 3 is every odd number, where 2.5.0 listed six of them. A divisor or value that is not a number is left unsolved. Closes #1629. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 371b38e commit 3cfecdd

5 files changed

Lines changed: 156 additions & 21 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,27 @@ and `13` is not, every `n` from `24` is `4 a + 9 b` and `23` is not
845845
| `exists a in ZZ* : exists b in ZZ* : 13 = 3 a + 8 b` | `UnhandledParseException` (left as written when quantifiers arrived) | `False` |
846846
| `forall n in ZZ : n >= 14 implies (exists a in ZZ* : exists b in ZZ* : n = 3 a + 8 b)` | `UnhandledParseException` (left as written when quantifiers arrived) | `True` |
847847

848+
### An equation in a remainder is solved to its whole family
849+
850+
`f mod a = r` has one solution per period, and the solver had no way to write that. On 2.5.0
851+
`(x mod 4) + 1 = 3` answered `{ }` -- no solution, for an equation with infinitely many. Where
852+
the numeric fallback reached it, it answered with the few solutions it found in its window:
853+
`(2x + 1) mod 4 = 3` was `{ -5, -3, -1, 1, 3, 5 }`, and 7 is a solution as well. The remainder
854+
is now inverted the way the trigonometric functions are, with a whole parameter: `f = r + a n`
855+
where `r` is a value the floored remainder takes -- `[0, a)` for a positive `a`, `(a, 0]` for a
856+
negative one -- and no solution where it is not. A divisor or value that is not a number is left
857+
unsolved.
858+
[#1629](https://github.com/asc-community/AngouriMath/issues/1629), Algebrite's issue 87 from
859+
[#180](https://github.com/asc-community/AngouriMath/issues/180). Both columns measured on a build,
860+
`v2.5.0` against this change.
861+
862+
| `"….".ToEntity().Solve("x")` of | Was (2.5.0) | Is |
863+
|---|---|---|
864+
| `(x mod 4) + 1 = 3` | `{ }` | `{ 2 + 4 * n_1 }` |
865+
| `(2x + 1) mod 4 = 3` | `{ -5, -3, -1, 1, 3, 5 }`, six of infinitely many | `{ (3 + 4 * n_1 - 1) / 2 }`, every odd number |
866+
| `x mod (-4) = -1` | `{ }` | `{ -1 + 4 * n_1 }` |
867+
| `x mod 4 = 5` | `{ }` | `{ }` (unchanged: modulo 4 the remainder never takes 5) |
868+
848869
### The modulus of a whole number is a whole number
849870

850871
With the facts in scope making `s` a whole number, `|s| in ZZ` and `|s| in ZZ*` are `True`, and
@@ -2411,18 +2432,19 @@ now has no value, as the interpreter's has none.
24112432
### An equation the solver cannot invert is left unsolved, not answered with no roots
24122433

24132434
`x! = 6` was answered `{ }`, a claim that it has no roots, and it has 3. The solver isolates `x`
2414-
by inverting the function around it, and for a factorial, a binomial coefficient, `mod`, `gcd`,
2415-
`lcm`, `min`, `max`, `phi`, `prime`, the valuation, a sum, a product, a limit, a set with `x`
2416-
inside it and a few more, the inversion had no way to write the preimage and returned none. Such
2417-
an equation is now left unsolved, as the set of `x` for which it holds, the way a statement
2418-
the solver has no arm for already was. Roots found beside it are kept. A value these functions
2419-
provably never take still has no roots: the factorial is the gamma function one along, which
2420-
has no zeros, so `x! = 0` is still `{ }`, and so is `arcsin(x) = 5`.
2435+
by inverting the function around it, and for a factorial, a binomial coefficient, `mod` by a
2436+
divisor that is not a number, `gcd`, `lcm`, `min`, `max`, `phi`, `prime`, the valuation, a sum,
2437+
a product, a limit, a set with `x` inside it and a few more, the inversion had no way to write
2438+
the preimage and returned none. Such an equation is now left unsolved, as the set of `x` for
2439+
which it holds, the way a statement the solver has no arm for already was. Roots found beside it
2440+
are kept. A value these functions provably never take still has no roots: the factorial is the
2441+
gamma function one along, which has no zeros, so `x! = 0` is still `{ }`, and so is
2442+
`arcsin(x) = 5`.
24212443

24222444
| Input | Was (2.5.0) | Now |
24232445
|---|---|---|
24242446
| `"x! = 6".Solve("x")` | `{ }` | `{ x : x! = 6 }` |
2425-
| `"x mod 3 = 1".Solve("x")` | `{ }` | `{ x : x mod 3 = 1 }` |
2447+
| `"x mod a = 1".Solve("x")` | `{ }` | `{ x : x mod a = 1 }` |
24262448
| `"gcd(x, 4) = 2".Solve("x")` | `{ }` | `{ x : gcd(x, 4) = 2 }` |
24272449
| `"max(x, 1) = 3".Solve("x")` | `{ }` | `{ x : max(x, 1) = 3 }` |
24282450
| `"phi(x) = 4".Solve("x")` | `{ }` | `{ x : phi(x) = 4 }` |

‎Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/InvertNode.Classes.cs‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,31 @@ partial record Divf
6969

7070
partial record Modf
7171
{
72-
// x % a = value has one solution per period, so inverting it means introducing
73-
// an integer parameter the way the trigonometric inversions do. Until that is
74-
// written the equation is left unsolved: no solutions would claim it has none.
72+
// x mod a = value has one solution per period, and is solved the way the
73+
// trigonometric inversions are, with a whole parameter n. The remainder is the
74+
// floored one, taking the sign of the divisor -- -1 mod 4 is 3, 5 mod (-4) is -3 and
75+
// 5/2 mod 2 is 1/2 -- so it takes each value in [0, a) once per period for a positive
76+
// a, and each in (a, 0] for a negative one. So x = value + |a| n for every whole n
77+
// where value is in that range -- the same family as value + a n, since n runs over
78+
// every whole number -- and there is no x where it is not: (x mod 4) + 1 = 3 is
79+
// x = 2 + 4 n, the congruence x = 2 (mod 4) written out. A divisor with x in it, or a
80+
// divisor or value that is not a number, leaves the equation unsolved, which is not
81+
// the same as claiming it has no solution.
82+
// https://github.com/asc-community/AngouriMath/issues/1629
7583
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
76-
=> null;
84+
{
85+
if (Divisor.ContainsNode(x)
86+
|| Divisor.Evaled is not Real { IsFinite: true, IsZero: false } period
87+
|| value.Evaled is not Real { IsFinite: true } remainder)
88+
return null;
89+
var inRange = period.IsNegative
90+
? (remainder.IsNegative || remainder.IsZero) && remainder > period
91+
: !remainder.IsNegative && remainder < period;
92+
if (!inRange)
93+
return Enumerable.Empty<Entity>();
94+
var step = period.IsNegative ? (-Divisor).InnerSimplified : Divisor;
95+
return Dividend.Invert(value + step * Variable.CreateUnique(this + value, "n"), x);
96+
}
7797
}
7898

7999
partial record Powf

‎Sources/Tests/UnitTests/Algebra/SolveTest/AnUnwrittenInverseIsNotTheEmptySetTest.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ namespace AngouriMath.Tests.Algebra.SolveTest
1515
{
1616
/// <summary>
1717
/// An equation in a function whose preimage the inverter cannot write -- a factorial, a
18-
/// binomial coefficient, a residue, a gcd -- was answered with the empty set, a claim that
18+
/// binomial coefficient, a gcd -- was answered with the empty set, a claim that
1919
/// it has no roots. <c>x! = 6</c> has the root 3. Such an equation is now left unsolved, as
2020
/// the set of <c>x</c> for which it holds.
2121
/// </summary>
2222
/// <remarks>
2323
/// These assert what the answers mean -- which values are in the set -- rather than the
2424
/// shape they are written in, so that solving one of them properly later does not fail them.
25+
/// A solution written as a family in a whole parameter, as a remainder's is, is checked at
26+
/// values of the parameter instead, since membership does not range over it.
2527
/// </remarks>
2628
[Trait("Area", "Algebra")]
2729
public sealed class AnUnwrittenInverseIsNotTheEmptySetTest
@@ -33,7 +35,6 @@ public sealed class AnUnwrittenInverseIsNotTheEmptySetTest
3335
[Theory]
3436
[InlineData("x! = 6", "3", "2")]
3537
[InlineData("binomial(x, 2) = 3", "-2", "2")]
36-
[InlineData("x mod 3 = 1", "4", "3")]
3738
[InlineData("gcd(x, 4) = 2", "6", "8")]
3839
[InlineData("max(x, 1) = 3", "3", "1")]
3940
[InlineData("prime(x) = 7", "4", "3")]
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.Linq;
9+
using AngouriMath;
10+
using AngouriMath.Extensions;
11+
using Xunit;
12+
using static AngouriMath.Entity;
13+
14+
namespace AngouriMath.Tests.Algebra
15+
{
16+
/// <summary>
17+
/// An equation in a remainder, <c>f mod a = r</c>, solved as the congruence it is:
18+
/// <c>f = r + a n</c> for every whole <c>n</c>, where <c>r</c> is a value the floored remainder
19+
/// takes, and no solution where it is not. Algebrite's issue 87, from #180.
20+
/// https://github.com/asc-community/AngouriMath/issues/1629
21+
/// </summary>
22+
[Trait("Area", "Algebra")]
23+
public sealed class RemainderEquationTest
24+
{
25+
/// <summary>Every member of the family satisfies the equation, at several values of its parameter.</summary>
26+
[Theory]
27+
[InlineData("(x mod 4) + 1 = 3")]
28+
[InlineData("x mod 4 = 0")]
29+
[InlineData("x mod (-4) = -1")]
30+
[InlineData("(2x + 1) mod 4 = 3")]
31+
[InlineData("x mod 2 = 1/2")]
32+
public void EverySolutionSatisfiesIt(string equation)
33+
{
34+
var solutions = Assert.IsType<Set.FiniteSet>(equation.ToEntity().Solve("x"));
35+
Assert.NotEmpty(solutions.Elements);
36+
foreach (var solution in solutions.Elements)
37+
{
38+
var parameter = Assert.Single(solution.Vars);
39+
for (var n = -2; n <= 2; n++)
40+
Assert.Equal(Boolean.True, equation.ToEntity().Substitute("x", solution.Substitute(parameter, n)).Evaled);
41+
}
42+
}
43+
44+
/// <summary>
45+
/// And the family is all of them: <c>x mod 4 = 2</c> holds exactly when <c>x - 2</c> is a
46+
/// multiple of 4, so at <c>n</c> from -2 to 2 the family is -6, -2, 2, 6 and 10.
47+
/// </summary>
48+
[Fact]
49+
public void TheFamilyIsEverySolution()
50+
{
51+
var solution = Assert.Single(Assert.IsType<Set.FiniteSet>("(x mod 4) + 1 = 3".ToEntity().Solve("x")).Elements);
52+
var parameter = Assert.Single(solution.Vars);
53+
Assert.Equal(new Entity[] { -6, -2, 2, 6, 10 },
54+
Enumerable.Range(-2, 5).Select(n => solution.Substitute(parameter, n).Evaled).ToArray());
55+
}
56+
57+
/// <summary>
58+
/// A value solves it exactly when the family reaches it at a whole parameter: for
59+
/// <c>x mod 3 = 1</c>, 4 is reached at <c>n = 1</c>, and 3 only at <c>n = 2/3</c>.
60+
/// </summary>
61+
[Theory]
62+
[InlineData("4", true)]
63+
[InlineData("3", false)]
64+
public void AValueSolvesItWhenTheFamilyReachesItAtAWholeParameter(string value, bool solves)
65+
{
66+
var solution = Assert.Single(Assert.IsType<Set.FiniteSet>("x mod 3 = 1".ToEntity().Solve("x")).Elements);
67+
var parameter = Assert.Single(solution.Vars);
68+
var at = Assert.Single(Assert.IsType<Set.FiniteSet>(solution.Equalizes(value.ToEntity()).Solve(parameter)).Elements);
69+
Assert.Equal(solves, at.Evaled is Number.Integer);
70+
}
71+
72+
/// <summary>
73+
/// A value the remainder never takes has no solution: modulo 4 it lies in [0, 4), modulo
74+
/// -4 in (-4, 0].
75+
/// </summary>
76+
[Theory]
77+
[InlineData("x mod 4 = 5")]
78+
[InlineData("x mod 4 = -1")]
79+
[InlineData("x mod (-4) = 1")]
80+
public void AValueTheRemainderNeverTakesHasNoSolution(string equation)
81+
=> Assert.Empty(Assert.IsType<Set.FiniteSet>(equation.ToEntity().Solve("x")).Elements);
82+
83+
/// <summary>A divisor or a value that is not a number leaves it unsolved, which is not the same as no solution.</summary>
84+
[Theory]
85+
[InlineData("x mod a = 1")]
86+
[InlineData("x mod 4 = y")]
87+
public void WhatItCannotReadIsLeftUnsolved(string equation)
88+
=> Assert.IsType<Set.ConditionalSet>(equation.ToEntity().Solve("x"));
89+
}
90+
}

‎Sources/Tests/UnitTests/Convenience/ModulusTest.cs‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,16 @@ public void TheLinqCompilerKeepsTheFractionalPart() =>
195195
Assert.Equal(1.5, "x mod y".ToEntity().Compile<double, double, double>("x", "y")(7.5, 2), 9);
196196

197197
/// <summary>
198-
/// x mod a = value has one solution per period, so answering it means introducing an
199-
/// integer parameter the way the trigonometric inversions do. Until that is written the
200-
/// equation is left unsolved, as the set of x for which it holds. This asserted the
201-
/// empty set, which claims there is no such x, and 4 is one. Pinned so that whoever
202-
/// writes the inversion sees this change.
198+
/// x mod a = value has one solution per period, and is answered the way the
199+
/// trigonometric inversions are, with a whole parameter: x mod 3 = 1 is x = 1 + 3 n,
200+
/// which is 4 at n = 1. https://github.com/asc-community/AngouriMath/issues/1629
203201
/// </summary>
204202
[Fact]
205-
public void SolvingIsNotClaimed() =>
206-
Assert.IsType<Entity.Set.ConditionalSet>(("x mod 3".ToEntity() - 1).SolveEquation("x"));
203+
public void SolvingGivesEverySolution()
204+
{
205+
var solution = Assert.Single(Assert.IsType<Entity.Set.FiniteSet>(("x mod 3".ToEntity() - 1).SolveEquation("x")).Elements);
206+
var parameter = Assert.Single(solution.Vars);
207+
Assert.Equal((Entity)4, solution.Substitute(parameter, 1).Evaled);
208+
}
207209
}
208210
}

0 commit comments

Comments
 (0)