Skip to content

Commit

Permalink
de-rowsize nmod_poly_mat, fmpz_poly_mat
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrik-johansson committed Jan 16, 2025
1 parent f207c1f commit 3824177
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/fmpz_poly_mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extern "C" {
FMPZ_POLY_MAT_INLINE
fmpz_poly_struct * fmpz_poly_mat_entry(const fmpz_poly_mat_t mat, slong i, slong j)
{
return mat->rows[i] + j;
return mat->entries + i * mat->stride + j;
}

FMPZ_POLY_MAT_INLINE
Expand Down
6 changes: 2 additions & 4 deletions src/fmpz_poly_mat/clear.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@
void
fmpz_poly_mat_clear(fmpz_poly_mat_t A)
{
if (A->entries)
if (A->entries != NULL)
{
slong i;

for (i = 0; i < A->r * A->c; i++)
fmpz_poly_clear(A->entries + i);

flint_free(A->entries);
flint_free(A->rows);
} else if (A->r != 0)
flint_free(A->rows);
}
}
8 changes: 3 additions & 5 deletions src/fmpz_poly_mat/fflu.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ fmpz_poly_mat_swap_rows(fmpz_poly_mat_t mat, slong * perm, slong r, slong s)
{
if (r != s)
{
fmpz_poly_struct * u;
slong t;
slong i, t;

if (perm)
{
Expand All @@ -29,9 +28,8 @@ fmpz_poly_mat_swap_rows(fmpz_poly_mat_t mat, slong * perm, slong r, slong s)
perm[r] = t;
}

u = mat->rows[s];
mat->rows[s] = mat->rows[r];
mat->rows[r] = u;
for (i = 0; i < mat->c; i++)
FLINT_SWAP(fmpz_poly_struct, *fmpz_poly_mat_entry(mat, r, i), *fmpz_poly_mat_entry(mat, s, i));
}
}

