FLINT3.7.0-dev
Module · 289 functions

fmpz.h

#include <flint/fmpz.h>

This module implements the integers Z\mathbb{Z}. An fmpz_t holds an integer of any size and is the coefficient type of most of FLINT: polynomials, matrices, rationals and number fields over Z\mathbb{Z} are arrays of fmpz. A value whose absolute value is at most COEFF_MAX =2FLINT_BITS−2−1= 2^{\mathtt{FLINT\_BITS} - 2} - 1 is stored directly in one machine word; larger values are stored in a GMP mpz structure that the word points to. The switch between the two forms is automatic, so fmpz_t behaves like GMP's mpz_t, but small values cost no memory allocation and most operations on them run without a function call into GMP. Large values use FLINT's own multiplication, division and square root code where it is faster than GMP's.

Start with Getting started with fmpz, and read The fmpz representation before writing code that accesses the representation or creates large arrays of integers. The naming, memory and aliasing conventions common to all FLINT modules are described in Types, naming and aliasing.

OverviewMultiplying integersHow FLINT multiplies integers of each size — its own assembly basecases, Karatsuba, GMP's Toom–Cook algorithms, and FFT multiplication with fft_small or Schönhage–Strassen — with the algorithms, their costs and the dispatch in flint_mpn_mul.

Guides#

Types and conventions#

An fmpz is an slong. If its two most significant bits are equal (0000 or 1111), it is an integer xx with ∣x∣≤|x| \le COEFF_MAX, stored directly. If they are 0101, the remaining bits hold a pointer to an mpz_struct shifted right by two bits, and the value is that of the mpz. An fmpz_t is an array of one fmpz, so that it is passed by reference like an mpz_t. A pointer fmpz * to an entry of an array can be passed wherever an fmpz_t is expected.

More

The largest and the smallest value stored inline: 2FLINT_BITS−2−12^{\mathtt{FLINT\_BITS} - 2} - 1 and −(2FLINT_BITS−2−1)-(2^{\mathtt{FLINT\_BITS} - 2} - 1), that is, ±(262−1)\pm(2^{62} - 1) on 64-bit and ±(230−1)\pm(2^{30} - 1) on 32-bit machines. The macro SMALL_FMPZ_BITCOUNT_MAX is FLINT_BITS−2\mathtt{FLINT\_BITS} - 2.

Nonzero if the fmpz value f (a word, not an fmpz_t) is a pointer to an mpz, zero if it is a small value.

mpz_ptr COEFF_TO_PTR(fmpz f)
fmpz PTR_TO_COEFF(mpz_ptr ptr)

Macros converting between an fmpz that holds a pointer and the mpz_ptr itself. PTR_TO_COEFF shifts the pointer right by two bits and sets the tag bits 0101; this relies on mpz_structs being aligned to at least four bytes. COEFF_TO_PTR must only be applied to values for which COEFF_IS_MPZ is true.

More

A structure with the same layout as GMP's mpz_struct, declared in fmpz_types.h: the fields are alloc (the number of allocated limbs), size (the number of limbs in use, negated for a negative value) and ptr (the limbs, least significant first). It lets FLINT's headers access the limbs of a large fmpz without including gmp.h.

Converts an fmpz value x that holds a pointer (COEFF_IS_MPZ(x) true) to a zz_ptr, like COEFF_TO_PTR.

The minimum number of limbs allocated for an mpz owned by an fmpz, currently 2. Every mpz handed out by _fmpz_new_mpz has at least this many limbs, and every mpz given back to _fmpz_clear_mpz must still have at least this many.

The following rules apply to all functions of the module.

  • Aliasing. Every output may be the same object as any input, unless stated otherwise. When a function has several outputs, such as a quotient and a remainder, the outputs must be distinct objects.
  • Canonical form. A value that fits inline is always stored inline. Every function expects its inputs in this form and produces its outputs in it; only the internal functions documented at the end may leave a small value in an mpz. See Canonical form.
  • Errors. Division by zero and other invalid arguments raise an exception through flint_throw (by default, a message and abort), unless stated otherwise. A few functions document preconditions that are not checked.
  • Functions with a leading underscore are internal or have additional preconditions, stated in each case.

Memory management#

Initialises ff to zero. This only stores a zero word and allocates nothing.

void fmpz_init2(fmpz_t f, ulong limbs)

Initialises ff to zero with space for limbs limbs. If limbs is zero, this is fmpz_init; otherwise an mpz is attached and grown to limbs limbs, so that ff is temporarily not in canonical form (its value is 0 in an mpz) until the first assignment. Only useful to avoid reallocations when the final size is known.

Releases the memory used by ff. A large value gives its mpz back to the memory manager (see Memory management); a small value needs no work.

void fmpz_init_set(fmpz_t f, const fmpz_t g)

Initialise ff and set it to gg.

Assignment and conversion#

void fmpz_set(fmpz_t f, const fmpz_t g)

Sets ff to gg.

Swaps ff and gg by exchanging the two words; no limbs are copied.

Set ff to 0 and 1. A large value of ff is released.

void fmpz_set_si(fmpz_t f, slong val)
void fmpz_set_ui(fmpz_t f, ulong val)
void fmpz_neg_ui(fmpz_t f, ulong val)

Set ff to val, and fmpz_neg_ui to −-val.

Set ff to h2FLINT_BITS+lh 2^{\mathtt{FLINT\_BITS}} + l, respectively to its negative, where hh and ll are the unsigned words hi and lo.

Set rr to the integer whose two's complement representation with 2FLINT_BITS2\mathtt{FLINT\_BITS} (respectively 3FLINT_BITS3\mathtt{FLINT\_BITS}) bits is hi, lo (respectively hi, mid, lo), the most significant word first. These convert the results of double-word signed arithmetic such as smul_ppmm.

void fmpz_set_ui_array(fmpz_t out, const ulong * in, slong n)
void fmpz_set_signed_ui_array(fmpz_t out, const ulong * in, slong n)

Set out to the integer with the n≥1n \ge 1 limbs in[0], ..., in[n-1], least significant first: interpreted as an unsigned integer, or as a two's complement integer of nFLINT_BITSn \mathtt{FLINT\_BITS} bits.

void fmpz_get_ui_array(ulong * out, slong n, const fmpz_t in)
void fmpz_get_signed_ui_array(ulong * out, slong n, const fmpz_t in)

Write the n≥1n \ge 1 limbs of in to out, least significant first. The unsigned version requires 0≤0 \le in <2nFLINT_BITS< 2^{n \mathtt{FLINT\_BITS}}; the signed version writes in modulo 2nFLINT_BITS2^{n \mathtt{FLINT\_BITS}}, which is its two's complement representation if it fits in nFLINT_BITSn \mathtt{FLINT\_BITS} bits.

void fmpz_set_mpn_large(fmpz_t z, nn_srcptr src, slong n, int negative)

