Skip to content

Commit

Permalink
modularize _is_communicating in cython backend
Browse files Browse the repository at this point in the history
  • Loading branch information
aadya940 committed Feb 19, 2024
1 parent 18a4ca2 commit 955b9c5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 11 additions & 3 deletions chainopy/_backend/_is_communicating.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ cimport numpy as cnp
import numpy as np
import cython


@cython.wraparound(False)
@cython.boundscheck(False)
def is_communicating_cython(cnp.ndarray[cnp.float64_t, ndim=2] tpm, list states, str state1, str state2, int threshold):
def _is_partially_communicating(cnp.ndarray[cnp.float64_t, ndim=2] tpm, list states, str state1, str state2, int threshold):
cdef Py_ssize_t i, idx1, idx2
cdef cnp.ndarray[cnp.float64_t, ndim=2] x = tpm
cdef bint result
Expand All @@ -17,9 +16,18 @@ def is_communicating_cython(cnp.ndarray[cnp.float64_t, ndim=2] tpm, list states,
idx2 = states.index(state2)

for i in range(threshold):
if x[idx1, idx2] > 0 and x[idx2, idx1] > 0:
if x[idx1, idx2] > 0:
return True

x = x @ tpm

return False


@cython.wraparound(False)
@cython.boundscheck(False)
def is_communicating_cython(cnp.ndarray[cnp.float64_t, ndim=2] tpm, list states, str state1, str state2, int threshold):
if (_is_partially_communicating(tpm, states, state1, state2, threshold) \
and _is_partially_communicating(tpm, states, state2, state1, threshold)):
return True
return False
4 changes: 3 additions & 1 deletion chainopy/markov_chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,9 @@ def is_absorbing(self) -> bool:

transient_states = set(self.states) - set(absorbing_states_)
for i in absorbing_states_:
if all(self.is_communicating(state, i) for state in transient_states):
if all(_is_communicating._is_partially_communicating(state, i)
for state in
transient_states):
return True
return False

Expand Down

0 comments on commit 955b9c5

Please sign in to comment.