Skip to content

Commit 9046071

Browse files
rossbarmelissawm
authored andcommitted
Apply suggestions from review - switch to fill_diagonal.
1 parent c587788 commit 9046071

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

Diff for: content/tutorial-svd.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,12 @@ compatible for multiplication. However, this is not true as `s` does not have a
197197
s @ Vt
198198
```
199199

200-
results in a `ValueError`. This happens because having a one-dimensional array for `s`, in this case, is much more economic in practice than building a diagonal matrix with the same data. To reconstruct the original matrix, we can rebuild the diagonal matrix $\Sigma$ with the elements of `s` in its diagonal and with the appropriate dimensions for multiplying: in our case, $\Sigma$ should be 768x1024 since `U` is 768x768 and `Vt` is
201-
1024x1024.
200+
results in a `ValueError`. This happens because having a one-dimensional array for `s`, in this case, is much more economic in practice than building a diagonal matrix with the same data. To reconstruct the original matrix, we can rebuild the diagonal matrix $\Sigma$ with the elements of `s` in its diagonal and with the appropriate dimensions for multiplying: in our case, $\Sigma$ should be 768x1024 since `U` is 768x768 and `Vt` is 1024x1024.
202201

203202
```{code-cell} ipython3
204203
import numpy as np
205-
Sigma = np.zeros((U.shape[0], Vt.shape[0]))
206-
diag_indices = (np.arange(U.shape[0]),) * 2
207-
Sigma[diag_indices] = s
204+
Sigma = np.zeros((U.shape[1], Vt.shape[0]))
205+
np.fill_diagonal(Sigma, s)
208206
```
209207

210208
Now, we want to check if the reconstructed `U @ Sigma @ Vt` is close to the original `img_gray` matrix.

0 commit comments

Comments
 (0)