Skip to content
Open
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
15 changes: 15 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ one point, or else `Simplify` proving it zero. A singular matrix answers as `[[1
| `"[[sin(x)^2, 1], [1 - cos(x)^2, 1]]^(-1)".Simplify()` | `NaN` | `1 / [[sin(x) ^ 2, 1], [sin(x) ^ 2, 1]]` |
| `"[[e^x, e^(2x)], [2, e^x]]^(-1)".Simplify()` | its inverse | unchanged |

### A root near 0 of an equation in tanh, sinh or cosh is not a near copy of 0

**Not silent: a false root is gone.** tanh, sinh and cosh are written with exponentials, and near 0 the parts of
`e^u - 1` or `e^u - e^(-u)` cancel. A root the numeric search found near 0 was judged against those parts, of size
`10^3` and more, rather than against the terms of the equation, and kept beside 0. A root found in decimals is now
also judged against the terms of the sum the equation is, each read whole, and dropped where it leaves a residual
large beside them ([#2645](https://github.com/asc-community/AngouriMath/issues/2645)).

| `.Solve("x")` | Was (2.10.0) | Now |
|---|---|---|
| `10^3 tanh(x / 10^3) = x / 2` | `{ 0, 1.1013…E-13 }` | `{ 0 }` |
| `10^20 tanh(x / 10^20) = x / 3` | `{ -3.1958…E-13, 0 }` | `{ 0 }` |
| `10^6 sinh(x / 10^6) = 2 x` | `{ -4.9027…E-13, 0, 4.9027…E-13 }` | `{ 0 }` |
| `tanh(x) = x / 2` | its roots | unchanged |

### The whole part of a large rational power is taken out

**Silent, in the printed form only.** A rational raised to a rational power was written as a whole part times a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ private static RootVerdict Verdict(Entity equation, Variable x, Entity root)
case ResidualWeight.CannotTell:
return RootVerdict.ShownNotARoot;
}
if (!IsExact(root) && IsLargeBesideTheTermsAsWritten(equation, x, root, substituted))
return RootVerdict.Spurious;
return !substituted.Vars.Any() && IsAboveTheIntervalFloor(equation, x, root) && IsShownNotZero(substituted)
? RootVerdict.ShownNotARoot
: RootVerdict.Kept;
Expand Down Expand Up @@ -626,6 +628,66 @@ private static bool IsExactAndAlgebraic(Entity expression)
_ => false,
};

/// <summary>
/// Whether a root found in decimals leaves a residual large beside the terms of
/// <paramref name="equation"/> as it is written in <paramref name="x"/>, each term's size its value at the root.
/// </summary>
/// <remarks>
/// <see cref="LargestTerm"/> reads the equation after the root is put in, where every part is a number, so it
/// multiplies out a sum that is a factor of a term, or the numerator of one, though that sum depends on the
/// root and cancels there. tanh is written with exponentials, and <c>10^3 tanh(x / 10^3) = x / 2</c> is
/// <c>10^3 (e^(2x / 10^3) - 1) / (e^(2x / 10^3) + 1) - x / 2</c>: near 0, <c>e^(2x / 10^3) - 1</c> cancels, and was
/// read as terms of 500, beside which the residual 5.5e-14 at 1.1e-13 was nothing, so 1.1e-13 was answered
/// beside 0. The terms are 1.1e-13 and 5.5e-14. Here the terms are those of the sum the equation is, each read
/// whole, and that sum is reached through a factor or a divisor that does not depend on <paramref name="x"/>,
/// as the terms of <c>(sin(x) - x) 10^-12</c> are <c>sin(x) 10^-12</c> and <c>x 10^-12</c>. sinh and cosh are
/// written with exponentials too, and <c>10^6 sinh(x / 10^6) = 2x</c> had <c>±4.9e-13</c> beside 0 the same way.
/// https://github.com/asc-community/AngouriMath/issues/2645
/// </remarks>
private static bool IsLargeBesideTheTermsAsWritten(Entity equation, Variable x, Entity root, Entity substituted)
{
if (substituted.Evaled is not Number.Complex residual || !residual.IsFinite)
return false;
var cancels = false;
if (LargestTermAsWritten(equation, x, root, ref cancels) is not { } largest || !cancels)
return false;
return residual.Abs().EDecimal.GreaterThan(largest.Multiply(RelativeResidualTolerance));
}

/// <summary>
/// The largest term of <paramref name="expr"/> at <paramref name="root"/>: the terms are those of the sum the
/// equation is, reached through factors and divisors that do not depend on <paramref name="x"/>, each its value
/// at the root and not split further; <see langword="null"/> where a part has no finite value there.
/// </summary>
private static EDecimal? LargestTermAsWritten(Entity expr, Variable x, Entity root, ref bool cancels)
{
switch (expr)
{
case Sumf or Minusf:
cancels = true;
var largest = EDecimal.Zero;
foreach (var term in Sumf.LinearChildren(expr))
{
if (SizeAt(term, x, root) is not { } size)
return null;
largest = EDecimal.Max(largest, size);
}
return largest;
case Mulf(var multiplier, var multiplicand) when !multiplier.ContainsNode(x):
return SizeAt(multiplier, x, root) is { } factor && LargestTermAsWritten(multiplicand, x, root, ref cancels) is { } rest ? factor.Multiply(rest) : null;
case Mulf(var multiplier, var multiplicand) when !multiplicand.ContainsNode(x):
return SizeAt(multiplicand, x, root) is { } constant && LargestTermAsWritten(multiplier, x, root, ref cancels) is { } other ? constant.Multiply(other) : null;
case Divf(var dividend, var divisor) when !divisor.ContainsNode(x):
return SizeAt(divisor, x, root) is { } below && !below.IsZero && LargestTermAsWritten(dividend, x, root, ref cancels) is { } above
? above.Divide(below, MathS.Settings.DecimalPrecisionContext) : null;
default:
return SizeAt(expr, x, root);
}

static EDecimal? SizeAt(Entity part, Variable x, Entity root)
=> part.Substitute(x, root).Evaled is Number.Complex { IsFinite: true } value ? value.Abs().EDecimal : null;
}

/// <summary>
/// The largest of the terms the equation adds up to at the root, which is the scale
/// the residual has to be read against, and in <paramref name="cancels"/> whether there
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// Copyright (c) 2019-2026 Angouri.
// AngouriMath is licensed under MIT.
// Details: https://github.com/asc-community/AngouriMath/blob/master/LICENSE.md.
// Website: https://am.angouri.org.
//

using System.Linq;
using AngouriMath;
using AngouriMath.Extensions;
using Xunit;
using static AngouriMath.Entity;
using static AngouriMath.Entity.Set;

namespace AngouriMath.Tests.Algebra.SolveTest
{
/// <summary>
/// tanh, sinh and cosh are written with exponentials, and near 0 the parts of e^u - 1 or e^u - e^(-u) cancel. A
/// candidate the numeric search found near 0 was judged against those parts, of size 10^3 and more, and kept:
/// <c>10^3 tanh(x / 10^3) = x / 2</c> was <c>{ 0, 1.1e-13 }</c>, where 1.1e-13 leaves half the size of its terms.
/// https://github.com/asc-community/AngouriMath/issues/2645
/// </summary>
[Trait("Area", "Algebra")]
public sealed class ARootNearZeroIsJudgedByTheTermsAsWrittenTest
{
// Every real root is a true one: each is 0 or at least the size the equation's terms put it at.
[Theory]
[InlineData("10^3 tanh(x / 10^3) = x / 2", 1e-3)]
[InlineData("10^6 tanh(x / 10^6) = x / 2", 1e-3)]
[InlineData("10^6 tanh(x / 10^6) = x / 3", 1e-3)]
[InlineData("10^20 tanh(x / 10^20) = x / 3", 1e-3)]
[InlineData("10^6 sinh(x / 10^6) = 2 x", 1e-3)]
public void NoRootIsANearCopyOfZero(string equation, double smallestTrueRoot)
{
var roots = Assert.IsType<FiniteSet>(equation.ToEntity().Solve("x"))
.Select(root => (Number.Complex)root.Evaled).ToList();
Assert.Contains(roots, root => root.IsZero);
foreach (var root in roots.Where(root => !root.IsZero))
Assert.True(root.Abs().EDecimal.ToDouble() >= smallestTrueRoot, $"{equation}: {root} is a near copy of 0");
}

// The roots found at unit size are kept, those near 0 among them.
[Theory]
[InlineData("tanh(x) = x / 2", 1.915008048154537)]
[InlineData("sinh(x) = 2 x", 2.177318984965310)]
[InlineData("sin(x) = x / 2", 1.895494267033981)]
[InlineData("(sin(x) - x / 2) * 10^(-12) = 0", 1.895494267033981)]
public void AUnitSizeRootIsKept(string equation, double positive)
{
var reals = Assert.IsType<FiniteSet>(equation.ToEntity().Solve("x"))
.Select(root => (Number.Complex)root.Evaled).Where(root => root.ImaginaryPart.IsZero)
.Select(root => root.RealPart.EDecimal.ToDouble()).ToList();
foreach (var expected in new[] { -positive, 0, positive })
Assert.Contains(reals, root => System.Math.Abs(root - expected) < 1e-9);
}
}
}
Loading