Skip to content

Commit 63281a0

Browse files
committed
Matrix conversion for structured matrices without a zero for the eltype
1 parent 6e5ea12 commit 63281a0

File tree

6 files changed

+51
-4
lines changed

6 files changed

+51
-4
lines changed

src/bidiag.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,13 @@ function Matrix{T}(A::Bidiagonal) where T
198198
end
199199
return B
200200
end
201-
Matrix(A::Bidiagonal{T}) where {T} = Matrix{promote_type(T, typeof(zero(T)))}(A)
201+
function Matrix(A::Bidiagonal{T}) where {T}
202+
if haszero(T)
203+
Matrix{promote_type(T, typeof(zero(T)))}(A)
204+
else
205+
convert(Matrix, [x for x in A])
206+
end
207+
end
202208
Array(A::Bidiagonal) = Matrix(A)
203209
promote_rule(::Type{Matrix{T}}, ::Type{<:Bidiagonal{S}}) where {T,S} =
204210
@isdefined(T) && @isdefined(S) ? Matrix{promote_type(T,S)} : Matrix

src/diagonal.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,13 @@ Diagonal{T}(D::Diagonal) where {T} = Diagonal{T}(D.diag)
113113

114114
AbstractMatrix{T}(D::Diagonal) where {T} = Diagonal{T}(D)
115115
AbstractMatrix{T}(D::Diagonal{T}) where {T} = copy(D)
116-
Matrix(D::Diagonal{T}) where {T} = Matrix{promote_type(T, typeof(zero(T)))}(D)
116+
function Matrix(D::Diagonal{T}) where {T}
117+
if haszero(T)
118+
Matrix{promote_type(T, typeof(zero(T)))}(D)
119+
else
120+
convert(Matrix, [i for i in D])
121+
end
122+
end
117123
Matrix(D::Diagonal{Any}) = Matrix{Any}(D)
118124
Array(D::Diagonal{T}) where {T} = Matrix(D)
119125
function Matrix{T}(D::Diagonal) where {T}

src/tridiag.jl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,13 @@ function Matrix{T}(M::SymTridiagonal) where T
148148
end
149149
return Mf
150150
end
151-
Matrix(M::SymTridiagonal{T}) where {T} = Matrix{promote_type(T, typeof(zero(T)))}(M)
151+
function Matrix(M::SymTridiagonal{T}) where {T}
152+
if haszero(T)
153+
Matrix{promote_type(T, typeof(zero(T)))}(M)
154+
else
155+
convert(Matrix, [x for x in M])
156+
end
157+
end
152158
Array(M::SymTridiagonal) = Matrix(M)
153159

154160
size(A::SymTridiagonal) = (n = length(A.dv); (n, n))
@@ -620,7 +626,13 @@ function Matrix{T}(M::Tridiagonal) where {T}
620626
end
621627
A
622628
end
623-
Matrix(M::Tridiagonal{T}) where {T} = Matrix{promote_type(T, typeof(zero(T)))}(M)
629+
function Matrix(M::Tridiagonal{T}) where {T}
630+
if haszero(T)
631+
Matrix{promote_type(T, typeof(zero(T)))}(M)
632+
else
633+
convert(Matrix, [x for x in M])
634+
end
635+
end
624636
Array(M::Tridiagonal) = Matrix(M)
625637

626638
similar(M::Tridiagonal, ::Type{T}) where {T} = Tridiagonal(similar(M.dl, T), similar(M.d, T), similar(M.du, T))

test/bidiag.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,4 +1164,11 @@ end
11641164
@test opnorm(B, Inf) == opnorm(Matrix(B), Inf)
11651165
end
11661166

1167+
@testset "Matrix conversion without zero" begin
1168+
D = Bidiagonal(fill(ones(2,2), 4), fill(ones(2,2), 3), :U)
1169+
M = Matrix(D)
1170+
@test M isa Matrix{eltype(D)}
1171+
@test M == D
1172+
end
1173+
11671174
end # module TestBidiagonal

test/diagonal.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,4 +1473,11 @@ end
14731473
@test opnorm(D, Inf) == opnorm(A, Inf)
14741474
end
14751475

1476+
@testset "Matrix conversion without zero" begin
1477+
D = Diagonal(fill(ones(2,2), 4))
1478+
M = Matrix(D)
1479+
@test M isa Matrix{eltype(D)}
1480+
@test M == D
1481+
end
1482+
14761483
end # module TestDiagonal

test/tridiag.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,4 +1099,13 @@ end
10991099
@test opnorm(S, Inf) == opnorm(Matrix(S), Inf)
11001100
end
11011101

1102+
@testset "Matrix conversion without zero" begin
1103+
dv, ev = fill(ones(2,2), 2), fill(ones(2,2), 1)
1104+
for T in (Tridiagonal(ev, dv, ev), SymTridiagonal(dv, ev))
1105+
M = Matrix(T)
1106+
@test M isa Matrix
1107+
@test M == T
1108+
end
1109+
end
1110+
11021111
end # module TestTridiagonal

0 commit comments

Comments
 (0)