Skip to content

Commit f1fd10f

Browse files
committed
Use views for wrappers
1 parent 5898006 commit f1fd10f

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/structuredbroadcast.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ _preprocess_broadcasted(::Type, x) = x
220220

221221
_preprocess_broadcasted(::Type{Diagonal}, d::Diagonal) = d.diag
222222
# fallback for types that might opt into Diagonal-like structured broadcasting, e.g. wrappers
223-
_preprocess_broadcasted(::Type{Diagonal}, d::AbstractMatrix) = diag(d)
223+
_preprocess_broadcasted(::Type{Diagonal}, d::AbstractMatrix) = diagview(d)
224224

225225
function copy(bc::Broadcasted{StructuredMatrixStyle{Diagonal}})
226226
if isstructurepreserving(bc) || fzeropreserving(bc)

test/structuredbroadcast.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,19 @@ end
398398
end
399399
end
400400

401-
@testset "forwarding for Diganoal" begin
401+
@testset "forwarding broadcast to the diag for a Diagonal" begin
402402
D = Diagonal(1:4)
403403
D2 = D .* 2
404404
@test D2 isa Diagonal{Int, <:AbstractRange{Int}}
405+
406+
# test for wrappers that opt into Diagonal-like broadcasting
407+
U = UpperTriangular(D)
408+
bc = Broadcast.broadcasted(+, D, U)
409+
bcD = Broadcast.broadcasted(+, D, D)
410+
S = typeof(Broadcast.BroadcastStyle(typeof(bcD)))
411+
bc2 = convert(Broadcast.Broadcasted{S}, bc)
412+
@test copy(bc2) == copy(bc) == copy(bcD)
413+
@test copy(bc2) isa Diagonal
405414
end
406415

407416
end

0 commit comments

Comments
 (0)