Ian Jauslin
summaryrefslogtreecommitdiff
blob: f8eda106281bc9574cc3410e94e49a4f67fc3131 (plain)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Copyright 2016 Ian Jauslin

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "hh_integral.h"

#include <stdarg.h>
// define MPFR_USE_VA_LIST to enable the use of mpfr_inits and mpfr_clears
#define MPFR_USE_VA_LIST
#include <mpfr.h>
#include <libinum.h>

int hh_integrate(mpfr_t* out, hh_params params, array_mpfr abcissa, array_mpfr weights){
  mpfr_t lower, upper;
  // arguments for first integral
  hh_argsint1 args1;
  int ret;

  args1.params=params;
  args1.abcissa=abcissa;
  args1.weights=weights;

  mpfr_inits(lower, upper, NULL);

  // compute the boundaries of the integral over theta
  // pi/6
  mpfr_const_pi(upper, MPFR_RNDN);
  mpfr_div_ui(upper, upper, 6, MPFR_RNDN);
  // -pi/6
  mpfr_neg(lower, upper, MPFR_RNDN);

  // integrate
  ret=integrate_gauss_mpfr(out, &hh_integrand1, lower, upper, abcissa, weights, &args1);

  mpfr_clears(lower, upper, NULL);

  return(ret);
}

// integrand of the integral over theta
int hh_integrand1(mpfr_t* out, mpfr_t theta, void* args){
  hh_argsint2 args2;
  mpfr_t lower, upper;
  int ret;

  mpfr_inits(upper, lower, NULL);

  // recover parameters
  args2.params=((hh_argsint1*)args)->params;
  args2.theta=theta;

  // boundaries of the integral over rho
  // 1/cos(theta-pi/6)
  mpfr_const_pi(upper, MPFR_RNDN);
  mpfr_div_ui(upper, upper, 6, MPFR_RNDN);
  mpfr_sub(upper, theta, upper, MPFR_RNDN);
  mpfr_cos(upper, upper, MPFR_RNDN);
  mpfr_ui_div(upper, 1, upper, MPFR_RNDN);

  // 0
  mpfr_set_ui(lower, 0, MPFR_RNDN);

  ret=integrate_gauss_mpfr(out, &hh_integrand2, lower, upper, ((hh_argsint1*)args)->abcissa, ((hh_argsint1*)args)->weights, &args2);

  mpfr_clears(upper, lower, NULL);

  return(ret);
}

// integrand of the integral over rho
int hh_integrand2(mpfr_t* out, mpfr_t rho, void* args){
  mpfr_t tmp1, tmp2, tmp3;

  mpfr_inits(tmp1, tmp2, tmp3, NULL);

  // out = Omega^2
  // tmp1 = alpha2
  hh_Omega2_alpha2(*out, tmp1, rho, ((hh_argsint2*)args)->theta, tmp2, tmp3);
  // tmp1 = m
  hh_m(tmp1, ((hh_argsint2*)args)->params.W, ((hh_argsint2*)args)->params.t2, ((hh_argsint2*)args)->params.sinphi, tmp1);
  // out = xi^2
  hh_xi2(*out, *out, tmp1, ((hh_argsint2*)args)->params.t1, tmp2);

  // out = rho/sqrt(xi^2)*m
  mpfr_sqrt(*out, *out, MPFR_RNDN);
  mpfr_div(*out, rho, *out, MPFR_RNDN);
  mpfr_mul(*out, *out, tmp1, MPFR_RNDN);

  mpfr_clears(tmp1, tmp2, tmp3, NULL);

  return(0);
}