Sets zz to the integer with the nn limbs src, negated if negative is

  1. Requires n≥2n \ge 2 and src[n-1] nonzero, so that the value does not fit inline; use fmpz_set_ui and fmpz_neg_ui for one limb, or fmpz_set_ui_array in general.

Return ff as an slong, respectively an ulong. The result is unspecified if ff does not fit (use fmpz_fits_si and fmpz_abs_fits_ui to check); currently fmpz_get_ui returns the low limb of ∣f∣|f|, so a negative ff is not reduced modulo 2FLINT_BITS2^{\mathtt{FLINT\_BITS}}.

void fmpz_get_uiui(ulong * hi, ulong * low, const fmpz_t f)

Sets *hi and *low to the high and low limbs of ff, for 0≤f<22FLINT_BITS0 \le f < 2^{2\mathtt{FLINT\_BITS}}.

void fmpz_get_signed_uiui(ulong * hi, ulong * lo, const fmpz_t x)

Sets *hi and *lo to the two limbs of xx modulo 22FLINT_BITS2^{2\mathtt{FLINT\_BITS}}, its two's complement representation if xx fits in 2FLINT_BITS2\mathtt{FLINT\_BITS} bits.

Returns f mod nf \bmod n in [0,n)[0, n) for the modulus nn of mod, for any ff.

double fmpz_get_d(const fmpz_t f)

Returns ff as a double, rounded towards zero if it is not exactly representable. The result is undefined if ∣f∣|f| exceeds the range of double; use fmpz_get_d_2exp for large values.

void fmpz_set_d(fmpz_t f, double c)

Sets ff to cc rounded towards zero. The result is undefined if cc is infinite or a NaN.

double fmpz_get_d_2exp(slong * exp, const fmpz_t f)

Returns a double dd with 0.5≤∣d∣<10.5 \le |d| < 1 and sets *exp to ee such that d2ed 2^e approximates ff; dd is f2−ef 2^{-e} truncated to 53 bits. For f=0f = 0 it returns 0 and sets *exp to 0.

void fmpz_set_d_2exp(fmpz_t f, double d, slong exp)

Sets ff to d2expd 2^{\mathit{exp}} rounded towards zero, so that 0.75⋅200.75 \cdot 2^0 gives 0.

void fmpz_get_mpz(mpz_t x, const fmpz_t f)
void fmpz_set_mpz(fmpz_t f, const mpz_t x)

Convert between fmpz_t and an initialised GMP mpz_t, copying the value. Declared only if gmp.h is included before the FLINT headers.

void fmpz_get_mpf(mpf_t x, const fmpz_t f)
void fmpz_set_mpf(fmpz_t f, const mpf_t x)

Convert between fmpz_t and GMP's mpf_t; fmpz_set_mpf truncates a fractional value towards zero. Declared only if gmp.h is included first.

void fmpz_get_mpfr(mpfr_t x, const fmpz_t f, mpfr_rnd_t rnd)

Sets the MPFR number xx to ff, rounded in the direction rnd to the precision of xx. Declared only if mpfr.h is included before the FLINT headers.

int fmpz_get_mpn(nn_ptr * n, fmpz_t n_in)

Allocates an array with flint_malloc, stores the limbs of ∣nin∣|n_{in}| in it, sets *n to it and returns the number of limbs (at least 1). The caller frees the array with flint_free. For nin=0n_{in} = 0 the array has one limb, which is 0.

Converting to and from GMP without copying#

These functions give GMP read access to an fmpz_t and FLINT read access to an mpz_t, without copying limbs. The guide Interfacing with GMP shows how to use them. All require gmp.h to be included before the FLINT headers.

void flint_mpz_init_set_readonly(mpz_t z, const fmpz_t f)

flint_mpz_init_set_readonly sets the uninitialised mpz_t zz to the value of ff, sharing the limbs of ff if it is large; zz must not be modified, ff must not change while zz is in use, and zz is released with flint_mpz_clear_readonly.

void fmpz_init_set_readonly(fmpz_t f, const mpz_t z)

fmpz_init_set_readonly sets the uninitialised fmpz_t ff to the value of zz, sharing the limbs of zz if the value is large; ff must not be modified, zz must not change while ff is in use, and ff is released with fmpz_clear_readonly.

Input and output#

The function page Strings, printing and binary input and output gives the details and an example.

char * fmpz_get_str(char * str, int b, const fmpz_t f)

Returns the representation of ff in base bb, 2≤b≤622 \le b \le 62, with a leading minus sign if f<0f < 0. If str is NULL, the string is allocated with flint_malloc and must be freed with flint_free; otherwise it is written to str, which must have room for fmpz_sizeinbase(f, b) + 2 characters.

Full documentation
int fmpz_set_str(fmpz_t f, const char * str, int b)

Sets ff to the integer written in base bb, 2≤b≤622 \le b \le 62, in the null-terminated string str, and returns 0. White space before and after the number and a minus sign are accepted. Returns −1-1 if the string is not a valid integer, in which case ff is undefined.

Full documentation
int fmpz_fprint(FILE * file, const fmpz_t x)
int fmpz_print(const fmpz_t x)

Print xx in decimal to file or to stdout, with a minus sign if negative and no newline. Return the number of characters written, or a nonpositive value on error.

Full documentation
int fmpz_fread(FILE * file, fmpz_t f)

Read a decimal integer (optional white space and minus sign, then digits) from file or from stdin into ff. Return a positive value on success and 0 on failure, following mpz_inp_str.

Full documentation
size_t fmpz_out_raw(FILE * fout, const fmpz_t x)
size_t fmpz_inp_raw(fmpz_t x, FILE * fin)

Write xx to, respectively read it from, a stream in GMP's portable binary format: four bytes holding the signed number of bytes, then the bytes of ∣x∣|x|, most significant first. Files are interchangeable with mpz_out_raw and mpz_inp_raw. Return the number of bytes written or read, and 0 on error.

Full documentation

Random integers#

All functions take a flint_rand_t; see flint.h. They are meant for testing and are not suitable for cryptography.

Set ff to a uniformly random integer with exactly bits bits (its top bit set), nonnegative for the unsigned version and with a random sign otherwise. For bits =0= 0 the result is 0.

Set ff to a random integer whose absolute value has a random number of bits between 0 and bits, nonnegative for the unsigned version. The values are biased towards sparse binary representations, 0, 1 and COEFF_MAX, which exercise special cases in tests.

As fmpz_randtest, but never 0. Requires bits ≥1\ge 1.

void fmpz_randm(fmpz_t f, flint_rand_t state, const fmpz_t m)

Sets ff to a uniformly random integer in [0,m)[0, m) for m>0m > 0, and in (m,0](m, 0] for m<0m < 0.

Sets ff to a uniformly random integer in [1,m)[1, m). Requires m≥2m \ge 2.

