From 7b50342bd1b1053729bc5ee2776d99559df18ab2 Mon Sep 17 00:00:00 2001 From: Rafael Vuijk Date: Sun, 15 Nov 2026 21:09:18 +0000 Subject: [PATCH] MathS.Equations reads a conjunction of equations as its equations MathS.Equations passed a conjunction on as one equation and the system had no solution: x + y = 3 and x - y = 1 was null, where the two equations listed are [[2, 1]], and the same held for a conjunction listed beside another equation. A conjunction whose every conjunct is an equation is now read as those equations, through EquationSystem.EquationsOf, as MathS.SolveSystem reads one. A conjunction with any other conjunct is passed on as it was, so its answer is unchanged: solving the equations alone would answer a different question. Measured on the 2.7.0 package and this branch: the three conjunctions of equations go from null to [[2, 1]], [[2, 1]] and [[-3, -4], [4, 3]]; x + y = 3 and x > 0 stays null. Recorded in BREAKING-CHANGES.md. Part of #2450. Co-Authored-By: Claude Opus 5.5 --- BREAKING-CHANGES.md | 17 +++++++++++ Sources/AngouriMath/Core/EquationSystem.cs | 19 ++++++++++-- .../UnitTests/Common/SolverRegressionTest.cs | 29 +++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 0f882f264..6fca7d369 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -186,6 +186,23 @@ For 80 to 160 it was answered with the one row `[-2P, -sqrt(2)]` too, and for 14 --- +### A conjunction of equations given to `MathS.Equations` is read as its equations + +**Not silent: no answer becomes the answer.** `MathS.Equations` passed a conjunction on as one equation, and the +system had no solution, so `x + y = 3 and x - y = 1` was `null` where the two equations listed are `[[2, 1]]`. A +conjunction whose every conjunct is an equation is now read as those equations, as `MathS.SolveSystem` reads one. A +conjunction with any other conjunct, an inequality or a membership, is passed on as it was, and its answer is +unchanged ([#2450](https://github.com/asc-community/AngouriMath/issues/2450)). + +| Input | Was (2.7.0) | Now | +|---|---|---| +| `MathS.Equations("x + y = 3 and x - y = 1").Solve("x", "y")` | `null` | `[[2, 1]]` | +| `MathS.Equations("x + y = 3 and x - y = 1", "x + 2 y = 4").Solve("x", "y")` | `null` | `[[2, 1]]` | +| `MathS.Equations("x^2 + y^2 = 25 and x - y = 1").Solve("x", "y")` | `null` | `[[-3, -4], [4, 3]]` | +| `MathS.Equations("x + y = 3 and x > 0").Solve("x", "y")` | `null` | unchanged | + +--- + ## 2.7.0 — since 2.6.0 **Read these first: names that read differently.** Each of these was read as something else on 2.6.0. Code that used diff --git a/Sources/AngouriMath/Core/EquationSystem.cs b/Sources/AngouriMath/Core/EquationSystem.cs index 97306175e..74167dd3e 100644 --- a/Sources/AngouriMath/Core/EquationSystem.cs +++ b/Sources/AngouriMath/Core/EquationSystem.cs @@ -24,6 +24,8 @@ public sealed class EquationSystem : ILatexizeable /// /// Each equation is an expression equal to zero, or an equality a = b, which is read as /// a - b = 0, as reads one in a single variable. + /// A conjunction of equations, x + y = 3 and x - y = 1, is read as the equations + /// it joins; one with any other conjunct is passed on as it is. /// After having created a system of equations, you may solve or latexize it. /// /// @@ -37,11 +39,13 @@ public sealed class EquationSystem : ILatexizeable /// }); /// /// - public EquationSystem(IEnumerable equations) => this.equations = equations.Select(AsAnExpressionEqualToZero).ToList(); + public EquationSystem(IEnumerable equations) => this.equations = equations.SelectMany(AsExpressionsEqualToZero).ToList(); /// /// Each equation is an expression equal to zero, or an equality a = b, which is read as /// a - b = 0, as reads one in a single variable. + /// A conjunction of equations, x + y = 3 and x - y = 1, is read as the equations + /// it joins; one with any other conjunct is passed on as it is. /// After having created a system of equations, you may solve or latexize it. /// /// @@ -55,7 +59,7 @@ public sealed class EquationSystem : ILatexizeable /// ); /// /// - public EquationSystem(params Entity[] equations) => this.equations = equations.Select(AsAnExpressionEqualToZero).ToList(); + public EquationSystem(params Entity[] equations) => this.equations = equations.SelectMany(AsExpressionsEqualToZero).ToList(); // An equality was passed on as it stood and the system answered that it had no solution: // `x + y = 3` and `x - y = 1` gave null, where `x + y - 3` and `x - y - 1` give `[[2, 1]]`. @@ -63,6 +67,17 @@ public sealed class EquationSystem : ILatexizeable private static Entity AsAnExpressionEqualToZero(Entity equation) => equation is Equalsf(var left, var right) ? left - right : equation; + // A conjunction of equations was passed on as one equation and the system had no + // solution: x + y = 3 and x - y = 1 gave null, where the two equations listed give + // [[2, 1]]. A conjunction is its conjuncts, where each is an equation; one with any other + // conjunct -- an inequality, a membership -- is passed on as it was, since solving the + // equations alone would answer a different question. + // https://github.com/asc-community/AngouriMath/issues/2450 + private static IEnumerable AsExpressionsEqualToZero(Entity equation) + => equation is Andf && EquationsOf(equation, out _) is { } conjuncts + ? conjuncts + : new[] { AsAnExpressionEqualToZero(equation) }; + /// /// The equations of , each an expression equal to zero, where it /// is a conjunction of equations -- nested or not, and one equation is a conjunction of diff --git a/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs b/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs index d894ae036..5ade56cc4 100644 --- a/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs +++ b/Sources/Tests/UnitTests/Common/SolverRegressionTest.cs @@ -458,5 +458,34 @@ public void Issue2450_AnInconsistentSystemIsNull(string statement) Assert.Null(MathS.SolveSystem(statement, "x", "y")); Assert.Null(MathS.Equations(AngouriMath.Core.EquationSystem.EquationsOf(statement, out _)!).Solve("x", "y")); } + + // MathS.Equations passed a conjunction on as one equation, and the system had no + // solution: x + y = 3 and x - y = 1 gave null, as did the same conjunction listed beside + // another equation. A conjunction of equations is now read as its conjuncts, so the + // answer is what the equations listed one by one give. + // https://github.com/asc-community/AngouriMath/issues/2450 + [Theory] + [InlineData(new[] { "x + y = 3 and x - y = 1" }, new[] { "x + y = 3", "x - y = 1" })] + [InlineData(new[] { "x + y = 3 and x - y = 1", "x + 2 y = 4" }, new[] { "x + y = 3", "x - y = 1", "x + 2 y = 4" })] + [InlineData(new[] { "(x + y = 3 and x - y = 1) and x + 2 y = 4" }, new[] { "x + y = 3", "x - y = 1", "x + 2 y = 4" })] + [InlineData(new[] { "x^2 + y^2 = 25 and x - y = 1" }, new[] { "x^2 + y^2 = 25", "x - y = 1" })] + [InlineData(new[] { "x = y = 1" }, new[] { "x = y", "y = 1" })] + public void Issue2450_EquationsReadsAConjunctionAsItsEquations(string[] written, string[] listed) + { + var asWritten = MathS.Equations(written.Select(e => (Entity)e)).Solve("x", "y"); + var asListed = MathS.Equations(listed.Select(e => (Entity)e)).Solve("x", "y"); + Assert.NotNull(asListed); + Assert.Equal(asListed, asWritten); + Assert.Equal(asListed, MathS.Equations(written.Select(e => (Entity)e).ToArray()).Solve("x", "y")); + } + + // A conjunction with a conjunct that is not an equation is passed on as it was, and the + // answer is unchanged: null, rather than the equations alone solved, which would answer + // a different question. + [Theory] + [InlineData("x + y = 3 and x > 0")] + [InlineData("x + y = 3 and x - y = 1 and x in RR")] + public void Issue2450_EquationsKeepsAConjunctionWithAnotherConjunct(string written) + => Assert.Null(MathS.Equations(written).Solve("x", "y")); } }