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_
void setAmplitude(Real amplitude)
Definition: Function.h:275
int getMaxDerivativeOrder() const
Get the maximum derivative order this Function_ object can calculate.
Definition: Function.h:242
int getMaxDerivativeOrder() const
Get the maximum derivative order this Function_ object can calculate.
Definition: Function.h:395
Polynomial(const Vector_< T > &coefficients)
Create a Function_::Polynomial object.
Definition: Function.h:214
This Array_ helper class is the base class for ArrayView_ which is the base class for Array_; here we...
Definition: Array.h:48
size_type size() const
Return the current number of elements stored in this array.
Definition: Array.h:2037
This is a Function_ subclass whose output value is a linear function of its arguments: f(x...
Definition: Function.h:156
#define SimTK_ERRCHK1_ALWAYS(cond, whereChecked, fmt, a1)
Definition: ExceptionMacros.h:285
double d2stepAny(double yRange, double x0, double oneOverXRange, double x)
Second derivative of stepAny(): d^2/dx^2 stepAny(x).
Definition: Scalar.h:971
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
virtual int getArgumentSize() const
Get the number of components expected in the input vector.
Definition: Function.h:134
virtual Real calcDerivative(const Array_< int > &derivComponents, const Vector &x) const
Calculate a partial derivative of this function at a particular point.
Definition: Function.h:290
int size() const
Definition: VectorBase.h:396
virtual Real calcValue(const Vector &x) const
Calculate the value of this function at a particular point.
Definition: Function.h:285
virtual T calcDerivative(const Array_< int > &derivComponents, const Vector &x) const =0
Calculate a partial derivative of this function at a particular point.
T calcDerivative(const Array_< int > &derivComponents, const Vector &x) const
Calculate a partial derivative of this function at a particular point.
Definition: Function.h:224
Real getAmplitude() const
Definition: Function.h:279
Constant(T value, int argumentSize=1)
Create a Function_::Constant object.
Definition: Function.h:124
T calcDerivative(const std::vector< int > &derivComponents, const Vector &x) const
This provides compatibility with std::vector without requiring any copying.
Definition: Function.h:399
T calcDerivative(const std::vector< int > &derivComponents, const Vector &x) const
This provides compatibility with std::vector without requiring any copying.
Definition: Function.h:248
SimTK_Real Real
This is the default compiled-in floating point type for SimTK, either float or double.
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:577
virtual T calcValue(const Vector &x) const =0
Calculate the value of this function at a particular point.
Real getFrequency() const
Definition: Function.h:280
int getMaxDerivativeOrder() const
Get the maximum derivative order this Function_ object can calculate.
Definition: Function.h:137
T calcDerivative(const Array_< int > &derivComponents, const Vector &x) const
Calculate a partial derivative of this function at a particular point.
Definition: Function.h:178
Step(const T &y0, const T &y1, Real x0, Real x1)
Create a Function_::Step object that smoothly interpolates its output through a given range as its in...
Definition: Function.h:352
T calcDerivative(const Array_< int > &derivComponents, const Vector &xin) const
Calculate a partial derivative of this function at a particular point.
Definition: Function.h:372
virtual int getMaxDerivativeOrder() const
Get the maximum derivative order this Function_ object can calculate.
Definition: Function.h:312
const Real NaN
This is the IEEE "not a number" constant for this implementation of the default-precision Real type; ...
void setPhase(Real phase)
Definition: Function.h:277
virtual int getArgumentSize() const =0
Get the number of components expected in the input vector.
This is a Function_ subclass whose output value is a polynomial of its argument: f(x) = ax^n+bx^(n-1)...
Definition: Function.h:206
Real calcDerivative(const std::vector< int > &derivComponents, const Vector &x) const
This provides compatibility with std::vector without requiring any copying.
Definition: Function.h:318
virtual int getMaxDerivativeOrder() const =0
Get the maximum derivative order this Function_ object can calculate.
double dstepAny(double yRange, double x0, double oneOverXRange, double x)
First derivative of stepAny(): d/dx stepAny(x).
Definition: Scalar.h:946
Sinusoid(Real amplitude, Real frequency, Real phase=0)
Create a Function::Sinusoid object, returning a*sin(w*x+p).
Definition: Function.h:272
T calcValue(const Vector &x) const
Calculate the value of this function at a particular point.
Definition: Function.h:127
virtual int getArgumentSize() const
Get the number of components expected in the input vector.
Definition: Function.h:185
T calcDerivative(const std::vector< int > &derivComponents, const Vector &x) const
This provides compatibility with std::vector without requiring any copying.
Definition: Function.h:143
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
Linear(const Vector_< T > &coefficients)
Create a Function_::Linear object.
Definition: Function.h:168
T calcValue(const Vector &x) const
Calculate the value of this function at a particular point.
Definition: Function.h:216
This is a Function_ subclass whose output value is a sinusoid of its argument: f(x) = a*sin(w*x + p) ...
Definition: Function.h:263
This abstract class represents a mathematical function that calculates a value of arbitrary type base...
Definition: Function.h:51
This is a Function_ subclass which simply returns a fixed value, independent of its arguments...
Definition: Function.h:115
double d3stepAny(double yRange, double x0, double oneOverXRange, double x)
Third derivative of stepAny(): d^3/dx^3 stepAny(x).
Definition: Scalar.h:996
T calcDerivative(const Array_< int > &derivComponents, const Vector &x) const
Calculate a partial derivative of this function at a particular point.
Definition: Function.h:131
unsigned int sign(unsigned char u)
Definition: Scalar.h:311
virtual int getArgumentSize() const
Get the number of components expected in the input vector.
Definition: Function.h:311
virtual int getArgumentSize() const
Get the number of components expected in the input vector.
Definition: Function.h:239
double stepAny(double y0, double yRange, double x0, double oneOverXRange, double x)
Interpolate smoothly from y0 to y1 as the input argument goes from x0 to x1, with first and second de...
Definition: Scalar.h:920
int getMaxDerivativeOrder() const
Get the maximum derivative order this Function_ object can calculate.
Definition: Function.h:188
void setFrequency(Real frequency)
Definition: Function.h:276
T calcValue(const Vector &x) const
Calculate the value of this function at a particular point.
Definition: Function.h:170
virtual ~Function_()
Definition: Function.h:58
T calcValue(const Vector &xin) const
Calculate the value of this function at a particular point.
Definition: Function.h:359
T calcDerivative(const std::vector< int > &derivComponents, const Vector &x) const
This provides compatibility with std::vector without requiring any copying.
Definition: Function.h:194
This is the header which should be included in user programs that would like to make use of all the S...
Includes internal headers providing declarations for the basic SimTK Core classes.
Real getPhase() const
Definition: Function.h:281
Function_< Real > Function
This typedef is used for the very common case that the return type of the Function object is Real...
Definition: Function.h:106
virtual int getArgumentSize() const
Get the number of components expected in the input vector.
Definition: Function.h:394
T calcDerivative(const std::vector< int > &derivComponents, const Vector &x) const
This provides compatibility with std::vector without requiring any copying.
Definition: Function.h:91
This is a Function_ subclass whose output value y=f(x) is smoothly stepped from y=y0 to y1 as its inp...
Definition: Function.h:333