FLINT3.7.0-dev
Module · 68 functions

nmod_poly_mat.h

#include <flint/nmod_poly_mat.h>

This module implements dense matrices whose entries are polynomials over \mathbb{Z}/n\mathbb{Z} (nmod_poly_t) for a word-size modulus n. Such matrices can also be seen as polynomials with matrix coefficients, and the module converts between the two views. Multiplication uses the classical algorithm, Kronecker substitution into an integer matrix product, evaluation and interpolation at many points (for a large prime modulus), or the small-prime FFT with each entry transformed once. Determinants, ranks, inverses, solutions and nullspaces are computed by fraction-free elimination over (\mathbb{Z}/n\mathbb{Z})[x] (or, for determinants, by evaluation and interpolation), which requires n to be prime. The algorithms are described in Matrices of polynomials.

AlgorithmMatrices of polynomialsHow FLINT multiplies matrices over \mathbb{Z}[x] and (\mathbb{Z}/n\mathbb{Z})[x] — classical products of polynomials, Kronecker substitution into one integer matrix product, evaluation and interpolation, and transforming each entry once with the small-prime FFT — and how it computes determinants, ranks, inverses and solutions by fraction-free elimination or by evaluation and interpolation.

Types and conventions#

An nmod_poly_mat_struct has the fields entries (a pointer to the entry in row 0, column 0), r and c (the numbers of rows and columns), stride (the distance between the starts of consecutive rows) and modulus. An nmod_poly_mat_t is an array of one nmod_poly_mat_struct.

All matrices passed to one function must have the same modulus. The entries are nmod_poly_t with that modulus and can be manipulated with the functions of nmod_poly.h. Outputs must be initialised with the dimensions of the result. The arithmetic functions accept any modulus n \ge 1 (except nmod_poly_mat_mul_interpolate); elimination, determinants for dimension at least 3, ranks, solving, inverses and nullspaces divide by polynomials and require n to be prime.

Memory management#

Initialises mat as a rows by cols matrix of zero polynomials modulo n. Throws if \mathit{rows} \cdot \mathit{cols} overflows an slong.

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

Frees the memory used by mat. Must not be called on a window.

Swaps two matrices in constant time by swapping their structs (not for windows), respectively swaps the entries of two matrices of the same dimensions one by one.

Entries and dimensions#

nmod_poly_struct * nmod_poly_mat_entry(const nmod_poly_mat_t mat, slong i, slong j)

Returns a pointer to the entry in row i and column j.

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

Returns the modulus n of mat.

Returns 1 if mat has no rows or no columns, respectively as many rows as columns, and 0 otherwise.

Returns the largest length of an entry (0 if all entries are zero), respectively the largest degree, which is the largest length minus one (-1 for the zero matrix).

Polynomial-matrix view#

Sets the matrix coeff to the coefficients of x^{\mathit{deg}} of the entries of pmat, respectively sets these coefficients of pmat to the entries of coeff. The dimensions must agree.

Sets pmat to the constant matrix cmat, which must have the same dimensions.

Returns 1 if pmat is the constant matrix cmat (same dimensions, all entries constant and equal), and 0 otherwise.

Sets res to pmat with every entry truncated to length len (reduced modulo x^{\mathit{len}}), respectively truncates pmat in place.

Sets res to pmat multiplied by x^k, respectively divided by x^k with the coefficients of degree less than k discarded. Aliasing is allowed.

Windows and concatenation#

Initialises window as the submatrix of mat formed by rows r_1 to r_2 - 1 and columns c_1 to c_2 - 1, sharing its entries and modulus with mat.

Releases a window. This does nothing at present.

Sets res to mat1 placed to the left of, respectively above, mat2, whose dimensions must be compatible.

Input and output#

void nmod_poly_mat_print(const nmod_poly_mat_t mat, const char * x)

Prints mat to stdout: a header <r x c matrix over Z/nZ[x]> (with the variable name x), then each row in square brackets with the entries printed by nmod_poly_print (length, modulus and coefficients) and separated by commas, then an empty line.

Random matrices#

Sets each entry to a random polynomial of length up to len, with uniformly random coefficients (nmod_poly_rand), respectively with a bias towards special values (nmod_poly_randtest).

Sets each entry, with probability density, to a random polynomial of length up to len, and to zero otherwise.

Comparison and special matrices#

Returns 1 if the matrices have the same dimensions and entries, and 0 otherwise.

Returns 1 if mat is zero, respectively has the constant 1 on the main diagonal and zeros elsewhere, and 0 otherwise.

Sets mat to the zero matrix, respectively to the matrix with the constant 1 on the main diagonal and zeros elsewhere.

Basic arithmetic#

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

Sets B to the transpose of A; B may be A if it is square. Throws if the dimensions are incompatible.