// derivative
int hh_d_integrate(mpfr_t* out, hh_params params, array_mpfr abcissa, array_mpfr weights){
  mpfr_t lower, upper;
  // arguments for first integral
  hh_argsint1 args1;
  int ret;

  args1.params=params;
  args1.abcissa=abcissa;
  args1.weights=weights;

  mpfr_inits(lower, upper, NULL);

  // compute the boundaries of the integral over theta
  // pi/6
  mpfr_const_pi(upper, MPFR_RNDN);
  mpfr_div_ui(upper, upper, 6, MPFR_RNDN);
  // -pi/6
  mpfr_neg(lower, upper, MPFR_RNDN);

  // integrate
  ret=integrate_gauss_mpfr(out, &hh_d_integrand1, lower, upper, abcissa, weights, &args1);

  mpfr_clears(lower, upper, NULL);

  return(ret);
}

// derivative of the integrand of the integral over theta
int hh_d_integrand1(mpfr_t* out, mpfr_t theta, void* args){
  hh_argsint2 args2;
  mpfr_t lower, upper;
  int ret;

  mpfr_inits(lower, upper, NULL);

  // recover parameters
  args2.params=((hh_argsint1*)args)->params;
  args2.theta=theta;

  // boundaries of the integral over rho
  // 1/cos(theta-pi/6)
  mpfr_const_pi(upper, MPFR_RNDN);
  mpfr_div_ui(upper, upper, 6, MPFR_RNDN);
  mpfr_sub(upper, theta, upper, MPFR_RNDN);
  mpfr_cos(upper, upper, MPFR_RNDN);
  mpfr_ui_div(upper, 1, upper, MPFR_RNDN);

  // 0
  mpfr_set_ui(lower, 0, MPFR_RNDN);

  ret=integrate_gauss_mpfr(out, &hh_d_integrand2, lower, upper, ((hh_argsint1*)args)->abcissa, ((hh_argsint1*)args)->weights, &args2);

  mpfr_clears(lower, upper, NULL);

  return(ret);
}

// derivative of the integrand of the integral over rho
int hh_d_integrand2(mpfr_t* out, mpfr_t rho, void* args){
  mpfr_t tmp1, tmp2, tmp3;

  mpfr_inits(tmp1, tmp2, tmp3, NULL);

  // out = Omega^2
  // tmp1 = alpha2
  hh_Omega2_alpha2(*out, tmp1, rho, ((hh_argsint2*)args)->theta, tmp2, tmp3);
  // tmp1 = m
  hh_m(tmp1, ((hh_argsint2*)args)->params.W, ((hh_argsint2*)args)->params.t2, ((hh_argsint2*)args)->params.sinphi, tmp1);
  // out = xi^2
  hh_xi2(*out, *out, tmp1, ((hh_argsint2*)args)->params.t1, tmp2);

  // tmp2 = 1-m^2/xi^2
  mpfr_pow_ui(tmp2, tmp1, 2,MPFR_RNDN);
  mpfr_div(tmp2, tmp2, *out, MPFR_RNDN);
  mpfr_ui_sub(tmp2, 1, tmp2, MPFR_RNDN);

  // out = rho/sqrt(xi^2)*(1-m^2/xi^2)
  mpfr_sqrt(*out, *out, MPFR_RNDN);
  mpfr_div(*out, rho, *out, MPFR_RNDN);
  mpfr_mul(*out, *out, tmp2, MPFR_RNDN);

  mpfr_clears(tmp1, tmp2, tmp3, NULL);

  return(0);
}

