LORENE
legendre.C
1/*
2 * Copyright (c) 1999-2001 Eric Gourgoulhon
3 *
4 * This file is part of LORENE.
5 *
6 * LORENE is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * LORENE is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with LORENE; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22
23
24
25/*
26 * Calcule les valeurs des fonctions de Legendre associees
27 * P_l^m(cos(theta)) / (2m-1)!!
28 * aux points
29 * theta_j = pi/2 j/(nt-1) 0 <= j <= nt-1
30 * qui echantillonnent uniformement l'intervalle [0, pi/2].
31 *
32 *
33 * Entree:
34 * -------
35 * int m : ordre de la fonction de Legendre associee P_l^m
36 * int nt : nombre de points en theta
37 *
38 * Sortie (valeur de retour) :
39 * -------------------------
40 * double* legendre : ensemble des (nt-m)*nt valeurs
41 * P_l^m(cos(theta))/(2m-1)!!
42 * stokees comme suit:
43 *
44 * legendre[nt* (l-m) + j] = P_l^m( cos(theta_j) ) / (2m-1)!!
45 *
46 * avec m <= l <= nt-1.
47 *
48 * NB: Cette routine effectue le calcul a chaque appel et ne renvoie pas
49 * un pointeur sur des valeurs precedemment calculees.
50 */
51
52
53/*
54 * $Id: legendre.C,v 1.7 2016/12/05 16:18:02 j_novak Exp $
55 * $Log: legendre.C,v $
56 * Revision 1.7 2016/12/05 16:18:02 j_novak
57 * Suppression of some global variables (file names, loch, ...) to prevent redefinitions
58 *
59 * Revision 1.6 2014/10/13 08:53:13 j_novak
60 * Lorene classes and functions now belong to the namespace Lorene.
61 *
62 * Revision 1.5 2014/10/06 15:16:02 j_novak
63 * Modified #include directives to use c++ syntax.
64 *
65 * Revision 1.4 2005/02/18 13:14:13 j_novak
66 * Changing of malloc/free to new/delete + suppression of some unused variables
67 * (trying to avoid compilation warnings).
68 *
69 * Revision 1.3 2003/01/31 10:31:24 e_gourgoulhon
70 * Suppressed the directive #include <malloc.h> for malloc is defined
71 * in <stdlib.h>
72 *
73 * Revision 1.2 2002/10/16 14:36:54 j_novak
74 * Reorganization of #include instructions of standard C++, in order to
75 * use experimental version 3 of gcc.
76 *
77 * Revision 1.1.1.1 2001/11/20 15:19:28 e_gourgoulhon
78 * LORENE
79 *
80 * Revision 2.0 1999/02/22 15:37:13 hyc
81 * *** empty log message ***
82 *
83 *
84 * $Header: /cvsroot/Lorene/C++/Source/Non_class_members/Coef/legendre.C,v 1.7 2016/12/05 16:18:02 j_novak Exp $
85 *
86 */
87
88// headers du C
89#include <cstdlib>
90#include <cassert>
91#include <cmath>
92
93#include "headcpp.h"
94
95namespace Lorene {
96//******************************************************************************
97
98double* legendre(int m, int nt) {
99
100int i, j, l ;
101
102 int lmax = nt - 1 ;
103 assert(m >= 0) ;
104 assert(m <= lmax) ;
105
106 double dt = M_PI / double(2*(nt-1)) ;
107
108// Allocation memoire pour le tableau resultat
109//--------------------------------------------
110
111 double* resu = new double[(lmax-m+1)*nt] ; //(double *)(malloc( (lmax-m+1)*nt * sizeof(double) )) ;
112
113 // Tableau de travail
114 double* cost = new double[nt] ; //(double*)( malloc( nt*sizeof(double) ) ) ;
115
116//-----------------------
117// 1/ Calcul de P_m^m
118//-----------------------
119
120 if (m==0) {
121 for (j=0; j<nt; j++) {
122 resu[j] = 1. ; // P_0^0(x) = 1.
123 }
124 }
125 else {
126
127//... P_m^m(x) = (-1)^m (1-x^2)^{m/2} <--- cette formule donne un P_m^m
128// plus petit par un facteur
129// (2m-1)!! que celui de la litterature
130
131 for (j=0; j<nt; j++) {
132 double y = 1. ;
133 double s = sin(j*dt) ;
134 for (i=1 ; i<2*m; i+=2) {
135 y *= - s ;
136// NB: Pour obtenir le P_m^m de la litterature, il faudrait remplacer la ligne
137// ci-dessus par : y *= - i*s ;
138 }
139 resu[j] = y ;
140//## resu[j] = pow(-s, double(m)) ;
141 }
142 } // fin du cas m != 0
143
144 if (lmax==m) {
145 delete [] cost ;
146 return resu ;
147 }
148 else {
149
150//-----------------------
151// 2/ Calcul de P_{m+1}^m
152//-----------------------
153
154//... Calcul des cos( theta_j ) :
155 for (j=0; j<nt; j++) {
156 cost[j] = cos(j*dt) ;
157 }
158
159 for (j=0; j<nt; j++) {
160 resu[nt+j] = cost[j] * (2.*m+1) * resu[j] ;
161 }
162
163//-----------------------
164// 3/ Calcul de P_l^m pour m+2 <= l <= lmax
165//-----------------------
166
167 for (l=m+2; l < lmax+1 ; l++) {
168 int i_l = nt*(l-m) ;
169 int i_lm1 = nt*(l-1-m) ;
170 int i_lm2 = nt*(l-2-m) ;
171 int a = 2*l - 1 ;
172 int b = l + m - 1 ;
173 int c = l - m ;
174
175 for (j=0; j<nt; j++) {
176 resu[i_l+j] = ( cost[j] * a * resu[i_lm1+j]
177 - b * resu[i_lm2+j] ) / c ;
178 }
179 }
180
181 delete [] cost ; //free (cost) ;
182 return resu ;
183
184 } // fin du cas lmax > m
185
186}
187
188
189
190}
Cmp sin(const Cmp &)
Sine.
Definition cmp_math.C:72
Cmp cos(const Cmp &)
Cosine.
Definition cmp_math.C:97
Lorene prototypes.
Definition app_hor.h:67
Coord cost
Definition map.h:734
Coord y
y coordinate centered on the grid
Definition map.h:739