forked from open-algebra/Oasis
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa0063c
commit dc52199
Showing
8 changed files
with
226 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// Created by Matthew McCall on 3/29/24. | ||
// | ||
|
||
#ifndef NEGATE_HPP | ||
#define NEGATE_HPP | ||
|
||
#include "Multiply.hpp" | ||
#include "fmt/core.h" | ||
|
||
#include "UnaryExpression.hpp" | ||
|
||
namespace Oasis { | ||
|
||
template <typename OperandT> | ||
class Negate final : public UnaryExpression<Negate, OperandT> { | ||
public: | ||
Negate() = default; | ||
Negate(const Negate& other) | ||
: UnaryExpression<Negate, OperandT>(other) | ||
{ | ||
} | ||
|
||
explicit Negate(const OperandT& operand) | ||
: UnaryExpression<Negate, OperandT>(operand) | ||
{ | ||
} | ||
|
||
[[nodiscard]] auto Simplify() const -> std::unique_ptr<Expression> override | ||
{ | ||
return Multiply { | ||
Real { -1.0 }, | ||
this->GetOperand() | ||
} | ||
.Simplify(); | ||
} | ||
|
||
auto Simplify(tf::Subflow& subflow) const -> std::unique_ptr<Expression> override | ||
{ | ||
return Multiply { | ||
Real { -1.0 }, | ||
this->GetOperand() | ||
} | ||
.Simplify(subflow); | ||
} | ||
|
||
[[nodiscard]] auto ToString() const -> std::string override | ||
{ | ||
return fmt::format("-({})", this->GetOperand().ToString()); | ||
} | ||
|
||
IMPL_SPECIALIZE_UNARYEXPR(Negate, OperandT) | ||
|
||
EXPRESSION_TYPE(Negate) | ||
EXPRESSION_CATEGORY(None) | ||
}; | ||
|
||
} // Oasis | ||
|
||
#endif // NEGATE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// | ||
// Created by Matthew McCall on 3/29/24. | ||
// | ||
|
||
#ifndef UNARYEXPRESSION_HPP | ||
#define UNARYEXPRESSION_HPP | ||
|
||
#include "Expression.hpp" | ||
|
||
namespace Oasis { | ||
|
||
template <template <IExpression> class DerivedT, IExpression OperandT> | ||
class UnaryExpression : public Expression { | ||
|
||
using DerivedSpecialized = DerivedT<OperandT>; | ||
using DerivedGeneralized = DerivedT<Expression>; | ||
|
||
public: | ||
UnaryExpression() = default; | ||
|
||
UnaryExpression(const UnaryExpression& other) | ||
: Expression(other) | ||
{ | ||
if (other.HasOperand()) { | ||
SetOperand(other.GetOperand()); | ||
} | ||
} | ||
|
||
explicit UnaryExpression(const OperandT& operand) | ||
{ | ||
SetOperand(operand); | ||
} | ||
|
||
[[nodiscard]] auto Copy() const -> std::unique_ptr<Expression> final | ||
{ | ||
return std::make_unique<DerivedSpecialized>(*static_cast<const DerivedSpecialized*>(this)); | ||
} | ||
|
||
auto Copy(tf::Subflow&) const -> std::unique_ptr<Expression> final | ||
{ | ||
return std::make_unique<DerivedSpecialized>(*static_cast<const DerivedSpecialized*>(this)); | ||
} | ||
|
||
[[nodiscard]] auto Equals(const Expression& other) const -> bool final | ||
{ | ||
if (!other.Is<DerivedSpecialized>()) { | ||
return false; | ||
} | ||
|
||
// generalize | ||
const auto otherGeneralized = other.Generalize(); | ||
const auto& otherUnaryGeneralized = dynamic_cast<const DerivedGeneralized&>(*otherGeneralized); | ||
|
||
return op->Equals(otherUnaryGeneralized.GetOperand()); | ||
} | ||
|
||
[[nodiscard]] auto Generalize() const -> std::unique_ptr<Expression> final | ||
{ | ||
return std::make_unique<DerivedGeneralized>(*this); | ||
} | ||
|
||
auto Generalize(tf::Subflow& subflow) const -> std::unique_ptr<Expression> final | ||
{ | ||
return DerivedGeneralized { *this }.Copy(subflow); | ||
} | ||
|
||
auto GetOperand() const -> const OperandT& | ||
{ | ||
return *op; | ||
} | ||
|
||
auto HasOperand() const -> bool | ||
{ | ||
return op != nullptr; | ||
} | ||
|
||
[[nodiscard]] auto StructurallyEquivalent(const Expression& other) const -> bool final | ||
{ | ||
return this->GetType() == other.GetType(); | ||
} | ||
|
||
auto StructurallyEquivalent(const Expression& other, tf::Subflow&) const -> bool final | ||
{ | ||
return this->GetType() == other.GetType(); | ||
} | ||
|
||
auto SetOperand(const OperandT& operand) -> void | ||
{ | ||
if constexpr (std::same_as<OperandT, Expression>) { | ||
this->op = operand.Copy(); | ||
} else { | ||
this->op = std::make_unique<OperandT>(operand); | ||
} | ||
} | ||
|
||
protected: | ||
std::unique_ptr<OperandT> op; | ||
}; | ||
|
||
#define IMPL_SPECIALIZE_UNARYEXPR(DerivedT, OperandT) \ | ||
static auto Specialize(const Expression& other) -> std::unique_ptr<DerivedT> \ | ||
{ \ | ||
if (!other.Is<DerivedT>()) { \ | ||
return nullptr; \ | ||
} \ | ||
\ | ||
auto specialized = std::make_unique<DerivedT<OperandT>>(); \ | ||
std::unique_ptr<Expression> otherGeneralized = other.Generalize(); \ | ||
const auto& otherUnary = dynamic_cast<const DerivedT<Expression>&>(*otherGeneralized); \ | ||
\ | ||
if (auto operand = OperandT::Specialize(otherUnary.GetOperand()); operand != nullptr) { \ | ||
specialized->op = std::move(operand); \ | ||
return specialized; \ | ||
} \ | ||
\ | ||
return nullptr; \ | ||
} \ | ||
\ | ||
static auto Specialize(const Expression& other, tf::Subflow&) -> std::unique_ptr<DerivedT> \ | ||
{ \ | ||
/* TODO: Actually implement */ \ | ||
return DerivedT<OperandT>::Specialize(other); \ | ||
} | ||
|
||
} // Oasis | ||
|
||
#endif // UNARYEXPRESSION_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// | ||
// Created by Matthew McCall on 3/29/24. | ||
// | ||
|
||
#include "Oasis/Negate.hpp" | ||
|
||
namespace Oasis { | ||
} // Oasis |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// Created by Matthew McCall on 4/5/24. | ||
// | ||
|
||
#include "catch2/catch_test_macros.hpp" | ||
|
||
#include <Oasis/Negate.hpp> | ||
|
||
TEST_CASE("Negate", "[Negate]") | ||
{ | ||
const Oasis::Negate negativeOne { | ||
Oasis::Real { 1.0 } | ||
}; | ||
|
||
const auto simplified = negativeOne.Simplify(); | ||
|
||
const Oasis::Real expected { -1.0 }; | ||
REQUIRE(simplified->Equals(expected)); | ||
} |