OASIS
Open Algebra Software
Loading...
Searching...
No Matches
Magnitude.hpp
Go to the documentation of this file.
1//
2// Created by Andrew Nazareth on 6/28/24.
3//
4
5#ifndef OASIS_MAGNITUDE_HPP
6#define OASIS_MAGNITUDE_HPP
7
8#include <memory>
9
10#include "Add.hpp"
11#include "Exponent.hpp"
12#include "Expression.hpp"
13#include "Imaginary.hpp"
14#include "Matrix.hpp"
15#include "Multiply.hpp"
17#include "Real.hpp"
18#include "RecursiveCast.hpp"
19#include "UnaryExpression.hpp"
20
21namespace Oasis {
22
28template <typename OperandT>
29class Magnitude final : public UnaryExpression<Magnitude, OperandT> {
30public:
31 Magnitude() = default;
32 Magnitude(const Magnitude& other)
33 : UnaryExpression<Magnitude, OperandT>(other)
34 {
35 }
36
37 explicit Magnitude(const OperandT& operand)
38 : UnaryExpression<Magnitude, OperandT>(operand)
39 {
40 }
41
42 [[deprecated]] [[nodiscard]] auto Simplify() const -> std::unique_ptr<Expression> override
43 {
44 SimplifyVisitor simplifyVisitor {};
45 auto simpOp = this->GetOperand().Simplify();
46 if (auto realCase = RecursiveCast<Real>(*simpOp); realCase != nullptr) {
47 double val = realCase->GetValue();
48 return val >= 0.0 ? std::make_unique<Real>(val) : std::make_unique<Real>(-val);
49 }
50 if (auto imgCase = RecursiveCast<Imaginary>(*simpOp); imgCase != nullptr) {
51 return std::make_unique<Real>(1.0);
52 }
53 if (auto mulImgCase = RecursiveCast<Multiply<Expression, Imaginary>>(*simpOp); mulImgCase != nullptr) {
54 auto e = Magnitude<Expression> { mulImgCase->GetMostSigOp() };
55 auto s = e.Accept(simplifyVisitor);
56 if (!s) {
57 return e.Generalize();
58 }
59 return std::move(s).value();
60 }
61 if (auto addCase = RecursiveCast<Add<Expression, Imaginary>>(*simpOp); addCase != nullptr) {
62 return Exponent { Add<Expression> { Exponent<Expression> { addCase->GetMostSigOp(), Real { 2 } },
63 Real { 1.0 } },
64 Real { 0.5 } }
65 .Simplify();
66 }
67 if (auto addCase = RecursiveCast<Add<Expression, Multiply<Expression, Imaginary>>>(*simpOp); addCase != nullptr) {
68 return Exponent { Add<Expression> { Exponent<Expression> { addCase->GetMostSigOp(), Real { 2 } },
69 Exponent<Expression> { addCase->GetLeastSigOp().GetMostSigOp(), Real { 2 } } },
70 Real { 0.5 } }
71 .Simplify();
72 }
73 if (auto matrixCase = RecursiveCast<Matrix>(*simpOp); matrixCase != nullptr) {
74 double sum = 0;
75 for (size_t i = 0; i < matrixCase->GetRows(); i++) {
76 for (size_t j = 0; j < matrixCase->GetCols(); j++) {
77 sum += pow(matrixCase->GetMatrix()(i, j), 2);
78 }
79 }
80 return Exponent { Real { sum }, Real { 0.5 } }.Simplify();
81 }
82
83 return this->Generalize();
84 }
85
86 [[nodiscard]] auto Differentiate(const Expression& var) const -> std::unique_ptr<Expression> override
87 {
88 // TODO: Implement
89 Oasis::SimplifyVisitor simplifyVisitor {};
90
91 const std::unique_ptr<Expression> operandDerivative = this->GetOperand().Differentiate(var);
93 *operandDerivative
94 }
95 .Generalize();
96 }
97
98 [[nodiscard]] auto Integrate(const Expression& integrationVar) const -> std::unique_ptr<Expression> override
99 {
100 // TODO: Implement
101 const std::unique_ptr<Expression> operandDerivative = this->GetOperand().Integrate(integrationVar);
102 return Magnitude<Expression> {
103 *operandDerivative
104 }
105 .Generalize();
106 }
107
110};
111
112} // Oasis
113
114#endif // OASIS_MAGNITUDE_HPP
#define EXPRESSION_CATEGORY(category)
Definition Expression.hpp:231
#define EXPRESSION_TYPE(type)
Definition Expression.hpp:220
The Add expression adds two expressions together.
Definition Add.hpp:141
auto GetLeastSigOp() const -> const LeastSigOpT &
Gets the least significant operand of this expression.
Definition BinaryExpression.hpp:296
auto GetMostSigOp() const -> const MostSigOpT &
Gets the most significant operand of this expression.
Definition BinaryExpression.hpp:286
The exponent expression creates an exponent two expressions.
Definition Exponent.hpp:86
An expression.
Definition Expression.hpp:63
auto Accept(T &visitor) const -> std::expected< typename T::RetT, std::string_view >
Definition Expression.hpp:200
virtual auto Simplify() const -> std::unique_ptr< Expression >
Simplifies this expression.
Definition Expression.cpp:244
Definition Magnitude.hpp:29
Magnitude(const Magnitude &other)
Definition Magnitude.hpp:32
Magnitude(const OperandT &operand)
Definition Magnitude.hpp:37
auto Differentiate(const Expression &var) const -> std::unique_ptr< Expression > override
Tries to differentiate this function.
Definition Magnitude.hpp:86
Magnitude()=default
auto Integrate(const Expression &integrationVar) const -> std::unique_ptr< Expression > override
Attempts to integrate this expression using integration rules.
Definition Magnitude.hpp:98
auto Simplify() const -> std::unique_ptr< Expression > override
Simplifies this expression.
Definition Magnitude.hpp:42
The Multiply expression multiplies two expressions.
Definition Multiply.hpp:126
A real number.
Definition Real.hpp:15
Definition SimplifyVisitor.hpp:25
Definition UnaryExpression.hpp:14
auto Generalize() const -> std::unique_ptr< Expression > final
Definition UnaryExpression.hpp:53
auto GetOperand() const -> const OperandT &
Definition UnaryExpression.hpp:62
T is_same_v
Definition Add.hpp:11
auto RecursiveCast(const Expression &other) -> std::unique_ptr< T >
Definition RecursiveCast.hpp:14
@ UnExp
Definition Expression.hpp:53