Skip to content

Commit e0f4bf1

Browse files
committed
Generalize Bidiagonal/Tridiagonal constructors
1 parent b064b72 commit e0f4bf1

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
lines changed

src/bidiag.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ julia> Bidiagonal(A, :L) # contains the main diagonal and first subdiagonal of A
109109
⋅ ⋅ 4 4
110110
```
111111
"""
112-
function Bidiagonal(A::AbstractMatrix, uplo::Symbol)
113-
Bidiagonal(diag(A, 0), diag(A, uplo === :U ? 1 : -1), uplo)
112+
function (::Type{Bi})(A::AbstractMatrix, uplo::Symbol) where {Bi<:Bidiagonal}
113+
Bi(diag(A, 0), diag(A, uplo === :U ? 1 : -1), uplo)
114114
end
115115

116116

src/diagonal.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@ julia> Diagonal(A)
9999
⋅ 5
100100
```
101101
"""
102-
Diagonal(A::AbstractMatrix) = Diagonal(diag(A))
103-
Diagonal{T}(A::AbstractMatrix) where T = Diagonal{T}(diag(A))
104-
Diagonal{T,V}(A::AbstractMatrix) where {T,V<:AbstractVector{T}} = Diagonal{T,V}(diag(A))
102+
(::Type{D})(A::AbstractMatrix) where {D<:Diagonal} = D(diag(A))
105103
function convert(::Type{T}, A::AbstractMatrix) where T<:Diagonal
106104
checksquare(A)
107105
isdiag(A) ? T(A) : throw(InexactError(:convert, T, A))

src/tridiag.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ julia> Tridiagonal(A)
590590
⋅ ⋅ 3 4
591591
```
592592
"""
593-
Tridiagonal(A::AbstractMatrix) = Tridiagonal(diag(A,-1), diag(A,0), diag(A,1))
593+
(::Type{Tri})(A::AbstractMatrix) where {Tri<:Tridiagonal} = Tri(diag(A,-1), diag(A,0), diag(A,1))
594594

595595
Tridiagonal(A::Tridiagonal) = A
596596
Tridiagonal{T}(A::Tridiagonal{T}) where {T} = A

test/bidiag.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,12 @@ end
11651165
M = diagm(0 => [1,2,3], -1=>[4,5])
11661166
B = convert(Bidiagonal, M)
11671167
@test B == Bidiagonal(M, :L)
1168+
B = convert(Bidiagonal{Int8}, M)
1169+
@test B == M
1170+
@test B isa Bidiagonal{Int8, Vector{Int8}}
1171+
B = convert(Bidiagonal{Int8, OffsetVector{Int8, Vector{Int8}}}, M)
1172+
@test B == M
1173+
@test B isa Bidiagonal{Int8, OffsetVector{Int8, Vector{Int8}}}
11681174
M = diagm(-1 => [1,2], 1=>[4,5])
11691175
@test_throws InexactError convert(Bidiagonal, M)
11701176
end

test/tridiag.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,12 @@ end
11061106
diagm(-1 => [1,2], 1=>[4,5])]
11071107
B = convert(Tridiagonal, M)
11081108
@test B == Tridiagonal(M)
1109+
B = convert(Tridiagonal{Int8}, M)
1110+
@test B == M
1111+
@test B isa Tridiagonal{Int8}
1112+
B = convert(Tridiagonal{Int8, OffsetVector{Int8, Vector{Int8}}}, M)
1113+
@test B == M
1114+
@test B isa Tridiagonal{Int8, OffsetVector{Int8, Vector{Int8}}}
11091115
end
11101116
@test_throws InexactError convert(Tridiagonal, fill(5, 4, 4))
11111117
end

0 commit comments

Comments
 (0)