Skip to content
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

Fix commutation issue of matrix adjoint, transpose, inverse #24738

Merged
merged 2 commits into from
Aug 25, 2023

Conversation

sylee957
Copy link
Member

@sylee957 sylee957 commented Feb 17, 2023

References to other Issues or PRs

Fixes #24735

Brief description of what is fixed or changed

I added a guard for bc_matmul for matrix power because list multiplied with negative integer gives empty list, which can be causing some bugs when MatPow is used with negative integer.

So this fixes more general issues with matrix power with negative integer block_collapse(MatPow(A.transpose(), -2))

This also fixes more general problem of adjoint, transpose, conjugate not giving same result as inverse
For example,

A = MatrixSymbol('A', n, n)

A.transpose().inverse() == A.inverse().transpose()
A.conjugate().inverse() == A.inverse().conjugate()
A.adjoint().inverse() == A.inverse().adjoint()

because each methods had used different code path.

Other comments

Release Notes

  • matrices
    • Fixed block_collapse giving wrong result for Inverse
    • Fixed an issue of adjoint, transpose, conjugate not commuting with inverse for matrix expressions. For example, A.transpose().inverse() and A.inverse().transpose() will give same result.

@sympy-bot
Copy link

Hi, I am the SymPy bot. I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

  • No release notes entry will be added for this pull request.
Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->


#### Brief description of what is fixed or changed

Fixes #24735

#### Other comments

This is an experimental fix of #24735

#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers. Formerly, `log(-x)` incorrectly gave `-log(x)`.

* physics.units
  * Corrected a semantical error in the conversion between volt and statvolt which
    reported the volt as being larger than the statvolt.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
NO ENTRY
<!-- END RELEASE NOTES -->

1 similar comment
@sympy-bot
Copy link

Hi, I am the SymPy bot. I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

  • No release notes entry will be added for this pull request.
Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->


#### Brief description of what is fixed or changed

Fixes #24735

#### Other comments

This is an experimental fix of #24735

#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers. Formerly, `log(-x)` incorrectly gave `-log(x)`.

* physics.units
  * Corrected a semantical error in the conversion between volt and statvolt which
    reported the volt as being larger than the statvolt.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
NO ENTRY
<!-- END RELEASE NOTES -->

@sympy-bot
Copy link

sympy-bot commented Feb 17, 2023

Hi, I am the SymPy bot. I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

Your release notes are in good order.

Here is what the release notes will look like:

  • matrices
    • Fixed block_collapse giving wrong result for Inverse (#24738 by @sylee957)

    • Fixed an issue of adjoint, transpose, conjugate not commuting with inverse for matrix expressions. For example, A.transpose().inverse() and A.inverse().transpose() will give same result. (#24738 by @sylee957)

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.13.

Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->

Fixes #24735

#### Brief description of what is fixed or changed

I added a guard for `bc_matmul` for matrix power because `list` multiplied with negative integer gives empty list, which can be causing some bugs when `MatPow` is used with negative integer.

So this fixes more general issues with matrix power with negative integer `block_collapse(MatPow(A.transpose(), -2))`

This also fixes more general problem of `adjoint, transpose, conjugate` not giving same result as `inverse`
For example,
```
A = MatrixSymbol('A', n, n)

A.transpose().inverse() == A.inverse().transpose()
A.conjugate().inverse() == A.inverse().conjugate()
A.adjoint().inverse() == A.inverse().adjoint()
```
because each methods had used different code path.

#### Other comments

#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers. Formerly, `log(-x)` incorrectly gave `-log(x)`.

* physics.units
  * Corrected a semantical error in the conversion between volt and statvolt which
    reported the volt as being larger than the statvolt.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
- matrices
  - Fixed `block_collapse` giving wrong result for `Inverse`
  - Fixed an issue of `adjoint`, `transpose`, `conjugate` not commuting with `inverse` for matrix expressions. For example, `A.transpose().inverse()` and `A.inverse().transpose()` will give same result.
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@sylee957 sylee957 marked this pull request as ready for review February 17, 2023 22:57
@github-actions
Copy link

Benchmark results from GitHub Actions

Lower numbers are good, higher numbers are bad. A ratio less than 1
means a speed up and greater than 1 means a slowdown. Green lines
beginning with + are slowdowns (the PR is slower then master or
master is slower than the previous release). Red lines beginning
with - are speedups.

Significantly changed benchmark results (PR vs master)

Significantly changed benchmark results (master vs previous release)

       before           after         ratio
     [41d90958]       [31656382]
     <sympy-1.11.1^0>                 
-        1.17±0ms         700±20μs     0.60  solve.TimeSparseSystem.time_linear_eq_to_matrix(10)
-     3.33±0.01ms         1.28±0ms     0.38  solve.TimeSparseSystem.time_linear_eq_to_matrix(20)
-     6.67±0.01ms         1.87±0ms     0.28  solve.TimeSparseSystem.time_linear_eq_to_matrix(30)

Full benchmark results can be found as artifacts in GitHub Actions
(click on checks at the top of the PR).

@sylee957 sylee957 requested review from a team and smichr August 23, 2023 11:58
@sylee957
Copy link
Member Author

I managed to get back this working. I'd like some reviews for this.

@oscarbenjamin
Copy link
Collaborator

Looks good to me. Thanks!

@oscarbenjamin oscarbenjamin merged commit f8bed8c into sympy:master Aug 25, 2023
54 of 55 checks passed
@sylee957 sylee957 deleted the block_collapse branch August 25, 2023 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

block_collapse fails on inverse-transpose of block matrix
3 participants