INET Framework for OMNeT++/OMNEST
inet::math::MemoizedFunction< R, D > Class Template Reference

Truncates the values of a multidimensional function outside the given domain to 0. More...

#include <CompoundFunctions.h>

Inheritance diagram for inet::math::MemoizedFunction< R, D >:
inet::math::FunctionBase< R, D > inet::math::IFunction< R, D >

Public Member Functions

 MemoizedFunction (const Ptr< const IFunction< R, D >> &function, int limit=INT_MAX)
 
virtual R getValue (const typename D::P &p) const override
 Returns the value of the function at the given point. More...
 
virtual void partition (const typename D::I &i, std::function< void(const typename D::I &, const IFunction< R, D > *)> callback) const override
 
- Public Member Functions inherited from inet::math::FunctionBase< R, D >
virtual void partition (const typename D::I &i, const std::function< void(const typename D::I &, const IFunction< R, D > *)> callback) const override
 Subdivides the provided domain and calls back f with the subdomains and the corresponding potentially simpler domain limited functions. More...
 
virtual Interval< R > getRange () const override
 Returns the valid range of the function as an interval. More...
 
virtual Interval< R > getRange (const typename D::I &i) const override
 Returns the valid range of the function as an interval for the given domain. More...
 
virtual D::I getDomain () const override
 Returns the valid domain of the function as an interval. More...
 
virtual bool isFinite () const override
 Returns true if the function value is finite in the whole domain. More...
 
virtual bool isFinite (const typename D::I &i) const override
 Returns true if the function value is finite in the given domain. More...
 
virtual bool isNonZero () const override
 Returns true if the function value is non-zero in the whole domain. More...
 
virtual bool isNonZero (const typename D::I &i) const override
 Returns true if the function value is non-zero in the given domain. More...
 
virtual R getMin () const override
 Returns the minimum value for the whole domain. More...
 
virtual R getMin (const typename D::I &i) const override
 Returns the minimum value for the given domain. More...
 
virtual R getMax () const override
 Returns the maximum value for the whole domain. More...
 
virtual R getMax (const typename D::I &i) const override
 Returns the maximum value for the given domain. More...
 
virtual R getMean () const override
 Returns the mean value for the whole domain. More...
 
virtual R getMean (const typename D::I &i) const override
 Returns the mean value for the given domain. More...
 
virtual R getIntegral () const override
 Returns the integral value for the whole domain. More...
 
virtual R getIntegral (const typename D::I &i) const override
 Returns the integral value for the given domain. More...
 
virtual const Ptr< const IFunction< R, D > > add (const Ptr< const IFunction< R, D >> &o) const override
 Adds the provided function to this function. More...
 
virtual const Ptr< const IFunction< R, D > > subtract (const Ptr< const IFunction< R, D >> &o) const override
 Substracts the provided function from this function. More...
 
virtual const Ptr< const IFunction< R, D > > multiply (const Ptr< const IFunction< double, D >> &o) const override
 Multiplies the provided function with this function. More...
 
virtual const Ptr< const IFunction< double, D > > divide (const Ptr< const IFunction< R, D >> &o) const override
 Divides this function with the provided function. More...
 
virtual std::ostream & printOn (std::ostream &os) const override
 
virtual void print (std::ostream &os, int level=0) const override
 Prints this function in human readable form to the provided stream for the whole domain. More...
 
virtual void print (std::ostream &os, const typename D::I &i, int level=0) const override
 Prints this function in a human readable form to the provided stream for the given domain. More...
 
virtual void printPartitioning (std::ostream &os, const typename D::I &i, int level) const override
 Prints the partitioning of this function in a human readable form to the provided stream for the given domain. More...
 
virtual void printPartition (std::ostream &os, const typename D::I &i, int level=0) const override
 Prints a single partition of this function in a human readable form to the provided stream for the given domain. More...
 
virtual void printStructure (std::ostream &os, int level=0) const override
 Prints the internal data structure of this function in a human readable form to the provided stream. More...
 
- Public Member Functions inherited from inet::math::IFunction< R, D >
virtual ~IFunction ()
 

Protected Attributes

const Ptr< const IFunction< R, D > > function
 
const int limit
 
std::map< typename D::P, R > cache
 

Detailed Description

template<typename R, typename D>
class inet::math::MemoizedFunction< R, D >

Truncates the values of a multidimensional function outside the given domain to 0.

Caches the values of a multidimensional function.

Constructor & Destructor Documentation

◆ MemoizedFunction()

template<typename R , typename D >
inet::math::MemoizedFunction< R, D >::MemoizedFunction ( const Ptr< const IFunction< R, D >> &  function,
int  limit = INT_MAX 
)
inline
160  :
161  function(function), limit(limit) {}

Member Function Documentation

◆ getValue()

template<typename R , typename D >
virtual R inet::math::MemoizedFunction< R, D >::getValue ( const typename D::P &  p) const
inlineoverridevirtual

Returns the value of the function at the given point.

The returned value falls into the range of the function. The provided point must fall into the domain of the function.

Implements inet::math::IFunction< R, D >.

163  {
164  auto it = cache.find(p);
165  if (it != cache.end())
166  return it->second;
167  else {
168  R v = function->getValue(p);
169 #ifdef _OPENMP
170 #pragma omp critical
171 #endif
172  {
173  cache[p] = v;
174  if ((int)cache.size() > limit)
175  cache.clear();
176  }
177  return v;
178  }
179  }

◆ partition()

template<typename R , typename D >
virtual void inet::math::MemoizedFunction< R, D >::partition ( const typename D::I &  i,
std::function< void(const typename D::I &, const IFunction< R, D > *)>  callback 
) const
inlineoverridevirtual
181  {
182  function->partition(i, callback);
183  }

Member Data Documentation

◆ cache

template<typename R , typename D >
std::map<typename D::P, R> inet::math::MemoizedFunction< R, D >::cache
mutableprotected

◆ function

template<typename R , typename D >
const Ptr<const IFunction<R, D> > inet::math::MemoizedFunction< R, D >::function
protected

◆ limit

template<typename R , typename D >
const int inet::math::MemoizedFunction< R, D >::limit
protected

The documentation for this class was generated from the following file:
inet::units::constants::R
const value< double, compose< units::J, compose< pow< units::mol, -1 >, pow< units::kg, -1 > > > > R(8.314472)
inet::math::MemoizedFunction::limit
const int limit
Definition: CompoundFunctions.h:156
inet::math::MemoizedFunction::cache
std::map< typename D::P, R > cache
Definition: CompoundFunctions.h:157