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 <any>
5#include <cstdint>
6#include <memory>
7#include <optional>
8#include <vector>
9
10#include "Concepts.hpp"
11
12namespace Oasis {
13
14class Visitor;
15
19enum class ExpressionType {
20 None,
21 Real,
24 Add,
27 Divide,
29 Log,
31 Limit,
33 Negate,
34 Sqrt,
35 Matrix,
36 Pi,
39};
40
44enum ExpressionCategory : uint32_t {
45 None = 0,
47 Commutative = 1 << 1,
48 BinExp = 1 << 2,
49 UnExp = 1 << 3,
50};
51
60public:
65 [[nodiscard]] virtual auto Copy() const -> std::unique_ptr<Expression> = 0;
66
71 [[nodiscard]] virtual auto Differentiate(const Expression&) const -> std::unique_ptr<Expression>;
72
84 [[nodiscard]] virtual auto Equals(const Expression& other) const -> bool = 0;
85
91 auto FindZeros() const -> std::vector<std::unique_ptr<Expression>>;
92
97 [[nodiscard]] virtual auto GetCategory() const -> uint32_t;
98
103 [[nodiscard]] virtual auto GetType() const -> ExpressionType;
104
115 [[nodiscard]] virtual auto Generalize() const -> std::unique_ptr<Expression>;
116
122 [[nodiscard]] virtual auto Integrate(const Expression&) const -> std::unique_ptr<Expression>;
123
129 [[nodiscard]] virtual auto IntegrateWithBounds(const Expression&, const Expression&, const Expression&) -> std::unique_ptr<Expression>;
130
137 template <IExpression T>
138 [[nodiscard]] bool Is() const
139 {
140 return GetType() == T::GetStaticType();
141 }
142
143 template <template <typename> typename T>
145 [[nodiscard]] bool Is() const
146 {
147 return GetType() == T<Expression>::GetStaticType();
148 }
149
150 template <template <typename, typename> typename T>
152 [[nodiscard]] bool Is() const
153 {
154 return GetType() == T<Expression, Expression>::GetStaticType();
155 }
156
161 [[nodiscard]] virtual auto Simplify() const -> std::unique_ptr<Expression>;
162
173 [[nodiscard]] virtual auto StructurallyEquivalent(const Expression& other) const -> bool = 0;
174
175 [[nodiscard]] virtual auto Substitute(const Expression& var, const Expression& val) -> std::unique_ptr<Expression> = 0;
176
177 template <IVisitor T>
178 std::optional<typename T::RetT> Accept(T& visitor) const;
179
180 virtual ~Expression() = default;
181
182protected:
188 virtual std::any AcceptInternal(Visitor& visitor) const = 0;
189};
190
191template <IVisitor T>
192std::optional<typename T::RetT> Expression::Accept(T& visitor) const
193{
194 try {
195 return std::any_cast<typename T::RetT>(this->AcceptInternal(visitor));
196 } catch (std::bad_any_cast&) {
197 return std::nullopt;
198 }
199}
200
201#define EXPRESSION_TYPE(type) \
202 auto GetType() const -> ExpressionType override \
203 { \
204 return ExpressionType::type; \
205 } \
206 \
207 static auto GetStaticType() -> ExpressionType \
208 { \
209 return ExpressionType::type; \
210 }
211
212#define EXPRESSION_CATEGORY(category) \
213 auto GetCategory() const -> uint32_t override \
214 { \
215 return category; \
216 } \
217 \
218 constexpr static auto GetStaticCategory() -> uint32_t \
219 { \
220 return category; \
221 }
222
223} // namespace Oasis
224
229
230#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:59
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 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:138
std::optional< typename T::RetT > Accept(T &visitor) const
Definition Expression.hpp:192
bool Is() const
Definition Expression.hpp:145
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
bool Is() const
Definition Expression.hpp:152
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.
virtual std::any AcceptInternal(Visitor &visitor) const =0
This function serializes the expression object.
Definition Magnitude.hpp:28
Definition Visit.hpp:52
Definition Concepts.hpp:53
Definition Concepts.hpp:58
An expression concept.
Definition Concepts.hpp:27
Definition Concepts.hpp:63
T is_same_v
Definition Add.hpp:11
ExpressionCategory
The category of an expression.
Definition Expression.hpp:44
@ UnExp
Definition Expression.hpp:49
@ None
Definition Expression.hpp:45
@ Commutative
Definition Expression.hpp:47
@ Associative
Definition Expression.hpp:46
@ BinExp
Definition Expression.hpp:48
ExpressionType
The type of an expression.
Definition Expression.hpp:19