1 #ifndef COTILA_MATRIX_UTILITY_H_ 2 #define COTILA_MATRIX_UTILITY_H_ 22 typename F,
typename T,
typename... Matrices,
23 typename U = std::invoke_result_t<F, T,
typename Matrices::value_type...>,
25 detail::all_same_value<std::size_t, Matrices::column_size...>::value,
27 detail::all_same_value<std::size_t, Matrices::row_size...>::value>
29 const Matrices &... matrices) {
31 for (std::size_t i = 0; i < N; ++i) {
32 for (std::size_t j = 0; j < M; ++j) {
34 std::apply(f, std::forward_as_tuple(m[i][j], matrices[i][j]...));
46 template <
typename T,
typename U, std::
size_t N, std::
size_t M>
48 return elementwise([](
const U u) {
return static_cast<T
>(u); }, m);
57 template <std::
size_t N, std::
size_t M,
typename F>
61 for (std::size_t i = 0; i < N; ++i) {
62 for (std::size_t j = 0; j < M; ++j) {
63 generated[i][j] = std::apply(f, std::forward_as_tuple(i, j));
75 template <std::
size_t N, std::
size_t M,
typename T>
77 return generate<N, M>([value](std::size_t, std::size_t) {
return value; });
84 template <
typename T, std::
size_t N>
86 = generate<N, N>([](std::size_t i, std::size_t j) {
return T(i == j ? 1 : 0); });
98 template <std::
size_t Row, std::
size_t Col, std::
size_t M, std::
size_t N,
typename T>
100 return generate<M * Row, N * Col>([&m](std::size_t i, std::size_t j) {
101 return m[i % M][j % N];
114 template <std::
size_t M, std::
size_t N,
typename T>
116 for (
int i = 0; i < N; i++)
134 template <std::
size_t M, std::
size_t N,
typename T>
136 for (
int i = 0; i < N; i++)
153 template<std::
size_t M, std::
size_t N, std::
size_t P,
typename T>
155 return generate<M, N+P>([&a, &b](std::size_t i, std::size_t j){
156 return j < N ? a[i][j] : b[i][j - N];
168 template<std::
size_t M, std::
size_t N, std::
size_t P,
typename T>
170 return generate<M + N, P>([&a, &b](std::size_t i, std::size_t j){
171 return i < M ? a[i][j] : b[i - M][j];
184 template<std::
size_t P, std::
size_t Q, std::
size_t M, std::
size_t N,
typename T>
186 if ((a + P > M) || (b + Q > N))
throw "index out of range";
187 return generate<P, Q>([&m, &a, &b](std::size_t i, std::size_t j){
188 return m[a + i][b + j];
198 template <std::size_t P, std::size_t Q, std::size_t M, std::size_t N,
201 static_assert(P * Q == M * N,
"Reshaped matrix must preserve size! P*Q != M*N");
202 return generate<P, Q>([&m](
203 std::size_t i, std::size_t j) {
204 return m[(i * Q + j) / N][(i * Q + j) % N];
214 template <
typename T, std::
size_t N>
216 return generate<N, 1>([&v](
auto i,
auto) {
return v[i]; });
225 template <
typename T, std::
size_t N>
227 return generate<1, N>([&v](
auto,
auto j) {
return v[j]; });
234 #endif // COTILA_MATRIX_UTILITY_H_ constexpr matrix< T, M, N > swapcol(matrix< T, M, N > m, std::size_t a, std::size_t b)
swaps columns of a matrix
Definition: utility.h:135
constexpr matrix< T, M+N, P > vertcat(const matrix< T, M, P > &a, const matrix< T, N, P > &b)
vertically concatenates two matrices
Definition: utility.h:169
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 vector< U, N > elementwise(F f, const vector< T, N > &v, const Vectors &... vectors)
applies a function elementwise between many vectors
Definition: utility.h:26
constexpr matrix< T, M, N > swaprow(matrix< T, M, N > m, std::size_t a, std::size_t b)
swaps rows of a matrix
Definition: utility.h:115
constexpr matrix< T, N, N > identity
the matrix identity
Definition: utility.h:86
A container representing a vector.
Definition: vector.h:25
Contains the definition of the cotila::matrix class.
constexpr matrix< T, N, 1 > as_column(const vector< T, N > &v)
converts a vector into a column vector
Definition: utility.h:215
constexpr matrix< T, P, Q > submat(const matrix< T, M, N > &m, std::size_t a, std::size_t b)
extracts the submatrix of a matrix
Definition: utility.h:185
constexpr matrix< T, 1, N > as_row(const vector< T, N > &v)
converts a vector into a row vector
Definition: utility.h:226
A container representing a matrix.
Definition: matrix.h:25
constexpr matrix< T, P, Q > reshape(const matrix< T, M, N > &m)
reshapes a matrix
Definition: utility.h:200
constexpr vector< T, N > cast(const vector< U, N > &v)
casts a vector to another type
Definition: utility.h:57
constexpr vector< T, N > fill(T value)
generates a vector containing a single value
Definition: utility.h:95
decltype(auto) constexpr generate(F &&f)
generates a vector as a function of its index
Definition: utility.h:108
constexpr matrix< T, M *Row, N *Col > repmat(const matrix< T, M, N > &m)
repeats a matrix
Definition: utility.h:99