funct.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2018, 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_FUNCT_H
24 #define O2SCL_FUNCT_H
25 
26 /** \file funct.h
27  \brief Function object classes for one-dimensional functions
28 */
29 
30 #include <string>
31 #include <functional>
32 
33 #include <gsl/gsl_math.h>
34 
35 #include <boost/numeric/ublas/vector.hpp>
36 
37 #include <o2scl/shunting_yard.h>
38 #include <o2scl/err_hnd.h>
39 
40 #ifndef DOXYGEN_NO_O2NS
41 namespace o2scl {
42 #endif
43 
44  /// One-dimensional function typedef
45  typedef std::function<double(double)> funct;
46 
47  /// One-dimensional function typedef
48  typedef std::function<long double(long double)> funct_ld;
49 
50  /** \brief One-dimensional function from a string
51 
52  For example,
53  \code
54  funct_string f("pi*r^2","r");
55  f.set_parm("pi",o2scl_const::pi);
56  for(double r=1.0;r<=2.0;r+=0.1) {
57  cout << f(x) << endl;
58  }
59  \endcode
60  will print out the area of circles having radii between 1 and 2.
61  */
62  class funct_string {
63 
64  public:
65 
66  /** \brief Specify the string and the parameters
67  */
68  funct_string(std::string expr, std::string var) {
69  calc.compile(expr.c_str(),&vars);
70  st_form=expr;
71  st_var=var;
72  }
73 
74  virtual ~funct_string() {
75  };
76 
77 
78  /** \brief Specify the string and the parameters
79  */
80  int set_function(std::string expr, std::string var) {
81  calc.compile(expr.c_str(),&vars);
82  st_form=expr;
83  st_var=var;
84  return 0;
85  }
86 
87  /** \brief Set the values of the auxilliary parameters that were
88  specified in \c parms in the constructor
89  */
90  int set_parm(std::string name, double val) {
91  if (name==st_var) {
92  O2SCL_ERR2("A parameter cannot have the same name as ",
93  "the variable in funct_string::set_parm().",
95  }
96  vars[name]=val;
97  return 0;
98  }
99 
100  /** \brief Compute the function at point \c x and return the result
101  */
102  virtual double operator()(double x) const {
103  vars[st_var]=x;
104  return calc.eval(&vars);
105  }
106 
107 #ifndef DOXYGEN_INTERNAL
108 
109  protected:
110 
111  /// The object for evaluating strings
112  mutable calculator calc;
113 
114  /// Parameter map
115  mutable std::map<std::string,double> vars;
116 
117  /// The expr
118  std::string st_form;
119  /// The variable
120  std::string st_var;
121 
122  funct_string() {};
123 
124 #endif
125 #ifndef DOXYGEN_NO_O2NS
126 
127  private:
128 
129  funct_string(const funct_string &);
130  funct_string& operator=(const funct_string&);
131 
132 #endif
133 
134  };
135 
136  /** \brief A wrapper to specify \ref o2scl::funct objects
137  to GSL
138  */
139  class funct_gsl : public gsl_function {
140 
141  protected:
142 
143  /// The function wrapper
144  static double funct_wrap(double x, void *params) {
145  funct *fp=(funct *)params;
146  return (*fp)(x);
147  }
148 
149  public:
150 
151  /// Create an object based on the specified function, \c f
152  funct_gsl(funct &f) {
153  function=&funct_wrap;
154  params=&f;
155  }
156 
157  };
158 
159  /** \brief Two-dimensional function from a string
160  */
162 
163  public:
164 
165  /** \brief Specify the string and the parameters
166  */
167  funct2_string(std::string expr, std::string var1, std::string var2) {
168  calc.compile(expr.c_str(),&vars);
169  st_form=expr;
170  st_var1=var1;
171  st_var2=var2;
172  }
173 
174  virtual ~funct2_string() {
175  };
176 
177 
178  /** \brief Specify the string and the parameters
179  */
180  int set_function(std::string expr, std::string var1,
181  std::string var2) {
182  calc.compile(expr.c_str(),&vars);
183  st_form=expr;
184  st_var1=var1;
185  st_var2=var2;
186  return 0;
187  }
188 
189  /** \brief Set the values of the auxilliary parameters that were
190  specified in \c parms in the constructor
191  */
192  int set_parm(std::string name, double val) {
193  if (name==st_var1 || name==st_var2) {
194  O2SCL_ERR2("A parameter cannot have the same name as ",
195  "a variable in funct_string::set_parm().",
197  }
198  vars[name]=val;
199  return 0;
200  }
201 
202  /** \brief Compute the function at point \c x and return the result
203  */
204  virtual double operator()(double x, double y) const {
205  vars[st_var1]=x;
206  vars[st_var2]=y;
207  return calc.eval(&vars);
208  }
209 
210 #ifndef DOXYGEN_INTERNAL
211 
212  protected:
213 
214  /// The object for evaluating strings
215  mutable calculator calc;
216 
217  /// Parameter map
218  mutable std::map<std::string,double> vars;
219 
220  /// The expr
221  std::string st_form;
222  /// The variable
223  std::string st_var1;
224  /// The variable
225  std::string st_var2;
226 
227  funct2_string() {};
228 
229 #endif
230 #ifndef DOXYGEN_NO_O2NS
231 
232  private:
233 
234  funct2_string(const funct2_string &);
235  funct2_string& operator=(const funct2_string&);
236 
237 #endif
238 
239  };
240 
241 
242 
243 #ifndef DOXYGEN_NO_O2NS
244 }
245 #endif
246 
247 #endif
virtual double operator()(double x) const
Compute the function at point x and return the result.
Definition: funct.h:102
calculator calc
The object for evaluating strings.
Definition: funct.h:112
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
funct_gsl(funct &f)
Create an object based on the specified function, f.
Definition: funct.h:152
std::map< std::string, double > vars
Parameter map.
Definition: funct.h:218
void compile(const char *expr, std::map< std::string, double > *vars=0, bool debug=false, std::map< std::string, int > opPrec=opPrecedence)
Compile expression expr using variables specified in vars.
static double funct_wrap(double x, void *params)
The function wrapper.
Definition: funct.h:144
invalid argument supplied by user
Definition: err_hnd.h:59
std::function< long double(long double)> funct_ld
One-dimensional function typedef.
Definition: funct.h:48
One-dimensional function from a string.
Definition: funct.h:62
std::string st_var2
The variable.
Definition: funct.h:225
funct_string(std::string expr, std::string var)
Specify the string and the parameters.
Definition: funct.h:68
virtual double operator()(double x, double y) const
Compute the function at point x and return the result.
Definition: funct.h:204
int set_function(std::string expr, std::string var)
Specify the string and the parameters.
Definition: funct.h:80
A wrapper to specify o2scl::funct objects to GSL.
Definition: funct.h:139
int set_parm(std::string name, double val)
Set the values of the auxilliary parameters that were specified in parms in the constructor.
Definition: funct.h:192
int set_parm(std::string name, double val)
Set the values of the auxilliary parameters that were specified in parms in the constructor.
Definition: funct.h:90
std::string st_var
The variable.
Definition: funct.h:120
#define O2SCL_ERR2(d, d2, n)
Set an error, two-string version.
Definition: err_hnd.h:281
double eval(std::map< std::string, double > *vars=0)
Evalate the previously compiled expression using variables specified in vars.
std::string st_form
The expr.
Definition: funct.h:118
funct2_string(std::string expr, std::string var1, std::string var2)
Specify the string and the parameters.
Definition: funct.h:167
Evaluate a mathematical expression in a string.
std::function< double(double)> funct
One-dimensional function typedef.
Definition: funct.h:45
int set_function(std::string expr, std::string var1, std::string var2)
Specify the string and the parameters.
Definition: funct.h:180
std::string st_var1
The variable.
Definition: funct.h:223
Two-dimensional function from a string.
Definition: funct.h:161
calculator calc
The object for evaluating strings.
Definition: funct.h:215
std::map< std::string, double > vars
Parameter map.
Definition: funct.h:115
std::string st_form
The expr.
Definition: funct.h:221

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