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