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