File: legendre_poly.C

package info (click to toggle)
lorene 0.0.0~cvs20161116%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, stretch
  • size: 26,444 kB
  • ctags: 13,953
  • sloc: cpp: 212,946; fortran: 21,645; makefile: 1,750; sh: 4
file content (273 lines) | stat: -rw-r--r-- 6,251 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * Definition of class Legendre_poly
 */
 
/*
 *   Copyright (c) 2005 Eric Gourgoulhon
 *
 *   This file is part of LORENE.
 *
 *   LORENE is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   LORENE is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with LORENE; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*
 * $Id: legendre_poly.C,v 1.3 2014/10/06 15:09:48 j_novak Exp $
 * $Log: legendre_poly.C,v $
 * Revision 1.3  2014/10/06 15:09:48  j_novak
 * Modified #include directives to use c++ syntax.
 *
 * Revision 1.2  2005/11/14 14:12:10  e_gourgoulhon
 * Added include <assert.h>
 *
 * Revision 1.1  2005/11/14 01:57:00  e_gourgoulhon
 * First version
 *
 *
 * $Header: /cvsroot/Lorene/School05/Monday/legendre_poly.C,v 1.3 2014/10/06 15:09:48 j_novak Exp $
 *
 */

#include <iostream>

using namespace std ;

#include <cstdlib>
#include <cmath>
#include <cassert>

#include "ortho_poly.h"
#include "grid.h"

void legendre_nodes_weight_GL(int n, double* coloc, double* weight, 
                              double prec = 1.e-12, int itemax = 100) ; 
     

//----------------------//
// Standard constructor //
//----------------------//

Legendre_poly::Legendre_poly(int ni) : Ortho_poly(ni) {}

//--------------------//
//  Copy constructor  //
//--------------------//

Legendre_poly::Legendre_poly(const Legendre_poly& pp) : Ortho_poly(pp) {}


//--------------//
//  Destructor  //
//--------------//

Legendre_poly::~Legendre_poly() {}


//--------------//
//  Assignment  //
//--------------//

void Legendre_poly::operator=(const Legendre_poly& ) {

    cerr << "Legendre_poly::operator= not implemented !" << endl ; 
    abort() ; 
    
}

//-----------//
//  Display  //
//-----------//

ostream& operator<<(ostream& ost, const Legendre_poly& pp) {

    ost << "Basis of Legendre polynomials up to degree " << pp.n() << endl ; 
    
    return ost ; 
}

//-------------------//
//  Weight function  //
//-------------------//

double Legendre_poly::weight(double ) const {
    
    return 1. ; 
    
} 


//-------------------------------------------//
//  Evaluation of the Legendre polynomials   //
//-------------------------------------------//

double Legendre_poly::operator()(int i, double x) const {

    assert( i >= 0 ) ; 
    assert( i <= nn ) ; 
    
    if (i==0) return 1. ; 

    if (i==1) return x ; 
    
    double tjm2 = 1. ;  // value at step j - 2
    double tjm1 = x  ;  // value at step j - 1
    
    for (int j=2; j<=i; j++) {
	    double tj = ( (2*j-1)*x* tjm1 - (j-1)*tjm2 ) / double(j) ;
        tjm2 = tjm1 ; 
        tjm1 = tj ;       	    
    }
    
    return tjm1 ;     
    
}

//------------------------------------//
//  Computation of nodes and weights  //
//------------------------------------//


const Grid& Legendre_poly::gauss_nodes() const {
    
    if (p_gauss_nodes == 0x0) {  // the nodes must initialized
        
        p_gauss_nodes = new Grid_Legendre_Gauss(nn+1) ;
    }
    
    return *p_gauss_nodes ;
}


double Legendre_poly::gauss_weight(int i) const {

    if (p_gauss_weights == 0x0) {  // the weights must be computed

        cerr << "Legendre_poly::gauss_weight : not implemented yet !"
            << endl ; 
        abort() ; 
    }
    
    return p_gauss_weights[i] ;

}


double Legendre_poly::gauss_gamma(int i) const {

    if (p_gauss_gamma == 0x0) {  // the weights must be computed

        p_gauss_gamma = new double[nn+1] ;
        
        for (int j=0; j<=nn; j++) 
            p_gauss_gamma[j] = 1. / (double(j) + 0.5) ; 
    }
    
    return p_gauss_gamma[i] ;

}


const Grid& Legendre_poly::gauss_lobatto_nodes() const {
    
    if (p_gauss_lobatto_nodes == 0x0) {  // the nodes must initialized

        p_gauss_lobatto_nodes = new Grid_Legendre_GL(nn+1) ; 
    }
    
    return *p_gauss_lobatto_nodes ;
}


double Legendre_poly::gauss_lobatto_weight(int i) const {

    if (p_gauss_lobatto_weights == 0x0) {  // the weights must be computed

        p_gauss_lobatto_weights = new double[nn+1] ;
        
        double* xx_work = new double[nn+1] ;
        
        double precis = 1.e-12 ;    // required precision
        int nitermax = 200 ;     // Maximum number of iterations
    
        legendre_nodes_weight_GL(nn+1, xx_work, p_gauss_lobatto_weights, 
                                 precis, nitermax) ; 
        
        delete [] xx_work ; 
    }
    
    return p_gauss_lobatto_weights[i] ;

}


double Legendre_poly::gauss_lobatto_gamma(int i) const {

    if (p_gauss_lobatto_gamma == 0x0) {  // the weights must be computed

        p_gauss_lobatto_gamma = new double[nn+1] ;
        
        for (int j=0; j<nn; j++) 
            p_gauss_lobatto_gamma[j] = 1. / (double(j) + 0.5) ; 

        p_gauss_lobatto_gamma[nn] = 2. / double(nn) ; 
        
    }
    
    return p_gauss_lobatto_gamma[i] ;

}


//---------------------------------------------//
//  Coefficient of the orthogonal projection   //
//---------------------------------------------//

void Legendre_poly::coef_projection(double (*f)(double), double* cf) const {

    // The computation is an approximate one:
    // it returns the coefficients of an interpolating polynomial with$
    // a large number of Gauss-Lobatto nodes
    
    int n_large = 128 ; 
    
    if (nn > n_large) {
        cerr << "Legendre_poly::coef_projection : nn > n_large !"
            << endl << "  nn = " << nn << "  n_large = " << n_large << endl ; 
        abort() ; 
    }
    
    Legendre_poly leg_large(n_large) ;
    
    double* cf_large = new double[n_large + 1] ;
    
    leg_large.coef_interpolant_GL(f, cf_large) ; 
    
    for (int i=0; i<=nn; i++) cf[i] = cf_large[i] ;  
    
    delete [] cf_large ; 
    
}