2
2
3
3
# # Diagonal matrices
4
4
5
- struct Diagonal{T} <: AbstractMatrix{T}
6
- diag:: Vector{T}
5
+ struct Diagonal{T,V <: AbstractVector{T} } <: AbstractMatrix{T}
6
+ diag:: V
7
7
end
8
8
"""
9
9
Diagonal(A::AbstractMatrix)
10
10
11
- Constructs a matrix from the diagonal of `A`.
12
-
13
- # Example
11
+ Construct a matrix from the diagonal of `A`.
14
12
13
+ # Examples
15
14
```jldoctest
16
15
julia> A = [1 2 3; 4 5 6; 7 8 9]
17
16
3×3 Array{Int64,2}:
@@ -20,36 +19,38 @@ julia> A = [1 2 3; 4 5 6; 7 8 9]
20
19
7 8 9
21
20
22
21
julia> Diagonal(A)
23
- 3×3 Diagonal{Int64}:
22
+ 3×3 Diagonal{Int64,Array{Int64,1} }:
24
23
1 ⋅ ⋅
25
24
⋅ 5 ⋅
26
25
⋅ ⋅ 9
27
26
```
28
27
"""
29
28
Diagonal (A:: AbstractMatrix ) = Diagonal (diag (A))
29
+
30
30
"""
31
31
Diagonal(V::AbstractVector)
32
32
33
- Constructs a matrix with `V` as its diagonal.
34
-
35
- # Example
33
+ Construct a matrix with `V` as its diagonal.
36
34
35
+ # Examples
37
36
```jldoctest
38
- julia> V = [1; 2]
37
+ julia> V = [1, 2]
39
38
2-element Array{Int64,1}:
40
39
1
41
40
2
42
41
43
42
julia> Diagonal(V)
44
- 2×2 Diagonal{Int64}:
43
+ 2×2 Diagonal{Int64,Array{Int64,1} }:
45
44
1 ⋅
46
45
⋅ 2
47
46
```
48
47
"""
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))
50
51
51
52
convert (:: 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))
53
54
convert (:: Type{AbstractMatrix{T}} , D:: Diagonal ) where {T} = convert (Diagonal{T}, D)
54
55
convert (:: Type{Matrix} , D:: Diagonal ) = diagm (D. diag)
55
56
convert (:: Type{Array} , D:: Diagonal ) = convert (Matrix, D)
0 commit comments