Skip to content

Instantly share code, notes, and snippets.

@nschloe
Created December 22, 2021 17:49
Show Gist options
  • Save nschloe/4d604c30ac3dda239413de0c2c595f82 to your computer and use it in GitHub Desktop.
Save nschloe/4d604c30ac3dda239413de0c2c595f82 to your computer and use it in GitHub Desktop.
np.linalg.solve vs np.linalg.solve (twice)
import perfplot
import numpy as np
def inv(AX):
A, X = AX
Ainv = np.linalg.inv(A)
return Ainv @ X @ Ainv.T
def double_solve(AX):
A, X = AX
return np.linalg.solve(A, np.linalg.solve(A, X).T).T
b = perfplot.bench(
setup=lambda n: (np.random.rand(n, n), np.random.rand(n, n)),
kernels=[inv, double_solve],
n_range=[2 ** k for k in range(10)],
)
b.save("out.png")
b.show()
@nschloe
Copy link
Author

nschloe commented Dec 22, 2021

out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment