ifndef MATRIX3X3_H define MATRIX3X3_H include mathematics include vect

  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
ifndef MATRIX3X3_H
#define MATRIX3X3_H
#include "mathematics.h"
#include "vector3.h"
#include <iostream>
template<typename T>
class Vector3;
template<typename T>
class Matrix3x3 {
public:
union {
T mat[3][3];
T row[9];
};
int* operator[] (size_t idx) {
return mat[idx];
}
Matrix3x3() {
for (int i = 0; i < 9; i++) {
this->row[i] = 0;
}
}
Matrix3x3(const Matrix3x3<T>& m) {
for (int i = 0; i < 9; i++) {
this->row[i] = m.row[i];
}
}
Matrix3x3<T> operator+(const Matrix3x3<T>& m) const {
Matrix3x3<T> res;
for (int i = 0; i < 9; i++) {
res.row[i] = this->row[i] + m.row[i];
}
return res;
}
Matrix3x3<T> operator-() const {
Matrix3x3<T> res;
for (int i = 0; i < 9; i++) {
res.row[i] = -this->row[i];
}
return res;
}
Matrix3x3<T> operator-(const Matrix3x3<T>& m) const {
Matrix3x3<T> res;
for (int i = 0; i < 9; i++) {
res.row[i] = this->row[i] - m.row[i];
}
return res;
}
Matrix3x3<T> operator*(const T& lambda) const {
Matrix3x3<T> res;
for (int i = 0; i < 9; i++) {
res.row[i] = this->row[i] * lambda;
}
return res;
}
Matrix3x3<T> operator/(const T& lambda) const {
Matrix3x3<T> res;
for (int i = 0; i < 9; i++) {
res.row[i] = this->row[i] / lambda;
}
return res;
}
Matrix3x3<T> operator*(const Matrix3x3<T>& m) const {
Matrix3x3<T> res;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
res.mat[i][j] += this->mat[i][k] * m.mat[k][j];
}
}
}
return res;
}
Matrix3x3<T> operator*(const Vector3<T>& vec) {
Matrix3x3<T> res;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
res.mat[i][j] += this->mat[i][k] * vec.xyz[k];
}
}
}
return res;
}
Matrix3x3<T>& operator =(const Matrix3x3<T>& m) {
for (int i = 0; i < 9; i++) {
this->row[i] = m.row[i];
}
return *this;
}
static Matrix3x3<T> transpose(const Matrix3x3<T>& m) {
Matrix3x3<T> res;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
res.mat[i][j] = m.mat[j][i];
}
}
return res;
}
void setIdentity() {
for (int i = 0; i < 9; i++) {
this->row[i] = 0;
}
this->mat[0][0] = 1;
this->mat[1][1] = 1;
this->mat[2][2] = 1;
}
// Метод Гауса
T det() {
T determinant = 1;
/* Matrix3x3<T> a(*this);
for (int i = 0; i < 3; ++i) {
int k = i;
// поиск наибольшего элемента в столбце
for (int j = i + 1; j < 3; ++j) {
if (abs(a.mat[j][i]) > abs(a.mat[k][i])) {
k = j;
}
}
if (a.mat[k][i] == 0) {
determinant = 0;
break;
}
if (i != k) {
T tmp;
for (int p = 0; p < 3; p++) {
tmp = a.mat[i][p];
a.mat[i][p] = a.mat[k][p];
a.mat[k][p] = tmp;
}
determinant = -determinant;
}
determinant *= a.mat[i][i];
for (int j = i + 1; j < 3; ++j) {
a.mat[i][j] /= a.mat[i][i];
}
for (int j = 0; j < 3; ++j) {
if (j != i && a.mat[j][i] != 0) {
for (int k = i + 1; k < 3; ++k) {
a.mat[j][k] -= a.mat[i][k] * a.mat[j][i];
}
}
}
}*/
return determinant;
}
};
template<typename T>
std::ostream& operator<<(std::ostream& os, const Matrix3x3<T>& mat)
{
for (int i = 0; i < 3; i++) {
std::cout << "| ";
for (int j = 0; j < 3; j++) {
std::cout << mat[i][j] << " ";
}
std::cout << "|" << std::endl;
}
return os;
}
#endif // MATRIX3X3_H