-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cc
executable file
·170 lines (144 loc) · 3.71 KB
/
Matrix.cc
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
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "matrix.h"
int ceilLogAndCompute(int x);
Matrix::Matrix() {
name = NULL;
type = 1;
initSize = size = startSize = 0;
mat = NULL;
}
Matrix::Matrix(int s, char *str) {
initSize = s;
type = 0;
startSize = size = ceilLogAndCompute(s);
if(size == 0) {
cerr << "Error: Inappropriate matrix size." << endl;
exit (1);
} else { // initialize every element to 0
name = new char[strlen(str)+2];
strcpy (name, str);
mat = new int [size*size];
memset (mat , 0, size*size*sizeof(int));
}
}
// Constructor that will be used by a submatrix function
Matrix::Matrix(int s, int adjSize, int c, Matrix& m) {
size = initSize = s;
type = 1;
startSize = adjSize;
mat = &(m.mat[c]);
}
Matrix::~Matrix() {
if (type == 0) {
delete mat;
}
}
Matrix * Matrix::submatrix(int which) {
switch(which) {
case 1:
return new Matrix (size/2, startSize, 0, *this);
case 2:
return new Matrix (size/2, startSize, size/2, *this);
case 3:
return new Matrix (size/2, startSize, (size / 2) * startSize, *this);
case 4:
return new Matrix (size/2, startSize, (size / 2) * startSize + size/2, *this);
default:
cerr << "Error: The matrix only divides in four submatrices." << endl;
exit(1);
}
}
Matrix * Matrix::mult(Matrix *A, Matrix *B) {
if ((B->initSize != A->initSize) || (B->size != A->size)) {
cerr << "Error: Can't multiply matrices of different sizes." << endl;
exit(1);
}
int matsize = this->initSize = A->initSize;
this->size = A->size;
int tmp, i, j, k;
for (i = 0; i < matsize; i++) {
for (j = 0; j < matsize; j++) {
for (tmp = 0, k = 0; k < matsize; k++) {
tmp += A->elt(i,k) * B->elt(k,j);
}
elt(i,j) = tmp;
}
}
return this;
}
ostream& operator<< (ostream& out, Matrix& m) {
for (int i = 0; i < m.initSize; i++) {
for (int j = 0; j < m.initSize; j++) {
out << setw(5) << m(i,j) << '\t';
}
out << endl;
}
return out;
}
// Assuming the input is going to be entered correctly
istream& operator >> (istream& in, Matrix& m) {
for (int i = 0; i < m.initSize; i++) {
for (int j = 0; j < m.initSize; j++) {
in >> m(i,j);
}
}
return in;
}
Matrix* Matrix::add (Matrix *A, Matrix *B) {
if ((A->size != B->size) || (A->initSize != B->initSize)) {
cerr << "Error: Trying to add two matrices of different sizes." << endl;
exit(1);
}
this->size = A->size;
int matsize = this->initSize = A->initSize;
for (int i = 0; i < matsize; i++) {
for (int j = 0; j < matsize; j++) {
elt(i,j) = A->elt(i,j) + B->elt(i,j);
}
}
return this;
}
Matrix* Matrix::sub (Matrix *A, Matrix *B) {
if ((A->size != B->size) || (A->initSize != B->initSize)) {
cerr << "Error: Trying to add two matrices of different sizes." << endl;
exit(1);
}
this->size = B->size;
int matsize = this->initSize = B->initSize;
for (int i = 0; i < matsize; i++) {
for (int j = 0; j < matsize; j++) {
elt(i,j) = A->elt(i,j) - B->elt(i,j);
}
}
return this;
}
int ceilLogAndCompute(int y) {
if (y <= 2)
return 2;
return (int) pow (2.0, (int)(log(y)/log(2.0) + .999));
}
void Matrix::SetSize(int s, char *str) {
if (mat)
delete [] mat;
type = 0;
initSize = s;
startSize = size = ceilLogAndCompute(s);
mat = new int [size * size];
name = new char[strlen(str)+2];
strcpy(name, str);
}
int Matrix::operator !=(Matrix & M) {
if ((size != M.size) || (initSize != M.initSize)) {
cerr << "Can\'t compare matrixes of diff sizes\n";
}
for (int i = 0; i < initSize ; i++)
for (int j = 0; j < initSize; j++) {
if (this->operator()(i,j) != M(i,j))
return 1;
}
return 0;
}