1 #ifndef SimTK_SimTKCOMMON_FUNCTION_H_
2 #define SimTK_SimTKCOMMON_FUNCTION_H_
87 const Vector& x)
const = 0;
125 : argumentSize(argumentSize), value(value) {
128 assert(x.
size() == argumentSize);
132 return static_cast<T
>(0);
147 const int argumentSize;
171 assert(x.
size() == coefficients.size()-1);
172 T value =
static_cast<T
>(0);
173 for (
int i = 0; i < x.
size(); ++i)
174 value += x[i]*coefficients[i];
175 value += coefficients[x.
size()];
179 assert(x.
size() == coefficients.size()-1);
180 assert(derivComponents.
size() > 0);
181 if (derivComponents.
size() == 1)
182 return coefficients(derivComponents[0]);
183 return static_cast<T
>(0);
186 return coefficients.size()-1;
217 assert(x.
size() == 1);
219 T value =
static_cast<T
>(0);
220 for (
int i = 0; i < coefficients.size(); ++i)
221 value = value*arg + coefficients[i];
225 assert(x.
size() == 1);
226 assert(derivComponents.
size() > 0);
228 T value =
static_cast<T
>(0);
229 const int derivOrder = (int)derivComponents.
size();
230 const int polyOrder = coefficients.size()-1;
231 for (
int i = 0; i <= polyOrder-derivOrder; ++i) {
232 T coeff = coefficients[i];
233 for (
int j = 0; j < derivOrder; ++j)
234 coeff *= polyOrder-i-j;
235 value = value*arg + coeff;
273 : a(amplitude), w(frequency), p(phase) {}
287 return a*std::sin(w*t + p);
293 const int order = derivComponents.
size();
299 case 0:
return a* std::sin(w*t + p);
300 case 1:
return a*w* std::cos(w*t + p);
301 case 2:
return -a*w*w* std::sin(w*t + p);
302 case 3:
return -a*w*w*w*std::cos(w*t + p);
305 const Real sc = (order & 0x1) ? std::cos(w*t+p) : std::sin(w*t+p);
306 const Real wn = std::pow(w, order);
353 : m_y0(y0), m_y1(y1), m_yr(y1-y0), m_zero(
Real(0)*y0),
354 m_x0(x0), m_x1(x1), m_ooxr(1/(x1-x0)), m_sign(
sign(m_ooxr))
356 "A zero-length switching interval is illegal; both ends were %g.", x0);
361 "Function_<T>::Step::calcValue()",
362 "Expected just one input argument but got %d.", xin.
size());
364 const Real x = xin[0];
365 if ((x-m_x0)*m_sign <= 0)
return m_y0;
366 if ((x-m_x1)*m_sign >= 0)
return m_y1;
369 return m_y0 + f*m_yr;
374 "Function_<T>::Step::calcDerivative()",
375 "Expected just one input argument but got %d.", xin.
size());
377 const int derivOrder = (int)derivComponents.
size();
379 "Function_<T>::Step::calcDerivative()",
380 "Only 1st, 2nd, and 3rd derivatives of the step are available,"
381 " but derivative %d was requested.", derivOrder);
382 const Real x = xin[0];
383 if ((x-m_x0)*m_sign <= 0)
return m_zero;
384 if ((x-m_x1)*m_sign >= 0)
return m_zero;
386 case 1:
return dstepAny (1,m_x0,m_ooxr, x) * m_yr;
387 case 2:
return d2stepAny(1,m_x0,m_ooxr, x) * m_yr;
388 case 3:
return d3stepAny(1,m_x0,m_ooxr, x) * m_yr;
389 default: assert(!
"impossible derivOrder");
402 const T m_y0, m_y1, m_yr;
404 const Real m_x0, m_x1, m_ooxr;
410 #endif // SimTK_SimTKCOMMON_FUNCTION_H_