-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cpp
357 lines (336 loc) · 7.38 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include "Matrix.h"
static inline double abs(double d){
return (d<0)?-d:d;
}
Matrix::Matrix(int m, int n){
w = n;
h = m;
a.resize(m*n);
}
Matrix::Matrix(Matrix* m){
w=m->w;
h=m->h;
a.resize(w*h);
for(int i=0, l=w*h;i<l;i++){
a[i]=m->a[i];
}
}
Matrix::~Matrix(){
a.clear();
}
void Matrix::set(int y, int x, double d){
a[y+x*h] = d;
}
double Matrix::get(int y, int x){
return a[y+x*h];
}
void Matrix::identity(){
if(w!=h){
w = h;
a.clear();
a.resize(w*w);
for(int y = 0; y < h; y++){
for(int x = 0; x < w; x++){
a[y+x*h] = (x==y)?1:0;
}
}
}else{
for(int y = 0; y < h; y++){
for(int x = 0; x < w; x++){
a[y+x*h] = (x==y)?1:0;
}
}
}
}
int Matrix::width(){
return w;
}
int Matrix::height(){
return h;
}
void Matrix::transpose(){
if(w!=h){
Matrix* t=new Matrix(w,h);
for(int y=0;y<h;y++){
for(int x=0;x<w;x++){
t->set(y,x,a[y+x*h]);
}
}
a.clear();
a=t->a;
}else{
double tmp;
for(int y=0;y<h;y++){
for(int x=y;x<w;x++){
tmp=a[y+x*w];
a[y+x*w]=a[x+y*w];
a[x+y*w]=tmp;
}
}
}
}
void Matrix::rowSubtract(int r1, int r2){
for(int x=0;x<w;x++){
a[r1+x*h]-=a[r2+x*h];
}
}
void Matrix::rowSwap(int r1, int r2){
double tmp;
for(int x=0;x<w;x++){
tmp=a[r1+x*h];
a[r1+x*h]=a[r2+x*h];
a[r2+x*h]=tmp;
}
}
void Matrix::rowMult(int r, double factor){
for(int x=0;x<w;x++){
a[r+x*h]*=factor;
}
}
void Matrix::rowMultAndSub(int r1, int r2, double factor){
for(int x=0;x<w;x++)
a[r1+x*h]=a[r1+x*h]-factor*a[r2+x*h];
}
bool Matrix::eschelon(){
int k,i,j,maxID, maxVal;
for(k=0;k<h;k++){
maxID=0;
maxVal=0;
for(i=k;i<h;i++){
if(abs(a[i+k*h])>maxVal){
maxVal=abs(a[i+k*h]);
maxID=i;
}
}
if(maxVal==0)
return false;
rowSwap(maxID, k);
for(i=k+1;i<h;i++){
rowMultAndSub(i,k, a[i+k*h]/a[k+k*h]);
}
rowMult(k, 1/a[k+k*h]);
}
return true;
}
bool Matrix::invertable(){
if(w!=h)
return false;
switch(w){
case 0:
case 1:
return true;
case 2:
return a[0]*a[3]-a[1]*a[2]!=0;
case 3:
double det = a[0]*a[4]*a[8]
- a[0]*a[7]*a[5]
- a[3]*a[1]*a[8]
+ a[3]*a[7]*a[2]
+ a[6]*a[1]*a[5]
- a[6]*a[4]*a[2];
return det!=0;
}
Matrix* e = new Matrix(this);
bool in = e->eschelon();
delete e;
return in;
}
Matrix Matrix::inverse(){
if(w != h)
throw MyException("Invalid bounds\n");
Matrix i2(w, h);
Matrix revert(this);
i2.identity();
int k,i,j,maxID, maxVal;
double val;
for(k=0;k<h;k++){
maxID=0;
maxVal=0;
for(i=k;i<h;i++){
if(abs(a[i+k*h])>maxVal){
maxVal=abs(a[i+k*h]);
maxID=i;
}
}
if(maxVal==0)
throw MyException("Non-invertable matrix\n");
rowSwap(maxID, k);
i2.rowSwap(maxID, k);
for(i=k+1;i<h;i++){
val=a[i+k*h]/a[k+k*h];
rowMultAndSub(i,k, val);
i2.rowMultAndSub(i,k, val);
}
val=1/a[k+k*h];
rowMult(k, val);
i2.rowMult(k, val);
for(j=k-1;j>=0;j--){
val=a[j+k*h];
rowMultAndSub(j, k, val);
i2.rowMultAndSub(j, k, val);
}
}
a=revert.a;
return i2;
}
Matrix& Matrix::invert(){
if(w != h)
throw MyException("Invalid bounds\n");
Matrix i2(w, h);
i2.identity();
int k,i,j,maxID, maxVal;
double val;
for(k=0;k<h;k++){
maxID=0;
maxVal=0;
for(i=k;i<h;i++){
if(abs(a[i+k*h])>maxVal){
maxVal=abs(a[i+k*h]);
maxID=i;
}
}
if(maxVal==0)
throw MyException("Non-invertable matrix\n");
rowSwap(maxID, k);
i2.rowSwap(maxID, k);
for(i=k+1;i<h;i++){
val=a[i+k*h]/a[k+k*h];
rowMultAndSub(i,k, val);
i2.rowMultAndSub(i,k, val);
}
val=1/a[k+k*h];
rowMult(k, val);
i2.rowMult(k, val);
for(j=k-1;j>=0;j--){
val=a[j+k*h];
rowMultAndSub(j, k, val);
i2.rowMultAndSub(j, k, val);
}
}
a=i2.a;
return *this;
}
void Matrix::print(){
int i,j;
for(i=0;i<h;i++){
printf("[ ");
for(j=0;j<w;j++){
printf("%f ",a[i+j*h]);
}
printf("]\n");
}
}
Matrix Matrix::operator+(Matrix m){
if(w==m.width()&& h==m.height()){
Matrix m2(this);
m2+=m;
return m2;
}else{
throw MyException("Invalid Bounds");
}
}
Matrix& Matrix::operator+=(Matrix m){
if(w==m.width()&&h==m.height()){
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
a[i+j*h]+=m.get(i,j);
}
}
return *this;
}
throw MyException("Invalid Bounds");
}
Matrix Matrix::operator-(Matrix m){
if(w==m.width() && h==m.height()){
Matrix m2(this);
m2+=m;
return m2;
}else{
throw MyException("Invalid Bounds");
}
}
Matrix& Matrix::operator-=(Matrix m){
if(w==m.width()&&h==m.height()){
for(int i=0;i<h;i++){
for(int j=0;j<w;j++){
a[i+j*h]-=m.get(i,j);
}
}
return *this;
}
throw MyException("Invalid Bounds");
}
Matrix Matrix::operator*(Matrix m){
if(w==m.h){
Matrix m2(h, m.w);
int i,j,k;
double sum;
for(i=0;i<h;i++){
for(j=0;j<m.w;j++){
sum=0;
for(k=0;k<w;k++)
sum+=a[i+k*h]*m.a[k+j*w];
m2.set(i,j,sum);
}
}
return m2;
}else{
throw MyException("Invalid Bounds");
}
}
Matrix& Matrix::operator*=(Matrix m){
if(w==m.h){
(*this)=(*this)*m;
return *this;
}else{
throw MyException("Invalid Bounds");
}
}
Matrix Matrix::operator*(uint64_t i){
Matrix m(this);
m*=i;
return m;
}
Matrix& Matrix::operator*=(uint64_t i){
for(int x = 0, j=w*h;x<j;x++)
a[x]*=i;
return *this;
}
Matrix Matrix::operator*(double d){
Matrix m(this);
m*=d;
return m;
}
Matrix& Matrix::operator*=(double d){
for(int i = 0, j=w*h;i<j;i++)
a[i]*=d;
return *this;
}
Matrix Matrix::operator/(Matrix m){
m.invert();
return this->operator*(m);
}
Matrix& Matrix::operator/=(Matrix m){
m.invert();
return this->operator*=(m);
}
Matrix Matrix::operator/(uint64_t i){
Matrix m(this);
m/=i;
return m;
}
Matrix& Matrix::operator/=(uint64_t i){
for(int x = 0, j=w*h;x<j;x++)
a[x]/=i;
return *this;
}
Matrix Matrix::operator/(double d){
Matrix m(this);
m/=d;
return m;
}
Matrix& Matrix::operator/=(double d){
for(int x = 0, j=w*h;x<j;x++)
a[x]/=d;
return *this;
}