Skip to content

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

Merged

Conversation

kedlaya
Copy link
Contributor

@kedlaya kedlaya commented Feb 16, 2024

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,10)
sage: v0 = vector(random_matrix(GF(2),10,1))
sage: %timeit A*v0
2.26 µs ± 4.57 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
981 ns ± 9.57 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

Copy link

Documentation preview for this PR (built with commit 86a75ae; changes) is ready! 🎉

Copy link
Member

@yyyyx4 yyyyx4 left a 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.

vbraun pushed a commit to vbraun/sage that referenced this pull request Feb 18, 2024
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
vbraun pushed a commit to vbraun/sage that referenced this pull request Feb 19, 2024
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
@grhkm21
Copy link
Contributor

grhkm21 commented Feb 21, 2024

Just a note, your timing doesn't show anything, since the dimensions are different (one's with $n = 100$, one is with $n = 10$)

@yyyyx4
Copy link
Member

yyyyx4 commented Feb 21, 2024

Oh! Good catch. But there is still a significant speedup: The reduction in runtime is about $50\,\%$ for $10\times10$ matrices and about $20\,\%$ for $100\times100$ matrices.

@kedlaya
Copy link
Contributor Author

kedlaya commented Feb 21, 2024

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.

@vbraun vbraun merged commit e01a438 into sagemath:develop Feb 25, 2024
vbraun pushed a commit to vbraun/sage that referenced this pull request May 29, 2025
…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
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.

5 participants