-
Notifications
You must be signed in to change notification settings - Fork 186
Rewrite det(inv(X)) → 1/det(X) #2102
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -459,3 +459,38 @@ def test_det_of_factorized_matrix_special_cases(original_fn, expected_fn): | |
| expected = expected_fn(x) | ||
| rewritten = rewrite_graph(out, include=["stabilize", "specialize"]) | ||
| assert_equal_computations([rewritten], [expected]) | ||
|
|
||
|
|
||
| def test_det_of_inv(): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this in test_summary?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because a determinant is a summary of a matrix
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nah this should clearly be linalg/volume.py |
||
| x = pt.tensor("x", shape=(3, 3)) | ||
| out = det(pt.linalg.inv(x)) | ||
| expected = pt.as_tensor(1.0, dtype="float64") / det(x) | ||
| rewritten = rewrite_graph(out, include=["canonicalize", "stabilize"]) | ||
| assert_equal_computations([rewritten], [expected]) | ||
|
|
||
|
|
||
| def test_slogdet_of_inv(): | ||
| x = pt.dmatrix("x") | ||
| # slogdet(inv(x)) -> (sign, logabsdet) | ||
| sign_inv, logabsdet_inv = pt.linalg.slogdet(pt.linalg.inv(x)) | ||
|
|
||
| # expected: (sign(det(x)), -logabsdet(det(x))) | ||
| # det(inv(x)) = 1/det(x), so sign is same. | ||
| # logabsdet(inv(x)) = log(abs(1/det(x))) = -log(abs(det(x))) | ||
| sign_x, logabsdet_x = pt.linalg.slogdet(x) | ||
| expected_sign = sign_x | ||
| expected_logabsdet = -logabsdet_x | ||
|
|
||
| # We need stabilize for det_of_inv and log_reciprocal | ||
| # and specialize for slogdet_specialization | ||
| rewritten_sign, rewritten_logabsdet = rewrite_graph( | ||
| [sign_inv, logabsdet_inv], include=["canonicalize", "stabilize", "specialize"] | ||
| ) | ||
|
|
||
| expected_sign_opt, expected_logabsdet_opt = rewrite_graph( | ||
| [expected_sign, expected_logabsdet], | ||
| include=["canonicalize", "stabilize", "specialize"], | ||
| ) | ||
|
|
||
| assert_equal_computations([rewritten_sign], [expected_sign_opt]) | ||
| assert_equal_computations([rewritten_logabsdet], [expected_logabsdet_opt]) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this same name as what is imported? Github rendering as duplicated