source: Sophya/trunk/SophyaLib/Samba/lambdaBuilder.cc@ 2959

Last change on this file since 2959 was 2959, checked in by ansari, 19 years ago

correction fonctions optimisees calcul Ylm en version <r_4> - Reza 1/06/2006

File size: 14.7 KB
Line 
1#include "sopnamsp.h"
2#include "lambdaBuilder.h"
3#include "nbconst.h"
4
5
6/*!
7 \class SOPHYA::Legendre
8generate Legendre polynomials : use in two steps :
9
10a) instanciate Legendre(\f$x\f$, \f$lmax\f$) ; \f$x\f$ is the value for wich Legendre polynomials will be required (usually equal to \f$\cos \theta\f$) and \f$lmax\f$ is the MAXIMUM value of the order of polynomials wich will be required in the following code (all polynomials, from \f$l=0 to lmax\f$, are computed once for all by an iterative formula).
11
12b) get the value of Legendre polynomial for a particular value of \f$l\f$ by calling the method getPl.
13
14*/
15
16
17Legendre::Legendre(r_8 x, int_4 lmax)
18{
19 if (fabs(x) > 1. )
20 {
21 throw RangeCheckError("variable for Legendre polynomials must have modules inferior to 1" );
22 }
23 x_ = x;
24 array_init(lmax);
25}
26
27/*! \fn void SOPHYA::Legendre::array_init(int_4 lmax)
28
29compute all \f$P_l(x,l_{max})\f$ for \f$l=1,l_{max}\f$
30*/
31void Legendre::array_init(int_4 lmax)
32{
33 lmax_ = lmax;
34 Pl_.ReSize(lmax_+1);
35 Pl_(0)=1.;
36 if (lmax>0) Pl_(1)=x_;
37 for (int k=2; k<Pl_.NElts(); k++)
38 {
39 Pl_(k) = ( (2.*k-1)*x_*Pl_(k-1)-(k-1)*Pl_(k-2) )/k;
40 }
41}
42
43TriangularMatrix<r_8> LambdaLMBuilder::a_recurrence_ = TriangularMatrix<r_8>();
44TriangularMatrix<r_8> LambdaLMBuilder::lam_fact_ = TriangularMatrix<r_8>();
45TVector<r_8>* LambdaLMBuilder::normal_l_ = NULL;
46
47
48
49/*! \class SOPHYA::LambdaLMBuilder
50
51
52This class generate the coefficients :
53\f[
54 \lambda_l^m=\sqrt{\frac{2l+1}{4\pi}\frac{(l-m)!}{(l+m)!}}
55 P_l^m(\cos{\theta})
56\f]
57where \f$P_l^m\f$ are the associated Legendre polynomials. The above coefficients contain the theta-dependance of spheric harmonics :
58\f[
59 Y_{lm}(\cos{\theta})=\lambda_l^m(\cos{\theta}) e^{im\phi}.
60\f]
61
62Each object has a fixed theta (radians), and maximum l and m to be calculated
63(lmax and mmax).
64 use the class in two steps :
65a) instanciate LambdaLMBuilder(\f$\theta\f$, \f$lmax\f$, \f$mmax\f$) ; \f$lmax\f$ and \f$mmax\f$ are MAXIMUM values for which \f$\lambda_l^m\f$ will be required in the following code (all coefficients, from \f$l=0 to lmax\f$, are computed once for all by an iterative formula).
66b) get the values of coefficients for particular values of \f$l\f$ and \f$m\f$ by calling the method lamlm.
67*/
68
69
70LambdaLMBuilder::LambdaLMBuilder(r_8 theta,int_4 lmax, int_4 mmax)
71 {
72 cth_=cos(theta);
73 sth_=sin(theta);
74 array_init(lmax, mmax);
75 }
76LambdaLMBuilder::LambdaLMBuilder(r_8 costet, r_8 sintet,int_4 lmax, int_4 mmax)
77 {
78 cth_=costet;
79 sth_=sintet;
80 array_init(lmax, mmax);
81 }
82void LambdaLMBuilder::array_init(int lmax, int mmax)
83 {
84 updateArrayRecurrence(lmax);
85
86 lmax_=lmax;
87 mmax_=mmax;
88 r_8 bignorm2 = 1.e268; // = 1e-20*1.d288
89
90 lambda_.ReSizeRow(lmax_+1);
91
92 r_8 lam_mm = 1. / sqrt(4.*Pi) *bignorm2;
93
94 for (int m=0; m<=mmax_;m++)
95 {
96
97
98 lambda_(m,m)= lam_mm / bignorm2;
99
100 r_8 lam_0=0.;
101 r_8 lam_1=1. /bignorm2 ;
102 // r_8 a_rec = LWK->a_recurr(m,m);
103 r_8 a_rec = a_recurrence_(m,m);
104 r_8 b_rec = 0.;
105 for (int l=m+1; l<=lmax_; l++)
106 {
107 r_8 lam_2 = (cth_*lam_1-b_rec*lam_0)*a_rec;
108 lambda_(l,m) = lam_2*lam_mm;
109 b_rec=1./a_rec;
110 // a_rec= LWK->a_recurr(l,m);
111 a_rec= a_recurrence_(l,m);
112 lam_0 = lam_1;
113 lam_1 = lam_2;
114 }
115
116 lam_mm = -lam_mm*sth_* sqrt( (2.*m+3.)/ (2.*m+2.) );
117
118 }
119 }
120
121/*!
122 \brief : Specialized/optimized static function for fast spherical harmonic transform.
123 Computes bm(m) = Sum_l>=m [ lambda(l,m) * alm(l,m) ]
124 See SphericalTransformServer<T>::GenerateFromAlm(map, pixsize, alm)
125*/
126void LambdaLMBuilder::ComputeBmFrAlm(r_8 theta,int_4 lmax, int_4 mmax,
127 const Alm<r_8>& alm, Bm< complex<r_8> >& bm)
128{
129 updateArrayRecurrence(lmax);
130 r_8 cth = cos(theta);
131 r_8 sth = sin(theta);
132
133 r_8 bignorm2 = 1.e268; // = 1e-20*1.d288
134
135 r_8 lam_mm = 1. / sqrt(4.*Pi) *bignorm2;
136 r_8 lam_0, lam_1, lam_2;
137 register r_8 a_rec, b_rec;
138
139 const complex<r_8>* almp = alm.columnData(0);
140 r_8* arecp = a_recurrence_.columnData(0);
141
142 int_4 l, m;
143
144 for (m=0; m<=mmax;m++) {
145 //MOV^ const complex<r_8>* almp = alm.columnData(m);
146 complex<r_8>* bmp = &(bm(m));
147
148 *bmp = (lam_mm / bignorm2)*(*almp); almp++;
149
150 lam_0=0.;
151 lam_1=1. /bignorm2 ;
152
153 a_rec = *arecp; arecp++; // a_recurrence_(m,m);
154 b_rec = 0.;
155 for (l=m+1; l<=lmax; l++) {
156 lam_2 = (cth*lam_1-b_rec*lam_0)*a_rec;
157
158 //DEL lambda_(l,m) = lam_2*lam_mm;
159 *bmp += lam_2*lam_mm*(*almp); almp++;
160 b_rec=1./a_rec;
161 // a_rec= LWK->a_recurr(l,m);
162 a_rec= *arecp; arecp++; // a_recurrence_(l,m);
163 lam_0 = lam_1;
164 lam_1 = lam_2;
165 }
166 lam_mm = -lam_mm*sth* sqrt( (2.*m+3.)/ (2.*m+2.) );
167 }
168}
169
170/*!
171 \brief : Specialized/optimized static function for fast spherical harmonic transform.
172*/
173void LambdaLMBuilder::ComputeBmFrAlm(r_8 theta,int_4 lmax, int_4 mmax,
174 const Alm<r_4>& alm, Bm< complex<r_4> >& bm)
175{
176 Alm<r_8> alm8(alm.Lmax());
177 for(sa_size_t k=0; k<alm8.Size(); k++)
178 alm8(k) = complex<r_8>((r_8)alm(k).real() , (r_8)alm(k).imag());
179 Bm< complex<r_8> > bm8(bm.Mmax());
180 ComputeBmFrAlm(theta, lmax, mmax, alm8, bm8);
181 for(sa_size_t kk=-bm.Mmax(); kk<=bm.Mmax(); kk++)
182 bm(kk)= complex<r_4>((r_4)bm8(kk).real() , (r_4)bm8(kk).imag());
183 return;
184}
185
186
187/*!
188 \brief : Specialized/optimized static function for fast spherical harmonic transform.
189 Computes alm(l,m) = Sum_l>=m [ lambda(l,m) * phase(l,m) ]
190 See SphericalTransformServer<T>::carteVersAlm(SphericalMap<T>& map, nlmax, ctcut, alm)
191*/
192void LambdaLMBuilder::ComputeAlmFrPhase(r_8 theta,int_4 lmax, int_4 mmax,
193 TVector< complex<r_8> >& phase, Alm<r_8> & alm)
194{
195 updateArrayRecurrence(lmax);
196 r_8 cth = cos(theta);
197 r_8 sth = sin(theta);
198
199 r_8 bignorm2 = 1.e268; // = 1e-20*1.d288
200
201 r_8 lam_mm = 1. / sqrt(4.*Pi) *bignorm2;
202 r_8 lam_0, lam_1, lam_2;
203 register r_8 a_rec, b_rec;
204
205 complex<r_8>* almp = alm.columnData(0);
206 r_8* arecp = a_recurrence_.columnData(0);
207
208 int_4 l, m;
209 for (m=0; m<=mmax;m++) {
210 //MOV^ complex<r_8>* almp = alm.columnData(m);
211 complex<r_8> phi = phase(m);
212
213 *almp += (lam_mm / bignorm2)*phi; almp++;
214
215 lam_0=0.;
216 lam_1=1. /bignorm2 ;
217
218 //MOV^ r_8* arecp = a_recurrence_.columnData(m);
219 a_rec = *arecp; arecp++; // a_recurrence_(m,m);
220 b_rec = 0.;
221 for (l=m+1; l<=lmax; l++) {
222 lam_2 = (cth*lam_1-b_rec*lam_0)*a_rec;
223
224 //DEL lambda_(l,m) = lam_2*lam_mm;
225 *almp += (lam_2*lam_mm) * phi; almp++;
226
227 b_rec=1./a_rec;
228 // a_rec= LWK->a_recurr(l,m);
229 a_rec= *arecp; arecp++; // a_recurrence_(l,m);
230 lam_0 = lam_1;
231 lam_1 = lam_2;
232 }
233 lam_mm = -lam_mm*sth* sqrt( (2.*m+3.)/ (2.*m+2.) );
234 }
235}
236
237/*!
238 \brief : Specialized/optimized static function for fast spherical harmonic transform.
239 Computes alm(l,m) = Sum_l>=m [ lambda(l,m) * phase(l,m) ]
240 See SphericalTransformServer<T>::carteVersAlm(SphericalMap<T>& map, nlmax, ctcut, alm)
241*/
242void LambdaLMBuilder::ComputeAlmFrPhase(r_8 theta,int_4 lmax, int_4 mmax,
243 TVector< complex<r_4> >& phase, Alm<r_4> & alm)
244{
245 updateArrayRecurrence(lmax);
246 r_8 cth = cos(theta);
247 r_8 sth = sin(theta);
248
249 r_8 bignorm2 = 1.e268; // = 1e-20*1.d288
250
251 r_8 lam_mm = 1. / sqrt(4.*Pi) *bignorm2;
252 r_8 lam_0, lam_1, lam_2;
253 register r_8 a_rec, b_rec;
254 register r_4 cff;
255 complex<r_4>* almp = alm.columnData(0);
256 r_8* arecp = a_recurrence_.columnData(0);
257
258 int_4 l, m;
259 for (m=0; m<=mmax;m++) {
260 //MOV^ complex<r_4>* almp = alm.columnData(m);
261 complex<r_4> phi = phase(m);
262
263 cff = lam_mm / bignorm2;
264 *almp += cff*phi; almp++;
265
266 lam_0=0.;
267 lam_1=1. /bignorm2 ;
268
269 //MOV^ r_8* arecp = a_recurrence_.columnData(m);
270 a_rec = *arecp; arecp++; // a_recurrence_(m,m);
271 b_rec = 0.;
272 for (l=m+1; l<=lmax; l++) {
273 lam_2 = (cth*lam_1-b_rec*lam_0)*a_rec;
274
275 //DEL lambda_(l,m) = lam_2*lam_mm;
276 cff = (lam_2*lam_mm);
277 *almp += cff * phi; almp++;
278
279 b_rec=1./a_rec;
280 // a_rec= LWK->a_recurr(l,m);
281 a_rec= *arecp; arecp++; // a_recurrence_(l,m);
282 lam_0 = lam_1;
283 lam_1 = lam_2;
284 }
285 lam_mm = -lam_mm*sth* sqrt( (2.*m+3.)/ (2.*m+2.) );
286 }
287}
288
289
290/*! \fn void SOPHYA::LambdaLMBuilder::updateArrayRecurrence(int_4 lmax)
291
292 compute a static array of coefficients independant from theta (common to all instances of the LambdaBuilder Class
293*/
294void LambdaLMBuilder::updateArrayRecurrence(int_4 lmax)
295 {
296 if ( (a_recurrence_.Size() > 0) && (lmax < a_recurrence_.rowNumber()) )
297 return; // Pas besoin de recalculer le tableau de recurrence
298 if (a_recurrence_.Size() > 0) {
299 cout << " WARNING : The classes LambdaXXBuilder will be more efficient "
300 << "if instanciated with parameter lmax = maximum value of l index "
301 << "which will be needed in the whole application (arrays not "
302 << "recomputed) " << endl;
303 cout << " lmax= " << lmax << " previous instanciation with lmax= "
304 << a_recurrence_.rowNumber()-1 << endl;
305 }
306
307 a_recurrence_.ReSizeRow(lmax+1);
308 for (int m=0; m<=lmax;m++)
309 {
310 a_recurrence_(m,m) = sqrt( 2.*m +3.);
311 for (int l=m+1; l<=lmax; l++)
312 {
313 r_8 fl2 = (l+1.)*(l+1.);
314 a_recurrence_(l,m)=sqrt( (4.*fl2-1.)/(fl2-m*m) );
315 }
316 }
317 }
318
319/*! \fn void SOPHYA::LambdaLMBuilder::updateArrayLamNorm()
320
321 compute static arrays of coefficients independant from theta (common to all instances of the derived classes
322*/
323void LambdaLMBuilder::updateArrayLamNorm()
324 {
325 lam_fact_.ReSizeRow(lmax_+1);
326 for(int m = 0;m<= lmax_; m++)
327 {
328 for (int l=m; l<=lmax_; l++)
329 {
330 lam_fact_(l,m) =2.*(r_8)sqrt( (2.*l+1)*(l+m)*(l-m)/(2.*l-1) );
331 }
332 }
333 (*normal_l_).ReSize(lmax_+1);
334 (*normal_l_)(0)=0.;
335 (*normal_l_)(1)=0.;
336 for (int l=2; l< (*normal_l_).NElts(); l++)
337 {
338 (*normal_l_)(l) =(r_8)sqrt( 2./( (l+2)*(l+1)*l*(l-1) ) );
339 }
340 }
341
342
343/*! \class SOPHYA::LambdaWXBuilder
344
345This class generates the coefficients :
346\f[
347 _{w}\lambda_l^m=-2\sqrt{\frac{2(l-2)!}{(l+2)!}\frac{(2l+1)}{4\pi}\frac{(l-m)!}{(l+m)!}} G^+_{lm}
348\f]
349\f[
350 _{x}\lambda_l^m=-2\sqrt{\frac{2(l-2)!}{(l+2)!}\frac{(2l+1)}{4\pi}\frac{(l-m)!}{(l+m)!}}G^-_{lm}
351\f]
352where
353\f[G^+_{lm}(\cos{\theta})=-\left( \frac{l-m^2}{\sin^2{\theta}}+\frac{1}{2}l\left(l-1\right)\right)P_l^m(\cos{\theta})+\left(l+m\right)\frac{\cos{\theta}}{\sin^2{\theta}}P^m_{l-1}(\cos{\theta})
354\f]
355and
356\f[G^-_{lm}(\cos{\theta})=\frac{m}{\sin^2{\theta}}\left(\left(l-1\right)\cos{\theta}P^m_l(\cos{\theta})-\left(l+m\right)P^m_{l-1}(\cos{\theta})\right)
357\f]
358 \f$P_l^m\f$ are the associated Legendre polynomials.
359
360The coefficients express the theta-dependance of the \f$W_{lm}(\cos{\theta})\f$ and \f$X_{lm}(\cos{\theta})\f$ functions :
361\f[W_{lm}(\cos{\theta}) = \sqrt{\frac{(l+2)!}{2(l-2)!}}_w\lambda_l^m(\cos{\theta})e^{im\phi}
362\f]
363\f[X_{lm}(\cos{\theta}) = -i\sqrt{\frac{(l+2)!}{2(l-2)!}}_x\lambda_l^m(\cos{\theta})e^{im\phi}
364\f]
365 where \f$W_{lm}(\cos{\theta})\f$ and \f$X_{lm}(\cos{\theta})\f$ are defined as :
366
367\f[
368W_{lm}(\cos{\theta})=-\frac{1}{2}\sqrt{\frac{(l+2)!}{(l-2)!}}\left(
369_{+2}Y_l^m(\cos{\theta})+_{-2}Y_l^m(\cos{\theta})\right)
370\f]
371\f[X_{lm}(\cos{\theta})=-\frac{i}{2}\sqrt{\frac{(l+2)!}{(l-2)!}}\left(
372_{+2}Y_l^m(\cos{\theta})-_{-2}Y_l^m(\cos{\theta})\right)
373\f]
374
375*/
376
377
378LambdaWXBuilder::LambdaWXBuilder(r_8 theta, int_4 lmax, int_4 mmax) : LambdaLMBuilder(theta, lmax, mmax)
379 {
380 array_init();
381 }
382
383
384void LambdaWXBuilder::array_init()
385 {
386 if (lam_fact_.Size() < 1 || normal_l_ == NULL)
387 {
388 // lam_fact_ = new TriangularMatrix<r_8>;
389 normal_l_ = new TVector<r_8>;
390 updateArrayLamNorm();
391 }
392 else
393 if ( lmax_ > lam_fact_.rowNumber()-1 || lmax_ > (*normal_l_).NElts()-1 )
394 {
395 updateArrayLamNorm();
396 }
397
398 r_8 one_on_s2 = 1. / (sth_*sth_) ; // 1/sin^2
399 r_8 c_on_s2 = cth_*one_on_s2;
400 lamWlm_.ReSizeRow(lmax_+1);
401 lamXlm_.ReSizeRow(lmax_+1);
402
403 // calcul des lambda
404 for(int m = 0;m<= mmax_; m++)
405 {
406 for (int l=m; l<=lmax_; l++)
407 {
408 lamWlm_(l,m) = 0.;
409 lamXlm_(l,m) = 0.;
410 }
411 }
412 for(int l = 2;l<= lmax_; l++)
413 {
414 r_8 normal_l = (*normal_l_)(l);
415 for (int m=0; m<=l; m++)
416 {
417 r_8 lam_lm1m = LambdaLMBuilder::lamlm(l-1,m);
418 r_8 lam_lm = LambdaLMBuilder::lamlm(l,m);
419 r_8 lam_fact_l_m = lam_fact_(l,m);
420 r_8 a_w = 2. * (l - m*m) * one_on_s2 + l*(l-1.);
421 r_8 b_w = c_on_s2 * lam_fact_l_m;
422 r_8 a_x = 2. * cth_ * (l-1.);
423 lamWlm_(l,m) = normal_l * ( a_w * lam_lm - b_w * lam_lm1m );
424 lamXlm_(l,m) = - normal_l * m* one_on_s2* ( a_x * lam_lm - lam_fact_l_m * lam_lm1m );
425 }
426 }
427
428 }
429
430/*! \class SOPHYA::LambdaPMBuilder
431
432This class generates the coefficients
433\f[
434 _{\pm}\lambda_l^m=2\sqrt{\frac{(l-2)!}{(l+2)!}\frac{(2l+1)}{4\pi}\frac{(l-m)!}{(l+m)!}}\left( G^+_{lm} \mp G^-_{lm}\right)
435\f]
436where
437\f[G^+_{lm}(\cos{\theta})=-\left( \frac{l-m^2}{\sin^2{\theta}}+\frac{1}{2}l\left(l-1\right)\right)P_l^m(\cos{\theta})+\left(l+m\right)\frac{\cos{\theta}}{\sin^2{\theta}}P^m_{l-1}(\cos{\theta})
438\f]
439and
440\f[G^-_{lm}(\cos{\theta})=\frac{m}{\sin^2{\theta}}\left(\left(l-1\right)\cos{\theta}P^m_l(\cos{\theta})-\left(l+m\right)P^m_{l-1}(\cos{\theta})\right)
441\f]
442and \f$P_l^m\f$ are the associated Legendre polynomials.
443The coefficients express the theta-dependance of the spin-2 spherical harmonics :
444\f[_{\pm2}Y_l^m(\cos{\theta})=_\pm\lambda_l^m(\cos{\theta})e^{im\phi}
445\f]
446*/
447
448LambdaPMBuilder::LambdaPMBuilder(r_8 theta, int_4 lmax, int_4 mmax) : LambdaLMBuilder(theta, lmax, mmax)
449 {
450 array_init();
451 }
452
453
454void LambdaPMBuilder::array_init()
455 {
456 if (lam_fact_.Size() < 1 || normal_l_ == NULL)
457 {
458 // lam_fact_ = new TriangularMatrix<r_8>;
459 normal_l_ = new TVector<r_8>;
460 updateArrayLamNorm();
461 }
462 else
463 if ( lmax_ > lam_fact_.rowNumber()-1 || lmax_ > (*normal_l_).NElts()-1 )
464 {
465 updateArrayLamNorm();
466 }
467
468 r_8 one_on_s2 = 1. / (sth_*sth_) ;
469 r_8 c_on_s2 = cth_*one_on_s2;
470 lamPlm_.ReSizeRow(lmax_+1);
471 lamMlm_.ReSizeRow(lmax_+1);
472
473 // calcul des lambda
474 for(int m = 0;m<= mmax_; m++)
475 {
476 for (int l=m; l<=lmax_; l++)
477 {
478 lamPlm_(l,m) = 0.;
479 lamMlm_(l,m) = 0.;
480 }
481 }
482
483 for(int l = 2;l<= lmax_; l++)
484 {
485 r_8 normal_l = (*normal_l_)(l);
486 for (int m=0; m<=l; m++)
487 {
488 r_8 lam_lm1m = LambdaLMBuilder::lamlm(l-1,m);
489 r_8 lam_lm = LambdaLMBuilder::lamlm(l,m);
490 r_8 lam_fact_l_m = lam_fact_(l,m);
491 r_8 a_w = 2. * (l - m*m) * one_on_s2 + l*(l-1.);
492 r_8 f_w = lam_fact_l_m/(sth_*sth_);
493 r_8 c_w = 2*m*(l-1.) * c_on_s2;
494
495 lamPlm_(l,m) = normal_l * ( -(a_w+c_w) * lam_lm + f_w*( cth_ + m) * lam_lm1m )/Rac2;
496 lamMlm_(l,m) = normal_l * ( -(a_w-c_w) * lam_lm + f_w*( cth_ - m) * lam_lm1m )/Rac2;
497 }
498 }
499
500 }
501
502
Note: See TracBrowser for help on using the repository browser.