FLINT3.7.0-dev
Module · 97 functions

ca_poly.h

#include <flint/ca_poly.h>

This module implements dense univariate polynomials whose coefficients are exact real or complex numbers of type ca_t (see ca.h), that is, elements of \mathbb{C}[x] with coefficients in the subfield of \mathbb{C} that ca_t can represent. Besides arithmetic and power series operations, it computes GCDs, squarefree factorisations and, where the roots can be expressed exactly, the roots of a polynomial with multiplicities.

Many operations are thin wrappers around the generic polynomial functions of gr_poly.h with a ca context; others have dedicated code that recognises coefficients lying in \mathbb{Q} or in a common number field and then uses fast arithmetic over \mathbb{Z} (fmpz_poly.h, fmpq_poly.h). Because coefficients are tested for zero with ca_check_is_zero, which may fail to decide, every operation that depends on a degree or on the invertibility of a leading coefficient can fail; such functions return an int success flag.

Types and conventions#

A polynomial with ca_t coefficients. The structure has the fields coeffs (an array of alloc initialised coefficients), length and alloc; the coefficient of x^i is coeffs[i]. A ca_poly_t is an array of one structure, so it is passed by reference.

Returns a pointer to the coefficient of x^i. The index must be less than the length (or the allocation, when writing coefficients before setting the length); this is not checked.

  • Normalisation. A polynomial is normalised by removing top coefficients that are proved to be zero. A top coefficient that cannot be proved zero is kept, so the length can exceed the degree of the mathematical polynomial plus one. When the exact degree matters, check the leading coefficient, for example with ca_poly_is_proper.
  • Special values. Undefined and infinities are not meaningful as coefficients. The functions in this module do not produce them from numerical input unless stated otherwise, and assume that the user does not store them. Unknown can occur as a coefficient to signal a number that could not be computed.
  • A polynomial is proper if all its coefficients are numbers and its leading coefficient is proved nonzero (or the polynomial is zero).
  • Aliasing. Functions without a leading underscore manage memory and allow outputs to be the same objects as inputs. Functions with a leading underscore work on coefficient arrays (poly, len) and leave memory management to the caller; unless stated otherwise they require positive lengths and do not allow the output to overlap the inputs.

Memory management#

void ca_poly_init2(ca_poly_t poly, slong len, ca_ctx_t ctx)

Initialises poly as the zero polynomial, with space for len coefficients for ca_poly_init2.

Clears poly, freeing the coefficients and the coefficient array.

Ensures that poly has at least len initialised coefficients allocated, growing the array to at least twice its size if it must grow. The length is not changed.

Sets the length of poly to len, which must not exceed the allocation. Coefficients beyond the new length are set to zero; nothing is normalised.

Decreases the length of poly while the top coefficient is proved to be zero by ca_check_is_zero.

void ca_poly_swap(ca_poly_t poly1, ca_poly_t poly2, ca_ctx_t ctx)

Swaps poly1 and poly2 in constant time.

Assignment and simple values#

void ca_poly_x(ca_poly_t poly, ca_ctx_t ctx)

Sets poly to the zero polynomial, the constant 1, or the monomial x.

void ca_poly_set_ca(ca_poly_t poly, const ca_t c, ca_ctx_t ctx)

Sets poly to the constant polynomial c (normalised, so the zero polynomial if c is proved zero).

void ca_poly_set(ca_poly_t res, const ca_poly_t src, ca_ctx_t ctx)

Sets res to the polynomial src.

void ca_poly_set_coeff_ca(ca_poly_t poly, slong n, const ca_t x, ca_ctx_t ctx)

Sets the coefficient of x^n to x, extending poly with zeros if needed, and normalises.

void ca_poly_transfer(ca_poly_t res, ca_ctx_t res_ctx, const ca_poly_t src, ca_ctx_t src_ctx)

Sets res, which belongs to the context res_ctx, to the polynomial src belonging to src_ctx, transferring each coefficient with ca_transfer.

Conversions#

void ca_poly_get_fexpr(fexpr_t res, const ca_poly_t A, ulong flags, ca_ctx_t ctx)

Sets res to a symbolic expression Polynomial(List(c_0, ..., c_n)) for A, wrapped in Where(..., Def(a_1, ...), ...) that defines the extension numbers occurring in the coefficients, as for ca_get_fexpr, which also explains flags.

Random generation#

