Skip to content

Commit 1a101c7

Browse files
authored
Add AbstractArray conversion special cases (#208)
* Add AbstractArray conversion special cases * Don't convert blocks to ints in blockcolstart/etc. * Update blocklinalg.jl * Add rtests
1 parent e427fc8 commit 1a101c7

File tree

7 files changed

+35
-11
lines changed

7 files changed

+35
-11
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BlockArrays"
22
uuid = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
3-
version = "0.16.15"
3+
version = "0.16.16"
44

55
[deps]
66
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"

src/BlockArrays.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import Base: @propagate_inbounds, Array, to_indices, to_index,
3030
BroadcastStyle, checkbounds, throw_boundserror,
3131
ones, zeros, intersect, Slice, resize!
3232
using Base: ReshapedArray, dataids
33+
import Base: AbstractArray
3334

3435

3536
import Base: (:), IteratorSize, iterate, axes1, strides, isempty

src/blockindices.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Number(index::Block{1}) = index.n[1]
9292

9393
# print
9494
Base.show(io::IO, B::Block{0,Int}) = print(io, "Block()")
95-
function Base.show(io::IO, B::Block{N,Int}) where N
95+
function Base.show(io::IO, B::Block{N,Int}) where N
9696
print(io, "Block($(B.n[1])")
9797
for n in Base.tail(B.n)
9898
print(io, ", $n")
@@ -177,7 +177,7 @@ end
177177
checkbounds(Bool, B, blockindex(I)...)
178178
end
179179

180-
checkbounds(::Type{Bool}, A::AbstractArray{<:Any,N}, I::AbstractVector{BlockIndex{N}}) where N =
180+
checkbounds(::Type{Bool}, A::AbstractArray{<:Any,N}, I::AbstractVector{BlockIndex{N}}) where N =
181181
all(checkbounds.(Bool, Ref(A), I))
182182

183183
struct BlockIndexRange{N,R<:NTuple{N,AbstractUnitRange{Int}}} <: AbstractArray{BlockIndex{N},N}
@@ -381,9 +381,9 @@ intersect(a::BlockRange{1}, b::BlockRange{1}) = BlockRange(intersect(a.indices[1
381381

382382
# needed for scalar-like broadcasting
383383

384-
BlockSlice{Block{1,BT},RT}(a::Base.OneTo) where {BT,RT<:AbstractUnitRange} =
384+
BlockSlice{Block{1,BT},RT}(a::Base.OneTo) where {BT,RT<:AbstractUnitRange} =
385385
BlockSlice(Block(convert(BT, 1)), convert(RT, a))::BlockSlice{Block{1,BT},RT}
386-
BlockSlice{BlockRange{1,Tuple{BT}},RT}(a::Base.OneTo) where {BT<:AbstractUnitRange,RT<:AbstractUnitRange} =
386+
BlockSlice{BlockRange{1,Tuple{BT}},RT}(a::Base.OneTo) where {BT<:AbstractUnitRange,RT<:AbstractUnitRange} =
387387
BlockSlice(BlockRange(convert(BT, Base.OneTo(1))), convert(RT, a))::BlockSlice{BlockRange{1,Tuple{BT}},RT}
388-
BlockSlice{BlockIndexRange{1,Tuple{BT}},RT}(a::Base.OneTo) where {BT<:AbstractUnitRange,RT<:AbstractUnitRange} =
388+
BlockSlice{BlockIndexRange{1,Tuple{BT}},RT}(a::Base.OneTo) where {BT<:AbstractUnitRange,RT<:AbstractUnitRange} =
389389
BlockSlice(BlockIndexRange(Block(1), convert(BT, Base.OneTo(1))), convert(RT, a))::BlockSlice{BlockIndexRange{1,Tuple{BT}},RT}

src/blocklinalg.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ blockcolstop(A...) = last(blockcolsupport(A...))
2222
blockrowstart(A...) = first(blockrowsupport(A...))
2323
blockrowstop(A...) = last(blockrowsupport(A...))
2424

25+
2526
for Func in (:blockcolstart, :blockcolstop, :blockrowstart, :blockrowstop)
26-
@eval $Func(A, i::Block{1}) = $Func(A, Int(i))
27+
@eval @deprecate $Func(A, i::Integer) $Func(A, Block(i))
2728
end
2829

29-
3030
abstract type AbstractBlockLayout <: MemoryLayout end
3131
struct BlockLayout{ArrLay,BlockLay} <: AbstractBlockLayout end
3232

@@ -277,7 +277,7 @@ function _matchingblocks_triangular_mul!(::Val{'L'}, UNIT, A::AbstractMatrix{T},
277277
b_2 = view(b, Block(K))
278278
= _triangular_matrix(Val('L'), UNIT, view(A, Block(K,K)))
279279
materialize!(Lmul(L̃, b_2))
280-
JR = blockrowstart(A,K):Block(K-1)
280+
JR = blockrowstart(A,Block(K)):Block(K-1)
281281
if !isempty(JR)
282282
muladd!(one(T), view(A, Block(K), JR), view(b,JR), one(T), b_2)
283283
end
@@ -327,7 +327,7 @@ for UNIT in ('U', 'N')
327327
materialize!(Ldiv(Ũ, b_2))
328328

329329
if K  2
330-
KR = blockcolstart(A, K):Block(K-1)
330+
KR = blockcolstart(A, Block(K)):Block(K-1)
331331
V_12 = view(A, KR, Block(K))
332332
b̃_1 = view(b, KR)
333333
muladd!(-one(T), V_12, b_2, one(T), b̃_1)
@@ -361,7 +361,7 @@ for UNIT in ('U', 'N')
361361
materialize!(Ldiv(L̃, b_2))
362362

363363
if K < N
364-
KR = Block(K+1):blockcolstop(A, K)
364+
KR = Block(K+1):blockcolstop(A, Block(K))
365365
V_12 = view(A, KR, Block(K))
366366
b̃_1 = view(b, KR)
367367
muladd!(-one(T), V_12, b_2, one(T), b̃_1)

src/pseudo_blockarray.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ convert(::Type{PseudoBlockArray}, A::PseudoBlockArray) = A
122122
convert(::Type{PseudoBlockArray{T,N,R,BS}}, A::PseudoBlockArray) where {T,N,R,BS} =
123123
PseudoBlockArray{T,N,R,BS}(convert(R, A.blocks), convert(BS, A.axes))
124124

125+
convert(::Type{AbstractArray{T,N}}, A::PseudoBlockArray{T,N}) where {T,N} = A
126+
convert(::Type{AbstractArray{V,N} where V}, A::PseudoBlockArray{T,N}) where {T,N} = A
127+
convert(::Type{AbstractArray{T}}, A::PseudoBlockArray{T}) where {T} = A
128+
convert(::Type{AbstractArray}, A::PseudoBlockArray) = A
129+
130+
125131

126132
PseudoBlockArray{T, N}(A::AbstractArray{T2, N}) where {T,T2,N} =
127133
PseudoBlockArray(Array{T, N}(A), axes(A))
@@ -135,6 +141,9 @@ convert(::Type{PseudoBlockArray{T1}}, A::AbstractArray{T2, N}) where {T1,T2,N} =
135141
convert(::Type{PseudoBlockArray}, A::AbstractArray{T, N}) where {T,N} =
136142
convert(PseudoBlockArray{T, N}, A)
137143

144+
AbstractArray{T}(A::PseudoBlockArray) where T = PseudoBlockArray(AbstractArray{T}(A.blocks), A.axes)
145+
AbstractArray{T,N}(A::PseudoBlockArray) where {T,N} = PseudoBlockArray(AbstractArray{T,N}(A.blocks), A.axes)
146+
138147
copy(A::PseudoBlockArray) = PseudoBlockArray(copy(A.blocks), A.axes)
139148

140149
###########################

test/test_blockarrayinterface.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ bview(a, b) = Base.invoke(view, Tuple{AbstractArray,Any}, a, b)
1818

1919
A = randn(5, 5)
2020
@test A[Block(1, 1)] == A
21+
@test A[BlockIndex((1,1), (1,2))] == A[1,2]
22+
@test_throws BoundsError A[BlockIndex((1,1), (1,6))]
2123

2224
@test similar(BlockVector{Float64}, Base.OneTo(5)) isa Vector{Float64}
2325
@test similar(BlockVector{Float64}, 5) isa Vector{Float64}

test/test_blockarrays.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,12 @@ end
345345
@testset "convert" begin
346346
# Could probably be DRY'd.
347347
A = PseudoBlockArray(rand(2,3), [1,1], [2,1])
348+
349+
@test convert(AbstractMatrix{Float64}, A) convert(AbstractMatrix, A) A
350+
@test convert(AbstractArray{Float64}, A) convert(AbstractArray, A) A
351+
@test convert(AbstractMatrix{Float32}, A) == AbstractMatrix{Float32}(A)
352+
@test convert(AbstractArray{Float32}, A) == AbstractArray{Float32}(A)
353+
348354
C = convert(BlockArray, A)
349355
@test C == A == BlockArray(A)
350356
@test eltype(C) == eltype(A)
@@ -365,6 +371,12 @@ end
365371

366372

367373
A = BlockArray(rand(2,3), [1,1], [2,1])
374+
375+
@test convert(AbstractMatrix{Float64}, A) convert(AbstractMatrix, A) A
376+
@test convert(AbstractArray{Float64}, A) convert(AbstractArray, A) A
377+
@test convert(AbstractMatrix{Float32}, A) == AbstractMatrix{Float32}(A)
378+
@test convert(AbstractArray{Float32}, A) == AbstractArray{Float32}(A)
379+
368380
C = convert(PseudoBlockArray, A)
369381
@test C == A == PseudoBlockArray(A)
370382
@test eltype(C) == eltype(A)

0 commit comments

Comments
 (0)