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
//! Binary-splitting engine for hypergeometric-like series.
//!
//! This module implements the standard "binary splitting" divide-and-conquer
//! algorithm for evaluating series of the form
//!
//! ```text
//! S = Σ_{k=lo}^{hi-1} a(k) · P(lo) · P(lo+1) · … · P(k)
//! / (Q(lo) · Q(lo+1) · … · Q(k)
//! · B(lo) · B(lo+1) · … · B(k))
//! ```
//!
//! where `P(k)`, `Q(k)`, `B(k)`, `a(k)` are integer-valued functions of `k`.
//!
//! # Algorithm
//!
//! Each recursive call returns a `BSSplit { p, q, b, t }` struct where
//! `t / (q · b)` equals the partial sum over `[lo, hi)`. The combine step is:
//!
//! ```text
//! p = p_L · p_R
//! q = q_L · q_R
//! b = b_L · b_R
//! t = t_L · q_R · b_R + t_R · p_L
//! ```
//!
//! This is `O(M(n) log n)` where `M(n)` is the cost of multiplying two n-digit
//! integers (Karatsuba in this implementation).
//!
//! # Usage
//!
//! Implement [`BSSeries`] for your series, then call [`binary_split`]:
//!
//! ```no_run
//! # use oxinum_float::native::binary_splitting::{BSSeries, BSSplit, binary_split};
//! # use oxinum_int::native::BigInt;
//! struct MySeries;
//! impl BSSeries for MySeries {
//! fn term(&self, k: u64) -> (BigInt, BigInt, BigInt, BigInt) {
//! (BigInt::one(), BigInt::one(), BigInt::one(), BigInt::one())
//! }
//! }
//! let split = binary_split(&MySeries, 0, 10);
//! ```
use BigInt;
// ---------------------------------------------------------------------------
// Public data type
// ---------------------------------------------------------------------------
/// Result of binary-splitting over a range `[lo, hi)`.
///
/// The partial sum equals `t / (q · b)`.
// ---------------------------------------------------------------------------
// Series trait
// ---------------------------------------------------------------------------
/// Trait that defines the per-term factors of a binary-splittable series.
///
/// For term index `k`, implementors return `(p_k, q_k, b_k, a_k)`:
///
/// * `p_k` — numerator factor at position `k`.
/// * `q_k` — denominator factor at position `k`.
/// * `b_k` — auxiliary denominator factor at position `k` (often `1`).
/// * `a_k` — coefficient / weight of the `k`-th term (can be negative).
///
/// The partial sum is then:
/// ```text
/// Σ a(k) · P(0..k) / (Q(0..k) · B(0..k))
/// ```
/// where `P(0..k) = p(0)·p(1)·…·p(k)`, etc.
// ---------------------------------------------------------------------------
// Core engine
// ---------------------------------------------------------------------------
/// Evaluate `Σ_{k=lo}^{hi-1}` using binary splitting.
///
/// `hi` must be strictly greater than `lo`.
///
/// # Panics
///
/// Panics if `hi <= lo`.
// ---------------------------------------------------------------------------
// Unit tests for the combining rule
// ---------------------------------------------------------------------------