Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions Sources/AngouriMath/Core/EquationSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public sealed class EquationSystem : ILatexizeable
/// <summary>
/// Each equation is an expression equal to zero, or an equality <c>a = b</c>, which is read as
/// <c>a - b = 0</c>, as <see cref="MathS.SolveEquation"/> reads one in a single variable.
/// A conjunction of equations, <c>x + y = 3 and x - y = 1</c>, 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.
/// </summary>
/// <param name="equations">
Expand All @@ -37,11 +39,13 @@ public sealed class EquationSystem : ILatexizeable
/// });
/// </code>
/// </example>
public EquationSystem(IEnumerable<Entity> equations) => this.equations = equations.Select(AsAnExpressionEqualToZero).ToList();
public EquationSystem(IEnumerable<Entity> equations) => this.equations = equations.SelectMany(AsExpressionsEqualToZero).ToList();

/// <summary>
/// Each equation is an expression equal to zero, or an equality <c>a = b</c>, which is read as
/// <c>a - b = 0</c>, as <see cref="MathS.SolveEquation"/> reads one in a single variable.
/// A conjunction of equations, <c>x + y = 3 and x - y = 1</c>, 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.
/// </summary>
/// <param name="equations">
Expand All @@ -55,14 +59,25 @@ public sealed class EquationSystem : ILatexizeable
/// );
/// </code>
/// </example>
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]]`.
// https://github.com/asc-community/AngouriMath/issues/1673
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<Entity> AsExpressionsEqualToZero(Entity equation)
=> equation is Andf && EquationsOf(equation, out _) is { } conjuncts
? conjuncts
: new[] { AsAnExpressionEqualToZero(equation) };

/// <summary>
/// The equations of <paramref name="system"/>, each an expression equal to zero, where it
/// is a conjunction of equations -- nested or not, and one equation is a conjunction of
Expand Down
29 changes: 29 additions & 0 deletions Sources/Tests/UnitTests/Common/SolverRegressionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,10 @@
var unknowns = variables.Split(',').Select(name => MathS.Var(name)).ToArray();
var solutions = MathS.SolveSystem(statement, unknowns);
Assert.NotNull(solutions);
Assert.Equal(rows, solutions.RowCount);

Check warning on line 411 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Dereference of a possibly null reference.

Check warning on line 411 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.

Check warning on line 411 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.
var equations = AngouriMath.Core.EquationSystem.EquationsOf(statement, out _);
Assert.NotNull(equations);
Assert.Equal(MathS.Equations(equations).Solve(unknowns), solutions);

Check warning on line 414 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Possible null reference argument for parameter 'equations' in 'EquationSystem MathS.Equations(IEnumerable<Entity> equations)'.

Check warning on line 414 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Possible null reference argument for parameter 'equations' in 'EquationSystem MathS.Equations(IEnumerable<Entity> equations)'.
for (var row = 0; row < solutions.RowCount; row++)
foreach (var equation in equations)
{
Expand All @@ -428,7 +428,7 @@
{
var solutions = MathS.SolveSystem("x + y = 3 and x - y = 1", "x", "y");
Assert.NotNull(solutions);
Assert.Equal(1, solutions.RowCount);

Check warning on line 431 in Sources/Tests/UnitTests/Common/SolverRegressionTest.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Dereference of a possibly null reference.
Assert.True(solutions[0, 0] == 2 && solutions[0, 1] == 1, solutions.ToString());
}

Expand Down Expand Up @@ -458,5 +458,34 @@
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"));
}
}
Loading