-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cpp
241 lines (191 loc) · 4.28 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//Program 2 - "Matrix Class" - Class functions
//Implement the Matrix class
//Due: January 28, 2007
//Created: January 26, 2007
//Last Modified: January 28, 2007
#include "Matrix.h"
#include <cstdlib>
#include <iostream>
using namespace std;
int Matrix::matrixCount = 0;
Matrix::Matrix(int r, int c)
{
if (r > 0)
{
rows = r;
}
else
{
rows = 0;
}
if (c > 0)
{
cols = c;
}
else
{
cols = 0;
}
element = new int* [rows];
for (int i=0; i<rows; i++)
{
element[i] = new int[cols];
for (int n = 0; n<cols; n++)
{
element[i][n] = 0;
}
}
matrixCount++;
}
Matrix::Matrix(const Matrix& m)
{
rows = m.rows;
cols = m.cols;
element = new int* [rows];
for (int i=0; i<rows; i++)
{
element[i] = new int[cols];
for (int n = 0; n<cols; n++)
{
element[i][n] = m.element[i][n];
}
}
matrixCount++;
}
Matrix::~Matrix()
{
if (element !=NULL)
{
for (int i=0; i<rows; i++)
{
if (element[i] != NULL)
delete []element[i];
}
delete []element;
}
matrixCount--;
}
Matrix Matrix::operator=(Matrix m)
{
if (element !=NULL)
{
for (int i=0; i<rows; i++)
{
if (element[i] != NULL)
delete []element[i];
}
delete []element;
}
rows = m.rows;
cols = m.cols;
element = new int* [rows];
for (int i=0; i<rows; i++)
{
element[i] = new int[cols];
for (int n = 0; n<cols; n++)
{
element[i][n] = m.element[i][n];
}
}
return *this;
}
int* Matrix::operator[] (int n)
{
return element[n];
}
int Matrix::getRows() const
{
return rows;
}
int Matrix::getColumns() const
{
return cols;
}
int Matrix::getElement(int r, int c) const
{
if (r<0)
r = 0;
else if (r>=rows)
r = rows-1;
if (c<0)
c = 0;
else if (c>=cols)
c = cols-1;
return element[r][c];
}
void Matrix::setElement (int r, int c, int v)
{
if (r>=0 && r<=rows && c>=0 && c<=cols)
element[r][c] = v;
}
Matrix Matrix::add (const Matrix& m) const
{
Matrix sum;
sum.rows = rows;
sum.cols = cols;
sum.element = new int* [rows];
for (int i=0; i<rows; i++)
{
sum.element[i] = new int[cols];
for (int n = 0; n<cols; n++)
{
sum.element[i][n] = element[i][n]+ m.element[i][n];
}
}
return sum;
}
Matrix Matrix::subtract (const Matrix& m) const
{
Matrix difference;
difference.rows = rows;
difference.cols = cols;
difference.element = new int* [rows];
for (int i=0; i<rows; i++)
{
difference.element[i] = new int[cols];
for (int n = 0; n<cols; n++)
{
difference.element[i][n] = element[i][n]- m.element[i][n];
}
}
return difference;
}
Matrix Matrix::operator * (const Matrix& m) const
{
Matrix product;
product.rows = rows;
product.cols = m.cols;
product.element = new int* [rows];
for (int i=0; i<product.rows; i++)
{
product.element[i] = new int[m.cols];
for (int n = 0; n<product.cols; n++)
{
product.element[i][n] = 0;
for (int r=0; r<cols; r++)
{
product.element[i][n] += element[i][r] * m.element[r][n];
}
}
}
return product;
}
Bit Matrix::operator * (const Bit& bit) const
{
//When multiplying a by a bit if there are more than 2 rows, throw bad_index
//if more than 2 cols the vectos wont fit in the Bit struct, throw bad_index
if(rows != 2 || cols != 2)
throw ("Index is Bad");
//New R comp
int rComp = ( element[0][0] * bit.r_ ) + ( element[0][1] * bit.c_ );
//New C comp
int cComp = ( element[1][0] * bit.r_ ) + ( element[1][1] * bit.c_ );
//Creating the new Bit to be filled
Bit flipBit(rComp, cComp);
//Return
return flipBit;
}
int Matrix::get_matrixCount()
{
return matrixCount;
}