Skip to content

Commit

Permalink
Narrower inbounds annotations in generic LinearAlgebra functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Oct 19, 2024
1 parent 34b5aa0 commit 3752c12
Showing 1 changed file with 41 additions and 43 deletions.
84 changes: 41 additions & 43 deletions stdlib/LinearAlgebra/src/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -709,14 +709,12 @@ function opnorm1(A::AbstractMatrix{T}) where T
Tnorm = typeof(float(real(zero(T))))
Tsum = promote_type(Float64, Tnorm)
nrm::Tsum = 0
@inbounds begin
for j = axes(A,2)
nrmj::Tsum = 0
for i = axes(A,1)
nrmj += norm(A[i,j])
end
nrm = max(nrm,nrmj)
for j = axes(A,2)
nrmj::Tsum = 0
for i = axes(A,1)
nrmj += norm(@inbounds A[i,j])
end
nrm = max(nrm,nrmj)
end
return convert(Tnorm, nrm)
end
Expand All @@ -736,14 +734,12 @@ function opnormInf(A::AbstractMatrix{T}) where T
Tnorm = typeof(float(real(zero(T))))
Tsum = promote_type(Float64, Tnorm)
nrm::Tsum = 0
@inbounds begin
for i = axes(A,1)
nrmi::Tsum = 0
for j = axes(A,2)
nrmi += norm(A[i,j])
end
nrm = max(nrm,nrmi)
for i = axes(A,1)
nrmi::Tsum = 0
for j = axes(A,2)
nrmi += norm(@inbounds A[i,j])
end
nrm = max(nrm,nrmi)
end
return convert(Tnorm, nrm)
end
Expand Down Expand Up @@ -938,7 +934,7 @@ function dot(x::AbstractArray, y::AbstractArray)
end
s = zero(dot(first(x), first(y)))
for (Ix, Iy) in zip(eachindex(x), eachindex(y))
@inbounds s += dot(x[Ix], y[Iy])
s += dot(@inbounds(x[Ix]), @inbounds(y[Iy]))
end
s
end
Expand Down Expand Up @@ -979,11 +975,11 @@ function dot(x::AbstractVector, A::AbstractMatrix, y::AbstractVector)
s = zero(T)
i₁ = first(eachindex(x))
x₁ = first(x)
@inbounds for j in eachindex(y)
yj = y[j]
for j in eachindex(y)
yj = @inbounds y[j]
if !iszero(yj)
temp = zero(adjoint(A[i₁,j]) * x₁)
@simd for i in eachindex(x)
temp = zero(adjoint(@inbounds A[i₁,j]) * x₁)
@inbounds @simd for i in eachindex(x)
temp += adjoint(A[i,j]) * x[i]
end
s += dot(temp, yj)
Expand Down Expand Up @@ -1596,10 +1592,12 @@ function rotate!(x::AbstractVector, y::AbstractVector, c, s)
if n != length(y)
throw(DimensionMismatch(lazy"x has length $(length(x)), but y has length $(length(y))"))
end
@inbounds for i = eachindex(x,y)
xi, yi = x[i], y[i]
x[i] = c *xi + s*yi
y[i] = -conj(s)*xi + c*yi
for i = eachindex(x,y)
@inbounds begin
xi, yi = x[i], y[i]
x[i] = c *xi + s*yi
y[i] = -conj(s)*xi + c*yi
end
end
return x, y
end
Expand All @@ -1619,10 +1617,12 @@ function reflect!(x::AbstractVector, y::AbstractVector, c, s)
if n != length(y)
throw(DimensionMismatch(lazy"x has length $(length(x)), but y has length $(length(y))"))
end
@inbounds for i = eachindex(x,y)
xi, yi = x[i], y[i]
x[i] = c *xi + s*yi
y[i] = conj(s)*xi - c*yi
for i = eachindex(x,y)
@inbounds begin
xi, yi = x[i], y[i]
x[i] = c *xi + s*yi
y[i] = conj(s)*xi - c*yi
end
end
return x, y
end
Expand All @@ -1633,18 +1633,16 @@ end
require_one_based_indexing(x)
n = length(x)
n == 0 && return zero(eltype(x))
@inbounds begin
ξ1 = x[1]
normu = norm(x)
if iszero(normu)
return zero(ξ1/normu)
end
ν = T(copysign(normu, real(ξ1)))
ξ1 += ν
x[1] = -ν
for i = 2:n
x[i] /= ξ1
end
ξ1 = @inbounds x[1]
normu = norm(x)
if iszero(normu)
return zero(ξ1/normu)
end
ν = T(copysign(normu, real(ξ1)))
ξ1 += ν
@inbounds x[1] = -ν
for i = 2:n
@inbounds x[i] /= ξ1
end
ξ1/ν
end
Expand All @@ -1661,10 +1659,10 @@ Multiplies `A` in-place by a Householder reflection on the left. It is equivalen
throw(DimensionMismatch(lazy"reflector has length $(length(x)), which must match the first dimension of matrix A, $m"))
end
m == 0 && return A
@inbounds for j = axes(A,2)
Aj, xj = view(A, 2:m, j), view(x, 2:m)
vAj = conj(τ)*(A[1, j] + dot(xj, Aj))
A[1, j] -= vAj
for j = axes(A,2)
Aj, xj = @inbounds view(A, 2:m, j), view(x, 2:m)
vAj = conj(τ)*(@inbounds(A[1, j]) + dot(xj, Aj))
@inbounds A[1, j] -= vAj
axpy!(-vAj, xj, Aj)
end
return A
Expand Down

0 comments on commit 3752c12

Please sign in to comment.