I'm using python to write a split-step fourier transform method using pyfftw. I think I mostly understand what is going on, but I can't understand why so many of my variables change when I try to operate on just one.
In the code below, I start with a 1D array (complex128), E
, and initially assign it to a
. Then I go through planning the DFTs so that I have 2 operations that go back and forth to between time and frequency.
My code:
a = pyfftw.empty_aligned(npts, dtype='complex128')
b = pyfftw.empty_aligned(npts, dtype='complex128')
a[:] = pyfftw.interfaces.numpy_fft.fftshift(E);
fft_object = pyfftw.FFTW(a, b)
EFT = fft_object()
ifft_object = pyfftw.FFTW(b, a, direction='FFTW_BACKWARD')
E = ifft_object()
A0 = E;
a[:] = np.power(np.absolute(A0),2)
IFT = fft_object()
b[:] = chi*IFT
Iz = np.power(np.absolute(A0),2) + ifft_object()
NLfn = A0 * Iz
The problem is once I get into the second part of the code, assigning a[:] = np.power(np.absolute(A0),2)
seems to alter A0
as well as a
.
This also happens in Iz = np.power(np.absolute(A0),2) + fft_object()
, where in each case variables that I have assigned a
or b
to seem to remain linked rather than being temporary variables.
What's going on???
E
isa
(check by doingE is a
). This is because the second argument to theFFTW
object is the output array, which is what is returned when you doifft_object()
.You then assign
A0
to beE
, so absolutely when you changea
, you also changeA0
, since they are the same object.If you use the
FFTW
object, you need to be careful of such things. It's designed to minimise copies, so it's up to you to get the arrays correct. If you don't want to do this, I suggest using thepyfftw.builders
interface.