Sets the matrix B over \mathbb{Z}/n\mathbb{Z} to A evaluated at x.

Sets C = A + B, C = A - B, respectively B = -A. Aliasing is allowed.

Sets B = cA for a polynomial, respectively a reduced constant, c. Aliasing is allowed.

Sets trace to the sum of the diagonal entries of the square matrix mat.

Multiplication#

Sets C = AB, choosing between the methods below from the dimensions, the lengths of the entries and the modulus. C may be A or B. See Choice.

Full documentation

Sets C = AB by sums of polynomial products, respectively by Kronecker substitution: the entries are packed into integers with nmod_poly_bit_pack, multiplied with fmpz_mat_mul and unpacked. C may be A or B.

Full documentation

Sets C = AB by evaluating A and B at L = \ell_A + \ell_B - 1 points (where \ell_A, \ell_B are the largest lengths of the entries), multiplying the L pairs of matrices over \mathbb{Z}/n\mathbb{Z} with nmod_mat_mul and interpolating. Requires n to be a prime with n \ge L; throws if n < L.

Full documentation

Sets each entry of C to the coefficients of degrees z_l to z_h - 1 of the corresponding entry of AB (the product shifted right by z_l and truncated to length z_h - z_l), and returns 1; returns 0 if the small-prime FFT is not available or no transform plan admits the size of the accumulated products, in which case C may be partially written. C may be A or B.

Full documentation

Sets B = A^2 for a square matrix A; B may be A. The classical version uses five polynomial products for 2 \times 2 matrices; the others are the squaring versions of the multiplication methods. nmod_poly_mat_sqr uses the classical algorithm for dimension less than 10. Above, it uses evaluation and interpolation for a prime modulus of more than \beta/4 bits that is at least 2\ell - 1, where \ell is the largest length of an entry, and dimension above 80 + \sqrt{\ell}; otherwise Kronecker substitution, or the classical algorithm for entries of length above 128.

Sets B = A^{\mathit{exp}} for a square matrix A by binary powering. A^0 is the identity. B may be A but must not be a window.

Row reduction#

These functions require a prime modulus.

Returns the index of a row r with start_row \le r < end_row whose entry in column c is nonzero, or -1 if there is none: the first such row, respectively one whose entry has least length.

slong nmod_poly_mat_fflu(nmod_poly_mat_t B, nmod_poly_t den, slong * perm, const nmod_poly_mat_t A, int rank_check)

Computes a fraction-free LU decomposition of A in B and returns the rank r of A over the field of rational functions. den is set to the last pivot, a nonzero r \times r minor of A (1 if r = 0). Row exchanges are applied to perm if it is not NULL; pivots are chosen with nmod_poly_mat_find_pivot_partial. If rank_check is set, the function returns 0 with den = 0 as soon as a column without pivot is found. B may be A. See Fraction-free elimination.

Sets (B, den) to the reduced row echelon form of A over the field of rational functions and returns the rank: B/\mathit{den} is in reduced row echelon form, with den on the pivot positions. B may be A.

Determinant and rank#

Sets det to the determinant of the square matrix A. nmod_poly_mat_det_fflu uses fraction-free elimination; nmod_poly_mat_det_interpolate evaluates A at the points 0, 1, \ldots, L - 1 with L = n(\ell - 1) + 1, where \ell is the largest length of an entry, computes the determinants with nmod_mat_det and interpolates, falling back to fraction-free elimination if L exceeds the modulus. nmod_poly_mat_det uses explicit formulas for n \le 2, fraction-free elimination for n < 15 and interpolation otherwise. The modulus must be prime for n \ge 3.

Returns the rank of A over the field of rational functions, computed by fraction-free elimination. Requires a prime modulus.

Solving and inversion#

These functions require a prime modulus.

For a square A of dimension n and an n \times k matrix B, sets (X, den) with AX = \mathit{den} \cdot B and \mathit{den} = \det A, and returns 1 if A is nonsingular; returns 0 with den = 0 if A is singular. Both use fraction-free elimination. X may be A but not B.

Solves AX = \mathit{den} \cdot B given the output FFLU, den, perm of nmod_poly_mat_fflu for a nonsingular A, with fraction-free forward and back substitution. The sign of the permutation is not taken into account. perm must not be NULL, and X must not be B.

Sets (Ainv, den) to the inverse of the square matrix A, so that A \cdot \mathit{Ainv} = \mathit{den} \cdot I with \mathit{den} = \det A, and returns 1 if A is nonsingular; returns 0 if it is singular. Ainv may be A.

Sets the first n - r columns of the n \times n matrix res to a basis of the right nullspace of the m \times n matrix mat over the field of rational functions, with polynomial entries, and returns n - r; the other columns are set to zero.

Cite this page

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