Skip to content

Commit dc07fd1

Browse files
Rafael-SOWNetclaude
andcommitted
Read finiteness from a number's parts where every evaluation passes
IsFinite is a cache made on an entity's first read of it, and each number an evaluation or a power produces is a new entity. Asked of every exact value in the exact-evaluation shortcut (the guard #2347 added) and of every power built (the check this branch added), it cost about 250 KB and 237 KB on each solve of the kernel benchmark's SolveHard, +5.1% against the gate's baseline. The parts answer the same question without the cache, a rational part being finite as it is. The size bound #2347 added is computed in longs as well; it measured free as EIntegers, but it is asked of every exact power. SolveHard allocates 9,549,209 B, against 9,546,434 B before #2347 and 10,027,870 B before this commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent bd6baab commit dc07fd1

2 files changed

Lines changed: 26 additions & 15 deletions

File tree

‎Sources/AngouriMath/Core/Entity/Continuous/Number/Operators.cs‎

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ static EDecimal ModifiedMultiply(EDecimal a, EDecimal b) =>
158158
}
159159
);
160160

161+
/// <summary>
162+
/// Whether both parts are finite, read from the parts: <see cref="Entity.IsFinite"/> is a cache made on
163+
/// an entity's first read of it, and asked of every power built, and of every exact value an evaluation
164+
/// takes, it was half a megabyte on each solve of the kernel benchmark's SolveHard. A rational part is
165+
/// finite and is not written out to a decimal to say so.
166+
/// </summary>
167+
internal static bool PartsAreFinite(Complex number) => PartIsFinite(number.RealPart) && PartIsFinite(number.ImaginaryPart);
168+
169+
private static bool PartIsFinite(Real part) => part is Rational || part.EDecimal.IsFinite;
170+
161171
/// <summary>
162172
/// The product of two finite numbers a part of which leaves the context's exponent range. Within it,
163173
/// <c>(1e600 + 1e600 i)^2</c> had <c>1e1200 - 1e1200</c> for its real part, which is <c>oo - oo</c> there,
@@ -636,7 +646,7 @@ static Complex BinaryIntPow(Complex num, EInteger val)
636646
return Real.CreateAtScale(pow.Sign < 0 ? EDecimal.One.Divide(raised, wholeContext) : raised, EDecimal.Zero);
637647
}
638648
var built = BinaryIntPow(@base, pow);
639-
if (built.IsFinite || @base is Real)
649+
if (@base is Real || PartsAreFinite(built))
640650
return built;
641651
// A finite complex base whose power leaves the exponent range on the way: each squaring past
642652
// it was oo - oo in a part, and (3 + 4i)^2000 and (1 + i)^(10^6) were NaN. The squarings are
@@ -746,7 +756,7 @@ private static Complex ByThePolarForm(EDecimal baseReal, EDecimal baseImaginary,
746756
/// The most bits an exact power is built to: <c>3^(10^7)</c>, at 15.8 million, takes three seconds,
747757
/// and each doubling of the exponent doubles the size and more than doubles the time.
748758
/// </summary>
749-
[ConstantField] private static readonly EInteger MaxExactPowerBits = EInteger.FromInt32(1 << 24);
759+
private const long MaxExactPowerBits = 1 << 24;
750760

751761
/// <summary>
752762
/// About how many bits an exact <paramref name="base"/> to the whole <paramref name="power"/> has: for
@@ -755,20 +765,21 @@ private static Complex ByThePolarForm(EDecimal baseReal, EDecimal baseImaginary,
755765
/// and a power of one is as small as it is. A base that is not exact is 0, being a decimal.
756766
/// </summary>
757767
internal static bool IsTooLargeToBuild(Complex @base, EInteger power)
758-
=> ExactPowerBits(@base, power).CompareTo(MaxExactPowerBits) > 0;
759-
760-
private static EInteger ExactPowerBits(Complex @base, EInteger power)
761768
{
769+
// In longs: the bound is asked of every exact power, and as EIntegers it was a quarter of a
770+
// megabyte on each solve of the kernel benchmark's SolveHard.
762771
if (@base.RealPart is not Rational { ERational: var re } || @base.ImaginaryPart is not Rational { ERational: var im })
763-
return EInteger.Zero;
764-
static EInteger Bits(ERational part)
765-
=> part.IsZero ? EInteger.Zero
766-
: part.Numerator.Abs().GetUnsignedBitLengthAsEInteger().Subtract(1)
767-
.Add(part.Denominator.GetUnsignedBitLengthAsEInteger().Subtract(1));
768-
var bits = Bits(re).CompareTo(Bits(im)) >= 0 ? Bits(re) : Bits(im);
769-
if (!re.IsZero && !im.IsZero)
770-
bits = bits.Add(1);
771-
return bits.Multiply(power.Abs());
772+
return false;
773+
static long Bits(ERational part)
774+
=> part.IsZero ? 0 : part.Numerator.GetUnsignedBitLengthAsInt64() - 1 + part.Denominator.GetUnsignedBitLengthAsInt64() - 1;
775+
var bits = Math.Max(Bits(re), Bits(im)) + (!re.IsZero && !im.IsZero ? 1 : 0);
776+
if (bits == 0)
777+
return false;
778+
var powerBits = power.GetUnsignedBitLengthAsInt64();
779+
if (powerBits > 40)
780+
return true;
781+
var magnitude = power.Sign < 0 ? -power.ToInt64Unchecked() : power.ToInt64Unchecked();
782+
return magnitude > MaxExactPowerBits / bits;
772783
}
773784

774785
/// <summary>e.g. Log(2, 32) = 5</summary>

‎Sources/AngouriMath/Functions/Evaluation/Evaluation.Classes.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private static bool IsAZeroOnlyRoundingMade(Entity node, Entity value)
6565
/// decimal's <c>+oo</c>, as it was. Asked only of a value that is not finite.
6666
/// </summary>
6767
private static bool IsAnInfinityOnlyRoundingMade(Entity node, Entity value)
68-
=> value is Complex { IsFinite: false } && !value.IsNaN && node.Nodes.Any(IsFiniteRoundedOntoAnInfinity);
68+
=> value is Complex number && !Number.PartsAreFinite(number) && !value.IsNaN && node.Nodes.Any(IsFiniteRoundedOntoAnInfinity);
6969

7070
private static bool IsFiniteRoundedOntoAnInfinity(Entity part)
7171
=> part is Sumf or Minusf or Mulf or Powf

0 commit comments

Comments
 (0)