Simbody  3.5
Differentiator.h
Go to the documentation of this file.
1 #ifndef SimTK_DIFFERENTIATOR_H_
2 #define SimTK_DIFFERENTIATOR_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKmath *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2006-12 Stanford University and the Authors. *
13  * Authors: Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
32 #include "SimTKcommon.h"
34 
35 namespace SimTK {
36 
37 
78 public:
79  // This are local classes within Differentiator; defined below.
80  class ScalarFunction; // ordinary scalar function of a scalar
81  class GradientFunction; // scalar function of vector
82  class JacobianFunction; // vector function of vector
83  class Function; // abstraction of the above
84 
85  // These are the exceptions that can be thrown by this class.
86  class OpNotAllowedForFunctionOfThisShape;
87  class UserFunctionThrewAnException;
88  class UserFunctionReturnedNonzeroStatus;
89  class UnknownMethodSpecified;
90 
91 
92  enum Method {
93  UnspecifiedMethod=0,
94  ForwardDifference=1,
95  CentralDifference=2
96  };
97  static bool isValidMethod(Method);
98  static const char* getMethodName(Method);
99  static int getMethodOrder(Method);
100 
101  virtual ~Differentiator();
102  explicit Differentiator(const Function& f,
103  Method defaultMethod=UnspecifiedMethod);
104 
105  // You can change the default method; normally it is ForwardDifference.
106  // If you set it to 'UnspecifiedMethod' it goes back to the original default.
107  Differentiator& setDefaultMethod(Method);
108  Method getDefaultMethod() const;
109 
110  // These are the real routines, which are efficient and flexible
111  // but somewhat messy to use.
112  void calcDerivative(Real y0, Real fy0, Real& dfdy,
113  Method=UnspecifiedMethod) const;
114  void calcGradient (const Vector& y0, Real fy0, Vector& gf,
115  Method=UnspecifiedMethod) const;
116  void calcJacobian (const Vector& y0, const Vector& fy0, Matrix& dfdy,
117  Method=UnspecifiedMethod) const;
118 
119  // These provide a simpler though less efficient interface. They will
120  // do some heap allocation, and will make an initial unperturbed call
121  // to the user function.
122  Real calcDerivative(Real y0, Method=UnspecifiedMethod) const;
123  Vector calcGradient (const Vector& y0, Method=UnspecifiedMethod) const;
124  Matrix calcJacobian (const Vector& y0, Method=UnspecifiedMethod) const;
125 
126  // Statistics (mutable)
127  void resetAllStatistics(); // reset all stats to zero
128  int getNumDifferentiations() const; // total # calls of calcWhatever
129  int getNumDifferentiationFailures() const; // # of those that failed
130  int getNumCallsToUserFunction() const; // total # calls to user function
131 
132  // This is a local class.
133  class DifferentiatorRep;
134 private:
135  // opaque implementation for binary compatibility
136  DifferentiatorRep* rep;
137 };
138 
153 public:
154  Function& setNumFunctions(int);
155  Function& setNumParameters(int);
156  Function& setEstimatedAccuracy(Real);
157 
158  // These values are fixed after construction.
159  int getNumFunctions() const;
160  int getNumParameters() const;
161  Real getEstimatedAccuracy() const; // approx. "roundoff" in f calculation
162 
163  // Statistics (mutable)
164  void resetAllStatistics();
165  int getNumCalls() const; // # evaluations of this function since reset
166  int getNumFailures() const; // # of calls which failed
167 
168  // This is the declaration of a local class name.
169  class FunctionRep;
170 protected:
171  Function();
172  ~Function();
173 
174  // opaque implementation for binary compatibility
175  FunctionRep* rep;
176 
177 private:
178  // suppress copy constructor and copy assignment
179 
180  // This is a workaround for a Visual Studio 2015 bug where
181  // the old-style C++03 deletion method didn't work.
182 #if defined(_MSC_VER) && (_MSC_VER >= 1900)
183  Function(const Function&) = delete;
184  Function& operator=(const Function&) = delete;
185 #else
186  Function(const Function&);
187  Function& operator=(const Function&);
188 #endif
189 
190 friend class Differentiator;
191 };
192 
198 public:
199  virtual int f(Real x, Real& fx) const=0;
200 
201 protected:
202  explicit ScalarFunction(Real acc=-1);
203  virtual ~ScalarFunction() { }
204 
205 private:
206  // suppress copy constructor and copy assignment
207  ScalarFunction(const Function&);
208  ScalarFunction& operator=(const Function&);
209 };
210 
217 public:
218  virtual int f(const Vector& y, Real& fy) const=0;
219 
220 protected:
221  explicit GradientFunction(int ny=-1, Real acc=-1);
222  virtual ~GradientFunction() { }
223 
224 private:
225  // suppress copy constructor and copy assignment
227  GradientFunction& operator=(const GradientFunction&);
228 };
229 
236 public:
237  virtual int f(const Vector& y, Vector& fy) const=0;
238 
239 protected:
240  explicit JacobianFunction(int nf=-1, int ny=-1, Real acc=-1);
241  virtual ~JacobianFunction() { }
242 
243 private:
244  // suppress copy constructor and copy assignment
246  JacobianFunction& operator=(const JacobianFunction&);
247 };
248 
249 } // namespace SimTK
250 
251 #endif // SimTK_DIFFERENTIATOR_H_
FunctionRep * rep
Definition: Differentiator.h:175
Given a function f(y), where f, y or both can be vectors, calculate the derivative (gradient...
Definition: Differentiator.h:77
Derive a concrete class from this one if you have a scalar function of a single scalar variable that ...
Definition: Differentiator.h:197
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
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:593
Includes internal headers providing declarations for the basic SimTK Core classes, including Simmatrix.
This abstract class defines a function to be differentiated (repeatedly) by a Differentiator object...
Definition: Differentiator.h:152
Method
Definition: Differentiator.h:92
virtual ~GradientFunction()
Definition: Differentiator.h:222
This is the header file that every Simmath compilation unit should include first. ...
virtual ~ScalarFunction()
Definition: Differentiator.h:203
Derive a concrete class from this one if you have a scalar function of multiple variables that you wa...
Definition: Differentiator.h:216
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
#define SimTK_SIMMATH_EXPORT
Definition: SimTKmath/include/simmath/internal/common.h:64
Derive a concrete class from this one if you have a set of functions (i.e., a vector-valued function)...
Definition: Differentiator.h:235
virtual ~JacobianFunction()
Definition: Differentiator.h:241