Skip to content

Commit 11df60f

Browse files
committed
added tests. fixed some bugs
1 parent 0fea390 commit 11df60f

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/LinearAlgebra.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ export
130130
lyap,
131131
mul!,
132132
norm,
133+
lpdist,
134+
l1dist,
135+
euclidean,
136+
l2dist,
133137
normalize!,
134138
normalize,
135139
nullspace,

src/generic.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,6 +907,8 @@ function _lpunnorm(x::T, y::T, fn) where {T}
907907
for i in eachindex(x,y)
908908
@inbounds r += fn(x[i] - y[i])
909909
end
910+
911+
return r
910912
end
911913

912914

@@ -940,7 +942,7 @@ function lpdist(x::T, y::T, p::Real) where {T <: AbstractVector}
940942
if p == Inf
941943
r = 0.0
942944
for i in eachindex(x,y)
943-
@inbounds r = max(x[i] - y[i], r)
945+
@inbounds r = max(abs(x[i] - y[i]), r)
944946
end
945947
return r
946948
elseif iszero(p)

test/generic.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,4 +837,20 @@ end
837837
end
838838
end
839839

840+
@testset "distance functions" begin
841+
x = [1., 2., 3.]; y = [1., 2.1, 3.4]
842+
@test lpdist(x, y, 1) norm(x .- y, 1)
843+
@test lpdist(x, y, 2) norm(x .- y, 2)
844+
@test lpdist(x, y, 4) norm(x .- y, 4)
845+
@test lpdist(x, y, 3.5) norm(x .- y, 3.5)
846+
@test euclidean(x, y) norm(x .- y, 2)
847+
@test l2dist(x, y) norm(x .- y, 2)
848+
@test l1dist(x, y) norm(x .- y, 1)
849+
@test lpdist(x, y, 0) norm(x .- y, 0)
850+
@test lpdist(x, y, Inf) norm(x .- y, Inf)
851+
@test_throws DomainError lpdist(x, y, -1)
852+
@test_throws DomainError lpdist(x, y, -0.5)
853+
@test_throws DimensionMismatch lpdist(x, [1.5, 2.0, 2.0, 3.1], 1)
854+
end
855+
840856
end # module TestGeneric

0 commit comments

Comments
 (0)