multi_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_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/shunting_yard.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  */
50 
51  public:
52 
53  /** \brief Specify the string and the parameters
54  */
55  template<class vec_string_t=std::vector<std::string> >
56  multi_funct11_strings(std::string expr, int nv,
57  vec_string_t &var_arr) {
58 
59  st_nv=nv;
60  st_funct=expr;
61  st_vars.resize(nv);
62  for (int i=0;i<nv;i++) {
63  calc.compile(expr.c_str(),&vars);
64  st_vars[i]=var_arr[i];
65  }
66  }
67 
68  /** \brief Specify the string and the parameters
69  */
70  template<class vec_string_t=std::vector<std::string> >
71  void set_function(std::string expr, int nv, vec_string_t &var_arr) {
72 
73  st_nv=nv;
74  st_funct=expr;
75  st_vars.resize(nv);
76  for (int i=0;i<nv;i++) {
77  calc.compile(expr.c_str(),&vars);
78  st_vars[i]=var_arr[i];
79  }
80  return;
81  }
82 
83  virtual ~multi_funct11_strings() {
84  };
85 
86  /** \brief Set the values of the auxilliary parameters that were
87  specified in \c parms in the constructor
88  */
89  int set_parm(std::string name, double val) {
90  vars[name]=val;
91  return 0;
92  }
93 
94  /** \brief Compute a function \c y of \c nv variables stored in \c x
95  with parameter \c pa.
96  */
97  template<class vec_t=boost::numeric::ublas::vector<double> >
98  double operator()(size_t nv, const vec_t &x) {
99 
100  for(int i=0;i<nv;i++) {
101  vars[st_vars[i]]=x[i];
102  }
103 
104  return calc.eval(&vars);
105  }
106 
107 #ifndef DOXYGEN_INTERNAL
108 
109  protected:
110 
111  /// The function parser
113 
114  /// External variables to include in the function parsing
115  std::map<std::string,double> vars;
116 
117  /// The number of variables
118  int st_nv;
119 
120  /// The function string
121  std::string st_funct;
122 
123  /// The variable string
124  std::vector<std::string> st_vars;
125 
127 
128 #ifndef DOXYGEN_NO_O2NS
129 #endif
130 
131  private:
132 
134  multi_funct11_strings& operator=(const multi_funct11_strings&);
135 
136 #endif
137 
138  };
139 
140 #ifndef DOXYGEN_NO_O2NS
141 }
142 #endif
143 
144 #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.
A multi-dimensional function from a string.
Definition: multi_funct.h:49
int st_nv
The number of variables.
Definition: multi_funct.h:118
multi_funct11_strings(std::string expr, int nv, vec_string_t &var_arr)
Specify the string and the parameters.
Definition: multi_funct.h:56
int set_parm(std::string name, double val)
Set the values of the auxilliary parameters that were specified in parms in the constructor.
Definition: multi_funct.h:89
void set_function(std::string expr, int nv, vec_string_t &var_arr)
Specify the string and the parameters.
Definition: multi_funct.h:71
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:98
double eval(std::map< std::string, double > *vars=0)
Evalate the previously compiled expression using variables specified in vars.
std::string st_funct
The function string.
Definition: multi_funct.h:121
Evaluate a mathematical expression in a string.
std::map< std::string, double > vars
External variables to include in the function parsing.
Definition: multi_funct.h:115
std::vector< std::string > st_vars
The variable string.
Definition: multi_funct.h:124
calculator calc
The function parser.
Definition: multi_funct.h:112
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).