Skip to content

Commit

Permalink
Improve convert definitions (#179)
Browse files Browse the repository at this point in the history
* Improve `convert` definitions

* Add tests

* Bump version number
  • Loading branch information
devmotion authored Sep 25, 2023
1 parent 53e64ac commit a9924c6
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "PDMats"
uuid = "90014a1f-27ba-587c-ab20-58faa44d9150"
version = "0.11.18"
version = "0.11.19"

[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Expand Down
4 changes: 4 additions & 0 deletions src/generics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
AbstractPDMat(A::AbstractPDMat) = A
AbstractPDMat(A::AbstractMatrix) = PDMat(A)

## convert
Base.convert(::Type{AbstractMatrix{T}}, a::AbstractPDMat) where {T<:Real} = convert(AbstractPDMat{T}, a)
Base.convert(::Type{AbstractArray{T}}, a::AbstractPDMat) where {T<:Real} = convert(AbstractMatrix{T}, a)

## arithmetics

pdadd!(r::Matrix, a::Matrix, b::AbstractPDMat{T}) where {T<:Real} = pdadd!(r, a, b, one(T))
Expand Down
8 changes: 6 additions & 2 deletions src/pdiagmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ AbstractPDMat(A::Symmetric{<:Real,<:Diagonal{<:Real}}) = PDiagMat(A.data.diag)
AbstractPDMat(A::Hermitian{<:Real,<:Diagonal{<:Real}}) = PDiagMat(A.data.diag)

### Conversion
Base.convert(::Type{PDiagMat{T}}, a::PDiagMat) where {T<:Real} = PDiagMat(convert(AbstractArray{T}, a.diag))
Base.convert(::Type{AbstractArray{T}}, a::PDiagMat) where {T<:Real} = convert(PDiagMat{T}, a)
Base.convert(::Type{PDiagMat{T}}, a::PDiagMat{T}) where {T<:Real} = a
function Base.convert(::Type{PDiagMat{T}}, a::PDiagMat) where {T<:Real}
diag = convert(AbstractVector{T}, a.diag)
return PDiagMat{T,typeof(diag)}(a.dim, diag)
end
Base.convert(::Type{AbstractPDMat{T}}, a::PDiagMat) where {T<:Real} = convert(PDiagMat{T}, a)

### Basics

Expand Down
11 changes: 8 additions & 3 deletions src/pdmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ PDMat(fac::Cholesky) = PDMat(AbstractMatrix(fac), fac)
AbstractPDMat(A::Cholesky) = PDMat(A)

### Conversion
Base.convert(::Type{PDMat{T}}, a::PDMat) where {T<:Real} = PDMat(convert(AbstractArray{T}, a.mat))
Base.convert(::Type{AbstractArray{T}}, a::PDMat) where {T<:Real} = convert(PDMat{T}, a)
Base.convert(::Type{AbstractArray{T}}, a::PDMat{T}) where {T<:Real} = a
Base.convert(::Type{PDMat{T}}, a::PDMat{T}) where {T<:Real} = a
function Base.convert(::Type{PDMat{T}}, a::PDMat) where {T<:Real}
chol = convert(Cholesky{T}, a.chol)
S = typeof(chol.factors)
mat = convert(S, a.mat)
return PDMat{T,S}(size(mat, 1), mat, chol)
end
Base.convert(::Type{AbstractPDMat{T}}, a::PDMat) where {T<:Real} = convert(PDMat{T}, a)

### Basics

Expand Down
10 changes: 9 additions & 1 deletion src/pdsparsemat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ AbstractPDMat(A::SparseMatrixCSC) = PDSparseMat(A)
AbstractPDMat(A::CholTypeSparse) = PDSparseMat(A)

### Conversion
Base.convert(::Type{PDSparseMat{T}}, a::PDSparseMat) where {T<:Real} = PDSparseMat(convert(SparseMatrixCSC{T}, a.mat))
Base.convert(::Type{PDSparseMat{T}}, a::PDSparseMat{T}) where {T<:Real} = a
function Base.convert(::Type{PDSparseMat{T}}, a::PDSparseMat) where {T<:Real}
# CholTypeSparse only supports Float64 and ComplexF64 type parameters!
# So there is no point in recomputing `cholesky(mat)` and we just reuse
# the existing Cholesky factorization
mat = convert(AbstractMatrix{T}, a.mat)
return PDSparseMat{T,typeof(mat)}(a.dim, mat, a.chol)
end
Base.convert(::Type{AbstractPDMat{T}}, a::PDSparseMat) where {T<:Real} = convert(PDSparseMat{T}, a)

### Basics

Expand Down
3 changes: 2 additions & 1 deletion src/scalmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ struct ScalMat{T<:Real} <: AbstractPDMat{T}
end

### Conversion
Base.convert(::Type{ScalMat{T}}, a::ScalMat{T}) where {T<:Real} = a
Base.convert(::Type{ScalMat{T}}, a::ScalMat) where {T<:Real} = ScalMat(a.dim, T(a.value))
Base.convert(::Type{AbstractArray{T}}, a::ScalMat) where {T<:Real} = convert(ScalMat{T}, a)
Base.convert(::Type{AbstractPDMat{T}}, a::ScalMat) where {T<:Real} = convert(ScalMat{T}, a)

### Basics

Expand Down
58 changes: 42 additions & 16 deletions test/pdmtypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,49 @@ using Test
end

@testset "float type conversions" begin
m = Matrix{Float32}(I, 2, 2)
@test convert(PDMat{Float64}, PDMat(m)).mat == PDMat(convert(Array{Float64}, m)).mat
@test convert(AbstractArray{Float64}, PDMat(m)).mat == PDMat(convert(Array{Float64}, m)).mat
m = ones(Float32,2)
@test convert(PDiagMat{Float64}, PDiagMat(m)).diag == PDiagMat(convert(Array{Float64}, m)).diag
@test convert(AbstractArray{Float64}, PDiagMat(m)).diag == PDiagMat(convert(Array{Float64}, m)).diag
x = one(Float32); d = 4
@test convert(ScalMat{Float64}, ScalMat(d, x)).value == ScalMat(d, convert(Float64, x)).value
@test convert(AbstractArray{Float64}, ScalMat(d, x)).value == ScalMat(d, convert(Float64, x)).value
s = SparseMatrixCSC{Float32}(I, 2, 2)
@test convert(PDSparseMat{Float64}, PDSparseMat(s)).mat == PDSparseMat(convert(SparseMatrixCSC{Float64}, s)).mat
end
for T in (Float32, Float64), S in (Float32, Float64)
A = PDMat(Matrix{T}(I, 2, 2))
for R in (AbstractArray{S}, AbstractMatrix{S}, AbstractPDMat{S}, PDMat{S})
B = @inferred(convert(R, A))
@test B isa PDMat{S}
@test B == A
@test (B === A) === (S === T)
@test (B.mat === A.mat) === (S === T)
@test (B.chol === A.chol) === (S === T)
end

A = PDiagMat(ones(T, 2))
for R in (AbstractArray{S}, AbstractMatrix{S}, AbstractPDMat{S}, PDiagMat{S})
B = @inferred(convert(R, A))
@test B isa PDiagMat{S}
@test B == A
@test (B === A) === (S === T)
@test (B.diag === A.diag) === (S === T)
end

@testset "no-op conversion with correct eltype (#101)" begin
X = PDMat((Y->Y'Y)(randn(Float32, 4, 4)))
@test convert(AbstractArray{Float32}, X) === X
@test convert(AbstractArray{Float64}, X) !== X
A = ScalMat(4, T(1))
for R in (AbstractArray{S}, AbstractMatrix{S}, AbstractPDMat{S}, ScalMat{S})
B = @inferred(convert(R, A))
@test B isa ScalMat{S}
@test B == A
@test (B === A) === (S === T)
@test (B.value === A.value) === (S === T)
end

if HAVE_CHOLMOD
A = PDSparseMat(SparseMatrixCSC{T}(I, 2, 2))
for R in (AbstractArray{S}, AbstractMatrix{S}, AbstractPDMat{S}, PDSparseMat{S})
B = @inferred(convert(R, A))
@test B isa PDSparseMat{S}
@test B == A
@test (B === A) === (S === T)
@test (B.mat === A.mat) === (S === T)
# CholMOD only supports Float64 and ComplexF64 type parameters!
# Hence the Cholesky factorization is reused
@test B.chol === A.chol
end
end
end
end

@testset "type stability of whiten! and unwhiten!" begin
Expand Down

2 comments on commit a9924c6

@devmotion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/92155

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.11.19 -m "<description of version>" a9924c675105142a4890c8707283019b3c0e45bf
git push origin v0.11.19

Please sign in to comment.