All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mm_funct.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_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/fparser.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  */
47  template<class vec_t=boost::numeric::ublas::vector<double> >
49  public:
50 
51  /** \brief Specify the strings
52  */
53  mm_funct11_strings(int nv, std::string *formulas, std::string vars,
54  int np=0, std::string parms="") {
55  int i;
56  fpw=new FunctionParser[nv];
57  if(np<1) {
58  for(i=0;i<nv;i++) {
59  fpw[i].Parse(formulas[i],vars);
60  }
61  st_np=0;
62  st_parms="";
63  } else {
64  std::string all=vars+","+parms;
65  for(i=0;i<nv;i++) {
66  fpw[i].Parse(formulas[i],all);
67  }
68  st_np=np;
69  st_parms=parms;
70  arr=new double[np];
71  }
72  st_forms=formulas;
73  st_vars=vars;
74  st_nv=nv;
75  }
76 
77  virtual ~mm_funct11_strings() {
78  if (st_np>0) {
79  delete[] arr;
80  }
81  delete[] fpw;
82  };
83 
84  /** \brief Set the values of the auxilliary parameters that were
85  specified in 'parms' in the constructor
86  */
87  int set_parms(const vec_t &p) {
88  for(int i=0;i<st_np;i++) {
89  arr[i]=p[i];
90  }
91  return 0;
92  }
93 
94 
95  /** \brief Compute \c nv functions, \c y, of \c nv variables
96  stored in \c x with parameter \c pa.
97  */
98  virtual int operator()(size_t nv, const vec_t &x, vec_t &y) {
99  int i;
100  if(st_np<1) {
101  for(i=0;i<st_nv;i++) {
102  y[i]=fpw[i].Eval(x);
103  }
104  } else {
105  double *all=new double[st_np+st_nv];
106  for(i=0;i<st_nv;i++) all[i]=x[i];
107  for(i=st_nv;i<st_np+st_nv;i++) {
108  all[i]=arr[i-st_nv];
109  }
110  for(i=0;i<st_nv;i++) {
111  y[i]=fpw[i].Eval(all);
112  }
113  delete[] all;
114  }
115  return 0;
116  }
117 
118  /// Set the functions
119  int set_function(int nv, std::string *formulas, std::string vars,
120  int np=0, std::string parms="") {
121  int i;
122  if (nv!=st_nv) {
123  delete[] fpw;
124  fpw=new FunctionParser[nv];
125  }
126  if(np<1) {
127  for(i=0;i<nv;i++) {
128  fpw[i].Parse(formulas[i],vars);
129  }
130  st_np=0;
131  st_parms="";
132  } else {
133  std::string all=vars+","+parms;
134  for(i=0;i<nv;i++) {
135  fpw[i].Parse(formulas[i],all);
136  }
137  st_np=np;
138  st_parms=parms;
139  arr=new double[np+nv];
140  }
141  st_forms=formulas;
142  st_vars=vars;
143  st_nv=nv;
144  return 0;
145  }
146 
147 #ifndef DOXYGEN_INTERNAL
148 
149  protected:
150 
151  /// The function parser
153 
154  /// The number of parameters
155  int st_np;
156 
157  /// The number of variables
158  int st_nv;
159 
160  /// The arguments to the function parser
161  double *arr;
162 
163  /// The formulas
164  std::string *st_forms;
165 
166  /// The variables
167  std::string st_vars;
168 
169  /// The parameters
170  std::string st_parms;
171 
172  mm_funct11_strings() {};
173 
174  private:
175 
177  mm_funct11_strings& operator=(const mm_funct11_strings&);
178 
179 #endif
180 
181  };
182 
183 #ifdef O2SCL_NEVER_DEFINED
184  /** \brief A wrapper to specify \ref o2scl::mm_funct11-like objects
185  to GSL
186  */
187  template<class vec_t>
188  class mm_funct_gsl : public gsl_multiroot_function {
189 
190  public:
191 
192  typedef std::function<int(size_t,const vec_t &, vec_t &)> func_t;
193 
194  protected:
195 
196  /// The function wrapper
197  static int funct_wrap(const gsl_vector *x, void *params,
198  gsl_vector *f) {
199  func_t *fp=(func_t *)params;
200  vec_t x2(x->size), f2(x->size);
201  o2scl::vector_copy<double *,vec_t>(x->size,x.data,x2);
202  int ret=(*fp)(x->size,x2,f2);
203  o2scl::vector_copy<vec_t,double *>(x->size,f2,f.data);
204  return ret;
205  }
206 
207  public:
208 
209  /// Create an object based on the specified function, \c f
210  funct_gsl(func_t &f) {
211  function=&funct_wrap;
212  params=&f;
213  }
214 
215  };
216 #endif
217 
218 #ifndef DOXYGEN_NO_O2NS
219 }
220 #endif
221 
222 #endif
Parse a mathematical function specified in a string.
Definition: fparser.h:29
virtual 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:98
FunctionParser * fpw
The function parser.
Definition: mm_funct.h:152
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
std::string st_parms
The parameters.
Definition: mm_funct.h:170
int set_parms(const vec_t &p)
Set the values of the auxilliary parameters that were specified in 'parms' in the constructor...
Definition: mm_funct.h:87
double * arr
The arguments to the function parser.
Definition: mm_funct.h:161
mm_funct11_strings(int nv, std::string *formulas, std::string vars, int np=0, std::string parms="")
Specify the strings.
Definition: mm_funct.h:53
int set_function(int nv, std::string *formulas, std::string vars, int np=0, std::string parms="")
Set the functions.
Definition: mm_funct.h:119
Array of multi-dimensional functions in an array of strings.
Definition: mm_funct.h:48
int st_np
The number of parameters.
Definition: mm_funct.h:155
std::string * st_forms
The formulas.
Definition: mm_funct.h:164
int st_nv
The number of variables.
Definition: mm_funct.h:158
static const double x2[5]
Definition: inte_qng_gsl.h:66
std::string st_vars
The variables.
Definition: mm_funct.h:167

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..