Skip to content

Commit 9cb68f9

Browse files
Merge pull request #1754 from flintlib/fmpz_mod2
Have fmpz_mod_mat use a context object
2 parents ab28e24 + 9c26b04 commit 9cb68f9

File tree

123 files changed

+1278
-1833
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+1278
-1833
lines changed

doc/source/fmpz_mod_mat.rst

Lines changed: 55 additions & 53 deletions
Large diffs are not rendered by default.

doc/source/fq_default_mat.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ Random matrix generation
207207
The matrix can be transformed into a dense matrix with unchanged
208208
rank by subsequently calling :func:`fq_default_mat_randops`.
209209

210-
.. function:: void fq_default_mat_randops(fq_default_mat_t mat, slong count, flint_rand_t state, const fq_default_ctx_t ctx)
210+
.. function:: void fq_default_mat_randops(fq_default_mat_t mat, flint_rand_t state, slong count, const fq_default_ctx_t ctx)
211211

212212
Randomises ``mat`` by performing elementary row or column
213213
operations. More precisely, at most ``count`` random additions

doc/source/fq_mat.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ Random matrix generation
204204
The matrix can be transformed into a dense matrix with unchanged
205205
rank by subsequently calling :func:`fq_mat_randops`.
206206

207-
.. function:: void fq_mat_randops(fq_mat_t mat, slong count, flint_rand_t state, const fq_ctx_t ctx)
207+
.. function:: void fq_mat_randops(fq_mat_t mat, flint_rand_t state, slong count, const fq_ctx_t ctx)
208208

209209
Randomises ``mat`` by performing elementary row or column
210210
operations. More precisely, at most ``count`` random additions

doc/source/fq_nmod_mat.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ Random matrix generation
203203
The matrix can be transformed into a dense matrix with unchanged
204204
rank by subsequently calling :func:`fq_nmod_mat_randops`.
205205

206-
.. function:: void fq_nmod_mat_randops(fq_nmod_mat_t mat, slong count, flint_rand_t state, const fq_nmod_ctx_t ctx)
206+
.. function:: void fq_nmod_mat_randops(fq_nmod_mat_t mat, flint_rand_t state, slong count, const fq_nmod_ctx_t ctx)
207207

208208
Randomises ``mat`` by performing elementary row or column
209209
operations. More precisely, at most ``count`` random additions

doc/source/fq_zech_mat.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ Random matrix generation
181181
The matrix can be transformed into a dense matrix with unchanged
182182
rank by subsequently calling :func:`fq_zech_mat_randops`.
183183

184-
.. function:: void fq_zech_mat_randops(fq_zech_mat_t mat, slong count, flint_rand_t state, const fq_zech_ctx_t ctx)
184+
.. function:: void fq_zech_mat_randops(fq_zech_mat_t mat, flint_rand_t state, slong count, const fq_zech_ctx_t ctx)
185185

186186
Randomises ``mat`` by performing elementary row or column
187187
operations. More precisely, at most ``count`` random additions

doc/source/nmod_mat.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ Random matrix generation
213213
The matrix can be transformed into a dense matrix with unchanged
214214
rank by subsequently calling :func:`nmod_mat_randops`.
215215

216-
.. function:: void nmod_mat_randops(nmod_mat_t mat, slong count, flint_rand_t state)
216+
.. function:: void nmod_mat_randops(nmod_mat_t mat, flint_rand_t state, slong count)
217217

218218
Randomises ``mat`` by performing elementary row or column
219219
operations. More precisely, at most ``count`` random additions

src/fmpz_mod_mat.h

Lines changed: 88 additions & 116 deletions
Large diffs are not rendered by default.

src/fmpz_mod_mat/add.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
Copyright (C) 2017 Luca De Feo
3+
Copyright (C) 2024 Fredrik Johansson
34
45
This file is part of FLINT.
56
@@ -9,10 +10,16 @@
910
(at your option) any later version. See <https://www.gnu.org/licenses/>.
1011
*/
1112

13+
#include "fmpz_mod_vec.h"
1214
#include "fmpz_mod_mat.h"
1315

14-
void fmpz_mod_mat_add(fmpz_mod_mat_t C, const fmpz_mod_mat_t A, const fmpz_mod_mat_t B)
16+
void fmpz_mod_mat_add(fmpz_mod_mat_t C, const fmpz_mod_mat_t A, const fmpz_mod_mat_t B, const fmpz_mod_ctx_t ctx)
1517
{
16-
fmpz_mat_add(C->mat, A->mat, B->mat);
17-
_fmpz_mod_mat_reduce(C);
18+
slong i;
19+
slong r = fmpz_mod_mat_nrows(A, ctx);
20+
slong c = fmpz_mod_mat_ncols(A, ctx);
21+
22+
if (c != 0)
23+
for (i = 0; i < r; i++)
24+
_fmpz_mod_vec_add(C->rows[i], A->rows[i], B->rows[i], c, ctx);
1825
}

