@@ -353,6 +353,21 @@ def stationary_distribution(T):
353
353
return mu
354
354
355
355
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
+
356
371
def eigenvalues (T , k = None , ncv = None , reversible = False , mu = None ):
357
372
r"""Find eigenvalues of the transition matrix.
358
373
@@ -399,6 +414,7 @@ def eigenvalues(T, k=None, ncv=None, reversible=False, mu=None):
399
414
400
415
"""
401
416
T = _types .ensure_ndarray_or_sparse (T , ndim = 2 , uniform = True , kind = 'numeric' )
417
+ k = _check_k (T , k )
402
418
if _issparse (T ):
403
419
return sparse .decomposition .eigenvalues (T , k , ncv = ncv , reversible = reversible , mu = mu )
404
420
else :
@@ -457,6 +473,7 @@ def timescales(T, tau=1, k=None, ncv=None, reversible=False, mu=None):
457
473
458
474
"""
459
475
T = _types .ensure_ndarray_or_sparse (T , ndim = 2 , uniform = True , kind = 'numeric' )
476
+ k = _check_k (T , k )
460
477
if _issparse (T ):
461
478
return sparse .decomposition .timescales (T , tau = tau , k = k , ncv = ncv ,
462
479
reversible = reversible , mu = mu )
@@ -537,17 +554,15 @@ def eigenvectors(T, k=None, right=True, ncv=None, reversible=False, mu=None):
537
554
538
555
"""
539
556
T = _types .ensure_ndarray_or_sparse (T , ndim = 2 , uniform = True , kind = 'numeric' )
557
+ k = _check_k (T , k )
540
558
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 )
546
560
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
551
566
552
567
553
568
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
624
639
625
640
"""
626
641
T = _types .ensure_ndarray_or_sparse (T , ndim = 2 , uniform = True , kind = 'numeric' )
642
+ k = _check_k (T , k )
627
643
if _issparse (T ):
628
644
return sparse .decomposition .rdl_decomposition (T , k = k , norm = norm , ncv = ncv ,
629
645
reversible = reversible , mu = mu )
0 commit comments