The line now reads

x[:,:] += 1.0f-5*dxdt

And the result is now correct, but memory usage increased. Shouldn't it go 
down if we're re-assigning to the same variable?

On Wednesday, December 10, 2014 11:57:28 PM UTC-7, Isaiah wrote:
>
> `x += ...` is equivalent to writing `x = x + ...` which rebinds the 
> variable within that function. Instead, do an explicit array assignment 
> `x[:,:] = ...`
>
> This is discussed in the manual with a warning about type changes, but the 
> implication for arrays should probably be made clear as well: 
> http://julia.readthedocs.org/en/latest/manual/mathematical-operations/#updating-operators
>
> (there are some ongoing discussions about in-place array operators to 
> improve the situation)
>
> On Wed, Dec 10, 2014 at 7:44 PM, Mark Stock <mark.j...@gmail.com 
> <javascript:>> wrote:
>
>> 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