All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
jacobian.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2014, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_JACOBIAN_H
24 #define O2SCL_JACOBIAN_H
25 
26 /** \file jacobian.h
27  \brief File for Jacobian evaluation and function classes
28 */
29 
30 #include <string>
31 #include <o2scl/mm_funct.h>
32 #include <o2scl/deriv_gsl.h>
33 #include <o2scl/columnify.h>
34 #include <o2scl/vector.h>
35 
36 #ifndef DOXYGEN_NO_O2NS
37 namespace o2scl {
38 #endif
39 
40  /// Jacobian function (not necessarily square)
41  typedef std::function<
45 
46  /** \brief Base for providing a numerical jacobian [abstract base]
47 
48  This is provides a Jacobian which is numerically determined
49  by differentiating a user-specified function (typically
50  of the form of \ref mm_funct11).
51 
52  By convention, the Jacobian is stored in the order
53  <tt>J[i][j]</tt> (or <tt>J(i,j)</tt>) where the rows have index
54  \c i which runs from 0 to <tt>ny-1</tt> and the columns have
55  index \c j with runs from 0 to <tt>nx-1</tt>.
56 
57  Default template arguments
58  - \c func_t - \ref mm_funct11
59  - \c vec_t - boost::numeric::ublas::vector<double>
60  - \c mat_t - boost::numeric::ublas::matrix<double>
61  */
62  template<class func_t=mm_funct11,
65 
66  public:
67 
68  jacobian() {
69  };
70 
71  virtual ~jacobian() {};
72 
73  /// Set the function to compute the Jacobian of
74  virtual int set_function(func_t &f) {
75  func=f;
76  return 0;
77  }
78 
79  /** \brief Evaluate the Jacobian \c j at point \c y(x)
80  */
81  virtual int operator()(size_t nx, vec_t &x, size_t ny, vec_t &y,
82  mat_t &j)=0;
83 
84 #ifndef DOXYGEN_INTERNAL
85 
86  protected:
87 
88  /// A pointer to the user-specified function
89  func_t func;
90 
91  private:
92 
93  jacobian(const jacobian &);
94  jacobian& operator=(const jacobian&);
95 
96 #endif
97 
98  };
99 
100  /** \brief Simple automatic Jacobian
101 
102  This class computes a numerical Jacobian by finite differencing.
103  The stepsize is chosen to be \f$ h_j = \mathrm{epsrel}~x_j \f$ or
104  \f$ h_j = \mathrm{epsmin} \f$ if \f$ \mathrm{epsrel}\times x_j <
105  \mathrm{epsmin} \f$.
106 
107  This is nearly equivalent to the GSL method for computing
108  Jacobians as in \c multiroots/fdjac.c. To obtain the GSL
109  behavior, set \ref epsrel to \c GSL_SQRT_DBL_EPSILON and set
110  \ref epsmin to zero. The \ref mroot_hybrids and \ref
111  chi_fit_funct classes set \ref epsrel to \c GSL_SQRT_DBL_EPSILON
112  in their constructor in order to partially mimic the GSL
113  behavior, but do not set \ref epsmin to zero.
114 
115  This class does not separately check the vector and matrix sizes
116  to ensure they are commensurate.
117 
118  Default template arguments
119  - \c func_t - \ref mm_funct11
120  - \c vec_t - boost::numeric::ublas::vector<double>
121  - \c mat_t - boost::numeric::ublas::matrix<double>
122  */
123  template<class func_t=mm_funct11,
126  class jacobian_gsl : public jacobian<func_t,vec_t,mat_t> {
127 
128 #ifndef DOXYGEN_INTERNAL
129 
130  protected:
131 
132  /// Function values
133  vec_t f;
134 
135  /// Function arguments
136  vec_t xx;
137 
138  /// Size of allocated memory in x
139  size_t mem_size_x;
140 
141  /// Size of allocated memory in y
142  size_t mem_size_y;
143 
144 #endif
145 
146  public:
147 
148  jacobian_gsl() {
149  epsrel=1.0e-4;
150  epsmin=1.0e-15;
151  err_nonconv=true;
152  mem_size_x=0;
153  mem_size_y=0;
154  }
155 
156  virtual ~jacobian_gsl() {
157  }
158 
159  /** \brief The relative stepsize for finite-differencing
160  (default \f$ 10^{-4} \f$ )
161  */
162  double epsrel;
163 
164  /// The minimum stepsize (default \f$ 10^{-15} \f$)
165  double epsmin;
166 
167  /// If true, call the error handler if the routine does not "converge"
169 
170  /** \brief The operator()
171  */
172  virtual int operator()(size_t nx, vec_t &x, size_t ny, vec_t &y,
173  mat_t &jac) {
174 
175  size_t i,j;
176  double h,temp;
177  bool success=true;
178 
179  if (mem_size_x!=nx || mem_size_y!=ny) {
180  f.resize(ny);
181  xx.resize(nx);
182  mem_size_x=nx;
183  mem_size_y=ny;
184  }
185 
186  vector_copy(nx,x,xx);
187 
188  for (j=0;j<nx;j++) {
189 
190  h=epsrel*fabs(x[j]);
191  if (fabs(h)<=epsmin) h=epsrel;
192 
193  xx[j]=x[j]+h;
194  (this->func)(nx,xx,f);
195  xx[j]=x[j];
196 
197  // This is the equivalent of GSL's test of
198  // gsl_vector_isnull(&col.vector)
199 
200  bool nonzero=false;
201  for (i=0;i<ny;i++) {
202  temp=(f[i]-y[i])/h;
203  if (temp!=0.0) nonzero=true;
204  jac(i,j)=temp;
205  }
206  if (nonzero==false) success=false;
207 
208 
209  }
210 
211  if (success==false) {
212  O2SCL_CONV2_RET("At least one row of the Jacobian is zero ",
213  "in jacobian_gsl::operator().",exc_esing,
214  this->err_nonconv);
215  }
216  return 0;
217  }
218 
219  };
220 
221  /** \brief A direct calculation of the jacobian using a \ref
222  deriv_base object
223 
224  Note that it is most often wasteful to use this Jacobian in a
225  root-finding routine and using more approximate Jacobians is
226  more efficient. This class is mostly useful for demonstration
227  and testing purposes.
228 
229  By default, the stepsize, \ref deriv_gsl::h is set to \f$
230  10^{-4} \f$ in the \ref jacobian_exact constructor.
231 
232  Default template arguments
233  - \c func_t - \ref mm_funct11
234  - \c vec_t - boost::numeric::ublas::vector<double>
235  - \c mat_t - boost::numeric::ublas::matrix<double>
236  */
237  template<class func_t=mm_funct11,
240  public jacobian<func_t,vec_t,mat_t> {
241 
242  public:
243 
244  jacobian_exact() {
245  def_deriv.h=1.0e-4;
246  dptr=&def_deriv;
247  }
248 
249  /** \brief Parameter structure for passing information
250 
251  This class is primarily useful for specifying derivatives
252  for using the jacobian::set_deriv() function.
253 
254  \comment
255  This type needs to be publicly available so that the
256  user can properly specify a base 1-dimensional derivative
257  object.
258  \endcomment
259  */
260  typedef struct {
261  /// The number of variables
262  size_t nx;
263  /// The number of variables
264  size_t ny;
265  /// The current x value
266  size_t xj;
267  /// The current y value
268  size_t yi;
269  /// The x vector
270  vec_t *x;
271  /// The y vector
272  vec_t *y;
273  } ej_parms;
274 
275  /// The default derivative object
277 
278  /// Set the derivative object
280  dptr=&de;
281  return 0;
282  }
283 
284  /** \brief The operator()
285  */
286  virtual int operator()(size_t nx, vec_t &x, size_t ny, vec_t &y,
287  mat_t &jac) {
288 
289  double h,temp;
290 
291  ej_parms ejp;
292  ejp.nx=nx;
293  ejp.ny=ny;
294  ejp.x=&x;
295  ejp.y=&y;
296 
297  funct11 dfnp=std::bind(std::mem_fn<double(double,ej_parms &)>
299  this,std::placeholders::_1,std::ref(ejp));
300 
301  for (size_t j=0;j<nx;j++) {
302  ejp.xj=j;
303  for (size_t i=0;i<ny;i++) {
304  ejp.yi=i;
305  double tmp=(*ejp.x)[j];
306  jac(i,j)=dptr->deriv(tmp,dfnp);
307  (*ejp.x)[j]=tmp;
308  }
309  }
310 
311  return 0;
312  }
313 
314 #ifndef DOXYGEN_INTERNAL
315 
316  protected:
317 
318  /// Pointer to the derivative object
320 
321  /// Function for the derivative object
322  double dfn(double x, ej_parms &ejp) {
323  (*ejp.x)[ejp.xj]=x;
324  (this->func)(ejp.nx,*ejp.x,*ejp.y);
325  return (*ejp.y)[ejp.yi];
326  }
327 
328 #endif
329 
330  };
331 
332 #ifndef DOXYGEN_NO_O2NS
333 }
334 #endif
335 
336 #endif
vec_t f
Function values.
Definition: jacobian.h:133
virtual int operator()(size_t nx, vec_t &x, size_t ny, vec_t &y, mat_t &j)=0
Evaluate the Jacobian j at point y(x)
virtual int set_function(func_t &f)
Set the function to compute the Jacobian of.
Definition: jacobian.h:74
size_t xj
The current x value.
Definition: jacobian.h:266
vec_t * y
The y vector.
Definition: jacobian.h:272
std::function< double(double)> funct11
One-dimensional function typedef.
Definition: funct.h:44
virtual int operator()(size_t nx, vec_t &x, size_t ny, vec_t &y, mat_t &jac)
The operator()
Definition: jacobian.h:286
std::function< int(size_t, const boost::numeric::ublas::vector< double > &, boost::numeric::ublas::vector< double > &) > mm_funct11
Array of multi-dimensional functions typedef.
Definition: mm_funct.h:43
apparent singularity detected
Definition: err_hnd.h:93
virtual double deriv(double x, func_t &func)
Calculate the first derivative of func w.r.t. x.
Definition: deriv.h:96
#define O2SCL_CONV2_RET(d, d2, n, b)
Set an error and return the error value, two-string version.
Definition: err_hnd.h:298
size_t mem_size_x
Size of allocated memory in x.
Definition: jacobian.h:139
size_t yi
The current y value.
Definition: jacobian.h:268
double h
Initial stepsize.
Definition: deriv_gsl.h:128
double epsrel
The relative stepsize for finite-differencing (default )
Definition: jacobian.h:162
double dfn(double x, ej_parms &ejp)
Function for the derivative object.
Definition: jacobian.h:322
virtual int operator()(size_t nx, vec_t &x, size_t ny, vec_t &y, mat_t &jac)
The operator()
Definition: jacobian.h:172
Numerical differentiation base [abstract base].
Definition: deriv.h:55
bool err_nonconv
If true, call the error handler if the routine does not "converge".
Definition: jacobian.h:168
size_t ny
The number of variables.
Definition: jacobian.h:264
std::function< int(size_t, boost::numeric::ublas::vector< double > &, size_t, boost::numeric::ublas::vector< double > &, boost::numeric::ublas::matrix< double > &) > jac_funct11
Jacobian function (not necessarily square)
Definition: jacobian.h:44
vec_t xx
Function arguments.
Definition: jacobian.h:136
deriv_base * dptr
Pointer to the derivative object.
Definition: jacobian.h:319
Numerical differentiation (GSL)
Definition: deriv_gsl.h:110
int set_deriv(deriv_base<> &de)
Set the derivative object.
Definition: jacobian.h:279
func_t func
A pointer to the user-specified function.
Definition: jacobian.h:89
size_t nx
The number of variables.
Definition: jacobian.h:262
double epsmin
The minimum stepsize (default )
Definition: jacobian.h:165
void vector_copy(vec_t &src, vec2_t &dest)
Simple generic vector copy.
Definition: vector.h:73
deriv_gsl def_deriv
The default derivative object.
Definition: jacobian.h:276
size_t mem_size_y
Size of allocated memory in y.
Definition: jacobian.h:142
vec_t * x
The x vector.
Definition: jacobian.h:270
Parameter structure for passing information.
Definition: jacobian.h:260
Success.
Definition: err_hnd.h:47
Simple automatic Jacobian.
Definition: jacobian.h:126
Base for providing a numerical jacobian [abstract base].
Definition: jacobian.h:64
A direct calculation of the jacobian using a deriv_base object.
Definition: jacobian.h:239

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).
Hosted at Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads..