Skip to content

Commit

Permalink
Fix vecnorm for Vector{Vector{T}} (#22945)
Browse files Browse the repository at this point in the history
Use iszero in countnz
  • Loading branch information
andreasnoack authored Aug 7, 2017
1 parent ed746ed commit 72be7cb
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
5 changes: 2 additions & 3 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -431,16 +431,15 @@ julia> vecnorm([1 2 3 4 5 6 7 8 9])
```
"""
function vecnorm(itr, p::Real=2)
isempty(itr) && return float(real(zero(eltype(itr))))
isempty(itr) && return float(norm(zero(eltype(itr))))
if p == 2
return vecnorm2(itr)
elseif p == 1
return vecnorm1(itr)
elseif p == Inf
return vecnormInf(itr)
elseif p == 0
return convert(typeof(float(real(zero(eltype(itr))))),
countnz(itr))
return typeof(float(norm(first(itr))))(count(!iszero, itr))
elseif p == -Inf
return vecnormMinusInf(itr)
else
Expand Down
2 changes: 1 addition & 1 deletion test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ end
# @test find(s) == [1,2,3,4,5]
@test find(c -> c == 'l', s) == [3]
g = graphemes("日本語")
@test find(g) == [1,2,3]
@test find(isascii, g) == Int[]
@test find((i % 2 for i in 1:10)) == collect(1:2:9)
end
@testset "findn" begin
b = findn(ones(2,2,2,2))
Expand Down
3 changes: 2 additions & 1 deletion test/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ end

@testset "generic vecnorm for arrays of arrays" begin
x = Vector{Int}[[1,2], [3,4]]
@test norm(x) sqrt(30)
@test @inferred(norm(x)) sqrt(30)
@test norm(x, 0) == length(x)
@test norm(x, 1) sqrt(5) + 5
@test norm(x, 3) cbrt(sqrt(125)+125)
end
Expand Down

0 comments on commit 72be7cb

Please sign in to comment.