Monte Carlo Integration Library 1.0
High-performance Monte Carlo methods for numerical integration and optimization
muParserXInterface.hpp
Go to the documentation of this file.
1#ifndef HH_MUPARSERXINTERFACE_HH
2#define HH_MUPARSERXINTERFACE_HH
3#include "mpParser.h"
4#include <array>
5#include <iostream>
6#include <string>
7#include <fstream>
8#include <sstream>
9#include <stdexcept>
10
11namespace mc::utils
12
13{
29template <int N, class ArgumentType = std::array<double, N> >
31{
32public:
39 : My_e(),
40 M_parser(mup::pckALL_NON_COMPLEX | mup::pckMATRIX), M_value{N, 0.0}
41 {
42 M_parser.DefineVar("x", mup::Variable(&M_value));
43 }
45 muParserXInterface(const std::string expression) : muParserXInterface()
46 {
47 My_e = expression;
48 M_parser.SetExpr(My_e.c_str());
49 }
50
64 : My_e(mpi.My_e),
65 M_parser(mup::pckALL_NON_COMPLEX | mup::pckMATRIX), M_value{N, 0.0}
66 {
67 M_parser.DefineVar("x", mup::Variable(&M_value));
68 M_parser.SetExpr(My_e.c_str());
69 }
80 {
81 if(this != &mpi)
82 {
83 this->My_e = mpi.My_e;
84 this->M_parser.ClearVar(); // clear the variables!
85 this->M_value = mpi.M_value;
86 M_parser.DefineVar("x", mup::Variable(&M_value));
87 M_parser.SetExpr(My_e.c_str());
88 }
89 return *this;
90 }
91
92
100 static muParserXInterface fromFile(const std::string &filename)
101 {
102 std::ifstream file(filename);
103 if (!file.is_open())
104 {
105 throw std::runtime_error("Cannot open function file: " + filename);
106 }
107
108 std::string expression;
109 // Legge l'intero contenuto o solo la prima riga.
110 // Qui leggiamo la prima riga non vuota.
111 while(std::getline(file, expression)) {
112 if(!expression.empty()) break;
113 }
114
115 file.close();
116
117 if(expression.empty()) {
118 throw std::runtime_error("File is empty or contains no expression: " + filename);
119 }
120
121 std::cout << "Read expression from file: " << expression << std::endl;
122
123 // Chiama il costruttore normale passando la stringa letta
124 return muParserXInterface(expression);
125 }
126
128
133 void
134 set_expression(const std::string &e)
135 {
136 My_e = e;
137 M_parser.SetExpr(e.c_str());
138 }
139
140 auto
141 operator()(ArgumentType const &x) const
142 {
143 for(int i = 0; i < N; ++i)
144 {
145 M_value.At(i) = x[i];
146 }
147 mup::Value ans;
148 try
149 {
150 ans = M_parser.Eval();
151 }
152 catch(mup::ParserError &error)
153 {
154 std::cerr << "Muparsex error with code:" << error.GetCode()
155 << std::endl;
156 ;
157 std::cerr << "While processing expression: " << error.GetExpr()
158 << std::endl;
159 std::cerr << "Error Message: " << error.GetMsg() << std::endl;
160 throw error;
161 }
162 return ans.GetFloat();
163 }
164
165private:
166 // a copy of the muparserX expression, used for the copy operations
167 std::string My_e;
168 // The muparseX engine
169 mup::ParserX M_parser;
170 // The muparserX value used to set the variables in the engine
171 mutable mup::Value M_value;
172};
173} // namespace mc::utils
174#endif
muParserXInterface()
Default constructor.
muParserXInterface(muParserXInterface const &mpi)
muParserXInterface operator=(muParserXInterface const &mpi)
void set_expression(const std::string &e)
Sets the muparserX expression.
static muParserXInterface fromFile(const std::string &filename)
auto operator()(ArgumentType const &x) const
muParserXInterface(const std::string expression)
Constructor that takes a string containing muParserX expression.