// Omega^2 and alpha_2
// provide two initialized tmp mpfr_t's
// Omega2 and alpha2 must be initialized
int hh_Omega2_alpha2(mpfr_t Omega2, mpfr_t alpha2, mpfr_t rho, mpfr_t theta, mpfr_t tmp1, mpfr_t tmp2){
  // Omega2 and alpha2 will be used as tmp variables whenever possible

  // Omega2 = pi
  mpfr_const_pi(Omega2, MPFR_RNDN);

  // tmp1 = pi/sqrt(3)*rho
  mpfr_sqrt_ui(tmp1, 3, MPFR_RNDN);
  mpfr_div(tmp1, Omega2, tmp1, MPFR_RNDN);
  mpfr_mul(tmp1, tmp1, rho, MPFR_RNDN);

  // alpha2 = cos(theta)
  mpfr_cos(alpha2, theta, MPFR_RNDN);

  // tmp1 = cos(pi/sqrt(3)*rho*cos(theta))
  mpfr_mul(tmp1, tmp1, alpha2, MPFR_RNDN);
  //// alpha2 free
  mpfr_cos(tmp1, tmp1, MPFR_RNDN);

  // alpha2 = pi/3*(1+rho*sin(theta))
  mpfr_sin(alpha2, theta, MPFR_RNDN);
  mpfr_mul(alpha2, alpha2, rho, MPFR_RNDN);
  mpfr_add_ui(alpha2, alpha2, 1, MPFR_RNDN);
  mpfr_div_ui(alpha2, alpha2, 3, MPFR_RNDN);
  mpfr_mul(alpha2, alpha2, Omega2, MPFR_RNDN);
  //// Omega2 free

  // Omega2 = cos(pi/3*(1+rho*sin(theta)))
  mpfr_cos(Omega2, alpha2, MPFR_RNDN);

  // tmp2 = sin(pi/3*(1+rho*sin(theta)))
  mpfr_sin(tmp2, alpha2, MPFR_RNDN);
  //// alpha2 free

  // alpha2 = -2*sin(pi/3*(1+rho*sin(theta)))*(cos(pi/3*(1+rho*sin(theta)))+cos(pi/sqrt(3)*rho*cos(theta)))
  mpfr_add(alpha2, Omega2, tmp1, MPFR_RNDN);
  mpfr_mul(alpha2, alpha2, tmp2, MPFR_RNDN);
  //// tmp2 free
  mpfr_mul_si(alpha2, alpha2, -2, MPFR_RNDN);

  // tmp1 = cos(pi/3*(1+rho*sin(theta)))-cos(pi/sqrt(3)*rho*cos(theta))
  mpfr_sub(tmp1, Omega2, tmp1, MPFR_RNDN);
  // Omega2 = 1+4*cos(pi/3*(1+rho*sin(theta)))*(cos(pi/3*(1+rho*sin(theta)))-cos(pi/sqrt(3)*rho*cos(theta)))
  mpfr_mul(Omega2, Omega2, tmp1, MPFR_RNDN);
  //// tmp1 free
  mpfr_mul_ui(Omega2, Omega2, 4, MPFR_RNDN);
  mpfr_add_ui(Omega2, Omega2, 1, MPFR_RNDN);

  return(0);
}

// m
// out must be initialized
// out and alpha2 can point to the same number
int hh_m(mpfr_t out, mpfr_t W, mpfr_t t2, mpfr_t sinphi, mpfr_t alpha2){
  // out = W-2*t2*sinphi*alpha2
  mpfr_mul(out, alpha2, sinphi, MPFR_RNDN);
  mpfr_mul(out, out, t2, MPFR_RNDN);
  mpfr_mul_ui(out, out, 2, MPFR_RNDN);
  mpfr_sub(out, W, out, MPFR_RNDN);
  return(0);
}

// xi^2
// provide one initialized tmp mpfr_t
// out must be initialized
// out and Omega2 can point to the same number
// tmp and m can point to the same number
int hh_xi2(mpfr_t out, mpfr_t Omega2, mpfr_t m, mpfr_t t1, mpfr_t tmp){
  // out = t1^2*Omega^2
  mpfr_mul(out, Omega2, t1, MPFR_RNDN);
  mpfr_mul(out, out, t1, MPFR_RNDN);

  // tmp = m^2
  mpfr_pow_ui(tmp, m, 2, MPFR_RNDN);

  // out = m^2+t1^2*Omega^2
  mpfr_add(out, out, tmp, MPFR_RNDN);

  return(0);
}