Skip to content

Commit

Permalink
MarkovChain: Replace full with custom todense
Browse files Browse the repository at this point in the history
in stationary_distributions
  • Loading branch information
oyamad committed Jul 12, 2016
1 parent 71886f0 commit ec45b0e
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/markov/mc_tools.jl
Expand Up @@ -244,8 +244,9 @@ for (S, ex_T, ex_gth) in ((Real, :(T), :(gth_solve!)),
dist = zeros(T1, n)
# * mc.p[rec_class, rec_class] is a copy, hence gth_solve!
# while gth_solve for Int matrix
# * full is to convert a sparse matrix to a dense matrix
A = full(mc.p[rec_class, rec_class])
# * todense is to convert a sparse matrix to a dense matrix
# with eltype T1
A = todense(T1, mc.p[rec_class, rec_class])
dist[rec_class] = $ex_gth(A)
stationary_dists[i] = dist
end
Expand All @@ -271,6 +272,24 @@ recurrent class.
""" stationary_distributions


"""Custom version of `full`, which allows convertion to type T"""
# From base/sparse/sparsematrix.jl
function todense(T::Type, S::SparseMatrixCSC)
A = zeros(T, S.m, S.n)
for Sj in 1:S.n
for Sk in nzrange(S, Sj)
Si = S.rowval[Sk]
Sv = S.nzval[Sk]
A[Si, Sj] = Sv
end
end
return A
end

"""If A is already dense, return A as is"""
todense(::Type, A::Array) = A


"""
Simulate time series of state transitions of the Markov chain `mc`.
Expand Down

0 comments on commit ec45b0e

Please sign in to comment.