Skip to content
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

Matrix construction over prime field fails for some types of inputs #36104

Closed
2 tasks done
vneiger opened this issue Aug 19, 2023 · 0 comments · Fixed by #39488
Closed
2 tasks done

Matrix construction over prime field fails for some types of inputs #36104

vneiger opened this issue Aug 19, 2023 · 0 comments · Fixed by #39488

Comments

@vneiger
Copy link
Contributor

vneiger commented Aug 19, 2023

Steps To Reproduce

The constructor for matrices of type matrix_modn_dense is supposed to accept a matrix of Python integers int. Yet it will fail with overflow if given integers that exceed the maximum value for a long.

Try the following example:

sage: x = int(2**128 + 1)
sage: mat = matrix(GF(9001), [[x]])

Expected Behavior

We expect that mat is constructed as an 1 x 1 matrix with single entry the element GF(9001)(x) (represented by 7633 in sage).

Actual Behavior

[...]
File src/sage/matrix/matrix_modn_dense_template.pxi:531, in sage.matrix.matrix_modn_dense_double.Matrix_modn_dense_template.__init__()
    529 x = next(it)
    530 if type(x) is int:
--> 531     tmp = (<long>x) % p
    532     v[j] = tmp + (tmp<0)*p
    533 elif type(x) is IntegerMod_int and (<IntegerMod_int>x)._parent is R:

OverflowError: Python int too large to convert to C long

Additional Information

We see in the error message that there is an attempt to convert x into a long, hence the overflow error.

Environment

- **OS**: ubuntu 22.04
- **Sage Version**: 10.1.rc0

Checklist

  • I have searched the existing issues for a bug report that matches the one I want to file, without success.
  • I have read the documentation and troubleshoot guide
@vneiger vneiger self-assigned this Aug 19, 2023
vbraun pushed a commit to vbraun/sage that referenced this issue Feb 11, 2025
sagemathgh-39488: Fix issue on matrix construction over integer mod ring for large coefficients
    
Fix Issue sagemath#36104.

In previous versions of Python, `int` size was limited and therefore
could be cast to `long`. With Python 3 this limit does not exists and
the code breaks with large integers. We modify the construction of
`Matrix_modn_dense_template`  from Python integers, directly using some
existing cast from Sage.

### 📝 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.
    
URL: sagemath#39488
Reported by: Hugo Passe
Reviewer(s): Vincent Neiger
vbraun pushed a commit to vbraun/sage that referenced this issue Feb 18, 2025
sagemathgh-39488: Fix issue on matrix construction over integer mod ring for large coefficients
    
Fixes sagemath#36104.

In previous versions of Python, `int` size was limited and therefore
could be cast to `long`. With Python 3 this limit does not exists and
the code breaks with large integers. We modify the construction of
`Matrix_modn_dense_template`  from Python integers, directly using some
existing cast from Sage.

### 📝 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.
    
URL: sagemath#39488
Reported by: Hugo Passe
Reviewer(s): Vincent Neiger
vbraun pushed a commit to vbraun/sage that referenced this issue Feb 21, 2025
sagemathgh-39488: Fix issue on matrix construction over integer mod ring for large coefficients
    
Fixes sagemath#36104.

In previous versions of Python, `int` size was limited and therefore
could be cast to `long`. With Python 3 this limit does not exists and
the code breaks with large integers. We modify the construction of
`Matrix_modn_dense_template`  from Python integers, directly using some
existing cast from Sage.

### 📝 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.
    
URL: sagemath#39488
Reported by: Hugo Passe
Reviewer(s): Vincent Neiger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant