Skip to content

Commit bfc7f5b

Browse files
Judge a root against the terms of an equation multiplied by a number (#2511)
A candidate, from a numerical search or from squaring or cubing an equation, is kept where the residual is small next to the terms the equation adds up to. A residual below an absolute 1e-6 was taken for none, whatever the terms: x (2 - 4x + 2x^2)^(1/3) = 2 times 10^-12 kept the two candidates cubing adds, which leave 3.46e-12 against terms of 2e-12. And an equation times a number is one product, read as one term and so as its own scale: (sin(x) - x) 10^12 = 0 lost every root Newton found, its residual being past the floor and the whole of itself. The largest term now reads through products and quotients, and the absolute bound is kept only where no sum was split, where the residual has nothing to be compared with. Solving 1400 generated equations as written and times 10^-12, 10^12: at 10^-12, 129 answers differed from the unscaled ones (113 radical equations with spurious roots, 16 inverse functions), now 0; at 10^12, the 20 trigonometric equations that lost roots and the 11 inverse ones that did now match. What differs at 10^12 is an OverflowException in the logarithm solver and answers left unsolved. The cross-checks of the unscaled equations against mpmath are unchanged. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f1fe8db commit bfc7f5b

4 files changed

Lines changed: 106 additions & 13 deletions

File tree

‎BREAKING-CHANGES.md‎

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

4343
## Unreleased — since 2.8.0
4444

45+
### An equation multiplied by a number has the roots it had
46+
47+
**Silent: answers lose roots that are not roots, and regain roots that were dropped.** A root found numerically, or
48+
a candidate that squaring or cubing an equation added, is kept when the equation's residual there is small next to the
49+
terms it is the sum of. A residual below an absolute 1e-6 was taken for none, so an equation multiplied by `10^-12`
50+
kept every candidate, roots or not. And an equation multiplied by a number is one product, which was read as one
51+
term and so as its own scale, so its residual was large next to itself and every root of it that was found
52+
numerically was dropped. The terms are now read through a product, and the absolute bound is left only for an
53+
equation with no terms to cancel, such as one function's value.
54+
55+
| Input | Was (2.8.0) | Now |
56+
|---|---|---|
57+
| `"(x * (2 - 4x + 2x^2)^(1/3) - 2) * 10^(-12) = 0".ToEntity().Solve("x")` | five roots, two of which leave 3.46e-12 | the three of `x * (2 - 4x + 2x^2)^(1/3) = 2` |
58+
| `"(sin(x) - x) * 10^12 = 0".ToEntity().Solve("x")` | `{ 0 }` | the nine of `sin(x) = x` |
59+
4560
### `MathS.Parse` and `Entity.TryParse` return a refused call rather than throw it
4661

4762
**Not silent: what threw now returns.** `MathS.Parse` is the parser that does not throw, and `Entity.TryParse` the

‎Sources/.editorconfig‎

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

156+
[Tests/UnitTests/Algebra/SolveTest/AScaledEquationHasTheSameRootsTest.cs]
157+
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
158+
156159
[Tests/UnitTests/Algebra/SolveTest/ADepressedBiquadraticQuarticTest.cs]
157160
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
158161

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

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,19 @@ internal static bool IsEvidenceAgainst(Entity substituted)
434434
// tenth more allocation on solving x^2 + x + 1.
435435
if (size.CompareTo(share.Multiply(share)) <= 0)
436436
return false;
437-
return size.GreaterThan(EDecimal.Max(LargestTerm(substituted), share).Multiply(share));
437+
var unused = false;
438+
return size.GreaterThan(EDecimal.Max(LargestTerm(substituted, ref unused), share).Multiply(share));
438439
}
439-
if (size.LessThan(MathS.Settings.PrecisionErrorCommon))
440-
return false;
441-
return size.GreaterThan(LargestTerm(substituted).Multiply(RelativeResidualTolerance));
440+
var cancels = false;
441+
var largest = LargestTerm(substituted, ref cancels);
442+
// Where nothing cancels, as in an equation that is one function's value, the residual
443+
// is the only term and its size against itself says nothing, so its size alone is
444+
// read. Where terms cancel the comparison is with them and nothing else: an absolute
445+
// floor took a residual of 3.46e-12 against terms of 2e-12 for none, so the equation
446+
// times 10^-12 kept two candidates that cubing it had added.
447+
if (!cancels)
448+
return !size.LessThan(MathS.Settings.PrecisionErrorCommon);
449+
return size.GreaterThan(largest.Multiply(RelativeResidualTolerance));
442450
}
443451

444452
/// <summary>
@@ -482,17 +490,33 @@ private static bool IsExactAndAlgebraic(Entity expression)
482490