Set ff to a random integer in [0,m)[0, m), respectively (−m/2,m/2](-m/2, m/2], for m>0m > 0, with an increased probability of values near the ends of the range (and near zero for the signed version).

void fmpz_randprime(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits, int proved)

Sets ff to a random prime with exactly bits ≥2\ge 2 bits: the next prime after a random integer of that size, retried until it has the right size, so the distribution is not exactly uniform over primes. If proved is nonzero the result is proved prime, otherwise it is a probable prime as in fmpz_nextprime.

Properties and comparison#

int fmpz_sgn(const fmpz_t f)

Returns −1-1, 00 or 11 according to the sign of ff.

int fmpz_is_zero(const fmpz_t f)
int fmpz_is_one(const fmpz_t f)
int fmpz_is_pm1(const fmpz_t f)

Return whether f=0f = 0, f=1f = 1 and f=±1f = \pm 1.

int fmpz_is_even(const fmpz_t f)
int fmpz_is_odd(const fmpz_t f)

Return whether ff is even, respectively odd.

int fmpz_equal(const fmpz_t f, const fmpz_t g)
int fmpz_equal_si(const fmpz_t f, slong g)
int fmpz_equal_ui(const fmpz_t f, ulong g)

Return 1 if f=gf = g and 0 otherwise.

int fmpz_cmp(const fmpz_t f, const fmpz_t g)
int fmpz_cmp_ui(const fmpz_t f, ulong g)
int fmpz_cmp_si(const fmpz_t f, slong g)

Return a negative value, zero or a positive value according to whether f<gf < g, f=gf = g or f>gf > g.

int fmpz_cmpabs(const fmpz_t f, const fmpz_t g)
int fmpz_cmp2abs(const fmpz_t f, const fmpz_t g)

Compare ∣f∣|f| with ∣g∣|g|, respectively with ∣2g∣|2g|, returning a negative value, zero or a positive value.

Return the number of bits, respectively limbs, of ∣f∣|f|; both are 0 for f=0f = 0.

size_t fmpz_sizeinbase(const fmpz_t f, int b)

Returns the number of digits of ∣f∣|f| in base bb, 2≤b≤622 \le b \le 62, and 1 for f=0f = 0. Unlike GMP's mpz_sizeinbase, the result is exact for all bases.

Returns the exponent of the largest power of 2 dividing xx (the number of trailing zero bits), and 0 for x=0x = 0.

int fmpz_fits_si(const fmpz_t f)

Return whether ∣f∣|f| fits in an ulong, respectively whether ff fits in an slong.

ulong fmpz_abs_lbound_ui_2exp(slong * exp, const fmpz_t x, int bits)
ulong fmpz_abs_ubound_ui_2exp(slong * exp, const fmpz_t x, int bits)

For x≠0x \ne 0 and 1≤1 \le bits ≤FLINT_BITS\le \mathtt{FLINT\_BITS}, return a mantissa mm with exactly bits bits and set *exp to ee such that m2e≤∣x∣m 2^e \le |x| (lower bound, mm is ∣x∣|x| truncated), respectively m2e≥∣x∣m 2^e \ge |x| (upper bound, mm is ∣x∣|x| rounded up, which may increase ee by one if the rounding carries).

double fmpz_dlog(const fmpz_t x)

Returns an approximation of log⁡x\log x for x>0x > 0, with a relative error of a few units in the last place of a double.

slong fmpz_flog(const fmpz_t x, const fmpz_t b)
slong fmpz_clog(const fmpz_t x, const fmpz_t b)

Return ⌊log⁡bx⌋\lfloor \log_b x \rfloor and ⌈log⁡bx⌉\lceil \log_b x \rceil, computed exactly. Require x≥1x \ge 1 and b≥2b \ge 2.

Addition and multiplication#

void fmpz_neg(fmpz_t f1, const fmpz_t f2)
void fmpz_abs(fmpz_t f1, const fmpz_t f2)

Set f1f_1 to −f2-f_2, respectively ∣f2∣|f_2|.

void fmpz_add(fmpz_t f, const fmpz_t g, const fmpz_t h)
void fmpz_add_ui(fmpz_t f, const fmpz_t g, ulong x)
void fmpz_add_si(fmpz_t f, const fmpz_t g, slong x)
void fmpz_sub(fmpz_t f, const fmpz_t g, const fmpz_t h)
void fmpz_sub_ui(fmpz_t f, const fmpz_t g, ulong x)
void fmpz_sub_si(fmpz_t f, const fmpz_t g, slong x)

Set ff to g+hg + h, respectively g−hg - h (with xx in place of hh).

void fmpz_mul(fmpz_t f, const fmpz_t g, const fmpz_t h)
void fmpz_mul_ui(fmpz_t f, const fmpz_t g, ulong x)
void fmpz_mul_si(fmpz_t f, const fmpz_t g, slong x)

Set ff to ghg h (with xx in place of hh). Two small operands are multiplied inline; large ones go to flint_mpn_mul or flint_mpn_sqr, which switch from basecase code through Karatsuba and GMP's Toom–Cook algorithms to FFT multiplication (Multiplying integers).

Full documentation
void fmpz_mul2_uiui(fmpz_t f, const fmpz_t g, ulong h1, ulong h2)

Sets ff to gh1h2g h_1 h_2.

void fmpz_addmul(fmpz_t f, const fmpz_t g, const fmpz_t h)
void fmpz_addmul_ui(fmpz_t f, const fmpz_t g, ulong x)
void fmpz_addmul_si(fmpz_t f, const fmpz_t g, slong x)
void fmpz_submul(fmpz_t f, const fmpz_t g, const fmpz_t h)
void fmpz_submul_ui(fmpz_t f, const fmpz_t g, ulong x)
void fmpz_submul_si(fmpz_t f, const fmpz_t g, slong x)

Set ff to f+ghf + g h, respectively f−ghf - g h (with xx in place of hh), without a temporary for the product when the operands are small.

Full documentation
void fmpz_fmma(fmpz_t f, const fmpz_t a, const fmpz_t b, const fmpz_t c, const fmpz_t d)
void fmpz_fmms(fmpz_t f, const fmpz_t a, const fmpz_t b, const fmpz_t c, const fmpz_t d)

Set ff to ab+cdab + cd, respectively ab−cdab - cd.

Full documentation
void fmpz_mul_2exp(fmpz_t f, const fmpz_t g, ulong exp)

Set ff to g2expg 2^{\mathit{exp}}, respectively 2exp2^{\mathit{exp}}. fmpz_mul_2exp assumes that exp+FLINT_BITS\mathit{exp} + \mathtt{FLINT\_BITS} does not overflow an ulong.

void fmpz_mul_tdiv_q_2exp(fmpz_t f, const fmpz_t g, const fmpz_t h, ulong exp)

Set ff to gh/2expg h / 2^{\mathit{exp}} (with xx in place of hh), rounded towards zero.

Powers and roots#

The function page Square roots, n-th roots and perfect powers describes the root functions and their algorithms.

