6 // Minds our p's and q's. The two last computed convergents.
11 typedef struct pqset_s
*pqset_ptr
;
12 typedef struct pqset_s pqset_t
[1];
14 void pqset_init(pqset_t pq
) {
21 void pqset_clear(pqset_t pq
) {
28 void pqset_neg(pqset_t pq
) {
29 mpz_neg(pq
->p
, pq
->p
);
30 mpz_neg(pq
->q
, pq
->q
);
31 mpz_neg(pq
->pold
, pq
->pold
);
32 mpz_neg(pq
->qold
, pq
->qold
);
35 void pqset_print(pqset_t pq
) {
36 gmp_printf("p's: %Zd %Zd\n", pq
->pold
, pq
->p
);
37 gmp_printf("q's: %Zd %Zd\n", pq
->qold
, pq
->q
);
40 // Compute the next convergent for regular continued fractions.
41 void pqset_regular_recur(pqset_t pq
, mpz_t denom
) {
42 mpz_addmul(pq
->pold
, denom
, pq
->p
);
43 mpz_swap(pq
->pold
, pq
->p
);
44 mpz_addmul(pq
->qold
, denom
, pq
->q
);
45 mpz_swap(pq
->qold
, pq
->q
);
48 // Compute the next convergent for nonregular continued fractions.
49 void pqset_nonregular_recur(pqset_t pq
, mpz_t num
, mpz_t denom
) {
50 mpz_mul(pq
->pold
, pq
->pold
, num
);
51 mpz_addmul(pq
->pold
, pq
->p
, denom
);
52 mpz_swap(pq
->pold
, pq
->p
);
53 mpz_mul(pq
->qold
, pq
->qold
, num
);
54 mpz_addmul(pq
->qold
, pq
->q
, denom
);
55 mpz_swap(pq
->qold
, pq
->q
);
58 // Get rid of nontrivial GCD for {p, q, pold, qold}.
59 // t0 and t1 are temporary variables.
60 void pqset_remove_gcd(pqset_ptr pq
, mpz_t t0
, mpz_t t1
) {
61 mpz_gcd(t0
, pq
->p
, pq
->q
);
62 mpz_gcd(t1
, pq
->pold
, pq
->qold
);
64 if (mpz_cmp_ui(t0
, 1)) {
65 mpz_divexact(pq
->pold
, pq
->pold
, t0
);
66 mpz_divexact(pq
->qold
, pq
->qold
, t0
);
67 mpz_divexact(pq
->p
, pq
->p
, t0
);
68 mpz_divexact(pq
->q
, pq
->q
, t0
);
72 // A Mobius transformation: four coefficients and the input.
73 // TODO: Use an array of size 4.
74 struct mobius_data_s
{
78 typedef struct mobius_data_s
*mobius_data_ptr
;
80 void pqset_set_mobius(pqset_t pq
, mobius_data_ptr md
) {
81 mpz_set(pq
->pold
, md
->b
); mpz_set(pq
->p
, md
->a
);
82 mpz_set(pq
->qold
, md
->d
); mpz_set(pq
->q
, md
->c
);
85 // Compute convergents of Mobius function applied to a regular
86 // continued fraction.
87 static void *mobius_convergent(cf_t cf
) {
88 mobius_data_ptr md
= cf_data(cf
);
89 cf_t input
= md
->input
;
92 pqset_set_mobius(pq
, md
);
98 pqset_regular_recur(pq
, denom
);
113 // Start a thread that, when signalled, computes the convergents of a Mobius
114 // transformation of a continued fraction.
115 cf_t
cf_new_mobius_convergent(cf_t x
, mpz_t a
, mpz_t b
, mpz_t c
, mpz_t d
) {
116 mobius_data_ptr md
= malloc(sizeof(*md
));
117 mpz_init(md
->a
); mpz_init(md
->b
); mpz_init(md
->c
); mpz_init(md
->d
);
118 mpz_set(md
->a
, a
); mpz_set(md
->b
, b
); mpz_set(md
->c
, c
); mpz_set(md
->d
, d
);
120 return cf_new(mobius_convergent
, md
);
123 // Start a thread that, when signalled, computes the convergents of a continued
125 cf_t
cf_new_convergent(cf_t x
) {
127 mpz_init(one
); mpz_init(zero
);
128 mpz_set_ui(one
, 1); mpz_set_ui(zero
, 0);
129 cf_t res
= cf_new_mobius_convergent(x
, one
, zero
, zero
, one
);
130 mpz_clear(one
); mpz_clear(zero
);
134 // Compute nonregular convergents of a Mobius function applied
135 // to a nonregular continued fraction.
136 static void *nonregular_mobius_convergent(cf_t cf
) {
137 mobius_data_ptr md
= cf_data(cf
);
138 cf_t input
= md
->input
;
139 pqset_t pq
; pqset_init(pq
); pqset_set_mobius(pq
, md
);
140 mpz_t num
; mpz_init(num
);
141 mpz_t denom
; mpz_init(denom
);
142 mpz_t t0
, t1
; mpz_init(t0
); mpz_init(t1
);
144 pqset_nonregular_recur(pq
, num
, denom
);
145 pqset_remove_gcd(pq
, t0
, t1
);
151 cf_get(denom
, input
);
155 cf_get(denom
, input
);
162 mpz_clear(md
->a
); mpz_clear(md
->b
); mpz_clear(md
->c
); mpz_clear(md
->d
);
163 mpz_clear(t0
); mpz_clear(t1
);
168 cf_t
cf_new_nonregular_mobius_convergent(cf_t x
, mpz_t a
, mpz_t b
, mpz_t c
, mpz_t d
) {
169 mobius_data_ptr md
= malloc(sizeof(*md
));
170 mpz_init(md
->a
); mpz_init(md
->b
); mpz_init(md
->c
); mpz_init(md
->d
);
171 mpz_set(md
->a
, a
); mpz_set(md
->b
, b
); mpz_set(md
->c
, c
); mpz_set(md
->d
, d
);
173 return cf_new(nonregular_mobius_convergent
, md
);
176 static void *mobius_nonregular_throughput(cf_t cf
) {
177 mobius_data_ptr md
= cf_data(cf
);
178 cf_t input
= md
->input
;
179 pqset_t pq
; pqset_init(pq
); pqset_set_mobius(pq
, md
);
180 mpz_t num
; mpz_init(num
);
181 mpz_t denom
; mpz_init(denom
);
182 mpz_t t0
, t1
, t2
; mpz_init(t2
); mpz_init(t1
); mpz_init(t0
);
184 pqset_nonregular_recur(pq
, num
, denom
);
185 pqset_remove_gcd(pq
, t0
, t1
);
187 if (mpz_sgn(pq
->qold
)) {
188 mpz_fdiv_qr(t1
, t0
, pq
->pold
, pq
->qold
);
189 mpz_mul(t2
, t1
, pq
->q
);
191 if (mpz_cmp(t2
, pq
->p
) <= 0) {
192 mpz_add(t2
, t2
, pq
->q
);
193 if (mpz_cmp(t2
, pq
->p
) > 0) {
194 // Output continued fraction term.
196 // Subtract: remainder of p/q.
197 mpz_sub(t2
, t2
, pq
->p
);
198 mpz_sub(t2
, pq
->q
, t2
);
200 mpz_set(pq
->pold
, pq
->qold
);
201 mpz_set(pq
->qold
, t0
);
202 mpz_set(pq
->p
, pq
->q
);
211 cf_get(denom
, input
);
216 cf_get(denom
, input
);
223 mpz_clear(md
->a
); mpz_clear(md
->b
); mpz_clear(md
->c
); mpz_clear(md
->d
);
225 mpz_clear(t2
); mpz_clear(t1
); mpz_clear(t0
);
229 cf_t
cf_new_nonregular_to_cf(cf_t x
, mpz_t a
, mpz_t b
, mpz_t c
, mpz_t d
) {
230 mobius_data_ptr md
= malloc(sizeof(*md
));
231 mpz_init(md
->a
); mpz_init(md
->b
); mpz_init(md
->c
); mpz_init(md
->d
);
232 mpz_set(md
->a
, a
); mpz_set(md
->b
, b
); mpz_set(md
->c
, c
); mpz_set(md
->d
, d
);
234 return cf_new(mobius_nonregular_throughput
, md
);
237 static void *mobius_decimal(cf_t cf
) {
238 mobius_data_ptr md
= cf_data(cf
);
239 cf_t input
= md
->input
;
240 pqset_t pq
; pqset_init(pq
); pqset_set_mobius(pq
, md
);
241 mpz_t denom
; mpz_init(denom
);
242 mpz_t t0
, t1
, t2
; mpz_init(t2
); mpz_init(t1
); mpz_init(t0
);
244 // Determine the sign.
245 cf_set_sign(cf
, cf_sign(input
));
246 while (mpz_sgn(pq
->pold
) != mpz_sgn(pq
->p
)
247 || mpz_sgn(pq
->qold
) != mpz_sgn(pq
->q
)) {
248 cf_get(denom
, input
);
249 pqset_regular_recur(pq
, denom
);
251 if (mpz_sgn(pq
->qold
) < 0) {
252 mpz_neg(pq
->qold
, pq
->qold
);
253 mpz_neg(pq
->q
, pq
->q
);
256 if (mpz_sgn(pq
->pold
) < 0) {
257 mpz_neg(pq
->pold
, pq
->pold
);
258 mpz_neg(pq
->p
, pq
->p
);
263 pqset_regular_recur(pq
, denom
);
265 // If the denominator is zero, we can't do anything yet.
266 if (mpz_sgn(pq
->qold
)) {
267 // Each term except possibly the first is one of {0, ..., 9}.
268 /* Naive attempt to expoit this didn't seem faster:
269 * (and I'd have to handle the first term properly)
272 for (i = 0; i <= 9; i++) {
273 if (mpz_cmp(t0, pq->p) > 0) break;
274 mpz_add(t0, t0, pq->q);
276 mpz_set_ui(pq->pold, i);
277 mpz_sub(t0, t0, pq->p);
278 mpz_sub(t0, pq->q, t0);
279 mpz_mul(pq->qold, pq->pold, pq->qnew);
282 mpz_fdiv_qr(t1
, t0
, pq
->pold
, pq
->qold
);
283 mpz_mul(t2
, t1
, pq
->q
);
284 if (mpz_cmp(t2
, pq
->p
) <= 0) {
285 mpz_add(t2
, t2
, pq
->q
);
286 if (mpz_cmp(t2
, pq
->p
) > 0) {
287 // Output a decimal digit.
289 // Compute t2 = remainder of p/q.
290 mpz_sub(t2
, t2
, pq
->p
);
291 mpz_sub(t2
, pq
->q
, t2
);
292 // Multiply numerator by 10.
293 mpz_mul_ui(pq
->pold
, t0
, 10);
294 mpz_mul_ui(pq
->p
, t2
, 10);
303 cf_get(denom
, input
);
308 mpz_clear(t0
); mpz_clear(t1
); mpz_clear(t2
);
309 mpz_clear(md
->a
); mpz_clear(md
->b
); mpz_clear(md
->c
); mpz_clear(md
->d
);
313 cf_t
cf_new_mobius_to_decimal(cf_t x
, mpz_t a
, mpz_t b
, mpz_t c
, mpz_t d
) {
314 mobius_data_ptr md
= malloc(sizeof(*md
));
315 mpz_init(md
->a
); mpz_init(md
->b
); mpz_init(md
->c
); mpz_init(md
->d
);
316 mpz_set(md
->a
, a
); mpz_set(md
->b
, b
); mpz_set(md
->c
, c
); mpz_set(md
->d
, d
);
318 return cf_new(mobius_decimal
, md
);
321 cf_t
cf_new_cf_to_decimal(cf_t x
) {
323 mpz_init(one
); mpz_init(zero
);
324 mpz_set_ui(one
, 1); mpz_set_ui(zero
, 0);
325 cf_t res
= cf_new_mobius_to_decimal(x
, one
, zero
, zero
, one
);
326 mpz_clear(one
); mpz_clear(zero
);
330 // This seems to be slower than regularizing the continued fraction
331 // and then converting to decimal.
332 static void *nonregular_mobius_decimal(cf_t cf
) {
333 mobius_data_ptr md
= cf_data(cf
);
334 cf_t input
= md
->input
;
335 pqset_t pq
; pqset_init(pq
); pqset_set_mobius(pq
, md
);
336 mpz_t num
; mpz_init(num
);
337 mpz_t denom
; mpz_init(denom
);
338 mpz_t t0
, t1
, t2
; mpz_init(t2
); mpz_init(t1
); mpz_init(t0
);
340 pqset_nonregular_recur(pq
, num
, denom
);
341 pqset_remove_gcd(pq
, t0
, t1
);
343 // If the denominator is zero, we can't do anything yet.
344 if (mpz_sgn(pq
->qold
)) {
345 mpz_fdiv_qr(t1
, t0
, pq
->pold
, pq
->qold
);
346 mpz_mul(t2
, t1
, pq
->q
);
347 if (mpz_cmp(t2
, pq
->p
) <= 0) {
348 mpz_add(t2
, t2
, pq
->q
);
349 if (mpz_cmp(t2
, pq
->p
) > 0) {
350 // Output a decimal digit.
352 // Subtract: remainder of p/q.
353 mpz_sub(t2
, t2
, pq
->p
);
354 mpz_sub(t2
, pq
->q
, t2
);
355 // Multiply numerator by 10.
356 mpz_mul_ui(pq
->pold
, t0
, 10);
357 mpz_mul_ui(pq
->p
, t2
, 10);
365 cf_get(denom
, input
);
370 cf_get(denom
, input
);
376 mpz_clear(t0
); mpz_clear(t1
); mpz_clear(t2
);
377 mpz_clear(md
->a
); mpz_clear(md
->b
); mpz_clear(md
->c
); mpz_clear(md
->d
);
382 cf_t
cf_new_nonregular_mobius_to_decimal(cf_t x
, mpz_t a
, mpz_t b
, mpz_t c
, mpz_t d
) {
383 mobius_data_ptr md
= malloc(sizeof(*md
));
384 mpz_init(md
->a
); mpz_init(md
->b
); mpz_init(md
->c
); mpz_init(md
->d
);
385 mpz_set(md
->a
, a
); mpz_set(md
->b
, b
); mpz_set(md
->c
, c
); mpz_set(md
->d
, d
);
387 return cf_new(nonregular_mobius_decimal
, md
);