FLINT3.7.0-dev
Module · 86 functions

fmpz_vec.h

#include <flint/fmpz_vec.h>

This module implements operations on vectors of integers. A vector is a plain array of fmpz together with a length, written (vec, len). It is the coefficient array of fmpz_poly and the row storage of fmpz_mat. The functions, all with a leading underscore, leave memory management to the caller. The module also has a small resizable vector type, fmpz_vec_t. See Arrays of integers for how to work with arrays of integers, and Dot products of integer vectors for dot products.

Conventions#

  • A vector (vec, len) is an array of at least len initialised fmpz. A length of zero is allowed unless stated otherwise.
  • Functions with an output vector allow it to be the same array as an input vector (exact aliasing), but not a partial overlap. A scalar output such as res in _fmpz_vec_sum must not be an entry of an input vector.
  • As for fmpz, division by zero raises an exception, and exact divisions are not checked.

Memory management#

Returns an array of len integers, all zero, allocated with flint_calloc (an fmpz with value zero is a zero word).

void _fmpz_vec_clear(fmpz * vec, slong len)

Clears the len entries of vec and frees the array.

Allocate a temporary vector of len initialised integers with TMP_ALLOC and assign it to vec, respectively clear its entries. Use between TMP_START and TMP_END.

Decreases the variable i while vec[i - 1] is zero, that is, strips zero entries from the end of a vector of length i. Used to normalise polynomial lengths.

FMPZ_VEC_SWAP(vec1, len1, vec2, len2)

Swaps the pointers vec1 and vec2 and the lengths len1 and len2.

Resizable vectors#

A vector that manages its own memory, with fields entries (an array of alloc initialised integers, of which the first length are in use), alloc and length. Entries from length to alloc - 1 are always zero.

Initialise vec to a vector of length len with all entries zero, respectively release its memory.

Returns the length of vec.

fmpz * fmpz_vec_entry_ptr(fmpz_vec_t vec, slong i)
const fmpz * fmpz_vec_entry_srcptr(const fmpz_vec_t vec, slong i)

Return a pointer to entry i, which can be used as an fmpz_t. The index is not checked.

Ensures that vec has room for at least len entries, at least doubling the allocation when it grows. The length is not changed.

Sets the length to len \ge 0. Entries beyond a smaller new length are set to zero (releasing their memory); entries added by a larger new length are zero.

void fmpz_vec_set(fmpz_vec_t res, const fmpz_vec_t src)

Sets res to a copy of src.

void fmpz_vec_append(fmpz_vec_t vec, const fmpz_t f)

Append f to vec, increasing its length by one.

Random vectors#

Set the entries to random integers of at most bits bits, generated by fmpz_randtest (respectively fmpz_randtest_unsigned). With probability 1/2 the vector is made sparse, with a random proportion of zero entries, to exercise special cases in tests.

Bit sizes and norms#

Return b or -b, where b is the maximum of the bit sizes of the absolute values of the entries; the sign is negative if some entry is negative. The _ref version is a simple reference implementation used in tests.

Returns the sum of the bit sizes fmpz_bits of the entries.

void _fmpz_vec_sum_max_bits(slong * sumabs, slong * maxabs, const fmpz * vec, slong len)

Sets *sumabs to the bit size of the sum of the absolute values of the entries, and *maxabs to the bit size of the largest absolute value.

Returns the maximum number of limbs of the absolute values of the entries, and 0 if all entries are zero.

void _fmpz_vec_height(fmpz_t height, const fmpz * vec, slong len)

_fmpz_vec_height sets height to the largest absolute value of an entry (the \ell^\infty norm), which is 0 for len = 0. _fmpz_vec_height_index returns the index of an entry of largest absolute value and requires len \ge 1.

Input and output#

int _fmpz_vec_fprint(FILE * file, const fmpz * vec, slong len)
int _fmpz_vec_print(const fmpz * vec, slong len)

Print the vector to file or stdout as the length, two spaces and the entries separated by single spaces; a vector of length zero is printed as 0. Return a positive value on success and a nonpositive value on failure.

int _fmpz_vec_fread(FILE * file, fmpz ** vec, slong * len)
int _fmpz_vec_read(fmpz ** vec, slong * len)

Read a vector in the format of _fmpz_vec_fprint from file or stdin. Return a positive value on success and a nonpositive value on failure.

More

Conversions#

void _fmpz_vec_get_nmod_vec(nn_ptr res, const fmpz * poly, slong len, nmod_t mod)

Sets res[i] to the residue in [0, n) of poly[i] modulo the modulus n of mod.

void _fmpz_vec_set_nmod_vec(fmpz * res, nn_srcptr poly, slong len, nmod_t mod)

Sets res[i] to the symmetric representative in (-n/2, n/2] of the reduced residue poly[i] modulo n.

slong _fmpz_vec_get_d_vec_2exp(double * appv, const fmpz * vec, slong len)

Returns an exponent e and sets appv[i] to doubles with appv[i]\cdot 2^e \approx vec[i], where e is the largest exponent of fmpz_get_d_2exp over the entries, so that all appv[i] have absolute value less than 1. Entries much smaller than the largest lose precision or become 0.

