diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 872f446da..9384bf7c3 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -694,6 +694,22 @@ shown and the argument list of symbolic length wait for v3. Every one of these w | `1 * 2 * ... * n` | `UnhandledParseException` | `product(k, k, 1, n)`, which is `n!` for `n >= 1` | | `{1, 4, 9, ..., n}` | `UnhandledParseException` | `InvalidArgumentParseException`, naming the term off the progression | +### A listed solution set is filtered by a decided condition + +`"x^2 = 4 and not x = 2".Solve("x")` is `{ -2 }`; it was the conjunction left as written, because +#1036 taught the solver not to intersect a listed set with a condition a search had left open +(1 is a root of `x^6 + x y + 1 = 0` only when `y` is -2). The two are told apart now: a member +at which the condition is decided is kept or dropped, and the conjunction stays written the +moment one member is undecided, so #1036's case is as it was. Decided by evaluation, and by +simplification where evaluation leaves it open, so that `forall u in RR : forall v in RR : +2 u + 1 = 2 v + 1 implies u = v` — the injectivity of a linear map, Def 7.4.6 of the reference +of [#1409](https://github.com/asc-community/AngouriMath/issues/1409) — is `True`. + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"x^2 = 4 and not x = 2".Solve("x")` | `{ x : x ^ 2 = 4 and not x = 2 }` | `{ -2 }` | +| `"x^2 = 4 and x > 0".Solve("x")` | `{ 2 }` | `{ 2 }` — unchanged, both sides being settled | +| `"x^6 + x*y + 1 = 0 and x - 1 = 0".Solve("x")` | `{ x : … }`, as written | unchanged | ### `image` and `preimage` of a set under an expression `image(f(x), x in A)` is `{ f(x) : x in A }` — `union({f(x)}, x in A)` — listed over a listed diff --git a/Sources/.editorconfig b/Sources/.editorconfig index fa8d89f23..45c516d2e 100644 --- a/Sources/.editorconfig +++ b/Sources/.editorconfig @@ -291,6 +291,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed [AngouriMath/Functions/NumberTheory/ResidueClasses.cs] 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 +[Tests/UnitTests/Algebra/ConjunctionFilterTest.cs] +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 + [Tests/UnitTests/Core/Sets/ImageAndPreImageTest.cs] 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 diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs index 2c73f591d..59590193a 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs @@ -247,9 +247,41 @@ private static Set Implication(Entity left, Entity right, Variable x) /// #1036 /// private static Set Conjunction(Set left, Set right, Entity statement, Variable x) - => (left, right) is (ConditionalSet, FiniteSet) or (FiniteSet, ConditionalSet) - ? new ConditionalSet(x, statement) - : (Set)MathS.Intersection(left, right); + => (left, right) switch + { + (ConditionalSet condition, FiniteSet listed) => Filtered(listed, condition, statement, x), + (FiniteSet listed, ConditionalSet condition) => Filtered(listed, condition, statement, x), + _ => (Set)MathS.Intersection(left, right), + }; + + /// + /// The members of a listed solution set at which the other side's condition is + /// decided: kept where it evaluates to True, dropped where False -- so + /// x^2 = 4 and not x = 2 is { -2 } -- and the conjunction as written the + /// moment one member is undecided, which is #1036's case, where a search left the + /// condition open and 1 was a root only when y was -2. + /// + private static Set Filtered(FiniteSet listed, ConditionalSet condition, Entity statement, Variable x) + { + if (condition.Var is not Variable bound) + return new ConditionalSet(x, statement); + var kept = new List(); + foreach (var member in listed) + { + // Evaluated, and simplified where evaluation leaves it open: the solution of + // 2 v + 1 = 2 u + 1 arrives as -(1 - 2u - 1)/2, and `not that = u` is decided + // by the simplifier and not by evaluation. + var at = condition.Predicate.Substitute(bound, member); + var decided = at.Evaled; + if (decided is not Entity.Boolean) + decided = at.Simplify(); + if (decided == Entity.Boolean.True) + kept.Add(member); + else if (decided != Entity.Boolean.False) + return new ConditionalSet(x, statement); + } + return new FiniteSet(kept); + } /// /// The solutions of difference = 0 (mod modulus) in , where diff --git a/Sources/Tests/UnitTests/Algebra/ConjunctionFilterTest.cs b/Sources/Tests/UnitTests/Algebra/ConjunctionFilterTest.cs new file mode 100644 index 000000000..d1946cc45 --- /dev/null +++ b/Sources/Tests/UnitTests/Algebra/ConjunctionFilterTest.cs @@ -0,0 +1,58 @@ +// +// Copyright (c) 2019-2026 Angouri. +// AngouriMath is licensed under MIT. +// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md. +// Website: https://am.angouri.org. +// + +using AngouriMath; +using AngouriMath.Extensions; +using Xunit; +using static AngouriMath.Entity; + +namespace AngouriMath.Tests.Algebra +{ + /// + /// A conjunction whose one side is a listed solution set and whose other is a condition: + /// the members are kept where the condition is decided True at them, dropped where + /// False, and the conjunction stays written the moment one is undecided -- which is + /// #1036's case, where the condition was a search left open. x^2 = 4 and not x = 2 + /// is { -2 }; and with it the injectivity of a linear map is decided, the + /// reference's Def 7.4.6 (Sullivan and Mackey). + /// , + /// . + /// + [Trait("Area", "Algebra")] + public sealed class ConjunctionFilterTest + { + [Theory] + [InlineData("x^2 = 4 and not x = 2", "{-2}")] + [InlineData("x^2 = 4 and not x = 3", "{-2, 2}")] + [InlineData("x^2 = 4 and x > 0", "{2}")] + [InlineData("(x - 1) (x - 2) = 0 and not x = 1 and not x = 2", "{}")] + public void AListedSetIsFilteredByADecidedCondition(string statement, string expected) + { + var solved = statement.ToEntity().Solve("x"); + var wanted = (Set)expected.ToEntity().Evaled; + Assert.Equal(Boolean.True, MathS.Sets.Subset(solved, wanted).Evaled); + Assert.Equal(Boolean.True, MathS.Sets.Subset(wanted, solved).Evaled); + } + + /// #1036: a member whose condition is not decided is not kept and not dropped -- the conjunction is left as written. + [Fact] + public void AnUndecidedConditionLeavesTheConjunctionWritten() + { + var solved = "x^6 + x*y + 1 = 0 and x - 1 = 0".ToEntity().Solve("x"); + Assert.IsType(solved); + } + + /// The inner statement of injectivity, and injectivity itself, for a linear map; the quadratic is refuted by a witness. + [Fact] + public void InjectivityOfALinearMapIsDecided() + { + Assert.Equal(Boolean.True, "forall v in RR : 2 v + 1 = 2 u + 1 implies v = u".ToEntity().Evaled); + Assert.Equal(Boolean.True, "forall u in RR : forall v in RR : 2 u + 1 = 2 v + 1 implies u = v".ToEntity().Evaled); + Assert.Equal(Boolean.False, "forall u in RR : forall v in RR : u^2 = v^2 implies u = v".ToEntity().Evaled); + } + } +}