>
> Those coming to Julia from languages like Python, R, Matlab/Octave, etc.
> have it ingrained in them to "fear the loop" and to perform loop-like
> calculations in "verbose and convoluted" ways.
>
Please compare my Python version with yours. If I look at the Python
version in 2 month, I'll immediately understand what it does and what it is
used for, yours is not as straight forward.
But I'm really not complaining. I was just surprised that I couldn't
decrease the allocation. I still don't understand why t[1] = 1.0+2.0
allocates nothing, while t[:] = 1.0+2.0 allocates things.
> It is actually quite easy to do this calculation in Julia with very
> little storage allocation by writing three nested loops.
>
> function mindist(pos::Matrix{Float64}, Acp::Matrix{Float64})
> m,n = size(pos)
> r,s = size(Acp)
> m == r || throw(DimensionMismatch(""))
> res = fill(Inf,(n,))
> for i in 1:n
> for j in 1:s
> sm = 0.
> for k in 1:m
> sm += abs2(pos[k,i] - Acp[k,j])
> end
> if sm < res[i]
> res[i] = sm
> end
> end
> end
> res
> end
>
> On my machine timing the second execution (the first execution includes
> compile time) I get
>
> julia> @time mindist(pos,Acp);
> elapsed time: 0.002806999 seconds (744 bytes allocated)
>
>
Thank you. Very good ideas there also.
Just a last related question: is there any way in Julia to work on complete
vector/matrices (without unrolling) without allocating stuff? For instance
t = zeros(Float64, 3)
a = rand(Float64, 3)
b = rand(Float64, 3)
t[:] = a[:]
t[:] .*= b[:]
but without allocation? @devec reduces it by half but it's still not zero.
There might be no better alternative yet.