Skip to content

Commit 803316a

Browse files
authored
Safer indexing in dense linalg methods (JuliaLang#56451)
Ensure that `eachindex` is used consistently alongside `@inbounds`, and use `diagind` to obtain indices along a diagonal.
1 parent ecfd1a0 commit 803316a

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

stdlib/LinearAlgebra/src/dense.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -559,14 +559,13 @@ function schurpow(A::AbstractMatrix, p)
559559
end
560560
end
561561
function (^)(A::AbstractMatrix{T}, p::Real) where T
562-
n = checksquare(A)
563-
562+
checksquare(A)
564563
# Quicker return if A is diagonal
565564
if isdiag(A)
566565
TT = promote_op(^, T, typeof(p))
567566
retmat = copymutable_oftype(A, TT)
568-
for i in axes(retmat,1)
569-
retmat[i, i] = retmat[i, i] ^ p
567+
for i in diagind(retmat, IndexStyle(retmat))
568+
retmat[i] = retmat[i] ^ p
570569
end
571570
return retmat
572571
end
@@ -1080,7 +1079,7 @@ function sin(A::AbstractMatrix{<:Complex})
10801079
T = complex(float(eltype(A)))
10811080
X = exp!(T.(im .* A))
10821081
Y = exp!(T.(.-im .* A))
1083-
@inbounds for i in eachindex(X)
1082+
@inbounds for i in eachindex(X, Y)
10841083
x, y = X[i]/2, Y[i]/2
10851084
X[i] = Complex(imag(x)-imag(y), real(y)-real(x))
10861085
end
@@ -1128,7 +1127,7 @@ function sincos(A::AbstractMatrix{<:Complex})
11281127
T = complex(float(eltype(A)))
11291128
X = exp!(T.(im .* A))
11301129
Y = exp!(T.(.-im .* A))
1131-
@inbounds for i in eachindex(X)
1130+
@inbounds for i in eachindex(X, Y)
11321131
x, y = X[i]/2, Y[i]/2
11331132
X[i] = Complex(imag(x)-imag(y), real(y)-real(x))
11341133
Y[i] = x+y
@@ -1200,7 +1199,7 @@ function tanh(A::AbstractMatrix)
12001199
end
12011200
X = exp(A)
12021201
Y = exp!(float.(.-A))
1203-
@inbounds for i in eachindex(X)
1202+
@inbounds for i in eachindex(X, Y)
12041203
x, y = X[i], Y[i]
12051204
X[i] = x - y
12061205
Y[i] = x + y

0 commit comments

Comments
 (0)