Skip to content

Trac #40167: Fix incorrect parent reuse in matrix-vector multiplication over GF(2) #40176

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

Conversation

local-ring
Copy link
Contributor

@local-ring local-ring commented May 28, 2025

Fixes #40167. This PR fixes a bug in Matrix_mod2_dense._matrix_times_vector_ revealed in #40167, where the parent of the resulting vector was incorrectly reused from the input vector.

The regression was introduced in PR #37375, which added an optimization to speed up matrix-vector multiplication when the matrix is square and matches the vector dimension. However, it failed to account for edge cases where the input vector's parent is not the full ambient vector space—for example, when working with subspaces. In this case, we can no longer assume that the ambient space is the vector's ambient space.

In such cases, reusing the parent leads to incorrect coercion or a result vector with an invalid parent space. This patch introduces an explicit check: if the vector's parent is the full space GF(2)^n, it is reused; otherwise, a default parent is constructed to ensure correctness.

Example (correct behavior restored)

sage: M = Matrix(GF(2), [[1, 1], [0, 1]])
sage: v = vector(GF(2), [0, 1])
sage: V = span([v])             # one-dimensional subspace of GF(2)^2
sage: image_basis = [M * b for b in V.basis()]
sage: image = span(image_basis)
sage: image.basis() == [(1, 1)]
True # now returns True

📝 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

@local-ring
Copy link
Contributor Author

local-ring commented May 28, 2025

cc: @tscrim @DaveWitteMorris can you review this pr? thanks!

@local-ring local-ring changed the title Trac #410167: Fix incorrect parent reuse in matrix-vector multiplication over GF(2) Trac #40167: Fix incorrect parent reuse in matrix-vector multiplication over GF(2) May 28, 2025
@tscrim
Copy link
Collaborator

tscrim commented May 28, 2025

I don't think checking the base rings agree is needed here as it should never reach here if they are different. If that's not the case, please give the example as that is a bug.

Couldn't you instead add the additional check v.parent().ambient() is v.parent()? That should cover what is needed. Otherwise, it would be sufficient to just check v.parent().rank() == self._nrows I believe.

@local-ring
Copy link
Contributor Author

I don't think checking the base rings agree is needed here as it should never reach here if they are different. If that's not the case, please give the example as that is a bug.

Couldn't you instead add the additional check v.parent().ambient() is v.parent()? That should cover what is needed. Otherwise, it would be sufficient to just check v.parent().rank() == self._nrows I believe.

I agree with you. The check I added before is redundant. Replace with the second one you suggested.

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.

Thanks. Let's get this in.

@DaveWitteMorris
Copy link
Member

Please add "Fixes #40167." as the first sentence of the description of this PR (before "This PR fixes a bug..."). That will tell github that the issue should be automatically closed when this PR is merged. (Currently, the Development box at the right for "Successfully merging this pull request may close these issues" says "None yet".)

@local-ring
Copy link
Contributor Author

Please add "Fixes #40167." as the first sentence of the description of this PR (before "This PR fixes a bug..."). That will tell github that the issue should be automatically closed when this PR is merged. (Currently, the Development box at the right for "Successfully merging this pull request may close these issues" says "None yet".)

Thank you for pointing this out! I've added the issue reference to the PR description so it will close automatically upon merging.

@vbraun vbraun merged commit 5379f2e into sagemath:develop Jun 1, 2025
15 of 22 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.

Wrong basis of vector space
4 participants