Hi Ivar,
Thanks for the idea. But replacing w += 10 with w = w + 10 still gives the
same problem. (As does w = w .+ 10).
Either way, after calling f2, the values of x are modified.
Even stranger, the values of x are not modified by the following function
function f3(x::Array)
w = x + 0.
for i in 1:length(w)
w[i] = w[i] + 10.0
end
return w
end
So the behavior of future lines of code depends on whether w is initialized
as w = x or w = x + 0.
It appears that for some reason, julia is doing different things when the
right hand side is just an array, versus when it is an expression.
Maybe somebody considers that a feature, but it's definitely non-intuitive.
I can't find any mention of this in the documentation. Amusingly, there
appears to be basically no documentation for what "=" does (despite it
being the most common symbol in most every julia program).
I thought = might be implemented as a generic function with different
behaviors for Arrays and expressions, but methods(=) and help(=) turned up
nothing.
On one hand, it seems, like = should be fairly obvious, but evidently there
are currently important differences in the behavior of = that need to be
documented and/or corrected.
Cheers,
Eric
On Friday, January 24, 2014 2:38:53 PM UTC-5, Ivar Nesje wrote:
>
> It becomes more obvious if you write "a += 3" as the longer form "a = a +
> 4"
>
> kl. 19:57:22 UTC+1 fredag 24. januar 2014 skrev Eric Ford følgende:
>>
>> Sorry. (Evidently, downloading a notebook is based on the last saved
>> version and what's currently on your screen.) IJulia notebook attached.
>> Readable version below.
>>
>> function f1(x::Array)
>> w = x
>> w += 10.0
>> return w
>> end
>>
>> function f2(x::Array)
>> w = x
>> for i in 1:length(x)
>> w[i] += 10.0
>> end
>> return w
>> end
>>
>> x=randn(10)
>> x_orig = deepcopy(x)
>> f1_of_x = f1(x)
>> println("After f1: ",sum((x.-x_orig).^2));
>> x = deepcopy(x_orig)
>> f2_of_x = f2(x)
>> println("After f2: ",sum((x.-x_orig).^2));
>>
>> After f1: 0.0
>> After f2: 1000.0
>>
>>
>> Thanks,
>> Eric
>>
>>
>>
>> On Friday, January 24, 2014 1:10:59 PM UTC-5, Kevin Squire wrote:
>>>
>>> In what you posted, `f1` and `f2` are identical (except for the name).
>>> Can you share the output of a Julia or IJulia session showing the problem?
>>>
>>> Cheers,
>>> Kevin
>>>
>>>
>>> On Fri, Jan 24, 2014 at 9:58 AM, Eric Ford <[email protected]> wrote:
>>>
>>>> I don't understand why the first function doesn't change x, but the
>>>> second function does.
>>>> Is the = calling deepcopy in f1, but copy in f2?
>>>> If so, why?
>>>>
>>>> function f1(x::Array)
>>>> w = x
>>>> for i in 1:length(x)
>>>> w[i] += 10.0
>>>> end
>>>> w += 10.0
>>>> return w
>>>> end
>>>>
>>>> function f2(x::Array)
>>>> w = x
>>>> for i in 1:length(x)
>>>> w[i] += 10.0
>>>> end
>>>> w += 10.0
>>>> return w
>>>> end
>>>>
>>>> x=randn(10)
>>>> x_orig = deepcopy(x)
>>>> f_of_x = f(x)
>>>> sum((x.-x_orig).^2)
>>>>
>>>> After f1: 0.0
>>>> After f2: 1000.0
>>>>
>>>> Thanks,
>>>> Eric (on behalf of an Astro 585 student)
>>>>
>>>>
>>>