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 diagonal matrix construction from base ring elements that have _matrix_ methods #38235

Merged
merged 3 commits into from
Jul 24, 2024

Conversation

mkoeppe
Copy link
Contributor

@mkoeppe mkoeppe commented Jun 17, 2024

The matrix constructors are designed to accept a scalar as the entries and then construct a diagonal matrix; special case: constructing the zero matrix from 0.

However, when the scalar is a base ring element that happens to have a _matrix_ method, that is tried first, leading to the errors reported in #38221.
Here we change it so that if the base ring (or matrix space) has been specified already and a scalar from that base ring is passed, that takes precedence over using a _matrix_ method. (Coercion is not attempted.)

For the special case of zero_matrix and MatrixSpace.zero_matrix, we now use entries=None, which is the fastest and most robust path in MatrixArgs.entries_type to designate a zero matrix.

Fixes #38221

📝 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

@mkoeppe mkoeppe changed the title MatrixSpace.zero_matrix: Pass entries=None to the element class Fix matrix construction from base ring elements that have _matrix_ methods Jun 17, 2024
Copy link

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

@mkoeppe mkoeppe requested a review from tscrim June 17, 2024 22:10
@mkoeppe mkoeppe changed the title Fix matrix construction from base ring elements that have _matrix_ methods Fix diagonal matrix construction from base ring elements that have _matrix_ methods Jun 17, 2024
@mkoeppe mkoeppe changed the title Fix diagonal matrix construction from base ring elements that have _matrix_ methods Fix diagonal matrix construction from base ring elements that have _matrix_ methods Jun 17, 2024
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.

src/sage/matrix/special.py Show resolved Hide resolved
@mkoeppe
Copy link
Contributor Author

mkoeppe commented Jun 20, 2024

Thank you!

@vbraun vbraun merged commit c2372a9 into sagemath:develop Jul 24, 2024
26 checks passed
@mkoeppe mkoeppe added this to the sage-10.5 milestone Jul 25, 2024
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.

Zero matrix over an algebra
3 participants