-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlsMatrix.cpp
364 lines (316 loc) · 8.73 KB
/
lsMatrix.cpp
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* LibStruct, original author: Frank Bergmann.
* Fixes and improvments: Totte Karsson
*/
#pragma hdrstop
#include <string.h>
#include <stdlib.h>
#include <complex>
#include "lsMatrix.h"
#include "lsUtils.h"
//---------------------------------------------------------------------------
namespace ls
{
//DoubleMatrix instantiate;
ostream& operator<<(ostream& stream, const IntMatrix& mat)
{
for(unsigned row = 0; row < mat.RSize(); row++)
{
for(unsigned col = 0; col < mat.CSize(); col++)
{
int val = mat(row,col);
stream<<val<<"\t";
}
stream<<"\n";
}
return stream;
}
ostream& operator<<(ostream& ss, const DoubleMatrix& mat)
{
// write col names (if they exist)
const std::vector<std::string> &colNames = mat.getColNames();
if(colNames.size() > 0) {
for(unsigned col = 0; col < colNames.size(); col++) {
ss << colNames[col];
if(col < colNames.size() -1) {
ss << ",";
}
else {
ss << std::endl;
}
}
}
//Then the data
for(unsigned row = 0; row < mat.RSize(); row++) {
for(unsigned col = 0; col < mat.CSize(); col++) {
if(col == 0) {
ss << mat(row, col);
}
else {
ss << mat(row, col);
}
if(col < mat.CSize() -1) {
ss << ",";
}
else {
ss << std::endl;
}
}
}
return ss;
}
ostream& operator<<(ostream& stream, const ComplexMatrix& mat)
{
for(unsigned row = 0; row < mat.RSize(); row++)
{
for(unsigned col = 0; col < mat.CSize(); col++)
{
Complex val = mat(row,col);
stream<<val<<"\t";
}
stream<<"\n";
}
return stream;
}
template<typename T> void Matrix<T>::initializeFrom2DMatrix(T** &oRawData, int rows, int cols)
{
resize(rows, cols);
for (unsigned int i = 0; i < _Rows; i++)
{
for (unsigned int j = 0; j < _Cols; j++)
{
this->operator ()(i,j) = oRawData[i][j];
}
}
}
template<typename T> void Matrix<T>::initializeFromConst2DMatrix(const T** oRawData, int rows, int cols)
{
resize(rows, cols);
for (unsigned int i = 0; i < _Rows; i++)
{
for (unsigned int j = 0; j < _Cols; j++)
{
(*this)(i,j) = oRawData[i][j];
}
}
}
template<typename T> std::vector<std::vector<T>> Matrix<T>::getValues(){
const unsigned int nrow = numRows();
const unsigned int ncol = numCols();
auto arr = get2DMatrix((int &) nrow, (int &) ncol);
std::vector<std::vector<T> > store(nrow, std::vector<T>(ncol));
for (int i = 0; i < numRows(); i++) {
// preallocate columns
for (int j = 0; j < numCols(); j++) {
store[i][j] = arr[i][j];
}
free(arr[i]);
}
free(arr);
return store;
}
template<typename T> T** Matrix<T>::get2DMatrix(int &nRows, int &nCols)
{
T** oBuffer = (T**) malloc(sizeof(T*)*_Rows);
for (unsigned int i = 0; i < _Rows; i++)
{
oBuffer[i] = (T*) malloc(sizeof(T)*_Cols);
}
for (unsigned int i = 0; i < _Rows; i++)
{
for (unsigned int j = 0; j < _Cols; j++)
{
oBuffer[i][j] = this->operator ()(i,j);
}
}
nRows = _Rows;
nCols = _Cols;
return oBuffer;
}
// ******************************************************************** }
// Multiply matrix 'm1' by 'm2' - returns a DoubleMatrix
// }
// Usage: A = mult (A1, A2); multiply A1 by A2 giving A }
// }
// ******************************************************************** }
ls::DoubleMatrix mult(ls::DoubleMatrix& m1, ls::DoubleMatrix& m2)
{
ls::DoubleMatrix result(0,0);
// Check dimensions
unsigned int m1_nRows = m1.numRows();
unsigned int m2_nRows = m2.numRows();
unsigned int m1_nColumns = m1.numCols();
unsigned int m2_nColumns = m2.numCols();
if (m1.size() == 0)
{
return m1;
}
if (m2.size() == 0)
{
return m2;
}
if (m1_nColumns == m2_nRows)
{
result.resize(m1_nRows, m2_nColumns);
for (unsigned int row = 0; row < result.numRows(); row++)
{
for (unsigned int col = 0; col < m2_nColumns; col++)
{
double sum = 0.0;
for (unsigned int k = 0; k < m1_nColumns; k++)
{
sum = sum + (m1[row][k] * m2[k][col]);
}
result[row][col] = sum;
}
}
return result;
}
if (m1_nRows == m2_nColumns)
{
return mult(m2, m1);
}
throw ("Incompatible matrix operands to multiply");
}
//Double matrix is a special case of a complex matrix for which the imag part is all zero..
//so it makes sense that return value is a Double matrix..
DoubleMatrix mult(ComplexMatrix& m1, DoubleMatrix& m2)
{
// Check dimensions
unsigned int m1_nRows = m1.numRows();
unsigned int m2_nRows = m2.numRows();
unsigned int m1_nColumns = m1.numCols();
unsigned int m2_nColumns = m2.numCols();
if (m1.size() == 0)
{
return real(m1);
}
if (m2.size() == 0)
{
return m2;
}
DoubleMatrix result(m1_nRows, m2_nColumns);
if (m1_nColumns == m2_nRows)
{
for (unsigned int row = 0; row < result.numRows(); row++)
{
for (unsigned int col = 0; col < m2_nColumns; col++)
{
double sum = 0.0;
for (unsigned int k = 0; k < m1_nColumns; k++)
{
sum = sum + (real(m1[row][k]) * m2[k][col]);
}
result[row][col] = sum;
}
}
return result;
}
if (m1_nRows == m2_nColumns)
{
return mult(m2, m1);
}
throw ("Incompatible matrix operands to multiply");
}
DoubleMatrix mult(DoubleMatrix& m2, ComplexMatrix& m1)
{
// Check dimensions
unsigned int m1_nRows = m1.numRows();
unsigned int m2_nRows = m2.numRows();
unsigned int m1_nColumns = m1.numCols();
unsigned int m2_nColumns = m2.numCols();
if (m1.size() == 0)
{
return real(m1);
}
if (m2.size() == 0)
{
return m2;
}
DoubleMatrix result(m1_nRows, m2_nColumns);
if (m1_nColumns == m2_nRows)
{
for (unsigned int row = 0; row < result.numRows(); row++)
{
for (unsigned int col = 0; col < m2_nColumns; col++)
{
double sum = 0.0;
for (unsigned int k = 0; k < m1_nColumns; k++)
{
sum = sum + (real(m1[row][k]) * m2[k][col]);
}
result[row][col] = sum;
}
}
return result;
}
if (m1_nRows == m2_nColumns)
{
return mult(m2, m1);
}
throw ("Incompatible matrix operands to multiply");
}
DoubleMatrix real(const ComplexMatrix& m2)
{
DoubleMatrix result(m2);
return result;
}
DoubleMatrix imag(const ComplexMatrix& m2)
{
DoubleMatrix result(m2, false); //This will copy imag part of complex matrix to the double one
return result;
}
ComplexMatrix subtract(ComplexMatrix& x, ComplexMatrix& y)
{
if(sameDimensions(x,y))
{
ComplexMatrix result(x.RSize(), x.CSize());
for (int i = 0; i < x.RSize(); i++)
{
for (int j = 0; j < x.CSize(); j++)
{
result(i, j) = x(i, j) - y(i, j);
}
}
return result;
}
else
{
throw ("Matrices must be the same dimension to perform subtraction");
}
}
//From Fransk CSharp code... a bug was fixed in this code..
bool sameDimensions(ComplexMatrix& x, ComplexMatrix& y)
{
return ((x.RSize() == y.RSize()) && (x.CSize() == y.CSize())) ? true : false;
}
ls::ComplexMatrix mult(ls::ComplexMatrix& m1, ls::ComplexMatrix& m2)
{
if(m1.CSize() != m2.RSize())
{
throw("Matrix product not defined, incompatible sizes..\n");
}
ComplexMatrix temp(m1.RSize(), m2.CSize());
Complex tempVal(0,0);
for(int row1 = 0; row1 < m1.RSize(); row1++)
{
for(int rescol = 0; rescol < m2.CSize(); rescol++)
{
for(int col1 = 0; col1 < m1.CSize(); col1++)
{
Complex val1 = m1(row1,col1);
Complex val2 = m2(col1,rescol);
Complex test = val1*val2;
tempVal = test + tempVal;
}
temp(row1,rescol) = tempVal;
tempVal = Complex(0,0);
}
}
return temp;
}
//The following instantiate each matrix type, AFAIK
template class Matrix<double>;
template class Matrix<int>;
template class Matrix< Complex >;
}