cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
Functional.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
22#include "CepGen/Utils/String.h"
23
24namespace cepgen {
25 namespace utils {
27 : NamedModule(params),
28 vars_orig_(steer<std::vector<std::string> >("variables")),
29 expression_orig_(steer<std::string>("expression")),
30 vars_(vars_orig_),
31 expression_(expression_orig_),
32 values_(vars_.size()) {
33 for (size_t i = 0; i < vars_.size(); ++i) {
34 vars_.at(i) = sanitise(vars_.at(i));
35 replaceAll(expression_, vars_orig_.at(i), vars_.at(i));
36 }
37 }
38
39 double Functional::operator()(double x) const {
40 if (vars_orig_.size() != 1)
41 throw CG_FATAL("Functional") << "This function only works with single-dimensional functions!";
42 return operator()(std::vector<double>{x});
43 }
44
45 double Functional::operator()(const std::vector<double>& x) const {
46 if (vars_.size() != x.size())
47 throw CG_FATAL("Functional") << "Invalid number of variables fed to the evaluator! Expecting " << vars_.size()
48 << ", got " << x.size() << ".";
49 values_ = x;
50 return eval();
51 }
52
53 ParametersList Functional::fromExpression(const std::string& expr, const std::vector<std::string>& vars) {
54 return ParametersList().set<std::string>("expression", expr).set<std::vector<std::string> >("variables", vars);
55 }
56
58 auto desc = ParametersDescription();
59 desc.setDescription("Unnamed functional evaluator");
60 desc.add<std::vector<std::string> >("variables", {}).setDescription("List of variables to evaluate");
61 desc.add<std::string>("expression", "").setDescription("Functional expression");
62 return desc;
63 }
64 } // namespace utils
65} // namespace cepgen
#define CG_FATAL(mod)
Definition Exception.h:61
Base runtime module object.
Definition NamedModule.h:28
A description object for parameters collection.
ParametersList & set(const std::string &, const T &)
Set a parameter value Set a recast parameter value.
double operator()(double x) const
Compute the functional for a given value of the variable (one-dimensional case)
std::vector< std::string > vars_
Computer-readable variable to be reached.
Definition Functional.h:62
Functional(const ParametersList &)
Default constructor.
virtual double eval() const =0
Compute the functional for a given value of the variables.
std::string expression_
Computer-readable expression.
Definition Functional.h:63
static ParametersDescription description()
static ParametersList fromExpression(const std::string &expr, const std::vector< std::string > &vars)
Build a collection of parameters to define a functional from its mathematical expression.
std::vector< double > values_
Last arguments list fed to the functional.
Definition Functional.h:64
std::string sanitise(const std::string &str)
Replace all unsafe characters to build a computer-readable (and filename-safe) string.
Definition String.cpp:105
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.