Skip to content

Commit

Permalink
Safer indexing in dense linalg methods (#56451)
Browse files Browse the repository at this point in the history
Ensure that `eachindex` is used consistently alongside `@inbounds`, and
use `diagind` to obtain indices along a diagonal.
  • Loading branch information
jishnub authored Nov 9, 2024
1 parent ecfd1a0 commit 803316a
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -559,14 +559,13 @@ function schurpow(A::AbstractMatrix, p)
end
end
function (^)(A::AbstractMatrix{T}, p::Real) where T
n = checksquare(A)

checksquare(A)
# Quicker return if A is diagonal
if isdiag(A)
TT = promote_op(^, T, typeof(p))
retmat = copymutable_oftype(A, TT)
for i in axes(retmat,1)
retmat[i, i] = retmat[i, i] ^ p
for i in diagind(retmat, IndexStyle(retmat))
retmat[i] = retmat[i] ^ p
end
return retmat
end
Expand Down Expand Up @@ -1080,7 +1079,7 @@ function sin(A::AbstractMatrix{<:Complex})
T = complex(float(eltype(A)))
X = exp!(T.(im .* A))
Y = exp!(T.(.-im .* A))
@inbounds for i in eachindex(X)
@inbounds for i in eachindex(X, Y)
x, y = X[i]/2, Y[i]/2
X[i] = Complex(imag(x)-imag(y), real(y)-real(x))
end
Expand Down Expand Up @@ -1128,7 +1127,7 @@ function sincos(A::AbstractMatrix{<:Complex})
T = complex(float(eltype(A)))
X = exp!(T.(im .* A))
Y = exp!(T.(.-im .* A))
@inbounds for i in eachindex(X)
@inbounds for i in eachindex(X, Y)
x, y = X[i]/2, Y[i]/2
X[i] = Complex(imag(x)-imag(y), real(y)-real(x))
Y[i] = x+y
Expand Down Expand Up @@ -1200,7 +1199,7 @@ function tanh(A::AbstractMatrix)
end
X = exp(A)
Y = exp!(float.(.-A))
@inbounds for i in eachindex(X)
@inbounds for i in eachindex(X, Y)
x, y = X[i], Y[i]
X[i] = x - y
Y[i] = x + y
Expand Down

0 comments on commit 803316a

Please sign in to comment.