diff --git a/stdlib/LinearAlgebra/src/generic.jl b/stdlib/LinearAlgebra/src/generic.jl index fcabd98212c3d..5db2b525ee584 100644 --- a/stdlib/LinearAlgebra/src/generic.jl +++ b/stdlib/LinearAlgebra/src/generic.jl @@ -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