Skip to content

Commit d10910e

Browse files
committed
CartesianIndex to BandIndex
1 parent add5813 commit d10910e

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/LinearAlgebra.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,12 @@ function _torowcol(b::BandIndex)
517517
end
518518
CartesianIndex(b::BandIndex) = CartesianIndex{2}(b)
519519
CartesianIndex{2}(b::BandIndex) = CartesianIndex{2}(_torowcol(b))
520+
function BandIndex(c::CartesianIndex{2})
521+
row, col = Tuple(c)
522+
band = col - row
523+
index = min(row, col)
524+
BandIndex(band, index)
525+
end
520526
function Base.to_indices(A, inds, t::Tuple{BandIndex, Vararg})
521527
to_indices(A, inds, (_torowcol(first(t))..., Base.tail(t)...))
522528
end

test/special.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,12 @@ end
764764
end
765765
@test_throws BoundsError D[BandIndex(size(D,1),1)]
766766
end
767+
@testset "BandIndex to CartesianIndex" begin
768+
b = BandIndex(1, 2)
769+
c = CartesianIndex(b)
770+
@test c == CartesianIndex(2, 3)
771+
@test BandIndex(c) == b
772+
end
767773
end
768774

769775
@testset "Partly filled Hermitian and Diagonal algebra" begin

0 commit comments

Comments
 (0)