suanPan
Expression.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (C) 2017-2023 Theodore Chang
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  ******************************************************************************/
28 #ifndef EXPRESSION_H
29 #define EXPRESSION_H
30 
31 #include <Domain/Tag.h>
32 #include <exprtk/exprtk.hpp>
33 
34 class Expression : public Tag {
35  static std::mutex parser_mutex;
36  static exprtk::parser<double> parser;
37 
38  std::string expression_text;
39 
40 protected:
41  Col<double> x;
42 
43  exprtk::expression<double> expression;
44 
45  exprtk::symbol_table<double> symbol_table;
46 
47 public:
48  explicit Expression(unsigned, const std::string&);
49 
50  [[nodiscard]] virtual uword input_size() const;
51  [[nodiscard]] virtual uword output_size() const;
52 
53  bool compile(const std::string&);
54  static string error();
55 
56  Mat<double> evaluate(double);
57  virtual Mat<double> evaluate(const Col<double>&) = 0;
58 
59  Mat<double> gradient(double);
60  virtual Mat<double> gradient(const Col<double>&) = 0;
61 
62  void print() override;
63 };
64 
66 public:
68 
69  Mat<double> evaluate(const Col<double>&) override;
70 
71  Mat<double> gradient(const Col<double>&) override;
72 };
73 
75  Col<double> y;
76 
77 public:
78  SimpleVectorExpression(unsigned, const std::string&, const std::string&);
79 
80  [[nodiscard]] uword output_size() const override;
81 
82  Mat<double> evaluate(const Col<double>&) override;
83 
84  Mat<double> gradient(const Col<double>&) override { throw std::runtime_error("gradient is not implemented for vector expression"); }
85 };
86 
87 #endif
88 
A Expression class represents a maths expression.
Definition: Expression.h:34
Mat< double > gradient(double)
Definition: Expression.cpp:56
exprtk::expression< double > expression
Definition: Expression.h:43
bool compile(const std::string &)
Definition: Expression.cpp:45
Expression(unsigned, const std::string &)
Definition: Expression.cpp:24
static string error()
Definition: Expression.cpp:52
Mat< double > evaluate(double)
Definition: Expression.cpp:54
void print() override
Definition: Expression.cpp:58
virtual uword output_size() const
Definition: Expression.cpp:43
virtual uword input_size() const
Definition: Expression.cpp:41
Col< double > x
Definition: Expression.h:41
virtual Mat< double > gradient(const Col< double > &)=0
virtual Mat< double > evaluate(const Col< double > &)=0
exprtk::symbol_table< double > symbol_table
Definition: Expression.h:45
Definition: Expression.h:65
Mat< double > gradient(const Col< double > &) override
Definition: Expression.cpp:69
Mat< double > evaluate(const Col< double > &) override
Definition: Expression.cpp:62
Definition: Expression.h:74
Mat< double > gradient(const Col< double > &) override
Definition: Expression.h:84
uword output_size() const override
Definition: Expression.cpp:87
SimpleVectorExpression(unsigned, const std::string &, const std::string &)
Definition: Expression.cpp:78
Mat< double > evaluate(const Col< double > &) override
Definition: Expression.cpp:89
A base Tag class.
Definition: Tag.h:38