All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
multi_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_MULTI_FUNCT_H
24 #define O2SCL_MULTI_FUNCT_H
25 
26 /** \file multi_funct.h
27  \brief Function object classes for a multi-dimensional function
28 */
29 
30 #include <string>
31 #include <functional>
32 
33 #include <boost/numeric/ublas/vector.hpp>
34 
35 #include <o2scl/err_hnd.h>
36 #include <o2scl/fparser.h>
37 
38 #ifndef DOXYGEN_NO_O2NS
39 namespace o2scl {
40 #endif
41 
42  /// Multi-dimensional function typedef
43  typedef std::function<
44  double(size_t,const boost::numeric::ublas::vector<double> &)>
46 
47  /** \brief A multi-dimensional function from a string
48  */
49  template<class vec_t=boost::numeric::ublas::vector<double> >
51 
52  public:
53 
54  /** \brief Specify the string and the parameters
55  */
56  multi_funct11_strings(std::string formula, int nv, std::string vars,
57  int np=0, std::string parms="") {
58 
59  if(np<1) {
60  fpw.Parse(formula,vars);
61  st_np=0;
62  st_parms="";
63  } else {
64  std::string all=vars+","+parms;
65  fpw.Parse(formula,all);
66  st_np=np;
67  st_parms=parms;
68  arr=new double[np];
69  }
70  st_form=formula;
71  st_vars=vars;
72  st_nv=nv;
73  }
74 
75  /** \brief Specify the string and the parameters
76  */
77  int set_function(std::string formula, int nv, std::string vars,
78  int np=0, std::string parms="") {
79 
80  if(np<1) {
81  fpw.Parse(formula,vars);
82  st_np=0;
83  st_parms="";
84  } else {
85  std::string all=vars+","+parms;
86  fpw.Parse(formula,all);
87  st_np=np;
88  st_parms=parms;
89  arr=new double[np];
90  }
91  st_form=formula;
92  st_vars=vars;
93  st_nv=nv;
94  return 0;
95  }
96 
97  virtual ~multi_funct11_strings() {
98  if (st_np>0) {
99  delete[] arr;
100  }
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_parms(const vec_t &p) {
107  for(int i=0;i<st_np;i++) {
108  arr[i]=p[i];
109  }
110  return 0;
111  }
112 
113  /** \brief Compute a function \c y of \c nv variables stored in \c x
114  with parameter \c pa.
115  */
116  virtual double operator()(size_t nv, const vec_t &x) {
117  int i;
118  double y;
119  if(st_np<1) {
120  y=fpw.Eval(x);
121  } else {
122  double *all=new double[st_np+st_nv];
123  for(i=0;i<st_nv;i++) {
124  all[i]=x[i];
125  }
126  for(i=st_nv;i<st_np+st_nv;i++) {
127  all[i]=arr[i-st_nv];
128  }
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  int st_np;
144  /// The number of variables
145  int st_nv;
146  /// Storage for the parameters for \ref fpw
147  double *arr;
148  /// The formula string
149  std::string st_form;
150  /// The variable string
151  std::string st_vars;
152  /// The parameter string
153  std::string st_parms;
154 
156 
157 #ifndef DOXYGEN_NO_O2NS
158 #endif
159 
160  private:
161 
163  multi_funct11_strings& operator=(const multi_funct11_strings&);
164 
165 #endif
166 
167  };
168 
169 #ifndef DOXYGEN_NO_O2NS
170 }
171 #endif
172 
173 #endif
FunctionParser fpw
The object for evaluating strings.
Definition: multi_funct.h:140
Parse a mathematical function specified in a string.
Definition: fparser.h:29
double * arr
Storage for the parameters for fpw.
Definition: multi_funct.h:147
A multi-dimensional function from a string.
Definition: multi_funct.h:50
std::string st_vars
The variable string.
Definition: multi_funct.h:151
int set_function(std::string formula, int nv, std::string vars, int np=0, std::string parms="")
Specify the string and the parameters.
Definition: multi_funct.h:77
virtual double operator()(size_t nv, const vec_t &x)
Compute a function y of nv variables stored in x with parameter pa.
Definition: multi_funct.h:116
std::string st_parms
The parameter string.
Definition: multi_funct.h:153
int set_parms(const vec_t &p)
Set the values of the auxilliary parameters that were specified in parms in the constructor.
Definition: multi_funct.h:106
std::string st_form
The formula string.
Definition: multi_funct.h:149
int st_np
The number of parameters.
Definition: multi_funct.h:143
multi_funct11_strings(std::string formula, int nv, std::string vars, int np=0, std::string parms="")
Specify the string and the parameters.
Definition: multi_funct.h:56
int st_nv
The number of variables.
Definition: multi_funct.h:145
std::function< double(size_t, const boost::numeric::ublas::vector< double > &)> multi_funct11
Multi-dimensional function typedef.
Definition: multi_funct.h:45

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