aimsdata  4.7.0
Neuroimaging data handling
trapezeitg.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 /*
35  * Trapeze numerical integration.
36  */
37 #ifndef AIMS_MATH_TRAPEZEITG_H
38 #define AIMS_MATH_TRAPEZEITG_H
39 
40 
41 #include <cstdlib>
42 #include <aims/math/integrator.h>
43 #include <math.h>
44 
45 namespace aims {
46 
47 template <class REAL>
49 {
50  public:
51 
52  // to have 2 ^ nStep + 1 steps
53  TrapezeIntegratorOf( int nStep = 10 ) : _nStep( nStep ) { }
54  virtual ~TrapezeIntegratorOf() { }
55 
56  virtual REAL stage( const Integrable& func,
57  REAL a, REAL b, int n ) const;
58  virtual REAL eval( const Integrable& func, REAL a, REAL b ) const;
59 
60  private:
61 
62  int _nStep;
63 };
64 
65 
66 template <class REAL> inline
68  REAL a, REAL b, int n ) const
69 {
70  REAL x, tnm, sum, del;
71 
72  // s was not initialized and is used if n != 1. I choosed to initialize it
73  // to 0 without thinking too much to the consequences.
74  REAL s = 0;
75 
76  int it = 1, j = 1;
77 
78  if ( n == 1 )
79  s = 0.5 * (b - a) * ( func.valueAt( a ) + func.valueAt( b ) );
80  else
81  {
82  for ( it = 1, j = 1; j < n - 1; j++ )
83  it <<= 1;
84  tnm = it;
85  del = ( b - a ) / tnm;
86  x = a + 0.5 * del;
87  for ( sum = 0.0, j = 1; j <= it; j++, x += del )
88  sum += func.valueAt( x );
89  s = 0.5 * ( s + ( b - a ) * sum / tnm );
90  }
91  return s;
92 }
93 
94 template <class REAL> inline
96  REAL a, REAL b ) const
97 {
98  REAL olds = -1.0e30;
99  REAL EPS = 1.0e-5;
100  REAL s;
101 
102  for ( int j = 1; j <= _nStep + 1; j++ ) {
103  s = stage( func, a, b, j );
104  if ( j > 5 )
105  if ( fabs( s - olds ) < EPS * fabs( olds ) ||
106  ( s == 0.0 && olds == 0.0 ) )
107  return s;
108  olds = s;
109  }
110 
111  return 0.0;
112 }
113 
114 
115 // For backward compatibility
117 
118 } // namespace aims
119 
120 #endif
virtual REAL valueAt(REAL) const =0
TrapezeIntegratorOf< float > TrapezeIntegrator
Definition: trapezeitg.h:116
DataTypeTraits< T >::LongType sum(const Volume< T > &vol)
The class for EcatSino data write operation.
Definition: border.h:42
virtual ~TrapezeIntegratorOf()
Definition: trapezeitg.h:54
virtual REAL eval(const Integrable &func, REAL a, REAL b) const
Definition: trapezeitg.h:95
TrapezeIntegratorOf(int nStep=10)
Definition: trapezeitg.h:53
virtual REAL stage(const Integrable &func, REAL a, REAL b, int n) const
Definition: trapezeitg.h:67