mroot.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2017, 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 /** \file mroot.h
24  \brief File for multi-dimensional solver base class
25 */
26 #ifndef O2SCL_MROOT_H
27 #define O2SCL_MROOT_H
28 
29 #include <o2scl/mm_funct.h>
30 #include <o2scl/jacobian.h>
31 
32 namespace o2scl {
33 
34  /** \brief Multidimensional root-finding [abstract base]
35 
36  <b>The template parameters:</b>
37  The template parameter \c func_t specifies the functions to
38  solve and should be a class containing a definition
39  \code
40  func_t::operator()(size_t nv, const vec_t &x, vec_t &y);
41  \endcode
42  where \c y is the value of the functions at \c x with parameter \c pa
43  and \c x and \c y are a array-like classes defining \c operator[]
44  of size \c nv. If the Jacobian matrix is to be specified by the
45  user, then the parameter \c jfunc_t specifies the jacobian
46  and should contain the definition
47  \code
48  jfunc_t::operator(size_t nv, vec_t &x, vec_t &y, mat_t &j);
49  \endcode
50  where \c x is the independent variables, \c y is the array of
51  function values, and \c j is the Jacobian matrix. This template
52  parameter can be ignored if only the function msolve() will be used.
53 
54  \warning Many of the routines assume that the scale of the
55  functions and their variables is of order unity. The solution
56  routines may lose precision if this is not the case.
57 
58  There is an example for the usage of the multidimensional solver
59  classes given in <tt>examples/ex_mroot.cpp</tt>, see \ref
60  ex_mroot_sect .
61 
62  \future Change ntrial to size_t?
63  */
64  template<class func_t=mm_funct11,
66  class jfunc_t=jac_funct11 > class mroot {
67 
68  public:
69 
70  mroot() {
71  ntrial=100;
72  tol_rel=1.0e-8;
73  verbose=0;
74  tol_abs=1.0e-12;
75  last_ntrial=0;
76  err_nonconv=true;
77  }
78 
79  virtual ~mroot() {}
80 
81  /// The maximum value of the functions for success (default 1.0e-8)
82  double tol_rel;
83 
84  /// The minimum allowable stepsize (default 1.0e-12)
85  double tol_abs;
86 
87  /// Output control (default 0)
88  int verbose;
89 
90  /// Maximum number of iterations (default 100)
91  int ntrial;
92 
93  /// The number of iterations for in the most recent minimization
95 
96  /** \brief If true, call the error handler if msolve() or
97  msolve_de() does not converge (default true)
98  */
100 
101  /// Return the type, \c "mroot".
102  virtual const char *type() { return "mroot"; }
103 
104  /// Solve \c func using \c x as an initial guess, returning \c x.
105  virtual int msolve(size_t n, vec_t &x, func_t &func)=0;
106 
107  /** \brief Solve \c func with derivatives \c dfunc using \c x as
108  an initial guess, returning \c x.
109 
110  By default, this function just calls msolve() and ignores the
111  last argument.
112  */
113  virtual int msolve_de(size_t n, vec_t &x, func_t &func,
114  jfunc_t &dfunc) {
115  return msolve(n,x,func);
116  }
117 
118  /** \brief Print out iteration information.
119 
120  Depending on the value of the variable verbose, this prints out
121  the iteration information. If verbose=0, then no information is
122  printed, while if verbose>1, then after each iteration, the
123  present values of x and y are output to std::cout along with the
124  iteration number. If verbose>=2 then each iteration waits for a
125  character.
126 
127  This is implemented as a template class using a new vector
128  type because sometimes the internal vector class is distinct
129  from the user-specified vector class (e.g. in \ref
130  o2scl::mroot_hybrids).
131  */
132  template<class vec2_t, class vec3_t>
133  int print_iter(size_t n, const vec2_t &x, const vec3_t &y,
134  int iter, double value=0.0, double limit=0.0,
135  std::string comment="")
136  {
137  if (verbose<=0) return o2scl::success;
138 
139  char ch;
140 
141  std::cout << comment << " Iteration: " << iter << std::endl;
142  for(size_t i=0;i<n;i++) {
143  std::cout.width(3);
144  std::cout << i;
145  if (x[i]>=0.0) {
146  std::cout << " " << x[i];
147  } else {
148  std::cout << " " << x[i];
149  }
150  if (y[i]>=0.0) {
151  std::cout << " " << y[i] << std::endl;
152  } else {
153  std::cout << " " << y[i] << std::endl;
154  }
155  }
156  std::cout << "Val: " << value << " Lim: " << limit << std::endl;
157  if (verbose>1) {
158  std::cout << "Press a key and type enter to continue. ";
159  std::cin >> ch;
160  }
161 
162  return o2scl::success;
163 
164  }
165 
166  };
167 
168 }
169 
170 #endif
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
int verbose
Output control (default 0)
Definition: mroot.h:88
virtual int msolve(size_t n, vec_t &x, func_t &func)=0
Solve func using x as an initial guess, returning x.
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
Multidimensional root-finding [abstract base].
Definition: mroot.h:66
int print_iter(size_t n, const vec2_t &x, const vec3_t &y, int iter, double value=0.0, double limit=0.0, std::string comment="")
Print out iteration information.
Definition: mroot.h:133
int ntrial
Maximum number of iterations (default 100)
Definition: mroot.h:91
bool err_nonconv
If true, call the error handler if msolve() or msolve_de() does not converge (default true) ...
Definition: mroot.h:99
int last_ntrial
The number of iterations for in the most recent minimization.
Definition: mroot.h:94
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
virtual int msolve_de(size_t n, vec_t &x, func_t &func, jfunc_t &dfunc)
Solve func with derivatives dfunc using x as an initial guess, returning x.
Definition: mroot.h:113
double tol_abs
The minimum allowable stepsize (default 1.0e-12)
Definition: mroot.h:85
virtual const char * type()
Return the type, "mroot".
Definition: mroot.h:102
double tol_rel
The maximum value of the functions for success (default 1.0e-8)
Definition: mroot.h:82
Success.
Definition: err_hnd.h:47

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).