I have now proposed to describe reshape under the 2nd bullet together with 
assignment, because like assignment also reshape returns a new reference to 
a value array. My proposed text is perhaps quite wordy, for a more terse 
style only the first sentence would be enough: 

* Arrays are assigned by reference. After ``A=B``, assigning into ``B``
   will modify ``A`` as well, the same is true after ``A=reshape(B, ...)``.
   In Matlab assignment and the reshape function are equivalent to 
``A=copy(B)``
   and    ``A=reshape(copy(B), ...)``, respectively. In Julia assignment
   conceptually creates another pointer to a value array, and the reshape
   function a different view of it. But beware, e.g. ``A+=0``, which does
   not exist in Matlab, is in Julia equivalent to A=copy(A)+0, thereafter
   assigning into ``B`` does not modify ``A`` any more.

 Also the third bullet, function arguments, could become clearer through 
expansion, in my opinion. 

Am Montag, 13. Oktober 2014 20:19:26 UTC+2 schrieb Jutho:
>
> Maybe point 3 on the noteworthy differences list could be expanded. If 
> Stephan doesn't do so, I can try to give it a shot later
>
> Op maandag 13 oktober 2014 19:53:58 UTC+2 schreef Ivar Nesje:
>>
>> This behaviour is already bullet 2 and 3 on the list you suggested. 
>> Emphasizing that Matlab implicitly inserts a copy(x), when you mutate 
>> arrays after some boundaries might be a good idea. I also find it somewhat 
>> hard to grasp the consequences of the current wording, but I'm not the 
>> target audience for that part of the documentation.
>>
>> As always when you want to change something in Julia, the prefered way is 
>> to submit a pull request on Github. For documentation, you can even use the 
>> github 
>> web ui 
>> <https://github.com/JuliaLang/julia/edit/master/doc/manual/noteworthy-differences.rst>
>>  to 
>> edit. If you open a pull request you should also link to this thread as 
>> previous discussion.
>>
>> Regards Ivar
>>
>> kl. 16:35:52 UTC+2 mandag 13. oktober 2014 skrev Stephan Buchert følgende:
>>>
>>> Thanks for the explanations.
>>>
>>> To me this seems now reasonable, after realizing that in Julia not only 
>>> reshape etc, but already the normal assignment b=a (for a an array) opens 
>>> for the same kind of "surprises" as I had noticed them with reshape. Coming 
>>> from Matlab this is a surprise, but for someone being familiar with certain 
>>> other languages, I guess, it is not?
>>>
>>>  julia> a=[1, 2, 3]
>>> 3-element Array{Int64,1}:
>>>  1
>>>  2
>>>  3
>>>
>>> julia> b=a;
>>>
>>> julia> b[2]=0
>>> 0
>>>
>>> julia> a
>>> 3-element Array{Int64,1}:
>>>  1
>>>  0
>>>  3
>>>
>>> But then "+-" is different again:
>>>
>>> julia> b+=1;
>>>
>>> julia> a
>>> 3-element Array{Int64,1}:
>>>  1
>>>  0
>>>  3
>>>
>>>
>>> Perhaps one could add in 
>>> http://julia.readthedocs.org/en/latest/manual/noteworthy-differences/ 
>>> under Matlab near the top something like:
>>>
>>> * Arrays are mutable. Assignments like b=a and functions b=f(a) behave 
>>> in Matlab always equivalent to b=copy(a) and b=f(copy(a)), but in Julia b 
>>> becomes conceptually a pointer or different view of the contents of a. The 
>>> values of a can be altered through b.
>>>
>>> PS: Yes, linear indices work directly also in Julia, I had overlooked 
>>> this
>>>
>>> Am Montag, 13. Oktober 2014 10:33:26 UTC+2 schrieb Jutho:
>>>>
>>>> Some more comments / answers:
>>>>
>>>> I too was confused by the precise use of ! when I first started using 
>>>> Julia in cases like reshape. However, it is very clear that reshape should 
>>>> not have an exclamation mark.
>>>>
>>>> The use of ! in Julia functions is whenever the function modifies one 
>>>> of its input arguments. This can be for several reasons:
>>>>
>>>>    1. The function performs some operation in-place: e.g. 
>>>>    scale!(A,lambda) multiplies all elements of the array A with the scalar 
>>>>    lambda in place.
>>>>    2. The operation stores the result of the computation in one 
>>>>    (typically the first) argument: e.g. copy!(B,A), 
>>>> permutedims!(B,A,perm), 
>>>>    scale!(B,A,lambda), Base.transpose!(B,A): do something with the data of 
>>>> A 
>>>>    and store the result in the pre-allocated but not necessarily 
>>>> initialised 
>>>>    array B
>>>>    3. The function overwrites the input arguments for use as temporary 
>>>>    workspace, but the actual result is just a normal return value as in 
>>>>    typical functions: e.g. the different [eig,svd,qr]fact!(A) methods
>>>>
>>>> The Base.LinAlg.BLAS methods do something in between 1. and 2.; they 
>>>> store the result of the computation in a dedicated 'output' argument as in 
>>>> 2., but they can also just add the result to that output argument, in 
>>>> which 
>>>> case it needs to be correctly initialised.
>>>>
>>>> In summary, the exclamation mark means that if you need the original 
>>>> values of all of the arguments after calling he function, you should 
>>>> better 
>>>> make a copy, since calling the function allows it to destroy the original 
>>>> values of its arguments (only possible with arrays or other mutable types).
>>>> Also note that most of these functions do not change the size of array 
>>>> arguments, resize! is one of the notable few exceptions and only works 
>>>> with 
>>>> one-dimensional arrays (vectors).
>>>>
>>>> reshape doesn't fit in any of these categories. B=reshape(A,newsize) 
>>>> does not destroy the values of a. It is however true that the output B 
>>>> shares data with A, just like if you do B=slice(A,...) or B=sub(A,...). 
>>>> Note that none of these functions use an exclamation mark, nor is there 
>>>> any 
>>>> other stylistic convention to indicate that this happens. That's just a 
>>>> new 
>>>> aspect of the language that you have to take into account when coming from 
>>>> e.g. Matlab, but that is very natural when coming from e.g. C.
>>>>
>>>> Finally, note that linear indexing indeed works on an array without 
>>>> reshaping, but that if you want the 'vectorized' version a 
>>>> multidimensional 
>>>> array, there is the build in function B=vec(A), which is indeed equivalent 
>>>> to B=reshape(A,prod(size(A)))
>>>>
>>>>
>>>>
>>>>
>>>> Op zaterdag 11 oktober 2014 22:20:13 UTC+2 schreef Stephan Buchert:
>>>>>
>>>>> julia> a=[1 2; 3 4; 5 6]
>>>>> 3x2 Array{Int64,2}:
>>>>>  1  2
>>>>>  3  4
>>>>>  5  6
>>>>>
>>>>> julia> b=reshape(a, prod(size(a)));
>>>>>
>>>>> julia> b[3]=0
>>>>> 0
>>>>>
>>>>> julia> a
>>>>> 3x2 Array{Int64,2}:
>>>>>  1  2
>>>>>  3  4
>>>>>  0  6
>>>>>
>>>>>

Reply via email to