Function.cc
Go to the documentation of this file.
1 #include <utl/Function.h>
2 
3 #include <utl/ExpressionParser.h>
4 #include <utl/AugerUnits.h>
5 #include <utl/SymbolTable.h>
6 
7 #include <iostream>
8 using namespace std;
9 
10 
11 namespace utl {
12 
13  Function::Function(const std::string& function,
14  const std::vector<std::string>& vars)
15  : fFunction(function) {
16  SymbolTable fVariables;
17  for (const auto &v : vars) {
18  fVariableNames.push_back(v);
19  fVariables[v] = 0;
20  }
21  fFunc = FunctionParser(fVariables);
22  }
23 
24  Function::Function(const std::string& function,
25  const std::string& var)
26  : fFunction(function) {
27  fVariableNames.push_back(var);
28  SymbolTable fVariables;
29  fVariables[var] = 0;
30  fFunc = FunctionParser(fVariables);
31  }
32 
33  double
34  Function::operator()(const double& var)
35  const
36  {
37  if (fVariableNames.size() != 1) {
38  ostringstream err;
39  err << "Number of input variables " << 1 << " is different from number of function parameters " << fVariableNames.size() << " for function \'" << fFunction << "\'";
40  ERROR(err);
41  throw XMLParseException(err.str());
42  }
44  return fFunc.Evaluate(fFunction);
45  }
46 
47  double
48  Function::operator()(const std::vector<double>& vars)
49  const
50  {
51  if (vars.size() != fVariableNames.size()) {
52  ostringstream err;
53  err << "Number of input variables " << vars.size() << " is different from number of function parameters " << fVariableNames.size() << " for function \'" << fFunction << "\'";
54  ERROR(err);
55  throw XMLParseException(err.str());
56  }
57  unsigned int i = 0;
58  for (const auto &v : vars) {
60  ++i;
61  }
62  return fFunc.Evaluate(fFunction);
63  }
64 
65 }
std::string fFunction
Definition: Function.h:46
double operator()() const
Definition: Function.h:38
FunctionParser fFunc
Definition: Function.h:49
Exception for errors encountered when parsing XML.
void SetVariable(const std::string &name, const double &v)
std::map< std::string, double > SymbolTable
Definition: SymbolTable.h:10
std::vector< std::string > fVariableNames
Definition: Function.h:47
utl::ExpressionParser< MathExpressionGrammar, AugerUnits > FunctionParser
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165

, generated on Tue Sep 26 2023.