ARTS 2.5.9 (git: 825fa5f2)
matpack_eigen.h
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4
5#include "matpackI.h"
6#include "matpack_complex.h"
7#include "matpack_concepts.h"
8
9#pragma GCC diagnostic push
10#pragma GCC diagnostic ignored "-Wshadow"
11#pragma GCC diagnostic ignored "-Wconversion"
12#pragma GCC diagnostic ignored "-Wfloat-conversion"
13#if defined(__clang__)
14#pragma GCC diagnostic ignored "-Wdeprecated-copy-with-dtor"
15#pragma GCC diagnostic ignored "-Wdeprecated-copy-with-user-provided-dtor"
16#else
17#pragma GCC diagnostic ignored "-Wclass-memaccess"
18#endif
19
20#include <Eigen/Dense>
21
22#pragma GCC diagnostic pop
23
24namespace matpack::eigen {
25
27template <typename internal_type, bool constant_type = true>
28struct eigen_type {
29 using stride_type = Eigen::Stride<Eigen::Dynamic, Eigen::Dynamic>;
30 using matrix_type = Eigen::
31 Matrix<internal_type, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
32 using constant_matrix_type = std::add_const_t<matrix_type>;
33 using mutable_matrix_type = std::remove_const_t<matrix_type>;
34 using map = Eigen::Map<std::conditional_t<constant_type,
37 Eigen::Unaligned,
39};
40
42template <typename internal_type, bool constant_type = true>
44
46template <typename internal_type, bool constant_type = true>
49
51auto col_vec(matpack::vector auto&& x) {
52 using internal_type =
53 std::remove_cvref_t<std::remove_pointer_t<decltype(x.get_c_array())>>;
54 constexpr bool constant_type = std::is_const_v<decltype(x)>;
55 constexpr bool real_type = std::is_same_v<internal_type, Numeric>;
56 using matrix_type_real =
57 std::conditional_t<real_type, Eigen::RowVectorXd, Eigen::RowVectorXcd>;
58 using matrix_type = std::
59 conditional_t<constant_type, const matrix_type_real, matrix_type_real>;
60 using stride_type = Eigen::InnerStride<Eigen::Dynamic>;
61 using map_type = Eigen::Map<matrix_type, Eigen::Unaligned, stride_type>;
62
63 static_assert(real_type or std::is_same_v<internal_type, Complex>,
64 "Only for Complex and Numeric");
65
66 return map_type(
67 x.get_c_array() + x.selem(), x.nelem(), stride_type{x.delem()});
68}
69
71auto row_vec(matpack::vector auto&& x) {
72 using internal_type =
73 std::remove_cvref_t<std::remove_pointer_t<decltype(x.get_c_array())>>;
74 constexpr bool constant_type = std::is_const_v<decltype(x)>;
75 constexpr bool real_type = std::is_same_v<internal_type, Numeric>;
76 using matrix_type_real =
77 std::conditional_t<real_type, Eigen::VectorXd, Eigen::VectorXcd>;
78 using matrix_type = std::
79 conditional_t<constant_type, const matrix_type_real, matrix_type_real>;
80 using stride_type = Eigen::InnerStride<Eigen::Dynamic>;
81 using map_type = Eigen::Map<matrix_type, Eigen::Unaligned, stride_type>;
82
83 static_assert(real_type or std::is_same_v<internal_type, Complex>,
84 "Only for Complex and Numeric");
85
86 return map_type(
87 x.get_c_array() + x.selem(), x.nelem(), stride_type{x.delem()});
88}
89
91auto mat(matpack::matrix auto&& x) {
92 using internal_type =
93 std::remove_cvref_t<std::remove_pointer_t<decltype(x.get_c_array())>>;
94 constexpr bool constant_type = std::is_const_v<decltype(x)>;
95
98
99 return matrix_map(x.get_c_array() + x.selem(),
100 x.nrows(),
101 x.ncols(),
102 stride_type(x.drows(), x.dcols()));
103}
104
106template <typename T>
107concept standard_vector = requires(T a) {
108 a.data();
109 { a.size() } -> std::integral;
110 { a[0] } -> complex_or_real;
111};
112
114auto col_vec(standard_vector auto&& x) {
115 using internal_type =
116 std::remove_cvref_t<std::remove_pointer_t<decltype(x.data())>>;
117
118 using stride_type = eigen_stride<internal_type>;
119 using matrix_map = eigen_map<internal_type>;
120
121 return matrix_map(x.data(), 1, x.size(), stride_type(1, 1));
122}
123
125auto row_vec(standard_vector auto&& x) {
126 using internal_type =
127 std::remove_cvref_t<std::remove_pointer_t<decltype(x.data())>>;
128
129 using stride_type = eigen_stride<internal_type>;
130 using matrix_map = eigen_map<internal_type>;
131
132 return matrix_map(x.data(), x.size(), 1, stride_type(1, 1));
133}
134
136template <typename T>
137concept arithmetic = std::is_arithmetic_v<T>;
138
141 using t = decltype(x);
142 if constexpr(matpack::matrix<t>) return mat(std::forward<t>(x));
143 else return row_vec(std::forward<t>(x));
144}
145} // namespace matpack::eigen
146
147auto operator*(matpack::matrix auto&& A, matpack::vector auto&& x) {
148 return matpack::eigen::mat(std::forward<decltype(A)>(A)) *
149 matpack::eigen::row_vec(std::forward<decltype(x)>(x));
150}
151
152auto operator*(matpack::matrix auto&& A, matpack::matrix auto&& B) {
153 return matpack::eigen::mat(std::forward<decltype(A)>(A)) *
154 matpack::eigen::mat(std::forward<decltype(B)>(B));
155}
156
158 return std::forward<decltype(a)>(a) *
159 matpack::eigen::as_eigen(std::forward<decltype(b)>(b));
160}
161
163 return matpack::eigen::as_eigen(std::forward<decltype(x)>(x)) +
164 matpack::eigen::as_eigen(std::forward<decltype(y)>(y));
165}
166
168 return matpack::eigen::as_eigen(std::forward<decltype(x)>(x)) -
169 matpack::eigen::as_eigen(std::forward<decltype(y)>(y));
170}
171
172
173template <typename Derived>
174auto operator*(Eigen::MatrixBase<Derived>&& A, matpack::matrix_or_vector auto&& x) {
175 return std::forward<decltype(A)>(A) *
176 matpack::eigen::as_eigen(std::forward<decltype(x)>(x));
177}
178
179template <typename Derived>
180auto operator+(Eigen::MatrixBase<Derived>&& A, matpack::matrix_or_vector auto&& x) {
181 return std::forward<decltype(A)>(A) +
182 matpack::eigen::as_eigen(std::forward<decltype(x)>(x));
183}
184
185template <typename Derived>
186auto operator-(Eigen::MatrixBase<Derived>&& A, matpack::matrix_or_vector auto&& x) {
187 return std::forward<decltype(A)>(A) -
188 matpack::eigen::as_eigen(std::forward<decltype(x)>(x));
189}
190
191
192template <typename Derived>
193auto operator*(matpack::matrix_or_vector auto&& A, Eigen::MatrixBase<Derived>&& x) {
194 return matpack::eigen::as_eigen(std::forward<decltype(A)>(A)) *
195 std::forward<decltype(x)>(x);
196}
197
198template <typename Derived>
199auto operator+(matpack::matrix_or_vector auto&& x, Eigen::MatrixBase<Derived>&& y) {
200 return matpack::eigen::as_eigen(std::forward<decltype(x)>(x)) +
201 std::forward<decltype(y)>(y);
202}
203
204template <typename Derived>
205auto operator-(matpack::matrix_or_vector auto&& x, Eigen::MatrixBase<Derived>&& y) {
206 return matpack::eigen::as_eigen(std::forward<decltype(x)>(x)) -
207 std::forward<decltype(y)>(y);
208}
A concept to state if the type is a floating point or a floating point complex.
A generic concept we might want to move out of here.
Test if the type represents a standard layout std::vector or std::array.
A concept for any of the Arts matrix types.
A concept for any of the Arts vector types.
Implementation of Matrix, Vector, and such stuff.
auto operator*(matpack::matrix auto &&A, matpack::vector auto &&x)
auto operator-(matpack::matrix_or_vector auto &&x, matpack::matrix_or_vector auto &&y)
auto operator+(matpack::matrix_or_vector auto &&x, matpack::matrix_or_vector auto &&y)
auto row_vec(matpack::vector auto &&x)
Map the input to a non-owning const-correct Eigen Map representing a column vector.
Definition: matpack_eigen.h:71
auto col_vec(matpack::vector auto &&x)
Map the input to a non-owning const-correct Eigen Map representing a column vector.
Definition: matpack_eigen.h:51
auto as_eigen(matrix_or_vector auto &&x)
Convert using row_vec or mat.
typename eigen_type< internal_type, constant_type >::map eigen_map
An actual map to an eigen type.
Definition: matpack_eigen.h:43
typename eigen_type< internal_type, constant_type >::stride_type eigen_stride
The stride of the eigen type.
Definition: matpack_eigen.h:48
auto mat(matpack::matrix auto &&x)
Map the input to a non-owning const-correct Eigen Map representing a row matrix.
Definition: matpack_eigen.h:91
Setup for eigen mapping.
Definition: matpack_eigen.h:28
std::add_const_t< matrix_type > constant_matrix_type
Definition: matpack_eigen.h:32
Eigen::Map< std::conditional_t< constant_type, constant_matrix_type, mutable_matrix_type >, Eigen::Unaligned, stride_type > map
Definition: matpack_eigen.h:38
Eigen::Matrix< internal_type, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > matrix_type
Definition: matpack_eigen.h:31
Eigen::Stride< Eigen::Dynamic, Eigen::Dynamic > stride_type
Definition: matpack_eigen.h:29
std::remove_const_t< matrix_type > mutable_matrix_type
Definition: matpack_eigen.h:33
#define a
#define b