-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlinAlgebra.cpp
311 lines (276 loc) · 9.81 KB
/
linAlgebra.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
#include <vector>
#include <assert.h>
#include <cmath>
#include <float.h>
#include "Matrix.h"
#include "linAlgebra.h"
using namespace std;
namespace np{
// scalar multiplication
std::unique_ptr<Matrix> multiply(std::unique_ptr<Matrix> & m1, double value){
std::unique_ptr<Matrix> m3(new Matrix(m1->getRows(), m1->getColumns(), false));
for(unsigned int i=0; i < m1->getRows(); i++){
for(unsigned int j=0; j < m1->getColumns(); j++){
m3->set(i,j,m1->get(i,j) * value);
}
}
return m3;
}
// hadamard product of matrices
std::unique_ptr<Matrix> multiply(std::unique_ptr<Matrix> & m1, std::unique_ptr<Matrix> & m2,
unsigned int slice){
assert(m1->getRows() == m2->getRows() && m1->getColumns() == m2->getColumns() - slice);
std::unique_ptr<Matrix> m3(new Matrix(m2->getRows(), m2->getColumns() - slice, true));
for(unsigned int i=0; i < m3->getRows(); i++){
for(unsigned int j=0; j < m3->getColumns(); j++){
m3->set(i,j,m1->get(i,j) * m2->get(i,j));
}
}
return m3;
}
// hadamard product of vectors
std::unique_ptr<std::vector<double> > multiply(std::unique_ptr<std::vector<double> > &v1,
std::unique_ptr<std::vector<double> > &v2){
assert(v1->size() == v2->size());
std::unique_ptr<std::vector<double> > vr = std::make_unique<std::vector<double> >();
for(unsigned int i = 0; i < v1->size(); i++){
vr->push_back((v1->at(i) * v2->at(i)));
}
return vr;
}
// take dot product and sum all elements
double multiply(std::unique_ptr<Matrix> & m1, std::unique_ptr<Matrix> & m2,
unsigned int xslice, unsigned int yslice){
assert(m2->getRows() >= m1->getRows() && m2->getColumns() >= m1->getColumns());
double accumulator = 0;
for(unsigned int i = 0; i < m1->getRows(); i++){
for(unsigned int j = 0; j < m1->getColumns(); j++){
accumulator += (m1->get(i,j) * m2->get(xslice + i, yslice + j));
}
}
return accumulator;
}
// dot product between two matrices
std::unique_ptr<Matrix> dot(std::unique_ptr<Matrix> & m1, std::unique_ptr<Matrix> & m2,
unsigned int slice){
assert(m1->getColumns() == m2->getRows());
std::unique_ptr<Matrix> m3(new Matrix(m1->getRows(), m2->getColumns() - slice, false));
for(unsigned int i=0; i < m3->getRows(); i++){
for(unsigned int j=0; j < m3->getColumns(); j++){
double w = 0;
for(unsigned int k=0; k < m1->getColumns(); k++){
w += (m1->get(i,k) * m2->get(k,j));
}
m3->set(i,j,w);
}
}
return m3;
}
// dot product with a vetor
std::unique_ptr<std::vector<double> > dot(std::unique_ptr<Matrix> & m1,
std::unique_ptr<std::vector<double> > & v, unsigned int v_slice){
assert(m1->getColumns() == v->size() - v_slice);
std::unique_ptr<std::vector<double> > vr = std::make_unique<std::vector<double> >();
for(unsigned int i=0; i < m1->getRows(); i++){
double w = 0;
for(unsigned int j=0; j < m1->getColumns() - v_slice; j++){
w += (m1->get(i,j) * v->at(j));
}
vr->push_back(w);
}
return vr;
}
// dot product of 2 vectors returning a Rank 1 Matrix
std::unique_ptr<Matrix> dot(std::unique_ptr<std::vector<double> > & v1,
std::unique_ptr<std::vector<double> > & v2, unsigned int v2_slice){
std::unique_ptr<Matrix> m3(new Matrix(v1->size(), v2->size() - v2_slice, true));
for(unsigned int i=0; i < m3->getRows(); i++){
for(unsigned int j=0; j< m3->getColumns(); j++){
m3->set(i,j,(v1->at(i) * v2->at(j)));
}
}
return m3;
}
// addition
std::unique_ptr<Matrix> add(std::unique_ptr<Matrix> & m1, std::unique_ptr<Matrix> & m2){
assert(m1->getRows() == m2->getRows() && m1->getColumns() == m2->getColumns());
std::unique_ptr<Matrix> m3(new Matrix(m1->getRows(), m1->getColumns(), true));
for(unsigned int i=0; i < m1->getRows(); i++){
for(unsigned int j=0; j < m1->getColumns(); j++){
m3->set(i,j,m1->get(i,j) + m2->get(i,j));
}
}
return m3;
}
// subtraction (of matrices)
std::unique_ptr<Matrix> subtract(std::unique_ptr<Matrix> & m1, std::unique_ptr<Matrix> & m2){
assert(m1->getRows() == m2->getRows() && m1->getColumns() == m2->getColumns());
std::unique_ptr<Matrix> m3(new Matrix(m1->getRows(), m1->getColumns(), true));
for(unsigned int i=0; i < m1->getRows(); i++){
for(unsigned int j=0; j < m1->getColumns(); j++){
m3->set(i,j,m1->get(i,j) - m2->get(i,j));
}
}
return m3;
}
// subtraction (of vectors)
std::unique_ptr<std::vector<double> > subtract(std::unique_ptr<std::vector<double> > & v1,
std::unique_ptr<std::vector<double> > & v2){
assert(v1->size() == v2->size());
std::unique_ptr<std::vector<double> > vr = std::make_unique<std::vector<double> >();
for(unsigned int i=0; i < v1->size(); i++){
vr->push_back(v1->at(i) - v2->at(i));
}
return vr;
}
// transpose
std::unique_ptr<Matrix> transpose(std::unique_ptr<Matrix> & m1){
std::unique_ptr<Matrix> m3(new Matrix(m1->getColumns(), m1->getRows(), false));
for(unsigned int i=0; i < m1->getRows(); i++){
for(unsigned int j=0; j < m1->getColumns(); j++){
m3->set(j,i,m1->get(i,j));
}
}
return m3;
}
// apply a function to every element of the matrix
std::unique_ptr<Matrix> applyFunction(std::unique_ptr<Matrix> & m1, double (*active_fn)(double)){
std::unique_ptr<Matrix> m3(new Matrix(m1->getRows(), m1->getColumns(), false));
for(unsigned int i=0; i < m1->getRows(); i++){
for(unsigned int j=0; j < m1->getColumns(); j++){
double ret = (*active_fn)(m1->get(i,j));
if(isnan(ret)) m3->set(i,j,0);
else m3->set(i,j,ret);
}
}
return m3;
}
// apply a function to every element of the vector
std::unique_ptr<std::vector<double> > applyFunction(std::unique_ptr<std::vector<double> > &v,
double (*active_fn)(double)){
std::unique_ptr<std::vector<double> > vr = std::make_unique<std::vector<double> >();
for(unsigned int i=0; i < v->size(); i++){
if(!isnan(v->at(i))){
double ret = (*active_fn)(v->at(i));
if(isnan(ret)) vr->push_back(0);
else vr->push_back(ret);
}else {
vr->push_back(0);
}
}
return vr;
}
// concatenate matrices
std::unique_ptr<Matrix> concatenate(std::unique_ptr<Matrix> & m1, std::unique_ptr<Matrix> & m2){
assert(m1->getRows() == m2->getRows());
std::unique_ptr<Matrix> m3(new Matrix(m1->getRows(), m1->getColumns() + m2->getColumns(), true));
for(unsigned int i=0; i < m1->getRows(); i++){
for(unsigned int j=0; j < m1->getColumns(); j++){
m3->set(i,j,m1->get(i,j));
}
for(unsigned int j=m1->getColumns(); j < m1->getColumns() + m2->getColumns(); j++){
m3->set(i,j,m2->get(i,j - m1->getColumns()));
}
}
return m3;
}
// concatenate matrix with a vector as additional column
std::unique_ptr<Matrix> concatenate(std::unique_ptr<Matrix> & m1, std::vector<double> & v){
assert(m1->getRows() == v.size());
std::unique_ptr<Matrix> m3(new Matrix(m1->getRows(), m1->getColumns() + 1, true));
for(unsigned int i=0; i < m1->getRows(); i++){
for(unsigned int j=0; j < m1->getColumns(); j++){
m3->set(i,j,m1->get(i,j));
}
m3->set(i, m1->getColumns(), v[i]);
}
return m3;
}
// normalize a matrix such that sum of each row is 1
std::unique_ptr<Matrix> normalize(std::unique_ptr<Matrix> & m1){
std::unique_ptr<Matrix> m3(new Matrix(m1->getRows(), m1->getColumns(), true));
for(unsigned int i=0; i < m1->getRows(); i++){
double sum = 0;
for(unsigned int j=0; j < m1->getColumns(); j++){
sum += m1->get(i,j);
}
for(unsigned int j=0; j < m1->getColumns(); j++){
m3->set(i,j,(m1->get(i,j)/sum));
}
}
return m3;
}
// normalize a vector such that sum of all elements is 1
std::unique_ptr<std::vector<double> > normalize(std::unique_ptr<std::vector<double> > &v){
std::unique_ptr<std::vector<double> > vr = std::make_unique<std::vector<double> >();
double sum = 0;
for(unsigned int i=0; i < v->size(); i++){
sum += v->at(i);
}
assert(sum != 0);
for(unsigned int i=0; i < v->size(); i++){
vr->push_back(v->at(i)/sum);
}
return vr;
}
// return sum of all elements in matrix
double element_sum(std::unique_ptr<Matrix> & m1){
double sum = 0;
for(unsigned int i=0; i < m1->getRows(); i++){
for(unsigned int j=0; j < m1->getColumns(); j++){
sum += m1->get(i,j);
}
}
return sum;
}
// return sum of all elements in a vector
double element_sum(std::unique_ptr<std::vector<double> > &v){
double sum = 0;
for(unsigned int i=0; i < v->size(); i++){
sum += v->at(i);
}
return sum;
}
// flatten the matrix. convert 2D matrix to 1D vector
std::unique_ptr<std::vector<double> > flatten(std::unique_ptr<Matrix> & m1){
std::unique_ptr<std::vector<double> > v(new std::vector<double>(m1->getRows() * m1->getColumns()));
for(unsigned int i=0; i < m1->getRows(); i++){
for(unsigned int j=0; j < m1->getColumns(); j++){
v->at((i*m1->getColumns()) + j) = m1->get(i,j);
}
}
return v;
}
// return the maximum of matrix within the boundaries specified by (xptr, yptr, window)
// set the index of maximum element in index variable
double maximum(std::unique_ptr<Matrix> & m1, unsigned int xptr, unsigned int yptr,
Shape window, std::unique_ptr<Shape> &index){
assert(xptr + window.rows <= m1->getRows() && yptr + window.columns <= m1->getColumns());
double max=-DBL_MAX;
unsigned int i=xptr;
while(i-xptr < window.rows && i < m1->getRows()){
unsigned int j=yptr;
while(j-yptr < window.columns && j < m1->getColumns()){
if(m1->get(i,j) > max){
max = m1->get(i,j);
index->rows = i;
index->columns = j;
}
j++;
}
i++;
}
return max;
}
// reshape a vector into a matrix
std::unique_ptr<Matrix> reshape(std::unique_ptr<std::vector<double> > &v, Shape shape){
assert((shape.rows * shape.columns) == v->size());
std::unique_ptr<Matrix> m3(new Matrix(shape.rows, shape.columns, true));
for(unsigned int i=0; i < shape.rows; i++){
for(unsigned int j=0; j < shape.columns; j++){
m3->set(i,j,v->at((i*shape.rows) + j));
}
}
return m3;
}
}