Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion stdlib/LinearAlgebra/src/structuredbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ find_uplo(bc::Broadcasted) = mapfoldl(find_uplo, merge_uplos, Broadcast.cat_nest
function structured_broadcast_alloc(bc, ::Type{Bidiagonal}, ::Type{ElType}, n) where {ElType}
uplo = n > 0 ? find_uplo(bc) : 'U'
n1 = max(n - 1, 0)
if uplo == 'T'
if count_structedmatrix(Bidiagonal, bc) > 1 && uplo == 'T'
return Tridiagonal(Array{ElType}(undef, n1), Array{ElType}(undef, n), Array{ElType}(undef, n1))
end
return Bidiagonal(Array{ElType}(undef, n),Array{ElType}(undef, n1), uplo)
Expand Down Expand Up @@ -135,6 +135,8 @@ iszerodefined(::Type{<:Number}) = true
iszerodefined(::Type{<:AbstractArray{T}}) where T = iszerodefined(T)
iszerodefined(::Type{<:UniformScaling{T}}) where T = iszerodefined(T)

count_structedmatrix(T, bc::Broadcasted) = sum(Base.Fix2(isa, T), Broadcast.cat_nested(bc); init = 0)

fzeropreserving(bc) = (v = fzero(bc); !ismissing(v) && (iszerodefined(typeof(v)) ? iszero(v) : v == 0))
# Like sparse matrices, we assume that the zero-preservation property of a broadcasted
# expression is stable. We can test the zero-preservability by applying the function
Expand Down
18 changes: 18 additions & 0 deletions stdlib/LinearAlgebra/test/structuredbroadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ using Test, LinearAlgebra
@test broadcast!(*, Z, X, Y) == broadcast(*, fX, fY)
end
end

@testset "type-stability in Bidiagonal" begin
B2 = @inferred (B -> .- B)(B)
@test B2 isa Bidiagonal
@test B2 == -1 * B
B2 = @inferred (B -> B .* 2)(B)
@test B2 isa Bidiagonal
@test B2 == B + B
B2 = @inferred (B -> 2 .* B)(B)
@test B2 isa Bidiagonal
@test B2 == B + B
B2 = @inferred (B -> B ./ 1)(B)
@test B2 isa Bidiagonal
@test B2 == B
B2 = @inferred (B -> 1 .\ B)(B)
@test B2 isa Bidiagonal
@test B2 == B
end
end

@testset "broadcast! where the destination is a structured matrix" begin
Expand Down