Skip to content

Commit e1775e1

Browse files
committed
Implement normalize and normalize!
Simple helper functions for normalizing vectors Closes #12047
1 parent b82ab6a commit e1775e1

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed

base/exports.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,8 @@ export
676676
lufact,
677677
lyap,
678678
norm,
679+
normalize,
680+
normalize!,
679681
nullspace,
680682
ordschur!,
681683
ordschur,

base/linalg.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ export
9595
lufact!,
9696
lyap,
9797
norm,
98+
normalize,
99+
normalize!,
98100
nullspace,
99101
ordschur!,
100102
ordschur,

base/linalg/generic.jl

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,53 @@ function isapprox{T<:Number,S<:Number}(x::AbstractArray{T}, y::AbstractArray{S};
529529
d = norm(x - y)
530530
return isfinite(d) ? d <= atol + rtol*max(norm(x), norm(y)) : x == y
531531
end
532+
533+
"""
534+
normalize!(v, [p=2])
535+
536+
Normalize the vector `v` in-place with respect to the `p`-norm.
537+
538+
# Inputs
539+
540+
- `v::AbstractVector` - vector to be normalized
541+
- `p::Real` - The `p`-norm to normalize with respect to. Default: 2
542+
543+
# Output
544+
545+
- `v` - A unit vector being the input vector, rescaled to have norm 1. The input vector is modified in-place.
546+
547+
# See also
548+
549+
`normalize`
550+
551+
"""
552+
function normalize!(v::AbstractVector, p::Real=2)
553+
nrm = norm(v, p)
554+
for i in eachindex(v)
555+
v[i] /= nrm
556+
end
557+
v
558+
end
559+
560+
"""
561+
normalize(v, [p=2])
562+
563+
Normalize the vector `v` with respect to the `p`-norm.
564+
565+
# Inputs
566+
567+
- `v::AbstractVector` - vector to be normalized
568+
- `p::Real` - The `p`-norm to normalize with respect to. Default: 2
569+
570+
# Output
571+
572+
- `v` - A unit vector being a copy of the input vector, scaled to have norm 1
573+
574+
# See also
575+
576+
`normalize!`
577+
"""
578+
function normalize(v::AbstractVector, p::Real=2)
579+
nrm = norm(v, p)
580+
v/nrm
581+
end

test/linalg/generic.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,24 @@ let
150150
@test LinAlg.axpy!(α, x, deepcopy(y)) == x .* Matrix{Int}[α]
151151
@test LinAlg.axpy!(α, x, deepcopy(y)) != Matrix{Int}[α] .* x
152152
end
153+
154+
let
155+
v = [3.0, 4.0]
156+
@test norm(v) === 5.0
157+
w = normalize(v)
158+
@test w == [0.6, 0.8]
159+
@test norm(w) === 1.0
160+
@test normalize!(v) == w
161+
end
162+
163+
#Test potential overflow in normalize!
164+
let
165+
δ = 1e-300
166+
v = [δ, -δ]
167+
168+
@test norm(v) === 1.4142135623730952e-300
169+
w = normalize(v)
170+
@test norm(w) === prevfloat(1.0)
171+
@test w [1/√2, -1/√2]
172+
@test normalize!(v) == w
173+
end

0 commit comments

Comments
 (0)