-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrix.cpp
101 lines (89 loc) · 2.1 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
#include "Matrix.h"
#include "Constants.h"
Matrix::Matrix() {
vals.resize(0);
}
Matrix::Matrix(const int &r, const int &c) {
vals.resize(r);
for(int i = 0; i < r; i++) {
vals[i].resize(c);
}
}
Matrix & Matrix::operator =(const Matrix &mat) {
int r, c;
setRow(r = mat.getRow());
setColumn(c = mat.getColumn());
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
setValue(i, j, mat.getValue(i, j));
}
}
return *this;
}
// Copy Constructor
Matrix::Matrix(const Matrix &mat){
*this=mat;
}
int Matrix::getRow() const {
return vals.size();
}
void Matrix::setRow(const int &r) {
vals.resize(r);
}
int Matrix::getColumn() const {
if(getRow() < 1) {
return 0;
}
return vals[0].size();
}
void Matrix::setColumn(const int &c) {
int r = getRow();
for(int i = 0; i < r; i++) {
vals[i].resize(c);
}
}
RTdouble Matrix::getValue(const int &i, const int &j) const {
// should check i, j with boundary
// fake now
return vals[i][j];
}
void Matrix::setValue(const int &i, const int &j, const RTdouble &v) {
// should check i, j with boundary
// fake now
vals[i][j] = v;
}
Matrix Matrix::operator *(const Matrix &mat) {
if(getColumn() == mat.getRow()) {
int r = getRow(), c = mat.getColumn(), t = getColumn();
Matrix temp(r, c);
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
RTdouble temp_val = RT_ZERO;
for(int k = 0; k < t; k++) {
temp_val += getValue(i, k) * mat.getValue(k, j);
}
temp.setValue(i, j, temp_val);
}
}
return temp;
}
else {
// should check!!!!
printf("column(a) != row(b), operation deprecated!\n");
printf("Matrix a returned!\n");
return *this;
}
}
Matrix & Matrix::operator *=(const Matrix &mat) {
return *this = Matrix(*this) * mat;
}
void Matrix::print(const string &prompt) const {
int r = getRow(), c = getColumn();
printf("Matrix %s |row=%d, column=%d|>\n", prompt.c_str(), r, c);
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
printf("%.2lf%c", getValue(i, j), j == c - 1?'\n':' ');
}
}
printf("END\n");
}