Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed norm #30481

Merged
merged 8 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,40 +273,40 @@ diag(A::AbstractVector) = throw(ArgumentError("use diagm instead of diag to cons
# special cases of norm; note that they don't need to handle isempty(x)
function generic_normMinusInf(x)
(v, s) = iterate(x)::Tuple
minabs = norm(v)
minabs = norm(float(v))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't right either, since v might not be a scalar.

while true
y = iterate(x, s)
y === nothing && break
(v, s) = y
vnorm = norm(v)
vnorm = norm(float(v))
minabs = ifelse(isnan(minabs) | (minabs < vnorm), minabs, vnorm)
end
return float(minabs)
end

function generic_normInf(x)
(v, s) = iterate(x)::Tuple
maxabs = norm(v)
maxabs = norm(float(v))
while true
y = iterate(x, s)
y === nothing && break
(v, s) = y
vnorm = norm(v)
vnorm = norm(float(v))
maxabs = ifelse(isnan(maxabs) | (maxabs > vnorm), maxabs, vnorm)
end
return float(maxabs)
end

function generic_norm1(x)
(v, s) = iterate(x)::Tuple
av = float(norm(v))
av = float(norm(float(v)))
T = typeof(av)
sum::promote_type(Float64, T) = av
while true
y = iterate(x, s)
y === nothing && break
(v, s) = y
sum += norm(v)
sum += norm(float(v))
end
return convert(T, sum)
end
Expand All @@ -331,12 +331,12 @@ function generic_norm2(x)
end
return convert(T, sqrt(sum))
else
sum = abs2(norm(v)/maxabs)
sum = abs2(norm(float(v))/maxabs)
while true
y = iterate(x, s)
y === nothing && break
(v, s) = y
sum += (norm(v)/maxabs)^2
sum += (norm(float(v))/maxabs)^2
end
return convert(T, maxabs*sqrt(sum))
end
Expand All @@ -355,21 +355,21 @@ function generic_normp(x, p)
end
spp::promote_type(Float64, T) = p
if -1 <= p <= 1 || (isfinite(length(x)*maxabs^spp) && maxabs^spp != 0) # scaling not necessary
sum::promote_type(Float64, T) = norm(v)^spp
sum::promote_type(Float64, T) = norm(float(v))^spp
while true
y = iterate(x, s)
y === nothing && break
(v, s) = y
sum += norm(v)^spp
sum += norm(float(v))^spp
end
return convert(T, sum^inv(spp))
else # rescaling
sum = (norm(v)/maxabs)^spp
sum = (norm(float(v))/maxabs)^spp
while true
y = iterate(x, s)
y == nothing && break
(v, s) = y
sum += (norm(v)/maxabs)^spp
sum += (norm(float(v))/maxabs)^spp
end
return convert(T, maxabs*sum^inv(spp))
end
Expand Down Expand Up @@ -447,17 +447,17 @@ true
function norm(itr, p::Real=2)
isempty(itr) && return float(norm(zero(eltype(itr))))
if p == 2
return norm2(float(itr))
return norm2(itr)
elseif p == 1
return norm1(float(itr))
return norm1(itr)
elseif p == Inf
return normInf(float(itr))
return normInf(itr)
elseif p == 0
return typeof(float(norm(first(itr))))(count(!iszero, itr))
elseif p == -Inf
return normMinusInf(float(itr))
return normMinusInf(itr)
else
normp(float(itr), p)
normp(itr, p)
end
end

Expand Down
6 changes: 6 additions & 0 deletions stdlib/LinearAlgebra/test/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ end
end
end

@testset "Issue #30466" begin
@test norm([typemin(Int), typemin(Int)], Inf) == 9.223372036854776e18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Int is Int32 on 32 bit systems so make the rhs here float(typemax(Int)) and 2float(typemax(Int)) in the line below.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean -float(typemin(Int)), I guess (since typemin and typemax are not equal in magnitude).

@test norm([typemin(Int), typemin(Int)], -Inf) == 9.223372036854776e18
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we discuss this anywhere? I'm not sure we should allow negative p arguments so probably better to leave out this test.

@test norm([typemin(Int), typemin(Int)], 1) == 1.8446744073709552e19
end

@testset "potential overflow in normalize!" begin
δ = inv(prevfloat(typemax(Float64)))
v = [δ, -δ]
Expand Down