Skip to content

Commit

Permalink
Handle case when norm==Inf in normalize (#29691)
Browse files Browse the repository at this point in the history
Fixes #29681
  • Loading branch information
andreasnoack authored Oct 19, 2018
1 parent cf544e6 commit 0ba31aa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
9 changes: 5 additions & 4 deletions stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1361,17 +1361,18 @@ end
# The largest positive floating point number whose inverse is less than infinity
δ = inv(prevfloat(typemax(nrm)))

if nrm δ # Safe to multiply with inverse
if nrm == Inf # Just divide since performance isn't important in this corner case
v ./= Inf
elseif nrm δ # Safe to multiply with inverse
invnrm = inv(nrm)
rmul!(v, invnrm)

else # scale elements to avoid overflow
else # Scale elements to avoid overflow
εδ = eps(one(nrm))/δ
rmul!(v, εδ)
rmul!(v, inv(nrm*εδ))
end

v
return v
end

"""
Expand Down
7 changes: 7 additions & 0 deletions stdlib/LinearAlgebra/test/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ end
@test norm(normalize!(v) - w, Inf) < eps()
end

@testset "normalize with Infs. Issue 29681." begin
@test all(isequal.(normalize([1, -1, Inf]),
[0.0, -0.0, NaN]))
@test all(isequal.(normalize([complex(1), complex(0, -1), complex(Inf, -Inf)]),
[0.0 + 0.0im, 0.0 - 0.0im, NaN + NaN*im]))
end

@testset "Issue 14657" begin
@test det([true false; false true]) == det(Matrix(1I, 2, 2))
end
Expand Down

0 comments on commit 0ba31aa

Please sign in to comment.