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

Last change on this file since 3817 was 3810, checked in by ansari, 15 years ago

Adaptation a la nouvelle classe LowerTriangularMatrix<T> remplacant TriangularMatrix<T> , Reza 26/07/2010

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