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 <cstdint>
5#include <expected>
6#include <memory>
7#include <vector>
8
9#include <boost/any/unique_any.hpp>
10
11#include "Concepts.hpp"
12
13namespace Oasis {
14
15using any = boost::anys::unique_any;
16
17class Visitor;
18
22enum class ExpressionType {
23 None,
24 Real,
27 Add,
30 Divide,
32 Log,
34 Limit,
36 Negate,
37 Sqrt,
38 Matrix,
39 Pi,
42 Sine
43};
44
48enum ExpressionCategory : uint32_t {
49 None = 0,
51 Commutative = 1 << 1,
52 BinExp = 1 << 2,
53 UnExp = 1 << 3,
54};
55
64public:
69 [[nodiscard]] virtual auto Copy() const -> std::unique_ptr<Expression> = 0;
70
75 [[nodiscard]] virtual auto Differentiate(const Expression&) const -> std::unique_ptr<Expression>;
76
88 [[nodiscard]] virtual auto Equals(const Expression& other) const -> bool = 0;
89
95 auto FindZeros() const -> std::vector<std::unique_ptr<Expression>>;
96
101 [[nodiscard]] virtual auto GetCategory() const -> uint32_t;
102
107 [[nodiscard]] virtual auto GetType() const -> ExpressionType;
108
119 [[nodiscard]] virtual auto Generalize() const -> std::unique_ptr<Expression>;
120
126 [[nodiscard]] virtual auto Integrate(const Expression&) const -> std::unique_ptr<Expression>;
127
133 [[nodiscard]] virtual auto IntegrateWithBounds(const Expression&, const Expression&, const Expression&) -> std::unique_ptr<Expression>;
134
141 template <IExpression T>
142 [[nodiscard]] bool Is() const
143 {
144 return GetType() == T::GetStaticType();
145 }
146
147 template <template <typename> typename T>
149 [[nodiscard]] bool Is() const
150 {
151 return GetType() == T<Expression>::GetStaticType();
152 }
153
154 template <template <typename, typename> typename T>
156 [[nodiscard]] bool Is() const
157 {
158 return GetType() == T<Expression, Expression>::GetStaticType();
159 }
160
165 [[nodiscard]] [[deprecated]] auto Simplify() const -> std::unique_ptr<Expression>;
166
177 [[nodiscard]] virtual auto StructurallyEquivalent(const Expression& other) const -> bool = 0;
178
179 [[nodiscard]] virtual auto Substitute(const Expression& var, const Expression& val) -> std::unique_ptr<Expression> = 0;
180
181 template <IVisitor T>
182 auto Accept(T& visitor) const -> std::expected<typename T::RetT, std::string_view>;
183
184 template <IVisitor T>
185 requires ExpectedWithString<typename T::RetT>
186 auto Accept(T& visitor) const -> typename T::RetT;
187
188 virtual ~Expression() = default;
189
190protected:
196 virtual any AcceptInternal(Visitor& visitor) const = 0;
197};
198
199template <IVisitor T>
200auto Expression::Accept(T& visitor) const -> std::expected<typename T::RetT, std::string_view>
201{
202 try {
203 return boost::any_cast<typename T::RetT>(this->AcceptInternal(visitor));
204 } catch (boost::bad_any_cast& e) {
205 return std::unexpected { e.what() };
206 }
207}
208
209template <IVisitor T>
211auto Expression::Accept(T& visitor) const -> typename T::RetT
212{
213 try {
214 return boost::any_cast<typename T::RetT>(this->AcceptInternal(static_cast<Visitor&>(visitor)));
215 } catch (boost::bad_any_cast& e) {
216 return std::unexpected { e.what() };
217 }
218}
219
220#define EXPRESSION_TYPE(type) \
221 auto GetType() const -> ExpressionType override \
222 { \
223 return ExpressionType::type; \
224 } \
225 \
226 static auto GetStaticType() -> ExpressionType \
227 { \
228 return ExpressionType::type; \
229 }
230
231#define EXPRESSION_CATEGORY(category) \
232 auto GetCategory() const -> uint32_t override \
233 { \
234 return category; \
235 } \
236 \
237 constexpr static auto GetStaticCategory() -> uint32_t \
238 { \
239 return category; \
240 }
241
242} // namespace Oasis
243
248
249#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:260
std::unique_ptr< Oasis::Expression > operator*(const std::unique_ptr< Oasis::Expression > &lhs, const std::unique_ptr< Oasis::Expression > &rhs)
Definition Expression.cpp:284
std::unique_ptr< Oasis::Expression > operator/(const std::unique_ptr< Oasis::Expression > &lhs, const std::unique_ptr< Oasis::Expression > &rhs)
Definition Expression.cpp:296
std::unique_ptr< Oasis::Expression > operator-(const std::unique_ptr< Oasis::Expression > &lhs, const std::unique_ptr< Oasis::Expression > &rhs)
Definition Expression.cpp:272
An expression.
Definition Expression.hpp:63
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:239
virtual any AcceptInternal(Visitor &visitor) const =0
This function serializes the expression object.
virtual auto GetCategory() const -> uint32_t
Gets the category of this expression.
Definition Expression.cpp:219
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:50
bool Is() const
Gets whether this expression is of a specific type.
Definition Expression.hpp:142
auto Accept(T &visitor) const -> std::expected< typename T::RetT, std::string_view >
Definition Expression.hpp:200
bool Is() const
Definition Expression.hpp:149
virtual auto GetType() const -> ExpressionType
Gets the type of this expression.
Definition Expression.cpp:229
virtual auto IntegrateWithBounds(const Expression &, const Expression &, const Expression &) -> std::unique_ptr< Expression >
Attempts to integrate this expression using integration rules.
Definition Expression.cpp:246
auto Simplify() const -> std::unique_ptr< Expression >
Simplifies this expression.
Definition Expression.cpp:254
virtual auto Differentiate(const Expression &) const -> std::unique_ptr< Expression >
Tries to differentiate this function.
Definition Expression.cpp:224
bool Is() const
Definition Expression.hpp:156
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:234
virtual auto Equals(const Expression &other) const -> bool=0
Compares this expression to another expression for equality.
Definition FwdDecls.hpp:44
Definition Visit.hpp:13
Definition Concepts.hpp:56
Definition Concepts.hpp:61
Definition Concepts.hpp:72
An expression concept.
Definition Concepts.hpp:30
Definition Concepts.hpp:66
Definition Add.hpp:11
boost::anys::unique_any any
Definition Expression.hpp:15
ExpressionCategory
The category of an expression.
Definition Expression.hpp:48
@ UnExp
Definition Expression.hpp:53
@ None
Definition Expression.hpp:49
@ Commutative
Definition Expression.hpp:51
@ Associative
Definition Expression.hpp:50
@ BinExp
Definition Expression.hpp:52
ExpressionType
The type of an expression.
Definition Expression.hpp:22
T unexpected(T... args)