Skip to content

Commit fa6c977

Browse files
committed
Use eachindex in generic_scale!
1 parent 02748d1 commit fa6c977

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

base/linalg/generic.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ scale(s::Number, X::AbstractArray) = s*X
88
# For better performance when input and output are the same array
99
# See https://github.com/JuliaLang/julia/issues/8415#issuecomment-56608729
1010
function generic_scale!(X::AbstractArray, s::Number)
11-
for i = 1:length(X)
12-
@inbounds X[i] *= s
11+
for I in eachindex(X)
12+
@inbounds X[I] *= s
1313
end
1414
X
1515
end
@@ -18,8 +18,14 @@ function generic_scale!(C::AbstractArray, X::AbstractArray, s::Number)
1818
if length(C) != length(X)
1919
throw(DimensionMismatch("first array has length $(length(C)) which does not match the length of the second, $(length(X))."))
2020
end
21-
for i = 1:length(X)
22-
@inbounds C[i] = X[i]*s
21+
if size(C) == size(X)
22+
for I in eachindex(C, X)
23+
@inbounds C[I] = X[I]*s
24+
end
25+
else
26+
for (IC, IX) in zip(eachindex(C), eachindex(X))
27+
@inbounds C[IC] = X[IX]*s
28+
end
2329
end
2430
C
2531
end

0 commit comments

Comments
 (0)