Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have fmpz_mod_mat use a context object #1754

Merged
merged 5 commits into from
Jan 28, 2024
Merged

Have fmpz_mod_mat use a context object #1754

merged 5 commits into from
Jan 28, 2024

Conversation

fredrik-johansson
Copy link
Collaborator

@fredrik-johansson fredrik-johansson commented Jan 28, 2024

Plus miscellaneous cleanup and optimizations.

@fredrik-johansson fredrik-johansson merged commit 9cb68f9 into main Jan 28, 2024
16 of 17 checks passed
@fredrik-johansson fredrik-johansson deleted the fmpz_mod2 branch January 30, 2024 09:29
@oscarbenjamin
Copy link
Contributor

To anyone looking to adapt their code for this change it is mostly just a case of changing e.g. fmpz_mod_mat_add(A, B) to fmpz_mod_mat_add(A, B, ctx) but there are a few notable exceptions:

  1. fmpz_mod_mat_init needs a fmpz_mod_ctx rather than an fmpz_mod.
  2. The undocumented fmpz_mod_mat_equal(A, B) is not quite equivalent to fmpz_mod_mat_equal(A, B, ctx) because the former compares the moduli whereas the latter does not.
  3. The fmpz_mod_mat_rref function is changed from previously fmpz_mod_mat_rref(perm, A) to fmpz_mod_mat_rref(B, A, ctx). Previously it could be used like fmpz_mod_mat_rref(NULL, A). Changing that to fmpz_mod_mat_rref(NULL, A, ctx) will compile but will segfault at runtime. The proper equivalent is fmpz_mod_mat_rref(A, A, ctx).

