Skip to content

Commit cfe446d

Browse files
committed
truncate k parameter if it exceeding the amount of valid components allowed by P
Fixes #105
1 parent 1611b57 commit cfe446d

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

msmtools/analysis/api.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,21 @@ def stationary_distribution(T):
353353
return mu
354354

355355

356+
def _check_k(T, k):
357+
# ensure k is not exceeding shape of transition matrix
358+
if k is None:
359+
return
360+
n = T.shape[0]
361+
if _issparse(T):
362+
# for sparse matrices we can compute an eigendecomposition of n - 1
363+
new_k = min(n - 1, k)
364+
else:
365+
new_k = min(n, k)
366+
if new_k < k:
367+
warnings.warn('truncated eigendecomposition to contain %s components' % new_k, category=UserWarning)
368+
return new_k
369+
370+
356371
def eigenvalues(T, k=None, ncv=None, reversible=False, mu=None):
357372
r"""Find eigenvalues of the transition matrix.
358373
@@ -399,6 +414,7 @@ def eigenvalues(T, k=None, ncv=None, reversible=False, mu=None):
399414
400415
"""
401416
T = _types.ensure_ndarray_or_sparse(T, ndim=2, uniform=True, kind='numeric')
417+
k =_check_k(T, k)
402418
if _issparse(T):
403419
return sparse.decomposition.eigenvalues(T, k, ncv=ncv, reversible=reversible, mu=mu)
404420
else:
@@ -457,6 +473,7 @@ def timescales(T, tau=1, k=None, ncv=None, reversible=False, mu=None):
457473
458474
"""
459475
T = _types.ensure_ndarray_or_sparse(T, ndim=2, uniform=True, kind='numeric')
476+
k =_check_k(T, k)
460477
if _issparse(T):
461478
return sparse.decomposition.timescales(T, tau=tau, k=k, ncv=ncv,
462479
reversible=reversible, mu=mu)
@@ -537,17 +554,15 @@ def eigenvectors(T, k=None, right=True, ncv=None, reversible=False, mu=None):
537554
538555
"""
539556
T = _types.ensure_ndarray_or_sparse(T, ndim=2, uniform=True, kind='numeric')
557+
k = _check_k(T, k)
540558
if _issparse(T):
541-
if right:
542-
return sparse.decomposition.eigenvectors(T, k=k, right=right, ncv=ncv)
543-
else:
544-
return sparse.decomposition.eigenvectors(T, k=k, right=right, ncv=ncv).T
545-
559+
ev = sparse.decomposition.eigenvectors(T, k=k, right=right, ncv=ncv, reversible=reversible, mu=mu)
546560
else:
547-
if right:
548-
return dense.decomposition.eigenvectors(T, k=k, right=right)
549-
else:
550-
return dense.decomposition.eigenvectors(T, k=k, right=right).T
561+
ev = dense.decomposition.eigenvectors(T, k=k, right=right, reversible=reversible, mu=mu)
562+
563+
if not right:
564+
ev = ev.T
565+
return ev
551566

552567

553568
def rdl_decomposition(T, k=None, norm='auto', ncv=None, reversible=False, mu=None):
@@ -624,6 +639,7 @@ def rdl_decomposition(T, k=None, norm='auto', ncv=None, reversible=False, mu=Non
624639
625640
"""
626641
T = _types.ensure_ndarray_or_sparse(T, ndim=2, uniform=True, kind='numeric')
642+
k = _check_k(T, k)
627643
if _issparse(T):
628644
return sparse.decomposition.rdl_decomposition(T, k=k, norm=norm, ncv=ncv,
629645
reversible=reversible, mu=mu)

0 commit comments

Comments
 (0)