Ian Jauslin
summaryrefslogtreecommitdiff
blob: 6df4c823299e99c378a583d445ebaf8e58cbd4ec (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
/*
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_root.h"

#include <mpfr.h>

#include "hh_integral.h"

// wrapper for the integration function, used for the Newton scheme
int integration_wrapper(mpfr_t* out, mpfr_t in, void* extra_args){
  mpfr_t tmp;
  hh_params params;
  int ret;

  mpfr_init(tmp);

  mpfr_set(((args_integration*)extra_args)->params.W, in, MPFR_RNDN);
  
  // out = I+
  ret=hh_integrate(out, ((args_integration*)extra_args)->params, ((args_integration*)extra_args)->abcissa, ((args_integration*)extra_args)->weights);
  if(ret<0){
    mpfr_clear(tmp);
    return(ret);
  }

  // clone params (to set sinphi=-sinphi in order to compute I_-)
  mpfr_inits(params.t1, params.t2, params.lambda, params.W, params.sinphi, NULL);
  mpfr_set(params.t1, ((args_integration*)extra_args)->params.t1, MPFR_RNDN);
  mpfr_set(params.t2, ((args_integration*)extra_args)->params.t2, MPFR_RNDN);
  mpfr_set(params.lambda, ((args_integration*)extra_args)->params.lambda, MPFR_RNDN);
  mpfr_set(params.W, ((args_integration*)extra_args)->params.W, MPFR_RNDN);
  mpfr_neg(params.sinphi, ((args_integration*)extra_args)->params.sinphi, MPFR_RNDN);

  // tmp = I-
  ret=hh_integrate(&tmp, params, ((args_integration*)extra_args)->abcissa, ((args_integration*)extra_args)->weights);
  if(ret<0){
    mpfr_clear(tmp);
    mpfr_clears(params.t1, params.t2, params.lambda, params.W, params.sinphi, NULL);
    return(ret);
  }

  mpfr_clears(params.t1, params.t2, params.lambda, params.W, params.sinphi, NULL);

  // out=I+ + I-
  mpfr_add(*out, *out, tmp, MPFR_RNDN);
  //// tmp free

  // tmp = sqrt(3)
  mpfr_sqrt_ui(tmp, 3, MPFR_RNDN);

  // out = W-sqrt(3)*lambda/6*(I+ + I-)
  mpfr_mul(*out, *out, ((args_integration*)extra_args)->params.lambda, MPFR_RNDN);
  mpfr_div_ui(*out, *out, 6, MPFR_RNDN);
  mpfr_mul(*out, *out, tmp, MPFR_RNDN);
  mpfr_sub(*out, ((args_integration*)extra_args)->params.W, *out, MPFR_RNDN);

  // tmp = 3*sqrt(3)*t2*sin(phi)
  mpfr_mul_ui(tmp, tmp, 3, MPFR_RNDN);
  mpfr_mul(tmp, tmp, ((args_integration*)extra_args)->params.t2, MPFR_RNDN);
  mpfr_mul(tmp, tmp, ((args_integration*)extra_args)->params.sinphi, MPFR_RNDN);

  // W+w*3*sqrt(3)*t2*sin(phi)-sqrt(3)*lambda/6*(I+ + I-)
  if(((args_integration*)extra_args)->params.omega==1){
    mpfr_add(*out, *out, tmp, MPFR_RNDN);
  }
  else{
    mpfr_sub(*out, *out, tmp, MPFR_RNDN);
  }
  //// tmp free

  mpfr_clear(tmp);
  return(0);
}

// wrapper for the derivative of the integration function, used for the Newton scheme
int d_integration_wrapper(mpfr_t* out, mpfr_t in, void* extra_args){
  mpfr_t tmp;
  hh_params params;
  int ret;

  mpfr_init(tmp);

  mpfr_set(((args_integration*)extra_args)->params.W, in, MPFR_RNDN);
  
  // out = dI+
  ret=hh_d_integrate(out, ((args_integration*)extra_args)->params, ((args_integration*)extra_args)->abcissa, ((args_integration*)extra_args)->weights);
  if(ret<0){
    mpfr_clear(tmp);
    return(ret);
  }

  // clone params (to set sinphi=-sinphi in order to compute dI_-)
  mpfr_inits(params.t1, params.t2, params.lambda, params.W, params.sinphi, params.phi, NULL);
  mpfr_set(params.t1, ((args_integration*)extra_args)->params.t1, MPFR_RNDN);
  mpfr_set(params.t2, ((args_integration*)extra_args)->params.t2, MPFR_RNDN);
  mpfr_set(params.lambda, ((args_integration*)extra_args)->params.lambda, MPFR_RNDN);
  mpfr_set(params.W, ((args_integration*)extra_args)->params.W, MPFR_RNDN);
  params.omega=((args_integration*)extra_args)->params.omega;
  mpfr_neg(params.sinphi, ((args_integration*)extra_args)->params.sinphi, MPFR_RNDN);
  mpfr_neg(params.phi, ((args_integration*)extra_args)->params.phi, MPFR_RNDN);

  // tmp = dI-
  ret=hh_d_integrate(&tmp, params, ((args_integration*)extra_args)->abcissa, ((args_integration*)extra_args)->weights);
  if(ret<0){
    mpfr_clear(tmp);
    mpfr_clears(params.t1, params.t2, params.lambda, params.W, params.sinphi, params.phi, NULL);
    return(ret);
  }

  mpfr_clears(params.t1, params.t2, params.lambda, params.W, params.sinphi, params.phi, NULL);

  // out=dI+ + dI-
  mpfr_add(*out, *out, tmp, MPFR_RNDN);
  //// tmp free

  // tmp = sqrt(3)
  mpfr_sqrt_ui(tmp, 3, MPFR_RNDN);

  // out = 1-sqrt(3)*lambda/6*(dI+ + dI-)
  mpfr_mul(*out, *out, ((args_integration*)extra_args)->params.lambda, MPFR_RNDN);
  mpfr_div_ui(*out, *out, 6, MPFR_RNDN);
  mpfr_mul(*out, *out, tmp, MPFR_RNDN);
  //// tmp free
  mpfr_ui_sub(*out, 1, *out, MPFR_RNDN);

  mpfr_clear(tmp);

  return(0);
}