Skip to content

Implement more algorithms for computing eigenvalues #39804

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 11 commits into from
Apr 18, 2025

Conversation

user202729
Copy link
Contributor

@user202729 user202729 commented Mar 27, 2025

Now eigenvalues() and eigenvectors_left() takes algorithm parameter.

Partially handles #39762

Also changes the default algorithm of eigenvalues() to pari_charpoly when the base field is RR or CC.

Additional changes required:

  • implement conversion from mpmath object to RR or CC
  • implement shifting elements of RR by a negative value
  • detect error of conversion to mpmath object
  • change a hard error to a warning

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

Copy link

github-actions bot commented Mar 27, 2025

Documentation preview for this PR (built with commit 74f8760; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

[0 0 0 0 0]
[0 0 0 0 0]
sage: P*D-H*P == matrix.zero(5)
True
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is necessary because now check=False is passed, so exactify is no longer called, so it shows some 0.000000?e-16 thing instead.

Copy link
Collaborator

@tscrim tscrim left a comment

Choose a reason for hiding this comment

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

LGTM modulo some details.

Co-authored-by: Travis Scrimshaw <[email protected]>
@user202729
Copy link
Contributor Author

I decide to make eigenvalues() and eigenvectors_left() with the default algorithm never raise experimental warning.

The reason being the functionality and interface never need to change, even if flint decide to remove algorithm=None, we can just automatically switch to something else (pari is the next fastest, but sage wins in case only eigenvalues are needed because pari need to compute eigenvectors also, though we can compute charpoly → polroots in pari)

@user202729
Copy link
Contributor Author

Thought: is .eigenvalues(algorithm="pari") a good idea? Later on pari may implement a function that computes the eigenvalues only and is faster, then what are we going to call it, .eigenvalues(algorithm="pari_faster")?

The situation is somewhat analogous to https://github.com/sagemath/sage/pull/39243/files where small_roots_algorithm is added…

@user202729
Copy link
Contributor Author

user202729 commented Mar 31, 2025

By benchmarking result it appears that pari_charpoly is faster than flint for computing eigenvalues of random matrix over RR, so I use that.

@user202729 user202729 mentioned this pull request Mar 31, 2025
5 tasks
Copy link
Collaborator

@tscrim tscrim left a comment

Choose a reason for hiding this comment

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

I think that is good for the algorithm to not issue a warning.

I don't have an answer with regards to PARI. It's based on a potential future case, and we can worry about it later if that does happen.

Just one last little doc tweak, then a positive review.

@user202729
Copy link
Contributor Author

user202729 commented Apr 2, 2025

Test failure looks like #22482 and unrelated to this one.

(Still waiting for documentation to build to check it)

Edit: the links work https://doc-pr-39804--sagemath.netlify.app/html/en/reference/matrices/sage/matrix/matrix2#hunk5

@tscrim
Copy link
Collaborator

tscrim commented Apr 3, 2025

Thank you. Sorry, I thought that short version would work based on what is important at startup. Positive review.

vbraun pushed a commit to vbraun/sage that referenced this pull request Apr 4, 2025
sagemathgh-39804: Implement more algorithms for computing eigenvalues
    
Now `eigenvalues()` and `eigenvectors_left()` takes `algorithm`
parameter.

Partially handles sagemath#39762

Also changes the default algorithm of `eigenvalues()` to `pari_charpoly`
when the base field is `RR` or `CC`.

Additional changes required:

* implement conversion from mpmath object to RR or CC
* implement shifting elements of RR by a negative value
* detect error of conversion to mpmath object
* change a hard error to a warning

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#39804
Reported by: user202729
Reviewer(s): Travis Scrimshaw, user202729
vbraun pushed a commit to vbraun/sage that referenced this pull request Apr 5, 2025
sagemathgh-39804: Implement more algorithms for computing eigenvalues
    
Now `eigenvalues()` and `eigenvectors_left()` takes `algorithm`
parameter.

Partially handles sagemath#39762

Also changes the default algorithm of `eigenvalues()` to `pari_charpoly`
when the base field is `RR` or `CC`.

Additional changes required:

* implement conversion from mpmath object to RR or CC
* implement shifting elements of RR by a negative value
* detect error of conversion to mpmath object
* change a hard error to a warning

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#39804
Reported by: user202729
Reviewer(s): Travis Scrimshaw, user202729
@vbraun
Copy link
Member

vbraun commented Apr 6, 2025

On 32-bit:

**********************************************************************
File "src/sage/matrix/matrix2.pyx", line 7321, in sage.matrix.matrix2.Matrix.list
Failed example:
    l = m.eigenvectors_left(algorithm="pari"); l
Expected:
    [(-1.4142135623730950487637880730318329370*I,
      [(0.707106781186547524*I, 1.00000000000000000)],
      1),
     (1.4142135623730950487637880730318329370*I,
      [(-0.707106781186547524*I, 1.00000000000000000)],
      1)]
Got:
    [(-1.41421356237309505*I,
      [(0.7071067811865475243818940365*I, 1.000000000000000000000000000)],
      1),
     (1.41421356237309505*I,
      [(-0.7071067811865475243818940365*I, 1.000000000000000000000000000)],
      1)]
**********************************************************************
File "src/sage/matrix/matrix2.pyx", line 7328, in sage.matrix.matrix2.Matrix.list
Failed example:
    m._fix_eigenvectors_extend(l, extend=True)
Expected:
    [(-1.4142135623730950487637880730318329370*I,
      [(0.707106781186547524*I, 1.00000000000000000)],
      1),
     (1.4142135623730950487637880730318329370*I,
      [(-0.707106781186547524*I, 1.00000000000000000)],
      1)]
Got:
    [(-1.41421356237309505*I,
      [(0.7071067811865475243818940365*I, 1.000000000000000000000000000)],
      1),
     (1.41421356237309505*I,
      [(-0.7071067811865475243818940365*I, 1.000000000000000000000000000)],
      1)]
**********************************************************************
File "src/sage/matrix/matrix2.pyx", line 7422, in sage.matrix.matrix2.Matrix.list
Failed example:
    m.eigenvectors_left(algorithm="pari")
Expected:
    [(-1.4142135623730950487637880730318329370*I,
      [(0.707106781186547524*I, 1.00000000000000000)],
      1),
     (1.4142135623730950487637880730318329370*I,
      [(-0.707106781186547524*I, 1.00000000000000000)],
      1)]
Got:
    [(-1.41421356237309505*I,
      [(0.7071067811865475243818940365*I, 1.000000000000000000000000000)],
      1),
     (1.41421356237309505*I,
      [(-0.7071067811865475243818940365*I, 1.000000000000000000000000000)],
      1)]
**********************************************************************

@user202729
Copy link
Contributor Author

user202729 commented Apr 6, 2025

Are they all the failures? (I haven't figured how to install pari 32-bit on my machine)

@vbraun
Copy link
Member

vbraun commented Apr 6, 2025

Yes, those were the only ones

vbraun pushed a commit to vbraun/sage that referenced this pull request Apr 7, 2025
sagemathgh-39804: Implement more algorithms for computing eigenvalues
    
Now `eigenvalues()` and `eigenvectors_left()` takes `algorithm`
parameter.

Partially handles sagemath#39762

Also changes the default algorithm of `eigenvalues()` to `pari_charpoly`
when the base field is `RR` or `CC`.

Additional changes required:

* implement conversion from mpmath object to RR or CC
* implement shifting elements of RR by a negative value
* detect error of conversion to mpmath object
* change a hard error to a warning

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#39804
Reported by: user202729
Reviewer(s): Travis Scrimshaw, user202729
vbraun pushed a commit to vbraun/sage that referenced this pull request Apr 10, 2025
sagemathgh-39804: Implement more algorithms for computing eigenvalues
    
Now `eigenvalues()` and `eigenvectors_left()` takes `algorithm`
parameter.

Partially handles sagemath#39762

Also changes the default algorithm of `eigenvalues()` to `pari_charpoly`
when the base field is `RR` or `CC`.

Additional changes required:

* implement conversion from mpmath object to RR or CC
* implement shifting elements of RR by a negative value
* detect error of conversion to mpmath object
* change a hard error to a warning

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#39804
Reported by: user202729
Reviewer(s): Travis Scrimshaw, user202729
vbraun pushed a commit to vbraun/sage that referenced this pull request Apr 13, 2025
sagemathgh-39804: Implement more algorithms for computing eigenvalues
    
Now `eigenvalues()` and `eigenvectors_left()` takes `algorithm`
parameter.

Partially handles sagemath#39762

Also changes the default algorithm of `eigenvalues()` to `pari_charpoly`
when the base field is `RR` or `CC`.

Additional changes required:

* implement conversion from mpmath object to RR or CC
* implement shifting elements of RR by a negative value
* detect error of conversion to mpmath object
* change a hard error to a warning

### 📝 Checklist

<!-- Put an `x` in all the boxes that apply. -->

- [x] The title is concise and informative.
- [x] The description explains in detail what this PR is about.
- [x] I have linked a relevant issue or discussion.
- [x] I have created tests covering the changes.
- [x] I have updated the documentation and checked the documentation
preview.

### ⌛ Dependencies

<!-- List all open PRs that this PR logically depends on. For example,
-->
<!-- - sagemath#12345: short description why this is a dependency -->
<!-- - sagemath#34567: ... -->
    
URL: sagemath#39804
Reported by: user202729
Reviewer(s): Travis Scrimshaw, user202729
@vbraun vbraun merged commit 3229ce4 into sagemath:develop Apr 18, 2025
23 of 24 checks passed
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.

3 participants