Skip to content

Fix nested MatrixSpace scalar actions and coercions - #42607

Open
cxzhong wants to merge 2 commits into
sagemath:developfrom
cxzhong:agent/gh-16247-matrix-base-ring-actions
Open

Fix nested MatrixSpace scalar actions and coercions#42607
cxzhong wants to merge 2 commits into
sagemath:developfrom
cxzhong:agent/gh-16247-matrix-base-ring-actions

Conversation

@cxzhong

@cxzhong cxzhong commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

This PR fixes the concrete nested-MatrixSpace action and coercion bugs identified while investigating #16247. It intentionally leaves the broader Modules(R) category-design issue open.

Root cause

When both operands belonged to matrix spaces, MatrixSpace._get_action_ selected matrix multiplication before considering that one matrix space was the other's base ring. Square nested matrix spaces also let the generic matrix constructor interpret a base-ring matrix as outer entries instead of using the canonical scalar embedding.

Changes

  • Prefer left or right base-ring scalar actions over MatrixMatrixAction when one matrix space canonically coerces into the other's base ring.
  • Embed a matrix base-ring element diagonally by multiplication with the outer identity, including for matrix semirings.
  • For square targets, prefer the canonical S -> base ring -> target coercion path for matrix spaces and other matrix-like parents such as matrix groups.
  • Construct an_element() from coordinate dictionaries when entries are matrices, avoiding the flat-versus-nested sequence ambiguity.
  • Add regression tests for both action-discovery orientations, noncommutative multiplication order, coercible matrix subrings, dense and sparse spaces, dimensions 0 through 3, semirings, matrix groups, and ordinary matrix multiplication.

Validation

src/bin/sage-runtests --force-lib \
    src/sage/categories/modules.py \
    src/sage/categories/left_modules.py \
    src/sage/categories/right_modules.py \
    src/sage/categories/bimodules.py \
    src/sage/categories/action.pyx \
    src/sage/matrix/action.pyx \
    src/sage/matrix/matrix0.pyx \
    src/sage/matrix/matrix_space.py

All 1839 doctests passed. Nested parent and element TestSuite checks also passed after excluding the existing unsupported _test_minpoly check for matrices over a non-domain matrix ring.

📝 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

None.

@github-actions

github-actions Bot commented Jul 31, 2026

Copy link
Copy Markdown

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

@cxzhong
cxzhong marked this pull request as ready for review August 3, 2026 04:57
@cxzhong
cxzhong requested a review from vincentmacri August 3, 2026 04:57
@vincentmacri
vincentmacri removed their request for review August 4, 2026 16:35
@vincentmacri

Copy link
Copy Markdown
Member

I imagine you're inferred this but just to be explicit: if I remove a request for review from me that just means I don't have the capacity for it at the moment and so you should try to request someone else. Please continue requesting reviews from me if you think it is a relevant part of the code that I can review, but I need to prioritize based on the amount of work and my own interest in the PR. I like to make sure I have enough time for when PRs that are especially interesting to me come in, like your recent set_to_product PR for matrix multiplication.

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.

2 participants