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
21
#include "
CepGen/Core/Exception.h
"
22
#include "
CepGen/Modules/FunctionalFactory.h
"
23
#include "
CepGen/Utils/Functional.h
"
24
#include "
CepGen/Utils/String.h
"
25
26
namespace
cepgen
{
27
namespace
utils {
28
class
FunctionalLibmatheval
final :
public
Functional
{
29
public
:
30
explicit
FunctionalLibmatheval
(
const
ParametersList
&);
31
double
eval
()
const
;
32
33
static
ParametersDescription
description
();
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
44
FunctionalLibmatheval::FunctionalLibmatheval
(
const
ParametersList
& params)
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
60
double
FunctionalLibmatheval::eval
()
const
{
61
return
evaluator_evaluate(eval_.get(), parsed_vars_.size(), c_parsed_vars_,
const_cast<
double
*
>
(
values_
.data()));
62
}
63
64
ParametersDescription
FunctionalLibmatheval::description
() {
65
auto
desc =
Functional::description
();
66
desc.setDescription(
"libmatheval evaluator"
);
67
return
desc;
68
}
69
}
// namespace utils
70
}
// namespace cepgen
71
typedef
cepgen::utils::FunctionalLibmatheval
LMEFunctional
;
72
REGISTER_FUNCTIONAL
(
"libmatheval"
,
LMEFunctional
);
Exception.h
CG_FATAL
#define CG_FATAL(mod)
Definition
Exception.h:61
CG_ERROR
#define CG_ERROR(mod)
Definition
Exception.h:60
FunctionalFactory.h
REGISTER_FUNCTIONAL
#define REGISTER_FUNCTIONAL(name, obj)
Add a generic functional object builder definition.
Definition
FunctionalFactory.h:25
LMEFunctional
cepgen::utils::FunctionalLibmatheval LMEFunctional
Definition
FunctionalLibmatheval.cpp:71
Functional.h
String.h
cepgen::ParametersDescription
A description object for parameters collection.
Definition
ParametersDescription.h:26
cepgen::ParametersList
Definition
ParametersList.h:52
cepgen::utils::FunctionalLibmatheval
Definition
FunctionalLibmatheval.cpp:28
cepgen::utils::FunctionalLibmatheval::eval
double eval() const
Compute the functional for a given value of the variables.
Definition
FunctionalLibmatheval.cpp:60
cepgen::utils::FunctionalLibmatheval::description
static ParametersDescription description()
Definition
FunctionalLibmatheval.cpp:64
cepgen::utils::FunctionalLibmatheval::FunctionalLibmatheval
FunctionalLibmatheval(const ParametersList &)
Definition
FunctionalLibmatheval.cpp:44
cepgen::utils::Functional
A string-to-functional parser.
Definition
Functional.h:32
cepgen::utils::Functional::vars_
std::vector< std::string > vars_
Computer-readable variable to be reached.
Definition
Functional.h:62
cepgen::utils::Functional::expression_
std::string expression_
Computer-readable expression.
Definition
Functional.h:63
cepgen::utils::Functional::description
static ParametersDescription description()
Definition
Functional.cpp:57
cepgen::utils::Functional::values_
std::vector< double > values_
Last arguments list fed to the functional.
Definition
Functional.h:64
cepgen::utils::s
std::string s(const std::string &word, float num, bool show_number)
Add a trailing "s" when needed.
Definition
String.cpp:228
cepgen
Common namespace for this Monte Carlo generator.
Definition
CommandLineHandler.cpp:36
CepGenAddOns
Functionals
FunctionalLibmatheval.cpp
Generated on Mon Jul 29 2024 for CepGen by
1.9.7