void fmpz_pow_ui(fmpz_t f, const fmpz_t g, ulong exp)

Set ff to gexpg^{\mathit{exp}}, respectively xx to beb^e, with 00=10^0 = 1.

int fmpz_pow_fmpz(fmpz_t a, const fmpz_t b, const fmpz_t e)

Sets aa to beb^e with 00=10^0 = 1 and returns 1. For ∣b∣≥2|b| \ge 2 and an exponent ee that does not fit in an slong (the result would not fit in memory), returns 0 and leaves aa unchanged. Throws an exception if e<0e < 0.

void fmpz_sqrt(fmpz_t f, const fmpz_t g)
void fmpz_sqrtrem(fmpz_t f, fmpz_t r, const fmpz_t g)

Set ff to ⌊g⌋\lfloor \sqrt g \rfloor and rr to g−f2g - f^2, for g≥0g \ge 0. Throw an exception if g<0g < 0. In fmpz_sqrtrem, ff and rr must be distinct objects.

Full documentation

If aa is a perfect square, sets ss to a≥0\sqrt a \ge 0 and returns 1; otherwise returns 0 and leaves ss undefined. Faster than fmpz_is_square followed by fmpz_sqrt.

Full documentation
int fmpz_is_square(const fmpz_t f)

Returns whether ff is a perfect square (f≥0f \ge 0).

Full documentation
int fmpz_root(fmpz_t r, const fmpz_t f, slong n)

Sets rr to the nn-th root of ff rounded towards zero and returns 1 if it is exact and 0 otherwise. Requires n≥1n \ge 1, and f≥0f \ge 0 if nn is even; otherwise an exception is raised.

Full documentation

If f=rkf = r^k for some k≥2k \ge 2, sets root to rr and returns kk; otherwise returns 0. Negative ff are allowed (then kk is odd). The exponent is not necessarily the largest one: 21202^{120} currently gives k=2k = 2, and −2120-2^{120} gives k=3k = 3. By convention 0 and 1 give k=2k = 2 and −1-1 gives k=1k = 1.

Full documentation

Division#

Division comes in four rounding conventions: tdiv rounds the quotient towards zero, fdiv towards −∞-\infty, cdiv towards +∞+\infty and ndiv to the nearest integer. The function page Division and rounding conventions explains them, and Integer division and Newton iteration the algorithms.

void fmpz_tdiv_qr(fmpz_t f, fmpz_t s, const fmpz_t g, const fmpz_t h)
void fmpz_fdiv_qr(fmpz_t f, fmpz_t s, const fmpz_t g, const fmpz_t h)
void fmpz_cdiv_qr(fmpz_t f, fmpz_t s, const fmpz_t g, const fmpz_t h)

Set ff to the quotient qq of gg by hh, rounded towards zero, −∞-\infty and +∞+\infty respectively, and ss to the remainder g−qhg - qh. The remainder has the sign of gg (tdiv), of hh (fdiv) or of −h-h (cdiv), and ∣s∣<∣h∣|s| < |h|. Throw an exception if h=0h = 0.

Full documentation
void fmpz_ndiv_qr(fmpz_t q, fmpz_t r, const fmpz_t a, const fmpz_t b)

Sets qq to a/ba/b rounded to the nearest integer, ties to even, and rr to a−qba - qb, so that ∣r∣≤∣b∣/2|r| \le |b|/2. Throws an exception if b=0b = 0.

Full documentation
void fmpz_tdiv_q(fmpz_t f, const fmpz_t g, const fmpz_t h)
void fmpz_fdiv_q(fmpz_t f, const fmpz_t g, const fmpz_t h)
void fmpz_cdiv_q(fmpz_t f, const fmpz_t g, const fmpz_t h)
void fmpz_tdiv_q_ui(fmpz_t f, const fmpz_t g, ulong h)
void fmpz_fdiv_q_ui(fmpz_t f, const fmpz_t g, ulong h)
void fmpz_cdiv_q_ui(fmpz_t f, const fmpz_t g, ulong h)
void fmpz_tdiv_q_si(fmpz_t f, const fmpz_t g, slong h)
void fmpz_fdiv_q_si(fmpz_t f, const fmpz_t g, slong h)
void fmpz_cdiv_q_si(fmpz_t f, const fmpz_t g, slong h)

Set ff to the quotient of gg by hh, rounded towards zero, −∞-\infty and +∞+\infty respectively. Throw an exception if h=0h = 0.

Full documentation
void fmpz_fdiv_r(fmpz_t f, const fmpz_t g, const fmpz_t h)

Sets ff to the remainder of fmpz_fdiv_qr, which has the sign of hh. Throws an exception if h=0h = 0.

Full documentation
void fmpz_tdiv_q_2exp(fmpz_t f, const fmpz_t g, ulong exp)
void fmpz_fdiv_q_2exp(fmpz_t f, const fmpz_t g, ulong exp)
void fmpz_cdiv_q_2exp(fmpz_t f, const fmpz_t g, ulong exp)
void fmpz_tdiv_r_2exp(fmpz_t f, const fmpz_t g, ulong exp)
void fmpz_fdiv_r_2exp(fmpz_t f, const fmpz_t g, ulong exp)
void fmpz_cdiv_r_2exp(fmpz_t f, const fmpz_t g, ulong exp)

Set ff to the quotient, respectively the remainder, of gg by 2exp2^{\mathit{exp}} with the rounding of the prefix. fmpz_fdiv_q_2exp is an arithmetic right shift and fmpz_fdiv_r_2exp the reduction to [0,2exp)[0, 2^{\mathit{exp}}).

Full documentation

Return the absolute value of the remainder of gg by hh with the rounding of the prefix. fmpz_fdiv_ui thus returns g mod hg \bmod h in [0,h)[0, h). Throw an exception if h=0h = 0.

Full documentation
void fmpz_divexact(fmpz_t f, const fmpz_t g, const fmpz_t h)
void fmpz_divexact_ui(fmpz_t f, const fmpz_t g, ulong h)
void fmpz_divexact_si(fmpz_t f, const fmpz_t g, slong h)

Set ff to g/hg / h, assuming that hh divides gg. This is not checked; if the division is not exact the result is meaningless. Faster than the other divisions, since it uses 2-adic (Hensel) division. Throw an exception if h=0h = 0.

Full documentation
void fmpz_divexact2_uiui(fmpz_t f, const fmpz_t g, ulong h1, ulong h2)

Sets ff to g/(h1h2)g / (h_1 h_2), assuming that h1h2h_1 h_2 divides gg. Throws an exception if h1h_1 or h2h_2 is 0.

Full documentation
int fmpz_div(fmpz_t q, const fmpz_t g, const fmpz_t h)
int fmpz_div_ui(fmpz_t q, const fmpz_t g, ulong h)
int fmpz_div_si(fmpz_t q, const fmpz_t g, slong h)

