Loading...
Searching...
No Matches
rombergIntegrator.h
Go to the documentation of this file.
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: rombergIntegrator.h,v 1.12 2003/08/26 08:04:22 oliver Exp $
//
#ifndef BALL_MATHS_ROMBERGINTEGRATOR_H
#define BALL_MATHS_ROMBERGINTEGRATOR_H
#ifndef BALL_MATHS_NUMERICALINTERGRATOR_H
# include <BALL/MATHS/numericalIntegrator.h>
#endif
namespace BALL
{
template <typename Function, typename DataType>
class RombergIntegrator: public NumericalIntegrator<Function, DataType>
{
public:
BALL_CREATE(RombergIntegrator)
RombergIntegrator(float epsilon=1E-5, Size nsteps=1000);
RombergIntegrator(const RombergIntegrator& romint);
~RombergIntegrator();
const RombergIntegrator& operator = (const RombergIntegrator& romint);
virtual void clear();
void setEpsilon(float eps);
void setMaxNumSteps(Size mns);
bool operator == (const RombergIntegrator& romint) const;
DataType integrate(DataType from, DataType to);
DataType trapezoid(DataType h, DataType from, DataType to);
protected:
float epsilon_;
Size maxNumSteps_;
vector<DataType> result_;
};
template<typename Function, typename DataType>
BALL_INLINE
RombergIntegrator<Function, DataType>::RombergIntegrator(float eps, Size nsteps): NumericalIntegrator<Function, DataType>(), epsilon_(eps), maxNumSteps_(nsteps)
{
result_.reserve(maxNumSteps_ / 10);
}
template<typename Function, typename DataType>
BALL_INLINE
RombergIntegrator<Function, DataType>::RombergIntegrator(const RombergIntegrator<Function, DataType>& romint):NumericalIntegrator<Function, DataType>(romint)
{
}
template<typename Function, typename DataType>
BALL_INLINE
RombergIntegrator<Function, DataType>::~RombergIntegrator()
{
}
template<typename Function, typename DataType>
BALL_INLINE
const RombergIntegrator<Function, DataType>&
RombergIntegrator<Function, DataType>::operator =
(const RombergIntegrator<Function, DataType>& romint)
{
function_ = romint.function_;
maxNumSteps_ = romint.maxNumSteps_;
epsilon_ = romint.epsilon_;
result_ = romint.result_;
return *this;
}
template<typename Function, typename DataType>
BALL_INLINE
void RombergIntegrator<Function, DataType>::clear()
{
// Problem: function_.clear() might not exist... function_.clear();
}
template<typename Function, typename DataType>
BALL_INLINE
void RombergIntegrator<Function, DataType>::setEpsilon(float eps)
{
epsilon_ = eps;
}
template<typename Function, typename DataType>
BALL_INLINE
void RombergIntegrator<Function, DataType>::setMaxNumSteps(Size nsteps)
{
maxNumSteps_ = nsteps;
result_.reserve(maxNumSteps_ / 10); // we hope that we do not need more than 1/10 - th of the allowed operations
}
template<typename Function, typename DataType>
BALL_INLINE
bool RombergIntegrator<Function, DataType>::operator ==
(const RombergIntegrator<Function, DataType> &romint) const
{
return ((function_ == romint.function_)
&& (epsilon_ == romint.epsilon_ )
&& (maxNumSteps_ == romint.maxNumSteps_)
&& (result_ == romint.result_ ));
}
template<typename Function, typename DataType>
BALL_INLINE
DataType RombergIntegrator<Function, DataType>::trapezoid(DataType h, DataType from, DataType to)
{
DataType sum=0;
DataType helper = (to - from);
int count;
Size nsteps = (Size) (sqrt((helper*helper)/(h*h)));
for (count=1; count<nsteps-1; count++)
{
sum +=function_(from+(count*h));
}
sum+=function_(from)+function_(to);
sum*=h;
return sum;
}
template<typename Function, typename DataType>
BALL_INLINE
DataType RombergIntegrator<Function, DataType>::integrate
(DataType from, DataType to)
{
float h = 1.;
result_.push_back(trapezoid(h, from, to)); // this is the zeroth approximation
int i=1;
int j=0;
int count = 0;
DataType dev;
do
{
result_.push_back(trapezoid(h/((float) i+1), from, to));
for (j=1; j <= i; j++)
{
result_.push_back(result_[(i*(i+1))/2 + (j-1)] + 1. / (pow(4, j) - 1) * (result_[(i*(i+1))/2 + j-1 - result_[((i-1)*i)/2+j-1]));
count++;
};
i++;
dev = result_[((i-2)*(i-1))/2+(i-2)] - result_[((i-1)*(i))/2+(i-1)];
} while ( (sqrt(dev*dev) > epsilon_) && (count < maxNumSteps_));
return (result_[((i-1)*(i))/2 + (i-1)]);
}
} // namespace BALL
#endif // BALL_MATHS_ROMBERGINTEGRATOR_H
BALL::NumericalIntegrator< Function, DataType >::function_
Function function_
Definition numericalIntegrator.h:97
BALL::NumericalIntegrator< Function, DataType >::NumericalIntegrator
NumericalIntegrator()
Definition numericalIntegrator.h:104
BALL::RombergIntegrator
Definition rombergIntegrator.h:21
BALL::RombergIntegrator::setMaxNumSteps
void setMaxNumSteps(Size mns)
Set the maximum number of steps we want to use in computation.
Definition rombergIntegrator.h:141
BALL::RombergIntegrator::epsilon_
float epsilon_
Definition rombergIntegrator.h:88
BALL::RombergIntegrator::integrate
DataType integrate(DataType from, DataType to)
Definition rombergIntegrator.h:183
BALL::RombergIntegrator::maxNumSteps_
Size maxNumSteps_
Definition rombergIntegrator.h:89
BALL::RombergIntegrator::RombergIntegrator
RombergIntegrator(float epsilon=1E-5, Size nsteps=1000)
Default constructor.
Definition rombergIntegrator.h:95
BALL::RombergIntegrator::operator=
const RombergIntegrator & operator=(const RombergIntegrator &romint)
Assignment operator.
Definition rombergIntegrator.h:116
BALL::RombergIntegrator::~RombergIntegrator
~RombergIntegrator()
Destructor.
Definition rombergIntegrator.h:108
BALL::RombergIntegrator::trapezoid
DataType trapezoid(DataType h, DataType from, DataType to)
Definition rombergIntegrator.h:161
BALL::RombergIntegrator::operator==
bool operator==(const RombergIntegrator &romint) const
Equality operator.
Definition rombergIntegrator.h:150
BALL::RombergIntegrator::clear
virtual void clear()
Clear method.
Definition rombergIntegrator.h:127
BALL::RombergIntegrator::result_
vector< DataType > result_
Definition rombergIntegrator.h:90
BALL::RombergIntegrator::setEpsilon
void setEpsilon(float eps)
Set the upper bound for the error we want to allow.
Definition rombergIntegrator.h:134
Function
BALL_CREATE
#define BALL_CREATE(name)
Definition create.h:62
BALL_INLINE
#define BALL_INLINE
Definition debug.h:15
BALL
Definition constants.h:13
BALL::Size
BALL_SIZE_TYPE Size
Definition COMMON/global.h:114
numericalIntegrator.h