Differentiator.h

Go to the documentation of this file.
00001 #ifndef SimTK_DIFFERENTIATOR_H_
00002 #define SimTK_DIFFERENTIATOR_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) 2006-10 Stanford University and the Authors.        *
00013  * Authors: Michael Sherman                                                   *
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 
00040 #include "SimTKcommon.h"
00041 #include "internal/common.h"
00042 #include "SimTKcommon/internal/BigMatrix.h"
00043 
00044 namespace SimTK {
00045 
00046 
00086 class SimTK_SIMMATH_EXPORT Differentiator {
00087 public:
00088     // This are local classes within Differentiator; defined below.
00089     class ScalarFunction;   // ordinary scalar function of a scalar
00090     class GradientFunction; // scalar function of vector
00091     class JacobianFunction; // vector function of vector
00092     class Function;         // abstraction of the above
00093 
00094     // These are the exceptions that can be thrown by this class.
00095     class OpNotAllowedForFunctionOfThisShape;
00096     class UserFunctionThrewAnException;
00097     class UserFunctionReturnedNonzeroStatus;
00098     class UnknownMethodSpecified;
00099 
00100 
00101     enum Method {
00102         UnspecifiedMethod=0,
00103         ForwardDifference=1,
00104         CentralDifference=2
00105     };
00106     static bool        isValidMethod(Method);
00107     static const char* getMethodName(Method);
00108     static int         getMethodOrder(Method);
00109 
00110     virtual ~Differentiator();
00111     explicit Differentiator(const Function& f, 
00112                             Method          defaultMethod=UnspecifiedMethod);
00113 
00114     // You can change the default method; normally it is ForwardDifference.
00115     // If you set it to 'UnspecifiedMethod' it goes back to the original default.
00116     Differentiator& setDefaultMethod(Method);
00117     Method          getDefaultMethod() const;
00118 
00119     // These are the real routines, which are efficient and flexible
00120     // but somewhat messy to use.
00121     void calcDerivative(Real y0, Real fy0, Real& dfdy, 
00122                         Method=UnspecifiedMethod) const;
00123     void calcGradient  (const Vector& y0, Real fy0, Vector& gf,
00124                         Method=UnspecifiedMethod) const;
00125     void calcJacobian  (const Vector& y0, const Vector& fy0, Matrix& dfdy,
00126                         Method=UnspecifiedMethod) const;
00127 
00128     // These provide a simpler though less efficient interface. They will
00129     // do some heap allocation, and will make an initial unperturbed call
00130     // to the user function.
00131     Real   calcDerivative(Real          y0, Method=UnspecifiedMethod) const;
00132     Vector calcGradient  (const Vector& y0, Method=UnspecifiedMethod) const;
00133     Matrix calcJacobian  (const Vector& y0, Method=UnspecifiedMethod) const;
00134 
00135     // Statistics (mutable)
00136     void resetAllStatistics();                 // reset all stats to zero
00137     int getNumDifferentiations() const;        // total # calls of calcWhatever
00138     int getNumDifferentiationFailures() const; // # of those that failed
00139     int getNumCallsToUserFunction() const;     // total # calls to user function
00140 
00141     // This is a local class.
00142     class DifferentiatorRep;
00143 private:
00144     // opaque implementation for binary compatibility
00145     DifferentiatorRep* rep;
00146 
00147 private:
00148     //OBSOLETE NAMES
00149     int getNDifferentiations() const {return getNumDifferentiations();}
00150     int getNDifferentiationFailures() const {return getNumDifferentiationFailures();}
00151     int getNCallsToUserFunction() const {return getNumCallsToUserFunction();}
00152 };
00153 
00167 class SimTK_SIMMATH_EXPORT Differentiator::Function {
00168 public:
00169     Function& setNumFunctions(int);
00170     Function& setNumParameters(int);
00171     Function& setEstimatedAccuracy(Real);
00172 
00173     // These values are fixed after construction.
00174     int  getNumFunctions()  const;
00175     int  getNumParameters() const;
00176     Real getEstimatedAccuracy() const; // approx. "roundoff" in f calculation
00177 
00178     // Statistics (mutable)
00179     void resetAllStatistics();
00180     int getNumCalls()    const; // # evaluations of this function since reset
00181     int getNumFailures() const; // # of calls which failed
00182 
00183     // This is the declaration of a local class name.
00184     class FunctionRep;
00185 protected:
00186     Function();
00187     ~Function();
00188 
00189     // opaque implementation for binary compatibility
00190     FunctionRep* rep;
00191 
00192 private:
00193     // suppress copy constructor and copy assignment
00194     Function(const Function&);
00195     Function& operator=(const Function&);
00196 
00197 private:
00198     //OBSOLETE NAMES
00199     Function& setNFunctions(int n) {return setNumFunctions(n);}
00200     Function& setNParameters(int n) {return setNumParameters(n);}
00201     int  getNFunctions()  const {return getNumFunctions();}
00202     int  getNParameters() const {return getNumParameters();}
00203     int getNCalls()    const {return getNumCalls();}
00204     int getNFailures() const {return getNumFailures();}
00205 
00206 
00207 
00208 friend class Differentiator;
00209 };
00210 
00215 class SimTK_SIMMATH_EXPORT Differentiator::ScalarFunction : public Differentiator::Function {
00216 public:
00217     virtual int f(Real x, Real& fx) const=0;
00218 
00219 protected:
00220     explicit ScalarFunction(Real acc=-1);
00221     virtual ~ScalarFunction() { }
00222 
00223 private:
00224     // suppress copy constructor and copy assignment
00225     ScalarFunction(const Function&);
00226     ScalarFunction& operator=(const Function&);
00227 };
00228 
00234 class SimTK_SIMMATH_EXPORT Differentiator::GradientFunction : public Differentiator::Function {
00235 public:
00236     virtual int f(const Vector& y, Real& fy) const=0;
00237 
00238 protected:
00239     explicit GradientFunction(int ny=-1, Real acc=-1);
00240     virtual ~GradientFunction() { }
00241 
00242 private:
00243     // suppress copy constructor and copy assignment
00244     GradientFunction(const GradientFunction&);
00245     GradientFunction& operator=(const GradientFunction&);
00246 };
00247 
00253 class SimTK_SIMMATH_EXPORT Differentiator::JacobianFunction : public Differentiator::Function {
00254 public:
00255     virtual int f(const Vector& y, Vector& fy) const=0;
00256 
00257 protected:
00258     explicit JacobianFunction(int nf=-1, int ny=-1, Real acc=-1); 
00259     virtual ~JacobianFunction() { }
00260 
00261 private:
00262     // suppress copy constructor and copy assignment
00263     JacobianFunction(const JacobianFunction&);
00264     JacobianFunction& operator=(const JacobianFunction&);
00265 };
00266 
00267 } // namespace SimTK
00268 
00269 #endif // SimTK_DIFFERENTIATOR_H_

Generated on Thu Aug 12 16:37:06 2010 for SimTKcore by  doxygen 1.6.1