suanPan
Loading...
Searching...
No Matches
Expression.h
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2025 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 UniqueTag {
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
52 [[nodiscard]] virtual unique_ptr<Expression> get_copy() const = 0;
53
54 [[nodiscard]] virtual uword input_size() const;
55 [[nodiscard]] virtual uword output_size() const;
56
57 bool compile(std::string_view);
58 static std::string error();
59
60 Mat<double> evaluate(double);
61 virtual Mat<double> evaluate(const Col<double>&) = 0;
62
63 Mat<double> gradient(double);
64 virtual Mat<double> gradient(const Col<double>&) = 0;
65
66 void print() override;
67};
68
69class SimpleScalarExpression final : public Expression {
70public:
71 SimpleScalarExpression(unsigned, std::string_view);
72
73 [[nodiscard]] unique_ptr<Expression> get_copy() const override;
74
75 Mat<double> evaluate(const Col<double>&) override;
76
77 Mat<double> gradient(const Col<double>&) override;
78};
79
80class SimpleVectorExpression final : public Expression {
81 Col<double> y;
82
83public:
84 SimpleVectorExpression(unsigned, std::string_view, std::string_view);
85
86 [[nodiscard]] unique_ptr<Expression> get_copy() const override;
87
88 [[nodiscard]] uword output_size() const override;
89
90 Mat<double> evaluate(const Col<double>&) override;
91
92 Mat<double> gradient(const Col<double>&) override { throw std::runtime_error("gradient is not implemented for vector expression"); }
93};
94
95#endif
96
A Expression class represents a maths expression.
Definition Expression.h:34
Mat< double > gradient(double)
Definition Expression.cpp:57
virtual unique_ptr< Expression > get_copy() const =0
exprtk::expression< double > expression
Definition Expression.h:45
virtual Mat< double > gradient(const Col< double > &)=0
Mat< double > evaluate(double)
Definition Expression.cpp:55
void print() override
Definition Expression.cpp:59
virtual uword output_size() const
Definition Expression.cpp:45
std::string expression_text
Definition Expression.h:41
static std::string error()
Definition Expression.cpp:53
virtual uword input_size() const
Definition Expression.cpp:43
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
virtual Mat< double > evaluate(const Col< double > &)=0
bool compile(std::string_view)
Definition Expression.cpp:47
Definition Expression.h:69
Mat< double > gradient(const Col< double > &) override
Definition Expression.cpp:79
Mat< double > evaluate(const Col< double > &) override
Definition Expression.cpp:72
unique_ptr< Expression > get_copy() const override
Definition Expression.cpp:66
Definition Expression.h:80
uword output_size() const override
Definition Expression.cpp:103
Mat< double > evaluate(const Col< double > &) override
Definition Expression.cpp:105
unique_ptr< Expression > get_copy() const override
Definition Expression.cpp:97
Mat< double > gradient(const Col< double > &) override
Definition Expression.h:92
Definition Tag.h:77