Skip to content

Conversation

sylee957
Copy link
Member

References to other Issues or PRs

Fixes #25282

Brief description of what is fixed or changed

Other comments

Release Notes

  • matrices
    • Fixed some corner cases when Matrix.eigenvals gives wrong result when multiplicity=False.

@sympy-bot
Copy link

sympy-bot commented Jun 23, 2023

Hi, I am the SymPy bot (v170). 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 some corner cases when Matrix.eigenvals gives wrong result when multiplicity=False. (#25283 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 #25282

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


#### 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 some corner cases when `Matrix.eigenvals` gives wrong result when `multiplicity=False`.
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

Copy link
Collaborator

@oscarbenjamin oscarbenjamin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@@ -258,7 +258,7 @@ def _eigenvals_dict(
f = charpoly.as_expr()
x = charpoly.gen
try:
eigs = {CRootOf(f, x, idx): 1 for idx in range(degree)}
eigs = Counter(CRootOf(f, x, idx) for idx in range(degree))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it could be charpoly.all_roots(multiple=False)

@oscarbenjamin
Copy link
Collaborator

Maybe use charpoly.all_roots(multiple=False)

@sylee957 sylee957 enabled auto-merge June 23, 2023 21:41
@alreadydone
Copy link

alreadydone commented Jun 25, 2023

Thanks for the PR! There are CI failures, which however don't seem to be related to this PR.

Have you considered using factorization of the characteristic polynomial to compute the eigenvalues with their algebraic multiplicities? That approach seems much faster according to my test on my example:

from sympy import Matrix, symbols
from time import time

dd = sd = [0] * 11 + [1]
ds = [2, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0]
ss = ds.copy()
ss[8] = 2

def rotate(x, i):
    return x[i:] + x[:i]

mat = []
for i in range(12):
    mat.append(rotate(ss, i) + rotate(sd, i))
for i in range(12):
    mat.append(rotate(ds, i) + rotate(dd, i))
mat = Matrix(mat)

t = time()
factors = mat.charpoly(symbols('lambda')).factor_list()
print('charpoly.factor_list takes:', time()-t)

t = time()
eigenvalues = mat.eigenvals()
print('eigenvals takes:', time()-t)

Output:

charpoly.factor_list takes: 0.08691191673278809
eigenvals takes: 0.9043083190917969

Once we get the irreducible factors, we can populate the eigenvalue dict with roots of the irreducible factors (CRootOf(p, i) for i = 0, ..., d-1 for a factor p of degree d, or x0 for a factor x - x0 of degree 1) and it seems that that would match the current eigenvals output.

@sylee957
Copy link
Member Author

The 2.7 failing backlog prevents the merges though

@oscarbenjamin
Copy link
Collaborator

Have you considered using factorization of the characteristic polynomial to compute the eigenvalues with their algebraic multiplicities?

Once we get the irreducible factors, we can populate the eigenvalue dict with roots of the irreducible factors (CRootOf(p, i) for i = 0, ..., d-1 for a factor p of degree d, or x0 for a factor x - x0 of degree 1) and it seems that that would match the current eigenvals output.

The all_roots method already does this. The additional time compared to factor_list is taken up by root isolation for the CRootOfs.

@oscarbenjamin oscarbenjamin disabled auto-merge June 26, 2023 11:01
@oscarbenjamin oscarbenjamin merged commit eaa65da into sympy:master Jun 26, 2023
@sylee957 sylee957 deleted the eigen branch June 26, 2023 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

incorrect multiplicities from eigenvals
4 participants