mm_funct.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 #ifndef O2SCL_MM_FUNCT_H
24 #define O2SCL_MM_FUNCT_H
25 
26 /** \file mm_funct.h
27  \brief Function object classes for multi-dimensional functions
28 */
29 
30 #include <string>
31 
32 #include <boost/numeric/ublas/vector.hpp>
33 
34 #include <o2scl/shunting_yard.h>
35 
36 #ifndef DOXYGEN_NO_O2NS
37 namespace o2scl {
38 #endif
39 
40  /// Array of multi-dimensional functions typedef
41  typedef std::function<
42  int(size_t,const boost::numeric::ublas::vector<double> &,
44 
45  /** \brief Array of multi-dimensional functions in an array of strings
46  */
48 
49  public:
50 
51  /** \brief Specify the strings
52  */
53  template<class vec_string_t=std::vector<std::string> >
54  mm_funct11_strings(int nv, vec_string_t &exprs,
55  vec_string_t &var_arr) {
56 
57  st_nv=nv;
58  st_forms.resize(nv);
59  st_vars.resize(nv);
60  calc.resize(nv);
61  for (int i=0;i<nv;i++) {
62  calc[i].compile(exprs[i],&vars);
63  st_vars[i]=var_arr[i];
64  st_forms[i]=exprs[i];
65  }
66  }
67 
68  virtual ~mm_funct11_strings() {
69  };
70 
71  /** \brief Set the values of the auxilliary parameters that were
72  specified in 'parms' in the constructor
73  */
74  int set_parm(std::string name, double val) {
75  vars[name]=val;
76  return 0;
77  }
78 
79  /** \brief Compute \c nv functions, \c y, of \c nv variables
80  stored in \c x with parameter \c pa.
81  */
82  template<class vec_t=boost::numeric::ublas::vector<double> >
83  int operator()(size_t nv, const vec_t &x, vec_t &y) {
84 
85  for(int i=0;i<nv;i++) {
86  vars[st_vars[i]]=x[i];
87  }
88  for(int i=0;i<nv;i++) {
89  y[i]=calc[i].eval(&vars);
90  }
91  return 0;
92  }
93 
94  /// Set the functions
95  template<class vec_string_t=std::vector<std::string> >
96  void set_function(int nv, vec_string_t &exprs,
97  vec_string_t &var_arr) {
98 
99  st_nv=nv;
100  st_forms.resize(nv);
101  st_vars.resize(nv);
102  calc.resize(nv);
103  for (int i=0;i<nv;i++) {
104  calc[i].compile(exprs[i],&vars);
105  st_vars[i]=var_arr[i];
106  st_forms[i]=exprs[i];
107  }
108 
109  return;
110  }
111 
112 #ifndef DOXYGEN_INTERNAL
113 
114  protected:
115 
116  /// The function parsers
117  std::vector<calculator> calc;
118 
119  /// External variables to include in the function parsing
120  std::map<std::string,double> vars;
121 
122  /// The expressions
123  std::vector<std::string> st_forms;
124 
125  /// The variables
126  std::vector<std::string> st_vars;
127 
128  /// The number of variables
129  int st_nv;
130 
131  mm_funct11_strings() {};
132 
133  private:
134 
136  mm_funct11_strings& operator=(const mm_funct11_strings&);
137 
138 #endif
139 
140  };
141 
142 #ifdef O2SCL_NEVER_DEFINED
143  /** \brief A wrapper to specify \ref o2scl::mm_funct11-like objects
144  to GSL
145  */
146  template<class vec_t>
147  class mm_funct_gsl : public gsl_multiroot_function {
148 
149  public:
150 
151  typedef std::function<int(size_t,const vec_t &, vec_t &)> func_t;
152 
153  protected:
154 
155  /// The function wrapper
156  static int funct_wrap(const gsl_vector *x, void *params,
157  gsl_vector *f) {
158  func_t *fp=(func_t *)params;
159  vec_t x2(x->size), f2(x->size);
160  o2scl::vector_copy<double *,vec_t>(x->size,x.data,x2);
161  int ret=(*fp)(x->size,x2,f2);
162  o2scl::vector_copy<vec_t,double *>(x->size,f2,f.data);
163  return ret;
164  }
165 
166  public:
167 
168  /// Create an object based on the specified function, \c f
169  funct_gsl(func_t &f) {
170  function=&funct_wrap;
171  params=&f;
172  }
173 
174  };
175 #endif
176 
177 #ifndef DOXYGEN_NO_O2NS
178 }
179 #endif
180 
181 #endif
int operator()(size_t nv, const vec_t &x, vec_t &y)
Compute nv functions, y, of nv variables stored in x with parameter pa.
Definition: mm_funct.h:83
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
mm_funct11_strings(int nv, vec_string_t &exprs, vec_string_t &var_arr)
Specify the strings.
Definition: mm_funct.h:54
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
int set_parm(std::string name, double val)
Set the values of the auxilliary parameters that were specified in &#39;parms&#39; in the constructor...
Definition: mm_funct.h:74
std::vector< std::string > st_forms
The expressions.
Definition: mm_funct.h:123
A wrapper to specify o2scl::funct11 objects to GSL.
Definition: funct.h:134
std::vector< std::string > st_vars
The variables.
Definition: mm_funct.h:126
std::vector< calculator > calc
The function parsers.
Definition: mm_funct.h:117
std::map< std::string, double > vars
External variables to include in the function parsing.
Definition: mm_funct.h:120
Array of multi-dimensional functions in an array of strings.
Definition: mm_funct.h:47
void set_function(int nv, vec_string_t &exprs, vec_string_t &var_arr)
Set the functions.
Definition: mm_funct.h:96
int st_nv
The number of variables.
Definition: mm_funct.h:129
static const double x2[5]
Definition: inte_qng_gsl.h:66

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