-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.cpp
270 lines (240 loc) · 4.36 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "matrix.h"
Matrix::Matrix()
{
row_w = 3;
col_w = 3;
numbers = (double*)malloc((3 * 3) * sizeof(double));
for(int i = 0; i < row_w; i++)
{
for(int j = 0; j < col_w; j++)
{
if(i == j)
{
numbers[(col_w*i) + j] = 1;
}
else
{
numbers[(col_w*i) + j] = 0;
}
}
}
}
Matrix::Matrix(int row_width, int col_width)
{
row_w = row_width;
col_w = col_width;
numbers = (double*)malloc((row_w*col_w) * sizeof(double));
for(int i = 0; i < row_w; i++)
{
for(int j = 0; j < col_w; j++)
{
numbers[(col_w*i) + j] = 0;
}
}
}
Matrix::Matrix(int n)
{
row_w = n;
col_w = n;
numbers = (double*)malloc((n * n) * sizeof(double));
for(int i = 0; i < row_w; i++)
{
for(int j = 0; j < col_w; j++)
{
if(i == j)
{
numbers[(col_w*i) + j] = 1;
}
else
{
numbers[(col_w*i) + j] = 0;
}
}
}
}
int Matrix::getColWidth() const
{
return col_w;
}
int Matrix::getRowWidth() const
{
return row_w;
}
double Matrix::getAt(int r, int c) const
{
return numbers[(col_w*r) + c];
}
void Matrix::setAt(int r, int c, double x)
{
numbers[(col_w*r) + c] = x;
}
Matrix& Matrix::operator=(const Matrix &rhs)
{
row_w = rhs.getRowWidth();
col_w = rhs.getColWidth();
free(numbers);
numbers = (double*)malloc((row_w * col_w) * sizeof(double));
for(int i = 0; i < row_w; i++)
{
for(int j = 0; j < col_w; j++)
{
numbers[(col_w*i) + j] = rhs.getAt(i, j);
}
}
return *this;
}
void Matrix::transpose()
{
int old_rw = row_w;
int old_cw = col_w;
Matrix temp(row_w);
temp = *(this);
row_w = old_cw;
col_w = old_rw;
for(int i = 0; i < old_rw; i++)
{
for(int j = 0; j < old_cw; j++)
{
setAt(j, i, temp.getAt(i, j));
}
}
}
Matrix Matrix::getTranspose() const
{
Matrix trans(col_w, row_w);
for(int i = 0; i < row_w; i++)
{
for(int j = 0; j < col_w; j++)
{
trans.setAt(j, i, numbers[(col_w * i) + j]);
}
}
return trans;
}
void Matrix::invert()
{
if((col_w == 2)&&(row_w == 2))
{
double a;
double b;
double c;
double d;
double o;
a = getAt(0, 0);
b = getAt(0, 1);
c = getAt(1, 0);
d = getAt(1, 1);
o = 1.0/((a*d) - (b*c));
setAt(0,0, (o*d));
setAt(0,1, (o*b*-1.0));
setAt(1,0, (o*c*-1.0));
setAt(1,1, (o*a));
}
else if((col_w == 3)&&(row_w == 3))
{
//not needed yet
}
return;
}
Matrix Matrix::getInverse() const
{
Matrix inv(2, 2);
if((col_w == 2)&&(row_w == 2))
{
double a;
double b;
double c;
double d;
double o;
a = getAt(0, 0);
b = getAt(0, 1);
c = getAt(1, 0);
d = getAt(1, 1);
o = 1.0/((a*d) - (b*c));
inv.setAt(0,0, (o*d));
inv.setAt(0,1, (o*b*-1.0));
inv.setAt(1,0, (o*c*-1.0));
inv.setAt(1,1, (o*a));
}
else if((col_w == 3)&&(row_w == 3))
{
//not needed yet
}
return inv;
}
const Vector Matrix::operator*(const Vector &other) const
{
//only made to work by 2X2 matricies and vec2's
Vector vec(0, 0, 0);
vec.setX( (getAt(0,0) * other.getX()) + (getAt(0,1) * other.getY()));
vec.setY( (getAt(1,0) * other.getX()) + (getAt(1,1) * other.getY()));
return vec;
}
const Matrix Matrix::operator-(const Matrix &other) const
{
Matrix result(row_w, col_w);
for(int i = 0; i < row_w; i++)
{
for(int j = 0; j < col_w; j++)
{
result.setAt(i, j, (getAt(i, j) - other.getAt(i, j)));
}
}
return result;
}
void Matrix::rotate2d(double degrees)
{
// assumes a 2x2 matrix
double radians = degrees * (M_PI / 180.0);
setAt(0,0, cos(radians));
setAt(0,1, (sin(radians)*-1.0f));
setAt(1,0, sin(radians));
setAt(1,1, cos(radians));
}
const Matrix Matrix::operator*(const double &other) const
{
Matrix result(row_w, col_w);
for(int i = 0; i < row_w; i++)
{
for(int j = 0; j < col_w; j++)
{
result.setAt(i, j, (getAt(i, j) * other));
}
}
return result;
}
void Matrix::print() const
{
for(int i = 0; i < row_w; i++)
{
std::cout << "[";
for(int j = 0; j < col_w; j++)
{
std::cout << " " << getAt(i,j) << " ";
}
std::cout << "]" << std::endl;
}
}
const Matrix Matrix::operator*(const Matrix &other) const
{
// this assumes row and colums make sense for multiplication
Matrix result(row_w, other.getColWidth());
double accumulator;
for(int i = 0; i < row_w; i++)
{
for(int k = 0; k < other.getColWidth(); k++)
{
accumulator = 0.0;
for(int j = 0; j< col_w; j++)
{
accumulator += getAt(i, j) * other.getAt(j, k);
}
result.setAt(i, k, accumulator);
}
}
return result;
}
Matrix::~Matrix()
{
free(numbers);
}