Skip to content

Commit 3558ff2

Browse files
committed
Band indexing for adj/trans
1 parent e7a8a15 commit 3558ff2

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/adjtrans.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ IndexStyle(::Type{<:AdjOrTransAbsVec}) = IndexLinear()
352352
@propagate_inbounds getindex(v::AdjOrTransAbsVec, ::Colon, is::AbstractArray{Int}) = wrapperop(v)(v.parent[is])
353353
@propagate_inbounds getindex(v::AdjOrTransAbsVec, ::Colon, ::Colon) = wrapperop(v)(v.parent[:])
354354

355+
# band indexing
356+
@propagate_inbounds function getindex(A::AdjOrTransAbsMat{T}, b::BandIndex) where {T}
357+
require_one_based_indexing(A)
358+
wrapperop(A)(A.parent[BandIndex(-b.band, b.index)])::T
359+
end
360+
355361
# conversion of underlying storage
356362
convert(::Type{Adjoint{T,S}}, A::Adjoint) where {T,S} = Adjoint{T,S}(convert(S, A.parent))::Adjoint{T,S}
357363
convert(::Type{Transpose{T,S}}, A::Transpose) where {T,S} = Transpose{T,S}(convert(S, A.parent))::Transpose{T,S}

test/adjtrans.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,4 +749,36 @@ end
749749
end
750750
end
751751

752+
@testset "band indexing" begin
753+
n = 3
754+
A = UnitUpperTriangular(reshape(1:n^2, n, n))
755+
@testset "every index" begin
756+
Aadj = A'
757+
for k in -(n-1):n-1
758+
di = diagind(Aadj, k, IndexStyle(Aadj))
759+
for (i,d) in enumerate(di)
760+
@test Aadj[LinearAlgebra.BandIndex(k,i)] == Aadj[d]
761+
end
762+
end
763+
end
764+
@testset "inference for structured matrices" begin
765+
v = @inferred ((A,i) -> Val(A'[LinearAlgebra.BandIndex(0,i)]))(A,1)
766+
@test v == Val(1)
767+
end
768+
@testset "non-square matrix" begin
769+
r = reshape(1:6, 2, 3)
770+
for d in (r, r*im)
771+
@test d'[LinearAlgebra.BandIndex(1,1)] == adjoint(d[2,1])
772+
@test d'[LinearAlgebra.BandIndex(-1,2)] == adjoint(d[2,3])
773+
@test transpose(d)[LinearAlgebra.BandIndex(1,1)] == transpose(d[2,1])
774+
@test transpose(d)[LinearAlgebra.BandIndex(-1,2)] == transpose(d[2,3])
775+
end
776+
end
777+
@testset "block matrix" begin
778+
B = reshape([[1 2; 3 4]*i for i in 1:4], 2, 2)
779+
@test B'[LinearAlgebra.BandIndex(1,1)] == adjoint(B[2,1])
780+
@test transpose(B)[LinearAlgebra.BandIndex(1,1)] == transpose(B[2,1])
781+
end
782+
end
783+
752784
end # module TestAdjointTranspose

0 commit comments

Comments
 (0)