"vecnorm" : column/row-wise norm

There is currently no direct function to take the norm of a 2D array by dimension. The easiest way would probably be to do either of the 2 functions below:

julia> A = rand(5,4);
                                         
julia> f1(A,d=1) = sqrt.(sum(abs2,A,d))
f1 (generic function with 1 method)               
                                                  
julia> f2(A) = [vecnorm(A[:,i]) for i=1:size(A,2)]
f2 (generic function with 1 method)               

julia> @btime f1($A)
  128.103 ns (2 allocations: 224 bytes)
1×4 Array{Float64,2}:
 1.08137  1.1315  1.23828  1.35529

julia> @btime f2($A)                    
  327.377 ns (11 allocations: 736 bytes)
4-element Array{Float64,1}:             
 1.08137                                
 1.1315                                 
 1.23828                                
 1.35529                                

f1 is faster because it acts mostly in place. It also allows you to change the dimension more easily.

5 Likes