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
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,13 @@ internal static Set Solve(Entity equation, Variable x)
for (var index = 0; index < variables.Length; index++)
replacements[variables[index]] = row[index];
var residuals = equations.Select(equation => equation.Substitute(replacements)).ToList();
if (residuals.Any(residual => !residual.Vars.Any()
&& (residual.Evaled is Complex { IsFinite: false } || StatementSolver.IsEvidenceAgainstUnlessEnclosed(residual))))
var weights = residuals.Select(residual => residual.Vars.Any() ? StatementSolver.ResidualWeight.None : StatementSolver.Weigh(residual)).ToList();
// A residual rounding reached says nothing of the row either way, and the row may stand for a
// solution that was lost: https://github.com/asc-community/AngouriMath/issues/2627
if (weights.Contains(StatementSolver.ResidualWeight.CannotTell))
return null;
if (residuals.Select((residual, index) => !residual.Vars.Any()
&& (residual.Evaled is Complex { IsFinite: false } || weights[index] is StatementSolver.ResidualWeight.Against)).Any(dropped => dropped))
continue;
if (residuals.Any(residual => residual.Vars.Any() && StatementSolver.IsEvidenceAgainst(AtASamplePoint(residual, own))))
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,14 @@ private static RootVerdict Verdict(Entity equation, Variable x, Entity root)
var substituted = equation.Substitute(x, root);
if (IsUndefinedExactly(substituted))
return RootVerdict.Spurious;
if (IsEvidenceAgainstUnlessEnclosed(substituted))
return RootVerdict.Spurious;
switch (Weigh(substituted))
{
case ResidualWeight.Against:
return RootVerdict.Spurious;
// https://github.com/asc-community/AngouriMath/issues/2627
case ResidualWeight.CannotTell:
return RootVerdict.ShownNotARoot;
}
return !substituted.Vars.Any() && IsAboveTheIntervalFloor(equation, x, root) && IsShownNotZero(substituted)
? RootVerdict.ShownNotARoot
: RootVerdict.Kept;
Expand Down Expand Up @@ -275,20 +281,24 @@ internal static bool IsExact(Entity entity)
/// <c>(1 + sqrt(2))^140</c> in the coefficients the terms are near <c>1e107</c>, and the
/// residual <c>-2</c> is inside an interval of a hundred digits.
/// </remarks>
internal static bool IsShownNotZero(Entity residual) => ExcludesZero(residual) is true;
internal static bool IsShownNotZero(Entity residual) => ExcludesZero(residual, out _) is true;

