Skip to content

Commit 947c6f4

Browse files
A system with a product equation is solved branch by branch (#2129)
* A system with a product equation is solved branch by branch The eliminator solved x * y = 0 for y as 0, which is the answer only where x is not zero, and in a system the triangularisation does not take nothing followed the branch where x is. Each row that came back satisfied the system, so the answer read as all of the solutions: x*y; sin(x) + y - 1 missing (0, 1) (x - 1)*(y - 2); e^x - y missing (1, e) x*e^y; x^2*y [[0, 0]], where every (0, t) is a solution A system with a product in more than one of its unknowns is now solved once for each way of choosing a factor of each such product, up to 64 systems, and its solutions are theirs together. A product whose factors all mention the same unknowns is left alone, since solving it reads every factor. A branch that answers null is taken to have no solutions, which is what Solve says of it. Every row of the union is put back into the system as written, because a factor can be zero where another is not defined: the branch x = 0 of x ln(y), x - y is (0, 0), where ln(0) is no number. A row whose residual has no symbol left and is not a finite number is now dropped, on the square path too, as a root of one equation is. A branch can leave an unknown that no equation mentions, and the eliminator had no equation to take it from and answered nothing. Such a system goes to the non-square solver, which makes the unknown a parameter: x - 1, x^2 - 1 in x, y was null and is [[1, t_1]]. SolveSystem.TestTrigSystem4 pinned null for a - 1, b * 0, where b * 0 is zero for every b; it now expects the one family (1, t_1). Measured with the generated check from #2126 on 1190 square systems in x and y built from a known integer solution: the known solution missing 22 times on master and 13 here, with no other change in its findings and no wrong row on either. The 13 left are roots found by Newton's method, one of several, and systems with abs. Fixes #2128 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura * Put the #2128 entry first in Unreleased, now that #2127 has merged Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 4b0a97e commit 947c6f4

5 files changed

Lines changed: 224 additions & 5 deletions

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,24 @@ A not-equal written `<>` is read as `not (a = b)`, and printed that way.
4242

4343
## Unreleased — since 2.6.0
4444

45+
### A system with a product equation is solved branch by branch
46+
47+
**Silent: solutions that were missing are answered.** `EquationSystem.Solve` eliminated `x * y = 0` for `y` as `0`,
48+
which is the answer only where `x` is not zero, and lost the solutions where it is, in a system the triangularisation
49+
does not take. A system with a product in more than one of its unknowns is now solved once for each factor, and its
50+
solutions are theirs together, each put back into the system as written. A row is dropped where an equation, with the
51+
row put in, is not a finite number, as `x ln(y)` is at `(0, 0)`. A square system with an unknown that no equation
52+
mentions answers it as a parameter, as a short system does, where it was `null`
53+
([#2128](https://github.com/asc-community/AngouriMath/issues/2128)).
54+
55+
| Input | Was (2.6.0) | Now |
56+
|---|---|---|
57+
| `MathS.Equations("x*y", "sin(x) + y - 1").Solve("x", "y")` | `[[pi / 2 + 2 * pi * n_1, 0], [pi - pi / 2 + 2 * pi * n_1, 0]]` | `[[0, 1], [pi / 2 + 2 * pi * n_1, 0], [pi - pi / 2 + 2 * pi * n_1, 0]]` |
58+
| `MathS.Equations("(x - 1)*(y - 2)", "e^x - y").Solve("x", "y")` | `[[ln(2), 2]]` | `[[1, e], [ln(2), 2]]` |
59+
| `MathS.Equations("x*e^y", "x^2*y").Solve("x", "y")` | `[[0, 0]]` | `[[0, t_1], [0, 0]]` |
60+
| `MathS.Equations("x - 1", "x^2 - 1").Solve("x", "y")` | `null` | `[[1, t_1]]` |
61+
| `MathS.Equations("a - 1", "b * 0").Solve("a", "b")` | `null` | `[[1, t_1]]` |
62+
4563
### A polynomial system is not answered with some of its solutions missing
4664

4765
**Silent: a polynomial system that lost solutions has all of them.** `EquationSystem.Solve` triangularises a

‎Sources/.editorconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
264264
[Tests/UnitTests/Algebra/SolveTest/ParametricSystemTest.cs]
265265
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
266266

267+
[Tests/UnitTests/Algebra/SolveTest/ProductSystemTest.cs]
268+
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
269+
267270
[Tests/UnitTests/Algebra/SolveTest/SpuriousSystemRowTest.cs]
268271
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
269272

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

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,21 @@ internal static Set Solve(Entity equation, Variable x)
140140
equations, variables, out var family))
141141
return family;
142142

143+
// A product is zero where any one of its factors is, and the elimination solves
144+
// `x * y = 0` for `y` as `0`, which is the answer only where `x` is not zero. So a
145+
// system with a product in more than one of its unknowns is solved once for each
146+
// factor, and its solutions are theirs together.
147+
// https://github.com/asc-community/AngouriMath/issues/2128
148+
if (Branches(equations, variables) is { } branches)
149+
return Together(branches, written, vars, variables);
150+
143151
// Nor is any other count: `x^2 + y^2 = 1` has a family of solutions in a
144152
// parameter, and `sin(x) = 0` with `x = 0` has one solution. Those are eliminated,
145153
// and a system whose answer cannot be shown to hold is declined, as `Solve`
146154
// documents. https://github.com/asc-community/AngouriMath/issues/212
147-
if (equations.Count != vars.Length)
155+
// An unknown that no equation mentions is free, as one is in a short system, and
156+
// the eliminator has no equation to take it from.
157+
if (equations.Count != vars.Length || variables.Any(variable => !equations.Any(equation => equation.ContainsNode(variable))))
148158
return NonSquareSystemSolver.Solve(equations, variables, ledger) is { } families
149159
? new MatrixBuilder(families, vars.Length).ToMatrix()
150160
: null;
@@ -171,6 +181,75 @@ internal static Set Solve(Entity equation, Variable x)
171181
}
172182
}
173183

