Skip to content

Fix bug in slogdet and expose it in linalg module #807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pytensor/tensor/nlinalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def perform(self, node, inputs, outputs):
(x,) = inputs
(sign, det) = outputs
try:
sign[0], det[0] = (z.astype(x.dtype) for z in np.linalg.slogdet(x))
sign[0], det[0] = (np.array(z, dtype=x.dtype) for z in np.linalg.slogdet(x))
except Exception:
print("Failed to compute determinant", x)
raise
Expand Down Expand Up @@ -1186,6 +1186,7 @@ def kron(a, b):
"lstsq",
"matrix_power",
"norm",
"slogdet",
"tensorinv",
"tensorsolve",
"kron",
Expand Down
5 changes: 5 additions & 0 deletions tests/tensor/test_nlinalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,11 @@ def test_slogdet():
sign, det = np.linalg.slogdet(r)
assert np.equal(sign, f_sign)
assert np.allclose(det, f_det)
# check numpy array types is returned
# see https://github.com/pymc-devs/pytensor/issues/799
sign, logdet = slogdet(x)
det = sign * pytensor.tensor.exp(logdet)
assert_array_almost_equal(det.eval({x: [[1]]}), np.array(1.0))


def test_trace():
Expand Down
Loading