Checked exact division: if hh divides gg, set q=g/hq = g/h and return 1; otherwise set q=0q = 0 and return 0. Division by zero counts as exact only for g=0g = 0 (with q=0q = 0).

Full documentation
int fmpz_divides(fmpz_t q, const fmpz_t g, const fmpz_t h)

Older name for fmpz_div.

int fmpz_divisible(const fmpz_t f, const fmpz_t g)

Returns whether gg divides ff, that is, whether f=qgf = qg for an integer qq. Every gg divides 0, and 0 divides only 0.

Return whether gg divides ff. As for fmpz_divisible, every gg divides 0, and 0 divides only 0.

A precomputed inverse of a divisor hh for fmpz_fdiv_qr_preinvn: the fields are dinv (an nn-limb approximation of the reciprocal of h′h', computed by flint_mpn_preinvn, where h′h' is ∣h∣|h| shifted left by norm bits to have its top bit set) and n (the number of limbs of hh).

Compute a precomputed inverse of f≠0f \ne 0, respectively release it.

void fmpz_fdiv_qr_preinvn(fmpz_t f, fmpz_t s, const fmpz_t g, const fmpz_t h, const fmpz_preinvn_t inv)
void fmpz_fdiv_r_preinvn(fmpz_t f, const fmpz_t g, const fmpz_t h, const fmpz_preinvn_t inv)

As fmpz_fdiv_qr and fmpz_fdiv_r, using the precomputed inverse inv of hh, for many divisions by the same multi-limb hh.

Modular arithmetic#

The function page Modular arithmetic with integers describes inversion and exponentiation.

void fmpz_mod(fmpz_t f, const fmpz_t g, const fmpz_t h)

Sets ff to g mod hg \bmod h in [0,∣h∣)[0, |h|). Requires h≠0h \ne 0, which is not checked (a zero divisor crashes with a division by zero).

Full documentation

Sets ff to g mod hg \bmod h in [0,h)[0, h) and also returns it. Throws an exception if h=0h = 0.

void fmpz_smod(fmpz_t f, const fmpz_t g, const fmpz_t h)

Sets ff to the symmetric remainder y≡g(modh)y \equiv g \pmod h with −∣h∣/2<y≤∣h∣/2-|h|/2 < y \le |h|/2. Requires h≠0h \ne 0 (not checked).

Full documentation

Sets ff to the symmetric remainder y≡x(modm)y \equiv x \pmod m with −m/2<y≤m/2-m/2 < y \le m/2, for 0≤x<m0 \le x < m (not checked).

void fmpz_negmod(fmpz_t r, const fmpz_t a, const fmpz_t mod)

Sets rr to −a-a modulo mod for 0≤a<0 \le a < mod (not checked).

int fmpz_invmod(fmpz_t f, const fmpz_t g, const fmpz_t h)

If gcd⁡(g,h)=1\gcd(g, h) = 1, sets ff to the inverse of gg modulo hh in [0,∣h∣)[0, |h|) and returns 1; otherwise returns 0 and ff is undefined. Every gg is invertible modulo h=±1h = \pm 1, with inverse 0. Throws an exception if h=0h = 0.

Full documentation
void fmpz_powm_ui(fmpz_t f, const fmpz_t g, ulong exp, const fmpz_t m)
void fmpz_powm(fmpz_t f, const fmpz_t g, const fmpz_t e, const fmpz_t m)

Set ff to ge mod mg^e \bmod m in [0,m)[0, m), for m≥1m \ge 1 (an exception is raised otherwise). For m=1m = 1 the result is 0, and otherwise g0=1g^0 = 1. fmpz_powm accepts e<0e < 0 if gg is invertible modulo mm and raises an exception if it is not.

Full documentation
int fmpz_sqrtmod(fmpz_t b, const fmpz_t a, const fmpz_t p)

For a prime pp: if aa is a square modulo pp, sets bb to a square root and returns 1, otherwise sets bb to 0 and returns 0. Primality of pp is not checked; for composite pp a return value of 1 means nothing.

Full documentation
slong fmpz_remove(fmpz_t rop, const fmpz_t op, const fmpz_t f)

Sets rop to op with all factors ff removed and returns their number, the largest vv with fv∣f^v \mid op. Requires f≥2f \ge 2 (an exception is raised otherwise). For op =0= 0, rop is set to 0 and 0 is returned.

int fmpz_jacobi(const fmpz_t a, const fmpz_t p)

Returns the Jacobi symbol (ap)\left(\frac{a}{p}\right) for any aa and odd p>0p > 0. The conditions on pp are not checked.

int fmpz_kronecker(const fmpz_t a, const fmpz_t n)

Returns the Kronecker symbol (an)\left(\frac{a}{n}\right) for any aa and nn.

void fmpz_divides_mod_list(fmpz_t xstart, fmpz_t xstride, fmpz_t xlength, const fmpz_t a, const fmpz_t b, const fmpz_t n)

Sets the outputs so that the solutions x∈[0,n)x \in [0, n) of ax≡b(modn)ax \equiv b \pmod n are exactly xstart+i⋅xstride\mathit{xstart} + i \cdot \mathit{xstride} for 0≤i<xlength0 \le i < \mathit{xlength}; there are none if xlength is 0. Requires n>0n > 0; the outputs must be distinct objects.

Arithmetic modulo powers of two#

These functions use 2-adic Newton iteration on the limbs (Exact division). Results are reduced to [0,2N)[0, 2^N).

Sets rop to the inverse of op modulo 2N2^N. Throws an exception if op is even.

void fmpz_divmod_2exp(fmpz_t rop, const fmpz_t a, const fmpz_t b, flint_bitcnt_t N)

Hensel division: sets rop to the unique q∈[0,2N)q \in [0, 2^N) with qb≡a(mod2N)qb \equiv a \pmod{2^N}. Throws an exception if bb is even.

If op is an odd square modulo 2N2^N (for N≥3N \ge 3: op ≡1(mod8)\equiv 1 \pmod 8), set rop to a square root yy, respectively an inverse square root (op y2≡1\mathit{op}\, y^2 \equiv 1), modulo 2N2^N, and return 1; otherwise return 0. The root returned is the one congruent to 1 modulo 4, reduced modulo 2N2^N. Even op is not supported (0 is returned).

Greatest common divisor#

The function page GCD, LCM and Bézout coefficients explains the extended GCD variants and their normalisations; Integer GCD describes the algorithms.

void fmpz_gcd(fmpz_t f, const fmpz_t g, const fmpz_t h)
void fmpz_gcd_ui(fmpz_t res, const fmpz_t a, ulong b)

Set ff to gcd⁡(g,h)≥0\gcd(g, h) \ge 0, with gcd⁡(0,0)=0\gcd(0, 0) = 0.

Full documentation
void fmpz_gcd3(fmpz_t f, const fmpz_t a, const fmpz_t b, const fmpz_t c)

