Skip to content

Commit 67731c2

Browse files
fredrikekreandreasnoack
authored andcommitted
parametrize Diagonal on the wrapped vector type (#22718)
1 parent 223dd50 commit 67731c2

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

NEWS.md

+6
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ This section lists changes that do not have deprecation warnings.
6060
longer present. Use `first(R)` and `last(R)` to obtain
6161
start/stop. ([#20974])
6262

63+
* The `Diagonal` type definition has changed from `Diagonal{T}` to
64+
`Diagonal{T,V<:AbstractVector{T}}` ([#22718]).
65+
6366
Library improvements
6467
--------------------
6568

@@ -110,6 +113,9 @@ Library improvements
110113

111114
* `Char`s can now be concatenated with `String`s and/or other `Char`s using `*` ([#22532]).
112115

116+
* `Diagonal` is now parameterized on the type of the wrapped vector. This allows
117+
for `Diagonal` matrices with arbitrary `AbstractVector`s ([#22718]).
118+
113119
Compiler/Runtime improvements
114120
-----------------------------
115121

base/linalg/diagonal.jl

+14-13
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,15 @@
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
77
end
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
1615
julia> A = [1 2 3; 4 5 6; 7 8 9]
1716
3×3 Array{Int64,2}:
@@ -20,36 +19,38 @@ julia> A = [1 2 3; 4 5 6; 7 8 9]
2019
7 8 9
2120
2221
julia> Diagonal(A)
23-
3×3 Diagonal{Int64}:
22+
3×3 Diagonal{Int64,Array{Int64,1}}:
2423
1 ⋅ ⋅
2524
⋅ 5 ⋅
2625
⋅ ⋅ 9
2726
```
2827
"""
2928
Diagonal(A::AbstractMatrix) = Diagonal(diag(A))
29+
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]
3938
2-element Array{Int64,1}:
4039
1
4140
2
4241
4342
julia> 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

5152
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))
5354
convert(::Type{AbstractMatrix{T}}, D::Diagonal) where {T} = convert(Diagonal{T}, D)
5455
convert(::Type{Matrix}, D::Diagonal) = diagm(D.diag)
5556
convert(::Type{Array}, D::Diagonal) = convert(Matrix, D)

test/linalg/diagonal.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ srand(1)
2121
@testset "Basic properties" begin
2222
@test eye(Diagonal{elty},n) == Diagonal(ones(elty,n))
2323
@test_throws ArgumentError size(D,0)
24-
@test typeof(convert(Diagonal{Complex64},D)) == Diagonal{Complex64}
25-
@test typeof(convert(AbstractMatrix{Complex64},D)) == Diagonal{Complex64}
24+
@test typeof(convert(Diagonal{Complex64},D)) <: Diagonal{Complex64}
25+
@test typeof(convert(AbstractMatrix{Complex64},D)) <: Diagonal{Complex64}
2626

2727
@test Array(real(D)) == real(DM)
2828
@test Array(abs.(D)) == abs.(DM)
@@ -312,7 +312,7 @@ end
312312
end
313313

314314
# allow construct from range
315-
@test Diagonal(linspace(1,3,3)) == Diagonal([1.,2.,3.])
315+
@test all(Diagonal(linspace(1,3,3)) .== Diagonal([1.0,2.0,3.0]))
316316

317317
# Issue 12803
318318
for t in (Float32, Float64, Int, Complex{Float64}, Rational{Int})

test/show.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ end
547547

548548
# test structured zero matrix printing for select structured types
549549
A = reshape(1:16,4,4)
550-
@test replstr(Diagonal(A)) == "4×4 Diagonal{$Int}:\n 1 ⋅ ⋅ ⋅\n ⋅ 6 ⋅ ⋅\n ⋅ ⋅ 11 ⋅\n ⋅ ⋅ ⋅ 16"
550+
@test replstr(Diagonal(A)) == "4×4 Diagonal{$(Int),Array{$(Int),1}}:\n 1 ⋅ ⋅ ⋅\n ⋅ 6 ⋅ ⋅\n ⋅ ⋅ 11 ⋅\n ⋅ ⋅ ⋅ 16"
551551
@test replstr(Bidiagonal(A,:U)) == "4×4 Bidiagonal{$Int}:\n 1 5 ⋅ ⋅\n ⋅ 6 10 ⋅\n ⋅ ⋅ 11 15\n ⋅ ⋅ ⋅ 16"
552552
@test replstr(Bidiagonal(A,:L)) == "4×4 Bidiagonal{$Int}:\n 1 ⋅ ⋅ ⋅\n 2 6 ⋅ ⋅\n ⋅ 7 11 ⋅\n ⋅ ⋅ 12 16"
553553
@test replstr(SymTridiagonal(A+A')) == "4×4 SymTridiagonal{$Int}:\n 2 7 ⋅ ⋅\n 7 12 17 ⋅\n ⋅ 17 22 27\n ⋅ ⋅ 27 32"

0 commit comments

Comments
 (0)