FLINT3.7.0-dev
Module · 173 functions

fmpq_mpoly.h

#include <flint/fmpq_mpoly.h>

This module implements sparse polynomials in \mathbb{Q}[x_0, \ldots, x_{n-1}]. A polynomial is stored as a rational number, its content, times a primitive integer polynomial with positive leading coefficient, an fmpz_mpoly_t (see fmpz_mpoly.h). Almost all operations reduce to the corresponding operations on the integer part, followed by an adjustment of the content, so the algorithms, the monomial orderings, the exponent representation and the performance characteristics are those of fmpz_mpoly; the guide Getting started with fmpz_mpoly applies with fmpz replaced by fmpq. Factorisation is in fmpq_mpoly_factor.

For polynomials over an arbitrary generic ring, including \mathbb{Q}, see gr_mpoly; fmpq_mpoly is the specialised and faster implementation for \mathbb{Q}.

Types and conventions#

A polynomial A = c \cdot Z with fields content (the rational c, an fmpq_t) and zpoly (the integer polynomial Z, an fmpz_mpoly_t). The representation is canonical when either both are zero, or c \ne 0 and Z is a canonical fmpz_mpoly whose coefficients have GCD 1 and whose leading coefficient is positive. Then the sign of A is carried by c. The functions of this module return canonical polynomials when given canonical input, except where noted.

The context (parent ring) of an fmpq_mpoly: the number of variables and the monomial ordering. It contains a single field zctx, the fmpz_mpoly_ctx_t that is used for the integer parts.

Variables are numbered from 0; variable 0 is the most significant one in the ordering. Exponent vectors passed as ulong *, slong * or arrays of fmpz are indexed by variable number. Output polynomials may alias inputs unless stated otherwise.

Context object#

Initialises a context for polynomials in nvars variables with the monomial ordering ord, one of ORD_LEX, ORD_DEGLEX and ORD_DEGREVLEX.

Initialises a context with a random number of variables between 0 and max_nvars and a random ordering, for testing.

Return the number of variables and the ordering of the context.

Frees the memory used by the context.

Memory management#

Initialise A to zero, in the last two versions with room for alloc terms, and in the last with exponent fields of bits bits (otherwise at least MPOLY_MIN_BITS).

Ensure room for len terms, ensure exponent fields of at least bits bits (repacking the terms), and reallocate the integer part to exactly alloc terms (its length must not exceed alloc).

Frees the memory used by A.

Input and output#

The variable names x are indexed by variable number. If x is NULL, the names x1, x2, ... are used.

char * fmpq_mpoly_get_str_pretty(const fmpq_mpoly_t A, const char ** x, const fmpq_mpoly_ctx_t ctx)

Returns a string representation of A, to be freed with flint_free.

int fmpq_mpoly_fprint_pretty(FILE * file, const fmpq_mpoly_t A, const char ** x, const fmpq_mpoly_ctx_t ctx)
int fmpq_mpoly_print_pretty(const fmpq_mpoly_t A, const char ** x, const fmpq_mpoly_ctx_t ctx)

Print A to file or to stdout, with coefficients such as 1/2*x^2.

int fmpq_mpoly_set_str_pretty(fmpq_mpoly_t A, const char * str, const char ** x, const fmpq_mpoly_ctx_t ctx)

Sets A to the polynomial given by the null-terminated string str and returns 0, or returns -1 if parsing fails, in which case the value of A is unspecified. The syntax is that of fmpz_mpoly_set_str_pretty: integers, the names in x, parentheses, +, -, *, / and ^ followed by a nonnegative integer; division by a polynomial must be exact, and division by integers gives rational coefficients.

Basic manipulation#

Sets A to the variable of index var.

If \mathit{var} \ge 0, returns whether A is the variable of index var; if \mathit{var} < 0, returns whether A is any variable.

Set A to B, and swap A and B in constant time.

Returns whether A = B.

Constants#

Returns whether A is a constant.

Sets c to the constant A; throws if A is not constant.

Return whether A is the constant c, 0 or 1.

Degrees#

The degree of the zero polynomial is -1.

Returns whether the degree of A in each variable fits an slong.

void fmpq_mpoly_degrees_fmpz(fmpz ** degs, const fmpq_mpoly_t A, const fmpq_mpoly_ctx_t ctx)
void fmpq_mpoly_degrees_si(slong * degs, const fmpq_mpoly_t A, const fmpq_mpoly_ctx_t ctx)

Set degs[i] to the degree of A in the variable of index i.

Set or return the degree of A in the variable of index var.

Return whether the total degree of A fits an slong, and set or return the total degree.

void fmpq_mpoly_used_vars(int * used, const fmpq_mpoly_t A, const fmpq_mpoly_ctx_t ctx)

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

Coefficients#

Sets d to the smallest positive integer such that dA has integer coefficients (the denominator of the content).

Returns whether the leading coefficient of A is 1 (false for zero).

Get or set the coefficient in A of the monomial of the single-term polynomial M (whose coefficient is ignored). Throw if M does not have exactly one term.

void fmpq_mpoly_get_coeff_fmpq_fmpz(fmpq_t c, const fmpq_mpoly_t A, fmpz * const * exp, const fmpq_mpoly_ctx_t ctx)
void fmpq_mpoly_get_coeff_fmpq_ui(fmpq_t c, const fmpq_mpoly_t A, const ulong * exp, const fmpq_mpoly_ctx_t ctx)

Set c to the coefficient of the monomial with exponent vector exp (zero if it does not occur). The underscore version takes the exponents as a plain array of fmpz.

void fmpq_mpoly_set_coeff_fmpq_fmpz(fmpq_mpoly_t A, const fmpq_t c, fmpz * const * exp, const fmpq_mpoly_ctx_t ctx)
void fmpq_mpoly_set_coeff_fmpq_ui(fmpq_mpoly_t A, const fmpq_t c, const ulong * exp, const fmpq_mpoly_ctx_t ctx)

Set the coefficient of the monomial with exponent vector exp to c, inserting or removing a term as needed.

void fmpq_mpoly_get_coeff_vars_ui(fmpq_mpoly_t C, const fmpq_mpoly_t A, const slong * vars, const ulong * exps, slong length, const fmpq_mpoly_ctx_t ctx)

Sets C to the coefficient of \prod_k x_{\mathit{vars}[k]}^{\mathit{exps}[k]} in A viewed as a polynomial in the variables vars (a polynomial in the other variables). The length variables in vars must be distinct, with 0 < \mathit{length} \le n.

Conversion#

Returns whether A involves no variable other than the one of index var.

Sets A to B as a univariate polynomial in the variable of index var and returns 1. Returns 0 if B involves another variable or the degree does not fit an slong.

Sets A to the univariate polynomial B in the variable of index var.

Comparison#

int fmpq_mpoly_cmp(const fmpq_mpoly_t A, const fmpq_mpoly_t B, const fmpq_mpoly_ctx_t ctx)

Returns 1, -1 or 0 according to a fixed total order on polynomials that agrees with the monomial ordering for monomials with equal coefficients.

Container operations#

These functions give access to the terms of A by index (from 0, in decreasing monomial order) and to the internal representation. An index outside [0, \mathit{length}) makes them throw. The mutating functions may leave A non-canonical; after setting up terms with distinct exponents in any order, call fmpq_mpoly_sort_terms and fmpq_mpoly_combine_like_terms, or fmpq_mpoly_reduce if the terms are already sorted, nonzero and distinct.

Return pointers to the content, to the integer part, and to the integer coefficient of the term of index i of the integer part.

Return whether A is in the canonical form described in the section on types, respectively throw an exception describing the defect if poly is not.

Returns the number of terms of A.

void fmpq_mpoly_resize(fmpq_mpoly_t A, slong new_length, const fmpq_mpoly_ctx_t ctx)

Sets the length of A to new_length, deleting terms at the end or appending terms with zero coefficient and zero exponents.

Get or set the coefficient of the term of index i.

Return whether every exponent of the term of index i fits an slong, respectively a ulong.

Set exps to the exponent vector of the term of index i. The ui and si versions throw if an exponent does not fit.

Return the exponent of the variable of index var in the term of index i; throw if it does not fit.

Set the exponent vector of the term of index i, which may break the ordering of the terms.

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

void fmpq_mpoly_push_term_fmpq_fmpz(fmpq_mpoly_t A, const fmpq_t c, fmpz * const * exp, const fmpq_mpoly_ctx_t ctx)
void fmpq_mpoly_push_term_fmpz_fmpz(fmpq_mpoly_t A, const fmpz_t c, fmpz * const * exp, const fmpq_mpoly_ctx_t ctx)
void fmpq_mpoly_push_term_fmpq_ui(fmpq_mpoly_t A, const fmpq_t c, const ulong * exp, const fmpq_mpoly_ctx_t ctx)
void fmpq_mpoly_push_term_fmpz_ui(fmpq_mpoly_t A, const fmpz_t c, const ulong * exp, const fmpq_mpoly_ctx_t ctx)

Append a term with coefficient c and exponent vector exp (an array of pointers to fmpz, an array of fmpz, or an array of ulong). The integer part and the denominator of the content are rescaled as needed, so the cost per term is constant on average when the denominators of the pushed coefficients are bounded. The result is not reduced; finish with fmpq_mpoly_reduce (or sort and combine first).

Moves the content and sign of the integer part into the content, so that the integer part is primitive with positive leading coefficient. If the terms of A are nonzero, sorted and have distinct exponents, the result is canonical.

A shortcut for fmpq_mpoly_reduce after an addition: if the integer part has easy_length terms (no cancellation happened), it is assumed to be primitive and only its sign is normalised; otherwise fmpq_mpoly_reduce is called. The integer part must have at most easy_length terms.

Sorts the terms of the integer part into decreasing monomial order, without combining like terms, removing zero terms or reducing.

Combines adjacent terms with equal exponents, removes zero terms and calls fmpq_mpoly_reduce. If the terms were sorted, the result is canonical.

Random generation#

The parameter coeff_bits is only a suggestion for the size of the coefficients, and the content is a random nonzero rational.

void fmpq_mpoly_randtest_bound(fmpq_mpoly_t A, flint_rand_t state, slong length, flint_bitcnt_t coeff_bits, ulong exp_bound, const fmpq_mpoly_ctx_t ctx)
void fmpq_mpoly_randtest_bounds(fmpq_mpoly_t A, flint_rand_t state, slong length, flint_bitcnt_t coeff_bits, ulong * exp_bounds, const fmpq_mpoly_ctx_t ctx)

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#

Set A to B + c, respectively B - c.

Set A to B + C, respectively B - C. The integer parts are combined with the cofactors of the GCD of the contents.

Scalar operations#

Unless B or the scalar is zero, these functions only change the content, so they take constant time when A and B are aliased.

Sets A to -B.

Sets A to B divided by its leading coefficient; throws if B is zero.

Differentiation and integration#

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

Sets A to the antiderivative of B with respect to the variable of index var that has no term free of that variable.

Evaluation and composition#

These functions return 1 for success and 0 if the result would be unreasonably large (for example a huge power of a rational with a large height).

int fmpq_mpoly_evaluate_all_fmpq(fmpq_t ev, const fmpq_mpoly_t A, fmpq * const * vals, const fmpq_mpoly_ctx_t ctx)

Sets ev to A evaluated at x_i = \mathit{vals}[i] for all i.

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

Sets A to B(C_0, \ldots, C_{n-1}) for univariate polynomials C_i.

Sets A to B(C_0, \ldots, C_{n-1}), where B is in ctxB and A and the C_i are in ctxAC. A must not alias B or any C_i.

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. Each nonnegative c[i] must be less than the number of variables of ctxAC.

Multiplication#

Sets A to BC, multiplying the integer parts with fmpz_mpoly_mul (see Multiplication).

Powering#

Set A to B^k. Return 0 if k is negative (in the fmpz version) or the result would be unreasonably large, and 1 otherwise.

Division#

Division by zero throws. Since \mathbb{Q} is a field, division with remainder is by the leading term of the divisor without any scaling: the remainder has no term divisible by the leading monomial of the divisor.

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

Set Q (and R) to the quotient (and remainder) of A by B, with A = QB + R and no term of R divisible by the leading monomial of B. They are computed with fmpz_mpoly_quasidiv and fmpz_mpoly_quasidivrem on the integer parts.

Generalised division by the len divisors B_i: sets the Q_i and R with A = \sum_i Q_i B_i + R and no term of R divisible by the leading monomial of any B_i.

Square roots#

If A is the square of a polynomial over \mathbb{Q}, sets Q to the square root with positive leading coefficient and returns 1; otherwise sets Q to zero and returns 0.

Returns whether A is the square of a polynomial over \mathbb{Q}.

Content and GCD#

The GCD functions return the monic GCD (zero for two zero inputs), computed with the function of the same name on the integer parts (see Greatest common divisors). They return 1 for success and 0 for failure, which happens only for unreasonably large inputs or when the requested algorithm does not apply; G is then unspecified.

Sets g to the nonnegative content of A (the absolute value of the stored content).

Sets M to the GCD of the terms of A: zero if A is zero, and otherwise the largest monomial dividing every term, with coefficient 1.

int fmpq_mpoly_content_vars(fmpq_mpoly_t g, const fmpq_mpoly_t A, slong * vars, slong vars_length, const fmpq_mpoly_ctx_t ctx)

Sets g to the GCD of the coefficients of A viewed as a polynomial in the variables vars, so that g does not involve these variables. Returns 1 for success and 0 for failure.

Sets G to the monic GCD of A and B.

Sets G to the monic GCD of A and B and also \bar A = A/G and \bar B = B/G.

Set G to the monic GCD of A and B using one particular algorithm. The warning on fmpz_mpoly_gcd_zippel applies to fmpq_mpoly_gcd_zippel.

Sets R to the resultant of A and B with respect to the variable of index var, computed with fmpz_mpoly_resultant on the integer parts and multiplied by the appropriate powers of the contents. Returns 1 for success and 0 for failure.

Sets R to the discriminant of A with respect to the variable of index var. Returns 1 for success and 0 for failure.

void fmpq_mpoly_inflate(fmpq_mpoly_t A, const fmpq_mpoly_t B, const fmpz * shift, const fmpz * stride, const fmpq_mpoly_ctx_t ctx)

Sets A to B with each exponent e of variable i replaced by \mathit{shift}[i] + \mathit{stride}[i] \cdot e.

Univariate polynomials with multivariate coefficients#

An fmpq_mpoly_univar_t holds a polynomial in one main variable with fmpq_mpoly_t coefficients in the other variables, which is useful for rewriting an element of \mathbb{Q}[x_0, \ldots, x_{n-1}] as an element of \mathbb{Q}[x_0, \ldots, \hat{x}_v, \ldots, x_{n-1}][x_v] and back.

A sparse univariate polynomial with fields coeffs (nonzero polynomials in the context of the multivariate polynomial, not involving the main variable), exps (strictly decreasing nonnegative fmpz exponents), alloc and length.

Initialise to zero, free, swap in constant time, and ensure room for length terms.

void fmpq_mpoly_univar_print_pretty(const fmpq_mpoly_univar_t A, const char ** x, const fmpq_mpoly_ctx_t ctx)

Prints A as a sum of terms (c)*X^e.

Throws if the exponents of A are not nonnegative and strictly decreasing or a coefficient is not canonical.

Sets A to B as a polynomial in the variable of index var.

Set A to the polynomial represented by B, with the variable of index var as main variable. The coefficients of B must not involve that variable. The second version packs the result into Abits bits, which must suffice.

Return whether the degree of A fits an slong, the number of terms, and the exponent of the term of index i.

Set c to, respectively swap c with, the coefficient of the term of index i.

Internal functions#

Prepares appending a term with coefficient C: multiplies the integer part by the numerator of the content (which becomes 1) and by the denominator of C \cdot \mathrm{den}(c), so that the new term can be appended with the integer coefficient left in the numerator of C.

Makes the canonical nonzero A monic by setting its content to the inverse of the leading coefficient of the integer part.

int _fmpq_mpoly_rescale(fmpq_t Acontent, fmpz * Acoeff, const fmpq_mpoly_t B, const fmpq * scales, const fmpq_mpoly_ctx_t ctx)

Writes B(s_0 x_0, \ldots, s_{n-1} x_{n-1}), for the rationals s_i in scales, as Acontent times the integer coefficients Acoeff on the monomials of B, by clearing the denominators b_i^{d_i} with d_i the maximal exponent of x_i and taking out a_i^{f_i} with f_i the minimal exponent. Returns 0 if the powers would be too large. Used by the composition functions.

Sets A to B with exponents packed into Abits bits. Returns 0 if the exponents do not fit, in which case A is unspecified.

Fills the generic ring interface R (used by the expression parser) with the operations of this module.

Test helper: throws if some term of r is divisible by the leading monomial of g, using fmpz_mpoly_remainder_strongtest on the integer parts.

Cite this page

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