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

Handle case when norm==Inf in normalize #29691

Merged
merged 1 commit into from
Oct 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
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