6663b6c9
adorian
projet complet av...
|
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
extern "C" {
#include <assert.h>
#include <float.h>
#include <stdlib.h>
}
#include <poincare/evaluation.h>
#include <poincare/division.h>
#include <poincare/matrix.h>
#include <poincare/expression.h>
#include <poincare/undefined.h>
#include <poincare/decimal.h>
#include <poincare/multiplication.h>
#include <poincare/opposite.h>
#include <poincare/addition.h>
#include <poincare/subtraction.h>
#include <poincare/matrix.h>
#include <poincare/power.h>
#include <poincare/symbol.h>
#include <ion.h>
#include <cmath>
namespace Poincare {
template<typename T>
T Complex<T>::toScalar() const {
if (this->imag() == 0.0) {
return this->real();
}
return NAN;
}
template <typename T>
static Expression * CreateDecimal(T f) {
if (std::isnan(f) || std::isinf(f)) {
return new Undefined();
}
return new Decimal(f);
}
template<typename T>
Expression * Complex<T>::complexToExpression(Expression::ComplexFormat complexFormat) const {
if (std::isnan(this->real()) || std::isnan(this->imag()) || std::isinf(this->real()) || std::isinf(this->imag())) {
return new Poincare::Undefined();
}
switch (complexFormat) {
case Expression::ComplexFormat::Cartesian:
{
Expression * real = nullptr;
Expression * imag = nullptr;
if (this->real() != 0 || this->imag() == 0) {
real = CreateDecimal(this->real());
}
if (this->imag() != 0) {
if (this->imag() == 1.0 || this->imag() == -1) {
imag = new Symbol(Ion::Charset::IComplex);
} else if (this->imag() > 0) {
imag = new Multiplication(CreateDecimal(this->imag()), new Symbol(Ion::Charset::IComplex), false);
} else {
imag = new Multiplication(CreateDecimal(-this->imag()), new Symbol(Ion::Charset::IComplex), false);
}
}
if (imag == nullptr) {
return real;
} else if (real == nullptr) {
if (this->imag() > 0) {
return imag;
} else {
return new Opposite(imag, false);
}
return imag;
} else if (this->imag() > 0) {
return new Addition(real, imag, false);
} else {
return new Subtraction(real, imag, false);
}
}
default:
{
assert(complexFormat == Expression::ComplexFormat::Polar);
Expression * norm = nullptr;
Expression * exp = nullptr;
T r = std::abs(*this);
T th = std::arg(*this);
if (r != 1 || th == 0) {
norm = CreateDecimal(r);
}
if (r != 0 && th != 0) {
Expression * arg = nullptr;
if (th == 1.0) {
arg = new Symbol(Ion::Charset::IComplex);
} else if (th == -1.0) {
arg = new Opposite(new Symbol(Ion::Charset::IComplex), false);
} else if (th > 0) {
arg = new Multiplication(CreateDecimal(th), new Symbol(Ion::Charset::IComplex), false);
} else {
arg = new Opposite(new Multiplication(CreateDecimal(-th), new Symbol(Ion::Charset::IComplex), false), false);
}
exp = new Power(new Symbol(Ion::Charset::Exponential), arg, false);
}
if (exp == nullptr) {
return norm;
} else if (norm == nullptr) {
return exp;
} else {
return new Multiplication(norm, exp, false);
}
}
}
}
template<typename T>
Complex<T> * Complex<T>::createInverse() const {
return new Complex<T>(Division::compute(std::complex<T>(1.0), *this));
}
template<typename T>
MatrixComplex<T>::MatrixComplex(std::complex<T> * operands, int numberOfRows, int numberOfColumns) :
m_numberOfRows(numberOfRows),
m_numberOfColumns(numberOfColumns)
{
m_operands = new std::complex<T> [numberOfRows*numberOfColumns];
for (int i=0; i<numberOfRows*numberOfColumns; i++) {
m_operands[i] = operands[i];
if (m_operands[i].real() == -0.0) {
m_operands[i].real(0.0);
}
if (m_operands[i].imag() == -0.0) {
m_operands[i].imag(0.0);
}
}
}
template<typename T>
MatrixComplex<T>::~MatrixComplex() {
if (m_operands != nullptr) {
delete [] m_operands;
}
}
template<typename T>
MatrixComplex<T>::MatrixComplex(MatrixComplex&& other) {
// Pilfer other's data
m_operands = other.m_operands;
m_numberOfRows = other.m_numberOfRows;
m_numberOfColumns = other.m_numberOfColumns;
// Reset other
other.m_operands = nullptr;
other.m_numberOfRows = 0;
other.m_numberOfColumns = 0;
}
template<typename T>
MatrixComplex<T>::MatrixComplex(const MatrixComplex& other) {
// Copy other's data
m_numberOfRows = other.m_numberOfRows;
m_numberOfColumns = other.m_numberOfColumns;
std::complex<T> * operands = new std::complex<T> [m_numberOfRows*m_numberOfColumns];
for (int i=0; i<m_numberOfRows*m_numberOfColumns; i++) {
operands[i] = other.m_operands[i];
}
m_operands = operands;
}
template<typename T>
MatrixComplex<T>& MatrixComplex<T>::operator=(MatrixComplex<T> && other) {
if (this != &other) {
if (m_operands) { delete [] m_operands; }
// Pilfer other's ivars
m_operands = other.m_operands;
m_numberOfRows = other.m_numberOfRows;
m_numberOfColumns = other.m_numberOfColumns;
// Reset other
other.m_operands = nullptr;
other.m_numberOfRows = 0;
other.m_numberOfColumns = 0;
}
return *this;
}
template<typename T>
Expression * MatrixComplex<T>::complexToExpression(Expression::ComplexFormat complexFormat) const {
Expression ** operands = new Expression * [numberOfComplexOperands()];
for (int i = 0; i < numberOfComplexOperands(); i++) {
operands[i] = Complex<T>(complexOperand(i)).complexToExpression(complexFormat);
}
Expression * result = new Matrix(operands, numberOfRows(), numberOfColumns(), false);
delete[] operands;
return result;
}
template<typename T>
std::complex<T> MatrixComplex<T>::createTrace() const {
if (numberOfRows() != numberOfColumns()) {
return std::complex<T>(NAN, NAN);
}
int dim = numberOfRows();
std::complex<T> c = std::complex<T>(0);
for (int i = 0; i < dim; i++) {
c += complexOperand(i*dim+i);
}
return c;
}
template<typename T>
std::complex<T> MatrixComplex<T>::createDeterminant() const {
if (numberOfRows() != numberOfColumns()) {
return std::complex<T>(NAN, NAN);
}
std::complex<T> * operandsCopy = new std::complex<T> [m_numberOfRows*m_numberOfColumns];
for (int i=0; i<m_numberOfRows*m_numberOfColumns; i++) {
operandsCopy[i] = m_operands[i];
}
std::complex<T> determinant = std::complex<T>(1);
Matrix::ArrayRowCanonize(operandsCopy, m_numberOfRows, m_numberOfColumns, &determinant);
delete[] operandsCopy;
return determinant;
}
template<typename T>
MatrixComplex<T> * MatrixComplex<T>::createInverse() const {
std::complex<T> * operandsCopy = new std::complex<T> [m_numberOfRows*m_numberOfColumns];
for (int i=0; i<m_numberOfRows*m_numberOfColumns; i++) {
operandsCopy[i] = m_operands[i];
}
int result = Matrix::ArrayInverse(operandsCopy, m_numberOfRows, m_numberOfColumns);
MatrixComplex<T> * inverse = nullptr;
if (result == 0) {
// Intentionally swapping dimensions for inverse, although it doesn't make a difference because it is square
inverse = new MatrixComplex<T>(operandsCopy, m_numberOfColumns, m_numberOfRows);
}
delete [] operandsCopy;
return inverse;
}
template<typename T>
MatrixComplex<T> * MatrixComplex<T>::createTranspose() const {
std::complex<T> * operands = new std::complex<T> [numberOfComplexOperands()];
for (int i = 0; i < numberOfRows(); i++) {
for (int j = 0; j < numberOfColumns(); j++) {
operands[j*numberOfRows()+i] = complexOperand(i*numberOfColumns()+j);
}
}
// Intentionally swapping dimensions for transpose
MatrixComplex<T> * matrix = new MatrixComplex<T>(operands, numberOfColumns(), numberOfRows());
delete[] operands;
return matrix;
}
template<typename T>
MatrixComplex<T> MatrixComplex<T>::createIdentity(int dim) {
std::complex<T> * operands = new std::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] = std::complex<T>(1);
} else {
operands[i*dim+j] = std::complex<T>(0);
}
}
}
MatrixComplex<T> matrix = MatrixComplex(operands, dim, dim);
delete [] operands;
return matrix;
}
template class Complex<float>;
template class Complex<double>;
template class MatrixComplex<float>;
template class MatrixComplex<double>;
}
|