Skip to content

Commit

Permalink
added conversions between element types (#46)
Browse files Browse the repository at this point in the history
Now that element types of PDMats can vary, and we might want to promote element types for multiple parameters in Distributions, it would be handy to have PDMats take care of conversion between matrices with different element types.
  • Loading branch information
jmxpearson committed Jun 14, 2016
1 parent 6285cde commit b9d9f95
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/PDMats.jl
Expand Up @@ -5,7 +5,7 @@ module PDMats
using Compat

import Base: +, *, \, /, ==
import Base: full, logdet, inv, diag, diagm, scale, scale!, eigmax, eigmin
import Base: full, logdet, inv, diag, diagm, scale, scale!, eigmax, eigmin, convert

export
# Types
Expand Down
2 changes: 2 additions & 0 deletions src/pdiagmat.jl
Expand Up @@ -14,6 +14,8 @@ end

PDiagMat(v::Vector) = PDiagMat(v, ones(v)./v)

### Conversion
convert{T<:Real}(::Type{PDiagMat{T}}, a::PDiagMat) = PDiagMat(convert(Vector{T}, a.diag))

### Basics

Expand Down
3 changes: 3 additions & 0 deletions src/pdmat.jl
Expand Up @@ -17,6 +17,9 @@ PDMat(mat::Matrix) = PDMat(mat,cholfact(mat))
PDMat(mat::Symmetric) = PDMat(full(mat))
PDMat(fac::CholType) = PDMat(full(fac),fac)

### Conversion
convert{T<:Real}(::Type{PDMat{T}}, a::PDMat) = PDMat(convert(Matrix{T}, a.mat))

### Basics

dim(a::PDMat) = a.dim
Expand Down
3 changes: 3 additions & 0 deletions src/pdsparsemat.jl
Expand Up @@ -16,6 +16,9 @@ end
PDSparseMat(mat::SparseMatrixCSC) = PDSparseMat(mat, cholfact(mat))
PDSparseMat(fac::CholTypeSparse) = PDSparseMat(sparse(fac) |> x -> x*x', fac)

### Conversion
convert{T<:Real}(::Type{PDSparseMat{T}}, a::PDSparseMat) = PDSparseMat(convert(SparseMatrixCSC{T}, a.mat))

### Basics

dim(a::PDSparseMat) = a.dim
Expand Down
6 changes: 5 additions & 1 deletion src/scalmat.jl
Expand Up @@ -6,7 +6,11 @@ immutable ScalMat{T<:Real} <: AbstractPDMat{T}
inv_value::T
end

ScalMat(d::Int,v::Real) = ScalMat{typeof(v)}(d, v, one(v) / v)
ScalMat(d::Int,v::Real) = ScalMat{typeof(one(v)/v)}(d, v, one(v) / v)

### Conversion
# can rewrite convert(T, a.value) as T(a.value) when we drop v0.3 support
convert{T<:Real}(::Type{ScalMat{T}}, a::ScalMat) = ScalMat(a.dim, convert(T, a.value))

### Basics

Expand Down
12 changes: 12 additions & 0 deletions test/pdmtypes.jl
Expand Up @@ -12,6 +12,7 @@ for T in [Float64,Float32]
#for Julia v0.3, do not test sparse matrices with Float32 (see issue #14076)
(T == Float64 || VERSION >= v"0.4.2") && (s = speye(T,2,2) ; @test PDSparseMat(s,cholfact(s)).mat == PDSparseMat(s).mat == PDSparseMat(cholfact(s)).mat)


#test the functionality
M = convert(Array{T,2},[4. -2. -1.; -2. 5. -1.; -1. -1. 6.])
V = convert(Array{T,1},[1.5, 2.5, 2.0])
Expand All @@ -23,3 +24,14 @@ for T in [Float64,Float32]
#for Julia v0.3, do not test sparse matrices with Float32 (see issue #14076)
(T == Float64 || VERSION >= v"0.4.2") && call_test_pdmat(PDSparseMat(sparse(M)),M)
end

m = eye(Float32,2)
@test convert(PDMat{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
x = one(Float32); d = 4
@test convert(ScalMat{Float64}, ScalMat(d, x)).value == ScalMat(d, convert(Float64, x)).value
if VERSION >= v"0.4.2"

This comment has been minimized.

Copy link
@tkelman

tkelman Jun 16, 2016

Contributor

what is this relying on?

s = speye(Float32, 2, 2)
@test convert(PDSparseMat{Float64}, PDSparseMat(s)).mat == PDSparseMat(convert(SparseMatrixCSC{Float64}, s)).mat
end

0 comments on commit b9d9f95

Please sign in to comment.