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]] 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
139 [[nodiscard]] virtual auto IntegrateWithBounds(const Expression&, const Expression&, const Expression&) -> std::unique_ptr<Expression>;
140
147 template <IExpression T>
148 [[nodiscard]] bool Is() const
149 {
150 return GetType() == T::GetStaticType();
151 }
152
153 template <template <typename> typename T>
155 [[nodiscard]] bool Is() const
156 {
157 return GetType() == T<Expression>::GetStaticType();
158 }
159
160 template <template <typename, typename> typename T>
162 [[nodiscard]] bool Is() const
163 {
164 return GetType() == T<Expression, Expression>::GetStaticType();
165 }
166
171 [[nodiscard]] [[deprecated]] auto Simplify() const -> std::unique_ptr<Expression>;
172
183 [[nodiscard]] virtual auto StructurallyEquivalent(const Expression& other) const -> bool = 0;
184
185 [[nodiscard]] virtual auto Substitute(const Expression& var, const Expression& val) -> std::unique_ptr<Expression> = 0;
186
187 template <IVisitor T>
188 auto Accept(T& visitor) const -> std::expected<typename T::RetT, std::string_view>;
189
190 template <IVisitor T>
191 requires ExpectedWithString<typename T::RetT>
192 auto Accept(T& visitor) const -> typename T::RetT;
193
194 virtual ~Expression() = default;
195
196protected:
202 virtual any AcceptInternal(Visitor& visitor) const = 0;
203};
204
205template <IVisitor T>
206auto Expression::Accept(T& visitor) const -> std::expected<typename T::RetT, std::string_view>
207{
208 try {
209 return boost::any_cast<typename T::RetT>(this->AcceptInternal(visitor));
210 } catch (boost::bad_any_cast& e) {
211 return std::unexpected { e.what() };
212 }
213}
214
215template <IVisitor T>
217auto Expression::Accept(T& visitor) const -> typename T::RetT
218{
219 try {
220 return boost::any_cast<typename T::RetT>(this->AcceptInternal(static_cast<Visitor&>(visitor)));
221 } catch (boost::bad_any_cast& e) {
222 return std::unexpected { e.what() };
223 }
224}
225
226#define EXPRESSION_TYPE(type) \
227 auto GetType() const -> ExpressionType override \
228 { \
229 return ExpressionType::type; \
230 } \
231 \
232 static auto GetStaticType() -> ExpressionType \
233 { \
234 return ExpressionType::type; \
235 }
236
237#define EXPRESSION_CATEGORY(category) \
238 auto GetCategory() const -> uint32_t override \
239 { \
240 return category; \
241 } \
242 \
243 constexpr static auto GetStaticCategory() -> uint32_t \
244 { \
245 return category; \
246 }
247
248} // namespace Oasis
249
254
255#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:267
std::unique_ptr< Oasis::Expression > operator*(const std::unique_ptr< Oasis::Expression > &lhs, const std::unique_ptr< Oasis::Expression > &rhs)
Definition Expression.cpp:291
std::unique_ptr< Oasis::Expression > operator/(const std::unique_ptr< Oasis::Expression > &lhs, const std::unique_ptr< Oasis::Expression > &rhs)
Definition Expression.cpp:303
std::unique_ptr< Oasis::Expression > operator-(const std::unique_ptr< Oasis::Expression > &lhs, const std::unique_ptr< Oasis::Expression > &rhs)
Definition Expression.cpp:279
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:246
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:221
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:52
bool Is() const
Gets whether this expression is of a specific type.
Definition Expression.hpp:148
auto Accept(T &visitor) const -> std::expected< typename T::RetT, std::string_view >
Definition Expression.hpp:206
bool Is() const
Definition Expression.hpp:155
virtual auto GetType() const -> ExpressionType
Gets the type of this expression.
Definition Expression.cpp:236
virtual auto IntegrateWithBounds(const Expression &, const Expression &, const Expression &) -> std::unique_ptr< Expression >
Attempts to integrate this expression using integration rules Then plugs in the bounds of the integra...
Definition Expression.cpp:253
auto Simplify() const -> std::unique_ptr< Expression >
Simplifies this expression.
Definition Expression.cpp:261
auto Differentiate(const Expression &) const -> std::unique_ptr< Expression >
Tries to differentiate this function.
Definition Expression.cpp:226
bool Is() const
Definition Expression.hpp:162
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:241
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)