File tree 2 files changed +35
-4
lines changed
2 files changed +35
-4
lines changed Original file line number Diff line number Diff line change @@ -270,7 +270,13 @@ def da2WSP(
270
270
# then eliminate zeros from the data. This changes the
271
271
# sparsity of the csr_matrix !!
272
272
if k > 1 and not include_nodata :
273
- sw = sum (sparse .linalg .matrix_power (sw , x ) for x in range (1 , k + 1 ))
273
+ #### Could be as follows after scipy >=1.12 is required
274
+ # sw = sum(sparse.linalg.matrix_power(sw, x) for x in range(1, k + 1))
275
+ tmp = sw .copy ()
276
+ for _ in range (k - 1 ):
277
+ tmp += tmp @ sw + sw
278
+ sw = tmp
279
+ ####
274
280
sw .setdiag (0 )
275
281
sw .eliminate_zeros ()
276
282
sw .data [:] = np .ones_like (sw .data , dtype = np .int8 )
Original file line number Diff line number Diff line change @@ -523,17 +523,42 @@ def higher_order_sp(
523
523
)
524
524
525
525
if lower_order :
526
- wk = sum (sparse .linalg .matrix_power (w , k ) for k in range (1 , k + 1 ))
527
526
shortest_path = False
527
+ #### Could be as follows after scipy >=1.12 is required
528
+ # wk = sum(sparse.linalg.matrix_power(w, k) for k in range(1, k+1))
529
+ wk = w .copy ()
530
+ for _ in range (k - 1 ):
531
+ wk = wk @ w + w
532
+ ####
528
533
else :
529
- wk = sparse .linalg .matrix_power (w , k )
534
+ #### Could be as follows after scipy >=1.12 is required
535
+ # wk = sparse.linalg.matrix_power(w, k)
536
+ wk = w .copy ()
537
+ x = 1
538
+ while 2 * x < k :
539
+ wk = wk @ wk
540
+ x *= 2
541
+ while x < k :
542
+ wk = wk @ w
543
+ x += 1
544
+ ####
530
545
531
546
rk , ck = wk .nonzero ()
532
547
sk = set (zip (rk , ck , strict = True ))
533
548
534
549
if shortest_path :
535
550
for j in range (1 , k ):
536
- wj = sparse .linalg .matrix_power (w , j )
551
+ #### Could be as follows after scipy >=1.12 is required
552
+ # wj = sparse.linalg.matrix_power(w, j)
553
+ wj = w .copy ()
554
+ x = 1
555
+ while 2 * x < j :
556
+ wj = wj @ wj
557
+ x *= 2
558
+ while x < j :
559
+ wj = wj @ w
560
+ x += 1
561
+ ####
537
562
rj , cj = wj .nonzero ()
538
563
sj = set (zip (rj , cj , strict = True ))
539
564
sk .difference_update (sj )
You can’t perform that action at this time.
0 commit comments