Skip to content

Commit 2adb794

Browse files
authored
Fix ambiguities and broadcasting bugs (#505)
1 parent 8c9d73e commit 2adb794

5 files changed

Lines changed: 17 additions & 4 deletions

File tree

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 = "1.9.3"
3+
version = "1.9.4"
44

55

66
[deps]

src/abstractblockarray.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ end
267267

268268
getindex(A::Adjoint{<:Any,<:LayoutMatrix}, kr::BlockRange{1}, jr::BlockRange{1}) = parent(A)[jr,kr]'
269269
getindex(A::Transpose{<:Any,<:LayoutMatrix}, kr::BlockRange{1}, jr::BlockRange{1}) = transpose(parent(A)[jr,kr])
270+
getindex(A::AdjOrTrans{<:Any,<:LayoutVector}, kr::BlockRange{1}, jr::BlockRange{1}) = ArrayLayouts.layout_getindex(A, kr, jr)
270271

271272
###
272273
# permutedims

src/blockbroadcast.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,13 @@ function _generic_blockbroadcast_copyto!(dest::AbstractArray,
142142

143143
bc = Broadcast.flatten(bc1)
144144
NArgs = length(bc.args)
145-
146145
bs = axes(bc)
146+
147+
if !all(a -> !(a isa AbstractArray) || size(a) == size(dest), bc.args)
148+
# code below assumes only scalars and matching sizes. We therefore go back to default.
149+
return copyto!(dest, Broadcasted{DefaultArrayStyle{NDims}}(bc.f, bc.args, bs))
150+
end
151+
147152
if !blockisequal(axes(dest), bs)
148153
copyto!(BlockedArray(dest, bs), bc)
149154
return dest
@@ -267,14 +272,12 @@ BroadcastStyle(::Type{<:SubArray{<:Any,N,<:BlockedArray,I}}) where {N,I<:Tuple{A
267272
for op in (:*, :/)
268273
@eval begin
269274
broadcasted(::AbstractBlockStyle, ::typeof($op), a::Zeros, b::AbstractArray) = FillArrays._broadcasted_zeros($op, a, b)
270-
broadcasted(::AbstractBlockStyle, ::typeof($op), a::Ones{T}, b::AbstractArray{V}) where {T,V} = LinearAlgebra.copy_oftype(b, Base.promote_op(*, T, V))
271275
end
272276
end
273277

274278
for op in (:*, :\)
275279
@eval begin
276280
broadcasted(::AbstractBlockStyle, ::typeof($op), a::AbstractArray, b::Zeros) = FillArrays._broadcasted_zeros($op, a, b)
277-
broadcasted(::AbstractBlockStyle, ::typeof($op), a::AbstractArray{T}, b::Ones{V}) where {T,V} = LinearAlgebra.copy_oftype(a, Base.promote_op(*, T, V))
278281
end
279282
end
280283

test/test_blockarrays.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,9 @@ end
10661066
@test (a')[:,Block.(1:2)] == transpose(a)[:,Block.(1:2)] == [1 2 3]
10671067
@test (a')[:,Block.(1:2)] isa Adjoint
10681068
@test transpose(a)[:,Block.(1:2)] isa Transpose
1069+
1070+
@test a'[Block.(1:1), Block.(2:3)] == a'[Block.(1:1), 3:4] == a'[Block(1), Block.(2:3)] == a'[Block(1)[1:1], Block.(2:3)] == [3 4]
1071+
@test a'[Block.(1:1), Block(2)[1:1]] == a'[Block(1), Block(2)[1:1]] == [3;;]
10691072
end
10701073

10711074
@testset "empty blocklengths" begin

test/test_blockbroadcast.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ using StaticArrays
327327
@test issorted(w)
328328
@test w == reverse(v)
329329
end
330+
331+
@testset "Ones bug" begin
332+
a = Ones((blockedrange([1,2]),))
333+
b = BlockArray([1 2 3], [1], [2,1])
334+
@test a .* b == Vector(a) .* b == a .* Matrix(b) == Vector(a) .* Matrix(b) == b .* a == Matrix(b) .* a == b .* Vector(a)
335+
end
330336
end
331337

332338
end # module

0 commit comments

Comments
 (0)