Loading...
Searching...
No Matches
matrix_3x3.hpp
Go to the documentation of this file.
1#pragma once
2
3/**
4 * Matrix3x3: a 3x3 real matrix for lattice, rotation, and stress tensor operations.
5 *
6 * Stored row-major with each row as a Vector3D. Provides matrix-vector product,
7 * transpose, determinant, inverse, and scalar multiplication.
8 */
9
11
12namespace q3 {
13
14/// 3×3 matrix stored row-major: row i = mat[i]
15struct [[nodiscard]] Matrix3x3 {
16 Vector3D rows[3];
17
18 Matrix3x3() = default;
19 Matrix3x3(Vector3D row_0, Vector3D row_1, Vector3D row_2) : rows{row_0, row_1, row_2} {}
20
21 [[nodiscard]] const Vector3D& operator[](int row_index) const {
22 return rows[row_index];
23 }
24 Vector3D& operator[](int row_index) {
25 return rows[row_index];
26 }
27
28 /// Matrix-vector product M * vector
29 [[nodiscard]] Vector3D operator*(const Vector3D& vector) const {
30 return {rows[0].dot(vector), rows[1].dot(vector), rows[2].dot(vector)};
31 }
32
33 /// Transpose
34 [[nodiscard]] Matrix3x3 transpose() const {
35 return {{rows[0].x, rows[1].x, rows[2].x},
36 {rows[0].y, rows[1].y, rows[2].y},
37 {rows[0].z, rows[1].z, rows[2].z}};
38 }
39
40 /// Column column_index as a Vector3D
41 [[nodiscard]] Vector3D column(int column_index) const {
42 return {rows[0][column_index], rows[1][column_index], rows[2][column_index]};
43 }
44
45 /// Scalar multiplication
46 [[nodiscard]] Matrix3x3 operator*(Real scalar) const {
47 return {rows[0] * scalar, rows[1] * scalar, rows[2] * scalar};
48 }
49
50 /// Determinant
51 [[nodiscard]] Real determinant() const {
52 return rows[0].x * (rows[1].y * rows[2].z - rows[1].z * rows[2].y) -
53 rows[0].y * (rows[1].x * rows[2].z - rows[1].z * rows[2].x) +
54 rows[0].z * (rows[1].x * rows[2].y - rows[1].y * rows[2].x);
55 }
56
57 /// Inverse (assumes non-singular)
58 [[nodiscard]] Matrix3x3 inverse() const {
59 Real determinant_value = determinant();
60 Matrix3x3 inverse_matrix;
61 inverse_matrix[0] = {(rows[1].y * rows[2].z - rows[1].z * rows[2].y) / determinant_value,
62 (rows[0].z * rows[2].y - rows[0].y * rows[2].z) / determinant_value,
63 (rows[0].y * rows[1].z - rows[0].z * rows[1].y) / determinant_value};
64 inverse_matrix[1] = {(rows[1].z * rows[2].x - rows[1].x * rows[2].z) / determinant_value,
65 (rows[0].x * rows[2].z - rows[0].z * rows[2].x) / determinant_value,
66 (rows[0].z * rows[1].x - rows[0].x * rows[1].z) / determinant_value};
67 inverse_matrix[2] = {(rows[1].x * rows[2].y - rows[1].y * rows[2].x) / determinant_value,
68 (rows[0].y * rows[2].x - rows[0].x * rows[2].y) / determinant_value,
69 (rows[0].x * rows[1].y - rows[0].y * rows[1].x) / determinant_value};
70 return inverse_matrix;
71 }
72};
73
74} // namespace q3
double Real
3×3 matrix stored row-major: row i = mat[i]
Matrix3x3 transpose() const
Transpose.
Vector3D operator*(const Vector3D &vector) const
Matrix-vector product M * vector.
Matrix3x3()=default
Real determinant() const
Determinant.
Matrix3x3(Vector3D row_0, Vector3D row_1, Vector3D row_2)
Vector3D & operator[](int row_index)
Matrix3x3 inverse() const
Inverse (assumes non-singular)
Matrix3x3 operator*(Real scalar) const
Scalar multiplication.
const Vector3D & operator[](int row_index) const
Vector3D column(int column_index) const
Column column_index as a Vector3D.
Real dot(const Vector3D &other) const
Definition vector_3d.hpp:59