sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.34k stars 452 forks source link

Create matrix class for FLINT's nmod_mat module #31548

Open roed314 opened 3 years ago

roed314 commented 3 years ago

This tickets adds a new class for dense matrices over Zmod(N) implemented using FLINT's nmod_mat_t type, along with some supporting ancillary changes:

Depends on #31069

CC: @edgarcosta

Component: linear algebra

Author: David Roe, Edgar Costa

Branch/Commit: u/roed/nmod_mat @ a3c8e38

Reviewer: Travis Scrimshaw

Issue created by migration from https://trac.sagemath.org/ticket/31548

roed314 commented 3 years ago

Branch: u/roed/nmod_mat

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

763533dFix bug in cinit
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Commit: 763533d

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 763533d to 8cdf9b1

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

8cdf9b1Add some functions to Matrix_nmod_dense
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 8cdf9b1 to 518a32f

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

9c6e9d3level 1 and level 2
518a32fUpdating todos
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

936668aAdd todos
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 518a32f to 936668a

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 936668a to aaf0680

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

aaf0680Update todo
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from aaf0680 to fb02349

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

fb02349Reorder todos
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from fb02349 to cecbe30

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

31d05e4inverses, swap rows and columns
d69baffrichmp
ba439fcMerge branch 'u/roed/nmod_mat' of git://trac.sagemath.org/sage into nmod_mat
9285ed3Merge branch 'u/roed/nmod_mat' of git://trac.sagemath.org/sage into nmod_mat
6a3d32ajustifying flags
a90423aMerge branch 'u/roed/nmod_mat' of git://trac.sagemath.org/sage into nmod_mat
c7c68cdsome progress
61821e8fixing various things
cecbe30Merge branch 'u/edgarcosta/nmod_mat' of git://trac.sagemath.org/sage into t/31548/nmod_mat
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from cecbe30 to db33b05

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

eb56a5eAdd all definitions to FLINT's ulong_extras, optimize matrix inversion, change default class for matrix mod n
db33b05Charpoly for Z/n
mkoeppe commented 3 years ago
comment:10

Sage development has entered the release candidate phase for 9.3. Setting a new milestone for this ticket based on a cursory review of ticket status, priority, and last modification date.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from db33b05 to 3eb1cd4

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. Last 10 new commits:

33dd395comment
b922d8eMerge branch 'u/roed/nmod_mat' of git://trac.sagemath.org/sage into nmod_mat
c1961a2right kernel matrix
b1d5396Merge branch 'u/roed/nmod_mat' of git://trac.sagemath.org/sage into nmod_mat
b9965beremove comment
1045207touch up right kernel
fb619b0work around echenolize
45de1c8remove stack
bf8aaabMerge branch 'u/edgarcosta/nmod_mat' of git://trac.sagemath.org/sage into t/31548/nmod_mat
3eb1cd4Fix import
roed314 commented 3 years ago

Dependencies: #31069

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

8c71a1c31069: flint version upgrade
a32be02Merge branch 'public/31069' of git://trac.sagemath.org/sage into t/31548/nmod_mat
aed2361Working on solve_right
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 3eb1cd4 to aed2361

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

8b37863Working on minpoly
1541005int64
8a4b815import
1f40b95int64
2d157e1Merge branch 'u/edgarcosta/nmod_mat' of git://trac.sagemath.org/sage into t/31548/nmod_mat
5e9b8a7Minpoly
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from aed2361 to 5e9b8a7

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

fc48ecaWorking on documentation
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 5e9b8a7 to fc48eca

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from fc48eca to 64e44fc

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

64e44fcFixing bugs and adding documentation to matrices mod n
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 64e44fc to e5c4c1e

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

e5c4c1eWorking on documentation
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from e5c4c1e to 1a7bb38

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

dad23f5Working on picking appropriate matrix types and fixing some bugs
1a7bb38Fixing some bugs
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 1a7bb38 to 6f41da9

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

6f41da9Fixing doctest problems from switching default implementation to flint for small matrices
roed314 commented 3 years ago
comment:20

Marking for patchbot to test....

roed314 commented 3 years ago

Author: David Roe, Edgar Costa

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 6f41da9 to 9748ffc

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

d0507c4Working on fixing reduction matrix bug
9748ffcAdd deprecation warning to _reduction_matrix
tscrim commented 3 years ago
comment:22

If you aren't yet ready for comments, I apologize. However, I made a quick skim over things before I realized you set this as needs review for the patchbot.

For def _change_implementation, wouldn't it make more sense to be cdef or cpdef since it should be called entirely from internal stuff? Also, in that implementation, I don't think we should use self.list() for spare matrices. I also generally find using sage.matrix.matrix_space.MatrixSpace unstable and think it is better to have a direct import.

I know you said this is for the patchbot, but it would be good to have doctests for _solve_right_modn. I would also add

cdef list X
cdef Py_ssize_t n

change

-            X = self.matrix_space(B.ncols(), self.ncols())(X)
-            return X.T
+            return self.matrix_space(B.ncols(), self.ncols())(X).T

