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

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

petite optimisation supplementaire par simplification du code ds la boucle du calcul Sum[alm*Plm] ... ds LambdaLMBuilder , Reza 2/6/2006

File size: 13.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 register r_8 lam_0, lam_1, lam_2;
137
138 int_4 l, m, k;
139
140 for (m=0; m<=mmax;m++) {
141 const complex<r_8>* almp = alm.columnData(m);
142 complex<r_8>* bmp = &(bm(m));
143 r_8* arecp = a_recurrence_.columnData(m);
144 *bmp = (lam_mm / bignorm2)*almp[0]; almp++;
145
146 lam_0=0.;
147 lam_1=1. /bignorm2 ;
148
149 // for (l=m+1; l<=lmax; l++) {
150 for (k=0; k<lmax-m; k++) {
151 lam_2 = (cth*lam_1-lam_0)*arecp[k];
152 lam_0 = lam_1/arecp[k];
153 lam_1 = lam_2;
154
155 *bmp += (lam_2*lam_mm)*almp[k];
156 }
157 lam_mm = -lam_mm*sth* sqrt( (2.*m+3.)/ (2.*m+2.) );
158 }
159}
160
161/*!
162 \brief : Specialized/optimized static function for fast spherical harmonic transform.
163*/
164void LambdaLMBuilder::ComputeBmFrAlm(r_8 theta,int_4 lmax, int_4 mmax,
165 const Alm<r_4>& alm, Bm< complex<r_4> >& bm)
166{
167 Alm<r_8> alm8(alm.Lmax());
168 for(sa_size_t k=0; k<alm8.Size(); k++)
169 alm8(k) = complex<r_8>((r_8)alm(k).real() , (r_8)alm(k).imag());
170 Bm< complex<r_8> > bm8(bm.Mmax());
171 ComputeBmFrAlm(theta, lmax, mmax, alm8, bm8);
172 for(sa_size_t kk=-bm.Mmax(); kk<=bm.Mmax(); kk++)
173 bm(kk)= complex<r_4>((r_4)bm8(kk).real() , (r_4)bm8(kk).imag());
174 return;
175}
176
177
178/*!
179 \brief : Specialized/optimized static function for fast spherical harmonic transform.
180 Computes alm(l,m) = Sum_l>=m [ lambda(l,m) * phase(l,m) ]
181 See SphericalTransformServer<T>::carteVersAlm(SphericalMap<T>& map, nlmax, ctcut, alm)
182*/
183void LambdaLMBuilder::ComputeAlmFrPhase(r_8 theta,int_4 lmax, int_4 mmax,
184 TVector< complex<r_8> >& phase, Alm<r_8> & alm)
185{
186 updateArrayRecurrence(lmax);
187 r_8 cth = cos(theta);
188 r_8 sth = sin(theta);
189
190 r_8 bignorm2 = 1.e268; // = 1e-20*1.d288
191
192 r_8 lam_mm = 1. / sqrt(4.*Pi) *bignorm2;
193 register r_8 lam_0, lam_1, lam_2;
194
195 int_4 l, m, k;
196
197 for (m=0; m<=mmax;m++) {
198 complex<r_8>* almp = alm.columnData(m);
199 complex<r_8> phi = phase(m);
200
201 *almp += (lam_mm / bignorm2)*phi; almp++;
202
203 lam_0=0.;
204 lam_1=1. /bignorm2 ;
205
206 r_8* arecp = a_recurrence_.columnData(m);
207 // for (l=m+1; l<=lmax; l++) {
208 for (k=0; k<lmax-m; k++) {
209 lam_2 = (cth*lam_1-lam_0)*arecp[k];
210 lam_0 = lam_1/arecp[k];
211 lam_1 = lam_2;
212
213 almp[k] += (lam_2*lam_mm) * phi;
214 }
215 lam_mm = -lam_mm*sth* sqrt( (2.*m+3.)/ (2.*m+2.) );
216 }
217}
218
219/*!
220 \brief : Specialized/optimized static function for fast spherical harmonic transform.
221 Computes alm(l,m) = Sum_l>=m [ lambda(l,m) * phase(l,m) ]
222 See SphericalTransformServer<T>::carteVersAlm(SphericalMap<T>& map, nlmax, ctcut, alm)
223*/
224void LambdaLMBuilder::ComputeAlmFrPhase(r_8 theta,int_4 lmax, int_4 mmax,
225 TVector< complex<r_4> >& phase, Alm<r_4> & alm)
226{
227 updateArrayRecurrence(lmax);
228 r_8 cth = cos(theta);
229 r_8 sth = sin(theta);
230
231 r_8 bignorm2 = 1.e268; // = 1e-20*1.d288
232
233 r_8 lam_mm = 1. / sqrt(4.*Pi) *bignorm2;
234 register r_8 lam_0, lam_1, lam_2;
235
236 int_4 l, m, k;
237
238 for (m=0; m<=mmax;m++) {
239 complex<r_4>* almp = alm.columnData(m);
240 complex<r_4> phi = phase(m);
241
242 *almp += ((r_4)(lam_mm / bignorm2))*phi; almp++;
243
244 lam_0=0.;
245 lam_1=1. /bignorm2 ;
246
247 r_8* arecp = a_recurrence_.columnData(m);
248 // for (l=m+1; l<=lmax; l++) {
249 for (k=0; k<lmax-m; k++) {
250 lam_2 = (cth*lam_1-lam_0)*arecp[k];
251 lam_0 = lam_1/arecp[k];
252 lam_1 = lam_2;
253
254 almp[k] += ((r_4)(lam_2*lam_mm)) * phi;
255 }
256 lam_mm = -lam_mm*sth* sqrt( (2.*m+3.)/ (2.*m+2.) );
257 }
258
259}
260
261
262
263/*! \fn void SOPHYA::LambdaLMBuilder::updateArrayRecurrence(int_4 lmax)
264
265 compute a static array of coefficients independant from theta (common to all instances of the LambdaBuilder Class
266*/
267
268void LambdaLMBuilder::updateArrayRecurrence(int_4 lmax)
269 {
270 if ( (a_recurrence_.Size() > 0) && (lmax < a_recurrence_.rowNumber()) )
271 return; // Pas besoin de recalculer le tableau de recurrence
272 if (a_recurrence_.Size() > 0) {
273 cout << " WARNING : The classes LambdaXXBuilder will be more efficient "
274 << "if instanciated with parameter lmax = maximum value of l index "
275 << "which will be needed in the whole application (arrays not "
276 << "recomputed) " << endl;
277 cout << " lmax= " << lmax << " previous instanciation with lmax= "
278 << a_recurrence_.rowNumber()-1 << endl;
279 }
280
281 a_recurrence_.ReSizeRow(lmax+1);
282 for (int m=0; m<=lmax;m++)
283 {
284 a_recurrence_(m,m) = sqrt( 2.*m +3.);
285 for (int l=m+1; l<=lmax; l++)
286 {
287 r_8 fl2 = (l+1.)*(l+1.);
288 a_recurrence_(l,m)=sqrt( (4.*fl2-1.)/(fl2-m*m) );
289 }
290 }
291 }
292
293/*! \fn void SOPHYA::LambdaLMBuilder::updateArrayLamNorm()
294
295 compute static arrays of coefficients independant from theta (common to all instances of the derived classes
296*/
297void LambdaLMBuilder::updateArrayLamNorm()
298 {
299 lam_fact_.ReSizeRow(lmax_+1);
300 for(int m = 0;m<= lmax_; m++)
301 {
302 for (int l=m; l<=lmax_; l++)
303 {
304 lam_fact_(l,m) =2.*(r_8)sqrt( (2.*l+1)*(l+m)*(l-m)/(2.*l-1) );
305 }
306 }
307 (*normal_l_).ReSize(lmax_+1);
308 (*normal_l_)(0)=0.;
309 (*normal_l_)(1)=0.;
310 for (int l=2; l< (*normal_l_).NElts(); l++)
311 {
312 (*normal_l_)(l) =(r_8)sqrt( 2./( (l+2)*(l+1)*l*(l-1) ) );
313 }
314 }
315
316
317/*! \class SOPHYA::LambdaWXBuilder
318
319This class generates the coefficients :
320\f[
321 _{w}\lambda_l^m=-2\sqrt{\frac{2(l-2)!}{(l+2)!}\frac{(2l+1)}{4\pi}\frac{(l-m)!}{(l+m)!}} G^+_{lm}
322\f]
323\f[
324 _{x}\lambda_l^m=-2\sqrt{\frac{2(l-2)!}{(l+2)!}\frac{(2l+1)}{4\pi}\frac{(l-m)!}{(l+m)!}}G^-_{lm}
325\f]
326where
327\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})
328\f]
329and
330\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)
331\f]
332 \f$P_l^m\f$ are the associated Legendre polynomials.
333
334The coefficients express the theta-dependance of the \f$W_{lm}(\cos{\theta})\f$ and \f$X_{lm}(\cos{\theta})\f$ functions :
335\f[W_{lm}(\cos{\theta}) = \sqrt{\frac{(l+2)!}{2(l-2)!}}_w\lambda_l^m(\cos{\theta})e^{im\phi}
336\f]
337\f[X_{lm}(\cos{\theta}) = -i\sqrt{\frac{(l+2)!}{2(l-2)!}}_x\lambda_l^m(\cos{\theta})e^{im\phi}
338\f]
339 where \f$W_{lm}(\cos{\theta})\f$ and \f$X_{lm}(\cos{\theta})\f$ are defined as :
340
341\f[
342W_{lm}(\cos{\theta})=-\frac{1}{2}\sqrt{\frac{(l+2)!}{(l-2)!}}\left(
343_{+2}Y_l^m(\cos{\theta})+_{-2}Y_l^m(\cos{\theta})\right)
344\f]
345\f[X_{lm}(\cos{\theta})=-\frac{i}{2}\sqrt{\frac{(l+2)!}{(l-2)!}}\left(
346_{+2}Y_l^m(\cos{\theta})-_{-2}Y_l^m(\cos{\theta})\right)
347\f]
348
349*/
350
351
352LambdaWXBuilder::LambdaWXBuilder(r_8 theta, int_4 lmax, int_4 mmax) : LambdaLMBuilder(theta, lmax, mmax)
353 {
354 array_init();
355 }
356
357
358void LambdaWXBuilder::array_init()
359 {
360 if (lam_fact_.Size() < 1 || normal_l_ == NULL)
361 {
362 // lam_fact_ = new TriangularMatrix<r_8>;
363 normal_l_ = new TVector<r_8>;
364 updateArrayLamNorm();
365 }
366 else
367 if ( lmax_ > lam_fact_.rowNumber()-1 || lmax_ > (*normal_l_).NElts()-1 )
368 {
369 updateArrayLamNorm();
370 }
371
372 r_8 one_on_s2 = 1. / (sth_*sth_) ; // 1/sin^2
373 r_8 c_on_s2 = cth_*one_on_s2;
374 lamWlm_.ReSizeRow(lmax_+1);
375 lamXlm_.ReSizeRow(lmax_+1);
376
377 // calcul des lambda
378 for(int m = 0;m<= mmax_; m++)
379 {
380 for (int l=m; l<=lmax_; l++)
381 {
382 lamWlm_(l,m) = 0.;
383 lamXlm_(l,m) = 0.;
384 }
385 }
386 for(int l = 2;l<= lmax_; l++)
387 {
388 r_8 normal_l = (*normal_l_)(l);
389 for (int m=0; m<=l; m++)
390 {
391 r_8 lam_lm1m = LambdaLMBuilder::lamlm(l-1,m);
392 r_8 lam_lm = LambdaLMBuilder::lamlm(l,m);
393 r_8 lam_fact_l_m = lam_fact_(l,m);
394 r_8 a_w = 2. * (l - m*m) * one_on_s2 + l*(l-1.);
395 r_8 b_w = c_on_s2 * lam_fact_l_m;
396 r_8 a_x = 2. * cth_ * (l-1.);
397 lamWlm_(l,m) = normal_l * ( a_w * lam_lm - b_w * lam_lm1m );
398 lamXlm_(l,m) = - normal_l * m* one_on_s2* ( a_x * lam_lm - lam_fact_l_m * lam_lm1m );
399 }
400 }
401
402 }
403
404/*! \class SOPHYA::LambdaPMBuilder
405
406This class generates the coefficients
407\f[
408 _{\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)
409\f]
410where
411\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})
412\f]
413and
414\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)
415\f]
416and \f$P_l^m\f$ are the associated Legendre polynomials.
417The coefficients express the theta-dependance of the spin-2 spherical harmonics :
418\f[_{\pm2}Y_l^m(\cos{\theta})=_\pm\lambda_l^m(\cos{\theta})e^{im\phi}
419\f]
420*/
421
422LambdaPMBuilder::LambdaPMBuilder(r_8 theta, int_4 lmax, int_4 mmax) : LambdaLMBuilder(theta, lmax, mmax)
423 {
424 array_init();
425 }
426
427
428void LambdaPMBuilder::array_init()
429 {
430 if (lam_fact_.Size() < 1 || normal_l_ == NULL)
431 {
432 // lam_fact_ = new TriangularMatrix<r_8>;
433 normal_l_ = new TVector<r_8>;
434 updateArrayLamNorm();
435 }
436 else
437 if ( lmax_ > lam_fact_.rowNumber()-1 || lmax_ > (*normal_l_).NElts()-1 )
438 {
439 updateArrayLamNorm();
440 }
441
442 r_8 one_on_s2 = 1. / (sth_*sth_) ;
443 r_8 c_on_s2 = cth_*one_on_s2;
444 lamPlm_.ReSizeRow(lmax_+1);
445 lamMlm_.ReSizeRow(lmax_+1);
446
447 // calcul des lambda
448 for(int m = 0;m<= mmax_; m++)
449 {
450 for (int l=m; l<=lmax_; l++)
451 {
452 lamPlm_(l,m) = 0.;
453 lamMlm_(l,m) = 0.;
454 }
455 }
456
457 for(int l = 2;l<= lmax_; l++)
458 {
459 r_8 normal_l = (*normal_l_)(l);
460 for (int m=0; m<=l; m++)
461 {
462 r_8 lam_lm1m = LambdaLMBuilder::lamlm(l-1,m);
463 r_8 lam_lm = LambdaLMBuilder::lamlm(l,m);
464 r_8 lam_fact_l_m = lam_fact_(l,m);
465 r_8 a_w = 2. * (l - m*m) * one_on_s2 + l*(l-1.);
466 r_8 f_w = lam_fact_l_m/(sth_*sth_);
467 r_8 c_w = 2*m*(l-1.) * c_on_s2;
468
469 lamPlm_(l,m) = normal_l * ( -(a_w+c_w) * lam_lm + f_w*( cth_ + m) * lam_lm1m )/Rac2;
470 lamMlm_(l,m) = normal_l * ( -(a_w-c_w) * lam_lm + f_w*( cth_ - m) * lam_lm1m )/Rac2;
471 }
472 }
473
474 }
475
476
Note: See TracBrowser for help on using the repository browser.