Sets ff to gcd⁡(a,b,c)≥0\gcd(a, b, c) \ge 0. Faster than two calls to fmpz_gcd when one operand is small or the GCD of the two shortest operands is 1.

Full documentation
void fmpz_lcm(fmpz_t f, const fmpz_t g, const fmpz_t h)

Sets ff to the least common multiple of gg and hh, which is nonnegative, and 0 if gg or hh is 0.

Full documentation
void fmpz_xgcd(fmpz_t d, fmpz_t a, fmpz_t b, const fmpz_t f, const fmpz_t g)

Sets d=gcd⁡(f,g)≥0d = \gcd(f, g) \ge 0 and cofactors aa, bb with af+bg=daf + bg = d. The outputs must be distinct objects.

Full documentation

As fmpz_xgcd, but the cofactors are the canonical Bézout coefficients defined on GCD, LCM and Bézout coefficients: the minimal ones, with fixed choices in the degenerate cases. Usually faster than fmpz_xgcd.

Full documentation
void fmpz_gcdinv(fmpz_t d, fmpz_t a, const fmpz_t f, const fmpz_t g)

For 0≤f<g0 \le f < g, sets d=gcd⁡(f,g)d = \gcd(f, g) and aa to a cofactor in [0,g)[0, g) with af≡d(modg)af \equiv d \pmod g, so that a=f−1 mod ga = f^{-1} \bmod g if d=1d = 1. For f=0f = 0 it sets d=gd = g and a=0a = 0. The outputs must be distinct objects.

Full documentation
void fmpz_xgcd_partial(fmpz_t co2, fmpz_t co1, fmpz_t r2, fmpz_t r1, const fmpz_t L)

Runs the extended Euclidean algorithm on r2>r1>0r_2 > r_1 > 0 until the remainder drops to at most LL, with Lehmer's acceleration. On return r2r_2 and r1r_1 are the last two remainders and co2, co1 the corresponding cofactors, with co2⋅r1−\cdot r_1 - co1⋅r2=±r2,orig\cdot r_2 = \pm r_{2,\mathrm{orig}}. No aliasing is allowed.

Chinese remaindering#

The function page Chinese remaindering compares the CRT interfaces and has examples; Remainder trees and the Chinese remainder theorem describes the product and remainder trees used for many moduli.

void fmpz_CRT_ui(fmpz_t out, const fmpz_t r1, const fmpz_t m1, ulong r2, ulong m2, int sign)
void fmpz_CRT(fmpz_t out, const fmpz_t r1, const fmpz_t m1, const fmpz_t r2, const fmpz_t m2, int sign)

Set out to the unique x≡r1(modm1)x \equiv r_1 \pmod{m_1}, x≡r2(modm2)x \equiv r_2 \pmod {m_2} in [0,m1m2)[0, m_1 m_2) if sign is 0, and in (−m1m2/2,m1m2/2](-m_1 m_2/2, m_1 m_2/2] otherwise. Require coprime m1,m2>0m_1, m_2 > 0, −m1≤r1<m1-m_1 \le r_1 < m_1 and 0≤r2<m20 \le r_2 < m_2; only the coprimality is checked (by fmpz_CRT, which raises an exception).

Full documentation
void _fmpz_CRT_ui_precomp(fmpz_t out, const fmpz_t r1, const fmpz_t m1, ulong r2, ulong m2, ulong m2inv, const fmpz_t m1m2, ulong c, int sign)

As fmpz_CRT_ui with precomputed data: m2inv == n_preinvert_limb(m2), m1m2 =m1m2= m_1 m_2 and c=m1−1 mod m2c = m_1^{-1} \bmod m_2. Used when the same pair of moduli recurs.

Precomputed data for reduction modulo, and reconstruction from, a fixed list of word-size moduli (fmpz_comb_t), and the scratch space for one call (fmpz_comb_temp_t). An fmpz_comb_t is a thin wrapper around the flint_mpn_crt_t of mpn_extras.

Flags for fmpz_comb_init2 selecting the precomputation for fmpz_multi_mod_ui and fmpz_multi_CRT_ui respectively.

void fmpz_comb_init(fmpz_comb_t C, nn_srcptr primes, slong num_primes)
void fmpz_comb_init2(fmpz_comb_t C, nn_srcptr primes, slong num_primes, int flags)

Initialise C for the num_primes≥1\mathit{num\_primes} \ge 1 pairwise coprime nonzero word-size moduli in primes (they need not be prime; the array is copied), respectively release it. fmpz_comb_init2 precomputes only what the operations in flags (FMPZ_COMB_MOD, FMPZ_COMB_CRT or both) need.

Full documentation

Allocate, respectively free, scratch space for calls with C. A scratch space is not thread-safe; each thread needs its own.

Full documentation
void fmpz_multi_mod_ui(ulong * out, const fmpz_t in, const fmpz_comb_t C, fmpz_comb_temp_t CT)

Sets out[i] to in modulo the ii-th modulus of C, in [0,mi)[0, m_i), for any in.

Full documentation
void fmpz_multi_CRT_ui(fmpz_t output, nn_srcptr residues, const fmpz_comb_t comb, fmpz_comb_temp_t temp, int sign)

Sets output to the integer congruent to residues[i] ∈[0,mi)\in [0, m_i) modulo each modulus of comb, in [0,M)[0, M) if sign is 0 and in (−M/2,M/2](-M/2, M/2] otherwise, where MM is the product of the moduli.

Full documentation
void fmpz_multi_mod_ui_once(ulong * out, const fmpz_t in, nn_srcptr primes, slong num_primes)
void fmpz_multi_CRT_ui_once(fmpz_t output, fmpz_t prod, nn_srcptr residues, nn_srcptr primes, slong num_primes, int sign)

One-shot versions of fmpz_multi_mod_ui and fmpz_multi_CRT_ui that do not need an fmpz_comb_t. If prod is not NULL, the CRT version also sets it to the product of the moduli.

Full documentation

Precomputed data for Chinese remaindering with a fixed list of moduli of any size.

Initialise CRT, respectively release P.

Prepares CRT for the len≥1\mathit{len} \ge 1 moduli (signs are ignored) and returns 1 if they are usable: one nonzero modulus, or several that are pairwise coprime and none of which is 00 or ±1\pm 1. Otherwise returns 0, and later reconstructions give undefined results.

Full documentation
void fmpz_multi_CRT_precomp(fmpz_t output, const fmpz_multi_CRT_t P, const fmpz * inputs, int sign)

Sets output to the integer congruent to inputs[i] modulo the ii-th modulus of P, for any values inputs[i]: in [0,M)[0, M) if sign is 0 and in (−M/2,M/2](-M/2, M/2] otherwise, where MM is the product of the absolute values of the moduli.

Full documentation
void _fmpz_multi_CRT_precomp(fmpz * outputs, const fmpz_multi_CRT_t P, const fmpz * inputs, int sign)

