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"
16#include "Real.hpp"
17#include "RecursiveCast.hpp"
18#include "UnaryExpression.hpp"
19
20namespace Oasis {
21
27template <typename OperandT>
28class Magnitude final : public UnaryExpression<Magnitude, OperandT> {
29public:
30 Magnitude() = default;
31 Magnitude(const Magnitude& other)
32 : UnaryExpression<Magnitude, OperandT>(other)
33 {
34 }
35
36 explicit Magnitude(const OperandT& operand)
37 : UnaryExpression<Magnitude, OperandT>(operand)
38 {
39 }
40
41 [[nodiscard]] auto Simplify() const -> std::unique_ptr<Expression> override
42 {
43 auto simpOp = this->GetOperand().Simplify();
44 if (auto realCase = RecursiveCast<Real>(*simpOp); realCase != nullptr) {
45 double val = realCase->GetValue();
46 return val >= 0.0 ? std::make_unique<Real>(val) : std::make_unique<Real>(-val);
47 }
48 if (auto imgCase = RecursiveCast<Imaginary>(*simpOp); imgCase != nullptr) {
49 return std::make_unique<Real>(1.0);
50 }
51 if (auto mulImgCase = RecursiveCast<Multiply<Expression, Imaginary>>(*simpOp); mulImgCase != nullptr) {
52 return Magnitude<Expression> { mulImgCase->GetMostSigOp() }.Simplify();
53 }
54 if (auto addCase = RecursiveCast<Add<Expression, Imaginary>>(*simpOp); addCase != nullptr) {
55 return Exponent { Add<Expression> { Exponent<Expression> { addCase->GetMostSigOp(), Real { 2 } },
56 Real { 1.0 } },
57 Real { 0.5 } }
58 .Simplify();
59 }
60 if (auto addCase = RecursiveCast<Add<Expression, Multiply<Expression, Imaginary>>>(*simpOp); addCase != nullptr) {
61 return Exponent { Add<Expression> { Exponent<Expression> { addCase->GetMostSigOp(), Real { 2 } },
62 Exponent<Expression> { addCase->GetLeastSigOp().GetMostSigOp(), Real { 2 } } },
63 Real { 0.5 } }
64 .Simplify();
65 }
66 if (auto matrixCase = RecursiveCast<Matrix>(*simpOp); matrixCase != nullptr) {
67 double sum = 0;
68 for (size_t i = 0; i < matrixCase->GetRows(); i++) {
69 for (size_t j = 0; j < matrixCase->GetCols(); j++) {
70 sum += pow(matrixCase->GetMatrix()(i, j), 2);
71 }
72 }
73 return Exponent { Real { sum }, Real { 0.5 } }.Simplify();
74 }
75
76 return this->Generalize();
77 }
78
79 [[nodiscard]] auto Differentiate(const Expression& var) const -> std::unique_ptr<Expression> override
80 {
81 // TODO: Implement
82
83 const std::unique_ptr<Expression> operandDerivative = this->GetOperand().Differentiate(var);
85 *operandDerivative
86 }
87 .Simplify();
88 }
89
90 [[nodiscard]] auto Integrate(const Expression& integrationVar) const -> std::unique_ptr<Expression> override
91 {
92 // TODO: Implement
93 const std::unique_ptr<Expression> operandDerivative = this->GetOperand().Integrate(integrationVar);
95 *operandDerivative
96 }
97 .Simplify();
98 }
99
102};
103
104} // Oasis
105
106#endif // OASIS_MAGNITUDE_HPP
#define EXPRESSION_CATEGORY(category)
Definition Expression.hpp:192
#define EXPRESSION_TYPE(type)
Definition Expression.hpp:181
The Add expression adds two expressions together.
Definition Add.hpp:42
auto GetLeastSigOp() const -> const LeastSigOpT &
Gets the least significant operand of this expression.
Definition BinaryExpression.hpp:286
auto GetMostSigOp() const -> const MostSigOpT &
Gets the most significant operand of this expression.
Definition BinaryExpression.hpp:276
The exponent expression creates an exponent two expressions.
Definition Exponent.hpp:43
An expression.
Definition Expression.hpp:56
virtual auto Simplify() const -> std::unique_ptr< Expression >
Simplifies this expression.
Definition Expression.cpp:244
Definition Magnitude.hpp:28
Magnitude(const Magnitude &other)
Definition Magnitude.hpp:31
Magnitude(const OperandT &operand)
Definition Magnitude.hpp:36
auto Differentiate(const Expression &var) const -> std::unique_ptr< Expression > override
Tries to differentiate this function.
Definition Magnitude.hpp:79
Magnitude()=default
auto Integrate(const Expression &integrationVar) const -> std::unique_ptr< Expression > override
Attempts to integrate this expression using integration rules.
Definition Magnitude.hpp:90
auto Simplify() const -> std::unique_ptr< Expression > override
Simplifies this expression.
Definition Magnitude.hpp:41
The Multiply expression multiplies two expressions.
Definition Multiply.hpp:40
A real number.
Definition Real.hpp:15
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:46