Expression Templates Library (ETL)
inplace_assignable.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
8 #pragma once
9 
10 #include "etl/impl/transpose.hpp"
11 #include "etl/impl/fft.hpp"
12 
18 namespace etl {
19 
25 template <typename D>
27  using derived_t = D;
28 
33  derived_t& as_derived() noexcept {
34  return *static_cast<derived_t*>(this);
35  }
36 
42  template <typename E>
44  as_derived() *= e;
45 
46  return as_derived();
47  }
48 
53  if constexpr (is_2d<derived_t>) {
54  as_derived().ensure_cpu_up_to_date();
55 
56  std::reverse(as_derived().begin(), as_derived().end());
57  }
58 
59  return as_derived();
60  }
61 
66  decltype(auto) mat = as_derived();
67 
68  if constexpr (is_3d<D>) {
69  for (size_t i = 0; i < etl::dim<0>(mat); ++i) {
70  mat(i).fflip_inplace();
71  }
72  } else {
73  for (size_t i = 0; i < etl::dim<0>(mat); ++i) {
74  mat(i).deep_fflip_inplace();
75  }
76  }
77 
78  return mat;
79  }
80 
85  decltype(auto) mat = as_derived();
86 
87  if constexpr (is_dyn_matrix<derived_t>) {
88  if constexpr (is_3d<derived_t>) {
89  for (size_t i = 0; i < etl::dim<0>(mat); ++i) {
90  mat(i).direct_transpose_inplace();
91  }
92  } else {
93  for (size_t i = 0; i < etl::dim<0>(mat); ++i) {
94  mat(i).direct_deep_transpose_inplace();
95  }
96  }
97 
98  static constexpr size_t d = etl_traits<derived_t>::dimensions();
99 
100  using std::swap;
101  swap(mat.unsafe_dimension_access(d - 1), mat.unsafe_dimension_access(d - 2));
102  } else {
103  if constexpr (is_3d<derived_t>) {
104  for (size_t i = 0; i < etl::dim<0>(mat); ++i) {
105  mat(i).transpose_inplace();
106  }
107  } else {
108  for (size_t i = 0; i < etl::dim<0>(mat); ++i) {
109  mat(i).deep_transpose_inplace();
110  }
111  }
112  }
113 
114  return mat;
115  }
116 
121  decltype(auto) mat = as_derived();
122 
123  if constexpr (is_3d<derived_t>) {
124  for (size_t i = 0; i < etl::dim<0>(mat); ++i) {
125  mat(i).direct_transpose_inplace();
126  }
127  } else {
128  for (size_t i = 0; i < etl::dim<0>(mat); ++i) {
129  mat(i).direct_deep_transpose_inplace();
130  }
131  }
132 
133  return mat;
134  }
135 
142  decltype(auto) mat = as_derived();
143 
144  if constexpr (is_dyn_matrix<derived_t>) {
145  if (etl::dim<0>(mat) == etl::dim<1>(mat)) {
147  } else {
149 
150  using std::swap;
151  swap(mat.unsafe_dimension_access(0), mat.unsafe_dimension_access(1));
152  }
153  } else {
154  cpp_assert(etl::dim<0>(mat) == etl::dim<1>(mat), "Only square fast matrices can be tranposed inplace");
155 
157  }
158 
159  return mat;
160  }
161 
168  decltype(auto) mat = as_derived();
169 
170  if (etl::dim<0>(mat) == etl::dim<1>(mat)) {
172  } else {
174  }
175 
176  return mat;
177  }
178 
182  derived_t& fft_inplace() requires(etl_1d<derived_t> && etl_complex_expr<derived_t>) {
183  decltype(auto) mat = as_derived();
184 
186 
187  return mat;
188  }
189 
195  derived_t& fft_many_inplace() requires(matrix<derived_t> && etl_complex_expr<derived_t>) {
196  decltype(auto) mat = as_derived();
197 
199 
200  return mat;
201  }
202 
206  derived_t& ifft_inplace() requires(etl_1d<derived_t> && etl_complex_expr<derived_t>) {
207  decltype(auto) mat = as_derived();
208 
210 
211  return mat;
212  }
213 
217  derived_t& ifft_many_inplace() requires(matrix<derived_t> && etl_complex_expr<derived_t>) {
218  decltype(auto) mat = as_derived();
219 
221 
222  return mat;
223  }
224 
228  derived_t& fft2_inplace() requires(etl_2d<derived_t> && etl_complex_expr<derived_t>) {
229  decltype(auto) mat = as_derived();
230 
232 
233  return mat;
234  }
235 
241  derived_t& fft2_many_inplace() requires(deep_mat<derived_t> && etl_complex_expr<derived_t>) {
242  decltype(auto) mat = as_derived();
243 
245 
246  return mat;
247  }
248 
252  derived_t& ifft2_inplace() requires(etl_2d<derived_t> && etl_complex_expr<derived_t>) {
253  decltype(auto) mat = as_derived();
254 
256 
257  return mat;
258  }
259 
263  derived_t& ifft2_many_inplace() requires(deep_mat<derived_t> && etl_complex_expr<derived_t>) {
264  decltype(auto) mat = as_derived();
265 
267 
268  return mat;
269  }
270 };
271 
272 } //end of namespace etl
derived_t & deep_transpose_inplace()
Transpose each sub 2D matrix in place.
Definition: inplace_assignable.hpp:84
derived_t & deep_fflip_inplace()
Fully flip each sub 2D matrix in place.
Definition: inplace_assignable.hpp:65
derived_t & direct_deep_transpose_inplace()
Transpose each sub 2D matrix in place.
Definition: inplace_assignable.hpp:120
derived_t & as_derived() noexcept
Returns a reference to the derived object, i.e. the object using the CRTP injector.
Definition: inplace_assignable.hpp:33
static void apply(C &&c)
Apply the functor.
Definition: fft.hpp:613
static void apply(C &&c)
Apply the functor.
Definition: fft.hpp:537
D D
The number of dimensions.
Definition: dyn_matrix_view.hpp:24
derived_t & ifft_many_inplace() requires(matrix< derived_t > &&etl_complex_expr< derived_t >)
Perform many inplace 1D Inverse FFT of the vector.
Definition: inplace_assignable.hpp:217
derived_t & direct_transpose_inplace() requires(etl_2d< derived_t >)
Transpose the matrix in place.
Definition: inplace_assignable.hpp:167
Implementations of inplace matrix transposition.
Root namespace for the ETL library.
Definition: adapter.hpp:15
static constexpr size_t dimensions()
Return the number of dimensions of the expression.
Definition: traits_base.hpp:31
derived_t & ifft_inplace() requires(etl_1d< derived_t > &&etl_complex_expr< derived_t >)
Perform inplace 1D Inverse FFT of the vector.
Definition: inplace_assignable.hpp:206
derived_t & fft2_many_inplace() requires(deep_mat< derived_t > &&etl_complex_expr< derived_t >)
Perform many inplace 2D FFT of the matrix.
Definition: inplace_assignable.hpp:241
static void apply(C &&c)
Apply the functor.
Definition: fft.hpp:727
CRTP class to inject inplace operations to matrix and vector structures.
Definition: inplace_assignable.hpp:26
void swap(custom_dyn_matrix_impl< T, SO, D > &lhs, custom_dyn_matrix_impl< T, SO, D > &rhs)
Swap two dyn matrix.
Definition: custom_dyn.hpp:403
derived_t & scale_inplace(E &&e)
Scale the matrix by the factor e, in place.
Definition: inplace_assignable.hpp:43
static void apply(C &&c)
Apply the functor.
Definition: fft.hpp:955
derived_t & ifft2_inplace() requires(etl_2d< derived_t > &&etl_complex_expr< derived_t >)
Perform inplace 2D Inverse FFT of the matrix.
Definition: inplace_assignable.hpp:252
requires(D > 0) struct dyn_base
Matrix with run-time fixed dimensions.
Definition: dyn_base.hpp:113
derived_t & fft_inplace() requires(etl_1d< derived_t > &&etl_complex_expr< derived_t >)
Perform inplace 1D FFT of the vector.
Definition: inplace_assignable.hpp:182
static void apply(C &&c)
Apply the functor.
Definition: fft.hpp:879
static void apply(C &&c)
Apply the functor.
Definition: fft.hpp:347
static void apply(C &&c)
Tranpose c inplace.
Definition: transpose.hpp:302
Matrix with compile-time fixed dimensions.
Definition: custom_fast.hpp:27
derived_t & fft_many_inplace() requires(matrix< derived_t > &&etl_complex_expr< derived_t >)
Perform many inplace 1D FFT of the matrix.
Definition: inplace_assignable.hpp:195
derived_t & fft2_inplace() requires(etl_2d< derived_t > &&etl_complex_expr< derived_t >)
Perform inplace 2D FFT of the matrix.
Definition: inplace_assignable.hpp:228
derived_t & ifft2_many_inplace() requires(deep_mat< derived_t > &&etl_complex_expr< derived_t >)
Perform many inplace 2D Inverse FFT of the matrix.
Definition: inplace_assignable.hpp:263
static void apply(C &&c)
Apply the functor.
Definition: fft.hpp:423
static void apply(C &&c)
Tranpose c inplace.
Definition: transpose.hpp:269
derived_t & fflip_inplace() requires(mat_or_vec< derived_t >)
Flip the matrix horizontally and vertically, in place.
Definition: inplace_assignable.hpp:52
derived_t & transpose_inplace() requires(etl_2d< derived_t >)
Transpose the matrix in place.
Definition: inplace_assignable.hpp:141
static void apply(C &&c)
Apply the functor.
Definition: fft.hpp:803