FLINT3.7.0-dev
Module · 241 functions

fmpz_mod_mpoly.h

#include <flint/fmpz_mod_mpoly.h>

This module implements multivariate polynomials over \mathbb{Z}/n\mathbb{Z} for an arbitrary modulus n \ge 1 given as an fmpz. It has the same representation and interface as nmod_mpoly (see nmod_mpoly.h, mpoly.h and Monomial orderings and exponent packing): an fmpz_mod_mpoly_t is a sorted array of terms, each an fmpz coefficient in [0, n) and a packed exponent vector, and the number of variables, the monomial ordering and the modulus are held in an fmpz_mod_mpoly_ctx_t, whose modulus is an fmpz_mod_ctx_t (see fmpz_mod). The functions taking scalars accept them as fmpz, ulong or slong, which are reduced modulo n.

Multiplication uses Johnson's heap algorithm [Joh1974] or Kronecker substitution into fmpz_mod_poly (Sparse and dense multiplication), and division the heap algorithm of Monagan and Pearce [MonPea2011] (Division, exact division and square roots). The GCD algorithms (Brown, Zippel, Hensel lifting; see Greatest common divisors and Multivariate Hensel lifting) work with evaluation points in \mathbb{Z}/p\mathbb{Z}; when the modulus fits a word, GCDs, square roots and factorisations are computed by converting to nmod_mpoly, which also handles small primes (GCDs over small finite fields). Factorisation is in fmpz_mod_mpoly_factor.h and rational functions in fmpz_mod_mpoly_q.h. The module also has a naive implementation of Buchberger's algorithm for Gröbner bases [BW1993], described with an example in Gröbner bases.

Types and conventions#

A multivariate polynomial over \mathbb{Z}/n\mathbb{Z}, with the fields coeffs (an array of fmpz), exps (the packed exponent vectors), length, bits (bits per exponent field), coeffs_alloc and exps_alloc. An fmpz_mod_mpoly_t is an array of length one of fmpz_mod_mpoly_struct.

The context of a polynomial ring (\mathbb{Z}/n\mathbb{Z})[x_0, \ldots, x_{m-1}], with the fields minfo (an mpoly_ctx_t holding the number of variables and the ordering) and ffinfo (an fmpz_mod_ctx_t for the modulus).

The conventions of Types and conventions apply: all polynomials passed to one function belong to the same context; variable 0 is the most significant; inputs and outputs are canonical (terms sorted in strictly decreasing order, nonzero coefficients reduced into [0, n)), except for the container operations; outputs may alias inputs unless stated otherwise. Ring operations work for any modulus, division needs an invertible leading coefficient of the divisor, and GCDs, resultants, square roots and factorisation assume that n is prime, which is not checked.

Context objects#

void fmpz_mod_mpoly_ctx_init(fmpz_mod_mpoly_ctx_t ctx, slong nvars, const ordering_t ord, const fmpz_t modulus)

Initialises ctx for polynomials in nvars variables with the monomial ordering ord (ORD_LEX, ORD_DEGLEX or ORD_DEGREVLEX) and coefficients modulo modulus, which must be positive.

Initialise ctx with a random number of variables in [0, max_nvars] and a random ordering, and the given modulus, a random modulus of at most max_bits bits, or a random prime of at most max_bits bits. Used for testing.

Return the number of variables and the ordering of ctx.

Return a pointer to the modulus of ctx (owned by the context), or set m to it.

Frees the memory used by ctx.

Memory management#

Initialise A as the zero polynomial, allocating nothing, space for alloc terms, or space for alloc terms with bits bits per exponent field.

Frees the memory used by A.

Ensure space for length terms; the second also repacks the exponents to at least bits bits, the third sets the field width to bits and leaves the exponent data undefined.

Keeps only the newlen leading terms of A if it is longer.

Set A to B (resp. modify A) with exponents repacked into Abits bits per field; return 0 if some exponent does not fit, setting A to zero.

Input and output#

The array x gives the variable names, starting with variable 0; if it is NULL, the variables are named x1, x2, and so on.

