cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
FunctionalExprTk.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2013-2021 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <exprtk.hpp>
20
24#include "CepGen/Utils/String.h"
25
26namespace cepgen {
27 namespace utils {
28 class FunctionalExprTk final : public Functional {
29 public:
30 explicit FunctionalExprTk(const ParametersList&);
31 double eval() const override;
32
34
35 private:
36 exprtk::symbol_table<double> symbols_;
37 exprtk::expression<double> expr_;
38 exprtk::parser<double> parser_;
39 };
40
42 for (size_t i = 0; i < vars_.size(); ++i)
43 symbols_.add_variable(vars_[i], values_[i]);
44 symbols_.add_constants();
45 expr_.register_symbol_table(symbols_);
46 auto expr = replaceAll(expression_, {{"**", "^"}});
47 if (!parser_.compile(expr, expr_))
48 throw CG_WARNING("FunctionalExprTk") << "Failed to compile expression \"" << expression() << "\".";
49 }
50
51 double FunctionalExprTk::eval() const { return expr_.value(); }
52
54 auto desc = Functional::description();
55 desc.setDescription("ExprTk functional evaluator");
56 return desc;
57 }
58 } // namespace utils
59} // namespace cepgen
cepgen::utils::FunctionalExprTk ExprTkFunctional
#define REGISTER_FUNCTIONAL(name, obj)
Add a generic functional object builder definition.
#define CG_WARNING(mod)
Definition Message.h:228
A description object for parameters collection.
double eval() const override
Compute the functional for a given value of the variables.
FunctionalExprTk(const ParametersList &)
static ParametersDescription description()
A string-to-functional parser.
Definition Functional.h:32
const std::string & expression() const
String expression held by this functional parser.
Definition Functional.h:52
std::vector< std::string > vars_
Computer-readable variable to be reached.
Definition Functional.h:62
std::string expression_
Computer-readable expression.
Definition Functional.h:63
static ParametersDescription description()
std::vector< double > values_
Last arguments list fed to the functional.
Definition Functional.h:64
size_t replaceAll(std::string &str, const std::string &from, const std::string &to)
Replace all occurrences of a text by another.
Definition String.cpp:118
Common namespace for this Monte Carlo generator.