Skip to content

Commit

Permalink
Fix case where elements are matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Jun 16, 2020
1 parent 77e5677 commit 421f029
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion stdlib/LinearAlgebra/src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,11 @@ function diagm_size(size::Tuple{Int,Int}, kv::Pair{<:Integer,<:AbstractVector}..
return m, n
end
# For some type `T`, `zero(T)` is not a `T` and `zeros(T, ...)` fails.
promote_with_zero(T::Type) = promote_type(T, typeof(zero(T)))
# `zero_type(T::Type) = typeof(zero(T))` would not work for types such
# as `Array` for which `zero(::T)` is defined but not `zero(::Type{T})`
# so we use `promote_op` instead.
zero_type(T::Type) = promote_op(zero, T)
promote_with_zero(T::Type) = promote_type(T, zero_type(T))
function diagm_container(size, kv::Pair{<:Integer,<:AbstractVector}...)
T = promote_type(map(x -> eltype(x.second), kv)...)
return zeros(promote_with_zero(T), diagm_size(size, kv...)...)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ end
end

struct TypeWithoutZero end
Base.zero(::Type{TypeWithoutZero}) = TypeWithZero()
Base.zero(::Union{TypeWithoutZero, Type{TypeWithoutZero}}) = TypeWithZero()
struct TypeWithZero end
Base.promote_rule(::Type{TypeWithoutZero}, ::Type{TypeWithZero}) = TypeWithZero
Base.convert(::Type{TypeWithZero}, ::TypeWithoutZero) = TypeWithZero()
Expand Down

0 comments on commit 421f029

Please sign in to comment.