char * fmpz_mod_mpoly_get_str_pretty(const fmpz_mod_mpoly_t A, const char ** x, const fmpz_mod_mpoly_ctx_t ctx)
int fmpz_mod_mpoly_fprint_pretty(FILE * file, const fmpz_mod_mpoly_t A, const char ** x, const fmpz_mod_mpoly_ctx_t ctx)
int fmpz_mod_mpoly_print_pretty(const fmpz_mod_mpoly_t A, const char ** x, const fmpz_mod_mpoly_ctx_t ctx)

Return a string representing A, which the caller frees with flint_free, or print it to file or stdout. The printing functions return a positive value on success.

int fmpz_mod_mpoly_set_str_pretty(fmpz_mod_mpoly_t A, const char * str, const char ** x, const fmpz_mod_mpoly_ctx_t ctx)

Parses str as in nmod_mpoly_set_str_pretty: integers, the variables in x, +, -, *, exact / and ^. Returns 0 on success, and -1 (setting A to zero) on failure.

Basic manipulation#

Set A to the variable of index var; return whether A is that variable, or, for var < 0, any variable.

Constants#

Return whether A is constant, and set c to the value of the constant A, throwing if A is not constant.

Set A to the constant c; fmpz_mod_mpoly_set_fmpz_mod requires c to be reduced, the other functions reduce it.

Return whether A equals the constant c \bmod n, zero or one.

Degrees#

The zero polynomial has degree -1 in every variable and total degree -1.

Set degs to the degrees of A in all variables, and test whether they fit an slong.

The degree of A in the variable of index var.

Sets used[i] to a nonzero value if the variable of index i appears in A, and to zero otherwise.

Coefficients#

Get or set the coefficient of the monomial M, which must be a monomial (throws otherwise).

Get or set the coefficient of the monomial with exponent vector exp, given as fmpz pointers or as ulong. Setting a coefficient to zero removes the term. Each call costs time linear in the length of A.

void fmpz_mod_mpoly_get_coeff_vars_ui(fmpz_mod_mpoly_t C, const fmpz_mod_mpoly_t A, const slong * vars, const ulong * exps, slong length, const fmpz_mod_mpoly_ctx_t ctx)

Sets C to the coefficient of \prod_i x_{v_i}^{e_i} in A viewed as a polynomial in the distinct variables x_{v_i}, v_i = vars[i], e_i = exps[i], 0 \le i < length, with coefficients in the other variables.

Returns a pointer to the leading coefficient of A, which must be nonzero.

Conversion to univariate polynomials#

Test whether A involves only the variable of index var; convert such a polynomial to an fmpz_mod_poly (returning 0 if it involves another variable); and set A to the univariate polynomial B in that variable.

Comparison#

Returns 1, -1 or 0 according as A is greater than, less than or equal to B in a total order on polynomials that agrees with the monomial ordering on monomials.

Container operations#

These functions access terms by their index i, 0 \le i < length, and can produce non-canonical polynomials, which fmpz_mod_mpoly_sort_terms and fmpz_mod_mpoly_combine_like_terms repair. An index out of range throws.

Return whether A is canonical (sorted, nonzero reduced coefficients, valid exponents), or throw if it is not.

Return the number of terms, or change it to new_length, deleting terms at the end or appending zero terms.

Test whether the exponents of the term of index i fit a ulong or slong, and get its exponent vector or the exponent of one variable; the ulong and slong versions throw if a value does not fit.

Set the exponent vector of the term of index i.

Set M to the term of index i, or to its monomial with coefficient 1.

Append a term with coefficient c \bmod n and exponent vector exp in constant amortised time. The exponents are an array of pointers to fmpz (_fmpz), an array of fmpz (_ffmpz) or of ulong (_ui).

Sort the terms into the order of the context (without combining), and combine adjacent equal monomials and remove zero coefficients; together they make A canonical.

Random generation#

Set A to a random polynomial with at most length terms and exponents below exp_bound, below exp_bounds[i] for variable i, or of at most exp_bits bits.