As fmpz_multi_CRT_precomp, with the result in outputs[0] and the other entries of the array, which must have P->localsize initialised entries, used as scratch space.

int fmpz_multi_CRT(fmpz_t output, const fmpz * moduli, const fmpz * values, slong len, int sign)

Combines fmpz_multi_CRT_precompute and fmpz_multi_CRT_precomp and returns the value of the former.

Full documentation

Precomputed data for reducing an integer modulo a fixed list of moduli of any size.

Initialise, respectively release, P.

Prepares P for reduction modulo the r≥1r \ge 1 moduli f (signs are ignored) and returns 1 if none of them is zero, 0 otherwise.

Full documentation
void fmpz_multi_mod_precomp(fmpz * outputs, const fmpz_multi_mod_t P, const fmpz_t input, int sign)
void _fmpz_multi_mod_precomp(fmpz * outputs, const fmpz_multi_mod_t P, const fmpz_t input, int sign, fmpz * tmp)

Set outputs[i] to input modulo the ii-th modulus mim_i of P: in [0,∣mi∣)[0, |m_i|) if sign is 0 and in (−∣mi∣/2,∣mi∣/2](-|m_i|/2, |m_i|/2] otherwise. The underscore version takes scratch space tmp of P->localsize initialised entries.

Full documentation

Bit operations#

Bit operations act on the infinite two's complement representation, as in GMP: a negative integer has infinitely many leading one bits. The function page Bit operations has examples.

int fmpz_tstbit(const fmpz_t f, ulong i)

Returns bit ii of ff (0 or 1).

Full documentation

Set, clear and complement bit ii of ff.

Full documentation
void fmpz_complement(fmpz_t r, const fmpz_t f)

Sets rr to the one's complement −f−1-f - 1 of ff.

Full documentation
void fmpz_and(fmpz_t r, const fmpz_t a, const fmpz_t b)
void fmpz_or(fmpz_t r, const fmpz_t a, const fmpz_t b)
void fmpz_xor(fmpz_t r, const fmpz_t a, const fmpz_t b)

Set rr to the bitwise and, inclusive or and exclusive or of aa and bb.

Full documentation

Returns the number of one bits of c≥0c \ge 0. For negative cc (infinitely many one bits) it returns 0.

Full documentation
int fmpz_bit_pack(nn_ptr arr, flint_bitcnt_t shift, flint_bitcnt_t bits, const fmpz_t coeff, int negate, int borrow)
int fmpz_bit_unpack(fmpz_t coeff, nn_srcptr arr, flint_bitcnt_t shift, flint_bitcnt_t bits, int negate, int borrow)

Pack an integer into, respectively extract it from, a field of bits bits starting at bit shift <FLINT_BITS<\mathtt{FLINT\_BITS} of the limb array arr. Signed fields are stored in two's complement with a borrow that propagates to the next field. These are the building blocks of Kronecker substitution in fmpz_poly.

More

Combinatorial functions#

Binary splitting and product trees explains the product trees behind these functions.

Sets ff to n!n!, from a table for n≤20n \le 20 (on 64-bit machines) and with GMP's mpz_fac_ui otherwise.

Sets res to the binomial coefficient (nk)\binom{n}{k}, which is 0 for k>nk > n.

void fmpz_rfac_ui(fmpz_t r, const fmpz_t x, ulong n)

Set rr to the rising factorial x(x+1)⋯(x+n−1)x(x+1)\cdots(x+n-1), with value 1 for n=0n = 0. Any sign of xx is allowed.

void _fmpz_rfac_ui(fmpz_t r, const fmpz_t x, ulong a, ulong b)

Sets rr to (x+a)(x+a+1)⋯(x+b−1)(x+a)(x+a+1)\cdots(x+b-1) for x≥0x \ge 0 and b>ab > a, by binary splitting with several factors packed per word at the leaves.

Sets ff to the Fibonacci number FnF_n.

Sets res to the primorial n#n\#, the product of the primes p≤np \le n, by a balanced product tree over word-size factors.

void fmpz_euler_phi(fmpz_t res, const fmpz_t n)

Sets res to Euler's totient φ(n)\varphi(n), the number of 1≤k≤n1 \le k \le n with gcd⁡(k,n)=1\gcd(k, n) = 1, and to 0 for n≤0n \le 0. Word-size nn use n_euler_phi; larger nn are factored. The function fmpz_factor_euler_phi of fmpz_factor takes a factorisation.

int fmpz_moebius_mu(const fmpz_t n)

Returns the Möbius function μ(∣n∣)\mu(|n|): 0 if nn has a square factor >1> 1, otherwise (−1)k(-1)^k for kk prime factors; μ(0)=0\mu(0) = 0. Factors nn unless it fits in a word.

void fmpz_divisor_sigma(fmpz_t res, ulong k, const fmpz_t n)

Sets res to σk(∣n∣)\sigma_k(|n|), the sum of the kk-th powers of the positive divisors of nn, and to 0 for n=0n = 0. Factors nn.

Primality testing#

Primality proving: APRCL and ECPP describes the proofs.

Returns 0 if pp is composite and 1 if it is a probable prime. For pp up to about 81 bits on 64-bit machines (where n_is_prime and n_ll_is_prime decide primality), the answer is exact; otherwise the test is trial division, a perfect square check and the Baillie–PSW test (a base-2 strong probable prime test and a Lucas test), for which no composite that passes is known.

int fmpz_is_prime(const fmpz_t n)

Returns 1 if nn is prime and 0 otherwise, with a proof.

More

Returns 1 if nn is a strong probable prime to base aa, and 0 otherwise.

Returns 1 if nn is a Lucas probable prime with parameters chosen by Selfridge's method A [PSW1980], and 0 otherwise. No prime is declared composite.

Returns 1 if nn passes the Baillie–PSW test [PSW1980] (a strong probable prime test to base 2 and a Lucas test), and 0 otherwise. No composite passing it is known, and no prime fails it.

Returns 0 if nn is composite and 1 if it is proved prime by the pseudosquare test (Theorem 2.7 of [LukPatWil1996]), using a table of pseudosquares up to 2642^{64}. Returns −1-1 if nn is too large (above about 94 bits).

int fmpz_is_prime_pocklington(fmpz_t F, fmpz_t R, const fmpz_t n, nn_ptr pm1, slong num_pm1)
int fmpz_is_prime_morrison(fmpz_t F, fmpz_t R, const fmpz_t n, nn_ptr pp1, slong num_pp1)

The n−1n - 1 test of Pocklington and the n+1n + 1 test of Morrison for odd nn (non-square for Morrison), given the num_pm1 (num_pp1) primes pm1 (pp1) dividing n−1n - 1 (n+1n + 1). Return 0 if nn is composite. Otherwise they set FF to the factored part of n∓1n \mp 1 and RR to (n∓1)/F(n \mp 1)/F and return 1, which proves that every prime factor of nn is ≡1(modF)\equiv 1 \pmod F (respectively ≡±1\equiv \pm 1). This proves nn prime if F≥nF \ge \sqrt n (respectively F>n+1F > \sqrt n + 1).

