FLINT3.7.0-dev
Module · 67 functions

fq_nmod_mat.h

#include <flint/fq_nmod_mat.h>

This module implements dense matrices over a finite field \mathbb{F}_q in the representation of fq_nmod.h (a prime p < 2^{\mathtt{FLINT\_BITS}}, elements stored as nmod_poly). It is generated from the same template as fq_mat.h and has the same functions, with fq_nmod in place of fq. As there, the linear algebra functions are wrappers of the generic gr_mat algorithms, and multiplication uses Kronecker substitution for all but small matrices.

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 and conventions#

A dense matrix with r rows and c columns. The entries are stored in the array entries row by row, with stride elements between the starts of consecutive rows (equal to c except for windows). fq_nmod_mat_t is an array of one structure.

Memory management#

void fq_nmod_mat_init(fq_nmod_mat_t mat, slong rows, slong cols, const fq_nmod_ctx_t ctx)

Initialises mat as a rows by cols zero matrix.

More

Initialises mat as a copy of src, with the same dimensions.

More

Frees the memory used by mat.

More
void fq_nmod_mat_window_init(fq_nmod_mat_t window, const fq_nmod_mat_t mat, slong r1, slong c1, slong r2, slong c2, const fq_nmod_ctx_t ctx)

Initialises window as the submatrix of mat formed by the rows r_1 \le i < r_2 and the columns c_1 \le j < c_2. The entries are shared with mat, not copied, so changes to one are visible in the other. mat must not be cleared while the window is in use.

More

Clears a window. The entries, which belong to the underlying matrix, are not freed.

More

Entries and dimensions#

fq_nmod_struct * fq_nmod_mat_entry(const fq_nmod_mat_t mat, slong i, slong j)

Returns a pointer to the entry in row i and column j, indexed from zero. No bounds checking is performed.

More
void fq_nmod_mat_entry_set(fq_nmod_mat_t mat, slong i, slong j, const fq_nmod_t x, const fq_nmod_ctx_t ctx)

Sets the entry in row i and column j to x.

More

Return the number of rows, respectively columns, of mat.

More

Assignment and permutations#

void fq_nmod_mat_set(fq_nmod_mat_t mat1, const fq_nmod_mat_t mat2, const fq_nmod_ctx_t ctx)

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

More

Swaps the two matrices by swapping their structures; the dimensions may differ. Not for windows.

More

Swaps the entries of two matrices of the same dimensions one by one; this also works for windows.

More

Set A to the zero matrix, respectively to the matrix with ones on the main diagonal and zeros elsewhere (also for non-square A).

More

Swap rows, respectively columns, r and s of mat. If perm is not NULL, its entries r and s are swapped as well, so that it records the permutation.

More

Reverse the order of the rows, respectively columns, of mat, by swapping row i and row r - 1 - i for 0 \le i < \lfloor r/2 \rfloor (and likewise for columns). If perm is not NULL, the same permutation is applied to it.

More

Conversion and concatenation#

Set mat1 to the image of the matrix mat2 over \mathbb{F}_p (or over \mathbb{Z}/n\mathbb{Z}, whose entries are mapped to \mathbb{F}_p by reduction) in \mathbb{F}_q. The dimensions must agree.

More

Set res to the block matrix \begin{pmatrix} A \\ B \end{pmatrix}, respectively (A \ B), for A = mat1 and B = mat2. For vertical concatenation of an m \times n and a k \times n matrix, res must be (m + k) \times n; for horizontal concatenation of an m \times n and an m \times k matrix, m \times (n + k).

More

Comparison#

int fq_nmod_mat_equal(const fq_nmod_mat_t mat1, const fq_nmod_mat_t mat2, const fq_nmod_ctx_t ctx)

Returns whether the matrices have the same dimensions and entries.

More

Return whether all entries are zero, respectively whether the diagonal entries are one and the others zero. An empty matrix is both zero and one.

More

Return whether mat has zero rows or zero columns, respectively whether it has as many rows as columns.

More

Random matrices#

Sets the entries of mat to random elements for testing (see fq_nmod_randtest).

More

Sets mat to a random row and column permutation of the diagonal matrix whose first n diagonal entries are diag and whose other entries are zero; n must not exceed the number of rows or columns. Returns the parity of the permutation (0 for even, 1 for odd).

More

Sets mat to a random sparse matrix of the given rank, with exactly rank nonzero entries, which are random nonzero elements. fq_nmod_mat_randops turns it into a dense matrix of the same rank.

More

Applies count random elementary row or column operations (additions or subtractions of a row or column to another) to mat. This preserves the rank and, for square matrices, the determinant.

More
void fq_nmod_mat_randtril(fq_nmod_mat_t mat, flint_rand_t state, int unit, const fq_nmod_ctx_t ctx)
void fq_nmod_mat_randtriu(fq_nmod_mat_t mat, flint_rand_t state, int unit, const fq_nmod_ctx_t ctx)

Set mat to a random lower, respectively upper, triangular matrix, with ones on the diagonal if unit is nonzero and random nonzero diagonal entries otherwise.

More

Input and output#

int fq_nmod_mat_fprint(FILE * file, const fq_nmod_mat_t mat, const fq_nmod_ctx_t ctx)

Print mat to file, respectively stdout: the number of rows and columns separated by a space, two spaces, and the entries in row-major order in the format of fq_nmod_fprint, separated by spaces. Return a positive value on success and a non-positive value on failure.

More
int fq_nmod_mat_fprint_pretty(FILE * file, const fq_nmod_mat_t mat, const fq_nmod_ctx_t ctx)

