From ca40da451f896acb404d56dc5cf7c424f88c1893 Mon Sep 17 00:00:00 2001 From: Mike Rouleau <44850854+mikerouleau@users.noreply.github.com> Date: Sat, 9 Apr 2022 20:08:58 +0000 Subject: [PATCH 1/4] add LinearAlgebra.normalize fallback for scalars (#44835) fix NaN == NaN error --- stdlib/LinearAlgebra/src/generic.jl | 20 ++++++++++++++++---- stdlib/LinearAlgebra/test/generic.jl | 6 ++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index 2449a78fda317..c79849535ad0a 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -1790,11 +1790,12 @@ end end """ - normalize(a::AbstractArray, p::Real=2) + normalize(a, p::Real=2) -Normalize the array `a` so that its `p`-norm equals unity, -i.e. `norm(a, p) == 1`. -See also [`normalize!`](@ref) and [`norm`](@ref). +Normalize `a` so that its `p`-norm equals unity, +i.e. `norm(a, p) == 1`. For scalars, this is similar to sign(a), +except normalize(0) = NaN. +See also [`normalize!`](@ref), [`norm`](@ref), and [`sign`](@ref). # Examples ```jldoctest @@ -1831,6 +1832,14 @@ julia> normalize(a) 0.154303 0.308607 0.617213 0.154303 0.308607 0.617213 +julia> normalize(3, 1) +1.0 + +julia> normalize(-8, 1) +-1.0 + +julia> normalize(0, 1) +NaN ``` """ function normalize(a::AbstractArray, p::Real = 2) @@ -1843,3 +1852,6 @@ function normalize(a::AbstractArray, p::Real = 2) return T[] end end + +normalize(x) = x / norm(x) +normalize(x, p::Real) = x / norm(x, p) diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 69f2fff00755f..8ca829184314b 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -375,6 +375,12 @@ end @test typeof(normalize([1 2 3; 4 5 6])) == Array{Float64,2} end +@testset "normalize for scalars" begin + @test normalize(8.0) == 1.0 + @test normalize(-3.0) == -1.0 + @test isnan(normalize(0.0)) +end + @testset "Issue #30466" begin @test norm([typemin(Int), typemin(Int)], Inf) == -float(typemin(Int)) @test norm([typemin(Int), typemin(Int)], 1) == -2float(typemin(Int)) From c9c5118c6da1d87a3fe4dd12ac9877b67ec9848b Mon Sep 17 00:00:00 2001 From: Mike Rouleau <44850854+mikerouleau@users.noreply.github.com> Date: Tue, 12 Apr 2022 17:23:32 +0000 Subject: [PATCH 2/4] add test for 2-argument version --- stdlib/LinearAlgebra/test/generic.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/LinearAlgebra/test/generic.jl b/stdlib/LinearAlgebra/test/generic.jl index 8ca829184314b..77668cdb69b62 100644 --- a/stdlib/LinearAlgebra/test/generic.jl +++ b/stdlib/LinearAlgebra/test/generic.jl @@ -378,6 +378,7 @@ end @testset "normalize for scalars" begin @test normalize(8.0) == 1.0 @test normalize(-3.0) == -1.0 + @test normalize(-3.0, 1) == -1.0 @test isnan(normalize(0.0)) end From 8036983f0c9d59f07a7d31e96066289315a08b3a Mon Sep 17 00:00:00 2001 From: Mike Rouleau <44850854+mikerouleau@users.noreply.github.com> Date: Tue, 12 Apr 2022 17:24:21 +0000 Subject: [PATCH 3/4] add NEWS.md entry --- NEWS.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS.md b/NEWS.md index f05ff20e078ed..93326704f2670 100644 --- a/NEWS.md +++ b/NEWS.md @@ -60,6 +60,8 @@ Standard library changes system image with other BLAS/LAPACK libraries is not supported. Instead, it is recommended that the LBT mechanism be used for swapping BLAS/LAPACK with vendor provided ones. ([#44360]) +* `normalize(x)` and `normalize(x, p::Real)` fallback methods created, + allowing use on any normed vector space, including scalars. #### Markdown From e96556a9143e9f5c6dc30dea734c3069786050aa Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Tue, 12 Apr 2022 16:20:22 -0400 Subject: [PATCH 4/4] Update NEWS.md --- NEWS.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 93326704f2670..5803982104c96 100644 --- a/NEWS.md +++ b/NEWS.md @@ -60,8 +60,7 @@ Standard library changes system image with other BLAS/LAPACK libraries is not supported. Instead, it is recommended that the LBT mechanism be used for swapping BLAS/LAPACK with vendor provided ones. ([#44360]) -* `normalize(x)` and `normalize(x, p::Real)` fallback methods created, - allowing use on any normed vector space, including scalars. +* `normalize(x, p=2)` now supports any normed vector space `x`, including scalars ([#44925]). #### Markdown