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
16 changes: 16 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,22 @@ rational function with a linear and an irreducible quadratic factor below the ba
| `ln(x + 1) + (-1/2 * ln(x^2 + 1) + arctan(x))`, limit at `+oo` | unevaluated | `pi / 2` |
| `ln(x) + (exp(x) - ln(sin(x)))`, limit at 0 from the right | unevaluated | `1` |

### A system that rounding reached is declined, not answered in part

**Not silent: an incomplete answer is now no answer.** `x y = 1, x + y = 10^45 + 7` was answered with one of its two
rows. In the other, `y` came out of the back-substitution as `10^45` exactly: rounding reached the exact solve through
downcasting, the row left `-7` against terms near `10^45`, and it was dropped as no solution. A row whose exact residual
is not zero but is no more than a part in `10^12` of its terms is now read as rounding having reached it, and the system
is declined. A residual of rationals alone is decided exactly rather than to the working digits
([#2635](https://github.com/asc-community/AngouriMath/issues/2635),
[#2634](https://github.com/asc-community/AngouriMath/issues/2634)).

| `MathS.Equations(...).Solve("x", "y")` | Was (2.10.0) | Now |
|---|---|---|
| `x y = 1, x + y = 10^45 + 7` | 1 row of 2 | `null` |
| `x y = -3, x + y = 10^45 + 7` | 1 row of 2 | `null` |
| `x y = 1, x + y = 10^20 + 7` | 2 rows | unchanged |

### A trigonometric root at a rational multiple of pi is one multiple, written once

**Silent, in the printed form only, and in how many members a family lists.** The inversion of `sin` and `cos` wrote a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,14 @@ internal enum ResidualWeight
/// </remarks>
internal static ResidualWeight Weigh(Entity substituted)
{
// A residual of rational numbers alone is decided exactly. Evaluated, a residual of 3e-217 is zero to the
// working digits, and an enclosure to them holds zero: y^2 - 3 = 0, x^2 + (P - Q y) x - 1 = 0 at the 190th
// power of 2 + sqrt(3) was answered with the rows x = -668385287/2 and y a rational within 1e-217 of
// sqrt(3), which hold every equation to every digit asked and are no solution.
// https://github.com/asc-community/AngouriMath/issues/2634
if (AsAnExactRational(substituted) is { } exactly)
return exactly.IsZero ? ResidualWeight.None
: IsSmallBesideTheTerms(substituted, exactly.Abs().EDecimal) ? ResidualWeight.CannotTell : ResidualWeight.Against;
if (!IsEvidenceAgainst(substituted))
return ResidualWeight.None;
if (!IsExactAndAlgebraic(substituted))
Expand Down Expand Up @@ -549,11 +557,50 @@ private static bool IsEvidenceAgainstWithMoreDigits(Entity substituted)
/// largest term or of one, the share being ten to the minus half the working digits.
/// </summary>
private static bool IsWithinRounding(Entity substituted, Numerics.PreciseComplexInterval enclosure)
=> IsSmallBesideTheTerms(substituted, EDecimal.Max(enclosure.Re.Magnitude, enclosure.Im.Magnitude));

/// <summary>
/// Whether a residual of size <paramref name="size"/>, exact and not zero, is no more than
/// <see cref="RoundingReach"/> of the largest term of <paramref name="substituted"/>, or than the share
/// <see cref="IsEvidenceAgainst"/> takes rounding to leave where that is larger.
/// </summary>
private static bool IsSmallBesideTheTerms(Entity substituted, EDecimal size)
{
var share = EDecimal.Create(EInteger.One, MathS.Settings.DecimalPrecisionContext.Value.Precision.Divide(2).Negate());
var unused = false;
var bound = EDecimal.Max(LargestTerm(substituted, ref unused), share).Multiply(share);
return EDecimal.Max(enclosure.Re.Magnitude, enclosure.Im.Magnitude).CompareTo(bound) <= 0;
var largest = LargestTerm(substituted, ref unused);
var bound = EDecimal.Max(EDecimal.Max(largest, share).Multiply(share), largest.Multiply(RoundingReach));
return size.CompareTo(bound) <= 0;
}

/// <summary>
/// How small beside its largest term an exact residual that is not zero is read as rounding having reached
/// the root or the row, rather than as its being none: a part in 10^12. Rounding reaches an exact solve where
/// downcasting rounds a number it built onto a nearby rational, and that rational is near the right value, so
/// the residual is a small part of the terms; a root that a rewrite added leaves a residual of their size.
/// <c>x y = 1, x + y = 10^45 + 7</c> was answered with one row of two: the other's <c>y</c> came out as
/// <c>10^45</c> exactly, and left <c>-7</c> against terms near <c>10^45</c>, read as no solution.
/// https://github.com/asc-community/AngouriMath/issues/2635
/// </summary>
[ConstantField] private static readonly EDecimal RoundingReach = EDecimal.FromString("1E-12");

/// <summary>
/// <paramref name="expression"/> as the rational it is, where it is rational numbers in sums, products, quotients
/// and whole powers alone, and so is worked out exactly; <see langword="null"/> elsewhere.
/// </summary>
private static Rational? AsAnExactRational(Entity expression)
{
static bool IsRationalArithmetic(Entity e) => e switch
{
Rational => true,
Sumf(var a, var b) => IsRationalArithmetic(a) && IsRationalArithmetic(b),
Minusf(var a, var b) => IsRationalArithmetic(a) && IsRationalArithmetic(b),
Mulf(var a, var b) => IsRationalArithmetic(a) && IsRationalArithmetic(b),
Divf(var a, var b) => IsRationalArithmetic(a) && IsRationalArithmetic(b),
Powf(var a, Integer) => IsRationalArithmetic(a),
_ => false,
};
return IsRationalArithmetic(expression) && expression.InnerSimplified is Rational value ? value : null;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// 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;

namespace AngouriMath.Tests.Algebra.SolveTest
{
/// <summary>
/// Where rounding reached an exact system solve, a row came out a little off. A row off by a residual of rationals
/// that every digit evaluated reads as zero was answered, and a row off by a small part of its terms was dropped as
/// none. Each must be answered as all of the rows or none.
/// https://github.com/asc-community/AngouriMath/issues/2634
/// https://github.com/asc-community/AngouriMath/issues/2635
/// </summary>
[Trait("Area", "Algebra")]
public sealed class ARoundedRowIsDeclinedNotAnsweredTest
{
private static void EveryRowHolds(Entity[] equations, Matrix solutions, int count)
{
Assert.Equal(count, solutions.RowCount);
for (var row = 0; row < solutions.RowCount; row++)
foreach (var equation in equations)
{
var residual = equation.Substitute("x", solutions[row, 0]).Substitute("y", solutions[row, 1]);
var enclosure = AngouriMath.Numerics.PreciseEvaluation.Of(residual, 400);
Assert.True(enclosure is { } value
&& value.Re.Magnitude.CompareTo(PeterO.Numbers.EDecimal.FromString("1E-60")) <= 0
&& value.Im.Magnitude.CompareTo(PeterO.Numbers.EDecimal.FromString("1E-60")) <= 0,
$"row {row} leaves {residual.Evaled} in {equation}");
// A row of rationals is checked exactly: within 1e-217 of a solution is not one.
if (solutions[row, 0] is Number.Rational && solutions[row, 1] is Number.Rational)
Assert.Equal(Number.Integer.Zero, residual.InnerSimplified);
}
}

[Theory]
[InlineData(3, 2, 1, 190, "1", "-")]
[InlineData(3, 2, 1, 190, "3", "+")]
[InlineData(2, 1, 1, 150, "1", "-")]
[InlineData(2, 1, 1, 200, "1", "+")]
public void APellSystemAtAHighPower(int d, int u, int w, int n, string c, string sign)
{
System.Numerics.BigInteger p = 1, q = 0;
for (var i = 0; i < n; i++)
(p, q) = (p * u + q * w * d, p * w + q * u);
var equations = new[] { $"y^2 - {d}".ToEntity(), $"x^2 + ({p} {sign} {q} * y) * x - {c}".ToEntity() };
if (MathS.Equations(equations).Solve("x", "y") is { } solutions)
EveryRowHolds(equations, solutions, 4);
}

[Theory]
[InlineData(45, "1")]
[InlineData(45, "-3")]
[InlineData(60, "2")]
public void ASumAndAProduct(int digits, string c)
{
var s = System.Numerics.BigInteger.Pow(10, digits) + 7;
var equations = new[] { $"x * y - ({c})".ToEntity(), $"x + y - {s}".ToEntity() };
if (MathS.Equations(equations).Solve("x", "y") is { } solutions)
EveryRowHolds(equations, solutions, 2);
}

// Still answered where nothing rounds.
[Theory]
[InlineData(10, "1")]
[InlineData(20, "-3")]
public void ASumAndAProductOfModestSize(int digits, string c)
{
var s = System.Numerics.BigInteger.Pow(10, digits) + 7;
var equations = new[] { $"x * y - ({c})".ToEntity(), $"x + y - {s}".ToEntity() };
var solutions = Assert.IsAssignableFrom<Matrix>(MathS.Equations(equations).Solve("x", "y"));
EveryRowHolds(equations, solutions, 2);
}
}
}
Loading