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>
35inline Eigen::Matrix<scalar_type_t<V>, -1, 1> fft(const V& x) {
36 // copy because fft() requires Eigen::Matrix type
37 Eigen::Matrix<scalar_type_t<V>, -1, 1> xv = x;
38 if (xv.size() <= 1)
39 return xv;
40 Eigen::FFT<base_type_t<V>> fft;
41 return fft.fwd(xv);
42}
43
64template <typename V, require_eigen_vector_vt<is_complex, V>* = nullptr,
65 require_not_var_t<base_type_t<value_type_t<V>>>* = nullptr>
66inline Eigen::Matrix<scalar_type_t<V>, -1, 1> inv_fft(const V& y) {
67 // copy because fft() requires Eigen::Matrix type
68 Eigen::Matrix<scalar_type_t<V>, -1, 1> yv = y;
69 if (y.size() <= 1)
70 return yv;
71 Eigen::FFT<base_type_t<V>> fft;
72 return fft.inv(yv);
73}
74
85template <typename M, require_eigen_dense_dynamic_vt<is_complex, M>* = nullptr,
86 require_not_var_t<base_type_t<value_type_t<M>>>* = nullptr>
87inline Eigen::Matrix<scalar_type_t<M>, -1, -1> fft2(const M& x) {
88 Eigen::Matrix<scalar_type_t<M>, -1, -1> y(x.rows(), x.cols());
89 for (int i = 0; i < y.rows(); ++i)
90 y.row(i) = fft(x.row(i));
91 for (int j = 0; j < y.cols(); ++j)
92 y.col(j) = fft(y.col(j));
93 return y;
94}
95
107template <typename M, require_eigen_dense_dynamic_vt<is_complex, M>* = nullptr,
108 require_not_var_t<base_type_t<value_type_t<M>>>* = nullptr>
109inline Eigen::Matrix<scalar_type_t<M>, -1, -1> inv_fft2(const M& y) {
110 Eigen::Matrix<scalar_type_t<M>, -1, -1> x(y.rows(), y.cols());
111 for (int j = 0; j < x.cols(); ++j)
112 x.col(j) = inv_fft(y.col(j));
113 for (int i = 0; i < x.rows(); ++i)
114 x.row(i) = inv_fft(x.row(i));
115 return x;
116}
117
118} // namespace math
119} // namespace stan
120
121#endif
Eigen::Matrix< scalar_type_t< M >, -1, -1 > fft2(const M &x)
Return the two-dimensional discrete Fourier transform of the specified complex matrix.
Definition fft.hpp:87
Eigen::Matrix< scalar_type_t< V >, -1, 1 > fft(const V &x)
Return the discrete Fourier transform of the specified complex vector.
Definition fft.hpp:35
Eigen::Matrix< scalar_type_t< V >, -1, 1 > inv_fft(const V &y)
Return the inverse discrete Fourier transform of the specified complex vector.
Definition fft.hpp:66
Eigen::Matrix< scalar_type_t< M >, -1, -1 > inv_fft2(const M &y)
Return the two-dimensional inverse discrete Fourier transform of the specified complex matrix.
Definition fft.hpp:109
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...