Skip to content

Commit 4e603e8

Browse files
A system is not answered with a copy of one of its rows (#2158)
Two branches of an elimination can reach one solution, and each was answered. The circles (x - 3/2)^2 + (y - sqrt(3)/2)^2 = 1 and (x - 1)^2 + y^2 = 1 meet at (1/2, sqrt(3)/2) and (2, 0), and came back with the first of those twice. Where the second branch went through Newton's method the copy was a decimal: with the first circle centred at (1/2, sqrt(3)/2) instead, the rows were (0, 0), (3/2, sqrt(3)/2) and a third whose x was 1.4999999999999997. RowsThatHold now drops a row equal to an earlier one, and a row with a decimal in it whose entries are each within a part in a million of an exact row's. The tolerance is not the double's own precision because a root taken of the decimal multiplies its error: that copy's y was out by about 2e-8. A decimal row that no exact row agrees with is kept, as the search's answer, as a root of one equation is. syscheck, 1190 generated systems, is unchanged: 0 wrong rows and the same 13 known solutions missing, on this branch and on its base. Claude-Session: https://claude.ai/code/session_012sonx8iAspMiwRwokT1Ura Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent bc65529 commit 4e603e8

4 files changed

Lines changed: 118 additions & 1 deletion

File tree

‎BREAKING-CHANGES.md‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,20 @@ exception around `Solve` to detect a non-square system no longer sees it.
189189
| `MathS.Equations("sqrt(y) - x").Solve("x", "y")` | `WrongNumberOfArgumentsException` | `null`: `y = x^2` is no solution where `x` is negative |
190190
| `MathS.Equations("x + y - a", "x - y - b", "x - c").Solve("x", "y")` | `WrongNumberOfArgumentsException` | `null`: there are solutions only where `a + b = 2c` |
191191

192+
### A system is not answered with a copy of one of its rows
193+
194+
**Silent: a repeated row goes.** Two branches of an elimination can reach one solution, and each was answered: two
195+
circles that meet at two points came back with three rows, one of them another again. Where the second branch went
196+
through Newton's method the copy was a decimal, `x = 1.4999999999999997` beside the exact `x = 3/2`. A row equal to an
197+
earlier one is dropped, and so is a row with a decimal in it whose entries are each within a part in a million of an
198+
exact row's. A decimal row that no exact row agrees with is kept, as the search's answer
199+
([#2138](https://github.com/asc-community/AngouriMath/issues/2138)).
200+
201+
| Input | Was (2.6.0) | Now |
202+
|---|---|---|
203+
| `MathS.Equations("(x-1/2)^2+(y-sqrt(3)/2)^2-1", "(x-1)^2+y^2-1").Solve("x", "y")` | `[[0, 0], [1.4999999999999997779553950749686919152736663818359375, …], [3/2, -(-2) * sqrt(3) * 1/2 / 2]]` | `[[0, 0], [3/2, -(-2) * sqrt(3) * 1/2 / 2]]` |
204+
| `MathS.Equations("(x-3/2)^2+(y-sqrt(3)/2)^2-1", "(x-1)^2+y^2-1").Solve("x", "y")` | `[[1/2, -(-2) * sqrt(3) * 1/2 / 2], [2, 0], [1/2, -(-2) * sqrt(3) * 1/2 / 2]]` | `[[1/2, -(-2) * sqrt(3) * 1/2 / 2], [2, 0]]` |
205+
192206
### `am_classify` is a preliminary tool, served by `amcli mcp --preliminary`
193207

194208
**Not silent: what answered is a failure naming the flag.** `am_classify` guesses which field a formula comes from by

‎Sources/.editorconfig‎

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

279+
[Tests/UnitTests/Algebra/SolveTest/DuplicateSystemRowTest.cs]
280+
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
281+
279282
[Tests/UnitTests/Common/LambdaArrowSyntaxTest.cs]
280283
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
281284

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

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,57 @@ internal static Set Solve(Entity equation, Variable x)
307307
return null;
308308
kept.Add(row);
309309
}
310-
return kept;
310+
return WithoutDecimalCopies(kept);
311+
}
312+
313+
/// <summary>
314+
/// <paramref name="rows"/> without a row that is a copy of another: one equal to an
315+
/// earlier row, and one with a number in it that is not exact, each of whose entries is
316+
/// within a part in a million of an exact row's.
317+
/// </summary>
318+
/// <remarks>
319+
/// <para>
320+
/// Two branches of an elimination can reach one solution, and both were answered: the
321+
/// circles <c>(x - 3/2)^2 + (y - sqrt(3)/2)^2 = 1</c> and <c>(x - 1)^2 + y^2 = 1</c> met at
322+
/// <c>(1/2, sqrt(3)/2)</c> twice. Where one branch went through Newton's method the copy
323+
/// is a decimal: <c>(x - 1/2)^2 + (y - sqrt(3)/2)^2 = 1</c> with the same second circle
324+
/// met at <c>(3/2, sqrt(3)/2)</c> and again where <c>x</c> is <c>1.4999999999999997</c>.
325+
/// </para>
326+
/// <para>
327+
/// The tolerance is a part in a million, not the double's own precision: a root taken
328+
/// of the decimal multiplies its error, and that copy's <c>y</c> is out by about
329+
/// <c>2e-8</c>. A decimal row that no exact row agrees with is kept: it is the answer the
330+
/// search found, as a root of one equation is.
331+
/// https://github.com/asc-community/AngouriMath/issues/2138
332+
/// </para>
333+
/// </remarks>
334+
private static List<List<Entity>> WithoutDecimalCopies(List<List<Entity>> rows)
335+
{
336+
static bool HasADecimal(List<Entity> row)
337+
=> row.Any(entry => entry.Nodes.Any(node => node is Number { IsExact: false }));
338+
static Complex? ValueOf(Entity entry)
339+
=> entry.Vars.Any() ? null : entry.Evaled as Complex;
340+
static bool Agree(List<Entity> row, List<Entity> exact)
341+
{
342+
for (var index = 0; index < row.Count; index++)
343+
{
344+
if (ValueOf(row[index]) is not { } value || ValueOf(exact[index]) is not { } other)
345+
return false;
346+
var difference = ((Complex)(value - other)).Abs().EDecimal;
347+
var scale = PeterO.Numbers.EDecimal.Max(PeterO.Numbers.EDecimal.One, other.Abs().EDecimal);
348+
if (difference.CompareTo(scale.Multiply(PeterO.Numbers.EDecimal.FromString("1e-6"))) > 0)
349+
return false;
350+
}
351+
return true;
352+
}
353+
var distinct = new List<List<Entity>>(rows.Count);
354+
foreach (var row in rows)
355+
if (!distinct.Any(earlier => earlier.SequenceEqual(row)))
356+
distinct.Add(row);
357+
var exactRows = distinct.Where(row => !HasADecimal(row)).ToList();
358+
if (exactRows.Count == distinct.Count)
359+
return distinct;
360+
return distinct.Where(row => !HasADecimal(row) || !exactRows.Any(exact => Agree(row, exact))).ToList();
311361
}
312362

313363
/// <summary>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
using static AngouriMath.Entity;
13+
14+
namespace AngouriMath.Tests.Algebra.SolveTest
15+
{
16+
/// <summary>
17+
/// Two branches of an elimination can reach one solution, and each was answered: two circles
18+
/// that meet at two points gave three rows, one of them the other again, and where the
19+
/// second branch went through Newton's method, again as a decimal.
20+
/// <a href="https://github.com/asc-community/AngouriMath/issues/2138">#2138</a>
21+
/// </summary>
22+
[Trait("Area", "Algebra")]
23+
public sealed class DuplicateSystemRowTest
24+
{
25+
private static Matrix Solve(string first, string second)
26+
{
27+
var solved = MathS.Equations(first.ToEntity(), second.ToEntity()).Solve("x", "y");
28+
Assert.NotNull(solved);
29+
return solved!;
30+
}
31+
32+
private static bool HasRow(Matrix solutions, string x, string y)
33+
=> Enumerable.Range(0, solutions.RowCount).Any(row =>
34+
(solutions[row, 0] - x).Simplify() == 0 && (solutions[row, 1] - y).Simplify() == 0);
35+
36+
[Theory]
37+
// the copy was a decimal: x = 1.4999999999999997
38+
[InlineData("(x - 1/2)^2 + (y - sqrt(3)/2)^2 - 1", "0", "0", "3/2", "sqrt(3)/2")]
39+
// the copy was the same row again
40+
[InlineData("(x - 3/2)^2 + (y - sqrt(3)/2)^2 - 1", "1/2", "sqrt(3)/2", "2", "0")]
41+
public void TwoCirclesMeetingTwiceGiveTwoExactRows(string circle, string x1, string y1, string x2, string y2)
42+
{
43+
var solutions = Solve(circle, "(x - 1)^2 + y^2 - 1");
44+
Assert.Equal(2, solutions.RowCount);
45+
Assert.True(HasRow(solutions, x1, y1), $"({x1}, {y1}) is not among {solutions}");
46+
Assert.True(HasRow(solutions, x2, y2), $"({x2}, {y2}) is not among {solutions}");
47+
Assert.DoesNotContain(solutions.Nodes, node => node is Number { IsExact: false });
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)