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_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 
39 #ifndef DOXYGEN_NO_O2NS
40 namespace o2scl {
41 #endif
42 
43  /// One-dimensional function typedef
44  typedef std::function<double(double)> funct11;
45 
46  /** \brief One-dimensional function from a string
47 
48  For example,
49  \code
50  funct11_string f("pi*r^2","r",1,"pi");
51  ubvector par(1);
52  par[0]=o2scl_const::pi;
53  f.set_parms(par);
54  for(double r=1.0;r<=2.0;r+=0.1) {
55  cout << f(x) << endl;
56  }
57  \endcode
58  will print out the area of circles having radii between 1 and 2.
59  */
61 
62  public:
63 
65 
66  /** \brief Specify the string and the parameters
67  */
68  funct11_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 ~funct11_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  vars[name]=val;
92  return 0;
93  }
94 
95  /** \brief Compute the function at point \c x and return the result
96  */
97  virtual double operator()(double x) const {
98  vars[st_var]=x;
99  return calc.eval(&vars);
100  }
101 
102 #ifndef DOXYGEN_INTERNAL
103 
104  protected:
105 
106  /// The object for evaluating strings
107  mutable calculator calc;
108 
109  /// Parameter map
110  mutable std::map<std::string,double> vars;
111 
112  /// The expr
113  std::string st_form;
114  /// The variable
115  std::string st_var;
116 
117  funct11_string() {};
118 
119 #endif
120 #ifndef DOXYGEN_NO_O2NS
121 
122  private:
123 
125  funct11_string& operator=(const funct11_string&);
126 
127 #endif
128 
129  };
130 
131  /** \brief A wrapper to specify \ref o2scl::funct11 objects
132  to GSL
133  */
134  class funct_gsl : public gsl_function {
135 
136  protected:
137 
138  /// The function wrapper
139  static double funct_wrap(double x, void *params) {
140  funct11 *fp=(funct11 *)params;
141  return (*fp)(x);
142  }
143 
144  public:
145 
146  /// Create an object based on the specified function, \c f
147  funct_gsl(funct11 &f) {
148  function=&funct_wrap;
149  params=&f;
150  }
151 
152  };
153 
154 
155 #ifndef DOXYGEN_NO_O2NS
156 }
157 #endif
158 
159 #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
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.
std::function< double(double)> funct11
One-dimensional function typedef.
Definition: funct.h:44
static double funct_wrap(double x, void *params)
The function wrapper.
Definition: funct.h:139
funct11_string(std::string expr, std::string var)
Specify the string and the parameters.
Definition: funct.h:68
A wrapper to specify o2scl::funct11 objects to GSL.
Definition: funct.h:134
std::string st_var
The variable.
Definition: funct.h:115
std::string st_form
The expr.
Definition: funct.h:113
std::map< std::string, double > vars
Parameter map.
Definition: funct.h:110
funct_gsl(funct11 &f)
Create an object based on the specified function, f.
Definition: funct.h:147
calculator calc
The object for evaluating strings.
Definition: funct.h:107
int set_function(std::string expr, std::string var)
Specify the string and the parameters.
Definition: funct.h:80
double eval(std::map< std::string, double > *vars=0)
Evalate the previously compiled expression using variables specified in vars.
Evaluate a mathematical expression in a string.
virtual double operator()(double x) const
Compute the function at point x and return the result.
Definition: funct.h:97
One-dimensional function from a string.
Definition: funct.h:60
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

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