@@ -84,6 +84,9 @@ function diagview(a::AbstractArray)
8484 return @view a[diagindices (a)]
8585end
8686
87+ using LinearAlgebra: Diagonal
88+ diagview (a:: Diagonal ) = a. diag
89+
8790function getdiagindex (a:: AbstractArray , i:: Integer )
8891 return diagview (a)[i]
8992end
110113 diagonal(v::AbstractVector) -> AbstractMatrix
111114
112115Return a diagonal matrix from a vector `v`.
113- This is an extension of `LinearAlgebra.Diagonal`, designed to avoid the implication of the output type.
116+ This is an extension of `LinearAlgebra.Diagonal`, designed to avoid
117+ the implication of the output type.
114118Defaults to `Diagonal(v)`.
115119"""
116120diagonal (v:: AbstractVector ) = LinearAlgebra. Diagonal (v)
121+
122+ """
123+ diagonal(m::AbstractMatrix) -> AbstractMatrix
124+
125+ Return a diagonal matrix from a matrix `m` where the diagonal
126+ values are copied from the diagonal of `m`.
127+ This is an extension of `LinearAlgebra.Diagonal`, designed to avoid
128+ the implication of the output type.
129+ Defaults to `diagonal(copy(diagview(m)))`, which in general is
130+ equivalent to `Diagonal(m)`.
131+ """
132+ diagonal (m:: AbstractMatrix ) = diagonal (copy (diagview (m)))
133+
134+ """
135+ diagonaltype(::AbstractVector) -> Type{<:AbstractMatrix}
136+ diagonaltype(::Type{<:AbstractVector}) -> Type{<:AbstractMatrix}
137+ diagonaltype(::AbstractMatrix) -> Type{<:AbstractMatrix}
138+ diagonaltype(::Type{<:AbstractMatrix}) -> Type{<:AbstractMatrix}
139+
140+ Return the type of diagonal matrix that would be created from a vector or matrix
141+ using the [`diagonal`](@ref) function.
142+ """
143+ diagonaltype
144+
145+ diagonaltype (v:: AbstractVector ) = diagonaltype (typeof (v))
146+ diagonaltype (V:: Type{<:AbstractVector} ) = Base. promote_op (diagonal, V)
147+ diagonaltype (m:: AbstractMatrix ) = diagonaltype (typeof (m))
148+ diagonaltype (M:: Type{<:AbstractMatrix} ) = Base. promote_op (diagonal, M)
0 commit comments