# HG changeset patch
# User dmharvey@math.harvard.edu
# Date 1217600922 14400
# Node ID 36c9629974915525ae9d0297b034136c68433637
# Parent 7daa34e031e74c472f648d9576cc88b8bc0b17c1
fixed overflow bug, added "r" prefix to comments, added algorithm="default" option to bernoulli(), per reviewer's comment on #3542
diff -r 7daa34e031e7 -r 36c962997491 sage/rings/arith.py
|
a
|
b
|
|
| 145 | 145 | |
| 146 | 146 | algebraic_dependency = algdep |
| 147 | 147 | |
| 148 | | def bernoulli(n, algorithm='pari', num_threads=1): |
| | 148 | def bernoulli(n, algorithm='default', num_threads=1): |
| 149 | 149 | r""" |
| 150 | 150 | Return the n-th Bernoulli number, as a rational number. |
| 151 | 151 | |
| 152 | 152 | INPUT: |
| 153 | 153 | n -- an integer |
| 154 | 154 | algorithm: |
| 155 | | 'pari' -- (default) use the PARI C library, which is |
| 156 | | by *far* the fastest. |
| | 155 | 'default' -- (default) use 'pari' for n <= 30000, and |
| | 156 | 'bernmm' for n > 30000 (this is just a heuristic, |
| | 157 | and not guaranteed to be optimal on all hardware) |
| | 158 | 'pari' -- use the PARI C library |
| 157 | 159 | 'gap' -- use GAP |
| 158 | 160 | 'gp' -- use PARI/GP interpreter |
| 159 | 161 | 'magma' -- use MAGMA (optional) |
| … |
… |
|
| 192 | 194 | """ |
| 193 | 195 | from sage.rings.all import Integer, Rational |
| 194 | 196 | n = Integer(n) |
| | 197 | |
| | 198 | if algorithm == 'default': |
| | 199 | algorithm = 'pari' if n <= 30000 else 'bernmm' |
| | 200 | |
| 195 | 201 | if n > 50000 and algorithm == 'pari': |
| 196 | 202 | algorithm = 'gp' |
| | 203 | |
| 197 | 204 | if algorithm == 'pari': |
| 198 | 205 | x = pari(n).bernfrac() # Use the PARI C library |
| 199 | 206 | return Rational(x) |
diff -r 7daa34e031e7 -r 36c962997491 sage/rings/bernmm.pyx
|
a
|
b
|
|
| 29 | 29 | |
| 30 | 30 | |
| 31 | 31 | def bernmm_bern_rat(long k, int num_threads = 1): |
| 32 | | """ |
| | 32 | r""" |
| 33 | 33 | Computes k-th Bernoulli number using a multimodular algorithm. |
| 34 | 34 | (Wrapper for bernmm library.) |
| 35 | 35 | |
| … |
… |
|
| 39 | 39 | |
| 40 | 40 | COMPLEXITY: |
| 41 | 41 | Pretty much quadratic in $k$. See the paper ``A multimodular algorithm |
| 42 | | for computing isolated Bernoulli numbers'', David Harvey, 2008, for |
| 43 | | more details. |
| | 42 | for computing Bernoulli numbers'', David Harvey, 2008, for more details. |
| 44 | 43 | |
| 45 | 44 | EXAMPLES: |
| 46 | 45 | sage: from sage.rings.bernmm import bernmm_bern_rat |
| … |
… |
|
| 73 | 72 | |
| 74 | 73 | |
| 75 | 74 | def bernmm_bern_modp(long p, long k): |
| 76 | | """ |
| | 75 | r""" |
| 77 | 76 | Computes $B_k \mod p$, where $B_k$ is the k-th Bernoulli number. |
| 78 | 77 | |
| 79 | 78 | If $B_k$ is not $p$-integral, returns -1. |
diff -r 7daa34e031e7 -r 36c962997491 sage/rings/bernmm/bern_modp.cpp
|
a
|
b
|
|
| 436 | 436 | |
| 437 | 437 | PRECONDITIONS: |
| 438 | 438 | 3 <= n < F, n odd |
| 439 | | 0 <= x < nF |
| | 439 | 0 <= x < nF (if n < F/2) |
| | 440 | 0 <= x < nF/2 (if n > F/2) |
| 440 | 441 | ninv2 = -1/n mod F |
| 441 | 442 | */ |
| 442 | 443 | #define LOW_MASK ((1L << (LONG_BIT / 2)) - 1) |
| … |
… |
|
| 638 | 639 | sum += y; |
| 639 | 640 | } |
| 640 | 641 | |
| 641 | | x = RedcFast(x * x_jump_redc, p, pinv2); |
| | 642 | x = Redc(x * x_jump_redc, p, pinv2); |
| 642 | 643 | } |
| 643 | 644 | |
| 644 | 645 | return sum % p; |
diff -r 7daa34e031e7 -r 36c962997491 sage/rings/bernmm/bernmm-test.cpp
|
a
|
b
|
|
| 189 | 189 | for (long k = 2; k <= p - 3 && success; k += 2) |
| 190 | 190 | success = success && testcase__bern_modp_pow2(p, k); |
| 191 | 191 | } |
| 192 | | |
| | 192 | |
| 193 | 193 | // a few larger values of p |
| 194 | 194 | for (long p = 1000000; p < 1030000; p++) |
| 195 | 195 | { |
| … |
… |
|
| 210 | 210 | success = success & testcase__bern_modp_pow2(p, 10); |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | | // one just below the REDC barrier |
| | 213 | // try a few just below the REDC barrier |
| | 214 | if (LONG_BIT == 32) |
| 214 | 215 | { |
| | 216 | long boundary = 1L << (LONG_BIT/2 - 1); |
| | 217 | for (long p = boundary - 1000; p < boundary && success; p++) |
| | 218 | { |
| | 219 | if (ProbPrime(p)) |
| | 220 | { |
| | 221 | for (long trial = 0; trial < 1000 && success; trial++) |
| | 222 | { |
| | 223 | long k = 2 * (rand() % ((p-3)/2)) + 2; |
| | 224 | success = success && testcase__bern_modp_pow2(p, k); |
| | 225 | } |
| | 226 | } |
| | 227 | } |
| | 228 | } |
| | 229 | else |
| | 230 | { |
| | 231 | // on a 64-bit machine, only try one, since these are huge! |
| 215 | 232 | long p = 1L << (LONG_BIT/2 - 1); |
| 216 | 233 | while (!ProbPrime(p)) |
| 217 | 234 | p--; |
| 218 | | success = success && testcase__bern_modp_pow2(p, 10); |
| 219 | | } |
| 220 | | |
| 221 | | // and one above the REDC barrier |
| 222 | | { |
| 223 | | long p = 1L << (LONG_BIT/2 - 1); |
| 224 | | while (!ProbPrime(p)) |
| 225 | | p++; |
| 226 | 235 | success = success && testcase__bern_modp_pow2(p, 10); |
| 227 | 236 | } |
| 228 | 237 | |