Repository navigation
Open
Description
Normally NumPy's rfft2/rfftn returns an array whose shape is largely the same as that of the input (as long as s is unspecified) with one exception. Namely the last axis in axes will specify the dimension whose length will be reduced by l // 2 + 1, where l was the original length of that dimension. This is even noted in the docs. However, it seems if the last axis is repeated this no longer holds. Now I could be wrong, but this is not what I would naively expect. If this does make sense for some reason, then the docs probably should be updated.
Details
In [1]: import numpy
In [2]: import numpy.fft
In [3]: a = numpy.random.random((6, 7, 8, 9))
In [4]: numpy.fft.rfft2(a, axes=(0,)).shape
Out[4]: (4, 7, 8, 9)
In [5]: numpy.fft.rfft2(a, axes=(1, 0)).shape
Out[5]: (4, 7, 8, 9)
In [6]: numpy.fft.rfft2(a, axes=(0, 0)).shape
Out[6]: (6, 7, 8, 9)
In [7]: numpy.fft.rfftn(a, axes=(0,)).shape
Out[7]: (4, 7, 8, 9)
In [8]: numpy.fft.rfftn(a, axes=(1, 0)).shape
Out[8]: (4, 7, 8, 9)
In [9]: numpy.fft.rfftn(a, axes=(0, 0)).shape
Out[9]: (6, 7, 8, 9)Exported environment details from conda:
Details
name: npenv
channels: !!python/tuple
- !!python/unicode
'conda-forge'
- !!python/unicode
'defaults'
dependencies:
- conda-forge::appnope=0.1.0=py27_0
- conda-forge::backports.shutil_get_terminal_size=1.0.0=py27_1
- conda-forge::blas=1.1=openblas
- conda-forge::ca-certificates=2017.1.23=0
- conda-forge::certifi=2017.1.23=py27_0
- conda-forge::decorator=4.0.11=py27_0
- conda-forge::enum34=1.1.6=py27_1
- conda-forge::ipython=5.3.0=py27_0
- conda-forge::ipython_genutils=0.2.0=py27_0
- conda-forge::libgfortran=3.0.0=0
- conda-forge::ncurses=5.9=10
- conda-forge::numpy=1.12.1=py27_blas_openblas_200
- conda-forge::openblas=0.2.19=1
- conda-forge::openssl=1.0.2h=3
- conda-forge::pathlib2=2.2.1=py27_0
- conda-forge::pexpect=4.2.1=py27_0
- conda-forge::pickleshare=0.7.3=py27_0
- conda-forge::prompt_toolkit=1.0.13=py27_0
- conda-forge::ptyprocess=0.5.1=py27_0
- conda-forge::pygments=2.2.0=py27_0
- conda-forge::python=2.7.13=0
- conda-forge::readline=6.2=0
- conda-forge::scandir=1.5=py27_1
- conda-forge::setuptools=33.1.1=py27_0
- conda-forge::simplegeneric=0.8.1=py27_0
- conda-forge::six=1.10.0=py27_1
- conda-forge::sqlite=3.13.0=1
- conda-forge::tk=8.5.19=1
- conda-forge::traitlets=4.3.2=py27_0
- conda-forge::wcwidth=0.1.7=py27_0
- conda-forge::zlib=1.2.11=0Metadata
Metadata
Assignees
Labels
No labels
Activity
jakirkham commentedon Mar 27, 2017
FWIW I don't see the same behavior with
irfft2/irfftn, which makes me more inclined to believe this is a bug.Details
jakirkham commentedon Mar 27, 2017
Tried to update the examples above to make it more obvious what is happening with the repeated axis case by keeping the same axis,
0, last.eric-wieser commentedon Mar 27, 2017
Do repeated axes even make any sense?
charris commentedon Mar 27, 2017
That would be my first question also: why are repeated axis allowed?
eric-wieser commentedon Mar 27, 2017
It looks like the whole function is basically
apply_over_axes(some_fft, axes...)- so repeating an axis amounts to doing something likefft(fft(data))- which IIRC is just some scaled/shifted version ofdata, as fft is almost its own inverse? Doesn't seem desirable to me.Also, ffts are associative, so
fft(fft(fft(data, axis=0), axis=1), axis=0)))is approximatelyfft(fft(fft(data, axis=0), axis=0), axis=1)- which again contains the meaningless expressionjakirkham commentedon Mar 27, 2017
Was wondering the same thing too. Is there any value to repeating an FFT transform to already transformed data?
FWIW in the
fttncase this is documented behavior, which is whatrfftn(and thusrfft2) are using.eric-wieser commentedon Mar 27, 2017
From this page
So no, this doesn't really seem desirable, and is a weird enough thing to do that I think the user should have to write it explicitly
jakirkham commentedon Mar 27, 2017
Just thinking through this, I guess repeated FFTs would teach you something about the frequency distribution itself, but I agree this doesn't seem like a common use case. Certainly not something I have encountered before.
eric-wieser commentedon Mar 27, 2017
yes, but you already had that to begin with from the data you passed in.
I guess you can use it to
-> fft -> pad -> fft -> pad ->through thesargument, which is less trivial than-> fft -> fft ->(which decays to-> reverse_and_scale ->)jakirkham commentedon Mar 27, 2017
Though if you are padding it before each FFT, that is already more complicated than simply repeating an axis and one would need to actually do it in multiple function applications. So that is another good argument for deprecation of this behavior.
FWIW I'm not defending the current behavior. Just trying to decide whether it is worth supporting in a downstream library. Thus far it seems that we are thinking it shouldn't even be in NumPy. So I guess I won't bother supporting it.
eric-wieser commentedon Mar 27, 2017
Nope, I'm pretty sure that the
sparameter allows padding (or at least cropping) on each successive axis.I think an important argument against repeated axes - the order of
sandaxesstart to matter, and depends on the (undocumented) order in which they are applied.I'd be +1 on deprecating this, and maybe even immediately removing this
jakirkham commentedon Mar 27, 2017
Me too.