I tried with sub/slice a while ago and I think it gave me a very large performance decrease. I will try again when I get to a good computer to test it.
The "`AbstractVecOrMat` with a default value for idx"-method sounds interesting though. I will try it out. Thank you. On Monday, February 16, 2015 at 2:34:23 PM UTC+1, Andreas Noack wrote: > > You could Use `AbstractVecOrMat` and `idx=1` as default value, but you > could also use `sub` or `slice` to avoid copying the matrix columns since > > *julia> **A = randn(2,2)* > > *2x2 Array{Float64,2}:* > > * -0.124018 1.3846 * > > * -0.259116 -1.19279* > > > *julia> **isa(sub(A, :,1), AbstractVector{Float64})* > > *true* > > 2015-02-16 8:11 GMT-05:00 Kristoffer Carlsson <kcarl...@gmail.com > <javascript:>>: > >> Actually the best thing would be if I could call a function with the >> >> dist(point_1::AbstractVector{T}, point_2::AbstractVector{T}) >> >> signature because then I could just use all the stuff in the Distances.jl >> package >> >> >> >> On Monday, February 16, 2015 at 2:05:17 PM UTC+1, Kristoffer Carlsson >> wrote: >>> >>> I am writing some code where I sometimes want to calculate the distance >>> between two vectors and sometimes the distance between a column in a matrix >>> and another vector. >>> >>> This is in a very hot spot of my program so any unnecessary copying is >>> unacceptable. >>> >>> What I am currently doing is basically having two versions of the >>> distance function, one for vector-vector and one for column-vector: >>> >>> # Reduced euclidian distances >>> >>> # Vector-Vector >>> @inline function euclidean_distance_red{T <: FloatingPoint}(point_1:: >>> AbstractVector{T}, >>> point_2:: >>> AbstractVector{T}) >>> dist = 0.0 >>> for i = 1:length(point_1) >>> @inbounds dist += abs2(point_1[i] - point_2[i]) >>> end >>> return dist >>> end >>> >>> # Column-Vector >>> @inline function euclidean_distance_red{T <: FloatingPoint}(data:: >>> AbstractMatrix{T}, >>> point:: >>> AbstractVector{T}, >>> idx::Int) >>> dist = 0.0 >>> for i = 1:length(point) >>> @inbounds dist += abs2(data[i, idx] - point[i]) >>> end >>> return dist >>> end >>> >>> >>> This feels unnecessary since the transformation from the second to the >>> first function is obvious. Is there anyway I can collapse these two >>> functions into one that will come with zero performance loss? >>> >>> Best regards, >>> Kristoffer Carlsson >>> >> >