Skip to content

Fix GF(2) matrix transpose with subdivisions #40423

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
merged 5 commits into from
Aug 2, 2025

Conversation

Biffo89
Copy link
Contributor

@Biffo89 Biffo89 commented Jul 16, 2025

📝 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.

A small bug I noticed when looking at the code for transposing matrices. GF(2) does not properly transpose its subdivisions, meaning transposed matrices can have incorrect subdivisions or cause errors if the indices fall out of range.

sage: M = matrix.random(GF(2),3,3)
sage: N = matrix.block([[M,M,M]])
sage: M
[1 0 0]
[0 0 1]
[0 1 0]
sage: N
[1 0 0|1 0 0|1 0 0]
[0 0 1|0 0 1|0 0 1]
[0 1 0|0 1 0|0 1 0]
sage: N.transpose()
<repr(<sage.matrix.matrix_mod2_dense.Matrix_mod2_dense at 0x7f1eb7f0c1c0>) failed: IndexError: list index out of range>

This also adds subdivision handling to transposes of matrix_complex_ball_dense, matrix_gap, and fixes subdivisions for transposes of empty matrices of type matrix_mod2_dense, matrix_numpy_dense

@xcaruso xcaruso added the gsoc: 2025 Tag for GSoC2025 issues/PRs label Jul 17, 2025
@vneiger
Copy link
Contributor

vneiger commented Jul 19, 2025

Thanks for the fix. Could you also fix the code for "empty" input matrices, i.e. when self has zero row or zero column? currently it does not define any subdivision in this case, whereas other types of matrices do. For example over GF(3):

sage: A = matrix(GF(3), 2, 0)
sage: B = matrix.block([[A], [A]])
sage: B.subdivisions()
([2], [])
sage: B.transpose().subdivisions()
([], [2])
sage: B = matrix.block([[A, A]])
sage: B.subdivisions()
([], [0])
sage: B.transpose().subdivisions()
([0], [])

@Biffo89
Copy link
Contributor Author

Biffo89 commented Jul 21, 2025

I have checked other matrix classes as well and fixed other instances of the zero matrix subdivision problem:
matrix_complex_ball_dense, matrix_gap (added subdivision handling)
matrix_mod2_dense, matrix_numpy_dense (added subdivision handling for empty matrices)

@xcaruso xcaruso self-requested a review July 21, 2025 14:34
@xcaruso
Copy link
Contributor

xcaruso commented Jul 21, 2025

You should add a doctest to show that the bug is indeed fixed.

@Biffo89
Copy link
Contributor Author

Biffo89 commented Jul 21, 2025

Doc tests are added now, checking for transposes of both 0-row and 0-column matrices.

Copy link
Contributor

@xcaruso xcaruso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few comments.
Apart for this, the ticket looks good to me.

@Biffo89
Copy link
Contributor Author

Biffo89 commented Jul 25, 2025

I've updated the doctests now. All instances of _subdivisions should be replaced with subdivisions()

@xcaruso
Copy link
Contributor

xcaruso commented Jul 25, 2025

Failures look unrelated to this PR. I give a positive review.

vbraun pushed a commit to vbraun/sage that referenced this pull request Jul 26, 2025
sagemathgh-40423: Fix GF(2) matrix transpose with subdivisions
    
<!-- ^ 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". -->



### 📝 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.
- [ ] 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.

A small bug I noticed when looking at the code for transposing matrices.
GF(2) does not properly transpose its subdivisions, meaning transposed
matrices can have incorrect subdivisions or cause errors if the indices
fall out of range.

sage: M = matrix.random(GF(2),3,3)
sage: N = matrix.block([[M,M,M]])
sage: M
[1 0 0]
[0 0 1]
[0 1 0]
sage: N
[1 0 0|1 0 0|1 0 0]
[0 0 1|0 0 1|0 0 1]
[0 1 0|0 1 0|0 1 0]
sage: N.transpose()
<repr(<sage.matrix.matrix_mod2_dense.Matrix_mod2_dense at
0x7f1eb7f0c1c0>) failed: IndexError: list index out of range>

This also adds subdivision handling to transposes of
matrix_complex_ball_dense, matrix_gap, and fixes subdivisions for
transposes of empty matrices of type matrix_mod2_dense,
matrix_numpy_dense
    
URL: sagemath#40423
Reported by: Biffo89
Reviewer(s): Xavier Caruso
vbraun pushed a commit to vbraun/sage that referenced this pull request Jul 26, 2025
sagemathgh-40423: Fix GF(2) matrix transpose with subdivisions
    
<!-- ^ 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". -->



### 📝 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.
- [ ] 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.

A small bug I noticed when looking at the code for transposing matrices.
GF(2) does not properly transpose its subdivisions, meaning transposed
matrices can have incorrect subdivisions or cause errors if the indices
fall out of range.

