Skip to content

The number-theoretic functions compile - #2217

Merged
Rafael-SOWNet merged 1 commit into
masterfrom
the-number-theoretic-functions-compile
Oct 30, 2026
Merged

Rafael-SOWNet merged 1 commit into
masterfrom
the-number-theoretic-functions-compile

Conversation

@Rafael-SOWNet

Copy link
Copy Markdown
Member

Part of #2195, item 3.

mobius, divisorsigma, primenu, primeomega, valuation, prime and primepi compile in FastExpression and in the LINQ compiler, over Complex and over double. prime and valuation were refused with UncompilableNodeException. The other five are new since 2.6.0.

Each uses the routine the interpreter uses:

function routine
mobius, primenu, primeomega, divisorsigma Primes.Factorization
valuation (to check that the base is prime) IsPrime
prime GetPrime
primepi CountUpTo

So a compiled value is exact at a whole argument and NaN at any other, as evaluation gives. Two inputs the interpreter leaves as written are NaN when compiled: a number too large to factorise, and a prime past the millionth.

2.6.0 now
"prime(x)".ToEntity().Compile("x").Call(10) UncompilableNodeException (29, 0)
"valuation(x, 2)".ToEntity().Compile("x").Call(12) UncompilableNodeException (2, 0)

NumberTheoreticCompilationTest compares both compilers with evaluation:

  • each function of a factorisation at 68 arguments, among them 2^40, 600851475143, 5/2 and 1 + i;
  • the divisor sigma at six orders, among them -1, 1/2 and i;
  • the valuation at seven numbers and six bases, two of the bases not prime;
  • the n-th prime up to the 10000th;
  • the prime count up to 100000.

gcd and lcm are not in this PR. Evaluation takes them over the rationals, and every finite double is a rational, so a compiled gcd at 0.1 would be exact for the double nearest 0.1 and meaningless for 0.1. Whether they should compile only at whole numbers, or stay refused, is a separate decision.

🤖 Generated with Claude Code

mobius, divisorsigma, primenu, primeomega, valuation, prime and primepi
compile in FastExpression and in the LINQ compiler, over Complex and over
double. prime and valuation were refused with UncompilableNodeException;
the other five are new since 2.6.0.

Each is computed by the routines the interpreter uses: Primes.Factorization
for the functions of a factorisation, IsPrime for the valuation's prime,
GetPrime for the n-th prime and CountUpTo for the prime count. So a compiled
value is exact at a whole argument and NaN at any other, as evaluation gives,
and a number too large to factorise or a prime past the millionth is NaN
where the interpreter leaves it as written.

The test compares both compilers with evaluation at 68 arguments for each
function of a factorisation, including 1099511627776 = 2^40, 600851475143
and the non-whole 5/2 and 1 + i; the divisor sigma at six orders, among them
-1, 1/2 and i; the valuation at seven numbers and six bases, two of them not
prime; the n-th prime up to the 10000th; and the prime count up to 100000.

prime(x) at 10: UncompilableNodeException (2.6.0), 29 now.
valuation(x, 2) at 12: UncompilableNodeException (2.6.0), 2 now.

Part of #2195.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Rafael-SOWNet Rafael-SOWNet moved this from Todo to In review in AngouriMath Oct 30, 2026
@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.02326% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.64%. Comparing base (8f410b8) to head (c32ee42).

Files with missing lines Patch % Lines
...gouriMath/Functions/Compilation/IntoFE/Compiler.cs 76.66% 7 Missing ⚠️
...s/AngouriMath/Numerics/NumberTheoreticFunctions.cs 95.65% 0 Missing and 2 partials ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2217      +/-   ##
==========================================
+ Coverage   86.62%   86.64%   +0.01%     
==========================================
  Files         305      306       +1     
  Lines       67365    67494     +129     
  Branches    19550    19566      +16     
==========================================
+ Hits        58357    58480     +123     
- Misses       4897     4901       +4     
- Partials     4111     4113       +2     
Flag Coverage Δ
cpp 37.77% <ø> (ø)
csharp 87.19% <93.02%> (+0.01%) ⬆️
fsharp 32.97% <ø> (ø)
interactive 40.72% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ath/Functions/Compilation/IntoFE/FastExpression.cs 96.64% <100.00%> (+0.28%) ⬆️
...ctions/Compilation/IntoLinq/CompilationProtocol.cs 90.08% <100.00%> (-0.83%) ⬇️
...h/Functions/Compilation/IntoLinq/MathAllMethods.cs 73.46% <100.00%> (+2.04%) ⬆️
...s/AngouriMath/Numerics/NumberTheoreticFunctions.cs 95.65% <95.65%> (ø)
...gouriMath/Functions/Compilation/IntoFE/Compiler.cs 85.31% <76.66%> (-1.09%) ⬇️

... and 9 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Rafael-SOWNet
Rafael-SOWNet merged commit b21f383 into master Oct 30, 2026
36 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in AngouriMath Oct 30, 2026
@Rafael-SOWNet
Rafael-SOWNet deleted the the-number-theoretic-functions-compile branch October 30, 2026 02:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant