Skip to content

Commit 8fdbfd5

Browse files
jishnubdkarrasch
andauthored
Add getindex for SymTridiagonal using a BandIndex (#1223)
This specialized method was missing for a `SymTridiagonal`. This improves performance: ```julia julia> S = SymTridiagonal(1:4000, 1:3999); julia> @Btime $S .+ $S; 15.217 μs (6 allocations: 62.64 KiB) # master 3.789 μs (6 allocations: 62.64 KiB) # this PR ``` --------- Co-authored-by: Daniel Karrasch <daniel.karrasch@posteo.de>
1 parent 91b8845 commit 8fdbfd5

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/tridiag.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,19 @@ end
474474
end
475475
end
476476

477+
@inline function getindex(A::SymTridiagonal, b::BandIndex)
478+
@boundscheck checkbounds(A, b)
479+
if b.band == 0
480+
return symmetric((@inbounds A.dv[b.index]), :U)::symmetric_type(eltype(A.dv))
481+
elseif b.band == -1
482+
return copy(transpose(@inbounds A.ev[b.index])) # materialized for type stability
483+
elseif b.band == 1
484+
return @inbounds A.ev[b.index]
485+
else
486+
return diagzero(A, b)
487+
end
488+
end
489+
477490
Base._reverse(A::SymTridiagonal, dims) = reverse!(Matrix(A); dims)
478491
Base._reverse(A::SymTridiagonal, dims::Colon) = SymTridiagonal(reverse(A.dv), reverse(A.ev))
479492
Base._reverse!(A::SymTridiagonal, dims::Colon) = (reverse!(A.dv); reverse!(A.ev); A)

0 commit comments

Comments
 (0)