Skip to content

Commit f1eaa53

Browse files
Merge pull request #393 from oscarbenjamin/pr_fmpz_mod_mat_det
Use fmpz_mod_mat in FLINT >= 3.1.0
2 parents d521145 + d676b9e commit f1eaa53

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

src/flint/flintlib/types/fmpz_mod_mat_compat.pxd

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ from flint.flintlib.types.fmpz_mod cimport fmpz_mod_ctx_t, fmpz_mod_mat_t, fmpz_
33

44
cdef extern from *:
55
"""
6+
#include <flint/fmpz_mod.h>
7+
#include <flint/fmpz_mod_poly.h>
8+
69
/*
710
* fmpz_mod_mat function signatures were changed in FLINT 3.1.0
811
*/
@@ -28,6 +31,7 @@ cdef extern from *:
2831
#define compat_fmpz_mod_mat_transpose(B, A, ctx) fmpz_mod_mat_transpose(B, A, ctx)
2932
#define compat_fmpz_mod_mat_solve(X, A, B, ctx) fmpz_mod_mat_solve(X, A, B, ctx)
3033
#define compat_fmpz_mod_mat_rref(mat, ctx) fmpz_mod_mat_rref(mat, mat, ctx)
34+
#define compat_fmpz_mod_mat_det(res, mat, ctx) fmpz_mod_mat_det(res, mat, ctx)
3135
#define compat_fmpz_mod_mat_charpoly(p, M, ctx) fmpz_mod_mat_charpoly(p, M, ctx)
3236
#define compat_fmpz_mod_mat_minpoly(p, M, ctx) fmpz_mod_mat_minpoly(p, M, ctx)
3337
@@ -53,9 +57,26 @@ cdef extern from *:
5357
#define compat_fmpz_mod_mat_transpose(B, A, ctx) fmpz_mod_mat_transpose(B, A)
5458
#define compat_fmpz_mod_mat_solve(X, A, B, ctx) fmpz_mod_mat_solve(X, A, B)
5559
#define compat_fmpz_mod_mat_rref(mat, ctx) fmpz_mod_mat_rref(NULL, mat)
60+
#define compat_fmpz_mod_mat_det(res, mat, ctx) compat_fmpz_mod_mat_det_fallback(res, mat, ctx)
5661
#define compat_fmpz_mod_mat_charpoly(p, M, ctx) fmpz_mod_mat_charpoly(p, M, ctx)
5762
#define compat_fmpz_mod_mat_minpoly(p, M, ctx) fmpz_mod_mat_minpoly(p, M, ctx)
5863
64+
/* fmpz_mod_mat_det was added in FLINT 3.1.0 */
65+
static inline void
66+
compat_fmpz_mod_mat_det_fallback(fmpz_t res, const fmpz_mod_mat_t mat, const fmpz_mod_ctx_t ctx)
67+
{
68+
fmpz_mod_poly_t p;
69+
70+
fmpz_mod_poly_init(p, ctx);
71+
fmpz_mod_mat_charpoly(p, mat, ctx);
72+
fmpz_mod_poly_get_coeff_fmpz(res, p, 0, ctx);
73+
74+
if (fmpz_mod_mat_nrows(mat) % 2)
75+
fmpz_mod_neg(res, res, ctx);
76+
77+
fmpz_mod_poly_clear(p, ctx);
78+
}
79+
5980
#endif
6081
"""
6182

@@ -81,6 +102,7 @@ cdef extern from "flint/fmpz_mod_mat.h":
81102
void compat_fmpz_mod_mat_transpose(fmpz_mod_mat_t B, const fmpz_mod_mat_t A, const fmpz_mod_ctx_t ctx)
82103
int compat_fmpz_mod_mat_solve(fmpz_mod_mat_t X, const fmpz_mod_mat_t A, const fmpz_mod_mat_t B, const fmpz_mod_ctx_t ctx)
83104
slong compat_fmpz_mod_mat_rref(fmpz_mod_mat_t mat, const fmpz_mod_ctx_t ctx)
105+
void compat_fmpz_mod_mat_det(fmpz_t res, const fmpz_mod_mat_t mat, const fmpz_mod_ctx_t ctx)
84106
void compat_fmpz_mod_mat_charpoly(fmpz_mod_poly_t p, const fmpz_mod_mat_t M, const fmpz_mod_ctx_t ctx)
85107
void compat_fmpz_mod_mat_minpoly(fmpz_mod_poly_t p, const fmpz_mod_mat_t M, const fmpz_mod_ctx_t ctx)
86108
#

src/flint/types/fmpz_mod_mat.pyx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ from flint.flintlib.types.fmpz_mod_mat_compat cimport (
2828
compat_fmpz_mod_mat_transpose,
2929
compat_fmpz_mod_mat_solve,
3030
compat_fmpz_mod_mat_rref,
31+
compat_fmpz_mod_mat_det,
3132
compat_fmpz_mod_mat_charpoly,
3233
compat_fmpz_mod_mat_minpoly,
3334
)
@@ -501,12 +502,13 @@ cdef class fmpz_mod_mat(flint_mat):
501502
fmpz_mod(5, 7)
502503
503504
"""
504-
# XXX: No fmpz_mod_mat_det function...
505-
p = self.charpoly()
506-
p0 = p[0]
507-
if self.nrows() % 2:
508-
p0 = -p0
509-
return p0
505+
cdef fmpz_mod d
506+
if self.nrows() != self.ncols():
507+
raise ValueError("fmpz_mod_mat det: matrix must be square")
508+
d = fmpz_mod.__new__(fmpz_mod)
509+
d.ctx = self.ctx
510+
compat_fmpz_mod_mat_det(d.val, self.val, self.ctx.val)
511+
return d
510512

511513
def charpoly(self):
512514
"""Return the characteristic polynomial of a matrix.

0 commit comments

Comments
 (0)