OASIS
Open Algebra Software
Loading...
Searching...
No Matches
BoundedUnaryExpression.hpp
Go to the documentation of this file.
1//
2// Created by Matthew McCall on 4/30/24.
3//
4
5#ifndef OASIS_BOUNDEDUNARYEXPRESSION_HPP
6#define OASIS_BOUNDEDUNARYEXPRESSION_HPP
7
8#include <cassert>
9
10#include "Bounds.hpp"
11#include "Expression.hpp"
12#include "Serialization.hpp"
13
14namespace Oasis {
15
16template <template <IExpression, IExpression, IExpression> class DerivedT, IExpression OperandT, IExpression LowerBoundT = Expression, IExpression UpperBoundT = LowerBoundT>
17class BoundedUnaryExpression : public BoundedExpression<LowerBoundT, UpperBoundT> {
18
19 using DerivedSpecialized = DerivedT<OperandT, LowerBoundT, UpperBoundT>;
20 using DerivedGeneralized = DerivedT<Expression, Expression, Expression>;
21
22public:
24
26 {
27 if (other.HasOperand())
28 SetOperand(other.GetOperand());
29 if (other.HasLowerBound())
31 if (other.HasUpperBound())
33 }
34
35 BoundedUnaryExpression(const OperandT& operand, const LowerBoundT& lowerBound, const UpperBoundT& upperBound)
36 {
37 SetOperand(operand);
38 SetLowerBound(lowerBound);
39 SetUpperBound(upperBound);
40 }
41
42 [[nodiscard]] auto Copy() const -> std::unique_ptr<Expression> final
43 {
44 return std::make_unique<DerivedSpecialized>(*static_cast<const DerivedSpecialized*>(this));
45 }
46
47 auto Copy(tf::Subflow&) const -> std::unique_ptr<Expression> final
48 {
49 // TODO: Actually implement
50 return std::make_unique<DerivedSpecialized>(*static_cast<const DerivedSpecialized*>(this));
51 }
52
53 [[nodiscard]] auto Differentiate(const Expression& differentiationVariable) const -> std::unique_ptr<Expression> override
54 {
55 return this->Generalize()->Differentiate(differentiationVariable);
56 }
57
58 [[nodiscard]] auto Equals(const Expression& other) const -> bool final
59 {
60
61 // TODO: Reimplement now that Specialize is gone
62 // if (const auto otherExpression = DerivedSpecialized::Specialize(other); otherExpression != nullptr) {
63 // return HasOperand() == otherExpression->HasOperand()
64 // && this->HasLowerBound() == otherExpression->HasLowerBound()
65 // && this->HasUpperBound() == otherExpression->HasUpperBound();
66 // }
67
68 return false;
69 }
70
71 [[nodiscard]] auto HasOperand() const -> bool
72 {
73 return operand != nullptr;
74 }
75
76 [[nodiscard]] auto GetOperand() const -> const OperandT&
77 {
78 assert(HasOperand() && "Operand is not set.");
79 return *operand;
80 }
81
82 template <typename T>
84 void SetOperand(const T& expr)
85 {
86 if constexpr (std::is_same_v<T, OperandT>) {
87 operand = std::make_unique<OperandT>(expr);
88 } else {
89 operand = expr.Copy();
90 }
91 }
92
93 auto Substitute(const Expression& var, const Expression& val) -> std::unique_ptr<Expression> override
94 {
95 std::unique_ptr<Expression> newOperand = operand->Substitute(var, val);
96 std::unique_ptr<Expression> newLowerBound = this->GetLowerBound().Substitute(var, val);
97 std::unique_ptr<Expression> newUpperBound = this->GetUpperBound().Substitute(var, val);
98 DerivedGeneralized substituted = DerivedGeneralized { operand, newLowerBound, newUpperBound };
99 return substituted.Copy();
100 }
101
103
104 void Serialize(SerializationVisitor& visitor) const override
105 {
106 const auto generalized = this->Generalize();
107 const auto& derivedGeneralized = dynamic_cast<const DerivedGeneralized&>(*generalized);
108 visitor.Serialize(derivedGeneralized);
109 }
110
111private:
113};
114
115}
116
117#endif // OASIS_BOUNDEDUNARYEXPRESSION_HPP
Definition BoundedExpression.hpp:15
auto GetLowerBound() const -> const LowerBoundT &
Definition BoundedExpression.hpp:43
auto GetUpperBound() const -> const UpperBoundT &
Definition BoundedExpression.hpp:49
auto HasLowerBound() const -> bool
Definition BoundedExpression.hpp:33
void SetLowerBound(const T &expr)
Definition BoundedExpression.hpp:57
auto HasUpperBound() const -> bool
Definition BoundedExpression.hpp:38
void SetUpperBound(const T &expr)
Definition BoundedExpression.hpp:68
Definition BoundedUnaryExpression.hpp:17
void SetOperand(const T &expr)
Definition BoundedUnaryExpression.hpp:84
auto Copy() const -> std::unique_ptr< Expression > final
Copies this expression.
Definition BoundedUnaryExpression.hpp:42
auto GetOperand() const -> const OperandT &
Definition BoundedUnaryExpression.hpp:76
auto Substitute(const Expression &var, const Expression &val) -> std::unique_ptr< Expression > override
Definition BoundedUnaryExpression.hpp:93
auto Copy(tf::Subflow &) const -> std::unique_ptr< Expression > final
Definition BoundedUnaryExpression.hpp:47
BoundedUnaryExpression(const BoundedUnaryExpression &other)
Definition BoundedUnaryExpression.hpp:25
auto operator=(const BoundedUnaryExpression &other) -> BoundedUnaryExpression &=default
auto HasOperand() const -> bool
Definition BoundedUnaryExpression.hpp:71
void Serialize(SerializationVisitor &visitor) const override
This function serializes the expression object.
Definition BoundedUnaryExpression.hpp:104
auto Differentiate(const Expression &differentiationVariable) const -> std::unique_ptr< Expression > override
Tries to differentiate this function.
Definition BoundedUnaryExpression.hpp:53
BoundedUnaryExpression(const OperandT &operand, const LowerBoundT &lowerBound, const UpperBoundT &upperBound)
Definition BoundedUnaryExpression.hpp:35
auto Equals(const Expression &other) const -> bool final
Compares this expression to another expression for equality.
Definition BoundedUnaryExpression.hpp:58
An expression.
Definition Expression.hpp:56
virtual auto Generalize() const -> std::unique_ptr< Expression >
Converts this expression to a more general expression.
Definition Expression.cpp:225
Definition Serialization.hpp:50
virtual void Serialize(const Real &real)=0
Checks if type T is same as any of the provided types in U.
Definition Concepts.hpp:48
T is_same_v
Definition Add.hpp:11