Skip to content

Instantly share code, notes, and snippets.

@MurageKibicho
Created October 18, 2025 06:14
Show Gist options
  • Save MurageKibicho/8dbb2a7abf6949e2bba7308fb58a44b0 to your computer and use it in GitHub Desktop.
Save MurageKibicho/8dbb2a7abf6949e2bba7308fb58a44b0 to your computer and use it in GitHub Desktop.
Flint Code to RREF a matrix
#include <flint/fmpz_mod_mat.h>
#include <flint/fmpz_mod.h>
#include <flint/fmpz.h>
#include <stdio.h>
int main() {
fmpz_t n;
fmpz_init_set_ui(n, 2); // Set modulus to 2
fmpz_mod_ctx_t ctx;
fmpz_mod_ctx_init(ctx, n);
slong rows = 18, cols = 7;
fmpz_mod_mat_t A, R;
fmpz_mod_mat_init(A, rows, cols, ctx);
fmpz_mod_mat_init(R, rows, cols, ctx);
// Fill matrix A with your values mod 2
slong data[18][7] = {
{4, 0, 3, 1, 0, 0, 8947},
{2, 6, 0, 0, 0, 0, 10848},
{6, 0, 0, 0, 0, 2, 5322},
{0, 4, 1, 0, 1, 0, 8994},
{2, 1, 0, 1, 0, 2, 16761},
{1, 1, 0, 1, 0, 1, 162},
{1, 2, 1, 0, 0, 1, 20718},
{2, 0, 0, 3, 0, 0, 8142},
{1, 3, 1, 1, 0, 0, 1659},
{4, 1, 0, 0, 1, 0, 796},
{1, 4, 1, 0, 0, 1, 15016},
{1, 3, 0, 0, 1, 1, 18623},
{2, 3, 0, 1, 0, 1, 8437},
{3, 2, 1, 0, 1, 0, 14735},
{1, 1, 2, 0, 0, 1, 12444},
{7, 0, 3, 0, 0, 0, 1626},
{4, 3, 2, 0, 0, 0, 4159},
{0, 4, 1, 2, 0, 0, 13137},
};
for (slong i = 0; i < rows; i++) {
for (slong j = 0; j < cols; j++) {
// Set the matrix entry mod 2 automatically
fmpz_mod_set_ui(fmpz_mod_mat_entry(A, i, j), data[i][j], ctx);
}
}
// Compute RREF mod 2
slong rank = fmpz_mod_mat_rref(R, A, ctx);
printf("Rank: %ld\n", rank);
printf("RREF matrix (mod 2):\n");
fmpz_mod_mat_print_pretty(R, ctx);
// Cleanup
fmpz_mod_mat_clear(A, ctx);
fmpz_mod_mat_clear(R, ctx);
fmpz_mod_ctx_clear(ctx);
fmpz_clear(n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment