-
Notifications
You must be signed in to change notification settings - Fork 1
/
Matrix.cpp
178 lines (163 loc) · 5.15 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
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
#include "../include/Matrix.hpp"
Matrix::Matrix(int rows, int cols){
this->rows = rows;
this->cols = cols;
for(int i = 0; i < rows; i++){
vector<int> row;
for(int j = 0; j < cols; j++){
row.push_back(0);
}
this->data.push_back(row);
}
}
Matrix::Matrix(int rows, int cols, int value){
this->rows = rows;
this->cols = cols;
for(int i = 0; i < rows; i++){
vector<int> row;
for(int j = 0; j < cols; j++){
row.push_back(value);
}
this->data.push_back(row);
}
}
Matrix::Matrix(vector<vector<int>> data){
for(int i = 0; i < data.size() - 1; i++){
if(data.at(i).size() != data.at(i + 1).size()){
cerr << "No of Cols must be equal for all rows!" << endl;
assert(false);
}
}
this->rows = data.size();
this->cols = data.at(0).size();
for(int i = 0; i < this->rows; i++){
vector<int> row;
for(int j = 0; j < this->cols; j++){
row.push_back(data.at(i).at(j));
}
this->data.push_back(row);
}
}
void Matrix::transpose(int rotation){
Matrix * temp = copy(this);
if(rotation == CLOCK_WISE){
for(int i = 0; i < this->rows; i++){
for(int j = 0; j < this->cols; j++){
this->data.at(i).at(j) =
temp->getData().at(temp->getRows() - j - 1).at(i);
}
}
}
else if(rotation == ANTI_CLOCK_WISE){
for(int i = 0; i < this->rows; i++){
for(int j = 0; j < this->cols; j++){
this->data.at(i).at(j) =
temp->getData().at(j).at(temp->getCols() - i - 1);
}
}
}
else if(rotation == DOUBLE_CLOCK_WISE){
Matrix::transpose(CLOCK_WISE);
Matrix::transpose(CLOCK_WISE);
}
delete temp;
}
void Matrix::print(){
for(int i = 0; i < this->rows; i++){
for(int j = 0; j < this->cols; j++){
cout << this->data.at(i).at(j) << " ";
}
cout<<endl;
}
}
void Matrix::print(int rowNo){
for(int j = 0; j < this->cols; j++){
cout << this->data.at(rowNo).at(j) << " ";
}
}
bool Matrix::isSquare(){
if(this->rows == this->cols){
return true;
}
return false;
}
Matrix * Matrix::copy(Matrix * matrix){
Matrix * newMatrix = new Matrix(matrix->getRows(), matrix->getCols());
for(int i = 0; i < newMatrix->getRows(); i++){
for(int j = 0; j < newMatrix->getCols(); j++){
newMatrix->setValue(i, j, matrix->getValue(i, j));
}
}
return newMatrix;
}
void Matrix::swap(Matrix * a, int index_a, bool row_a, Matrix * b, int index_b, bool row_b, bool reverse){
int temp;
if(row_a && row_b){
if(a->getCols() != b->getCols()){
cerr << "Cols of both Matrices must be equal!" << endl;
assert(false);
}
for(int i = 0; i < b->getCols(); i++){
temp = b->getValue(index_b, i);
if(reverse){
b->setValue(index_b, i, a->getValue(index_a, a->getCols() - i - 1));
a->setValue(index_a, a->getCols() - i - 1, temp);
}
else{
b->setValue(index_b, i, a->getValue(index_a, i));
a->setValue(index_a, i, temp);
}
}
}
else if(row_a && !row_b){
if(a->getCols() != b->getRows()){
cerr << "Cols of Matrix A must be equal to Rows of Matrix b!" << endl;
assert(false);
}
for(int i = 0; i < b->getRows(); i++){
temp = b->getValue(i, index_b);
if(reverse){
b->setValue(i, index_b, a->getValue(index_a, a->getCols() - i - 1));
a->setValue(index_a, a->getCols() - i - 1, temp);
}
else{
b->setValue(i, index_b, a->getValue(index_a, i));
a->setValue(index_a, i, temp);
}
}
}
else if(!row_a && row_b){
if(a->getRows() != b->getCols()){
cerr << "Rows of Matrix A must be euqal to Cols of Matrix b!" << endl;
assert(false);
}
for(int i = 0; i < b->getCols(); i++){
temp = b->getValue(index_b, i);
if(reverse){
b->setValue(index_b, i, a->getValue(a->getRows() - i - 1, index_a));
a->setValue(a->getRows() - i - 1, index_a, temp);
}
else{
b->setValue(index_b, i, a->getValue(i, index_a));
a->setValue(i, index_a, temp);
}
}
}
else if(!row_a && !row_b){
if(a->getRows() != b->getCols()){
cerr << "Rows of both Matrices must be equal!" << endl;
assert(false);
}
for(int i = 0; i < b->getRows(); i++){
temp = b->getValue(i, index_b);
if(reverse){
b->setValue(i, index_b, a->getValue(a->getRows() - i - 1, index_a));
a->setValue(a->getRows() - i - 1, index_a, temp);
}
else{
b->setValue(i, index_b, a->getValue(i, index_a));
a->setValue(i, index_a, temp);
}
}
}
}