Completed removal of pnew, qnew.
[frac.git] / cf_famous.c
blobc8e604dc9739fb5b6cf68881eb8f1ae9b40e6df1
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <gmp.h>
4 #include "cf.h"
6 // e = [2; 1, 2, 1, 1, 4, 1, ...]
7 static void *e_expansion(cf_t cf) {
8 mpz_t even, one;
9 mpz_init(even); mpz_init(one);
10 mpz_set_ui(even, 2); mpz_set_ui(one, 1);
12 cf_put(cf, even);
14 while(cf_wait(cf)) {
15 cf_put(cf, one);
16 cf_put(cf, even);
17 mpz_add_ui(even, even, 2);
18 cf_put(cf, one);
21 mpz_clear(one);
22 mpz_clear(even);
23 return NULL;
26 cf_t cf_new_e() {
27 return cf_new(e_expansion, NULL);
30 static void *pi_arctan_sequence(cf_t cf) {
31 mpz_t num, denom;
32 mpz_init(num);
33 mpz_init(denom);
35 mpz_set_ui(denom, 1);
36 cf_put(cf, denom);
38 mpz_set_ui(num, 1);
39 cf_put(cf, num);
41 while(cf_wait(cf)) {
42 mpz_add_ui(denom, denom, 2);
43 cf_put(cf, denom);
44 mpz_add(num, num, denom);
45 cf_put(cf, num);
48 mpz_clear(num);
49 mpz_clear(denom);
50 return NULL;
53 static void *regularized_pi(cf_t cf) {
54 mpz_t a, b, c, d;
55 mpz_init(a); mpz_init(b); mpz_init(c); mpz_init(d);
56 mpz_set_ui(a, 0); mpz_set_ui(b, 4);
57 mpz_set_ui(c, 1); mpz_set_ui(d, 0);
58 cf_t nonregpi = cf_new(pi_arctan_sequence, NULL);
59 cf_t conv = cf_new_nonregular_to_cf(nonregpi, a, b, c, d);
60 mpz_t z;
61 mpz_init(z);
62 while(cf_wait(cf)) {
63 cf_get(z, conv);
64 cf_put(cf, z);
66 mpz_clear(z);
67 cf_free(conv);
68 cf_free(nonregpi);
69 mpz_clear(a); mpz_clear(b); mpz_clear(c); mpz_clear(d);
70 return NULL;
73 cf_t cf_new_pi() {
74 return cf_new(regularized_pi, NULL);
77 void *exp_expansion(cf_t cf) {
78 mpz_ptr z = cf_data(cf);
79 mpz_t minusz;
80 mpz_t odd, two;
81 mpz_init(odd); mpz_init(two); mpz_init(minusz);
82 mpz_set_ui(odd, 1);
83 mpz_set_ui(two, 2);
84 mpz_neg(minusz, z);
85 cf_put(cf, odd);
86 while(cf_wait(cf)) {
87 cf_put(cf, z);
88 cf_put(cf, odd);
89 mpz_add_ui(odd, odd, 2);
90 cf_put(cf, minusz);
91 cf_put(cf, two);
93 mpz_clear(odd); mpz_clear(two); mpz_clear(minusz);
94 mpz_clear(z);
95 free(z);
96 return NULL;
99 cf_t cf_new_one_arg(void *(*fun)(cf_t), mpz_t z) {
100 mpz_ptr p = malloc(sizeof(*p));
101 mpz_init(p);
102 mpz_set(p, z);
103 return cf_new(fun, p);
106 struct funarg_s {
107 void *(*fun)(cf_t);
108 mpz_t arg;
110 typedef struct funarg_s *funarg_ptr;
112 static void *one_arg_nonreg(cf_t cf) {
113 funarg_ptr p = cf_data(cf);
114 mpz_t a, b, c, d;
115 mpz_init(a); mpz_init(b); mpz_init(c); mpz_init(d);
116 mpz_set_ui(a, 1); mpz_set_ui(b, 0);
117 mpz_set_ui(c, 0); mpz_set_ui(d, 1);
118 mpz_ptr copy = malloc(sizeof(*copy));
119 mpz_init(copy);
120 mpz_set(copy, p->arg);
121 cf_t nonreg = cf_new(p->fun, copy);
122 cf_t conv = cf_new_nonregular_to_cf(nonreg, a, b, c, d);
123 mpz_t z;
124 mpz_init(z);
125 while(cf_wait(cf)) {
126 cf_get(z, conv);
127 cf_put(cf, z);
129 mpz_clear(z);
130 cf_free(conv);
131 cf_free(nonreg);
132 mpz_clear(a); mpz_clear(b); mpz_clear(c); mpz_clear(d);
134 mpz_clear(p->arg);
135 free(p);
136 return NULL;
139 cf_t cf_new_one_arg_nonreg(void *(*fun)(cf_t), mpz_t z) {
140 funarg_ptr p = malloc(sizeof(*p));
141 p->fun = fun;
142 mpz_init(p->arg);
143 mpz_set(p->arg, z);
144 return cf_new(one_arg_nonreg, p);
147 cf_t cf_new_epow(mpz_t pow) {
148 return cf_new_one_arg_nonreg(exp_expansion, pow);