(perhaps casting the result as a matrix too), and mark B as a Matrix.

I am not sure about the name of the files being matrix_nmod_dense and the subsequently the name of the class. It conflicts with the matrix_modn_* files and I feel it is too generic given that it is for a specific implementation.

Why is poly_crt in the matrix file?

can't -> cannot as we want to make the writing formal.

It would be good to implement a get_is_zero_unsafe method.

roed314 commented 3 years ago
comment:23

No problem! I'm still working on stuff, so comments are easily incorporated. I also realized that the patchbot won't test this since it relies on spkgs until #31069 is merged.

Do you think matrix_modn_flint and Matrix_modn_flint would be better?

The poly_crt function is left over from an earlier implementation of charpoly or minpoly (I forget which). I will take care of it.

Thanks!

tscrim commented 3 years ago
comment:24

Replying to @roed314:

No problem! I'm still working on stuff, so comments are easily incorporated. I also realized that the patchbot won't test this since it relies on spkgs until #31069 is merged.

Yea, I didn't quite realize that either. ^^;;

Do you think matrix_modn_flint and Matrix_modn_flint would be better?

The former for the name of the file, the latter for the name of the class considering the other matrix classes in Sage.

Let me know if there is anything I can do to help too.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

cd1b853Merge branch 'u/roed/nmod_mat' of trac.sagemath.org:sage into t/31548/nmod_mat
6e2b75aMerge branch 'u/roed/nmod_mat' of trac.sagemath.org:sage into t/31548/nmod_mat
ee10b06Working on documentation and fixing tests
465f51dWorking on documentation
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 9748ffc to 465f51d

roed314 commented 3 years ago
comment:26

Replying to @tscrim:

For def _change_implementation, wouldn't it make more sense to be cdef or cpdef since it should be called entirely from internal stuff? Also, in that implementation, I don't think we should use self.list() for spare matrices. I also generally find using sage.matrix.matrix_space.MatrixSpace unstable and think it is better to have a direct import.

Even for a 2 by 2 matrix where the calling overhead is most substantial, changing to cpdef makes a negligible difference:

sage: A = random_matrix(Zmod(120), 2)                                                                                                               
sage: A                                                                                                                                             
[ 32 108]
[  2 112]
sage: %time B = A._change_implementation("linbox") # using def                                                                                                
CPU times: user 339 µs, sys: 8 µs, total: 347 µs
Wall time: 350 µs
sage: A = random_matrix(Zmod(120), 2) # using cpdef                                                                                                                                                                                                                       
sage: %time B = A._change_implementation("linbox")                                                                                                                                                                                                            
CPU times: user 334 µs, sys: 5 µs, total: 339 µs
Wall time: 343 µs

I don't think it's worth the cost of recompiling everything depending on matrix0.pxd, and the risk of developers accidentally using def instead of cpdef on a matrix class in the future.

I agree about sparse matrices, but currently in sage.matrix.matrix_space.get_matrix_class we don't allow the implementation keyword for sparse matrices, so the issue is moot. I'd be happy to have more sparse matrices in Sage eventually, but for now there's no way to even test a version of _change_implementation in Matrix_sparse.

I'm not sure what you mean by using sage.matrix.matrix_space.MatrixSpace unstable. Do you mean switching to from sage.matrix.matrix_space import MatrixSpace? I was just copying what's done a couple dozen lines above in matrix2.pyx, but I'm happy to make this change if desired.

I know you said this is for the patchbot, but it would be good to have doctests for _solve_right_modn.

Agreed. I've added them.

I would also add

cdef list X
cdef Py_ssize_t n

change

-            X = self.matrix_space(B.ncols(), self.ncols())(X)
-            return X.T
+            return self.matrix_space(B.ncols(), self.ncols())(X).T

(perhaps casting the result as a matrix too), and mark B as a Matrix.

I've added the cdef Py_ssize_t n since that can speed up the loop, and made the change you suggested in the diff. I don't think it's necessary to mark X as a list or B as a matrix, since I'm not calling any functions that will benefit from knowing the types (and the runtime is surely dominated by the call to Pari's matsolvemod)

I am not sure about the name of the files being matrix_nmod_dense and the subsequently the name of the class. It conflicts with the matrix_modn_* files and I feel it is too generic given that it is for a specific implementation.

Agreed. I've changed it to matrix_modn_dense_flint.

Why is poly_crt in the matrix file?

I've created #31731 and removed it from this ticket.

can't -> cannot as we want to make the writing formal.

It would be good to implement a get_is_zero_unsafe method.

Both done.

roed314 commented 3 years ago
comment:27

All tests pass in matrix modules rings modular crypto. I'm happy to run tests on all of Sage if people are happy with the changes in principle.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

760e0c9Small fixes
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 465f51d to 760e0c9

roed314 commented 3 years ago
comment:29

(I accidentally made a comment instead of editing the ticket description)


New commits:

760e0c9Small fixes
roed314 commented 3 years ago
comment:30

This ticket is now actually ready for review, though the patchbot won't run currently (since it depends on #31069 which changes spkgs).