cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
FunctionalLibmatheval.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 <matheval.h>
20
24#include "CepGen/Utils/String.h"
25
26namespace cepgen {
27 namespace utils {
28 class FunctionalLibmatheval final : public Functional {
29 public:
31 double eval() const;
32
34
35 private:
36 struct eval_deleter {
37 void operator()(void* eval) { evaluator_destroy(eval); }
38 };
39 std::unique_ptr<void, eval_deleter> eval_;
40 std::vector<std::string> parsed_vars_;
41 char** c_parsed_vars_;
42 };
43
45 : Functional(params), eval_(evaluator_create(const_cast<char*>(expression_.c_str()))) {
46 if (!eval_)
47 throw CG_ERROR("FunctionalLibmatheval")
48 << "Evaluator was not properly initialised.\n"
49 << "Likely a syntax error was detected in the expression \"" << expression_ << "\".";
50 int num_vars;
51 evaluator_get_variables(eval_.get(), &c_parsed_vars_, &num_vars);
52 for (int i = 0; i < num_vars; ++i)
53 parsed_vars_.emplace_back(c_parsed_vars_[i]);
54 if (parsed_vars_.size() != vars_.size())
55 throw CG_FATAL("FunctionalLibmatheval")
56 << "Parsed " << utils::s("variable", num_vars, true) << ": " << parsed_vars_ << " where "
57 << utils::s("variable", vars_.size(), true) << " is/are expected: " << vars_ << "!";
58 }
59
61 return evaluator_evaluate(eval_.get(), parsed_vars_.size(), c_parsed_vars_, const_cast<double*>(values_.data()));
62 }
63
65 auto desc = Functional::description();
66 desc.setDescription("libmatheval evaluator");
67 return desc;
68 }
69 } // namespace utils
70} // namespace cepgen
#define CG_FATAL(mod)
Definition Exception.h:61
#define CG_ERROR(mod)
Definition Exception.h:60
#define REGISTER_FUNCTIONAL(name, obj)
Add a generic functional object builder definition.
cepgen::utils::FunctionalLibmatheval LMEFunctional
A description object for parameters collection.
double eval() const
Compute the functional for a given value of the variables.
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()
std::vector< double > values_
Last arguments list fed to the functional.
Definition Functional.h:64
std::string s(const std::string &word, float num, bool show_number)
Add a trailing "s" when needed.
Definition String.cpp:228
Common namespace for this Monte Carlo generator.