Skip to content

Commit 543040e

Browse files
committed
CartesianIndex constructor for BandIndex
1 parent fd115f4 commit 543040e

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/LinearAlgebra.jl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import Base: USE_BLAS64, abs, acos, acosh, acot, acoth, acsc, acsch, adjoint, as
1515
IndexStyle, kron, kron!, length, log, map, ndims, one, oneunit, parent, permutecols!,
1616
permutedims, permuterows!, power_by_squaring, promote_rule, real, isreal, sec, sech, setindex!,
1717
show, similar, sin, sincos, sinh, size, sqrt, strides, stride, tan, tanh, transpose, trunc,
18-
typed_hcat, vec, view, zero
18+
typed_hcat, vec, view, zero, CartesianIndex
1919
import Base: AbstractArray, AbstractMatrix, Array, Matrix
2020
using Base: IndexLinear, promote_eltype, promote_op, print_matrix,
2121
@propagate_inbounds, reduce, typed_hvcat, typed_vcat, require_one_based_indexing,
@@ -508,21 +508,23 @@ struct BandIndex
508508
band :: Int
509509
index :: Int
510510
end
511-
function _cartinds(b::BandIndex)
511+
function _torowcol(b::BandIndex)
512512
(; band, index) = b
513-
bandg0 = max(band,0)
514-
row = index - band + bandg0
515-
col = index + bandg0
516-
CartesianIndex(row, col)
513+
minband0, maxband0 = minmax(band,0)
514+
row = index - minband0
515+
col = index + maxband0
516+
row, col
517517
end
518-
function Base.to_indices(A, inds, t::Tuple{BandIndex, Vararg{Any}})
519-
to_indices(A, inds, (_cartinds(first(t)), Base.tail(t)...))
518+
CartesianIndex(b::BandIndex) = CartesianIndex{2}(b)
519+
CartesianIndex{2}(b::BandIndex) = CartesianIndex{2}(_torowcol(b))
520+
function Base.to_indices(A, inds, t::Tuple{BandIndex, Vararg})
521+
to_indices(A, inds, (_torowcol(first(t))..., Base.tail(t)...))
520522
end
521523
function Base.checkbounds(::Type{Bool}, A::AbstractMatrix, b::BandIndex)
522-
checkbounds(Bool, A, _cartinds(b))
524+
checkbounds(Bool, A, _torowcol(b)...)
523525
end
524526
function Base.checkbounds(A::Broadcasted, b::BandIndex)
525-
checkbounds(A, _cartinds(b))
527+
checkbounds(A, _torowcol(b)...)
526528
end
527529

528530
include("adjtrans.jl")

0 commit comments

Comments
 (0)