Skip to content

Commit 48732d8

Browse files
committed
parametrize SymTridiagonal on the wrapped vector type
1 parent 42b521f commit 48732d8

File tree

5 files changed

+73
-34
lines changed

5 files changed

+73
-34
lines changed

NEWS.md

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

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

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

@@ -153,9 +154,9 @@ Library improvements
153154

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

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

160161
* Mutating versions of `randperm` and `randcycle` have been added:
161162
`randperm!` and `randcycle!` ([#22723]).
@@ -218,8 +219,8 @@ Deprecated or removed
218219
* `Bidiagonal` constructors now use a `Symbol` (`:U` or `:L`) for the upper/lower
219220
argument, instead of a `Bool` or a `Char` ([#22703]).
220221

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

224225
* Calling `nfields` on a type to find out how many fields its instances have is deprecated.
225226
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/tridiag.jl

+40-24
Original file line numberDiff line numberDiff line change
@@ -3,55 +3,71 @@
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)
5773
SymTridiagonal(diag(A), diag(A,1))
@@ -61,10 +77,10 @@ function SymTridiagonal(A::AbstractMatrix)
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/tridiag.jl

+14-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ for elty in (Float32, Float64, Complex64, Complex128, Int)
2828
v = convert(Vector{elty}, v)
2929
B = convert(Matrix{elty}, B)
3030
end
31+
32+
@testset "constructor" begin
33+
for (x, y) in ((d, dl), (GenericArray(d), GenericArray(dl)))
34+
ST = (SymTridiagonal(x, y))::SymTridiagonal{elty, typeof(x)}
35+
@test ST == Matrix(ST)
36+
@test ST.dv === x
37+
@test ST.ev === y
38+
end
39+
# enable when deprecations for 0.7 are dropped
40+
# @test_throws MethodError SymTridiagonal(dv, GenericArray(ev))
41+
# @test_throws MethodError SymTridiagonal(GenericArray(dv), ev)
42+
end
43+
3144
ε = eps(abs2(float(one(elty))))
3245
T = Tridiagonal(dl, d, du)
3346
Ts = SymTridiagonal(d, dl)
@@ -251,7 +264,7 @@ let n = 12 #Size of matrix problem to test
251264
b += im*convert(Vector{elty}, randn(n-1))
252265
end
253266

254-
@test_throws DimensionMismatch SymTridiagonal(a, ones(n+1))
267+
@test_throws DimensionMismatch SymTridiagonal(a, ones(elty, n+1))
255268
@test_throws ArgumentError SymTridiagonal(rand(n,n))
256269

257270
A = SymTridiagonal(a, b)

test/show.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ A = reshape(1:16,4,4)
558558
@test replstr(Diagonal(A)) == "4×4 Diagonal{$(Int),Array{$(Int),1}}:\n 1 ⋅ ⋅ ⋅\n ⋅ 6 ⋅ ⋅\n ⋅ ⋅ 11 ⋅\n ⋅ ⋅ ⋅ 16"
559559
@test replstr(Bidiagonal(A,:U)) == "4×4 Bidiagonal{$(Int),Array{$(Int),1}}:\n 1 5 ⋅ ⋅\n ⋅ 6 10 ⋅\n ⋅ ⋅ 11 15\n ⋅ ⋅ ⋅ 16"
560560
@test replstr(Bidiagonal(A,:L)) == "4×4 Bidiagonal{$(Int),Array{$(Int),1}}:\n 1 ⋅ ⋅ ⋅\n 2 6 ⋅ ⋅\n ⋅ 7 11 ⋅\n ⋅ ⋅ 12 16"
561-
@test replstr(SymTridiagonal(A+A')) == "4×4 SymTridiagonal{$Int}:\n 2 7 ⋅ ⋅\n 7 12 17 ⋅\n ⋅ 17 22 27\n ⋅ ⋅ 27 32"
561+
@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"
562562
@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"
563563
@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"
564564
@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)