diff --git a/stdlib/LinearAlgebra/src/LinearAlgebra.jl b/stdlib/LinearAlgebra/src/LinearAlgebra.jl index ebdb78a0c63c2..acafc466f2919 100644 --- a/stdlib/LinearAlgebra/src/LinearAlgebra.jl +++ b/stdlib/LinearAlgebra/src/LinearAlgebra.jl @@ -454,8 +454,13 @@ _cut_B(x::AbstractVector, r::UnitRange) = length(x) > length(r) ? x[r] : x _cut_B(X::AbstractMatrix, r::UnitRange) = size(X, 1) > length(r) ? X[r,:] : X ## append right hand side with zeros if necessary -_zeros(::Type{T}, b::AbstractVector, n::Integer) where {T} = zeros(T, max(length(b), n)) -_zeros(::Type{T}, B::AbstractMatrix, n::Integer) where {T} = zeros(T, max(size(B, 1), n), size(B, 2)) +_zeros(::Type{T}, b::AbstractVector, n::Integer) where {T} = + fill!(similar(b, T, max(length(b), n)), zero(T)) +_zeros(::Type{T}, B::AbstractMatrix, n::Integer) where {T} = + fill!(similar(B, T, max(size(B, 1), n), size(B, 2)), zero(T)) +# return a dense array, since similar(::Union{BiTriSym, Diagonal}) returns a sparse array +_zeros(::Type{T}, B::Union{BiTriSym, Diagonal}, n::Integer) where {T} = + zeros(T, max(size(B, 1), n), size(B, 2)) # General fallback definition for handling under- and overdetermined system as well as square problems # While this definition is pretty general, it does e.g. promote to common element type of lhs and rhs diff --git a/stdlib/SparseArrays/src/SparseArrays.jl b/stdlib/SparseArrays/src/SparseArrays.jl index e3fcd1ef955c9..97fd6096f07af 100644 --- a/stdlib/SparseArrays/src/SparseArrays.jl +++ b/stdlib/SparseArrays/src/SparseArrays.jl @@ -54,6 +54,11 @@ similar(M::Tridiagonal, ::Type{T}, dims::Union{Dims{1},Dims{2}}) where {T} = spz zero(a::AbstractSparseArray) = spzeros(eltype(a), size(a)...) +LinearAlgebra._zeros(::Type{T}, b::AbstractSparseVector, n::Integer) where {T} = + zeros(T, max(length(b), n)) +LinearAlgebra._zeros(::Type{T}, B::AbstractSparseMatrix, n::Integer) where {T} = + zeros(T, max(size(B, 1), n), size(B, 2)) + const BiTriSym = Union{Bidiagonal,SymTridiagonal,Tridiagonal} function *(A::BiTriSym, B::BiTriSym) TS = promote_op(matprod, eltype(A), eltype(B))