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

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

1/ passage en sa_size_t (au lieu de int) dans Alm<T> et Bm<T>
2/ Ajout des methodes optimisees (statiques) pour calcul transforme Ylm
ds LambdaLMBuilder et utilisation ds SphericalTransformServer

Reza , 1/06/2006

File size: 13.8 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;
226 almp++;
227
228 b_rec=1./a_rec;
229 // a_rec= LWK->a_recurr(l,m);
230 a_rec= *arecp; arecp++; // a_recurrence_(l,m);
231 lam_0 = lam_1;
232 lam_1 = lam_2;
233 }
234 lam_mm = -lam_mm*sth* sqrt( (2.*m+3.)/ (2.*m+2.) );
235 }
236}
237
238/*!
239 \brief : Specialized/optimized static function for fast spherical harmonic transform.
240 Computes alm(l,m) = Sum_l>=m [ lambda(l,m) * phase(l,m) ]
241 See SphericalTransformServer<T>::carteVersAlm(SphericalMap<T>& map, nlmax, ctcut, alm)
242*/
243void LambdaLMBuilder::ComputeAlmFrPhase(r_8 theta,int_4 lmax, int_4 mmax,
244 TVector< complex<r_4> >& phase, Alm<r_4> & alm)
245{
246 TVector< complex<r_8> > phase8;
247 phase8 = phase;
248 Alm<r_8> alm8(alm.Lmax());
249 ComputeAlmFrPhase(theta, lmax, mmax, phase8, alm8);
250 for(sa_size_t k=0; k<alm.Size(); k++)
251 alm(k) = complex<r_4>((r_4)alm8(k).real() , (r_4)alm8(k).imag());
252 return;
253}
254
255
256/*! \fn void SOPHYA::LambdaLMBuilder::updateArrayRecurrence(int_4 lmax)
257
258 compute a static array of coefficients independant from theta (common to all instances of the LambdaBuilder Class
259*/
260void LambdaLMBuilder::updateArrayRecurrence(int_4 lmax)
261 {
262 if ( (a_recurrence_.Size() > 0) && (lmax < a_recurrence_.rowNumber()) )
263 return; // Pas besoin de recalculer le tableau de recurrence
264 if (a_recurrence_.Size() > 0) {
265 cout << " WARNING : The classes LambdaXXBuilder will be more efficient "
266 << "if instanciated with parameter lmax = maximum value of l index "
267 << "which will be needed in the whole application (arrays not "
268 << "recomputed) " << endl;
269 cout << " lmax= " << lmax << " previous instanciation with lmax= "
270 << a_recurrence_.rowNumber()-1 << endl;
271 }
272
273 a_recurrence_.ReSizeRow(lmax+1);
274 for (int m=0; m<=lmax;m++)
275 {
276 a_recurrence_(m,m) = sqrt( 2.*m +3.);
277 for (int l=m+1; l<=lmax; l++)
278 {
279 r_8 fl2 = (l+1.)*(l+1.);
280 a_recurrence_(l,m)=sqrt( (4.*fl2-1.)/(fl2-m*m) );
281 }
282 }
283 }
284
285/*! \fn void SOPHYA::LambdaLMBuilder::updateArrayLamNorm()
286
287 compute static arrays of coefficients independant from theta (common to all instances of the derived classes
288*/
289void LambdaLMBuilder::updateArrayLamNorm()
290 {
291 lam_fact_.ReSizeRow(lmax_+1);
292 for(int m = 0;m<= lmax_; m++)
293 {
294 for (int l=m; l<=lmax_; l++)
295 {
296 lam_fact_(l,m) =2.*(r_8)sqrt( (2.*l+1)*(l+m)*(l-m)/(2.*l-1) );
297 }
298 }
299 (*normal_l_).ReSize(lmax_+1);
300 (*normal_l_)(0)=0.;
301 (*normal_l_)(1)=0.;
302 for (int l=2; l< (*normal_l_).NElts(); l++)
303 {
304 (*normal_l_)(l) =(r_8)sqrt( 2./( (l+2)*(l+1)*l*(l-1) ) );
305 }
306 }
307
308
309/*! \class SOPHYA::LambdaWXBuilder
310
311This class generates the coefficients :
312\f[
313 _{w}\lambda_l^m=-2\sqrt{\frac{2(l-2)!}{(l+2)!}\frac{(2l+1)}{4\pi}\frac{(l-m)!}{(l+m)!}} G^+_{lm}
314\f]
315\f[
316 _{x}\lambda_l^m=-2\sqrt{\frac{2(l-2)!}{(l+2)!}\frac{(2l+1)}{4\pi}\frac{(l-m)!}{(l+m)!}}G^-_{lm}
317\f]
318where
319\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})
320\f]
321and
322\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)
323\f]
324 \f$P_l^m\f$ are the associated Legendre polynomials.
325
326The coefficients express the theta-dependance of the \f$W_{lm}(\cos{\theta})\f$ and \f$X_{lm}(\cos{\theta})\f$ functions :
327\f[W_{lm}(\cos{\theta}) = \sqrt{\frac{(l+2)!}{2(l-2)!}}_w\lambda_l^m(\cos{\theta})e^{im\phi}
328\f]
329\f[X_{lm}(\cos{\theta}) = -i\sqrt{\frac{(l+2)!}{2(l-2)!}}_x\lambda_l^m(\cos{\theta})e^{im\phi}
330\f]
331 where \f$W_{lm}(\cos{\theta})\f$ and \f$X_{lm}(\cos{\theta})\f$ are defined as :
332
333\f[
334W_{lm}(\cos{\theta})=-\frac{1}{2}\sqrt{\frac{(l+2)!}{(l-2)!}}\left(
335_{+2}Y_l^m(\cos{\theta})+_{-2}Y_l^m(\cos{\theta})\right)
336\f]
337\f[X_{lm}(\cos{\theta})=-\frac{i}{2}\sqrt{\frac{(l+2)!}{(l-2)!}}\left(
338_{+2}Y_l^m(\cos{\theta})-_{-2}Y_l^m(\cos{\theta})\right)
339\f]
340
341*/
342
343
344LambdaWXBuilder::LambdaWXBuilder(r_8 theta, int_4 lmax, int_4 mmax) : LambdaLMBuilder(theta, lmax, mmax)
345 {
346 array_init();
347 }
348
349
350void LambdaWXBuilder::array_init()
351 {
352 if (lam_fact_.Size() < 1 || normal_l_ == NULL)
353 {
354 // lam_fact_ = new TriangularMatrix<r_8>;
355 normal_l_ = new TVector<r_8>;
356 updateArrayLamNorm();
357 }
358 else
359 if ( lmax_ > lam_fact_.rowNumber()-1 || lmax_ > (*normal_l_).NElts()-1 )
360 {
361 updateArrayLamNorm();
362 }
363
364 r_8 one_on_s2 = 1. / (sth_*sth_) ; // 1/sin^2
365 r_8 c_on_s2 = cth_*one_on_s2;
366 lamWlm_.ReSizeRow(lmax_+1);
367 lamXlm_.ReSizeRow(lmax_+1);
368
369 // calcul des lambda
370 for(int m = 0;m<= mmax_; m++)
371 {
372 for (int l=m; l<=lmax_; l++)
373 {
374 lamWlm_(l,m) = 0.;
375 lamXlm_(l,m) = 0.;
376 }
377 }
378 for(int l = 2;l<= lmax_; l++)
379 {
380 r_8 normal_l = (*normal_l_)(l);
381 for (int m=0; m<=l; m++)
382 {
383 r_8 lam_lm1m = LambdaLMBuilder::lamlm(l-1,m);
384 r_8 lam_lm = LambdaLMBuilder::lamlm(l,m);
385 r_8 lam_fact_l_m = lam_fact_(l,m);
386 r_8 a_w = 2. * (l - m*m) * one_on_s2 + l*(l-1.);
387 r_8 b_w = c_on_s2 * lam_fact_l_m;
388 r_8 a_x = 2. * cth_ * (l-1.);
389 lamWlm_(l,m) = normal_l * ( a_w * lam_lm - b_w * lam_lm1m );
390 lamXlm_(l,m) = - normal_l * m* one_on_s2* ( a_x * lam_lm - lam_fact_l_m * lam_lm1m );
391 }
392 }
393
394 }
395
396/*! \class SOPHYA::LambdaPMBuilder
397
398This class generates the coefficients
399\f[
400 _{\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)
401\f]
402where
403\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})
404\f]
405and
406\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)
407\f]
408and \f$P_l^m\f$ are the associated Legendre polynomials.
409The coefficients express the theta-dependance of the spin-2 spherical harmonics :
410\f[_{\pm2}Y_l^m(\cos{\theta})=_\pm\lambda_l^m(\cos{\theta})e^{im\phi}
411\f]
412*/
413
414LambdaPMBuilder::LambdaPMBuilder(r_8 theta, int_4 lmax, int_4 mmax) : LambdaLMBuilder(theta, lmax, mmax)
415 {
416 array_init();
417 }
418
419
420void LambdaPMBuilder::array_init()
421 {
422 if (lam_fact_.Size() < 1 || normal_l_ == NULL)
423 {
424 // lam_fact_ = new TriangularMatrix<r_8>;
425 normal_l_ = new TVector<r_8>;
426 updateArrayLamNorm();
427 }
428 else
429 if ( lmax_ > lam_fact_.rowNumber()-1 || lmax_ > (*normal_l_).NElts()-1 )
430 {
431 updateArrayLamNorm();
432 }
433
434 r_8 one_on_s2 = 1. / (sth_*sth_) ;
435 r_8 c_on_s2 = cth_*one_on_s2;
436 lamPlm_.ReSizeRow(lmax_+1);
437 lamMlm_.ReSizeRow(lmax_+1);
438
439 // calcul des lambda
440 for(int m = 0;m<= mmax_; m++)
441 {
442 for (int l=m; l<=lmax_; l++)
443 {
444 lamPlm_(l,m) = 0.;
445 lamMlm_(l,m) = 0.;
446 }
447 }
448
449 for(int l = 2;l<= lmax_; l++)
450 {
451 r_8 normal_l = (*normal_l_)(l);
452 for (int m=0; m<=l; m++)
453 {
454 r_8 lam_lm1m = LambdaLMBuilder::lamlm(l-1,m);
455 r_8 lam_lm = LambdaLMBuilder::lamlm(l,m);
456 r_8 lam_fact_l_m = lam_fact_(l,m);
457 r_8 a_w = 2. * (l - m*m) * one_on_s2 + l*(l-1.);
458 r_8 f_w = lam_fact_l_m/(sth_*sth_);
459 r_8 c_w = 2*m*(l-1.) * c_on_s2;
460
461 lamPlm_(l,m) = normal_l * ( -(a_w+c_w) * lam_lm + f_w*( cth_ + m) * lam_lm1m )/Rac2;
462 lamMlm_(l,m) = normal_l * ( -(a_w-c_w) * lam_lm + f_w*( cth_ - m) * lam_lm1m )/Rac2;
463 }
464 }
465
466 }
467
468
Note: See TracBrowser for help on using the repository browser.