184+
/// <summary>The most systems a system is split into by its products.</summary>
185+
private const int MostBranches = 64;
186+
187+
/// <summary>
188+
/// One system for each way of choosing a factor of each product in more than one of
189+
/// <paramref name="variables"/>, with the product replaced by the factor, or
190+
/// <see langword="null"/> where there is no such product or the choices are too many.
191+
/// </summary>
192+
/// <remarks>
193+
/// A product whose factors all mention the same unknowns is left alone: solving it for
194+
/// any of them reads every factor, as <c>x (x - 1)</c> is solved for <c>x</c> as
195+
/// <c>0</c> and <c>1</c>.
196+
/// </remarks>
197+
private static List<List<Entity>>? Branches(List<Entity> equations, Variable[] variables)
198+
{
199+
var branches = new List<List<Entity>> { new(equations) };
200+
var split = false;
201+
for (var i = 0; i < equations.Count; i++)
202+
{
203+
if (equations[i] is not Mulf)
204+
continue;
205+
var factors = Mulf.LinearChildren(equations[i]).Where(factor => variables.Any(factor.ContainsNode)).ToList();
206+
if (factors.Count < 2)
207+
continue;
208+
var first = variables.Where(factors[0].ContainsNode).ToList();
209+
if (factors.All(factor => variables.Where(factor.ContainsNode).SequenceEqual(first)))
210+
continue;
211+
if (branches.Count * factors.Count > MostBranches)
212+
return null;
213+
split = true;
214+
branches = branches.SelectMany(branch => factors.Select(factor =>
215+
{
216+
var chosen = new List<Entity>(branch);
217+
chosen[i] = factor;
218+
return chosen;
219+
})).ToList();
220+
}
221+
return split ? branches : null;
222+
}
223+
224+
/// <summary>
225+
/// The solutions of each of <paramref name="branches"/> together, each kept only where
226+
/// it satisfies the system as written: a factor can be zero where another is not
227+
/// defined, as <c>x ln(y)</c> is at <c>x = 0</c> and <c>y = 0</c>.
228+
/// </summary>
229+
/// <remarks>
230+
/// A branch answered by <see langword="null"/> is taken to have no solutions, which is
231+
/// what <see cref="EquationSystem.Solve"/> says of it: <c>x e^y = 0</c> has none where
232+
/// <c>e^y</c> is zero.
233+
/// </remarks>
234+
private static Matrix? Together(List<List<Entity>> branches, List<Entity> written, ReadOnlySpan<Variable> vars, Variable[] variables)
235+
{
236+
var rows = new List<List<Entity>>();
237+
foreach (var branch in branches)
238+
{
239+
if (SolveSystem(branch, vars) is not { } solved)
240+
continue;
241+
for (var row = 0; row < solved.RowCount; row++)
242+
{
243+
var values = Enumerable.Range(0, solved.ColumnCount).Select(column => solved[row, column]).ToList();
244+
if (!rows.Any(other => other.SequenceEqual(values)))
245+
rows.Add(values);
246+
}
247+
}
248+
if (rows.Count == 0 || RowsThatHold(written, variables, rows) is not { Count: > 0 } kept)
249+
return null;
250+
return new MatrixBuilder(kept, vars.Length).ToMatrix();
251+
}
252+
174253
/// <summary>
175254
/// The rows of <paramref name="rows"/> that satisfy each of <paramref name="equations"/>,
176255
/// or <see langword="null"/> where a row is shown not to for some value of the symbols
@@ -180,8 +259,9 @@ internal static Set Solve(Entity equation, Variable x)
180259
/// <para>
181260
/// A row is dropped on the evidence a root of one equation is dropped on,
182261
/// <see cref="StatementSolver.IsEvidenceAgainst"/>: an equation that, with the row put
183-
/// in, has no symbol left and is a number away from zero. Such a row is no solution for
184-
/// any value of anything, so dropping it loses none.
262+
/// in, has no symbol left and is a number away from zero, or is no finite number at all, as
263+
/// <c>x ln(y)</c> is at <c>(0, 0)</c>. Such a row is no solution for any value of anything,
264+
/// so dropping it loses none.
185265
/// </para>
186266
/// <para>
187267
/// A row with symbols left in it is tried at one value of them, and one that fails there
@@ -207,7 +287,8 @@ internal static Set Solve(Entity equation, Variable x)
207287
for (var index = 0; index < variables.Length; index++)
208288
replacements[variables[index]] = row[index];
209289
var residuals = equations.Select(equation => equation.Substitute(replacements)).ToList();
210-
if (residuals.Any(residual => !residual.Vars.Any() && StatementSolver.IsEvidenceAgainst(residual)))
290+
if (residuals.Any(residual => !residual.Vars.Any()
291+
&& (residual.Evaled is Complex { IsFinite: false } || StatementSolver.IsEvidenceAgainst(residual))))
211292
continue;
212293
if (residuals.Any(residual => residual.Vars.Any() && StatementSolver.IsEvidenceAgainst(AtASamplePoint(residual, own))))
213294
return null;
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//
2+
// Copyright (c) 2019-2026 Angouri.
3+
// AngouriMath is licensed under MIT.
4+
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
5+
// Website: https://am.angouri.org.
6+
//
7+
8+
using AngouriMath;
9+
using AngouriMath.Extensions;
10+
using System.Linq;
11+
using Xunit;
12+
13+
namespace AngouriMath.Tests.Algebra
14+
{
15+
/// <summary>
16+
/// Systems with an equation that is a product in more than one unknown, which the
17+
/// elimination solved for one unknown as if the other factors were not zero: <c>x y = 0</c>
18+
/// for <c>y</c> is <c>0</c>, and the branch <c>x = 0</c> was lost. Each is now solved once
19+
/// for each factor.
20+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2128">#2128</a>
21+
/// </summary>
22+
[Trait("Area", "Algebra")]
23+
public sealed class ProductSystemTest
24+
{
25+
private static Entity.Matrix? Solve(params string[] equations)
26+
=> MathS.Equations(equations.Select(e => e.ToEntity()).ToArray()).Solve("x", "y");
27+
28+
/// <summary>
29+
/// Each row satisfies each equation, at several values of what is left in it: an
30+
/// integer parameter at whole numbers, anything else at values of both signs.
31+
/// </summary>
32+
private static void EveryRowHolds(Entity.Matrix solutions, params string[] equations)
33+
{
34+
for (var row = 0; row < solutions.RowCount; row++)
35+
foreach (var equation in equations)
36+
{
37+
var residual = equation.ToEntity().Substitute("x", solutions[row, 0]).Substitute("y", solutions[row, 1]);
38+
foreach (var at in new[] { -1, 0, 2 })
39+
{
40+
var pinned = residual;
41+
foreach (var symbol in residual.Vars.ToList())
42+
pinned = pinned.Substitute(symbol, symbol.Name.StartsWith("n_") ? at : at + 0.3);
43+
var value = Assert.IsAssignableFrom<Entity.Number.Complex>(pinned.Evaled);
44+
Assert.True(((Entity.Number.Real)value.Abs()).EDecimal.ToDouble() < 1e-9, $"{equation} at row {row}, {at}: {value}");
45+
}
46+
}
47+
}
48+
49+
private static bool HasRow(Entity.Matrix solutions, string x, string y)
50+
=> Enumerable.Range(0, solutions.RowCount).Any(row =>
51+
(solutions[row, 0] - x).Simplify() == 0 && (solutions[row, 1] - y).Simplify() == 0);
52+
53+
/// <summary>
54+
/// The branch the elimination lost: <c>(0, 1)</c> for the first two, where <c>x</c> is
55+
/// zero, and <c>(1, e)</c> for the third, where <c>x - 1</c> is.
56+
/// </summary>
57+
[Theory]
58+
[InlineData("x * y", "sin(x) + y - 1", 3, "0", "1")]
59+
[InlineData("x * y", "y - cos(x)", 3, "0", "1")]
60+
[InlineData("(x - 1) * (y - 2)", "e^x - y", 2, "1", "e")]
61+
[InlineData("(x - 1) * (y - 2)", "e^x - y", 2, "ln(2)", "2")]
62+
public void EachFactorIsABranch(string first, string second, int rows, string x, string y)
63+
{
64+
var solutions = Solve(first, second);
65+
Assert.NotNull(solutions);
66+
Assert.Equal(rows, solutions!.RowCount);
67+
Assert.True(HasRow(solutions, x, y), $"({x}, {y}) is not among {solutions}");
68+
EveryRowHolds(solutions, first, second);
69+
}
70+
71+
/// <summary>
72+
/// <c>x e^y = 0</c> and <c>x^2 y = 0</c> hold wherever <c>x</c> is zero, which was
73+
/// answered as the one point <c>(0, 0)</c>.
74+
/// </summary>
75+
[Fact]
76+
public void ABranchThatLeavesAnUnknownFreeIsAFamily()
77+
{
78+
var solutions = Solve("x * e^y", "x^2 * y");
79+
Assert.NotNull(solutions);
80+
Assert.Contains(Enumerable.Range(0, solutions!.RowCount),
81+
row => solutions[row, 0] == 0 && solutions[row, 1] is Entity.Variable);
82+
EveryRowHolds(solutions, "x * e^y", "x^2 * y");
83+
}
84+
85+
/// <summary>
86+
/// The branch <c>x = 0</c> of <c>x ln(y) = 0</c> with <c>x = y</c> is <c>(0, 0)</c>,
87+
/// where <c>ln(0)</c> is not a number and the product is not zero.
88+
/// </summary>
89+
[Fact]
90+
public void AFactorZeroWhereAnotherIsUndefinedIsNoSolution()
91+
{
92+
var solutions = Solve("x * ln(y)", "x - y");
93+
Assert.NotNull(solutions);
94+
Assert.Equal(1, solutions!.RowCount);
95+
Assert.True(HasRow(solutions, "1", "1"));
96+
}
97+
98+
/// <summary>
99+
/// A branch can leave an unknown that no equation mentions, and so can a caller. It is
100+
/// a parameter, as in a system with fewer equations than unknowns; the elimination had
101+
/// no equation to take it from and answered nothing.
102+
/// </summary>
103+
[Fact]
104+
public void AnUnknownNoEquationMentionsIsAParameter()
105+
{
106+
var solutions = Solve("x - 1", "x^2 - 1");
107+
Assert.NotNull(solutions);
108+
Assert.Equal(1, solutions!.RowCount);
109+
Assert.Equal(Entity.Number.Integer.Create(1), solutions[0, 0]);
110+
Assert.IsType<Entity.Variable>(solutions[0, 1]);
111+
}
112+
}
113+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,15 @@ public void TestTrigSystem3() => AssertSystemSolvable(new Entity[] {
7979
"3a+4b-4c"
8080
}, new Entity.Variable[] { "a", "b", "c" }, 1);
8181

82+
// b * 0 is zero for every b, so b is free and the solutions are (1, t) for every t. This
83+
// was answered null, because no equation was left to eliminate b from; an unknown that
84+
// no equation mentions is a parameter now, as it is in a short system.
85+
// https://github.com/asc-community/AngouriMath/issues/2128
8286
[Fact]
8387
public void TestTrigSystem4() => AssertSystemSolvable(new Entity[] {
8488
"a - 1",
8589
"b * 0"
86-
}, new Entity.Variable[] { "a", "b" }, 0);
90+
}, new Entity.Variable[] { "a", "b" }, 1);
8791

8892
// https://www.youtube.com/watch?v=dVs26SSUJSA
8993
[Fact]

0 commit comments

Comments
 (0)