00001 #ifndef SimTK_SIMMATH_FUNCTION_H_ 00002 #define SimTK_SIMMATH_FUNCTION_H_ 00003 00004 /* -------------------------------------------------------------------------- * 00005 * SimTK Core: SimTK Simmath(tm) * 00006 * -------------------------------------------------------------------------- * 00007 * This is part of the SimTK Core biosimulation toolkit originating from * 00008 * Simbios, the NIH National Center for Physics-Based Simulation of * 00009 * Biological Structures at Stanford, funded under the NIH Roadmap for * 00010 * Medical Research, grant U54 GM072970. See https://simtk.org. * 00011 * * 00012 * Portions copyright (c) 2008 Stanford University and the Authors. * 00013 * Authors: Peter Eastman * 00014 * Contributors: * 00015 * * 00016 * Permission is hereby granted, free of charge, to any person obtaining a * 00017 * copy of this software and associated documentation files (the "Software"), * 00018 * to deal in the Software without restriction, including without limitation * 00019 * the rights to use, copy, modify, merge, publish, distribute, sublicense, * 00020 * and/or sell copies of the Software, and to permit persons to whom the * 00021 * Software is furnished to do so, subject to the following conditions: * 00022 * * 00023 * The above copyright notice and this permission notice shall be included in * 00024 * all copies or substantial portions of the Software. * 00025 * * 00026 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * 00027 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * 00028 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * 00029 * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 00030 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 00031 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE * 00032 * USE OR OTHER DEALINGS IN THE SOFTWARE. * 00033 * -------------------------------------------------------------------------- */ 00034 00035 #include "SimTKcommon.h" 00036 00037 namespace SimTK { 00038 00050 template <int N> 00051 class Function { 00052 public: 00053 class Constant; 00054 class Linear; 00055 class Polynomial; 00056 virtual ~Function() { 00057 } 00063 virtual Vec<N> calcValue(const Vector& x) const = 0; 00075 virtual Vec<N> calcDerivative(const std::vector<int>& derivComponents, const Vector& x) const = 0; 00079 virtual int getArgumentSize() const = 0; 00083 virtual int getMaxDerivativeOrder() const = 0; 00084 }; 00085 00090 template <int N> 00091 class Function<N>::Constant : public Function<N> { 00092 public: 00099 Constant(Vec<N> value, int argumentSize) : value(value), argumentSize(argumentSize) { 00100 } 00101 Vec<N> calcValue(const Vector& x) const { 00102 assert(x.size() == argumentSize); 00103 return value; 00104 } 00105 Vec<N> calcDerivative(const std::vector<int>& derivComponents, const Vector& x) const { 00106 return Vec<N>(0.0); 00107 } 00108 virtual int getArgumentSize() const { 00109 return argumentSize; 00110 } 00111 int getMaxDerivativeOrder() const { 00112 return std::numeric_limits<int>::max(); 00113 } 00114 private: 00115 const int argumentSize; 00116 const Vec<N> value; 00117 }; 00118 00124 template <int N> 00125 class Function<N>::Linear : public Function<N> { 00126 public: 00135 Linear(const Vector_<Vec<N> >& coefficients) : coefficients(coefficients) { 00136 } 00137 Vec<N> calcValue(const Vector& x) const { 00138 assert(x.size() == coefficients.size()-1); 00139 Vec<N> value(0); 00140 for (int i = 0; i < x.size(); ++i) 00141 value += x[i]*coefficients[i]; 00142 value += coefficients[x.size()]; 00143 return value; 00144 } 00145 Vec<N> calcDerivative(const std::vector<int>& derivComponents, const Vector& x) const { 00146 assert(x.size() == coefficients.size()-1); 00147 assert(derivComponents.size() > 0); 00148 if (derivComponents.size() == 1) 00149 return coefficients(derivComponents[0]); 00150 return Vec<N>(0.0); 00151 } 00152 virtual int getArgumentSize() const { 00153 return coefficients.size()-1; 00154 } 00155 int getMaxDerivativeOrder() const { 00156 return std::numeric_limits<int>::max(); 00157 } 00158 private: 00159 const Vector_<Vec<N> > coefficients; 00160 }; 00161 00162 00168 template <int N> 00169 class Function<N>::Polynomial : public Function<N> { 00170 public: 00176 Polynomial(const Vector_<Vec<N> >& coefficients) : coefficients(coefficients) { 00177 } 00178 Vec<N> calcValue(const Vector& x) const { 00179 assert(x.size() == 1); 00180 Real arg = x[0]; 00181 Vec<N> value(0); 00182 for (int i = 0; i < coefficients.size(); ++i) 00183 value = value*arg + coefficients[i]; 00184 return value; 00185 } 00186 Vec<N> calcDerivative(const std::vector<int>& derivComponents, const Vector& x) const { 00187 assert(x.size() == 1); 00188 assert(derivComponents.size() > 0); 00189 Real arg = x[0]; 00190 Vec<N> value(0); 00191 const int derivOrder = (int)derivComponents.size(); 00192 const int polyOrder = coefficients.size()-1; 00193 for (int i = 0; i <= polyOrder-derivOrder; ++i) { 00194 Vec<N> coeff = coefficients[i]; 00195 for (int j = 0; j < derivOrder; ++j) 00196 coeff *= polyOrder-i-j; 00197 value = value*arg + coeff; 00198 } 00199 return value; 00200 } 00201 virtual int getArgumentSize() const { 00202 return 1; 00203 } 00204 int getMaxDerivativeOrder() const { 00205 return std::numeric_limits<int>::max(); 00206 } 00207 private: 00208 const Vector_<Vec<N> > coefficients; 00209 }; 00210 00211 } // namespace SimTK 00212 00213 #endif // SimTK_SIMMATH_FUNCTION_H_ 00214 00215