grid.C

00001 /*
00002  * Definition of class Grid
00003  */
00004  
00005 /*
00006  *   Copyright (c) 2005 Eric Gourgoulhon
00007  *
00008  *   This file is part of LORENE.
00009  *
00010  *   LORENE is free software; you can redistribute it and/or modify
00011  *   it under the terms of the GNU General Public License as published by
00012  *   the Free Software Foundation; either version 2 of the License, or
00013  *   (at your option) any later version.
00014  *
00015  *   LORENE is distributed in the hope that it will be useful,
00016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *   GNU General Public License for more details.
00019  *
00020  *   You should have received a copy of the GNU General Public License
00021  *   along with LORENE; if not, write to the Free Software
00022  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  */
00025 
00026 /*
00027  * $Id: grid.C,v 1.2 2005/11/14 14:12:10 e_gourgoulhon Exp $
00028  * $Log: grid.C,v $
00029  * Revision 1.2  2005/11/14 14:12:10  e_gourgoulhon
00030  * Added include <assert.h>
00031  *
00032  * Revision 1.1  2005/11/14 01:56:58  e_gourgoulhon
00033  * First version
00034  *
00035  *
00036  * $Header: /cvsroot/Lorene/School05/Monday/grid.C,v 1.2 2005/11/14 14:12:10 e_gourgoulhon Exp $
00037  *
00038  */
00039 
00040 #include <iostream>
00041 
00042 using namespace std ;
00043 
00044 #include <stdlib.h>
00045 #include <math.h>
00046 #include <assert.h>
00047 
00048 #include "grid.h"
00049 #include "plot.h"
00050 
00051 //----------------------//
00052 // Standard constructor //
00053 //----------------------//
00054 
00055 Grid::Grid(int nb_nodes, double* xi) : nn(nb_nodes-1) {
00056     
00057     assert(nb_nodes > 1) ; 
00058 
00059     xx = new double[nb_nodes] ; 
00060     
00061     for (int i=0; i<=nn; i++) {
00062         double xi1 = xi[i] ; 
00063         if ((xi1 < -1) || (xi1 > 1)) {
00064             cerr << "Grid::Grid: node no. " << i << " is not in [-1,1] !" << endl ;
00065             abort() ;  
00066         }
00067         for (int j=0; j<i; j++) {
00068             if (xi1 == xx[j]) {
00069                 cerr << "Grid::Grid: the nodes must be different !" << endl ; 
00070                 abort() ; 
00071             }
00072         }
00073         
00074         xx[i] = xi1 ; 
00075     }
00076 }
00077 
00078 
00079 //--------------------//
00080 //  Copy constructor  //
00081 //--------------------//
00082 
00083 Grid::Grid(const Grid& nod) : nn(nod.nn) {
00084 
00085     xx = new double[nn+1] ; 
00086     for (int i=0; i<=nn; i++) {
00087         xx[i] = nod.xx[i] ; 
00088     }
00089 }
00090 
00091 //---------------------------------//
00092 // Constructor for derived classes //
00093 //---------------------------------//
00094 
00095 Grid::Grid(int nb_nodes) : nn(nb_nodes-1) {
00096     
00097     assert(nb_nodes > 1) ; 
00098     
00099     xx = new double[nb_nodes] ; 
00100     
00101     // Only the memory allocation for the array xx is performed here,
00102     // setting the values in the array is left to the derived classes
00103 }
00104 
00105 
00106 //--------------//
00107 //  Destructor  //
00108 //--------------//
00109 
00110 Grid::~Grid() {
00111 
00112     delete [] xx ; 
00113     
00114 }
00115 
00116 
00117 //--------------//
00118 //  Assignment  //
00119 //--------------//
00120 
00121 void Grid::operator=(const Grid& nod) {
00122 
00123     assert( nod.nn == nn ) ;
00124     for (int i=0; i<=nn; i++) {
00125         xx[i] = nod.xx[i] ; 
00126     }
00127     
00128 }
00129 
00130 
00131 //-------------//
00132 //  Accessors  //
00133 //-------------//
00134 
00135 int Grid::n() const {
00136 
00137     return nn ; 
00138     
00139 }
00140 
00141 double Grid::operator()(int i) const {
00142 
00143     assert( (i >= 0) && (i<=nn) ) ; 
00144 
00145     return xx[i] ; 
00146 }
00147 
00148 
00149 //-----------//
00150 //  Display  //
00151 //-----------//
00152 
00153 ostream& operator<<(ostream& ost, const Grid& xn) {
00154 
00155     int nn = xn.n() ; 
00156     ost << "Number of nodes : " << nn + 1 << endl ; 
00157     for (int i=0; i<=nn; i++) {
00158         ost << "   x(" << i << ") = " << xn(i) << endl ; 
00159     }
00160     
00161     return ost ; 
00162 }
00163 
00164 
00165 void Grid::plot(int color, int nfig, double ymin, double ymax, 
00166             const char* title, const char* label_y, 
00167             const char* device) const {
00168     
00169     for (int i=0; i<=nn; i++) {
00170 
00171         plot_point(xx[i], 0., color, nfig, ymin, ymax, title, label_y, device) ;
00172         
00173     }    
00174 
00175 }
00176 
00177 
00178 //-----------------------//
00179 // Lagrange polynomials  //
00180 //-----------------------//
00181 
00182 double Grid::lagrange(int i, double x) const {
00183 
00184     double y = 1 ; 
00185     
00186     for (int j = 0; j<=nn ; j++) {
00187         if (j==i) continue ; 
00188         y *= (x - xx[j]) / (xx[i] - xx[j]) ;         
00189     }
00190     
00191     return y ; 
00192 }
00193 
00194 
00195 //-------------------//
00196 // Nodal polynomial  //
00197 //-------------------//
00198 
00199 double Grid::nodal_polynomial(double x) const {
00200     
00201     double y = 1 ; 
00202     
00203     for (int i = 0; i<=nn ; i++) {
00204         y *= x - xx[i] ;         
00205     }
00206     
00207     return y ; 
00208     
00209 }
00210 
00211 //---------------------------//
00212 // Interpolating polynomial  //
00213 //---------------------------//
00214         
00215 double Grid::interpole(double (*f)(double), double x) const {
00216 
00217     double y = 0 ; 
00218     
00219     for (int i = 0; i<=nn ; i++) {
00220         y += f(xx[i]) * lagrange(i, x) ;         
00221     }
00222     
00223     return y ; 
00224         
00225 } 
00226 
00227 //--------------------//
00228 // Lebesgue constant  //
00229 //--------------------//
00230         
00231 double Grid::lebesgue_constant() const {
00232 
00233     int np = 10000 ; 
00234     double lbc = 0 ; 
00235     
00236     for (int j=0; j<np; j++) {  // scan of [-1,1]
00237         double x = -1. + 2. * double(j) / double(np-1) ; 
00238         double som = fabs( lagrange(0, x) ) ; 
00239         for (int i=1; i<=nn; i++) {
00240             som += fabs( lagrange(i, x) ) ; 
00241         }
00242         if (som > lbc) lbc = som ; 
00243     }
00244     
00245     return lbc ; 
00246     
00247 } 

Generated on Tue Dec 6 14:48:44 2011 for POLYNOM by  doxygen 1.4.6