Skip to content

Commit 84f83fd

Browse files
Solve reads a membership, and no longer answers the empty set for a statement it cannot read (#1440)
"x^2 in (0; 1)".Solve("x") was {} -- and so was every f(x) in S with anything but a bare x on the left, and every statement the solver had no arm for: the arm at the end of the dispatch answered the empty set, a claim that there is no such x. A membership is solved now: the members of a listed set each as an equation, an interval as its bounds, each strict or not as the end is; and a statement that is not read is the set of x with the property, left as written, which is not a claim. A quantifier that put the negated body to the solver read that emptiness as a proof: forall x in RR : x^2 in ZZ was True, and is False, by the witness 1/2. On the way, the subset decision reads membership of a union, an intersection and a difference as the connectives they are, so that the solver's answer to x^2 in (0; 1), ((-oo; 0) \/ (0; +oo)) /\ (-1; 1), is compared with (-1; 0) \/ (0; 1) as a set. The reference's pre-images (Sullivan and Mackey, Ex 7.3.10) are the rows; the book prints (-1, 1) for the pre-image of (0, 1) under x^2, which is wrong at 0. #1409 Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 6d3c3d9 commit 84f83fd

5 files changed

Lines changed: 139 additions & 2 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,23 @@ with the multiplier gathered from the whole product in the exponent
656656
| `"e^(2*acoth(2*x))*(3 - 12*x^2)^2".Integrate("x")` | `integral(…)` — left unevaluated | `(144 * x ^ 5 / 5 + 144 * x ^ 4 / 4 + (-36) * x ^ 2 / 2 + (-9) * x provided not 2 * x + -1 = 0) + C` |
657657
| `"e^(1/3*acoth(x))*x^2".Integrate("x")` | `integral(…)` — left unevaluated | an antiderivative in `((x + 1)/(x - 1))^(1/6)` |
658658

659+
### `Solve` no longer answers the empty set for a statement it cannot read
660+
661+
**Wrong answer fixed.** `"x^2 in (0; 1)".Solve("x")` was `{}` — and so was every `f(x) in S` with
662+
anything but a bare `x` on the left, and every statement the solver had no arm for: the arm at the
663+
end of the dispatch answered the empty set, which claims there is no such `x`. A membership is
664+
solved now — the members of a listed set each as an equation, an interval as its bounds, each
665+
strict or not as the end is — and a statement that is not read is the set of `x` with the
666+
property, left as written. A quantifier that put the negated body to the solver read that
667+
emptiness as a proof: `forall x in RR : x^2 in ZZ` was `True`
668+
([#1409](https://github.com/asc-community/AngouriMath/issues/1409), the reference's pre-images).
669+
670+
| Input | Was (2.5.0) | Now |
671+
|---|---|---|
672+
| `"x^2 in (0; 1)".Solve("x")` | `{}` — wrong | `((-oo; 0) \/ (0; +oo)) /\ (-1; 1)` |
673+
| `"x^2 in {1, 4}".Solve("x")` | `{}` — wrong | `{ 1, -1, 2, -2 }` |
674+
| `"x^2 in ZZ".Solve("x")` | `{}` — wrong | `{ x : x ^ 2 in ZZ }`, left as written |
675+
| `forall x in RR : x^2 in ZZ` | `UnhandledParseException` (quantifiers are new since) | `False` |
659676
### `...` is the pattern operator
660677

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

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
291291
[AngouriMath/Functions/NumberTheory/ResidueClasses.cs]
292292
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
293293

294+
[Tests/UnitTests/Algebra/MembershipSolvingTest.cs]
295+
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
296+
294297
[AngouriMath/Core/PatternOperator.cs]
295298
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
296299

‎Sources/AngouriMath/Core/Entity/Omni/Sets/SetOperators.Subset.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ internal static partial class SetOperators
223223
var over = sub is ConditionalSet { DeclaredMembership: (var declared, _) } ? declared as Set : sub;
224224
switch (super)
225225
{
226+
// The operators are the connectives: x in A \/ B is x in A or x in B.
227+
case Unionf(Set a, Set b):
228+
return MembershipAsComparison(x, a, sub) is { } inA && MembershipAsComparison(x, b, sub) is { } inB ? inA | inB : null;
229+
case Intersectionf(Set a, Set b):
230+
return MembershipAsComparison(x, a, sub) is { } inBoth && MembershipAsComparison(x, b, sub) is { } andIn ? inBoth & andIn : null;
231+
case SetMinusf(Set a, Set b):
232+
return MembershipAsComparison(x, a, sub) is { } inLeft && MembershipAsComparison(x, b, sub) is { } notIn ? inLeft & !notIn : null;
226233
case ConditionalSet { DeclaredMembership: (var declaredOfSuper, var rest), Var: Variable y } builder
227234
when declaredOfSuper is Set declaredSet:
228235
// x in { y in S : p } is x in S and p(x); the first conjunct is read the

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using PeterO.Numbers;
1313
using AngouriMath.Functions.Continuous.Solvers.SetSolver;
1414
using static AngouriMath.Entity;
15+
using static AngouriMath.Entity.Number;
1516
using static AngouriMath.Entity.Set;
1617

1718
namespace AngouriMath.Functions.Algebra.AnalyticalSolving
@@ -319,6 +320,11 @@ internal static Set Solve(Entity expr, Variable x)
319320
Variable when expr == x => new FiniteSet(true),
320321

321322
Inf(var var, Set set) when var == x => set,
323+
// f(x) in S: the members of a listed S each as an equation, an interval as its
324+
// two bounds, and anything else left as the set of x with the property, since
325+
// a solver that answered "no x" here was answering wrongly -- x^2 in (0; 1) was
326+
// the empty set. https://github.com/asc-community/AngouriMath/issues/1409
327+
Inf(var member, Set set) when member.ContainsNode(x) => Membership(member, set, x) ?? new ConditionalSet(x, expr),
322328

323329
// a x + b = c (mod n): one residue class, or none, by the gcd; a congruence of
324330
// higher degree by the residues that satisfy it, where the modulus is small
@@ -328,8 +334,43 @@ internal static Set Solve(Entity expr, Variable x)
328334
Providedf(var e, var predicate) => Solve(e, x).Filter(predicate, x),
329335
Piecewise p => EquationSolver.SolvePiecewise(p, x, Solve),
330336

331-
// TODO: Although piecewise needed?
332-
_ => Set.Empty
337+
// A statement the solver has no arm for is the set of x with the property, left
338+
// as written -- not the empty set, which claims there is no such x. A statement
339+
// that does not mention x is one of everything or of nothing, and the set
340+
// builder over it evaluates to that.
341+
_ => new ConditionalSet(x, expr)
333342
};
343+
344+
/// <summary>
345+
/// The <c>x</c> with <c>f(x)</c> in a set: for a listed set, the union of the equations
346+
/// <c>f(x) = s</c>; for an interval, the conjunction of its bounds, each strict or not as
347+
/// the end is; <see langword="null"/> for a set that is neither.
348+
/// </summary>
349+
private static Set? Membership(Entity member, Set set, Variable x)
350+
{
351+
switch (set)
352+
{
353+
case FiniteSet listed:
354+
Set? union = null;
355+
foreach (var element in listed)
356+
{
357+
var solutions = Solve(member.Equalizes(element), x);
358+
union = union is null ? solutions : MathS.Union(union, solutions);
359+
}
360+
return union ?? Set.Empty;
361+
case Interval interval:
362+
Entity? condition = null;
363+
if (interval.Left.Evaled != Real.NegativeInfinity)
364+
condition = interval.LeftClosed ? member >= interval.Left : member > interval.Left;
365+
if (interval.Right.Evaled != Real.PositiveInfinity)
366+
{
367+
var upper = interval.RightClosed ? member <= interval.Right : member < interval.Right;
368+
condition = condition is null ? upper : condition & upper;
369+
}
370+
return condition is null ? MathS.Sets.R : Solve(condition, x);
371+
default:
372+
return null;
373+
}
374+
}
334375
}
335376
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.Extensions;
10+
using Xunit;
11+
using static AngouriMath.Entity;
12+
13+
namespace AngouriMath.Tests.Algebra
14+
{
15+
/// <summary>
16+
/// <c>Solve</c> of <c>f(x) in S</c>: the members of a listed <c>S</c> each as an equation,
17+
/// an interval as its bounds. It answered the <b>empty set</b> for every one of these, and
18+
/// for every statement it had no arm for -- a claim that no <c>x</c> exists, made about
19+
/// <c>x^2 in (0; 1)</c>. A statement the solver cannot read is the set of <c>x</c> with the
20+
/// property now, left as written. The reference's pre-images (Sullivan and Mackey,
21+
/// Ex 7.3.10, §7.3.5 Try 1) are this.
22+
/// <see href="https://github.com/asc-community/AngouriMath/issues/1409"/>
23+
/// </summary>
24+
[Trait("Area", "Algebra")]
25+
public sealed class MembershipSolvingTest
26+
{
27+
private static Set Solved(string statement) => statement.ToEntity().Solve("x");
28+
29+
/// <summary>Ex 7.3.10: the pre-images of <c>x^2</c>; the book prints <c>(-1, 1)</c> for the last, which is wrong at <c>0</c>.</summary>
30+
[Theory]
31+
[InlineData("x^2 in {1}", "{-1, 1}")]
32+
[InlineData("x^2 in {1, 4}", "{-2, -1, 1, 2}")]
33+
[InlineData("x^2 in (-oo; 0)", "{}")]
34+
[InlineData("x^2 in [0; +oo)", "RR")]
35+
[InlineData("x^2 in (0; 1)", "(-1; 0) \\/ (0; 1)")]
36+
[InlineData("x + 1 in [2; 3]", "[1; 2]")]
37+
[InlineData("2 x in (0; 4]", "(0; 2]")]
38+
public void APreImageIsSolvedThroughTheMembersOrTheBounds(string statement, string expected)
39+
{
40+
// Compared at points rather than as printed forms: the solver writes a union of
41+
// intervals in its own order, and the same set two ways is the same set.
42+
var solved = Solved(statement);
43+
var wanted = (Set)expected.ToEntity().Evaled;
44+
foreach (var at in new[] { -3.0, -2.0, -1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 3.0 })
45+
{
46+
var point = (Entity)at;
47+
Assert.True(solved.TryContains(point, out var got), $"{solved} does not decide {at}");
48+
Assert.True(wanted.TryContains(point, out var want), $"{wanted} does not decide {at}");
49+
Assert.True(got == want, $"{statement} solved as {solved}: {at} is {(got ? "in" : "out")}, and should be {(want ? "in" : "out")}");
50+
}
51+
}
52+
53+
/// <summary>A statement with no arm is the set of <c>x</c> with the property, not the empty set.</summary>
54+
[Fact]
55+
public void WhatIsNotReadIsLeftAsWritten()
56+
{
57+
Assert.Equal("{ x : x^2 in ZZ }".ToEntity(), Solved("x^2 in ZZ"));
58+
Assert.Equal("{ x : x^2 in QQ }".ToEntity(), Solved("x^2 in QQ"));
59+
// And a quantifier that asked the solver no longer reads that emptiness as a proof:
60+
// forall x in RR : x^2 in ZZ was True.
61+
Assert.Equal(Boolean.False, "forall x in RR : x^2 in ZZ".ToEntity().Evaled);
62+
}
63+
64+
/// <summary>The trigonometric equation's family is the pre-image of <c>{0}</c> under the sine.</summary>
65+
[Fact]
66+
public void AMemberEquationKeepsItsFamily()
67+
=> Assert.Contains("n_1", Solved("sin(x) in {0}").Stringize());
68+
}
69+
}

0 commit comments

Comments
 (0)