Print mat to file, respectively stdout, as a bracketed list of bracketed rows, with the entries in the format of fq_nmod_fprint_pretty. Return a positive value on success.

More

Arithmetic#

Sets B to A^T. B must have the transposed dimensions of A; aliasing is allowed for square matrices.

More

Set C to A + B, respectively A - B. All three matrices must have the same dimensions.

More

Sets B to -A.

More

Set B to cA, where c is an element of \mathbb{F}_q or an integer mapped to \mathbb{F}_p.

More

Sets C = AB, where A is m \times k, B is k \times n and C is m \times n. Aliasing is allowed. The function uses fq_nmod_mat_mul_KS if FQ_NMOD_MAT_MUL_KS_CUTOFF returns 1 for m and n, and fq_nmod_mat_mul_classical otherwise.

More

Returns 1 if fq_nmod_mat_mul should use Kronecker substitution for an r \times k times k \times c product, which is the case if 2(r + 1)c > \max(0, 12 - d)^2, in particular always for d \ge 12.

Sets C = AB by computing each entry as a dot product with _fq_nmod_vec_dot, which reduces modulo the defining polynomial only once per entry. Aliasing is allowed.

More

Sets C = AB by Kronecker substitution. C must not be aliased with A or B.

More

Sets D = C - AB. D may be aliased with C, A or B.

More
void fq_nmod_mat_mul_vec(fq_nmod_struct * c, const fq_nmod_mat_t A, const fq_nmod_struct * b, slong blen, const fq_nmod_ctx_t ctx)
void fq_nmod_mat_mul_vec_ptr(fq_nmod_struct * const * c, const fq_nmod_mat_t A, const fq_nmod_struct * const * b, slong blen, const fq_nmod_ctx_t ctx)

Set the vector c to the matrix-vector product Ab. The vector (b, blen) is truncated or extended by zeros to the number of columns of A, and c receives as many entries as A has rows. The _ptr version takes arrays of pointers to the entries. c must not alias b.

More
void fq_nmod_mat_vec_mul(fq_nmod_struct * c, const fq_nmod_struct * a, slong alen, const fq_nmod_mat_t B, const fq_nmod_ctx_t ctx)
void fq_nmod_mat_vec_mul_ptr(fq_nmod_struct * const * c, const fq_nmod_struct * const * a, slong alen, const fq_nmod_mat_t B, const fq_nmod_ctx_t ctx)

Set the vector c to the vector-matrix product aB. The vector (a, alen) is truncated or extended by zeros to the number of rows of B, and c receives as many entries as B has columns. c must not alias a.

More

Gaussian elimination#

slong fq_nmod_mat_lu(slong * P, fq_nmod_mat_t A, int rank_check, const fq_nmod_ctx_t ctx)

Computes a generalised LU decomposition PA = LU of A in place, and returns the rank of A. P must have room for as many entries as A has rows; it receives the row permutation.

More

Sets B to the reduced row echelon form of A and returns the rank of A. The form is computed from an LU decomposition. B may be aliased with A. If A is zero, B is set to zero and 0 is returned.

More

Reduces row m of A against the earlier rows, which must already be in reduced form (pivot entries 1, not necessarily in order), and returns the column of the new pivot, or -1 if row m becomes zero. P[i] is the row with its pivot in column i, or -1; it is updated when a new pivot is found, and the new pivot row is scaled to make the pivot 1. L[i] is the number of possibly nonzero columns of row i, which allows faster reduction of matrices that are zero on the right; the entries of L must be nondecreasing, and can all be set to the number of columns. The arithmetic is done without intermediate reductions modulo the defining polynomial.

More

Returns the rank of A, computed by LU decomposition.

More

If the square matrix A is invertible, sets B = A^{-1} and returns 1; otherwise returns 0 and the entries of B are undefined. A is not modified (although not declared const), and B may be aliased with A.

More
void fq_nmod_mat_solve_tril(fq_nmod_mat_t X, const fq_nmod_mat_t L, const fq_nmod_mat_t B, int unit, const fq_nmod_ctx_t ctx)
void fq_nmod_mat_solve_triu(fq_nmod_mat_t X, const fq_nmod_mat_t U, const fq_nmod_mat_t B, int unit, const fq_nmod_ctx_t ctx)

Set X = L^{-1} B, respectively X = U^{-1} B, for a square lower, respectively upper, triangular matrix with nonzero diagonal entries. If unit is nonzero, the diagonal is assumed to consist of ones and is not read. Only the triangular part of the matrix is read. Aliasing of X and B is allowed. Classical substitution is used if B has fewer than 64 rows or columns, and block recursion otherwise.

More

Solves AX = C for a square matrix A. Returns 1 and sets X if A is nonsingular; otherwise returns 0 and X is undefined.

More

Solves AX = B for a matrix A of any shape: if a solution exists, sets X to one and returns 1, otherwise returns 0 and X is undefined. For an m \times n matrix A and an m \times k matrix B, X must be n \times k.

More

Computes a basis of the right nullspace \{v : Av = 0\} of A and returns its dimension k (the nullity). X must have as many rows and at least as many columns as A has columns; its first k columns receive the basis and the remaining columns are set to zero.

More

Characteristic and minimal polynomial#

Replaces the square matrix A by P^{-1} A P, where P is the identity matrix with the entries in row r and every column j \notin \{r - 1, r\} replaced by d. Such transforms preserve the characteristic and minimal polynomials.

More
Cite this page

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