void ca_poly_randtest(ca_poly_t poly, flint_rand_t state, slong len, slong depth, slong bits, ca_ctx_t ctx)

Sets poly to a random polynomial of length up to len whose coefficients are generated by ca_randtest with the parameters depth and bits.

Sets poly to a random polynomial of length up to len with rational coefficients of up to bits bits.

Input and output#

void ca_poly_print(const ca_poly_t poly, ca_ctx_t ctx)

Prints the line ca_poly of length n: followed by the coefficients, from the constant term upwards, one per line with ca_print and indented by four spaces, and a blank line.

void ca_poly_printn(const ca_poly_t poly, slong digits, ca_ctx_t ctx)

Prints decimal approximations of the coefficients with digits significant digits, comma-separated in square brackets, followed by a newline.

Degree and leading coefficient#

int ca_poly_is_proper(const ca_poly_t poly, ca_ctx_t ctx)

Returns 1 if all coefficients of poly are numbers (not special values) and the leading coefficient is proved nonzero, so that poly is an element of \mathbb{C}[x] of known degree; also returns 1 for the zero polynomial. Returns 0 otherwise.

Sets res to poly divided by its leading coefficient and returns 1, or returns 0 if poly is zero or its leading coefficient is not proved nonzero.

void _ca_poly_reverse(ca_ptr res, ca_srcptr poly, slong len, slong n, ca_ctx_t ctx)
void ca_poly_reverse(ca_poly_t res, const ca_poly_t poly, slong n, ca_ctx_t ctx)

Sets res to the reversal x^{n-1} f(1/x) of poly considered as a polynomial of length n, truncating or zero-padding as needed. The underscore version requires 0 < \mathit{len} \le n. Wrappers of _gr_poly_reverse.

Comparisons#

truth_t ca_poly_check_equal(const ca_poly_t poly1, const ca_poly_t poly2, ca_ctx_t ctx)

Checks whether poly1 and poly2 are equal as polynomials, comparing coefficients with ca_check_equal (and the excess coefficients of the longer one with zero). The underscore version requires \mathit{len1} \ge \mathit{len2}. Wrappers of _gr_poly_equal.

Checks whether poly is the zero polynomial or the constant 1.

Arithmetic#

void ca_poly_shift_left(ca_poly_t res, const ca_poly_t poly, slong n, ca_ctx_t ctx)
void ca_poly_shift_right(ca_poly_t res, const ca_poly_t poly, slong n, ca_ctx_t ctx)

Sets res to poly multiplied by x^n, or divided by x^n with the n lowest coefficients discarded. Wrappers of _gr_poly_shift_left and _gr_poly_shift_right.

void ca_poly_neg(ca_poly_t res, const ca_poly_t src, ca_ctx_t ctx)

Sets res to -\mathit{src}.

void _ca_poly_add(ca_ptr res, ca_srcptr poly1, slong len1, ca_srcptr poly2, slong len2, ca_ctx_t ctx)
void ca_poly_add(ca_poly_t res, const ca_poly_t poly1, const ca_poly_t poly2, ca_ctx_t ctx)
void _ca_poly_sub(ca_ptr res, ca_srcptr poly1, slong len1, ca_srcptr poly2, slong len2, ca_ctx_t ctx)
void ca_poly_sub(ca_poly_t res, const ca_poly_t poly1, const ca_poly_t poly2, ca_ctx_t ctx)

Sets res to the sum or difference of poly1 and poly2. The underscore versions write \max(\mathit{len1}, \mathit{len2}) coefficients and allow aliasing. Wrappers of _gr_poly_add and _gr_poly_sub.

void _ca_poly_mul(ca_ptr C, ca_srcptr A, slong lenA, ca_srcptr B, slong lenB, ca_ctx_t ctx)
void ca_poly_mul(ca_poly_t res, const ca_poly_t poly1, const ca_poly_t poly2, ca_ctx_t ctx)
void ca_poly_mullow(ca_poly_t res, const ca_poly_t poly1, const ca_poly_t poly2, slong n, ca_ctx_t ctx)

Sets res to the product of poly1 and poly2, or to the product truncated to length n. The underscore versions require \mathit{lenA} \ge \mathit{lenB} \ge 1 and 0 < n \le \mathit{lenA} + \mathit{lenB} - 1, and write lenA + lenB - 1 or n coefficients.

More

