From c7b16a38715e4fe0e7fd3d474595ebcbc2b5f138 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Mon, 28 Sep 2026 20:00:59 +0000 Subject: [PATCH] InvertNode returns null where it threw `InvertNode`'s contract is that a node which cannot write its preimage returns null, and the solver leaves the equation unsolved. Thirteen overrides threw `NotSufficientlySupportedException` instead: the five comparisons, `Intersectionf`, `SetMinusf`, `Inf`, `Subsetf`, `Powersetf`, `Cardf`, `Dividesf` and `Congruentf`. For `card(x) = 3`, `(x /\ {1, 2}) = {1}`, `(x \ {1}) = {2}` and `powerset(x) = {{}, {1}}` the exception came out of `Solve`. They return null now, and those equations are left unsolved, as the set of x for which they hold. Closes #1544. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 15 ++++++ .../EquationSolver/InvertNode.Classes.cs | 53 ++++++++++--------- .../AnUnwrittenInverseIsNotTheEmptySetTest.cs | 14 +++++ 3 files changed, 56 insertions(+), 26 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index edd19a65c..8c2103c83 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -1667,6 +1667,21 @@ has no zeros, so `x! = 0` is still `{ }`, and so is `arcsin(x) = 5`. | `"phi(x) = 4".Solve("x")` | `{ }` | `{ x : phi(x) = 4 }` | | `"binomial(x, 2) = 3".Solve("x")` | `UnhandledParseException` | `{ x : binomial(x, 2) = 3 }` | +### An equation in a set, a cardinality or a power set of the unknown is left unsolved, and threw + +`Solve` threw `NotSufficientlySupportedException` for an equation whose unknown sits under a set +operation or a cardinality, where the inverter's contract is to decline and leave the equation +unsolved, as the equations in the entry on inverses no node writes are. A power set of the unknown +was answered, and wrongly: the answer held `x` +([#1544](https://github.com/asc-community/AngouriMath/issues/1544)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"card(x) = 3".ToEntity().Solve("x")` | throws `NotSufficientlySupportedException` | `{ x : #x = 3 }` | +| `"(x /\\ {1, 2}) = {1}".ToEntity().Solve("x")` | throws `NotSufficientlySupportedException` | `{ x : x /\ { 1, 2 } = { 1 } }` | +| `"(x \\ {1}) = {2}".ToEntity().Solve("x")` | throws `NotSufficientlySupportedException` | `{ x : x \ { 1 } = { 2 } }` | +| `"powerset(x) = {{}, {1}}".ToEntity().Solve("x")` | `{ { { }, { 1 / x } } }` | `{ x : powerset(x) = { { }, { 1 } } }` | + ### `erf`, `erfc` and `erfi` are functions **Addition, and two silent misreadings fixed.** The error function `erf(z) = 2/sqrt(pi) int_0^z diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/InvertNode.Classes.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/InvertNode.Classes.cs index d3d221eef..47f332252 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/InvertNode.Classes.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/InvertNode.Classes.cs @@ -598,37 +598,42 @@ partial record Impliesf partial record Equalsf { + // A statement equal to a value has a set for its preimage, which the inverter cannot + // write, so it declines, and the solver leaves the equation unsolved. private protected override IEnumerable? InvertNode(Entity value, Entity x) - => throw new NotSufficientlySupportedException( - "Inverting this node would need a set-valued answer, which the inverter cannot give"); + => null; } partial record Greaterf { + // A statement equal to a value has a set for its preimage, which the inverter cannot + // write, so it declines, and the solver leaves the equation unsolved. private protected override IEnumerable? InvertNode(Entity value, Entity x) - => throw new NotSufficientlySupportedException( - "Inverting this node would need a set-valued answer, which the inverter cannot give"); + => null; } partial record GreaterOrEqualf { + // A statement equal to a value has a set for its preimage, which the inverter cannot + // write, so it declines, and the solver leaves the equation unsolved. private protected override IEnumerable? InvertNode(Entity value, Entity x) - => throw new NotSufficientlySupportedException( - "Inverting this node would need a set-valued answer, which the inverter cannot give"); + => null; } partial record Lessf { + // A statement equal to a value has a set for its preimage, which the inverter cannot + // write, so it declines, and the solver leaves the equation unsolved. private protected override IEnumerable? InvertNode(Entity value, Entity x) - => throw new NotSufficientlySupportedException( - "Inverting this node would need a set-valued answer, which the inverter cannot give"); + => null; } partial record LessOrEqualf { + // A statement equal to a value has a set for its preimage, which the inverter cannot + // write, so it declines, and the solver leaves the equation unsolved. private protected override IEnumerable? InvertNode(Entity value, Entity x) - => throw new NotSufficientlySupportedException( - "Inverting this node would need a set-valued answer, which the inverter cannot give"); + => null; } partial record Set @@ -701,17 +706,19 @@ partial record Unionf partial record Intersectionf { // f(x) /\ A = value + // Deciding the membership would need a piecewise condition, which is not written + // here; declined. private protected override IEnumerable? InvertNode(Entity value, Entity x) - => throw new NotSufficientlySupportedException( - "Deciding this membership would need a piecewise condition, which is not supported here"); + => null; } partial record SetMinusf { // f(x) \ A = value + // Deciding the membership would need a piecewise condition, which is not written + // here; declined. private protected override IEnumerable? InvertNode(Entity value, Entity x) - => throw new NotSufficientlySupportedException( - "Deciding this membership would need a piecewise condition, which is not supported here"); + => null; } partial record Inf @@ -719,16 +726,14 @@ partial record Inf // TODO: CSet is needed here => InvertNode to return a Set, not an IEnumerable // f(x) in A = value private protected override IEnumerable? InvertNode(Entity value, Entity x) - => throw new NotSufficientlySupportedException( - "Inverting this node would need a set-valued answer, which the inverter cannot give"); + => null; } partial record Subsetf { // A subset B = value asks for a set of sets, which the inverter cannot return. private protected override IEnumerable? InvertNode(Entity value, Entity x) - => throw new NotSufficientlySupportedException( - "Inverting this node would need a set-valued answer, which the inverter cannot give"); + => null; } partial record IndexedSetOperation @@ -742,8 +747,7 @@ partial record Powersetf { // powerset(f(x)) = value has no inverse the inverter can write. private protected override IEnumerable? InvertNode(Entity value, Entity x) - => throw new NotSufficientlySupportedException( - "Inverting this node would need a set-valued answer, which the inverter cannot give"); + => null; } } @@ -777,8 +781,7 @@ partial record Cardf // card(S) = n has every set of n elements for its solutions, which is not something // the inverter can hand back. private protected override IEnumerable? InvertNode(Entity value, Entity x) - => throw new NotSufficientlySupportedException( - "Inverting this node would need a set-valued answer, which the inverter cannot give"); + => null; } partial record Dividesf @@ -786,8 +789,7 @@ partial record Dividesf // (a divides x) = value has the multiples of a for its solutions -- a set, which the // inverter cannot return, the same as membership. private protected override IEnumerable? InvertNode(Entity value, Entity x) - => throw new NotSufficientlySupportedException( - "Inverting this node would need a set-valued answer, which the inverter cannot give"); + => null; } partial record Congruentf @@ -795,8 +797,7 @@ partial record Congruentf // (x = b (mod n)) = value has a residue class for its solutions -- a set, which the // inverter cannot return, the same as divisibility and membership. private protected override IEnumerable? InvertNode(Entity value, Entity x) - => throw new NotSufficientlySupportedException( - "Inverting this node would need a set-valued answer, which the inverter cannot give"); + => null; } partial record Quantifier diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/AnUnwrittenInverseIsNotTheEmptySetTest.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/AnUnwrittenInverseIsNotTheEmptySetTest.cs index a380e0de7..1cf765add 100644 --- a/Sources/Tests/UnitTests/Algebra/SolveTest/AnUnwrittenInverseIsNotTheEmptySetTest.cs +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/AnUnwrittenInverseIsNotTheEmptySetTest.cs @@ -46,6 +46,20 @@ public void ARootIsInTheAnswerAndANonRootIsNot(string equation, string root, str Assert.Equal(Entity.Boolean.False, IsIn(other, solved)); } + /// + /// A set, a cardinality or a power set of the unknown has a set of sets for its preimage, + /// which the inverter cannot write, and it threw NotSufficientlySupportedException + /// out of Solve where its contract is to decline and leave the equation unsolved. + /// https://github.com/asc-community/AngouriMath/issues/1544 + /// + [Theory] + [InlineData("card(x) = 3", "{1, 2, 3}", "{1}")] + [InlineData("(x /\\ {1, 2}) = {1}", "{1}", "{2}")] + [InlineData("(x \\ {1}) = {2}", "{2}", "{1, 2, 3}")] + [InlineData("powerset(x) = {{}, {1}}", "{1}", "{2}")] + public void ASetOfSetsIsLeftUnsolved(string equation, string root, string other) + => ARootIsInTheAnswerAndANonRootIsNot(equation, root, other); + /// /// The factorial is the gamma function one along, which has no zeros, so a factorial /// equal to zero does have none, and the roots found beside one are the whole answer.