22
33# # Diagonal matrices
44
5- struct Diagonal{T} <: AbstractMatrix{T}
6- diag:: Vector{T}
5+ struct Diagonal{T, V <: AbstractVector{T} } <: AbstractMatrix{T}
6+ diag:: V
77end
88"""
99 Diagonal(A::AbstractMatrix)
1010
11- Constructs a matrix from the diagonal of `A`.
12-
13- # Example
11+ Construct a matrix from the diagonal of `A`.
1412
13+ # Examples
1514```jldoctest
1615julia> A = [1 2 3; 4 5 6; 7 8 9]
17163×3 Array{Int64,2}:
@@ -20,36 +19,38 @@ julia> A = [1 2 3; 4 5 6; 7 8 9]
2019 7 8 9
2120
2221julia> Diagonal(A)
23- 3×3 Diagonal{Int64}:
22+ 3×3 Diagonal{Int64,Array{Int64,1} }:
2423 1 ⋅ ⋅
2524 ⋅ 5 ⋅
2625 ⋅ ⋅ 9
2726```
2827"""
28+
2929Diagonal (A:: AbstractMatrix ) = Diagonal (diag (A))
3030"""
3131 Diagonal(V::AbstractVector)
3232
33- Constructs a matrix with `V` as its diagonal.
34-
35- # Example
33+ Construct a matrix with `V` as its diagonal.
3634
35+ # Examples
3736```jldoctest
38- julia> V = [1; 2]
37+ julia> V = [1, 2]
39382-element Array{Int64,1}:
4039 1
4140 2
4241
4342julia> Diagonal(V)
44- 2×2 Diagonal{Int64}:
43+ 2×2 Diagonal{Int64,Array{Int64,1} }:
4544 1 ⋅
4645 ⋅ 2
4746```
4847"""
49- Diagonal (V:: AbstractVector ) = Diagonal (collect (V))
48+ Diagonal (V:: AbstractVector{T} ) where {T} = Diagonal {T,typeof(V)} (V)
49+ Diagonal {T} (V:: AbstractVector{T} ) where {T} = Diagonal {T,typeof(V)} (V)
50+ Diagonal {T} (V:: AbstractVector ) where {T} = Diagonal {T} (convert (AbstractVector{T}, V))
5051
5152convert (:: Type{Diagonal{T}} , D:: Diagonal{T} ) where {T} = D
52- convert (:: Type{Diagonal{T}} , D:: Diagonal ) where {T} = Diagonal {T} (convert (Vector {T}, D. diag))
53+ convert (:: Type{Diagonal{T}} , D:: Diagonal ) where {T} = Diagonal {T} (convert (AbstractVector {T}, D. diag))
5354convert (:: Type{AbstractMatrix{T}} , D:: Diagonal ) where {T} = convert (Diagonal{T}, D)
5455convert (:: Type{Matrix} , D:: Diagonal ) = diagm (D. diag)
5556convert (:: Type{Array} , D:: Diagonal ) = convert (Matrix, D)
0 commit comments