-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.h
85 lines (80 loc) · 2.44 KB
/
matrix.h
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
#include<iostream>
using namespace std;
double pi = 3.1415926535897932384626433;
class matrix3
{
public: float mat[3][3];
matrix3()
{
for(int i = 0; i<3; i++)
for(int j = 0; j<3; j++)
mat[i][j] = 0;
}
matrix3(float mat1[3][3])
{
for(int i = 0; i<3; i++)
for(int j = 0; j<3; j++)
mat[i][j] = mat1[i][j];
}
matrix3(const matrix3 &matrix)
{
for(int i = 0; i<3; i++)
for(int j = 0; j<3; j++)
mat[i][j] = matrix.mat[i][j];
}
matrix3 operator+(matrix3 a)
{
matrix3 temp;
for(int i = 0; i<3; i++)
for(int j = 0; j<3; j++)
temp.mat[i][j] = mat[i][j] + a.mat[i][j];
return temp;
}
matrix3 operator*(double a)
{
matrix3 temp;
for(int i = 0; i<3; i++)
for(int j = 0; j<3; j++)
temp.mat[i][j] = mat[i][j] * a;
return temp;
}
matrix3 operator-(matrix3 a)
{
matrix3 temp;
for(int i = 0; i<3; i++)
for(int j = 0; j<3; j++)
temp.mat[i][j] = mat[i][j] - a.mat[i][j];
return temp;
}
matrix3 operator=(matrix3 a)
{
matrix3 temp;
for(int i = 0; i<3; i++)
for(int j = 0; j<3; j++)
temp.mat[i][j] = a.mat[i][j];
return temp;
}
float trace()
{
return (mat[1][1] + mat[2][2] + mat[3][3]);
}
};
matrix3 invert_mat(matrix3 a)
{
matrix3 b;
float det = 0;
for(int i = 0; i<3; i++)
det = det + (a.mat[0][i]*(a.mat[1][(i+1)%3]*a.mat[2][(i+2)%3] - a.mat[1][(i+2)%3]*a.mat[2][(i+1)%3]));
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
b.mat[j][i] = ((a.mat[(i+1)%3][(j+1)%3]*a.mat[(i+2)%3][(j+2)%3])-(a.mat[(i+1)%3][(j+2)%3]*a.mat[(i+2)%3][(j+1)%3])) / det;
return b;
}
matrix3 transpose(matrix3 a)
{
matrix3 b;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
b.mat[i][j] = a.mat[j][i];
return b;
}