Skip to content

Commit 3e4e9ca

Browse files
committed
Use views for wrappers
1 parent b69158d commit 3e4e9ca

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
@@ -242,7 +242,7 @@ _preprocess_broadcasted(::Type, x) = x
242242

243243
_preprocess_broadcasted(::Type{Diagonal}, d::Diagonal) = d.diag
244244
# fallback for types that might opt into Diagonal-like structured broadcasting, e.g. wrappers
245-
_preprocess_broadcasted(::Type{Diagonal}, d::AbstractMatrix) = diag(d)
245+
_preprocess_broadcasted(::Type{Diagonal}, d::AbstractMatrix) = diagview(d)
246246

247247
function copy(bc::Broadcasted{StructuredMatrixStyle{Diagonal}})
248248
if isstructurepreserving(bc) || fzeropreserving(bc)

test/structuredbroadcast.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,19 @@ end
410410
@test UH2 isa UpperHessenberg
411411
end
412412

413-
@testset "forwarding for Diganoal" begin
413+
@testset "forwarding broadcast to the diag for a Diagonal" begin
414414
D = Diagonal(1:4)
415415
D2 = D .* 2
416416
@test D2 isa Diagonal{Int, <:AbstractRange{Int}}
417+
418+
# test for wrappers that opt into Diagonal-like broadcasting
419+
U = UpperTriangular(D)
420+
bc = Broadcast.broadcasted(+, D, U)
421+
bcD = Broadcast.broadcasted(+, D, D)
422+
S = typeof(Broadcast.BroadcastStyle(typeof(bcD)))
423+
bc2 = convert(Broadcast.Broadcasted{S}, bc)
424+
@test copy(bc2) == copy(bc) == copy(bcD)
425+
@test copy(bc2) isa Diagonal
417426
end
418427

419428
end

0 commit comments

Comments
 (0)