1 #ifndef SimTK_SimTKCOMMON_FUNCTION_H_
2 #define SimTK_SimTKCOMMON_FUNCTION_H_
87 const Vector& x)
const = 0;
136 : argumentSize(argumentSize), value(value) {
139 assert(x.
size() == argumentSize);
143 const Vector& x)
const override {
144 return static_cast<T
>(0);
185 assert(x.
size() == coefficients.size()-1);
186 T value =
static_cast<T
>(0);
187 for (
int i = 0; i < x.
size(); ++i)
188 value += x[i]*coefficients[i];
189 value += coefficients[x.
size()];
193 const Vector& x)
const override {
194 assert(x.
size() == coefficients.size()-1);
195 assert(derivComponents.
size() > 0);
196 if (derivComponents.
size() == 1)
197 return coefficients(derivComponents[0]);
198 return static_cast<T
>(0);
201 return coefficients.size()-1;
234 assert(x.
size() == 1);
236 T value =
static_cast<T
>(0);
237 for (
int i = 0; i < coefficients.size(); ++i)
238 value = value*arg + coefficients[i];
242 const Vector& x)
const override {
243 assert(x.
size() == 1);
244 assert(derivComponents.
size() > 0);
246 T value =
static_cast<T
>(0);
247 const int derivOrder = (int)derivComponents.
size();
248 const int polyOrder = coefficients.size()-1;
249 for (
int i = 0; i <= polyOrder-derivOrder; ++i) {
250 T coeff = coefficients[i];
251 for (
int j = 0; j < derivOrder; ++j)
252 coeff *= polyOrder-i-j;
253 value = value*arg + coeff;
293 : a(amplitude), w(frequency), p(phase) {}
307 return a*std::sin(w*t + p);
311 const Vector& x)
const override {
313 const int order = derivComponents.
size();
319 case 0:
return a* std::sin(w*t + p);
320 case 1:
return a*w* std::cos(w*t + p);
321 case 2:
return -a*w*w* std::sin(w*t + p);
322 case 3:
return -a*w*w*w*std::cos(w*t + p);
325 const Real sc = (order & 0x1) ? std::cos(w*t+p) : std::sin(w*t+p);
326 const Real wn = std::pow(w, order);
375 { setParameters(y0,y1,x0,x1); }
381 "A zero-length switching interval is illegal; both ends were %g.", x0);
382 m_y0 = y0; m_y1 = y1; m_yr = y1-y0; m_zero =
Real(0)*y0;
383 m_x0 = x0; m_x1 = x1; m_ooxr = 1/(x1-x0); m_sign =
sign(m_ooxr);
388 "Function_<T>::Step::calcValue()",
389 "Expected just one input argument but got %d.", xin.
size());
391 const Real x = xin[0];
392 if ((x-m_x0)*m_sign <= 0)
return m_y0;
393 if ((x-m_x1)*m_sign >= 0)
return m_y1;
396 return m_y0 + f*m_yr;
400 const Vector& xin)
const override {
402 "Function_<T>::Step::calcDerivative()",
403 "Expected just one input argument but got %d.", xin.
size());
405 const int derivOrder = (int)derivComponents.
size();
407 "Function_<T>::Step::calcDerivative()",
408 "Only 1st, 2nd, and 3rd derivatives of the step are available,"
409 " but derivative %d was requested.", derivOrder);
410 const Real x = xin[0];
411 if ((x-m_x0)*m_sign <= 0)
return m_zero;
412 if ((x-m_x1)*m_sign >= 0)
return m_zero;
414 case 1:
return dstepAny (1,m_x0,m_ooxr, x) * m_yr;
415 case 2:
return d2stepAny(1,m_x0,m_ooxr, x) * m_yr;
416 case 3:
return d3stepAny(1,m_x0,m_ooxr, x) * m_yr;
417 default: assert(!
"impossible derivOrder");
434 Real m_x0, m_x1, m_ooxr;
#define SimTK_ERRCHK1_ALWAYS(cond, whereChecked, fmt, a1)
Definition: ExceptionMacros.h:285
#define SimTK_THROW2(exc, a1, a2)
Definition: Exception.h:327
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.
This Array_ helper class is the base class for ArrayView_ which is the base class for Array_; here we...
Definition: Array.h:324
size_type size() const
Return the current number of elements stored in this array.
Definition: Array.h:2075
Definition: Exception.h:257
This is a Function_ subclass which simply returns a fixed value, independent of its arguments.
Definition: Function.h:126
Constant * clone() const override
Create a new heap-allocated copy of this concrete Function.
Definition: Function.h:153
T calcDerivative(const Array_< int > &derivComponents, const Vector &x) const override
Calculate a partial derivative of this function at a particular point.
Definition: Function.h:142
T calcValue(const Vector &x) const override
Calculate the value of this function at a particular point.
Definition: Function.h:138
int getMaxDerivativeOrder() const override
Get the maximum derivative order this Function_ object can calculate.
Definition: Function.h:149
int getArgumentSize() const override
Get the number of components expected in the input vector.
Definition: Function.h:146
Constant(T value, int argumentSize=1)
Create a Function_::Constant object.
Definition: Function.h:135
T calcDerivative(const std::vector< int > &derivComponents, const Vector &x) const
This provides compatibility with std::vector without requiring any copying.
Definition: Function.h:157
This is a Function_ subclass whose output value is a linear function of its arguments: f(x,...
Definition: Function.h:170
T calcDerivative(const std::vector< int > &derivComponents, const Vector &x) const
This provides compatibility with std::vector without requiring any copying.
Definition: Function.h:211
int getMaxDerivativeOrder() const override
Get the maximum derivative order this Function_ object can calculate.
Definition: Function.h:203
T calcDerivative(const Array_< int > &derivComponents, const Vector &x) const override
Calculate a partial derivative of this function at a particular point.
Definition: Function.h:192
T calcValue(const Vector &x) const override
Calculate the value of this function at a particular point.
Definition: Function.h:184
Linear(const Vector_< T > &coefficients)
Create a Function_::Linear object.
Definition: Function.h:182
int getArgumentSize() const override
Get the number of components expected in the input vector.
Definition: Function.h:200
Linear * clone() const override
Create a new heap-allocated copy of this concrete Function.
Definition: Function.h:207
This is a Function_ subclass whose output value is a polynomial of its argument: f(x) = ax^n+bx^(n-1)...
Definition: Function.h:223
T calcValue(const Vector &x) const override
Calculate the value of this function at a particular point.
Definition: Function.h:233
Polynomial(const Vector_< T > &coefficients)
Create a Function_::Polynomial object.
Definition: Function.h:231
int getMaxDerivativeOrder() const override
Get the maximum derivative order this Function_ object can calculate.
Definition: Function.h:260
T calcDerivative(const std::vector< int > &derivComponents, const Vector &x) const
This provides compatibility with std::vector without requiring any copying.
Definition: Function.h:268
T calcDerivative(const Array_< int > &derivComponents, const Vector &x) const override
Calculate a partial derivative of this function at a particular point.
Definition: Function.h:241
Polynomial * clone() const override
Create a new heap-allocated copy of this concrete Function.
Definition: Function.h:264
int getArgumentSize() const override
Get the number of components expected in the input vector.
Definition: Function.h:257
This is a Function_ subclass whose output value is a sinusoid of its argument: f(x) = a*sin(w*x + p) ...
Definition: Function.h:283
Sinusoid * clone() const override
Create a new heap-allocated copy of this concrete Function.
Definition: Function.h:336
virtual Real calcValue(const Vector &x) const override
Calculate the value of this function at a particular point.
Definition: Function.h:305
virtual Real calcDerivative(const Array_< int > &derivComponents, const Vector &x) const override
Calculate a partial derivative of this function at a particular point.
Definition: Function.h:310
Real getFrequency() const
Definition: Function.h:300
void setAmplitude(Real amplitude)
Definition: Function.h:295
void setPhase(Real phase)
Definition: Function.h:297
int getMaxDerivativeOrder() const override
Get the maximum derivative order this Function_ object can calculate.
Definition: Function.h:332
int getArgumentSize() const override
Get the number of components expected in the input vector.
Definition: Function.h:331
Real calcDerivative(const std::vector< int > &derivComponents, const Vector &x) const
This provides compatibility with std::vector without requiring any copying.
Definition: Function.h:340
Sinusoid(Real amplitude, Real frequency, Real phase=0)
Create a Function::Sinusoid object, returning a*sin(w*x+p).
Definition: Function.h:292
void setFrequency(Real frequency)
Definition: Function.h:296
Real getAmplitude() const
Definition: Function.h:299
Real getPhase() const
Definition: Function.h:301
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:355
Step * clone() const override
Create a new heap-allocated copy of this concrete Function.
Definition: Function.h:425
void setParameters(const T &y0, const T &y1, Real x0, Real x1)
Change the four parameters that define the step function; see constructor for documentation.
Definition: Function.h:379
T calcValue(const Vector &xin) const override
Calculate the value of this function at a particular point.
Definition: Function.h:386
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:374
int getMaxDerivativeOrder() const override
Get the maximum derivative order this Function_ object can calculate.
Definition: Function.h:423
T calcDerivative(const Array_< int > &derivComponents, const Vector &xin) const override
Calculate a partial derivative of this function at a particular point.
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:429
int getArgumentSize() const override
Get the number of components expected in the input vector.
Definition: Function.h:422
This abstract class represents a mathematical function that calculates a value of arbitrary type base...
Definition: Function.h:51
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
virtual int getArgumentSize() const =0
Get the number of components expected in the input vector.
virtual T calcValue(const Vector &x) const =0
Calculate the value of this function at a particular point.
virtual Function_ * clone() const
Create a new heap-allocated copy of this concrete Function.
Definition: Function.h:109
virtual T calcDerivative(const Array_< int > &derivComponents, const Vector &x) const =0
Calculate a partial derivative of this function at a particular point.
virtual ~Function_()
Definition: Function.h:58
virtual int getMaxDerivativeOrder() const =0
Get the maximum derivative order this Function_ object can calculate.
int size() const
Definition: VectorBase.h:396
const Real NaN
This is the IEEE "not a number" constant for this implementation of the default-precision Real type; ...
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
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:117
double d3stepAny(double yRange, double x0, double oneOverXRange, double x)
Third derivative of stepAny(): d^3/dx^3 stepAny(x).
Definition: Scalar.h:949
double dstepAny(double yRange, double x0, double oneOverXRange, double x)
First derivative of stepAny(): d/dx stepAny(x).
Definition: Scalar.h:899
ELEM max(const VectorBase< ELEM > &v)
Definition: VectorMath.h:251
double d2stepAny(double yRange, double x0, double oneOverXRange, double x)
Second derivative of stepAny(): d^2/dx^2 stepAny(x).
Definition: Scalar.h:924
unsigned int sign(unsigned char u)
Definition: Scalar.h:311
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:873
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:607