-
-
Notifications
You must be signed in to change notification settings - Fork 652
Speed up square matrix times vector over GF(2) #37375
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
Speed up square matrix times vector over GF(2) #37375
Conversation
…rix_times_vector_over_F2' into speed_up_square_matrix_times_vector_over_F2
Documentation preview for this PR (built with commit 86a75ae; changes) is ready! 🎉 |
There was a problem hiding this 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.
When doing a matrix-times-vector multiplication over F_2, there is some inefficiency caused by calling `VectorSpace` to create a parent for the result. This becomes noticeable when repeatedly multiplying small matrices. When the dimensions are all the same, this can be improved by using the parent of the vector also as the parent for the result. Before: ``` sage: A = random_matrix(GF(2),10^2,10^2) sage: v0 = vector(random_matrix(GF(2),10^2,1)) sage: %timeit A*v0 2.78 µs ± 128 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) ``` After: ``` sage: A = random_matrix(GF(2),10,10) sage: v0 = vector(random_matrix(GF(2),10,1)) sage: %timeit A*v0 995 ns ± 22.9 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) ``` URL: sagemath#37375 Reported by: kedlaya Reviewer(s): Lorenz Panny
sagemathgh-37375: Speed up square matrix times vector over GF(2) When doing a matrix-times-vector multiplication over F_2, there is some inefficiency caused by calling `VectorSpace` to create a parent for the result. This becomes noticeable when repeatedly multiplying small matrices. When the dimensions are all the same, this can be improved by using the parent of the vector also as the parent for the result. Before: ``` sage: A = random_matrix(GF(2),10^2,10^2) sage: v0 = vector(random_matrix(GF(2),10^2,1)) sage: %timeit A*v0 2.78 µs ± 128 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) ``` After: ``` sage: A = random_matrix(GF(2),10,10) sage: v0 = vector(random_matrix(GF(2),10,1)) sage: %timeit A*v0 995 ns ± 22.9 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) ``` URL: sagemath#37375 Reported by: kedlaya Reviewer(s): Lorenz Panny
Just a note, your timing doesn't show anything, since the dimensions are different (one's with |
Oh! Good catch. But there is still a significant speedup: The reduction in runtime is about |
I fixed the examples to both use 10 by 10 matrices, as indeed this problem is more acute with small matrices. There seems to be a fixed amount of overhead with matrix/vector creation which is rampant throughout Sage; a broader fix for that would obviate the need for such trickery. |
…matrix-vector multiplication over GF(2) <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> Fixes [sagemath#40167](sagemath#40167). This PR fixes a bug in `Matrix_mod2_dense._matrix_times_vector_` revealed in [sagemath#40167](sagemath#40167), where the parent of the resulting vector was incorrectly reused from the input vector. The regression was introduced in [PR sagemath#37375](sagemath#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) ```python 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 <!-- 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. - [ ] 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#40176 Reported by: Aolong Li Reviewer(s): Travis Scrimshaw
When doing a matrix-times-vector multiplication over F_2, there is some inefficiency caused by calling
VectorSpace
to create a parent for the result. This becomes noticeable when repeatedly multiplying small matrices.When the dimensions are all the same, this can be improved by using the parent of the vector also as the parent for the result. Before:
After: