Debian Codegraph @

School05/Monday/grid.C

raw
1 /*
2 * Definition of class Grid
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: grid.C,v 1.3 2014/10/06 15:09:47 j_novak Exp $
28 * $Log: grid.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:10 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/grid.C,v 1.3 2014/10/06 15:09:47 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 "grid.h"
52 #include "plot.h"
53
54 //----------------------//
55 // Standard constructor //
56 //----------------------//
57
58 Grid::Grid(int nb_nodes, double* xi) : nn(nb_nodes-1) {
59
60 assert(nb_nodes > 1) ;
61
62 xx = new double[nb_nodes] ;
63
64 for (int i=0; i<=nn; i++) {
65 double xi1 = xi[i] ;
66 if ((xi1 < -1) || (xi1 > 1)) {
67 cerr << "Grid::Grid: node no. " << i << " is not in [-1,1] !" << endl ;
68 abort() ;
69 }
70 for (int j=0; j<i; j++) {
71 if (xi1 == xx[j]) {
72 cerr << "Grid::Grid: the nodes must be different !" << endl ;
73 abort() ;
74 }
75 }
76
77 xx[i] = xi1 ;
78 }
79 }
80
81
82 //--------------------//
83 // Copy constructor //
84 //--------------------//
85
86 Grid::Grid(const Grid& nod) : nn(nod.nn) {
87
88 xx = new double[nn+1] ;
89 for (int i=0; i<=nn; i++) {
90 xx[i] = nod.xx[i] ;
91 }
92 }
93
94 //---------------------------------//
95 // Constructor for derived classes //
96 //---------------------------------//
97
98 Grid::Grid(int nb_nodes) : nn(nb_nodes-1) {
99
100 assert(nb_nodes > 1) ;
101
102 xx = new double[nb_nodes] ;
103
104 // Only the memory allocation for the array xx is performed here,
105 // setting the values in the array is left to the derived classes
106 }
107
108
109 //--------------//
110 // Destructor //
111 //--------------//
112
113 Grid::~Grid() {
114
115 delete [] xx ;
116
117 }
118
119
120 //--------------//
121 // Assignment //
122 //--------------//
123
124 void Grid::operator=(const Grid& nod) {
125
126 assert( nod.nn == nn ) ;
127 for (int i=0; i<=nn; i++) {
128 xx[i] = nod.xx[i] ;
129 }
130
131 }
132
133
134 //-------------//
135 // Accessors //
136 //-------------//
137
138 int Grid::n() const {
139
140 return nn ;
141
142 }
143
144 double Grid::operator()(int i) const {
145
146 assert( (i >= 0) && (i<=nn) ) ;
147
148 return xx[i] ;
149 }
150
151
152 //-----------//
153 // Display //
154 //-----------//
155
156 ostream& operator<<(ostream& ost, const Grid& xn) {
157
158 int nn = xn.n() ;
159 ost << "Number of nodes : " << nn + 1 << endl ;
160 for (int i=0; i<=nn; i++) {
161 ost << " x(" << i << ") = " << xn(i) << endl ;
162 }
163
164 return ost ;
165 }
166
167
168 void Grid::plot(int color, int nfig, double ymin, double ymax,
169 const char* title, const char* label_y,
170 const char* device) const {
171
172 for (int i=0; i<=nn; i++) {
173
174 plot_point(xx[i], 0., color, nfig, ymin, ymax, title, label_y, device) ;
175
176 }
177
178 }
179
180
181 //-----------------------//
182 // Lagrange polynomials //
183 //-----------------------//
184
185 double Grid::lagrange(int i, double x) const {
186
187 double y = 1 ;
188
189 for (int j = 0; j<=nn ; j++) {
190 if (j==i) continue ;
191 y *= (x - xx[j]) / (xx[i] - xx[j]) ;
192 }
193
194 return y ;
195 }
196
197
198 //-------------------//
199 // Nodal polynomial //
200 //-------------------//
201
202 double Grid::nodal_polynomial(double x) const {
203
204 double y = 1 ;
205
206 for (int i = 0; i<=nn ; i++) {
207 y *= x - xx[i] ;
208 }
209
210 return y ;
211
212 }
213
214 //---------------------------//
215 // Interpolating polynomial //
216 //---------------------------//
217
218 double Grid::interpole(double (*f)(double), double x) const {
219
220 double y = 0 ;
221
222 for (int i = 0; i<=nn ; i++) {
223 y += f(xx[i]) * lagrange(i, x) ;
224 }
225
226 return y ;
227
228 }
229
230 //--------------------//
231 // Lebesgue constant //
232 //--------------------//
233
234 double Grid::lebesgue_constant() const {
235
236 int np = 10000 ;
237 double lbc = 0 ;
238
239 for (int j=0; j<np; j++) { // scan of [-1,1]
240 double x = -1. + 2. * double(j) / double(np-1) ;
241 double som = fabs( lagrange(0, x) ) ;
242 for (int i=1; i<=nn; i++) {
243 som += fabs( lagrange(i, x) ) ;
244 }
245 if (som > lbc) lbc = som ;
246 }
247
248 return lbc ;
249
250 }