void _fmpz_nm1_trial_factors(const fmpz_t n, nn_ptr pm1, slong * num_pm1, ulong limit)
void _fmpz_np1_trial_factors(const fmpz_t n, nn_ptr pp1, slong * num_pp1, ulong limit)

Trial factor n−1n - 1 (respectively n+1n + 1) by the primes up to about limit and store the primes found in the array, setting its length. The array needs room for log⁡2n+2\log_2 n + 2 entries. A limit of a small multiple of (log⁡n)3/10(\log n)^3 / 10 is a reasonable choice for the tests above.

void fmpz_lucas_chain(fmpz_t Vm, fmpz_t Vm1, const fmpz_t A, const fmpz_t m, const fmpz_t n)
void fmpz_lucas_chain_full(fmpz_t Vm, fmpz_t Vm1, const fmpz_t A, const fmpz_t B, const fmpz_t m, const fmpz_t n)

Set VmV_m and Vm+1V_{m+1} modulo nn for the Lucas sequence V0=2V_0 = 2, V1=AV_1 = A, Vj=AVj−1−Vj−2V_j = A V_{j-1} - V_{j-2} (respectively AVj−1−BVj−2A V_{j-1} - B V_{j-2}), by the doubling formulas. No aliasing is allowed.

void fmpz_lucas_chain_double(fmpz_t U2m, fmpz_t U2m1, const fmpz_t Um, const fmpz_t Um1, const fmpz_t A, const fmpz_t B, const fmpz_t n)
void fmpz_lucas_chain_add(fmpz_t Umn, fmpz_t Umn1, const fmpz_t Um, const fmpz_t Um1, const fmpz_t Un, const fmpz_t Un1, const fmpz_t A, const fmpz_t B, const fmpz_t n)
void fmpz_lucas_chain_mul(fmpz_t Ukm, fmpz_t Ukm1, const fmpz_t Um, const fmpz_t Um1, const fmpz_t A, const fmpz_t B, const fmpz_t k, const fmpz_t n)

For the Lucas sequence U0=0U_0 = 0, U1=1U_1 = 1, Uj=AUj−1−BUj−2U_j = A U_{j-1} - B U_{j-2} modulo nn: given Um,Um+1U_m, U_{m+1} (and Un,Un+1U_n, U_{n+1}), compute U2m,U2m+1U_{2m}, U_{2m+1}, respectively Um+n,Um+n+1U_{m+n}, U_{m+n+1}, respectively Ukm,Ukm+1U_{km}, U_{km+1}. Each output may alias the corresponding input (U2mU_{2m} with UmU_m, and so on); no other aliasing is allowed.

void fmpz_lucas_chain_VtoU(fmpz_t Um, fmpz_t Um1, const fmpz_t Vm, const fmpz_t Vm1, const fmpz_t A, const fmpz_t B, const fmpz_t Dinv, const fmpz_t n)

Given VmV_m and Vm+1V_{m+1} modulo nn, sets UmU_m and Um+1U_{m+1}, where Dinv is the inverse of D=A2−4BD = A^2 - 4B modulo nn. UmU_m may alias VmV_m and Um+1U_{m+1} may alias Vm+1V_{m+1}.

void fmpz_nextprime(fmpz_t res, const fmpz_t n, int proved)

Sets res to the smallest prime greater than nn (2 for n≤1n \le 1). If proved is nonzero, the result is proved prime with fmpz_is_prime; otherwise it is a probable prime (n_nextprime for small nn, GMP's mpz_nextprime, a Baillie–PSW test, otherwise).

Lenstra's algorithm: for 0<r<s<n0 < r < s < n with gcd⁡(r,s)=1\gcd(r, s) = 1, finds a divisor of nn congruent to rr modulo ss if there is one, sets fac to it and returns 1; otherwise returns 0. Efficient when s3>ns^3 > n, as in the final step of the n±1n \pm 1 primality tests.

Internal functions#

These functions manage the representation directly. They are needed only to write new low-level functions; see The fmpz representation. Those returning mpz_ptr are declared only if gmp.h is included before the FLINT headers.

mpz_ptr _fmpz_new_mpz(void)

Take an initialised mpz with at least MPZ_MIN_ALLOC limbs from the memory manager, respectively give back the mpz that the large value ff points to. The mpz given back must still have at least MPZ_MIN_ALLOC limbs.

void _fmpz_cleanup(void)

Free the mpzs cached by the current thread (the second also frees the cache array). Called by flint_cleanup; they do nothing in builds configured with --enable-reentrant.

Ensure that ff holds an mpz and return it. _fmpz_promote does not preserve the value of a small ff (the mpz holds garbage); it is used before overwriting ff. _fmpz_promote_val preserves the value. Either may leave ff temporarily non-canonical.

_fmpz_demote releases the mpz of ff, if any, and sets ff to 0 (any value then fits inline). _fmpz_demote_val moves the value of ff back inline if it fits, restoring canonical form.

Out-of-line parts of the inline assignment functions: set an initialised (_init_ versions: an uninitialised) ff to a value that does not fit inline. Calling them with a value that fits leaves ff non-canonical.

void _fmpz_init_readonly_mpz(fmpz_t f, const mpz_t z)

Helpers of the read-only conversions: wrap the mpz zz in a fresh mpz slot of ff, respectively release the mpz of such an ff when its value is small.

Returns whether xx is in canonical form: inline, or an mpz whose value does not fit inline and whose top limb is nonzero. Used in tests.

void _fmpz_smod(fmpz_t r, const fmpz_t a, const fmpz_t m, int sign, fmpz_t t)

Sets rr to aa modulo m≠0m \ne 0 in a range selected by sign: for sign <0< 0 the remainder of truncating division (∣r∣<∣m∣|r| < |m|, sign of aa), for 0 that of floor division (∣r∣<∣m∣|r| < |m|, sign of mm), and for sign >0> 0 the symmetric remainder −∣m∣<2r≤∣m∣-|m| < 2r \le |m|. The scratch variable tt must not alias any other argument.

slong _fmpz_remove(fmpz_t x, const fmpz_t f, double finv)

Divides x≠0x \ne 0 in place by the largest power of f>1f > 1 dividing it and returns the exponent. finv must be n_precompute_inverse(f) if ff is small and 0 otherwise. xx and ff must be distinct.

References#

  • [PSW1980] C. Pomerance, J. L. Selfridge and S. S. Wagstaff, Jr., "The pseudoprimes to 25·10^9", Mathematics of Computation 35 (1980), 1003–1026
  • [LukPatWil1996] R. F. Lukes and C. D. Patterson and H. C. Williams "Some results on pseudosquares" Math. Comp. 1996, no. 65, 361–372
Cite this page

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