Multiplies polynomials all of whose coefficients are rational numbers or elements of the number field K = \mathbb{Q}(a) of degree m, by Kronecker substitution: the coefficients are written as polynomials in a over a common denominator, packed into a single polynomial over \mathbb{Z} of length about \mathit{Alen} \cdot (2m - 1), multiplied with _fmpz_poly_mullow and unpacked, followed by reduction modulo the minimal polynomial of a. Requirements as for _ca_poly_mullow.

void ca_poly_mul_ca(ca_poly_t res, const ca_poly_t poly, const ca_t c, ca_ctx_t ctx)
void ca_poly_div_ca(ca_poly_t res, const ca_poly_t poly, const ca_t c, ca_ctx_t ctx)
void ca_poly_div_fmpz(ca_poly_t res, const ca_poly_t poly, const fmpz_t c, ca_ctx_t ctx)

Sets res to poly multiplied or divided by the scalar c. Division by zero gives special values as coefficients (see _ca_vec_scalar_div_ca).

void _ca_poly_divrem_basecase(ca_ptr Q, ca_ptr R, ca_srcptr A, slong lenA, ca_srcptr B, slong lenB, const ca_t invB, ca_ctx_t ctx)
void _ca_poly_divrem(ca_ptr Q, ca_ptr R, ca_srcptr A, slong lenA, ca_srcptr B, slong lenB, const ca_t invB, ca_ctx_t ctx)
int ca_poly_div(ca_poly_t Q, const ca_poly_t A, const ca_poly_t B, ca_ctx_t ctx)
int ca_poly_rem(ca_poly_t R, const ca_poly_t A, const ca_poly_t B, ca_ctx_t ctx)

If the leading coefficient of B is proved nonzero, sets Q and R to the quotient and remainder of the division of A by B, with \deg R < \deg B, and returns 1. Otherwise, in particular if B is zero, returns 0 and leaves the outputs unspecified.

More
void _ca_poly_pow_ui_trunc(ca_ptr res, ca_srcptr f, slong flen, ulong exp, slong len, ca_ctx_t ctx)
void ca_poly_pow_ui_trunc(ca_poly_t res, const ca_poly_t poly, ulong exp, slong len, ca_ctx_t ctx)
void _ca_poly_pow_ui(ca_ptr res, ca_srcptr f, slong flen, ulong exp, ca_ctx_t ctx)
void ca_poly_pow_ui(ca_poly_t res, const ca_poly_t poly, ulong exp, ca_ctx_t ctx)

Sets res to poly raised to the power exp, truncated to length len for the trunc versions. Exponents larger than 2 use _gr_poly_pow_series_ui_binexp. The underscore trunc version writes len coefficients and the other one \mathit{exp} (\mathit{flen} - 1) + 1.

Evaluation and composition#

void _ca_poly_evaluate_horner(ca_t res, ca_srcptr f, slong len, const ca_t x, ca_ctx_t ctx)
void ca_poly_evaluate_horner(ca_t res, const ca_poly_t f, const ca_t a, ca_ctx_t ctx)
void _ca_poly_evaluate(ca_t res, ca_srcptr f, slong len, const ca_t x, ca_ctx_t ctx)
void ca_poly_evaluate(ca_t res, const ca_poly_t f, const ca_t a, ca_ctx_t ctx)

Sets res to f(a), by Horner's rule or by the default algorithm of _gr_poly_evaluate, of which these are wrappers.

void _ca_poly_compose(ca_ptr res, ca_srcptr poly1, slong len1, ca_srcptr poly2, slong len2, ca_ctx_t ctx)
void ca_poly_compose(ca_poly_t res, const ca_poly_t poly1, const ca_poly_t poly2, ca_ctx_t ctx)

Sets res to the composition \mathit{poly1}(\mathit{poly2}(x)). Wrappers of _gr_poly_compose; the underscore version writes (\mathit{len1} - 1)(\mathit{len2} - 1) + 1 coefficients.

Derivative and integral#

void ca_poly_derivative(ca_poly_t res, const ca_poly_t poly, ca_ctx_t ctx)

Sets res to the derivative of poly. The underscore version writes \mathit{len} - 1 coefficients and is a wrapper of _gr_poly_derivative.

void _ca_poly_integral(ca_ptr res, ca_srcptr poly, slong len, ca_ctx_t ctx)
void ca_poly_integral(ca_poly_t res, const ca_poly_t poly, ca_ctx_t ctx)

