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

#include <AlgebraicOperations.h>

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

Public Member Functions

 AddedFunction (const Ptr< const IFunction< R, D >> &function1, const Ptr< const IFunction< R, D >> &function2)
 
virtual D::I getDomain () const override
 Returns the valid domain of the function as an interval. More...
 
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, 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 bool isFinite (const typename D::I &i) const override
 Returns true if the function value is finite in 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::FunctionBase< R, D >
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 bool isFinite () const override
 Returns true if the function value is finite in the whole 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...
 
- Public Member Functions inherited from inet::math::IFunction< R, D >
virtual ~IFunction ()
 

Protected Attributes

const Ptr< const IFunction< R, D > > function1
 
const Ptr< const IFunction< R, D > > function2
 

Constructor & Destructor Documentation

◆ AddedFunction()

template<typename R , typename D >
inet::math::AddedFunction< R, D >::AddedFunction ( const Ptr< const IFunction< R, D >> &  function1,
const Ptr< const IFunction< R, D >> &  function2 
)
inline
25  :
27  {}

Member Function Documentation

◆ getDomain()

template<typename R , typename D >
virtual D::I inet::math::AddedFunction< R, D >::getDomain ( ) const
inlineoverridevirtual

Returns the valid domain of the function as an interval.

Reimplemented from inet::math::FunctionBase< R, D >.

29  {
30  return function1->getDomain().getIntersected(function2->getDomain());
31  }

◆ getValue()

template<typename R , typename D >
virtual R inet::math::AddedFunction< 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 >.

33  {
34  return function1->getValue(p) + function2->getValue(p);
35  }

◆ isFinite()

template<typename R , typename D >
virtual bool inet::math::AddedFunction< R, D >::isFinite ( const typename D::I &  i) const
inlineoverridevirtual

Returns true if the function value is finite in the given domain.

Reimplemented from inet::math::FunctionBase< R, D >.

100  {
101  return function1->isFinite(i) & function2->isFinite(i);
102  }

◆ partition()

template<typename R , typename D >
virtual void inet::math::AddedFunction< R, D >::partition ( const typename D::I &  i,
const std::function< void(const typename D::I &, const IFunction< R, D > *)>  f 
) const
inlineoverridevirtual

Subdivides the provided domain and calls back f with the subdomains and the corresponding potentially simpler domain limited functions.

Reimplemented from inet::math::FunctionBase< R, D >.

