Skip to content

Commit cb82e11

Browse files
authored
parametrize SymTridiagonal on the wrapped vector type (#23035)
* parametrize SymTridiagonal on the wrapped vector type
1 parent 472353c commit cb82e11

File tree

7 files changed

+94
-52
lines changed

7 files changed

+94
-52
lines changed

NEWS.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ This section lists changes that do not have deprecation warnings.
8686
longer present. Use `first(R)` and `last(R)` to obtain
8787
start/stop. ([#20974])
8888

89-
* The `Diagonal` and `Bidiagonal` type definitions have changed from `Diagonal{T}` and
90-
`Bidiagonal{T}` to `Diagonal{T,V<:AbstractVector{T}}` and
91-
`Bidiagonal{T,V<:AbstractVector{T}}` respectively ([#22718], [#22925]).
89+
* The `Diagonal`, `Bidiagonal` and `SymTridiagonal` type definitions have changed from
90+
`Diagonal{T}`, `Bidiagonal{T}` and `SymTridiagonal{T}` to `Diagonal{T,V<:AbstractVector{T}}`,
91+
`Bidiagonal{T,V<:AbstractVector{T}}` and `SymTridiagonal{T,V<:AbstractVector{T}}`
92+
respectively ([#22718], [#22925], [#23035]).
9293

9394
* Spaces are no longer allowed between `@` and the name of a macro in a macro call ([#22868]).
9495

@@ -156,9 +157,9 @@ Library improvements
156157

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

159-
* `Diagonal` and `Bidiagonal` are now parameterized on the type of the wrapped vectors,
160-
allowing `Diagonal` and `Bidiagonal` matrices with arbitrary
161-
`AbstractVector`s ([#22718], [#22925]).
160+
* `Diagonal`, `Bidiagonal` and `SymTridiagonal` are now parameterized on the type of the
161+
wrapped vectors, allowing `Diagonal`, `Bidiagonal` and `SymTridiagonal` matrices with
162+
arbitrary `AbstractVector`s ([#22718], [#22925], [#23035]).
162163

163164
* Mutating versions of `randperm` and `randcycle` have been added:
164165
`randperm!` and `randcycle!` ([#22723]).
@@ -221,8 +222,8 @@ Deprecated or removed
221222
* `Bidiagonal` constructors now use a `Symbol` (`:U` or `:L`) for the upper/lower
222223
argument, instead of a `Bool` or a `Char` ([#22703]).
223224

224-
* `Bidiagonal` constructors that automatically converted the input vectors to
225-
the same type are deprecated in favor of explicit conversion ([#22925]).
225+
* `Bidiagonal` and `SymTridiagonal` constructors that automatically converted the input
226+
vectors to the same type are deprecated in favor of explicit conversion ([#22925], [#23035]).
226227

227228
* Calling `nfields` on a type to find out how many fields its instances have is deprecated.
228229
Use `fieldcount` instead. Use `nfields` only to get the number of fields in a specific object ([#22350]).

base/deprecated.jl

+9
Original file line numberDiff line numberDiff line change
@@ -1604,6 +1604,15 @@ function Bidiagonal(dv::AbstractVector{T}, ev::AbstractVector{S}, uplo::Symbol)
16041604
Bidiagonal(convert(Vector{R}, dv), convert(Vector{R}, ev), uplo)
16051605
end
16061606

1607+
# PR #23035
1608+
# also uncomment constructor tests in test/linalg/tridiag.jl
1609+
function SymTridiagonal(dv::AbstractVector{T}, ev::AbstractVector{S}) where {T,S}
1610+
depwarn(string("SymTridiagonal(dv::AbstractVector{T}, ev::AbstractVector{S}) ",
1611+
"where {T,S} is deprecated; convert both vectors to the same type instead."), :SymTridiagonal)
1612+
R = promote_type(T, S)
1613+
SymTridiagonal(convert(Vector{R}, dv), convert(Vector{R}, ev))
1614+
end
1615+
16071616
# END 0.7 deprecations
16081617

16091618
# BEGIN 1.0 deprecations

base/linalg/ldlt.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ convert(::Type{Factorization{T}}, F::LDLt{S,U}) where {T,S,U} = convert(LDLt{T,U
2121
2222
Same as [`ldltfact`](@ref), but saves space by overwriting the input `A`, instead of creating a copy.
2323
"""
24-
function ldltfact!(S::SymTridiagonal{T}) where T<:Real
24+
function ldltfact!(S::SymTridiagonal{T,V}) where {T<:Real,V}
2525
n = size(S,1)
2626
d = S.dv
2727
e = S.ev
2828
@inbounds @simd for i = 1:n-1
2929
e[i] /= d[i]
3030
d[i+1] -= abs2(e[i])*d[i]
3131
end
32-
return LDLt{T,SymTridiagonal{T}}(S)
32+
return LDLt{T,SymTridiagonal{T,V}}(S)
3333
end
3434

3535
"""
@@ -46,7 +46,7 @@ end
4646

4747
factorize(S::SymTridiagonal) = ldltfact(S)
4848

49-
function A_ldiv_B!(S::LDLt{T,SymTridiagonal{T}}, B::AbstractVecOrMat{T}) where T
49+
function A_ldiv_B!(S::LDLt{T,M}, B::AbstractVecOrMat{T}) where {T,M<:SymTridiagonal{T}}
5050
n, nrhs = size(B, 1), size(B, 2)
5151
if size(S,1) != n
5252
throw(DimensionMismatch("Matrix has dimensions $(size(S)) but right hand side has first dimension $n"))

base/linalg/tridiag.jl

+41-25
Original file line numberDiff line numberDiff line change
@@ -3,68 +3,84 @@
33
#### Specialized matrix types ####
44

55
## (complex) symmetric tridiagonal matrices
6-
struct SymTridiagonal{T} <: AbstractMatrix{T}
7-
dv::Vector{T} # diagonal
8-
ev::Vector{T} # subdiagonal
9-
function SymTridiagonal{T}(dv::Vector{T}, ev::Vector{T}) where T
6+
struct SymTridiagonal{T,V<:AbstractVector{T}} <: AbstractMatrix{T}
7+
dv::V # diagonal
8+
ev::V # subdiagonal
9+
function SymTridiagonal{T}(dv::V, ev::V) where {T,V<:AbstractVector{T}}
1010
if !(length(dv) - 1 <= length(ev) <= length(dv))
1111
throw(DimensionMismatch("subdiagonal has wrong length. Has length $(length(ev)), but should be either $(length(dv) - 1) or $(length(dv))."))
1212
end
13-
new(dv,ev)
13+
new{T,V}(dv,ev)
1414
end
1515
end
1616

1717
"""
18-
SymTridiagonal(dv, ev)
18+
SymTridiagonal(dv::V, ev::V) where V <: AbstractVector
1919
20-
Construct a symmetric tridiagonal matrix from the diagonal and first sub/super-diagonal,
21-
respectively. The result is of type `SymTridiagonal` and provides efficient specialized
22-
eigensolvers, but may be converted into a regular matrix with
23-
[`convert(Array, _)`](@ref) (or `Array(_)` for short).
20+
Construct a symmetric tridiagonal matrix from the diagonal (`dv`) and first
21+
sub/super-diagonal (`ev`), respectively. The result is of type `SymTridiagonal`
22+
and provides efficient specialized eigensolvers, but may be converted into a
23+
regular matrix with [`convert(Array, _)`](@ref) (or `Array(_)` for short).
2424
2525
# Examples
2626
```jldoctest
27-
julia> dv = [1; 2; 3; 4]
27+
julia> dv = [1, 2, 3, 4]
2828
4-element Array{Int64,1}:
2929
1
3030
2
3131
3
3232
4
3333
34-
julia> ev = [7; 8; 9]
34+
julia> ev = [7, 8, 9]
3535
3-element Array{Int64,1}:
3636
7
3737
8
3838
9
3939
4040
julia> SymTridiagonal(dv, ev)
41-
4×4 SymTridiagonal{Int64}:
41+
4×4 SymTridiagonal{Int64,Array{Int64,1}}:
4242
1 7 ⋅ ⋅
4343
7 2 8 ⋅
4444
⋅ 8 3 9
4545
⋅ ⋅ 9 4
4646
```
4747
"""
48-
SymTridiagonal(dv::Vector{T}, ev::Vector{T}) where {T} = SymTridiagonal{T}(dv, ev)
48+
SymTridiagonal(dv::V, ev::V) where {T,V<:AbstractVector{T}} = SymTridiagonal{T}(dv, ev)
4949

50-
function SymTridiagonal(dv::AbstractVector{Td}, ev::AbstractVector{Te}) where {Td,Te}
51-
T = promote_type(Td,Te)
52-
SymTridiagonal(convert(Vector{T}, dv), convert(Vector{T}, ev))
53-
end
50+
"""
51+
SymTridiagonal(A::AbstractMatrix)
52+
53+
Construct a symmetric tridiagonal matrix from the diagonal and
54+
first sub/super-diagonal, of the symmetric matrix `A`.
5455
56+
# Examples
57+
```jldoctest
58+
julia> A = [1 2 3; 2 4 5; 3 5 6]
59+
3×3 Array{Int64,2}:
60+
1 2 3
61+
2 4 5
62+
3 5 6
63+
64+
julia> SymTridiagonal(A)
65+
3×3 SymTridiagonal{Int64,Array{Int64,1}}:
66+
1 2 ⋅
67+
2 4 5
68+
⋅ 5 6
69+
```
70+
"""
5571
function SymTridiagonal(A::AbstractMatrix)
5672
if diag(A,1) == diag(A,-1)
57-
SymTridiagonal(diag(A), diag(A,1))
73+
SymTridiagonal(diag(A,0), diag(A,1))
5874
else
5975
throw(ArgumentError("matrix is not symmetric; cannot convert to SymTridiagonal"))
6076
end
6177
end
6278

6379
convert(::Type{SymTridiagonal{T}}, S::SymTridiagonal) where {T} =
64-
SymTridiagonal(convert(Vector{T}, S.dv), convert(Vector{T}, S.ev))
80+
SymTridiagonal(convert(AbstractVector{T}, S.dv), convert(AbstractVector{T}, S.ev))
6581
convert(::Type{AbstractMatrix{T}}, S::SymTridiagonal) where {T} =
66-
SymTridiagonal(convert(Vector{T}, S.dv), convert(Vector{T}, S.ev))
67-
function convert(::Type{Matrix{T}}, M::SymTridiagonal{T}) where T
82+
SymTridiagonal(convert(AbstractVector{T}, S.dv), convert(AbstractVector{T}, S.ev))
83+
function convert(::Type{Matrix{T}}, M::SymTridiagonal) where T
6884
n = size(M, 1)
6985
Mf = zeros(T, n, n)
7086
@inbounds begin
@@ -311,7 +327,7 @@ end
311327
# R. Usmani, "Inversion of a tridiagonal Jacobi matrix",
312328
# Linear Algebra and its Applications 212-213 (1994), pp.413-414
313329
# doi:10.1016/0024-3795(94)90414-6
314-
function inv_usmani(a::Vector{T}, b::Vector{T}, c::Vector{T}) where T
330+
function inv_usmani(a::V, b::V, c::V) where {T,V<:AbstractVector{T}}
315331
n = length(b)
316332
θ = ZeroOffsetVector(zeros(T, n+1)) #principal minors of A
317333
θ[0] = 1
@@ -341,7 +357,7 @@ end
341357

342358
#Implements the determinant using principal minors
343359
#Inputs and reference are as above for inv_usmani()
344-
function det_usmani(a::Vector{T}, b::Vector{T}, c::Vector{T}) where T
360+
function det_usmani(a::V, b::V, c::V) where {T,V<:AbstractVector{T}}
345361
n = length(b)
346362
θa = one(T)
347363
if n == 0
@@ -635,7 +651,7 @@ convert(::Type{AbstractMatrix{T}},M::Tridiagonal) where {T} = convert(Tridiagona
635651
convert(::Type{Tridiagonal{T}}, M::SymTridiagonal{T}) where {T} = Tridiagonal(M)
636652
function convert(::Type{SymTridiagonal{T}}, M::Tridiagonal) where T
637653
if M.dl == M.du
638-
return SymTridiagonal(convert(Vector{T},M.d), convert(Vector{T},M.dl))
654+
return SymTridiagonal{T}(convert(AbstractVector{T},M.d), convert(AbstractVector{T},M.dl))
639655
else
640656
throw(ArgumentError("Tridiagonal is not symmetric, cannot convert to SymTridiagonal"))
641657
end

test/linalg/lapack.jl

+15-13
Original file line numberDiff line numberDiff line change
@@ -448,40 +448,42 @@ end
448448

449449
@testset "ptsv" begin
450450
@testset for elty in (Float32, Float64, Complex64, Complex128)
451-
dv = real(ones(elty,10))
451+
dv = ones(elty,10)
452452
ev = zeros(elty,9)
453+
rdv = real(dv)
453454
A = SymTridiagonal(dv,ev)
454455
if elty <: Complex
455456
A = Tridiagonal(conj(ev),dv,ev)
456457
end
457458
B = rand(elty,10,10)
458459
C = copy(B)
459-
@test A\B LAPACK.ptsv!(dv,ev,C)
460-
@test_throws DimensionMismatch LAPACK.ptsv!(dv,ones(elty,10),C)
461-
@test_throws DimensionMismatch LAPACK.ptsv!(dv,ev,ones(elty,11,11))
460+
@test A\B LAPACK.ptsv!(rdv,ev,C)
461+
@test_throws DimensionMismatch LAPACK.ptsv!(rdv,ones(elty,10),C)
462+
@test_throws DimensionMismatch LAPACK.ptsv!(rdv,ev,ones(elty,11,11))
462463
end
463464
end
464465

465466
@testset "pttrf and pttrs" begin
466467
@testset for elty in (Float32, Float64, Complex64, Complex128)
467-
dv = real(ones(elty,10))
468+
dv = ones(elty,10)
468469
ev = zeros(elty,9)
470+
rdv = real(dv)
469471
A = SymTridiagonal(dv,ev)
470472
if elty <: Complex
471473
A = Tridiagonal(conj(ev),dv,ev)
472474
end
473-
dv,ev = LAPACK.pttrf!(dv,ev)
474-
@test_throws DimensionMismatch LAPACK.pttrf!(dv,ones(elty,10))
475+
rdv,ev = LAPACK.pttrf!(rdv,ev)
476+
@test_throws DimensionMismatch LAPACK.pttrf!(rdv,dv)
475477
B = rand(elty,10,10)
476478
C = copy(B)
477479
if elty <: Complex
478-
@test A\B LAPACK.pttrs!('U',dv,ev,C)
479-
@test_throws DimensionMismatch LAPACK.pttrs!('U',dv,ones(elty,10),C)
480-
@test_throws DimensionMismatch LAPACK.pttrs!('U',dv,ev,rand(elty,11,11))
480+
@test A\B LAPACK.pttrs!('U',rdv,ev,C)
481+
@test_throws DimensionMismatch LAPACK.pttrs!('U',rdv,ones(elty,10),C)
482+
@test_throws DimensionMismatch LAPACK.pttrs!('U',rdv,ev,rand(elty,11,11))
481483
else
482-
@test A\B LAPACK.pttrs!(dv,ev,C)
483-
@test_throws DimensionMismatch LAPACK.pttrs!(dv,ones(elty,10),C)
484-
@test_throws DimensionMismatch LAPACK.pttrs!(dv,ev,rand(elty,11,11))
484+
@test A\B LAPACK.pttrs!(rdv,ev,C)
485+
@test_throws DimensionMismatch LAPACK.pttrs!(rdv,ones(elty,10),C)
486+
@test_throws DimensionMismatch LAPACK.pttrs!(rdv,ev,rand(elty,11,11))
485487
end
486488
end
487489
end

test/linalg/tridiag.jl

+16-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ B = randn(n,2)
3434
F[i,i+1] = du[i]
3535
F[i+1,i] = dl[i]
3636
end
37+
38+
@testset "constructor" begin
39+
for (x, y) in ((d, dl), (GenericArray(d), GenericArray(dl)))
40+
ST = (SymTridiagonal(x, y))::SymTridiagonal{elty, typeof(x)}
41+
@test ST == Matrix(ST)
42+
@test ST.dv === x
43+
@test ST.ev === y
44+
end
45+
# enable when deprecations for 0.7 are dropped
46+
# @test_throws MethodError SymTridiagonal(dv, GenericArray(ev))
47+
# @test_throws MethodError SymTridiagonal(GenericArray(dv), ev)
48+
end
49+
3750
@testset "size and Array" begin
3851
@test_throws ArgumentError size(Ts,0)
3952
@test size(Ts,3) == 1
@@ -119,7 +132,8 @@ B = randn(n,2)
119132
@test_throws DimensionMismatch Tldlt\rand(elty,n+1)
120133
@test size(Tldlt) == size(Ts)
121134
if elty <: AbstractFloat
122-
@test typeof(convert(Base.LinAlg.LDLt{Float32},Tldlt)) == Base.LinAlg.LDLt{Float32,SymTridiagonal{elty}}
135+
@test typeof(convert(Base.LinAlg.LDLt{Float32},Tldlt)) ==
136+
Base.LinAlg.LDLt{Float32,SymTridiagonal{elty,Vector{elty}}}
123137
end
124138
for vv in (vs, view(vs, 1:n))
125139
invFsv = Fs\vv
@@ -216,7 +230,7 @@ let n = 12 #Size of matrix problem to test
216230
b += im*convert(Vector{elty}, randn(n-1))
217231
end
218232

219-
@test_throws DimensionMismatch SymTridiagonal(a, ones(n+1))
233+
@test_throws DimensionMismatch SymTridiagonal(a, ones(elty, n+1))
220234
@test_throws ArgumentError SymTridiagonal(rand(n,n))
221235

222236
A = SymTridiagonal(a, b)

test/show.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ A = reshape(1:16,4,4)
597597
@test replstr(Diagonal(A)) == "4×4 Diagonal{$(Int),Array{$(Int),1}}:\n 1 ⋅ ⋅ ⋅\n ⋅ 6 ⋅ ⋅\n ⋅ ⋅ 11 ⋅\n ⋅ ⋅ ⋅ 16"
598598
@test replstr(Bidiagonal(A,:U)) == "4×4 Bidiagonal{$(Int),Array{$(Int),1}}:\n 1 5 ⋅ ⋅\n ⋅ 6 10 ⋅\n ⋅ ⋅ 11 15\n ⋅ ⋅ ⋅ 16"
599599
@test replstr(Bidiagonal(A,:L)) == "4×4 Bidiagonal{$(Int),Array{$(Int),1}}:\n 1 ⋅ ⋅ ⋅\n 2 6 ⋅ ⋅\n ⋅ 7 11 ⋅\n ⋅ ⋅ 12 16"
600-
@test replstr(SymTridiagonal(A+A')) == "4×4 SymTridiagonal{$Int}:\n 2 7 ⋅ ⋅\n 7 12 17 ⋅\n ⋅ 17 22 27\n ⋅ ⋅ 27 32"
600+
@test replstr(SymTridiagonal(A+A')) == "4×4 SymTridiagonal{$(Int),Array{$(Int),1}}:\n 2 7 ⋅ ⋅\n 7 12 17 ⋅\n ⋅ 17 22 27\n ⋅ ⋅ 27 32"
601601
@test replstr(Tridiagonal(diag(A,-1),diag(A),diag(A,+1))) == "4×4 Tridiagonal{$Int}:\n 1 5 ⋅ ⋅\n 2 6 10 ⋅\n ⋅ 7 11 15\n ⋅ ⋅ 12 16"
602602
@test replstr(UpperTriangular(copy(A))) == "4×4 UpperTriangular{$Int,Array{$Int,2}}:\n 1 5 9 13\n ⋅ 6 10 14\n ⋅ ⋅ 11 15\n ⋅ ⋅ ⋅ 16"
603603
@test replstr(LowerTriangular(copy(A))) == "4×4 LowerTriangular{$Int,Array{$Int,2}}:\n 1 ⋅ ⋅ ⋅\n 2 6 ⋅ ⋅\n 3 7 11 ⋅\n 4 8 12 16"

0 commit comments

Comments
 (0)