All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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_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/fparser.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 formula, std::string var,
69  int np=0, std::string parms="") {
70  if(np<1) {
71  fpw.Parse(formula,var);
72  st_np=0;
73  st_parms="";
74  } else {
75  std::string all=var+","+parms;
76  fpw.Parse(formula,all);
77  st_np=np;
78  st_parms=parms;
79  arr.resize(np);
80  }
81  st_form=formula;
82  st_var=var;
83  }
84 
85  virtual ~funct11_string() {
86  };
87 
88 
89  /** \brief Specify the string and the parameters
90  */
91  int set_function(std::string formula, std::string var,
92  int np=0, std::string parms="") {
93  if(np<1) {
94  fpw.Parse(formula,var);
95  st_np=0;
96  st_parms="";
97  } else {
98  std::string all=var+","+parms;
99  fpw.Parse(formula,all);
100  st_np=np;
101  st_parms=parms;
102  arr.resize(np);
103  }
104  st_form=formula;
105  st_var=var;
106  return 0;
107  }
108 
109  /** \brief Set the values of the auxilliary parameters that were
110  specified in \c parms in the constructor
111  */
112  template<class vec_t> int set_parms(const vec_t &p) {
113  for(int i=0;i<st_np;i++) {
114  arr[i]=p[i];
115  }
116  return 0;
117  }
118 
119  /** \brief Compute the function at point \c x and return the result
120  */
121  virtual double operator()(double x) const {
122  double y;
123  if(st_np<1) {
124  y=fpw.Eval(&x);
125  } else {
126  double *all=new double[st_np+1];
127  all[0]=x;
128  for(size_t i=1;i<=st_np;i++) all[i]=arr[i-1];
129  y=fpw.Eval(all);
130  delete[] all;
131  }
132  return y;
133  }
134 
135 #ifndef DOXYGEN_INTERNAL
136 
137  protected:
138 
139  /// The object for evaluating strings
141 
142  /// The number of parameters
143  size_t st_np;
144 
145  /// Storage for the \ref fpw call.
147 
148  /// The formula
149  std::string st_form;
150  /// The variables
151  std::string st_var;
152  /// The parameters
153  std::string st_parms;
154 
155  funct11_string() {};
156 
157 #endif
158 #ifndef DOXYGEN_NO_O2NS
159 
160  private:
161 
163  funct11_string& operator=(const funct11_string&);
164 
165 #endif
166 
167  };
168 
169  /** \brief A wrapper to specify \ref o2scl::funct11 objects
170  to GSL
171  */
172  class funct_gsl : public gsl_function {
173 
174  protected:
175 
176  /// The function wrapper
177  static double funct_wrap(double x, void *params) {
178  funct11 *fp=(funct11 *)params;
179  return (*fp)(x);
180  }
181 
182  public:
183 
184  /// Create an object based on the specified function, \c f
186  function=&funct_wrap;
187  params=&f;
188  }
189 
190  };
191 
192 
193 #ifndef DOXYGEN_NO_O2NS
194 }
195 #endif
196 
197 #endif
Parse a mathematical function specified in a string.
Definition: fparser.h:29
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:177
int set_function(std::string formula, std::string var, int np=0, std::string parms="")
Specify the string and the parameters.
Definition: funct.h:91
size_t st_np
The number of parameters.
Definition: funct.h:143
int set_parms(const vec_t &p)
Set the values of the auxilliary parameters that were specified in parms in the constructor.
Definition: funct.h:112
A wrapper to specify o2scl::funct11 objects to GSL.
Definition: funct.h:172
std::string st_var
The variables.
Definition: funct.h:151
std::string st_form
The formula.
Definition: funct.h:149
funct_gsl(funct11 &f)
Create an object based on the specified function, f.
Definition: funct.h:185
std::string st_parms
The parameters.
Definition: funct.h:153
virtual double operator()(double x) const
Compute the function at point x and return the result.
Definition: funct.h:121
FunctionParser fpw
The object for evaluating strings.
Definition: funct.h:140
ubvector arr
Storage for the fpw call.
Definition: funct.h:146
One-dimensional function from a string.
Definition: funct.h:60
funct11_string(std::string formula, std::string var, int np=0, std::string parms="")
Specify the string and the parameters.
Definition: funct.h:68

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