cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
FunctionalTinyExpr.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2013-2022 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 <tinyexpr.h>
20
24#include "CepGen/Utils/String.h"
25
26namespace cepgen {
27 namespace utils {
28 class FunctionalTinyExpr final : public Functional {
29 public:
30 explicit FunctionalTinyExpr(const ParametersList&);
31 double eval() const override;
32
34
35 private:
36 struct te_deleter {
37 void operator()(te_expr* expr) { te_free(expr); }
38 };
39 std::unique_ptr<te_expr, te_deleter> eval_;
40 };
41
43 std::vector<te_variable> te_vars;
44 for (size_t i = 0; i < vars_.size(); ++i)
45 te_vars.emplace_back(te_variable{vars_.at(i).c_str(), &values_.at(i), TE_VARIABLE, nullptr});
46 const auto expr = utils::replaceAll(expression_, {{"**", "^"}});
47 int error;
48 eval_.reset(te_compile(expr.c_str(), te_vars.data(), vars_.size(), &error));
49 if (!eval_) {
50 const std::string pre_syntax_err = "A syntax error was detected in the expression \"";
51 const std::string postfix = (expr != expression_) ? " (adapted from \"" + expression_ + "\")" : "";
52 throw CG_ERROR("FunctionalTinyExpr") << "Evaluator was not properly initialised.\n"
53 << pre_syntax_err << expr << "\"" << postfix << "\n"
54 << std::string(pre_syntax_err.size() + error - 1, ' ') + "^";
55 }
56 }
57
58 double FunctionalTinyExpr::eval() const { return te_eval(eval_.get()); }
59
61 auto desc = Functional::description();
62 desc.setDescription("TinyExpr evaluator");
63 return desc;
64 }
65 } // namespace utils
66} // namespace cepgen
#define CG_ERROR(mod)
Definition Exception.h:60
#define REGISTER_FUNCTIONAL(name, obj)
Add a generic functional object builder definition.
cepgen::utils::FunctionalTinyExpr TinyExprFunctional
A description object for parameters collection.
double eval() const override
Compute the functional for a given value of the variables.
FunctionalTinyExpr(const ParametersList &)
static ParametersDescription description()
A string-to-functional parser.
Definition Functional.h:32
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()
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.