complex_matrix.cpp
2.71 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
100
101
102
103
extern "C" {
#include <assert.h>
#include <stdlib.h>
}
#include <poincare/complex_matrix.h>
#include <poincare/complex.h>
#include "layout/grid_layout.h"
#include "layout/bracket_layout.h"
#include <cmath>
#include <float.h>
#include <string.h>
namespace Poincare {
template<typename T>
ComplexMatrix<T>::ComplexMatrix(const Complex<T> * complexes, int numberOfRows, int numberOfColumns) :
m_numberOfRows(numberOfRows),
m_numberOfColumns(numberOfColumns)
{
assert(complexes != nullptr);
m_values = new Complex<T>[numberOfRows*numberOfColumns];
for (int i = 0; i < numberOfRows*numberOfColumns; i++) {
m_values[i] = complexes[i];
}
}
template<typename T>
ComplexMatrix<T>::~ComplexMatrix() {
delete[] m_values;
}
template<typename T>
T ComplexMatrix<T>::toScalar() const {
if (m_numberOfRows != 1 || m_numberOfColumns != 1) {
return NAN;
}
if (m_values[0].b() != 0) {
return NAN;
}
return m_values[0].a();
}
template<typename T>
int ComplexMatrix<T>::numberOfRows() const {
return m_numberOfRows;
}
template<typename T>
int ComplexMatrix<T>::numberOfColumns() const {
return m_numberOfColumns;
}
template<typename T>
const Complex<T> * ComplexMatrix<T>::complexOperand(int i) const {
return &m_values[i];
}
template<typename T>
ComplexMatrix<T> * ComplexMatrix<T>::clone() const {
return new ComplexMatrix<T>(m_values, m_numberOfRows, m_numberOfColumns);
}
template<typename T>
ComplexMatrix<T> * ComplexMatrix<T>::cloneWithDifferentOperands(Expression** newOperands,
int numberOfOperands, bool cloneOperands) const {
assert(newOperands != nullptr);
return new ComplexMatrix((Complex<T> *)newOperands[0], m_numberOfRows, m_numberOfColumns);
}
template<typename T>
Evaluation<T> * ComplexMatrix<T>::createIdentity(int dim) {
Complex<T> * operands = new Complex<T> [dim*dim];
for (int i = 0; i < dim; i++) {
for (int j = 0; j < dim; j++) {
if (i == j) {
operands[i*dim+j] = Complex<T>::Float(1.0);
} else {
operands[i*dim+j] = Complex<T>::Float(0.0);
}
}
}
Evaluation<T> * matrix = new ComplexMatrix<T>(operands, dim, dim);
delete [] operands;
return matrix;
}
template<typename T>
template <class U>
Evaluation<U> * ComplexMatrix<T>::templatedEvaluate(Context& context, Expression::AngleUnit angleUnit) const {
Complex<U> * values = new Complex<U>[m_numberOfRows*m_numberOfColumns];
for (int i = 0; i < m_numberOfRows*m_numberOfColumns; i++) {
values[i] = Complex<U>::Cartesian(m_values[i].a(), m_values[i].b());
}
Evaluation<U> * result = new ComplexMatrix<U>(values, m_numberOfRows, m_numberOfColumns);
delete [] values;
return result;
}
template class Poincare::ComplexMatrix<float>;
template class Poincare::ComplexMatrix<double>;
}