Skip to content

Diophantine reduction for exp, sin, cos, tan in the fixed module - #2845

Merged
fredrik-johansson merged 1 commit into
flintlib:mainfrom
fredrik-johansson:diophantine2
Sep 20, 2026
Merged

fredrik-johansson merged 1 commit into
flintlib:mainfrom
fredrik-johansson:diophantine2

Conversation

@fredrik-johansson

Copy link
Copy Markdown
Collaborator

This reimplements arb's Diophantine argument reduction for elementary functions in the fixed module along with larger lookup tables allowing one to use more primes than arb's default n = 13, currently up to n = 48.

  • fixed_exp_diophantine - exp on [0, 1) by reduction against logarithms of the first n primes.
  • fixed_sin_cos_diophantine, fixed_tan_diophantine - the trigonometric counterpart using Gaussian primes.
  • relation tables (fixed_rel_table) - precomputed for n = 2, 4, 6, 8, 10, 12, 13, 16, 20, 24, 32, 40, 48 in both the rational and Gaussian cases; any other n up to 64 is generated on first use and cached per thread. arb's 13-prime tables are removed in favour of these
  • Machin formulas (fixed_machin_table) - arb's sets, plus new ones for n = 32, 40, 48 of each kind and every intermediate size up to 32, taken from fredrik-johansson/machin. The coefficient matrices are not stored: they are reconstructed on first use by factoring the arguments and inverting the exponent matrix (<1 ms, under 1% of an evaluation at 10⁵ bits and above), which keeps the table file at ~50 KB instead of ~275 KB.
  • arb_log_primes_vec_bsplit / arb_atan_gauss_primes_vec_bsplit now use the shared tables, so they gain the larger sets and the intermediate sizes; their previous hand-written size dispatch and 32-bit-only fallback sets are gone.
  • examples/machin.c additionally verifies some of FLINT's tabulated formulas exactly with ca

Developed using Claude Fable 5.1 and Opus 5.

Baselines

Absolute time to evaluate the elementary functions without any precomputations:

precision exp_notab sin_cos_notab sin_cos_notab / exp_notab
16k bits (256 limbs) 0.0003 s 0.0004 s 1.19
64k bits (1024 limbs) 0.0023 s 0.0027 s 1.19
256k bits (4096 limbs) 0.0147 s 0.0184 s 1.25
1M bits (16384 limbs) 0.0900 s 0.1061 s 1.18
4M bits (65536 limbs) 0.5123 s 0.6352 s 1.24

exp

Timings (precomp / warm) are relative to computing the exponential function with the table-free fixed_exp_notab. So 15.22 / 0.246 means that a first call requires 15x as much time as a table-free evaluation for precomputations, while subsequent evaluations with a warm cache are 4x faster than the table-free evaluation.

precision bitwise diophantine, 13 20 32 48
16k 15.22 / 0.246 2.88 / 0.439 3.92 / 0.398 6.63 / 0.364 10.60 / 0.433
64k 23.57 / 0.297 2.58 / 0.577 3.52 / 0.512 5.41 / 0.453 8.49 / 0.426
256k 26.93 / 0.320 2.20 / 0.627 3.07 / 0.508 4.66 / 0.460 7.08 / 0.431
1M 33.01 / 0.384 1.87 / 0.591 2.60 / 0.550 3.97 / 0.493 6.02 / 0.481
4M 38.66 / 0.462 1.71 / 0.702 2.38 / 0.644 3.58 / 0.626 5.64 / 0.630

sin_cos

precision bitwise diophantine, 13 20 32 48
16k 10.33 / 0.487 3.06 / 0.697 4.61 / 0.630 6.69 / 0.573 10.96 / 0.528
64k 14.48 / 0.467 2.80 / 0.743 3.91 / 0.679 5.85 / 0.529 8.81 / 0.573
256k 19.84 / 0.448 2.45 / 0.732 3.41 / 0.688 4.98 / 0.607 7.65 / 0.553
1M 25.67 / 0.517 2.04 / 0.779 2.91 / 0.709 4.23 / 0.641 6.42 / 0.603
4M 33.37 / 0.627 1.94 / 0.874 2.73 / 0.813 3.90 / 0.732 5.91 / 0.695

Break-even

How many times one needs to call the function for the precomputation to pay off vs no precomputation at all.

exp

precision bitwise diophantine, 13 diophantine, 32
16k 20 5 10
64k 34 6 10
256k 40 6 9
1M 54 5 8
4M 72 6 10

sin_cos

precision bitwise diophantine, 13 diophantine, 32
16k 15 6 11
64k 20 6 9
256k 25 5 8
1M 39 5 8
4M 54 5 8

bitwise against diophantine (32 primes)

precision exp sin_cos
16k 73 42
64k 116 139
256k 159 93
1M 266 173
4M 214 281

Note: fixed_exp_diophantine and fixed_sin_cos_diophantine are 1.2-1.6x faster than arb_exp and arb_sin_cos when using arb's default n=13.

@fredrik-johansson
fredrik-johansson merged commit 7bc8796 into flintlib:main Sep 20, 2026
19 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant