Skip to content

Commit faea9fb

Browse files
committed
BUG: correct irfft with n=1 on larger input
1 parent b123cc9 commit faea9fb

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

numpy/fft/_pocketfft_umath.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ copy_data(char* in, npy_intp step_in, npy_intp nin,
3333
char* out, npy_intp step_out, npy_intp nout, npy_intp elsize)
3434
{
3535
npy_intp ncopy = nin <= nout? nin : nout;
36+
if (ncopy <= 0) {
37+
return; /* safety, and indeed required for npts=1 in irfft */
38+
}
3639
if (step_in == elsize && step_out == elsize) {
3740
memcpy(out, in, ncopy*elsize);
3841
}
@@ -235,11 +238,11 @@ irfft_loop(char **args, npy_intp const *dimensions, npy_intp const *steps, void
235238
double *op_double = (double *)op_or_buff;
236239
/* Copy complex input to buffer in FFTpack order */
237240
op_double[0] = ((double *)ip)[0];
238-
if (nin > 1) {
241+
if (npts > 1) {
239242
copy_data(ip + step_in, step_in, nin - 2,
240243
(char *)&op_double[1], sizeof(npy_cdouble), npts / 2 - 1,
241244
sizeof(npy_cdouble));
242-
npy_intp ncopied = (npts / 2 - 1 < nin - 2 ? npts / 2 - 1 : nin - 2);
245+
npy_intp ncopied = (npts / 2 - 1 < nin - 2 ? npts / 2 - 1 : nin - 2);
243246
double *last = (double *)(ip + (ncopied + 1) * step_in);
244247
op_double[ncopied * 2 + 1] = last[0];
245248
if (npts % 2 == 1) { /* For odd n, we still imag real of the last point */

numpy/fft/tests/test_pocketfft.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ def test_identity(self):
3333
assert_allclose(np.fft.irfft(np.fft.rfft(xr[0:i]), i),
3434
xr[0:i], atol=1e-12)
3535

36+
def test_identity_reversed(self):
37+
maxlen = 16
38+
x = random(maxlen) + 1j*random(maxlen)
39+
for i in range(1, maxlen):
40+
assert_allclose(np.fft.fft(np.fft.ifft(x, n=i)),
41+
x[0:i], atol=1e-12)
42+
y = x.copy()
43+
n = i // 2 + 1
44+
y.imag[0] = 0
45+
if i % 2 == 0:
46+
y.imag[n-1] = 0
47+
assert_allclose(np.fft.rfft(np.fft.irfft(y, n=i)),
48+
y[0:n], atol=1e-12)
49+
3650
def test_fft(self):
3751
x = random(30) + 1j*random(30)
3852
assert_allclose(fft1(x), np.fft.fft(x), atol=1e-6)
@@ -492,3 +506,10 @@ def test_rfft(self):
492506
def test_irfft(self):
493507
a = np.ones(self.input_shape) * 1+0j
494508
self._test_mtsame(np.fft.irfft, a)
509+
510+
511+
def test_irfft_with_n_1_regression():
512+
# Regression test for gh-25661
513+
x = np.arange(10)
514+
np.fft.irfft(x, n=1)
515+
np.fft.hfft(x, n=1)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy