Newton's method for quadratics involving continued fractions.
[frac.git] / mobius_test.c
blob150d840e7c211b9aa3fb458d8e4cc381025a4af2
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <gmp.h>
4 #include "cf.h"
5 #include "test.h"
7 static void *sqrt2(cf_t cf) {
8 cf_put_int(cf, 1);
9 while(cf_wait(cf)) {
10 cf_put_int(cf, 2);
12 return NULL;
15 int main() {
16 cf_t x, conv;
17 x = cf_new_const(sqrt2);
18 conv = cf_new_cf_convergent(x);
20 mpz_t p, q;
21 mpz_init(p);
22 mpz_init(q);
24 cf_get(p, conv);
25 EXPECT(!mpz_cmp_ui(p, 1));
26 cf_get(q, conv);
27 EXPECT(!mpz_cmp_ui(q, 1));
29 cf_get(p, conv);
30 EXPECT(!mpz_cmp_ui(p, 3));
31 cf_get(q, conv);
32 EXPECT(!mpz_cmp_ui(q, 2));
34 cf_get(p, conv);
35 EXPECT(!mpz_cmp_ui(p, 7));
36 cf_get(q, conv);
37 EXPECT(!mpz_cmp_ui(q, 5));
39 cf_get(p, conv);
40 EXPECT(!mpz_cmp_ui(p, 17));
41 cf_get(q, conv);
42 EXPECT(!mpz_cmp_ui(q, 12));
44 mpz_clear(p);
45 mpz_clear(q);
46 cf_free(conv);
47 cf_free(x);
49 x = cf_new_const(sqrt2);
50 cf_t mob;
51 mpz_t z[4];
52 for (int i = 0; i < 4; i++) mpz_init(z[i]);
53 // Identity Mobius transformation.
54 mpz_set_si(z[0], 1);
55 mpz_set_si(z[3], 1);
56 mob = cf_new_mobius_to_cf(x, z);
57 CF_EXPECT_DEC(mob, "1.4142135623730");
58 cf_free(mob);
59 cf_free(x);
60 x = cf_new_const(sqrt2);
61 // (x - 2)/(-3x + 4)
62 // Works out to be 1 + sqrt(2).
63 mpz_set_si(z[0], 1);
64 mpz_set_si(z[1], -2);
65 mpz_set_si(z[2], -3);
66 mpz_set_si(z[3], 4);
67 mob = cf_new_mobius_to_cf(x, z);
68 CF_EXPECT_DEC(mob, "2.4142135623730");
69 cf_free(x);
70 cf_free(mob);
71 for (int i = 0; i < 4; i++) mpz_clear(z[i]);
73 return 0;