Expand Down
19 changes: 2 additions & 17 deletions src/fmpz_poly_mat/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ fmpz_poly_mat_init(fmpz_poly_mat_t A, slong rows, slong cols)
{
slong i;

if (rows != 0)
A->rows = flint_malloc(rows * sizeof(fmpz_poly_struct *));
else
A->rows = NULL;

A->entries = NULL;
A->r = rows;
A->c = cols;
A->stride = cols;

if (rows != 0 && cols != 0)
{
Expand All @@ -40,18 +37,6 @@ fmpz_poly_mat_init(fmpz_poly_mat_t A, slong rows, slong cols)

for (i = 0; i < rows * cols; i++)
fmpz_poly_init(A->entries + i);

for (i = 0; i < rows; i++)
A->rows[i] = A->entries + i * cols;
}
else
{
A->entries = NULL;
if (rows != 0)
{
for (i = 0; i < rows; i++)
A->rows[i] = NULL;
}
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/fmpz_poly_mat/nullspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fmpz_poly_mat_nullspace(fmpz_poly_mat_t res, const fmpz_poly_mat_t mat)
if (rank == 0)
{
for (i = 0; i < nullity; i++)
fmpz_poly_set_ui(res->rows[i] + i, UWORD(1));
fmpz_poly_one(fmpz_poly_mat_entry(res, i, i));
}
else if (nullity)
{
Expand All @@ -43,7 +43,7 @@ fmpz_poly_mat_nullspace(fmpz_poly_mat_t res, const fmpz_poly_mat_t mat)

for (i = j = k = 0; i < rank; i++)
{
while (fmpz_poly_is_zero(tmp->rows[i] + j))
while (fmpz_poly_is_zero(fmpz_poly_mat_entry(tmp, i, j)))
{
nonpivots[k] = j;
k++;
Expand All @@ -59,14 +59,14 @@ fmpz_poly_mat_nullspace(fmpz_poly_mat_t res, const fmpz_poly_mat_t mat)
j++;
}

fmpz_poly_set(den, tmp->rows[0] + pivots[0]);
fmpz_poly_set(den, fmpz_poly_mat_entry(tmp, 0, pivots[0]));

for (i = 0; i < nullity; i++)
{
for (j = 0; j < rank; j++)
fmpz_poly_set(res->rows[pivots[j]] + i,
tmp->rows[j] + nonpivots[i]);
fmpz_poly_neg(res->rows[nonpivots[i]] + i, den);
fmpz_poly_set(fmpz_poly_mat_entry(res, pivots[j], i),
fmpz_poly_mat_entry(tmp, j, nonpivots[i]));
fmpz_poly_neg(fmpz_poly_mat_entry(res, nonpivots[i], i), den);
}

flint_free(pivots);
Expand Down
26 changes: 5 additions & 21 deletions src/fmpz_poly_mat/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,16 @@ void
fmpz_poly_mat_window_init(fmpz_poly_mat_t window, const fmpz_poly_mat_t mat, slong r1,
slong c1, slong r2, slong c2)
{
slong i;
window->entries = NULL;

if (r2 > r1)
window->rows = (fmpz_poly_struct **) flint_malloc((r2 - r1)
* sizeof(fmpz_poly_struct *));
else
window->rows = NULL;

if (mat->c > 0)
{
for (i = 0; i < r2 - r1; i++)
window->rows[i] = mat->rows[r1 + i] + c1;
} else
{
for (i = 0; i < r2 - r1; i++)
window->rows[i] = NULL;
}
FLINT_ASSERT(r1 >= 0 && r1 <= r2 && r2 <= mat->r);
FLINT_ASSERT(c2 >= 0 && c1 <= c2 && c2 <= mat->c);

window->entries = fmpz_poly_mat_entry(mat, r1, c1);
window->r = r2 - r1;
window->c = c2 - c1;
window->stride = mat->stride;
}

void
fmpz_poly_mat_window_clear(fmpz_poly_mat_t window)
fmpz_poly_mat_window_clear(fmpz_poly_mat_t FLINT_UNUSED(window))
{
if (window->r != 0)
flint_free(window->rows);
}
2 changes: 1 addition & 1 deletion src/fmpz_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ typedef struct
fmpz_poly_struct * entries;
slong r;
slong c;
fmpz_poly_struct ** rows;
slong stride;
}
fmpz_poly_mat_struct;

Expand Down
2 changes: 1 addition & 1 deletion src/nmod_poly_mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
NMOD_POLY_MAT_INLINE
nmod_poly_struct * nmod_poly_mat_entry(const nmod_poly_mat_t mat, slong i, slong j)
{
return mat->rows[i] + j;
return mat->entries + i * mat->stride + j;
}

NMOD_POLY_MAT_INLINE slong
Expand Down
8 changes: 2 additions & 6 deletions src/nmod_poly_mat/clear.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@
void
nmod_poly_mat_clear(nmod_poly_mat_t A)
{
if (A->entries)
if (A->entries != NULL)
{
slong i;

for (i = 0; i < A->r * A->c; i++)
nmod_poly_clear(A->entries + i);

flint_free(A->entries);
flint_free(A->rows);
} else if (A->r != 0)
flint_free(A->rows);
}
}
8 changes: 3 additions & 5 deletions src/nmod_poly_mat/fflu.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ nmod_poly_mat_swap_rows(nmod_poly_mat_t mat, slong * perm, slong r, slong s)
{
if (r != s)
{
nmod_poly_struct * u;
slong t;
slong i, t;

if (perm)
{
Expand All @@ -29,9 +28,8 @@ nmod_poly_mat_swap_rows(nmod_poly_mat_t mat, slong * perm, slong r, slong s)
perm[r] = t;
}

u = mat->rows[s];
mat->rows[s] = mat->rows[r];
mat->rows[r] = u;
for (i = 0; i < mat->c; i++)
FLINT_SWAP(nmod_poly_struct, *nmod_poly_mat_entry(mat, r, i), *nmod_poly_mat_entry(mat, s, i));
}
}

Expand Down
21 changes: 3 additions & 18 deletions src/nmod_poly_mat/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ nmod_poly_mat_init(nmod_poly_mat_t A, slong rows, slong cols, ulong n)
{
slong i;

if (rows > 0)
A->rows = flint_malloc(rows * sizeof(nmod_poly_struct *));
else
A->rows = NULL;

A->modulus = n;
A->r = rows;
A->c = cols;
A->entries = NULL;
A->stride = cols;

if (rows > 0 && cols > 0)
if (rows != 0 && cols != 0)
{
slong num;
int of;
Expand All @@ -41,17 +38,5 @@ nmod_poly_mat_init(nmod_poly_mat_t A, slong rows, slong cols, ulong n)

for (i = 0; i < rows * cols; i++)
nmod_poly_init(A->entries + i, n);

for (i = 0; i < rows; i++)
A->rows[i] = A->entries + i * cols;
}
else
{
A->entries = NULL;
if (rows > 0)
{
for (i = 0; i < rows; i++)
A->rows[i] = NULL;
}
}
}
12 changes: 6 additions & 6 deletions src/nmod_poly_mat/nullspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ nmod_poly_mat_nullspace(nmod_poly_mat_t res, const nmod_poly_mat_t mat)
if (rank == 0)
{
for (i = 0; i < nullity; i++)
nmod_poly_one(res->rows[i] + i);
nmod_poly_one(nmod_poly_mat_entry(res, i, i));
}
else if (nullity)
{
Expand All @@ -42,7 +42,7 @@ nmod_poly_mat_nullspace(nmod_poly_mat_t res, const nmod_poly_mat_t mat)

for (i = j = k = 0; i < rank; i++)
{
while (nmod_poly_is_zero(tmp->rows[i] + j))
while (nmod_poly_is_zero(nmod_poly_mat_entry(tmp, i, j)))
{
nonpivots[k] = j;
k++;
Expand All @@ -58,14 +58,14 @@ nmod_poly_mat_nullspace(nmod_poly_mat_t res, const nmod_poly_mat_t mat)
j++;
}

nmod_poly_set(den, tmp->rows[0] + pivots[0]);
nmod_poly_set(den, nmod_poly_mat_entry(tmp, 0, pivots[0]));

for (i = 0; i < nullity; i++)
{
for (j = 0; j < rank; j++)
nmod_poly_set(res->rows[pivots[j]] + i,
tmp->rows[j] + nonpivots[i]);
nmod_poly_neg(res->rows[nonpivots[i]] + i, den);
nmod_poly_set(nmod_poly_mat_entry(res, pivots[j], i),
nmod_poly_mat_entry(tmp, j, nonpivots[i]));
nmod_poly_neg(nmod_poly_mat_entry(res, nonpivots[i], i), den);
}

flint_free(pivots);
Expand Down
22 changes: 5 additions & 17 deletions src/nmod_poly_mat/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,17 @@ void
nmod_poly_mat_window_init(nmod_poly_mat_t window, const nmod_poly_mat_t mat,
slong r1, slong c1, slong r2, slong c2)
{
slong i;
window->entries = NULL;

if (r2 > r1)
window->rows = flint_malloc((r2 - r1) * sizeof(nmod_poly_t));
else
window->rows = NULL;

if (mat->c > 0)
for (i = 0; i < r2 - r1; i++)
window->rows[i] = mat->rows[r1 + i] + c1;
else
for (i = 0; i < r2 - r1; i++)
window->rows[i] = NULL;
FLINT_ASSERT(r1 >= 0 && r1 <= r2 && r2 <= mat->r);
FLINT_ASSERT(c2 >= 0 && c1 <= c2 && c2 <= mat->c);

window->entries = nmod_poly_mat_entry(mat, r1, c1);
window->r = r2 - r1;
window->c = c2 - c1;
window->stride = mat->stride;
window->modulus = mat->modulus;
}

void
nmod_poly_mat_window_clear(nmod_poly_mat_t window)
nmod_poly_mat_window_clear(nmod_poly_mat_t FLINT_UNUSED(window))
{
if (window->r != 0)
flint_free(window->rows);
}
2 changes: 1 addition & 1 deletion src/nmod_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef struct
nmod_poly_struct * entries;
slong r;
slong c;
nmod_poly_struct ** rows;
slong stride;
ulong modulus;
}
nmod_poly_mat_struct;
Expand Down

0 comments on commit 3824177

Please sign in to comment.