Differentiator.h

Go to the documentation of this file.
00001 #ifndef SimTK_DIFFERENTIATOR_H_
00002 #define SimTK_DIFFERENTIATOR_H_
00003 
00004 /* Portions copyright (c) 2006 Stanford University and Michael Sherman.
00005  * Contributors:
00006  * 
00007  * Permission is hereby granted, free of charge, to any person obtaining
00008  * a copy of this software and associated documentation files (the
00009  * "Software"), to deal in the Software without restriction, including 
00010  * without limitation the rights to use, copy, modify, merge, publish, 
00011  * distribute, sublicense, and/or sell copies of the Software, and to
00012  * permit persons to whom the Software is furnished to do so, subject
00013  * to the following conditions:
00014  * 
00015  * The above copyright notice and this permission notice shall be included 
00016  * in all copies or substantial portions of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00021  * IN NO EVENT SHALL THE AUTHORS, COPYRIGHT HOLDERS, OR CONTRIBUTORS BE
00022  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00023  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00024  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  */
00026 
00032 #include "SimTKcommon.h"
00033 #include "internal/common.h"
00034 #include "SimTKcommon/internal/BigMatrix.h"
00035 
00036 namespace SimTK {
00037 
00038 
00049 class SimTK_SIMMATH_EXPORT Differentiator {
00050 public:
00051     // This are local classes within Differentiator; defined below.
00052     class ScalarFunction;   // ordinary scalar function of a scalar
00053     class GradientFunction; // scalar function of vector
00054     class JacobianFunction; // vector function of vector
00055     class Function;         // abstraction of the above
00056 
00057     // These are the exceptions that can be thrown by this class.
00058     class OpNotAllowedForFunctionOfThisShape;
00059     class UserFunctionThrewAnException;
00060     class UserFunctionReturnedNonzeroStatus;
00061     class UnknownMethodSpecified;
00062 
00063 
00064     enum Method {
00065         UnspecifiedMethod=0,
00066         ForwardDifference=1,
00067         CentralDifference=2
00068     };
00069     static bool        isValidMethod(Method);
00070     static const char* getMethodName(Method);
00071     static int         getMethodOrder(Method);
00072 
00073     virtual ~Differentiator();
00074     explicit Differentiator(const Function& f, 
00075                             Method          defaultMethod=UnspecifiedMethod);
00076 
00077     // You can change the default method; normally it is ForwardDifference.
00078     // If you set it to 'UnspecifiedMethod' it goes back to the original default.
00079     Differentiator& setDefaultMethod(Method);
00080     Method          getDefaultMethod() const;
00081 
00082     // These are the real routines, which are efficient and flexible
00083     // but somewhat messy to use.
00084     void calcDerivative(Real y0, Real fy0, Real& dfdy, 
00085                         Method=UnspecifiedMethod) const;
00086     void calcGradient  (const Vector& y0, Real fy0, Vector& gf,
00087                         Method=UnspecifiedMethod) const;
00088     void calcJacobian  (const Vector& y0, const Vector& fy0, Matrix& dfdy,
00089                         Method=UnspecifiedMethod) const;
00090 
00091     // These provide a simpler though less efficient interface. They will
00092     // do some heap allocation, and will make an initial unperturbed call
00093     // to the user function.
00094     Real   calcDerivative(Real          y0, Method=UnspecifiedMethod) const;
00095     Vector calcGradient  (const Vector& y0, Method=UnspecifiedMethod) const;
00096     Matrix calcJacobian  (const Vector& y0, Method=UnspecifiedMethod) const;
00097 
00098     // Statistics (mutable)
00099     void resetAllStatistics();                // reset all stats to zero
00100     long getNDifferentiations() const;        // total # calls of calcWhatever
00101     long getNDifferentiationFailures() const; // # of those that failed
00102     long getNCallsToUserFunction() const;     // total # calls to user function
00103 
00104     // This is a local class.
00105     class DifferentiatorRep;
00106 private:
00107     // opaque implementation for binary compatibility
00108     DifferentiatorRep* rep;
00109 };
00110 
00124 class SimTK_SIMMATH_EXPORT Differentiator::Function {
00125 public:
00126     Function& setNFunctions(int);
00127     Function& setNParameters(int);
00128     Function& setEstimatedAccuracy(Real);
00129 
00130     // These values are fixed after construction.
00131     int  getNFunctions()  const;
00132     int  getNParameters() const;
00133     Real getEstimatedAccuracy() const; // approx. "roundoff" in f calculation
00134 
00135     // Statistics (mutable)
00136     void resetAllStatistics();
00137     long getNCalls()    const; // # evaluations of this function since reset
00138     long getNFailures() const; // # of calls which failed
00139 
00140     // This is the declaration of a local class name.
00141     class FunctionRep;
00142 protected:
00143     Function();
00144     ~Function();
00145 
00146     // opaque implementation for binary compatibility
00147     FunctionRep* rep;
00148 
00149 private:
00150     // suppress copy constructor and copy assignment
00151     Function(const Function&);
00152     Function& operator=(const Function&);
00153 
00154 friend class Differentiator;
00155 };
00156 
00161 class SimTK_SIMMATH_EXPORT Differentiator::ScalarFunction : public Differentiator::Function {
00162 public:
00163     virtual int f(Real x, Real& fx) const=0;
00164 
00165 protected:
00166     explicit ScalarFunction(Real acc=-1);
00167     virtual ~ScalarFunction() { }
00168 
00169 private:
00170     // suppress copy constructor and copy assignment
00171     ScalarFunction(const Function&);
00172     ScalarFunction& operator=(const Function&);
00173 };
00174 
00180 class SimTK_SIMMATH_EXPORT Differentiator::GradientFunction : public Differentiator::Function {
00181 public:
00182     virtual int f(const Vector& y, Real& fy) const=0;
00183 
00184 protected:
00185     explicit GradientFunction(int ny=-1, Real acc=-1);
00186     virtual ~GradientFunction() { }
00187 
00188 private:
00189     // suppress copy constructor and copy assignment
00190     GradientFunction(const GradientFunction&);
00191     GradientFunction& operator=(const GradientFunction&);
00192 };
00193 
00199 class SimTK_SIMMATH_EXPORT Differentiator::JacobianFunction : public Differentiator::Function {
00200 public:
00201     virtual int f(const Vector& y, Vector& fy) const=0;
00202 
00203 protected:
00204     explicit JacobianFunction(int nf=-1, int ny=-1, Real acc=-1); 
00205     virtual ~JacobianFunction() { }
00206 
00207 private:
00208     // suppress copy constructor and copy assignment
00209     JacobianFunction(const JacobianFunction&);
00210     JacobianFunction& operator=(const JacobianFunction&);
00211 };
00212 
00213 } // namespace SimTK
00214 
00215 #endif // SimTK_DIFFERENTIATOR_H_

Generated on Wed Dec 30 11:04:33 2009 for SimTKcore by  doxygen 1.6.1