From 38534107c43308e298cbe79781be91acb54b2c86 Mon Sep 17 00:00:00 2001 From: "David A. van Leeuwen" Date: Thu, 5 Mar 2015 18:37:59 +0100 Subject: [PATCH] campatibility with v0.4-dev Triangular, remove iceil() --- REQUIRE | 1 + src/GaussianMixtures.jl | 2 ++ src/bayes.jl | 4 ++-- src/compat.jl | 12 ++++++++---- src/gmms.jl | 4 ++-- src/gmmtypes.jl | 2 +- src/nomodule.jl | 4 +++- src/stats.jl | 2 +- src/train.jl | 4 ++-- 9 files changed, 22 insertions(+), 13 deletions(-) diff --git a/REQUIRE b/REQUIRE index d272dae..c1b7bd7 100644 --- a/REQUIRE +++ b/REQUIRE @@ -3,3 +3,4 @@ Clustering Distributions PDMats HDF5 +Compat diff --git a/src/GaussianMixtures.jl b/src/GaussianMixtures.jl index 7907ea3..38926ca 100644 --- a/src/GaussianMixtures.jl +++ b/src/GaussianMixtures.jl @@ -11,7 +11,9 @@ using Distributions using PDMats using Clustering using HDF5, JLD +using Compat +include("compat.jl") include("gmmtypes.jl") include("bayestypes.jl") diff --git a/src/bayes.jl b/src/bayes.jl index 6a93a27..533f712 100644 --- a/src/bayes.jl +++ b/src/bayes.jl @@ -36,8 +36,8 @@ Base.copy(vg::VGMM) = VGMM(vg.n, vg.d, copy(vg.π), copy(vg.α), copy(vg.β), copy(vg.m), copy(vg.ν), copy(vg.W), copy(vg.hist)) ## W[k] really is chol(W_k, :U), use precision() to get it back -precision(c::Triangular) = c' * c -mylogdet(c::Triangular) = 2sum(log(diag(c))) +precision(c::AbstractTriangular) = c' * c +mylogdet(c::AbstractTriangular) = 2sum(log(diag(c))) mylogdet(m::Matrix) = logdet(m) ## sharpen VGMM to a GMM diff --git a/src/compat.jl b/src/compat.jl index adcf11a..f0f4da5 100644 --- a/src/compat.jl +++ b/src/compat.jl @@ -1,10 +1,14 @@ ## bugs in v0.3 and compatibility if VERSION < v"0.4.0-dev" - Base.copy{T,A,uplo}(t::Triangular{T,A,uplo}) = Triangular(copy(t.data), uplo) +# Base.copy{T,A,uplo}(t::Triangular{T,A,uplo}) = Triangular(copy(t.data), uplo) + typealias AbstractTriangular Triangular + typealias UpperTriangular{T,M} Triangular{T,M,:U,false} +else + import Base.LinAlg.AbstractTriangular end ## this we need for xμTΛxμ! -Base.A_mul_Bc!(A::StridedMatrix{Float64}, B::Triangular{Float32}) = A_mul_Bc!(A, convert(AbstractMatrix{Float64}, B)) -Base.A_mul_Bc!(A::Matrix{Float32}, B::Triangular{Float64}) = A_mul_Bc!(A, convert(AbstractMatrix{Float32}, B)) +#Base.A_mul_Bc!(A::StridedMatrix{Float64}, B::AbstractTriangular{Float32}) = A_mul_Bc!(A, convert(AbstractMatrix{Float64}, B)) +#Base.A_mul_Bc!(A::Matrix{Float32}, B::AbstractTriangular{Float64}) = A_mul_Bc!(A, convert(AbstractMatrix{Float32}, B)) ## this for diagstats -Base.BLAS.gemm!(a::Char, b::Char, alpha::Float64, A::Matrix{Float32}, B::Matrix{Float64}, beta::Float64, C::Matrix{Float64}) = Base.BLAS.gemm!(a, b, alpha, float64(A), B, beta, C) +#Base.BLAS.gemm!(a::Char, b::Char, alpha::Float64, A::Matrix{Float32}, B::Matrix{Float64}, beta::Float64, C::Matrix{Float64}) = Base.BLAS.gemm!(a, b, alpha, float64(A), B, beta, C) diff --git a/src/gmms.jl b/src/gmms.jl index 95d7a34..4ab94b9 100644 --- a/src/gmms.jl +++ b/src/gmms.jl @@ -19,7 +19,7 @@ end Base.eltype{T}(gmm::GMM{T}) = T ## switch between full covariance and inverse cholesky decomposition representations. -covar{T}(ci::Triangular{T}) = (c = inv(ci); c * c') +covar{T}(ci::AbstractTriangular{T}) = (c = inv(ci); c * c') cholinv{T}(Σ::Matrix{T}) = chol(inv(cholfact(0.5(Σ+Σ'))), :U) kind{T}(g::GMM{T,DiagCov{T}}) = :diag @@ -64,7 +64,7 @@ function Base.full{T}(gmm::GMM{T}) if kind(gmm) == :full return gmm end - Σ = convert(FullCov{T}, [Triangular(diagm(vec(1./√gmm.Σ[i,:])), :U, false) for i=1:gmm.n]) + Σ = convert(FullCov{T}, [UpperTriangular(diagm(vec(1./√gmm.Σ[i,:]))) for i=1:gmm.n]) new = GMM(copy(gmm.w), copy(gmm.μ), Σ, copy(gmm.hist), gmm.nx) addhist!(new, "Converted to full covariance") end diff --git a/src/gmmtypes.jl b/src/gmmtypes.jl index f99cfc2..713611f 100644 --- a/src/gmmtypes.jl +++ b/src/gmmtypes.jl @@ -30,7 +30,7 @@ abstract GaussianMixture{T,CT} ## Full covariance is represented by inverse cholesky of the covariance matrix, ## i.e., Σ^-1 = ci * ci' typealias DiagCov{T} Matrix{T} -typealias FullCov{T} Vector{Triangular{T,Matrix{T},:U,false}} +typealias FullCov{T} Vector{UpperTriangular{T,Matrix{T}}} ## GMMs can be of type FLoat32 or Float64, and diagonal or full type GMM{T<:FloatingPoint, CT<:Union(Matrix,Vector)} <: GaussianMixture{T,CT} diff --git a/src/nomodule.jl b/src/nomodule.jl index 3e68579..a8efde4 100644 --- a/src/nomodule.jl +++ b/src/nomodule.jl @@ -5,7 +5,9 @@ using Distributions using PDMats using Clustering using HDF5, JLD +using Compat +include("compat.jl") require("gmmtypes.jl") require("bayestypes.jl") @@ -17,5 +19,5 @@ include("rand.jl") include("data.jl") include("recognizer.jl") -include("bayes.jl") ## experimental, not in main module +include("bayes.jl") diff --git a/src/stats.jl b/src/stats.jl index 96bbe85..262c051 100644 --- a/src/stats.jl +++ b/src/stats.jl @@ -126,7 +126,7 @@ function stats{T<:FloatingPoint}(gmm::GMM, x::Matrix{T}; order::Int=2, parallel= elseif kind(gmm) == :full bytes = sizeof(T) * ((d + d^2 + 5nₓ + nₓ*d)ng + (2d + 2)nₓ) end - blocks = iceil(bytes / (mem * (1<<30))) + blocks = @compat ceil(Integer, bytes / (mem * (1<<30))) if parallel blocks= min(nₓ, max(blocks, nworkers())) end diff --git a/src/train.jl b/src/train.jl index 2c97832..1f6aab3 100644 --- a/src/train.jl +++ b/src/train.jl @@ -59,7 +59,7 @@ function GMMk{T}(n::Int, x::DataOrMatrix{T}; kind=:diag, nInit::Int=50, nIter::I yy = Matrix[] for y in x ny = size(y,1) - nsample = min(ny, iceil(nneeded / length(x))) + nsample = min(ny, @compat ceil(Integer, nneeded / length(x))) push!(yy, y[sample(1:ny, nsample, replace=false),:]) end xx = vcat(yy...) @@ -265,7 +265,7 @@ end ## A function we see more often... Λ is in chol(inv(Σ)) form ## compute Δ_i = (x_i - μ)' Λ (x_i - μ) ## Note: the return type of Δ should be the promote_type of x and μ/ciΣ -function xμTΛxμ!(Δ::Matrix, x::Matrix, μ::Matrix, ciΣ::Triangular) +function xμTΛxμ!(Δ::Matrix, x::Matrix, μ::Matrix, ciΣ::UpperTriangular) # broadcast!(-, Δ, x, μ) # size: nₓ × d, add ops: nₓ * d (nₓ, d) = size(x) @inbounds for j = 1:d