Skip to content

Commit e9ad9f2

Browse files
InvertNode returns null where it threw (#1560)
`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 <noreply@anthropic.com>
1 parent 5cffb55 commit e9ad9f2

3 files changed

Lines changed: 56 additions & 26 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,21 @@ has no zeros, so `x! = 0` is still `{ }`, and so is `arcsin(x) = 5`.
17771777
| `"phi(x) = 4".Solve("x")` | `{ }` | `{ x : phi(x) = 4 }` |
17781778
| `"binomial(x, 2) = 3".Solve("x")` | `UnhandledParseException` | `{ x : binomial(x, 2) = 3 }` |
17791779

1780+
### An equation in a set, a cardinality or a power set of the unknown is left unsolved, and threw
1781+
1782+
`Solve` threw `NotSufficientlySupportedException` for an equation whose unknown sits under a set
1783+
operation or a cardinality, where the inverter's contract is to decline and leave the equation
1784+
unsolved, as the equations in the entry on inverses no node writes are. A power set of the unknown
1785+
was answered, and wrongly: the answer held `x`
1786+
([#1544](https://github.com/asc-community/AngouriMath/issues/1544)).
1787+
1788+
| Input | Was (2.5.0) | Now |
1789+
|---|---|---|
1790+
| `"card(x) = 3".ToEntity().Solve("x")` | throws `NotSufficientlySupportedException` | `{ x : #x = 3 }` |
1791+
| `"(x /\\ {1, 2}) = {1}".ToEntity().Solve("x")` | throws `NotSufficientlySupportedException` | `{ x : x /\ { 1, 2 } = { 1 } }` |
1792+
| `"(x \\ {1}) = {2}".ToEntity().Solve("x")` | throws `NotSufficientlySupportedException` | `{ x : x \ { 1 } = { 2 } }` |
1793+
| `"powerset(x) = {{}, {1}}".ToEntity().Solve("x")` | `{ { { }, { 1 / x } } }` | `{ x : powerset(x) = { { }, { 1 } } }` |
1794+
17801795
### `erf`, `erfc` and `erfi` are functions
17811796

17821797
**Addition, and two silent misreadings fixed.** The error function `erf(z) = 2/sqrt(pi) int_0^z

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

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -598,37 +598,42 @@ partial record Impliesf
598598

599599
partial record Equalsf
600600
{
601+
// A statement equal to a value has a set for its preimage, which the inverter cannot
602+
// write, so it declines, and the solver leaves the equation unsolved.
601603
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
602-
=> throw new NotSufficientlySupportedException(
603-
"Inverting this node would need a set-valued answer, which the inverter cannot give");
604+
=> null;
604605
}
605606

606607
partial record Greaterf
607608
{
609+
// A statement equal to a value has a set for its preimage, which the inverter cannot
610+
// write, so it declines, and the solver leaves the equation unsolved.
608611
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
609-
=> throw new NotSufficientlySupportedException(
610-
"Inverting this node would need a set-valued answer, which the inverter cannot give");
612+
=> null;
611613
}
612614

613615
partial record GreaterOrEqualf
614616
{
617+
// A statement equal to a value has a set for its preimage, which the inverter cannot
618+
// write, so it declines, and the solver leaves the equation unsolved.
615619
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
616-
=> throw new NotSufficientlySupportedException(
617-
"Inverting this node would need a set-valued answer, which the inverter cannot give");
620+
=> null;
618621
}
619622

620623
partial record Lessf
621624
{
625+
// A statement equal to a value has a set for its preimage, which the inverter cannot
626+
// write, so it declines, and the solver leaves the equation unsolved.
622627
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
623-
=> throw new NotSufficientlySupportedException(
624-
"Inverting this node would need a set-valued answer, which the inverter cannot give");
628+
=> null;
625629
}
626630

627631
partial record LessOrEqualf
628632
{
633+
// A statement equal to a value has a set for its preimage, which the inverter cannot
634+
// write, so it declines, and the solver leaves the equation unsolved.
629635
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
630-
=> throw new NotSufficientlySupportedException(
631-
"Inverting this node would need a set-valued answer, which the inverter cannot give");
636+
=> null;
632637
}
633638

634639
partial record Set
@@ -701,34 +706,34 @@ partial record Unionf
701706
partial record Intersectionf
702707
{
703708
// f(x) /\ A = value
709+
// Deciding the membership would need a piecewise condition, which is not written
710+
// here; declined.
704711
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
705-
=> throw new NotSufficientlySupportedException(
706-
"Deciding this membership would need a piecewise condition, which is not supported here");
712+
=> null;
707713
}
708714

709715
partial record SetMinusf
710716
{
711717
// f(x) \ A = value
718+
// Deciding the membership would need a piecewise condition, which is not written
719+
// here; declined.
712720
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
713-
=> throw new NotSufficientlySupportedException(
714-
"Deciding this membership would need a piecewise condition, which is not supported here");
721+
=> null;
715722
}
716723

717724
partial record Inf
718725
{
719726
// TODO: CSet is needed here => InvertNode to return a Set, not an IEnumerable
720727
// f(x) in A = value
721728
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
722-
=> throw new NotSufficientlySupportedException(
723-
"Inverting this node would need a set-valued answer, which the inverter cannot give");
729+
=> null;
724730
}
725731

726732
partial record Subsetf
727733
{
728734
// A subset B = value asks for a set of sets, which the inverter cannot return.
729735
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
730-
=> throw new NotSufficientlySupportedException(
731-
"Inverting this node would need a set-valued answer, which the inverter cannot give");
736+
=> null;
732737
}
733738

734739
partial record IndexedSetOperation
@@ -742,8 +747,7 @@ partial record Powersetf
742747
{
743748
// powerset(f(x)) = value has no inverse the inverter can write.
744749
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
745-
=> throw new NotSufficientlySupportedException(
746-
"Inverting this node would need a set-valued answer, which the inverter cannot give");
750+
=> null;
747751
}
748752
}
749753

@@ -777,26 +781,23 @@ partial record Cardf
777781
// card(S) = n has every set of n elements for its solutions, which is not something
778782
// the inverter can hand back.
779783
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
780-
=> throw new NotSufficientlySupportedException(
781-
"Inverting this node would need a set-valued answer, which the inverter cannot give");
784+
=> null;
782785
}
783786

784787
partial record Dividesf
785788
{
786789
// (a divides x) = value has the multiples of a for its solutions -- a set, which the
787790
// inverter cannot return, the same as membership.
788791
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
789-
=> throw new NotSufficientlySupportedException(
790-
"Inverting this node would need a set-valued answer, which the inverter cannot give");
792+
=> null;
791793
}
792794

793795
partial record Congruentf
794796
{
795797
// (x = b (mod n)) = value has a residue class for its solutions -- a set, which the
796798
// inverter cannot return, the same as divisibility and membership.
797799
private protected override IEnumerable<Entity>? InvertNode(Entity value, Entity x)
798-
=> throw new NotSufficientlySupportedException(
799-
"Inverting this node would need a set-valued answer, which the inverter cannot give");
800+
=> null;
800801
}
801802

802803
partial record Quantifier

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ public void ARootIsInTheAnswerAndANonRootIsNot(string equation, string root, str
4646
Assert.Equal(Entity.Boolean.False, IsIn(other, solved));
4747
}
4848

49+
/// <summary>
50+
/// A set, a cardinality or a power set of the unknown has a set of sets for its preimage,
51+
/// which the inverter cannot write, and it threw <c>NotSufficientlySupportedException</c>
52+
/// out of <c>Solve</c> where its contract is to decline and leave the equation unsolved.
53+
/// https://github.com/asc-community/AngouriMath/issues/1544
54+
/// </summary>
55+
[Theory]
56+
[InlineData("card(x) = 3", "{1, 2, 3}", "{1}")]
57+
[InlineData("(x /\\ {1, 2}) = {1}", "{1}", "{2}")]
58+
[InlineData("(x \\ {1}) = {2}", "{2}", "{1, 2, 3}")]
59+
[InlineData("powerset(x) = {{}, {1}}", "{1}", "{2}")]
60+
public void ASetOfSetsIsLeftUnsolved(string equation, string root, string other)
61+
=> ARootIsInTheAnswerAndANonRootIsNot(equation, root, other);
62+
4963
/// <summary>
5064
/// The factorial is the gamma function one along, which has no zeros, so a factorial
5165
/// equal to zero does have none, and the roots found beside one are the whole answer.

0 commit comments

Comments
 (0)