20 #ifndef _PIECEWISE_DEFINED_FUNCTIONS_H_
21 #define _PIECEWISE_DEFINED_FUNCTIONS_H_
23 #include "mbsim/functions/function.h"
29 template <
typename Ret,
typename Arg>
38 template<
typename Ret>
44 for(
unsigned int i=0; i<
function.size(); i++)
47 void addLimitedFunction(
const LimitedFunction<Ret(
double)> &limitedFunction) {
48 function.push_back(limitedFunction.function);
49 limitedFunction.function->setParent(
this);
50 a.push_back(limitedFunction.limit);
52 Ret zeros(
const Ret &x) {
return Ret(x.size()); }
53 Ret operator()(
const double &x) {
54 for(
unsigned int i=0; i<
function.size(); i++)
56 return y0[i] + (*
function[i])(x-x0[i]);
57 throw MBSimError(
"(PiecewiseDefinedFunction::operator()): x out of range! x= "+numtostr(x)+
", upper bound= "+numtostr(a[
function.size()]));
59 typename B::DRetDArg parDer(
const double &x) {
60 for(
unsigned int i=0; i<
function.size(); i++)
62 return function[i]->parDer(x-x0[i]);
63 throw MBSimError(
"(PiecewiseDefinedFunction::parDer): x out of range! x= "+numtostr(x)+
", upper bound= "+numtostr(a[
function.size()]));
65 typename B::DRetDArg parDerDirDer(
const double &xDir,
const double &x) {
66 for(
unsigned int i=0; i<
function.size(); i++)
68 return function[i]->parDerDirDer(xDir,x-x0[i]);
69 throw MBSimError(
"(PiecewiseDefinedFunction::parDerDirDer): x out of range! x= "+numtostr(x)+
", upper bound= "+numtostr(a[
function.size()]));
71 typename B::DDRetDDArg parDerParDer(
const double &x) {
72 for(
unsigned int i=0; i<
function.size(); i++)
74 return function[i]->parDerParDer(x-x0[i]);
75 throw MBSimError(
"(PiecewiseDefinedFunction::parDerParDer): x out of range! x= "+numtostr(x)+
", upper bound= "+numtostr(a[
function.size()]));
78 void initializeUsingXML(xercesc::DOMElement *element) {
79 xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%
"limitedFunctions");
80 xercesc::DOMElement *ee=e->getFirstElementChild();
81 while(ee && MBXMLUtils::E(ee)->getTagName()==MBSIM%
"LimitedFunction") {
82 addLimitedFunction(
LimitedFunction<Ret(
double)>(
ObjectFactory::createAndInit<
Function<Ret(
double)> >(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%
"function")->getFirstElementChild()),Element::getDouble(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%
"limit"))));
83 ee=ee->getNextElementSibling();
85 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%
"shiftAbscissa");
86 if(e) shiftAbscissa=Element::getBool(e);
87 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%
"shiftOrdinate");
88 if(e) shiftOrdinate=Element::getBool(e);
92 for(
typename std::vector<
Function<Ret(
double)> *>::iterator it=
function.begin(); it!=
function.end(); it++)
96 for(
unsigned int i=1; i<a.size(); i++)
102 y0.resize(a.size(),zeros((*
function[0])(0)));
104 for(
unsigned int i=1; i<a.size(); i++)
105 y0[i] = (*
this)(a[i]);
110 std::vector<Function<Ret(double)> *>
function;
111 std::vector<double> a, x0;
113 bool shiftAbscissa, shiftOrdinate;
117 inline double PiecewiseDefinedFunction<double(double)>::zeros(
const double &x) {
return 0; }
119 template<
typename Ret,
typename Arg>
125 for(
unsigned int i=0; i<
function.size(); i++)
128 void addLimitedFunction(
const LimitedFunction<Ret(Arg)> &limitedFunction) {
129 function.push_back(limitedFunction.function);
130 limitedFunction.function->setParent(
this);
131 a.push_back(limitedFunction.limit);
133 int getArgSize()
const {
return 1; }
134 Ret zeros(
const Ret &x) {
return Ret(x.size()); }
135 Ret operator()(
const Arg &x) {
136 for(
unsigned int i=0; i<
function.size(); i++)
139 throw MBSimError(
"(PiecewiseDefinedFunction::operator()): x out of range! x= "+numtostr(x)+
", upper bound= "+numtostr(a[
function.size()]));
141 typename B::DRetDArg parDer(
const Arg &x) {
142 for(
unsigned int i=0; i<
function.size(); i++)
145 throw MBSimError(
"(PiecewiseDefinedFunction::parDer): x out of range! x= "+numtostr(x)+
", upper bound= "+numtostr(a[
function.size()]));
147 typename B::DRetDArg parDerDirDer(
const Arg &xDir,
const Arg &x) {
148 for(
unsigned int i=0; i<
function.size(); i++)
151 throw MBSimError(
"(PiecewiseDefinedFunction::parDerDirDer): x out of range! x= "+numtostr(x)+
", upper bound= "+numtostr(a[
function.size()]));
154 void initializeUsingXML(xercesc::DOMElement *element) {
155 xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%
"limitedFunctions");
156 xercesc::DOMElement *ee=e->getFirstElementChild();
157 while(ee && MBXMLUtils::E(ee)->getTagName()==MBSIM%
"LimitedFunction") {
158 addLimitedFunction(
LimitedFunction<Ret(Arg)>(
ObjectFactory::createAndInit<
Function<Ret(Arg)> >(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%
"function")->getFirstElementChild()),Element::getDouble(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%
"limit"))));
159 ee=ee->getNextElementSibling();
161 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%
"shiftAbscissa");
162 if(e) shiftAbscissa=Element::getBool(e);
163 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%
"shiftOrdinate");
164 if(e) shiftOrdinate=Element::getBool(e);
168 for(
typename std::vector<
Function<Ret(Arg)> *>::iterator it=
function.begin(); it!=
function.end(); it++)
172 for(
unsigned int i=1; i<a.size(); i++)
178 y0.resize(a.size(),zeros((*
function[0])(Arg(1))));
180 for(
unsigned int i=1; i<a.size(); i++)
186 std::vector<Function<Ret(Arg)> *>
function;
187 std::vector<double> a, x0;
189 bool shiftAbscissa, shiftOrdinate;
193 inline double PiecewiseDefinedFunction<double(fmatvec::VecV)>::zeros(
const double &x) {
return 0; }
Definition: piecewise_defined_function.h:27
void init(Element::InitStage stage)
plots time series header
Definition: piecewise_defined_function.h:90
InitStage
The stages of the initialization.
Definition: element.h:97
Definition: planar_contour.h:31
Definition: element.h:100
void init(Element::InitStage stage)
plots time series header
Definition: piecewise_defined_function.h:166
virtual void init(InitStage stage)
plots time series header
Definition: element.cc:70
static ContainerType * createAndInit(const xercesc::DOMElement *element)
Definition: objectfactory.h:87
basic error class for mbsim
Definition: mbsim_event.h:38
Definition: piecewise_defined_function.h:36