Debian Codegraph @

School05/Monday/chebyshev_poly.C

raw
1 /*
2 * Definition of class Chebyshev_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: chebyshev_poly.C,v 1.3 2014/10/06 15:09:47 j_novak Exp $
28 * $Log: chebyshev_poly.C,v $
29 * Revision 1.3 2014/10/06 15:09:47 j_novak
30 * Modified #include directives to use c++ syntax.
31 *
32 * Revision 1.2 2005/11/14 14:12:09 e_gourgoulhon
33 * Added include <assert.h>
34 *
35 * Revision 1.1 2005/11/14 01:56:58 e_gourgoulhon
36 * First version
37 *
38 *
39 * $Header: /cvsroot/Lorene/School05/Monday/chebyshev_poly.C,v 1.3 2014/10/06 15:09:47 j_novak Exp $
40 *
41 */
42
43
44 #include <iostream>
45
46 using namespace std ;
47
48 #include <cstdlib>
49 #include <cmath>
50 #include <cassert>
51
52 #include "ortho_poly.h"
53 #include "grid.h"
54
55 void coef_cheb_fft(int np, const double* ff, double* cf) ;
56
57 //----------------------//
58 // Standard constructor //
59 //----------------------//
60
61 Chebyshev_poly::Chebyshev_poly(int ni) : Ortho_poly(ni) {}
62
63 //--------------------//
64 // Copy constructor //
65 //--------------------//
66
67 Chebyshev_poly::Chebyshev_poly(const Chebyshev_poly& pp) : Ortho_poly(pp) {}
68
69
70 //--------------//
71 // Destructor //
72 //--------------//
73
74 Chebyshev_poly::~Chebyshev_poly() {}
75
76
77 //--------------//
78 // Assignment //
79 //--------------//
80
81 void Chebyshev_poly::operator=(const Chebyshev_poly& ) {
82
83 cerr << "Chebyshev_poly::operator= not implemented !" << endl ;
84 abort() ;
85
86 }
87
88 //-----------//
89 // Display //
90 //-----------//
91
92 ostream& operator<<(ostream& ost, const Chebyshev_poly& pp) {
93
94 ost << "Basis of Chebyshev polynomials up to degree " << pp.n() << endl ;
95
96 return ost ;
97 }
98
99 //-------------------//
100 // Weight function //
101 //-------------------//
102
103 double Chebyshev_poly::weight(double x) const {
104
105 return 1. / sqrt( 1. - x*x) ;
106
107 }
108
109
110 //-------------------------------------------//
111 // Evaluation of the Chebyshev polynomials //
112 //-------------------------------------------//
113
114 double Chebyshev_poly::operator()(int i, double x) const {
115
116 assert( i >= 0 ) ;
117 assert( i <= nn ) ;
118
119 if (i==0) return 1. ;
120
121 if (i==1) return x ;
122
123 double tjm2 = 1. ; // value at step j - 2
124 double tjm1 = x ; // value at step j - 1
125
126 for (int j=2; j<=i; j++) {
127 double tj = 2*x* tjm1 - tjm2 ;
128 tjm2 = tjm1 ;
129 tjm1 = tj ;
130 }
131
132 return tjm1 ;
133
134 }
135
136 //------------------------------------//
137 // Computation of nodes and weights //
138 //------------------------------------//
139
140
141 const Grid& Chebyshev_poly::gauss_nodes() const {
142
143 if (p_gauss_nodes == 0x0) { // the nodes must initialized
144
145 p_gauss_nodes = new Grid_Chebyshev_Gauss(nn+1) ;
146 }
147
148 return *p_gauss_nodes ;
149 }
150
151
152 double Chebyshev_poly::gauss_weight(int i) const {
153
154 if (p_gauss_weights == 0x0) { // the weights must be computed
155
156 p_gauss_weights = new double[nn+1] ;
157
158 for (int j=0; j<=nn; j++) p_gauss_weights[j] = M_PI / double(nn+1) ;
159 }
160
161 return p_gauss_weights[i] ;
162
163 }
164
165
166 double Chebyshev_poly::gauss_gamma(int i) const {
167
168 if (p_gauss_gamma == 0x0) { // the weights must be computed
169
170 p_gauss_gamma = new double[nn+1] ;
171
172 p_gauss_gamma[0] = M_PI ;
173
174 for (int j=1; j<=nn; j++) p_gauss_gamma[j] = 0.5 * M_PI ;
175 }
176
177 return p_gauss_gamma[i] ;
178
179 }
180
181
182 const Grid& Chebyshev_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_Chebyshev_GL(nn+1) ;
187 }
188
189 return *p_gauss_lobatto_nodes ;
190 }
191
192
193 double Chebyshev_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 p_gauss_lobatto_weights[0] = M_PI / double(2*nn) ;
200
201 for (int j=1; j<nn; j++)
202 p_gauss_lobatto_weights[j] = M_PI / double(nn) ;
203
204 p_gauss_lobatto_weights[nn] = M_PI / double(2*nn) ;
205 }
206
207 return p_gauss_lobatto_weights[i] ;
208
209 }
210
211
212 double Chebyshev_poly::gauss_lobatto_gamma(int i) const {
213
214 if (p_gauss_lobatto_gamma == 0x0) { // the weights must be computed
215
216 p_gauss_lobatto_gamma = new double[nn+1] ;
217
218 p_gauss_lobatto_gamma[0] = M_PI ;
219
220 for (int j=1; j<nn; j++) p_gauss_lobatto_gamma[j] = 0.5 * M_PI ;
221
222 p_gauss_lobatto_gamma[nn] = M_PI ;
223
224 }
225
226 return p_gauss_lobatto_gamma[i] ;
227
228 }
229
230
231 //---------------------------------------------------//
232 // Gauss-Lobatto interpolant polynomial via a FFT //
233 //---------------------------------------------------//
234
235 void Chebyshev_poly::coef_interpolant_GL_FFT(double (*f)(double),
236 double* cf) const {
237
238 // Values of the function at the Gauss-Lobatto nodes
239
240 double* ff = new double[nn+1] ;
241
242 for (int i=0; i<=nn; i++) ff[i] = f( gauss_lobatto_nodes()(i) ) ;
243
244 // Chebyshev transform via a FFT
245
246 coef_cheb_fft(nn+1, ff, cf) ;
247
248 delete [] ff ;
249
250 }
251
252
253 //---------------------------------------------//
254 // Coefficient of the orthogonal projection //
255 //---------------------------------------------//
256
257 void Chebyshev_poly::coef_projection(double (*f)(double), double* cf) const {
258
259 // The computation is an approximate one:
260 // it returns the coefficients of an interpolating polynomial with$
261 // a large number of Gauss-Lobatto nodes
262
263 int n_large = 128 ;
264
265 if (nn > n_large) {
266 cerr << "Chebyshev_poly::coef_projection : nn > n_large !"
267 << endl << " nn = " << nn << " n_large = " << n_large << endl ;
268 abort() ;
269 }
270
271 Chebyshev_poly cheb_large(n_large) ;
272
273 double* cf_large = new double[n_large + 1] ;
274
275 cheb_large.coef_interpolant_GL(f, cf_large) ;
276
277 for (int i=0; i<=nn; i++) cf[i] = cf_large[i] ;
278
279 delete [] cf_large ;
280
281 }
282
283
284
285
286
287
288
289
290
291
292
293