LORENE
integrate_1D.C
1 /*
2  * Integration of f(x) in the interval [xx(0), xx(n-1)], with non-equally spaced
3  * n-size xx grid.
4  *
5  * The function f is approximated by piecewise parabolae, The integral of f
6  * is set to 0 at xx(0).
7  */
8 
9 /*
10  * Copyright (c) 2015 Jerome Novak
11  *
12  * This file is part of LORENE.
13  *
14  * LORENE is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  *
19  * LORENE is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with LORENE; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27  *
28  */
29 
30 
31 
32 
33 /*
34  * $Id: integrate_1D.C,v 1.2 2016/12/05 16:18:11 j_novak Exp $
35  * $Log: integrate_1D.C,v $
36  * Revision 1.2 2016/12/05 16:18:11 j_novak
37  * Suppression of some global variables (file names, loch, ...) to prevent redefinitions
38  *
39  * Revision 1.1 2015/01/09 15:28:52 j_novak
40  * New integration function for general non-equally-spaced grids.
41  *
42  *
43  */
44 
45 // Headers Lorene
46 #include "tbl.h"
47 
48 namespace Lorene {
49 
50 Tbl integ1D(const Tbl& xx, const Tbl& ff) {
51 
52  Tbl resu(ff) ;
53  if (ff.get_etat() != ETATZERO) {
54 
55  assert (xx.get_etat() == ETATQCQ) ;
56  assert (ff.get_etat() == ETATQCQ) ;
57  int nx = xx.get_taille() ;
58  assert(nx > 2) ;
59  assert (ff.get_taille() == nx) ;
60 
61  resu.set(0) = 0. ;
62  double x0 = xx(0) ;
63  double x1(0), x2(0), x3(0);
64  double a1(0), a2(0), a3(0);
65  double b1(0), b2(0), b3(0);
66  double c1(0), c2(0), c3(0) ;
67 
68  for (int i=1; i<nx-1; i++) {
69  x1 = xx(i-1) ;
70  x2 = xx(i) ;
71  x3 = xx(i+1) ;
72  a1 = ff(i-1) / ( (x1 - x2)*(x1 - x3) ) ;
73  a2 = ff(i) / ( (x2 - x1)*(x2 - x3) ) ;
74  a3 = ff(i+1) / ( (x3 - x1)*(x3 - x2) ) ;
75  b1 = a1 + a2 + a3 ;
76  b2 = -(x2 + x3)*a1 - (x1 + x3)*a2 - (x1 + x2)*a3 ;
77  b3 = x2*x3*a1 + x1*x3*a2 + x1*x2*a3 ;
78  if (i==1) {
79  c1 = b1 ;
80  c2 = b2 ;
81  c3 = b3 ;
82  }
83  else {
84  c1 = 0.5*(b1 + c1) ;
85  c2 = 0.5*(b2 + c2) ;
86  c3 = 0.5*(b3 + c3) ;
87  }
88  resu.set(i) = resu(i-1) + c1*(x2*x2*x2 - x0*x0*x0)/3.
89  + 0.5*c2*(x2*x2 - x0*x0) + c3*(x2 - x0) ;
90  c1 = b1 ;
91  c2 = b2 ;
92  c3 = b3 ;
93  x0 = x2 ;
94  }
95 
96  x2 = xx(nx-1) ;
97  resu.set(nx-1) = resu(nx-2) + c1*(x2*x2*x2 - x0*x0*x0)/3.
98  + 0.5*c2*(x2*x2 - x0*x0) + c3*(x2 - x0) ;
99 
100  }
101  return resu ;
102 }
103 
104 } // End of namespace Lorene
Lorene prototypes.
Definition: app_hor.h:67
double & set(int i)
Read/write of a particular element (index i) (1D case)
Definition: tbl.h:301
int get_etat() const
Gives the logical state.
Definition: tbl.h:414
Tbl integ1D(const Tbl &xx, const Tbl &ff)
Integrates a function defined on an unequally-spaced grid, approximating it by piecewise parabolae...
Definition: integrate_1D.C:50
int get_taille() const
Gives the total size (ie dim.taille)
Definition: tbl.h:417
Basic array class.
Definition: tbl.h:164