Skip to content

Commit 760c6e4

Browse files
committed
workaround scipy1.8 limitation. Could be reverted for scipy1.12
1 parent c2054e5 commit 760c6e4

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

libpysal/weights/raster.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,13 @@ def da2WSP(
270270
# then eliminate zeros from the data. This changes the
271271
# sparsity of the csr_matrix !!
272272
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+
####
274280
sw.setdiag(0)
275281
sw.eliminate_zeros()
276282
sw.data[:] = np.ones_like(sw.data, dtype=np.int8)

libpysal/weights/util.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,17 +523,42 @@ def higher_order_sp(
523523
)
524524

525525
if lower_order:
526-
wk = sum(sparse.linalg.matrix_power(w, k) for k in range(1, k+1))
527526
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+
####
528533
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+
####
530545

531546
rk, ck = wk.nonzero()
532547
sk = set(zip(rk, ck, strict=True))
533548

534549
if shortest_path:
535550
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+
####
537562
rj, cj = wj.nonzero()
538563
sj = set(zip(rj, cj, strict=True))
539564
sk.difference_update(sj)

0 commit comments

Comments
 (0)