From 4c132cf0f4dec94a18a2e45b27d29e0bf1465e0d Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Mon, 28 Sep 2026 21:28:13 +0000 Subject: [PATCH] A statement equal to a truth value is solved as the statement or its negation "(x > 2) = (3 > 1)".Solve("x") was { }, and so was every equation between a statement and a truth value: the arm for an equation of numbers subtracted the two sides. A statement equal to True is the statement, equal to False its negation, and two statements equal are equivalent. Closes #1549. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --- BREAKING-CHANGES.md | 14 +++++++++++ .../Solvers/EquationSolver/SolveStatement.cs | 18 ++++++++++++++ .../Algebra/SolveTest/SolveStatement.cs | 24 +++++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index beac197a1..4bcd1ad21 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -789,6 +789,20 @@ coefficient that is zero now answers the whole line or nothing. | `"x^2 < x^2 - 2".Solve("x")` | `InvalidCastException` | `{ }` | | `forall x in RR : x + 1 > x` | `UnhandledParseException` (`InvalidCastException` since quantifiers arrived) | `True` | +### A statement equal to a truth value is solved as the statement or its negation + +**Wrong answer fixed.** `"(x > 2) = (3 > 1)".Solve("x")` was `{ }`, and so was every equation +between a statement and a truth value: the arm for an equation of numbers subtracted the two +sides, which claimed there was no root. A statement equal to `True` is the statement, equal to +`False` its negation, and two statements equal are equivalent +([#1549](https://github.com/asc-community/AngouriMath/issues/1549)). + +| Input | Was (2.5.0) | Now | +|---|---|---| +| `"(x > 2) = (3 > 1)".Solve("x")` | `{ }` — wrong | `(2; +oo)` | +| `"(x = 1) = true".Solve("x")` | `{ }` — wrong | `{ 1 }` | +| `"(x > 0) = (x > 1)".Solve("x")` | `UncompilableNodeException` | `(1; +oo) \/ (-oo; 0]` | + ### An infinite base has its whole powers, and surjectivity is decided through the image `(-oo)^3` was `NaN` — a claim that the value does not exist — and so were `(-oo)^2` and diff --git a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs index 948312c0a..208ea4f8c 100644 --- a/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs +++ b/Sources/AngouriMath/Functions/Continuous/Solvers/EquationSolver/SolveStatement.cs @@ -329,6 +329,18 @@ private static Set AsWritten(Set solved, Entity difference, Entity equation, Var ? new ConditionalSet(x, equation) : solved; + /// + /// a = b between two statements, as the statement it is: a where b is + /// True, not a where it is False, and the two agreeing otherwise. + /// + private static Entity Equivalent(Entity left, Entity right) + => (left, right) switch + { + (_, Entity.Boolean truth) => truth == Entity.Boolean.True ? left : !left, + (Entity.Boolean truth, _) => truth == Entity.Boolean.True ? right : !right, + _ => (left & right) | (!left & !right), + }; + /// /// The solution set as gives it, simplified, or /// where the solver declines the statement: an inequality it @@ -342,6 +354,12 @@ private static Set AsWritten(Set solved, Entity difference, Entity equation, Var internal static Set? Solve(Entity expr, Variable x) => expr switch { + // A statement equal to a truth value is the statement or its negation, and two + // statements equal are equivalent: (x > 2) = (3 > 1) holds where x > 2 does. The + // arm for an equation of numbers subtracted them, and answered { }. + // https://github.com/asc-community/AngouriMath/issues/1549 + Equalsf(Statement left, Statement right) => Solve(Equivalent(left, right), x), + Equalsf(var left, var right) when left is Set || right is Set => AnalyticalSetSolver.Solve(left, right, x), diff --git a/Sources/Tests/UnitTests/Algebra/SolveTest/SolveStatement.cs b/Sources/Tests/UnitTests/Algebra/SolveTest/SolveStatement.cs index 28cfb6c9e..71712a692 100644 --- a/Sources/Tests/UnitTests/Algebra/SolveTest/SolveStatement.cs +++ b/Sources/Tests/UnitTests/Algebra/SolveTest/SolveStatement.cs @@ -23,5 +23,29 @@ public void TestStatementSolver(string statement, string expectedRaw) var actual = expr.Solve("x"); Assert.Equal(expected, actual); } + + /// + /// A statement equal to a truth value is solved as the statement or its negation, and two + /// statements equal as their equivalence, compared at points. Each was solved to the + /// empty set. https://github.com/asc-community/AngouriMath/issues/1549 + /// + [Theory] + [InlineData("(x > 2) = (3 > 1)", "3 5/2", "2 0")] + [InlineData("(x = 1) = true", "1", "2 0")] + [InlineData("true = (x in {1, 2})", "1 2", "3")] + [InlineData("(x > 2) = false", "2 0", "3")] + [InlineData("(2 divides x) = true", "4 -2", "3")] + [InlineData("(x = 1 (mod 3)) = true", "1 4 -2", "2 3")] + [InlineData("(x > 0) = (x > 1)", "2 -1 0", "1/2 1")] + public void AStatementEqualToATruthValueIsSolved(string statement, string inside, string outside) + { + var solved = MathS.FromString(statement, useCache: false).Solve("x"); + foreach (var (points, expected) in new[] { (inside, true), (outside, false) }) + foreach (var point in points.Split(' ')) + { + Assert.True(solved.TryContains(MathS.FromString(point), out var contains), $"{solved} does not decide {point}"); + Assert.Equal(expected, contains); + } + } } }