BALL
1.4.1
|
00001 // -*- Mode: C++; tab-width: 2; -*- 00002 // vi: set ts=2: 00003 // 00004 // $Id: numericalIntegrator.h,v 1.18 2004/05/27 19:49:42 oliver Exp $ 00005 // 00006 00007 #ifndef BALL_MATHS_NUMERICALINTEGRATOR_H 00008 #define BALL_MATHS_NUMERICALINTEGRATOR_H 00009 00010 #ifndef BALL_MATHS_FUNCTION_H 00011 # include <BALL/MATHS/function.h> 00012 #endif 00013 00014 namespace BALL 00015 { 00019 template <typename Function, typename DataType = float> 00020 class NumericalIntegrator 00021 { 00022 00023 public: 00024 00025 BALL_CREATE(NumericalIntegrator) 00026 00027 00028 00029 00030 00031 NumericalIntegrator(); 00032 00034 NumericalIntegrator(const NumericalIntegrator& nint); 00035 00037 virtual ~NumericalIntegrator(); 00038 00040 00041 00043 00044 00046 NumericalIntegrator& operator = (const NumericalIntegrator& nint); 00047 00049 00050 00052 00053 00055 bool operator == (const NumericalIntegrator& nint) const; 00056 00058 00059 00061 00062 00066 void setFunction(const Function& function); 00067 00071 const Function& getFunction() const { return function_; } 00072 00076 Function& getFunction() { return function_; } 00077 00082 DataType getValue(const DataType& x) const; 00083 00089 DataType integrate(const DataType& from, const DataType& to) const; 00090 00092 00093 00094 protected: 00095 00096 //_ The function to be integrated 00097 Function function_; 00098 00099 }; 00100 00101 00102 template<typename Function, typename DataType> 00103 BALL_INLINE 00104 NumericalIntegrator<Function, DataType>::NumericalIntegrator() 00105 : function_() 00106 { 00107 } 00108 00109 00110 template<typename Function, typename DataType> 00111 BALL_INLINE 00112 NumericalIntegrator<Function, DataType>::NumericalIntegrator(const NumericalIntegrator<Function, DataType>& nint) 00113 : function_(nint.function_) 00114 { 00115 } 00116 00117 00118 template<typename Function, typename DataType> 00119 BALL_INLINE 00120 NumericalIntegrator<Function, DataType>::~NumericalIntegrator() 00121 { 00122 } 00123 00124 00125 template<typename Function, typename DataType> 00126 BALL_INLINE 00127 NumericalIntegrator<Function, DataType>& 00128 NumericalIntegrator<Function, DataType>::operator = 00129 (const NumericalIntegrator<Function, DataType>& nint) 00130 { 00131 function_ = nint.function_; 00132 return *this; 00133 } 00134 00135 00136 template<typename Function, typename DataType> 00137 BALL_INLINE 00138 void NumericalIntegrator<Function, DataType>::setFunction(const Function& function) 00139 { 00140 function_ = function; 00141 } 00142 00143 00144 template<typename Function, typename DataType> 00145 BALL_INLINE 00146 bool NumericalIntegrator<Function, DataType>::operator == 00147 (const NumericalIntegrator<Function, DataType>& nint) const 00148 { 00149 return (function_ == nint.function_); 00150 } 00151 00152 00153 template<typename Function, typename DataType> 00154 BALL_INLINE 00155 DataType NumericalIntegrator<Function, DataType>::getValue(const DataType& x) const 00156 { 00157 return function_(x); 00158 } 00159 00160 00161 template<typename Function, typename DataType> 00162 BALL_INLINE 00163 DataType NumericalIntegrator<Function, DataType>::integrate( 00164 const DataType& from, const DataType& to) const 00165 { 00166 // ????? 00167 // the number of samples has to be user configurable 00168 Size samples = 30; 00169 Size n = samples; 00170 00171 DataType area = 0; 00172 DataType step = (to - from) / n; 00173 DataType x = from; 00174 00175 while (n > 0) 00176 { 00177 area += (function_(x) + function_(x + step)) / 2.0 * step; 00178 x += step; 00179 --n; 00180 } 00181 00182 return area; 00183 } 00184 } 00185 00186 #endif // BALL_MATHS_NUMERICALINTEGRATOR_H