Debian Codegraph @

School05/Monday/legendre_poly.C

raw
1 /*
2 * Definition of class Legendre_poly
3 */
4
5 /*
6 * Copyright (c) 2005 Eric Gourgoulhon
7 *
8 * This file is part of LORENE.
9 *
10 * LORENE is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * LORENE is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with LORENE; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
25
26 /*
27 * $Id: legendre_poly.C,v 1.3 2014/10/06 15:09:48 j_novak Exp $
28 * $Log: legendre_poly.C,v $
29 * Revision 1.3 2014/10/06 15:09:48 j_novak
30 * Modified #include directives to use c++ syntax.
31 *
32 * Revision 1.2 2005/11/14 14:12:10 e_gourgoulhon
33 * Added include <assert.h>
34 *
35 * Revision 1.1 2005/11/14 01:57:00 e_gourgoulhon
36 * First version
37 *
38 *
39 * $Header: /cvsroot/Lorene/School05/Monday/legendre_poly.C,v 1.3 2014/10/06 15:09:48 j_novak Exp $
40 *
41 */
42
43 #include <iostream>
44
45 using namespace std ;
46
47 #include <cstdlib>
48 #include <cmath>
49 #include <cassert>
50
51 #include "ortho_poly.h"
52 #include "grid.h"
53
54 void legendre_nodes_weight_GL(int n, double* coloc, double* weight,
55 double prec = 1.e-12, int itemax = 100) ;
56
57
58 //----------------------//
59 // Standard constructor //
60 //----------------------//
61
62 Legendre_poly::Legendre_poly(int ni) : Ortho_poly(ni) {}
63
64 //--------------------//
65 // Copy constructor //
66 //--------------------//
67
68 Legendre_poly::Legendre_poly(const Legendre_poly& pp) : Ortho_poly(pp) {}
69
70
71 //--------------//
72 // Destructor //
73 //--------------//
74
75 Legendre_poly::~Legendre_poly() {}
76
77
78 //--------------//
79 // Assignment //
80 //--------------//
81
82 void Legendre_poly::operator=(const Legendre_poly& ) {
83
84 cerr << "Legendre_poly::operator= not implemented !" << endl ;
85 abort() ;
86
87 }
88
89 //-----------//
90 // Display //
91 //-----------//
92
93 ostream& operator<<(ostream& ost, const Legendre_poly& pp) {
94
95 ost << "Basis of Legendre polynomials up to degree " << pp.n() << endl ;
96
97 return ost ;
98 }
99
100 //-------------------//
101 // Weight function //
102 //-------------------//
103
104 double Legendre_poly::weight(double ) const {
105
106 return 1. ;
107
108 }
109
110
111 //-------------------------------------------//
112 // Evaluation of the Legendre polynomials //
113 //-------------------------------------------//
114
115 double Legendre_poly::operator()(int i, double x) const {
116
117 assert( i >= 0 ) ;
118 assert( i <= nn ) ;
119
120 if (i==0) return 1. ;
121
122 if (i==1) return x ;
123
124 double tjm2 = 1. ; // value at step j - 2
125 double tjm1 = x ; // value at step j - 1
126
127 for (int j=2; j<=i; j++) {
128 double tj = ( (2*j-1)*x* tjm1 - (j-1)*tjm2 ) / double(j) ;
129 tjm2 = tjm1 ;
130 tjm1 = tj ;
131 }
132
133 return tjm1 ;
134
135 }
136
137 //------------------------------------//
138 // Computation of nodes and weights //
139 //------------------------------------//
140
141
142 const Grid& Legendre_poly::gauss_nodes() const {
143
144 if (p_gauss_nodes == 0x0) { // the nodes must initialized
145
146 p_gauss_nodes = new Grid_Legendre_Gauss(nn+1) ;
147 }
148
149 return *p_gauss_nodes ;
150 }
151
152
153 double Legendre_poly::gauss_weight(int i) const {
154
155 if (p_gauss_weights == 0x0) { // the weights must be computed
156
157 cerr << "Legendre_poly::gauss_weight : not implemented yet !"
158 << endl ;
159 abort() ;
160 }
161
162 return p_gauss_weights[i] ;
163
164 }
165
166
167 double Legendre_poly::gauss_gamma(int i) const {
168
169 if (p_gauss_gamma == 0x0) { // the weights must be computed
170
171 p_gauss_gamma = new double[nn+1] ;
172
173 for (int j=0; j<=nn; j++)
174 p_gauss_gamma[j] = 1. / (double(j) + 0.5) ;
175 }
176
177 return p_gauss_gamma[i] ;
178
179 }
180
181
182 const Grid& Legendre_poly::gauss_lobatto_nodes() const {
183
184 if (p_gauss_lobatto_nodes == 0x0) { // the nodes must initialized
185
186 p_gauss_lobatto_nodes = new Grid_Legendre_GL(nn+1) ;
187 }
188
189 return *p_gauss_lobatto_nodes ;
190 }
191
192
193 double Legendre_poly::gauss_lobatto_weight(int i) const {
194
195 if (p_gauss_lobatto_weights == 0x0) { // the weights must be computed
196
197 p_gauss_lobatto_weights = new double[nn+1] ;
198
199 double* xx_work = new double[nn+1] ;
200
201 double precis = 1.e-12 ; // required precision
202 int nitermax = 200 ; // Maximum number of iterations
203
204 legendre_nodes_weight_GL(nn+1, xx_work, p_gauss_lobatto_weights,
205 precis, nitermax) ;
206
207 delete [] xx_work ;
208 }
209
210 return p_gauss_lobatto_weights[i] ;
211
212 }
213
214
215 double Legendre_poly::gauss_lobatto_gamma(int i) const {
216
217 if (p_gauss_lobatto_gamma == 0x0) { // the weights must be computed
218
219 p_gauss_lobatto_gamma = new double[nn+1] ;
220
221 for (int j=0; j<nn; j++)
222 p_gauss_lobatto_gamma[j] = 1. / (double(j) + 0.5) ;
223
224 p_gauss_lobatto_gamma[nn] = 2. / double(nn) ;
225
226 }
227
228 return p_gauss_lobatto_gamma[i] ;
229
230 }
231
232
233 //---------------------------------------------//
234 // Coefficient of the orthogonal projection //
235 //---------------------------------------------//
236
237 void Legendre_poly::coef_projection(double (*f)(double), double* cf) const {
238
239 // The computation is an approximate one:
240 // it returns the coefficients of an interpolating polynomial with$
241 // a large number of Gauss-Lobatto nodes
242
243 int n_large = 128 ;
244
245 if (nn > n_large) {
246 cerr << "Legendre_poly::coef_projection : nn > n_large !"
247 << endl << " nn = " << nn << " n_large = " << n_large << endl ;
248 abort() ;
249 }
250
251 Legendre_poly leg_large(n_large) ;
252
253 double* cf_large = new double[n_large + 1] ;
254
255 leg_large.coef_interpolant_GL(f, cf_large) ;
256
257 for (int i=0; i<=nn; i++) cf[i] = cf_large[i] ;
258
259 delete [] cf_large ;
260
261 }
262
263
264
265
266
267
268
269
270
271
272
273