# Irfft with A\_mul\_B! messes up its input

**URL:** https://discourse.julialang.org/t/irfft-with-a-mul-b-messes-up-its-input/4762
**Category:** Numerics
**Tags:** question, bug
**Created:** [July 10, 2017, 2:27pm UTC](https://discourse.julialang.org/t/irfft-with-a-mul-b-messes-up-its-input/4762 "2017-07-10T14:27:42Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![navidcy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/navidcy/32/2750_2.png) [@navidcy](https://discourse.julialang.org/u/navidcy)
#### Post date: [July 10, 2017, 2:27pm UTC](https://discourse.julialang.org/t/irfft-with-a-mul-b-messes-up-its-input/4762/1 "2017-07-10T14:27:42Z")

</div>

Using A\_mul\_B! seems to be messing up it’s input. I attach a .jl file (INPUT) and the output I get on my machine. The output’s last line is the one which is puzzling!

# INPUT

```julia-auto
# Code snippet to test

nx, ny = 8, 8
nk, nl = nx, ny
nkr = trunc(Int, round(nx/2+1))

# Initialize
f = randn(nx, ny)
fh = rfft(f)

fcopy = deepcopy(f)
fhcopy = deepcopy(fh)
f3 = zeros(f)

# Plan FFT
effort = FFTW.MEASURE

rfftplan = plan_rfft(f; flags=effort)
irfftplan = plan_irfft(fh, nx; flags=effort)

# Restore original values without changing address in memory
f .= fcopy
fh .= fhcopy

@printf("\nAfter deepcopy( ):
  norm(fh-fhcopy) = %e", norm(fh-fhcopy))

f2 = irfft(fh, nx)

@printf("\nInverse with irfft( ):
  norm(f-f2) = %e
  norm(fh-fhcopy) = %e", norm(f-f2), norm(fh-fhcopy))

A_mul_B!(f3, irfftplan, fh)

@printf("\nInverse with A_mul_B!( ):
  norm(f-f3) = %e
  norm(fh-fhcopy) = %e\n\n", norm(f-f3), norm(fh-fhcopy))

```

# OUTPUT

```julia-auto
After deepcopy( ):
  norm(fh-fhcopy) = 0.000000e+00
Inverse with irfft( ):
  norm(f-f2) = 1.233205e-15
  norm(fh-fhcopy) = 0.000000e+00
Inverse with A_mul_B!( ):
  norm(f-f3) = 1.233205e-15
  norm(fh-fhcopy) = 1.034644e+02

```

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [July 10, 2017, 2:53pm UTC](https://discourse.julialang.org/t/irfft-with-a-mul-b-messes-up-its-input/4762/2 "2017-07-10T14:53:01Z")

</div>

> [@navidcy](#):
>
> Using A\_mul\_B! seems to be messing up it’s input. I attach a .jl file (INPUT) and the output I get on my machine. The output’s last line is the one which is puzzling!

This is a limitation of FFTW. As [explained in the FFTW manual](http://fftw.org/fftw3_doc/One_002dDimensional-DFTs-of-Real-Data.html#One_002dDimensional-DFTs-of-Real-Data),

> As noted above, the c2r transform destroys its input array even for out-of-place transforms. This can be prevented, if necessary, by including `FFTW_PRESERVE_INPUT` in the flags, with unfortunately some sacrifice in performance. This flag is also not currently supported for multi-dimensional real DFTs (next section).

Should probably be documented in Julia (or the new FFTW.jl package) too.
