Automatic Differentiation
 
Loading...
Searching...
No Matches
fft.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_FFT_HPP
2#define STAN_MATH_PRIM_FUN_FFT_HPP
3
6#include <unsupported/Eigen/FFT>
7#include <Eigen/Dense>
8#include <complex>
9#include <type_traits>
10#include <vector>
11
12namespace stan {
13namespace math {
14
33template <typename V, require_eigen_vector_vt<is_complex, V>* = nullptr,
34 require_not_var_t<base_type_t<value_type_t<V>>>* = nullptr,
35 require_not_fvar_t<base_type_t<value_type_t<V>>>* = nullptr>
36inline Eigen::Matrix<scalar_type_t<V>, -1, 1> fft(const V& x) {
37 // copy because fft() requires Eigen::Matrix type
38 Eigen::Matrix<scalar_type_t<V>, -1, 1> xv = x;
39 if (xv.size() <= 1)
40 return xv;
41 Eigen::FFT<base_type_t<V>> fft;
42 return fft.fwd(xv);
43}
44
65template <typename V, require_eigen_vector_vt<is_complex, V>* = nullptr,
66 require_not_var_t<base_type_t<value_type_t<V>>>* = nullptr,
67 require_not_fvar_t<base_type_t<value_type_t<V>>>* = nullptr>
68inline Eigen::Matrix<scalar_type_t<V>, -1, 1> inv_fft(const V& y) {
69 // copy because fft() requires Eigen::Matrix type
70 Eigen::Matrix<scalar_type_t<V>, -1, 1> yv = y;
71 if (y.size() <= 1)
72 return yv;
73 Eigen::FFT<base_type_t<V>> fft;
74 return fft.inv(yv);
75}
76
87template <typename M, require_eigen_dense_dynamic_vt<is_complex, M>* = nullptr,
88 require_not_var_t<base_type_t<value_type_t<M>>>* = nullptr,
89 require_not_fvar_t<base_type_t<value_type_t<M>>>* = nullptr>
90inline Eigen::Matrix<scalar_type_t<M>, -1, -1> fft2(const M& x) {
91 Eigen::Matrix<scalar_type_t<M>, -1, -1> y(x.rows(), x.cols());
92 for (int i = 0; i < y.rows(); ++i)
93 y.row(i) = fft(x.row(i));
94 for (int j = 0; j < y.cols(); ++j)
95 y.col(j) = fft(y.col(j));
96 return y;
97}
98
110template <typename M, require_eigen_dense_dynamic_vt<is_complex, M>* = nullptr,
111 require_not_var_t<base_type_t<value_type_t<M>>>* = nullptr,
112 require_not_fvar_t<base_type_t<value_type_t<M>>>* = nullptr>
113inline Eigen::Matrix<scalar_type_t<M>, -1, -1> inv_fft2(const M& y) {
114 Eigen::Matrix<scalar_type_t<M>, -1, -1> x(y.rows(), y.cols());
115 for (int j = 0; j < x.cols(); ++j)
116 x.col(j) = inv_fft(y.col(j));
117 for (int i = 0; i < x.rows(); ++i)
118 x.row(i) = inv_fft(x.row(i));
119 return x;
120}
121
122} // namespace math
123} // namespace stan
124
125#endif
Eigen::Matrix< scalar_type_t< V >, -1, 1 > inv_fft(V &&y)
Return the inverse discrete Fourier transform of the specified complex vector for forward-mode autodi...
Definition fft.hpp:61
Eigen::Matrix< scalar_type_t< M >, -1, -1 > inv_fft2(M &&y)
Return the two-dimensional inverse discrete Fourier transform of the specified complex matrix for for...
Definition fft.hpp:127
Eigen::Matrix< scalar_type_t< M >, -1, -1 > fft2(M &&x)
Return the two-dimensional discrete Fourier transform of the specified complex matrix for forward-mod...
Definition fft.hpp:96
Eigen::Matrix< scalar_type_t< V >, -1, 1 > fft(V &&x)
Return the discrete Fourier transform of the specified complex vector for forward-mode autodiff.
Definition fft.hpp:26
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...