Sets res to the integral of poly with constant term zero. The underscore version writes \mathit{len} + 1 coefficients.

Greatest common divisor#

int ca_poly_gcd(ca_poly_t G, const ca_poly_t A, const ca_poly_t B, ca_ctx_t ctx)

Sets G to the monic greatest common divisor of A and B (zero if both are zero) and returns 1. Returns 0, leaving G unspecified, if a zero test of a leading coefficient fails during the computation (in particular if the leading coefficient of an input is not proved nonzero).

More

Power series#

void ca_poly_inv_series(ca_poly_t res, const ca_poly_t f, slong len, ca_ctx_t ctx)

Sets res to the power series inverse 1/f \bmod x^{\mathit{len}}. The constant term must be invertible: if it is zero the result consists of special values (unsigned infinity or Undefined), and Unknown values if it cannot be decided whether it is zero. Rational series are inverted over \mathbb{Q}; otherwise the classical recurrence or, for long series with fast multiplication, Newton iteration is used.

void _ca_poly_div_series(ca_ptr res, ca_srcptr f, slong flen, ca_srcptr g, slong glen, slong len, ca_ctx_t ctx)
void ca_poly_div_series(ca_poly_t res, const ca_poly_t f, const ca_poly_t g, slong len, ca_ctx_t ctx)

Sets res to the power series quotient f/g \bmod x^{\mathit{len}}. As for ca_poly_inv_series, the constant term of g must be nonzero; remove common factors x^k first when an exact cancellation is expected.

void ca_poly_exp_series(ca_poly_t res, const ca_poly_t f, slong len, ca_ctx_t ctx)

Sets res to the power series \exp(f) \bmod x^{\mathit{len}}. The constant term is \exp(f_0) computed with ca_exp. Rational series use _fmpq_poly_exp_series; otherwise the basecase recurrence or Newton iteration is used.

void ca_poly_log_series(ca_poly_t res, const ca_poly_t f, slong len, ca_ctx_t ctx)

Sets res to the power series \log(f) \bmod x^{\mathit{len}}, computed as \log(f_0) + \int f'/f; the constant term must be nonzero. Binomials a + b x^d are handled directly.

Roots and factorisation#

Computes the squarefree factorisation F = c \, f_1^{e_1} \cdots f_k^{e_k}, where the f_i are monic, squarefree, pairwise coprime and nonconstant and e_1 < \cdots < e_k. Sets c to the leading coefficient, fac to the vector of the f_i and the first k entries of exp to the e_i, and returns 1. Returns 0 if F is not proper or if a GCD computation fails. The array exp must have room for \deg F entries.

Full documentation

Sets res to the monic squarefree part f / \gcd(f, f') of poly and returns 1, or returns 0 if a GCD computation fails.

Full documentation
void _ca_poly_set_roots(ca_ptr poly, ca_srcptr roots, const ulong * exp, slong len, ca_ctx_t ctx)
void ca_poly_set_roots(ca_poly_t poly, ca_vec_t roots, const ulong * exp, ca_ctx_t ctx)

Sets poly to the monic polynomial (x - r_0)^{e_0} \cdots (x - r_{n-1})^{e_{n-1}} with the roots r_i in roots (of length len, or the length of the vector) and the multiplicities e_i in exp. The product is computed by binary splitting. The underscore version writes e_0 + \cdots + e_{n-1} + 1 coefficients.

Full documentation
int _ca_poly_roots(ca_ptr roots, ca_srcptr poly, slong len, ca_ctx_t ctx)
int ca_poly_roots(ca_vec_t roots, ulong * exp, const ca_poly_t poly, ca_ctx_t ctx)

Attempts to compute all complex roots of poly. On success, returns 1, sets roots to the distinct roots in no particular order and exp to their multiplicities. Returns 0 on failure, leaving the outputs unspecified. See Roots and squarefree factorisation.

Full documentation

Vectors of polynomials#

A vector of polynomials, with the fields entries (an array of alloc initialised polynomials), length and alloc. A ca_poly_vec_t is an array of one structure.

Allocates an array of len zero polynomials, or initialises res as a vector of len zero polynomials.

Ensures that vec has space for len polynomials, without changing its length.

Sets the length of vec to len, appending zero polynomials or truncating as needed.

Clears the polynomials and frees the array.

Appends a copy of f to vec.

Cite this page

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