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 <expected>
5#include <memory>
6#include <vector>
7
8#include <boost/any/unique_any.hpp>
9
10#include "Concepts.hpp"
11
12namespace Oasis {
13
14using any = boost::anys::unique_any;
15
16class Visitor;
17
21enum class ExpressionType {
22 None,
23 Real,
26 Add,
29 Divide,
31 Log,
33 Limit,
35 Negate,
36 Sqrt,
37 Matrix,
38 Pi,
41 Sine
42};
43
47enum ExpressionCategory : uint32_t {
48 None = 0,
50 Commutative = 1 << 1,
51 BinExp = 1 << 2,
52 UnExp = 1 << 3,
53};
54
63public:
68 [[nodiscard]] virtual auto Copy() const -> std::unique_ptr<Expression> = 0;
69
74 [[nodiscard]] auto Differentiate(const Expression&) const -> std::unique_ptr<Expression>;
75
87 [[nodiscard]] virtual auto Equals(const Expression& other) const -> bool = 0;
88
101 auto FindZeros() const -> std::vector<std::unique_ptr<Expression>>;
102
118 [[nodiscard]] auto ApproximateZeros(const Expression& variable, const Expression& guess, int iterations) const -> std::unique_ptr<Expression>;
119
124 [[nodiscard]] virtual auto GetCategory() const -> uint32_t;
125
130 [[nodiscard]] virtual auto GetType() const -> ExpressionType;
131
142 [[nodiscard]] virtual auto Generalize() const -> std::unique_ptr<Expression>;
143
149 [[nodiscard]] virtual auto Integrate(const Expression&) const -> std::unique_ptr<Expression>;
150
162 [[nodiscard]] virtual auto IntegrateWithBounds(const Expression&, const Expression&, const Expression&) -> std::unique_ptr<Expression>;
163
170 template <IExpression T>
171 [[nodiscard]] bool Is() const
172 {
173 return GetType() == T::GetStaticType();
174 }
175
176 template <template <typename> typename T>
178 [[nodiscard]] bool Is() const
179 {
180 return GetType() == T<Expression>::GetStaticType();
181 }
182
183 template <template <typename, typename> typename T>
185 [[nodiscard]] bool Is() const
186 {
187 return GetType() == T<Expression, Expression>::GetStaticType();
188 }
189
194 [[nodiscard]] [[deprecated]] auto Simplify() const -> std::unique_ptr<Expression>;
195
206 [[nodiscard]] virtual auto StructurallyEquivalent(const Expression& other) const -> bool = 0;
207
208 [[nodiscard]] virtual auto Substitute(const Expression& var, const Expression& val) -> std::unique_ptr<Expression> = 0;
209
210 template <IVisitor T>
211 auto Accept(T& visitor) const -> std::expected<typename T::RetT, std::string_view>;
212
213 template <IVisitor T>
214 requires ExpectedWithString<typename T::RetT>
215 auto Accept(T& visitor) const -> typename T::RetT;
216
217 virtual ~Expression() = default;
218
219protected:
225 virtual any AcceptInternal(Visitor& visitor) const = 0;
226};
227
228template <IVisitor T>
229auto Expression::Accept(T& visitor) const -> std::expected<typename T::RetT, std::string_view>
230{
231 try {
232 return boost::any_cast<typename T::RetT>(this->AcceptInternal(visitor));
233 } catch (boost::bad_any_cast& e) {
234 return std::unexpected { e.what() };
235 }
236}
237
238template <IVisitor T>
240auto Expression::Accept(T& visitor) const -> typename T::RetT
241{
242 try {
243 return boost::any_cast<typename T::RetT>(this->AcceptInternal(static_cast<Visitor&>(visitor)));
244 } catch (boost::bad_any_cast& e) {
245 return std::unexpected { e.what() };
246 }
247}
248
249#define EXPRESSION_TYPE(type) \
250 auto GetType() const -> ExpressionType override \
251 { \
252 return ExpressionType::type; \
253 } \
254 \
255 static auto GetStaticType() -> ExpressionType \
256 { \
257 return ExpressionType::type; \
258 }
259
260#define EXPRESSION_CATEGORY(category) \
261 auto GetCategory() const -> uint32_t override \
262 { \
263 return category; \
264 } \
265 \
266 constexpr static auto GetStaticCategory() -> uint32_t \
267 { \
268 return category; \
269 }
270
271} // namespace Oasis
272
277
278#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:355
std::unique_ptr< Oasis::Expression > operator*(const std::unique_ptr< Oasis::Expression > &lhs, const std::unique_ptr< Oasis::Expression > &rhs)
Definition Expression.cpp:379
std::unique_ptr< Oasis::Expression > operator/(const std::unique_ptr< Oasis::Expression > &lhs, const std::unique_ptr< Oasis::Expression > &rhs)
Definition Expression.cpp:391
std::unique_ptr< Oasis::Expression > operator-(const std::unique_ptr< Oasis::Expression > &lhs, const std::unique_ptr< Oasis::Expression > &rhs)
Definition Expression.cpp:367
An expression.
Definition Expression.hpp:62
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:334
virtual any AcceptInternal(Visitor &visitor) const =0
This function serializes the expression object.
auto ApproximateZeros(const Expression &variable, const Expression &guess, int iterations) const -> std::unique_ptr< Expression >
Approximates a zero of this expression using Newton's method.
Definition Expression.cpp:221
virtual auto GetCategory() const -> uint32_t
Gets the category of this expression.
Definition Expression.cpp:309
auto FindZeros() const -> std::vector< std::unique_ptr< Expression > >
Finds the exact zeros of a polynomial when they can be derived symbolically.
Definition Expression.cpp:52
bool Is() const
Gets whether this expression is of a specific type.
Definition Expression.hpp:171
auto Accept(T &visitor) const -> std::expected< typename T::RetT, std::string_view >
Definition Expression.hpp:229
bool Is() const
Definition Expression.hpp:178
virtual auto GetType() const -> ExpressionType
Gets the type of this expression.
Definition Expression.cpp:324
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:341
auto Simplify() const -> std::unique_ptr< Expression >
Simplifies this expression.
Definition Expression.cpp:349
auto Differentiate(const Expression &) const -> std::unique_ptr< Expression >
Tries to differentiate this function.
Definition Expression.cpp:314
bool Is() const
Definition Expression.hpp:185
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:329
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:14
ExpressionCategory
The category of an expression.
Definition Expression.hpp:47
@ UnExp
Definition Expression.hpp:52
@ None
Definition Expression.hpp:48
@ Commutative
Definition Expression.hpp:50
@ Associative
Definition Expression.hpp:49
@ BinExp
Definition Expression.hpp:51
ExpressionType
The type of an expression.
Definition Expression.hpp:21
T unexpected(T... args)