Expression Templates Library (ETL)
fft_expr.hpp
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/expr/base_temporary_expr.hpp"
11 
12 namespace etl {
13 
18 template <etl_expr A, typename T, typename Impl>
19 struct fft_expr : base_temporary_expr_un<fft_expr<A, T, Impl>, A> {
20  using value_type = T;
24 
25  static constexpr auto storage_order = sub_traits::storage_order;
26 
31  static constexpr bool gpu_computable = Impl::template gpu_computable<A>;
32 
37  explicit fft_expr(A a) : base_type(a) {
38  //Nothing else to init
39  }
40 
41  // Assignment functions
42 
47  template <same_dimensions<A> C>
48  void assign_to(C&& c) const {
49  inc_counter("temp:assign");
50 
51  Impl::apply(this->a(), c);
52  }
53 
58  template <same_dimensions<A> L>
59  void assign_add_to(L&& lhs) const {
60  std_add_evaluate(*this, lhs);
61  }
62 
67  template <same_dimensions<A> L>
68  void assign_sub_to(L&& lhs) const {
69  std_sub_evaluate(*this, lhs);
70  }
71 
76  template <same_dimensions<A> L>
77  void assign_mul_to(L&& lhs) const {
78  std_mul_evaluate(*this, lhs);
79  }
80 
85  template <same_dimensions<A> L>
86  void assign_div_to(L&& lhs) const {
87  std_div_evaluate(*this, lhs);
88  }
89 
94  template <same_dimensions<A> L>
95  void assign_mod_to(L&& lhs) const {
96  std_mod_evaluate(*this, lhs);
97  }
98 
105  friend std::ostream& operator<<(std::ostream& os, const fft_expr& expr) {
106  return os << "fft(" << expr._a << ")";
107  }
108 };
109 
114 template <typename A, typename T, typename Impl>
115 struct etl_traits<etl::fft_expr<A, T, Impl>> {
117  using sub_expr_t = std::decay_t<A>;
119  using value_type = T;
120 
121  static constexpr bool is_etl = true;
122  static constexpr bool is_transformer = false;
123  static constexpr bool is_view = false;
124  static constexpr bool is_magic_view = false;
125  static constexpr bool is_fast = sub_traits::is_fast;
126  static constexpr bool is_linear = false;
127  static constexpr bool is_thread_safe = true;
128  static constexpr bool is_value = false;
129  static constexpr bool is_direct = true;
130  static constexpr bool is_generator = false;
131  static constexpr bool is_padded = false;
132  static constexpr bool is_aligned = true;
133  static constexpr bool is_temporary = true;
134  static constexpr bool gpu_computable = is_gpu_t<value_type> && cuda_enabled;
135  static constexpr order storage_order = sub_traits::storage_order;
136 
142  template <vector_mode_t V>
143  static constexpr bool vectorizable = true;
144 
149  template <size_t DD>
150  static constexpr size_t dim() {
151  return decay_traits<A>::template dim<DD>();
152  }
153 
160  static size_t dim(const expr_t& e, size_t d) {
161  return etl::dim(e._a, d);
162  }
163 
169  static size_t size(const expr_t& e) {
170  return sub_traits::size(e._a);
171  }
172 
177  static constexpr size_t size() {
178  return sub_traits::size();
179  }
180 
185  static constexpr size_t dimensions() {
186  return sub_traits::dimensions();
187  }
188 
193  static constexpr int complexity() noexcept {
194  return -1;
195  }
196 };
197 
198 //Helpers to compute the type of the result
199 
200 namespace detail {
201 
205 template <typename A>
206 using fft_value_type = std::conditional_t<is_complex<A>, value_t<A>, std::complex<value_t<A>>>;
207 
211 template <typename A>
212 using ifft_value_type = std::conditional_t<is_complex<A>, value_t<A>, std::complex<value_t<A>>>;
213 
217 template <typename A>
218 using ifft_real_value_type = std::conditional_t<is_complex<A>, typename value_t<A>::value_type, value_t<A>>;
219 
220 } //end of namespace detail
221 
227 template <etl_expr A>
228 fft_expr<detail::build_type<A>, detail::fft_value_type<A>, detail::fft1_impl> fft_1d(A&& a) {
229  return fft_expr<detail::build_type<A>, detail::fft_value_type<A>, detail::fft1_impl>{a};
230 }
231 
238 template <etl_expr A, etl_expr C>
239 auto fft_1d(A&& a, C&& c) {
240  validate_assign(c, a);
241 
242  c = fft_1d(a);
243  return c;
244 }
245 
251 template <etl_expr A>
252 fft_expr<detail::build_type<A>, detail::ifft_value_type<A>, detail::ifft1_impl> ifft_1d(A&& a) {
253  return fft_expr<detail::build_type<A>, detail::ifft_value_type<A>, detail::ifft1_impl>{a};
254 }
255 
262 template <etl_expr A, etl_expr C>
263 auto ifft_1d(A&& a, C&& c) {
264  validate_assign(c, a);
265 
266  c = ifft_1d(a);
267  return c;
268 }
269 
275 template <etl_expr A>
276 fft_expr<detail::build_type<A>, detail::ifft_real_value_type<A>, detail::ifft1_real_impl> ifft_1d_real(A&& a) {
277  return fft_expr<detail::build_type<A>, detail::ifft_real_value_type<A>, detail::ifft1_real_impl>{a};
278 }
279 
286 template <etl_expr A, etl_expr C>
287 auto ifft_1d_real(A&& a, C&& c) {
288  validate_assign(c, a);
289 
290  c = ifft_1d_real(a);
291  return c;
292 }
293 
299 template <etl_expr A>
300 fft_expr<detail::build_type<A>, detail::fft_value_type<A>, detail::fft2_impl> fft_2d(A&& a) {
301  return fft_expr<detail::build_type<A>, detail::fft_value_type<A>, detail::fft2_impl>{a};
302 }
303 
310 template <etl_expr A, etl_expr C>
311 auto fft_2d(A&& a, C&& c) {
312  validate_assign(c, a);
313 
314  c = fft_2d(a);
315  return c;
316 }
317 
323 template <etl_expr A>
324 fft_expr<detail::build_type<A>, detail::ifft_value_type<A>, detail::ifft2_impl> ifft_2d(A&& a) {
325  return fft_expr<detail::build_type<A>, detail::ifft_value_type<A>, detail::ifft2_impl>{a};
326 }
327 
334 template <etl_expr A, etl_expr C>
335 auto ifft_2d(A&& a, C&& c) {
336  validate_assign(c, a);
337 
338  c = ifft_2d(a);
339  return c;
340 }
341 
347 template <etl_expr A>
348 fft_expr<detail::build_type<A>, detail::ifft_real_value_type<A>, detail::ifft2_real_impl> ifft_2d_real(A&& a) {
349  return fft_expr<detail::build_type<A>, detail::ifft_real_value_type<A>, detail::ifft2_real_impl>{a};
350 }
351 
358 template <etl_expr A, etl_expr C>
359 auto ifft_2d_real(A&& a, C&& c) {
360  validate_assign(c, a);
361 
362  c = ifft_2d_real(a);
363  return c;
364 }
365 
374 template <matrix A>
376  return fft_expr<detail::build_type<A>, detail::fft_value_type<A>, detail::fft1_many_impl>{a};
377 }
378 
388 template <matrix A, matrix C>
389 auto fft_1d_many(A&& a, C&& c) {
390  validate_assign(c, a);
391 
392  c = fft_1d_many(a);
393  return c;
394 }
395 
404 template <matrix A>
406  return fft_expr<detail::build_type<A>, detail::ifft_value_type<A>, detail::ifft1_many_impl>{a};
407 }
408 
418 template <matrix A, matrix C>
419 auto ifft_1d_many(A&& a, C&& c) {
420  validate_assign(c, a);
421 
422  c = ifft_1d_many(a);
423  return c;
424 }
425 
434 template <deep_mat A>
436  return fft_expr<detail::build_type<A>, detail::fft_value_type<A>, detail::fft2_many_impl>{a};
437 }
438 
448 template <deep_mat A, deep_mat C>
449 auto fft_2d_many(A&& a, C&& c) {
450  validate_assign(c, a);
451 
452  c = fft_2d_many(a);
453  return c;
454 }
455 
464 template <deep_mat A>
466  return fft_expr<detail::build_type<A>, detail::ifft_value_type<A>, detail::ifft2_many_impl>{a};
467 }
468 
478 template <deep_mat A, deep_mat C>
479 auto ifft_2d_many(A&& a, C&& c) {
480  validate_assign(c, a);
481 
482  c = ifft_2d_many(a);
483  return c;
484 }
485 
486 } //end of namespace etl
void assign_sub_to(L &&lhs) const
Sub from the given left-hand-side expression.
Definition: fft_expr.hpp:68
fft_expr< detail::build_type< A >, detail::ifft_value_type< A >, detail::ifft2_many_impl > ifft_2d_many(A &&a)
Creates an expression representing several 2D Fast-Fourrier-Transform of the given expression...
Definition: fft_expr.hpp:465
fft_expr< detail::build_type< A >, detail::fft_value_type< A >, detail::fft1_impl > fft_1d(A &&a)
Creates an expression representing the 1D Fast-Fourrier-Transform of the given expression.
Definition: fft_expr.hpp:228
Functor for 2D IFFT (real)
Definition: fft.hpp:637
std::decay_t< A > sub_expr_t
The sub expression type.
Definition: fft_expr.hpp:117
std::add_lvalue_reference_t< A > a()
Returns the sub expression.
Definition: base_temporary_expr.hpp:489
Functor for 2D FFT.
Definition: fft.hpp:485
static constexpr size_t dimensions()
Returns the number of dimensions of the expression.
Definition: fft_expr.hpp:185
fft_expr< detail::build_type< A >, detail::ifft_value_type< A >, detail::ifft1_impl > ifft_1d(A &&a)
Creates an expression representing the 1D inverse Fast-Fourrier-Transform of the given expression...
Definition: fft_expr.hpp:252
Functor for Batched 2D IFFT.
Definition: fft.hpp:903
constexpr bool is_magic_view
Traits indicating if the given ETL type is a magic view expression.
Definition: traits.hpp:311
fft_expr< detail::build_type< A >, detail::fft_value_type< A >, detail::fft2_many_impl > fft_2d_many(A &&a)
Creates an expression representing several 2D Fast-Fourrier-Transform of the given expression...
Definition: fft_expr.hpp:435
static constexpr size_t dim()
Returns the DDth dimension of the expression.
Definition: fft_expr.hpp:150
T value_type
The value type of the expression.
Definition: fft_expr.hpp:119
friend std::ostream & operator<<(std::ostream &os, const fft_expr &expr)
Print a representation of the expression on the given stream.
Definition: fft_expr.hpp:105
fft_expr< detail::build_type< A >, detail::fft_value_type< A >, detail::fft1_many_impl > fft_1d_many(A &&a)
Creates an expression representing several 1D Fast-Fourrier-Transform of the given expression...
Definition: fft_expr.hpp:375
Functor for Batched 1D IFFT.
Definition: fft.hpp:827
Functor for 1D IFFT.
Definition: fft.hpp:371
order
Storage order of a matrix.
Definition: order.hpp:15
constexpr bool cuda_enabled
Indicates if CUDA is available.
Definition: config.hpp:94
static constexpr int complexity() noexcept
Estimate the complexity of computation.
Definition: fft_expr.hpp:193
A _a
The sub expression reference.
Definition: base_temporary_expr.hpp:447
fft_expr< detail::build_type< A >, detail::fft_value_type< A >, detail::fft2_impl > fft_2d(A &&a)
Creates an expression representing the 2D Fast-Fourrier-Transform of the given expression.
Definition: fft_expr.hpp:300
constexpr bool is_fast
Traits to test if the given ETL expresion type is fast (sizes known at compile-time) ...
Definition: traits.hpp:588
Traits to get information about ETL types.
Definition: tmp.hpp:68
T value_type
The type of value of the expression.
Definition: fft_expr.hpp:20
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
fft_expr(A a)
Construct a new expression.
Definition: fft_expr.hpp:37
void assign_mod_to(L &&lhs) const
Modulo the given left-hand-side expression.
Definition: fft_expr.hpp:95
auto dim(E &&value, size_t i) -> detail::identity_helper< E, dim_view< detail::build_identity_type< E >, D >>
Return a view representing the ith Dth dimension.
Definition: view_expression_builder.hpp:25
void assign_add_to(L &&lhs) const
Add to the given left-hand-side expression.
Definition: fft_expr.hpp:59
void std_mod_evaluate(Expr &&expr, Result &&result)
Compound modulo evaluation of the expr into result.
Definition: evaluator.hpp:1271
static constexpr size_t size()
Returns the size of the expression.
Definition: fft_expr.hpp:177
void assign_div_to(L &&lhs) const
Divide the given left-hand-side expression.
Definition: fft_expr.hpp:86
void assign_mul_to(L &&lhs) const
Multiply the given left-hand-side expression.
Definition: fft_expr.hpp:77
Functor for Batched 2D FFT.
Definition: fft.hpp:751
A transposition expression.
Definition: fft_expr.hpp:19
void std_mul_evaluate(Expr &&expr, Result &&result)
Compound multiply evaluation of the expr into result.
Definition: evaluator.hpp:1233
constexpr bool is_transformer
Traits indicating if the given ETL type is a transformer expression.
Definition: traits.hpp:297
Functor for 1D FFT.
Definition: fft.hpp:295
constexpr bool is_view
Traits indicating if the given ETL type is a view expression.
Definition: traits.hpp:304
void assign_to(C &&c) const
Assign to a matrix of the same storage order.
Definition: fft_expr.hpp:48
static constexpr bool is_fast
Indicates if T is a fast structure.
Definition: traits_base.hpp:25
Functor for Batched 1D FFT.
Definition: fft.hpp:675
void std_sub_evaluate(Expr &&expr, Result &&result)
Compound subtract evaluation of the expr into result.
Definition: evaluator.hpp:1214
static size_t dim(const expr_t &e, size_t d)
Returns the dth dimension of the expression.
Definition: fft_expr.hpp:160
Abstract base class for temporary unary expression.
Definition: base_temporary_expr.hpp:443
constexpr bool is_thread_safe
Traits to test if the given ETL expresion type is thread safe.
Definition: traits.hpp:687
static size_t size(const expr_t &e)
Returns the size of the expression.
Definition: fft_expr.hpp:169
fft_expr< detail::build_type< A >, detail::ifft_value_type< A >, detail::ifft1_many_impl > ifft_1d_many(A &&a)
Creates an expression representing several 1D Inverse Fast-Fourrier-Transform of the given expression...
Definition: fft_expr.hpp:405
static constexpr bool gpu_computable
Indicates if the temporary expression can be directly evaluated using only GPU.
Definition: fft_expr.hpp:31
fft_expr< detail::build_type< A >, detail::ifft_value_type< A >, detail::ifft2_impl > ifft_2d(A &&a)
Creates an expression representing the 2D inverse Fast-Fourrier-Transform of the given expression...
Definition: fft_expr.hpp:324
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
Functor for 2D IFFT.
Definition: fft.hpp:561
void std_div_evaluate(Expr &&expr, Result &&result)
Compound divide evaluation of the expr into result.
Definition: evaluator.hpp:1252
void inc_counter([[maybe_unused]] const char *name)
Increase the given counter.
Definition: counters.hpp:25
fft_expr< detail::build_type< A >, detail::ifft_real_value_type< A >, detail::ifft2_real_impl > ifft_2d_real(A &&a)
Creates an expression representing the real part of the 2D inverse Fast-Fourrier-Transform of the giv...
Definition: fft_expr.hpp:348
fft_expr< detail::build_type< A >, detail::ifft_real_value_type< A >, detail::ifft1_real_impl > ifft_1d_real(A &&a)
Creates an expression representing the real part of the 1D inverse Fast-Fourrier-Transform of the giv...
Definition: fft_expr.hpp:276
void std_add_evaluate(Expr &&expr, Result &&result)
Compound add evaluation of the expr into result.
Definition: evaluator.hpp:1195
Functor for 1D IFFT (real)
Definition: fft.hpp:447
static constexpr auto storage_order
The sub storage order.
Definition: fft_expr.hpp:25