suanPan
Expression.h
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2024 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
34class Expression : public Tag {
35 static std::mutex parser_mutex;
36 static exprtk::parser<double> parser;
37
38protected:
39 Col<double> x;
40
41 std::string expression_text;
42
43 std::vector<std::string> variable_text_list;
44
45 exprtk::expression<double> expression;
46
47 exprtk::symbol_table<double> symbol_table;
48
49public:
50 Expression(unsigned, std::vector<std::string>&&);
51 Expression(const Expression&) = delete;
52 Expression(Expression&&) noexcept = delete;
53 Expression& operator=(const Expression&) = delete;
54 Expression& operator=(Expression&&) noexcept = delete;
55 ~Expression() override = default;
56
57 [[nodiscard]] virtual unique_ptr<Expression> get_copy() const = 0;
58
59 [[nodiscard]] virtual uword input_size() const;
60 [[nodiscard]] virtual uword output_size() const;
61
62 bool compile(const std::string_view&);
63 static std::string error();
64
65 Mat<double> evaluate(double);
66 virtual Mat<double> evaluate(const Col<double>&) = 0;
67
68 Mat<double> gradient(double);
69 virtual Mat<double> gradient(const Col<double>&) = 0;
70
71 void print() override;
72};
73
74class SimpleScalarExpression final : public Expression {
75public:
76 SimpleScalarExpression(unsigned, const std::string_view&);
77
78 [[nodiscard]] unique_ptr<Expression> get_copy() const override;
79
80 Mat<double> evaluate(const Col<double>&) override;
81
82 Mat<double> gradient(const Col<double>&) override;
83};
84
85class SimpleVectorExpression final : public Expression {
86 Col<double> y;
87
88public:
89 SimpleVectorExpression(unsigned, const std::string_view&, const std::string_view&);
90
91 [[nodiscard]] unique_ptr<Expression> get_copy() const override;
92
93 [[nodiscard]] uword output_size() const override;
94
95 Mat<double> evaluate(const Col<double>&) override;
96
97 Mat<double> gradient(const Col<double>&) override { throw std::runtime_error("gradient is not implemented for vector expression"); }
98};
99
100#endif
101
A Expression class represents a maths expression.
Definition: Expression.h:34
Mat< double > gradient(double)
Definition: Expression.cpp:56
virtual unique_ptr< Expression > get_copy() const =0
exprtk::expression< double > expression
Definition: Expression.h:45
Mat< double > evaluate(double)
Definition: Expression.cpp:54
void print() override
Definition: Expression.cpp:58
Expression(unsigned, std::vector< std::string > &&)
Definition: Expression.cpp:24
virtual uword output_size() const
Definition: Expression.cpp:44
Expression(Expression &&) noexcept=delete
std::string expression_text
Definition: Expression.h:41
static std::string error()
Definition: Expression.cpp:52
virtual uword input_size() const
Definition: Expression.cpp:42
Col< double > x
Definition: Expression.h:39
std::vector< std::string > variable_text_list
Definition: Expression.h:43
exprtk::symbol_table< double > symbol_table
Definition: Expression.h:47
Expression(const Expression &)=delete
bool compile(const std::string_view &)
Definition: Expression.cpp:46
Definition: Expression.h:74
Definition: Expression.h:85
Mat< double > gradient(const Col< double > &) override
Definition: Expression.h:97
A base Tag class.
Definition: Tag.h:38