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

One-dimensional interpolated (e.g. More...

#include <PrimitiveFunctions.h>

Inheritance diagram for inet::math::Interpolated1DFunction< R, X >:
inet::math::FunctionBase< R, Domain< X > > inet::math::IFunction< R, Domain< X > >

Public Member Functions

 Interpolated1DFunction (const std::map< X, R > &rs, const IInterpolator< X, R > *interpolator)
 
 Interpolated1DFunction (const std::map< X, std::pair< R, const IInterpolator< X, R > * >> &rs)
 
virtual R getValue (const Point< X > &p) const override
 
virtual void partition (const Interval< X > &i, const std::function< void(const Interval< X > &, const IFunction< R, Domain< X >> *)> callback) const override
 
virtual bool isFinite (const Interval< X > &i) const override
 
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::FunctionBase< R, Domain< X > >
virtual void partition (const typename Domain< X > ::I &i, const std::function< void(const typename Domain< X > ::I &, const IFunction< R, Domain< X > > *)> 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 Domain< X > ::I &i) const override
 Returns the valid range of the function as an interval for the given domain. More...
 
virtual Domain< X > ::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 Domain< X > ::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 Domain< X > ::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 Domain< X > ::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 Domain< X > ::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 Domain< X > ::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 Domain< X > ::I &i) const override
 Returns the integral value for the given domain. More...
 
virtual const Ptr< const IFunction< R, Domain< X > > > add (const Ptr< const IFunction< R, Domain< X > >> &o) const override
 Adds the provided function to this function. More...
 
virtual const Ptr< const IFunction< R, Domain< X > > > subtract (const Ptr< const IFunction< R, Domain< X > >> &o) const override
 Substracts the provided function from this function. More...
 
virtual const Ptr< const IFunction< R, Domain< X > > > multiply (const Ptr< const IFunction< double, Domain< X > >> &o) const override
 Multiplies the provided function with this function. More...
 