483491
/// <summary>
484492
/// The largest of the terms the equation adds up to at the root, which is the scale
485-
/// the residual has to be read against. A single term is its own largest, so an
486-
/// equation that is not a sum is judged on the size of its residual alone -- there
487-
/// is nothing there for a root to have cancelled inexactly.
493+
/// the residual has to be read against, and in <paramref name="cancels"/> whether there
494+
/// are terms at all: whether a sum was split.
488495
/// </summary>
489-
private static EDecimal LargestTerm(Entity substituted)
496+
/// <remarks>
497+
/// A product or a quotient by a number is multiplied out first. The terms of
498+
/// <c>(sin(x) - x) 10^-12</c> are <c>sin(x) 10^-12</c> and <c>x 10^-12</c>; read as one
499+
/// term, it was its own scale, and every root Newton's method found for it was thrown out
500+
/// at <c>10^12</c>, where its residual is past the floor.
501+
/// </remarks>
502+
private static EDecimal LargestTerm(Entity substituted, ref bool cancels)
490503
{
491-
var largest = EDecimal.Zero;
492-
foreach (var term in Sumf.LinearChildren(substituted))
493-
if (term.Evaled is Number.Complex value && value.IsFinite)
494-
largest = EDecimal.Max(largest, value.Abs().EDecimal);
495-
return largest;
504+
switch (substituted)
505+
{
506+
case Sumf or Minusf:
507+
cancels = true;
508+
var largest = EDecimal.Zero;
509+
foreach (var term in Sumf.LinearChildren(substituted))
510+
largest = EDecimal.Max(largest, LargestTerm(term, ref cancels));
511+
return largest;
512+
case Mulf(var multiplier, var multiplicand):
513+
return LargestTerm(multiplier, ref cancels).Multiply(LargestTerm(multiplicand, ref cancels));
514+
case Divf(var dividend, var divisor) when divisor.Evaled is Number.Complex { IsFinite: true } value
515+
&& !value.Abs().EDecimal.IsZero:
516+
return LargestTerm(dividend, ref cancels).Divide(value.Abs().EDecimal, MathS.Settings.DecimalPrecisionContext);
517+
default:
518+
return substituted.Evaled is Number.Complex { IsFinite: true } number ? number.Abs().EDecimal : EDecimal.Zero;
519+
}
496520
}
497521

498522
/// <summary>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 System.Linq;
9+
using AngouriMath.Extensions;
10+
using Xunit;
11+
using static AngouriMath.Entity;
12+
using static AngouriMath.Entity.Set;
13+
14+
namespace AngouriMath.Tests.Algebra
15+
{
16+
/// <summary>
17+
/// <c>L = R</c> and <c>(L - R) s = 0</c> have the same roots for any number <c>s</c> that is not
18+
/// 0. A candidate that cubing an equation adds was thrown out by its residual, but a residual
19+
/// below an absolute 1e-6 was taken for none, so at <c>s = 10^-12</c> every such candidate
20+
/// stayed: <c>x (2 - 4x + 2x^2)^(1/3) = 2</c> has three roots, and scaled it had five, two of
21+
/// which leave 3.46 times <c>s</c>. And the scaled equation is one product, which was read as
22+
/// one term and so as its own scale: <c>(sin(x) - x) 10^12 = 0</c> had only the root 0. Found
23+
/// by solving generated equations as written and scaled; no issue was filed.
24+
/// </summary>
25+
[Trait("Area", "Algebra")]
26+
public sealed class AScaledEquationHasTheSameRootsTest
27+
{
28+
[Theory]
29+
[InlineData("x * (2 - 4x + 2x^2)^(1/3) = 2", "10^(-12)")]
30+
[InlineData("x * (3 + 3x + 2x^2)^(1/3) = -2", "10^(-12)")]
31+
[InlineData("2 - 3x^2 - (-3x)^(1/3) = 0", "10^(-12)")]
32+
[InlineData("x * (2 - 4x + 2x^2)^(1/3) = 2", "10^12")]
33+
// Newton's roots, judged against the terms of the product, not against the product.
34+
[InlineData("sin(x) = x", "10^12")]
35+
[InlineData("sin(x) = x", "10^(-12)")]
36+
[InlineData("arctan(x) = x / 3", "10^(-12)")]
37+
[InlineData("x^2 - 2 = 0", "10^(-12)")]
38+
[InlineData("x^3 - 3x + 1 = 0", "10^(-30)")]
39+
public void TheRootsAreTheRootsOfTheEquationAsWritten(string equation, string scale)
40+
{
41+
var asWritten = Assert.IsType<FiniteSet>(equation.ToEntity().Solve("x"));
42+
var sides = equation.Split(" = ");
43+
var scaled = Assert.IsType<FiniteSet>(
44+
$"(({sides[0]}) - ({sides[1]})) * {scale} = 0".ToEntity().Solve("x"));
45+
Assert.Equal(asWritten.Count, scaled.Count);
46+
foreach (var root in scaled)
47+
Assert.Contains(asWritten, other =>
48+
((Number.Complex)(root - other).EvalNumerical()).Abs() < 1e-8);
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)