Hello, n00b Julia user here. I have two functions that change the values of 
a passed-in array. One works (dxFromX), but for the other one (eulerStep) 
the caller does not see any changes to the array. Why is this?

function dxFromX!(x,dxdt)
  fill!(dxdt,0.0)

  for itarg = 1:size(x,1)
    for isrc = 1:size(x,1)
      dx = x[isrc,1] - x[itarg,1]
      dy = x[isrc,2] - x[itarg,2]
      coeff = 1.0f0 / (dx^2 + dy^2 + 0.1f0)
      dxdt[itarg,1] -= coeff * dy
      dxdt[itarg,2] += coeff * dx
    end
  end
end

function eulerStep!(x)
  dxdt = zeros(x)
  print ("\ndxdt before\n",dxdt[1:5,:],"\n")
  dxFromX!(x,dxdt)
  print ("\ndxdt after has changed\n",dxdt[1:5,:],"\n")
  x += 1.0f-5*dxdt
  print ("\nx inside\n",x[1:5,:],"\n")
end

x = float32(rand(1024,2))
print ("\nx before\n",x[1:5,:],"\n")
@time eulerStep!(x)
print ("\nx after is unchanged!\n",x[1:5,:],"\n")

I see the same behavior on 0.3.3 and 0.4.0, both release and debug 
binaries, on OSX and Linux. 

Reply via email to