xcal
基于 C++23 的现代图形渲染引擎
载入中...
搜索中...
未找到
mat.hpp
浏览该文件的文档.
1
8#pragma once
9#ifndef XCMATH_MAT_H
10#define XCMATH_MAT_H
11
12#include <cstddef>
13
14#include "../utils/concepts.h"
15#include "./declaration.hpp"
16#include "./vec.hpp"
17
18namespace xcmath {
26template <typename _Tp, size_t _rows, size_t _cols = _rows>
27class mat : public vec<vec<_Tp, _cols>, _rows> {
28 public:
32 static constexpr vec2<size_t> shape{_rows, _cols};
33
37 using vec<vec<_Tp, _cols>, _rows>::vec;
38
42 using vec<vec<_Tp, _cols>, _rows>::operator[];
43
47 using vec<vec<_Tp, _cols>, _rows>::begin;
48
52 using vec<vec<_Tp, _cols>, _rows>::end;
53
57 using vec<vec<_Tp, _cols>, _rows>::operator=;
58
64 template <class _T>
66
68 : vec<vec<_Tp, _cols>, _rows>(o) {}
69
77 template <size_t _ncols>
79 const mat<_Tp, _cols, _ncols>& other) const {
81 for (size_t i = 0; i < _rows; i++) {
82 for (size_t j = 0; j < _ncols; j++) {
83 for (size_t k = 0; k < _cols; k++) {
84 res[i][j] += this->data[i][k] * other[k][j];
85 }
86 }
87 }
88 return res;
89 }
90
98 template <Vec _VTp>
99 requires(_VTp::length == _rows) &&
102 constexpr _VTp operator^(const _VTp& other) const {
103 _VTp res;
104 for (size_t i = 0; i < _rows; i++) {
105 for (size_t j = 0; j < _cols; j++) {
106 res[i] += this->data[i][j] * other[j];
107 }
108 }
109 return res;
110 }
111
117 constexpr mat<_Tp, _cols, _rows> T() const {
119 for (size_t i = 0; i < _rows; i++) {
120 for (size_t j = 0; j < _cols; j++) {
121 res[j][i] = this->data[i][j];
122 }
123 }
124 return res;
125 }
126
127 constexpr mat<_Tp, _cols, _rows> inv() const
128 requires(_rows == _cols)
129 {
130 const _Tp det = this->det();
131 mat<_Tp, _rows, _cols> adjugate;
132 for (size_t i = 0; i < _rows; ++i) {
133 for (size_t j = 0; j < _cols; ++j) {
134 // Compute the submatrix (minor)
135 mat<_Tp, _rows - 1, _cols - 1> submat;
136 size_t sub_i = 0;
137 for (size_t k = 0; k < _rows; ++k) {
138 if (k == i) continue;
139 size_t sub_j = 0;
140 for (size_t l = 0; l < _cols; ++l) {
141 if (l == j) continue;
142 submat[sub_i][sub_j++] = this->data[k][l];
143 }
144 ++sub_i;
145 }
146 // Compute the cofactor (signed minor)
147 _Tp cofactor = ((i + j) % 2 == 0 ? 1 : -1) * submat.det();
148 adjugate[j][i] = cofactor; // Transpose for adjugate
149 }
150 }
151 return adjugate / det;
152 }
153
159 constexpr static mat<_Tp, _rows, _cols> ones() {
160 return Self<_Tp>{vec<_Tp, _cols>{_Tp{1}}};
161 }
162
168 constexpr static mat<_Tp, _rows, _cols> eye() {
170 size_t n = _rows < _cols ? _rows : _cols;
171 for (size_t i = 0; i < n; i++) {
172 res[i][i] = 1;
173 }
174 return res;
175 }
176
182 constexpr _Tp det() const
183 requires(_rows == _cols)
184 {
185 if constexpr (_rows == 1) {
186 return this->data[0][0];
187 } else if constexpr (_rows == 2) {
188 return this->data[0][0] * this->data[1][1] -
189 this->data[0][1] * this->data[1][0];
190 } else {
191 _Tp res = 0;
192 for (size_t j = 0; j < _rows; j++) {
193 mat<_Tp, _rows - 1, _cols - 1> submat;
194 for (size_t i = 1; i < _rows; i++) {
195 for (size_t k = 0; k < j; k++) {
196 submat[i - 1][k] = this->data[i][k];
197 }
198 for (size_t k = j + 1; k < _cols; k++) {
199 submat[i - 1][k - 1] = this->data[i][k];
200 }
201 }
202 res +=
203 ((j % 2 == 0) ? 1 : -1) * this->data[0][j] * submat.det();
204 }
205 return res;
206 }
207 }
208};
209
210} // namespace xcmath
211
212#endif // XCMATH_MAT_H
Matrix class template
Definition mat.hpp:27
constexpr mat< _Tp, _cols, _rows > inv() const
Definition mat.hpp:127
static constexpr mat< _Tp, _rows, _cols > eye()
Create an identity matrix
Definition mat.hpp:168
constexpr mat< _Tp, _cols, _rows > T() const
Transpose operator
Definition mat.hpp:117
static constexpr vec2< size_t > shape
Shape of the matrix (rows x columns)
Definition mat.hpp:32
static constexpr mat< _Tp, _rows, _cols > ones()
Create a matrix filled with ones
Definition mat.hpp:159
constexpr _Tp det() const
Compute determinant of the matrix
Definition mat.hpp:182
constexpr mat< _Tp, _rows, _ncols > operator^(const mat< _Tp, _cols, _ncols > &other) const
Matrix multiplication operator
Definition mat.hpp:78
Vector class template
Definition vec.hpp:206
constexpr vec()
Construct a zero-initialized vector Notw: _Tp must be default constructible
Definition vec.hpp:273
constexpr const _Tp * begin() const
Get pointer to the beginning of the data array
Definition vec.hpp:371
_Tp data[_length]
Component storage array
Definition vec.hpp:212
constexpr const _Tp * end() const
Get pointer to the end of the data array
Definition vec.hpp:378
Declaration of vector, matrix, quaternion, and complex classes
Compiler-specific type information handling for MSVC
Definition complex.hpp:12