ode_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_ODE_FUNCT_H
24 #define O2SCL_ODE_FUNCT_H
25 
26 /** \file ode_funct.h
27  \brief Function object classes for ODE functions
28 */
29 
30 #include <string>
31 #include <map>
32 
33 #include <boost/numeric/ublas/vector.hpp>
34 
35 #include <o2scl/shunting_yard.h>
36 
37 #ifndef DOXYGEN_NO_O2NS
38 namespace o2scl {
39 #endif
40 
41  /** \brief Ordinary differential equation function
42  */
43  typedef std::function<int(double,size_t,
47 
48  /** \brief One-dimensional function from strings
49  \nothing
50  */
52 
53  public:
54 
55  /** \brief Specify the string and the parameters
56  */
57  template<class vec_string_t=std::vector<std::string> >
58  ode_funct11_strings(size_t nv, vec_string_t &exprs,
59  vec_string_t &funcs, std::string var) {
60 
61  calc.resize(nv);
62  st_forms.resize(nv);
63  st_funcs.resize(nv);
64  for(size_t i=0;i<nv;i++) {
65  st_forms[i]=exprs[i];
66  calc[i].compile(exprs[i].c_str(),&vars);
67  st_funcs[i]=funcs[i];
68  }
69  st_nv=nv;
70  st_var=var;
71  }
72 
73  virtual ~ode_funct11_strings() {
74  }
75 
76  /** \brief Specify the string and the parameters
77  */
78  template<class vec_string_t=std::vector<std::string> >
79  void set_function(size_t nv, vec_string_t &exprs,
80  vec_string_t &funcs, std::string var) {
81  calc.resize(nv);
82  st_forms.resize(nv);
83  st_funcs.resize(nv);
84  for(size_t i=0;i<nv;i++) {
85  st_forms[i]=exprs[i];
86  st_funcs[i]=funcs[i];
87  }
88  st_nv=nv;
89  st_var=var;
90  return;
91  }
92 
93  /** \brief Set the values of the auxilliary parameters that were
94  specified in 'parms' in the constructor
95  */
96  int set_parm(std::string name, double val) {
97  vars[name]=val;
98  return 0;
99  }
100 
101  template <class vec_y_t=boost::numeric::ublas::vector<double>,
102  class vec_dydx_t=vec_y_t>
103  int operator()(double x, size_t nv, const vec_y_t &y,
104  vec_dydx_t &dydx) {
105 
106  for(size_t i=0;i<st_nv;i++) {
107  vars[st_funcs[i]]=y[i];
108  vars[st_var]=x;
109  }
110  for(size_t i=0;i<st_nv;i++) {
111  dydx[i]=calc[i].eval(&vars);
112  }
113  return 0;
114  }
115 
116 #ifndef DOXYGEN_INTERNAL
117 
118  protected:
119 
120  /// The function parser
121  std::vector<calculator> calc;
122 
123  /// Desc
124  std::map<std::string,double> vars;
125 
126  /// The number of variables
127  size_t st_nv;
128  /// The expressions
129  std::vector<std::string> st_forms;
130  /// The variables
131  std::string st_var;
132  /// The function names
133  std::vector<std::string> st_funcs;
134 
135  ode_funct11_strings() {};
136 
137  private:
138 
140  ode_funct11_strings& operator=(const ode_funct11_strings&);
141 
142 #endif
143 
144  };
145 
146 #ifndef DOXYGEN_NO_O2NS
147 }
148 #endif
149 
150 #endif
int set_parm(std::string name, double val)
Set the values of the auxilliary parameters that were specified in &#39;parms&#39; in the constructor...
Definition: ode_funct.h:96
size_t st_nv
The number of variables.
Definition: ode_funct.h:127
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
std::vector< calculator > calc
The function parser.
Definition: ode_funct.h:121
void set_function(size_t nv, vec_string_t &exprs, vec_string_t &funcs, std::string var)
Specify the string and the parameters.
Definition: ode_funct.h:79
std::map< std::string, double > vars
Desc.
Definition: ode_funct.h:124
One-dimensional function from strings.
Definition: ode_funct.h:51
std::vector< std::string > st_funcs
The function names.
Definition: ode_funct.h:133
std::vector< std::string > st_forms
The expressions.
Definition: ode_funct.h:129
ode_funct11_strings(size_t nv, vec_string_t &exprs, vec_string_t &funcs, std::string var)
Specify the string and the parameters.
Definition: ode_funct.h:58
std::function< int(double, size_t, const boost::numeric::ublas::vector< double > &, boost::numeric::ublas::vector< double > &)> ode_funct11
Ordinary differential equation function.
Definition: ode_funct.h:46
std::string st_var
The variables.
Definition: ode_funct.h:131

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