Skip to content

Commit 1093c44

Browse files
committed
[analysis/rdl_decomp] discard imag part if norm is reversible or P is reversible.
1 parent 27d9d63 commit 1093c44

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

msmtools/analysis/api.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ def mfpt(T, target, origin=None, tau=1, mu=None):
718718

719719

720720
def hitting_probability(T, target):
721-
"""
721+
r"""
722722
Computes the hitting probabilities for all states to the target states.
723723
724724
The hitting probability of state i to the target set A is defined as the minimal,
@@ -732,7 +732,7 @@ def hitting_probability(T, target):
732732
----------
733733
T : (M, M) ndarray or scipy.sparse matrix
734734
Transition matrix
735-
B : array_like
735+
target: array_like
736736
List of integer state labels for the target set
737737
738738
Returns

msmtools/analysis/dense/decomposition.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def eigenvalues(T, k=None, reversible=False, mu=None):
7474
if reversible:
7575
try:
7676
evals = eigenvalues_rev(T, k=k, mu=mu)
77-
except:
77+
except ValueError:
7878
evals = eigvals(T).real # use fallback code but cast to real
7979
else:
8080
evals = eigvals(T) # nonreversible
@@ -314,6 +314,9 @@ def rdl_decomposition(T, k=None, reversible=False, norm='standard', mu=None):
314314
else:
315315
R, D, L = rdl_decomposition_nrev(T, norm=norm)
316316

317+
if reversible or norm == 'reversible':
318+
D = D.real
319+
317320
if k is None:
318321
return R, D, L
319322
else:

msmtools/analysis/sparse/decomposition.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,13 @@ def rdl_decomposition(T, k=None, norm='auto', ncv=None, reversible=False, mu=Non
296296
else:
297297
norm = 'standard'
298298
if reversible:
299-
return rdl_decomposition_rev(T, k, norm=norm, ncv=ncv, mu=mu)
299+
R, D, L = rdl_decomposition_rev(T, k, norm=norm, ncv=ncv, mu=mu)
300300
else:
301-
return rdl_decomposition_nrev(T, k, norm=norm, ncv=ncv)
301+
R, D, L = rdl_decomposition_nrev(T, k, norm=norm, ncv=ncv)
302+
303+
if reversible or norm == 'reversible':
304+
D = D.real
305+
return R, D, L
302306

303307

304308
def rdl_decomposition_nrev(T, k, norm='standard', ncv=None):

tests/analysis/test_decomposition.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from msmtools.util.exceptions import SpectralWarning, ImaginaryEigenValueWarning
3333
from msmtools.util.birth_death_chain import BirthDeathChain
3434

35-
from msmtools.analysis import stationary_distribution, eigenvalues, eigenvectors
35+
from msmtools.analysis import stationary_distribution, eigenvalues, eigenvectors, is_reversible
3636
from msmtools.analysis import rdl_decomposition, timescales
3737

3838
################################################################################
@@ -162,6 +162,7 @@ def test_eigenvectors_reversible(self):
162162

163163
def test_rdl_decomposition(self):
164164
P = self.bdc.transition_matrix()
165+
assert is_reversible(P)
165166
mu = self.bdc.stationary_distribution()
166167

167168
"""Non-reversible"""
@@ -194,6 +195,7 @@ def test_rdl_decomposition(self):
194195

195196
"""k=None"""
196197
Rn, Dn, Ln = rdl_decomposition(P, norm='reversible')
198+
assert Dn.dtype in (np.float32, np.float64)
197199
Xn = np.dot(Ln, Rn)
198200
"""Right-eigenvectors"""
199201
assert_allclose(np.dot(P, Rn), np.dot(Rn, Dn))

0 commit comments

Comments
 (0)