Addition and subtraction#

Scalar operations#

Sets A to B divided by its leading coefficient; throws if B is zero or the leading coefficient is not invertible.

Differentiation#

Sets A to the derivative of B with respect to the variable of index var.

Evaluation and composition#

void fmpz_mod_mpoly_evaluate_all_fmpz(fmpz_t eval, const fmpz_mod_mpoly_t A, fmpz * const * alphas, const fmpz_mod_mpoly_ctx_t ctx)

Sets eval to the value of A at the point with coordinates alphas[0], alphas[1], ... (one per variable).

Sets A to B with the variable of index var replaced by val.

Set A to B evaluated at the polynomials C[i] of the context ctxAC, one per variable of ctxB. A must not alias B or any C[i]. Return 1 for success and 0 if the computation would be too large. The main function transforms exponents by a matrix when every C[i] is zero or a monomial with coefficient 1, and otherwise uses a Horner scheme or a geobucket sum.

Sets A to B with the variable of index i of ctxB replaced by the variable of index c[i] of ctxAC, or by zero if c[i] is negative.

Multiplication#

Sets A to B C, by Kronecker substitution if the product is dense enough and otherwise by Johnson's heap algorithm.

Sets A to B C by Johnson's heap algorithm [Joh1974].

Tries to set A to B C by Kronecker substitution into fmpz_mod_poly; returns 0 if the dense representation would be too large.

Powering#

Set A to B^k. fmpz_mod_mpoly_pow_fmpz throws if k < 0 and returns 0 if k does not fit an slong and B has more than one term; otherwise both return 1.

Sets A to B^k by repeated multiplication.

Division#

The division functions need the leading coefficient of the divisor to be invertible; they throw FLINT_DIVZERO for a zero divisor.

If B divides A, sets Q to A/B and returns 1; otherwise sets Q to zero and returns 0.

Sets Q to A/B, assuming that the division is exact.

Variants of fmpz_mod_mpoly_divides: the heap algorithm of Monagan and Pearce [MonPea2011], and Kronecker substitution, which returns -1 if the dense representation would be too large.

Set Q and R to the quotient and remainder of A divided by B: A = QB + R with no term of R divisible by the leading monomial of B.

Divides A by the len polynomials B[i]: A = R + \sum_i Q_i B_i with no term of R divisible by any leading monomial of a B_i.

Square roots#

If A is a square, set Q to a square root and return 1, otherwise return 0. The modulus must be prime. A word-size modulus is handled by nmod_mpoly_sqrt.

If Q^2 + AQ = B has a solution, sets Q to one and returns 1; otherwise returns 0. The modulus must be prime.

Greatest common divisors#

These functions assume that the modulus is prime. They return 1 for success and 0 for failure, which only happens when the exponents need more than one word per field even after deflation.

Sets M to the GCD of the terms of A: zero, or the monomial with coefficient 1 whose exponents are the minima over the terms of A.

Sets g to the GCD of the coefficients of A viewed as a polynomial in the variables vars[i], and returns 1 for success.

Set G to the monic GCD of A and B (zero if both are zero), and fmpz_mod_mpoly_gcd_cofactors also \bar{A} = A/G and \bar{B} = B/G. For a word-size modulus the computation is done by nmod_mpoly.

Set G to the monic GCD by one algorithm: Brown's dense interpolation, Hensel lifting, Zippel's sparse interpolation, sparse interpolation with two main variables, or the subresultant pseudo-remainder sequence in one variable. Return 0 if the algorithm cannot be applied.

void fmpz_mod_mpoly_deflation(fmpz * shift, fmpz * stride, const fmpz_mod_mpoly_t A, const fmpz_mod_mpoly_ctx_t ctx)
void fmpz_mod_mpoly_deflate(fmpz_mod_mpoly_t A, const fmpz_mod_mpoly_t B, const fmpz * shift, const fmpz * stride, const fmpz_mod_mpoly_ctx_t ctx)
void fmpz_mod_mpoly_inflate(fmpz_mod_mpoly_t A, const fmpz_mod_mpoly_t B, const fmpz * shift, const fmpz * stride, const fmpz_mod_mpoly_ctx_t ctx)

As nmod_mpoly_deflation, nmod_mpoly_deflate and nmod_mpoly_inflate: the shifts and strides of the exponents of each variable, and the substitutions e \mapsto (e - \mathit{shift})/\mathit{stride} and e \mapsto \mathit{shift} + \mathit{stride} \cdot e.

Resultants and discriminants#

Set R to the resultant of A and B, or the discriminant of A, with respect to the variable of index var, by the subresultant algorithm of Ducos [Duc2000]. Return 1 for success. The modulus must be prime.

Univariate representation#

An fmpz_mod_mpoly_univar_t holds a polynomial in a main variable with fmpz_mod_mpoly_t coefficients in the other variables, as in Univariate representation.

The fields are coeffs, exps (fmpz exponents), alloc and length.

Convert B to a polynomial in the variable of index var, and back (the coefficients must not involve that variable).

A GCD up to factors free of the main variable, the resultant and the discriminant with respect to the main variable, by subresultants. Return 1 for success.

Vectors#

A vector of polynomials, with the fields p (the entries), alloc and length.

fmpz_mod_mpoly_vec_entry(vec, i)

A pointer to the entry of index i of vec.

Initialise vec to len zero polynomials, free it, swap two vectors, ensure room for len entries, and set the length (truncating or appending zeros).

Copy a vector, append a copy of f, and insert f unless it is already present, returning its index.

Print a vector, and set it to len random nonzero polynomials with at most poly_len terms and exponents below exp_bound.

Ideals and Gröbner bases#

These functions work with ideals of (\mathbb{Z}/n\mathbb{Z})[x_0, \ldots, x_{m-1}] for prime n (leading coefficients are inverted), using monic polynomials as normalised generators. They follow [BW1993] and are meant for small examples.

Sets res to the S-polynomial of f and g, computed after making f and g monic: \mathrm{lcm}/\mathrm{lm}(f) \cdot f/\mathrm{lc}(f) - \mathrm{lcm}/\mathrm{lm}(g) \cdot g/\mathrm{lc}(g), where lcm is the least common multiple of the leading monomials.

Sets res to the remainder of f on division by the polynomials in Iv (with fmpz_mod_mpoly_divrem_ideal), made monic unless it is zero.

Returns whether G is a Gröbner basis of the ideal it generates (all S-polynomials reduce to zero) and, if F is not NULL, every element of F reduces to zero modulo G, so that the ideal of F is contained in that of G. The converse inclusion is not checked.

Full documentation

Returns whether G is autoreduced (inter-reduced).

Full documentation

Sets H to the autoreduction of the Gröbner basis G, which is the reduced Gröbner basis of the ideal, unique up to the order of the entries.

Full documentation

Sets G to the nonzero entries of F made monic, without duplicates.

Set G to a Gröbner basis of the ideal generated by F, by Buchberger's algorithm in the form GROEBNERNEW2 of [BW1993], which discards useless pairs with the criteria of Gebauer and Möller [GM1988]. The basis is not reduced in general; fmpz_mod_mpoly_vec_autoreduction_groebner reduces it. The second version stops if the basis gets more than ideal_len_limit elements or a polynomial gets more than poly_len_limit terms, and returns 0 in that case (G then generates the ideal but need not be a Gröbner basis); otherwise it returns 1.

Full documentation

Conversions#

Sets res to the reduction modulo n of the integer polynomial f of the context ctx, which must have the same number of variables and ordering as ctxm.

Sets res to f with its coefficients lifted to integers in [0, n).

Internal functions#

void _fmpz_mod_mpoly_fit_length(fmpz ** coeffs, slong * coeffs_alloc, ulong ** exps, slong * exps_alloc, slong N, slong length)

Ensure room for length coefficients and exponent vectors of N words in the given arrays, and set the length field.

void _fmpz_mod_mpoly_radix_sort1(fmpz *, ulong *, slong left, slong right, flint_bitcnt_t pos, ulong cmpmask, ulong totalmask)
void _fmpz_mod_mpoly_radix_sort(fmpz *, ulong *, slong left, slong right, flint_bitcnt_t pos, slong N, ulong * cmpmask)

Radix sort of the terms left to right - 1 (coefficients and exponent arrays given explicitly), appending a term with undefined coefficient, and setting a coefficient given an array of fmpz exponents.

void _fmpz_mod_mpoly_eval_all_fmpz_mod(fmpz_t eval, const fmpz * Acoeffs, const ulong * Aexps, slong Alen, flint_bitcnt_t Abits, const fmpz * alphas, const mpoly_ctx_t mctx, const fmpz_mod_ctx_t fctx)

Evaluation of term arrays at a point, and substitution of monomials by a matrix acting on the exponent vectors.

Multiplication and division methods given the maximum exponent fields of the inputs.

Set up D as a dense univariate polynomial holding the Kronecker substitution of A with the given degree bounds, and free it.

void _fmpz_mod_mpoly_to_fmpz_mod_poly_deflate(fmpz_mod_poly_t A, const fmpz_mod_mpoly_t B, slong var, const ulong * Bshift, const ulong * Bstride, const fmpz_mod_mpoly_ctx_t ctx)

Conversions to and from univariate representations with given exponent bits, shifts and strides.

Convert between an fmpz_mod_mpoly with a word-size modulus and an nmod_mpoly in a context with the same variables, ordering and modulus.

For A viewed as a polynomial in its first num_vars variables (in a lexicographic context): the leading coefficient, and the GCD of the coefficients (returning 1 for success).

void fmpz_mod_mpoly_to_mpolyl_perm_deflate(fmpz_mod_mpoly_t A, const fmpz_mod_mpoly_ctx_t lctx, const fmpz_mod_mpoly_t B, const fmpz_mod_mpoly_ctx_t ctx, const slong * perm, const ulong * shift, const ulong * stride)
void fmpz_mod_mpoly_from_mpolyl_perm_inflate(fmpz_mod_mpoly_t A, flint_bitcnt_t Abits, const fmpz_mod_mpoly_ctx_t ctx, const fmpz_mod_mpoly_t B, const fmpz_mod_mpoly_ctx_t lctx, const slong * perm, const ulong * shift, const ulong * stride)

Permute, deflate and convert to a lexicographic context lctx, and undo this with Abits-bit exponents.

Sets res to f divided by its leading coefficient, or to zero if f is zero. Throws if the leading coefficient is not invertible. Not used in FLINT.

Sets up the generic ring interface R used by the parser and the subresultant code.

Throws if some term of r is divisible by the leading monomial of g.

References#

  • [Joh1974] S. C. Johnson. Sparse polynomial arithmetic. ACM SIGSAM Bulletin 8:3 (1974), 63–71. https://doi.org/10.1145/1086837.1086847
  • [MonPea2011] M. Monagan and R. Pearce. Sparse polynomial division using a heap. Journal of Symbolic Computation 46:7 (2011), 807–822. https://doi.org/10.1016/j.jsc.2010.08.014
  • [BW1993] T. Becker and V. Weispfenning, Gröbner Bases: A Computational Approach to Commutative Algebra, Graduate Texts in Mathematics 141, Springer-Verlag (1993). https://doi.org/10.1007/978-1-4612-0913-3
  • [Duc2000] L. Ducos. Optimizations of the subresultant algorithm. Journal of Pure and Applied Algebra 145 (2000), 149–163. https://doi.org/10.1016/S0022-4049(98)00081-4
  • [GM1988] R. Gebauer and H. M. Möller. On an installation of Buchberger's algorithm. Journal of Symbolic Computation 6:2–3 (1988), 275–286.
  • [Yan1998] T. Yan. The geobucket data structure for polynomials. Journal of Symbolic Computation 25:3 (1998), 285–293. https://doi.org/10.1006/jsco.1997.0176
Cite this page

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