## 7.13 Complex special matrix functions

### 7.13.1 Fast Fourier transforms

Stan’s fast Fourier transform functions take the standard definition of the discrete Fourier transform (see the definitions below for specifics) and scale the inverse transform by one over dimensionality so that the following identities hold for complex vectors u and v,

    fft(inv_fft(u)) == u        inv_fft(fft(v)) == v

and in the 2-dimensional case for complex matrices A and B,

    fft2(inv_fft2(A)) == A      inv_fft2(fft2(B)) == B

Although the FFT functions only accept complex inputs, real vectors and matrices will be promoted to their complex counterparts before applying the FFT functions.

complex_vector fft(complex_vector v)
Return the discrete Fourier transform of the specified complex vector v. If $$v \in \mathbb{C}^N$$ is a complex vector with $$N$$ elements and $$u = \textrm{fft}(v)$$, then $u_n = \sum_{m < n} v_m \cdot \exp\left(\frac{-n \cdot m \cdot 2 \cdot \pi \cdot \sqrt{-1}}{N}\right).$
Available since 2.30

complex_matrix fft2(complex_matrix m)
Return the 2D discrete Fourier transform of the specified complex matrix m. The 2D FFT is defined as the result of applying the FFT to each row and then to each column.
Available since 2.30

complex_vector inv_fft(complex_vector u)
Return the inverse of the discrete Fourier transform of the specified complex vector u. The inverse FFT (this function) is scaled so that fft(inv_fft(u)) == u. If $$u \in \mathbb{C}^N$$ is a complex vector with $$N$$ elements and $$v = \textrm{fft}^{-1}(u)$$, then $v_n = \frac{1}{N} \sum_{m < n} u_m \cdot \exp\left(\frac{n \cdot m \cdot 2 \cdot \pi \cdot \sqrt{-1}}{N}\right).$ This only differs from the FFT by the sign inside the exponential and the scaling. The $$\frac{1}{N}$$ scaling ensures that fft(inv_fft(u)) == u and inv_fft(fft(v)) == v for complex vectors u and v.
Available since 2.30

complex_matrix inv_fft2(complex_matrix m)
Return the inverse of the 2D discrete Fourier transform of the specified complex matrix m. The 2D inverse FFT is defined as the result of applying the inverse FFT to each row and then to each column. The invertible scaling of the inverse FFT ensures fft2(inv_fft2(A)) == A and inv_fft2(fft2(B)) == B.
Available since 2.30

### 7.13.2 Cumulative sums

The cumulative sum of a sequence $$x_1,\ldots,x_N$$ is the sequence $$y_1,\ldots,y_N$$, where $y_n = \sum_{m = 1}^{n} x_m.$

array[] complex cumulative_sum(array[] complex x)
The cumulative sum of x
Available since 2.30

complex_vector cumulative_sum(complex_vector v)
The cumulative sum of v
Available since 2.30

complex_row_vector cumulative_sum(complex_row_vector rv)
The cumulative sum of rv
Available since 2.30