Skip to content

Commit c9f688b

Browse files
An equation the solver cannot invert is left unsolved, not answered with no roots (#1504)
x! = 6 was answered { }, a claim that it has no roots, and it has 3. The solver isolates x by inverting the function around it, and for a factorial, a binomial coefficient, mod, gcd, lcm, min, max, phi, prime, the valuation, a sum, a product, a limit, a binder and a set with x inside it, the inversion had no way to write the preimage and returned none. It now throws CannotInvertException, which the analytical solvers catch at the level it was met, answering that equation as the set of x for which it holds, the way a statement with no arm already was. Roots found beside it are kept, and an equation left whole is given back as written. A value such a function provably never takes still has no roots: the factorial is the gamma function one along, which has no zeros, so x! = 0 is { } -- which is also what an induction proof needs to read n!^2 as non-zero, and now reads for that reason. Three tests pinned the empty set, one of them a wrong answer (x! - 1 has the roots 0 and 1); each is rewritten to what the answer means. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent fd06b1a commit c9f688b

11 files changed

Lines changed: 197 additions & 40 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,26 @@ in 2.5.0.
15581558
| `forall p in PP : forall a in ZZ : a^p mod p = a mod p` | `UnhandledParseException` | `True` |
15591559
| `forall p in PP : forall a, b in ZZ : (a + b)^p = a^p + b^p (mod p)` | `UnhandledParseException` | `True` |
15601560

1561+
### An equation the solver cannot invert is left unsolved, not answered with no roots
1562+
1563+
`x! = 6` was answered `{ }`, a claim that it has no roots, and it has 3. The solver isolates `x`
1564+
by inverting the function around it, and for a factorial, a binomial coefficient, `mod`, `gcd`,
1565+
`lcm`, `min`, `max`, `phi`, `prime`, the valuation, a sum, a product, a limit, a set with `x`
1566+
inside it and a few more, the inversion had no way to write the preimage and returned none. Such
1567+
an equation is now left unsolved, as the set of `x` for which it holds, the way a statement
1568+
the solver has no arm for already was. Roots found beside it are kept. A value these functions
1569+
provably never take still has no roots: the factorial is the gamma function one along, which
1570+
has no zeros, so `x! = 0` is still `{ }`, and so is `arcsin(x) = 5`.
1571+
1572+
| Input | Was (2.5.0) | Now |
1573+
|---|---|---|
1574+
| `"x! = 6".Solve("x")` | `{ }` | `{ x : x! = 6 }` |
1575+
| `"x mod 3 = 1".Solve("x")` | `{ }` | `{ x : x mod 3 = 1 }` |
1576+
| `"gcd(x, 4) = 2".Solve("x")` | `{ }` | `{ x : gcd(x, 4) = 2 }` |
1577+
| `"max(x, 1) = 3".Solve("x")` | `{ }` | `{ x : max(x, 1) = 3 }` |
1578+
| `"phi(x) = 4".Solve("x")` | `{ }` | `{ x : phi(x) = 4 }` |
1579+
| `"binomial(x, 2) = 3".Solve("x")` | `UnhandledParseException` | `{ x : binomial(x, 2) = 3 }` |
1580+
15611581
### `binomial(n, k)` is a function
15621582

15631583
**Addition, not silent.** The binomial coefficient is a node, `Entity.Binomialf`, spelled

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
207207
[Tests/UnitTests/Algebra/SolveTest/NegationIsNotTheEmptySetTest.cs]
208208
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
209209

210+
[Tests/UnitTests/Algebra/SolveTest/AnUnwrittenInverseIsNotTheEmptySetTest.cs]
211+
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
212+
210213
[Tests/UnitTests/Common/LimitTermination.cs]
211214
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
212215

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ internal static class AnalyticalEquationSolver
8787
/// <param name="expr">Expression</param>
8888
/// <param name="x">Variable to solve over</param>
8989
internal static Set Solve(Entity expr, Variable x, bool compensateSolving = false)
90+
{
91+
// An inversion that could not write its preimage leaves this equation unsolved, as
92+
// the set of x for which it holds. At this level and no further out, so the roots
93+
// found beside it stay: (x - 1) x! = 0 is { 1 } and the x with x! = 0.
94+
try
95+
{
96+
return SolveInverting(expr, x, compensateSolving);
97+
}
98+
catch (CannotInvertException)
99+
{
100+
return new ConditionalSet(x, (compensateSolving ? expr : expr.InnerSimplified).Equalizes(0));
101+
}
102+
}
103+
104+
private static Set SolveInverting(Entity expr, Variable x, bool compensateSolving)
90105
{
91106
if (!compensateSolving) expr = expr.InnerSimplified; // don't simplify away the 0 on the right hand side of the subtraction
92107
if (expr == x)

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

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ partial record Modf
7171
{
7272
// x % a = value has one solution per period, so inverting it means introducing
7373
// an integer parameter the way the trigonometric inversions do. Until that is
74-
// written, no solutions is the honest answer -- a wrong one would be worse.
74+
// written the equation is left unsolved: no solutions would claim it has none.
7575
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
76-
=> Enumerable.Empty<Entity>();
76+
=> throw new CannotInvertException();
7777
}
7878

7979
partial record Powf
@@ -212,18 +212,21 @@ private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x
212212

213213
partial record Factorialf
214214
{
215-
// TODO: Inverse of factorial not implemented yet
215+
// The factorial is the gamma function one along, and the gamma function has no
216+
// zeros anywhere in the complex plane, so x! = 0 has no roots. Every other value
217+
// has some -- x! = 6 at 3 -- and no node here writes them.
216218
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x) =>
217-
Enumerable.Empty<Entity>();
219+
value is Integer { IsZero: true } ? Enumerable.Empty<Entity>() : throw new CannotInvertException();
218220
}
219221

220222
partial record Binomialf
221223
{
222224
// The preimage of a binomial coefficient is not a function of either argument
223225
// that can be undone: binomial(n, k) = binomial(n, n - k), and 1 is taken at
224-
// every (n, 0) and (n, n).
226+
// every (n, 0) and (n, n). It has one all the same -- binomial(x, 2) = 3 at 3 and
227+
// at -2 -- so the equation is left unsolved rather than answered with none.
225228
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x) =>
226-
Enumerable.Empty<Entity>();
229+
throw new CannotInvertException();
227230
}
228231