src/fmpz_mod_mat/can_solve.c

Lines changed: 47 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Copyright (C) 2018 Tommy Hofmann
33
Copyright (C) 2020 William Hart
44
Copyright (C) 2021 Daniel Schultz
5+
Copyright (C) 2024 Fredrik Johansson
56
67
This file is part of FLINT.
78
@@ -16,43 +17,41 @@
1617
#include "fmpz_mod_mat.h"
1718

1819
int fmpz_mod_mat_can_solve(fmpz_mod_mat_t X, const fmpz_mod_mat_t A,
19-
const fmpz_mod_mat_t B)
20+
const fmpz_mod_mat_t B, const fmpz_mod_ctx_t ctx)
2021
{
2122
slong i, j, k, col, *pivots, rank, *perm;
2223
fmpz_mod_mat_t LU, LU2, PB;
2324
int result = 1;
2425

25-
if (A->mat->r != B->mat->r || A->mat->c != X->mat->r || X->mat->c != B->mat->c)
26+
if (A->r != B->r || A->c != X->r || X->c != B->c)
2627
{
2728
return 0;
2829
}
2930

30-
if (A->mat->r == 0 || B->mat->c == 0)
31+
if (A->r == 0 || B->c == 0)
3132
{
32-
fmpz_mod_mat_zero(X);
33-
33+
fmpz_mod_mat_zero(X, ctx);
3434
return 1;
3535
}
3636

37-
if (A->mat->c == 0)
37+
if (A->c == 0)
3838
{
39-
fmpz_mod_mat_zero(X);
40-
41-
return fmpz_mod_mat_is_zero(B);
39+
fmpz_mod_mat_zero(X, ctx);
40+
return fmpz_mod_mat_is_zero(B, ctx);
4241
}
4342

44-
fmpz_mod_mat_init_set(LU, A);
45-
perm = flint_malloc(sizeof(slong) * A->mat->r);
46-
for (i = 0; i < A->mat->r; i++)
43+
fmpz_mod_mat_init_set(LU, A, ctx);
44+
perm = flint_malloc(sizeof(slong) * A->r);
45+
for (i = 0; i < A->r; i++)
4746
perm[i] = i;
4847

49-
rank = fmpz_mod_mat_lu(perm, LU, 0);
48+
rank = fmpz_mod_mat_lu(perm, LU, 0, ctx);
5049

51-
fmpz_mod_mat_window_init(PB, B, 0, 0, B->mat->r, B->mat->c);
52-
for (i = 0; i < B->mat->r; i++)
53-
PB->mat->rows[i] = B->mat->rows[perm[i]];
50+
fmpz_mod_mat_window_init(PB, B, 0, 0, B->r, B->c, ctx);
51+
for (i = 0; i < B->r; i++)
52+
PB->rows[i] = B->rows[perm[i]];
5453

55-
fmpz_mod_mat_init(LU2, rank, rank, A->mod);
54+
fmpz_mod_mat_init(LU2, rank, rank, ctx);
5655

5756
pivots = flint_malloc(sizeof(slong)*rank);
5857

@@ -65,77 +64,77 @@ int fmpz_mod_mat_can_solve(fmpz_mod_mat_t X, const fmpz_mod_mat_t A,
6564
pivots[i] = col;
6665

6766
for (j = 0; j < rank; j++)
68-
fmpz_mod_mat_set_entry(LU2, j, i, fmpz_mod_mat_entry(LU, j, col));
67+
fmpz_mod_mat_set_entry(LU2, j, i, fmpz_mod_mat_entry(LU, j, col), ctx);
6968

7069
col++;
7170
}
7271

73-
X->mat->r = rank;
74-
PB->mat->r = rank;
75-
LU->mat->r = rank;
76-
fmpz_mod_mat_solve_tril(X, LU, PB, 1);
77-
LU->mat->r = A->mat->r;
72+
X->r = rank;
73+
PB->r = rank;
74+
LU->r = rank;
75+
fmpz_mod_mat_solve_tril(X, LU, PB, 1, ctx);
76+
LU->r = A->r;
7877

79-
if (A->mat->r > rank)
78+
if (A->r > rank)
8079
{
8180
fmpz_mod_mat_t P;
8281

83-
LU->mat->rows += rank;
84-
LU->mat->r = A->mat->r - rank;
85-
X->mat->r = LU->mat->c;
82+
LU->rows += rank;
83+
LU->r = A->r - rank;
84+
X->r = LU->c;
8685

87-
fmpz_mod_mat_init(P, LU->mat->r, B->mat->c, A->mod);
86+
fmpz_mod_mat_init(P, LU->r, B->c, ctx);
8887

89-
fmpz_mod_mat_mul(P, LU, X);
88+
fmpz_mod_mat_mul(P, LU, X, ctx);
9089

91-
PB->mat->r = LU->mat->r;
92-
PB->mat->rows += rank;
90+
PB->r = LU->r;
91+
PB->rows += rank;
9392

94-
result = fmpz_mod_mat_equal(P, PB);
93+
result = fmpz_mod_mat_equal(P, PB, ctx);
9594

96-
PB->mat->rows -= rank;
97-
fmpz_mod_mat_clear(P);
95+
PB->rows -= rank;
96+
fmpz_mod_mat_clear(P, ctx);
9897

99-
LU->mat->rows -= rank;
98+
LU->rows -= rank;
10099

101100
if (!result)
102101
{
103-
X->mat->r = A->mat->c;
104-
fmpz_mod_mat_zero(X);
102+
X->r = A->c;
103+
fmpz_mod_mat_zero(X, ctx);
105104
goto cleanup;
106105
}
107106
}
108107

109-
fmpz_mod_mat_solve_triu(X, LU2, X, 0);
108+
fmpz_mod_mat_solve_triu(X, LU2, X, 0, ctx);
110109

111-
X->mat->r = A->mat->c;
110+
X->r = A->c;
112111

113112
k = rank - 1;
114-
for (i = A->mat->c - 1; i >= 0; i--)
113+
for (i = A->c - 1; i >= 0; i--)
115114
{
116115
if (k < 0 || i != pivots[k])
117116
{
118-
for (j = 0; j < B->mat->c; j++)
117+
for (j = 0; j < B->c; j++)
119118
fmpz_zero(fmpz_mod_mat_entry(X, i, j));
120119
}
121120
else
122121
{
123-
for (j = 0; j < B->mat->c; j++)
124-
fmpz_mod_mat_set_entry(X, i, j, fmpz_mod_mat_entry(X, k, j));
122+
for (j = 0; j < B->c; j++)
123+
fmpz_mod_mat_set_entry(X, i, j, fmpz_mod_mat_entry(X, k, j), ctx);
125124

126125
k--;
127126
}
128127
}
129128

130129
cleanup:
131130

132-
fmpz_mod_mat_clear(LU2);
131+
fmpz_mod_mat_clear(LU2, ctx);
133132

134-
PB->mat->r = B->mat->r;
135-
fmpz_mod_mat_window_clear(PB);
133+
PB->r = B->r;
134+
fmpz_mod_mat_window_clear(PB, ctx);
136135

137-
LU->mat->r = A->mat->r;
138-
fmpz_mod_mat_clear(LU);
136+
LU->r = A->r;
137+
fmpz_mod_mat_clear(LU, ctx);
139138
flint_free(perm);
140139

141140
flint_free(pivots);

src/fmpz_mod_mat/charpoly_berkowitz.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ void _fmpz_mod_mat_charpoly_berkowitz(fmpz* cp, const fmpz_mod_mat_t mat,
2929
void fmpz_mod_mat_charpoly_berkowitz(fmpz_mod_poly_t cp,
3030
const fmpz_mod_mat_t mat, const fmpz_mod_ctx_t ctx)
3131
{
32-
if (!fmpz_mod_mat_is_square(mat))
32+
if (!fmpz_mod_mat_is_square(mat, ctx))
3333
{
3434
flint_throw(FLINT_ERROR, "Exception (fmpz_mod_mat_charpoly_berkowitz). Non-square matrix.\n");
3535
}
3636

37-
fmpz_mod_poly_fit_length(cp, fmpz_mod_mat_nrows(mat) + 1, ctx);
37+
fmpz_mod_poly_fit_length(cp, fmpz_mod_mat_nrows(mat, ctx) + 1, ctx);
3838
_fmpz_mod_mat_charpoly_berkowitz(cp->coeffs, mat, ctx);
39-
_fmpz_mod_poly_set_length(cp, fmpz_mod_mat_nrows(mat) + 1);
39+
_fmpz_mod_poly_set_length(cp, fmpz_mod_mat_nrows(mat, ctx) + 1);
4040
_fmpz_mod_poly_normalise(cp);
4141
}

0 commit comments

Comments
 (0)