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

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