Skip to content

Commit ea20d0b

Browse files
committed
CartesianIndex to BandIndex
1 parent 543040e commit ea20d0b

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
@@ -762,6 +762,12 @@ end
762762
end
763763
@test_throws BoundsError D[BandIndex(size(D,1),1)]
764764
end
765+
@testset "BandIndex to CartesianIndex" begin
766+
b = BandIndex(1, 2)
767+
c = CartesianIndex(b)
768+
@test c == CartesianIndex(2, 3)
769+
@test BandIndex(c) == b
770+
end
765771
end
766772

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

0 commit comments

Comments
 (0)