OASIS
Open Algebra Software
Loading...
Searching...
No Matches
Expression.hpp
Go to the documentation of this file.
1#ifndef OASIS_EXPRESSION_HPP
2#define OASIS_EXPRESSION_HPP
3
4#include <memory>
5#include <vector>
6
7#include "Concepts.hpp"
8
9namespace Oasis {
10
11class SerializationVisitor;
12
16enum class ExpressionType {
17 None,
18 Real,
21 Add,
24 Divide,
26 Log,
28 Limit,
30 Negate,
31 Sqrt,
32 Matrix,
33 Pi,
36};
37
41enum ExpressionCategory : uint32_t {
42 None = 0,
44 Commutative = 1 << 1,
45 BinExp = 1 << 2,
46 UnExp = 1 << 3,
47};
48
57public:
62 [[nodiscard]] virtual auto Copy() const -> std::unique_ptr<Expression> = 0;
63
68 [[nodiscard]] virtual auto Differentiate(const Expression&) const -> std::unique_ptr<Expression>;
69
81 [[nodiscard]] virtual auto Equals(const Expression& other) const -> bool = 0;
82
88 auto FindZeros() const -> std::vector<std::unique_ptr<Expression>>;
89
94 [[nodiscard]] virtual auto GetCategory() const -> uint32_t;
95
100 [[nodiscard]] virtual auto GetType() const -> ExpressionType;
101
112 [[nodiscard]] virtual auto Generalize() const -> std::unique_ptr<Expression>;
113
119 [[nodiscard]] virtual auto Integrate(const Expression&) const -> std::unique_ptr<Expression>;
120
126 [[nodiscard]] virtual auto IntegrateWithBounds(const Expression&, const Expression&, const Expression&) -> std::unique_ptr<Expression>;
133 template <IExpression T>
134 [[nodiscard]] bool Is() const
135 {
136 return GetType() == T::GetStaticType();
137 }
138
139 template <template <typename> typename T>
140 [[nodiscard]] bool Is() const
141 {
142 return GetType() == T<Expression>::GetStaticType();
143 }
144
145 template <template <typename, typename> typename T>
146 [[nodiscard]] bool Is() const
147 {
148 return GetType() == T<Expression, Expression>::GetStaticType();
149 }
150
155 [[nodiscard]] virtual auto Simplify() const -> std::unique_ptr<Expression>;
156
167 [[nodiscard]] virtual auto StructurallyEquivalent(const Expression& other) const -> bool = 0;
168
169 [[nodiscard]] virtual auto Substitute(const Expression& var, const Expression& val) -> std::unique_ptr<Expression> = 0;
170
176 virtual void Serialize(SerializationVisitor& visitor) const = 0;
177
178 virtual ~Expression() = default;
179};
180
181#define EXPRESSION_TYPE(type) \
182 auto GetType() const -> ExpressionType override \
183 { \
184 return ExpressionType::type; \
185 } \
186 \
187 static auto GetStaticType() -> ExpressionType \
188 { \
189 return ExpressionType::type; \
190 }
191
192#define EXPRESSION_CATEGORY(category) \
193 auto GetCategory() const -> uint32_t override \
194 { \
195 return category; \
196 } \
197 \
198 constexpr static auto GetStaticCategory() -> uint32_t \
199 { \
200 return category; \
201 }
202
203#define DECL_SPECIALIZE(type)
204
205} // namespace Oasis
206
211
212#endif // OASIS_EXPRESSION_HPP
std::unique_ptr< Oasis::Expression > operator+(const std::unique_ptr< Oasis::Expression > &lhs, const std::unique_ptr< Oasis::Expression > &rhs)
Definition Expression.cpp:250
std::unique_ptr< Oasis::Expression > operator*(const std::unique_ptr< Oasis::Expression > &lhs, const std::unique_ptr< Oasis::Expression > &rhs)
Definition Expression.cpp:258
std::unique_ptr< Oasis::Expression > operator/(const std::unique_ptr< Oasis::Expression > &lhs, const std::unique_ptr< Oasis::Expression > &rhs)
Definition Expression.cpp:262
std::unique_ptr< Oasis::Expression > operator-(const std::unique_ptr< Oasis::Expression > &lhs, const std::unique_ptr< Oasis::Expression > &rhs)
Definition Expression.cpp:254
An expression.
Definition Expression.hpp:56
virtual auto Copy() const -> std::unique_ptr< Expression >=0
Copies this expression.
virtual auto StructurallyEquivalent(const Expression &other) const -> bool=0
Checks whether this expression is structurally equivalent to another expression.
virtual void Serialize(SerializationVisitor &visitor) const =0
This function serializes the expression object.
virtual auto Integrate(const Expression &) const -> std::unique_ptr< Expression >
Attempts to integrate this expression using integration rules.
Definition Expression.cpp:230
virtual auto GetCategory() const -> uint32_t
Gets the category of this expression.
Definition Expression.cpp:212
auto FindZeros() const -> std::vector< std::unique_ptr< Expression > >
The FindZeros function finds all rational real zeros, and up to 2 irrational/complex zeros of a polyn...
Definition Expression.cpp:51
bool Is() const
Gets whether this expression is of a specific type.
Definition Expression.hpp:134
virtual auto GetType() const -> ExpressionType
Gets the type of this expression.
Definition Expression.cpp:220
virtual auto IntegrateWithBounds(const Expression &, const Expression &, const Expression &) -> std::unique_ptr< Expression >
Attempts to integrate this expression using integration rules.
Definition Expression.cpp:237
virtual auto Simplify() const -> std::unique_ptr< Expression >
Simplifies this expression.
Definition Expression.cpp:244
virtual auto Differentiate(const Expression &) const -> std::unique_ptr< Expression >
Tries to differentiate this function.
Definition Expression.cpp:216
virtual auto Substitute(const Expression &var, const Expression &val) -> std::unique_ptr< Expression >=0
virtual auto Generalize() const -> std::unique_ptr< Expression >
Converts this expression to a more general expression.
Definition Expression.cpp:225
virtual auto Equals(const Expression &other) const -> bool=0
Compares this expression to another expression for equality.
Definition Magnitude.hpp:28
Definition Serialization.hpp:50
An expression concept.
Definition Concepts.hpp:25
Definition Add.hpp:11
ExpressionCategory
The category of an expression.
Definition Expression.hpp:41
@ UnExp
Definition Expression.hpp:46
@ None
Definition Expression.hpp:42
@ Commutative
Definition Expression.hpp:44
@ Associative
Definition Expression.hpp:43
@ BinExp
Definition Expression.hpp:45
ExpressionType
The type of an expression.
Definition Expression.hpp:16