void _fmpz_vec_get_fft(ulong ** coeffs_f, const fmpz * coeffs_m, slong l, slong length)
void _fmpz_vec_set_fft(fmpz * coeffs_m, slong length, const nn_ptr * coeffs_f, slong limbs, slong sign)

Convert between a vector of integers and the coefficient arrays of the Schönhage–Strassen FFT of the fft module, used by fmpz_poly_mul_SS. _fmpz_vec_get_fft writes each of the length entries to coeffs_f[i] as l limbs plus one limb for overflow, negative values in two's complement over the l + 1 limbs (the entries must fit in l limbs). The conversion is done in parallel when threads are available. _fmpz_vec_set_fft converts fully reduced residues of limbs limbs back, to the range [-N, N] with N = 2^{\mathtt{FLINT\_BITS} \cdot \mathit{limbs} - 1} if sign is nonzero and to [0, 2N] otherwise.

Assignment and comparison#

void _fmpz_vec_set(fmpz * vec1, const fmpz * vec2, slong len2)
void _fmpz_vec_swap(fmpz * vec1, fmpz * vec2, slong len2)
void _fmpz_vec_zero(fmpz * vec, slong len)

Copy vec2 to vec1, swap the entries of the two vectors (exchanging words, without copying limbs), and set all entries to zero.

int _fmpz_vec_equal(const fmpz * vec1, const fmpz * vec2, slong len)
int _fmpz_vec_is_zero(const fmpz * vec, slong len)

Return whether the two vectors are equal, respectively whether all entries are zero.

void _fmpz_vec_max(fmpz * vec1, const fmpz * vec2, const fmpz * vec3, slong len)
void _fmpz_vec_min(fmpz * vec1, const fmpz * vec2, const fmpz * vec3, slong len)
void _fmpz_vec_max_inplace(fmpz * vec1, const fmpz * vec2, slong len)
void _fmpz_vec_min_inplace(fmpz * vec1, const fmpz * vec2, slong len)

Set vec1 to the entrywise maximum (minimum) of vec2 and vec3, respectively of vec1 and vec2.

void _fmpz_vec_sort(fmpz * vec, slong len)

Sorts the entries in increasing order (with qsort and fmpz_cmp).

Addition and negation#

void _fmpz_vec_add(fmpz * res, const fmpz * vec1, const fmpz * vec2, slong len2)
void _fmpz_vec_sub(fmpz * res, const fmpz * vec1, const fmpz * vec2, slong len2)

Set res to the entrywise sum, respectively difference, of the vectors of length len2.

void _fmpz_vec_neg(fmpz * vec1, const fmpz * vec2, slong len2)
void _fmpz_vec_scalar_abs(fmpz * vec1, const fmpz * vec2, slong len2)

Set vec1 to the negation, respectively the entrywise absolute value, of vec2.

Scalar multiplication and division#

void _fmpz_vec_scalar_mul_fmpz(fmpz * vec1, const fmpz * vec2, slong len2, const fmpz_t x)
void _fmpz_vec_scalar_mul_si(fmpz * vec1, const fmpz * vec2, slong len2, slong c)
void _fmpz_vec_scalar_mul_ui(fmpz * vec1, const fmpz * vec2, slong len2, ulong c)
void _fmpz_vec_scalar_mul_2exp(fmpz * vec1, const fmpz * vec2, slong len2, ulong exp)

Set vec1 to vec2 multiplied by the scalar (x, c or 2^{\mathit{exp}}).

void _fmpz_vec_scalar_addmul_fmpz(fmpz * poly1, const fmpz * poly2, slong len2, const fmpz_t x)
void _fmpz_vec_scalar_addmul_si(fmpz * vec1, const fmpz * vec2, slong len2, slong c)
void _fmpz_vec_scalar_addmul_ui(fmpz * vec1, const fmpz * vec2, slong len2, ulong c)
void _fmpz_vec_scalar_submul_fmpz(fmpz * vec1, const fmpz * vec2, slong len2, const fmpz_t x)
void _fmpz_vec_scalar_submul_si(fmpz * vec1, const fmpz * vec2, slong len2, slong c)

Add (respectively subtract) the second vector multiplied by the scalar to (from) the first.

void _fmpz_vec_scalar_addmul_si_2exp(fmpz * vec1, const fmpz * vec2, slong len2, slong c, ulong exp)
void _fmpz_vec_scalar_submul_si_2exp(fmpz * vec1, const fmpz * vec2, slong len2, slong c, ulong exp)

Add (respectively subtract) vec2 multiplied by c 2^{\mathit{exp}} to (from) vec1.

void _fmpz_vec_scalar_divexact_fmpz(fmpz * vec1, const fmpz * vec2, slong len2, const fmpz_t x)
void _fmpz_vec_scalar_divexact_si(fmpz * vec1, const fmpz * vec2, slong len2, slong c)
void _fmpz_vec_scalar_divexact_ui(fmpz * vec1, const fmpz * vec2, slong len2, ulong c)

Set vec1 to vec2 divided by the nonzero scalar, assuming that every division is exact (not checked).

