Skip to content

Commit

Permalink
simplify norm computations. (#43190)
Browse files Browse the repository at this point in the history
* simplify norm computations.
  • Loading branch information
oscardssmith authored Nov 29, 2021
1 parent 1a1f3d7 commit 43bc48b
Showing 1 changed file with 3 additions and 37 deletions.
40 changes: 3 additions & 37 deletions stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -449,45 +449,11 @@ diag(A::AbstractVector) = throw(ArgumentError("use diagm instead of diag to cons
# Dot products and norms

# 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)
while true
y = iterate(x, s)
y === nothing && break
(v, s) = y
vnorm = norm(v)
minabs = ifelse(isnan(minabs) | (minabs < vnorm), minabs, vnorm)
end
return float(minabs)
end
generic_normMinusInf(x) = float(mapreduce(norm, min, x))

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

function generic_norm1(x)
(v, s) = iterate(x)::Tuple
av = float(norm(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)
end
return convert(T, sum)
end
generic_norm1(x) = mapreduce(float norm, +, x)

# faster computation of norm(x)^2, avoiding overflow for integers
norm_sqr(x) = norm(x)^2
Expand Down

0 comments on commit 43bc48b

Please sign in to comment.