-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.h
More file actions
156 lines (141 loc) · 4.39 KB
/
Matrix.h
File metadata and controls
156 lines (141 loc) · 4.39 KB
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
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
#include <vector>
#include <random>
class Matrix{
public:
std::vector<double> data;
int num_of_rows;
int num_of_cols;
// constructor
Matrix(int num_of_rows=0, int num_of_cols=0): num_of_rows(num_of_rows), num_of_cols(num_of_cols){
this->data.resize(num_of_cols*num_of_rows, 0.0);
}
// copy constructor
Matrix(const Matrix &mat){
this->num_of_rows = mat.num_of_rows;
this->num_of_cols = mat.num_of_cols;
this->data = mat.data;
}
// random init
Matrix randomize(double min=-1, double max=1){
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dis(min, max);
for (int i=0; i<this->num_of_cols*this->num_of_rows; i++){
this->data[i] = dis(gen);
}
return *this;
}
// indexing
double& at(int row, int col){
return data.at(row*this->num_of_cols + col);
}
// transpose
Matrix transpose(){
Matrix new_mat(this->num_of_cols, this->num_of_rows);
for(int i=0; i<this->num_of_rows; i++){
for(int j=0; j<this->num_of_cols; j++){
new_mat.at(j, i) = this->at(i, j);
}
}
return new_mat;
}
// print matrix
void print(){
std::cout << "[";
for(int i=0; i<this->num_of_rows; i++){
for(int j=0; j<this->num_of_cols; j++){
if(i!=0 and j==0){
std::cout << " ";
}
std::cout << this->at(i, j);
if(j != this->num_of_cols-1){
std::cout << ", ";
}
}
if(i != this->num_of_rows-1){
std::cout << std::endl;
}
}
std::cout << "]" << std::endl;
}
// Dot product
Matrix dot_product(Matrix mat2){
if(this->num_of_cols != mat2.num_of_rows){
throw "Matrix Dimensions mismatch error ";
}
Matrix res_mat(this->num_of_rows, mat2.num_of_cols);
for(int i=0; i<this->num_of_rows; i++){
for(int j=0; j<res_mat.num_of_cols; j++){
double e = 0;
for (int k=0; k<mat2.num_of_rows; k++){
e += this->at(i, k)*mat2.at(k, j);
}
res_mat.at(i, j) = e;
}
}
return res_mat;
}
// scaler multiply
Matrix operator*(double coficient){
Matrix res_mat(*this);
for(int i=0; i<this->num_of_rows; i++){
for(int j=0; j<this->num_of_cols; j++){
res_mat.at(i, j) = this->at(i, j) * coficient;
}
}
return res_mat;
}
// vector element-wise multiply
Matrix operator*(Matrix mat2){
if(this->num_of_cols!=mat2.num_of_cols or this->num_of_rows!=mat2.num_of_rows){
throw "Matrix Dimensions mismatch error ";
}
Matrix res_mat(*this);
for(int i=0; i<this->num_of_rows; i++){
for(int j=0; j<this->num_of_cols; j++){
res_mat.at(i, j) = this->at(i, j) * mat2.at(i, j);
}
}
return res_mat;
}
// addition
Matrix operator+(Matrix mat2){
if(this->num_of_cols!=mat2.num_of_cols or this->num_of_rows!=mat2.num_of_rows){
throw "Matrix Dimensions mismatch error ";
}
Matrix res_mat(*this);
for(int i=0; i<this->num_of_rows; i++){
for(int j=0; j<this->num_of_cols; j++){
res_mat.at(i, j) = this->at(i, j) + mat2.at(i, j);
}
}
return res_mat;
}
// subtract
Matrix operator-(Matrix mat2){
if(this->num_of_cols!=mat2.num_of_cols or this->num_of_rows!=mat2.num_of_rows){
throw "Matrix Dimensions mismatch error ";
}
Matrix res_mat(*this);
for(int i=0; i<this->num_of_rows; i++){
for(int j=0; j<this->num_of_cols; j++){
res_mat.at(i, j) = this->at(i, j) - mat2.at(i, j);
}
}
return res_mat;
}
// element operator
Matrix apply(double (*function)(double)){
Matrix res_mat(*this);
for(int i=0; i<this->num_of_rows; i++){
for(int j=0; j<this->num_of_cols; j++){
res_mat.at(i, j) = function(this->at(i, j));
}
}
return res_mat;
}
};
#endif