Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
Matrix3x3.hpp
Go to the documentation of this file.
1#ifndef PHYTH_MATRIX3X3_HPP
2#define PHYTH_MATRIX3X3_HPP
3
5#include "Matrix.hpp"
6#include "Vector3.hpp"
7#include <ostream>
8#include <stdexcept>
9
10namespace Phyth {
11 template <typename T>
13
26 template<typename T>
27 struct Matrix<3, 3, T> {
28 static_assert(is_quantity_v<T>, "Matrix3x3 only supports Quantity types");
29
33
39 constexpr Matrix() : x_basis(0, 0, 0), y_basis(0, 0, 0), z_basis(0, 0, 0) {
40 }
41
49 constexpr Matrix(const Vector3<T> &col0, const Vector3<T> &col1, const Vector3<T> &col2)
50 : x_basis(col0), y_basis(col1), z_basis(col2) {
51 }
52
75 constexpr Matrix(
76 T m00, T m01, T m02,
77 T m10, T m11, T m12,
78 T m20, T m21, T m22
79 ) : x_basis(m00, m10, m20),
80 y_basis(m01, m11, m21),
81 z_basis(m02, m12, m22) {
82 }
83
94 static constexpr Matrix Identity() {
95 return Matrix(
96 T(1), T(0), T(0),
97 T(0), T(1), T(0),
98 T(0), T(0), T(1)
99 );
100 }
101
111 [[nodiscard]] constexpr Matrix Transpose() const {
112 return Matrix(
113 x_basis.x, y_basis.x, z_basis.x,
114 x_basis.y, y_basis.y, z_basis.y,
115 x_basis.z, y_basis.z, z_basis.z
116 );
117 }
118
129 [[nodiscard]] constexpr T Trace() const {
130 return x_basis.x + y_basis.y + z_basis.z;
131 }
132
143 [[nodiscard]] constexpr auto Symmetric() const {
144 return (*this + this->Transpose()) * T(0.5);
145 }
146
157 [[nodiscard]] constexpr auto Antisymmetric() const {
158 return (*this - this->Transpose()) * T(0.5);
159 }
160
172 [[nodiscard]] const T& At(const int i, const int j) const {
173 if (i < 0 || i > 2 || j < 0 || j > 2) {
174 throw std::out_of_range("Matrix3x3::at: index out of range");
175 }
176 switch (j) {
177 case 0: return x_basis[i];
178 case 1: return y_basis[i];
179 case 2: return z_basis[i];
180 default: throw std::out_of_range("Matrix3x3::at: invalid column");
181 }
182 }
183
195 T& At(const int i, const int j) {
196 if (i < 0 || i > 2 || j < 0 || j > 2) {
197 throw std::out_of_range("Matrix3x3::at: index out of range");
198 }
199 switch (j) {
200 case 0: return x_basis[i];
201 case 1: return y_basis[i];
202 case 2: return z_basis[i];
203 default: throw std::out_of_range("Matrix3x3::at: invalid column");
204 }
205 }
206
220 [[nodiscard]] constexpr const T& operator()(const int i, const int j) const {
221 if (j == 0) return x_basis[i];
222 if (j == 1) return y_basis[i];
223 return z_basis[i];
224 }
225
239 constexpr T& operator()(const int i, const int j) {
240 if (j == 0) return x_basis[i];
241 if (j == 1) return y_basis[i];
242 return z_basis[i];
243 }
244
255 template<typename U>
256 constexpr auto operator+(const Matrix3x3<U> &other) const {
257 return Matrix3x3<decltype(x_basis + other.x_basis)>(
258 x_basis + other.x_basis,
259 y_basis + other.y_basis,
260 z_basis + other.z_basis
261 );
262 }
263
274 template<typename U>
275 constexpr auto operator-(const Matrix3x3<U> &other) const {
276 return Matrix3x3<decltype(x_basis - other.x_basis)>(
277 x_basis - other.x_basis,
278 y_basis - other.y_basis,
279 z_basis - other.z_basis
280 );
281 }
282
291 constexpr auto operator-() const {
292 return Matrix(-x_basis, -y_basis, -z_basis);
293 }
294
305 template<typename U>
306 constexpr auto operator*(U scalar) const {
308 x_basis * scalar,
309 y_basis * scalar,
310 z_basis * scalar
311 );
312 }
313
324 template<typename U>
325 constexpr auto operator*(const Vector3<U> &v) const {
326 // M * v = x_basis * v.x + y_basis * v.y + z_basis * v.z
327 return x_basis * v.x + y_basis * v.y + z_basis * v.z;
328 }
329
340 template<typename U>
341 constexpr auto operator*(const Matrix3x3<U> &other) const {
342 // A * B: column j of result = A * (column j of B)
343 return Matrix3x3<decltype(*this * other.x_basis)>(
344 *this * other.x_basis,
345 *this * other.y_basis,
346 *this * other.z_basis
347 );
348 }
349
360 template<typename U>
361 constexpr Matrix &operator+=(const Matrix3x3<U> &other) {
362 x_basis += other.x_basis;
363 y_basis += other.y_basis;
364 z_basis += other.z_basis;
365 return *this;
366 }
367
378 template<typename U>
379 constexpr Matrix &operator-=(const Matrix3x3<U> &other) {
380 x_basis -= other.x_basis;
381 y_basis -= other.y_basis;
382 z_basis -= other.z_basis;
383 return *this;
384 }
385
396 template<typename U>
397 constexpr Matrix &operator*=(U scalar) {
398 x_basis *= scalar;
399 y_basis *= scalar;
400 z_basis *= scalar;
401 return *this;
402 }
403
414 template<typename U>
415 constexpr bool operator==(const Matrix3x3<U> &other) const {
416 return x_basis == other.x_basis && y_basis == other.y_basis && z_basis == other.z_basis;
417 }
418
429 template<typename U>
430 constexpr bool operator!=(const Matrix3x3<U> &other) const {
431 return !(*this == other);
432 }
433
443 template<typename UnitT>
446 x_basis.template As<UnitT>(),
447 y_basis.template As<UnitT>(),
448 z_basis.template As<UnitT>()
449 );
450 }
451
462 friend std::ostream& operator<<(std::ostream& os, const Matrix& m) {
463 os << "[" << m.x_basis.x << " " << m.y_basis.x << " " << m.z_basis.x << "\n"
464 << " " << m.x_basis.y << " " << m.y_basis.y << " " << m.z_basis.y << "\n"
465 << " " << m.x_basis.z << " " << m.y_basis.z << " " << m.z_basis.z << "]";
466 return os;
467 }
468 };
469
482 template<typename T, typename U>
483 constexpr auto operator*(T scalar, const Matrix3x3<U> &m) {
484 return m * scalar;
485 }
486}
487
488#endif // PHYTH_MATRIX3X3_HPP
constexpr auto operator*(Quantity< U1 > a, Quantity< U2 > b)
Multiply two quantities.
Definition Quantity.hpp:524
constexpr bool is_unit_v
Definition Unit.hpp:79
3x3 matrix with Quantity components
Definition Matrix3x3.hpp:27
constexpr auto operator*(const Matrix3x3< U > &other) const
Matrix-matrix multiplication: A * B.
constexpr bool operator==(const Matrix3x3< U > &other) const
Equality comparison.
constexpr auto operator-(const Matrix3x3< U > &other) const
Matrix subtraction.
constexpr const T & operator()(const int i, const int j) const
Access element at row i, column j (0-based indexing) - no bounds checking.
constexpr auto operator+(const Matrix3x3< U > &other) const
Matrix addition.
T & At(const int i, const int j)
Access element at row i, column j with bounds checking (0-based indexing) - mutable version.
static constexpr Matrix Identity()
Create an identity matrix.
Definition Matrix3x3.hpp:94
const T & At(const int i, const int j) const
Access element at row i, column j with bounds checking (0-based indexing)
constexpr Matrix(const Vector3< T > &col0, const Vector3< T > &col1, const Vector3< T > &col2)
Construct from three column vectors.
Definition Matrix3x3.hpp:49
friend std::ostream & operator<<(std::ostream &os, const Matrix &m)
Stream insertion operator.
constexpr Matrix & operator*=(U scalar)
Scalar multiplication assignment.
constexpr Matrix()
Construct a zero matrix.
Definition Matrix3x3.hpp:39
constexpr auto operator-() const
Unary negation.
constexpr Matrix Transpose() const
Compute the transpose of the matrix.
constexpr Matrix(T m00, T m01, T m02, T m10, T m11, T m12, T m20, T m21, T m22)
Construct from individual components (row-major ordering)
Definition Matrix3x3.hpp:75
constexpr bool operator!=(const Matrix3x3< U > &other) const
Inequality comparison.
constexpr auto Symmetric() const
Compute the symmetric part of the matrix.
constexpr auto operator*(U scalar) const
Scalar multiplication (matrix * scalar)
constexpr T & operator()(const int i, const int j)
Access element at row i, column j (0-based indexing) - mutable version.
constexpr auto operator*(const Vector3< U > &v) const
Matrix-vector multiplication: M * v.
Matrix3x3< Quantity< UnitT > > As() const
Convert all components to a different unit of the same dimension.
constexpr auto Antisymmetric() const
Compute the antisymmetric part of the matrix.
constexpr T Trace() const
Compute the trace of the matrix.
constexpr Matrix & operator+=(const Matrix3x3< U > &other)
Addition assignment.
constexpr Matrix & operator-=(const Matrix3x3< U > &other)
Subtraction assignment.
constexpr Matrix()
Definition Matrix.hpp:21
constexpr auto Transpose() const
Definition Matrix.hpp:45
3D vector with Quantity components
Definition Vector3.hpp:28