1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//! Riemann theta functions, classical modular functions and Weierstrass
//! elliptic functions — **as rigorous enclosures**, backed by FLINT's Arb layer.
//!
//! Every value returned by this module is a [`ComplexBall`]: a midpoint and a
//! radius, with the guarantee that the true value lies inside. Nothing here
//! returns a bare `f64`, and the one accessor that produces one
//! ([`ComplexBall::value_if_accurate`]) refuses unless the enclosure meets a
//! stated bit requirement. That is the whole point of the module: a theta value
//! is the kind of quantity that is easy to compute wrongly and impossible to
//! check afterwards, so the error bound travels with it.
//!
//! ```
//! use alkahest_cas::experimental::{j_invariant, ComplexBall, Precision};
//!
//! // j(i) = 1728, exactly — the classical normalisation check.
//! let tau = ComplexBall::exact_i64(0, 1, 256);
//! let j = j_invariant(&tau, Precision::Bits(256)).unwrap();
//! assert!(j.contains_f64(1728.0, 0.0));
//! assert!(j.accuracy_bits() > 200);
//! ```
//!
//! # What is here
//!
//! * **Genus 1** ([`acb_modular`], [`acb_elliptic`]): [`dedekind_eta`],
//! [`j_invariant`], [`modular_lambda`], [`modular_discriminant`],
//! [`eisenstein_series`], the Jacobi theta functions [`jacobi_theta`] /
//! [`jacobi_theta_null`], and the Weierstrass functions
//! [`weierstrass_p`], [`weierstrass_p_prime`], [`weierstrass_zeta`],
//! [`weierstrass_sigma`], [`weierstrass_invariants`], [`weierstrass_roots`].
//! * **Genus `g`** (`acb_theta`): [`riemann_theta`], [`riemann_theta_squared`],
//! [`riemann_theta_characteristic`], the characteristic helpers
//! [`theta_characteristic_index`] / [`theta_characteristic_is_even`], and the
//! Siegel helpers [`SiegelMatrix::is_certainly_in_siegel_upper_half_space`],
//! [`siegel_is_reduced`] and [`siegel_reduce`].
//! * **Ball arithmetic** on [`ComplexBall`] and [`RealBall`], enough to build
//! inputs that are not rational — `rho = exp(2 pi i / 3)`, for instance — and
//! to check identities between computed results inside their own radii.
//!
//! [`acb_modular`]: https://flintlib.org/doc/acb_modular.html
//! [`acb_elliptic`]: https://flintlib.org/doc/acb_elliptic.html
//!
//! # Normalisation (read this before comparing against another system)
//!
//! Theta functions have more inconsistent conventions in the literature than
//! almost anything else in analysis. These are FLINT's, and they are the ones
//! used here without reinterpretation:
//!
//! * `theta_{a,b}(z, tau) = sum_{n in Z^g + a/2} exp(pi i n^T tau n + 2 pi i n^T (z + b/2))`
//! for `a, b` in `{0,1}^g`. The `4^g` values are indexed by the `2g`-bit
//! integer `(a << g) | b`, with `a` in the more significant half.
//! * The classical Jacobi functions use `q = exp(pi i tau)` and
//! `w = exp(pi i z)`, so `theta_3(z, tau) = 1 + 2 sum_{n>=1} q^{n^2} cos(2 n pi z)`.
//! * Consequently, in genus 1,
//! `(theta_1, theta_2, theta_3, theta_4) = (-theta_{1,1}, theta_{1,0}, theta_{0,0}, theta_{0,1})`.
//! The sign on `theta_1` is real and is checked by
//! `theta::tests::genus_one_agrees_with_classical_jacobi`, which evaluates
//! both FLINT code paths and asserts the balls overlap.
//! * `eta`, `j`, `lambda` and `Delta` are the standard ones: `j(i) = 1728`,
//! `j(rho) = 0` for `rho = exp(2 pi i / 3)`, `eta(i) = Gamma(1/4) / (2 pi^{3/4})`.
//! * Weierstrass `℘(z, tau)` is for the lattice `Z + tau Z`, and
//! [`weierstrass_invariants`] returns `(g2, g3)` for that same lattice.
//!
//! # Precision, accuracy and refusal
//!
//! [`Precision`] has two modes and they mean different things:
//!
//! * [`Precision::Bits`] spends a stated working precision once and hands back
//! whatever enclosure that produced — possibly a wide one, possibly an
//! infinite one. Nothing is hidden; interrogate
//! [`ComplexBall::accuracy_bits`].
//! * [`Precision::AccurateTo`] raises the working precision until the result
//! has at least the requested **relative** accuracy, and refuses with
//! [`ThetaError::InsufficientAccuracy`] if it runs out of room.
//!
//! One consequence deserves stating plainly: **`AccurateTo` can never be
//! satisfied by a value that is exactly zero.** Relative accuracy of zero is
//! not a thing. `j(rho) = 0` is checked with [`Precision::Bits`] and
//! [`ComplexBall::contains_zero`], which is the correct question to ask of a
//! vanishing quantity; asking `AccurateTo` there will correctly refuse forever.
//!
//! # Scope limits
//!
//! * **Genus ceiling.** [`MAX_GENUS`] is 6, and that is a guard rather than a
//! recommendation. `acb_theta_all` returns `4^g` values, so the output alone
//! is exponential in `g` before any work is counted. Measured on one x86-64
//! machine, release build, `z = 0`, 128-bit working precision, one
//! [`riemann_theta`] call on a near-diagonal period matrix:
//!
//! | genus | values | time |
//! |-------|--------|---------|
//! | 1 | 4 | 0.9 ms |
//! | 2 | 16 | 5.7 ms |
//! | 3 | 64 | 62 ms |
//! | 4 | 256 | 0.62 s |
//! | 5 | 1024 | 11 s |
//! | 6 | 4096 | 244 s |
//!
//! Genus 1 and 2 are what this module is built and tested for. Genus 3 is
//! exercised by a test. **Genus 4 to 6 are accepted and covered by no test
//! here**: treat a result there as untested code, and read its
//! [`ThetaValues::worst_accuracy_bits`] before believing any of it. The cost
//! grows by roughly an order of magnitude per genus and the last step is
//! worse than that, so a genus-7 request would be minutes to hours — hence
//! the cap rather than an open-ended refusal at run time.
//! * **No derivatives.** FLINT's `acb_theta_jet` computes partial derivatives
//! of theta with respect to `z` to any order; only order 0 is wrapped. The
//! ordering conventions for jets are intricate enough that exposing them
//! without tests would be worse than not exposing them.
//! * **No genus-2 Siegel modular forms.** `acb_theta_g2_*` (`chi_5`, `chi_35`,
//! the Igusa–Clebsch invariants) is not wrapped.
//! * **Inputs are balls, not exact symbols.** A period matrix is given as
//! enclosures. If the input balls are wide, the output balls are wider, and
//! this module will not pretend otherwise.
//! * **Working precision is not output accuracy.** FLINT's own precision
//! management means a `Precision::Bits(p)` request typically returns rather
//! fewer than `p` accurate bits: genus 1 loses a handful, and at genus 2 a
//! 300-bit request came back with enclosures around `1e-46` on `O(1)` values,
//! i.e. roughly half the bits asked for. That is why
//! [`Precision::AccurateTo`] exists — it is the mode to use when a specific
//! accuracy is actually required, rather than guessing at a working
//! precision.
//! * **`Im(tau)` must be *provably* positive definite** at the working
//! precision, or the call refuses with
//! [`ThetaError::NotInSiegelUpperHalfSpace`]. A refusal means "not proved",
//! never "proved false" — raising the precision may settle it.
//!
//! # Relationship to the rest of the crate
//!
//! [`crate::ball::ArbBall`] is a *separate*, MPFR-backed ball type used by
//! `IntervalEval`. It is untouched by this module and continues to work exactly
//! as before. This module is the first genuine `arb_t`/`acb_t` binding in the
//! crate; migrating `ball` onto it is deliberately left as separate work.
//!
//! [`crate::funcfield`] models hyperelliptic curves, divisors and Riemann–Roch
//! symbolically over `Q`. There is no wiring between the two: the analytic
//! Jacobian of such a curve is exactly what a period matrix here describes, but
//! computing the period matrix *from* a curve is not implemented.
//!
//! # FLINT versions
//!
//! Arb became part of FLINT in 3.0 and `acb_theta` was rewritten in 3.2.
//! `build.rs` probes `libflint`'s symbol table and sets `flint_arb` /
//! `flint_acb_theta`. On a FLINT that lacks either, this module still compiles
//! and every entry point refuses with `E-THETA-001`, so the Python surface is
//! the same shape on every build.
pub use ;
pub use ThetaError;
pub use ;
pub use ;
// ---------------------------------------------------------------------------
// Limits
// ---------------------------------------------------------------------------
/// Largest working precision, in bits, this module will accept or climb to.
///
/// A million bits is well past the point where a theta evaluation stops being
/// interactive; the cap exists so that an unsatisfiable
/// [`Precision::AccurateTo`] refuses in bounded time rather than running until
/// the machine gives up.
pub const MAX_PRECISION_BITS: u32 = 1 << 20;
/// Smallest working precision this module will accept.
pub const MIN_PRECISION_BITS: u32 = 2;
/// Working precision used by the convenience paths that do not take one.
pub const DEFAULT_PRECISION_BITS: u32 = 256;
/// Largest genus accepted by [`riemann_theta`] and friends.
///
/// See the module-level "Scope limits": genus 1 and 2 are what this is built
/// for, genus 3 is tested, and 4 to 6 are accepted but untested.
pub const MAX_GENUS: usize = 6;
// ---------------------------------------------------------------------------
// Precision
// ---------------------------------------------------------------------------
/// How much precision to spend, and what to do if the result is not accurate.
// ---------------------------------------------------------------------------
// Enclosure summary + the refinement driver
// ---------------------------------------------------------------------------
/// Anything a theta entry point can return, summarised by how good its worst
/// enclosure is. Implemented for the shapes the public functions use.
pub
/// Run `f` at the precision [`Precision`] asks for, refining when it asks for
/// accuracy rather than for a precision.
///
/// The refusal carries the accuracy that *was* reached and the precision it was
/// reached at, so a caller can tell "the value is zero" (accuracy stays at
/// `i64::MIN` however much precision is spent) from "this needs more room".
pub
// ---------------------------------------------------------------------------
// Backend operation selectors
// ---------------------------------------------------------------------------
pub
pub
pub
pub
pub
pub
/// Is the genus-`g` Riemann theta path (FLINT's `acb_theta`) compiled in?
///
/// `false` on a build whose FLINT predates 3.2; every `riemann_*` entry point
/// then refuses with `E-THETA-001`. The genus-1 modular functions may still be
/// available — check [`arb_backend_available`].
/// Is the Arb ball layer (FLINT >= 3.1) available in this build?