229232
partial record Derivativef
@@ -250,49 +253,50 @@ partial record Maximumf
250253
{
251254
// The unknown sits under a binder; see Summationf below.
252255
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x) =>
253-
Enumerable.Empty<Entity>();
256+
throw new CannotInvertException();
254257
}
255258

256259
partial record Minimumf
257260
{
258261
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x) =>
259-
Enumerable.Empty<Entity>();
262+
throw new CannotInvertException();
260263
}
261264

262265
partial record Argmaxf
263266
{
264267
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x) =>
265-
Enumerable.Empty<Entity>();
268+
throw new CannotInvertException();
266269
}
267270

268271
partial record Argminf
269272
{
270273
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x) =>
271-
Enumerable.Empty<Entity>();
274+
throw new CannotInvertException();
272275
}
273276

274277
partial record Summationf
275278
{
276279
// The unknown sits under a binder, and inverting would have to solve for it inside a
277280
// sum whose length may be symbolic. Declining is the honest answer -- and the wrong
278281
// one is on record: solving through an opaque Derivativef returns a non-solution,
279-
// https://github.com/asc-community/AngouriMath/issues/964.
282+
// https://github.com/asc-community/AngouriMath/issues/964. Declining is leaving the
283+
// equation unsolved; no roots would claim it has none.
280284
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x) =>
281-
Enumerable.Empty<Entity>();
285+
throw new CannotInvertException();
282286
}
283287

284288
partial record Productf
285289
{
286290
// Same reasoning as Summationf above.
287291
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x) =>
288-
Enumerable.Empty<Entity>();
292+
throw new CannotInvertException();
289293
}
290294

291295
partial record Limitf
292296
{
293-
// TODO: We can't just do a limit on the inverse function: https://math.stackexchange.com/q/3397326/627798
297+
// We can't just do a limit on the inverse function: https://math.stackexchange.com/q/3397326/627798
294298
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x) =>
295-
Enumerable.Empty<Entity>();
299+
throw new CannotInvertException();
296300
}
297301

