aab54a4c4efce76f723844f55b9f388425218dbf
[frac.git] / cf.h
blobaab54a4c4efce76f723844f55b9f388425218dbf
1 // Requires gmp.h
2 //
3 // Opaque interface to continued fractions object.
5 #ifndef __CF_H__
6 #define __CF_H__
8 struct cf_s;
9 typedef struct cf_s *cf_t;
11 cf_t cf_new(void *(*func)(cf_t), void *data);
12 static inline cf_t cf_new_const(void *(*func)(cf_t)) {
13 return cf_new(func, NULL);
15 void cf_free(cf_t cf);
17 void cf_set_sign(cf_t cf, int sign);
18 int cf_sign(cf_t cf);
19 int cf_flip_sign(cf_t cf);
20 void cf_get(mpz_t z, cf_t cf);
21 void cf_put(cf_t cf, mpz_t z);
22 void cf_put_int(cf_t cf, int n);
24 int cf_wait(cf_t cf);
26 void *cf_data(cf_t cf);
28 // cf_mobius.c:
30 // Compute convergents of a simple continued fraction x.
31 // Outputs p then q on channel, where p/q is the last convergent computed.
32 cf_t cf_new_cf_convergent(cf_t x);
33 // Compute decimal representation of a simple continued fraction x.
34 // Outputs integer part first, then digits one at a time.
35 cf_t cf_new_cf_to_decimal(cf_t x);
37 // Compute convergents of (a x + b)/(c x + d)
38 // where x is a regular continued fraction.
39 cf_t cf_new_mobius_convergent(cf_t x, mpz_t a, mpz_t b, mpz_t c, mpz_t d);
40 cf_t cf_new_mobius_to_decimal(cf_t x, mpz_t a, mpz_t b, mpz_t c, mpz_t d);
41 cf_t cf_new_mobius_to_cf(cf_t x, mpz_t z[4]);
43 // Compute convergents of (a x + b)/(c x + d)
44 // where x is a nonregular continued fraction.
45 cf_t cf_new_nonregular_mobius_convergent(cf_t x, mpz_t a, mpz_t b, mpz_t c, mpz_t d);
46 // Input: Mobius transformation and nonregular continued fraction.
47 // Output: Regular continued fraction. Assumes input fraction is well-behaved.
48 cf_t cf_new_nonregular_to_cf(cf_t x, mpz_t a, mpz_t b, mpz_t c, mpz_t d);
49 // Does both of the above at once. Seems slow.
50 cf_t cf_new_nonregular_mobius_to_decimal(cf_t x, mpz_t a[4]);
52 // Well-known continued fraction expansions.
53 // cf_famous.c:
54 // e:
55 cf_t cf_new_sqrt2();
56 cf_t cf_new_e();
57 cf_t cf_new_pi();
58 cf_t cf_new_tan1();
59 cf_t cf_new_epow(mpz_t pow);
60 cf_t cf_new_tanh(mpz_t z);
62 // This won't work because my code cannot handle negative denominators,
63 // and also assumes the sequence of convergents alternatively overshoot
64 // and undershoots the target. The tan expansion leads to a sequence of
65 // strictly increasing convergents (for positive input).
66 cf_t cf_new_tan(mpz_t z);
68 // Gosper's method for computing bihomographic functions of continued fractions.
69 cf_t cf_new_bihom(cf_t x, cf_t y, mpz_t a[8]);
71 // From taylor.c:
72 cf_t cf_new_sin1();
73 cf_t cf_new_cos1();
75 // From newton.c:
76 // Use Newton's method to find solutions of:
78 // a0 xy + a1 x + a2 y + a3
79 // y = ------------------------
80 // a4 xy - a0 x + a5 y - a2
81 cf_t cf_new_newton(cf_t x, mpz_t a[6], mpz_t lower);
82 cf_t cf_new_sqrt(cf_t x);
84 #endif // __CF_H__