forked from rikigigi/analisi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
specialfunctions.h
371 lines (320 loc) · 10.8 KB
/
specialfunctions.h
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#ifndef ASSOCIATEDLEGENDREPOLY_H
#define ASSOCIATEDLEGENDREPOLY_H
#include <iostream>
#include <cmath>
#define PI 3.14159265358979323846264338327950288419716939937510
namespace SpecialFunctions {
template <int m,class T>
void inline chebyshevRecursiveAngleMultiplication(const T &x,const T &y, T * res) noexcept{
res[0]=1.0; //cos(0*teta)
res[1]=0.0; //sin(0*teta)
res[2]=x; //cos(1*teta)
res[3]=y; //sin(1*teta)
for (int i=2;i<=m;++i){ //recursion for sin,cos (i*teta)
int j=0;
res[i*2+j]=2*x*res[(i-1)*2+j]-res[(i-2)*2+j];
j=1;
res[i*2+j]=2*x*res[(i-1)*2+j]-res[(i-2)*2+j];
}
}
template <int m,class T>
void print_chebyshevRecursiveAngleMultiplication( T * res,std::ostream & out) {
for (int i=0;i<=m;++i){
out << "[cos("<<i<<"*teta)"<< ",sin("<<i<<"*teta)] = ["<<res[i*2]<<","<<res[i*2+1]<<"]"<<std::endl;
}
}
template <class T>
constexpr T realSpericalHarmonics_coeff(long int l, long int m, long int cur_v, T value=T(1)) {
if (cur_v==0) {
return realSpericalHarmonics_coeff(l,m,1,value);
}
if (m==0) {
return sqrt(T(2*l+1)/(4*PI));
} else if (m<0) {
return realSpericalHarmonics_coeff(l,-m,cur_v,value);
}
if (cur_v<=l+m) {
return realSpericalHarmonics_coeff<T>(l,m,cur_v+1,value*cur_v);
} else {
return sqrt(T(2*l+1)/(4*PI)*2 / value)*pow(-1,m);
}
}
void print_realSpericalHarmonics_coeff(int l,std::ostream & out) {
for (int j=0;j<=l;++j) {
for (int i=-j;i<=j;++i){
out << "(l,m) = ("<<j<<","<<i <<"): "<<realSpericalHarmonics_coeff<double>(j,i,j-abs(i)) <<std::endl;
}
}
}
namespace MultiValStatic {
template <int l, class T>
struct MultiVal {
T val[l+1]; // only positive m + zero
T val_minus[l]; //negative values of m
MultiVal<l-1,T> val_;
template <typename ...Args>
void inline apply_f ( T f(int,int,T,Args...) , Args...args) noexcept {
for (int m=0;m<l+1;++m) {
val[m]=f(l,m,val[m],args...);
}
for (int m=0;m<l;++m) {
val_minus[m]=f(l,-1-m,val_minus[m],args...);
}
val_.apply_f(f,args...);
}
void inline running_average_m_zero_vector(T* average, const T & counter) const {
average[0]+=(val[0]-average[0])/counter;
val_.running_average_m_zero_vector(average+1,counter);
}
void inline sum_in_m_zero() noexcept {
for (int m=0;m<l;++m) { //negative m
val[0]+=val_minus[m];
}
for (int m=0;m<l;++m) { //positives m
val[0]+=val[m+1];
}
val_.sum_in_m_zero();
}
void inline copy_mplus_mminus() noexcept{
for (int m=1;m<l+1;++m) {
val_minus[m-1]=val[m];
}
val_.copy_mplus_mminus();
}
void print(std::ostream & out) {
val_.print(out);
for (int m=0;m<l;m++){
out << "(l,m) = ("<<l<<","<<-(l-m) <<"): "<< val_minus[l-m-1]<<std::endl;
}
for (int m=0;m<l+1;m++){
out << "(l,m) = ("<<l<<","<<m <<"): "<< val[m]<<std::endl;
}
}
template<int j>
constexpr MultiVal<j,T> & valm() {if constexpr (j==l) return *this; else return val_.template valm<j>();}
};
template <class T>
struct MultiVal<0,T> {
T val[1];
template <typename ...Args>
inline void apply_f ( T f(int,int,T,Args...) , Args...args) noexcept{
val[0]=f(0,0,val[0],args...);
}
void inline running_average_m_zero_vector(T* average, const T & counter) const {
average[0]+=(val[0]-average[0])/counter;
}
void inline sum_in_m_zero() noexcept {}
void copy_mplus_mminus(){}
void print(std::ostream & out) {
out << "(l,m) = ("<<0<<","<<0 <<"): "<< val[0]<<std::endl;
}
template<int j>
constexpr MultiVal<j,T> & valm() noexcept{
static_assert (j==0, "You specified a wrong value of j!" );
return *this;
}
};
}
namespace MultiValDynamic {
template <int l, class T>
struct MultiVal {
T *val; // only positive m + zero
T *val_minus; //negative values of m
MultiVal<l-1,T> val_;
void inline init(T * array) noexcept{
val_minus=array;
val=array+l;
val_.init(val+l+1);
}
template <typename ...Args>
void inline apply_f ( T f(int,int,T,Args...) , Args...args) noexcept {
for (int m=0;m<l+1;++m) {
val[m]=f(l,m,val[m],args...);
}
for (int m=0;m<l;++m) {
val_minus[m]=f(l,-1-m,val_minus[m],args...);
}
val_.apply_f(f,args...);
}
void inline running_average_m_zero_vector(T* average, const T & counter) const {
average[0]+=(val[0]-average[0])/counter;
val_.running_average_m_zero_vector(average+1,counter);
}
void inline copy_mplus_mminus() noexcept{
for (int m=1;m<l+1;++m) {
val_minus[m-1]=val[m];
}
val_.copy_mplus_mminus();
}
void inline sum_in_m_zero() noexcept {
for (int m=0;m<l;++m) { //negative m
val[0]+=val_minus[m];
}
for (int m=0;m<l;++m) { //positives m
val[0]+=val[m+1];
}
val_.sum_in_m_zero();
}
void print(std::ostream & out) {
val_.print(out);
for (int m=0;m<l;m++){
out << "(l,m) = ("<<l<<","<<-(l-m) <<"): "<< val_minus[l-m-1]<<std::endl;
}
for (int m=0;m<l+1;m++){
out << "(l,m) = ("<<l<<","<<m <<"): "<< val[m]<<std::endl;
}
}
template<int j>
constexpr MultiVal<j,T> & valm() {if constexpr (j==l) return *this; else return val_.template valm<j>();}
};
template <class T>
struct MultiVal<0,T> {
T *val;
void inline init(T * array) noexcept{
val=array;
}
template <typename ...Args>
inline void apply_f ( T f(int,int,T,Args...) , Args...args) noexcept{
val[0]=f(0,0,val[0],args...);
}
void inline running_average_m_zero_vector(T* average, const T & counter) const {
average[0]+=(val[0]-average[0])/counter;
}
void inline sum_in_m_zero() noexcept {}
void copy_mplus_mminus()noexcept{}
void print(std::ostream & out) {
out << "(l,m) = ("<<0<<","<<0 <<"): "<< val[0]<<std::endl;
}
template<int j>
constexpr MultiVal<j,T> & valm() noexcept{
static_assert (j==0, "You specified a wrong value of j!" );
return *this;
}
};
}
//selector for memory model
template <int l, class T, bool dynamic>
struct MultiVal;
template <int l, class T>
struct MultiVal<l,T,false> : public MultiValStatic::MultiVal<l,T> {};
template <int l, class T>
struct MultiVal<l,T,true> : public MultiValDynamic::MultiVal<l,T> {};
template <int lmax,int l, int m, class T, bool dynamic>
struct AssociatedLegendrePoly
{
static inline void calc1(const T &x, MultiVal<lmax,T,dynamic> & res) noexcept {
/* l+1,l step:
* Implements the following:
* P^{l}_{l+1}=(2l+1) x P^l_l(x)
*/
res.template valm<l>().val[m]=
res.template valm<l-1>().val[m] *x*(2*m+1);
if constexpr (l<lmax){
AssociatedLegendrePoly<lmax,l+1,m,T,dynamic>::calc2(x,res); //start l+1,m recursion
}
}
static inline void calc2(const T &x, MultiVal<lmax,T,dynamic> & res) noexcept{
/*
* l+1,m recursion (that can go forever)
* P^m_{l} = (x(2l - 1)P^m_{l-1} - (l + m - 1)P^m_{l-2}) / (l - m)
*/
res.template valm<l>().val[m]=(res.template valm<l-1>().val[m] *x*(2*l-1) - res.template valm<l-2>().val[m] *(l+m-1))/
(l-m) ;
if constexpr(l<lmax) {
AssociatedLegendrePoly<lmax,l+1,m,T,dynamic>::calc2(x,res); //continue l+1,m recursion
}
}
};
template <int lmax, int l,class T, bool dynamic>
struct AssociatedLegendrePoly<lmax,l,l,T,dynamic> {
static inline void calc (const T&x, MultiVal<lmax,T,dynamic> & res) noexcept {
/* l,l recursion:
* Implements the following:
* P^{l}_{l}=-(2l-1)\sqrt{1-x^2}P^{l-1}_{l-1}(x)
*/
res.template valm<l>().val[l]= -(2*l-1)* sqrt(1-x*x) * res.template valm<l-1>().val[l-1];
if constexpr (lmax>l){
AssociatedLegendrePoly<lmax,l+1,l+1,T,dynamic>::calc(x,res);//continue l,l recursion
AssociatedLegendrePoly<lmax,l+1,l,T,dynamic>::calc1(x,res);//start a new l+1,l recursion
}
}
};
template <int lmax, class T, bool dynamic>
struct AssociatedLegendrePoly<lmax,0,0,T,dynamic> {
static inline void calc (const T&x, MultiVal<lmax,T,dynamic> & res) noexcept {
//begin with l=0 and go up till lmax
//store the result while recursing in res
res.template valm<0>().val[0]=1.0;
if constexpr (lmax>0){
AssociatedLegendrePoly<lmax,1,1,T,dynamic>::calc(x,res);//start l,l recursion
AssociatedLegendrePoly<lmax,1,0,T,dynamic>::calc1(x,res);//start l+1,l recursion
}
}
};
template<int L, class T,bool plus=true,int l=L, bool dynamic>
inline T* get_multival (int j, MultiVal<L,T,dynamic> & val) noexcept {
if (j==l) {
if constexpr (plus)
return val.template valm<l>().val;
else{
if constexpr (l>0){
return val.template valm<l>().val_minus;
} else {
return nullptr;
}
}
} else {
if constexpr (l>0){
return get_multival<L,T,plus,l-1>(j,val);
} else {
return nullptr;
}
}
}
template <int l,class T, bool dynamic,bool cartesian=false>
class SphericalHarmonics{
public:
SphericalHarmonics(T x, T y, T z, T* cheby, T* result) noexcept : cheby{cheby} {
if constexpr (dynamic) {
val.init(result);
}
if constexpr(cartesian) {
T rxy=sqrt(x*x+y*y);
T r=sqrt(x*x+y*y+z*z);
cost=z/r;
sinp=y/rxy;
cosp=x/rxy;
} else {
cost=x; sinp=y; cosp=z;
}
}
inline
T* get_l_mplus(int j) const noexcept{
return get_multival<l,T,true>(j,val);
}
inline
T* get_l_mminus(int j) const noexcept{
return get_multival<l,T,false>(j,val);
}
inline
MultiVal<l,T,dynamic> & get_val() noexcept {return val;}
void inline calc() noexcept{
AssociatedLegendrePoly<l,0,0,T,dynamic>::calc(cost,val);
val.copy_mplus_mminus();
chebyshevRecursiveAngleMultiplication<l>(cosp,sinp,cheby);
val.apply_f(&mixall,cheby);
}
private:
T static inline mixall(int ll, int m, T val, T * cheby) noexcept {
if (m>=0){
return val*realSpericalHarmonics_coeff<T>(ll,m,ll-m+1)*cheby[2*m];
} else {
return val*realSpericalHarmonics_coeff<T>(ll,m,ll-abs(m)+1)*cheby[2*abs(m)+1];
}
}
T * cheby;
T cost,sinp,cosp;
MultiVal<l,T,dynamic> val;
};
}
#endif // ASSOCIATEDLEGENDREPOLY_H