More
void _fmpz_vec_scalar_divexact_fmpz_strided(fmpz * B, slong Bstride, const fmpz * A, slong Astride, slong r, slong c, const fmpz_t x)

Sets the r \times c array B to A divided exactly by x, where row i of B (respectively A) is the c entries starting at B + i * Bstride (A + i * Astride). Exactness is not checked. In-place operation is allowed with B = A and Bstride = Astride.

More
void _fmpz_vec_scalar_fdiv_q_fmpz(fmpz * vec1, const fmpz * vec2, slong len2, const fmpz_t c)
void _fmpz_vec_scalar_fdiv_q_si(fmpz * vec1, const fmpz * vec2, slong len2, slong c)
void _fmpz_vec_scalar_fdiv_q_ui(fmpz * vec1, const fmpz * vec2, slong len2, ulong c)
void _fmpz_vec_scalar_tdiv_q_fmpz(fmpz * vec1, const fmpz * vec2, slong len2, const fmpz_t c)
void _fmpz_vec_scalar_tdiv_q_si(fmpz * vec1, const fmpz * vec2, slong len2, slong c)
void _fmpz_vec_scalar_tdiv_q_ui(fmpz * vec1, const fmpz * vec2, slong len2, ulong c)

Set vec1 to the quotients of the entries of vec2 by c \ne 0, rounded towards -\infty (fdiv) or towards zero (tdiv); see Division and rounding conventions.

void _fmpz_vec_scalar_fdiv_q_2exp(fmpz * vec1, const fmpz * vec2, slong len2, ulong exp)
void _fmpz_vec_scalar_tdiv_q_2exp(fmpz * vec1, const fmpz * vec2, slong len2, ulong exp)
void _fmpz_vec_scalar_fdiv_r_2exp(fmpz * vec1, const fmpz * vec2, slong len2, ulong exp)

Set vec1 to the quotients of the entries by 2^{\mathit{exp}}, rounded towards -\infty or towards zero, respectively to the remainders of floor division, which lie in [0, 2^{\mathit{exp}}).

Reduction and Chinese remaindering#

void _fmpz_vec_scalar_mod_fmpz(fmpz * res, const fmpz * vec, slong len, const fmpz_t p)
void _fmpz_vec_scalar_smod_fmpz(fmpz * res, const fmpz * vec, slong len, const fmpz_t p)

Reduce the entries modulo p > 0, to [0, p), respectively to the symmetric range (-p/2, p/2].

void _fmpz_vec_multi_CRT_ui(fmpz * res, nn_srcptr * residues, slong len, nn_srcptr primes, slong num_primes, int sign)

Sets res[j] for 0 \le j < len to the integer congruent to residues[i][j] modulo primes[i] for all 0 \le i < num_primes, in [0, M) if sign is 0 and in (-M/2, M/2] otherwise, where M is the product of the pairwise coprime word-size moduli. The precomputation is shared by all entries; each entry is reconstructed as by fmpz_multi_CRT_ui (Chinese remaindering).

Sums, products and content#

void _fmpz_vec_sum(fmpz_t res, const fmpz * vec, slong len)

Sets res to the sum of the entries.

void _fmpz_vec_prod(fmpz_t res, const fmpz * vec, slong len)

Set res to the product of the entries of an array of integers, respectively of words; the empty product is 1. Both use a balanced product tree (Binary splitting and product trees), with leaves of at most 3 integers, respectively 15 words, multiplied in sequence.

void _fmpz_vec_content(fmpz_t res, const fmpz * vec, slong len)
void _fmpz_vec_content_chained(fmpz_t res, const fmpz * vec, slong len, const fmpz_t inp)

Set res to the nonnegative GCD of the entries (0 for a zero or empty vector), respectively of the entries and inp, which allows the common content of several vectors to be accumulated. The computation stops as soon as the GCD becomes 1.

void _fmpz_vec_lcm(fmpz_t res, const fmpz * vec, slong len)

Sets res to the nonnegative least common multiple of the entries: 0 if an entry is zero and 1 for an empty vector.

Dot products#

void _fmpz_vec_dot_general(fmpz_t res, const fmpz_t initial, int subtract, const fmpz * a, const fmpz * b, int reverse, slong len)
void _fmpz_vec_dot_general_naive(fmpz_t res, const fmpz_t initial, int subtract, const fmpz * a, const fmpz * b, int reverse, slong len)

Set res to s + (-1)^{\mathit{subtract}} \sum_{i=0}^{\mathit{len}-1} a_i b_i, where s is initial (or 0 if initial is NULL), subtract is 0 or 1, and b is read in reverse order (b_{len-1-i} in place of b_i) if reverse is 1. res may alias initial but not an entry of a or b. The _naive version is a loop of fmpz_addmul used in tests.

Full documentation
void _fmpz_vec_dot(fmpz_t res, const fmpz * vec1, const fmpz * vec2, slong len2)

Sets res to \sum_i vec1[i] vec2[i]; equivalent to _fmpz_vec_dot_general(res, NULL, 0, vec1, vec2, 0, len2).

Full documentation
Cite this page

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