Skip to content
  • Sponsor
  • Notifications You must be signed in to change notification settings
  • Fork 16
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix conversion for structured matrices without a zero #1159

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/bidiag.jl
Original file line number Diff line number Diff line change
@@ -198,7 +198,13 @@ function Matrix{T}(A::Bidiagonal) where T
end
return B
end
Matrix(A::Bidiagonal{T}) where {T} = Matrix{promote_type(T, typeof(zero(T)))}(A)
function Matrix(A::Bidiagonal{T}) where {T}
if haszero(T)
Matrix{promote_type(T, typeof(zero(T)))}(A)
else
convert(Matrix, [x for x in A])
end
end
Array(A::Bidiagonal) = Matrix(A)
promote_rule(::Type{Matrix{T}}, ::Type{<:Bidiagonal{S}}) where {T,S} =
@isdefined(T) && @isdefined(S) ? Matrix{promote_type(T,S)} : Matrix
3 changes: 2 additions & 1 deletion src/dense.jl
Original file line number Diff line number Diff line change
@@ -114,7 +114,8 @@ norm2(x::Union{Array{T},StridedVector{T}}) where {T<:BlasFloat} =
Return whether a type `T` has a unique zero element defined using `zero(T)`.
If a type `M` specializes `zero(M)`, it may also choose to set `haszero(M)` to `true`.
By default, `haszero` is assumed to be `false`, in which case the zero elements
are deduced from values rather than the type.
are deduced from values rather than the type. In general, if a type defines `zero(::Type{MyType})`,
it should also define `haszero(::Type{MyType}) = true`.

!!! note
`haszero` is a conservative check that is used to dispatch to
8 changes: 7 additions & 1 deletion src/diagonal.jl
Original file line number Diff line number Diff line change
@@ -113,7 +113,13 @@ Diagonal{T}(D::Diagonal) where {T} = Diagonal{T}(D.diag)

AbstractMatrix{T}(D::Diagonal) where {T} = Diagonal{T}(D)
AbstractMatrix{T}(D::Diagonal{T}) where {T} = copy(D)
Matrix(D::Diagonal{T}) where {T} = Matrix{promote_type(T, typeof(zero(T)))}(D)
function Matrix(D::Diagonal{T}) where {T}
if haszero(T)
Matrix{promote_type(T, typeof(zero(T)))}(D)
else
convert(Matrix, [i for i in D])
end
end
Matrix(D::Diagonal{Any}) = Matrix{Any}(D)
Array(D::Diagonal{T}) where {T} = Matrix(D)
function Matrix{T}(D::Diagonal) where {T}
16 changes: 14 additions & 2 deletions src/tridiag.jl
Original file line number Diff line number Diff line change
@@ -156,7 +156,13 @@ function Matrix{T}(M::SymTridiagonal) where T
end
return Mf
end
Matrix(M::SymTridiagonal{T}) where {T} = Matrix{promote_type(T, typeof(zero(T)))}(M)
function Matrix(M::SymTridiagonal{T}) where {T}
if haszero(T)
Matrix{promote_type(T, typeof(zero(T)))}(M)
else
convert(Matrix, [x for x in M])
end
end
Array(M::SymTridiagonal) = Matrix(M)

size(A::SymTridiagonal) = (n = length(A.dv); (n, n))
@@ -647,7 +653,13 @@ function Matrix{T}(M::Tridiagonal) where {T}
end
A
end
Matrix(M::Tridiagonal{T}) where {T} = Matrix{promote_type(T, typeof(zero(T)))}(M)
function Matrix(M::Tridiagonal{T}) where {T}
if haszero(T)
Matrix{promote_type(T, typeof(zero(T)))}(M)
else
convert(Matrix, [x for x in M])
end
end
Array(M::Tridiagonal) = Matrix(M)

similar(M::Tridiagonal, ::Type{T}) where {T} = Tridiagonal(similar(M.dl, T), similar(M.d, T), similar(M.du, T))
7 changes: 7 additions & 0 deletions test/bidiag.jl
Original file line number Diff line number Diff line change
@@ -1158,6 +1158,13 @@ end
@test opnorm(B, Inf) == opnorm(Matrix(B), Inf)
end

@testset "Matrix conversion without zero" begin
D = Bidiagonal(fill(ones(2,2), 4), fill(ones(2,2), 3), :U)
M = Matrix(D)
@test M isa Matrix{eltype(D)}
@test M == D
end

@testset "convert to Bidiagonal" begin
M = diagm(0 => [1,2,3], 1=>[4,5])
B = convert(Bidiagonal, M)
7 changes: 7 additions & 0 deletions test/diagonal.jl
Original file line number Diff line number Diff line change
@@ -1474,6 +1474,13 @@ end
@test opnorm(D, Inf) == opnorm(A, Inf)
end

@testset "Matrix conversion without zero" begin
D = Diagonal(fill(ones(2,2), 4))
M = Matrix(D)
@test M isa Matrix{eltype(D)}
@test M == D
end

@testset "issymmetric with NaN" begin
D = Diagonal(fill(NaN,3))
A = Array(D)
2 changes: 2 additions & 0 deletions test/special.jl
Original file line number Diff line number Diff line change
@@ -115,6 +115,8 @@ Random.seed!(1)
struct TypeWithZero end
Base.promote_rule(::Type{TypeWithoutZero}, ::Type{TypeWithZero}) = TypeWithZero
Base.convert(::Type{TypeWithZero}, ::TypeWithoutZero) = TypeWithZero()
LinearAlgebra.haszero(::Type{TypeWithoutZero}) = true
LinearAlgebra.haszero(::Type{TypeWithZero}) = true
Base.zero(x::Union{TypeWithoutZero, TypeWithZero}) = zero(typeof(x))
Base.zero(::Type{<:Union{TypeWithoutZero, TypeWithZero}}) = TypeWithZero()
LinearAlgebra.symmetric(::TypeWithoutZero, ::Symbol) = TypeWithoutZero()
9 changes: 9 additions & 0 deletions test/tridiag.jl
Original file line number Diff line number Diff line change
@@ -1099,6 +1099,15 @@ end
@test opnorm(S, Inf) == opnorm(Matrix(S), Inf)
end

@testset "Matrix conversion without zero" begin
dv, ev = fill(ones(2,2), 2), fill(ones(2,2), 1)
for T in (Tridiagonal(ev, dv, ev), SymTridiagonal(dv, ev))
M = Matrix(T)
@test M isa Matrix
@test M == T
end
end

@testset "convert to Tridiagonal/SymTridiagonal" begin
@testset "Tridiagonal" begin
for M in [diagm(0 => [1,2,3], 1=>[4,5]),