opposite.cpp
3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <poincare/opposite.h>
#include <poincare/complex_matrix.h>
#include <poincare/complex.h>
extern "C" {
#include <assert.h>
#include <stdlib.h>
}
#include <cmath>
#include "layout/horizontal_layout.h"
#include "layout/parenthesis_layout.h"
#include "layout/string_layout.h"
namespace Poincare {
Opposite::Opposite(Expression * operand, bool cloneOperands) {
assert(operand != nullptr);
if (cloneOperands) {
m_operand = operand->clone();
} else {
m_operand = operand;
}
}
Opposite::~Opposite() {
delete m_operand;
}
bool Opposite::hasValidNumberOfArguments() const {
return m_operand->hasValidNumberOfArguments();
}
const Expression * Opposite::operand(int i) const {
assert(i == 0);
return m_operand;
}
int Opposite::numberOfOperands() const {
return 1;
}
Expression * Opposite::clone() const {
return this->cloneWithDifferentOperands((Expression**)&m_operand, 1, true);
}
template<typename T>
Complex<T> Opposite::compute(const Complex<T> c) {
return Complex<T>::Cartesian(-c.a(), -c.b());
}
template<typename T>
Evaluation<T> * Opposite::computeOnMatrix(Evaluation<T> * m) {
Complex<T> * operands = new Complex<T>[m->numberOfRows() * m->numberOfColumns()];
for (int i = 0; i < m->numberOfRows() * m->numberOfColumns(); i++) {
Complex<T> entry = *(m->complexOperand(i));
operands[i] = Complex<T>::Cartesian(-entry.a(), -entry.b());
}
Evaluation<T> * matrix = new ComplexMatrix<T>(operands, m->numberOfRows(), m->numberOfColumns());
delete[] operands;
return matrix;
}
template<typename T>
Evaluation<T> * Opposite::templatedEvaluate(Context& context, AngleUnit angleUnit) const {
Evaluation<T> * operandEvalutation = m_operand->evaluate<T>(context, angleUnit);
Evaluation<T> * result = nullptr;
if (operandEvalutation->numberOfRows() == 1 && operandEvalutation->numberOfColumns() == 1) {
result = new Complex<T>(compute(*(operandEvalutation->complexOperand(0))));
} else {
result = computeOnMatrix(operandEvalutation);
}
delete operandEvalutation;
return result;
}
ExpressionLayout * Opposite::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const {
assert(floatDisplayMode != FloatDisplayMode::Default);
assert(complexFormat != ComplexFormat::Default);
ExpressionLayout * children_layouts[2];
char string[2] = {'-', '\0'};
children_layouts[0] = new StringLayout(string, 1);
children_layouts[1] = m_operand->type() == Type::Opposite ? new ParenthesisLayout(m_operand->createLayout(floatDisplayMode, complexFormat)) : m_operand->createLayout(floatDisplayMode, complexFormat);
return new HorizontalLayout(children_layouts, 2);
}
Expression::Type Opposite::type() const {
return Expression::Type::Opposite;
}
Expression * Opposite::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) const {
assert(newOperands != nullptr);
assert(numberOfOperands == 1);
return new Opposite(newOperands[0], cloneOperands);
}
}
template Poincare::Complex<float> Poincare::Opposite::compute<float>(Poincare::Complex<float>);
template Poincare::Complex<double> Poincare::Opposite::compute<double>(Poincare::Complex<double>);