-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintegral_decomp.c
210 lines (176 loc) · 6.1 KB
/
integral_decomp.c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include "integral_decomp.h"
#define deb(d)\
printf("deb %f\n",d)
#undef debug_mode
//#define debug_mode 1
mpfr_t* simple_pole_case_c(long pole_order_max, mpfr_t base, mpfr_t pole_position, mpfr_t incomplete_gamma_factor, mpfr_prec_t prec){
mpfr_t* result=malloc(sizeof(mpfr_t)*(pole_order_max+1));
mpfr_t temp1;
mpfr_init2(temp1,prec);
mpfr_t temp2;
mpfr_init2(temp2,prec);
mpfr_t temp3;
mpfr_init2(temp3,prec);
mpfr_t minus_pole_position;
mpfr_init2(minus_pole_position,prec);
mpfr_neg(minus_pole_position,pole_position,MPFR_RNDN);
mpfr_t factorial;
mpfr_init2(factorial,prec);
mpfr_set_ui(factorial,1,MPFR_RNDN);
mpfr_t minus_log_base;
mpfr_init2(minus_log_base,prec);
mpfr_log(minus_log_base,base,prec);
mpfr_neg(minus_log_base,minus_log_base,MPFR_RNDN);
mpfr_ui_div(minus_log_base,1,minus_log_base,MPFR_RNDN);
mpfr_t log_base_power;
mpfr_init2(log_base_power,prec);
mpfr_set(log_base_power,minus_log_base,MPFR_RNDN);
mpfr_set_ui(temp1,0,MPFR_RNDN);
mpfr_mul(temp2,pole_position,incomplete_gamma_factor,MPFR_RNDN);
mpfr_init2(result[0],prec);
mpfr_set(result[0],incomplete_gamma_factor,MPFR_RNDN);
for(long j=1;j<=pole_order_max;j++){
mpfr_init2(result[j],prec);
mpfr_mul(temp1,temp1,pole_position,MPFR_RNDN);
mpfr_mul(temp3,factorial,log_base_power,MPFR_RNDN);
mpfr_add(temp1,temp3,temp1,MPFR_RNDN);
mpfr_add(result[j],temp1,temp2,MPFR_RNDN);
if(j<pole_order_max){
mpfr_mul(temp2,temp2,pole_position,MPFR_RNDN);
mpfr_mul(log_base_power,log_base_power,minus_log_base,MPFR_RNDN);
mpfr_mul_si(factorial,factorial,j,MPFR_RNDN);
}
}
mpfr_clear(temp1);
mpfr_clear(temp2);
mpfr_clear(temp3);
mpfr_clear(minus_pole_position);
mpfr_clear(factorial);
mpfr_clear(minus_log_base);
mpfr_clear(log_base_power);
return result;
}
mpfr_t* double_pole_case_c(long pole_order_max, mpfr_t base, mpfr_t pole_position, mpfr_t incomplete_gamma_factor, mpfr_prec_t prec){
mpfr_t* result=malloc(sizeof(mpfr_t)*(pole_order_max+1));
mpfr_t temp1;
mpfr_init2(temp1,prec);
mpfr_t double_pole_integral;
mpfr_init2(double_pole_integral,prec);
mpfr_t temp2;
mpfr_init2(temp2,prec);
mpfr_t minus_pole_position;
mpfr_init2(minus_pole_position,prec);
mpfr_neg(minus_pole_position,pole_position,MPFR_RNDN);
// mpfr_t factorial;
// mpfr_init2(factorial,prec);
// mpfr_set_ui(factorial,1,MPFR_RNDN);
mpfr_t minus_log_base;
mpfr_init2(minus_log_base,prec);
mpfr_log(minus_log_base,base,prec);
mpfr_neg(minus_log_base,minus_log_base,MPFR_RNDN);
mpfr_ui_div(minus_log_base,1,minus_log_base,MPFR_RNDN);
mpfr_t log_base_power;
mpfr_init2(log_base_power,prec);
mpfr_set(log_base_power,minus_log_base,MPFR_RNDN);
//mpfr_set_ui(temp1,0,MPFR_RNDN);
//mpfr_mul(double_pole_integral,temp1,incomplete_gamma_factor,MPFR_RNDN);
mpfr_log(temp1,base,MPFR_RNDN);
mpfr_mul(double_pole_integral,incomplete_gamma_factor,temp1,MPFR_RNDN);
mpfr_ui_div(temp1,1,minus_pole_position,MPFR_RNDN);
mpfr_add(double_pole_integral,double_pole_integral,temp1,MPFR_RNDN);
//mpfr_printf("before loop: double_pole_integral = %.32RNf\n",double_pole_integral);
for(int i=0;i<=pole_order_max;i++){
mpfr_init2(result[i],prec);
mpfr_set(result[i],double_pole_integral,MPFR_RNDN);
if(i<pole_order_max){
mpfr_mul(double_pole_integral,double_pole_integral,pole_position,MPFR_RNDN);
}
}
#ifdef debug_mode
for(int i=0;i<=pole_order_max;i++){
mpfr_printf("result[%d] = %.16RNf\n",i,result[i],MPFR_RNDN);
}
#endif
mpfr_t * factorial_times_power_lnb;
mpfr_t * single_pole_coeffs;
/*
* x**1/(x+a)**2 case
* */
if(pole_order_max >= 1){
single_pole_coeffs=malloc(sizeof(mpfr_t)*(pole_order_max));
/* x^(j+1)=(
* single_pole_coeffs[0] x^(j-1) +
* single_pole_coeffs[1] x^(j-2) + ... +
* single_pole_coeffs[j-1] x^0
* )*(x-a)^2 +
*
* single_pole_coeffs[j](x-a) * ((x-a) + a)
*
* + a^(j+1)
*
* => single_pole_coeffs[j+1]
*
* single_pole_coeffs[j+1] = single_pole_coeffs[j]*a + a^j+1
* single_pole_coeffs[0] =
* */
if(pole_order_max>=2){
factorial_times_power_lnb=malloc(sizeof(mpfr_t)*(pole_order_max-1));
mpfr_init2(factorial_times_power_lnb[0],prec);
mpfr_set(factorial_times_power_lnb[0],minus_log_base,MPFR_RNDN);
}
mpfr_set(temp1,pole_position,MPFR_RNDN);
/* below temp1 is used as pole_position^j*/
mpfr_init2(single_pole_coeffs[0],prec);
mpfr_set_ui(single_pole_coeffs[0], 1 ,MPFR_RNDN);
mpfr_add(result[1],result[1],incomplete_gamma_factor,MPFR_RNDN);
for(int j=1;j<=pole_order_max-1;j++){
mpfr_init2(single_pole_coeffs[j],prec);
mpfr_mul(single_pole_coeffs[j],single_pole_coeffs[j-1],pole_position,MPFR_RNDN);
mpfr_add(single_pole_coeffs[j],single_pole_coeffs[j],temp1,MPFR_RNDN);
mpfr_mul(temp2,single_pole_coeffs[j],incomplete_gamma_factor,MPFR_RNDN);
mpfr_add(result[j+1],result[j+1],temp2,MPFR_RNDN);
if(j<=pole_order_max-2){
mpfr_init2(factorial_times_power_lnb[j],prec);
mpfr_mul(temp1,temp1,pole_position,MPFR_RNDN);
mpfr_mul(factorial_times_power_lnb[j],factorial_times_power_lnb[j-1],minus_log_base,MPFR_RNDN);
mpfr_mul_ui(factorial_times_power_lnb[j],factorial_times_power_lnb[j],j,MPFR_RNDN);
}
}
}
#ifdef debug_mode
for(int j=0;j<=pole_order_max-1;j++){
mpfr_printf("single_pole_coeffs[%d] = %.16RNf\n",j,single_pole_coeffs[j]);
}
#endif
#ifdef debug_mode
for(int j=0;j<=pole_order_max-2;j++){
mpfr_printf("factorial_times_power_lnb[%d] = %.16RNf\n",j,factorial_times_power_lnb[j]);
}
#endif
for(int j=0;j<=pole_order_max-2;j++){
for(int k=0;k<=j;k++){
mpfr_mul(temp2,factorial_times_power_lnb[k],single_pole_coeffs[j-k],MPFR_RNDN);
mpfr_add(result[j+2],result[j+2],temp2,MPFR_RNDN);
}
}
for(int i=0;i<=pole_order_max-1;i++){
mpfr_clear(single_pole_coeffs[i]);
}
for(int i=0;i<=pole_order_max-2;i++){
mpfr_clear(factorial_times_power_lnb[i]);
}
if(pole_order_max > 0){
free(single_pole_coeffs);
}
if(pole_order_max > 1){
free(factorial_times_power_lnb);
}
mpfr_clear(temp1);
mpfr_clear(double_pole_integral);
mpfr_clear(temp2);
mpfr_clear(minus_pole_position);
//mpfr_clear(factorial);
mpfr_clear(minus_log_base);
mpfr_clear(log_base_power);
return result;
}