Skip to content

Commit

Permalink
Merge pull request #725 from shriharshtendulkar/patch-1
Browse files Browse the repository at this point in the history
Update als method to use sparse matrix construction
  • Loading branch information
matteobachetti authored May 9, 2023
2 parents d47d50d + 44da794 commit d25edae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changes/725.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid allocation of an unneeded square matrix to improve memory management in ``_als`` (fix Issue 724)
9 changes: 8 additions & 1 deletion stingray/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,14 @@ def _als(y, lam, p, niter=10):
from scipy import sparse

L = len(y)
D = sparse.csc_matrix(np.diff(np.eye(L), 2))

indptr = np.arange(0, L - 1, dtype=np.int32) * 3
indices = np.vstack(
(np.arange(0, L - 2).T, np.arange(0, L - 2).T + 1, np.arange(0, L - 2).T + 2)
).T.flatten()
data = np.tile([1, -2, 1], L - 2)
D = sparse.csc_matrix((data, indices, indptr), shape=(L, L - 2))

w = np.ones(L)
for _ in range(niter):
W = sparse.spdiags(w, 0, L, L)
Expand Down

0 comments on commit d25edae

Please sign in to comment.