37  {
38  function1->partition(i, [&] (const typename D::I& i1, const IFunction<R, D> *f1) {
39  // NOTE: optimization for 0 + x
40  if (auto f1c = dynamic_cast<const ConstantFunction<R, D> *>(f1)) {
41  if (toDouble(f1c->getConstantValue()) == 0) {
42  function2->partition(i1, callback);
43  return;
44  }
45  }
46  function2->partition(i1, [&] (const typename D::I& i2, const IFunction<R, D> *f2) {
47  // TODO use template specialization for compile time optimization
48  if (auto f1c = dynamic_cast<const ConstantFunction<R, D> *>(f1)) {
49  if (auto f2c = dynamic_cast<const ConstantFunction<R, D> *>(f2)) {
50  ConstantFunction<R, D> g(f1c->getConstantValue() + f2c->getConstantValue());
51  callback(i2, &g);
52  }
53  else if (auto f2l = dynamic_cast<const UnilinearFunction<R, D> *>(f2)) {
54  UnilinearFunction<R, D> g(i2.getLower(), i2.getUpper(), f2l->getValue(i2.getLower()) + f1c->getConstantValue(), f2l->getValue(i2.getUpper()) + f1c->getConstantValue(), f2l->getDimension());
55  simplifyAndCall(i2, &g, callback);
56  }
57  else
58  throw cRuntimeError("TODO");
59  }
60  else if (auto f1l = dynamic_cast<const UnilinearFunction<R, D> *>(f1)) {
61  if (auto f2c = dynamic_cast<const ConstantFunction<R, D> *>(f2)) {
62  UnilinearFunction<R, D> g(i2.getLower(), i2.getUpper(), f1l->getValue(i2.getLower()) + f2c->getConstantValue(), f1l->getValue(i2.getUpper()) + f2c->getConstantValue(), f1l->getDimension());
63  simplifyAndCall(i2, &g, callback);
64  }
65  else if (auto f2l = dynamic_cast<const UnilinearFunction<R, D> *>(f2)) {
66  if (f1l->getDimension() == f2l->getDimension()) {
67  UnilinearFunction<R, D> g(i2.getLower(), i2.getUpper(), f1l->getValue(i2.getLower()) + f2l->getValue(i2.getLower()), f1l->getValue(i2.getUpper()) + f2l->getValue(i2.getUpper()), f1l->getDimension());
68  simplifyAndCall(i2, &g, callback);
69  }
70  else {
71  typename D::P lowerLower = D::P::getZero();
72  typename D::P lowerUpper = D::P::getZero();
73  typename D::P upperLower = D::P::getZero();
74  typename D::P upperUpper = D::P::getZero();
75  lowerLower.set(f1l->getDimension(), i1.getLower().get(f1l->getDimension()));
76  lowerUpper.set(f1l->getDimension(), i1.getLower().get(f1l->getDimension()));
77  upperLower.set(f1l->getDimension(), i1.getUpper().get(f1l->getDimension()));
78  upperUpper.set(f1l->getDimension(), i1.getUpper().get(f1l->getDimension()));
79  lowerLower.set(f2l->getDimension(), i2.getLower().get(f2l->getDimension()));
80  lowerUpper.set(f2l->getDimension(), i2.getUpper().get(f2l->getDimension()));
81  upperLower.set(f2l->getDimension(), i2.getLower().get(f2l->getDimension()));
82  upperUpper.set(f2l->getDimension(), i2.getUpper().get(f2l->getDimension()));
83  R rLowerLower = f1l->getValue(lowerLower) + f2l->getValue(lowerLower);
84  R rLowerUpper = f1l->getValue(lowerUpper) + f2l->getValue(lowerUpper);
85  R rUpperLower = f1l->getValue(upperLower) + f2l->getValue(upperLower);
86  R rUpperUpper = f1l->getValue(upperUpper) + f2l->getValue(upperUpper);
87  BilinearFunction<R, D> g(lowerLower, lowerUpper, upperLower, upperUpper, rLowerLower, rLowerUpper, rUpperLower, rUpperUpper, f1l->getDimension(), f2l->getDimension());
88  simplifyAndCall(i2, &g, callback);
89  }
90  }
91  else
92  throw cRuntimeError("TODO");
93  }
94  else
95  throw cRuntimeError("TODO");
96  });
97  });
98  }

◆ printStructure()

template<typename R , typename D >
virtual void inet::math::AddedFunction< R, D >::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, D >.

104  {
105  os << "(+ ";
106  function1->printStructure(os, level + 3);
107  os << "\n" << std::string(level + 3, ' ');
108  function2->printStructure(os, level + 3);
109  os << ")";
110  }

Member Data Documentation

◆ function1

template<typename R , typename D >
const Ptr<const IFunction<R, D> > inet::math::AddedFunction< R, D >::function1
protected

◆ function2

template<typename R , typename D >
const Ptr<const IFunction<R, D> > inet::math::AddedFunction< R, D >::function2
protected

The documentation for this class was generated from the following file:
inet::math::AddedFunction::function1
const Ptr< const IFunction< R, D > > function1
Definition: AlgebraicOperations.h:21
inet::math::toDouble
double toDouble(const T v)
Definition: Point.h:28
inet::units::units::g
milli< kg >::type g
Definition: Units.h:1071
inet::units::constants::R
const value< double, compose< units::J, compose< pow< units::mol, -1 >, pow< units::kg, -1 > > > > R(8.314472)
inet::math::AddedFunction::function2
const Ptr< const IFunction< R, D > > function2
Definition: AlgebraicOperations.h:22
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