Skip to content

Commit

Permalink
fq matrices, more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrik-johansson committed Jan 16, 2025
1 parent cffdfb6 commit 01dabdd
Show file tree
Hide file tree
Showing 50 changed files with 229 additions and 267 deletions.
26 changes: 13 additions & 13 deletions src/arith/test/t-stirling.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ TEST_FUNCTION_START(arith_stirling, state)
{
row = _fmpz_vec_init(k);
arith_stirling_number_1u_vec(row, n, k);
if (!_fmpz_vec_equal(row, mat->rows[n], k))
if (!_fmpz_vec_equal(row, fmpz_mat_row(mat, n), k))
{
flint_printf("stirling1u mat != vec ");
flint_printf("nn,n,k=%wd,%wd,%wd\n", nn, n, k);
flint_printf("mat: ");
_fmpz_vec_print(mat->rows[n], k);
_fmpz_vec_print(fmpz_mat_row(mat, n), k);
flint_printf("\nvec: ");
_fmpz_vec_print(row, k);
fflush(stdout);
Expand All @@ -169,12 +169,12 @@ TEST_FUNCTION_START(arith_stirling, state)
_fmpz_vec_clear(row, k);

arith_stirling_number_1u(s, n, k);
if (!fmpz_equal(mat->rows[n]+k, s))
if (!fmpz_equal(fmpz_mat_row(mat, n)+k, s))
{
flint_printf("stirling1u mat != single ");
flint_printf("nn,n,k=%wd,%wd,%wd\n", nn, n, k);
flint_printf("mat: ");
fmpz_print(mat->rows[n]+k);
fmpz_print(fmpz_mat_row(mat, n)+k);
flint_printf("\nsingle: ");
fmpz_print(s);
fflush(stdout);
Expand All @@ -198,12 +198,12 @@ TEST_FUNCTION_START(arith_stirling, state)
{
row = _fmpz_vec_init(k);
arith_stirling_number_1_vec(row, n, k);
if (!_fmpz_vec_equal(row, mat->rows[n], k))
if (!_fmpz_vec_equal(row, fmpz_mat_row(mat, n), k))
{
flint_printf("stirling1 mat != vec ");
flint_printf("nn,n,k=%wd,%wd,%wd\n", nn, n, k);
flint_printf("mat: ");
_fmpz_vec_print(mat->rows[n], k);
_fmpz_vec_print(fmpz_mat_row(mat, n), k);
flint_printf("\nvec: ");
_fmpz_vec_print(row, k);
fflush(stdout);
Expand All @@ -212,12 +212,12 @@ TEST_FUNCTION_START(arith_stirling, state)
_fmpz_vec_clear(row, k);

arith_stirling_number_1(s, n, k);
if (!fmpz_equal(mat->rows[n]+k, s))
if (!fmpz_equal(fmpz_mat_row(mat, n)+k, s))
{
flint_printf("stirling1 mat != single ");
flint_printf("nn,n,k=%wd,%wd,%wd\n", nn, n, k);
flint_printf("mat: ");
fmpz_print(mat->rows[n]+k);
fmpz_print(fmpz_mat_row(mat, n)+k);
flint_printf("\nsingle: ");
fmpz_print(s);
fflush(stdout);
Expand All @@ -241,12 +241,12 @@ TEST_FUNCTION_START(arith_stirling, state)
{
row = _fmpz_vec_init(k);
arith_stirling_number_2_vec(row, n, k);
if (!_fmpz_vec_equal(row, mat->rows[n], k))
if (!_fmpz_vec_equal(row, fmpz_mat_row(mat, n), k))
{
flint_printf("stirling2 mat != vec ");
flint_printf("nn,n,k=%wd,%wd,%wd\n", nn, n, k);
flint_printf("mat: ");
_fmpz_vec_print(mat->rows[n], k);
_fmpz_vec_print(fmpz_mat_row(mat, n), k);
flint_printf("\nvec: ");
_fmpz_vec_print(row, k);
fflush(stdout);
Expand All @@ -255,12 +255,12 @@ TEST_FUNCTION_START(arith_stirling, state)
_fmpz_vec_clear(row, k);

arith_stirling_number_2(s, n, k);
if (!fmpz_equal(mat->rows[n]+k, s))
if (!fmpz_equal(fmpz_mat_row(mat, n)+k, s))
{
flint_printf("stirling2 mat != single ");
flint_printf("nn,n,k=%wd,%wd,%wd\n", nn, n, k);
flint_printf("mat: ");
fmpz_print(mat->rows[n]+k);
fmpz_print(fmpz_mat_row(mat, n)+k);
flint_printf("\nsingle: ");
fmpz_print(s);
fflush(stdout);
Expand Down Expand Up @@ -289,7 +289,7 @@ TEST_FUNCTION_START(arith_stirling, state)
{
for (k = 0; k < nn; k++)
{
if (fmpz_get_ui(mat3->rows[n]+k) != (n == k))
if (fmpz_get_ui(fmpz_mat_row(mat3, n)+k) != (n == k))
{
flint_printf("not identity matrix: %wd, %wd, %wd\n", nn, n, k);
fflush(stdout);
Expand Down
57 changes: 36 additions & 21 deletions src/fmpz_mat/mul_blas.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ typedef struct {
slong Astoprow;
slong Bstartrow;
slong Bstoprow;
fmpz ** Arows;
fmpz ** Brows;
fmpz * Aentries;
slong Astride;
fmpz * Bentries;
slong Bstride;
double * dA;
double * dB;
} _red_worker_arg;
Expand All @@ -48,18 +50,20 @@ static void _red_worker(void * varg)
slong Astoprow = arg->Astoprow;
slong Bstartrow = arg->Bstartrow;
slong Bstoprow = arg->Bstoprow;
fmpz ** Arows = arg->Arows;
fmpz ** Brows = arg->Brows;
fmpz * Aentries = arg->Aentries;
slong Astride;
fmpz * Bentries = arg->Bentries;
slong Bstride;
double * dA = arg->dA;
double * dB = arg->dB;

for (i = Astartrow; i < Astoprow; i++)
for (j = 0; j < k; j++)
dA[k*i + j] = (double)(Arows[i][j]);
dA[k*i + j] = (double)(Aentries[i * Astride + j]);

for (i = Bstartrow; i < Bstoprow; i++)
for (j = 0; j < n; j++)
dB[n*i + j] = (double)(Brows[i][j]);
dB[n*i + j] = (double)(Bentries[i * Bstride + j]);
}

static int _fmpz_mat_mul_blas_direct(
Expand All @@ -85,8 +89,10 @@ static int _fmpz_mat_mul_blas_direct(
mainarg.m = m = A->r;
mainarg.k = k = A->c;
mainarg.n = n = B->c;
mainarg.Arows = A->rows;
mainarg.Brows = B->rows;
mainarg.Aentries = A->entries;
mainarg.Astride = A->stride;
mainarg.Bentries = B->entries;
mainarg.Bstride = B->stride;
mainarg.dA = dA;
mainarg.dB = dB;

Expand Down Expand Up @@ -140,7 +146,7 @@ static int _fmpz_mat_mul_blas_direct(

for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
fmpz_set_si(&C->rows[i][j], (slong)(dC[n*i + j]));
fmpz_set_si(fmpz_mat_entry(C, i, j), (slong)(dC[n*i + j]));

flint_free(dA);
flint_free(dB);
Expand Down Expand Up @@ -255,9 +261,12 @@ typedef struct {
double * dA;
double * dB;
double * dC;
fmpz ** Arows;
fmpz ** Brows;
fmpz ** Crows;
fmpz * Aentries;
slong Astride;
fmpz * Bentries;
slong Bstride;
fmpz * Centries;
slong Cstride;
const fmpz_comb_struct * comb;
int sign;
} _worker_arg;
Expand All @@ -275,8 +284,10 @@ static void _mod_worker(void * arg_ptr)
slong Bstoprow = arg->Bstoprow;
uint32_t * bigA = arg->bigA;
uint32_t * bigB = arg->bigB;
fmpz ** Arows = arg->Arows;
fmpz ** Brows = arg->Brows;
fmpz * Aentries = arg->Aentries;
slong Astride = arg->Astride;
fmpz * Bentries = arg->Bentries;
slong Bstride = arg->Bstride;
const fmpz_comb_struct * comb = arg->comb;
fmpz_comb_temp_t comb_temp;

Expand All @@ -285,12 +296,12 @@ static void _mod_worker(void * arg_ptr)
for (i = Astartrow; i < Astoprow; i++)
for (j = 0; j < k; j++)
fmpz_multi_mod_uint32_stride(bigA + i*k*num_primes + j, k,
&Arows[i][j], comb, comb_temp);
Aentries + i * Astride + j, comb, comb_temp);

for (i = Bstartrow; i < Bstoprow; i++)
for (j = 0; j < n; j++)
fmpz_multi_mod_uint32_stride(bigB + i*n*num_primes + j, n,
&Brows[i][j], comb, comb_temp);
Bentries + i * Bstride + j, comb, comb_temp);

fmpz_comb_temp_clear(comb_temp);
}
Expand Down Expand Up @@ -360,7 +371,8 @@ void _crt_worker(void * arg_ptr)
slong Cstartrow = arg->Cstartrow;
slong Cstoprow = arg->Cstoprow;
uint32_t * bigC = arg->bigC;
fmpz ** Crows = arg->Crows;
fmpz * Centries = arg->Centries;
slong Cstride = arg->Cstride;
const fmpz_comb_struct * comb = arg->comb;
fmpz_comb_temp_t comb_temp;
ulong * r;
Expand All @@ -376,7 +388,7 @@ void _crt_worker(void * arg_ptr)
for (k = 0; k < num_primes; k++)
r[k] = bigC[(i*num_primes + k)*n + j];

fmpz_multi_CRT_ui(&Crows[i][j], r, comb, comb_temp, sign);
fmpz_multi_CRT_ui(Centries + i * Cstride + j, r, comb, comb_temp, sign);
}
}

Expand Down Expand Up @@ -510,9 +522,12 @@ int _fmpz_mat_mul_blas(
args[i].bigA = bigA;
args[i].bigB = bigB;
args[i].bigC = bigC;
args[i].Arows = A->rows;
args[i].Brows = B->rows;
args[i].Crows = C->rows;
args[i].Aentries = A->entries;
args[i].Astride = A->stride;
args[i].Bentries = B->entries;
args[i].Bstride = B->stride;
args[i].Centries = C->entries;
args[i].Cstride = C->stride;
args[i].dA = dA;
args[i].dB = dB;
args[i].dC = dC;
Expand Down
14 changes: 6 additions & 8 deletions src/fmpz_mat/test/t-col_partition.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ TEST_FUNCTION_START(fmpz_mat_col_partition, state)

/* set first row */
for (j = 0; j < n; j++)
fmpz_randtest(A->rows[0] + j, state, 100);
fmpz_randtest(fmpz_mat_entry(A, 0, j), state, 100);
/* ensure row is distinct */
fmpz_set_ui(A->rows[0] + n_randint(state, n), 0);
fmpz_set_ui(fmpz_mat_entry(A, 0, n_randint(state, n)), 0);

/* fill remaining rows */
for (k = 1; k < m; k++)
Expand All @@ -45,15 +45,15 @@ TEST_FUNCTION_START(fmpz_mat_col_partition, state)
{
/* random row */
for (j = 0; j < n; j++)
fmpz_randtest(A->rows[k] + j, state, 100);
fmpz_randtest(fmpz_mat_entry(A, k, j), state, 100);
/* ensure row is distinct */
fmpz_set_ui(A->rows[k] + n_randint(state, n), k);
fmpz_set_ui(fmpz_mat_entry(A, k, n_randint(state, n)), k);
p1++;
} else
{
/* same as last row */
for (j = 0; j < n; j++)
fmpz_set(A->rows[k] + j, A->rows[k - 1] + j);
fmpz_set(fmpz_mat_entry(A, k, j), fmpz_mat_entry(A, k - 1, j));
}
}

Expand All @@ -62,9 +62,7 @@ TEST_FUNCTION_START(fmpz_mat_col_partition, state)
{
slong r1 = n_randint(state, m);
slong r2 = n_randint(state, m);
fmpz * t = A->rows[r1];
A->rows[r1] = A->rows[r2];
A->rows[r2] = t;
fmpz_mat_swap_rows(A, NULL, r1, r2);
}

/* transpose so rows are now columns */
Expand Down
4 changes: 2 additions & 2 deletions src/fmpz_mat/test/t-howell_form_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fmpz_mat_mod_is_in_howell_form(const fmpz_mat_t A, const fmpz_t mod)
{
fmpz_gcd(g, mod, fmpz_mat_entry(A, i, pivots[i]));
fmpz_divexact(g, mod, g);
_fmpz_vec_scalar_mul_fmpz(extra_row, A->rows[i], A->c, g);
_fmpz_vec_scalar_mul_fmpz(extra_row, fmpz_mat_row(A, i), A->c, g);
_fmpz_vec_scalar_mod_fmpz(extra_row, extra_row, A->c, mod);

for ( j = pivots[i] + 1; j < A->c; j++)
Expand All @@ -121,7 +121,7 @@ fmpz_mat_mod_is_in_howell_form(const fmpz_mat_t A, const fmpz_t mod)
{
fmpz_divexact(g, extra_row + j, fmpz_mat_entry(A, r, pivots[r]));
fmpz_neg(g, g);
_fmpz_vec_scalar_addmul_fmpz(extra_row, A->rows[r], A->c, g);
_fmpz_vec_scalar_addmul_fmpz(extra_row, fmpz_mat_row(A, r), A->c, g);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/fmpz_mat/test/t-init_clear.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ TEST_FUNCTION_START(fmpz_mat_init_clear, state)

for (j = 0; j < rows; j++)
for (k = 0; k < cols; k++)
fmpz_zero(a->rows[j] + k);
fmpz_zero(fmpz_mat_entry(a, j, k));

fmpz_mat_clear(a);
}
Expand Down
5 changes: 2 additions & 3 deletions src/fmpz_mat/test/t-inv.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ TEST_FUNCTION_START(fmpz_mat_inv, state)
{
fmpz_mat_t A, B, C, I;
fmpz_t den;
slong i, j, m, r;
slong i, m, r;

{
fmpz_t d;
Expand Down Expand Up @@ -50,8 +50,7 @@ TEST_FUNCTION_START(fmpz_mat_inv, state)
fmpz_mat_init(I, m, m);
fmpz_init(den);

for (j = 0; j < m; j++)
fmpz_set_ui(&I->rows[j][j], UWORD(1));
fmpz_mat_one(I);

/* Verify that A * A^-1 = I for random matrices */

Expand Down
8 changes: 4 additions & 4 deletions src/fmpz_mat/test/t-scalar_smod.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ TEST_FUNCTION_START(fmpz_mat_scalar_smod, state)
{
for (k = 0; k < A->c; k++)
{
fmpz_smod(c, A->rows[j] + k, P);
fmpz_smod(c, fmpz_mat_entry(A, j, k), P);

if (!fmpz_equal(c, B->rows[j] + k))
if (!fmpz_equal(c, fmpz_mat_entry(B, j, k)))
{
flint_printf("FAIL!\n");
flint_printf("%wd, %wd\n", j, k);
fmpz_print(c); flint_printf("\n");
fmpz_print(A->rows[j] + k); flint_printf("\n");
fmpz_print(B->rows[j] + k); flint_printf("\n");
fmpz_print(fmpz_mat_entry(A, j, k)); flint_printf("\n");
fmpz_print(fmpz_mat_entry(B, j, k)); flint_printf("\n");
fmpz_print(P); flint_printf("\n");
fflush(stdout);
flint_abort();
Expand Down
4 changes: 2 additions & 2 deletions src/fmpz_mod_mat/test/t-howell_form.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fmpz_mod_mat_is_in_howell_form(const fmpz_mod_mat_t A, const fmpz_mod_ctx_t ctx)
{
fmpz_gcd(g, ctx->n, fmpz_mod_mat_entry(A, i, pivots[i]));
fmpz_divexact(g, ctx->n, g);
_fmpz_vec_scalar_mul_fmpz(extra_row, A->rows[i], A->c, g);
_fmpz_vec_scalar_mul_fmpz(extra_row, fmpz_mod_mat_entry(A, i, 0), A->c, g);
_fmpz_vec_scalar_mod_fmpz(extra_row, extra_row, A->c, ctx->n);

for ( j = pivots[i] + 1; j < A->c; j++)
Expand All @@ -122,7 +122,7 @@ fmpz_mod_mat_is_in_howell_form(const fmpz_mod_mat_t A, const fmpz_mod_ctx_t ctx)
{
fmpz_divexact(g, extra_row + j, fmpz_mod_mat_entry(A, r, pivots[r]));
fmpz_neg(g, g);
_fmpz_vec_scalar_addmul_fmpz(extra_row, A->rows[r], A->c, g);
_fmpz_vec_scalar_addmul_fmpz(extra_row, fmpz_mod_mat_entry(A, r, 0), A->c, g);
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/fmpz_mod_mat/test/t-lu.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/

#include <string.h>
#include "test_helpers.h"
#include "fmpz.h"
#include "fmpz_mod.h"
Expand All @@ -18,17 +19,15 @@
void perm(fmpz_mod_mat_t A, slong * P)
{
slong i;
fmpz ** tmp;
fmpz * tmp;

if (A->c == 0 || A->r == 0)
return;

tmp = flint_malloc(sizeof(fmpz *) * A->r);
tmp = flint_malloc(sizeof(fmpz) * A->r * A->c);

for (i = 0; i < A->r; i++)
tmp[P[i]] = A->rows[i];
for (i = 0; i < A->r; i++)
A->rows[i] = tmp[i];
for (i = 0; i < A->r; i++) memcpy(tmp + P[i] * A->c, fmpz_mat_entry(A, i, 0), A->c * sizeof(fmpz));
for (i = 0; i < A->r; i++) memcpy(fmpz_mat_entry(A, i, 0), tmp + i * A->c, A->c * sizeof(fmpz));

flint_free(tmp);
}
Expand Down
Loading

0 comments on commit 01dabdd

Please sign in to comment.