All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ode_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_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 
32 #include <boost/numeric/ublas/vector.hpp>
33 
34 #include <o2scl/fparser.h>
35 
36 #ifndef DOXYGEN_NO_O2NS
37 namespace o2scl {
38 #endif
39 
40  /** \brief Ordinary differential equation function
41  */
42  typedef std::function<int(double,size_t,
46 
47  /** \brief One-dimensional function from strings
48  \nothing
49  */
50  template <class vec_y_t=boost::numeric::ublas::vector<double>,
51  class vec_dydx_t=vec_y_t>
53  public:
54 
55  /** \brief Specify the string and the parameters
56  */
57  ode_funct11_strings(size_t nv, std::string *formulas, std::string funcs,
58  std::string var, size_t np=0, std::string parms="") {
59  size_t i;
60  fpw=new FunctionParser[nv];
61  if(np<1) {
62  for(i=0;i<nv;i++) {
63  fpw[i].Parse(formulas[i],funcs+","+var);
64  }
65  st_np=0;
66  st_parms="";
67  } else {
68  for(i=0;i<nv;i++) {
69  fpw[i].Parse(formulas[i],funcs+","+var+","+parms);
70  }
71  st_np=np;
72  st_parms=parms;
73  arr=new double[np];
74  }
75  st_forms=formulas;
76  st_var=var;
77  st_funcs=funcs;
78  st_nv=nv;
79  }
80 
81  virtual ~ode_funct11_strings() {
82  if (st_np>0) {
83  delete[] arr;
84  }
85  delete[] fpw;
86  }
87 
88  /** \brief Specify the string and the parameters
89  */
90  int set_function(size_t nv, std::string *formulas, std::string funcs,
91  std::string var, size_t np=0, std::string parms="") {
92  size_t i;
93  if (nv!=st_nv) {
94  delete[] fpw;
95  fpw=new FunctionParser[nv];
96  }
97  if(np<1) {
98  for(i=0;i<nv;i++) {
99  fpw[i].Parse(formulas[i],funcs+","+var);
100  }
101  st_np=0;
102  st_parms="";
103  } else {
104  for(i=0;i<nv;i++) {
105  fpw[i].Parse(formulas[i],funcs+","+var+","+parms);
106  }
107  st_np=np;
108  st_parms=parms;
109  arr=new double[np];
110  }
111  st_forms=formulas;
112  st_var=var;
113  st_funcs=funcs;
114  st_nv=nv;
115  return 0;
116  }
117 
118  /** \brief Set the values of the auxilliary parameters that were
119  specified in 'parms' in the constructor
120  */
121  template<class vec_p_t> void set_parms(vec_p_t &p) {
122  for(size_t i=0;i<st_np;i++) {
123  arr[i]=p[i];
124  }
125  return;
126  }
127 
128  virtual int operator()(double x, size_t nv, const vec_y_t &y,
129  vec_dydx_t &dydx) {
130  size_t i;
131  if(st_np<1) {
132  double *all=new double[nv+1];
133  for(i=0;i<nv;i++) all[i]=y[i];
134  all[nv]=x;
135  for(i=0;i<st_nv;i++) {
136  dydx[i]=fpw[i].Eval(all);
137  }
138  delete[] all;
139  } else {
140  double *all=new double[st_np+st_nv+1];
141  for(i=0;i<st_nv;i++) all[i]=y[i];
142  all[st_nv]=x;
143  for(i=st_nv+1;i<st_np+st_nv+1;i++) all[i]=arr[i-st_nv-1];
144  for(i=0;i<st_nv;i++) {
145  dydx[i]=fpw[i].Eval(all);
146  }
147  delete[] all;
148  }
149  return 0;
150  }
151 
152 #ifndef DOXYGEN_INTERNAL
153 
154  protected:
155 
156  /// The formula parser
158  /// The number of parameters
159  size_t st_np;
160  /// The number of variables
161  size_t st_nv;
162  /// The parameters
163  double *arr;
164  /// The formulas
165  std::string *st_forms;
166  /// The variables
167  std::string st_var;
168  /// The parameters
169  std::string st_parms;
170  /// The function names
171  std::string st_funcs;
172 
173  ode_funct11_strings() {};
174 
175  private:
176 
178  ode_funct11_strings& operator=(const ode_funct11_strings&);
179 
180 #endif
181 
182  };
183 
184 
185 #ifndef DOXYGEN_NO_O2NS
186 }
187 #endif
188 
189 #endif
ode_funct11_strings(size_t nv, std::string *formulas, std::string funcs, std::string var, size_t np=0, std::string parms="")
Specify the string and the parameters.
Definition: ode_funct.h:57
Parse a mathematical function specified in a string.
Definition: fparser.h:29
void set_parms(vec_p_t &p)
Set the values of the auxilliary parameters that were specified in 'parms' in the constructor...
Definition: ode_funct.h:121
int set_function(size_t nv, std::string *formulas, std::string funcs, std::string var, size_t np=0, std::string parms="")
Specify the string and the parameters.
Definition: ode_funct.h:90
double * arr
The parameters.
Definition: ode_funct.h:163
std::string st_parms
The parameters.
Definition: ode_funct.h:169
One-dimensional function from strings.
Definition: ode_funct.h:52
std::string * st_forms
The formulas.
Definition: ode_funct.h:165
FunctionParser * fpw
The formula parser.
Definition: ode_funct.h:157
std::string st_var
The variables.
Definition: ode_funct.h:167
size_t st_np
The number of parameters.
Definition: ode_funct.h:159
std::string st_funcs
The function names.
Definition: ode_funct.h:171
size_t st_nv
The number of variables.
Definition: ode_funct.h:161
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: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..