-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.cpp
447 lines (341 loc) · 8.07 KB
/
matrix.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
_____ __ _____
| | | | _ |
| | | | |__| __|
|_|_|_|_____|__| C++11 Standard
Multilayer perceptron (MLP) with backpropagation
A very simple implementation of artificial neural network
Written by Emilio Schinina' (emilioschi@gmail.com), JAN 2019
File: matrix.cpp
*/
#include "matrix.h"
matrix::matrix(int r, int c)
{
#if _DEBUG_MATRIX_
std::cout << "[#] Allocation of new " << r << "x" <<c<< " matrix: "<< this << std::endl;
#endif
row = r;
column = c;
length = r * c;
if(!(data = (double*)malloc(sizeof(double) * length))){
exit(1);
}
memset(data, 0.0, length * sizeof(double));
}
matrix::matrix(int r, int c, double* array)
{
#if _DEBUG_MATRIX_
std::cout << "[#] Allocation of new " << r << "x" <<c<< " matrix: "<< this << std::endl;
#endif
ASSERT(r > 0 && c > 0);
row = r;
column = c;
length = r * c;
if(!(data = (double*)malloc(sizeof(double) * length))){
exit(1);
}
double *ptr = data;
double *ptr_d = array;
for (int i = 0; i < length; i++) {
*ptr = *ptr_d;
ptr++;
ptr_d++;
}
//memset(data, 0.0, length * sizeof(double));
}
matrix::matrix(const matrix& source)
{
#if _DEBUG_MATRIX_
std::cout << "[#] Copy Constructor called. ";
std::cout << source.row << "x" << source.column << " matrix: "<< this << std::endl;
#endif
row = source.row;
column = source.column;
length = row * column;
if(!(data = (double*)malloc(sizeof(double) * length))){
exit(1);
}
double* ptr_a = data;
double* ptr_b = source.data;
// TODO Check NULL pointer
for (int i = 0; i < length; i++)
*(ptr_a++) = *(ptr_b++);
}
double matrix::getvalue(int i, int j)
{
i -= 1; j -=1; // C become from zero
return *(data + (i * column) + j);
}
void matrix::setvalue(int i, int j, double value)
{
i -= 1; j -=1; // C become from zero
*(data + (i * column) + j) = value;
}
void matrix::randmatrix(double lower, double upper)
{
double value;
double* ptr = data;
for (int i = 0; i < length; i++) {
value = lower + (double)rand() / (double)RAND_MAX * (upper - lower);
*(ptr++) = value;
}
}
void matrix::scale(double value)
{
double *ptr = data;
for (int i = 0; i < length; i++)
*(ptr++) *= value;
}
void matrix::dot(matrix source)
{
ASSERT(row == source.row && column == source.column);
double *ptr_a = data;
double *ptr_b = source.data;
for (int i = 0; i < length; i++)
*(ptr_a++) *= *(ptr_b++);
}
matrix matrix::transpose()
{
matrix result(column, row);
double* ptr_out = result.data;
double* ptr = data;
for (int i = 0; i < row; i++)
{
ptr_out = &result.data[i];
for (int j = 0; j < column; j++)
{
*ptr_out = *ptr;
ptr++;
ptr_out += row;
}
}
return result;
}
matrix& matrix::operator=(const matrix& source)
{
#if _DEBUG_MATRIX_
std::cout << "[#] Overoaded Assignent called. " << source.row << "x" << source.column << " matrix: "<< this << std::endl;
#endif
// self assignment check
if (this == &source) {
return *this;
}
row = source.row;
column = source.column;
length = row * column;
// cambiano le dimensioni
if(!(data = (double*)realloc(data, sizeof(double) * length))){
exit(1);
}
double* ptr_a = data;
double* ptr_b = source.data;
// TODO Check NULL pointer
for (int i = 0; i < length; i++)
*(ptr_a++) = *(ptr_b++);
return *this;
}
matrix matrix::operator+(const matrix& source)
{
ASSERT(row == source.row && column == source.column);
matrix result(row, column);
double* ptr_out = result.data;
double* ptr_a = data;
double* ptr_b = source.data;
for (int i = 0; i < length; ++i)
*(ptr_out++) = *(ptr_a++) + *(ptr_b++);
return result;
}
matrix matrix::operator-(const matrix& source)
{
ASSERT(row == source.row && column == source.column);
matrix result(row, column);
double* ptr_out = result.data;
double* ptr_a = data;
double* ptr_b = source.data;
for (int i = 0; i < length; ++i)
*(ptr_out++) = *(ptr_a++) - *(ptr_b++);
return result;
}
matrix matrix::operator*(const matrix& source)
{
ASSERT(column == source.row);
ASSERT(column != 0);
ASSERT(row != 0);
#if _DEBUG_MATRIX_
std::cout << "[#] Multiplicity " << row << "x" << source.column << " matrix: "<< this << std::endl;
#endif
matrix result(row, source.column);
double* ptr_out = result.data;
double* ptr_a = data;
double* ptr_b = source.data;
for (int i = 0; i < row; i++) {
for (int j = 0; j < source.column; j++) {
ptr_a = &data[ i * column ];
ptr_b = &source.data[ j ];
*ptr_out = 0;
for (int k = 0; k < column; k++) {
*ptr_out += *ptr_a * *ptr_b;
ptr_a++;
ptr_b += source.column;
}
ptr_out++;
}
}
return result;
}
matrix matrix::operator+(double value)
{
ASSERT(column != 0);
ASSERT(row != 0);
#if _DEBUG_MATRIX_
std::cout << "[#] Add value. value: " << value << ": " << row << "x" << column << " matrix: "<< this << std::endl;
#endif
matrix result(row, column);
double* ptr_out = result.data;
double* ptr_a = data;
for (int i = 0; i < length; i++)
*(ptr_out++) = *(ptr_a++) + value;
return result;
}
matrix matrix::operator*(double scale)
{
ASSERT(column != 0);
ASSERT(row != 0);
#if _DEBUG_MATRIX_
std::cout << "[#] Scale multiplicity. value: " << scale << ": " << row << "x" << column << " matrix: "<< this << std::endl;
#endif
matrix result(row, column);
double* ptr_out = result.data;
double* ptr_a = data;
for (int i = 0; i < length; i++)
*(ptr_out++) = *(ptr_a++) * scale;
return result;
}
void matrix::randindex()
{
int j,k;
int n = column;
double tmp;
double* ptr = data;
for (int i = 1; i <= n; i++)
*(ptr++) = i;
for (int i = 0; i < n * 2; i++) {
j = (int)(rand() % n);
k = (int)(rand() % n);
tmp = this->getvalue(1, j+1);
this->setvalue(1, j+1, this->getvalue(1, k+1));
this->setvalue(1, k+1, tmp);
}
}
matrix matrix::strip_row(int i)
{
ASSERT(0 < i && i <= row);
i -= 1;
matrix result(1, column);
double* ptr_out = result.data;
double* ptr_a = data;
for (int j = 0; j < column; j++)
*(ptr_out++) = *(ptr_a + (i * column) + j);
return result;
}
matrix matrix::strip_column(int i)
{
ASSERT(0 < i && i <= column);
i -= 1;
matrix result(row, 1);
double* ptr_out = result.data;
double* ptr_a = data;
for (int j = 0; j < row; j++)
*(ptr_out++) = *(ptr_a + (j * column) + i);
return result;
}
matrix matrix::row_add()
{
matrix result(1, row);
double sum;
for (int j = 1; j <= row; j++) {
sum = 0;
for (int i = 1; i <= column; i++) {
sum += this->getvalue(j, i);
}
result.setvalue(1, j, sum);
}
return result;
}
matrix matrix::column_add()
{
matrix result(1, column);
double sum;
for (int j = 1; j <= column; j++) {
sum = 0;
for (int i = 1; i <= row; i++) {
sum += this->getvalue(i, j);
}
result.setvalue(1, j, sum / column);
}
return result;
}
double matrix::median()
{
matrix tmp(row, column);
tmp = *this;
tmp = tmp.bubblesort();
double* ptr_1 = tmp.data;
double* ptr_2 = tmp.data;
return (length % 2) ? (*(ptr_1) + ((length) / 2 )) : ( (*(ptr_1 + (length / 2 - 1))) + (*(ptr_2 + (length / 2))) ) / 2;
}
matrix matrix::median_column()
{
matrix result(1, column);
double median_value;
for (int j = 1; j <= column; j++) {
//this->strip_column(j).print("column");
median_value = this->strip_column(j).median();
result.setvalue(1, j, median_value);
}
return result;
}
matrix matrix::bubblesort()
{
double tmp;
matrix result(row, column);
result = *this;
double* arr = result.data;
for(int i = 0; i < (length - 1); i++)
{
for(int j = 0; j < (length - i - 1); j++)
{
if(arr[j] > arr[j+1])
{
tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
return result;
}
void matrix::print(std::string str)
{
double* ptr = data;
#if _DEBUG_MATRIX_
std::cout << row << "x" << column << " "<< str << " @ " << this << std::endl;
#else
std::cout << row << "x" << column << " "<< str << std::endl;
#endif
for (int i = 0; i < row; i++) {
std::cout << " ";
for (int j = 0; j < column; j++) {
std::cout.width(12);
std::cout << std::left << *(ptr++);
}
std::cout << std::endl;
}
}
matrix::~matrix()
{
#if _DEBUG_MATRIX_
std::cout << "[#] Destructor called. Memory location: " << this << std::endl;
#endif
SAFE_FREE(data);
}