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
15 changes: 15 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,21 @@ carried it once combined: a second derivative of `1/sqrt(x^2 + 13)` stated `not
| `"(a provided b > 0) + (c provided b > 0)".ToEntity().InnerSimplified` | `a + c provided b > 0 and b > 0` | `a + c provided b > 0` |
| `MathS.Equations("sqrt(y) - x", "y - a").Solve("x", "y")` | `[[sqrt(a), (sqrt(a)) ^ 2], [-sqrt(a), (-sqrt(a)) ^ 2]]` | `[[sqrt(a) provided C(sqrt(a)), (sqrt(a)) ^ 2 provided C(sqrt(a))], [-sqrt(a) provided C(-sqrt(a)), (-sqrt(a)) ^ 2 provided C(-sqrt(a))]]`, `C(v)` being `v = 0 or -1/2 * pi < arg(v) and arg(v) <= 1/2 * pi` |

### A root in exact numbers is put back to the working precision

**Silent: a number that is not a root, in exact numbers, is no longer answered as one.** Every root is put back into the
equation, and one was dropped only where it left more than a hundredth of the largest term. That bound is for roots
found numerically, which are only as accurate as the search. An exact root in an equation of exact numbers is evaluated
to `MathS.Settings.DecimalPrecisionContext`'s digits, so a true root leaves only rounding, and it is now held to half of
those digits. `(3x - 4) sqrt(x) + 3 + 3x - 4x^2 = 0` is solved by squaring, and one of the roots of the quartic that
gives, 1.3233 in radicals, leaves 0.069 against a largest term of 7. A root with a function, a constant like `pi`, or a
decimal in it is still held to a hundredth. No issue was filed; a check of generated equations in `sqrt(x)` beside
powers of `x` found it.

| Input | Was (2.6.0) | Now |
|---|---|---|
| `"(3x - 4) * sqrt(x) + 3 + 3x - 4x^2 = 0".Solve("x")` | four roots, one of them 1.3233…, which is not a root | the other three |

### A limit of a logarithm that is not decided is left unevaluated

**Not silent: what overflowed the stack now returns.** A limit of a logarithm whose argument has no limit, as
Expand Down
3 changes: 3 additions & 0 deletions Sources/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ file_header_template=\nCopyright (c) 2019-2026 Angouri.\nAngouriMath is licensed
[Tests/UnitTests/Algebra/SolveTest/ARadicalTimesAPowerTest.cs]
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

[Tests/UnitTests/Algebra/SolveTest/AnExactRootIsCheckedToTheWorkingPrecisionTest.cs]
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

[Tests/UnitTests/Core/FactorialCacheIsThreadSafeTest.cs]
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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ internal static Set UnsolvedWhereIndependenceIsDenied(Set roots, Entity conditio
/// 1e-15, while every spurious one sits between 1.5 and 2. This is a hundredth:
/// twenty times above the worst genuine root and a hundred and fifty times below
/// the mildest spurious one.
///
/// It is a bound for roots found numerically. An equation and a root that are exact numbers
/// are held to the working precision instead, in <see cref="IsEvidenceAgainst"/>.
/// </remarks>
[ConstantField] private static readonly EDecimal RelativeResidualTolerance = EDecimal.FromString("0.01");

Expand Down Expand Up @@ -194,11 +197,49 @@ internal static bool IsEvidenceAgainst(Entity substituted)
if (substituted.Evaled is not Number.Complex residual || !residual.IsFinite)
return false;
var size = residual.Abs().EDecimal;
// Exact numbers in sums, products, quotients and rational powers are evaluated to the
// working precision, so what they leave is either what rounding leaves of zero or a whole
// quantity, and a percent is no bound for it. (3x - 4) sqrt(x) + 3 + 3x - 4x^2 = 0, raised
// to a quartic, has a root of the quartic in radicals at 1.3233 that leaves 0.069 against
// terms of 7, which a percent of them kept.
if (IsExactAndAlgebraic(substituted))
{
var share = EDecimal.Create(EInteger.One, MathS.Settings.DecimalPrecisionContext.Value.Precision.Divide(2).Negate());
// No more than share^2, which a true root usually leaves, is under the bound whatever
// the terms are, so they are not evaluated again for their size: that cost nearly a
// tenth more allocation on solving x^2 + x + 1.
if (size.CompareTo(share.Multiply(share)) <= 0)
return false;
return size.GreaterThan(EDecimal.Max(LargestTerm(substituted), share).Multiply(share));
}
if (size.LessThan(MathS.Settings.PrecisionErrorCommon))
return false;
return size.GreaterThan(LargestTerm(substituted).Multiply(RelativeResidualTolerance));
}

/// <summary>
/// Whether <paramref name="expression"/> is exact numbers in sums, products, quotients and
/// rational powers, which evaluation carries to the working precision. A function, a constant
/// such as <c>pi</c> or a decimal is not: a special function may be evaluated in doubles, and
/// a decimal is only as accurate as what found it.
/// </summary>
/// <remarks>
/// Walked by hand rather than through <see cref="Entity.Nodes"/>: the tree is a fresh
/// substitution, so the lazy enumerators behind that are built anew for every root checked,
/// and they were a percent more allocation on solving <c>x^2 + x + 1</c>.
/// </remarks>
private static bool IsExactAndAlgebraic(Entity expression)
=> expression switch
{
Complex number => number.RealPart is Rational && number.ImaginaryPart is Rational,
Sumf(var augend, var addend) => IsExactAndAlgebraic(augend) && IsExactAndAlgebraic(addend),
Minusf(var minuend, var subtrahend) => IsExactAndAlgebraic(minuend) && IsExactAndAlgebraic(subtrahend),
Mulf(var multiplier, var multiplicand) => IsExactAndAlgebraic(multiplier) && IsExactAndAlgebraic(multiplicand),
Divf(var dividend, var divisor) => IsExactAndAlgebraic(dividend) && IsExactAndAlgebraic(divisor),
Powf(var @base, Rational) => IsExactAndAlgebraic(@base),
_ => false,
};

/// <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. A single term is its own largest, so an
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// 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.Extensions;
using PeterO.Numbers;
using Xunit;
using static AngouriMath.Entity;
using static AngouriMath.Entity.Number;

namespace AngouriMath.Tests.Algebra
{
/// <summary>
/// A root in exact numbers is put back into an equation in exact numbers to the working
/// precision, not to a percent of its largest term. <c>(3x - 4) sqrt(x) + 3 + 3x - 4x^2 = 0</c>,
/// squared, is a quartic whose roots come back in radicals, and one of them, 1.3233, is the
/// quartic's and not the equation's: it leaves 0.069 against a largest term of 7, under the
/// percent that a root found numerically is allowed. Found by a check of generated equations in
/// <c>sqrt(x)</c> beside powers of <c>x</c>; no issue was filed.
/// </summary>
[Trait("Area", "Algebra")]
public sealed class AnExactRootIsCheckedToTheWorkingPrecisionTest
{
[Fact]
public void ARootOfTheSquaredEquationOnlyIsNotAnswered()
{
const string residual = "((-4) + 3x) * sqrt(x) + 3 + 3x - 4x^2";
var roots = Assert.IsType<Set.FiniteSet>($"{residual} = 0".ToEntity().Solve("x"));
Assert.Equal(3, roots.Count);
foreach (var root in roots)
{
var left = Complex.Abs(residual.ToEntity().Substitute("x", root).EvalNumerical()).EDecimal;
Assert.True(left.CompareTo(EDecimal.FromString("1E-30")) < 0, $"{root} leaves {left}");
}
Assert.DoesNotContain(roots, root => root.EvalNumerical() is Real real
&& (real.EDecimal - EDecimal.FromString("1.3233")).Abs().CompareTo(EDecimal.FromString("0.001")) < 0);
}
}
}
Loading