I ended up with these cross-version compatible macros:

    /*
     * fmpz_mod_mat function signatures were changed in FLINT 3.1.0
     */
    #if __FLINT_RELEASE >= 30100 /* Flint 3.1.0 or later */
    #define compat_fmpz_mod_mat_init(mat, rows, cols, ctx) fmpz_mod_mat_init(mat, rows, cols, ctx)
    #define compat_fmpz_mod_mat_init_set(mat, src, ctx) fmpz_mod_mat_init_set(mat, src, ctx)
    #define compat_fmpz_mod_mat_clear(mat, ctx) fmpz_mod_mat_clear(mat, ctx)
    #define compat_fmpz_mod_mat_set(A, B, ctx) fmpz_mod_mat_set(A, B, ctx)
    #define compat_fmpz_mod_mat_nrows(mat, ctx) fmpz_mod_mat_nrows(mat, ctx)
    #define compat_fmpz_mod_mat_ncols(mat, ctx) fmpz_mod_mat_ncols(mat, ctx)
    #define compat_fmpz_mod_mat_entry(mat, i, j) fmpz_mod_mat_entry(mat, i, j)
    #define compat_fmpz_mod_mat_set_entry(mat, i, j, val, ctx) fmpz_mod_mat_set_entry(mat, i, j, val, ctx)
    #define compat_fmpz_mod_mat_one(mat, ctx) fmpz_mod_mat_one(mat, ctx)
    #define compat_fmpz_mod_mat_equal(mat1, mat2, ctx) fmpz_mod_mat_equal(mat1, mat2, ctx)
    #define compat_fmpz_mod_mat_is_zero(mat, ctx) fmpz_mod_mat_is_zero(mat, ctx)
    #define compat_fmpz_mod_mat_neg(B, A, ctx) fmpz_mod_mat_neg(B, A, ctx)
    #define compat_fmpz_mod_mat_add(C, A, B, ctx) fmpz_mod_mat_add(C, A, B, ctx)
    #define compat_fmpz_mod_mat_sub(C, A, B, ctx) fmpz_mod_mat_sub(C, A, B, ctx)
    #define compat_fmpz_mod_mat_scalar_mul_fmpz(B, A, c, ctx) fmpz_mod_mat_scalar_mul_fmpz(B, A, c, ctx)
    #define compat_fmpz_mod_mat_mul(C, A, B, ctx) fmpz_mod_mat_mul(C, A, B, ctx)
    #define compat_fmpz_mod_mat_inv(B, A, ctx) fmpz_mod_mat_inv(B, A, ctx)
    #define compat_fmpz_mod_mat_transpose(B, A, ctx) fmpz_mod_mat_transpose(B, A, ctx)
    #define compat_fmpz_mod_mat_solve(X, A, B, ctx) fmpz_mod_mat_solve(X, A, B, ctx)
    #define compat_fmpz_mod_mat_rref(mat, ctx) fmpz_mod_mat_rref(mat, mat, ctx)
    #define compat_fmpz_mod_mat_charpoly(p, M, ctx) fmpz_mod_mat_charpoly(p, M, ctx)
    #define compat_fmpz_mod_mat_minpoly(p, M, ctx) fmpz_mod_mat_minpoly(p, M, ctx)
    #else /* Flint 3.0.0 or 3.0.1 */
    #define compat_fmpz_mod_mat_init(mat, rows, cols, ctx) fmpz_mod_mat_init(mat, rows, cols, ctx->n)
    #define compat_fmpz_mod_mat_init_set(mat, src, ctx) fmpz_mod_mat_init_set(mat, src)
    #define compat_fmpz_mod_mat_clear(mat, ctx) fmpz_mod_mat_clear(mat)
    #define compat_fmpz_mod_mat_set(A, B, ctx) fmpz_mod_mat_set(A, B)
    #define compat_fmpz_mod_mat_nrows(mat, ctx) fmpz_mod_mat_nrows(mat)
    #define compat_fmpz_mod_mat_ncols(mat, ctx) fmpz_mod_mat_ncols(mat)
    #define compat_fmpz_mod_mat_entry(mat, i, j) fmpz_mod_mat_entry(mat, i, j)
    #define compat_fmpz_mod_mat_set_entry(mat, i, j, val, ctx) fmpz_mod_mat_set_entry(mat, i, j, val)
    #define compat_fmpz_mod_mat_one(mat, ctx) fmpz_mod_mat_one(mat)
    #define compat_fmpz_mod_mat_equal(mat1, mat2, ctx) fmpz_mod_mat_equal(mat1, mat2)
    #define compat_fmpz_mod_mat_is_zero(mat, ctx) fmpz_mod_mat_is_zero(mat)
    #define compat_fmpz_mod_mat_neg(B, A, ctx) fmpz_mod_mat_neg(B, A)
    #define compat_fmpz_mod_mat_add(C, A, B, ctx) fmpz_mod_mat_add(C, A, B)
    #define compat_fmpz_mod_mat_sub(C, A, B, ctx) fmpz_mod_mat_sub(C, A, B)
    #define compat_fmpz_mod_mat_scalar_mul_fmpz(B, A, c, ctx) fmpz_mod_mat_scalar_mul_fmpz(B, A, c)
    #define compat_fmpz_mod_mat_mul(C, A, B, ctx) fmpz_mod_mat_mul(C, A, B)
    #define compat_fmpz_mod_mat_inv(B, A, ctx) fmpz_mod_mat_inv(B, A)
    #define compat_fmpz_mod_mat_transpose(B, A, ctx) fmpz_mod_mat_transpose(B, A)
    #define compat_fmpz_mod_mat_solve(X, A, B, ctx) fmpz_mod_mat_solve(X, A, B)
    #define compat_fmpz_mod_mat_rref(mat, ctx) fmpz_mod_mat_rref(NULL, mat)
    #define compat_fmpz_mod_mat_charpoly(p, M, ctx) fmpz_mod_mat_charpoly(p, M, ctx)
    #define compat_fmpz_mod_mat_minpoly(p, M, ctx) fmpz_mod_mat_minpoly(p, M, ctx)
    #endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants