FLINT3.7.0-dev
Module · 59 functions

fq_default_mat.h

#include <flint/fq_default_mat.h>

This module implements dense matrices over a finite field given by an fq_default_ctx_t (see fq_default.h). A matrix is a union of the matrix types of the five representations (fq_zech_mat, fq_nmod_mat, fq_mat, nmod_mat and fmpz_mod_mat), and every function calls the function of the representation of the context. The semantics are those of fq_mat.h, where the algorithms are described: matrices are initialised with fixed dimensions, outputs may alias inputs of the same dimensions, and the linear algebra functions use Gaussian elimination based on fast matrix multiplication. Over prime fields the nmod_mat and fmpz_mod_mat functions are used, which have their own algorithms (see nmod_mat.h). The characteristic and minimal polynomials, fq_default_mat_charpoly and fq_default_mat_minpoly, are declared in fq_default_poly.h and documented with fq_default_poly.h.

AlgorithmGaussian elimination and LU decompositionLU decomposition with row pivoting over \mathbb{Z}/p\mathbb{Z} in its classical, delayed-reduction and recursive forms, and how FLINT derives triangular solving, linear system solving, inverses, determinants, ranks, echelon forms and nullspaces from it; plus division-free determinants and the Howell form over \mathbb{Z}/n\mathbb{Z}.

Types#

A matrix over an fq_default field: a union of an fq_mat_t, an fq_nmod_mat_t, an fq_zech_mat_t, an nmod_mat_t and an fmpz_mod_mat_t, of which the member matching the context is used. fq_default_mat_t is an array of one union.

Memory management#

Initialises mat as a rows by cols zero matrix over the field.

Initialises mat as a copy of src.

Frees the memory used by mat.

Initialise window as the submatrix of rows r_1 \le i < r_2 and columns c_1 \le j < c_2 of mat, sharing its entries, and clear such a window without freeing the entries.

Entries and dimensions#

void fq_default_mat_entry(fq_default_t val, const fq_default_mat_t mat, slong i, slong j, const fq_default_ctx_t ctx)

Sets val to a copy of the entry in row i and column j. (Unlike fq_mat_entry, this does not return a pointer.)

void fq_default_mat_entry_set(fq_default_mat_t mat, slong i, slong j, const fq_default_t x, const fq_default_ctx_t ctx)
void fq_default_mat_entry_set_fmpz(fq_default_mat_t mat, slong i, slong j, const fmpz_t x, const fq_default_ctx_t ctx)

Set the entry in row i and column j to x, respectively to the image of the integer x in \mathbb{F}_p.

Return the number of rows, respectively columns.

Assignment and permutations#

Copies mat2 into mat1, which must have the same dimensions.

Swaps the two matrices efficiently; the dimensions may differ.

Set A to the zero matrix, respectively to the matrix with ones on the main diagonal.

Swap rows or columns r and s, or reverse the order of the rows or columns; if perm is not NULL, the same permutation is applied to it.

Conversion and concatenation#

Set mat1 to the image of the matrix mat2 over \mathbb{F}_p, or over \mathbb{Z} with entries reduced modulo p. The modulus of mat2 must be p and the dimensions must agree.

Set res to the block matrix with mat1 above, respectively to the left of, mat2.

Comparison#

Returns whether the matrices have the same dimensions and entries.

Return whether the matrix is zero, the identity, empty (no rows or no columns), respectively square.

Random matrices#

Random matrices for testing: random entries, a sparse matrix of the given rank, count random elementary row and column operations (which preserve the rank and the determinant), and random lower or upper triangular matrices with unit (if unit is nonzero) or random nonzero diagonal, as for fq_mat.

Input and output#

int fq_default_mat_fprint(FILE * file, const fq_default_mat_t mat, const fq_default_ctx_t ctx)

Print the matrix to file, respectively stdout, in the plain format of the representation (for fq_mat: the dimensions followed by the entries in row-major order).

int fq_default_mat_fprint_pretty(FILE * file, const fq_default_mat_t mat, const fq_default_ctx_t ctx)

Print the matrix as a bracketed list of bracketed rows to file, respectively stdout.

Arithmetic#

Set the output to A + B, A - B, respectively -A.

Sets C = AB, with Kronecker substitution for extension fields (fq_mat_mul and its variants) and the fast multiplication of nmod_mat or fmpz_mod_mat for prime fields.

Gaussian elimination#

Computes a generalised LU decomposition PA = LU in place and returns the rank, as fq_mat_lu; with nonzero rank_check it returns 0 as soon as A is found to be singular.

Sets B to the reduced row echelon form of A and returns the rank.

Returns the rank of A.

If the square matrix A is invertible, sets B = A^{-1} and returns 1; otherwise returns 0.

Set X = L^{-1}B, respectively X = U^{-1}B, for a lower, respectively upper, triangular matrix with nonzero diagonal (assumed to be one if unit is nonzero).

Solves AX = C for a square matrix A; returns 0 if A is singular.

Solves AX = B for a matrix A of any shape; returns 1 and sets X to a solution if one exists, and 0 otherwise.

Sets the first columns of X to a basis of the right nullspace of A and returns its dimension; X must be n \times n for an m \times n matrix A, and its other columns are set to zero.

Replaces the square matrix A by the similar matrix P^{-1}AP described for fq_mat_similarity.

Example#

Solving a linear system over \mathbb{F}_{2^{10}}:

Examplefq_default_mat_solve.cchecked by make checkdoc
#include <flint/flint.h>
#include <flint/fmpz.h>
#include <flint/fq_default_mat.h>

int main(void)
{
    fmpz_t p;
    fq_default_ctx_t K;
    fq_default_mat_t A, X, B, C;
    fq_default_t a;
    slong i, j;

    fmpz_init_set_ui(p, 2);
    fq_default_ctx_init(K, p, 10, "a");
    fq_default_mat_init(A, 3, 3, K);
    fq_default_mat_init(X, 3, 1, K);
    fq_default_mat_init(B, 3, 1, K);
    fq_default_mat_init(C, 3, 1, K);
    fq_default_init(a, K);

    /* A[i][j] = a^(i*j + i + j), B[i] = a^i */
    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            fq_default_gen(a, K);
            fq_default_pow_ui(a, a, i * j + i + j, K);
            fq_default_mat_entry_set(A, i, j, a, K);
        }
        fq_default_gen(a, K);
        fq_default_pow_ui(a, a, i, K);
        fq_default_mat_entry_set(B, i, 0, a, K);
    }

    flint_printf("solvable: %d\n", fq_default_mat_solve(X, A, B, K));
    fq_default_mat_mul(C, A, X, K);
    flint_printf("A*X == B: %d\n", fq_default_mat_equal(C, B, K));
    flint_printf("rank: %wd\n", fq_default_mat_rank(A, K));

    fq_default_clear(a, K);
    fq_default_mat_clear(A, K);
    fq_default_mat_clear(X, K);
    fq_default_mat_clear(B, K);
    fq_default_mat_clear(C, K);
    fq_default_ctx_clear(K);
    fmpz_clear(p);
    return 0;
}
Output
solvable: 1
A*X == B: 1
rank: 3
Cite this page

The FLINT team, “fq_default_mat.h (module overview),” FLINT: Fast Library for Number Theory, documentation, version 3.7.0-dev, 2026. https://flintlib.org/doc/fq_default_mat/index.html (generated 2026-10-20).