4 #ifndef COTILA_MATRIX_MATH_H_ 5 #define COTILA_MATRIX_MATH_H_ 13 #include <cotila/matrix/utility.h> 14 #include <cotila/detail/assert.h> 29 template <
typename T, std::
size_t M, std::
size_t N>
41 template <
typename T, std::
size_t M, std::
size_t N>
54 template <
typename T, std::
size_t M, std::
size_t N>
55 constexpr matrix<detail::remove_complex_t<T>, M, N>
67 template <
typename T, std::
size_t M, std::
size_t N>
69 return generate<N, M>([&m](
auto i,
auto j){
return m[j][i];});
79 template <
typename T, std::
size_t M, std::
size_t N>
92 template <
typename T, std::
size_t M, std::
size_t N, std::
size_t P>
95 return generate<M, P>([&a, &b](
auto i,
auto j){
return cotila::sum(a.row(i)*b.
column(j));});
107 template <
typename T, std::size_t M, std::size_t N,
108 std::size_t P, std::size_t Q>
111 return generate<M * P, N * Q>([&a, &b](
auto i,
auto j) {
return a[i / P][j / Q] * b[i % P][j % Q]; });
122 template <
typename T, std::
size_t M, std::
size_t N>
125 generate<N>([&m](std::size_t i) {
return sum(
abs(m.
column(i))); }));
136 template <
typename T, std::
size_t M, std::
size_t N>
138 return max(generate<M>([&m](std::size_t i) {
return sum(
abs(m.
row(i))); }));
142 template <
typename T, std::
size_t M, std::
size_t N>
143 constexpr std::tuple<matrix<T, M, N>, std::size_t, T>
145 COTILA_DETAIL_ASSERT_FLOATING_POINT(T)
146 COTILA_DETAIL_ASSERT_REAL(T)
149 auto negligible = [&tolerance](
const T &v) {
return abs(v) < tolerance; };
152 std::size_t
rank = 0;
153 std::size_t i = 0, j = 0;
154 while (i < M && j < N) {
156 for (std::size_t ip = i + 1; ip < M; ++ip) {
157 if (
abs(m[ip][j]) >
abs(m[i][j])) {
158 for (std::size_t jp = 0; jp < N; ++jp) {
159 auto tmp = m[ip][jp];
160 m[ip][jp] = m[i][jp];
169 if (!negligible(m[i][j])) {
172 for (std::size_t jp = 0; jp < N; ++jp)
177 for (std::size_t ip = 0; ip < M; ++ip) {
180 if (!negligible(m[ip][j])) {
183 for (std::size_t jp = 0; jp < N; ++jp)
184 m[ip][jp] -= s * m[i][jp];
197 det = (rank == M) ? det : 0;
198 return {m,
rank, det};
202 template <
typename T, std::
size_t M, std::
size_t N>
203 constexpr std::tuple<matrix<T, M, N>, std::size_t, T>
205 T tol =
std::max(N, M) * std::numeric_limits<T>::epsilon() *
mars(m);
206 return gauss_jordan_impl(m, tol);
219 template <
typename T, std::
size_t M, std::
size_t N>
221 return std::get<0>(gauss_jordan_impl(m));
234 template <
typename T, std::
size_t M, std::
size_t N>
236 return std::get<0>(gauss_jordan_impl(m), tolerance);
245 template <
typename T, std::
size_t M, std::
size_t N>
247 return std::get<1>(gauss_jordan_impl(m));
256 template <
typename T, std::
size_t M>
258 return std::get<2>(gauss_jordan_impl(m));
269 template <
typename T, std::
size_t M>
272 throw "matrix is not invertible";
273 return submat<M, M>(
rref(
horzcat(m, identity<T, M>)), 0, M);
283 template <
typename T, std::
size_t M>
285 return sum(generate<M>([&m](std::size_t i){
return m[i][i]; }));
292 #endif // COTILA_MATRIX_MATH_H_ constexpr vector< detail::remove_complex_t< T >, N > abs(const vector< T, N > &v)
computes the elementwise absolute value
Definition: math.h:73
constexpr T trace(const matrix< T, M, M > &m)
computes the trace
Definition: math.h:284
constexpr T macs(const matrix< T, M, N > &m)
Computes the maximum absolute column sum norm.
Definition: math.h:123
Mathematical operations on vectors.
constexpr matrix< T, M, N+P > horzcat(const matrix< T, M, N > &a, const matrix< T, M, P > &b)
horizontally concatenates two matrices
Definition: utility.h:154
constexpr matrix< T, N, M > transpose(const matrix< T, M, N > &m)
computes the transpose
Definition: math.h:68
constexpr std::size_t rank(const matrix< T, M, N > &m)
Compute the rank.
Definition: math.h:246
constexpr T max(const vector< T, N > &v)
computes the maximum valued element
Definition: math.h:119
constexpr vector< detail::remove_complex_t< T >, N > real(const vector< T, N > &v)
computes the elementwise real
Definition: math.h:50
constexpr matrix< T, M, P > matmul(const matrix< T, M, N > &a, const matrix< T, N, P > &b)
computes the matrix product
Definition: math.h:93
constexpr T det(const matrix< T, M, M > &m)
Compute the determinant.
Definition: math.h:257
Contains the definition of the cotila::matrix class.
constexpr vector< T, M > row(std::size_t i) const
access specified row
Definition: matrix.h:43
constexpr matrix< T, M, M > inverse(const matrix< T, M, M > &m)
computes the matrix inverse
Definition: math.h:270
constexpr vector< T, N > column(std::size_t i) const
access specified column
Definition: matrix.h:55
Contains the definition of the cotila::vector class.
A container representing a matrix.
Definition: matrix.h:25
constexpr vector< detail::remove_complex_t< T >, N > imag(const vector< T, N > &v)
computes the elementwise imag
Definition: math.h:62
constexpr matrix< U, N, M > elementwise(F f, const matrix< T, N, M > &m, const Matrices &... matrices)
applies a function elementwise between many matrices
Definition: utility.h:28
constexpr matrix< T, M, N > rref(const matrix< T, M, N > &m, T tolerance)
Compute the reduced row echelon form.
Definition: math.h:235
constexpr matrix< T, M *P, N *Q > kron(const matrix< T, M, N > &a, const matrix< T, P, Q > &b)
Computes the kronecker tensor product.
Definition: math.h:109
Mathematical operations on scalar values.
constexpr matrix< T, N, M > hermitian(const matrix< T, M, N > &m)
computes the Hermitian transpose
Definition: math.h:80
constexpr T sum(const vector< T, N > &v)
computes the sum of elements
Definition: math.h:99
constexpr T mars(const matrix< T, M, N > &m)
Computes the maximum absolute row sum norm.
Definition: math.h:137
constexpr vector< T, N > conj(const vector< T, N > &v)
computes the elementwise complex conjugate
Definition: math.h:27