Skip to content

Commit 4340733

Browse files
authored
Merge pull request numpy#26354 from WarrenWeckesser/fix-rfft
BUG: Fix rfft for even input length.
2 parents 65aa836 + 5cec3fb commit 4340733

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

numpy/fft/_pocketfft_umath.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,15 @@ rfft_impl(char **args, npy_intp const *dimensions, npy_intp const *steps,
183183
* Pocketfft uses FFTpack order, R0,R1,I1,...Rn-1,In-1,Rn[,In] (last
184184
* for npts odd only). To make unpacking easy, we place the real data
185185
* offset by one in the buffer, so that we just have to move R0 and
186-
* create I0=0. Note that copy_data will zero the In component for
186+
* create I0=0. Note that copy_input will zero the In component for
187187
* even number of points.
188188
*/
189189
copy_input(ip, step_in, nin, &((T *)op_or_buff)[1], nout*2 - 1);
190190
plan->exec(&((T *)op_or_buff)[1], *(T *)fp, pocketfft::FORWARD);
191191
op_or_buff[0] = op_or_buff[0].imag(); // I0->R0, I0=0
192+
if (!(npts & 1)) {
193+
op_or_buff[nout - 1].imag(0.0);
194+
}
192195
if (buffered) {
193196
copy_output(op_or_buff, op, step_out, nout);
194197
}

numpy/fft/tests/test_pocketfft.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ def test_fft_bad_out(self):
183183
with pytest.raises(TypeError, match="Cannot cast"):
184184
np.fft.fft(x, out=np.zeros_like(x, dtype=float))
185185

186-
187186
@pytest.mark.parametrize('norm', (None, 'backward', 'ortho', 'forward'))
188187
def test_ifft(self, norm):
189188
x = random(30) + 1j*random(30)
@@ -258,6 +257,17 @@ def test_rfft(self):
258257
np.fft.rfft(x, n=n) / n,
259258
np.fft.rfft(x, n=n, norm="forward"), atol=1e-6)
260259

260+
def test_rfft_even(self):
261+
x = np.arange(8)
262+
n = 4
263+
y = np.fft.rfft(x, n)
264+
assert_allclose(y, np.fft.fft(x[:n])[:n//2 + 1], rtol=1e-14)
265+
266+
def test_rfft_odd(self):
267+
x = np.array([1, 0, 2, 3, -3])
268+
y = np.fft.rfft(x)
269+
assert_allclose(y, np.fft.fft(x)[:3], rtol=1e-14)
270+
261271
def test_irfft(self):
262272
x = random(30)
263273
assert_allclose(x, np.fft.irfft(np.fft.rfft(x)), atol=1e-6)

0 commit comments

Comments
 (0)