-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.hpp
220 lines (178 loc) · 5.67 KB
/
matrix.hpp
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
//
// Created by Imran on 05-Sep-18.
//
#ifndef VANILLA_NN_MATRIX_HPP
#define VANILLA_NN_MATRIX_HPP
#include <iostream>
#include <random>
static const int MAT_SEED = 0;
enum Distribution { UNIFORM, NORMAL }; // for Net
template <typename E>
class Matrix{
private:
int rows{}, cols{};
E *mat{};
public:
static size_t copied;
Matrix(){
this->rows = 0;
this->cols = 0;
this->mat = nullptr;
}
Matrix(int rows, int cols){
this->rows = rows;
this->cols = cols;
this->mat = new E [rows*cols];
for(auto i=0; i<rows*cols; i++)
mat[i] = 0;
}
Matrix(int rows, int cols, E val){
this->rows = rows;
this->cols = cols;
this->mat = new E [rows*cols];
for(auto i=0; i<rows*cols; i++)
mat[i] = val;
}
// to initialize using a distribution
Matrix(int rows, int cols, Distribution d, E mean, E std_dev){
this->rows = rows;
this->cols = cols;
this->mat = new E [rows*cols];
switch(d){
case UNIFORM:
{
std::random_device rand_dev;
std::mt19937 rng(rand_dev());
std::uniform_real_distribution<E> uni_dist(mean, std_dev);
for(int i=0; i<rows*cols; i++)
mat[i] = uni_dist(rng);
break;
}
case NORMAL:
{
std::random_device rand_dev;
std::mt19937 rng(rand_dev());
std::normal_distribution<E> normal_dist(mean, std_dev);
for(int i=0; i<rows*cols; i++)
mat[i] = normal_dist(rng);
break;
}
}
}
//copy constructor
Matrix(const Matrix<E>& m){
// std::cout << "copy constructor called\n";
rows = m.rows;
cols = m.cols;
mat = new E [rows*cols];
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++)
mat[i*cols+j] = m.mat[i*cols+j];
}
copied += m.cols*m.rows;
// std::cout << "constructor copied "<< m.rows * m.cols<<"elements\n";
}
//move constructor
Matrix(Matrix<E>&& m) noexcept {
// std::cout << "move constructor called\n";
rows = m.rows;
cols = m.cols;
mat = m.mat;
m.rows = 0;
m.cols = 0;
m.mat = nullptr;
}
//copy assignment operator
Matrix<E>& operator = (const Matrix<E>& m){
// std::cout << "copy assignment called\n";
if(this != &m){
delete [] mat;
rows = m.rows;
cols = m.cols;
mat = new E[rows*cols];
for(int i=0; i<rows*cols; i++)
mat[i] = m.mat[i];
copied += m.cols*m.rows;
// std::cout << "copy assignment copied "<< m.rows * m.cols<<"elements\n";
return *this;
}
else{
std::cerr << "trying to copy the same matrix\n";
return *this;
}
}
//move assignment operator
Matrix<E>& operator = (Matrix<E>&& m) noexcept {
// std::cout << "move assignment called\n";
if(this != &m){
rows = m.rows;
cols = m.cols;
mat = m.mat;
m.rows = 0;
m.cols = 0;
m.mat = nullptr;
}
return *this;
}
~Matrix(){
delete [] this->mat;
this->mat = nullptr;
}
int getRows() const { return this->rows; }
int getCols() const { return this->cols; }
std::string shape() const;
void operator += (const Matrix& m);
void operator -= (const Matrix& m);
void operator *= (E n);
Matrix<E>& hadamard(const Matrix<E>& m);
Matrix<E> T() const;
E& operator ()(int i, int j); // access to matrix elements
void print() const;
friend Matrix<E> operator * (const Matrix<E>& a, const Matrix<E>& b){
if(a.getCols() != b.getRows()){
std::cerr<<"multiplication: bad dimensions ["<<a.getRows()<<", "<<a.getCols()<<"] and ["<<b.getRows()<<", "<<b.getCols()<<"]\n";
}
// if(a.getRows() == b.getRows() && a.getCols() == 1 && b.getCols() == 1) b = b.T();
Matrix<E> res = Matrix<E>(a.getRows(), b.getCols());
for(int j=0; j<b.getCols(); j++){
for(int i=0; i<a.getRows(); i++){
for(int k=0; k<b.getRows(); k++)
res.mat[i*res.cols+j] += a.mat[i*a.cols+k] * b.mat[k*b.cols+j];
}
}
return res;
}
friend Matrix<E> operator * (Matrix<E>& a, E scalar){
Matrix<E> res = Matrix<E>(a);
for(int i=0; i<a.rows; i++){
for(int j=0; j<a.cols; j++){
res.mat[i*a.cols+j] *= scalar;
}
}
return res;
}
friend Matrix<E> operator * (E scalar, Matrix<E>& a){
Matrix<E> res = Matrix<E>(a);
for(int i=0; i<a.rows; i++){
for(int j=0; j<a.cols; j++){
res.mat[i*a.cols+j] *= scalar;
}
}
return res;
}
friend bool operator == (Matrix<E>& a, Matrix<E>& b){
if(a.rows != b.rows || a.cols != b.cols)
return false;
for(int i=0; i<a.rows; i++){
for(int j=0; j<a.cols; j++){
if(a(i, j) != b(i, j))
return false;
}
}
return true;
}
};
template <typename E>
size_t Matrix<E>::copied = 0;
#include "matrix.cpp"
#endif //VANILLA_NN_MATRIX_HPP