-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.h
139 lines (137 loc) · 4.31 KB
/
Matrix.h
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
#ifndef MATRIX
#define MATRIX
#include<cstring>
#include <vector>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
using namespace std;
template <class T>
class Matrix: public std::vector<std::vector<T>>{
public:
int height;
int width;
Matrix(){
this->height = 0;
this->width = 0;
}
Matrix(const Matrix<T>& right){
this->height = right.height;
this->width = right.width;
this->resize(this->height , std::vector<T>( (this->width) , 0));
for(int i = 0; i < this->height; i ++) {
for(int j = 0; j < this->width; j ++) {
(*this)[i][j] = right[i][j];
}
}
}
Matrix(int height, int width): std::vector<std::vector<T>>(){
this->height = height;
this->width = width;
this->resize(this->height , std::vector<T>( (this->width) , 0));
}
void print() {
for(int i = 0; i < this->height; i ++) {
cout << "\n|";
for(int j = 0; j < this->width; j ++) {
cout << setw(8) << left << fixed << std::setprecision(3) << (*this)[i][j];
}
cout << "|";
}
cout << "\n";
}
Matrix<T> operator*(Matrix<T> right) {
Matrix temp((*this));
return (temp *= right);
}
Matrix<T> operator*=(Matrix<T> right) {
Matrix temp(right.height, this->width);
for(int i = 0; i < right.height; i ++) {
for(int j = 0; j < this->width; j ++) {
for(int k = 0; k < this->height; k ++) {
temp[i][j] += (*this)[k][j] * right[i][k];
}
}
}
return temp;
}
Matrix<T> operator+=(Matrix<T> right) {
for(int i = 0; i < this->height; i ++) {
for(int j = 0; j < this->width; j ++) {
(*this)[i][j] = (*this)[i][j] + right[i][j];
}
}
return (*this);
}
Matrix<T> operator-=(Matrix<T> right) {
for(int i = 0; i < this->height; i ++) {
for(int j = 0; j < this->width; j ++) {
(*this)[i][j] -= right[i][j];
}
}
return *this;
}
Matrix<T> operator-(Matrix<T> right) {
Matrix temp((*this));
return (temp -= right);
}
Matrix<T> operator+(Matrix<T> right) {
Matrix temp((*this));
return (temp += right);
}
Matrix<T> operator=(Matrix<T> right) {
this->clear();
this->height = right.height;
this->width = right.width;
this->resize(this->height , std::vector<T>( (this->width) , 0));
for(int i = 0; i < this->height; i ++) {
for(int j = 0; j < this->width; j ++) {
(*this)[i][j] = right[i][j];
}
}
return (*this);
}
Matrix<T> operator=(Matrix* right) {
this->clear();
this->height = right->height;
this->width = right->width;
this->resize(this->height , std::vector<T>( (this->width) , 0));
for(int i = 0; i < this->height; i ++) {
for(int j = 0; j < this->width; j ++) {
(*this)[i][j] = (*right)[i][j];
}
}
delete right;
return (*this);
}
void randGen(T top, T bot) {
srand (time(NULL)); // seed with time bc why not
int range = abs(top - bot);
for(int i = 0; i < this->height; i ++) {
for(int j = 0; j < this->width; j ++) {
//cout << ((T)((rand() / (double)RAND_MAX) * range)) << "\n";
(*this)[i][j] = ((T)((rand() / (double)RAND_MAX) * range)) + bot;
}
}
}
void setTo(T a) {
srand (time(NULL));
for(int i = 0; i < this->height; i ++) {
for(int j = 0; j < this->width; j ++) {
(*this)[i][j] = a;
}
}
}
Matrix<T> apply(T (*f)(T)) {
for(int i = 0; i < this->height; i ++) {
for(int j = 0; j < this->width; j ++) {
(*this)[i][j] = (*f)((*this)[i][j]);
}
}
return (*this);
}
~Matrix() {
this->clear();
}
};
#endif