virtual const Ptr< const IFunction< double, Domain< X > > > divide (const Ptr< const IFunction< R, Domain< X > >> &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 Domain< X > ::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 Domain< X > ::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 Domain< X > ::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...
 
- Public Member Functions inherited from inet::math::IFunction< R, Domain< X > >
virtual ~IFunction ()
 
virtual R getValue (const typename Domain< X > ::P &p) const=0
 Returns the value of the function at the given point. More...
 

Protected Attributes

const std::map< X, std::pair< R, const IInterpolator< X, R > * > > rs
 

Detailed Description

template<typename R, typename X>
class inet::math::Interpolated1DFunction< R, X >

One-dimensional interpolated (e.g.

constant, linear) function between intervals defined by points on the X axis.

Constructor & Destructor Documentation

◆ Interpolated1DFunction() [1/2]

template<typename R , typename X >
inet::math::Interpolated1DFunction< R, X >::Interpolated1DFunction ( const std::map< X, R > &  rs,
const IInterpolator< X, R > *  interpolator 
)
inline
680  : rs([&] () {
681  std::map<X, std::pair<R, const IInterpolator<X, R> *>> result;
682  for (auto it : rs)
683  result[it.first] = { it.second, interpolator };
684  return result;
685  } ()) {}

◆ Interpolated1DFunction() [2/2]

template<typename R , typename X >
inet::math::Interpolated1DFunction< R, X >::Interpolated1DFunction ( const std::map< X, std::pair< R, const IInterpolator< X, R > * >> &  rs)
inline
687 : rs(rs) {}

Member Function Documentation

◆ getValue()

template<typename R , typename X >
virtual R inet::math::Interpolated1DFunction< R, X >::getValue ( const Point< X > &  p) const
inlineoverridevirtual
689  {
690  X x = std::get<0>(p);
691  auto it = rs.equal_range(x);
692  auto& lt = it.first;
693  auto& ut = it.second;
694  // TODO this nested if looks horrible
695  if (lt != rs.end() && lt->first == x) {
696  if (ut == rs.end())
697  return lt->second.first;
698  else {
699  const auto interpolator = lt->second.second;
700  return interpolator->getValue(lt->first, lt->second.first, ut->first, ut->second.first, x);
701  }
702  }
703  else {
704  ASSERT(lt != rs.end() && ut != rs.end());
705  lt--;
706  const auto interpolator = lt->second.second;
707  return interpolator->getValue(lt->first, lt->second.first, ut->first, ut->second.first, x);
708  }
709  }

◆ isFinite()

template<typename R , typename X >
virtual bool inet::math::Interpolated1DFunction< R, X >::isFinite ( const Interval< X > &  i) const
inlineoverridevirtual
746 { return true; }

◆ partition()

template<typename R , typename X >
virtual void inet::math::Interpolated1DFunction< R, X >::partition ( const Interval< X > &  i,
const std::function< void(const Interval< X > &, const IFunction< R, Domain< X >> *)>  callback 
) const
inlineoverridevirtual
711  {
712  // loop from less or equal than lower to greater or equal than upper inclusive both ends
713  auto lt = rs.lower_bound(std::get<0>(i.getLower()));
714  auto ut = rs.upper_bound(std::get<0>(i.getUpper()));
715  if (lt->first > std::get<0>(i.getLower()))
716  lt--;
717  if (ut == rs.end())
718  ut--;
719  for (auto it = lt; it != ut; it++) {
720  auto jt = it;
721  jt++;
722  auto kt = jt;
723  kt++;
724  auto i1 = i.getIntersected(Interval<X>(Point<X>(it->first), Point<X>(jt->first), 0b1, kt == rs.end() ? 0b1 : 0b0, 0b0));
725  if (!i1.isEmpty()) {
726  const auto interpolator = it->second.second;
727  auto xLower = std::get<0>(i1.getLower());
728  auto xUpper = std::get<0>(i1.getUpper());
729  if (dynamic_cast<const ConstantInterpolatorBase<X, R> *>(interpolator)) {
730  auto value = interpolator->getValue(it->first, it->second.first, jt->first, jt->second.first, (xLower + xUpper) / 2);
731  ConstantFunction<R, Domain<X>> g(value);
732  callback(i1, &g);
733  }
734  else if (dynamic_cast<const LinearInterpolator<X, R> *>(interpolator)) {
735  auto yLower = interpolator->getValue(it->first, it->second.first, jt->first, jt->second.first, xLower);
736  auto yUpper = interpolator->getValue(it->first, it->second.first, jt->first, jt->second.first, xUpper);
737  UnilinearFunction<R, Domain<X>> g(xLower, xUpper, yLower, yUpper, 0);
738  simplifyAndCall(i1, &g, callback);
739  }
740  else
741  throw cRuntimeError("TODO");
742  }
743  }
744  }

◆ printStructure()

template<typename R , typename X >
virtual void inet::math::Interpolated1DFunction< R, X >::printStructure ( std::ostream &  os,
int  level = 0 
) const
inlineoverridevirtual

Prints the internal data structure of this function in a human readable form to the provided stream.

Reimplemented from inet::math::FunctionBase< R, Domain< X > >.

748  {
749  os << "(Interpolated1D";
750  auto size = rs.size();
751  for (auto entry : rs) {
752  const char *interpolatorClassName = nullptr;
753  if (entry.second.second != nullptr) {
754  interpolatorClassName = entry.second.second->getClassName();
755  if (!strncmp(interpolatorClassName, "inet::math::", 12))
756  interpolatorClassName += 12;
757  else if (!strncmp(interpolatorClassName, "inet::", 6))
758  interpolatorClassName += 6;
759  }
760  if (size < 8) {
761  os << ", " << entry.first << " → " << entry.second.first;
762  if (interpolatorClassName != nullptr)
763  os << ", " << interpolatorClassName;
764  }
765  else {
766  os << "\n" << std::string(level + 2, ' ') << entry.first << " → " << entry.second.first;
767  if (interpolatorClassName != nullptr)
768  os << "\n" << std::string(level + 4, ' ') << interpolatorClassName;
769  }
770  }
771  os << ")";
772  }

Member Data Documentation

◆ rs

template<typename R , typename X >
const std::map<X, std::pair<R, const IInterpolator<X, R> *> > inet::math::Interpolated1DFunction< R, X >::rs
protected

The documentation for this class was generated from the following file:
inet::math::Interpolated1DFunction::rs
const std::map< X, std::pair< R, const IInterpolator< X, R > * > > rs
Definition: PrimitiveFunctions.h:677
inet::units::units::g
milli< kg >::type g
Definition: Units.h:1071
inet::math::simplifyAndCall
void simplifyAndCall(const typename D::I &i, const IFunction< R, D > *f, const std::function< void(const typename D::I &, const IFunction< R, D > *)> callback)
Definition: PrimitiveFunctions.h:776