From 78d57013c0ffccc09946ab8da689e739d6859ba6 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Mon, 5 Oct 2015 17:49:50 -0500 Subject: [PATCH] Use eachindex in generic_scale! (cherry picked from commit fa6c97706a5a799f4ec9ea71bd4aec9d708c76e9) ref #13460 --- base/linalg/generic.jl | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/base/linalg/generic.jl b/base/linalg/generic.jl index bf2bcd1917cde..c8948f30c3eff 100644 --- a/base/linalg/generic.jl +++ b/base/linalg/generic.jl @@ -8,8 +8,8 @@ scale(s::Number, X::AbstractArray) = s*X # For better performance when input and output are the same array # See https://github.com/JuliaLang/julia/issues/8415#issuecomment-56608729 function generic_scale!(X::AbstractArray, s::Number) - for i = 1:length(X) - @inbounds X[i] *= s + for I in eachindex(X) + @inbounds X[I] *= s end X end @@ -18,8 +18,14 @@ function generic_scale!(C::AbstractArray, X::AbstractArray, s::Number) if length(C) != length(X) throw(DimensionMismatch("first array has length $(length(C)) which does not match the length of the second, $(length(X)).")) end - for i = 1:length(X) - @inbounds C[i] = X[i]*s + if size(C) == size(X) + for I in eachindex(C, X) + @inbounds C[I] = X[I]*s + end + else + for (IC, IX) in zip(eachindex(C), eachindex(X)) + @inbounds C[IC] = X[IX]*s + end end C end