ParFormula.h
Go to the documentation of this file.
1 // ParFormula.h
2 //
3 // David Adams
4 // July 2021
5 //
6 // Interface for a formula with N variables and M named parameters.
7 // Methods are provided to set the parameters and evaluate
8 // given variables using those settings.
9 
10 #ifndef ParFormula_H
11 #define ParFormula_H
12 
13 #include <string>
14 #include <vector>
15 
16 class ParFormula {
17 
18 public:
19 
20  using Value = double; // Type for variables, parameters and evaluation
21  using Values = std::vector<Value>;
22  using Index = unsigned int;
23  using Name = std::string;
24  using Names = std::vector<Name>;
25 
26 public: // const methods
27 
28  // Dtor;
29  virtual ~ParFormula() { }
30 
31  // Formula name.
32  virtual Name name() const { return ""; }
33 
34  // String representation of the formula.
35  // Expected but not required that prameters are in square brackets
36  // and variables x, y, z or x[0], x[1], ....
37  // E.g. for parameters Par1 and Par2: [Par1]*sin(x + [Par2])
38  virtual Name formulaString() const =0;
39 
40  // Number of variables.
41  virtual Index nvar() const =0;
42 
43  // Parameter count and names.
44  virtual Index npar() const { return pars().size(); }
45  virtual Names pars() const =0;
46 
47  // Return the set, unset and reset parameter names.
48  virtual Names setPars() const =0;
49  virtual Names unsetPars() const =0;
50  virtual Names resetPars() const =0;
51 
52  // Return if a parameter appears in the equation.
53  virtual bool isPar(Name parnam) const =0;
54 
55  // Return if all parameters are set.
56  virtual bool ready() const { return setPars().size() == npar(); }
57 
58  // Default evaluation value.
59  virtual Value defaultEval() const { return 0.0; }
60 
61  // Evaluate the formula for N, 1 and 0 variables.
62  virtual double eval(const Values& vars) const =0;
63  virtual double eval(Value var) const =0;
64  virtual double eval() const =0;
65 
66 public: // non-const methods
67 
68  // Set a parameter value.
69  // Return 0 for success.
70  virtual int setParValue(Name parnam, Value parval) =0;
71 
72  // Set the default evaluation value.
73  // Return 0 for success.
74  virtual int setDefaultEval(Value val) { return 1; }
75 
76  // Unset the parameters.
77  virtual int unsetParValues() =0;
78 
79 };
80 
81 #endif
std::string Name
Definition: ParFormula.h:23
virtual int setDefaultEval(Value val)
Definition: ParFormula.h:74
virtual Names pars() const =0
std::string string
Definition: nybbler.cc:12
unsigned int Index
Definition: ParFormula.h:22
virtual double eval() const =0
virtual Name formulaString() const =0
virtual bool ready() const
Definition: ParFormula.h:56
virtual Names unsetPars() const =0
double Value
Definition: ParFormula.h:20
virtual Index npar() const
Definition: ParFormula.h:44
virtual Index nvar() const =0
virtual Names setPars() const =0
virtual int setParValue(Name parnam, Value parval)=0
std::vector< Name > Names
Definition: ParFormula.h:24
int var
Definition: 018_def.c:9
std::vector< Value > Values
Definition: ParFormula.h:21
virtual Value defaultEval() const
Definition: ParFormula.h:59
virtual Names resetPars() const =0
virtual int unsetParValues()=0
virtual bool isPar(Name parnam) const =0
virtual Name name() const
Definition: ParFormula.h:32
virtual ~ParFormula()
Definition: ParFormula.h:29