diff options
Diffstat (limited to 'matrix3x3.cpp')
-rw-r--r-- | matrix3x3.cpp | 271 |
1 files changed, 271 insertions, 0 deletions
diff --git a/matrix3x3.cpp b/matrix3x3.cpp new file mode 100644 index 0000000..cb3d892 --- /dev/null +++ b/matrix3x3.cpp | |||
@@ -0,0 +1,271 @@ | |||
1 | #include "matrix3x3.h" | ||
2 | #include <iostream> | ||
3 | #include <cmath> | ||
4 | #include <vector> | ||
5 | #include <cassert> | ||
6 | |||
7 | Matrix3x3::Matrix3x3() | ||
8 | { | ||
9 | |||
10 | } | ||
11 | |||
12 | Matrix3x3::Matrix3x3(std::initializer_list<Vector3D> vals) | ||
13 | { | ||
14 | std::vector<Vector3D> _entries(vals); | ||
15 | assert(_entries.size() == 3); | ||
16 | |||
17 | entries[0] = _entries[0]; | ||
18 | entries[1] = _entries[1]; | ||
19 | entries[2] = _entries[2]; | ||
20 | } | ||
21 | |||
22 | double& Matrix3x3::operator()(int i, int j) | ||
23 | { | ||
24 | return entries[j][i]; | ||
25 | } | ||
26 | |||
27 | const double& Matrix3x3::operator()(int i, int j) const | ||
28 | { | ||
29 | return entries[j][i]; | ||
30 | } | ||
31 | |||
32 | Vector3D& Matrix3x3::operator[](int j) | ||
33 | { | ||
34 | return entries[j]; | ||
35 | } | ||
36 | |||
37 | const Vector3D& Matrix3x3::operator[](int j) const | ||
38 | { | ||
39 | return entries[j]; | ||
40 | } | ||
41 | |||
42 | void Matrix3x3::zero(double val) | ||
43 | { | ||
44 | // sets all elements to val | ||
45 | entries[0] = entries[1] = entries[2] = Vector3D( val, val, val ); | ||
46 | } | ||
47 | |||
48 | double Matrix3x3::det() const | ||
49 | { | ||
50 | const Matrix3x3& A( *this ); | ||
51 | |||
52 | return -A(0,2)*A(1,1)*A(2,0) + A(0,1)*A(1,2)*A(2,0) + | ||
53 | A(0,2)*A(1,0)*A(2,1) - A(0,0)*A(1,2)*A(2,1) - | ||
54 | A(0,1)*A(1,0)*A(2,2) + A(0,0)*A(1,1)*A(2,2) ; | ||
55 | } | ||
56 | |||
57 | double Matrix3x3::norm() const | ||
58 | { | ||
59 | return sqrt( entries[0].norm2() + | ||
60 | entries[1].norm2() + | ||
61 | entries[2].norm2() ); | ||
62 | } | ||
63 | |||
64 | Matrix3x3 Matrix3x3::operator-() const | ||
65 | { | ||
66 | const Matrix3x3& A( *this ); | ||
67 | Matrix3x3 B; | ||
68 | |||
69 | B(0,0) = -A(0,0); B(0,1) = -A(0,1); B(0,2) = -A(0,2); | ||
70 | B(1,0) = -A(1,0); B(1,1) = -A(1,1); B(1,2) = -A(1,2); | ||
71 | B(2,0) = -A(2,0); B(2,1) = -A(2,1); B(2,2) = -A(2,2); | ||
72 | |||
73 | return B; | ||
74 | } | ||
75 | |||
76 | void Matrix3x3::operator+=(const Matrix3x3& B) | ||
77 | { | ||
78 | Matrix3x3& A( *this ); | ||
79 | double* Aij = (double*) &A; | ||
80 | const double* Bij = (const double*) &B; | ||
81 | |||
82 | *Aij++ += *Bij++; | ||
83 | *Aij++ += *Bij++; | ||
84 | *Aij++ += *Bij++; | ||
85 | *Aij++ += *Bij++; | ||
86 | *Aij++ += *Bij++; | ||
87 | *Aij++ += *Bij++; | ||
88 | *Aij++ += *Bij++; | ||
89 | *Aij++ += *Bij++; | ||
90 | *Aij++ += *Bij++; | ||
91 | } | ||
92 | |||
93 | Matrix3x3 Matrix3x3::operator-(const Matrix3x3& B) const | ||
94 | { | ||
95 | const Matrix3x3& A( *this ); | ||
96 | Matrix3x3 C; | ||
97 | |||
98 | for( int i = 0; i < 3; i++ ) | ||
99 | for( int j = 0; j < 3; j++ ) | ||
100 | { | ||
101 | C(i,j) = A(i,j) - B(i,j); | ||
102 | } | ||
103 | |||
104 | return C; | ||
105 | } | ||
106 | |||
107 | Matrix3x3 Matrix3x3::operator*(double c) const | ||
108 | { | ||
109 | const Matrix3x3& A( *this ); | ||
110 | Matrix3x3 B; | ||
111 | |||
112 | for( int i = 0; i < 3; i++ ) | ||
113 | for( int j = 0; j < 3; j++ ) | ||
114 | { | ||
115 | B(i,j) = c*A(i,j); | ||
116 | } | ||
117 | |||
118 | return B; | ||
119 | } | ||
120 | |||
121 | Matrix3x3 operator*(double c, const Matrix3x3& A) | ||
122 | { | ||
123 | Matrix3x3 cA; | ||
124 | const double* Aij = (const double*) &A; | ||
125 | double* cAij = (double*) &cA; | ||
126 | |||
127 | *cAij++ = c * (*Aij++); | ||
128 | *cAij++ = c * (*Aij++); | ||
129 | *cAij++ = c * (*Aij++); | ||
130 | *cAij++ = c * (*Aij++); | ||
131 | *cAij++ = c * (*Aij++); | ||
132 | *cAij++ = c * (*Aij++); | ||
133 | *cAij++ = c * (*Aij++); | ||
134 | *cAij++ = c * (*Aij++); | ||
135 | *cAij++ = c * (*Aij++); | ||
136 | |||
137 | return cA; | ||
138 | } | ||
139 | |||
140 | Matrix3x3 Matrix3x3::operator*(const Matrix3x3& B) const | ||
141 | { | ||
142 | const Matrix3x3& A( *this ); | ||
143 | Matrix3x3 C; | ||
144 | |||
145 | for( int i = 0; i < 3; i++ ) | ||
146 | for( int j = 0; j < 3; j++ ) | ||
147 | { | ||
148 | C(i,j) = 0.; | ||
149 | |||
150 | for( int k = 0; k < 3; k++ ) | ||
151 | { | ||
152 | C(i,j) += A(i,k)*B(k,j); | ||
153 | } | ||
154 | } | ||
155 | |||
156 | return C; | ||
157 | } | ||
158 | |||
159 | Vector3D Matrix3x3::operator*(const Vector3D& x) const | ||
160 | { | ||
161 | return x[0]*entries[0] + | ||
162 | x[1]*entries[1] + | ||
163 | x[2]*entries[2] ; | ||
164 | } | ||
165 | |||
166 | Matrix3x3 Matrix3x3::T() const | ||
167 | { | ||
168 | const Matrix3x3& A( *this ); | ||
169 | Matrix3x3 B; | ||
170 | |||
171 | for( int i = 0; i < 3; i++ ) | ||
172 | for( int j = 0; j < 3; j++ ) | ||
173 | { | ||
174 | B(i,j) = A(j,i); | ||
175 | } | ||
176 | |||
177 | return B; | ||
178 | } | ||
179 | |||
180 | Matrix3x3 Matrix3x3::inv() const | ||
181 | { | ||
182 | const Matrix3x3& A( *this ); | ||
183 | Matrix3x3 B; | ||
184 | |||
185 | B(0,0) = -A(1,2)*A(2,1) + A(1,1)*A(2,2); B(0,1) = A(0,2)*A(2,1) - A(0,1)*A(2,2); B(0,2) = -A(0,2)*A(1,1) + A(0,1)*A(1,2); | ||
186 | B(1,0) = A(1,2)*A(2,0) - A(1,0)*A(2,2); B(1,1) = -A(0,2)*A(2,0) + A(0,0)*A(2,2); B(1,2) = A(0,2)*A(1,0) - A(0,0)*A(1,2); | ||
187 | B(2,0) = -A(1,1)*A(2,0) + A(1,0)*A(2,1); B(2,1) = A(0,1)*A(2,0) - A(0,0)*A(2,1); B(2,2) = -A(0,1)*A(1,0) + A(0,0)*A(1,1); | ||
188 | |||
189 | B /= det(); | ||
190 | |||
191 | return B; | ||
192 | } | ||
193 | |||
194 | void Matrix3x3::operator/=(double x) | ||
195 | { | ||
196 | Matrix3x3& A( *this ); | ||
197 | double rx = 1./x; | ||
198 | |||
199 | for( int i = 0; i < 3; i++ ) | ||
200 | for( int j = 0; j < 3; j++ ) | ||
201 | { | ||
202 | A( i, j ) *= rx; | ||
203 | } | ||
204 | } | ||
205 | |||
206 | Matrix3x3 Matrix3x3::identity() | ||
207 | { | ||
208 | Matrix3x3 B; | ||
209 | |||
210 | B(0,0) = 1.; B(0,1) = 0.; B(0,2) = 0.; | ||
211 | B(1,0) = 0.; B(1,1) = 1.; B(1,2) = 0.; | ||
212 | B(2,0) = 0.; B(2,1) = 0.; B(2,2) = 1.; | ||
213 | |||
214 | return B; | ||
215 | } | ||
216 | |||
217 | Matrix3x3 Matrix3x3::crossProduct(const Vector3D& u) | ||
218 | { | ||
219 | Matrix3x3 B; | ||
220 | |||
221 | B(0,0) = 0.; B(0,1) = -u.z; B(0,2) = u.y; | ||
222 | B(1,0) = u.z; B(1,1) = 0.; B(1,2) = -u.x; | ||
223 | B(2,0) = -u.y; B(2,1) = u.x; B(2,2) = 0.; | ||
224 | |||
225 | return B; | ||
226 | } | ||
227 | |||
228 | Matrix3x3 outer(const Vector3D& u, const Vector3D& v) | ||
229 | { | ||
230 | Matrix3x3 B; | ||
231 | double* Bij = (double*) &B; | ||
232 | |||
233 | *Bij++ = u.x*v.x; | ||
234 | *Bij++ = u.y*v.x; | ||
235 | *Bij++ = u.z*v.x; | ||
236 | *Bij++ = u.x*v.y; | ||
237 | *Bij++ = u.y*v.y; | ||
238 | *Bij++ = u.z*v.y; | ||
239 | *Bij++ = u.x*v.z; | ||
240 | *Bij++ = u.y*v.z; | ||
241 | *Bij++ = u.z*v.z; | ||
242 | |||
243 | return B; | ||
244 | } | ||
245 | |||
246 | std::ostream& operator<<(std::ostream& os, const Matrix3x3& A) | ||
247 | { | ||
248 | for (int i = 0; i < 3; i++) | ||
249 | { | ||
250 | os << "[ "; | ||
251 | |||
252 | for (int j = 0; j < 3; j++) | ||
253 | { | ||
254 | os << A(i,j) << " "; | ||
255 | } | ||
256 | |||
257 | os << "]" << std::endl; | ||
258 | } | ||
259 | |||
260 | return os; | ||
261 | } | ||
262 | |||
263 | Vector3D& Matrix3x3::column(int i) | ||
264 | { | ||
265 | return entries[i]; | ||
266 | } | ||
267 | |||
268 | const Vector3D& Matrix3x3::column(int i) const | ||
269 | { | ||
270 | return entries[i]; | ||
271 | } | ||