298302
partial record Signumf
@@ -386,28 +390,28 @@ partial record Minf
386390
// of x. Nothing here can express that, and inventing a branch would answer
387391
// confidently and wrongly, so this declines the way Factorialf and Phif do.
388392
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
389-
=> Enumerable.Empty<Entity>();
393+
=> throw new CannotInvertException();
390394
}
391395

392396
partial record Maxf
393397
{
394398
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
395-
=> Enumerable.Empty<Entity>();
399+
=> throw new CannotInvertException();
396400
}
397401

398402
partial record Gcdf
399403
{
400404
// The preimage of a gcd is every pair whose greatest common divisor is the
401405
// value, which is not a function of x that can be undone.
402406
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
403-
=> Enumerable.Empty<Entity>();
407+
=> throw new CannotInvertException();
404408
}
405409

406410
partial record Lcmf
407411
{
408412
// As for the gcd.
409413
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
410-
=> Enumerable.Empty<Entity>();
414+
=> throw new CannotInvertException();
411415
}
412416

413417
partial record Boolean
@@ -564,7 +568,7 @@ partial record FiniteSet
564568
{
565569
// set{,,,} = value
566570
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
567-
=> this == x ? new[] { value } : Enumerable.Empty<Entity>();
571+
=> this == x ? new[] { value } : throw new CannotInvertException();
568572
}
569573

570574
partial record Interval
@@ -580,14 +584,14 @@ private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x
580584
return Left.Invert(Right, x).Select(c => c.Provided(MathS.Equality(Right, value)));
581585
else
582586
return Right.Invert(Left, x).Select(c => c.Provided(MathS.Equality(Left, value)));
583-
return Enumerable.Empty<Entity>();
587+
throw new CannotInvertException();
584588
}
585589
}
586590

587591
partial record ConditionalSet
588592
{
589593
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
590-
=> this == x ? new[] { value } : Enumerable.Empty<Entity>();
594+
=> this == x ? new[] { value } : throw new CannotInvertException();
591595
}
592596

593597
partial record SpecialSet
@@ -660,7 +664,7 @@ partial record IndexedSetOperation
660664
{
661665
// The unknown sits under a binder; see Summationf.
662666
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
663-
=> Enumerable.Empty<Entity>();
667+
=> throw new CannotInvertException();
664668
}
665669

666670
partial record Powersetf
@@ -677,15 +681,15 @@ partial record Valuationf
677681
// valuation(n, p) = k has every multiple of p^k prime to p for a solution, a set the
678682
// inverter cannot hand back.
679683
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
680-
=> Enumerable.Empty<Entity>();
684+
=> throw new CannotInvertException();
681685
}
682686

683687
partial record Primef
684688
{
685689
// prime(n) = p has the one solution n = pi(p) where p is prime and none otherwise,
686690
// and pi is not a node here; declined the way Phif is.
687691
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
688-
=> Enumerable.Empty<Entity>();
692+
=> throw new CannotInvertException();
689693
}
690694

691695
partial record Phif
@@ -694,7 +698,7 @@ partial record Phif
694698
// There is an algorithm to find exactly one solution but we can do no more.
695699
// TODO: Mess with that...
696700
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
697-
=> Enumerable.Empty<Entity>();
701+
=> throw new CannotInvertException();
698702
}
699703

700704
partial record Cardf
@@ -728,7 +732,7 @@ partial record Quantifier
728732
{
729733
// The unknown sits under a binder; see Summationf.
730734
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
731-
=> Enumerable.Empty<Entity>();
735+
=> throw new CannotInvertException();
732736
}
733737

734738
partial record Providedf
@@ -758,14 +762,14 @@ partial record Application
758762
{
759763
// TODO
760764
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
761-
=> Enumerable.Empty<Entity>();
765+
=> throw new CannotInvertException();
762766
}
763767

