Re: [julia-users] Changes to array are not visible from caller
Wow. I thought I read the docs thoroughly, and I didn't see anything about how += (without the [] operator!) rebinds. That's a surprising behavior to many scientific programmers, and should be better documented. Thank you for the quick reply! 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.
Re: [julia-users] Changes to array are not visible from caller
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.
Re: [julia-users] Changes to array are not visible from caller
Is there any way to update the array in-place without writing an explicit loop? I imagine that would be more efficient. Mark On Thursday, December 11, 2014 10:23:35 AM UTC-7, John Myles White wrote: Nope. You'll find Julia much easier to program in if you always replace x += y with x = x + y before attempting to reason about performance. In this case, you'll get x[:, :] = x[:, :] + 1.0f - 5 * dxdt In other words, you literally make a copy of the entire matrix x before doing any useful work. -- John On Dec 11, 2014, at 12:21 PM, Mark Stock mark.j...@gmail.com javascript: wrote: 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 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.
[julia-users] Changes to array are not visible from caller
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.