28#ifndef KIDS_EXPRESSION_H
29#define KIDS_EXPRESSION_H
34#ifdef KIDS_EXPRESSION_USE_EXPRTK
35#include "exprtk/exprtk.hpp"
36#elif defined(KIDS_EXPRESSION_USE_LEPTON)
38#include "fparser/fparser.hh"
58 static std::vector<Expression<T>> _GLOBAL_EXPRESSIONS;
59 return _GLOBAL_EXPRESSIONS;
69 static int registerExpression(
const std::string& expression,
const std::vector<std::string>& variables) {
72 for (
auto it = allExpressions.begin(); it != allExpressions.end(); ++it, ++index) {
73 if (expression == it->expressionString)
return index;
75 allExpressions.push_back(
Expression<T>(expression, variables));
76 return allExpressions.size() - 1;
88#ifdef KIDS_EXPRESSION_USE_EXPRTK
91 exprtk::expression<T> expressionValue;
92 std::vector<T> variablesValue;
100 Expression(
const std::string& expressionStr,
const std::vector<std::string>& varsStr)
102 exprtk::symbol_table<T> symbolTable;
105 symbolTable.add_constants();
106 expressionValue.register_symbol_table(symbolTable);
111 for (
int i = 0; i < variablesValue.size(); ++i) variablesValue[i] = data[i];
112 return expressionValue.value();
115#elif defined(KIDS_EXPRESSION_USE_LEPTON)
127 Expression(
const std::string& expressionStr,
const std::vector<std::string>& varsStr)
130 for (
int i = 0; i < varsStr.size(); ++i) {
131 variablesString += (i == 0) ? varsStr[i] : (std::string(
",") + varsStr[i]);
134 if (res != -1)
throw std::runtime_error(
parser.ErrorMsg());
A class representing a mathematical expression.
static int registerExpression(const std::string &expression, const std::vector< std::string > &variables)
Register a new mathematical expression.
static std::vector< Expression< T > > & getExpressions()
Get the list of registered expressions.
FunctionParserBase< T > parser
The parser for the expression.
T evaluate(T *data)
Evaluate the expression with given variable values.
std::string expressionString
The mathematical expression string.
std::string variablesString
List of variables in the expression.
Expression(const std::string &expressionStr, const std::vector< std::string > &varsStr)
Private constructor to initialize the expression.
< http://warp.povusers.org/FunctionParser/fparser.html