Skip to content

Commit 11192f6

Browse files
committed
Band indexing for adj/trans
1 parent b5cc56a commit 11192f6

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
@@ -747,4 +747,36 @@ end
747747
end
748748
end
749749

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

0 commit comments

Comments
 (0)