764768
partial record Lambda
765769
{
766770
// TODO
767771
private protected override IEnumerable<Entity> InvertNode(Entity value, Entity x)
768-
=> Enumerable.Empty<Entity>();
772+
=> throw new CannotInvertException();
769773
}
770774
}
771775
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,16 @@ private static Set Filtered(FiniteSet listed, ConditionalSet condition, Entity s
319319
/// <summary>How large a modulus is tried residue by residue for a congruence that is not linear.</summary>
320320
private const int LargestModulusTried = 4096;
321321

322+
/// <summary>
323+
/// The equation as it was written where the solver left the whole of it unsolved: the
324+
/// solver works on <c>left - right = 0</c>, and <c>{ x : x! = 6 }</c> reads better than
325+
/// <c>{ x : x! - 6 = 0 }</c>, which says the same.
326+
/// </summary>
327+
private static Set AsWritten(Set solved, Entity difference, Entity equation, Variable x)
328+
=> solved is ConditionalSet { Predicate: Equalsf(var rearranged, Integer { IsZero: true }) } && rearranged == difference.InnerSimplified
329+
? new ConditionalSet(x, equation)
330+
: solved;
331+
322332
internal static Set Solve(Entity expr, Variable x)
323333
=> expr switch
324334
{
@@ -327,7 +337,7 @@ internal static Set Solve(Entity expr, Variable x)
327337

328338
Equalsf(var left, var right) when left is not Set && right is not Set
329339
=> UnsolvedWhereIndependenceIsDenied(
330-
WithoutSpuriousRoots(AnalyticalEquationSolver.Solve(left - right, x), left - right, x),
340+
WithoutSpuriousRoots(AsWritten(AnalyticalEquationSolver.Solve(left - right, x), left - right, expr, x), left - right, x),
331341
expr, x),
332342

333343
Equalsf => Empty,

‎Sources/AngouriMath/Functions/Continuous/Solvers/InvertNode.Definition.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77

88
namespace AngouriMath
99
{
10+
/// <summary>
11+
/// Thrown by an inversion that cannot write the preimage it is asked for, so that the
12+
/// equation is answered as the set of <c>x</c> for which it holds rather than as the empty
13+
/// set. Caught by the analytical solvers, never seen by a caller.
14+
/// </summary>
15+
internal sealed class CannotInvertException : Core.Exceptions.AngouriMathBaseException
16+
{
17+
internal CannotInvertException() : base("The preimage has no written form") { }
18+
}
19+
1020
partial record Entity : ILatexizeable
1121
{
1222
/// <summary><para>This <see cref="Entity"/> MUST contain exactly ONE occurance of <paramref name="x"/>,
@@ -27,6 +37,12 @@ internal IEnumerable<Entity> Invert(Entity value, Entity x)
2737
return InvertNode(simplified, x).Where(el => el.IsFinite);
2838
}
2939
/// <summary>Use <see cref="Invert(Entity, Entity)"/> instead which auto-simplifies <paramref name="value"/></summary>
40+
/// <remarks>
41+
/// No roots means the equation has none. A node that cannot write the preimage it is
42+
/// asked for throws <see cref="CannotInvertException"/> instead, and the solver answers
43+
/// the equation as unsolved. <c>x! = 6</c> has the root 3, and no node here inverts a
44+
/// factorial, so returning no roots answered <c>{ }</c>: a claim that it has none.
45+
/// </remarks>
3046
private protected abstract IEnumerable<Entity> InvertNode(Entity value, Entity x);
3147
/// <summary>
3248
/// Returns true if <paramref name="a"/> is inside a rect with corners <paramref name="from"/>

‎Sources/AngouriMath/Functions/Continuous/Solvers/SetSolver/AnalyticalSetSolver.cs‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,18 @@ internal static Set Solve(Entity left, Entity right, Variable x)
3030
+
3131
right.DirectChildren.Count<Entity>(c => c == x) != 1)
3232
return Empty;
33-
if (left.ContainsNode(x))
34-
return left.Invert(right, x).ToSet();
35-
else
36-
return right.Invert(left, x).ToSet();
33+
// As in the equation solver, an inversion with no written form leaves it unsolved.
34+
try
35+
{
36+
if (left.ContainsNode(x))
37+
return left.Invert(right, x).ToSet();
38+
else
39+
return right.Invert(left, x).ToSet();
40+
}
41+
catch (CannotInvertException)
42+
{
43+
return new ConditionalSet(x, left.EqualTo(right));
44+
}
3745
}
3846
}
3947
}

0 commit comments

Comments
 (0)