sage: M = matrix.random(GF(2),3,3)
sage: N = matrix.block([[M,M,M]])
sage: M
[1 0 0]
[0 0 1]
[0 1 0]
sage: N
[1 0 0|1 0 0|1 0 0]
[0 0 1|0 0 1|0 0 1]
[0 1 0|0 1 0|0 1 0]
sage: N.transpose()
<repr(<sage.matrix.matrix_mod2_dense.Matrix_mod2_dense at
0x7f1eb7f0c1c0>) failed: IndexError: list index out of range>

This also adds subdivision handling to transposes of
matrix_complex_ball_dense, matrix_gap, and fixes subdivisions for
transposes of empty matrices of type matrix_mod2_dense,
matrix_numpy_dense
    
URL: sagemath#40423
Reported by: Biffo89
Reviewer(s): Xavier Caruso
vbraun pushed a commit to vbraun/sage that referenced this pull request Jul 27, 2025
sagemathgh-40423: Fix GF(2) matrix transpose with subdivisions
    
<!-- ^ 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". -->



### 📝 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.
- [ ] 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.

A small bug I noticed when looking at the code for transposing matrices.
GF(2) does not properly transpose its subdivisions, meaning transposed
matrices can have incorrect subdivisions or cause errors if the indices
fall out of range.

sage: M = matrix.random(GF(2),3,3)
sage: N = matrix.block([[M,M,M]])
sage: M
[1 0 0]
[0 0 1]
[0 1 0]
sage: N
[1 0 0|1 0 0|1 0 0]
[0 0 1|0 0 1|0 0 1]
[0 1 0|0 1 0|0 1 0]
sage: N.transpose()
<repr(<sage.matrix.matrix_mod2_dense.Matrix_mod2_dense at
0x7f1eb7f0c1c0>) failed: IndexError: list index out of range>

This also adds subdivision handling to transposes of
matrix_complex_ball_dense, matrix_gap, and fixes subdivisions for
transposes of empty matrices of type matrix_mod2_dense,
matrix_numpy_dense
    
URL: sagemath#40423
Reported by: Biffo89
Reviewer(s): Xavier Caruso
vbraun pushed a commit to vbraun/sage that referenced this pull request Jul 28, 2025
sagemathgh-40423: Fix GF(2) matrix transpose with subdivisions
    
<!-- ^ 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". -->



### 📝 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.
- [ ] 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.

A small bug I noticed when looking at the code for transposing matrices.
GF(2) does not properly transpose its subdivisions, meaning transposed
matrices can have incorrect subdivisions or cause errors if the indices
fall out of range.

sage: M = matrix.random(GF(2),3,3)
sage: N = matrix.block([[M,M,M]])
sage: M
[1 0 0]
[0 0 1]
[0 1 0]
sage: N
[1 0 0|1 0 0|1 0 0]
[0 0 1|0 0 1|0 0 1]
[0 1 0|0 1 0|0 1 0]
sage: N.transpose()
<repr(<sage.matrix.matrix_mod2_dense.Matrix_mod2_dense at
0x7f1eb7f0c1c0>) failed: IndexError: list index out of range>

This also adds subdivision handling to transposes of
matrix_complex_ball_dense, matrix_gap, and fixes subdivisions for
transposes of empty matrices of type matrix_mod2_dense,
matrix_numpy_dense
    
URL: sagemath#40423
Reported by: Biffo89
Reviewer(s): Xavier Caruso
vbraun pushed a commit to vbraun/sage that referenced this pull request Jul 29, 2025
sagemathgh-40423: Fix GF(2) matrix transpose with subdivisions
    
<!-- ^ 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". -->



### 📝 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.
- [ ] 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.

A small bug I noticed when looking at the code for transposing matrices.
GF(2) does not properly transpose its subdivisions, meaning transposed
matrices can have incorrect subdivisions or cause errors if the indices
fall out of range.

sage: M = matrix.random(GF(2),3,3)
sage: N = matrix.block([[M,M,M]])
sage: M
[1 0 0]
[0 0 1]
[0 1 0]
sage: N
[1 0 0|1 0 0|1 0 0]
[0 0 1|0 0 1|0 0 1]
[0 1 0|0 1 0|0 1 0]
sage: N.transpose()
<repr(<sage.matrix.matrix_mod2_dense.Matrix_mod2_dense at
0x7f1eb7f0c1c0>) failed: IndexError: list index out of range>

This also adds subdivision handling to transposes of
matrix_complex_ball_dense, matrix_gap, and fixes subdivisions for
transposes of empty matrices of type matrix_mod2_dense,
matrix_numpy_dense
    
URL: sagemath#40423
Reported by: Biffo89
Reviewer(s): Xavier Caruso
Biffo89 referenced this pull request in Biffo89/sage Jul 29, 2025
@vbraun vbraun merged commit 45e6ea4 into sagemath:develop Aug 2, 2025
20 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gsoc: 2025 Tag for GSoC2025 issues/PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants