FLINT3.7.0-dev
Module · 173 functions

fmpq_mpoly.h

#include <flint/fmpq_mpoly.h>

This module implements sparse polynomials in Q[x0,…,xn−1]\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 Q\mathbb{Q}, see gr_mpoly; fmpq_mpoly is the specialised and faster implementation for Q\mathbb{Q}.

Types and conventions#

A polynomial A=c⋅ZA = c \cdot Z with fields content (the rational cc, an fmpq_t) and zpoly (the integer polynomial ZZ, an fmpz_mpoly_t). The representation is canonical when either both are zero, or c≠0c \ne 0 and ZZ is a canonical fmpz_mpoly whose coefficients have GCD 1 and whose leading coefficient is positive. Then the sign of AA is carried by cc. 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 AA 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 AA.

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 AA, 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 AA 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 AA to the polynomial given by the null-terminated string str and returns 0, or returns −1-1 if parsing fails, in which case the value of AA 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 AA to the variable of index var.

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

Set AA to BB, and swap AA and BB in constant time.

Returns whether A=BA = B.

Constants#

Returns whether AA is a constant.

Sets cc to the constant AA; throws if AA is not constant.

Set AA to the constant cc, to 0 or to 1.

Return whether AA is the constant cc, 0 or 1.

Degrees#

The degree of the zero polynomial is −1-1.

Returns whether the degree of AA 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 AA in the variable of index ii.

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

Return whether the total degree of AA 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 ii occurs in AA, and to zero otherwise.

Coefficients#

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

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

Get or set the coefficient in AA of the monomial of the single-term polynomial MM (whose coefficient is ignored). Throw if MM 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 cc 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 cc, 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 CC to the coefficient of ∏kxvars[k]exps[k]\prod_k x_{\mathit{vars}[k]}^{\mathit{exps}[k]} in AA viewed as a polynomial in the variables vars (a polynomial in the other variables). The length variables in vars must be distinct, with 0<length≤n0 < \mathit{length} \le n.

Conversion#

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

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

Sets AA to the univariate polynomial BB 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 11, −1-1 or 00 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 AA by index (from 0, in decreasing monomial order) and to the internal representation. An index outside [0,length)[0, \mathit{length}) makes them throw. The mutating functions may leave AA 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 ii of the integer part.

Return whether AA 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 AA.

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

Sets the length of AA 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 ii.

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

Set exps to the exponent vector of the term of index ii. 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 ii; throw if it does not fit.

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

Set MM to the term of index ii, 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 cc 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 AA 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 AA to a random polynomial with at most length terms and exponents below exp_bound, below exp_bounds[i] for variable ii, or of at most exp_bits bits.

Addition and subtraction#

Set AA to B+cB + c, respectively B−cB - c.

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

Scalar operations#

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

Sets AA to −B-B.

Set AA to B/cB/c for nonzero cc.

Sets AA to BB divided by its leading coefficient; throws if BB is zero.

Differentiation and integration#

Sets AA to the derivative of BB with respect to the variable of index var.

Sets AA to the antiderivative of BB 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 AA evaluated at xi=vals[i]x_i = \mathit{vals}[i] for all ii.

Sets AA to BB with the variable of index var replaced by val.

Sets AA to B(C0,…,Cn−1)B(C_0, \ldots, C_{n-1}) for univariate polynomials CiC_i.

Sets AA to B(C0,…,Cn−1)B(C_0, \ldots, C_{n-1}), where BB is in ctxB and AA and the CiC_i are in ctxAC. AA must not alias BB or any CiC_i.

Sets AA to BB with the variable of index ii 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 AA to BCBC, multiplying the integer parts with fmpz_mpoly_mul (see Multiplication).

Powering#

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

Division#

Division by zero throws. Since Q\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 BB divides AA, sets Q=A/BQ = A/B and returns 1; otherwise sets QQ to zero and returns 0. See Division.

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

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

Square roots#

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

Returns whether AA is the square of a polynomial over Q\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; GG is then unspecified.

Sets gg to the nonnegative content of AA (the absolute value of the stored content).

Sets MM to the GCD of the terms of AA: zero if AA 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 gg to the GCD of the coefficients of AA viewed as a polynomial in the variables vars, so that gg does not involve these variables. Returns 1 for success and 0 for failure.

Sets GG to the monic GCD of AA and BB.

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

Set GG to the monic GCD of AA and BB using one particular algorithm. The warning on fmpz_mpoly_gcd_zippel applies to fmpq_mpoly_gcd_zippel.

Sets RR to the resultant of AA and BB 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 RR to the discriminant of AA 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 AA to BB with each exponent ee of variable ii replaced by shift[i]+stride[i]⋅e\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 Q[x0,…,xn−1]\mathbb{Q}[x_0, \ldots, x_{n-1}] as an element of Q[x0,…,x^v,…,xn−1][xv]\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 AA as a sum of terms (c)*X^e.

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

Sets AA to BB as a polynomial in the variable of index var.

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

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

Set cc to, respectively swap cc with, the coefficient of the term of index ii.

Internal functions#

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

Makes the canonical nonzero AA 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(s0x0,…,sn−1xn−1)B(s_0 x_0, \ldots, s_{n-1} x_{n-1}), for the rationals sis_i in scales, as Acontent times the integer coefficients Acoeff on the monomials of BB, by clearing the denominators bidib_i^{d_i} with did_i the maximal exponent of xix_i and taking out aifia_i^{f_i} with fif_i the minimal exponent. Returns 0 if the powers would be too large. Used by the composition functions.

Sets AA to BB with exponents packed into Abits bits. Returns 0 if the exponents do not fit, in which case AA 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 rr is divisible by the leading monomial of gg, 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-11-02).