Repository navigation
The factorial, gamma and the totient compile under Compile<TIn, TOut> - #1620
Merged
Merged
Conversation
Compile<TIn, TOut> had no form for x!, gamma or phi, and Compile's own had defects. Both compilers now call one routine in SpecialFunctions. The factorial is exact at a whole number up to 170, rounded once to a double, and infinite past it; NaN at a negative whole number, one of the gamma function's poles, as in the interpreter; and elsewhere the Lanczos approximation Compile always used (g = 7, nine terms), taken in logarithms, the reflection formula included. Compile's overflowed its own power before the value did, so from just past 141!, about 2e243, it compiled to NaN, up to 170!. At a whole number it was a rounding off the integer, 23.999999999999996 at 4, which a compilation over integers truncates to 23. Off the real line the reflection's sin(pi z) overflowed where the value is tiny; past |Im(pi z)| = 20 it is a logarithm written out now. gamma(x) is (x - 1)!. The totient is the totient at a whole number, 0 at one that is not positive, and NaN at any other number, as the interpreter's is. Compile truncated a fraction to the whole number below it, so phi(2.5) was phi(2). Under Compile<TIn, TOut>, long, int and BigInteger take it exactly. Against mpmath the compiled factorial is within 1.1e-13 of the value on a grid of [-14, 14]^2, 2.4e-13 up the imaginary axis to 10^4.5 and 3.2e-13 on circles out to radius 300. It is further out only where the conditioning forces it: next to a pole, and in the subnormal range past -177. SpecialFunctionCompilationTest gains the factorial and gamma against the interpreter, through both compilers and over doubles; whole numbers exact, including under Compile<long, long>; 171 infinite and the poles NaN; and the totient at whole numbers over every integer type, NaN elsewhere. UserInvalidExceptions keeps binomial, gcd, lcm and prime as the nodes with no compiled form. Closes #1607. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The other half of #1607:
x!,gammaandphiunderCompile<TIn, TOut>, which had no form for them.Compilehad one, and it had defects of its own.The factorial. Both compilers now call one routine in
SpecialFunctions:Compilehas always used (g = 7, nine terms), taken in logarithms, the reflection formula included.Compile's factorial overflowed its own power before the value did. From just past141!, about2e243, it compiled to NaN, up to170!. At a whole number it was a rounding off the integer,23.999999999999996at 4, which a compilation over integers would truncate to 23. Off the real line the reflection'ssin(pi z)overflowed where the value is tiny, and gave NaN; it is now a logarithm written out past|Im(pi z)| = 20.gamma(x)is(x - 1)!and needs nothing of its own.The totient. At a whole number it is the totient, and 0 at one that is not positive, as the interpreter has it. At any other number it is NaN, as the interpreter's is.
Compiletruncated a fraction to the whole number below it, sophi(2.5)wasphi(2), which is 1. UnderCompile<TIn, TOut>,long,intandBigIntegertake it exactly.Measured against mpmath, the compiled factorial is within 1.1e-13 of the value on a grid of
[-14, 14]^2, within 2.4e-13 up the imaginary axis to 10^4.5, and within 3.2e-13 on circles out to radius 300. The error is larger only where the conditioning forces it: next to a pole, and in the subnormal range past -177, where a double holds few digits of the value.Measured
SpecialFunctionCompilationTestgains:Compile<long, long>;UserInvalidExceptions:phi,gammaand the factorials compile now. The theory of nodes with no compiled form isbinomial,gcd,lcmandprime.BREAKING-CHANGES.md: each changed answer, as measured on 2.5.0.RunEasy,RunMedium,RunHard) still allocate nothing and take the same time.Closes #1607.
🤖 Generated with Claude Code