/// <summary>
/// Whether an interval enclosing <paramref name="residual"/> leaves out zero, as
/// <see cref="IsShownNotZero"/> works it out; <see langword="false"/> where it holds zero,
/// and <see langword="null"/> where no interval can be worked out.
/// </summary>
private static bool? ExcludesZero(Entity residual)
private static bool? ExcludesZero(Entity residual, out Numerics.PreciseComplexInterval? excluding)
{
excluding = null;
var digits = IntervalDigits();
if (Numerics.PreciseEvaluation.Of(residual, digits) is not { } enclosure)
return null;
if (Excludes(enclosure))
{
excluding = enclosure;
return true;
}
var width = PeterO.Numbers.EDecimal.Max(
enclosure.Re.High.Subtract(enclosure.Re.Low), enclosure.Im.High.Subtract(enclosure.Im.Low));
if (width.IsZero)
Expand All @@ -302,7 +312,10 @@ internal static bool IsExact(Entity entity)
return false;
if (!more.CanFitInInt32() || more.ToInt32Unchecked() > MostMoreDigits)
more = MostMoreDigits;
return Numerics.PreciseEvaluation.Of(residual, digits + more.ToInt32Unchecked()) is { } finer && Excludes(finer);
if (Numerics.PreciseEvaluation.Of(residual, digits + more.ToInt32Unchecked()) is not { } finer || !Excludes(finer))
return false;
excluding = finer;
return true;

static bool Excludes(Numerics.PreciseComplexInterval enclosure)
=> enclosure.Re.Mignitude.Sign > 0 || enclosure.Im.Mignitude.Sign > 0;
Expand Down Expand Up @@ -467,7 +480,81 @@ internal static bool IsEvidenceAgainst(Entity substituted)
/// <a href="https://github.com/asc-community/AngouriMath/issues/2448">#2448</a>
/// </remarks>
internal static bool IsEvidenceAgainstUnlessEnclosed(Entity substituted)
=> IsEvidenceAgainst(substituted) && !(IsExactAndAlgebraic(substituted) && ExcludesZero(substituted) is false);
=> Weigh(substituted) is ResidualWeight.Against;

/// <summary>What a residual says of the root or the row put in.</summary>
internal enum ResidualWeight
{
/// <summary>Nothing against it.</summary>
None,
/// <summary>It is no solution.</summary>
Against,
/// <summary>Rounding reached it, and whether it is one cannot be told.</summary>
CannotTell,
}

/// <summary>
/// <see cref="IsEvidenceAgainst"/>, weighed against an interval where <paramref name="substituted"/> is exact and
/// algebraic. An enclosure that holds zero says the residual is what rounding left, and is no evidence. One that
/// leaves zero out but is no further from it than rounding leaves, or none at all where twice the digits evaluate
/// the residual to something else, says rounding reached what found the root or the row: whether it is one cannot
/// be told, and the caller declines, since dropping it may lose a solution and keeping it may keep a point that is
/// none.
/// </summary>
/// <remarks>
/// In <c>y^2 - 2, x^2 + (P - Q y) x - 3</c>, with <c>(1 + sqrt(2))^n = P + Q sqrt(2)</c>, a system solved for
/// <c>y</c> from the second equation goes through <c>((x^2 + P x - 3)/(Q x))^2 = 2</c>, a quartic in <c>x</c> with
/// coefficients near <c>Q^2</c>. At <c>n = 90</c> its small root, exact, left <c>-0.042</c> against terms of 3 at a
/// hundred digits, with no enclosure, and something else at two hundred; at <c>n = 70</c>, merged with a
/// change that kept <c>P + Q sqrt(2)</c> exact, the row of that root left <c>-7.2e-21</c> where its enclosure is
/// near <c>6e-53</c>, exact and not zero. Each was dropped as no solution, and three rows of four were answered.
/// https://github.com/asc-community/AngouriMath/issues/2627
/// </remarks>
internal static ResidualWeight Weigh(Entity substituted)
{
if (!IsEvidenceAgainst(substituted))
return ResidualWeight.None;
if (!IsExactAndAlgebraic(substituted))
return ResidualWeight.Against;
return ExcludesZero(substituted, out var excluding) switch
{
false => ResidualWeight.None,
true => IsWithinRounding(substituted, excluding!.Value) ? ResidualWeight.CannotTell : ResidualWeight.Against,
// No interval: an enclosure divides by one that holds zero where the terms cancel past the digits it
// is worked to. A residual that twice the digits evaluate to about the same is a whole quantity; one
// they move is what rounding left.
null => IsEvidenceAgainstWithMoreDigits(substituted) ? ResidualWeight.Against : ResidualWeight.CannotTell,
};
}

/// <summary>
/// Whether <paramref name="substituted"/> evaluates to about the same residual with twice the working digits,
/// to within a half of it: rounding's residual moves with the digits, and a whole quantity does not.
/// </summary>
private static bool IsEvidenceAgainstWithMoreDigits(Entity substituted)
{
var context = MathS.Settings.DecimalPrecisionContext.Value;
if (context.Precision.IsZero || !context.Precision.CanFitInInt32() || substituted.Evaled is not Number.Complex atTheDigits)
return true;
Entity atMoreDigits;
using (MathS.Settings.DecimalPrecisionContext.Set(context.WithPrecision(2 * context.Precision.ToInt32Unchecked())))
atMoreDigits = substituted.Evaled;
return atMoreDigits is Number.Complex more && more.IsFinite
&& ((Number.Complex)(more - atTheDigits)).Abs().EDecimal.CompareTo(atTheDigits.Abs().EDecimal.Divide(2)) <= 0;
}

/// <summary>
/// Whether <paramref name="enclosure"/> of <paramref name="substituted"/>, exact and algebraic, is no
/// further from zero than <see cref="IsEvidenceAgainst"/> takes rounding to leave: a share of the
/// 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)
{
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;
}

/// <summary>
/// Whether <paramref name="expression"/> is exact numbers in sums, products, quotients and
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// 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 AngouriMath;
using AngouriMath.Extensions;
using Xunit;

namespace AngouriMath.Tests.Algebra.SolveTest
{
/// <summary>
/// <c>y^2 - 2 = 0, x^2 + (P - Q y) x - c = 0</c>, with <c>(1 + sqrt(2))^n = P + Q sqrt(2)</c>, has four solutions. Solved
/// for <c>y</c> from the second equation, the small root at <c>y = -sqrt(2)</c> is a root of
/// <c>((x^2 + P x - c)/(Q x))^2 = 2</c> near <c>c / (2P)</c>, which a hundred digits evaluate to a residual rounding made,
/// with no interval to say so; it was dropped as no root, and three rows were answered. The answer must be all four or
/// none. https://github.com/asc-community/AngouriMath/issues/2627
/// </summary>
[Trait("Area", "Algebra")]
public sealed class ARowIsNotDroppedOnARoundedResidualTest
{
[Theory]
[InlineData(70, 3)]
[InlineData(80, 5)]
[InlineData(90, 2)]
[InlineData(90, 5)]
[InlineData(100, 2)]
[InlineData(120, 2)]
[InlineData(120, 3)]
[InlineData(120, 5)]
public void IsAnsweredWithEveryRowOrNone(int n, int c)
{
System.Numerics.BigInteger p = 1, q = 0;
for (var i = 0; i < n; i++)
(p, q) = (p + 2 * q, p + q);
var equations = new[] { "y^2 - 2".ToEntity(), $"x^2 + ({p} - {q} * y) * x - {c}".ToEntity() };
var solutions = MathS.Equations(equations).Solve("x", "y");
if (solutions is null)
return;
Assert.Equal(4, 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, 300);
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}");
}
}
}
}
Loading