Testing latest pari + WASM + node.js... and it works?! Wow.
License: GPL3
ubuntu2004
% Copyright (c) 2000 The PARI Group1%2% This file is part of the PARI/GP documentation3%4% Permission is granted to copy, distribute and/or modify this document5% under the terms of the GNU General Public License6\chapter{Technical Reference Guide: the basics}78In the following chapters, we describe all public low-level functions of the9PARI library. These include specialized functions for handling all the PARI10types. Simple higher level functions, such as arithmetic or transcendental11functions, are described in Chapter~3 of the GP user's manual; we will12eventually see more general or flexible versions in the chapters to come. A13general introduction to the major concepts of PARI programming can be found14in Chapter~4, which you should really read first.1516We shall now study specialized functions, more efficient than the library17wrappers, but sloppier on argument checking and damage control; besides18speed, their main advantage is to give finer control about the inner19workings of generic routines, offering more options to the programmer.2021\misctitle{Important advice} Generic routines eventually call lower level22functions. Optimize your algorithms first, not overhead and conversion costs23between PARI routines. For generic operations, use generic routines first;24do not waste time looking for the most specialized one available unless you25identify a genuine bottleneck, or you need some special behavior the generic26routine does not offer. The PARI source code is part of the documentation;27look for inspiration there.\smallskip2829The type \kbd{long} denotes a \tet{BITS_IN_LONG}-bit signed long integer (3230or 64 bits). The type \tet{ulong} is defined as \kbd{unsigned long}. The word31\emph{stack} always refer to the PARI stack, allocated through an initial32\kbd{pari\_init} call. Refer to Chapters 1--2 and~4 for general background.33\kbdsidx{BIL}3435We shall often refer to the notion of \tev{shallow} function, which means that36some components of the result may point to components of the input, which is37more efficient than a \emph{deep} copy (full recursive copy of the object38tree). Such outputs are not suitable for \kbd{gerepileupto} and particular39care must be taken when garbage collecting objects which have been input to40shallow functions: corresponding outputs also become invalid and should no41longer be accessed.4243A function is \emph{not stack clean} if it leaves intermediate data on the44stack besides its output, for efficiency reasons.4546\section{Initializing the library}4748The following functions enable you to start using the PARI functions49in a program, and cleanup without exiting the whole program.5051\subsec{General purpose}5253\fun{void}{pari_init}{size_t size, ulong maxprime} initialize the54library, with a stack of \kbd{size} bytes and a prime table55up to the maximum of \kbd{maxprime} and $2^{16}$. Unless otherwise56mentioned, no PARI function will function properly before such an57initialization.5859\fun{void}{pari_close}{void} stop using the library (assuming it was60initialized with \kbd{pari\_init}) and frees all allocated objects.6162\subsec{Technical functions}\label{se:pari_init_tech}6364\fun{void}{pari_init_opts}{size_t size, ulong maxprime, ulong opts} as65\kbd{pari\_init}, more flexible. \kbd{opts} is a mask of flags66among the following:6768\kbd{INIT\_JMPm}: install PARI error handler. When an exception is69raised, the program is terminated with \kbd{exit(1)}.7071\kbd{INIT\_SIGm}: install PARI signal handler.7273\kbd{INIT\_DFTm}: initialize the \kbd{GP\_DATA} environment structure.74This one \emph{must} be enabled once. If you close pari, then restart it,75you need not reinitialize \kbd{GP\_DATA}; if you do not, then old values are76restored.7778\kbd{INIT\_noPRIMEm}: do not compute the prime table (ignore the79\kbd{maxprime} argument). The user \emph{must} call80\tet{pari_init_primes} later.8182\kbd{INIT\_noIMTm}: (technical, see \kbd{pari\_mt\_init} in the Developer's83Guide for detail). Do not call \tet{pari_mt_init} to initialize the84multi-thread engine. If this flag is set, \kbd{pari\_mt\_init()} will need to85be called manually. See \kbd{examples/pari-mt.c} for an example.8687\kbd{INIT\_noINTGMPm}: do not install PARI-specific GMP memory functions.88This option is ignored when the GMP library is not in use. You may89install PARI-specific GMP memory functions later by calling9091\fun{void}{pari_kernel_init}{void}9293\noindent and restore the previous values using9495\fun{void}{pari_kernel_close}{void}9697This option should not be used without a thorough understanding of the98problem you are trying to solve. The GMP memory functions are global99variables used by the GMP library. If your program is linked with two100libraries that require these variables to be set to different values,101conflict ensues. To avoid a conflict, the proper solution is to record102their values with \kbd{mp\_get\_memory\_functions} and to call103\kbd{mp\_set\_memory\_functions} to restore the expected values each time the104code switches from using one library to the other. Here is an example:105\bprog106void *(*pari_alloc_ptr) (size_t);107void *(*pari_realloc_ptr) (void *, size_t, size_t);108void (*pari_free_ptr) (void *, size_t);109void *(*otherlib_alloc_ptr) (size_t);110void *(*otherlib_realloc_ptr) (void *, size_t, size_t);111void (*otherlib_free_ptr) (void *, size_t);112113void init(void)114{115pari_init(8000000, 500000);116mp_get_memory_functions(&pari_alloc_ptr,&pari_realloc_ptr,117&pari_free_ptr);118otherlib_init();119mp_get_memory_functions(&otherlib_alloc_ptr,&otherlib_realloc_ptr,120&otherlib_free_ptr);121}122void function_that_use_pari(void)123{124mp_set_memory_functions(pari_alloc_ptr,pari_realloc_ptr,125pari_free_ptr);126/*use PARI functions*/127}128void function_that_use_otherlib(void)129{130mp_set_memory_functions(otherlib_alloc_ptr,otherlib_realloc_ptr,131otherlib_free_ptr);132/*use OTHERLIB functions*/133}134@eprog135136\fun{void}{pari_close_opts}{ulong init_opts} as \kbd{pari\_close},137for a library initialized with a mask of options using138\kbd{pari\_init\_opts}. \kbd{opts} is a mask of flags among139140\kbd{INIT\_SIGm}: restore \kbd{SIG\_DFL} default action for signals141tampered with by PARI signal handler.142143\kbd{INIT\_DFTm}: frees the \kbd{GP\_DATA} environment structure.144145\kbd{INIT\_noIMTm}: (technical, see \kbd{pari\_mt\_init} in the Developer's146Guide for detail). Do not call \tet{pari_mt_close} to close the multi-thread147engine.148\kbd{INIT\_noINTGMPm}: do not restore GMP memory functions.149150\fun{void}{pari_sig_init}{void (*f)(int)} install the signal handler \kbd{f}151(see \kbd{signal(2)}): the signals \kbd{SIGBUS}, \kbd{SIGFPE}, \kbd{SIGINT},152\kbd{SIGBREAK}, \kbd{SIGPIPE} and \kbd{SIGSEGV} are concerned.153154\fun{void}{pari_init_primes}{ulong maxprime} Initialize the PARI155primes. This function is called by \kbd{pari\_init(\dots,maxprime)}.156It is provided for users calling \kbd{pari\_init\_opts} with the157flag \kbd{INIT\_noPRIMEm}.158159\fun{void}{pari_sighandler}{int signum} the actual signal handler that160PARI uses. This can be used as argument to \kbd{pari\_sig\_init} or161\kbd{signal(2)}.162163\fun{void}{pari_stackcheck_init}{void *stackbase} controls the system stack164exhaustion checking code in the GP interpreter. This should be used when the165system stack base address change or when the address seen by \kbd{pari\_init}166is too far from the base address. If \kbd{stackbase} is \kbd{NULL}, disable the167check, else set the base address to \kbd{stackbase}. It is normally used this168way169\bprog170int thread_start (...)171{172long first_item_on_the_stack;173...174pari_stackcheck_init(&first_item_on_the_stack);175}176@eprog177178\fun{int}{pari_daemon}{void} forks a PARI daemon, detaching from the main179process group. The function returns 1 in the parent, and 0 in the180forked son.181182\fun{void}{paristack_setsize}{size_t rsize, size_t vsize}183sets the default \kbd{parisize} to \kbd{rsize} and the184default \kbd{parisizemax} to \kbd{vsize}, and reallocate the185stack to match these value, destroying its content.186Generally used just after \kbd{pari\_init}.187188\fun{void}{paristack_resize}{ulong newsize}189changes the current stack size to \kbd{newsize}190(double it if \kbd{newsize} is 0).191The new size is clipped to be at least the current stack size and192at most \kbd{parisizemax}. The stack content is not affected193by this operation.194195\fun{void}{parivstack_reset}{void}196resets the current stack to its default size \kbd{parisize}. This is197used to recover memory after a computation that enlarged the stack.198This function destroys the content of the enlarged stack (between199the old and the new bottom of the stack).200Before calling this function, you must ensure that \kbd{avma} lies201within the new smaller stack.202203\fun{void}{paristack_newrsize}{ulong newsize}204\emph{(does not return)}. Library version of205\bprog206default(parisize, "newsize")207@eprog\noindent Set the default \kbd{parisize} to \kbd{newsize}, or double208\kbd{parisize} if \kbd{newsize} is equal to 0, then call209\kbd{cb\_pari\_err\_recover(-1)}.210211\fun{void}{parivstack_resize}{ulong newsize}212\emph{(does not return)}. Library version of213\bprog214default(parisizemax, "newsize")215@eprog\noindent Set the default \kbd{parisizemax} to \kbd{newsize} and call216\kbd{cb\_pari\_err\_recover(-1)}.217218\subsec{Notions specific to the GP interpreter}219220An \kbd{entree} is the generic object attached to an identifier (a name)221in GP's interpreter, be it a built-in or user function, or a variable. For222a function, it has at least the following fields:223224\kbd{char *name}: the name under which the interpreter knows us.225226\kbd{void *value}: a pointer to the C function to call.227228\kbd{long menu}: a small integer $\geq 1$ (to which group of function229help do we belong, for the \kbd{?$n$} help menu).230231\kbd{char *code}: the prototype code.232233\kbd{char *help}: the help text for the function.234235A routine in GP is described to the analyzer by an \kbd{entree}236structure. Built-in PARI routines are grouped in \emph{modules}, which237are arrays of \kbd{entree} structs, the last of which satisfy238\kbd{name = NULL} (sentinel). There are currently four modules in PARI/GP:239240\item general functions (\tet{functions_basic}, known to \kbd{libpari}),241242\item gp-specific functions (\tet{functions_gp}),243244\noindent and two modules of obsolete functions. The function245\kbd{pari\_init} initializes the interpreter and declares all symbols in246\kbd{functions\_basic}. You may declare further functions on a case by case247basis or as a whole module using248249\fun{void}{pari_add_function}{entree *ep} adds a single routine to the250table of symbols in the interpreter. It assumes \kbd{pari\_init} has been251called.252253\fun{void}{pari_add_module}{entree *mod} adds all the routines in module254\kbd{mod} to the table of symbols in the interpreter. It assumes255\kbd{pari\_init} has been called.256257\noindent For instance, gp implements a number of private routines, which258it adds to the default set via the calls259\bprog260pari_add_module(functions_gp);261@eprog262263A GP \kbd{default} is likewise attached to a helper routine, that is run264when the value is consulted, or changed by \tet{default0} or \tet{setdefault}.265Such routines are grouped in the module \tet{functions_default}.266267\fun{void}{pari_add_defaults_module}{entree *mod} adds all the defaults in268module \kbd{mod} to the interpreter. It assumes that \kbd{pari\_init} has269been called. From this point on, all defaults in module \kbd{mod} are known270to \tet{setdefault} and friends.271272\subsec{Public callbacks}273274The \kbd{gp} calculator associates elaborate functions (for instance the275break loop handler) to the following callbacks, and so can you:276277\doc{cb_pari_ask_confirm}{void (*cb_pari_ask_confirm)(const char *s)}278initialized to \kbd{NULL}. Called with argument $s$ whenever PARI wants279confirmation for action $s$, for instance in \tet{secure} mode.280281\doc{cb_pari_init_histfile}{void (*cb_pari_init_histfile)(void)}282initialized to \kbd{NULL}. Called when the \kbd{histfile} default283is changed. The intent is for that callback to read the file content, append284it to history in memory, then dump the expanded history to the new285\kbd{histfile}.286287\doc{cb_pari_is_interactive}{int (*cb_pari_is_interactive)(void)};288initialized to \kbd{NULL}.289290\doc{cb_pari_quit}{void (*cb_pari_quit)(long)}291initialized to a no-op. Called when \kbd{gp} must evaluate the \kbd{quit}292command.293294\doc{cb_pari_start_output}{void (*cb_pari_start_output)(void)}295initialized to \kbd{NULL}.296297\doc{cb_pari_handle_exception}{int (*cb_pari_handle_exception)(long)}298initialized to \kbd{NULL}. If not \kbd{NULL}, this routine is called with299argument $-1$ on \kbd{SIGINT}, and argument \kbd{err} on error \kbd{err}. If300it returns a nonzero value, the error or signal handler returns, in effect301further ignoring the error or signal, otherwise it raises a fatal error.302A possible simple-minded handler, used by the \kbd{gp} interpreter, is303304\fun{int}{gp_handle_exception}{long err} if the \kbd{breakloop}305default is enabled (set to $1$) and \tet{cb_pari_break_loop} is not306\kbd{NULL}, we call this routine with \kbd{err} argument and return the307result.308309\doc{cb_pari_err_handle}{int (*cb_pari_err_handle)(GEN)}310If not \kbd{NULL}, this routine is called with a \typ{ERROR} argument311from \kbd{pari\_err}. If it returns a nonzero value, the error returns, in312effect further ignoring the error, otherwise it raises a fatal error.313314The default behavior is to print a descriptive error315message (display the error), then return 0, thereby raising a fatal error.316This differs from \tet{cb_pari_handle_exception} in that the317function is not called on \kbd{SIGINT} (which do not generate a \typ{ERROR}),318only from \kbd{pari\_err}. Use \tet{cb_pari_sigint} if you need to handle319\kbd{SIGINT} as well.320321The following function can be used by \kbd{cb\_pari\_err\_handle} to display322the error message.323324\fun{const char*}{closure_func_err}{} return a statically allocated string325holding the name of the function that triggered the error. Return NULL if the326error was not caused by a function.327328\doc{cb_pari_break_loop}{int (*cb_pari_break_loop)(int)}329initialized to \kbd{NULL}.330331\doc{cb_pari_sigint}{void (*cb_pari_sigint)(void)}.332Function called when we receive \kbd{SIGINT}. By default, raises333\bprog334pari_err(e_MISC, "user interrupt");335@eprog\noindent A possible simple-minded variant, used by the336\kbd{gp} interpreter, is337338\fun{void}{gp_sigint_fun}{void}339340\doc{cb_pari_pre_recover}{void (*cb_pari_pre_recover)(long)}341initialized to \kbd{NULL}. If not \kbd{NULL}, this routine is called just342before PARI cleans up from an error. It is not required to return. The error343number is passed as argument.344345\doc{cb_pari_err_recover}{void (*cb_pari_err_recover)(long)}346initialized to \kbd{pari\_exit()}. This callback must not return.347It is called after PARI has cleaned-up from an error. The error number is348passed as argument, unless the PARI stack has been destroyed, in which case349it is called with argument $-1$.350351\doc{cb_pari_whatnow}{int (*cb_pari_whatnow)(PariOUT *out, const char *s, int352flag)} initialized to \kbd{NULL}. If not \kbd{NULL}, must check whether $s$353existed in older versions of \kbd{pari} (the \kbd{gp} callback checks against354\kbd{pari-1.39.15}). All output must be done via \kbd{out} methods.355356\item $\fl = 0$: should print verbosely the answer, including help text if357available.358359\item $\fl = 1$: must return $0$ if the function did not change, and a360nonzero result otherwise. May print a help message.361362\subsec{Configuration variables}363364\tet{pari_library_path}: If set, It should be a path to the libpari library.365It is used by the function \tet{gpinstall} to locate the PARI library when366searching for symbols. This should only be useful on Windows.367368\subsec{Utility functions}369370\fun{void}{pari_ask_confirm}{const char *s} raise an error if the371callback \tet{cb_pari_ask_confirm} is \kbd{NULL}. Otherwise372calls373\bprog374cb_pari_ask_confirm(s);375@eprog376377\fun{char*}{gp_filter}{const char *s} pre-processor for the GP378parser: filter out whitespace and GP comments from $s$. The returned string379is allocated on the PARI stack and must not be freed.380381\fun{GEN}{pari_compile_str}{const char *s} low-level form of382\tet{compile_str}: assumes that $s$ does not contain spaces or GP comments and383returns the closure attached to the GP expression $s$. Note384that GP metacommands are not recognized.385386\fun{int}{gp_meta}{const char *s, int ismain} low-level component of387\tet{gp_read_str}: assumes that $s$ does not contain spaces or GP comments and388try to interpret $s$ as a GP metacommand (e.g. starting by \kbd{\bs} or389\kbd{?}). If successful, execute the metacommand and return $1$; otherwise390return $0$. The \kbd{ismain} parameter modifies the way \kbd{\bs r} commands391are handled: if nonzero, act as if the file contents were entered via392standard input (i.e. call \tet{switchin} and divert \tet{pari_infile});393otherwise, simply call \tet{gp_read_file}.394395\fun{void}{pari_hit_return}{void} wait for the use to enter \kbd{\bs n}396via standard input.397398\fun{void}{gp_load_gprc}{void} read and execute the user's \kbd{GPRC} file.399400\fun{void}{pari_center}{const char *s} print $s$, centered.401402\fun{void}{pari_print_version}{void} print verbose version information.403404\fun{long}{pari_community}{void} return the index of the support section405n the help.406407\fun{const char*}{gp_format_time}{long t} format a delay of $t$ ms408suitable for \kbd{gp} output, with \kbd{timer} set. The string is allocated409in the PARI stack via \kbd{stack\_malloc}.410411\fun{const char*}{gp_format_prompt}{const char *p} format a prompt $p$412suitable for \kbd{gp} prompting (includes colors and protecting ANSI escape413sequences for readline).414415\fun{void}{pari_alarm}{long s} set an alarm after $s$ seconds (raise an416\tet{e_ALARM} exception).417418\fun{void}{gp_help}{const char *s, long flag} print help for $s$, depending419on the value of \fl:420421\item \tet{h_REGULAR}, basic help (\kbd{?});422423\item \tet{h_LONG}, extended help (\kbd{??});424425\item \tet{h_APROPOS}, a propos help (\kbd{??}).426427\fun{const char **}{gphelp_keyword_list}{void} return a428\kbd{NULL}-terminated array a strings, containing keywords known to429\kbd{gphelp} besides GP functions (e.g. \kbd{modulus} or \kbd{operator}).430Used by the online help system and the contextual completion engine.431432\fun{void}{gp_echo_and_log}{const char *p, const char *s} given a prompt433$p$ and attached input command $s$, update logfile and possibly434print on standard output if \tet{echo} is set and we are not in interactive435mode. The callback \tet{cb_pari_is_interactive} must be set to a sensible436value.437438\fun{void}{gp_alarm_handler}{int sig} the \kbd{SIGALRM} handler439set by the \kbd{gp} interpreter.440441\fun{void}{print_fun_list}{char **list, long n}442print all elements of \kbd{list} in columns, pausing (hit return)443every $n$ lines. \kbd{list} is \kbd{NULL} terminated.444445\subsec{Saving and restoring the GP context}446447\fun{void}{gp_context_save}{struct gp_context* rec} save the current GP448context.449450\fun{void}{gp_context_restore}{struct gp_context* rec} restore a GP context.451The new context must be an ancestor of the current context.452453\subsec{GP history}454455These functions allow to control the GP history (the \kbd{\%} operator).456457\fun{void}{pari_add_hist}{GEN x, long t, long r} adds \kbd{x} as the last history458entry; $t$ (resp. r) is the cpu (resp. real) time used to compute it.459460\fun{GEN}{pari_get_hist}{long p}, if $p>0$ returns entry of index $p$461(i.e. \kbd{\%p}), else returns entry of index $n+p$ where $n$ is the462index of the last entry (used for \kbd{\%}, \kbd{\%`}, \kbd{\%``}, etc.).463464\fun{long}{pari_get_histtime}{long p} as \tet{pari_get_hist},465returning the cpu time used to compute the history entry, instead of the entry466itself.467468\fun{long}{pari_get_histrtime}{long p} as \tet{pari_get_hist},469returning the real time used to compute the history entry, instead of the entry470itself.471472\fun{GEN}{pari_histtime}{long p} return the vector \kbd{[cpu, real]} where473\kbd{cpu} and \kbd{real} are as above.474475\fun{ulong}{pari_nb_hist}{void} return the index of the last entry.476477\section{Handling \kbd{GEN}s}478\noindent Almost all these functions are either macros or inlined. Unless479mentioned otherwise, they do not evaluate their arguments twice. Most of them480are specific to a set of types, although no consistency checks are made:481e.g.~one may access the \kbd{sign} of a \typ{PADIC}, but the result is482meaningless.483484\subsec{Allocation}485486\fun{GEN}{cgetg}{long l, long t} allocates (the root of) a \kbd{GEN}487of type $t$ and length $l$. Sets $z[0]$.488489\fun{GEN}{cgeti}{long l} allocates a \typ{INT} of length $l$ (including the4902 codewords). Sets $z[0]$ only.491492\fun{GEN}{cgetr}{long l} allocates a \typ{REAL} of length $l$ (including the4932 codewords). Sets $z[0]$ only.494495\fun{GEN}{cgetc}{long prec} allocates a \typ{COMPLEX} whose real and496imaginary parts are \typ{REAL}s of length \kbd{prec}.497498\fun{GEN}{cgetg_copy}{GEN x, long *lx} fast version of \kbd{cgetg}:499allocate a \kbd{GEN} with the same type and length as $x$, setting \kbd{*lx}500to \kbd{lg(x)} as a side-effect. (Only sets the first codeword.) This is501a little faster than \kbd{cgetg} since we may reuse the bitmask in502$x[0]$ instead of recomputing it, and we do not need to check that the503length does not overflow the possibilities of the504implementation (since an object with that length already exists). Note that505\kbd{cgetg} with arguments known at compile time, as in506\bprog507cgetg(3, t_INTMOD)508@eprog\noindent will be even faster since the compiler will directly perform509all computations and checks.510511\fun{GEN}{vectrunc_init}{long l} perform \kbd{cgetg(l,t\_VEC)}, then512set the length to $1$ and return the result. This is used to implement513vectors whose final length is easily bounded at creation time, that we intend514to fill gradually using:515516\fun{void}{vectrunc_append}{GEN x, GEN y} assuming $x$ was allocated using517\tet{vectrunc_init}, appends $y$ as the last element of $x$, which518grows in the process. The function is shallow: we append $y$, not a copy;519it is equivalent to520\bprog521long lx = lg(x); gel(x,lx) = y; setlg(x, lx+1);522@eprog\noindent523Beware that the maximal size of $x$ (the $l$ argument to \tet{vectrunc_init})524is unknown, hence unchecked, and stack corruption will occur if we append525more than $l-1$ elements to $x$. Use the safer (but slower)526\kbd{shallowconcat} when $l$ is not easy to bound in advance.527528An other possibility is simply to allocate using \kbd{cgetg(l, t)} then fill529the components as they become available: this time the downside is that we do530not obtain a correct \kbd{GEN} until the vector is complete. Almost no PARI531function will be able to operate on it.532533\fun{void}{vectrunc_append_batch}{GEN x, GEN y} successively apply534\bprog535vectrunc_append(x, gel(y, i))536@eprog537for all elements of the vector $y$.538539\fun{GEN}{coltrunc_init}{long l} as \kbd{vectrunc\_init} but perform540\kbd{cgetg(l,t\_COL)}.541542\fun{GEN}{vecsmalltrunc_init}{long l}543544\fun{void}{vecsmalltrunc_append}{GEN x, long t} analog to the above for a545\typ{VECSMALL} container.546547\subsec{Length conversions}548549These routines convert a nonnegative length to different units. Their550behavior is undefined at negative integers.551552\fun{long}{ndec2nlong}{long x} converts a number of decimal digits to a number553of words. Returns $ 1 + \kbd{floor}(x \times \B \log_2 10)$.554555\fun{long}{ndec2prec}{long x} converts a number of decimal digits to a number556of codewords. This is equal to 2 + \kbd{ndec2nlong(x)}.557558\fun{long}{ndec2nbits}{long x} convers a number of decimal digits to a559number of bits.560561\fun{long}{prec2ndec}{long x} converts a number of codewords to a562number of decimal digits.563564\fun{long}{nbits2nlong}{long x} converts a number of bits to a number of565words. Returns the smallest word count containing $x$ bits, i.e $566\kbd{ceil}(x / \B)$.567568\fun{long}{nbits2ndec}{long x} converts a number of bits to a number of569decimal digits.570571\fun{long}{nbits2lg}{long x} converts a number of bits to a length572in code words. Currently an alias for \kbd{nbits2nlong}.573574\fun{long}{nbits2prec}{long x} converts a number of bits to a number of575codewords. This is equal to 2 + \kbd{nbits2nlong(x)}.576577\fun{long}{nbits2extraprec}{long x} converts a number of bits to the mantissa578length of a \typ{REAL} in codewords. This is currently an alias to579\kbd{nbits2nlong(x)}.580581\fun{long}{nchar2nlong}{long x} converts a number of bytes to number of582words. Returns the smallest word count containing $x$ bytes, i.e583$\kbd{ceil}(x / \kbd{sizeof(long)})$.584585\fun{long}{prec2nbits}{long x} converts a \typ{REAL} length into a number586of significant bits; returns $(x - 2)\B$.587588\fun{double}{prec2nbits_mul}{long x, double y} returns589\kbd{prec2nbits}$(x)\times y$.590591\fun{long}{bit_accuracy}{long x} converts a length into a number592of significant bits; currently an alias for \kbd{prec2nbits}.593594\fun{double}{bit_accuracy_mul}{long x, double y} returns595\kbd{bit\_accuracy}$(x)\times y$.596597\fun{long}{realprec}{GEN x} length of a \typ{REAL} in words; currently an alias598for \kbd{lg}.599600\fun{long}{bit_prec}{GEN x} length of a \typ{REAL} in bits.601602\fun{long}{precdbl}{long prec} given a length in words corresponding to a603\typ{REAL} precision, return the length corresponding to doubling the604precision. Due to the presence of 2 code words, this is605$2(\kbd{prec} - 2) + 2$.606607\subsec{Read type-dependent information}608609\fun{long}{typ}{GEN x} returns the type number of~\kbd{x}. The header files610included through \kbd{pari.h} define symbolic constants for the \kbd{GEN}611types: \typ{INT} etc. Never use their actual numerical values. E.g to determine612whether \kbd{x} is a \typ{INT}, simply check613\bprog614if (typ(x) == t_INT) { }615@eprog\noindent616The types are internally ordered and this simplifies the implementation of617commutative binary operations (e.g addition, gcd). Avoid using the ordering618directly, as it may change in the future; use type grouping functions619instead (\secref{se:typegroup}).620621\fun{const char*}{type_name}{long t} given a type number \kbd{t} this routine622returns a string containing its symbolic name. E.g \kbd{type\_name(\typ{INT})}623returns \kbd{"\typ{INT}"}. The return value is read-only.624625\fun{long}{lg}{GEN x} returns the length of~\kbd{x} in \B-bit words.626627\fun{long}{lgefint}{GEN x} returns the effective length of the \typ{INT}628\kbd{x} in \B-bit words.629630\fun{long}{signe}{GEN x} returns the sign ($-1$, 0 or 1) of~\kbd{x}. Can be631used for \typ{INT}, \typ{REAL}, \typ{POL} and \typ{SER} (for the last two632types, only 0 or 1 are possible).633634\fun{long}{gsigne}{GEN x} returns the sign of a real number $x$,635valid for \typ{INT}, \typ{REAL} as \kbd{signe}, but also for \typ{FRAC}636and \typ{QUAD} of positive discriminants. Raise a type error if \kbd{typ(x)}637is not among those.638639\fun{long}{expi}{GEN x} returns the binary exponent of the real number equal640to the \typ{INT}~\kbd{x}. This is a special case of \kbd{gexpo}.641642\fun{long}{expo}{GEN x} returns the binary exponent of the643\typ{REAL}~\kbd{x}.644645\fun{long}{mpexpo}{GEN x} returns the binary exponent of the \typ{INT}646or \typ{REAL}~\kbd{x}.647648\fun{long}{gexpo}{GEN x} same as \kbd{expo}, but also valid when \kbd{x}649is not a \typ{REAL} (returns the largest exponent found among the components650of \kbd{x}). When \kbd{x} is an exact~0, this returns651\hbox{\kbd{-HIGHEXPOBIT}}, which is lower than any valid exponent.652653\fun{long}{gexpo_safe}{GEN x} same as \kbd{gexpo}, but returns a value654strictly less than \hbox{\kbd{-HIGHEXPOBIT}} when the exponent is not defined655(e.g. for a \typ{PADIC} or \typ{INTMOD} component).656657\fun{long}{valp}{GEN x} returns the $p$-adic valuation (for658a \typ{PADIC}) or $X$-adic valuation (for a \typ{SER}, taken with respect to659the main variable) of~\kbd{x}.660661\fun{long}{precp}{GEN x} returns the precision of the \typ{PADIC}~\kbd{x}.662663\fun{long}{varn}{GEN x} returns the variable number of the664\typ{POL} or \typ{SER}~\kbd{x} (between 0 and \kbd{MAXVARN}).665666\fun{long}{gvar}{GEN x} returns the main variable number when any variable667at all occurs in the composite object~\kbd{x} (the smallest variable number668which occurs), and \tet{NO_VARIABLE} otherwise.669670\fun{long}{gvar2}{GEN x} returns the variable number for the ring over which671$x$ is defined, e.g. if $x\in \Z[a][b]$ return (the variable number for)672$a$. Return \tet{NO_VARIABLE} if $x$ has no variable or is not defined over a673polynomial ring.674675\fun{long}{degpol}{GEN x} is a simple macro returning \kbd{lg(x) - 3}.676This is the degree of the \typ{POL}~\kbd{x} with respect to its main677variable, \emph{if} its leading coefficient is nonzero (a rational $0$ is678impossible, but an inexact $0$ is allowed, as well as an exact modular $0$,679e.g. \kbd{Mod(0,2)}). If $x$ has no coefficients (rational $0$ polynomial),680its length is $2$ and we return the expected $-1$.681682\fun{long}{lgpol}{GEN x} is equal to \kbd{degpol(x) + 1}. Used to loop over683the coefficients of a \typ{POL} in the following situation:684\bprog685GEN xd = x + 2;686long i, l = lgpol(x);687for (i = 0; i < l; i++) foo( xd[i] ).688@eprog689690\fun{long}{precision}{GEN x} If \kbd{x} is of type \typ{REAL}, returns the691precision of~\kbd{x}, namely the length of \kbd{x} in \B-bit words if \kbd{x}692is not zero, and a reasonable quantity obtained from the exponent of \kbd{x}693if \kbd{x} is numerically equal to zero. If \kbd{x} is of type694\typ{COMPLEX}, returns the minimum of the precisions of the real and695imaginary part. Otherwise, returns~0 (which stands for infinite precision).696697\fun{long}{lgcols}{GEN x} is equal to \kbd{lg(gel(x,1))}. This is the length698of the columns of a \typ{MAT} with at least one column.699700\fun{long}{nbrows}{GEN x} is equal to \kbd{lg(gel(x,1))-1}. This is the number701of rows of a \typ{MAT} with at least one column.702703\fun{long}{gprecision}{GEN x} as \kbd{precision} for scalars. Returns the704lowest precision encountered among the components otherwise.705706\fun{long}{sizedigit}{GEN x} returns 0 if \kbd{x} is exactly~0. Otherwise,707returns \kbd{\key{gexpo}(x)} multiplied by $\log_{10}(2)$. This gives a crude708estimate for the maximal number of decimal digits of the components709of~\kbd{x}.710711\subsec{Eval type-dependent information}712These routines convert type-dependent information to bitmask to fill the713codewords of \kbd{GEN} objects (see \secref{se:impl}). E.g for a714\typ{REAL}~\kbd{z}:715\bprog716z[1] = evalsigne(-1) | evalexpo(2)717@eprog718Compatible components of a codeword for a given type can be OR-ed as above.719720\fun{ulong}{evaltyp}{long x} convert type~\kbd{x} to bitmask (first721codeword of all \kbd{GEN}s)722723\fun{long}{evallg}{long x} convert length~\kbd{x} to bitmask (first724codeword of all \kbd{GEN}s). Raise overflow error if \kbd{x} is so large that725the corresponding length cannot be represented726727\fun{long}{_evallg}{long x} as \kbd{evallg} \emph{without} the overflow728check.729730\fun{ulong}{evalvarn}{long x} convert variable number~\kbd{x} to bitmask731(second codeword of \typ{POL} and \typ{SER})732733\fun{long}{evalsigne}{long x} convert sign~\kbd{x} (in $-1,0,1$) to bitmask734(second codeword of \typ{INT}, \typ{REAL}, \typ{POL}, \typ{SER})735736\fun{long}{evalprecp}{long x} convert $p$-adic ($X$-adic) precision~\kbd{x}737to bitmask (second codeword of \typ{PADIC}, \typ{SER}). Raise overflow error738if \kbd{x} is so large that the corresponding precision cannot be739represented.740741\fun{long}{_evalprecp}{long x} same as \kbd{evalprecp} \emph{without} the742overflow check.743744\fun{long}{evalvalp}{long x} convert $p$-adic ($X$-adic) valuation~\kbd{x} to745bitmask (second codeword of \typ{PADIC}, \typ{SER}). Raise overflow error if746\kbd{x} is so large that the corresponding valuation cannot be represented.747748\fun{long}{_evalvalp}{long x} same as \kbd{evalvalp} \emph{without} the749overflow check.750751\fun{long}{evalexpo}{long x} convert exponent~\kbd{x} to bitmask (second752codeword of \typ{REAL}). Raise overflow error if \kbd{x} is so753large that the corresponding exponent cannot be represented754755\fun{long}{_evalexpo}{long x} same as \kbd{evalexpo} \emph{without} the756overflow check.757758\fun{long}{evallgefint}{long x} convert effective length~\kbd{x} to bitmask759(second codeword \typ{INT}). This should be less or equal than the length760of the \typ{INT}, hence there is no overflow check for the effective length.761762\subsec{Set type-dependent information}763Use these functions and macros with extreme care since usually the764corresponding information is set otherwise, and the components and further765codeword fields (which are left unchanged) may not be compatible with the new766information.767768\fun{void}{settyp}{GEN x, long s} sets the type number of~\kbd{x} to~\kbd{s}.769770\fun{void}{setlg}{GEN x, long s} sets the length of~\kbd{x} to~\kbd{s}. This771is an efficient way of truncating vectors, matrices or polynomials.772773\fun{void}{setlgefint}{GEN x, long s} sets the effective length774of the \typ{INT} \kbd{x} to~\kbd{s}. The number \kbd{s} must be less than or775equal to the length of~\kbd{x}.776777\fun{void}{setsigne}{GEN x, long s} sets the sign of~\kbd{x} to~\kbd{s}.778If \kbd{x} is a \typ{INT} or \typ{REAL}, \kbd{s} must be equal to $-1$, 0779or~1, and if \kbd{x} is a \typ{POL} or \typ{SER}, \kbd{s} must be equal to 0780or~1. No sanity check is made; in particular, setting the sign of a781$0$ \typ{INT} to $\pm1$ creates an invalid object.782783\fun{void}{togglesign}{GEN x} sets the sign $s$ of~\kbd{x} to $-s$, in place.784785\fun{void}{togglesign_safe}{GEN *x} sets the $s$ sign of~\kbd{*x} to $-s$, in786place, unless \kbd{*x} is one of the integer universal constants in which case787replace \kbd{*x} by its negation (e.g.~replace \kbd{gen\_1} by \kbd{gen\_m1}).788789\fun{void}{setabssign}{GEN x} sets the sign $s$ of~\kbd{x} to $|s|$, in place.790791\fun{void}{affectsign}{GEN x, GEN y} shortcut for \kbd{setsigne(y, signe(x))}.792No sanity check is made; in particular, setting the sign of a793$0$ \typ{INT} to $\pm1$ creates an invalid object.794795\fun{void}{affectsign_safe}{GEN x, GEN *y} sets the sign of~\kbd{*y} to that796of~\kbd{x}, in place, unless \kbd{*y} is one of the integer universal797constants in which case replace \kbd{*y} by its negation if needed798(e.g.~replace \kbd{gen\_1} by \kbd{gen\_m1} if \kbd{x} is negative). No other799sanity check is made; in particular, setting the sign of a $0$800\typ{INT} to $\pm1$ creates an invalid object.801802\fun{void}{normalize_frac}{GEN z} assuming $z$ is of the form \kbd{mkfrac(a,b)}803with $b\neq 0$, make sure that $b > 0$ by changing the sign of $a$ in place if804needed (use \kbd{togglesign}).805806\fun{void}{setexpo}{GEN x, long s} sets the binary exponent of the807\typ{REAL}~\kbd{x} to \kbd{s}. The value \kbd{s} must be a 24-bit signed808number.809810\fun{void}{setvalp}{GEN x, long s} sets the $p$-adic or $X$-adic valuation811of~\kbd{x} to~\kbd{s}, if \kbd{x} is a \typ{PADIC} or a \typ{SER},812respectively.813814\fun{void}{setprecp}{GEN x, long s} sets the $p$-adic precision of the815\typ{PADIC}~\kbd{x} to~\kbd{s}.816817\fun{void}{setvarn}{GEN x, long s} sets the variable number of the \typ{POL}818or \typ{SER}~\kbd{x} to~\kbd{s} (where $0\le \kbd{s}\le\kbd{MAXVARN}$).819820\subsec{Type groups}\label{se:typegroup}821In the following functions, \kbd{t} denotes the type of a \kbd{GEN}.822They used to be implemented as macros, which could evaluate their argument823twice; \emph{no longer}: it is not inefficient to write824\bprog825is_intreal_t(typ(x))826@eprog827828\fun{int}{is_recursive_t}{long t} \kbd{true} iff \kbd{t} is a recursive829type (the nonrecursive types are \typ{INT}, \typ{REAL},830\typ{STR}, \typ{VECSMALL}). Somewhat contrary to intuition, \typ{LIST} is831also nonrecursive, ; see the Developer's guide for details.832833\fun{int}{is_intreal_t}{long t} \kbd{true} iff \kbd{t} is \typ{INT}834or \typ{REAL}.835836\fun{int}{is_rational_t}{long t} \kbd{true} iff \kbd{t} is \typ{INT}837or \typ{FRAC}.838839\fun{int}{is_real_t}{long t} \kbd{true} iff \kbd{t} is \typ{INT}840or \typ{REAL} or \typ{FRAC}.841842\fun{int}{is_qfb_t}{long t} \kbd{true} iff \kbd{t} is \typ{QFB}.843844\fun{int}{is_vec_t}{long t} \kbd{true} iff \kbd{t} is \typ{VEC}845or \typ{COL}.846847\fun{int}{is_matvec_t}{long t} \kbd{true} iff \kbd{t} is \typ{MAT}, \typ{VEC}848or \typ{COL}.849850\fun{int}{is_scalar_t}{long t} \kbd{true} iff \kbd{t} is a scalar, i.e851a \typ{INT},852a \typ{REAL},853a \typ{INTMOD},854a \typ{FRAC},855a \typ{COMPLEX},856a \typ{PADIC},857a \typ{QUAD},858or859a \typ{POLMOD}.860861\fun{int}{is_extscalar_t}{long t} \kbd{true} iff \kbd{t} is a scalar (see862\kbd{is\_scalar\_t}) or \kbd{t} is \typ{POL}.863864\fun{int}{is_const_t}{long t} \kbd{true} iff \kbd{t} is a scalar which is not865\typ{POLMOD}.866867\fun{int}{is_noncalc_t}{long t} true if generic operations (\kbd{gadd},868\kbd{gmul}) do not make sense for $t$: corresponds to types869\typ{LIST}, \typ{STR}, \typ{VECSMALL}, \typ{CLOSURE}870871\subsec{Accessors and components}\label{se:accessors}872The first two functions return \kbd{GEN} components as copies on the stack:873874\fun{GEN}{compo}{GEN x, long n} creates a copy of the \kbd{n}-th true875component (i.e.\ not counting the codewords) of the object~\kbd{x}.876877\fun{GEN}{truecoeff}{GEN x, long n} creates a copy of the coefficient of878degree~\kbd{n} of~\kbd{x} if \kbd{x} is a scalar, \typ{POL} or \typ{SER},879and otherwise of the \kbd{n}-th component of~\kbd{x}.880\smallskip881882\noindent On the contrary, the following routines return the address of a883\kbd{GEN} component. No copy is made on the stack:884885\fun{GEN}{constant_coeff}{GEN x} returns the address of the constant886coefficient of \typ{POL}~\kbd{x}. By convention, a $0$ polynomial (whose887\kbd{sign} is $0$) has \kbd{gen\_0} constant term.888889\fun{GEN}{leading_coeff}{GEN x} returns the address of the leading coefficient890of \typ{POL}~\kbd{x}, i.e. the coefficient of largest index stored in the891array representing $x$. This may be an inexact $0$. By convention, return892\kbd{gen\_0} if the coefficient array is empty.893894\fun{GEN}{gel}{GEN x, long i} returns the address of the895\kbd{x[i]} entry of~\kbd{x}. (\kbd{el} stands for element.)896897\fun{GEN}{gcoeff}{GEN x, long i, long j} returns the address of the898\kbd{x[i,j]} entry of \typ{MAT}~\kbd{x}, i.e.~the coefficient at row~\kbd{i}899and column~\kbd{j}.900901\fun{GEN}{gmael}{GEN x, long i, long j} returns the address of the902\kbd{x[i][j]} entry of~\kbd{x}. (\kbd{mael} stands for multidimensional array903element.)904905\fun{GEN}{gmael2}{GEN A, long x1, long x2} is an alias for \kbd{gmael}.906Similar macros \tet{gmael3}, \tet{gmael4}, \tet{gmael5} are available.907908\section{Global numerical constants}909These are defined in the various public PARI headers.910911\subsec{Constants related to word size}912913\noindent \kbd{long} $\tet{BITS_IN_LONG} = 2^{\tet{TWOPOTBITS_IN_LONG}}$:914number of bits in a \kbd{long} (32 or 64).915916\noindent \kbd{long} \tet{BITS_IN_HALFULONG}: \kbd{BITS\_IN\_LONG} divided by917$2$.918919\noindent \kbd{long} \tet{LONG_MAX}: the largest positive \kbd{long}.920921\noindent \kbd{ulong} \tet{ULONG_MAX}: the largest \kbd{ulong}.922923\noindent \kbd{long} \tet{DEFAULTPREC}: the length (\kbd{lg}) of a924\typ{REAL} with 64 bits of accuracy925926\noindent \kbd{long} \tet{MEDDEFAULTPREC}: the length (\kbd{lg}) of a927\typ{REAL} with 128 bits of accuracy928929\noindent \kbd{long} \tet{BIGDEFAULTPREC}: the length (\kbd{lg}) of a930\typ{REAL} with 192 bits of accuracy931932\noindent \kbd{ulong} \tet{HIGHBIT}: the largest power of $2$ fitting in an933\kbd{ulong}.934935\noindent \kbd{ulong} \tet{LOWMASK}: bitmask yielding the least significant936bits.937938\noindent \kbd{ulong} \tet{HIGHMASK}: bitmask yielding the most significant939bits.940941\noindent The last two are used to implement the following convenience macros,942returning half the bits of their operand:943944\fun{ulong}{LOWWORD}{ulong a} returns least significant bits.945946\fun{ulong}{HIGHWORD}{ulong a} returns most significant bits.947948\noindent Finally949950\fun{long}{divsBIL}{long n} returns the Euclidean quotient of $n$ by951\kbd{BITS\_IN\_LONG} (with nonnegative remainder).952953\fun{long}{remsBIL}{n} returns the (nonnegative) Euclidean remainder of $n$954by \kbd{BITS\_IN\_LONG}955956\fun{long}{dvmdsBIL}{long n, long *r}957958\fun{ulong}{dvmduBIL}{ulong n, ulong *r} sets $r$ to \kbd{remsBIL(n)}959and returns \kbd{divsBIL(n)}.960961\subsec{Masks used to implement the \kbd{GEN} type}962963These constants are used by higher level macros, like \kbd{typ} or \kbd{lg}:964965\noindent \tet{EXPOnumBITS},966\tet{LGnumBITS},967\tet{SIGNnumBITS},968\tet{TYPnumBITS},969\tet{VALPnumBITS},970\tet{VARNnumBITS}:971number of bits used to encode \kbd{expo}, \kbd{lg}, \kbd{signe},972\kbd{typ}, \kbd{valp}, \kbd{varn}.973974\noindent \tet{PRECPSHIFT},975\tet{SIGNSHIFT},976\tet{TYPSHIFT},977\tet{VARNSHIFT}: shifts used to recover or encode \kbd{precp}, \kbd{varn},978\kbd{typ}, \kbd{signe}979980\noindent \tet{CLONEBIT},981\tet{EXPOBITS},982\tet{LGBITS},983\tet{PRECPBITS},984\tet{SIGNBITS},985\tet{TYPBITS},986\tet{VALPBITS},987\tet{VARNBITS}: bitmasks used to extract \kbd{isclone}, \kbd{expo}, \kbd{lg},988\kbd{precp}, \kbd{signe}, \kbd{typ}, \kbd{valp}, \kbd{varn} from \kbd{GEN}989codewords.990991\noindent \tet{MAXVARN}: the largest possible variable number.992993\noindent \tet{NO_VARIABLE}: sentinel returned by \kbd{gvar(x)} when \kbd{x}994does not contain any polynomial; has a lower priority than any valid variable995number.996997\noindent \tet{HIGHEXPOBIT}: a power of $2$, one more that the largest possible998exponent for a \typ{REAL}.9991000\noindent \tet{HIGHVALPBIT}: a power of $2$, one more that the largest possible1001valuation for a \typ{PADIC} or a \typ{SER}.10021003\subsec{$\log 2$, $\pi$}10041005These are \kbd{double} approximations to useful constants:10061007\noindent \tet{M_PI}: $\pi$.10081009\noindent \tet{M_LN2}: $\log 2$.10101011\noindent \tet{LOG10_2}: $\log 2 / \log 10$.10121013\noindent \tet{LOG2_10}: $\log 10 / \log 2$.10141015\section{Iterating over small primes, low-level interface}1016\label{se:primetable}10171018One of the methods used by the high-level prime iterator (see1019\secref{se:primeiter}), is a precomputed table. Its direct use is deprecated,1020but documented here.10211022After \kbd{pari\_init(size, maxprime)}, a ``prime table'' is1023initialized with the successive \emph{differences} of primes up to (possibly1024just a little beyond) \kbd{maxprime}. The prime table occupies roughly1025$\kbd{maxprime}/\log(\kbd{maxprime})$ bytes in memory, so be sensible when1026choosing \kbd{maxprime}; it is $500000$ by default under \kbd{gp} and there1027is no real benefit in choosing a much larger value: the high-level1028iterator provide \emph{fast} access to primes up to the \emph{square}1029of \kbd{maxprime}. In any case, the implementation requires that1030$\tet{maxprime} < 2^{\B} - 2048$, whatever memory is available.10311032PARI currently guarantees that the first 6547 primes, up to and including103365557, are present in the table, even if you set \kbd{maxprime} to zero.1034in the \kbd{pari\_init} call.10351036\noindent Some convenience functions:10371038\fun{ulong}{maxprime}{} the largest prime computable using our prime table.10391040\fun{ulong}{maxprimeN}{} the index $N$ of the largest prime computable using1041the prime table. I.e., $p_N = \kbd{maxprime()}$.10421043\fun{void}{maxprime_check}{ulong B} raise an error if \kbd{maxprime()} is $< B$.10441045After the following initializations (the names $p$ and \var{ptr} are1046arbitrary of course)1047\bprog1048byteptr ptr = diffptr;1049ulong p = 0;1050@eprog1051\noindent calling the macro \tet{NEXT_PRIME_VIADIFF_CHECK}$(p, \var{ptr})$1052repeatedly will assign the successive prime numbers to $p$. Overrunning the1053prime table boundary will raise the error \tet{e_MAXPRIME}, which just1054prints the error message:10551056\kbd{*** not enough precomputed primes, need primelimit \til $c$}10571058\noindent (for some numerical value $c$), then the macro aborts the1059computation. The alternative macro \tet{NEXT_PRIME_VIADIFF} operates in the1060same way, but will omit that check, and is slightly faster. It should be used1061in the following way:1062%1063\bprog1064byteptr ptr = diffptr;1065ulong p = 0;10661067if (maxprime() < goal) pari_err_MAXPRIME(goal); /*@Ccom not enough primes */1068while (p <= goal) /*@Ccom run through all primes up to \kbd{goal} */1069{1070NEXT_PRIME_VIADIFF(p, ptr);1071...1072}1073@eprog\noindent1074Here, we use the general error handling function \kbd{pari\_err} (see1075\secref{se:err}), with the codeword \kbd{e\_MAXPRIME}, raising the ``not enough1076primes'' error. This could be rewritten as1077\bprog1078maxprime_check(goal);1079while (p <= goal) /*@Ccom run through all primes up to \kbd{goal} */1080{1081NEXT_PRIME_VIADIFF(p, ptr);1082...1083}1084@eprog10851086\fun{bytepr}{initprimes}{ulong maxprime, long *L, ulong *lastp}1087computes a (malloc'ed) ``prime table'', in fact a table of all prime1088differences for $p < \kbd{maxprime}$ (and possibly a little beyond). Set $L$1089to the table length (argument to \kbd{malloc}), and \var{lastp} to the last1090prime in the table.10911092\fun{void}{initprimetable}{ulong maxprime} computes a prime table (of all prime1093differences for $p < \kbd{maxprime}$) and assign it to the global variable1094\kbd{diffptr}. Don't change \kbd{diffptr} directly, call this function1095instead. This calls \kbd{initprimes} and updates internal data recording the1096table size.10971098\fun{ulong}{init_primepointer_geq}{ulong a, byteptr *pd}1099returns the smallest prime $p \geq a$, and sets \kbd{*pd} to the proper offset1100of \kbd{diffptr} so that \kbd{NEXT\_PRIME\_VIADIFF(p, *pd)} correctly1101returns \kbd{unextprime(p + 1)}.11021103\fun{ulong}{init_primepointer_gt}{ulong a, byteptr *pd} returns the smallest1104prime $p > a$.11051106\fun{ulong}{init_primepointer_leq}{ulong a, byteptr *pd} returns the largest1107prime $p \leq a$.11081109\fun{ulong}{init_primepointer_lt}{ulong a, byteptr *pd} returns the largest1110prime $p < a$.11111112\section{Handling the PARI stack}11131114\subsec{Allocating memory on the stack}11151116\fun{GEN}{cgetg}{long n, long t} allocates memory on the stack for1117an object of length \kbd{n} and type~\kbd{t}, and initializes its first1118codeword.11191120\fun{GEN}{cgeti}{long n} allocates memory on the stack for a \typ{INT}1121of length~\kbd{n}, and initializes its first codeword. Identical to1122\kbd{cgetg(n,\typ{INT})}.11231124\fun{GEN}{cgetr}{long n} allocates memory on the stack for a \typ{REAL}1125of length~\kbd{n}, and initializes its first codeword. Identical to1126\kbd{cgetg(n,\typ{REAL})}.11271128\fun{GEN}{cgetc}{long n} allocates memory on the stack for a1129\typ{COMPLEX}, whose real and imaginary parts are \typ{REAL}s1130of length~\kbd{n}.11311132\fun{GEN}{cgetp}{GEN x} creates space sufficient to hold the1133\typ{PADIC}~\kbd{x}, and sets the prime $p$ and the $p$-adic precision to1134those of~\kbd{x}, but does not copy (the $p$-adic unit or zero representative1135and the modulus of)~\kbd{x}.11361137\fun{GEN}{new_chunk}{size_t n} allocates a \kbd{GEN} with $n$ components,1138\emph{without} filling the required code words. This is the low-level1139constructor underlying \kbd{cgetg}, which calls \kbd{new\_chunk} then sets1140the first code word. It works by simply returning the address1141\kbd{((GEN)avma) - n}, after checking that it is larger than \kbd{(GEN)bot}.11421143\fun{void}{new_chunk_resize}{size_t x} this function is called by1144\kbd{new\_chunk} when the PARI stack overflows. There is no need to call it1145manually. It will either extend the stack or report an \kbd{e\_STACK} error.11461147\fun{char*}{stack_malloc}{size_t n} allocates memory on the stack for $n$1148chars (\emph{not} $n$ \kbd{GEN}s). This is faster than using \kbd{malloc},1149and easier to use in most situations when temporary storage is needed. In1150particular there is no need to \kbd{free} individually all variables thus1151allocated: a simple \kbd{set\_avma(oldavma)} might be enough. On the other hand,1152beware that this is not permanent independent storage, but part of the stack.1153The memory is aligned on \kbd{sizeof(long)} bytes boundaries.11541155\fun{char*}{stack_malloc_align}{size_t n, long k} as \kbd{stack\_malloc},1156but the memory is aligned on \kbd{k} bytes boundaries. The number\kbd{k} must1157be a multiple of the \kbd{sizeof(long)}.11581159\fun{char*}{stack_calloc}{size_t n} as \kbd{stack\_malloc}, setting the memory1160to zero.11611162\fun{char*}{stack_calloc_align}{size_t n, long k} as1163\kbd{stack\_malloc\_align}, setting the memory to zero.11641165\noindent Objects allocated through these last three functions cannot be1166\kbd{gerepile}'d, since they are not yet valid \kbd{GEN}s: their codewords1167must be filled first.11681169\fun{GEN}{cgetalloc}{long t, size_t l}, same as \kbd{cgetg(t, l)}, except1170that the result is allocated using \tet{pari_malloc} instead of the PARI1171stack. The resulting \kbd{GEN} is now impervious to garbage collecting1172routines, but should be freed using \tet{pari_free}.11731174\subsec{Stack-independent binary objects}11751176\fun{GENbin*}{copy_bin}{GEN x} copies $x$ into a malloc'ed structure suitable1177for stack-independent binary transmission or storage. The object obtained1178is architecture independent provided, \kbd{sizeof(long)} remains the same1179on all PARI instances involved, as well as the multiprecision kernel (either1180native or GMP).11811182\fun{GENbin*}{copy_bin_canon}{GEN x} as \kbd{copy\_bin}, ensuring furthermore1183that the binary object is independent of the multiprecision kernel. Slower1184than \kbd{copy\_bin}.11851186\fun{GEN}{bin_copy}{GENbin *p} assuming $p$ was created by \kbd{copy\_bin(x)}1187(not necessarily by the same PARI instance: transmission or external storage1188may be involved), restores $x$ on the PARI stack.11891190\noindent The routine \kbd{bin\_copy} transparently encapsulate the following1191functions:11921193\fun{GEN}{GENbinbase}{GENbin *p} the \kbd{GEN} data actually stored in $p$.1194All addresses are stored as offsets with respect to a common reference point,1195so the resulting \kbd{GEN} is unusable unless it is a nonrecursive type;1196private low-level routines must be called first to restore absolute addresses.11971198\fun{void}{shiftaddress}{GEN x, long dec} converts relative addresses to1199absolute ones.12001201\fun{void}{shiftaddress_canon}{GEN x, long dec} converts relative addresses to1202absolute ones, and converts leaves from a canonical form to the one1203specific to the multiprecision kernel in use. The \kbd{GENbin} type stores1204whether leaves are stored in canonical form, so \kbd{bin\_copy} can call1205the right variant.12061207\noindent Objects containing closures are harder to e.g. copy and save to disk,1208since closures contain pointers to libpari functions that will not be valid in1209another gp instance: there is little chance for them to be loaded at the exact1210same address in memory. Such objects must be saved along with a linking table.12111212\fun{GEN}{copybin_unlink}{GEN C} returns a linking table allowing to safely1213store and transmit \typ{CLOSURE} objects in $C$. If $C = \kbd{NULL}$ return a1214linking table corresponding to the content of all gp variables. $C$ may then be1215dumped to disk in binary form, for instance.12161217\fun{void}{bincopy_relink}{GEN C, GEN V} given a binary object $C$, as dumped1218by writebin and read back into a session, and a linking table $V$, restore all1219closures contained in $C$ (function pointers are translated to their current1220value).12211222\subsec{Garbage collection}1223See \secref{se:garbage} for a detailed explanation and many examples.12241225\fun{void}{set_avma}{ulong av} reset \kbd{avma} to \kbd{av}.12261227\fun{GEN}{gc_NULL}{pari_sp av} reset \kbd{avma} to \kbd{av} and return1228\kbd{NULL}.12291230The following 6 functions reset \kbd{avma} to \kbd{av} and return $x$:12311232\fun{int}{gc_bool}{pari_sp av, int x}12331234\fun{double}{gc_double}{pari_sp av, double x}12351236\fun{int}{gc_int}{pari_sp av, int x}12371238\fun{long}{gc_long}{pari_sp av, long x}12391240\fun{ulong}{gc_ulong}{pari_sp av, ulong x} This allows for instance1241to return \kbd{gc\_ulong(av, itou(z))}, whereas1242\bprog1243pari_sp av = avma;1244GEN z = ...1245set_avma(av);1246return itou(z);1247@eprog should be frowned upon since \kbd{set\_avma(av)} conceptually1248destroys everything from the reference point on, including $z$.12491250\fun{long}{gc_const}{pari_sp av, GEN x} assumes that $x$ is either not on the1251stack (clone, universal constant such as \kbd{gen\_0}) or was defined1252before \kbd{av}.12531254\fun{void}{cgiv}{GEN x} frees object \kbd{x}, assuming it is the last created1255on the stack.12561257\fun{GEN}{gerepile}{pari_sp p, pari_sp q, GEN x} general garbage collector1258for the stack.12591260\fun{void}{gerepileall}{pari_sp av, int n, ...} cleans up the stack from1261\kbd{av} on (i.e from \kbd{avma} to \kbd{av}), preserving the \kbd{n} objects1262which follow in the argument list (of type \kbd{GEN*}). For instance,1263\kbd{gerepileall(av, 2, \&x, \&y)} preserves \kbd{x} and \kbd{y}.12641265\fun{void}{gerepileallsp}{pari_sp av, pari_sp ltop, int n, ...}1266cleans up the stack between \kbd{av} and \kbd{ltop}, updating1267the \kbd{n} elements which follow \kbd{n} in the argument list (of type1268\kbd{GEN*}). Check that the elements of \kbd{g} have no component between1269\kbd{av} and \kbd{ltop}, and assumes that no garbage is present between1270\kbd{avma} and \kbd{ltop}. Analogous to (but faster than) \kbd{gerepileall}1271otherwise.12721273\fun{GEN}{gerepilecopy}{pari_sp av, GEN x} cleans up the stack from1274\kbd{av} on, preserving the object \kbd{x}. Special case of \kbd{gerepileall}1275(case $\kbd{n} = 1$), except that the routine returns the preserved \kbd{GEN}1276instead of updating its address through a pointer.12771278\fun{void}{gerepilemany}{pari_sp av, GEN* g[], int n} alternative interface1279to \kbd{gerepileall}. The preserved \kbd{GEN}s are the elements of the array1280\kbd{g} of length $n$: \kbd{g[0]}, \kbd{g[1]}, \dots,1281\kbd{g[$n$-1]}. Obsolete: no more efficient than \kbd{gerepileall},1282error-prone, and clumsy (need to declare an extra \kbd{GEN *g}).12831284\fun{void}{gerepilemanysp}{pari_sp av, pari_sp ltop, GEN* g[], int n}1285alternative interface to \kbd{gerepileallsp}. Obsolete.12861287\fun{void}{gerepilecoeffs}{pari_sp av, GEN x, int n} cleans up the stack1288from \kbd{av} on, preserving \kbd{x[0]}, \dots, \kbd{x[n-1]} (which are1289\kbd{GEN}s).12901291\fun{void}{gerepilecoeffssp}{pari_sp av, pari_sp ltop, GEN x, int n}1292cleans up the stack from \kbd{av} to \kbd{ltop}, preserving \kbd{x[0]},1293\dots, \kbd{x[n-1]} (which are \kbd{GEN}s). Same assumptions as in1294\kbd{gerepilemanysp}, of which this is a variant. For instance1295\bprog1296z = cgetg(3, t_COMPLEX);1297av = avma; garbage(); ltop = avma;1298z[1] = fun1();1299z[2] = fun2();1300gerepilecoeffssp(av, ltop, z + 1, 2);1301return z;1302@eprog\noindent1303cleans up the garbage between \kbd{av} and \kbd{ltop}, and connects \kbd{z}1304and its two components. This is marginally more efficient than the standard1305\bprog1306av = avma; garbage(); ltop = avma;1307z = cgetg(3, t_COMPLEX);1308z[1] = fun1();1309z[2] = fun2(); return gerepile(av, ltop, z);1310@eprog\noindent13111312\fun{GEN}{gerepileupto}{pari_sp av, GEN q} analogous to (but faster than)1313\kbd{gerepilecopy}. Assumes that \kbd{q} is connected and that its root was1314created before any component. If \kbd{q} is not on the stack, this is1315equivalent to \kbd{set\_avma(av)}; in particular, sentinels which are not even1316proper \kbd{GEN}s such as \kbd{q = NULL} are allowed.13171318\fun{GEN}{gerepileuptoint}{pari_sp av, GEN q} analogous to (but faster than)1319\kbd{gerepileupto}. Assumes further that \kbd{q} is a \typ{INT}. The1320length and effective length of the resulting \typ{INT} are equal.13211322\fun{GEN}{gerepileuptoleaf}{pari_sp av, GEN q} analogous to (but faster than)1323\kbd{gerepileupto}. Assumes further that \kbd{q} is a leaf, i.e a1324nonrecursive type (\kbd{is\_recursive\_t(typ(q))} is nonzero). Contrary to1325\kbd{gerepileuptoint} and \kbd{gerepileupto}, \kbd{gerepileuptoleaf} leaves1326length and effective length of a \typ{INT} unchanged.13271328\subsec{Garbage collection: advanced use}13291330\fun{void}{stackdummy}{pari_sp av, pari_sp ltop} inhibits the memory area1331between \kbd{av} \emph{included} and \kbd{ltop} \emph{excluded} with respect to1332\kbd{gerepile}, in order to avoid a call to \kbd{gerepile(av, ltop,...)}.1333The stack space is not reclaimed though.13341335More precisely, this routine assumes that \kbd{av} is recorded earlier1336than \kbd{ltop}, then marks the specified stack segment as a1337nonrecursive type of the correct length. Thus gerepile will not inspect1338the zone, at most copy it. To be used in the following situation:1339\bprog1340av0 = avma; z = cgetg(t_VEC, 3);1341gel(z,1) = HUGE(); av = avma; garbage(); ltop = avma;1342gel(z,2) = HUGE(); stackdummy(av, ltop);1343@eprog\noindent1344Compared to the orthodox1345\bprog1346gel(z,2) = gerepile(av, ltop, gel(z,2));1347@eprog\noindent1348or even more wasteful1349\bprog1350z = gerepilecopy(av0, z);1351@eprog\noindent1352we temporarily lose $(\kbd{av} - \kbd{ltop})$ words but save a costly1353\kbd{gerepile}. In principle, a garbage collection higher up the call1354chain should reclaim this later anyway.13551356Without the \kbd{stackdummy}, if the $[\kbd{av}, \kbd{ltop}]$ zone is1357arbitrary (not even valid \kbd{GEN}s as could happen after direct1358truncation via \kbd{setlg}), we would leave dangerous data in the middle1359of~\kbd{z}, which would be a problem for a later1360\bprog1361gerepile(..., ... , z);1362@eprog\noindent1363And even if it were made of valid \kbd{GEN}s, inhibiting the area makes sure1364\kbd{gerepile} will not inspect their components, saving time.13651366Another natural use in low-level routines is to ``shorten'' an existing1367\kbd{GEN} \kbd{z} to its first $\kbd{n}-1$ components:1368\bprog1369setlg(z, n);1370stackdummy((pari_sp)(z + lg(z)), (pari_sp)(z + n));1371@eprog\noindent1372or to its last \kbd{n} components:1373\bprog1374long L = lg(z) - n, tz = typ(z);1375stackdummy((pari_sp)(z + L), (pari_sp)z);1376z += L; z[0] = evaltyp(tz) | evallg(L);1377@eprog13781379The first scenario (safe shortening an existing \kbd{GEN}) is in fact so1380common, that we provide a function for this:13811382\fun{void}{fixlg}{GEN z, long ly} a safe variant of \kbd{setlg(z, ly)}. If1383\kbd{ly} is larger than \kbd{lg(z)} do nothing. Otherwise, shorten $z$ in1384place, using \kbd{stackdummy} to avoid later \kbd{gerepile} problems.13851386\fun{GEN}{gcopy_avma}{GEN x, pari_sp *AVMA} return a copy of $x$ as from1387\kbd{gcopy}, except that we pretend that initially \kbd{avma} is \kbd{*AVMA},1388and that \kbd{*AVMA} is updated accordingly (so that the total size of $x$ is1389the difference between the two successive values of \kbd{*AVMA}). It is not1390necessary for \kbd{*AVMA} to initially point on the stack: \tet{gclone} is1391implemented using this mechanism.13921393\fun{GEN}{icopy_avma}{GEN x, pari_sp av} analogous to \kbd{gcopy\_avma} but1394simpler: assume $x$ is a \typ{INT} and return a copy allocated as if1395initially we had \kbd{avma} equal to \kbd{av}. There is no need to pass a1396pointer and update the value of the second argument: the new (fictitious)1397\kbd{avma} is just the return value (typecast to \kbd{pari\_sp}).13981399\subsec{Debugging the PARI stack}14001401\fun{int}{chk_gerepileupto}{GEN x} returns 1 if \kbd{x} is suitable for1402\kbd{gerepileupto}, and 0 otherwise. In the latter case, print a warning1403explaining the problem.14041405\fun{void}{dbg_gerepile}{pari_sp ltop} outputs the list of all objects on the1406stack between \kbd{avma} and \kbd{ltop}, i.e. the ones that would be inspected1407in a call to \kbd{gerepile(...,ltop,...)}.14081409\fun{void}{dbg_gerepileupto}{GEN q} outputs the list of all objects on the1410stack that would be inspected in a call to \kbd{gerepileupto(...,q)}.14111412\subsec{Copies}14131414\fun{GEN}{gcopy}{GEN x} creates a new copy of $x$ on the stack.14151416\fun{GEN}{gcopy_lg}{GEN x, long l} creates a new copy of $x$1417on the stack, pretending that \kbd{lg(x)} is $l$, which must be less than or1418equal to \kbd{lg(x)}. If equal, the function is equivalent to \kbd{gcopy(x)}.14191420\fun{int}{isonstack}{GEN x} \kbd{true} iff $x$ belongs to the stack.14211422\fun{void}{copyifstack}{GEN x, GEN y} sets \kbd{y = gcopy(x)} if1423$x$ belongs to the stack, and \kbd{y = x} otherwise. This macro evaluates1424its arguments once, contrary to1425\bprog1426y = isonstack(x)? gcopy(x): x;1427@eprog14281429\fun{void}{icopyifstack}{GEN x, GEN y} as \kbd{copyifstack} assuming \kbd{x}1430is a \typ{INT}.14311432\subsec{Simplify}14331434\fun{GEN}{simplify}{GEN x} you should not need that function in library mode.1435One rather uses:14361437\fun{GEN}{simplify_shallow}{GEN x} shallow, faster, version of \tet{simplify}.14381439\section{The PARI heap}1440\subsec{Introduction}14411442It is implemented as a doubly-linked list of \kbd{malloc}'ed blocks of1443memory, equipped with reference counts. Each block has type \kbd{GEN} but need1444not be a valid \kbd{GEN}: it is a chunk of data preceded by a hidden header1445(meaning that we allocate $x$ and return $x + \kbd{header size}$). A1446\tev{clone}, created by \tet{gclone}, is a block which is a valid \kbd{GEN}1447and whose \emph{clone bit} is set.14481449\subsec{Public interface}14501451\fun{GEN}{newblock}{size_t n} allocates a block of $n$ \emph{words} (not bytes).14521453\fun{void}{killblock}{GEN x} deletes the block~$x$ created by \kbd{newblock}.1454Fatal error if $x$ not a block.14551456\fun{GEN}{gclone}{GEN x} creates a new permanent copy of $x$ on the heap1457(allocated using \kbd{newblock}). The \emph{clone bit} of the result is set.14581459\fun{GEN}{gcloneref}{GEN x} if $x$ is not a clone, clone it and return the1460result; otherwise, increase the clone reference count and return $x$.14611462\fun{void}{gunclone}{GEN x} deletes a clone. Deletion at first only decreases1463the reference count by $1$. If the count remains positive, no further action is1464taken; if the count becomes zero, then the clone is actually deleted. In the1465current implementation, this is an alias for \kbd{killblock}, but it is cleaner1466to kill clones (valid \kbd{GEN}s) using this function, and other blocks using1467\kbd{killblock}.14681469\fun{void}{guncloneNULL}{GEN x} same as \kbd{gunclone}, first checking1470whether $x$ is \kbd{NULL} (and doing nothing in this case).14711472\fun{void}{gunclone_deep}{GEN x} is only useful in the context of the GP1473interpreter which may replace arbitrary components of container types1474(\typ{VEC}, \typ{COL}, \typ{MAT}, \typ{LIST}) by clones. If $x$ is such1475a container, the function recursively deletes all clones among the components1476of $x$, then unclones $x$. Useless in library mode: simply use1477\kbd{gunclone}.14781479\fun{void}{guncloneNULL_deep}{GEN x} same as \kbd{gunclone\_deep}, first1480checking whether $x$ is \kbd{NULL} (and doing nothing in this case).14811482\fun{void}{traverseheap}{void(*f)(GEN, void *), void *data} this applies1483\kbd{f($x$, data)} to each object $x$ on the PARI heap, most recent1484first. Mostly for debugging purposes.14851486\fun{GEN}{getheap}{} a simple wrapper around \kbd{traverseheap}. Returns a1487two-component row vector giving the number of objects on the heap and the1488amount of memory they occupy in long words.14891490\fun{GEN}{cgetg_block}{long x, long y} as \kbd{cgetg(x,y)}, creating the return1491value as a \kbd{block}, not on the PARI stack.14921493\fun{GEN}{cgetr_block}{long prec} as \kbd{cgetr(prec)}, creating the return1494value as a \kbd{block}, not on the PARI stack.14951496\subsec{Implementation note} The hidden block header is manipulated using the1497following private functions:14981499\fun{void*}{bl_base}{GEN x} returns the pointer that was actually allocated1500by \kbd{malloc} (can be freed).15011502\fun{long}{bl_refc}{GEN x} the reference count of $x$: the number of pointers1503to this block. Decremented in \kbd{killblock}, incremented by the private1504function \fun{void}{gclone_refc}{GEN x}; block is freed when the reference1505count reaches $0$.15061507\fun{long}{bl_num}{GEN x} the index of this block in the list of all blocks1508allocated so far (including freed blocks). Uniquely identifies a block until1509$2^\B$ blocks have been allocated and this wraps around.15101511\fun{GEN}{bl_next}{GEN x} the block \emph{after} $x$ in the linked list of1512blocks (\kbd{NULL} if $x$ is the last block allocated not yet killed).15131514\fun{GEN}{bl_prev}{GEN x} the block allocated \emph{before} $x$ (never1515\kbd{NULL}).15161517We documented the last four routines as functions for clarity (and type1518checking) but they are actually macros yielding valid lvalues. It is allowed1519to write \kbd{bl\_refc(x)++} for instance.15201521\section{Handling user and temp variables}1522Low-level implementation of user / temporary variables is liable to change. We1523describe it nevertheless for completeness. Currently variables are1524implemented by a single array of values divided in 3 zones: 0--\kbd{nvar}1525(user variables), \kbd{max\_avail}--\kbd{MAXVARN} (temporary variables),1526and \kbd{nvar+1}--\kbd{max\_avail-1} (pool of free variable numbers).15271528\subsec{Low-level}15291530\fun{void}{pari_var_init}{}: a small part of \kbd{pari\_init}. Resets1531variable counters \kbd{nvar} and \kbd{max\_avail}, notwithstanding existing1532variables! In effect, this even deletes \kbd{x}. Don't use it.15331534\fun{void}{pari_var_close}{void} attached destructor, called by1535\kbd{pari\_close}.15361537\fun{long}{pari_var_next}{}: returns \kbd{nvar}, the number of the next user1538variable we can create.15391540\fun{long}{pari_var_next_temp}{} returns \kbd{max\_avail}, the number of the1541next temp variable we can create.15421543\fun{long}{pari_var_create}{entree *ep} low-level initialization of an1544\kbd{EpVAR}. Return the attached (new) variable number.15451546\fun{GEN}{vars_sort_inplace}{GEN z} given a \typ{VECSMALL} $z$ of variable1547numbers, sort $z$ in place according to variable priorities (highest priority1548comes first).15491550\fun{GEN}{vars_to_RgXV}{GEN h} given a \typ{VECSMALL} $z$ of variable numbers,1551return the \typ{VEC} of \kbd{pol\_x}$(z[i])$.15521553\subsec{User variables}15541555\fun{long}{fetch_user_var}{char *s} returns a user variable whose name1556is \kbd{s}, creating it is needed (and using an existing variable otherwise).1557Returns its variable number.15581559\fun{GEN}{fetch_var_value}{long v} returns a shallow copy of the1560current value of the variable numbered $v$. Return \kbd{NULL} for a temporary1561variable.15621563\fun{entree*}{is_entry}{const char *s} returns the \kbd{entree*} attached1564to an identifier \kbd{s} (variable or function), from the interpreter1565hashtables. Return \kbd{NULL} is the identifier is unknown.15661567\subsec{Temporary variables}15681569\fun{long}{fetch_var}{void} returns the number of a new temporary variable1570(decreasing \kbd{max\_avail}).15711572\fun{long}{delete_var}{void} delete latest temp variable created and return1573the number of previous one.15741575\fun{void}{name_var}{long n, char *s} rename temporary variable number1576\kbd{n} to \kbd{s}; mostly useful for nicer printout. Error when trying to1577rename a user variable.15781579\section{Adding functions to PARI}1580\subsec{Nota Bene}1581%1582As mentioned in the \kbd{COPYING} file, modified versions of the PARI package1583can be distributed under the conditions of the GNU General Public License. If1584you do modify PARI, however, it is certainly for a good reason, and we1585would like to know about it, so that everyone can benefit from your changes.1586There is then a good chance that your improvements are incorporated into the1587next release.15881589We classify changes to PARI into four rough classes, where changes of the1590first three types are almost certain to be accepted. The first type includes1591all improvements to the documentation, in a broad sense. This includes1592correcting typos or inaccuracies of course, but also items which are not1593really covered in this document, e.g.~if you happen to write a tutorial,1594or pieces of code exemplifying fine points unduly omitted in the present1595manual.15961597The second type is to expand or modify the configuration routines and skeleton1598files (the \kbd{Configure} script and anything in the \kbd{config/}1599subdirectory) so that compilation is possible (or easier, or more efficient)1600on an operating system previously not catered for. This includes discovering1601and removing idiosyncrasies in the code that would hinder its portability.16021603The third type is to modify existing (mathematical) code, either to correct1604bugs, to add new functionality to existing functions, or to improve their1605efficiency.16061607Finally the last type is to add new functions to PARI. We explain here how1608to do this, so that in particular the new function can be called from \kbd{gp}.16091610\subsec{Coding guidelines}\label{se:coding_guidelines}1611\noindent1612Code your function in a file of its own, using as a guide other functions1613in the PARI sources. One important thing to remember is to clean the stack1614before exiting your main function, since otherwise successive calls to1615the function clutters the stack with unnecessary garbage, and stack1616overflow occurs sooner. Also, if it returns a \kbd{GEN} and you want it1617to be accessible to \kbd{gp}, you have to make sure this \kbd{GEN} is1618suitable for \kbd{gerepileupto} (see \secref{se:garbage}).16191620If error messages or warnings are to be generated in your function, use1621\kbd{pari\_err} and \kbd{pari\_warn} respectively.1622Recall that \kbd{pari\_err} does not return but ends with a \kbd{longjmp}1623statement. As well, instead of explicit \kbd{printf}~/ \kbd{fprintf}1624statements, use the following encapsulated variants:16251626\fun{void}{pari_putc}{char c}: write character \kbd{c} to the output stream.16271628\fun{void}{pari_puts}{char *s}: write \kbd{s} to the output stream.16291630\fun{void}{pari_printf}{const char *fmt, ...}: write following arguments to the1631output stream, according to the conversion specifications in format \kbd{fmt}1632(see \tet{printf}).16331634\fun{void}{err_printf}{const char *fmt, ...}: as \tet{pari_printf}, writing to1635PARI's current error stream.16361637\fun{void}{err_flush}{void} flush error stream.16381639Declare all public functions in an appropriate header file, if you1640want to access them from C. The other functions should be declared1641\kbd{static} in your file.16421643Your function is now ready to be used in library mode after compilation and1644creation of the library. If possible, compile it as a shared library (see1645the \kbd{Makefile} coming with the \kbd{extgcd} example in the1646distribution). It is however still inaccessible from \kbd{gp}.\smallskip16471648\subsec{GP prototypes, parser codes}1649\label{se:gp.interface}1650A \tev{GP prototype} is a character string describing all the GP parser needs1651to know about the function prototype. It contains a sequence of the following1652atoms:16531654\settabs\+\indent&\kbd{Dxxx}\quad&\cr16551656\noindent\item Return type: \kbd{GEN} by default (must be valid for1657\kbd{gerepileupto}), otherwise the following can appear as the \emph{first}1658char of the code string:1659%1660\+& \kbd{i} & return \kbd{int}\cr1661\+& \kbd{l} & return \kbd{long}\cr1662\+& \kbd{u} & return \kbd{ulong}\cr1663\+& \kbd{v} & return \kbd{void}\cr1664\+& \kbd{m} & return a \kbd{GEN} which is not \kbd{gerepile}-safe.\cr16651666The \kbd{m} code is used for member functions, to avoid unnecessary copies. A1667copy opcode is generated by the compiler if the result needs to be kept safe1668for later use.16691670\noindent\item Mandatory arguments, appearing in the same order as the1671input arguments they describe:1672%1673\+& \kbd{G} & \kbd{GEN}\cr1674\+& \kbd{\&}& \kbd{*GEN}\cr1675\+& \kbd{L} & \kbd{long} (we implicitly typecast \kbd{int} to \kbd{long})\cr1676\+& \kbd{U} & \kbd{ulong} \cr1677\+& \kbd{V} & loop variable\cr1678\+& \kbd{n} & variable, expects a \idx{variable number} (a \kbd{long}, not an1679\kbd{*entree})\cr1680\+& \kbd{W} & a \kbd{GEN} which is a lvalue to be modified in place1681(for \typ{LIST})\cr1682\+& \kbd{r} & raw input (treated as a string without quotes). Quoted1683args are copied as strings\cr1684\+&&\quad Stops at first unquoted \kbd{')'} or \kbd{','}. Special chars can1685be quoted using \kbd{'\bs'}\cr1686\+&&\quad Example: \kbd{aa"b\bs n)"c} yields the string \kbd{"aab\bs{n})c"}\cr1687\+& \kbd{s} & expanded string. Example: \kbd{Pi"x"2} yields \kbd{"3.142x2"}\cr1688\+&&\quad Unquoted components can be of any PARI type, converted to string1689following\cr1690\+&&\quad current output format\cr1691\+& \kbd{I} & closure whose value is ignored, as in \kbd{for} loops,\cr1692\+&&\quad to be processed by \fun{void}{closure_evalvoid}{GEN C}\cr1693\+& \kbd{E} & closure whose value is used, as in \kbd{sum} loops,\cr1694\+&&\quad to be processed by \fun{void}{closure_evalgen}{GEN C}\cr1695\+& \kbd{J} & implicit function of arity $1$, as in \kbd{parsum} loops,\cr1696\+&&\quad to be processed by \fun{void}{closure_callgen1}{GEN C}\cr16971698\noindent A \tev{closure} is a GP function in compiled (bytecode) form. It1699can be efficiently evaluated using the \kbd{closure\_eval}$xxx$ functions.17001701\noindent\item Automatic arguments:1702%1703\+& \kbd{f} & Fake \kbd{*long}. C function requires a pointer but we1704do not use the resulting \kbd{long}\cr1705\+& \kbd{b} & current real precision in bits \cr1706\+& \kbd{p} & current real precision in words \cr1707\+& \kbd{P} & series precision (default \kbd{seriesprecision},1708global variable \kbd{precdl} for the library)\cr1709\+& \kbd{C} & lexical context (internal, for \kbd{eval},1710see \kbd{localvars\_read\_str})\cr17111712\noindent\item Syntax requirements, used by functions like1713\kbd{for}, \kbd{sum}, etc.:1714%1715\+& \kbd{=} & separator \kbd{=} required at this point (between two1716arguments)\cr17171718\noindent\item Optional arguments and default values:1719%1720\+& \kbd{E*} & any number of expressions, possibly 0 (see \kbd{E})\cr1721\+& \kbd{s*} & any number of strings, possibly 0 (see \kbd{s})\cr1722\+& \kbd{D\var{xxx}} & argument can be omitted and has a default value\cr17231724The \kbd{E*} code reads all remaining arguments in closure context and passes1725them as a single \typ{VEC}.1726The \kbd{s*} code reads all remaining arguments in \tev{string context} and1727passes the list of strings as a single \typ{VEC}. The automatic concatenation1728rules in string context are implemented so that adjacent strings1729are read as different arguments, as if they had been comma-separated. For1730instance, if the remaining argument sequence is: \kbd{"xx" 1, "yy"}, the1731\kbd{s*} atom sends \kbd{[a, b, c]}, where1732$a$, $b$, $c$ are \kbd{GEN}s of type \typ{STR} (content \kbd{"xx"}),1733\typ{INT} (equal to $1$) and \typ{STR} (content \kbd{"yy"}).17341735The format to indicate a default value (atom starts with a \kbd{D}) is1736``\kbd{D\var{value},\var{type},}'', where \var{type} is the code for any1737mandatory atom (previous group), \var{value} is any valid GP expression1738which is converted according to \var{type}, and the ending comma is1739mandatory. For instance \kbd{D0,L,} stands for ``this optional argument is1740converted to a \kbd{long}, and is \kbd{0} by default''. So if the1741user-given argument reads \kbd{1 + 3} at this point, \kbd{4L} is sent to1742the function; and \kbd{0L} if the argument is omitted. The following1743special notations are available:17441745\settabs\+\indent\indent&\kbd{Dxxx}\quad& optional \kbd{*GEN},&\cr1746\+&\kbd{DG}& optional \kbd{GEN}, & send \kbd{NULL} if argument omitted.\cr17471748\+&\kbd{D\&}& optional \kbd{*GEN}, send \kbd{NULL} if argument omitted.\cr1749\+&&\quad The argument must be prefixed by \kbd{\&}.\cr17501751\+&\kbd{DI}, \kbd{DE}& optional closure, send \kbd{NULL} if argument omitted.\cr17521753\+&\kbd{DP}& optional \kbd{long}, send \kbd{precdl} if argument omitted.\cr17541755\+&\kbd{DV}& optional \kbd{*entree}, send \kbd{NULL} if argument omitted.\cr17561757\+&\kbd{Dn}& optional variable number, $-1$ if omitted.\cr17581759\+&\kbd{Dr}& optional raw string, send \kbd{NULL} if argument omitted.\cr17601761\+&\kbd{Ds}& optional \kbd{char *}, send \kbd{NULL} if argument omitted.\cr17621763\misctitle{Hardcoded limit} C functions using more than 20 arguments are not1764supported. Use vectors if you really need that many parameters.17651766When the function is called under \kbd{gp}, the prototype is scanned and each1767time an atom corresponding to a mandatory argument is met, a user-given1768argument is read (\kbd{gp} outputs an error message it the argument was1769missing). Each time an optional atom is met, a default value is inserted if the1770user omits the argument. The ``automatic'' atoms fill in the argument list1771transparently, supplying the current value of the corresponding variable (or a1772dummy pointer).17731774For instance, here is how you would code the following prototypes, which1775do not involve default values:1776\bprog1777GEN f(GEN x, GEN y, long prec) ----> "GGp"1778void f(GEN x, GEN y, long prec) ----> "vGGp"1779void f(GEN x, long y, long prec) ----> "vGLp"1780long f(GEN x) ----> "lG"1781int f(long x) ----> "iL"1782@eprog\noindent1783If you want more examples, \kbd{gp} gives you easy access to the parser codes1784attached to all GP functions: just type \kbd{\b{h} \var{function}}. You1785can then compare with the C prototypes as they stand in \kbd{paridecl.h}.17861787\misctitle{Remark} If you need to implement complicated control statements1788(probably for some improved summation functions), you need to know1789how the parser implements closures and lexicals and how the evaluator lets1790you deal with them, in particular the \tet{push_lex} and \tet{pop_lex}1791functions. Check their descriptions and adapt the source code in1792\kbd{language/sumiter.c} and \kbd{language/intnum.c}.17931794\subsec{Integration with \kbd{gp} as a shared module}17951796In this section we assume that your Operating System is supported by1797\tet{install}. You have written a function in C following the guidelines is1798\secref{se:coding_guidelines}; in case the function returns a \kbd{GEN}, it1799must satisfy \kbd{gerepileupto} assumptions (see \secref{se:garbage}).18001801You then succeeded in building it as part of a shared library and want to1802finally tell \kbd{gp} about your function. First, find a name for it. It does1803not have to match the one used in library mode, but consistency is nice. It1804has to be a valid GP identifier, i.e.~use only alphabetic characters, digits1805and the underscore character (\kbd{\_}), the first character being1806alphabetic.18071808Then figure out the correct \idx{parser code} corresponding to the function1809prototype (as explained in~\secref{se:gp.interface}) and write a GP script1810like the following:1811\bprog1812install(libname, code, gpname, library)1813addhelp(gpname, "some help text")1814@eprog1815\noindent The \idx{addhelp} part is not mandatory, but very useful if you1816want others to use your module. \kbd{libname} is how the function is named in1817the library, usually the same name as one visible from C.18181819Read that file from your \kbd{gp} session, for instance from your1820\idx{preferences file} (or \kbd{gprc}), and that's it. You1821can now use the new function \var{gpname} under \kbd{gp}, and we would very1822much like to hear about it!1823\smallskip18241825\misctitle{Example}1826A complete description could look like this:1827\bprog1828{1829install(bnfinit0, "GD0,L,DGp", ClassGroupInit, "libpari.so");1830addhelp(ClassGroupInit, "ClassGroupInit(P,{flag=0},{data=[]}):1831compute the necessary data for ...");1832}1833@eprog\noindent which means we have a function \kbd{ClassGroupInit} under1834\kbd{gp}, which calls the library function \kbd{bnfinit0} . The function has1835one mandatory argument, and possibly two more (two \kbd{'D'} in the code),1836plus the current real precision. More precisely, the first argument is a1837\kbd{GEN}, the second one is converted to a \kbd{long} using \kbd{itos}1838(\kbd{0} is passed if it is omitted), and the third one is also a \kbd{GEN},1839but we pass \kbd{NULL} if no argument was supplied by the user. This matches1840the C prototype (from \kbd{paridecl.h}):1841%1842\bprog1843GEN bnfinit0(GEN P, long flag, GEN data, long prec)1844@eprog\noindent1845This function is in fact coded in \kbd{basemath/buch2.c}, and is in this case1846completely identical to the GP function \kbd{bnfinit} but \kbd{gp} does not1847need to know about this, only that it can be found somewhere in the shared1848library \kbd{libpari.so}.18491850\misctitle{Important note} You see in this example that it is the1851function's responsibility to correctly interpret its operands: \kbd{data =1852NULL} is interpreted \emph{by the function} as an empty vector. Note that1853since \kbd{NULL} is never a valid \kbd{GEN} pointer, this trick always1854enables you to distinguish between a default value and actual input: the1855user could explicitly supply an empty vector!18561857\subsec{Library interface for \kbd{install}}18581859There is a corresponding library interface for this \kbd{install}1860functionality, letting you expand the GP parser/evaluator available in the1861library with new functions from your C source code. Functions such as1862\tet{gp_read_str} may then evaluate a GP expression sequence involving calls1863to these new function!18641865\fun{entree *}{install}{void *f, const char *gpname, const char *code}18661867\noindent where \kbd{f} is the (address of the) function (cast to1868\kbd{void*}), \kbd{gpname} is the name by which you want to access your1869function from within your GP expressions, and \kbd{code} is as above.187018711872\subsec{Integration by patching \kbd{gp}}18731874If \tet{install} is not available, and installing Linux or a BSD operating1875system is not an option (why?), you have to hardcode your function in the1876\kbd{gp} binary. Here is what needs to be done:18771878\item Fetch the complete sources of the PARI distribution.18791880\item Drop the function source code module in an appropriate directory1881(a priori \kbd{src/modules}), and declare all public functions1882in \kbd{src/headers/paridecl.h}.18831884\item Choose a help section and add a file1885\kbd{src/functions/\var{section}/\var{gpname}}1886containing the following, keeping the notation above:1887\bprog1888Function: @com\var{gpname}1889Section: @com\var{section}1890C-Name: @com\var{libname}1891Prototype: @com\var{code}1892Help: @com\var{some help text}1893@eprog\noindent1894(If the help text does not fit on a single line, continuation lines must1895start by a whitespace character.) Two GP2C-related fields (\kbd{Description}1896and \kbd{Wrapper}) are also available to improve the code GP2C generates when1897compiling scripts involving your function. See the GP2C documentation for1898details.18991900\item Launch \kbd{Configure}, which should pick up your C files and build an1901appropriate \kbd{Makefile}. At this point you can recompile \kbd{gp}, which1902will first rebuild the functions database.19031904\misctitle{Example} We reuse the \kbd{ClassGroupInit} / \kbd{bnfinit0}1905from the preceding section. Since the C source code is already part1906of PARI, we only need to add a file19071908\kbd{functions/number\_fields/ClassGroupInit}19091910\noindent containing the following:1911\bprog1912Function: ClassGroupInit1913Section: number_fields1914C-Name: bnfinit01915Prototype: GD0,L,DGp1916Help: ClassGroupInit(P,{flag=0},{tech=[]}): this routine does @com\dots1917@eprog\noindent1918and recompile \kbd{gp}.19191920\section{Globals related to PARI configuration}1921\subsec{PARI version numbers}19221923\noindent \tet{paricfg_version_code} encodes in a single \kbd{long}, the Major1924and minor version numbers as well as the patchlevel.19251926\fun{long}{PARI_VERSION}{long M, long m, long p} produces the version code1927attached to release $M.m.p$. Each code identifies a unique PARI release,1928and corresponds to the natural total order on the set of releases (bigger1929code number means more recent release).19301931\noindent \tet{PARI_VERSION_SHIFT} is the number of bits used to store each of1932the integers $M$, $m$, $p$ in the version code.19331934\noindent \tet{paricfg_vcsversion} is a version string related to the1935revision control system used to handle your sources, if any. For instance1936\kbd{git-}\emph{commit hash} if compiled from a git repository.19371938The two character strings \tet{paricfg_version} and \tet{paricfg_buildinfo},1939correspond to the first two lines printed by \kbd{gp} just before the1940Copyright message. The character string \tet{paricfg_compiledate} is the1941date of compilation which appears on the next line. The character string1942\tet{paricfg_mt_engine} is the name of the threading engine on the next line.19431944In the string \kbd{paricfg\_buildinfo}, the substring \kbd{"\%s"} needs1945to be substituted by the output of the function \kbd{pari\_kernel\_version}.19461947\fun{const char *}{pari_kernel_version}{void}19481949\fun{GEN}{pari_version}{} returns the version number as a PARI object, a1950\typ{VEC} with three \typ{INT} and one \typ{STR} components.19511952\subsec{Miscellaneous}19531954\tet{paricfg_datadir}: character string. The location of PARI's \tet{datadir}.19551956\tet{paricfg_gphelp}: character string. The name of an external help command1957for \kbd{??} (such as the \kbd{gphelp} script)19581959\newpage1960\chapter{Arithmetic kernel: Level 0 and 1}19611962\section{Level 0 kernel (operations on ulongs)}19631964\subsec{Micro-kernel}1965The Level 0 kernel simulates basic operations of the 68020 processor on which1966PARI was originally implemented. They need ``global'' \kbd{ulong} variables1967\kbd{overflow} (which will contain only 0 or 1) and \kbd{hiremainder} to1968function properly. A routine using one of these lowest-level functions1969where the description mentions either \kbd{hiremainder} or \kbd{overflow}1970must declare the corresponding1971\bprog1972LOCAL_HIREMAINDER; /* provides 'hiremainder' */1973LOCAL_OVERFLOW; /* provides 'overflow' */1974@eprog\noindent1975in a declaration block. Variables \kbd{hiremainder} and \kbd{overflow} then1976become available in the enclosing block. For instance a loop over the powers1977of an \kbd{ulong}~\kbd{p} protected from overflows could read1978\bprog1979while (pk < lim)1980{1981LOCAL_HIREMAINDER;1982...1983pk = mulll(pk, p); if (hiremainder) break;1984}1985@eprog\noindent1986For most architectures, the functions mentioned below are really chunks of1987inlined assembler code, and the above `global' variables are actually1988local register values.19891990\fun{ulong}{addll}{ulong x, ulong y} adds \kbd{x} and \kbd{y}, returns the1991lower \B\ bits and puts the carry bit into \kbd{overflow}.19921993\fun{ulong}{addllx}{ulong x, ulong y} adds \kbd{overflow} to the sum of the1994\kbd{x} and \kbd{y}, returns the lower \B\ bits and puts the carry bit into1995\kbd{overflow}.19961997\fun{ulong}{subll}{ulong x, ulong y} subtracts \kbd{x} and \kbd{y}, returns1998the lower \B\ bits and put the carry (borrow) bit into \kbd{overflow}.19992000\fun{ulong}{subllx}{ulong x, ulong y} subtracts \kbd{overflow} from the2001difference of \kbd{x} and \kbd{y}, returns the lower \B\ bits and puts the2002carry (borrow) bit into \kbd{overflow}.20032004\fun{int}{bfffo}{ulong x} returns the number of leading zero bits in \kbd{x}.2005That is, the number of bit positions by which it would have to be shifted2006left until its leftmost bit first becomes equal to~1, which can be between 02007and $\B-1$ for nonzero \kbd{x}. When \kbd{x} is~0, the result is undefined.20082009\fun{ulong}{mulll}{ulong x, ulong y} multiplies \kbd{x} by \kbd{y}, returns2010the lower \B\ bits and stores the high-order \B\ bits into \kbd{hiremainder}.20112012\fun{ulong}{addmul}{ulong x, ulong y} adds \kbd{hiremainder} to the product2013of \kbd{x} and \kbd{y}, returns the lower \B\ bits and stores the high-order2014\B\ bits into \kbd{hiremainder}.20152016\fun{ulong}{divll}{ulong x, ulong y} returns the quotient of2017$ \left(\kbd{hiremainder} * 2^{\B}\right) + \kbd{x} $2018by \kbd{y} and stores the remainder into \kbd{hiremainder}. An error occurs2019if the quotient cannot be represented by an \kbd{ulong}, i.e.~if initially2020$\kbd{hiremainder}\ge\kbd{y}$.20212022\fun{long}{hammingl}{ulong x)} returns the Hamming weight of $x$, i.e.~the2023number of nonzero bits in its binary expansion.20242025\misctitle{Obsolete routines} Those functions are awkward and no longer used;2026they are only provided for backward compatibility:20272028\fun{ulong}{shiftl}{ulong x, ulong y} returns $x$ shifted left by $y$ bits,2029i.e.~\kbd{$x$ << $y$}, where we assume that $0\leq y\leq\B$. The global variable2030\kbd{hiremainder} receives the bits that were shifted out,2031i.e.~\kbd{$x$ >> $(\B - y)$}.20322033\fun{ulong}{shiftlr}{ulong x, ulong y} returns $x$ shifted right by $y$ bits,2034i.e.~\kbd{$x$ >> $y$}, where we assume that $0\leq y\leq\B$. The global variable2035\kbd{hiremainder} receives the bits that were shifted out,2036i.e.~\kbd{$x$ << $(\B - y)$}.20372038\subsec{Modular kernel}2039The following routines are not part of the level 0 kernel per se, but2040implement modular operations on words in terms of the above. They are written2041so that no overflow may occur. Let $m \geq 1$ be the modulus; all operands2042representing classes modulo $m$ are assumed to belong to $[0,m-1]$. The2043result may be wrong for a number of reasons otherwise: it may not be reduced,2044overflow can occur, etc.20452046\fun{int}{odd}{ulong x} returns 1 if $x$ is odd, and 0 otherwise.20472048\fun{int}{both_odd}{ulong x, ulong y} returns 1 if $x$ and $y$ are both odd,2049and 0 otherwise.20502051\fun{ulong}{invmod2BIL}{ulong x} returns the smallest2052positive representative of $x^{-1}$ mod $2^\B$, assuming $x$ is odd.20532054\fun{ulong}{Fl_add}{ulong x, ulong y, ulong m} returns the smallest2055nonnegative representative of $x + y$ modulo $m$.20562057\fun{ulong}{Fl_neg}{ulong x, ulong m} returns the smallest2058nonnegative representative of $-x$ modulo $m$.20592060\fun{ulong}{Fl_sub}{ulong x, ulong y, ulong m} returns the smallest2061nonnegative representative of $x - y$ modulo $m$.20622063\fun{long}{Fl_center}{ulong x, ulong m, ulong mo2} returns the representative2064in $]-m/2,m/2]$ of $x$ modulo $m$. Assume $0 \leq x < m$ and2065$\kbd{mo2} = m >> 1$.20662067\fun{ulong}{Fl_mul}{ulong x, ulong y, ulong m} returns the smallest2068nonnegative representative of $x y$ modulo $m$.20692070\fun{ulong}{Fl_double}{ulong x, ulong m} returns $2x$ modulo $m$.20712072\fun{ulong}{Fl_triple}{ulong x, ulong m} returns $3x$ modulo $m$.20732074\fun{ulong}{Fl_halve}{ulong x, ulong m} returns $z$ such that $2\*z = x$ modulo2075$m$ assuming such $z$ exists.20762077\fun{ulong}{Fl_sqr}{ulong x, ulong m} returns the smallest nonnegative2078representative of $x^2$ modulo $m$.20792080\fun{ulong}{Fl_inv}{ulong x, ulong m} returns the smallest2081positive representative of $x^{-1}$ modulo $m$. If $x$ is not invertible2082mod~$m$, raise an exception.20832084\fun{ulong}{Fl_invsafe}{ulong x, ulong m} returns the smallest2085positive representative of $x^{-1}$ modulo $m$. If $x$ is not invertible2086mod~$m$, return $0$ (which is ambiguous if $m=1$).20872088\fun{ulong}{Fl_invgen}{ulong x, ulong m, ulong *pg} set \kbd{*pg} to2089$g = \gcd(x,m)$ and return $u$ in $(\Z/m\Z)^*$ such that $x u = g$ modulo $m$.2090We have $g = 1$ if and only if $x$ is invertible, and in this case $u$2091is its inverse.20922093\fun{ulong}{Fl_div}{ulong x, ulong y, ulong m} returns the smallest2094nonnegative representative of $x y^{-1}$ modulo $m$. If $y$ is not invertible2095mod $m$, raise an exception.20962097\fun{ulong}{Fl_powu}{ulong x, ulong n, ulong m} returns the smallest2098nonnegative representative of $x^n$ modulo $m$.20992100\fun{GEN}{Fl_powers}{ulong x, long n, ulong p} returns2101$[\kbd{x}^0, \dots, \kbd{x}^\kbd{n}]$ modulo $m$, as a \typ{VECSMALL}.21022103\fun{ulong}{Fl_sqrt}{ulong x, ulong p} returns the square root of \kbd{x}2104modulo \kbd{p} (smallest nonnegative representative). Assumes \kbd{p} to be2105prime, and \kbd{x} to be a square modulo \kbd{p}.21062107\fun{ulong}{Fl_sqrtl}{ulong x, ulong l, ulong p} returns a $l$-the root of \kbd{x}2108modulo \kbd{p}. Assumes \kbd{p} to be prime and $p \equiv 1 \pmod{l}$, and2109\kbd{x} to be a $l$-th power modulo \kbd{p}.21102111\fun{ulong}{Fl_sqrtn}{ulong a, ulong n, ulong p, ulong *zn}2112returns \kbd{ULONG\_MAX} if $a$ is not an $n$-th power residue mod $p$.2113Otherwise, returns an $n$-th root of $a$; if \kbd{zn} is not \kbd{NULL}2114set it to a primitive $m$-th root of 1, $m = \gcd(p-1,n)$ allowing to compute2115all $m$ solutions in $\F_p$ of the equation $x^n = a$.21162117\fun{ulong}{Fl_log}{ulong a, ulong g, ulong ord, ulong p} Let $g$ such that2118$g^{ord} \equiv 1 \pmod{p}$. Return an integer $e$ such that2119$a^e \equiv g \pmod{p}$. If $e$ does not exist, the result is undefined.21202121\fun{ulong}{Fl_order}{ulong a, ulong o, ulong p} returns the order of the2122\kbd{Fp} \kbd{a}. It is assumed that \kbd{o} is a multiple of the order of2123\kbd{a}, $0$ being allowed (no nontrivial information).21242125\fun{ulong}{random_Fl}{ulong p} returns a pseudo-random integer uniformly2126distributed in $0, 1, \dots p-1$.21272128\fun{ulong}{nonsquare_Fl}{ulong p} return a quadratic nonresidue modulo $p$,2129assuming $p$ is an odd prime. If $p$ is $3$ mod $4$, return $p-1$, else2130return the smallest (prime) nonresidue.21312132\fun{ulong}{pgener_Fl}{ulong p} returns the smallest \idx{primitive root}2133modulo \kbd{p}, assuming \kbd{p} is prime.21342135\fun{ulong}{pgener_Zl}{ulong p} returns the smallest primitive root modulo2136$p^k$, $k > 1$, assuming $p$ is an odd prime.21372138\fun{ulong}{pgener_Fl_local}{ulong p, GEN L}, see \kbd{gener\_Fp\_local},2139\kbd{L} is an \kbd{Flv}.21402141\fun{ulong}{factorial_Fl}{long n, ulong p} return $n!$ mod $p$.21422143\subsec{Modular kernel with ``precomputed inverse''}21442145This is based on an algorithm by T. Grandlund and N. M\"{o}ller in2146``Improved division by invariant integers''2147\url{http://gmplib.org/~tege/division-paper.pdf}.21482149In the following, we set $B=\B$.21502151\fun{ulong}{get_Fl_red}{ulong p} returns a pseudo inverse \var{pi} for $p$21522153\fun{ulong}{divll_pre}{ulong x, ulong p, ulong yi}2154as divll, where $yi$ is the pseudo inverse of $y$.21552156\fun{ulong}{remll_pre}{ulong u1, ulong u0, ulong p, ulong pi} returns2157the Euclidean remainder of $u_1\*2^B+u_0$ modulo $p$, assuming $pi$ is the2158pseudo inverse of $p$. This function is faster if $u_1 < p$.21592160\fun{ulong}{remlll_pre}{ulong u2, ulong u1, ulong u0, ulong p, ulong pi}2161returns the Euclidean remainder of $u_2\*2^{2\*B}+u_1\*2^{B}+u_0$ modulo $p$,2162assuming $pi$ is the pseudo inverse of $p$.21632164\fun{ulong}{Fl_sqr_pre}{ulong x, ulong p, ulong pi} returns $x^2$ modulo $p$,2165assuming $pi$ is the pseudo inverse of $p$.21662167\fun{ulong}{Fl_mul_pre}{ulong x, ulong y, ulong p, ulong pi} returns $x\*y$2168modulo $p$, assuming $pi$ is the pseudo inverse of $p$.21692170\fun{ulong}{Fl_addmul_pre}{ulong a, ulong b, ulong c, ulong p, ulong pi}2171returns $a+b\*c$ modulo $p$, assuming $pi$ is the pseudo inverse of $p$.21722173\fun{ulong}{Fl_addmulmul_pre}{ulong a,ulong b, ulong c,ulong d, ulong p, ulong pi}2174returns $a\*b+c\*d$ modulo $p$, assuming $pi$ is the pseudo inverse of $p$.21752176\fun{ulong}{Fl_powu_pre}{ulong x, ulong n, ulong p, ulong pi} returns2177$x^n$ modulo $p$, assuming $pi$ is the pseudo inverse of $p$.21782179\fun{GEN}{Fl_powers_pre}{ulong x, long n, ulong p, ulong pi} returns2180the vector (\typ{VECSMALL}) $(x^0, \dots, x^n)$, assuming $pi$ is2181the pseudo inverse of $p$.21822183\fun{ulong}{Fl_log_pre}{ulong a, ulong g, ulong ord, ulong p, ulong pi}2184as \kbd{Fl\_log}, assuming $pi$ is the pseudo inverse of $p$.21852186\fun{ulong}{Fl_sqrt_pre}{ulong x, ulong p, ulong pi} returns a square root2187of $x$ modulo $p$, assuming $pi$ is the pseudo inverse of $p$.2188See \kbd{Fl\_sqrt}.21892190\fun{ulong}{Fl_sqrtl_pre}{ulong x, ulong l, ulong p, ulong pi}2191returns a $l$-the root of \kbd{x}2192modulo \kbd{p}, assuming $pi$ is the pseudo inverse of $p$,2193$p$ prime and $p \equiv 1 \pmod{l}$, and \kbd{x} to be a $l$-th power modulo2194\kbd{p}.21952196\fun{ulong}{Fl_sqrtn_pre}{ulong x, ulong n, ulong p, ulong *zn}2197See \kbd{Fl\_sqrtn}, assuming $pi$ is the pseudo inverse of $p$.21982199\fun{ulong}{Fl_2gener_pre}{ulong p, ulong pi} return a generator of2200the $2$-Sylow subgroup of $\F_p^*$. To use with \kbd{Fl\_sqrt\_pre\_i}.22012202\fun{ulong}{Fl_sqrt_pre_i}{ulong x, ulong s2, ulong p, ulong pi}2203as \kbd{Fl\_sqrt\_pre} where \kbd{s2} is the element returned by2204\kbd{Fl\_2gener\_pre}.22052206\subsec{Switching between Fl\_xxx and standard operators}22072208Even though the \kbd{Fl\_xxx} routines are efficient, they are slower than2209ordinary \kbd{long} operations, using the standard \kbd{+}, \kbd{\%}, etc.2210operators.2211The following macro is used to choose in a portable way the most efficient2212functions for given operands:22132214\fun{int}{SMALL_ULONG}{ulong p} true if $2p^2 <2^\B$. In that case, it is2215possible to use ordinary operators efficiently. If $p < 2^\B$, one2216may still use the \kbd{Fl\_xxx} routines. Otherwise, one must use generic2217routines. For instance, the scalar product of the \kbd{GEN}s $x$ and $y$ mod2218$p$ could be computed as follows.2219\bprog2220long i, l = lg(x);2221if (lgefint(p) > 3)2222{ /* arbitrary */2223GEN s = gen_0;2224for (i = 1; i < l; i++) s = addii(s, mulii(gel(x,i), gel(y,i)));2225return modii(s, p).2226}2227else2228{2229ulong s = 0, pp = itou(p);2230x = ZV_to_Flv(x, pp);2231y = ZV_to_Flv(y, pp);2232if (SMALL_ULONG(pp))2233{ /* very small */2234for (i = 1; i < l; i++)2235{2236s += x[i] * y[i];2237if (s & HIGHBIT) s %= pp;2238}2239s %= pp;2240}2241else2242{ /* small */2243for (i = 1; i < l; i++)2244s = Fl_add(s, Fl_mul(x[i], y[i], pp), pp);2245}2246return utoi(s);2247}2248@eprog\noindent2249In effect, we have three versions of the same code: very small, small, and2250arbitrary inputs. The very small and arbitrary variants use lazy reduction2251and reduce only when it becomes necessary: when overflow might occur (very2252small), and at the very end (very small, arbitrary).22532254\section{Level 1 kernel (operations on longs, integers and reals)}22552256\misctitle{Note} Some functions consist of an elementary operation,2257immediately followed by an assignment statement. They will be introduced as2258in the following example:22592260\fun{GEN}{gadd[z]}{GEN x, GEN y[, GEN z]} followed by the explicit2261description of the function22622263\fun{GEN}{gadd}{GEN x, GEN y}22642265\noindent which creates its result on the stack, returning a \kbd{GEN} pointer2266to it, and the parts in brackets indicate that there exists also a function22672268\fun{void}{gaddz}{GEN x, GEN y, GEN z}22692270\noindent which assigns its result to the pre-existing object2271\kbd{z}, leaving the stack unchanged. These assignment variants are kept for2272backward compatibility but are inefficient: don't use them.22732274\subsec{Creation}22752276\fun{GEN}{cgeti}{long n} allocates memory on the PARI stack for a \typ{INT}2277of length~\kbd{n}, and initializes its first codeword. Identical to2278\kbd{cgetg(n,\typ{INT})}.22792280\fun{GEN}{cgetipos}{long n} allocates memory on the PARI stack for a2281\typ{INT} of length~\kbd{n}, and initializes its two codewords. The sign2282of \kbd{n} is set to $1$.22832284\fun{GEN}{cgetineg}{long n} allocates memory on the PARI stack for a negative2285\typ{INT} of length~\kbd{n}, and initializes its two codewords. The sign2286of \kbd{n} is set to $-1$.22872288\fun{GEN}{cgetr}{long n} allocates memory on the PARI stack for a \typ{REAL}2289of length~\kbd{n}, and initializes its first codeword. Identical to2290\kbd{cgetg(n,\typ{REAL})}.22912292\fun{GEN}{cgetc}{long n} allocates memory on the PARI stack for a2293\typ{COMPLEX}, whose real and imaginary parts are \typ{REAL}s2294of length~\kbd{n}.22952296\fun{GEN}{real_1}{long prec} create a \typ{REAL} equal to $1$ to \kbd{prec}2297words of accuracy.22982299\fun{GEN}{real_1_bit}{long bitprec} create a \typ{REAL} equal to $1$ to2300\kbd{bitprec} bits of accuracy.23012302\fun{GEN}{real_m1}{long prec} create a \typ{REAL} equal to $-1$ to \kbd{prec}2303words of accuracy.23042305\fun{GEN}{real_0_bit}{long bit} create a \typ{REAL} equal to $0$ with2306exponent $-\kbd{bit}$.23072308\fun{GEN}{real_0}{long prec} is a shorthand for2309\bprog2310real_0_bit( -prec2nbits(prec) )2311@eprog23122313\fun{GEN}{int2n}{long n} creates a \typ{INT} equal to \kbd{1<<n} (i.e2314$2^n$ if $n \geq 0$, and $0$ otherwise).23152316\fun{GEN}{int2u}{ulong n} creates a \typ{INT} equal to $2^n$.23172318\fun{GEN}{int2um1}{long n} creates a \typ{INT} equal to $2^n - 1$.23192320\fun{GEN}{real2n}{long n, long prec} create a \typ{REAL} equal to $2^n$2321to \kbd{prec} words of accuracy.23222323\fun{GEN}{real_m2n}{long n, long prec} create a \typ{REAL} equal to $-2^n$2324to \kbd{prec} words of accuracy.23252326\fun{GEN}{strtoi}{char *s} convert the character string \kbd{s} to a2327nonnegative \typ{INT}.2328Decimal numbers, hexadecimal numbers prefixed by \kbd{0x} and binary numbers prefixed2329by \kbd{0b} are allowed. The string \kbd{s} consists exclusively of digits:2330no leading sign, no whitespace. Leading zeroes are discarded.23312332\fun{GEN}{strtor}{char *s, long prec} convert the character string \kbd{s} to2333a nonnegative \typ{REAL} of precision \kbd{prec}. The string \kbd{s}2334consists exclusively of digits and optional decimal point and exponent2335(\kbd{e} or \kbd{E}): no leading sign, no whitespace. Leading zeroes are2336discarded.23372338\subsec{Assignment}2339In this section, the \kbd{z} argument in the \kbd{z}-functions must be of type2340\typ{INT} or~\typ{REAL}.23412342\fun{void}{mpaff}{GEN x, GEN z} assigns \kbd{x} into~\kbd{z} (where \kbd{x}2343and \kbd{z} are \typ{INT} or \typ{REAL}).2344Assumes that $\kbd{lg(z)} > 2$.23452346\fun{void}{affii}{GEN x, GEN z} assigns the \typ{INT} \kbd{x} into the2347\typ{INT}~\kbd{z}.23482349\fun{void}{affir}{GEN x, GEN z} assigns the \typ{INT} \kbd{x} into the2350\typ{REAL}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.23512352\fun{void}{affiz}{GEN x, GEN z} assigns \typ{INT}~\kbd{x} into \typ{INT} or2353\typ{REAL}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.23542355\fun{void}{affsi}{long s, GEN z} assigns the \kbd{long}~\kbd{s} into the2356\typ{INT}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.23572358\fun{void}{affsr}{long s, GEN z} assigns the \kbd{long}~\kbd{s} into the2359\typ{REAL}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.23602361\fun{void}{affsz}{long s, GEN z} assigns the \kbd{long}~\kbd{s} into the2362\typ{INT} or \typ{REAL}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.23632364\fun{void}{affui}{ulong u, GEN z} assigns the \kbd{ulong}~\kbd{u} into the2365\typ{INT}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.23662367\fun{void}{affur}{ulong u, GEN z} assigns the \kbd{ulong}~\kbd{u} into the2368\typ{REAL}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.23692370\fun{void}{affrr}{GEN x, GEN z} assigns the \typ{REAL}~\kbd{x} into the2371\typ{REAL}~\kbd{z}.23722373\fun{void}{affgr}{GEN x, GEN z} assigns the scalar \kbd{x} into the2374\typ{REAL}~\kbd{z}, if possible.23752376\noindent The function \kbd{affrs} and \kbd{affri} do not exist. So don't use2377them.23782379\fun{void}{affrr_fixlg}{GEN y, GEN z} a variant of \kbd{affrr}. First shorten2380$z$ so that it is no longer than $y$, then assigns $y$ to $z$. This is used2381in the following scenario: room is reserved for the result but, due to2382cancellation, fewer words of accuracy are available than had been2383anticipated; instead of appending meaningless $0$s to the mantissa, we store2384what was actually computed.23852386Note that shortening $z$ is not quite straightforward, since \kbd{setlg(z, ly)}2387would leave garbage on the stack, which \kbd{gerepile} might later inspect.2388It is done using23892390\fun{void}{fixlg}{GEN z, long ly} see \tet{stackdummy} and the examples that2391follow.23922393\subsec{Copy}23942395\fun{GEN}{icopy}{GEN x} copy relevant words of the \typ{INT}~\kbd{x} on the2396stack: the length and effective length of the copy are equal.23972398\fun{GEN}{rcopy}{GEN x} copy the \typ{REAL}~\kbd{x} on the stack.23992400\fun{GEN}{leafcopy}{GEN x} copy the leaf~\kbd{x} on the2401stack (works in particular for \typ{INT}s and \typ{REAL}s).2402Contrary to \kbd{icopy}, \kbd{leafcopy} preserves the original2403length of a \typ{INT}. The obsolete form \fun{GEN}{mpcopy}{GEN x}2404is still provided for backward compatibility.24052406This function also works on recursive types, copying them as if they were2407leaves, i.e.~making a shallow copy in that case: the components of the copy2408point to the same data as the component of the source; see also2409\kbd{shallowcopy}.24102411\fun{GEN}{leafcopy_avma}{GEN x, pari_sp av} analogous to \kbd{gcopy\_avma}2412but simpler: assume $x$ is a leaf and return a copy allocated as if2413initially we had \kbd{avma} equal to \kbd{av}. There is no need to pass a2414pointer and update the value of the second argument: the new (fictitious)2415\kbd{avma} is just the return value (typecast to \kbd{pari\_sp}).24162417\fun{GEN}{icopyspec}{GEN x, long nx} copy the \kbd{nx} words2418\kbd{x[2]}, \dots, \kbd{x[nx+1]} to make up a new \typ{INT}. Set the sign2419to $1$.24202421\subsec{Conversions}24222423\fun{GEN}{itor}{GEN x, long prec} converts the \typ{INT}~\kbd{x} to a2424\typ{REAL} of length \kbd{prec} and return the latter.2425Assumes that $\kbd{prec} > 2$.24262427\fun{long}{itos}{GEN x} converts the \typ{INT}~\kbd{x} to a \kbd{long} if2428possible, otherwise raise an exception. We consider the conversion2429to be possible if and only if $|x| \leq \kbd{LONG\_MAX}$, i.e. $|x| < 2^{63}$2430on a 64-bit architecture. Since the range is symetric, the output of2431\kbd{itos} can safely be negated.24322433\fun{long}{itos_or_0}{GEN x} converts the \typ{INT}~\kbd{x} to a \kbd{long} if2434possible, otherwise return $0$.24352436\fun{int}{is_bigint}{GEN n} true if \kbd{itos(n)} would give an error.24372438\fun{ulong}{itou}{GEN x} converts the \typ{INT}~\kbd{|x|} to an \kbd{ulong} if2439possible, otherwise raise an exception. The conversion is possible if2440and only if $\kbd{lgefint}(x) \leq 3$.24412442\fun{long}{itou_or_0}{GEN x} converts the \typ{INT}~\kbd{|x|} to an2443\kbd{ulong} if possible, otherwise return $0$.24442445\fun{GEN}{stoi}{long s} creates the \typ{INT} corresponding to the2446\kbd{long}~\kbd{s}.24472448\fun{GEN}{stor}{long s, long prec} converts the \kbd{long}~\kbd{s} into a2449\typ{REAL} of length \kbd{prec} and return the latter. Assumes that2450$\kbd{prec} > 2$.24512452\fun{GEN}{utoi}{ulong s} converts the \kbd{ulong}~\kbd{s} into a \typ{INT}2453and return the latter.24542455\fun{GEN}{utoipos}{ulong s} converts the \emph{nonzero} \kbd{ulong}~\kbd{s}2456into a \typ{INT} and return the latter.24572458\fun{GEN}{utoineg}{ulong s} converts the \emph{nonzero} \kbd{ulong}~\kbd{s}2459into the \typ{INT} $-s$ and return the latter.24602461\fun{GEN}{utor}{ulong s, long prec} converts the \kbd{ulong}~\kbd{s} into a2462\typ{REAL} of length \kbd{prec} and return the latter. Assumes that2463$\kbd{prec} > 2$.24642465\fun{GEN}{rtor}{GEN x, long prec} converts the \typ{REAL}~\kbd{x} to a2466\typ{REAL} of length \kbd{prec} and return the latter. If2467$\kbd{prec} < \kbd{lg(x)}$, round properly. If $\kbd{prec} > \kbd{lg(x)}$,2468pad with zeroes. Assumes that $\kbd{prec} > 2$.24692470\noindent The following function is also available as a special case of2471\tet{mkintn}:24722473\fun{GEN}{uu32toi}{ulong a, ulong b} returns the \kbd{GEN} equal to $2^{32} a +2474b$, \emph{assuming} that $a,b < 2^{32}$. This does not depend on2475\kbd{sizeof(long)}: the behavior is as above on both $32$ and $64$-bit2476machines.24772478\fun{GEN}{uu32toineg}{ulong a, ulong b} returns the \kbd{GEN} equal to2479$- (2^{32} a + b)$, \emph{assuming} that $a,b < 2^{32}$ and that one of $a$2480or $b$ is positive. This does not depend on \kbd{sizeof(long)}: the behavior2481is as above on both $32$ and $64$-bit machines.24822483\fun{GEN}{uutoi}{ulong a, ulong b} returns the \kbd{GEN} equal to2484$2^{\B} a + b$.24852486\fun{GEN}{uutoineg}{ulong a, ulong b} returns the \kbd{GEN} equal to2487$-(2^{\B} a + b)$.24882489\subsec{Integer parts}2490The following four functions implement the conversion from \typ{REAL} to2491\typ{INT} using standard rounding modes. Contrary to usual semantics2492(complement the mantissa with an infinite number of 0), they will raise an2493error \emph{precision loss in truncation} if the \typ{REAL} represents a2494range containing more than one integer.24952496\fun{GEN}{ceilr}{GEN x} smallest integer larger or equal2497to the \typ{REAL}~\kbd{x} (i.e.~the \kbd{ceil} function).24982499\fun{GEN}{floorr}{GEN x} largest integer smaller or equal to the2500\typ{REAL}~\kbd{x} (i.e.~the \kbd{floor} function).25012502\fun{GEN}{roundr}{GEN x} rounds the \typ{REAL} \kbd{x} to the nearest integer2503(towards~$+\infty$ in case of tie).25042505\fun{GEN}{truncr}{GEN x} truncates the \typ{REAL}~\kbd{x} (not the same as2506\kbd{floorr} if \kbd{x} is negative).25072508The following four function are analogous, but can also treat the trivial2509case when the argument is a \typ{INT}:25102511\fun{GEN}{mpceil}{GEN x}2512as \kbd{ceilr} except that \kbd{x} may be a \typ{INT}.25132514\fun{GEN}{mpfloor}{GEN x}2515as \kbd{floorr} except that \kbd{x} may be a \typ{INT}.25162517\fun{GEN}{mpround}{GEN x}2518as \kbd{roundr} except that \kbd{x} may be a \typ{INT}.25192520\fun{GEN}{mptrunc}{GEN x}2521as \kbd{truncr} except that \kbd{x} may be a \typ{INT}.25222523\fun{GEN}{diviiround}{GEN x, GEN y} if \kbd{x} and \kbd{y} are \typ{INT}s,2524returns the quotient $\kbd{x}/\kbd{y}$ of \kbd{x} and~\kbd{y}, rounded to2525the nearest integer. If $\kbd{x}/\kbd{y}$ falls exactly halfway between2526two consecutive integers, then it is rounded towards~$+\infty$ (as for2527\tet{roundr}).25282529\fun{GEN}{ceil_safe}{GEN x}, \kbd{x} being a real number (not necessarily a2530\typ{REAL}) returns the smallest integer which is larger than any possible2531incarnation of \kbd{x}. (Recall that a \typ{REAL} represents an interval of2532possible values.) Note that \kbd{gceil} raises an exception if the input2533accuracy is too low compared to its magnitude.25342535\fun{GEN}{floor_safe}{GEN x}, \kbd{x} being a real number (not necessarily a2536\typ{REAL}) returns the largest integer which is smaller than any possible2537incarnation of \kbd{x}. (Recall that a \typ{REAL} represents an interval of2538possible values.) Note that \kbd{gfloor} raises an exception if the input2539accuracy is too low compared to its magnitude.25402541\fun{GEN}{trunc_safe}{GEN x}, \kbd{x} being a real number (not necessarily a2542\typ{REAL}) returns the integer with the largest absolute value, which is closer2543to $0$ than any possible incarnation of \kbd{x}. (Recall that a \typ{REAL}2544represents an interval of possible values.)25452546\fun{GEN}{roundr_safe}{GEN x} rounds the \typ{REAL} \kbd{x} to the nearest2547integer (towards~$+\infty$). Complement the mantissa with an infinite number2548of $0$ before rounding, hence never raise an exception.25492550\subsec{$2$-adic valuations and shifts}25512552\fun{long}{vals}{long s} 2-adic valuation of the \kbd{long}~\kbd{s}. Returns2553$-1$ if \kbd{s} is equal to 0.25542555\fun{long}{vali}{GEN x} 2-adic valuation of the \typ{INT}~\kbd{x}. Returns $-1$2556if \kbd{x} is equal to 0.25572558\fun{GEN}{mpshift}{GEN x, long n} shifts the~\typ{INT} or2559\typ{REAL} \kbd{x} by~\kbd{n}. If \kbd{n} is positive, this is a left shift,2560i.e.~multiplication by $2^{\kbd{n}}$. If \kbd{n} is negative, it is a right2561shift by~$-\kbd{n}$, which amounts to the truncation of the quotient of \kbd{x}2562by~$2^{-\kbd{n}}$.25632564\fun{GEN}{shifti}{GEN x, long n} shifts the \typ{INT}~$x$ by~$n$.25652566\fun{GEN}{shiftr}{GEN x, long n} shifts the \typ{REAL}~$x$ by~$n$.25672568\fun{void}{shiftr_inplace}{GEN x, long n} shifts the \typ{REAL}~$x$ by~$n$,2569in place.25702571\fun{GEN}{trunc2nr}{GEN x, long n} given a \typ{REAL} $x$, returns2572\kbd{truncr(shiftr(x,n))}, but faster, without leaving garbage on the stack2573and never raising a \emph{precision loss in truncation} error.2574Called by \tet{gtrunc2n}.25752576\fun{GEN}{mantissa2nr}{GEN x, long n} given a \typ{REAL} $x$, returns2577the mantissa of $x 2^n$ (disregards the exponent of $x$). Equivalent to2578\bprog2579trunc2nr(x, n-expo(x)+bit_prec(x)-1)2580@eprog25812582\fun{GEN}{mantissa_real}{GEN z, long *e} returns the mantissa $m$ of $z$, and2583sets \kbd{*e} to the exponent $\kbd{bit\_accuracy(lg(z))}-1-\kbd{expo}(z)$,2584so that $z = m / 2^e$.25852586\misctitle{Low-level} In the following two functions, $s$(ource) and $t$(arget)2587need not be valid \kbd{GEN}s (in practice, they usually point to some part of a2588\typ{REAL} mantissa): they are considered as arrays of words representing some2589mantissa, and we shift globally $s$ by $n > 0$ bits, storing the result in2590$t$. We assume that $m\leq M$ and only access $s[m], s[m+1],\ldots s[M]$2591(read) and likewise for $t$ (write); we may have $s = t$ but more general2592overlaps are not allowed. The word $f$ is concatenated to $s$ to supply extra2593bits.25942595\fun{void}{shift_left}{GEN t, GEN s, long m, long M, ulong f, ulong n}2596shifts the mantissa2597$$s[m], s[m+1],\ldots s[M], f$$2598left by $n$ bits.25992600\fun{void}{shift_right}{GEN t, GEN s, long m, long M, ulong f, ulong n}2601shifts the mantissa2602$$f, s[m], s[m+1],\ldots s[M]$$2603right by $n$ bits.26042605\subsec{From \typ{INT} to bits or digits in base $2^k$ and back}26062607\fun{GEN}{binary_zv}{GEN x} given a \typ{INT} $x$, return a \typ{VECSMALL} of2608bits, from most significant to least significant.26092610\fun{GEN}{binary_2k}{GEN x, long k} given a \typ{INT} $x$, and2611$k > 0$, return a \typ{VEC} of digits of $x$ in base $2^k$, as \typ{INT}s, from2612most significant to least significant.26132614\fun{GEN}{binary_2k_nv}{GEN x, long k} given a \typ{INT} $x$, and $0 < k <2615\tet{BITS_IN_LONG}$, return a \typ{VECSMALL} of digits of $x$ in base $2^k$, as2616\kbd{ulong}s, from most significant to least significant.26172618\fun{GEN}{bits_to_int}{GEN x, long l} given a vector $x$ of $l$ bits (as a2619\typ{VECSMALL} or even a pointer to a part of a larger vector, so not a2620proper \kbd{GEN}), return the integer $\sum_{i = 1}^l x[i] 2^{l-i}$, as a2621\typ{INT}.26222623\fun{ulong}{bits_to_u}{GEN v, long l} same as \tet{bits_to_int}, where2624$l < \tet{BITS_IN_LONG}$, so we can return an \kbd{ulong}.26252626\fun{GEN}{fromdigitsu}{GEN x, GEN B}2627given a \typ{VECSMALL} $x$ of length $l$ and a \typ{INT} $B$,2628return the integer $\sum_{i = 1}^l x[i] B^{i-1}$, as a \typ{INT},2629where the \kbd{x[i]} are seen as unsigned integers.26302631\fun{GEN}{fromdigits_2k}{GEN x, long k} converse of \tet{binary_2k};2632given a \typ{VEC} $x$ of length $l$ and a positive \kbd{long} $k$,2633where each $x[i]$ is a \typ{INT} with $0\leq x[i] < 2^k$, return the2634integer $\sum_{i = 1}^l x[i] 2^{k(l-i)}$, as a \typ{INT}.26352636\fun{GEN}{nv_fromdigits_2k}{GEN x, long k} as \tet{fromdigits_2k}, but2637with $x$ being a \typ{VECSMALL} and each $x[i]$ being a \kbd{ulong}2638with $0\leq x[i] < 2^{\min\{k,\tet{BITS_IN_LONG}\}}$. Here $k$ may be2639any positive \kbd{long}, and the $x[i]$ are regarded as $k$-bit2640integers by truncating or extending with zeroes.26412642\subsec{Integer valuation}2643For integers $x$ and $p$, such that $x\neq 0$ and $|p| > 1$, we define2644$v_p(x)$ to be the largest integer exponent $e$ such that $p^e$ divides $x$.2645If $p$ is prime, this is the ordinary valuation of $x$ at $p$.26462647\fun{long}{Z_pvalrem}{GEN x, GEN p, GEN *r} applied to \typ{INT}s2648$\kbd{x}\neq 0$ and~\kbd{p}, $|\kbd{p}| > 1$, returns $e := v_p(x)$2649The quotient $\kbd{x}/\kbd{p}^e$ is returned in~\kbd{*r}. If2650$|\kbd{p}|$ is a prime, \kbd{*r} is the prime-to-\kbd{p} part of~\kbd{x}.26512652\fun{long}{Z_pval}{GEN x, GEN p} as \kbd{Z\_pvalrem} but only returns2653$v_p(x)$.26542655\fun{long}{Z_lvalrem}{GEN x, ulong p, GEN *r} as \kbd{Z\_pvalrem},2656except that \kbd{p} is an \kbd{ulong} ($\kbd{p} > 1$).26572658\fun{long}{Z_lvalrem_stop}{GEN *x, ulong p, int *stop} assume $x > 0$;2659returns $e := v_p(x)$ and replaces $x$ by $x / p^e$. Set \kbd{stop} to $1$ if2660the new value of $x$ is $ < p^2$ (and $0$ otherwise). To be used when trial2661dividing $x$ by successive primes: the \kbd{stop} condition is cheaply tested2662while testing whether $p$ divides $x$ (is the quotient less than $p$?), and2663allows to decide that $n$ is prime if no prime $< p$ divides $n$. Not2664memory-clean.26652666\fun{long}{Z_lval}{GEN x, ulong p} as \kbd{Z\_pval},2667except that \kbd{p} is an \kbd{ulong} ($\kbd{p} > 1$).26682669\fun{long}{u_lvalrem}{ulong x, ulong p, ulong *r} as \kbd{Z\_pvalrem},2670except the inputs/outputs are now \kbd{ulong}s.26712672\fun{long}{u_lvalrem_stop}{ulong *n, ulong p, int *stop} as2673\kbd{Z\_pvalrem\_stop}.26742675\fun{long}{u_pvalrem}{ulong x, GEN p, ulong *r} as \kbd{Z\_pvalrem},2676except \kbd{x} and \kbd{r} are now \kbd{ulong}s.26772678\fun{long}{u_lval}{ulong x, ulong p} as \kbd{Z\_pval},2679except the inputs are now \kbd{ulong}s.26802681\fun{long}{u_pval}{ulong x, GEN p} as \kbd{Z\_pval},2682except \kbd{x} is now an \kbd{ulong}.26832684\fun{long}{z_lval}{long x, ulong p} as \kbd{u\_lval}, for signed \kbd{x}.26852686\fun{long}{z_lvalrem}{long x, ulong p} as \kbd{u\_lvalrem}, for signed \kbd{x}.26872688\fun{long}{z_pval}{long x, GEN p} as \kbd{Z\_pval},2689except \kbd{x} is now a \kbd{long}.26902691\fun{long}{z_pvalrem}{long x, GEN p} as \kbd{Z\_pvalrem},2692except \kbd{x} is now a \kbd{long}.26932694\fun{long}{factorial_lval}{ulong n, ulong p} returns $v_p(n!)$, assuming2695$p$ is prime.26962697The following convenience functions generalize \kbd{Z\_pval} and its variants2698to ``containers'' (\kbd{ZV} and \kbd{ZX}):269927002701\fun{long}{ZV_pvalrem}{GEN x, GEN p, GEN *r} $x$ being a \kbd{ZV} (a vector2702of \typ{INT}s), return the min $v$ of the valuations of its components and2703set \kbd{*r} to $x/p^v$. Infinite loop if $x$ is the zero vector.2704This function is not stack clean.27052706\fun{long}{ZV_pval}{GEN x, GEN p} as \kbd{ZV\_pvalrem} but only returns the2707``valuation''.27082709\fun{int}{ZV_Z_dvd}{GEN x, GEN p} returns $1$ if $p$ divides all components2710of $x$ and $0$ otherwise. Faster than testing \kbd{ZV\_pval(x,p) >= 1}.27112712\fun{long}{ZV_lvalrem}{GEN x, ulong p, GEN *px} as \kbd{ZV\_pvalrem},2713except that \kbd{p} is an \kbd{ulong} ($\kbd{p} > 1$).2714This function is not stack-clean.27152716\fun{long}{ZV_lval}{GEN x, ulong p} as \kbd{ZV\_pval},2717except that \kbd{p} is an \kbd{ulong} ($\kbd{p} > 1$).271827192720\fun{long}{ZX_pvalrem}{GEN x, GEN p, GEN *r} as \kbd{ZV\_pvalrem}, for2721a \kbd{ZX} $x$ (a \typ{POL} with \typ{INT} coefficients).2722This function is not stack-clean.27232724\fun{long}{ZX_pval}{GEN x, GEN p} as \kbd{ZV\_pval} for a \kbd{ZX} $x$.27252726\fun{long}{ZX_lvalrem}{GEN x, ulong p, GEN *px} as \kbd{ZV\_lvalrem},2727a \kbd{ZX} $x$.2728This function is not stack-clean.27292730\fun{long}{ZX_lval}{GEN x, ulong p} as \kbd{ZX\_pval},2731except that \kbd{p} is an \kbd{ulong} ($\kbd{p} > 1$).27322733\subsec{Generic unary operators} Let ``\op'' be a unary operation among27342735\item \key{neg}: negation ($-x$).27362737\item \key{abs}: absolute value ($|x|$).27382739\item \key{sqr}: square ($x^2$).27402741\noindent The names and prototypes of the low-level functions corresponding2742to \op\ are as follows. The result is of the same type as~\kbd{x}.27432744\funno{GEN}{\op i}{GEN x} creates the result of \op\ applied to the2745\typ{INT}~\kbd{x}.27462747\funno{GEN}{\op r}{GEN x} creates the result of \op\ applied to the2748\typ{REAL}~\kbd{x}.27492750\funno{GEN}{mp\op}{GEN x} creates the result of \op\ applied to the2751\typ{INT} or \typ{REAL}~\kbd{x}.27522753\noindent Complete list of available functions:27542755\fun{GEN}{absi}{GEN x}, \fun{GEN}{absr}{GEN x}, \fun{GEN}{mpabs}{GEN x}27562757\fun{GEN}{negi}{GEN x}, \fun{GEN}{negr}{GEN x}, \fun{GEN}{mpneg}{GEN x}27582759\fun{GEN}{sqri}{GEN x}, \fun{GEN}{sqrr}{GEN x}, \fun{GEN}{mpsqr}{GEN x}27602761\fun{GEN}{absi_shallow}{GEN x} $x$ being a \typ{INT}, returns a shallow copy of2762$|x|$, in particular returns $x$ itself when $x \geq 0$, and \kbd{negi($x$)}2763otherwise.27642765\fun{GEN}{mpabs_shallow}{GEN x} $x$ being a \typ{INT} or a \typ{REAL}, returns2766a shallow copy of $|x|$, in particular returns $x$ itself when $x \geq 0$, and2767\kbd{mpneg($x$)} otherwise.276827692770\noindent Some miscellaneous routines:27712772\fun{GEN}{sqrs}{long x} returns $x^2$.27732774\fun{GEN}{sqru}{ulong x} returns $x^2$.27752776\subsec{Comparison operators}27772778\fun{int}{cmpss}{long s, long t} compares the \kbd{long}~\kbd{s} to the2779\typ{long}~\kbd{t}.27802781\fun{int}{cmpuu}{ulong u, ulong v} compares the \kbd{ulong}~\kbd{u} to the2782\typ{ulong}~\kbd{v}.27832784\fun{long}{minss}{long x, long y}27852786\fun{ulong}{minuu}{ulong x, ulong y}27872788\fun{double}{mindd}{double x, double y} returns the \kbd{min} of $x$ and $y$.278927902791\fun{long}{maxss}{long x, long y}27922793\fun{ulong}{maxuu}{ulong x, ulong y}27942795\fun{double}{maxdd}{double x, double y} returns the \kbd{max} of $x$ and $y$.27962797\smallskip27982799\fun{int}{mpcmp}{GEN x, GEN y} compares the \typ{INT} or \typ{REAL}~\kbd{x}2800to the \typ{INT} or \typ{REAL}~\kbd{y}. The result is the sign of2801$\kbd{x}-\kbd{y}$.28022803\fun{int}{cmpii}{GEN x, GEN y} compares the \typ{INT} \kbd{x} to the2804\typ{INT}~\kbd{y}.28052806\fun{int}{cmpir}{GEN x, GEN y} compares the \typ{INT} \kbd{x} to the2807\typ{REAL}~\kbd{y}.28082809\fun{int}{cmpis}{GEN x, long s} compares the \typ{INT}~\kbd{x} to the2810\kbd{long}~\kbd{s}.28112812\fun{int}{cmpiu}{GEN x, ulong s} compares the \typ{INT}~\kbd{x} to the2813\kbd{ulong}~\kbd{s}.28142815\fun{int}{cmpsi}{long s, GEN x} compares the \kbd{long}~\kbd{s} to the2816\typ{INT}~\kbd{x}.28172818\fun{int}{cmpui}{ulong s, GEN x} compares the \kbd{ulong}~\kbd{s} to the2819\typ{INT}~\kbd{x}.28202821\fun{int}{cmpsr}{long s, GEN x} compares the \kbd{long}~\kbd{s} to the2822\typ{REAL}~\kbd{x}.28232824\fun{int}{cmpri}{GEN x, GEN y} compares the \typ{REAL}~\kbd{x} to the2825\typ{INT}~\kbd{y}.28262827\fun{int}{cmprr}{GEN x, GEN y} compares the \typ{REAL}~\kbd{x} to the2828\typ{REAL}~\kbd{y}.28292830\fun{int}{cmprs}{GEN x, long s} compares the \typ{REAL}~\kbd{x} to the2831\kbd{long}~\kbd{s}.28322833\fun{int}{equalii}{GEN x, GEN y} compares the \typ{INT}s \kbd{x} and~\kbd{y}.2834The result is $1$ if $\kbd{x} = \kbd{y}$, $0$ otherwise.28352836\fun{int}{equalrr}{GEN x, GEN y} compares the \typ{REAL}s \kbd{x} and~\kbd{y}.2837The result is $1$ if $\kbd{x} = \kbd{y}$, $0$ otherwise. Equality is decided2838according to the following rules: all real zeroes are equal, and2839different from a nonzero real; two nonzero reals are equal if all their2840digits coincide up to the length of the shortest of the two, and the2841remaining words in the mantissa of the longest are all $0$.28422843\fun{int}{equalis}{GEN x, long s} compare the \typ{INT} \kbd{x} and2844the \kbd{long}~\kbd{s}. The result is $1$ if $\kbd{x} = \kbd{y}$, $0$ otherwise.28452846\fun{int}{equalsi}{long s, GEN x}28472848\fun{int}{equaliu}{GEN x, ulong s} compare the \typ{INT} \kbd{x} and2849the \kbd{ulong}~\kbd{s}. The result is $1$ if $\kbd{x} = \kbd{y}$, $0$2850otherwise.28512852\fun{int}{equalui}{ulong s, GEN x}28532854The remaining comparison operators disregard the sign of their operands28552856\fun{int}{absequaliu}{GEN x, ulong u} compare the absolute value of the2857\typ{INT} \kbd{x} and the \kbd{ulong}~\kbd{s}. The result is $1$ if2858$|\kbd{x}| = \kbd{y}$, $0$ otherwise. This is marginally more efficient2859than \kbd{equalis} even when \kbd{x} is known to be nonnegative.28602861\fun{int}{absequalui}{ulong u, GEN x}28622863\fun{int}{abscmpiu}{GEN x, ulong u} compare the absolute value of the2864\typ{INT} \kbd{x} and the \kbd{ulong}~\kbd{u}.28652866\fun{int}{abscmpui}{ulong u, GEN x}286728682869\fun{int}{abscmpii}{GEN x, GEN y} compares the \typ{INT}s \kbd{x} and~\kbd{y}.2870The result is the sign of $|\kbd{x}| - |\kbd{y}|$.28712872\fun{int}{absequalii}{GEN x, GEN y} compares the \typ{INT}s \kbd{x}2873and~\kbd{y}. The result is $1$ if $|\kbd{x}| = |\kbd{y}|$, $0$ otherwise.28742875\fun{int}{abscmprr}{GEN x, GEN y} compares the \typ{REAL}s \kbd{x} and~\kbd{y}.2876The result is the sign of $|\kbd{x}| - |\kbd{y}|$.28772878\fun{int}{absrnz_equal2n}{GEN x} tests whether a nonzero \typ{REAL} \kbd{x}2879is equal to $\pm 2^e$ for some integer $e$.28802881\fun{int}{absrnz_equal1}{GEN x} tests whether a nonzero \typ{REAL} \kbd{x}2882is equal to $\pm 1$.28832884\subsec{Generic binary operators}\label{se:genbinop} The operators in this2885section have arguments of C-type \kbd{GEN}, \kbd{long}, and \kbd{ulong}, and2886only \typ{INT} and \typ{REAL} \kbd{GEN}s are allowed. We say an argument is a2887real type if it is a \typ{REAL} \kbd{GEN}, and an integer type otherwise. The2888result is always a \typ{REAL} unless both \kbd{x} and \kbd{y} are integer2889types.28902891Let ``\op'' be a binary operation among28922893\item \key{add}: addition (\kbd{x + y}).28942895\item \key{sub}: subtraction (\kbd{x - y}).28962897\item \key{mul}: multiplication (\kbd{x * y}).28982899\item \key{div}: division (\kbd{x / y}). In the case where \kbd{x} and \kbd{y}2900are both integer types, the result is the Euclidean quotient, where the2901remainder has the same sign as the dividend~\kbd{x}. It is the ordinary2902division otherwise. A division-by-$0$ error occurs if \kbd{y} is equal to2903$0$.29042905The last two generic operations are defined only when arguments have integer2906types; and the result is a \typ{INT}:29072908\item \key{rem}: remainder (``\kbd{x \% y}''). The result is the Euclidean2909remainder corresponding to \kbd{div},~i.e. its sign is that of the2910dividend~\kbd{x}.29112912\item \key{mod}: true remainder (\kbd{x \% y}). The result is the true2913Euclidean remainder, i.e.~nonnegative and less than the absolute value2914of~\kbd{y}.29152916\misctitle{Important technical note} The rules given above fixing the output2917type (to \typ{REAL} unless both inputs are integer types) are subtly2918incompatible with the general rules obeyed by PARI's generic functions, such2919as \kbd{gmul} or \kbd{gdiv} for instance: the latter return a result2920containing as much information as could be deduced from the inputs, so it is2921not true that if $x$ is a \typ{INT} and $y$ a \typ{REAL}, then2922\kbd{gmul(x,y)} is always the same as \kbd{mulir(x,y)}. The exception2923is $x = 0$, in that case we can deduce that the result is an exact $0$,2924so \kbd{gmul} returns \kbd{gen\_0}, while \kbd{mulir} returns a2925\typ{REAL} $0$. Specifically, the one resulting from the conversion of2926\kbd{gen\_0} to a \typ{REAL} of precision \kbd{precision(y)}, multiplied by2927$y$; this determines the exponent of the real $0$ we obtain.29282929The reason for the discrepancy between the two rules is that we use the two2930sets of functions in different contexts: generic functions allow to write2931high-level code forgetting about types, letting PARI return results which are2932sensible and as simple as possible; type specific functions are used in2933kernel programming, where we do care about types and need to maintain strict2934consistency: it is much easier to compute the types of results when they are2935determined from the types of the inputs only (without taking into account2936further arithmetic properties, like being nonzero).2937\smallskip29382939The names and prototypes of the low-level functions corresponding2940to \op\ are as follows. In this section, the \kbd{z} argument in the2941\kbd{z}-functions must be of type \typ{INT} when no \kbd{r} or \kbd{mp}2942appears in the argument code (no \typ{REAL} operand is involved, only integer2943types), and of type \typ{REAL} otherwise.29442945\funno{GEN}{mp\op[z]}{GEN x, GEN y[, GEN z]} applies \op\ to2946the \typ{INT} or \typ{REAL} \kbd{x} and~\kbd{y}. The function2947\kbd{mpdivz} does not exist (its semantic would change drastically2948depending on the type of the \kbd{z} argument), and neither do2949\kbd{mprem[z]} nor \kbd{mpmod[z]} (specific to integers).29502951\funno{GEN}{\op si[z]}{long s, GEN x[, GEN z]} applies \op\ to the2952\kbd{long}~\kbd{s} and the \typ{INT}~\kbd{x}.2953These functions always return the global constant2954\kbd{gen\_0} (not a copy) when the sign of the result is $0$.29552956\funno{GEN}{\op sr[z]}{long s, GEN x[, GEN z]} applies \op\ to the2957\kbd{long}~\kbd{s} and the \typ{REAL}~\kbd{x}.29582959\funno{GEN}{\op ss[z]}{long s, long t[, GEN z]} applies \op\ to the longs2960\kbd{s} and~\kbd{t}. These functions always return the global constant2961\kbd{gen\_0} (not a copy) when the sign of the result is $0$.29622963\funno{GEN}{\op ii[z]}{GEN x, GEN y[, GEN z]} applies \op\ to the2964\typ{INT}s \kbd{x} and~\kbd{y}. These functions always return the global2965constant \kbd{gen\_0} (not a copy) when the sign of the result is $0$.29662967\funno{GEN}{\op ir[z]}{GEN x, GEN y[, GEN z]} applies \op\ to the2968\typ{INT} \kbd{x} and the \typ{REAL}~\kbd{y}.29692970\funno{GEN}{\op is[z]}{GEN x, long s[, GEN z]} applies \op\ to the2971\typ{INT}~\kbd{x} and the \kbd{long}~\kbd{s}. These functions always return2972the global constant \kbd{gen\_0} (not a copy) when the sign of the result2973is $0$.29742975\funno{GEN}{\op ri[z]}{GEN x, GEN y[, GEN z]} applies \op\ to the2976\typ{REAL}~\kbd{x} and the \typ{INT}~\kbd{y}.29772978\funno{GEN}{\op rr[z]}{GEN x, GEN y[, GEN z]} applies \op\ to the2979\typ{REAL}s~\kbd{x} and~\kbd{y}.29802981\funno{GEN}{\op rs[z]}{GEN x, long s[, GEN z]} applies \op\ to the2982\typ{REAL}~\kbd{x} and the \kbd{long}~\kbd{s}.29832984\noindent Some miscellaneous routines:29852986\fun{long}{expu}{ulong x} assuming $x > 0$, returns the binary exponent of2987the real number equal to $x$. This is a special case of \kbd{gexpo}.29882989\fun{GEN}{adduu}{ulong x, ulong y}29902991\fun{GEN}{addiu}{GEN x, ulong y}29922993\fun{GEN}{addui}{ulong x, GEN y} adds \kbd{x} and \kbd{y}.29942995\fun{GEN}{subuu}{ulong x, ulong y}29962997\fun{GEN}{subiu}{GEN x, ulong y}29982999\fun{GEN}{subui}{ulong x, GEN y} subtracts \kbd{x} by \kbd{y}.30003001\fun{GEN}{muluu}{ulong x, ulong y} multiplies \kbd{x} by \kbd{y}.30023003\fun{ulong}{umuluu_le}{ulong x, ulong y, ulong n} multiplies \kbd{x} by \kbd{y}.3004Return $xy$ if $xy \leq n$ and $0$ otherwise (in particular if $xy$ does not3005fit in an \kbd{ulong}).30063007\fun{ulong}{umuluu_or_0}{ulong x, ulong y} multiplies \kbd{x} by \kbd{y}.3008Return $0$ if $xy$ does not fit in an \kbd{ulong}.30093010\fun{GEN}{mului}{ulong x, GEN y} multiplies \kbd{x} by \kbd{y}.30113012\fun{GEN}{muluui}{ulong x, ulong y, GEN z} return $xyz$.30133014\fun{GEN}{muliu}{GEN x, ulong y} multiplies \kbd{x} by \kbd{y}.30153016\fun{void}{addumului}{ulong a, ulong b, GEN x} return $a + b|X|$.30173018\fun{GEN}{addmuliu}{GEN x, GEN y, ulong u} returns $x +yu$.30193020\fun{GEN}{addmulii}{GEN x, GEN y, GEN z} returns $x + yz$.30213022\fun{GEN}{addmulii_inplace}{GEN x, GEN y, GEN z} returns $x + yz$, but3023returns $x$ itself and not a copy if $yz = 0$. Not suitable for3024\tet{gerepile} or \tet{gerepileupto}.30253026\fun{GEN}{addmuliu_inplace}{GEN x, GEN y, ulong u} returns $x +yu$, but3027returns $x$ itself and not a copy if $yu = 0$. Not suitable for3028\tet{gerepile} or \tet{gerepileupto}.30293030\fun{GEN}{submuliu_inplace}{GEN x, GEN y, ulong u} returns $x- yu$, but3031returns $x$ itself and not a copy if $yu = 0$. Not suitable for3032\tet{gerepile} or \tet{gerepileupto}.30333034\fun{GEN}{lincombii}{GEN u, GEN v, GEN x, GEN y} returns $ux + vy$.30353036\fun{GEN}{mulsubii}{GEN y, GEN z, GEN x} returns $yz - x$.30373038\fun{GEN}{submulii}{GEN x, GEN y, GEN z} returns $x - yz$.30393040\fun{GEN}{submuliu}{GEN x, GEN y, ulong u} returns $x -yu$.30413042\fun{GEN}{mulu_interval}{ulong a, ulong b} returns $a(a+1)\cdots b$, assuming3043that $a \leq b$.30443045\fun{GEN}{mulu_interval_step}{ulong a, ulong b, ulong s} returns the product3046of all integers in $[a,b]$ congruent to $a$ modulo $s$. Assume $a\leq b$ and3047$s > 0$;30483049\fun{GEN}{muls_interval}{long a, long b} returns $a(a+1)\cdots b$, assuming3050that $a \leq b$.30513052\fun{GEN}{invr}{GEN x} returns the inverse of the nonzero \typ{REAL}~$x$.30533054\fun{GEN}{truedivii}{GEN x, GEN y} returns the true Euclidean quotient3055(with nonnegative remainder less than $|y|$).30563057\fun{GEN}{truedivis}{GEN x, long y} returns the true Euclidean quotient3058(with nonnegative remainder less than $|y|$).30593060\fun{GEN}{truedivsi}{long x, GEN y} returns the true Euclidean quotient3061(with nonnegative remainder less than $|y|$).30623063\fun{GEN}{centermodii}{GEN x, GEN y, GEN y2}, given3064\typ{INT}s \kbd{x}, \kbd{y}, returns $z$ congruent to \kbd{x} modulo \kbd{y},3065such that $-\kbd{y}/2 \leq z < \kbd{y}/2$. The function requires an extra3066argument \kbd{y2}, such that \kbd{y2 = shifti(y, -1)}. (In most cases, \kbd{y}3067is constant for many reductions and \kbd{y2} need only be computed once.)30683069\fun{GEN}{remi2n}{GEN x, long n} returns \kbd{x} mod $2^n$.30703071\fun{GEN}{addii_sign}{GEN x, long sx, GEN y, long sy} add the \typ{INT}s3072$x$ and $y$ as if their signs were \kbd{sx} and \kbd{sy}.30733074\fun{GEN}{addir_sign}{GEN x, long sx, GEN y, long sy}3075add the \typ{INT} $x$ and the \typ{REAL} $y$ as if their signs were \kbd{sx}3076and \kbd{sy}.30773078\fun{GEN}{addrr_sign}{GEN x, long sx, GEN y, long sy} add the \typ{REAL}s $x$3079and $y$ as if their signs were \kbd{sx} and \kbd{sy}.30803081\fun{GEN}{addsi_sign}{long x, GEN y, long sy} add $x$ and the \typ{INT} $y$3082as if its sign was \kbd{sy}.30833084\fun{GEN}{addui_sign}{ulong x, GEN y, long sy} add $x$ and the \typ{INT} $y$3085as if its sign was \kbd{sy}.30863087\subsec{Exact division and divisibility}30883089\fun{GEN}{diviiexact}{GEN x, GEN y} returns the Euclidean quotient3090$\kbd{x} / \kbd{y}$, assuming $\kbd{y}$ divides $\kbd{x}$. Uses Jebelean3091algorithm (Jebelean-Krandick bidirectional exact division is not3092implemented).30933094\fun{GEN}{diviuexact}{GEN x, ulong y} returns the Euclidean quotient3095$\kbd{x} / \kbd{y}$, assuming $\kbd{y}$ divides3096$\kbd{x}$ and $\kbd{y}$ is nonzero.30973098\fun{GEN}{diviuuexact}{GEN x, ulong y, ulong z} returns the Euclidean3099quotient $x/(yz)$, assuming $yz$ divides $x$ and $yz \neq 0$.31003101The following routines return 1 (true) if \kbd{y} divides \kbd{x}, and31020 otherwise. All \kbd{GEN} are assumed to be \typ{INT}s:31033104\fun{int}{dvdii}{GEN x, GEN y},3105\fun{int}{dvdis}{GEN x, long y},3106\fun{int}{dvdiu}{GEN x, ulong y},31073108\fun{int}{dvdsi}{long x, GEN y},3109\fun{int}{dvdui}{ulong x, GEN y}.31103111The following routines return 1 (true) if \kbd{y} divides \kbd{x}, and in3112that case assign the quotient to \kbd{z}; otherwise they return 0. All3113\kbd{GEN} are assumed to be \typ{INT}s:31143115\fun{int}{dvdiiz}{GEN x, GEN y, GEN z},3116\fun{int}{dvdisz}{GEN x, long y, GEN z}.31173118\fun{int}{dvdiuz}{GEN x, ulong y, GEN z} if \kbd{y} divides \kbd{x}, assigns3119the quotient $|\kbd{x}|/\kbd{y}$ to \kbd{z} and returns 1 (true), otherwise3120returns 0 (false).31213122\subsec{Division with integral operands and \typ{REAL} result}31233124\fun{GEN}{rdivii}{GEN x, GEN y, long prec}, assuming $x$ and $y$3125are both of type \typ{INT}, return the quotient $x/y$ as a \typ{REAL} of3126precision \kbd{prec}.31273128\fun{GEN}{rdiviiz}{GEN x, GEN y, GEN z}, assuming $x$ and $y$3129are both of type \typ{INT}, and $z$ is a \typ{REAL},3130assign the quotient $x/y$ to $z$.31313132\fun{GEN}{rdivis}{GEN x, long y, long prec}, assuming \kbd{x}3133is of type \typ{INT}, return the quotient x/y as a \typ{REAL} of3134precision \kbd{prec}.31353136\fun{GEN}{rdivsi}{long x, GEN y, long prec}, assuming \kbd{y}3137is of type \typ{INT}, return the quotient x/y as a \typ{REAL} of3138precision \kbd{prec}.31393140\fun{GEN}{rdivss}{long x, long y, long prec}, return the quotient x/y as a3141\typ{REAL} of precision \kbd{prec}.314231433144\subsec{Division with remainder} The following functions return two objects,3145unless specifically asked for only one of them~--- a quotient and a remainder.3146The quotient is returned and the remainder is returned through the variable3147whose address is passed as the \kbd{r} argument. The term \emph{true3148Euclidean remainder} refers to the nonnegative one (\kbd{mod}), and3149\emph{Euclidean remainder} by itself to the one with the same sign as the3150dividend (\kbd{rem}). All \kbd{GEN}s, whether returned directly or through a3151pointer, are created on the stack.31523153\fun{GEN}{dvmdii}{GEN x, GEN y, GEN *r} returns the Euclidean quotient of the3154\typ{INT}~\kbd{x} by a \typ{INT}~\kbd{y} and puts the remainder3155into~\kbd{*r}. If \kbd{r} is equal to \kbd{NULL}, the remainder is not3156created, and if \kbd{r} is equal to \kbd{ONLY\_REM}, only the remainder is3157created and returned. In the generic case, the remainder is created after the3158quotient and can be disposed of individually with a \kbd{cgiv(r)}. The3159remainder is always of the sign of the dividend~\kbd{x}. If the remainder3160is $0$ set \kbd{r = gen\_0}.31613162\fun{void}{dvmdiiz}{GEN x, GEN y, GEN z, GEN t} assigns the Euclidean3163quotient of the \typ{INT}s \kbd{x} and \kbd{y} into the \typ{INT}~\kbd{z},3164and the Euclidean remainder into the \typ{INT}~\kbd{t}.31653166\noindent Analogous routines \tet{dvmdis}\kbd{[z]}, \tet{dvmdsi}\kbd{[z]},3167\tet{dvmdss}\kbd{[z]} are available, where \kbd{s} denotes a \kbd{long}3168argument. But the following routines are in general more flexible:31693170\fun{long}{sdivss_rem}{long s, long t, long *r} computes the Euclidean3171quotient and remainder of the longs \kbd{s} and~\kbd{t}. Puts the remainder3172into \kbd{*r}, and returns the quotient. The remainder is of the sign of the3173dividend~\kbd{s}, and has strictly smaller absolute value than~\kbd{t}.31743175\fun{long}{sdivsi_rem}{long s, GEN x, long *r} computes the Euclidean3176quotient and remainder of the \kbd{long}~\kbd{s} by the \typ{INT}~\kbd{x}. As3177\kbd{sdivss\_rem} otherwise.31783179\fun{long}{sdivsi}{long s, GEN x} as \kbd{sdivsi\_rem}, without3180remainder.31813182\fun{GEN}{divis_rem}{GEN x, long s, long *r} computes the Euclidean quotient3183and remainder of the \typ{INT}~\kbd{x} by the \kbd{long}~\kbd{s}. As3184\kbd{sdivss\_rem} otherwise.31853186\fun{GEN}{absdiviu_rem}{GEN x, ulong s, ulong *r} computes the Euclidean quotient3187and remainder of \emph{absolute value} of the \typ{INT}~\kbd{x} by the3188\kbd{ulong}~\kbd{s}. As \kbd{sdivss\_rem} otherwise.31893190\fun{ulong}{uabsdiviu_rem}{GEN n, ulong d, ulong *r} as \tet{absdiviu_rem}, assuming3191that $|n|/d$ fits into an \kbd{ulong}.31923193\fun{ulong}{uabsdivui_rem}{ulong x, GEN y, ulong *rem}3194computes the Euclidean quotient and remainder of $x$ by $|y|$. As3195\kbd{sdivss\_rem} otherwise.31963197\fun{ulong}{udivuu_rem}{ulong x, ulong y, ulong *rem}3198computes the Euclidean quotient and remainder of $x$ by $y$. As3199\kbd{sdivss\_rem} otherwise.32003201\fun{ulong}{ceildivuu}{ulong x, ulong y} return the ceiling of $x / y$.32023203\fun{GEN}{divsi_rem}{long s, GEN y, long *r} computes the Euclidean quotient3204and remainder of the \kbd{long}~\kbd{s} by the \kbd{GEN}~\kbd{y}. As3205\kbd{sdivss\_rem} otherwise.32063207\fun{GEN}{divss_rem}{long x, long y, long *r} computes the Euclidean quotient3208and remainder of the \kbd{long}~\kbd{x} by the \kbd{long}~\kbd{y}. As3209\kbd{sdivss\_rem} otherwise.3210\smallskip3211\fun{GEN}{truedvmdii}{GEN x, GEN y, GEN *r}, as \kbd{dvmdii} but with a3212nonnegative remainder.32133214\fun{GEN}{truedvmdis}{GEN x, long y, GEN *z}, as \kbd{dvmdis} but with a3215nonnegative remainder.32163217\fun{GEN}{truedvmdsi}{long x, GEN y, GEN *z}, as \kbd{dvmdsi} but with a3218nonnegative remainder.32193220\subsec{Modulo to longs} The following variants of \kbd{modii} do not3221clutter the stack:32223223\fun{long}{smodis}{GEN x, long y} computes the true Euclidean3224remainder of the \typ{INT}~\kbd{x} by the \kbd{long}~\kbd{y}. This is the3225nonnegative remainder, not the one whose sign is the sign of \kbd{x}3226as in the \kbd{div} functions.32273228\fun{long}{smodss}{long x, long y} computes the true Euclidean3229remainder of the \kbd{long}~\kbd{x} by a \kbd{long}~\kbd{y}.32303231\fun{ulong}{umodsu}{long x, ulong y} computes the true Euclidean3232remainder of the \kbd{long}~\kbd{x} by a \kbd{ulong}~\kbd{y}.32333234\fun{ulong}{umodiu}{GEN x, ulong y} computes the true Euclidean3235remainder of the \typ{INT}~\kbd{x} by the \kbd{ulong}~\kbd{y}.32363237\fun{ulong}{umodui}{ulong x, GEN y} computes the true Euclidean3238remainder of the \kbd{ulong}~\kbd{x} by the \typ{INT}~\kbd{|y|}.32393240The routine \tet{smodsi} does not exist, since it would not always be3241defined: for a \emph{negative} \kbd{x}, if the quotient is $\pm1$, the result3242\kbd{x + |y|} would in general not fit into a \kbd{long}. Use either3243\kbd{umodui} or \kbd{modsi}.32443245These functions directly access the binary data and are thus much faster than3246the generic modulo functions:32473248\fun{int}{mpodd}{GEN x} which is 1 if \kbd{x} is odd, and 0 otherwise.32493250\fun{ulong}{Mod2}{GEN x}32513252\fun{ulong}{Mod4}{GEN x}32533254\fun{ulong}{Mod8}{GEN x}32553256\fun{ulong}{Mod16}{GEN x}32573258\fun{ulong}{Mod32}{GEN x}32593260\fun{ulong}{Mod64}{GEN x} give the residue class of $x$ modulo the3261corresponding power of $2$.32623263\fun{ulong}{umodi2n}{GEN x, long n} give the residue class of $x$ modulo3264$2^n$, $0 \leq n < BITS\_IN\_LONG$.32653266The following functions assume that $x\neq 0$ and in fact disregard the3267sign of $x$. There are about $10\%$ faster than the safer variants above:32683269\fun{long}{mod2}{GEN x}32703271\fun{long}{mod4}{GEN x}32723273\fun{long}{mod8}{GEN x}32743275\fun{long}{mod16}{GEN x}32763277\fun{long}{mod32}{GEN x}32783279\fun{long}{mod64}{GEN x} give the residue class of $|x|$ modulo the3280corresponding power of 2, for \emph{nonzero}~\kbd{x}. As well,32813282\fun{ulong}{mod2BIL}{GEN x} returns the least significant word of $|x|$, still3283assuming that $x\neq 0$.32843285\subsec{Powering, Square root}32863287\fun{GEN}{powii}{GEN x, GEN n}, assumes $x$ and $n$ are \typ{INT}s and3288returns $x^n$.32893290\fun{GEN}{powuu}{ulong x, ulong n}, returns $x^n$.32913292\fun{GEN}{powiu}{GEN x, ulong n}, assumes $x$ is a \typ{INT} and returns $x^n$.32933294\fun{GEN}{powis}{GEN x, long n}, assumes $x$ is a \typ{INT} and returns $x^n$3295(possibly a \typ{FRAC} if $n < 0$).32963297\fun{GEN}{powrs}{GEN x, long n}, assumes $x$ is a \typ{REAL} and returns3298$x^n$. This is considered as a sequence of \kbd{mulrr}, possibly empty:3299as such the result has type \typ{REAL}, even if $n = 0$.3300Note that the generic function \kbd{gpowgs(x,0)} would return \kbd{gen\_1},3301see the technical note in \secref{se:genbinop}.33023303\fun{GEN}{powru}{GEN x, ulong n}, assumes $x$ is a \typ{REAL} and returns $x^n$3304(always a \typ{REAL}, even if $n = 0$).33053306\fun{GEN}{powersr}{GEN e, long n}. Given a \typ{REAL} $e$, return the vector3307$v$ of all $e^i$, $0 \leq i \leq n$, where $v[i] = e^{i-1}$.33083309\fun{GEN}{powrshalf}{GEN x, long n}, assumes $x$ is a \typ{REAL} and returns3310$x^{n/2}$ (always a \typ{REAL}, even if $n = 0$).33113312\fun{GEN}{powruhalf}{GEN x, ulong n}, assumes $x$ is a \typ{REAL} and returns3313$x^{n/2}$ (always a \typ{REAL}, even if $n = 0$).33143315\fun{GEN}{powrfrac}{GEN x, long n, long d}, assumes $x$ is a \typ{REAL} and3316returns $x^{n/d}$ (always a \typ{REAL}, even if $n = 0$).33173318\fun{GEN}{powIs}{long n} returns $I^n\in\{1,I,-1,-I\}$ (\typ{INT} for even $n$,3319\typ{COMPLEX} otherwise).33203321\fun{ulong}{upowuu}{ulong x, ulong n}, returns $x^n$ when $< 2^\B$, and $0$3322otherwise (overflow).33233324\fun{ulong}{upowers}{ulong x, long n}, returns $[1,x,\ldots,x^n]$ as a3325\typ{VECSMALL}. Assume there is no overflow.33263327\fun{GEN}{sqrtremi}{GEN N, GEN *r}, returns the integer square root $S$ of3328the nonnegative \typ{INT}~\kbd{N} (rounded towards 0) and puts the remainder3329$R$ into~\kbd{*r}. Precisely, $N = S^2 + R$ with $0\leq R \leq 2S$. If3330\kbd{r} is equal to \kbd{NULL}, the remainder is not created. In the generic3331case, the remainder is created after the quotient and can be disposed of3332individually with \kbd{cgiv(R)}. If the remainder is $0$ set \kbd{R = gen\_0}.33333334Uses a divide and conquer algorithm (discrete variant of Newton iteration)3335due to Paul Zimmermann (``Karatsuba Square Root'', INRIA Research Report 38053336(1999)).33373338\fun{GEN}{sqrti}{GEN N}, returns the integer square root $S$ of3339the nonnegative \typ{INT}~\kbd{N} (rounded towards 0). This is identical3340to \kbd{sqrtremi(N, NULL)}.33413342\fun{long}{logintall}{GEN B, GEN y, GEN *ptq}3343returns the floor $e$ of $\log_y B$, where $B > 0$ and $y > 1$ are integers.3344If \kbd{ptq} is not \kbd{NULL}, set it to $y^e$. (Analogous to \kbd{logint0},3345whithout sanity checks.)33463347\fun{ulong}{ulogintall}{ulong B, ulong y, ulong *ptq} as \kbd{logintall} for3348\kbd{ulong} arguments.33493350\fun{long}{logint}{GEN B, GEN y} returns the floor $e$ of $\log_y B$, where3351$B > 0$ and $y > 1$ are integers.33523353\fun{ulong}{ulogint}{ulong B, ulong y} as \kbd{logint} for3354\kbd{ulong} arguments.33553356\fun{GEN}{vecpowuu}{long N, ulong a} return the vector of $n^a$, $n = 1,3357\dots, N$. Not memory clean.33583359\fun{GEN}{vecpowug}{long N, GEN a, long prec} return the vector of $n^a$, $n3360= 1, \dots, N$, where the powers are computed at precision \kbd{prec}. Not3361memory clean.33623363\subsec{GCD, extended GCD and LCM}33643365\fun{long}{cgcd}{long x, long y} returns the GCD of \kbd{x} and \kbd{y}.33663367\fun{ulong}{ugcd}{ulong x, ulong y} returns the GCD of \kbd{x} and \kbd{y}.33683369\fun{ulong}{ugcdiu}{GEN x, ulong y} returns the GCD of \kbd{x} and \kbd{y}.33703371\fun{ulong}{ugcdui}{ulong x, GEN y} returns the GCD of \kbd{x} and \kbd{y}.33723373\fun{GEN}{coprimes_zv}{ulong N} return a \typ{VECSMALL} $T$ with $N$ entries3374such that $T[i] = 1$ iff $(i,N) = 1$ and $0$ otherwise.33753376\fun{long}{clcm}{long x, long y} returns the LCM of \kbd{x} and \kbd{y},3377provided it fits into a \kbd{long}. Silently overflows otherwise.33783379\fun{ulong}{ulcm}{ulong x, ulong y} returns the LCM of \kbd{x} and \kbd{y},3380provided it fits into an \kbd{ulong}. Silently overflows otherwise.33813382\fun{GEN}{gcdii}{GEN x, GEN y}, returns the GCD of the \typ{INT}s \kbd{x} and3383\kbd{y}.33843385\fun{GEN}{lcmii}{GEN x, GEN y}, returns the LCM of the \typ{INT}s \kbd{x} and3386\kbd{y}.33873388\fun{GEN}{bezout}{GEN a,GEN b, GEN *u,GEN *v}, returns the GCD $d$ of3389\typ{INT}s \kbd{a} and \kbd{b} and sets \kbd{u}, \kbd{v} to the Bezout3390coefficients such that $\kbd{au} + \kbd{bv} = d$.33913392\fun{long}{cbezout}{long a,long b, long *u,long *v}, returns the GCD3393$d$ of \kbd{a} and \kbd{b} and sets \kbd{u}, \kbd{v} to the Bezout coefficients3394such that $\kbd{au} + \kbd{bv} = d$.33953396\fun{GEN}{halfgcdii}{GEN x, GEN y}3397assuming \kbd{x} and \kbd{y} are \typ{INT}s,3398returns a $2$-components \typ{VEC} $[M,V]$ where $M$ is a $2\times 2$3399\typ{MAT} and $V$ a $2$-component \typ{COL}, both with \typ{INT} entries,3400such that $M*[x,y]~==V$ and such that if $V=[a,b]~$, then3401$a \geq \ceil{\sqrt{\max(|x|,|y|)}} > b$.34023403\fun{GEN}{ZV_extgcd}{GEN A} given a vector of $n$ integers $A$, returns $[d,3404U]$, where $d$ is the GCD of the $A[i]$ and $U$ is a matrix3405in $\text{GL}_n(\Z)$ such that $AU = [0,\dots,0,D]$.34063407\fun{GEN}{ZV_lcm}{GEN v} given a vector $v$ of integers3408returns the LCM of its entries.34093410\fun{GEN}{ZV_snf_gcd}{GEN v, GEN N} given a vector $v$ of integers and a3411positive integer $N$, return the vector whose entries are the gcds3412$(v[i],N)$. Use case: if $v$ gives the cyclic components for some Abelian3413group $G$ of finite type, then this returns the structure of the finite3414groupe $G/G^N$.34153416\subsec{Continued fractions and convergents}34173418\fun{GEN}{ZV_allpnqn}{GEN x} given $x = [a_0, ..., a_n]$ a3419continued fraction from \tet{gboundcf}, $n\geq0$, return all3420convergents as $[P,Q]$, where $P = [p_0,\dots,p_n]$ and $Q =3421[q_0,\dots,q_n]$.34223423\subsec{Pseudo-random integers}3424These routine return pseudo-random integers uniformly distributed in some3425interval. The all use the same underlying generator which can be seeded and3426restarted using \tet{getrand} and \tet{setrand}.34273428\fun{void}{setrand}{GEN seed} reseeds the random number generator using the3429seed $n$. The seed is either a technical array output by \kbd{getrand}3430or a small positive integer, used to generate deterministically a suitable3431state array. For instance, running a randomized computation starting by3432\kbd{setrand(1)} twice will generate the exact same output.34333434\fun{GEN}{getrand}{void} returns the current value of the seed used by the3435pseudo-random number generator \tet{random}. Useful mainly for debugging3436purposes, to reproduce a specific chain of computations. The returned value3437is technical (reproduces an internal state array of type \typ{VECSMALL}),3438and can only be used as an argument to \tet{setrand}.34393440\fun{ulong}{pari_rand}{void} returns a random $0 \leq x < 2^\B$.34413442\fun{long}{random_bits}{long k} returns a random $0 \leq x < 2^k$. Assumes3443that $0 \leq k \leq \B$.34443445\fun{ulong}{random_Fl}{ulong p} returns a pseudo-random integer3446in $0, 1, \dots p-1$.34473448\fun{GEN}{randomi}{GEN n} returns a random \typ{INT} between $0$ and $\kbd{n}3449- 1$.34503451\fun{GEN}{randomr}{long prec} returns a random \typ{REAL} in $[0,1[$, with3452precision \kbd{prec}.34533454\subsec{Modular operations} In this subsection, all \kbd{GEN}s are3455\typ{INT}.34563457\fun{GEN}{Fp_red}{GEN a, GEN m} returns \kbd{a} modulo \kbd{m} (smallest3458nonnegative residue). (This is identical to modii).34593460\fun{GEN}{Fp_neg}{GEN a, GEN m} returns $-$\kbd{a} modulo \kbd{m} (smallest3461nonnegative residue).34623463\fun{GEN}{Fp_add}{GEN a, GEN b, GEN m} returns the sum of \kbd{a} and3464\kbd{b} modulo \kbd{m} (smallest nonnegative residue).34653466\fun{GEN}{Fp_sub}{GEN a, GEN b, GEN m} returns the difference of \kbd{a} and3467\kbd{b} modulo \kbd{m} (smallest nonnegative residue).34683469\fun{GEN}{Fp_center}{GEN a, GEN p, GEN pov2} assuming that \kbd{pov2} is3470\kbd{shifti(p,-1)} and that $-p/2 < a < p$, returns the representative of3471\kbd{a} in the symmetric residue system $]-p/2,p/2]$.34723473\fun{GEN}{Fp_center_i}{GEN a, GEN p, GEN pov2} internal variant of3474\tet{Fp_center}, not \kbd{gerepile}-safe: when $a$ is already in the3475proper interval, it is returned as is, without a copy.34763477\fun{GEN}{Fp_mul}{GEN a, GEN b, GEN m} returns the product of \kbd{a} by3478\kbd{b} modulo \kbd{m} (smallest nonnegative residue).34793480\fun{GEN}{Fp_addmul}{GEN x, GEN y, GEN z, GEN p} returns $x + y\*z$.34813482\fun{GEN}{Fp_mulu}{GEN a, ulong b, GEN m} returns the product of \kbd{a} by3483\kbd{b} modulo \kbd{m} (smallest nonnegative residue).34843485\fun{GEN}{Fp_muls}{GEN a, long b, GEN m} returns the product of \kbd{a} by3486\kbd{b} modulo \kbd{m} (smallest nonnegative residue).34873488\fun{GEN}{Fp_halve}{GEN x, GEN m} returns $z$ such that $2\*z = x$ modulo3489$m$ assuming such $z$ exists.34903491\fun{GEN}{Fp_sqr}{GEN a, GEN m} returns $\kbd{a}^2$ modulo \kbd{m} (smallest3492nonnegative residue).34933494\fun{ulong}{Fp_powu}{GEN x, ulong n, GEN m} raises \kbd{x} to the \kbd{n}-th3495power modulo \kbd{m} (smallest nonnegative residue). Not memory-clean, but3496suitable for \kbd{gerepileupto}.34973498\fun{ulong}{Fp_pows}{GEN x, long n, GEN m} raises \kbd{x} to the \kbd{n}-th3499power modulo \kbd{m} (smallest nonnegative residue). A negative \kbd{n} is3500allowed Not memory-clean, but suitable for \kbd{gerepileupto}.35013502\fun{GEN}{Fp_pow}{GEN x, GEN n, GEN m} returns $\kbd{x}^\kbd{n}$3503modulo \kbd{m} (smallest nonnegative residue).35043505\fun{GEN}{Fp_pow_init}{GEN x, GEN n, long k, GEN p}3506Return a table \kbd{R} that can be used with3507\kbd{Fp\_pow\_table} to compute the powers of $x$ up to $n$.3508The table is of size $2^k\*\log_2(n)$.35093510\fun{GEN}{Fp_pow_table}{GEN R, GEN n, GEN p}3511return $x^n$, where $R$ is given by \kbd{Fp\_pow\_init(x,m,k,p)}3512for some integer $m\geq n$.35133514\fun{GEN}{Fp_powers}{GEN x, long n, GEN m} returns3515$[\kbd{x}^0, \dots, \kbd{x}^\kbd{n}]$ modulo \kbd{m} as a \typ{VEC}3516(smallest nonnegative residue).35173518\fun{GEN}{Fp_inv}{GEN a, GEN m} returns an inverse of \kbd{a} modulo \kbd{m}3519(smallest nonnegative residue). Raise an error if \kbd{a} is not invertible.35203521\fun{GEN}{Fp_invsafe}{GEN a, GEN m} as \kbd{Fp\_inv}, but return3522\kbd{NULL} if \kbd{a} is not invertible.35233524\fun{GEN}{Fp_invgen}{GEN x, GEN m, GEN *pg} set \kbd{*pg} to3525$g = \gcd(x,m)$ and return $u$ in $(\Z/m\Z)^*$ such that $x u = g$ modulo $m$.3526We have $g = 1$ if and only if $x$ is invertible, and in this case $u$3527is its inverse.35283529\fun{GEN}{FpV_prod}{GEN x, GEN p} returns the product of the components of3530$x$.35313532\fun{GEN}{FpV_inv}{GEN x, GEN m} $x$ being a vector of \typ{INT}s, return3533the vector of inverses of the $x[i]$ mod $m$. The routine uses Montgomery's3534trick, and involves a single inversion mod $m$, plus $3(N-1)$ multiplications3535for $N$ entries. The routine is not stack-clean: $2N$ integers mod $m$3536are left on stack, besides the $N$ in the result.35373538\fun{GEN}{Fp_div}{GEN a, GEN b, GEN m} returns the quotient of \kbd{a} by3539\kbd{b} modulo \kbd{m} (smallest nonnegative residue). Raise an error if3540\kbd{b} is not invertible.35413542\fun{GEN}{Fp_divu}{GEN a, ulong b, GEN m} returns the quotient of \kbd{a} by3543\kbd{b} modulo \kbd{m} (smallest nonnegative residue). Raise an error if3544\kbd{b} is not invertible.35453546\fun{int}{invmod}{GEN a, GEN m, GEN *g}, return $1$ if \kbd{a}3547modulo \kbd{m} is invertible, else return $0$ and set3548$\kbd{g} = \gcd(\kbd{a},\kbd{m})$.35493550In the following three functions the integer parameter \kbd{ord} can be given3551either as a positive \typ{INT} $N$, or as its factorization matrix $\var{faN}$,3552or as a pair $[N,\var{faN}]$. The parameter may be omitted by setting it to3553\kbd{NULL} (the value is then $p-1$).35543555\fun{GEN}{Fp_log}{GEN a, GEN g, GEN ord, GEN p} Let $g$ such that3556$g^{ord} \equiv 1 \pmod{p}$. Return an integer $e$ such that3557$a^e \equiv g \pmod{p}$. If $e$ does not exist, the result is undefined.35583559\fun{GEN}{Fp_order}{GEN a, GEN ord, GEN p} returns the order of the3560\kbd{Fp} \kbd{a}. Assume that \kbd{ord} is a multiple of the order of3561\kbd{a}.35623563\fun{GEN}{Fp_factored_order}{GEN a, GEN ord, GEN p} returns $[o,F]$, where $o$3564is the multiplicative order of the \kbd{Fp} $a$ in $\F_p^*$, and $F$ is the3565factorization of $o$. Assume that \kbd{ord} is a multiple of the order of3566\kbd{a}.35673568\fun{int}{Fp_issquare}{GEN x, GEN p} returns $1$ if \kbd{x} is a square3569modulo \kbd{p}, and $0$ otherwise.35703571\fun{int}{Fp_ispower}{GEN x, GEN n, GEN p} returns $1$ if \kbd{x} is an3572$n$-th power modulo \kbd{p}, and $0$ otherwise.35733574\fun{GEN}{Fp_sqrt}{GEN x, GEN p} returns a square root of \kbd{x} modulo3575\kbd{p} (the smallest nonnegative residue), where \kbd{x}, \kbd{p} are3576\typ{INT}s, and \kbd{p} is assumed to be prime. Return \kbd{NULL}3577if \kbd{x} is not a quadratic residue modulo \kbd{p}.35783579\fun{GEN}{Fp_2gener}{GEN p} return a generator of3580the $2$-Sylow subgroup of $\F_p^*$. To use with \kbd{Fp\_sqrt\_i}.35813582\fun{GEN}{Fp_sqrt_i}{GEN x, GEN s2, GEN p}3583as \kbd{Fp\_sqrt} where \kbd{s2} is the element returned by3584\kbd{Fp\_2gener}.35853586\fun{GEN}{Fp_sqrtn}{GEN a, GEN n, GEN p, GEN *zn}3587returns \kbd{NULL} if $a$ is not an $n$-th power residue mod $p$.3588Otherwise, returns an $n$-th root of $a$; if \kbd{zn} is not \kbd{NULL}3589set it to a primitive $m$-th root of 1, $m = \gcd(p-1,n)$ allowing to compute3590all $m$ solutions in $\F_p$ of the equation $x^n = a$.35913592\fun{GEN}{Zn_sqrt}{GEN x, GEN n} returns one of the square roots of \kbd{x}3593modulo \kbd{n} (possibly not prime), where \kbd{x} is a \typ{INT} and \kbd{n}3594is either a \typ{INT} or is given by its factorization matrix. Return3595\kbd{NULL} if no such square root exist.35963597\fun{GEN}{Zn_quad_roots}{GEN N, GEN B, GEN C} solves the equation $X^2 + B X3598+ C$ modulo $N$. Return \kbd{NULL} if there are no solutions. Else returns3599$[v, M]$ where $M \mid N$ and the \kbd{FpV} $v$ of distinct integers3600(reduced, implicitly modulo $M$) is such that $x$ modulo $N$ is a solution to3601the equation if and only if $x$ modulo $M$ belongs to $v$. If the3602discriminant $B^2-4C$ is coprime to $N$, we have $M = N$ but in general $M$3603can be a strict divisor of $N$.36043605\fun{long}{kross}{long x, long y} returns the \idx{Kronecker symbol} $(x|y)$,3606i.e.$-1$, $0$ or $1$. If \kbd{y} is an odd prime, this is the \idx{Legendre3607symbol}. (Contrary to \kbd{krouu}, \kbd{kross} also supports $\kbd{y} = 0$)36083609\fun{long}{krouu}{ulong x, ulong y} returns the \idx{Kronecker symbol}3610$(x|y)$, i.e.~$-1$, $0$ or $1$. Assumes \kbd{y} is nonzero. If \kbd{y} is an3611odd prime, this is the \idx{Legendre symbol}.36123613\fun{long}{krois}{GEN x, long y} returns the \idx{Kronecker symbol} $(x|y)$3614of \typ{INT}~x and \kbd{long}~\kbd{y}. As \kbd{kross} otherwise.36153616\fun{long}{kroiu}{GEN x, ulong y} returns the \idx{Kronecker symbol} $(x|y)$3617of \typ{INT}~x and nonzero \kbd{ulong}~\kbd{y}. As \kbd{krouu} otherwise.36183619\fun{long}{krosi}{long x, GEN y} returns the \idx{Kronecker symbol} $(x|y)$3620of \kbd{long}~x and \typ{INT}~\kbd{y}. As \kbd{kross} otherwise.36213622\fun{long}{kroui}{ulong x, GEN y} returns the \idx{Kronecker symbol} $(x|y)$3623of \kbd{long}~x and \typ{INT}~\kbd{y}. As \kbd{kross} otherwise.36243625\fun{long}{kronecker}{GEN x, GEN y} returns the \idx{Kronecker symbol} $(x|y)$3626of \typ{INT}s~x and~\kbd{y}. As \kbd{kross} otherwise.36273628\fun{GEN}{factorial_Fp}{long n, GEN p} return $n!$ mod $p$.36293630\fun{GEN}{pgener_Fp}{GEN p} returns the smallest primitive root modulo3631\kbd{p}, assuming \kbd{p} is prime.36323633\fun{GEN}{pgener_Zp}{GEN p} returns the smallest primitive root modulo $p^k$,3634$k > 1$, assuming \kbd{p} is an odd prime.36353636\fun{long}{Zp_issquare}{GEN x, GEN p} returns 1 if the \typ{INT} $x$ is3637a $p$-adic square, $0$ otherwise.36383639\fun{long}{Zn_issquare}{GEN x, GEN n} returns 1 if \typ{INT} $x$ is3640a square modulo \kbd{n} (possibly not prime), where $n$ is either a \typ{INT}3641or is given by its factorization matrix. Return $0$ otherwise.36423643\fun{long}{Zn_ispower}{GEN x, GEN n, GEN K, GEN *py} returns 1 if \typ{INT}3644$x$ is a $K$-th power modulo \kbd{n} (possibly not prime), where $n$ is3645either a \typ{INT} or is given by its factorization matrix. Return $0$3646otherwise. If \kbd{py} is not \kbd{NULL}, set it to $y$ such that $y^K = x$3647modulo $n$.36483649\fun{GEN}{pgener_Fp_local}{GEN p, GEN L}, \kbd{L} being a vector of3650primes dividing $p - 1$, returns the smallest integer $x > 1$ which is a3651generator of the $\ell$-Sylow of $\F_p^*$ for every $\ell$ in \kbd{L}. In3652other words, $x^{(p-1)/\ell} \neq 1$ for all such $\ell$. In particular,3653returns \kbd{pgener\_Fp(p)} if \kbd{L} contains all primes dividing $p - 1$.3654It is not necessary, and in fact slightly inefficient, to include $\ell=2$,3655since 2 is treated separately in any case, i.e. the generator obtained is3656never a square.36573658\fun{GEN}{rootsof1_Fp}{GEN n, GEN p} returns a primitive $n$-th root modulo3659the prime $p$.36603661\fun{GEN}{rootsof1u_Fp}{ulong n, GEN p} returns a primitive $n$-th root modulo3662the prime $p$.36633664\fun{ulong}{rootsof1_Fl}{ulong n, ulong p} returns a primitive $n$-th root3665modulo the prime $p$.36663667\subsec{Extending functions to vector inputs}36683669The following functions apply $f$ to the given arguments, recursively3670if they are of vector / matrix type:36713672\fun{GEN}{map_proto_G}{GEN (*f)(GEN), GEN x} For instance, if $x$ is a3673\typ{VEC}, return a \typ{VEC} whose components are the $f(x[i])$.36743675\fun{GEN}{map_proto_lG}{long (*f)(GEN), GEN x} As above, applying the3676function \kbd{stoi( f() )}.36773678\fun{GEN}{map_proto_GL}{GEN (*f)(GEN,long), GEN x, long y}36793680\fun{GEN}{map_proto_lGL}{long (*f)(GEN,long), GEN x, long y}36813682In the last function, $f$ implements an associative binary operator, which we3683extend naturally to an $n$-ary operator $f_n$ for any $n$: by convention,3684$f_0() = 1$, $f_1(x) = x$, and3685$$ f_n(x_1,\dots,x_n) = f( f_{n-1}(x_1,\dots,x_{n-1}), x_n)),$$3686for $n \geq 2$.36873688\fun{GEN}{gassoc_proto}{GEN (*f)(GEN,GEN),GEN x, GEN y} If $y$ is not3689\kbd{NULL}, return $f(x,y)$. Otherwise, $x$ must be of vector type, and we3690return the result of $f$ applied to its components, computed using a3691divide-and-conquer algorithm. More precisely, return3692$$f( f(x_1,\kbd{NULL}), f(x_2,\kbd{NULL}) ),$$3693where $x_1$, $x_2$ are the two halves of $x$.36943695\subsec{Miscellaneous arithmetic functions}36963697\fun{long}{bigomegau}{ulong n} returns the number of prime divisors of $n >36980$, counted with multiplicity.36993700\fun{ulong}{coreu}{ulong n}, unique squarefree integer $d$ dividing $n$ such3701that $n/d$ is a square.37023703\fun{ulong}{coreu_fact}{GEN fa} same, where \kbd{fa} is \kbd{factoru(n)}.37043705\fun{ulong}{corediscs}{long d, ulong *pt_f}, $d$ (possibly negative)3706being congruent to $0$ or $1$ modulo $4$, return the fundamental3707discriminant $D$ such that $d=D*f^2$ and set \kbd{*pt\_f} to $f$3708(if \kbd{*pt\_f} not \kbd{NULL}).37093710\fun{ulong}{eulerphiu}{ulong n}, Euler's totient function of $n$.37113712\fun{ulong}{eulerphiu_fact}{GEN fa} same, where \kbd{fa} is \kbd{factoru(n)}.37133714\fun{long}{moebiusu}{ulong n}, Moebius $\mu$-function of $n$.37153716\fun{long}{moebiusu_fact}{GEN fa} same, where \kbd{fa} is \kbd{factoru(n)}.37173718\fun{ulong}{radicalu}{ulong n}, product of primes dividing $n$.37193720\fun{GEN}{divisorsu}{ulong n}, returns the divisors of $n$ in a3721\typ{VECSMALL}, sorted by increasing order.37223723\fun{GEN}{divisorsu_fact}{GEN fa} same, where \kbd{fa} is \kbd{factoru(n)}.37243725\fun{GEN}{divisorsu_fact_factored}{GEN fa} where \kbd{fa} is \kbd{factoru(n)}.3726Return a vector $[D,F]$, where $D$ is a a \typ{VECSMALL} containing the3727divisors of $u$ and $F[i]$ contains \kbd{factoru}$(D[i])$.37283729\fun{GEN}{divisorsu_moebius}{GEN P} returns the divisors of $n$ of the form3730$\prod_{p\in S} (-p)$, $S\subset P$ in a \typ{VECSMALL}. The vector is not3731sorted but its first element is guaranteed to be $1$. If $P$ is3732\kbd{factoru(n)[1]}, this returns the set of $\mu(d) d$ where $d$ runs3733through the squarefree divisors of $n$.37343735\fun{long}{numdivu}{ulong n}, returns the number of positive divisors of $n>0$.37363737\fun{long}{numdivu_fact}{GEN fa} same, where \kbd{fa} is \kbd{factoru(n)}.37383739\fun{long}{omegau}{ulong n} returns the number of prime divisors of $n > 0$.37403741\fun{long}{uissquarefree}{ulong n} returns $1$ if \kbd{n}3742is square-free, and $0$ otherwise.37433744\fun{long}{uissquarefree_fact}{GEN fa} same, where \kbd{fa} is3745\kbd{factoru(n)}.37463747\fun{long}{uposisfundamental}{ulong x} return $1$ if $x$ is a fundamental3748discriminant, and $0$ otherwise.37493750\fun{long}{unegisfundamental}{ulong x} return $1$ if $-x$ is a fundamental3751discriminant, and $0$ otherwise.37523753\fun{long}{sisfundamental}{long x} return $1$ if $x$ is a fundamental3754discriminant, and $0$ otherwise.37553756\fun{int}{uis_357_power}{ulong x, ulong *pt, ulong *mask} as \tet{is_357_power}3757for \kbd{ulong} $x$.37583759\fun{int}{uis_357_powermod}{ulong x, ulong *mask} as \tet{uis_357_power}, but3760only check for 3rd, 5th or 7th powers modulo3761$211\times209\times61\times203\times117\times31\times43\times71$.37623763\fun{long}{uisprimepower}{ulong n, ulong *p} as \tet{isprimepower}, for3764\kbd{ulong} $n$.37653766\fun{int}{uislucaspsp}{ulong n} returns $1$ if the \kbd{ulong} $n$ fails Lucas3767compositeness test (it thus may be prime or composite), and $0$ otherwise3768(proving that $n$ is composite).37693770\fun{int}{uis2psp}{ulong n} returns $1$ if the odd \kbd{ulong} $n > 1$ fails a3771strong Rabin-Miller test for the base $2$ (it thus may be prime or3772composite), and $0$ otherwise (proving that $n$ is composite).37733774\fun{int}{uispsp}{ulong a, ulong n} returns $1$ if the odd \kbd{ulong} $n > 1$3775fails a strong Rabin-Miller test for the base $1 < a < n$ (it thus may be3776prime or composite), and $0$ otherwise (proving that $n$ is composite).37773778\fun{ulong}{sumdigitsu}{ulong n} returns the sum of decimal digits of $u$.37793780\fun{GEN}{usumdiv_fact}{GEN fa}, sum of divisors of \kbd{ulong} $n$, where3781\kbd{fa} is \kbd{factoru(n)}.37823783\fun{GEN}{usumdivk_fact}{GEN fa, ulong k}, sum of $k$-th powers of divisors3784of \kbd{ulong} $n$, where \kbd{fa} is \kbd{factoru(n)}.37853786\fun{GEN}{hilbertii}{GEN x, GEN y, GEN p}, returns the Hilbert symbol3787$(x,y)$ at the prime $p$ (\kbd{NULL} for the place at infinity); $x$ and $y$3788are \typ{INT}s.37893790\fun{GEN}{sumdedekind}{GEN h, GEN k} returns the Dedekind sum attached to3791the \typ{INT} $h$ and $k$, $k > 0$.37923793\fun{GEN}{sumdedekind_coprime}{GEN h, GEN k} as \kbd{sumdedekind}, except3794that $h$ and $k$ are assumed to be coprime \typ{INT}s.37953796\fun{GEN}{u_sumdedekind_coprime}{long h, long k}3797Let $k > 0$, $0 \leq h < k$, $(h,k) = 1$. Returns $[s_1,s_2]$3798in a \typ{VECSMALL}, such that $s(h,k) = (s_2 + k s_1) / (12k)$.3799Requires $\max(h + k/2, k) < \kbd{LONG\_MAX}$3800to avoid overflow, in particular $k \leq (2/3)\kbd{LONG\_MAX}$ is fine.38013802\newpage3803\chapter{Level 2 kernel}38043805These functions deal with modular arithmetic, linear algebra and polynomials3806where assumptions can be made about the types of the coefficients.38073808\section{Naming scheme}\label{se:level2names}3809A function name is built in the following way:3810$A_1\kbd{\_}\dots\kbd{\_}A_n\var{fun}$ for an operation \var{fun} with $n$3811arguments of class $A_1$,\dots, $A_n$. A class name is given by a base ring3812followed by a number of code letters. Base rings are among38133814\kbd{Fl}: $\Z/l\Z$ where $l < 2^{\B}$ is not necessarily prime. Implemented3815using \kbd{ulong}s38163817\kbd{Fp}: $\Z/p\Z$ where $p$ is a \typ{INT}, not necessarily prime.3818Implemented as \typ{INT}s $z$, preferably satisfying $0 \leq z < p$.3819More precisely, any \typ{INT} can be used as an \kbd{Fp}, but reduced3820inputs are treated more efficiently. Outputs from \kbd{Fp}xxx routines are3821reduced.38223823\kbd{Fq}: $\Z[X]/(p,T(X))$, $p$ a \typ{INT}, $T$ a \typ{POL} with \kbd{Fp}3824coefficients or \kbd{NULL} (in which case no reduction modulo \kbd{T} is3825performed). Implemented as \typ{POL}s $z$ with \kbd{Fp} coefficients,3826$\deg(z) < \deg \kbd{T}$, although $z$ a \typ{INT} is allowed for elements in3827the prime field.38283829\kbd{Z}: the integers $\Z$, implemented as \typ{INT}s.38303831\kbd{Zp}: the $p$-adic integers $\Z_p$, implemented as \typ{INT}s, for arbitrary $p$38323833\kbd{Zl}: the $p$-adic integers $\Z_p$, implemented as \typ{INT}s, for $p< 2^{\B}$38343835\kbd{z}: the integers $\Z$, implemented using (signed) \kbd{long}s.38363837\kbd{Q}: the rational numbers $\Q$, implemented as \typ{INT}s and3838\typ{FRAC}s.38393840\kbd{Rg}: a commutative ring, whose elements can be3841\kbd{gadd}-ed, \kbd{gmul}-ed, etc.38423843\noindent Possible letters are:38443845\kbd{X}: polynomial in $X$ (\typ{POL} in a fixed variable), e.g. \kbd{FpX}3846means $\Z/p\Z[X]$38473848\kbd{Y}: polynomial in $Y\neq X$. This is used to resolve ambiguities.3849E.g. \kbd{FpXY} means $((\Z/p\Z)[X])[Y]$.38503851\kbd{V}: vector (\typ{VEC} or \typ{COL}), treated as a line vector3852(independently of the actual type). E.g. \kbd{ZV} means $\Z^k$ for some $k$.38533854\kbd{C}: vector (\typ{VEC} or \typ{COL}), treated as a column vector3855(independently of the actual type). The difference with \kbd{V} is purely3856semantic: if the result is a vector, it will be of type \typ{COL} unless3857mentioned otherwise. For instance the function \kbd{ZC\_add} receives two3858integral vectors (\typ{COL} or \typ{VEC}, possibly different types) of the3859same length and returns a \typ{COL} whose entries are the sums of the input3860coefficients.38613862\kbd{M}: matrix (\typ{MAT}). E.g. \kbd{QM} means a matrix with rational3863entries38643865\kbd{T}: Trees. Either a leaf or a \typ{VEC} of trees.38663867\kbd{E}: point over an elliptic curve, represented3868as two-component vectors \kbd{[x,y]}, except for the represented by the3869one-component vector \kbd{[0]}. Not all curve models are supported.38703871\kbd{Q}: representative (\typ{POL}) of a class in a polynomial quotient ring.3872E.g.~an \kbd{FpXQ} belongs to $(\Z/p\Z)[X]/(T(X))$, \kbd{FpXQV} means a3873vector of such elements, etc.38743875\kbd{n}: a polynomial representative (\typ{POL}) for a truncated power3876series modulo $X^n$. E.g.~an \kbd{FpXn} belongs to $(\Z/p\Z)[X]/(X^n)$,3877\kbd{FpXnV} means a vector of such elements, etc.38783879\kbd{x}, \kbd{y}, \kbd{m}, \kbd{v}, \kbd{c}, \kbd{q}: as their uppercase3880counterpart, but coefficient arrays are implemented using \typ{VECSMALL}s,3881which coefficient understood as \kbd{ulong}s.38823883\kbd{x} and \kbd{y} (and \kbd{q}) are implemented by a \typ{VECSMALL} whose3884first coefficient is used as a code-word and the following are the3885coefficients , similarly to a \typ{POL}. This is known as a 'POLSMALL'.38863887\kbd{m} are implemented by a \typ{MAT} whose components (columns) are3888\typ{VECSMALL}s. This is known as a 'MATSMALL'.38893890\kbd{v} and \kbd{c} are regular \typ{VECSMALL}s. Difference between the3891two is purely semantic.38923893\noindent Omitting the letter means the argument is a scalar in the base3894ring. Standard functions \var{fun} are38953896\kbd{add}: add38973898\kbd{sub}: subtract38993900\kbd{mul}: multiply39013902\kbd{sqr}: square39033904\kbd{div}: divide (Euclidean quotient)39053906\kbd{rem}: Euclidean remainder39073908\kbd{divrem}: return Euclidean quotient, store remainder in a pointer3909argument. Three special values of that pointer argument modify the default3910behavior: \kbd{NULL} (do not store the remainder, used to implement3911\kbd{div}), \tet{ONLY_REM} (return the remainder, used to implement3912\kbd{rem}), \tet{ONLY_DIVIDES} (return the quotient if the division is exact,3913and \kbd{NULL} otherwise).39143915\kbd{gcd}: GCD39163917\kbd{extgcd}: return GCD, store Bezout coefficients in pointer arguments39183919\kbd{pow}: exponentiate39203921\kbd{eval}: evaluation / composition39223923\section{Coefficient ring}39243925\fun{long}{Rg_type}{GEN x, GEN *ptp, GEN *ptpol, long *ptprec} returns3926the ``natural'' base ring over which the object $x$ is defined.39273928Raise an error if it detects consistency problems in modular objects:3929incompatible rings (e.g. $\F_p$ and $\F_q$ for primes $p\neq q$,3930$\F_p[X]/(T)$ and $\F_p[X]/(U)$ for $T\neq U$). Minor discrepancies are3931supported if they make general sense (e.g. $\F_p$ and $\F_{p^k}$, but not3932$\F_p$ and $\Q_p$); \typ{FFELT} and \typ{POLMOD} of \typ{INTMOD}s are3933considered inconsistent, even if they define the same field: if you need to3934use simultaneously these different finite field implementations, multiply the3935polynomial by a \typ{FFELT} equal to $1$ first.39363937\item 0: none of the others (presumably multivariate, possibly inconsistent).39383939\item \typ{INT}: defined over $\Z$.39403941\item \typ{FRAC}: defined over $\Q$.39423943\item \typ{INTMOD}: defined over $\Z/p\Z$, where \kbd{*ptp} is set to $p$.3944It is not checked whether $p$ is prime.39453946\item \typ{COMPLEX}: defined over $\C$ (at least one \typ{COMPLEX} with at3947least one inexact floating point \typ{REAL} component). Set \kbd{*ptprec}3948to the minimal accuracy (as per \kbd{precision}) of inexact components.39493950\item \typ{REAL}: defined over $\R$ (at least one inexact floating point3951\typ{REAL} component). Set \kbd{*ptprec} to the minimal accuracy (as per3952\kbd{precision}) of inexact components.39533954\item \typ{PADIC}: defined over $\Q_p$, where \kbd{*ptp} is set to $p$ and3955\kbd{*ptprec} to the $p$-adic accuracy.39563957\item \typ{FFELT}: defined over a finite field $\F_{p^k}$, where \kbd{*ptp}3958is set to the field characteristic $p$ and \kbd{*ptpol} is set to a3959\typ{FFELT} belonging to the field.39603961\item \typ{POL}: defined over a polynomial ring.39623963\item other values are composite corresponding to quotients $R[X]/(T)$, with3964one primary type \kbd{t1}, describing the form of the quotient,3965and a secondary type \kbd{t2}, describing $R$. If \kbd{t} is the3966\kbd{RgX\_type}, \kbd{t1} and \kbd{t2} are recovered using39673968\fun{void}{RgX_type_decode}{long t, long *t1, long *t2}39693970\kbd{t1} is one of39713972\typ{POLMOD}: at least one \typ{POLMOD} component,3973set \kbd{*ppol} to the modulus,39743975\typ{QUAD}: no \typ{POLMOD}, at least one \typ{QUAD} component,3976set \kbd{*ppol} to the modulus (\kbd{$-$.pol}) of the \typ{QUAD},39773978\typ{COMPLEX}: no \typ{POLMOD} or \typ{QUAD}, at least one \typ{COMPLEX}3979component, set \kbd{*ppol} to $y^2 + 1$.39803981and the underlying base ring $R$ is given by \kbd{t2}, which3982is one of \typ{INT}, \typ{INTMOD} (set \kbd{*ptp}) or \typ{PADIC}3983(set \kbd{*ptp} and \kbd{*ptprec}), with the same meaning3984as above.39853986\fun{int}{RgX_type_is_composite}{long t} $t$ as returned by \kbd{RgX\_type},3987return 1 if $t$ is a composite type, and 0 otherwise.39883989\fun{GEN}{Rg_get_0}{GEN x} returns $0$ in the base ring over which $x$3990is defined, to the proper accuracy (e.g. \kbd{0}, \kbd{Mod(0,3)},3991\kbd{O(5\pow 10)}).39923993\fun{GEN}{Rg_get_1}{GEN x} returns $1$ in the base ring over which $x$3994is defined, to the proper accuracy (e.g. \kbd{0}, \kbd{Mod(0,3)},39953996\fun{long}{RgX_type}{GEN x, GEN *ptp, GEN *ptpol, long *ptprec} returns3997the ``natural'' base ring over which the polynomial $x$ is defined,3998otherwise as \kbd{Rg\_type}.39994000\fun{long}{RgX_Rg_type}{GEN x, GEN y, GEN *ptp, GEN *ptpol, long *ptprec}4001returns the ``natural'' base ring over which the polynomial $x$ and the element4002$y$ are defined, otherwise as \kbd{Rg\_type}.40034004\fun{long}{RgX_type2}{GEN x, GEN y, GEN *ptp, GEN *ptpol, long *ptprec} returns4005the ``natural'' base ring over which the polynomials $x$ and $y$ are defined,4006otherwise as \kbd{Rg\_type}.40074008\fun{long}{RgX_type3}{GEN x, GEN y, GNE z, GEN *ptp, GEN *ptpol, long *ptprec}4009returns the ``natural'' base ring over which the polynomials $x$, $y$ and $z$4010are defined, otherwise as \kbd{Rg\_type}.40114012\fun{long}{RgM_type}{GEN x, GEN *ptp, GEN *ptpol, long *ptprec} returns4013the ``natural'' base ring over which the matrix $x$ is defined,4014otherwise as \kbd{Rg\_type}.40154016\fun{long}{RgM_type2}{GEN x, GEN y, GEN *ptp, GEN *ptpol, long *ptprec} returns4017the ``natural'' base ring over which the matrices $x$ and $y$ are defined,4018otherwise as \kbd{Rg\_type}.40194020\fun{long}{RgV_type}{GEN x, GEN *ptp, GEN *ptpol, long *ptprec} returns4021the ``natural'' base ring over which the vector $x$ is defined,4022otherwise as \kbd{Rg\_type}.40234024\fun{long}{RgV_type2}{GEN x, GEN y, GEN *ptp, GEN *ptpol, long *ptprec} returns4025the ``natural'' base ring over which the vectors $x$ and $y$ are defined,4026otherwise as \kbd{Rg\_type}.40274028\fun{long}{RgM_RgC_type}{GEN x, GEN y, GEN *ptp, GEN *ptpol, long *ptprec}4029returns the ``natural'' base ring over which the matrix $x$ and the vector4030$y$ are defined, otherwise as \kbd{Rg\_type}.40314032\section{Modular arithmetic}40334034\noindent These routines implement univariate polynomial arithmetic and4035linear algebra over finite fields, in fact over finite rings of the form4036$(\Z/p\Z)[X]/(T)$, where $p$ is not necessarily prime and $T\in(\Z/p\Z)[X]$ is4037possibly reducible; and finite extensions thereof. All this can be emulated4038with \typ{INTMOD} and \typ{POLMOD} coefficients and using generic routines,4039at a considerable loss of efficiency. Also, specialized routines are4040available that have no obvious generic equivalent.40414042\subsec{\kbd{FpC} / \kbd{FpV}, \kbd{FpM}} A \kbd{ZV}4043(resp.~a~\kbd{ZM}) is a \typ{VEC} or \typ{COL} (resp.~\typ{MAT}) with4044\typ{INT} coefficients. An \kbd{FpV} or \kbd{FpM}, with respect to a given4045\typ{INT}~\kbd{p}, is the same with \kbd{Fp} coordinates; operations are4046understood over $\Z/p\Z$.40474048\subsubsec{Conversions}40494050\fun{int}{Rg_is_Fp}{GEN z, GEN *p}, checks if \kbd{z} can be mapped to4051$\Z/p\Z$: a \typ{INT} or a \typ{INTMOD} whose modulus is equal to \kbd{*p},4052(if \kbd{*p} not \kbd{NULL}), in that case return $1$, else $0$. If a modulus4053is found it is put in \kbd{*p}, else \kbd{*p} is left unchanged.40544055\fun{int}{RgV_is_FpV}{GEN z, GEN *p}, \kbd{z} a \typ{VEC} (resp. \typ{COL}),4056checks if it can be mapped to a \kbd{FpV} (resp. \kbd{FpC}), by checking4057\kbd{Rg\_is\_Fp} coefficientwise.40584059\fun{int}{RgM_is_FpM}{GEN z, GEN *p}, \kbd{z} a \typ{MAT},4060checks if it can be mapped to a \kbd{FpM}, by checking \kbd{RgV\_is\_FpV}4061columnwise.40624063\fun{GEN}{Rg_to_Fp}{GEN z, GEN p}, \kbd{z} a scalar which can be mapped to4064$\Z/p\Z$: a \typ{INT}, a \typ{INTMOD} whose modulus is divisible by $p$,4065a \typ{FRAC} whose denominator is coprime to $p$, or a \typ{PADIC} with4066underlying prime $\ell$ satisfying $p = \ell^n$ for some $n$ (less than the4067accuracy of the input). Returns \kbd{lift(z * Mod(1,p))}, normalized.40684069\fun{GEN}{padic_to_Fp}{GEN x, GEN p} special case of \tet{Rg_to_Fp},4070for a $x$ a \typ{PADIC}.40714072\fun{GEN}{RgV_to_FpV}{GEN z, GEN p}, \kbd{z} a \typ{VEC} or \typ{COL},4073returns the \kbd{FpV} (as a \typ{VEC}) obtained by applying \kbd{Rg\_to\_Fp}4074coefficientwise.40754076\fun{GEN}{RgC_to_FpC}{GEN z, GEN p}, \kbd{z} a \typ{VEC} or \typ{COL},4077returns the \kbd{FpC} (as a \typ{COL}) obtained by applying \kbd{Rg\_to\_Fp}4078coefficientwise.40794080\fun{GEN}{RgM_to_FpM}{GEN z, GEN p}, \kbd{z} a \typ{MAT},4081returns the \kbd{FpM} obtained by applying \kbd{RgC\_to\_FpC}4082columnwise.40834084\fun{GEN}{RgM_Fp_init}{GEN z, GEN p, ulong *pp}, given an \kbd{RgM} $z$,4085whose entries can be mapped to $\F_p$ (as per \tet{Rg_to_Fp}), and a prime4086number $p$. This routine returns a normal form of $z$: either an4087\kbd{F2m} ($p = 2$), an \kbd{Flm} ($p$ fits into an \kbd{ulong})4088or an \kbd{FpM}. In the first two cases, \kbd{pp} is set to \kbd{itou}$(p)$,4089and to $0$ in the last.40904091The functions above are generally used as follows:4092\bprog4093GEN add(GEN x, GEN y)4094{4095GEN p = NULL;4096if (Rg_is_Fp(x, &p) && Rg_is_Fp(y, &p) && p)4097{4098x = Rg_to_Fp(x, p); y = Rg_to_Fp(y, p);4099z = Fp_add(x, y, p);4100return Fp_to_mod(z);4101}4102else return gadd(x, y);4103}4104@eprog41054106\fun{GEN}{FpC_red}{GEN z, GEN p}, \kbd{z} a \kbd{ZC}. Returns \kbd{lift(Col(z) *4107Mod(1,p))}, hence a \typ{COL}.41084109\fun{GEN}{FpV_red}{GEN z, GEN p}, \kbd{z} a \kbd{ZV}. Returns \kbd{lift(Vec(z) *4110Mod(1,p))}, hence a \typ{VEC}41114112\fun{GEN}{FpM_red}{GEN z, GEN p}, \kbd{z} a \kbd{ZM}. Returns \kbd{lift(z *4113Mod(1,p))}, which is an \kbd{FpM}.41144115\subsubsec{Basic operations}41164117\fun{GEN}{random_FpC}{long n, GEN p} returns a random \kbd{FpC} with $n$4118components.41194120\fun{GEN}{random_FpV}{long n, GEN p} returns a random \kbd{FpV} with $n$4121components.41224123\fun{GEN}{FpC_center}{GEN z, GEN p, GEN pov2} returns a \typ{COL} whose4124entries are the \kbd{Fp\_center} of the \kbd{gel(z,i)}.41254126\fun{GEN}{FpM_center}{GEN z, GEN p, GEN pov2} returns a matrix whose4127entries are the \kbd{Fp\_center} of the \kbd{gcoeff(z,i,j)}.41284129\fun{void}{FpC_center_inplace}{GEN z, GEN p, GEN pov2}4130in-place version of \kbd{FpC\_center}, using \kbd{affii}.41314132\fun{void}{FpM_center_inplace}{GEN z, GEN p, GEN pov2}4133in-place version of \kbd{FpM\_center}, using \kbd{affii}.41344135\fun{GEN}{FpC_add}{GEN x, GEN y, GEN p} adds the \kbd{ZC} $x$ and $y$4136and reduce modulo $p$ to obtain an \kbd{FpC}.41374138\fun{GEN}{FpV_add}{GEN x, GEN y, GEN p} same as \kbd{FpC\_add}, returning and4139\kbd{FpV}.41404141\fun{GEN}{FpM_add}{GEN x, GEN y, GEN p} adds the two \kbd{ZM}s~\kbd{x}4142and \kbd{y} (assumed to have compatible dimensions), and reduce modulo4143\kbd{p} to obtain an \kbd{FpM}.41444145\fun{GEN}{FpC_sub}{GEN x, GEN y, GEN p} subtracts the \kbd{ZC} $y$ to4146the \kbd{ZC} $x$ and reduce modulo $p$ to obtain an \kbd{FpC}.41474148\fun{GEN}{FpV_sub}{GEN x, GEN y, GEN p} same as \kbd{FpC\_sub}, returning and4149\kbd{FpV}.41504151\fun{GEN}{FpM_sub}{GEN x, GEN y, GEN p} subtracts the two \kbd{ZM}s~\kbd{x}4152and \kbd{y} (assumed to have compatible dimensions), and reduce modulo4153\kbd{p} to obtain an \kbd{FpM}.41544155\fun{GEN}{FpC_Fp_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{ZC}~\kbd{x}4156(seen as a column vector) by the \typ{INT}~\kbd{y} and reduce modulo \kbd{p} to4157obtain an \kbd{FpC}.41584159\fun{GEN}{FpM_Fp_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{ZM}~\kbd{x}4160(seen as a column vector) by the \typ{INT}~\kbd{y} and reduce modulo \kbd{p} to4161obtain an \kbd{FpM}.41624163\fun{GEN}{FpC_FpV_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{ZC}~\kbd{x}4164(seen as a column vector) by the \kbd{ZV}~\kbd{y} (seen as a row vector,4165assumed to have compatible dimensions), and reduce modulo \kbd{p} to obtain4166an \kbd{FpM}.41674168\fun{GEN}{FpM_mul}{GEN x, GEN y, GEN p} multiplies the two \kbd{ZM}s~\kbd{x}4169and \kbd{y} (assumed to have compatible dimensions), and reduce modulo4170\kbd{p} to obtain an \kbd{FpM}.41714172\fun{GEN}{FpM_powu}{GEN x, ulong n, GEN p} computes $x^n$ where $x$ is a4173square \kbd{FpM}.41744175\fun{GEN}{FpM_FpC_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{ZM}~\kbd{x}4176by the \kbd{ZC}~\kbd{y} (seen as a column vector, assumed to have compatible4177dimensions), and reduce modulo \kbd{p} to obtain an \kbd{FpC}.41784179\fun{GEN}{FpM_FpC_mul_FpX}{GEN x, GEN y, GEN p, long v} is a memory-clean4180version of4181\bprog4182GEN tmp = FpM_FpC_mul(x,y,p);4183return RgV_to_RgX(tmp, v);4184@eprog41854186\fun{GEN}{FpV_FpC_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{ZV}~\kbd{x}4187(seen as a row vector) by the \kbd{ZC}~\kbd{y} (seen as a column vector,4188assumed to have compatible dimensions), and reduce modulo \kbd{p} to obtain4189an \kbd{Fp}.41904191\fun{GEN}{FpV_dotproduct}{GEN x,GEN y,GEN p} scalar product of4192$x$ and $y$ (assumed to have the same length).41934194\fun{GEN}{FpV_dotsquare}{GEN x, GEN p} scalar product of $x$ with itself.4195has \typ{INT} entries.41964197\fun{GEN}{FpV_factorback}{GEN L, GEN e, GEN p} given an \kbd{FpV} $L$4198and a \kbd{ZV} or \kbd{zv} $e$ of the same length, return $\prod_i L_i^{e_i}$4199modulo $p$.42004201\subsubsec{\kbd{Fp}-linear algebra} The implementations are not4202asymptotically efficient ($O(n^3)$ standard algorithms).42034204\fun{GEN}{FpM_deplin}{GEN x, GEN p} returns a nontrivial kernel vector,4205or \kbd{NULL} if none exist.42064207\fun{GEN}{FpM_det}{GEN x, GEN p} as \kbd{det}42084209\fun{GEN}{FpM_gauss}{GEN a, GEN b, GEN p} as \kbd{gauss}, where $a$ and4210$b$ are \kbd{FpM}.42114212\fun{GEN}{FpM_FpC_gauss}{GEN a, GEN b, GEN p} as \kbd{gauss}, where $a$4213is a \kbd{FpM} and $b$ a \kbd{FpC}.42144215\fun{GEN}{FpM_image}{GEN x, GEN p} as \kbd{image}42164217\fun{GEN}{FpM_intersect}{GEN x, GEN y, GEN p} as \kbd{intersect}42184219\fun{GEN}{FpM_intersect_i}{GEN x, GEN y, GEN p} internal variant of4220\kbd{FpM\_intersect} but the result is only a generating set, not4221necessarily an $\F_p$-basis. It is not \kbd{gerepile}-clean either, but4222suitable for \kbd{gerepileupto}.42234224\fun{GEN}{FpM_inv}{GEN x, GEN p} returns a left inverse of \kbd{x}4225(the inverse if $x$ is square), or \kbd{NULL} if \kbd{x} is not invertible.42264227\fun{GEN}{FpM_FpC_invimage}{GEN A, GEN y, GEN p}4228given an \kbd{FpM} $A$ and an \kbd{FpC} $y$, returns an $x$ such that $Ax =4229y$, or \kbd{NULL} if no such vector exist.42304231\fun{GEN}{FpM_invimage}{GEN A, GEN y, GEN p}4232given two \kbd{FpM} $A$ and $y$, returns $x$ such that $Ax = y$, or \kbd{NULL}4233if no such matrix exist.42344235\fun{GEN}{FpM_ker}{GEN x, GEN p} as \kbd{ker}42364237\fun{long}{FpM_rank}{GEN x, GEN p} as \kbd{rank}42384239\fun{GEN}{FpM_indexrank}{GEN x, GEN p} as \kbd{indexrank}42404241\fun{GEN}{FpM_suppl}{GEN x, GEN p} as \kbd{suppl}42424243\fun{GEN}{FpM_hess}{GEN x, GEN p} upper Hessenberg form of $x$ over $\F_p$.42444245\fun{GEN}{FpM_charpoly}{GEN x, GEN p} characteristic polynomial of $x$.42464247\subsubsec{\kbd{FqC}, \kbd{FqM} and \kbd{Fq}-linear algebra}42484249An \kbd{FqM} (resp. \kbd{FqC}) is a matrix (resp a \typ{COL}) with4250\kbd{Fq} coefficients (with respect to given \kbd{T}, \kbd{p}), not necessarily4251reduced (i.e arbitrary \typ{INT}s and \kbd{ZX}s in the same variable as4252\kbd{T}).42534254\fun{GEN}{RgC_to_FqC}{GEN z, GEN T, GEN p}42554256\fun{GEN}{RgM_to_FqM}{GEN z, GEN T, GEN p}42574258\fun{GEN}{FqC_add}{GEN a, GEN b, GEN T, GEN p}42594260\fun{GEN}{FqC_sub}{GEN a, GEN b, GEN T, GEN p}42614262\fun{GEN}{FqC_Fq_mul}{GEN a, GEN b, GEN T, GEN p}42634264\fun{GEN}{FqC_FqV_mul}{GEN a, GEN b, GEN T, GEN p}42654266\fun{GEN}{FqM_FqC_gauss}{GEN a, GEN b, GEN T, GEN p}4267as \kbd{gauss}, where $b$ is a \kbd{FqC}.42684269\fun{GEN}{FqM_FqC_invimage}{GEN a, GEN b, GEN T, GEN p}42704271\fun{GEN}{FqM_FqC_mul}{GEN a, GEN b, GEN T, GEN p}42724273\fun{GEN}{FqM_deplin}{GEN x, GEN T, GEN p} returns a nontrivial4274kernel vector, or \kbd{NULL} if none exist.42754276\fun{GEN}{FqM_det}{GEN x, GEN T, GEN p} as \kbd{det}42774278\fun{GEN}{FqM_gauss}{GEN a, GEN b, GEN T, GEN p}4279as \kbd{gauss}, where $b$ is a \kbd{FqM}.42804281\fun{GEN}{FqM_image}{GEN x, GEN T, GEN p} as \kbd{image}42824283\fun{GEN}{FqM_indexrank}{GEN x, GEN T, GEN p} as \kbd{indexrank}42844285\fun{GEN}{FqM_inv}{GEN x, GEN T, GEN p} returns the inverse of \kbd{x}, or4286\kbd{NULL} if \kbd{x} is not invertible.42874288\fun{GEN}{FqM_invimage}{GEN a, GEN b, GEN T, GEN p} as \kbd{invimage}42894290\fun{GEN}{FqM_ker}{GEN x, GEN T, GEN p} as \kbd{ker}42914292\fun{GEN}{FqM_mul}{GEN a, GEN b, GEN T, GEN p}42934294\fun{long}{FqM_rank}{GEN x, GEN T, GEN p} as \kbd{rank}42954296\fun{GEN}{FqM_suppl}{GEN x, GEN T, GEN p} as \kbd{suppl}429742984299\subsec{\kbd{Flc} / \kbd{Flv}, \kbd{Flm}} See \kbd{FpV}, \kbd{FpM}4300operations.43014302\fun{GEN}{Flv_copy}{GEN x} returns a copy of \kbd{x}.43034304\fun{GEN}{Flv_center}{GEN z, ulong p, ulong ps2}43054306\fun{GEN}{random_Flv}{long n, ulong p} returns a random \kbd{Flv} with $n$4307components.43084309\fun{GEN}{Flm_copy}{GEN x} returns a copy of \kbd{x}.43104311\fun{GEN}{matid_Flm}{long n} returns an \kbd{Flm} which is an $n \times n$4312identity matrix.43134314\fun{GEN}{scalar_Flm}{long s, long n} returns an \kbd{Flm} which is $s$ times4315the $n \times n$ identity matrix.43164317\fun{GEN}{Flm_center}{GEN z, ulong p, ulong ps2}43184319\fun{GEN}{Flm_Fl_add}{GEN x, ulong y, ulong p} returns $x + y*\text{Id}$4320($x$ must be square).43214322\fun{GEN}{Flm_Fl_sub}{GEN x, ulong y, ulong p} returns $x - y*\text{Id}$4323($x$ must be square).43244325\fun{GEN}{Flm_Flc_mul}{GEN x, GEN y, ulong p} multiplies \kbd{x} and \kbd{y}4326(assumed to have compatible dimensions).43274328\fun{GEN}{Flm_Flc_mul_pre}{GEN x, GEN y, ulong p, ulong pi} multiplies \kbd{x}4329and \kbd{y} (assumed to have compatible dimensions), assuming $pi$ is the4330pseudo inverse of $p$.43314332\fun{GEN}{Flc_Flv_mul}{GEN x, GEN y, ulong p} multiplies the column vector $x$4333by the row vector $y$. The result is a matrix.43344335\fun{GEN}{Flm_Flc_mul_pre_Flx}{GEN x, GEN y, ulong p, ulong pi, long sv}4336return \kbd{Flv\_to\_Flx(Flm\_Flc\_mul\_pre(x, y, p, pi), sv)}.43374338\fun{GEN}{Flm_Fl_mul}{GEN x, ulong y, ulong p} multiplies the \kbd{Flm}4339\kbd{x} by \kbd{y}.43404341\fun{GEN}{Flm_Fl_mul_pre}{GEN x, ulong y, ulong p, ulong pi} multiplies the4342\kbd{Flm} \kbd{x} by \kbd{y} assuming $pi$ is the pseudo inverse of $p$.43434344\fun{GEN}{Flm_neg}{GEN x, ulong p} negates the \kbd{Flm} \kbd{x}.43454346\fun{void}{Flm_Fl_mul_inplace}{GEN x, ulong y, ulong p} replaces4347the \kbd{Flm} \kbd{x} by $\kbd{x}*\kbd{y}$.43484349\fun{GEN}{Flv_Fl_mul}{GEN x, ulong y, ulong p} multiplies the \kbd{Flv}4350\kbd{x} by \kbd{y}.43514352\fun{void}{Flv_Fl_mul_inplace}{GEN x, ulong y, ulong p} replaces4353the \kbd{Flc} \kbd{x} by $\kbd{x}*\kbd{y}$.43544355\fun{void}{Flv_Fl_mul_part_inplace}{GEN x, ulong y, ulong p, long l}4356multiplies $x[1..l]$ by $y$ modulo $p$. In place.43574358\fun{GEN}{Flv_Fl_div}{GEN x, ulong y, ulong p} divides the \kbd{Flv}4359\kbd{x} by \kbd{y}.43604361\fun{void}{Flv_Fl_div_inplace}{GEN x, ulong y, ulong p} replaces4362the \kbd{Flv} \kbd{x} by $\kbd{x}/\kbd{y}$.43634364\fun{void}{Flc_lincomb1_inplace}{GEN X, GEN Y, ulong v, ulong q}4365sets $X\leftarrow X + vY$, where $X,Y$ are \kbd{Flc}. Memory efficient (e.g.4366no-op if $v = 0$), and gerepile-safe.43674368\fun{GEN}{Flv_add}{GEN x, GEN y, ulong p} adds two \kbd{Flv}.43694370\fun{void}{Flv_add_inplace}{GEN x, GEN y, ulong p} replaces4371$x$ by $x+y$.43724373\fun{GEN}{Flv_neg}{GEN x, ulong p} returns $-x$.43744375\fun{void}{Flv_neg_inplace}{GEN x, ulong p} replaces $x$ by $-x$.43764377\fun{GEN}{Flv_sub}{GEN x, GEN y, ulong p} subtracts \kbd{y} to \kbd{x}.43784379\fun{void}{Flv_sub_inplace}{GEN x, GEN y, ulong p} replaces $x$ by $x-y$.43804381\fun{ulong}{Flv_dotproduct}{GEN x, GEN y, ulong p} returns the scalar product4382of \kbd{x} and \kbd{y}43834384\fun{ulong}{Flv_dotproduct_pre}{GEN x, GEN y, ulong p, ulong pi} returns the4385scalar product of \kbd{x} and \kbd{y} assuming $pi$ is the pseudo inverse of4386$p$.43874388\fun{GEN}{Flv_factorback}{GEN L, GEN e, ulong p} given an \kbd{Flv} $L$4389and a \kbd{zv} $e$ of the same length, return $\prod_i L_i^{e_i}$ modulo $p$.43904391\fun{ulong}{Flv_sum}{GEN x, ulong p} returns the sum of the components of $x$.43924393\fun{ulong}{Flv_prod}{GEN x, ulong p} returns the product of the components of4394$x$.43954396\fun{ulong}{Flv_prod_pre}{GEN x, ulong p, ulong pi} as \kbd{Flv\_prod}4397assuming $pi$ is the pseudo inverse of $p$.43984399\fun{GEN}{Flv_inv}{GEN x, ulong p} returns the vector of inverses of the elements4400of $x$ (as a \kbd{Flv}). Use Montgomery trick.44014402\fun{void}{Flv_inv_inplace}{GEN x, ulong p} in place variant of \kbd{Flv\_inv}.44034404\fun{GEN}{Flv_inv_pre}{GEN x, ulong p, ulong pi} as \kbd{Flv\_inv}4405assuming $pi$ is the pseudo inverse of $p$.44064407\fun{void}{Flv_inv_pre_inplace}{GEN x, ulong p, ulong pi} in place variant of4408\kbd{Flv\_inv}.44094410\fun{GEN}{Flc_FpV_mul}{GEN x, GEN y, GEN p} multiplies $x$4411(seen as a column vector) by $y$ (seen as a row vector,4412assumed to have compatible dimensions) to obtain an \kbd{Flm}.44134414\fun{GEN}{zero_Flm}{long m, long n} creates a \kbd{Flm} with \kbd{m} x \kbd{n}4415components set to $0$. Note that the result allocates a4416\emph{single} column, so modifying an entry in one column modifies it in4417all columns.44184419\fun{GEN}{zero_Flm_copy}{long m, long n} creates a \kbd{Flm} with \kbd{m} x4420\kbd{n} components set to $0$.44214422\fun{GEN}{zero_Flv}{long n} creates a \kbd{Flv} with \kbd{n} components set to4423$0$.44244425\fun{GEN}{Flm_row}{GEN A, long x0} return $A[i,]$, the $i$-th row of the4426\kbd{Flm} $A$.44274428\fun{GEN}{Flm_add}{GEN x, GEN y, ulong p} adds \kbd{x} and \kbd{y}4429(assumed to have compatible dimensions).44304431\fun{GEN}{Flm_sub}{GEN x, GEN y, ulong p} subtracts \kbd{x} and \kbd{y}4432(assumed to have compatible dimensions).44334434\fun{GEN}{Flm_mul}{GEN x, GEN y, ulong p} multiplies \kbd{x} and \kbd{y}4435(assumed to have compatible dimensions).44364437\fun{GEN}{Flm_mul_pre}{GEN x, GEN y, ulong p, ulong pi} multiplies \kbd{x} and4438\kbd{y} (assumed to have compatible dimensions), assuming $pi$ is the pseudo4439inverse of $p$.44404441\fun{GEN}{Flm_powers}{GEN x, ulong n, ulong p} returns4442$[\kbd{x}^0, \dots, \kbd{x}^\kbd{n}]$ as a \typ{VEC} of \kbd{Flm}s.44434444\fun{GEN}{Flm_powu}{GEN x, ulong n, ulong p} computes $x^n$ where $x$ is a4445square \kbd{Flm}.44464447\fun{GEN}{Flm_charpoly}{GEN x, ulong p} return the characteristic polynomial of4448the square \kbd{Flm} $x$, as a \kbd{Flx}.44494450\fun{GEN}{Flm_deplin}{GEN x, ulong p}44514452\fun{ulong}{Flm_det}{GEN x, ulong p}44534454\fun{ulong}{Flm_det_sp}{GEN x, ulong p}, as \kbd{Flm\_det}, in place4455(destroys~\kbd{x}).44564457\fun{GEN}{Flm_gauss}{GEN a, GEN b, ulong p} as \kbd{gauss}, where $b$ is a4458\kbd{Flm}.44594460\fun{GEN}{Flm_Flc_gauss}{GEN a, GEN b, ulong p} as \kbd{gauss}, where $b$ is4461a \kbd{Flc}.44624463\fun{GEN}{Flm_indexrank}{GEN x, ulong p}44644465\fun{GEN}{Flm_inv}{GEN x, ulong p}44664467\fun{GEN}{Flm_adjoint}{GEN x, ulong p} as \kbd{matadjoint}.44684469\fun{GEN}{Flm_Flc_invimage}{GEN A, GEN y, ulong p} given an \kbd{Flm}4470$A$ and an \kbd{Flc} $y$, returns an $x$ such that $Ax = y$, or \kbd{NULL}4471if no such vector exist.44724473\fun{GEN}{Flm_invimage}{GEN A, GEN y, ulong p}4474given two \kbd{Flm} $A$ and $y$, returns $x$ such that $Ax = y$, or \kbd{NULL}4475if no such matrix exist.44764477\fun{GEN}{Flm_ker}{GEN x, ulong p}44784479\fun{GEN}{Flm_ker_sp}{GEN x, ulong p, long deplin}, as \kbd{Flm\_ker} (if4480\kbd{deplin=0}) or \kbd{Flm\_deplin} (if \kbd{deplin=1}) , in place4481(destroys~\kbd{x}).44824483\fun{long}{Flm_rank}{GEN x, ulong p}44844485\fun{long}{Flm_suppl}{GEN x, ulong p}44864487\fun{GEN}{Flm_image}{GEN x, ulong p}44884489\fun{GEN}{Flm_intersect}{GEN x, GEN y, ulong p}44904491\fun{GEN}{Flm_intersect_i}{GEN x, GEN y, GEN p} internal variant of4492\kbd{Flm\_intersect} but the result is only a generating set, not4493necessarily an $\F_p$-basis. It \emph{is} a basis if both $x$ and $y$4494have independent columns. It is not \kbd{gerepile}-clean either, but4495suitable for \kbd{gerepileupto}.44964497\fun{GEN}{Flm_transpose}{GEN x}44984499\fun{GEN}{Flm_hess}{GEN x, ulong p} upper Hessenberg form of $x$ over $\F_p$.45004501\subsec{\kbd{F2c} / \kbd{F2v}, \kbd{F2m}} An \kbd{F2v}~\kbd{v} is a4502\typ{VECSMALL} representing a vector over $\F_2$. Specifically \kbd{z[0]} is4503the usual codeword, \kbd{z[1]} is the number of components of $v$ and the4504coefficients are given by the bits of remaining words by increasing indices.45054506\fun{ulong}{F2v_coeff}{GEN x, long i} returns the coefficient $i\ge 1$ of $x$.45074508\fun{void}{F2v_clear}{GEN x, long i} sets the coefficient $i\ge 1$ of $x$ to4509$0$.45104511\fun{int}{F2v_equal0}{GEN x} returns $1$ if all entries are $0$, and return4512$0$ otherwise.45134514\fun{void}{F2v_flip}{GEN x, long i} adds $1$ to the coefficient $i\ge 1$ of $x$.45154516\fun{void}{F2v_set}{GEN x, long i} sets the coefficient $i\ge 1$ of $x$ to $1$.45174518\fun{void}{F2v_copy}{GEN x} returns a copy of $x$.45194520\fun{GEN}{F2v_slice}{GEN x, long a, long b} returns the \kbd{F2v} with4521entries $x[a]$, \dots, $x[b]$. Assumes $a \leq b$.45224523\fun{ulong}{F2m_coeff}{GEN x, long i, long j} returns the coefficient $(i,j)$4524of $x$.45254526\fun{void}{F2m_clear}{GEN x, long i, long j} sets the coefficient $(i,j)$ of $x$4527to $0$.45284529\fun{void}{F2m_flip}{GEN x, long i, long j} adds $1$ to the coefficient $(i,j)$4530of $x$.45314532\fun{void}{F2m_set}{GEN x, long i, long j} sets the coefficient $(i,j)$ of $x$4533to $1$.45344535\fun{GEN}{F2m_copy}{GEN x} returns a copy of $x$.45364537\fun{GEN}{F2m_transpose}{GEN x} returns the transpose of $x$.45384539\fun{GEN}{F2m_row}{GEN x, long j} returns the \kbd{F2v} which corresponds4540to the $j$-th row of the \kbd{F2m} $x$.45414542\fun{GEN}{F2m_rowslice}{GEN x, long a, long b} returns the \kbd{F2m} built4543from the $a$-th to $b$-th rows of the \kbd{F2m} $x$. Assumes $a \leq b$.45444545\fun{GEN}{F2m_F2c_mul}{GEN x, GEN y} multiplies \kbd{x} and \kbd{y} (assumed4546to have compatible dimensions).45474548\fun{GEN}{F2m_image}{GEN x} gives a subset of the columns of $x$ that generate4549the image of $x$.45504551\fun{GEN}{F2m_invimage}{GEN A, GEN B}45524553\fun{GEN}{F2m_F2c_invimage}{GEN A, GEN y}45544555\fun{GEN}{F2m_gauss}{GEN a, GEN b}4556as \kbd{gauss}, where $b$ is a \kbd{F2m}.45574558\fun{GEN}{F2m_F2c_gauss}{GEN a, GEN b}4559as \kbd{gauss}, where $b$ is a \kbd{F2c}.456045614562\fun{GEN}{F2m_indexrank}{GEN x} $x$ being a matrix of rank $r$, returns a4563vector with two \typ{VECSMALL} components $y$ and $z$ of length $r$ giving a4564list of rows and columns respectively (starting from 1) such that the extracted4565matrix obtained from these two vectors using \kbd{vecextract}$(x,y,z)$ is4566invertible.45674568\fun{GEN}{F2m_mul}{GEN x, GEN y} multiplies \kbd{x} and \kbd{y} (assumed to4569have compatible dimensions).45704571\fun{GEN}{F2m_powu}{GEN x, ulong n} computes $x^n$ where $x$ is a square4572\kbd{F2m}.45734574\fun{long}{F2m_rank}{GEN x} as \kbd{rank}.45754576\fun{long}{F2m_suppl}{GEN x} as \kbd{suppl}.45774578\fun{GEN}{matid_F2m}{long n} returns an \kbd{F2m} which is an $n \times n$4579identity matrix.45804581\fun{GEN}{zero_F2v}{long n} creates a \kbd{F2v} with \kbd{n} components set to4582$0$.45834584\fun{GEN}{const_F2v}{long n} creates a \kbd{F2v} with \kbd{n} components set to4585$1$.45864587\fun{GEN}{F2v_ei}{long n, long i} creates a \kbd{F2v} with \kbd{n} components4588set to $0$, but for the $i$-th one, which is set to $1$ ($i$-th vector in the4589canonical basis).45904591\fun{GEN}{zero_F2m}{long m, long n} creates a \kbd{Flm} with \kbd{m} x \kbd{n}4592components set to $0$. Note that the result allocates a4593\emph{single} column, so modifying an entry in one column modifies it in4594all columns.45954596\fun{GEN}{zero_F2m_copy}{long m, long n} creates a \kbd{F2m} with \kbd{m} x4597\kbd{n} components set to $0$.45984599\fun{GEN}{F2v_to_Flv}{GEN x}46004601\fun{GEN}{F2c_to_ZC}{GEN x}46024603\fun{GEN}{ZV_to_F2v}{GEN x}46044605\fun{GEN}{RgV_to_F2v}{GEN x}46064607\fun{GEN}{F2m_to_Flm}{GEN x}46084609\fun{GEN}{F2m_to_ZM}{GEN x}46104611\fun{GEN}{Flv_to_F2v}{GEN x}46124613\fun{GEN}{Flm_to_F2m}{GEN x}46144615\fun{GEN}{ZM_to_F2m}{GEN x}46164617\fun{GEN}{RgM_to_F2m}{GEN x}46184619\fun{void}{F2v_add_inplace}{GEN x, GEN y} replaces $x$ by $x+y$. It is4620allowed for $y$ to be shorter than $x$.46214622\fun{void}{F2v_and_inplace}{GEN x, GEN y} replaces $x$ by the term-by term product4623of $x$ and $y$ (which is the logical \kbd{and}). It is allowed for $y$ to be4624shorter than $x$.46254626\fun{void}{F2v_negimply_inplace}{GEN x, GEN y} replaces $x$ by the term-by term4627logical \kbd{and not} of $x$ and $y$. It is allowed for $y$ to be4628shorter than $x$.46294630\fun{void}{F2v_or_inplace}{GEN x, GEN y} replaces $x$ by the term-by term4631logical \kbd{or} of $x$ and $y$. It is allowed for $y$ to be4632shorter than $x$.46334634\fun{ulong}{F2v_hamming}{GEN x} returns the Hamming weight of \kbd{x}, that is4635the number of nonzero entries.46364637\fun{ulong}{F2m_det}{GEN x}46384639\fun{ulong}{F2m_det_sp}{GEN x}, as \kbd{F2m\_det}, in place (destroys~\kbd{x}).46404641\fun{GEN}{F2m_deplin}{GEN x}46424643\fun{ulong}{F2v_dotproduct}{GEN x, GEN y} returns the scalar product of \kbd{x}4644and \kbd{y}46454646\fun{GEN}{F2m_inv}{GEN x}46474648\fun{GEN}{F2m_ker}{GEN x}46494650\fun{GEN}{F2m_ker_sp}{GEN x, long deplin}, as \kbd{F2m\_ker} (if4651\kbd{deplin=0}) or \kbd{F2m\_deplin} (if \kbd{deplin=1}), in place4652(destroys~\kbd{x}).46534654\subsec{\kbd{F3c} / \kbd{F3v}, \kbd{F3m}} An \kbd{F3v}~\kbd{v} is a4655\typ{VECSMALL} representing a vector over $\F_3$. Specifically \kbd{z[0]} is4656the usual codeword, \kbd{z[1]} is the number of components of $v$ and the4657coefficients are given by pair of adjacent bits of remaining words by4658increasing indices, with the coding $00\mapsto 0, 01\mapsto 1, 10\mapsto 2$ and4659$11$ is undefined.46604661\fun{ulong}{F3v_coeff}{GEN x, long i} returns the coefficient $i\ge 1$ of $x$.46624663\fun{void}{F3v_clear}{GEN x, long i} sets the coefficient $i\ge 1$ of $x$ to4664$0$.46654666\fun{void}{F3v_set}{GEN x, long i, ulong n} sets the coefficient $i\ge 1$ of4667$x$ to $n<3$,46684669\fun{ulong}{F3m_coeff}{GEN x, long i, long j} returns the coefficient $(i,j)$4670of $x$.46714672\fun{void}{F3m_set}{GEN x, long i, long j, ulong n} sets the coefficient4673$(i,j)$ of $x$ to $n<3$.46744675\fun{GEN}{F3m_copy}{GEN x} returns a copy of $x$.46764677\fun{GEN}{F3m_transpose}{GEN x} returns the transpose of $x$.46784679\fun{GEN}{F3m_row}{GEN x, long j} returns the \kbd{F3v} which corresponds4680to the $j$-th row of the \kbd{F3m} $x$.46814682\fun{GEN}{F3m_ker}{GEN x}46834684\fun{GEN}{F3m_ker_sp}{GEN x, long deplin}, as \kbd{F3m\_ker} (if4685\kbd{deplin=0}) or \kbd{F2m\_deplin} (if \kbd{deplin=1}), in place4686(destroys~\kbd{x}).46874688\fun{GEN}{F3m_mul}{GEN x, GEN y} multiplies \kbd{x} and \kbd{y} (assumed to4689have compatible dimensions).46904691\fun{GEN}{zero_F3v}{long n} creates a \kbd{F3v} with \kbd{n} components set to4692$0$.46934694\fun{GEN}{zero_F2m_copy}{long m, long n} creates a \kbd{F3m} with \kbd{m} x4695\kbd{n} components set to $0$.46964697\fun{GEN}{F3v_to_Flv}{GEN x}46984699\fun{GEN}{ZV_to_F3v}{GEN x}47004701\fun{GEN}{RgV_to_F3v}{GEN x}47024703\fun{GEN}{F3m_to_Flm}{GEN x}47044705\fun{GEN}{F3m_to_ZM}{GEN x}47064707\fun{GEN}{Flv_to_F3v}{GEN x}47084709\fun{GEN}{Flm_to_F3m}{GEN x}47104711\fun{GEN}{ZM_to_F3m}{GEN x}47124713\fun{GEN}{RgM_to_F3m}{GEN x}47144715\subsec{\kbd{FlxqV}, \kbd{FlxqC}, \kbd{FlxqM}}4716See \kbd{FqV}, \kbd{FqC}, \kbd{FqM} operations.47174718\fun{GEN}{FlxqV_dotproduct}{GEN x, GEN y, GEN T, ulong p} as4719\kbd{FpV\_dotproduct}.47204721\fun{GEN}{FlxM_Flx_add_shallow}{GEN x, GEN y, ulong p} as4722\kbd{RgM\_Rg\_add\_shallow}.47234724\fun{GEN}{FlxqC_Flxq_mul}{GEN x, GEN y, GEN T, ulong p}47254726\fun{GEN}{FlxqM_Flxq_mul}{GEN x, GEN y, GEN T, ulong p}47274728\fun{GEN}{FlxqM_FlxqC_gauss}{GEN a, GEN b, GEN T, ulong p}47294730\fun{GEN}{FlxqM_FlxqC_invimage}{GEN a, GEN b, GEN T, ulong p}47314732\fun{GEN}{FlxqM_FlxqC_mul}{GEN a, GEN b, GEN T, ulong p}47334734\fun{GEN}{FlxqM_deplin}{GEN x, GEN T, ulong p}47354736\fun{GEN}{FlxqM_det}{GEN x, GEN T, ulong p}47374738\fun{GEN}{FlxqM_gauss}{GEN a, GEN b, GEN T, ulong p}47394740\fun{GEN}{FlxqM_image}{GEN x, GEN T, ulong p}47414742\fun{GEN}{FlxqM_indexrank}{GEN x, GEN T, ulong p}47434744\fun{GEN}{FlxqM_inv}{GEN x, GEN T, ulong p}47454746\fun{GEN}{FlxqM_invimage}{GEN a, GEN b, GEN T, ulong p}47474748\fun{GEN}{FlxqM_ker}{GEN x, GEN T, ulong p}47494750\fun{GEN}{FlxqM_mul}{GEN a, GEN b, GEN T, ulong p}47514752\fun{long}{FlxqM_rank}{GEN x, GEN T, ulong p}47534754\fun{GEN}{FlxqM_suppl}{GEN x, GEN T, ulong p}47554756\fun{GEN}{matid_FlxqM}{long n, GEN T, ulong p}47574758\subsec{\kbd{FpX}} Let \kbd{p} an understood \typ{INT}, to be given in4759the function arguments; in practice \kbd{p} is not assumed to be prime, but4760be wary. Recall than an \kbd{Fp} object is a \typ{INT}, preferably belonging4761to $[0, \kbd{p}-1]$; an \kbd{FpX} is a \typ{POL} in a fixed variable whose4762coefficients are \kbd{Fp} objects. Unless mentioned otherwise, all outputs in4763this section are \kbd{FpX}s. All operations are understood to take place in4764$(\Z/\kbd{p}\Z)[X]$.47654766\subsubsec{Conversions} In what follows \kbd{p} is always a \typ{INT},4767not necessarily prime.47684769\fun{int}{RgX_is_FpX}{GEN z, GEN *p}, \kbd{z} a \typ{POL},4770checks if it can be mapped to a \kbd{FpX}, by checking \kbd{Rg\_is\_Fp}4771coefficientwise.47724773\fun{GEN}{RgX_to_FpX}{GEN z, GEN p}, \kbd{z} a \typ{POL}, returns the4774\kbd{FpX} obtained by applying \kbd{Rg\_to\_Fp} coefficientwise.47754776\fun{GEN}{FpX_red}{GEN z, GEN p}, \kbd{z} a \kbd{ZX}, returns \kbd{lift(z *4777Mod(1,p))}, normalized.47784779\fun{GEN}{FpXV_red}{GEN z, GEN p}, \kbd{z} a \typ{VEC} of \kbd{ZX}. Applies4780\kbd{FpX\_red} componentwise and returns the result (and we obtain a vector4781of \kbd{FpX}s).47824783\fun{GEN}{FpXT_red}{GEN z, GEN p}, \kbd{z} a tree of \kbd{ZX}. Applies4784\kbd{FpX\_red} to each leaf and returns the result (and we obtain a tree4785of \kbd{FpX}s).47864787\subsubsec{Basic operations} In what follows \kbd{p} is always a \typ{INT},4788not necessarily prime.47894790\noindent Now, except for \kbd{p}, the operands and outputs are all \kbd{FpX}4791objects. Results are undefined on other inputs.47924793\fun{GEN}{FpX_add}{GEN x,GEN y, GEN p} adds \kbd{x} and \kbd{y}.47944795\fun{GEN}{FpX_neg}{GEN x,GEN p} returns $-\kbd{x}$, the components are4796between $0$ and $p$ if this is the case for the components of $x$.47974798\fun{GEN}{FpX_renormalize}{GEN x, long l}, as \kbd{normalizepol}, where4799$\kbd{l} = \kbd{lg(x)}$, in place.48004801\fun{GEN}{FpX_sub}{GEN x,GEN y,GEN p} returns $x-y$.48024803\fun{GEN}{FpX_halve}{GEN x, GEN p} returns $z$ such that $2\*z = x$ modulo4804$p$ assuming such $z$ exists.48054806\fun{GEN}{FpX_mul}{GEN x,GEN y,GEN p} returns $x\*y$.48074808\fun{GEN}{FpX_mulspec}{GEN a, GEN b, GEN p, long na, long nb}4809see \kbd{ZX\_mulspec}48104811\fun{GEN}{FpX_sqr}{GEN x,GEN p} returns $\kbd{x}^2$.48124813\fun{GEN}{FpX_powu}{GEN x, ulong n, GEN p} returns $x^n$.48144815\fun{GEN}{FpX_convol}{GEN x, GEN y, GEN p} return the-term by-term product of $x$4816and $y$.48174818\fun{GEN}{FpX_divrem}{GEN x, GEN y, GEN p, GEN *pr} returns the quotient4819of \kbd{x} by \kbd{y}, and sets \kbd{pr} to the remainder.48204821\fun{GEN}{FpX_div}{GEN x, GEN y, GEN p} returns the quotient of \kbd{x} by4822\kbd{y}.48234824\fun{GEN}{FpX_div_by_X_x}{GEN A, GEN a, GEN p, GEN *r} returns the4825quotient of the \kbd{FpX}~\kbd{A} by $(X - \kbd{a})$, and sets \kbd{r} to the4826remainder $\kbd{A}(\kbd{a})$.48274828\fun{GEN}{FpX_rem}{GEN x, GEN y, GEN p} returns the remainder \kbd{x} mod4829\kbd{y}.48304831\fun{long}{FpX_valrem}{GEN x, GEN t, GEN p, GEN *r} The arguments \kbd{x} and4832\kbd{e} being nonzero \kbd{FpX} returns the highest exponent $e$ such that4833$\kbd{t}^{e}$ divides~\kbd{x}. The quotient $\kbd{x}/\kbd{t}^{e}$ is returned4834in~\kbd{*r}. In particular, if \kbd{t} is irreducible, this returns the4835valuation at \kbd{t} of~\kbd{x}, and \kbd{*r} is the prime-to-\kbd{t} part4836of~\kbd{x}.48374838\fun{GEN}{FpX_deriv}{GEN x, GEN p} returns the derivative of \kbd{x}.4839This function is not memory-clean, but nevertheless suitable for4840\kbd{gerepileupto}.48414842\fun{GEN}{FpX_integ}{GEN x, GEN p} returns the primitive of \kbd{x} whose4843constant term is $0$.48444845\fun{GEN}{FpX_digits}{GEN x, GEN B, GEN p} returns a vector of \kbd{FpX}4846$[c_0,\ldots,c_n]$ of degree less than the degree of $B$ and such that4847$x=\sum_{i=0}^{n}{c_i\*B^i}$.48484849\fun{GEN}{FpXV_FpX_fromdigits}{GEN v, GEN B, GEN p} where $v=[c_0,\ldots,c_n]$4850is a vector of \kbd{FpX}, returns $\sum_{i=0}^{n}{c_i\*B^i}$.48514852\fun{GEN}{FpX_translate}{GEN P, GEN c, GEN p} let $c$ be an \kbd{Fp} and let4853$P$ be an \kbd{FpX}; returns the translated \kbd{FpX} of $P(X+c)$.48544855\fun{GEN}{FpX_gcd}{GEN x, GEN y, GEN p} returns a (not necessarily monic)4856greatest common divisor of $x$ and $y$.48574858\fun{GEN}{FpX_halfgcd}{GEN x, GEN y, GEN p} returns a two-by-two \kbd{FpXM}4859$M$ with determinant $\pm 1$ such that the image $(a,b)$ of $(x,y)$ by $M$4860has the property that $\deg a \geq {\deg x \over 2} > \deg b$.48614862\fun{GEN}{FpX_extgcd}{GEN x, GEN y, GEN p, GEN *u, GEN *v} returns4863$d = \text{GCD}(\kbd{x},\kbd{y})$ (not necessarily monic), and sets \kbd{*u},4864\kbd{*v} to the Bezout coefficients such that $\kbd{*ux} + \kbd{*vy} = d$.4865If \kbd{*u} is set to \kbd{NULL}, it is not computed which is a bit faster.4866This is useful when computing the inverse of $y$ modulo $x$.48674868\fun{GEN}{FpX_center}{GEN z, GEN p, GEN pov2} returns the polynomial whose4869coefficient belong to the symmetric residue system. Assumes the coefficients4870already belong to $]-p/2, p[$ and that \kbd{pov2} is \kbd{shifti(p,-1)}.48714872\fun{GEN}{FpX_center_i}{GEN z, GEN p, GEN pov2} internal variant of4873\tet{FpX_center}, not \kbd{gerepile}-safe.48744875\fun{GEN}{FpX_Frobenius}{GEN T, GEN p} returns $X^{p}\pmod{T(X)}$.48764877\fun{GEN}{FpX_matFrobenius}{GEN T, GEN p} returns the matrix of the4878Frobenius automorphism $x\mapsto x^p$ over the power basis of $\F_p[X]/(T)$.48794880\subsubsec{Mixed operations}4881The following functions implement arithmetic operations between \kbd{FpX}4882and \kbd{Fp} operands, the result being of type \kbd{FpX}. The integer4883\kbd{p} need not be prime.48844885\fun{GEN}{Z_to_FpX}{GEN x, GEN p, long v} converts a \typ{INT} to a scalar4886polynomial in variable $v$, reduced modulo $p$.48874888\fun{GEN}{FpX_Fp_add}{GEN y, GEN x, GEN p} add the \kbd{Fp}~\kbd{x} to the4889\kbd{FpX}~\kbd{y}.48904891\fun{GEN}{FpX_Fp_add_shallow}{GEN y, GEN x, GEN p} add the \kbd{Fp}~\kbd{x}4892to the \kbd{FpX}~\kbd{y}, using a shallow copy (result not suitable for4893\kbd{gerepileupto})48944895\fun{GEN}{FpX_Fp_sub}{GEN y, GEN x, GEN p} subtract the \kbd{Fp}~\kbd{x} from4896the \kbd{FpX}~\kbd{y}.48974898\fun{GEN}{FpX_Fp_sub_shallow}{GEN y, GEN x, GEN p} subtract the4899\kbd{Fp}~\kbd{x} from the \kbd{FpX}~\kbd{y}, using a shallow copy (result not4900suitable for \kbd{gerepileupto})49014902\fun{GEN}{Fp_FpX_sub}{GEN x,GEN y,GEN p} returns $x - y$, where $x$ is4903a \typ{INT} and $y$ an \kbd{FpX}.49044905\fun{GEN}{FpX_Fp_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{FpX}~\kbd{x}4906by the \kbd{Fp}~\kbd{y}.49074908\fun{GEN}{FpX_Fp_mulspec}{GEN x, GEN y, GEN p, long lx} see \kbd{ZX\_mulspec}49094910\fun{GEN}{FpX_mulu}{GEN x, ulong y, GEN p} multiplies the \kbd{FpX}~\kbd{x}4911by \kbd{y}.49124913\fun{GEN}{FpX_Fp_mul_to_monic}{GEN y,GEN x,GEN p} returns $y\*x$ assuming the4914result is monic of the same degree as $y$ (in particular $x\neq 0$).49154916\fun{GEN}{FpX_Fp_div}{GEN x, GEN y, GEN p} divides the \kbd{FpX}~\kbd{x}4917by the \kbd{Fp}~\kbd{y}.49184919\fun{GEN}{FpX_divu}{GEN x, ulong y, GEN p} divides the \kbd{FpX}~\kbd{x}4920by \kbd{y}.49214922\subsubsec{Miscellaneous operations}49234924\fun{GEN}{FpX_normalize}{GEN z, GEN p} divides the \kbd{FpX}~\kbd{z} by its4925leading coefficient. If the latter is~$1$, \kbd{z} itself is returned, not a4926copy. If not, the inverse remains uncollected on the stack.49274928\fun{GEN}{FpX_invBarrett}{GEN T, GEN p}, returns the Barrett inverse4929$M$ of $T$ defined by $M(x)\*x^n\*T(1/x)\equiv 1\pmod{x^{n-1}}$ where $n$ is4930the degree of $T$.49314932\fun{GEN}{FpX_rescale}{GEN P, GEN h, GEN p} returns $h^{\deg(P)} P(x/h)$.4933\kbd{P} is an \kbd{FpX} and \kbd{h} is a nonzero \kbd{Fp} (the routine would4934work with any nonzero \typ{INT} but is not efficient in this case).4935Neither memory-clean nor suitable for \kbd{gerepileupto}.49364937\fun{GEN}{FpX_eval}{GEN x, GEN y, GEN p} evaluates the \kbd{FpX}~\kbd{x}4938at the \kbd{Fp}~\kbd{y}. The result is an~\kbd{Fp}.49394940\fun{GEN}{FpX_FpV_multieval}{GEN P, GEN v, GEN p} returns the vector4941$[P(v[1]),\ldots,P(v[n])]$ as a \kbd{FpV}.49424943\fun{GEN}{FpX_dotproduct}{GEN x, GEN y, GEN p} return the scalar product4944$\sum_{i\geq 0} x_i\*y_i$ of the coefficients of $x$ and $y$.49454946\fun{GEN}{FpXV_FpC_mul}{GEN V, GEN W, GEN p} multiplies a nonempty line4947vector of\kbd{FpX} by a column vector of \kbd{Fp} of compatible dimensions.4948The result is an~\kbd{FpX}.49494950\fun{GEN}{FpXV_prod}{GEN V, GEN p}, \kbd{V} being a vector of \kbd{FpX},4951returns their product.49524953\fun{GEN}{FpXV_factorback}{GEN L, GEN e, GEN p, long v} returns4954$\prod_i L_i^{e_i}$ where $L$ is a vector of \kbd{FpX}s in the variable \kbd{v}4955and $e$ a vector of non-negative \typ{INT}s or a \typ{VECSMALL}.49564957\fun{GEN}{FpV_roots_to_pol}{GEN V, GEN p, long v}, \kbd{V} being a vector4958of \kbd{INT}s, returns the monic \kbd{FpX}4959$\prod_i (\kbd{pol\_x[v]} - \kbd{V[i]})$.49604961\fun{GEN}{FpX_chinese_coprime}{GEN x, GEN y, GEN Tx, GEN Ty, GEN Tz, GEN p}:4962returns an \kbd{FpX}, congruent to \kbd{x} mod \kbd{Tx} and to \kbd{y} mod4963\kbd{Ty}. Assumes \kbd{Tx} and \kbd{Ty} are coprime, and \kbd{Tz = Tx * Ty}4964or \kbd{NULL} (in which case it is computed within).49654966\fun{GEN}{FpV_polint}{GEN x, GEN y, GEN p, long v} returns the \kbd{FpX}4967interpolation polynomial with value \kbd{y[i]} at \kbd{x[i]}. Assumes lengths4968are the same, components are \typ{INT}s, and the \kbd{x[i]} are distinct4969modulo \kbd{p}.49704971\fun{GEN}{FpV_FpM_polint}{GEN x, GEN V, GEN p, long v} equivalent (but4972faster) to applying \kbd{FpV\_polint(x,$\ldots$)} to all the elements of the4973vector $V$ (thus, returns a \kbd{FpXV}).49744975\fun{GEN}{FpX_FpXV_multirem}{GEN A, GEN P, GEN p}4976given a \kbd{FpX} $A$ and a vector $P$ of pairwise coprime \kbd{FpX} of length4977$n\ge 1$, return a vector $B$ of the same length such that $B[i] =4978A\pmod{P[i]}$ and $B[i]$ of minimal degree for all $1\leq i\leq n$.49794980\fun{GEN}{FpXV_chinese}{GEN A, GEN P, GEN p, GEN *pM}4981let $P$ be a vector of pairwise coprime \kbd{FpX}, let $A$ be a vector of4982\kbd{FpX} of the same length $n\ge 1$ and let $M$ be the product of the4983elements of $P$. Returns a \kbd{FpX} of minimal degree congruent to $A[i]$ mod4984$P[i]$ for all $1\leq i\leq n$.4985If \kbd{pM} is not \kbd{NULL}, set \kbd{*pM} to $M$.49864987\fun{GEN}{FpV_invVandermonde}{GEN L, GEN d, GEN p} $L$ being a \kbd{FpV}4988of length $n$, return the inverse $M$ of the Vandermonde matrix attached to4989the elements of $L$, eventually multiplied by \kbd{d} if it is not4990\kbd{NULL}. If $A$ is a \kbd{FpV} and $B=M\*A$, then the polynomial4991$P=\sum_{i=1}^n B[i]\*X^{i-1}$ verifies $P(L[i])=d\*A[i]$ for4992$1 \leq i \leq n$.49934994\fun{int}{FpX_is_squarefree}{GEN f, GEN p} returns $1$ if the4995\kbd{FpX}~\kbd{f} is squarefree, $0$ otherwise.49964997\fun{int}{FpX_is_irred}{GEN f, GEN p} returns $1$ if the \kbd{FpX}~\kbd{f}4998is irreducible, $0$ otherwise. Assumes that \kbd{p} is prime. If~\kbd{f} has4999few factors, \kbd{FpX\_nbfact(f,p) == 1} is much faster.50005001\fun{int}{FpX_is_totally_split}{GEN f, GEN p} returns $1$ if the5002\kbd{FpX}~\kbd{f} splits into a product of distinct linear factors, $0$5003otherwise. Assumes that \kbd{p} is prime. The $0$ polynomial is not5004totally split.50055006\fun{long}{FpX_ispower}{GEN f, ulong k, GEN p, GEN *pt}5007return $1$ if the \kbd{FpX} $f$ is a $k$-th power, $0$ otherwise.5008If \kbd{pt} is not \kbd{NULL}, set it to $g$ such that $g^k = f$.50095010\fun{GEN}{FpX_factor}{GEN f, GEN p}, factors the \kbd{FpX}~\kbd{f}. Assumes5011that \kbd{p} is prime. The returned value \kbd{v} is a \typ{VEC} with two5012components: \kbd{v[1]} is a vector of distinct irreducible (\kbd{FpX})5013factors, and \kbd{v[2]} is a \typ{VECSMALL} of corresponding exponents. The5014order of the factors is deterministic (the computation is not).50155016\fun{GEN}{FpX_factor_squarefree}{GEN f, GEN p} returns the squarefree5017factorization of $f$ modulo $p$. This is a vector $[u_1,\dots,u_k]$5018of squarefree and pairwise coprime \kbd{FpX} such that $u_k \neq 1$ and $f =5019\prod u_i^i$. The other~$u_i$ may equal~$1$.5020Shallow function.50215022\fun{GEN}{FpX_ddf}{GEN f, GEN p} assuming that $f$ is squarefree,5023returns the distinct degree factorization of $f$ modulo $p$.5024The returned value \kbd{v} is a \typ{VEC} with two5025components: \kbd{F=v[1]} is a vector of (\kbd{FpX})5026factors, and \kbd{E=v[2]} is a \typ{VECSMALL}, such that5027$f$ is equal to the product of the \kbd{F[i]} and each \kbd{F[i]}5028is a product of irreducible factors of degree \kbd{E[i]}.50295030\fun{long}{FpX_ddf_degree}{GEN f, GEN XP, GEN p} assuming that $f$ is squarefree5031and that all its factors have the same degree, return the common degree,5032where \kbd{XP} is \kbd{FpX\_Frobenius(f, p)}.50335034\fun{long}{FpX_nbfact}{GEN f, GEN p}, assuming the \kbd{FpX}~f is squarefree,5035returns the number of its irreducible factors. Assumes that \kbd{p} is prime.50365037\fun{long}{FpX_nbfact_Frobenius}{GEN f, GEN XP, GEN p}, as5038\kbd{FpX\_nbfact(f, p)} but faster,5039where \kbd{XP} is \kbd{FpX\_Frobenius(f, p)}.50405041\fun{GEN}{FpX_degfact}{GEN f, GEN p}, as \kbd{FpX\_factor}, but the5042degrees of the irreducible factors are returned instead of the factors5043themselves (as a \typ{VECSMALL}). Assumes that \kbd{p} is prime.50445045\fun{long}{FpX_nbroots}{GEN f, GEN p} returns the number of distinct5046roots in \kbd{\Z/p\Z} of the \kbd{FpX}~\kbd{f}. Assumes that \kbd{p} is prime.50475048\fun{GEN}{FpX_oneroot}{GEN f, GEN p} returns one root in \kbd{\Z/p\Z} of5049the \kbd{FpX}~\kbd{f}. Return \kbd{NULL} if no root exists.5050Assumes that \kbd{p} is prime.50515052\fun{GEN}{FpX_oneroot_split}{GEN f, GEN p} as \kbd{FpX\_oneroot}.5053Faster when $f$ is close to be totally split.50545055\fun{GEN}{FpX_roots}{GEN f, GEN p} returns the roots in \kbd{\Z/p\Z} of5056the \kbd{FpX}~\kbd{f} (without multiplicity, as a vector of \kbd{Fp}s).5057Assumes that \kbd{p} is prime.50585059\fun{GEN}{FpX_split_part}{GEN f, GEN p} returns the largest totally split5060squarefree factor of $f$.50615062\fun{GEN}{random_FpX}{long d, long v, GEN p} returns a random \kbd{FpX}5063in variable \kbd{v}, of degree less than~\kbd{d}.50645065\fun{GEN}{FpX_resultant}{GEN x, GEN y, GEN p} returns the resultant5066of \kbd{x} and \kbd{y}, both \kbd{FpX}. The result is a \typ{INT}5067belonging to $[0,p-1]$.50685069\fun{GEN}{FpX_disc}{GEN x, GEN p} returns the discriminant5070of the \kbd{FpX} \kbd{x}. The result is a \typ{INT} belonging to $[0,p-1]$.50715072\fun{GEN}{FpX_FpXY_resultant}{GEN a, GEN b, GEN p}, \kbd{a} a \typ{POL} of5073\typ{INT}s (say in variable $X$), \kbd{b} a \typ{POL} (say in variable $X$)5074whose coefficients are either \typ{POL}s in $\Z[Y]$ or \typ{INT}s.5075Returns $\text{Res}_X(a, b)$ in $\F_p[Y]$ as an \kbd{FpY}. The function5076assumes that $X$ has lower priority than $Y$.50775078\fun{GEN}{FpX_Newton}{GEN x, long n, GEN p} return5079$\sum{i=0}^{n-1} \pi_i\*X^i$ where $\pi_i$ is the sum of the $i$th-power5080of the roots of $x$ in an algebraic closure.50815082\fun{GEN}{FpX_fromNewton}{GEN x, GEN p} recover a polynomial from it5083s Newton sums given by the coefficients of $x$.5084This function assumes that $p$ and the accuracy of $x$ as a \kbd{FpXn} is5085larger than the degree of the solution.50865087\fun{GEN}{FpX_Laplace}{GEN x, GEN p} return5088$\sum_{i=0}^{n-1} x_i\*i!\*X^i$.50895090\fun{GEN}{FpX_invLaplace}{GEN x, GEN p} return5091$\sum_{i=0}^{n-1} x_i/{i!}\*X^i$.50925093\subsec{\kbd{FpXQ}, \kbd{Fq}} Let \kbd{p} a \typ{INT} and \kbd{T} an5094\kbd{FpX} for \kbd{p}, both to be given in the function arguments; an \kbd{FpXQ}5095object is an \kbd{FpX} whose degree is strictly less than the degree of5096\kbd{T}. An \kbd{Fq} is either an \kbd{FpXQ} or an \kbd{Fp}. Both represent5097a class in $(\Z/\kbd{p}\Z)[X] / (T)$, in which all operations below take5098place. In addition, \kbd{Fq} routines also allow $\kbd{T} = \kbd{NULL}$, in5099which case no reduction mod \kbd{T} is performed on the result.51005101For efficiency, the routines in this section may leave small unused objects5102behind on the stack (their output is still suitable for \kbd{gerepileupto}).5103Besides \kbd{T} and \kbd{p}, arguments are either \kbd{FpXQ} or \kbd{Fq}5104depending on the function name. (All \kbd{Fq} routines accept \kbd{FpXQ}s by5105definition, not the other way round.)51065107\subsubsec{Preconditioned reduction}51085109For faster reduction, the modulus $T$ can be replaced by an extended modulus5110in all \kbd{FpXQ}- and \kbd{Fq}-classes functions, and in \kbd{FpX\_rem} and5111\kbd{FpX\_divrem}. An extended modulus(\kbd{FpXT}, which is a tree whose leaves are \kbd{FpX})In5112current implementation, an extended modulus is either a plain modulus (an5113\kbd{FpX}) or a pair of polynomials, one being the plain modulus $T$ and the5114other being \tet{FpX_invBarret}$(T,p)$.51155116\fun{GEN}{FpX_get_red}{GEN T, GEN p} returns the extended modulus \kbd{eT}.51175118To write code that works both with plain and extended moduli, the following5119accessors are defined:51205121\fun{GEN}{get_FpX_mod}{GEN eT} returns the underlying modulus $T$.51225123\fun{GEN}{get_FpX_var}{GEN eT} returns the variable number \kbd{varn}$(T)$.51245125\fun{GEN}{get_FpX_degree}{GEN eT} returns the degree \kbd{degpol}$(T)$.51265127\subsubsec{Conversions}51285129\fun{int}{ff_parse_Tp}{GEN Tp, GEN *T, GEN *p, long red} \kbd{Tp} is either5130a prime number $p$ or a \typ{VEC} with 2 entries $T$ (an irreducible5131polynomial mod $p$) and $p$ (a prime number). Sets \kbd{*p} and \kbd{*T}5132to the corresponding \kbd{GEN}s (\kbd{NULL} if undefined). If \kbd{red}5133is nonzero, normalize \kbd{*T} as an \kbd{FpX}; on the other hand,5134to initialize a $p$-adic function, set \kbd{red} to $0$ and \kbd{*T} is left5135as is and must be a \kbd{ZX} to start with. Return $1$ on success, and $0$ on5136failure. This helper routine is used by GP functions such as \kbd{factormod}5137where a single user argument defines a finite field. \typ{FFELT} is not5138supported.51395140\fun{GEN}{Rg_is_FpXQ}{GEN z, GEN *T, GEN *p}, checks if \kbd{z} is a \kbd{GEN}5141which can be mapped to $\F_p[X]/(T)$: anything for which \kbd{Rg\_is\_Fp} return5142$1$, a \typ{POL} for which \kbd{RgX\_to\_FpX} return $1$, a \typ{POLMOD}5143whose modulus is equal to \kbd{*T} if \kbd{*T} is not \kbd{NULL} (once mapped5144to a \kbd{FpX}), or a \typ{FFELT} $z$ with the same definition field as \kbd{*T}5145if \kbd{*T} is not \kbd{NULL} and is a \typ{FFELT}.51465147If an integer modulus is found it is put in \kbd{*p}, else \kbd{*p} is left5148unchanged. If a polynomial modulus is found it is put in \kbd{*T},5149if a \typ{FFELT} $z$ is found, $z$ is put in \kbd{*T}, else5150\kbd{*T} is left unchanged.51515152\fun{int}{RgX_is_FpXQX}{GEN z, GEN *T, GEN *p}, \kbd{z} a \typ{POL},5153checks if it can be mapped to a \kbd{FpXQX}, by checking \kbd{Rg\_is\_FpXQ}5154coefficientwise.51555156\fun{GEN}{Rg_to_FpXQ}{GEN z, GEN T, GEN p}, \kbd{z} a \kbd{GEN} which can be5157mapped to $\F_p[X]/(T)$: anything \kbd{Rg\_to\_Fp} can be applied to,5158a \typ{POL} to which \kbd{RgX\_to\_FpX} can be applied to, a \typ{POLMOD}5159whose modulus is divisible by $T$ (once mapped to a \kbd{FpX}), a suitable5160\typ{RFRAC}. Returns \kbd{z} as an \kbd{FpXQ}, normalized.51615162\fun{GEN}{Rg_to_Fq}{GEN z, GEN T, GEN p}, applies \kbd{Rg\_to\_Fp} if $T$5163is \kbd{NULL} and \kbd{Rg\_to\_FpXQ} otherwise.51645165\fun{GEN}{RgX_to_FpXQX}{GEN z, GEN T, GEN p}, \kbd{z} a \typ{POL}, returns the5166\kbd{FpXQ} obtained by applying \kbd{Rg\_to\_FpXQ} coefficientwise.51675168\fun{GEN}{RgX_to_FqX}{GEN z, GEN T, GEN p}: let \kbd{z} be a \typ{POL};5169returns the \kbd{FqX} obtained by applying \kbd{Rg\_to\_Fq} coefficientwise.51705171\fun{GEN}{Fq_to_FpXQ}{GEN z, GEN T, GEN p /*unused*/}5172if $z$ is a \typ{INT}, convert it to a constant polynomial in the variable of5173$T$, otherwise return $z$ (shallow function).51745175\fun{GEN}{Fq_red}{GEN x, GEN T, GEN p}, \kbd{x} a \kbd{ZX} or \typ{INT},5176reduce it to an \kbd{Fq} ($\kbd{T} = \kbd{NULL}$ is allowed iff \kbd{x} is a5177\typ{INT}).51785179\fun{GEN}{FqX_red}{GEN x, GEN T, GEN p}, \kbd{x} a \typ{POL}5180whose coefficients are \kbd{ZX}s or \typ{INT}s, reduce them to \kbd{Fq}s. (If5181$\kbd{T} = \kbd{NULL}$, as \kbd{FpXX\_red(x, p)}.)51825183\fun{GEN}{FqV_red}{GEN x, GEN T, GEN p}, \kbd{x} a vector of \kbd{ZX}s or5184\typ{INT}s, reduce them to \kbd{Fq}s. (If $\kbd{T} = \kbd{NULL}$, only5185reduce components mod \kbd{p} to \kbd{FpX}s or \kbd{Fp}s.)51865187\fun{GEN}{FpXQ_red}{GEN x, GEN T,GEN p} \kbd{x} a \typ{POL}5188whose coefficients are \typ{INT}s, reduce them to \kbd{FpXQ}s.51895190\subsec{\kbd{FpXQ}}51915192\fun{GEN}{FpXQ_add}{GEN x, GEN y, GEN T,GEN p}51935194\fun{GEN}{FpXQ_sub}{GEN x, GEN y, GEN T,GEN p}51955196\fun{GEN}{FpXQ_mul}{GEN x, GEN y, GEN T,GEN p}51975198\fun{GEN}{FpXQ_sqr}{GEN x, GEN T, GEN p}51995200\fun{GEN}{FpXQ_div}{GEN x, GEN y, GEN T,GEN p}52015202\fun{GEN}{FpXQ_inv}{GEN x, GEN T, GEN p} computes the inverse of \kbd{x}52035204\fun{GEN}{FpXQ_invsafe}{GEN x,GEN T,GEN p}, as \kbd{FpXQ\_inv}, returning5205\kbd{NULL} if \kbd{x} is not invertible.52065207\fun{GEN}{FpXQ_pow}{GEN x, GEN n, GEN T, GEN p} computes $\kbd{x}^\kbd{n}$.52085209\fun{GEN}{FpXQ_powu}{GEN x, ulong n, GEN T, GEN p} computes $\kbd{x}^\kbd{n}$5210for small $n$.52115212In the following three functions the integer parameter \kbd{ord} can be given5213either as a positive \typ{INT} $N$, or as its factorization matrix $\var{faN}$,5214or as a pair $[N,\var{faN}]$. The parameter may be omitted by setting it to5215\kbd{NULL} (the value is then $p^d-1$, $d = \deg T$).52165217\fun{GEN}{FpXQ_log}{GEN a, GEN g, GEN ord, GEN T, GEN p} Let \kbd{g} be of5218order dividing \kbd{ord} in the finite field $\F_p[X]/(T)$, return $e$ such that5219$a^e=g$. If $e$ does not exists, the result is undefined. Assumes5220that \kbd{T} is irreducible mod \kbd{p}.52215222\fun{GEN}{Fp_FpXQ_log}{GEN a, GEN g, GEN ord, GEN T, GEN p} As5223\kbd{FpXQ\_log}, \kbd{a} being a \kbd{Fp}.52245225\fun{GEN}{FpXQ_order}{GEN a, GEN ord, GEN T, GEN p} returns the order of the5226\kbd{FpXQ} \kbd{a}. Assume that \kbd{ord} is a multiple of the order of5227\kbd{a}. Assume that \kbd{T} is irreducible mod \kbd{p}.52285229\fun{int}{FpXQ_issquare}{GEN x, GEN T, GEN p} returns $1$ if $x$ is a square5230and $0$ otherwise. Assumes that \kbd{T} is irreducible mod \kbd{p}.52315232\fun{GEN}{FpXQ_sqrt}{GEN x, GEN T, GEN p} returns a square root of \kbd{x}.5233Return \kbd{NULL} if \kbd{x} is not a square.52345235\fun{GEN}{FpXQ_sqrtn}{GEN x, GEN n, GEN T, GEN p, GEN *zn}5236Let $T$be irreducible mod $p$ and $q = p^{\deg T}$; returns \kbd{NULL} if $a$5237is not an $n$-th power residue mod $p$. Otherwise, returns an $n$-th root of5238$a$; if \kbd{zn} is not \kbd{NULL} set it to a primitive $m$-th root of $1$5239in $\F_q$, $m = \gcd(q-1,n)$ allowing to compute all $m$ solutions in $\F_q$5240of the equation $x^n = a$.52415242\subsec{\kbd{Fq}}52435244\fun{GEN}{Fq_add}{GEN x, GEN y, GEN T/*unused*/, GEN p}52455246\fun{GEN}{Fq_sub}{GEN x, GEN y, GEN T/*unused*/, GEN p}52475248\fun{GEN}{Fq_mul}{GEN x, GEN y, GEN T, GEN p}52495250\fun{GEN}{Fq_Fp_mul}{GEN x, GEN y, GEN T, GEN p} multiplies the \kbd{Fq} $x$5251by the \typ{INT} $y$.52525253\fun{GEN}{Fq_mulu}{GEN x, ulong y, GEN T, GEN p} multiplies the \kbd{Fq} $x$5254by the scalar $y$.52555256\fun{GEN}{Fq_halve}{GEN x, GEN T, GEN p} returns $z$ such that $2\*z = x$5257assuming such $z$ exists.52585259\fun{GEN}{Fq_sqr}{GEN x, GEN T, GEN p}52605261\fun{GEN}{Fq_neg}{GEN x, GEN T, GEN p}52625263\fun{GEN}{Fq_neg_inv}{GEN x, GEN T, GEN p} computes $-\kbd{x}^{-1}$52645265\fun{GEN}{Fq_inv}{GEN x, GEN pol, GEN p} computes $\kbd{x}^{-1}$, raising an5266error if \kbd{x} is not invertible.52675268\fun{GEN}{Fq_invsafe}{GEN x, GEN pol, GEN p} as \kbd{Fq\_inv}, but returns5269\kbd{NULL} if \kbd{x} is not invertible.52705271\fun{GEN}{Fq_div}{GEN x, GEN y, GEN T, GEN p}52725273\fun{GEN}{FqV_inv}{GEN x, GEN T, GEN p} $x$ being a vector of \kbd{Fq}s,5274return the vector of inverses of the $x[i]$. The routine uses Montgomery's5275trick, and involves a single inversion, plus $3(N-1)$ multiplications for5276$N$ entries. The routine is not stack-clean: $2N$ \kbd{FpXQ} are left on5277stack, besides the $N$ in the result.52785279\fun{GEN}{FqV_factorback}{GEN L, GEN e, GEN T, GEN p} given an \kbd{FqV} $L$5280and a \kbd{ZV} or \kbd{zv} $e$ of the same length, return $\prod_i L_i^{e_i}$5281modulo $p$.52825283\fun{GEN}{Fq_pow}{GEN x, GEN n, GEN pol, GEN p} returns $\kbd{x}^\kbd{n}$.52845285\fun{GEN}{Fq_powu}{GEN x, ulong n, GEN pol, GEN p} returns $\kbd{x}^\kbd{n}$5286for small $n$.52875288\fun{GEN}{Fq_log}{GEN a, GEN g, GEN ord, GEN T, GEN p} as5289\tet{Fp_log} or \tet{FpXQ_log}.52905291\fun{int}{Fq_issquare}{GEN x, GEN T, GEN p} returns $1$ if $x$ is a square5292and $0$ otherwise. Assumes that \kbd{T} is irreducible mod \kbd{p} and that5293$p$ is prime; $T = \kbd{NULL}$ is forbidden unless $x$ is an \kbd{Fp}.52945295\fun{long}{Fq_ispower}{GEN x, GEN n, GEN T, GEN p} returns $1$ if $x$5296is a $n$-th power and $0$ otherwise. Assumes that \kbd{T} is irreducible mod5297\kbd{p} and that $p$ is prime; $T = \kbd{NULL}$ is forbidden unless $x$ is an5298\kbd{Fp}.52995300\fun{GEN}{Fq_sqrt}{GEN x, GEN T, GEN p} returns a square root of \kbd{x}.5301Return \kbd{NULL} if \kbd{x} is not a square.53025303\fun{GEN}{Fq_sqrtn}{GEN a, GEN n, GEN T, GEN p, GEN *zn}5304as \tet{FpXQ_sqrtn}.53055306\fun{GEN}{FpXQ_charpoly}{GEN x, GEN T, GEN p} returns the characteristic5307polynomial of \kbd{x}53085309\fun{GEN}{FpXQ_minpoly}{GEN x, GEN T, GEN p} returns the minimal polynomial5310of \kbd{x}53115312\fun{GEN}{FpXQ_norm}{GEN x, GEN T, GEN p} returns the norm of \kbd{x}53135314\fun{GEN}{FpXQ_trace}{GEN x, GEN T, GEN p} returns the trace of \kbd{x}53155316\fun{GEN}{FpXQ_conjvec}{GEN x, GEN T, GEN p} returns the vector of conjugates5317$[x,x^p,x^{p^2},\ldots,x^{p^{n-1}}]$ where $n$ is the degree of $T$.53185319\fun{GEN}{gener_FpXQ}{GEN T, GEN p, GEN *po} returns a primitive root modulo5320$(T,p)$. $T$ is an \kbd{FpX} assumed to be irreducible modulo the prime5321$p$. If \kbd{po} is not \kbd{NULL} it is set to $[o,\var{fa}]$, where $o$ is5322the order of the multiplicative group of the finite field, and \var{fa} is5323its factorization.53245325\fun{GEN}{gener_FpXQ_local}{GEN T, GEN p, GEN L}, \kbd{L} being a vector of5326primes dividing $p^{\deg T} - 1$, returns an element of $G:=\F_p[x]/(T)$5327which is a generator of the $\ell$-Sylow of $G$ for every $\ell$ in5328\kbd{L}. It is not necessary, and in fact slightly inefficient, to include5329$\ell=2$, since 2 is treated separately in any case, i.e. the generator5330obtained is never a square if $p$ is odd.53315332\fun{GEN}{gener_Fq_local}{GEN T, GEN p, GEN L} as5333\kbd{pgener\_Fp\_local(p, L)} if $T$ is \kbd{NULL},5334or \kbd{gener\_FpXQ\_local} (otherwise).533553365337\fun{GEN}{FpXQ_powers}{GEN x, long n, GEN T, GEN p} returns $[\kbd{x}^0,5338\dots, \kbd{x}^\kbd{n}]$ as a \typ{VEC} of \kbd{FpXQ}s.53395340\fun{GEN}{FpXQ_matrix_pow}{GEN x, long m, long n, GEN T, GEN p}, as5341\kbd{FpXQ\_powers}$(x, n-1, T, p)$, but returns the powers as a an5342$m\times n$ matrix. Usually, we have $m = n = \deg T$.53435344\fun{GEN}{FpXQ_autpow}{GEN a, ulong n, GEN T, GEN p} computes $\sigma^n(X)$5345assuming $a=\sigma(X)$ where $\sigma$ is an automorphism of the algebra5346$\F_p[X]/T(X)$.53475348\fun{GEN}{FpXQ_autsum}{GEN a, ulong n, GEN T, GEN p}5349$a$ being a two-component vector,5350$\sigma$ being the automorphism defined by $\sigma(X)=a[1]\pmod{T(X)}$,5351returns the vector $[\sigma^n(X),b\sigma(b)\ldots\sigma^{n-1}(b)]$5352where $b=a[2]$.53535354\fun{GEN}{FpXQ_auttrace}{GEN a, ulong n, GEN T, GEN p}5355$a$ being a two-component vector,5356$\sigma$ being the automorphism defined by $\sigma(X)=a[1]\pmod{T(X)}$,5357returns the vector $[\sigma^n(X),b+\sigma(b)+\ldots+\sigma^{n-1}(b)]$5358where $b=a[2]$.53595360\fun{GEN}{FpXQ_autpowers}{GEN S, long n, GEN T, GEN p} returns5361$[x,S(x),S(S(x)),\dots,S^{(n)}(x)]$ as a \typ{VEC} of \kbd{FpXQ}s.53625363\fun{GEN}{FpXQM_autsum}{GEN a, long n, GEN T, GEN p}5364$\sigma$ being the automorphism defined by $\sigma(X)=a[1]\pmod{T(X)}$,5365returns the vector $[\sigma^n(X),b\sigma(b)\ldots\sigma^{n-1}(b)]$5366where $b=a[2]$ is a square matrix.53675368\fun{GEN}{FpX_FpXQ_eval}{GEN f, GEN x, GEN T, GEN p} returns5369$\kbd{f}(\kbd{x})$.53705371\fun{GEN}{FpX_FpXQV_eval}{GEN f, GEN V, GEN T, GEN p} returns5372$\kbd{f}(\kbd{x})$, assuming that \kbd{V} was computed by5373$\kbd{FpXQ\_powers}(\kbd{x}, n, \kbd{T}, \kbd{p})$.53745375\fun{GEN}{FpXC_FpXQ_eval}{GEN C, GEN x, GEN T,GEN p} applies5376\kbd{FpX\_FpXQV\_eval} to all elements of the vector $C$5377and returns a \typ{COL}.53785379\fun{GEN}{FpXC_FpXQV_eval}{GEN C, GEN V,GEN T,GEN p} applies5380\kbd{FpX\_FpXQV\_eval} to all elements of the vector $C$5381and returns a \typ{COL}.53825383\fun{GEN}{FpXM_FpXQV_eval}{GEN M, GEN V,GEN T,GEN p} applies5384\kbd{FpX\_FpXQV\_eval} to all elements of the matrix $M$.53855386\subsec{\kbd{FpXn}} Let \kbd{p} a \typ{INT} and \kbd{T} an5387\kbd{FpX} for \kbd{p}, both to be given in the function arguments; an \kbd{FpXn}5388object is an \kbd{FpX} whose degree is strictly less than \kbd{n}.5389They represent a class in $(\Z/\kbd{p}\Z)[X] / (X^n)$, in which all operations5390below take place. They can be seen as truncated power series.53915392\fun{GEN}{FpXn_mul}{GEN x, GEN y, long n, GEN p} return $x\*y\pmod{X^n}$.53935394\fun{GEN}{FpXn_sqr}{GEN x, long n, GEN p} return $x^2\pmod{X^n}$.53955396\fun{GEN}{FpXn_inv}{GEN x, long n, GEN p} return $1/x\pmod{X^n}$.53975398\fun{GEN}{FpXn_exp}{GEN f, long n, GEN p} return $\exp(f)$5399as a composition of formal power series.5400It is required that the valuation of $f$ is positive and that $p>n$.54015402\fun{GEN}{FpXn_expint}{GEN f, long n, GEN p} return $\exp(F)$5403where $F$ is the primitive of $f$ that vanishes at $0$.5404It is required that $p>n$.54055406\subsec{\kbd{FpXC}, \kbd{FpXM}}54075408\fun{GEN}{FpXC_center}{GEN C, GEN p, GEN pov2}54095410\fun{GEN}{FpXM_center}{GEN M, GEN p, GEN pov2}54115412\subsec{\kbd{FpXX}, \kbd{FpXY}}5413Contrary to what the name implies, an \kbd{FpXX} is a \typ{POL} whose5414coefficients are either \typ{INT}s or \kbd{FpX}s. This reduces memory5415overhead at the expense of consistency. The prefix \kbd{FpXY} is an5416alias for \kbd{FpXX} when variables matters.54175418\fun{GEN}{FpXX_red}{GEN z, GEN p}, \kbd{z} a \typ{POL} whose coefficients are5419either \kbd{ZX}s or \typ{INT}s. Returns the \typ{POL} equal to \kbd{z} with5420all components reduced modulo \kbd{p}.54215422\fun{GEN}{FpXX_renormalize}{GEN x, long l}, as \kbd{normalizepol}, where5423$\kbd{l} = \kbd{lg(x)}$, in place.54245425\fun{GEN}{FpXX_add}{GEN x, GEN y, GEN p} adds \kbd{x} and \kbd{y}.54265427\fun{GEN}{FpXX_sub}{GEN x, GEN y, GEN p} returns $\kbd{x}-\kbd{y}$.54285429\fun{GEN}{FpXX_neg}{GEN x, GEN p} returns $-\kbd{x}$.54305431\fun{GEN}{FpXX_Fp_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{FpXX}~\kbd{x}5432by the \kbd{Fp}~\kbd{y}.54335434\fun{GEN}{FpXX_FpX_mul}{GEN x, GEN y, GEN p} multiplies the coefficients of the5435\kbd{FpXX}~\kbd{x} by the \kbd{FpX}~\kbd{y}.54365437\fun{GEN}{FpXX_mulu}{GEN x, GEN y, GEN p} multiplies the \kbd{FpXX}~\kbd{x}5438by the scalar \kbd{y}.54395440\fun{GEN}{FpXX_halve}{GEN x, GEN p} returns $z$ such that $2\*z = x$5441assuming such $z$ exists.54425443\fun{GEN}{FpXX_deriv}{GEN P, GEN p} differentiates \kbd{P} with respect to5444the main variable.54455446\fun{GEN}{FpXX_integ}{GEN P, GEN p} returns the primitive of \kbd{P} with5447respect to the main variable whose constant term is $0$.54485449\fun{GEN}{FpXY_eval}{GEN Q, GEN y, GEN x, GEN p} $Q$ being an \kbd{FpXY},5450i.e.~a \typ{POL} with \kbd{Fp} or \kbd{FpX} coefficients representing an5451element of $\F_p[X][Y]$. Returns the \kbd{Fp} $Q(x,y)$.54525453\fun{GEN}{FpXY_evalx}{GEN Q, GEN x, GEN p} $Q$ being an \kbd{FpXY}, returns the5454\kbd{FpX} $Q(x,Y)$, where $Y$ is the main variable of $Q$.54555456\fun{GEN}{FpXY_evaly}{GEN Q, GEN y, GEN p, long vx} $Q$ an \kbd{FpXY}, returns5457the \kbd{FpX} $Q(X,y)$, where $X$ is the second variable of $Q$, and \kbd{vx}5458is the variable number of $X$.54595460\fun{GEN}{FpXY_FpXQ_evaly}{GEN Q, GEN y, GEN T, GEN p, long vx} $Q$ an \kbd{FpXY}5461and $y$ being an \kbd{FpXQ}, returns the \kbd{FpXQX} $Q(X,y)$, where $X$ is the5462second variable of $Q$, and \kbd{vx} is the variable number of $X$.54635464\fun{GEN}{FpXY_Fq_evaly}{GEN Q, GEN y, GEN T, GEN p, long vx} $Q$ an \kbd{FpXY}5465and $y$ being an \kbd{Fq}, returns the \kbd{FqX} $Q(X,y)$, where $X$ is the5466second variable of $Q$, and \kbd{vx} is the variable number of $X$.54675468\fun{GEN}{FpXY_FpXQ_evalx}{GEN Q, GEN x, ulong p} $Q$ an \kbd{FpXY} and5469$x$ being an \kbd{FpXQ}, returns the \kbd{FpXQX} $Q(x,Y)$, where $Y$ is the5470first variable of $Q$.54715472\fun{GEN}{FpXY_FpXQV_evalx}{GEN Q, GEN V, ulong p} $Q$ an \kbd{FpXY} and5473$x$ being an \kbd{FpXQ}, returns the \kbd{FpXQX} $Q(x,Y)$, where $Y$ is the5474first variable of $Q$, assuming that \kbd{V} was computed by5475$\kbd{FpXQ\_powers}(\kbd{x}, n, \kbd{T}, \kbd{p})$.54765477\fun{GEN}{FpXYQQ_pow}{GEN x, GEN n, GEN S, GEN T, GEN p}, \kbd{x} being a5478\kbd{FpXY}, \kbd{T} being a \kbd{FpX} and \kbd{S} being a \kbd{FpY},5479return $x^n \pmod{S,T,p}$.54805481\subsec{\kbd{FpXQX}, \kbd{FqX}}5482Contrary to what the name implies, an \kbd{FpXQX} is a \typ{POL} whose5483coefficients are \kbd{Fq}s. So the only difference between \kbd{FqX} and5484\kbd{FpXQX} routines is that $\kbd{T} = \kbd{NULL}$ is not allowed in the5485latter. (It was thought more useful to allow \typ{INT} components than to5486enforce strict consistency, which would not imply any efficiency gain.)54875488\subsubsec{Basic operations}54895490\fun{GEN}{FqX_add}{GEN x,GEN y,GEN T,GEN p}54915492\fun{GEN}{FqX_Fq_add}{GEN x, GEN y, GEN T, GEN p} adds the5493\kbd{Fq}~\kbd{y} to the \kbd{FqX}~\kbd{x}.54945495\fun{GEN}{FqX_Fq_sub}{GEN x, GEN y, GEN T, GEN p} substracts the5496\kbd{Fq}~\kbd{y} to the \kbd{FqX}~\kbd{x}.54975498\fun{GEN}{FqX_neg}{GEN x,GEN T,GEN p}54995500\fun{GEN}{FqX_sub}{GEN x,GEN y,GEN T,GEN p}55015502\fun{GEN}{FqX_mul}{GEN x, GEN y, GEN T, GEN p}55035504\fun{GEN}{FqX_Fq_mul}{GEN x, GEN y, GEN T, GEN p} multiplies the5505\kbd{FqX}~\kbd{x} by the \kbd{Fq}~\kbd{y}.55065507\fun{GEN}{FqX_mulu}{GEN x, ulong y, GEN T, GEN p} multiplies the5508\kbd{FqX}~\kbd{x} by the scalar~\kbd{y}.55095510\fun{GEN}{FqX_halve}{GEN x, GEN T, GEN p} returns $z$ such that $2\*z = x$5511assuming such $z$ exists.55125513\fun{GEN}{FqX_Fp_mul}{GEN x, GEN y, GEN T, GEN p} multiplies the5514\kbd{FqX}~\kbd{x} by the \typ{INT}~\kbd{y}.55155516\fun{GEN}{FqX_Fq_mul_to_monic}{GEN x, GEN y, GEN T, GEN p}5517returns $x\*y$ assuming the result is monic of the same degree as $x$ (in5518particular $y\neq 0$).55195520\fun{GEN}{FpXQX_normalize}{GEN z, GEN T, GEN p}55215522\fun{GEN}{FqX_normalize}{GEN z, GEN T, GEN p} divides the \kbd{FqX}~\kbd{z}5523by its leading term. The leading coefficient becomes $1$ as a \typ{INT}.55245525\fun{GEN}{FqX_sqr}{GEN x, GEN T, GEN p}55265527\fun{GEN}{FqX_powu}{GEN x, ulong n, GEN T, GEN p}55285529\fun{GEN}{FqX_divrem}{GEN x, GEN y, GEN T, GEN p, GEN *z}55305531\fun{GEN}{FqX_div}{GEN x, GEN y, GEN T, GEN p}55325533\fun{GEN}{FqX_div_by_X_x}{GEN a, GEN x, GEN T, GEN p, GEN *r}55345535\fun{GEN}{FqX_rem}{GEN x, GEN y, GEN T, GEN p}55365537\fun{GEN}{FqX_deriv}{GEN x, GEN T, GEN p} returns the derivative of \kbd{x}.5538(This function is suitable for \kbd{gerepilupto} but not memory-clean.)55395540\fun{GEN}{FqX_integ}{GEN x, GEN T, GEN p} returns the primitive of \kbd{x}.5541whose constant term is $0$.55425543\fun{GEN}{FqX_translate}{GEN P, GEN c, GEN T, GEN p} let $c$ be an \kbd{Fq}5544defined modulo $(p, T)$, and let $P$ be an \kbd{FqX}; returns the translated5545\kbd{FqX} of $P(X+c)$.55465547\fun{GEN}{FqX_gcd}{GEN P, GEN Q, GEN T, GEN p} returns a (not necessarily5548monic) greatest common divisor of $x$ and $y$.55495550\fun{GEN}{FqX_extgcd}{GEN x, GEN y, GEN T, GEN p, GEN *ptu, GEN *ptv}5551returns $d = \text{GCD}(\kbd{x},\kbd{y})$ (not necessarily monic), and sets5552\kbd{*u}, \kbd{*v} to the Bezout coefficients such that $\kbd{*ux} +5553\kbd{*vy} = d$.55545555\fun{GEN}{FqX_halfgcd}{GEN x, GEN y, GEN T, GEN p} returns a two-by-two5556\kbd{FqXM} $M$ with determinant $\pm 1$ such that the image $(a,b)$ of $(x,y)$5557by $M$ has the property that $\deg a \geq {\deg x \over 2} > \deg b$.55585559\fun{GEN}{FqX_eval}{GEN x, GEN y, GEN T, GEN p} evaluates the \kbd{FqX}~\kbd{x}5560at the \kbd{Fq}~\kbd{y}. The result is an~\kbd{Fq}.55615562\fun{GEN}{FqXY_eval}{GEN Q, GEN y, GEN x, GEN T, GEN p} $Q$ an \kbd{FqXY},5563i.e.~a \typ{POL} with \kbd{Fq} or \kbd{FqX} coefficients representing an5564element of $\F_q[X][Y]$. Returns the \kbd{Fq} $Q(x,y)$.55655566\fun{GEN}{FqXY_evalx}{GEN Q, GEN x, GEN T, GEN p} $Q$ being an \kbd{FqXY},5567returns the \kbd{FqX} $Q(x,Y)$, where $Y$ is the main variable of $Q$.55685569\fun{GEN}{random_FpXQX}{long d, long v, GEN T, GEN p} returns a random5570\kbd{FpXQX} in variable \kbd{v}, of degree less than~\kbd{d}.55715572\fun{GEN}{FpXQX_renormalize}{GEN x, long lx}55735574\fun{GEN}{FpXQX_red}{GEN z, GEN T, GEN p} \kbd{z} a \typ{POL} whose5575coefficients are \kbd{ZX}s or \typ{INT}s, reduce them to \kbd{FpXQ}s.55765577\fun{GEN}{FpXQX_mul}{GEN x, GEN y, GEN T, GEN p}55785579\fun{GEN}{Kronecker_to_FpXQX}{GEN z, GEN T, GEN p}. Let $n = \deg T$ and let5580$P(X,Y)\in \Z[X,Y]$ lift a polynomial in $K[Y]$, where $K := \F_p[X]/(T)$ and5581$\deg_X P < 2n-1$ --- such as would result from multiplying minimal degree5582lifts of two polynomials in $K[Y]$. Let $z = P(t,t^{2*n-1})$ be a Kronecker5583form of $P$ (see \tet{RgXX_to_Kronecker}), this function returns $Q\in5584\Z[X,t]$ such that $Q$ is congruent to $P(X,t)$ mod $(p, T(X))$, $\deg_X Q <5585n$, and all coefficients are in $[0,p[$. Not stack-clean. Note that $t$ need5586not be the same variable as $Y$!55875588\fun{GEN}{FpXQX_FpXQ_mul}{GEN x, GEN y, GEN T, GEN p}55895590\fun{GEN}{FpXQX_sqr}{GEN x, GEN T, GEN p}55915592\fun{GEN}{FpXQX_divrem}{GEN x, GEN y, GEN T, GEN p, GEN *pr}55935594\fun{GEN}{FpXQX_div}{GEN x, GEN y, GEN T, GEN p}55955596\fun{GEN}{FpXQX_div_by_X_x}{GEN a, GEN x, GEN T, GEN p, GEN *r}55975598\fun{GEN}{FpXQX_rem}{GEN x, GEN y, GEN T, GEN p}55995600\fun{GEN}{FpXQX_powu}{GEN x, ulong n, GEN T, GEN p} returns $x^n$.56015602\fun{GEN}{FpXQX_digits}{GEN x, GEN B, GEN T, GEN p}56035604\fun{GEN}{FpXQX_dotproduct}{GEN x, GEN y, GEN T, GEN p} returns the scalar5605product of the coefficients of $x$ and $y$.56065607\fun{GEN}{FpXQXV_FpXQX_fromdigits}{GEN v, GEN B, GEN T, GEN p}56085609\fun{GEN}{FpXQX_invBarrett}{GEN y, GEN T, GEN p} returns the Barrett inverse of5610the \kbd{FpXQX} $y$, namely a lift of $1/\kbd{polrecip}(y)+O(x^{\deg(y)-1})$.56115612\fun{GEN}{FpXQXV_prod}{GEN V, GEN T, GEN p}, \kbd{V} being a vector of5613\kbd{FpXQX}, returns their product.56145615\fun{GEN}{FpXQX_gcd}{GEN x, GEN y, GEN T, GEN p}56165617\fun{GEN}{FpXQX_extgcd}{GEN x, GEN y, GEN T, GEN p, GEN *ptu, GEN *ptv}56185619\fun{GEN}{FpXQX_halfgcd}{GEN x, GEN y, GEN T, GEN p}56205621\fun{GEN}{FpXQX_resultant}{GEN x, GEN y, GEN T, GEN p} returns the resultant5622of \kbd{x} and \kbd{y}.56235624\fun{GEN}{FpXQX_disc}{GEN x, GEN T, GEN p} returns the discriminant5625of \kbd{x}.56265627\fun{GEN}{FpXQX_FpXQXQ_eval}{GEN f,GEN x,GEN S, GEN T,GEN p} returns5628$\kbd{f}(\kbd{x})$.56295630\subsec{\kbd{FpXQXn}, \kbd{FqXn}}56315632A \kbd{FpXQXn} is a \typ{FpXQX} which represents an element of the ring5633$(Fp[X]/T(X))[Y]/(Y^n)$, where $T$ is a \kbd{FpX}.56345635\fun{GEN}{FpXQXn_sqr}{GEN x, long n, GEN T, GEN p}56365637\fun{GEN}{FqXn_sqr}{GEN x, long n, GEN T, GEN p}56385639\fun{GEN}{FpXQXn_mul}{GEN x, GEN y, long n, GEN T, GEN p}56405641\fun{GEN}{FqXn_mul}{GEN x, GEN y, long n, GEN T, GEN p}56425643\fun{GEN}{FpXQXn_inv}{GEN x, long n, GEN T, GEN p}56445645\fun{GEN}{FqXn_inv}{GEN x, long n, GEN T, GEN p}56465647\fun{GEN}{FpXQXn_exp}{GEN x, long n, GEN T, GEN p} return $\exp(x)$5648as a composition of formal power series.5649It is required that the valuation of $x$ is positive and that $p>n$.56505651\fun{GEN}{FqXn_exp}{GEN x, long n, GEN T, GEN p}56525653\fun{GEN}{FpXQXn_expint}{GEN f, long n, GEN p} return $\exp(F)$5654where $F$ is the primitive of $f$ that vanishes at $0$.5655It is required that $p>n$.56565657\fun{GEN}{FqXn_expint}{GEN x, long n, GEN T, GEN p}56585659\subsec{\kbd{FpXQXQ}, \kbd{FqXQ}}56605661A \kbd{FpXQXQ} is a \typ{FpXQX} which represents an element of the ring5662$(Fp[X]/T(X))[Y]/S(X,Y)$, where $T$ is a \kbd{FpX} and $S$ a \kbd{FpXQX}5663modulo $T$. A \kbd{FqXQ} is identical except that $T$ is allowed to be5664\kbd{NULL} in which case $S$ must be a \kbd{FpX}.56655666\subsubsec{Preconditioned reduction}56675668For faster reduction, the modulus \kbd{S} can be replaced by an extended5669modulus, which is an \kbd{FpXQXT}, in all \kbd{FpXQXQ}- and \kbd{FqXQ}-classes5670functions, and in \kbd{FpXQX\_rem} and \kbd{FpXQX\_divrem}.56715672\fun{GEN}{FpXQX_get_red}{GEN S, GEN T, GEN p} returns the extended modulus5673\kbd{eS}.56745675\fun{GEN}{FqX_get_red}{GEN S, GEN T, GEN p} identical, but allow $T$ to5676be \kbd{NULL}, in which case it returns \kbd{FpX\_get\_red(S,p)}.56775678To write code that works both with plain and extended moduli, the following5679accessors are defined:56805681\fun{GEN}{get_FpXQX_mod}{GEN eS} returns the underlying modulus \kbd{S}.56825683\fun{GEN}{get_FpXQX_var}{GEN eS} returns the variable number of the modulus.56845685\fun{GEN}{get_FpXQX_degree}{GEN eS} returns the degree of the modulus.56865687Furthermore, \kbd{ZXXT\_to\_FlxXT} allows to convert an extended modulus for5688a \kbd{FpXQX} to an extended modulus for the corresponding \kbd{FlxqX}.56895690\subsubsec{basic operations}56915692\fun{GEN}{FpXQX_FpXQXQV_eval}{GEN f,GEN V,GEN S,GEN T,GEN p} returns5693$\kbd{f}(\kbd{x})$, assuming that \kbd{V} was computed by5694$\kbd{FpXQXQ\_powers}(\kbd{x}, n, \kbd{S}, \kbd{T}, \kbd{p})$.56955696\fun{GEN}{FpXQXQ_div}{GEN x, GEN y, GEN S, GEN T, GEN p}, \kbd{x}, \kbd{y} and5697\kbd{S} being \kbd{FpXQX}s, returns $\kbd{x}*\kbd{y}^{-1}$ modulo \kbd{S}.56985699\fun{GEN}{FpXQXQ_inv}{GEN x, GEN S, GEN T, GEN p}, \kbd{x} and5700\kbd{S} being \kbd{FpXQX}s, returns $\kbd{x}^{-1}$ modulo \kbd{S}.57015702\fun{GEN}{FpXQXQ_invsafe}{GEN x, GEN S, GEN T,GEN p}, as \kbd{FpXQXQ\_inv},5703returning \kbd{NULL} if \kbd{x} is not invertible.57045705\fun{GEN}{FpXQXQ_mul}{GEN x, GEN y, GEN S, GEN T, GEN p}, \kbd{x}, \kbd{y} and5706\kbd{S} being \kbd{FpXQX}s, returns $\kbd{x}\*\kbd{y}$ modulo \kbd{S}.57075708\fun{GEN}{FpXQXQ_sqr}{GEN x, GEN S, GEN T, GEN p}, \kbd{x} and5709\kbd{S} being \kbd{FpXQX}s, returns $\kbd{x}^2$ modulo \kbd{S}.57105711\fun{GEN}{FpXQXQ_pow}{GEN x, GEN n, GEN S, GEN T, GEN p}, \kbd{x} and5712\kbd{S} being \kbd{FpXQX}s, returns $\kbd{x}^\kbd{n}$ modulo \kbd{S}.57135714\fun{GEN}{FpXQXQ_powers}{GEN x, long n, GEN S, GEN T, GEN p}, \kbd{x} and5715\kbd{S} being \kbd{FpXQX}s, returns $[\kbd{x}^0, \dots, \kbd{x}^\kbd{n}]$ as a5716\typ{VEC} of \kbd{FpXQXQ}s.57175718\fun{GEN}{FpXQXQ_halfFrobenius}{GEN A, GEN S, GEN T, GEN p} returns5719$A(X)^{(q-1)/2}\pmod{S(X)}$ over the finite field $\F_q$ defined by $T$5720and $p$, thus $q=p^n$ where $n$ is the degree of $T$.57215722\fun{GEN}{FpXQXQ_minpoly}{GEN x, GEN S, GEN T, GEN p}, as5723\kbd{FpXQ\_minpoly}57245725\fun{GEN}{FpXQXQ_matrix_pow}{GEN x, long m, long n, GEN S, GEN T, GEN p}5726returns the same powers of \kbd{x} as \kbd{FpXQXQ\_powers}$(x, n-1,S, T, p)$,5727but as an $m\times n$ matrix.57285729\fun{GEN}{FpXQXQ_autpow}{GEN a, long n, GEN S, GEN T, GEN p}5730$\sigma$ being the automorphism defined by $\sigma(X)=a[1]\pmod{T(X)}$,5731$\sigma(Y)=a[2]\pmod{S(X,Y),T(X)}$, returns $[\sigma^n(X),\sigma^n(Y)]$.57325733\fun{GEN}{FpXQXQ_autsum}{GEN a, long n, GEN S, GEN T, GEN p}5734$\sigma$ being the automorphism defined by $\sigma(X)=a[1]\pmod{T(X)}$,5735$\sigma(Y)=a[2]\pmod{S(X,Y),T(X)}$, returns the vector5736$[\sigma^n(X),\sigma^n(Y),b\sigma(b)\ldots\sigma^{n-1}(b)]$5737where $b=a[3]$.57385739\fun{GEN}{FpXQXQ_auttrace}{GEN a, long n, GEN S, GEN T, GEN p}5740$\sigma$ being the automorphism defined by $\sigma(X)=X\pmod{T(X)}$,5741$\sigma(Y)=a[1]\pmod{S(X,Y),T(X)}$, returns the vector5742$[\sigma^n(X),\sigma^n(Y),b+\sigma(b)+\ldots+\sigma^{n-1}(b)]$5743where $b=a[2]$.57445745% FqXQ57465747\fun{GEN}{FqXQ_add}{GEN x, GEN y, GEN S, GEN T, GEN p}, \kbd{x}, \kbd{y} and5748\kbd{S} being \kbd{FqX}s, returns $\kbd{x} + \kbd{y}$ modulo \kbd{S}.57495750\fun{GEN}{FqXQ_sub}{GEN x, GEN y, GEN S, GEN T, GEN p}, \kbd{x}, \kbd{y} and5751\kbd{S} being \kbd{FqX}s, returns $\kbd{x} - \kbd{y}$ modulo \kbd{S}.57525753\fun{GEN}{FqXQ_mul}{GEN x, GEN y, GEN S, GEN T, GEN p}, \kbd{x}, \kbd{y} and5754\kbd{S} being \kbd{FqX}s, returns $\kbd{x}\*\kbd{y}$ modulo \kbd{S}.57555756\fun{GEN}{FqXQ_div}{GEN x, GEN y, GEN S, GEN T, GEN p}, \kbd{x} and5757\kbd{S} being \kbd{FqX}s, returns $\kbd{x}/\kbd{y}$ modulo \kbd{S}.57585759\fun{GEN}{FqXQ_inv}{GEN x, GEN S, GEN T, GEN p}, \kbd{x} and5760\kbd{S} being \kbd{FqX}s, returns $\kbd{x}^{-1}$ modulo \kbd{S}.57615762\fun{GEN}{FqXQ_invsafe}{GEN x, GEN S, GEN T, GEN p} , as \kbd{FqXQ\_inv},5763returning \kbd{NULL} if \kbd{x} is not invertible.57645765\fun{GEN}{FqXQ_sqr}{GEN x, GEN S, GEN T, GEN p}, \kbd{x} and5766\kbd{S} being \kbd{FqX}s, returns $\kbd{x}^2$ modulo \kbd{S}.57675768\fun{GEN}{FqXQ_pow}{GEN x, GEN n, GEN S, GEN T, GEN p}, \kbd{x} and5769\kbd{S} being \kbd{FqX}s, returns $\kbd{x}^\kbd{n}$ modulo \kbd{S}.57705771\fun{GEN}{FqXQ_powers}{GEN x, long n, GEN S, GEN T, GEN p}, \kbd{x} and5772\kbd{S} being \kbd{FqX}s, returns $[\kbd{x}^0, \dots, \kbd{x}^\kbd{n}]$ as a5773\typ{VEC} of \kbd{FqXQ}s.57745775\fun{GEN}{FqXQ_matrix_pow}{GEN x, long m, long n, GEN S, GEN T, GEN p}5776returns the same powers of \kbd{x} as \kbd{FqXQ\_powers}$(x, n-1,S, T, p)$,5777but as an $m\times n$ matrix.57785779\fun{GEN}{FqV_roots_to_pol}{GEN V, GEN T, GEN p, long v},5780\kbd{V} being a vector of \kbd{Fq}s, returns the monic \kbd{FqX}5781$\prod_i (\kbd{pol\_x[v]} - \kbd{V[i]})$.57825783\subsubsec{Miscellaneous operations}57845785\fun{GEN}{init_Fq}{GEN p, long n, long v} returns an irreducible polynomial5786of degree $\kbd{n} > 0$ over $\F_p$, in variable \kbd{v}.57875788\fun{int}{FqX_is_squarefree}{GEN P, GEN T, GEN p}57895790\fun{GEN}{FpXQX_roots}{GEN f, GEN T, GEN p} return the roots of $f$ in5791$\F_p[X]/(T)$. Assumes \kbd{p} is prime and \kbd{T} irreducible in $\F_p[X]$.57925793\fun{GEN}{FqX_roots}{GEN f, GEN T, GEN p} same but allow $\kbd{T} = \kbd{NULL}$.57945795\fun{GEN}{FpXQX_factor}{GEN f, GEN T, GEN p} same output convention as5796\kbd{FpX\_factor}. Assumes \kbd{p} is prime and \kbd{T} irreducible5797in $\F_p[X]$.57985799\fun{GEN}{FqX_factor}{GEN f, GEN T, GEN p} same but allow $\kbd{T} = \kbd{NULL}$.58005801\fun{GEN}{FpXQX_factor_squarefree}{GEN f, GEN T, GEN p} squarefree5802factorization of $f$ modulo $(T,p)$; same output convention as5803\kbd{FpX\_factor\_squarefree}. Assumes \kbd{p} is prime and \kbd{T}5804irreducible in $\F_p[X]$.58055806\fun{GEN}{FqX_factor_squarefree}{GEN f, GEN T, GEN p} same but allow5807$\kbd{T} = \kbd{NULL}$.58085809\fun{GEN}{FpXQX_ddf}{GEN f, GEN T, GEN p} as \kbd{FpX\_ddf}.58105811\fun{GEN}{FqX_ddf}{GEN f, GEN T, GEN p} same but allow $\kbd{T} = \kbd{NULL}$.58125813\fun{long}{FpXQX_ddf_degree}{GEN f, GEN XP, GEN T, GEN p}, as5814\kbd{FpX\_ddf\_degree}.58155816\fun{GEN}{FpXQX_degfact}{GEN f, GEN T, GEN p}, as \kbd{FpX\_degfact}.58175818\fun{GEN}{FqX_degfact}{GEN f, GEN T, GEN p} same but allow5819$\kbd{T} = \kbd{NULL}$.58205821\fun{GEN}{FpXQX_split_part}{GEN f, GEN T, GEN p} returns the largest totally5822split squarefree factor of $f$.58235824\fun{long}{FpXQX_ispower}{GEN f, ulong k, GEN T, GEN p, GEN *pt}5825return $1$ if the \kbd{FpXQX} $f$ is a $k$-th power, $0$ otherwise.5826If \kbd{pt} is not \kbd{NULL}, set it to $g$ such that $g^k = f$.58275828\fun{long}{FqX_ispower}{GEN f, ulong k, GEN T, GEN p, GEN *pt}5829same but allow $\kbd{T} = \kbd{NULL}$.58305831\fun{GEN}{FpX_factorff}{GEN P, GEN T, GEN p}. Assumes \kbd{p} prime5832and \kbd{T} irreducible in $\F_p[X]$. Factor the \kbd{FpX} \kbd{P}5833over the finite field $\F_p[Y]/(T(Y))$. See \kbd{FpX\_factorff\_irred}5834if \kbd{P} is known to be irreducible of $\F_p$.58355836\fun{GEN}{FpX_rootsff}{GEN P, GEN T, GEN p}. Assumes \kbd{p} prime5837and \kbd{T} irreducible in $\F_p[X]$. Returns the roots of the \kbd{FpX}5838\kbd{P} belonging to the finite field $\F_p[Y]/(T(Y))$.58395840\fun{GEN}{FpX_factorff_irred}{GEN P, GEN T, GEN p}. Assumes \kbd{p} prime5841and \kbd{T} irreducible in $\F_p[X]$. Factors the \emph{irreducible}5842\kbd{FpX} \kbd{P} over the finite field $\F_p[Y]/(T(Y))$ and returns the5843vector of irreducible \kbd{FqX}s factors (the exponents, being all equal to5844$1$, are not included).58455846\fun{GEN}{FpX_ffisom}{GEN P, GEN Q, GEN p}. Assumes \kbd{p} prime,5847\kbd{P}, \kbd{Q} are \kbd{ZX}s, both irreducible mod \kbd{p}, and5848$\deg(P) \mid \deg Q$. Outputs a monomorphism between $\F_p[X]/(P)$ and5849$\F_p[X]/(Q)$, as a polynomial $R$ such that $\kbd{Q} \mid \kbd{P}(R)$ in5850$\F_p[X]$. If \kbd{P} and \kbd{Q} have the same degree, it is of course an5851isomorphism.58525853\fun{void}{FpX_ffintersect}{GEN P, GEN Q, long n, GEN p, GEN *SP,GEN *SQ, GEN5854MA,GEN MB}\hfil\break5855Assumes \kbd{p} is prime, \kbd{P}, \kbd{Q} are \kbd{ZX}s, both5856irreducible mod \kbd{p}, and \kbd{n} divides both the degree of \kbd{P} and5857\kbd{Q}. Compute \kbd{SP} and \kbd{SQ} such that the subfield of5858$\F_p[X]/(P)$ generated by \kbd{SP} and the subfield of $\F_p[X]/(Q)$5859generated by \kbd{SQ} are isomorphic of degree \kbd{n}. The polynomials5860\kbd{P} and \kbd{Q} do not need to be of the same variable. If \kbd{MA}5861(resp. \kbd{MB}) is not \kbd{NULL}, it must be the matrix of the Frobenius5862map in $\F_p[X]/(P)$ (resp.~$\F_p[X]/(Q)$).58635864\fun{GEN}{FpXQ_ffisom_inv}{GEN S, GEN T, GEN p}. Assumes \kbd{p} is prime,5865\kbd{T} a \kbd{ZX}, which is irreducible modulo \kbd{p}, \kbd{S} a5866\kbd{ZX} representing an automorphism of $\F_q := \F_p[X]/(\kbd{T})$.5867($\kbd{S}(X)$ is the image of $X$ by the automorphism.) Returns the5868inverse automorphism of \kbd{S}, in the same format, i.e.~an \kbd{FpX}~$H$5869such that $H(\kbd{S}) \equiv X$ modulo $(\kbd{T}, \kbd{p})$.58705871\fun{long}{FpXQX_nbfact}{GEN S, GEN T, GEN p} returns the number of5872irreducible factors of the polynomial $S$ over the finite field $\F_q$5873defined by $T$ and $p$.58745875\fun{long}{FpXQX_nbfact_Frobenius}{GEN S, GEN Xq, GEN T, GEN p} as5876\kbd{FpXQX\_nbfact} where \kbd{Xq} is \kbd{FpXQX\_Frobenius(S, T, p)}.58775878\fun{long}{FqX_nbfact}{GEN S, GEN T, GEN p} as above but accept \kbd{T=NULL}.58795880\fun{long}{FpXQX_nbroots}{GEN S, GEN T, GEN p} returns the number of roots of5881the polynomial $S$ over the finite field $\F_q$ defined by $T$ and $p$.58825883\fun{long}{FqX_nbroots}{GEN S, GEN T, GEN p} as above but accept \kbd{T=NULL}.58845885\fun{GEN}{FpXQX_Frobenius}{GEN S, GEN T, GEN p} returns5886$X^{q}\pmod{S(X)}$ over the finite field $\F_q$ defined by $T$ and $p$, thus5887$q=p^n$ where $n$ is the degree of $T$.58885889\subsec{\kbd{Flx}} Let \kbd{p} be an \kbd{ulong}, not assumed to be5890prime unless mentioned otherwise (e.g., all functions involving Euclidean5891divisions and factorizations), to be given the function arguments; an5892\kbd{Fl} is an \kbd{ulong} belonging to $[0,\kbd{p}-1]$, an \kbd{Flx}~\kbd{z}5893is a \typ{VECSMALL} representing a polynomial with small integer5894coefficients. Specifically \kbd{z[0]} is the usual codeword, \kbd{z[1] =5895evalvarn($v$)} for some variable $v$, then the coefficients by increasing5896degree. An \kbd{FlxX} is a \typ{POL} whose coefficients are \kbd{Flx}s.58975898\noindent In the following, an argument called \kbd{sv} is of the form5899\kbd{evalvarn}$(v)$ for some variable number~$v$.59005901\subsubsec{Preconditioned reduction}59025903For faster reduction, the modulus \kbd{T} can be replaced by an extended5904modulus (\kbd{FlxT}) in all \kbd{Flxq}-classes functions, and in5905\kbd{Flx\_divrem}.59065907\fun{GEN}{Flx_get_red}{GEN T, ulong p} returns the extended modulus \kbd{eT}.59085909To write code that works both with plain and extended moduli, the following5910accessors are defined:59115912\fun{GEN}{get_Flx_mod}{GEN eT} returns the underlying modulus \kbd{T}.59135914\fun{GEN}{get_Flx_var}{GEN eT} returns the variable number of the modulus.59155916\fun{GEN}{get_Flx_degree}{GEN eT} returns the degree of the modulus.59175918Furthermore, \kbd{ZXT\_to\_FlxT} allows to convert an extended modulus for5919a \kbd{FpX} to an extended modulus for the corresponding \kbd{Flx}.59205921\subsubsec{Basic operations}59225923\fun{ulong}{Flx_lead}{GEN x} returns the leading coefficient of $x$ as a5924\kbd{ulong} (return $0$ for the zero polynomial).59255926\fun{ulong}{Flx_constant}{GEN x} returns the constant coefficient of $x$ as a5927\kbd{ulong} (return $0$ for the zero polynomial).59285929\fun{GEN}{Flx_red}{GEN z, ulong p} converts from \kbd{zx} with5930nonnegative coefficients to \kbd{Flx} (by reducing them mod \kbd{p}).59315932\fun{int}{Flx_equal1}{GEN x} returns 1 (true) if the \kbd{Flx} $x$ is equal5933to~1, 0~(false) otherwise.59345935\fun{int}{Flx_equal}{GEN x, GEN y} returns 1 (true) if the \kbd{Flx} $x$5936and $y$ are equal, and 0~(false) otherwise.59375938\fun{GEN}{Flx_copy}{GEN x} returns a copy of \kbd{x}.59395940\fun{GEN}{Flx_add}{GEN x, GEN y, ulong p}59415942\fun{GEN}{Flx_Fl_add}{GEN y, ulong x, ulong p}59435944\fun{GEN}{Flx_neg}{GEN x, ulong p}59455946\fun{GEN}{Flx_neg_inplace}{GEN x, ulong p}, same as \kbd{Flx\_neg}, in place5947(\kbd{x} is destroyed).59485949\fun{GEN}{Flx_sub}{GEN x, GEN y, ulong p}59505951\fun{GEN}{Flx_Fl_sub}{GEN y, ulong x, ulong p}59525953\fun{GEN}{Flx_halve}{GEN x, ulong p} returns $z$ such that $2\*z = x$ modulo5954$p$ assuming such $z$ exists.59555956\fun{GEN}{Flx_mul}{GEN x, GEN y, ulong p}59575958\fun{GEN}{Flx_Fl_mul}{GEN y, ulong x, ulong p}59595960\fun{GEN}{Flx_double}{GEN y, ulong p} returns $2\*y$.59615962\fun{GEN}{Flx_triple}{GEN y, ulong p} returns $3\*y$.59635964\fun{GEN}{Flx_mulu}{GEN y, ulong x, ulong p} as \kbd{Flx\_Fl\_mul} but do not5965assume that $x<p$.59665967\fun{GEN}{Flx_Fl_mul_to_monic}{GEN y, ulong x, ulong p} returns $y\*x$5968assuming the result is monic of the same degree as $y$ (in particular $x\neq59690$).59705971\fun{GEN}{Flx_sqr}{GEN x, ulong p}59725973\fun{GEN}{Flx_powu}{GEN x, ulong n, ulong p} return $x^n$.59745975\fun{GEN}{Flx_divrem}{GEN x, GEN y, ulong p, GEN *pr},5976here $p$ must be prime.59775978\fun{GEN}{Flx_div}{GEN x, GEN y, ulong p},5979here $p$ must be prime.59805981\fun{GEN}{Flx_rem}{GEN x, GEN y, ulong p},5982here $p$ must be prime.59835984\fun{GEN}{Flx_deriv}{GEN z, ulong p}59855986\fun{GEN}{Flx_integ}{GEN z, ulong p},5987here $p$ must be prime.59885989\fun{GEN}{Flx_translate1}{GEN P, ulong p} return $P(x+1)$, $p$ must be prime.5990Asymptotically fast (quasi-linear in the degree of $P$).59915992\fun{GEN}{Flx_translate1_basecase}{GEN P, ulong p} return $P(x+1)$, $p$5993need not be prime. Not asymptotically fast (quadratic in the degree of $P$).59945995\fun{GEN}{zlx_translate1}{GEN P, ulong p, long e} return $P(x+1)$5996modulo $p^e$ for prime $p$. Asymptotically fast (quasi-linear in the degree of5997$P$).59985999\fun{GEN}{Flx_diff1}{GEN P, ulong p} return $P(x+1)-P(x)$; $p$ must be prime.60006001\fun{GEN}{Flx_digits}{GEN x, GEN B, ulong p} returns a vector of \kbd{Flx}6002$[c_0,\ldots,c_n]$ of degree less than the degree of $B$ and such that6003$x=\sum_{i=0}^{n}{c_i\*B^i}$.60046005\fun{GEN}{FlxV_Flx_fromdigits}{GEN v, GEN B, ulong p} where $v=[c_0,\ldots,c_n]$6006is a vector of \kbd{Flx}, returns $\sum_{i=0}^{n}{c_i\*B^i}$.60076008\fun{GEN}{Flx_Frobenius}{GEN T, ulong p}6009here $p$ must be prime.60106011\fun{GEN}{Flx_matFrobenius}{GEN T, ulong p}6012here $p$ must be prime.60136014\fun{GEN}{Flx_gcd}{GEN a, GEN b, ulong p} returns a (not necessarily monic)6015greatest common divisor of $x$ and $y$. Here $p$ must be prime.60166017\fun{GEN}{Flx_halfgcd}{GEN x, GEN y, ulong p} returns a two-by-two \kbd{FlxM}6018$M$ with determinant $\pm 1$ such that the image $(a,b)$ of $(x,y)$ by $M$6019has the property that $\deg a \geq {\deg x \over 2} > \deg b$.6020Assumes that $p$ is prime.60216022\fun{GEN}{Flx_extgcd}{GEN a, GEN b, ulong p, GEN *ptu, GEN *ptv},6023here $p$ must be prime.60246025\fun{GEN}{Flx_roots}{GEN f, ulong p} returns the vector of roots6026of $f$ (without multiplicity, as a \typ{VECSMALL}). Assumes that $p$ is6027prime.60286029\fun{ulong}{Flx_oneroot}{GEN f, ulong p} returns one root $0 \leq r < p$ of6030the \kbd{Flx}~\kbd{f} in \kbd{\Z/p\Z}. Return $p$ if no root exists.6031Assumes that $p$ is prime.60326033\fun{ulong}{Flx_oneroot_split}{GEN f, ulong p} as \kbd{Flx\_oneroot} but6034assume $f$ is totally split. Assumes that $p$ is prime.60356036\fun{long}{Flx_ispower}{GEN f, ulong k, ulong p, GEN *pt}6037return $1$ if the \kbd{Flx} $f$ is a $k$-th power, $0$ otherwise.6038If \kbd{pt} is not \kbd{NULL}, set it to $g$ such that $g^k = f$.60396040\fun{GEN}{Flx_factor}{GEN f, ulong p} Assumes that $p$ is prime.60416042\fun{GEN}{Flx_ddf}{GEN f, ulong p} Assumes that $p$ is prime.60436044\fun{GEN}{Flx_factor_squarefree}{GEN f, ulong p} returns the squarefree6045factorization of $f$ modulo $p$. This is a vector $[u_1,\dots,u_k]$6046of pairwise coprime \kbd{Flx} such that $u_k \neq 1$ and $f = \prod u_i^i$.6047Shallow function. Assumes that $p$ is prime.60486049\fun{GEN}{Flx_mod_Xn1}{GEN T, ulong n, ulong p} return $T$ modulo6050$(X^n + 1, p)$. Shallow function.60516052\fun{GEN}{Flx_mod_Xnm1}{GEN T, ulong n, ulong p} return $T$ modulo6053$(X^n - 1, p)$. Shallow function.60546055\fun{GEN}{Flx_degfact}{GEN f, ulong p} as \tet{FpX_degfact}. Assumes that $p$6056is prime.60576058\fun{GEN}{Flx_factorff_irred}{GEN P, GEN Q, ulong p} as6059\tet{FpX_factorff_irred}. Assumes that $p$ is prime.60606061\fun{GEN}{Flx_rootsff}{GEN P, GEN T, ulong p} as \tet{FpX_rootsff}.6062Assumes that $p$ is prime.60636064\fun{GEN}{Flx_ffisom}{GEN P,GEN Q,ulong l} as \tet{FpX_ffisom}.6065Assumes that $p$ is prime.60666067\subsubsec{Miscellaneous operations}60686069\fun{GEN}{pol0_Flx}{long sv} returns a zero \kbd{Flx} in variable $v$.60706071\fun{GEN}{zero_Flx}{long sv} alias for \kbd{pol0\_Flx}60726073\fun{GEN}{pol1_Flx}{long sv} returns the unit \kbd{Flx} in variable $v$.60746075\fun{GEN}{polx_Flx}{long sv} returns the variable $v$ as degree~1~\kbd{Flx}.60766077\fun{GEN}{polxn_Flx}{long n, long sv} Returns the monomial of degree $n$ as6078a \kbd{Flx} in variable $v$; assume that $n \geq 0$.60796080\fun{GEN}{monomial_Flx}{ulong a, long d, long sv} returns the \kbd{Flx}6081$a\*X^d$ in variable $v$.60826083\fun{GEN}{init_Flxq}{ulong p, long n, long sv} returns an irreducible6084polynomial of degree $\kbd{n} > 0$ over $\F_p$, in variable \kbd{v}.60856086\fun{GEN}{Flx_normalize}{GEN z, ulong p}, as \kbd{FpX\_normalize}.60876088\fun{GEN}{Flx_rescale}{GEN P, ulong h, ulong p} returns $h^{\deg(P)} P(x/h)$,6089\kbd{P} is a \kbd{Flx} and \kbd{h} is a nonzero integer.60906091\fun{GEN}{random_Flx}{long d, long sv, ulong p} returns a random \kbd{Flx}6092in variable \kbd{v}, of degree less than~\kbd{d}.60936094\fun{GEN}{Flx_recip}{GEN x}, returns the reciprocal polynomial60956096\fun{ulong}{Flx_resultant}{GEN a, GEN b, ulong p}, returns the resultant6097of \kbd{a} and \kbd{b}. Assumes that $p$ is prime.60986099\fun{ulong}{Flx_extresultant}{GEN a, GEN b, ulong p, GEN *ptU, GEN *ptV}6100given two \kbd{Flx} \kbd{a} and \kbd{b},6101returns their resultant and sets Bezout coefficients (if the resultant is 0,6102the latter are not set). Assumes that $p$ is prime.61036104\fun{GEN}{Flx_invBarrett}{GEN T, ulong p}, returns the Barrett inverse6105$M$ of $T$ defined by $M(x)\*x^n\*T(1/x)\equiv 1\pmod{x^{n-1}}$ where $n$ is6106the degree of $T$. Assumes that $p$ is prime.61076108\fun{GEN}{Flx_renormalize}{GEN x, long l}, as \kbd{FpX\_renormalize}, where6109$\kbd{l} = \kbd{lg(x)}$, in place.61106111\fun{GEN}{Flx_shift}{GEN T, long n} returns $\kbd{T} * x^n$ if $n\geq 0$,6112and $\kbd{T} \bs x^{-n}$ otherwise.61136114\fun{long}{Flx_val}{GEN x} returns the valuation of \kbd{x}, i.e. the6115multiplicity of the $0$ root.61166117\fun{long}{Flx_valrem}{GEN x, GEN *Z} as \kbd{RgX\_valrem}, returns the6118valuation of \kbd{x}. In particular, if the valuation is $0$, set \kbd{*Z}6119to $x$, not a copy.61206121\fun{GEN}{Flx_div_by_X_x}{GEN A, ulong a, ulong p, ulong *rem}, returns the6122Euclidean quotient of the \kbd{Flx}~\kbd{A} by $X - \kbd{a}$, and sets6123\kbd{rem} to the remainder $ \kbd{A}(\kbd{a})$.61246125\fun{ulong}{Flx_eval}{GEN x, ulong y, ulong p}, as \kbd{FpX\_eval}.61266127\fun{ulong}{Flx_eval_pre}{GEN x, ulong y, ulong p, ulong pi}, as \kbd{Flx\_eval},6128assuming $pi$ is the pseudo inverse of $p$.61296130\fun{ulong}{Flx_eval_powers_pre}{GEN P, GEN y, ulong p, ulong pi}. Let $y$ be6131the \typ{VECSMALL} $(1,a,\dots,a^n)$, where $n$ is the degree of the6132\kbd{Flx} $P$, return $P(a)$, assuming $pi$ is the pseudo inverse of $p$.61336134\fun{GEN}{Flx_Flv_multieval}{GEN P, GEN v, ulong p} returns the vector6135$[P(v[1]),\ldots,P(v[n])]$ as a \kbd{Flv}.61366137\fun{ulong}{Flx_dotproduct}{GEN x, GEN y, ulong p} returns the scalar product6138of the coefficients of $x$ and $y$.61396140\fun{GEN}{Flx_deflate}{GEN P, long d} assuming $P$ is a polynomial of the6141form $Q(X^d)$, return $Q$.61426143\fun{GEN}{Flx_inflate}{GEN P, long d} returns $P(X^d)$.61446145\fun{GEN}{Flx_splitting}{GEN P, long k}, as \tet{RgX_splitting}.61466147\fun{GEN}{Flx_blocks}{GEN P, long n, long m}, as \tet{RgX_blocks}.61486149\fun{int}{Flx_is_squarefree}{GEN z, ulong p}. Assumes that $p$ is prime.61506151\fun{int}{Flx_is_irred}{GEN f, ulong p}, as \kbd{FpX\_is\_irred}.6152Assumes that $p$ is prime.61536154\fun{int}{Flx_is_totally_split}{GEN f, ulong p} returns $1$ if the6155\kbd{Flx}~\kbd{f} splits into a product of distinct linear factors, $0$6156otherwise. Assumes that \kbd{p} is prime.61576158\fun{int}{Flx_is_smooth}{GEN f, long r, ulong p} return $1$ if all6159irreducible factors of $f$ are of degree at most $r$, $0$ otherwise.6160Assumes that $p$ is prime.61616162\fun{long}{Flx_nbroots}{GEN f, ulong p}, as \kbd{FpX\_nbroots}.6163Assumes that $p$ is prime.61646165\fun{long}{Flx_nbfact}{GEN z, ulong p}, as \kbd{FpX\_nbfact}.6166Assumes that $p$ is prime.61676168\fun{long}{Flx_nbfact_Frobenius}{GEN f, GEN XP, ulong p},6169as \kbd{FpX\_nbfact\_Frobenius}. Assumes that $p$ is prime.61706171\fun{GEN}{Flx_degfact}{GEN f, ulong p}, as \kbd{FpX\_degfact}.6172Assumes that $p$ is prime.61736174\fun{GEN}{Flx_nbfact_by_degree}{GEN z, long *nb, ulong p} Assume6175that the \kbd{Flx} $z$ is squarefree mod the prime $p$. Returns a6176\typ{VECSMALL} $D$ with $\deg z$ entries, such that $D[i]$ is the number of6177irreducible factors of degree $i$. Set \kbd{nb} to the total number of6178irreducible factors (the sum of the $D[i]$).6179Assumes that $p$ is prime.61806181\fun{void}{Flx_ffintersect}{GEN P,GEN Q, long n, ulong p, GEN*SP, GEN*SQ, GEN6182MA,GEN MB},\hfil\break6183as \kbd{FpX\_ffintersect}. Assumes that $p$ is prime.61846185\fun{GEN}{Flx_Laplace}{GEN x, ulong p}61866187\fun{GEN}{Flx_invLaplace}{GEN x, ulong p}61886189\fun{GEN}{Flx_Newton}{GEN x, long n, ulong p}61906191\fun{GEN}{Flx_fromNewton}{GEN x, ulong p}61926193\fun{GEN}{Flx_Teichmuller}{GEN P, ulong p, long n}6194Return a \kbd{ZX} $Q$ such that $P\equiv Q\pmod{p}$ and6195$Q(X^p)=0\pmod{Q,p^n}$. Assumes that $p$ is prime.61966197\fun{GEN}{Flv_polint}{GEN x, GEN y, ulong p, long sv} as \kbd{FpV\_polint},6198returning an \kbd{Flx} in variable $v$. Assumes that $p$ is prime.61996200\fun{GEN}{Flv_Flm_polint}{GEN x, GEN V, ulong p, long sv} equivalent (but6201faster) to applying \kbd{Flv\_polint(x,$\ldots$)} to all the elements of the6202vector $V$ (thus, returns a \kbd{FlxV}). Assumes that $p$ is prime.62036204\fun{GEN}{Flv_invVandermonde}{GEN L, ulong d, ulong p} $L$ being a \kbd{Flv}6205of length $n$, return the inverse $M$ of the Vandermonde matrix attached to6206the elements of $L$, multiplied by \kbd{d}.6207If $A$ is a \kbd{Flv} and $B=M\*A$, then the polynomial6208$P=\sum_{i=1}^n B[i]\*X^{i-1}$ verifies $P(L[i])=d\*A[i]$ for6209$1 \leq i \leq n$. Assumes that $p$ is prime.62106211\fun{GEN}{Flv_roots_to_pol}{GEN a, ulong p, long sv} as6212\kbd{FpV\_roots\_to\_pol} returning an \kbd{Flx} in variable $v$.62136214\subsec{\kbd{FlxV}} See \kbd{FpXV} operations.62156216\fun{GEN}{FlxV_Flc_mul}{GEN V, GEN W, ulong p}, as \kbd{FpXV\_FpC\_mul}.62176218\fun{GEN}{FlxV_red}{GEN V, ulong p} reduces each components with \kbd{Flx\_red}.62196220\fun{GEN}{FlxV_prod}{GEN V, ulong p}, \kbd{V} being a vector of \kbd{Flx},6221returns their product.62226223\fun{ulong}{FlxC_eval_powers_pre}{GEN x, GEN y, ulong p, ulong pi}6224apply \kbd{Flx\_eval\_powers\_pre} to all elements of \kbd{x}.62256226\fun{GEN}{FlxV_Flv_multieval}{GEN F, GEN v, ulong p} returns the vector6227$[[F[1](v[1]),\ldots,F[1](v[n])],\ldots,[F[m](v[1]),\ldots,F[m](v[n])]]$6228as a \kbd{FlvV}.62296230\fun{GEN}{FlxC_neg}{GEN x, ulong p}62316232\fun{GEN}{FlxC_sub}{GEN x, GEN y, ulong p}62336234\fun{GEN}{zero_FlxC}{long n, long sv}62356236\subsec{\kbd{FlxM}} See \kbd{FpXM} operations.62376238\fun{ulong}{FlxM_eval_powers_pre}{GEN M, GEN y, ulong p, ulong pi}6239this function applies \kbd{FlxC\_eval\_powers\_pre} to all entries of \kbd{M}.62406241\fun{GEN}{FlxM_neg}{GEN x, ulong p}62426243\fun{GEN}{FlxM_sub}{GEN x, GEN y, ulong p}62446245\fun{GEN}{zero_FlxM}{long r, long c, long sv}62466247\subsec{\kbd{FlxT}} See \kbd{FpXT} operations.62486249\fun{GEN}{FlxT_red}{GEN V, ulong p} reduces each leaf with \kbd{Flx\_red}.62506251\subsec{\kbd{Flxn}} See \kbd{FpXn} operations.62526253\fun{GEN}{Flxn_inv}{GEN a, long n, ulong p}6254returns $1/a$ modulo $X^n$.62556256\fun{GEN}{Flxn_mul}{GEN a, GEN b, long n, ulong p}6257returns $a\*b$ modulo $X^n$.62586259\fun{GEN}{Flxn_sqr}{GEN a, long n, ulong p}6260returns $a^2$ modulo $X^n$.62616262\fun{GEN}{Flxn_red}{GEN a, long n}6263returns $a$ modulo $X^n$.62646265\fun{GEN}{Flxn_exp}{GEN x, long n, ulong p} return $\exp(x)$6266as a composition of formal power series.6267It is required that the valuation of $x$ is positive and that $p>n$.62686269\fun{GEN}{Flxn_expint}{GEN f, long n, ulong p} return $\exp(F)$6270where $F$ is the primitive of $f$ that vanishes at $0$.6271It is required that $p>n$.62726273\subsec{\kbd{Flxq}} See \kbd{FpXQ} operations.62746275\fun{GEN}{Flxq_add}{GEN x, GEN y, GEN T, ulong p}62766277\fun{GEN}{Flxq_sub}{GEN x, GEN y, GEN T, ulong p}62786279\fun{GEN}{Flxq_mul}{GEN x, GEN y, GEN T, ulong p}62806281\fun{GEN}{Flxq_sqr}{GEN y, GEN T, ulong p}62826283\fun{GEN}{Flxq_inv}{GEN x, GEN T, ulong p}62846285\fun{GEN}{Flxq_invsafe}{GEN x, GEN T, ulong p}62866287\fun{GEN}{Flxq_div}{GEN x, GEN y, GEN T, ulong p}62886289\fun{GEN}{Flxq_pow}{GEN x, GEN n, GEN T, ulong p}62906291\fun{GEN}{Flxq_powu}{GEN x, ulong n, GEN T, ulong p}62926293\fun{GEN}{FlxqV_factorback}{GEN L, GEN e, GEN Tp, ulong p}62946295\fun{GEN}{Flxq_pow_init}{GEN x, GEN n, long k, GEN T, ulong p}62966297\fun{GEN}{Flxq_pow_table}{GEN R, GEN n, GEN T, ulong p}62986299\fun{GEN}{Flxq_powers}{GEN x, long n, GEN T, ulong p}63006301\fun{GEN}{Flxq_matrix_pow}{GEN x, long m, long n, GEN T, ulong p},6302see \kbd{FpXQ\_matrix\_pow}.63036304\fun{GEN}{Flxq_autpow}{GEN a, long n, GEN T, ulong p}6305see \kbd{FpXQ\_autpow}.63066307\fun{GEN}{Flxq_autpowers}{GEN a, long n, GEN T, ulong p}6308return $[X,\sigma(X),\ldots,\sigma^n(X)]$,6309assuming $a=\sigma(X)$ where $\sigma$ is an automorphism of the algebra6310$\F_p[X]/T(X)$.63116312\fun{GEN}{Flxq_autsum}{GEN a, long n, GEN T, ulong p}6313see \kbd{FpXQ\_autsum}.63146315\fun{GEN}{Flxq_auttrace}{GEN a, ulong n, GEN T, ulong p}6316see \kbd{FpXQ\_auttrace}.63176318\fun{GEN}{Flxq_ffisom_inv}{GEN S, GEN T, ulong p}, as \kbd{FpXQ\_ffisom\_inv}.63196320\fun{GEN}{Flx_Flxq_eval}{GEN f, GEN x, GEN T, ulong p} returns6321$\kbd{f}(\kbd{x})$.63226323\fun{GEN}{Flx_FlxqV_eval}{GEN f, GEN x, GEN T, ulong p},6324see \kbd{FpX\_FpXQV\_eval}.63256326\fun{GEN}{FlxC_Flxq_eval}{GEN C, GEN x, GEN T, ulong p},6327see \kbd{FpXC\_FpXQ\_eval}.63286329\fun{GEN}{FlxC_FlxqV_eval}{GEN C, GEN V, GEN T, ulong p}6330see \kbd{FpXC\_FpXQV\_eval}.63316332\fun{GEN}{FlxqV_roots_to_pol}{GEN V, GEN T, ulong p, long v} as6333\kbd{FqV\_roots\_to\_pol} returning an \kbd{FlxqX} in variable $v$.63346335\fun{int}{Flxq_issquare}{GEN x, GEN T, ulong p} returns $1$ if $x$ is a square6336and $0$ otherwise. Assume that \kbd{T} is irreducible mod \kbd{p}.63376338\fun{int}{Flxq_is2npower}{GEN x, long n, GEN T, ulong p} returns $1$ if $x$ is6339a $2^n$-th power and $0$ otherwise. Assume that \kbd{T} is irreducible mod6340\kbd{p}.63416342\fun{GEN}{Flxq_order}{GEN a, GEN ord, GEN T, ulong p}6343as \tet{FpXQ_order}.63446345\fun{GEN}{Flxq_log}{GEN a, GEN g, GEN ord, GEN T, ulong p}6346as \tet{FpXQ_log}63476348\fun{GEN}{Flxq_sqrtn}{GEN x, GEN n, GEN T, ulong p, GEN *zn} as6349\tet{FpXQ_sqrtn}.63506351\fun{GEN}{Flxq_sqrt}{GEN x, GEN T, ulong p} returns a square root of \kbd{x}.6352Return \kbd{NULL} if \kbd{x} is not a square.63536354\fun{GEN}{Flxq_lroot}{GEN a, GEN T, ulong p} returns $x$ such that $x^p = a$.63556356\fun{GEN}{Flxq_lroot_fast}{GEN a, GEN V, GEN T, ulong p} assuming that6357\kbd{V=Flxq\_powers(s,p-1,T,p)} where $s(x)^p \equiv x\pmod{T(x),p}$,6358returns $b$ such that $b^p=a$. Only useful if $p$ is less than the degree of6359$T$.63606361\fun{GEN}{Flxq_charpoly}{GEN x, GEN T, ulong p} returns the characteristic6362polynomial of \kbd{x}63636364\fun{GEN}{Flxq_minpoly}{GEN x, GEN T, ulong p} returns the minimal polynomial6365of \kbd{x}63666367\fun{ulong}{Flxq_norm}{GEN x, GEN T, ulong p} returns the norm of \kbd{x}63686369\fun{ulong}{Flxq_trace}{GEN x, GEN T, ulong p} returns the trace of \kbd{x}63706371\fun{GEN}{Flxq_conjvec}{GEN x, GEN T, ulong p} returns the conjugates6372$[x,x^p,x^{p^2},\ldots,x^{p^{n-1}}]$ where $n$ is the degree of $T$.63736374\fun{GEN}{gener_Flxq}{GEN T, ulong p, GEN *po} returns a primitive root modulo6375$(T,p)$. $T$ is an \kbd{Flx} assumed to be irreducible modulo the prime6376$p$. If \kbd{po} is not \kbd{NULL} it is set to $[o,\var{fa}]$, where $o$ is the6377order of the multiplicative group of the finite field, and \var{fa} is6378its factorization.63796380\subsec{\kbd{FlxX}} See \kbd{FpXX} operations.63816382\fun{GEN}{pol1_FlxX}{long vX, long sx} returns the unit \kbd{FlxX} as a6383\typ{POL} in variable \kbd{vX} which only coefficient is \kbd{pol1\_Flx(sx)}.63846385\fun{GEN}{polx_FlxX}{long vX, long sx} returns the variable $X$ as a6386degree~1~\typ{POL} with \kbd{Flx} coefficients in the variable $x$.63876388\fun{long}{FlxY_degreex}{GEN P} return the degree of $P$ with respect to6389the secondary variable.63906391\fun{GEN}{FlxX_add}{GEN P, GEN Q, ulong p}63926393\fun{GEN}{FlxX_sub}{GEN P, GEN Q, ulong p}63946395\fun{GEN}{FlxX_Fl_mul}{GEN x, ulong y, ulong p}63966397\fun{GEN}{FlxX_double}{GEN x, ulong p}63986399\fun{GEN}{FlxX_triple}{GEN x, ulong p}64006401\fun{GEN}{FlxX_neg}{GEN x, ulong p}64026403\fun{GEN}{FlxX_Flx_add}{GEN x, GEN y, ulong p}64046405\fun{GEN}{FlxX_Flx_sub}{GEN x, GEN y, ulong p}64066407\fun{GEN}{FlxX_Flx_mul}{GEN x, GEN y, ulong p}64086409\fun{GEN}{FlxY_Flx_div}{GEN x, GEN y, ulong p} divides the coefficients of $x$6410by $y$ using \kbd{Flx\_div}.64116412\fun{GEN}{FlxX_deriv}{GEN P, ulong p} returns the derivative of \kbd{P} with6413respect to the main variable.64146415\fun{GEN}{FlxX_Laplace}{GEN x, ulong p}64166417\fun{GEN}{FlxX_invLaplace}{GEN x, ulong p}64186419\fun{GEN}{FlxY_evalx}{GEN P, ulong z, ulong p} $P$ being an \kbd{FlxY}, returns6420the \kbd{Flx} $P(z,Y)$, where $Y$ is the main variable of $P$.64216422\fun{GEN}{FlxX_translate1}{GEN P, ulong p, long n} $P$ being an \kbd{FlxX} with6423all coefficients of degree at most $n$, return $(P(x,Y+1)$, where $Y$ is the6424main variable of $P$.64256426\fun{GEN}{zlxX_translate1}{GEN P, ulong p, long e, long n}6427$P$ being an \kbd{zlxX} with all coefficients of degree at most $n$, return6428$(P(x,Y+1)$ modulo $p^e$ for prime $p$ , where $Y$ is the main variable of6429$P$.64306431\fun{GEN}{FlxY_Flx_translate}{GEN P, GEN f, ulong p} $P$ being an \kbd{FlxY} and $f$6432being an \kbd{Flx}, return $(P(x,Y+f(x))$, where $Y$ is the main variable of $P$.64336434\fun{ulong}{FlxY_evalx_powers_pre}{GEN P, GEN xp, ulong p, ulong pi}, \kbd{xp}6435being the vector $[1,x,\dots,x^n]$, where $n$ is larger or equal to the degree6436of $P$ in $X$, return $P(x,Y)$, where $Y$ is the main variable of $Q$, assuming6437$pi$ is the pseudo inverse of $p$.64386439\fun{ulong}{FlxY_eval_powers_pre}{GEN P, GEN xp, GEN yp, ulong p, ulong pi},6440\kbd{xp} being the vector $[1,x,\dots,x^n]$, where $n$ is larger or equal to the degree6441of $P$ in $X$ and \kbd{yp} being the vector $[1,y,\dots,y^m]$, where $m$ is larger or equal to the degree of $P$ in $Y$ return $P(x,y)$, assuming6442$pi$ is the pseudo inverse of $p$.64436444\fun{GEN}{FlxY_Flxq_evalx}{GEN x, GEN y, GEN T, ulong p} as \kbd{FpXY\_FpXQ\_evalx}.64456446\fun{GEN}{FlxY_FlxqV_evalx}{GEN x, GEN V, GEN T, ulong p} as \kbd{FpXY\_FpXQV\_evalx}.64476448\fun{GEN}{FlxX_renormalize}{GEN x, long l}, as \kbd{normalizepol}, where6449$\kbd{l} = \kbd{lg(x)}$, in place.64506451\fun{GEN}{FlxX_resultant}{GEN u, GEN v, ulong p, long sv} Returns6452$\text{Res}_X(u, v)$, which is an \kbd{Flx}. The coefficients of \kbd{u}6453and \kbd{v} are assumed to be in the variable $v$.64546455\fun{GEN}{Flx_FlxY_resultant}{GEN a, GEN b, ulong p}6456Returns $\text{Res}_x(a, b)$, which is an \kbd{Flx}6457in the main variable of \kbd{b}.64586459\fun{GEN}{FlxX_blocks}{GEN P, long n, long m, long sv}, as \tet{RgX_blocks},6460where $v$ is the secondary variable.64616462\fun{GEN}{FlxX_shift}{GEN a, long n, long sv}, as \kbd{RgX\_shift\_shallow},6463where $v$ is the secondary variable.64646465\fun{GEN}{FlxX_swap}{GEN x, long n, long ws}, as \kbd{RgXY\_swap}.64666467\fun{GEN}{FlxYqq_pow}{GEN x, GEN n, GEN S, GEN T, ulong p}, as6468\kbd{FpXYQQ\_pow}.64696470\subsec{\kbd{FlxqX}} See \kbd{FpXQX} operations.64716472\subsubsec{Preconditioned reduction}64736474For faster reduction, the modulus \kbd{S} can be replaced by an extended6475modulus, which is an \kbd{FlxqXT}, in all \kbd{FlxqXQ}-classes6476functions, and in \kbd{FlxqX\_rem} and \kbd{FlxqX\_divrem}.64776478\fun{GEN}{FlxqX_get_red}{GEN S, GEN T, ulong p} returns the extended modulus6479\kbd{eS}.64806481To write code that works both with plain and extended moduli, the following6482accessors are defined:64836484\fun{GEN}{get_FlxqX_mod}{GEN eS} returns the underlying modulus \kbd{S}.64856486\fun{GEN}{get_FlxqX_var}{GEN eS} returns the variable number of the modulus.64876488\fun{GEN}{get_FlxqX_degree}{GEN eS} returns the degree of the modulus.64896490\subsubsec{basic functions}64916492\fun{GEN}{random_FlxqX}{long d, long v, GEN T, ulong p} returns a random6493\kbd{FlxqX} in variable \kbd{v}, of degree less than~\kbd{d}.64946495\fun{GEN}{zxX_to_Kronecker}{GEN P, GEN Q} assuming $P(X,Y)$ is a polynomial6496of degree in $X$ strictly less than $n$, returns $P(X,X^{2*n-1})$, the6497Kronecker form of $P$.64986499\fun{GEN}{Kronecker_to_FlxqX}{GEN z, GEN T, ulong p}. Let $n = \deg T$ and let6500$P(X,Y)\in \Z[X,Y]$ lift a polynomial in $K[Y]$, where $K := \F_p[X]/(T)$ and6501$\deg_X P < 2n-1$ --- such as would result from multiplying minimal degree6502lifts of two polynomials in $K[Y]$. Let $z = P(t,t^{2*n-1})$ be a Kronecker6503form of $P$, this function returns $Q\in \Z[X,t]$ such that $Q$ is congruent to6504$P(X,t)$ mod $(p, T(X))$, $\deg_X Q < n$, and all coefficients are in $[0,p[$.6505Not stack-clean. Note that $t$ need not be the same variable as $Y$!65066507\fun{GEN}{FlxqX_red}{GEN z, GEN T, ulong p}65086509\fun{GEN}{FlxqX_normalize}{GEN z, GEN T, ulong p}65106511\fun{GEN}{FlxqX_mul}{GEN x, GEN y, GEN T, ulong p}65126513\fun{GEN}{FlxqX_Flxq_mul}{GEN P, GEN U, GEN T, ulong p}65146515\fun{GEN}{FlxqX_Flxq_mul_to_monic}{GEN P, GEN U, GEN T, ulong p}6516returns $P*U$ assuming the result is monic of the same degree as $P$ (in6517particular $U\neq 0$).65186519\fun{GEN}{FlxqX_sqr}{GEN x, GEN T, ulong p}65206521\fun{GEN}{FlxqX_powu}{GEN x, ulong n, GEN T, ulong p}65226523\fun{GEN}{FlxqX_divrem}{GEN x, GEN y, GEN T, ulong p, GEN *pr}65246525\fun{GEN}{FlxqX_div}{GEN x, GEN y, GEN T, ulong p}65266527\fun{GEN}{FlxqX_rem}{GEN x, GEN y, GEN T, ulong p}65286529\fun{GEN}{FlxqX_invBarrett}{GEN T, GEN Q, ulong p}65306531\fun{GEN}{FlxqX_gcd}{GEN x, GEN y, ulong p} returns a (not necessarily monic)6532greatest common divisor of $x$ and $y$.65336534\fun{GEN}{FlxqX_extgcd}{GEN x, GEN y, GEN T, ulong p, GEN *ptu, GEN *ptv}65356536\fun{GEN}{FlxqX_halfgcd}{GEN x, GEN y, GEN T, ulong p}, see \kbd{FpX\_halfgcd}.65376538\fun{GEN}{FlxqX_resultant}{GEN x, GEN y, GEN T, ulong p}65396540\fun{GEN}{FlxqX_saferesultant}{GEN P, GEN Q, GEN T, ulong p} Returns the6541resultant of $P$ and $Q$ if Euclid's algorithm succeeds and \kbd{NULL} otherwise.6542In particular, if $p$ is not prime or $T$ is not irreducible over $\F_p[X]$, the6543routine may still be used (but will fail if noninvertible leading terms6544occur).65456546\fun{GEN}{FlxqX_disc}{GEN x, GEN T, ulong p}65476548\fun{GEN}{FlxqXV_prod}{GEN V, GEN T, ulong p}65496550\fun{GEN}{FlxqX_safegcd}{GEN P, GEN Q, GEN T, ulong p} Returns the \emph{monic}6551GCD of $P$ and $Q$ if Euclid's algorithm succeeds and \kbd{NULL} otherwise. In6552particular, if $p$ is not prime or $T$ is not irreducible over $\F_p[X]$, the6553routine may still be used (but will fail if noninvertible leading terms6554occur).65556556\fun{GEN}{FlxqX_dotproduct}{GEN x, GEN y, GEN T, ulong p} returns the scalar6557product of the coefficients of $x$ and $y$.65586559\fun{GEN}{FlxqX_Newton}{GEN x, long n, GEN T, ulong p}65606561\fun{GEN}{FlxqX_fromNewton}{GEN x, GEN T, ulong p}65626563\fun{long}{FlxqX_is_squarefree}{GEN S, GEN T, ulong p}, as6564\kbd{FpX\_is\_squarefree}.65656566\fun{long}{FlxqX_ispower}{GEN f, ulong k, GEN T, ulong p, GEN *pt}6567return $1$ if the \kbd{FlxqX} $f$ is a $k$-th power, $0$ otherwise.6568If \kbd{pt} is not \kbd{NULL}, set it to $g$ such that $g^k = f$.65696570\fun{GEN}{FlxqX_Frobenius}{GEN S, GEN T, ulong p}, as \kbd{FpXQX\_Frobenius}65716572\fun{GEN}{FlxqX_roots}{GEN f, GEN T, ulong p} return the roots of \kbd{f} in6573$\F_p[X]/(T)$. Assumes \kbd{p} is prime and \kbd{T} irreducible in $\F_p[X]$.65746575\fun{GEN}{FlxqX_factor}{GEN f, GEN T, ulong p} return the factorization of6576\kbd{f} over $\F_p[X]/(T)$. Assumes \kbd{p} is prime and \kbd{T} irreducible6577in $\F_p[X]$.65786579\fun{GEN}{FlxqX_factor_squarefree}{GEN f, GEN T, ulong p} returns the squarefree6580factorization of $f$, see \kbd{FpX\_factor\_squarefree}.65816582\fun{GEN}{FlxqX_ddf}{GEN f, GEN T, ulong p} as \kbd{FpX\_ddf}.65836584\fun{long}{FlxqX_ddf_degree}{GEN f, GEN XP, GEN T, GEN p},6585as \kbd{FpX\_ddf\_degree}.65866587\fun{GEN}{FlxqX_degfact}{GEN f, GEN T, ulong p}, as \kbd{FpX\_degfact}.65886589\fun{long}{FlxqX_nbroots}{GEN S, GEN T, ulong p}, as \kbd{FpX\_nbroots}.65906591\fun{long}{FlxqX_nbfact}{GEN S, GEN T, ulong p}, as \kbd{FpX\_nbfact}.65926593\fun{long}{FlxqX_nbfact_Frobenius}{GEN S, GEN Xq, GEN T, ulong p},6594as \kbd{FpX\_nbfact\_Frobenius}.65956596\fun{GEN}{FlxqX_FlxqXQ_eval}{GEN Q, GEN x, GEN S, GEN T, ulong p} as6597\kbd{FpX\_FpXQ\_eval}.65986599\fun{GEN}{FlxqX_FlxqXQV_eval}{GEN P, GEN V, GEN S, GEN T, ulong p} as6600\kbd{FpX\_FpXQV\_eval}.66016602\subsec{\kbd{FlxqXQ}} See \kbd{FpXQXQ} operations.66036604\fun{GEN}{FlxqXQ_mul}{GEN x, GEN y, GEN S, GEN T, ulong p}66056606\fun{GEN}{FlxqXQ_sqr}{GEN x, GEN S, GEN T, ulong p}66076608\fun{GEN}{FlxqXQ_inv}{GEN x, GEN S, GEN T, ulong p}66096610\fun{GEN}{FlxqXQ_invsafe}{GEN x, GEN S, GEN T, ulong p}66116612\fun{GEN}{FlxqXQ_div}{GEN x, GEN y, GEN S, GEN T, ulong p}66136614\fun{GEN}{FlxqXQ_pow}{GEN x, GEN n, GEN S, GEN T, ulong p}66156616\fun{GEN}{FlxqXQ_powu}{GEN x, ulong n, GEN S, GEN T, ulong p}66176618\fun{GEN}{FlxqXQ_powers}{GEN x, long n, GEN S, GEN T, ulong p}66196620\fun{GEN}{FlxqXQ_matrix_pow}{GEN x, long n, long m, GEN S, GEN T, ulong p}66216622\fun{GEN}{FlxqXQ_autpow}{GEN a, long n, GEN S, GEN T, ulong p}6623as \kbd{FpXQXQ\_autpow}66246625\fun{GEN}{FlxqXQ_autsum}{GEN a, long n, GEN S, GEN T, ulong p}6626as \kbd{FpXQXQ\_autsum}66276628\fun{GEN}{FlxqXQ_auttrace}{GEN a, long n, GEN S, GEN T, ulong p}6629as \kbd{FpXQXQ\_auttrace}66306631\fun{GEN}{FlxqXQ_halfFrobenius}{GEN A, GEN S, GEN T, ulong p}, as6632\kbd{FpXQXQ\_halfFrobenius}66336634\fun{GEN}{FlxqXQ_minpoly}{GEN x, GEN S, GEN T, ulong p}, as6635\kbd{FpXQ\_minpoly}66366637\subsec{\kbd{FlxqXn}} See \kbd{FpXn} operations.66386639\fun{GEN}{FlxXn_red}{GEN a, long n} returns $a$ modulo $X^n$.66406641\fun{GEN}{FlxqXn_mul}{GEN a, GEN b, long n, GEN T, ulong p}66426643\fun{GEN}{FlxqXn_sqr}{GEN a, long n, GEN T, ulong p}66446645\fun{GEN}{FlxqXn_inv}{GEN a, long n, GEN T, ulong p}66466647\fun{GEN}{FlxqXn_expint}{GEN a, long n, GEN T, ulong p}66486649\subsec{\kbd{F2x}} An \kbd{F2x}~\kbd{z} is a \typ{VECSMALL}6650representing a polynomial over $\F_2[X]$. Specifically6651\kbd{z[0]} is the usual codeword, \kbd{z[1] = evalvarn($v$)} for some6652variable $v$ and the coefficients are given by the bits of remaining6653words by increasing degree.66546655\subsubsec{Preconditioned reduction}66566657For faster reduction, the modulus \kbd{T} can be replaced by an extended6658modulus (\kbd{FlxT}) in all \kbd{Flxq}-classes functions, and in6659\kbd{Flx\_divrem}.66606661\fun{GEN}{F2x_get_red}{GEN T} returns the extended modulus \kbd{eT}.66626663To write code that works both with plain and extended moduli, the following6664accessors are defined:66656666\fun{GEN}{get_F2x_mod}{GEN eT} returns the underlying modulus \kbd{T}.66676668\fun{GEN}{get_F2x_var}{GEN eT} returns the variable number of the modulus.66696670\fun{GEN}{get_F2x_degree}{GEN eT} returns the degree of the modulus.66716672\subsubsec{Basic operations}66736674\fun{ulong}{F2x_coeff}{GEN x, long i} returns the coefficient $i\ge 0$ of $x$.66756676\fun{void}{F2x_clear}{GEN x, long i} sets the coefficient $i\ge 0$ of $x$ to6677$0$.66786679\fun{void}{F2x_flip}{GEN x, long i} adds $1$ to the coefficient $i\ge 0$ of $x$.66806681\fun{void}{F2x_set}{GEN x, long i} sets the coefficient $i\ge 0$ of $x$ to $1$.66826683\fun{GEN}{F2x_copy}{GEN x}66846685\fun{GEN}{Flx_to_F2x}{GEN x}66866687\fun{GEN}{Z_to_F2x}{GEN x, long sv}66886689\fun{GEN}{ZX_to_F2x}{GEN x}66906691\fun{GEN}{F2v_to_F2x}{GEN x, long sv}66926693\fun{GEN}{F2x_to_Flx}{GEN x}66946695\fun{GEN}{F2x_to_F2xX}{GEN x, long sv}66966697\fun{GEN}{F2x_to_ZX}{GEN x}66986699\fun{GEN}{pol0_F2x}{long sv} returns a zero \kbd{F2x} in variable $v$.67006701\fun{GEN}{zero_F2x}{long sv} alias for \kbd{pol0\_F2x}.67026703\fun{GEN}{pol1_F2x}{long sv} returns the \kbd{F2x} in variable $v$ constant to6704$1$.67056706\fun{GEN}{polx_F2x}{long sv} returns the variable $v$ as degree~1~\kbd{F2x}.67076708\fun{GEN}{monomial_F2x}{long d, long sv} returns the \kbd{F2x}6709$X^d$ in variable $v$.67106711\fun{GEN}{random_F2x}{long d, long sv} returns a random \kbd{F2x}6712in variable \kbd{v}, of degree less than~\kbd{d}.67136714\fun{long}{F2x_degree}{GEN x} returns the degree of the \kbd{F2x x}. The6715degree of $0$ is defined as $-1$.67166717\fun{GEN}{F2x_recip}{GEN x}67186719\fun{int}{F2x_equal1}{GEN x}67206721\fun{int}{F2x_equal}{GEN x, GEN y}67226723\fun{GEN}{F2x_1_add}{GEN y} returns \kbd{y+1} where \kbd{y} is a \kbd{Flx}.67246725\fun{GEN}{F2x_add}{GEN x, GEN y}67266727\fun{GEN}{F2x_mul}{GEN x, GEN y}67286729\fun{GEN}{F2x_sqr}{GEN x}67306731\fun{GEN}{F2x_divrem}{GEN x, GEN y, GEN *pr}67326733\fun{GEN}{F2x_rem}{GEN x, GEN y}67346735\fun{GEN}{F2x_div}{GEN x, GEN y}67366737\fun{GEN}{F2x_renormalize}{GEN x, long lx}67386739\fun{GEN}{F2x_deriv}{GEN x}67406741\fun{GEN}{F2x_deflate}{GEN x, long d}67426743\fun{ulong}{F2x_eval}{GEN P, ulong u} returns $P(u)$.67446745\fun{void}{F2x_shift}{GEN x, long d} as \tet{RgX_shift}67466747\fun{void}{F2x_even_odd}{GEN P, GEN *pe, GEN *po} as \tet{RgX_even_odd}67486749\fun{long}{F2x_valrem}{GEN x, GEN *Z}67506751\fun{GEN}{F2x_extgcd}{GEN a, GEN b, GEN *ptu, GEN *ptv}67526753\fun{GEN}{F2x_gcd}{GEN a, GEN b}67546755\fun{GEN}{F2x_halfgcd}{GEN a, GEN b}67566757\fun{int}{F2x_issquare}{GEN x} returns $1$ if $x$ is a square of a \kbd{F2x}6758and $0$ otherwise.67596760\fun{int}{F2x_is_irred}{GEN f}, as \tet{FpX_is_irred}.67616762\fun{GEN}{F2x_degfact}{GEN f} as \tet{FpX_degfact}.67636764\fun{GEN}{F2x_sqrt}{GEN x} returns the squareroot of $x$, assuming $x$ is a6765square of a \kbd{F2x}.67666767\fun{GEN}{F2x_Frobenius}{GEN T}67686769\fun{GEN}{F2x_matFrobenius}{GEN T}67706771\fun{GEN}{F2x_factor}{GEN f}67726773\fun{GEN}{F2x_factor_squarefree}{GEN f}67746775\fun{GEN}{F2x_ddf}{GEN f}67766777\fun{GEN}{F2x_Teichmuller}{GEN P, long n}6778Return a \kbd{ZX} $Q$ such that $P\equiv Q\pmod{2}$ and6779$Q(X^p)=0\pmod{Q,2^n}$.67806781\subsec{\kbd{F2xq}} See \kbd{FpXQ} operations.67826783\fun{GEN}{F2xq_mul}{GEN x, GEN y, GEN T}67846785\fun{GEN}{F2xq_sqr}{GEN x, GEN T}67866787\fun{GEN}{F2xq_div}{GEN x,GEN y,GEN T}67886789\fun{GEN}{F2xq_inv}{GEN x, GEN T}67906791\fun{GEN}{F2xq_invsafe}{GEN x, GEN T}67926793\fun{GEN}{F2xq_pow}{GEN x, GEN n, GEN T}67946795\fun{GEN}{F2xq_powu}{GEN x, ulong n, GEN T}67966797\fun{GEN}{F2xq_pow_init}{GEN x, GEN n, long k, GEN T}67986799\fun{GEN}{F2xq_pow_table}{GEN R, GEN n, GEN T}68006801\fun{ulong}{F2xq_trace}{GEN x, GEN T}68026803\fun{GEN}{F2xq_conjvec}{GEN x, GEN T} returns the vector of conjugates6804$[x,x^2,x^{2^2},\ldots,x^{2^{n-1}}]$ where $n$ is the degree of $T$.68056806\fun{GEN}{F2xq_log}{GEN a, GEN g, GEN ord, GEN T}68076808\fun{GEN}{F2xq_order}{GEN a, GEN ord, GEN T}68096810\fun{GEN}{F2xq_Artin_Schreier}{GEN a, GEN T} returns a solution of $x^2+x=a$,6811assuming it exists.68126813\fun{GEN}{F2xq_sqrt}{GEN a, GEN T}68146815\fun{GEN}{F2xq_sqrt_fast}{GEN a, GEN s, GEN T} assuming that6816$s^2 \equiv x\pmod{T(x)}$, computes $b \equiv a(s)\pmod{T}$ so that $b^2=a$.68176818\fun{GEN}{F2xq_sqrtn}{GEN a, GEN n, GEN T, GEN *zeta}68196820\fun{GEN}{gener_F2xq}{GEN T, GEN *po}68216822\fun{GEN}{F2xq_powers}{GEN x, long n, GEN T}68236824\fun{GEN}{F2xq_matrix_pow}{GEN x, long m, long n, GEN T}68256826\fun{GEN}{F2x_F2xq_eval}{GEN f, GEN x, GEN T}68276828\fun{GEN}{F2x_F2xqV_eval}{GEN f, GEN x, GEN T}, see \kbd{FpX\_FpXQV\_eval}.68296830\fun{GEN}{F2xq_autpow}{GEN a, long n, GEN T} computes $\sigma^n(X)$ assuming6831$a=\sigma(X)$ where $\sigma$ is an automorphism of the algebra $\F_2[X]/T(X)$.68326833\subsec{\kbd{F2xn}} See \kbd{FpXn} operations.68346835\fun{GEN}{F2xn_red}{GEN a, long n}68366837\fun{GEN}{F2xn_inv}{GEN f, long e}68386839\subsec{\kbd{F2xqV}, \kbd{F2xqM}}. See \kbd{FqV}, \kbd{FqM} operations.68406841\fun{GEN}{F2xqM_F2xqC_gauss}{GEN a, GEN b, GEN T}68426843\fun{GEN}{F2xqM_F2xqC_invimage}{GEN a, GEN b, GEN T}68446845\fun{GEN}{F2xqM_F2xqC_mul}{GEN a, GEN b, GEN T}68466847\fun{GEN}{F2xqM_deplin}{GEN x, GEN T}68486849\fun{GEN}{F2xqM_det}{GEN a, GEN T}68506851\fun{GEN}{F2xqM_gauss}{GEN a, GEN b, GEN T}68526853\fun{GEN}{F2xqM_image}{GEN x, GEN T}68546855\fun{GEN}{F2xqM_indexrank}{GEN x, GEN T}68566857\fun{GEN}{F2xqM_inv}{GEN a, GEN T}68586859\fun{GEN}{F2xqM_invimage}{GEN a, GEN b, GEN T}68606861\fun{GEN}{F2xqM_ker}{GEN x, GEN T}68626863\fun{GEN}{F2xqM_mul}{GEN a, GEN b, GEN T}68646865\fun{long}{F2xqM_rank}{GEN x, GEN T}68666867\fun{GEN}{F2xqM_suppl}{GEN x, GEN T}68686869\fun{GEN}{matid_F2xqM}{long n, GEN T}68706871\subsec{\kbd{F2xX}}. See \kbd{FpXX} operations.68726873\fun{GEN}{ZXX_to_F2xX}{GEN x, long v}68746875\fun{GEN}{FlxX_to_F2xX}{GEN x}68766877\fun{GEN}{F2xX_to_FlxX}{GEN B}68786879\fun{GEN}{F2xX_to_F2xC}{GEN B, long N, long sv}68806881\fun{GEN}{F2xXV_to_F2xM}{GEN B, long N, long sv}68826883\fun{GEN}{F2xX_to_ZXX}{GEN B}68846885\fun{GEN}{F2xX_renormalize}{GEN x, long lx}68866887\fun{long}{F2xY_degreex}{GEN P} return the degree of $P$ with respect to6888the secondary variable.68896890\fun{GEN}{pol1_F2xX}{long v, long sv}68916892\fun{GEN}{polx_F2xX}{long v, long sv}68936894\fun{GEN}{F2xX_add}{GEN x, GEN y}68956896\fun{GEN}{F2xX_F2x_add}{GEN x, GEN y}68976898\fun{GEN}{F2xX_F2x_mul}{GEN x, GEN y}68996900\fun{GEN}{F2xX_deriv}{GEN P} returns the derivative of \kbd{P} with respect to6901the main variable.69026903\fun{GEN}{Kronecker_to_F2xqX}{GEN z, GEN T}69046905\fun{GEN}{F2xX_to_Kronecker}{GEN z, GEN T}69066907\fun{GEN}{F2xY_F2xq_evalx}{GEN x, GEN y, GEN T} as \kbd{FpXY\_FpXQ\_evalx}.69086909\fun{GEN}{F2xY_F2xqV_evalx}{GEN x, GEN V, GEN T} as \kbd{FpXY\_FpXQV\_evalx}.69106911\subsec{\kbd{F2xXV/F2xXC}}. See \kbd{FpXXV} operations.69126913\fun{GEN}{FlxXC_to_F2xXC}{GEN B}69146915\fun{GEN}{F2xXC_to_ZXXC}{GEN B}69166917\subsec{\kbd{F2xqX}}. See \kbd{FlxqX} operations.69186919\subsubsec{Preconditioned reduction}69206921For faster reduction, the modulus \kbd{S} can be replaced by an extended6922modulus, which is an \kbd{F2xqXT}, in all \kbd{F2xqXQ}-classes6923functions, and in \kbd{F2xqX\_rem} and \kbd{F2xqX\_divrem}.69246925\fun{GEN}{F2xqX_get_red}{GEN S, GEN T} returns the extended modulus6926\kbd{eS}.69276928To write code that works both with plain and extended moduli, the following6929accessors are defined:69306931\fun{GEN}{get_F2xqX_mod}{GEN eS} returns the underlying modulus \kbd{S}.69326933\fun{GEN}{get_F2xqX_var}{GEN eS} returns the variable number of the modulus.69346935\fun{GEN}{get_F2xqX_degree}{GEN eS} returns the degree of the modulus.69366937\subsubsec{basic functions}69386939\fun{GEN}{random_F2xqX}{long d, long v, GEN T, ulong p} returns a random6940\kbd{F2xqX} in variable \kbd{v}, of degree less than~\kbd{d}.69416942\fun{GEN}{F2xqX_red}{GEN z, GEN T}69436944\fun{GEN}{F2xqX_normalize}{GEN z, GEN T}69456946\fun{GEN}{F2xqX_F2xq_mul}{GEN P, GEN U, GEN T}69476948\fun{GEN}{F2xqX_F2xq_mul_to_monic}{GEN P, GEN U, GEN T}69496950\fun{GEN}{F2xqX_mul}{GEN x, GEN y, GEN T}69516952\fun{GEN}{F2xqX_sqr}{GEN x, GEN T}69536954\fun{GEN}{F2xqX_powu}{GEN x, ulong n, GEN T}69556956\fun{GEN}{F2xqX_rem}{GEN x, GEN y, GEN T}69576958\fun{GEN}{F2xqX_div}{GEN x, GEN y, GEN T}69596960\fun{GEN}{F2xqX_divrem}{GEN x, GEN y, GEN T, GEN *pr}69616962\fun{GEN}{F2xqXQ_inv}{GEN x, GEN S, GEN T}69636964\fun{GEN}{F2xqXQ_invsafe}{GEN x, GEN S, GEN T}69656966\fun{GEN}{F2xqX_invBarrett}{GEN T, GEN Q}69676968\fun{GEN}{F2xqX_extgcd}{GEN x, GEN y, GEN T, GEN *ptu, GEN *ptv}69696970\fun{GEN}{F2xqX_gcd}{GEN x, GEN y, GEN T}69716972\fun{GEN}{F2xqX_halfgcd}{GEN x, GEN y, GEN T}69736974\fun{GEN}{F2xqX_resultant}{GEN x, GEN y, GEN T}69756976\fun{GEN}{F2xqX_disc}{GEN x, GEN T}69776978\fun{long}{F2xqX_ispower}{GEN f, ulong k, GEN T, GEN *pt}69796980\fun{GEN}{F2xqX_F2xqXQ_eval}{GEN Q, GEN x, GEN S, GEN T} as6981\kbd{FpX\_FpXQ\_eval}.69826983\fun{GEN}{F2xqX_F2xqXQV_eval}{GEN P, GEN V, GEN S, GEN T} as6984\kbd{FpX\_FpXQV\_eval}.69856986\fun{GEN}{F2xqX_roots}{GEN f, GEN T} return the roots of \kbd{f} in6987$\F_2[X]/(T)$. Assumes \kbd{T} irreducible in $\F_2[X]$.69886989\fun{GEN}{F2xqX_factor}{GEN f, GEN T} return the factorization of \kbd{f} over6990$\F_2[X]/(T)$. Assumes \kbd{T} irreducible in $\F_2[X]$.69916992\fun{GEN}{F2xqX_factor_squarefree}{GEN f, GEN T} as6993\kbd{FlxqX\_factor\_squarefree}.69946995\fun{GEN}{F2xqX_ddf}{GEN f, GEN T} as \kbd{FpX\_ddf}.69966997\fun{GEN}{F2xqX_degfact}{GEN f, GEN T} as \kbd{FpX\_degfact}.69986999\subsec{\kbd{F2xqXQ}}. See \kbd{FlxqXQ} operations.70007001\fun{GEN}{FlxqXQ_inv}{GEN x, GEN S, GEN T}70027003\fun{GEN}{FlxqXQ_invsafe}{GEN x, GEN S, GEN T}70047005\fun{GEN}{F2xqXQ_mul}{GEN x, GEN y, GEN S, GEN T}70067007\fun{GEN}{F2xqXQ_sqr}{GEN x, GEN S, GEN T}70087009\fun{GEN}{F2xqXQ_pow}{GEN x, GEN n, GEN S, GEN T}70107011\fun{GEN}{F2xqXQ_powers}{GEN x, long n, GEN S, GEN T}70127013\fun{GEN}{F2xqXQ_autpow}{GEN a, long n, GEN S, GEN T}7014as \kbd{FpXQXQ\_autpow}70157016\fun{GEN}{F2xqXQ_auttrace}{GEN a, long n, GEN S, GEN T}. Let7017$\sigma$ be the automorphism defined by $\sigma(X)=a[1]\pmod{T(X)}$ and7018$\sigma(Y)=a[2]\pmod{S(X,Y),T(X)}$; returns the vector7019$[\sigma^n(X),\sigma^n(Y),b+\sigma(b)+\ldots+\sigma^{n-1}(b)]$7020where $b=a[3]$.70217022\fun{GEN}{F2xqXQV_red}{GEN x, GEN S, GEN T}70237024\subsec{Functions returning objects with \typ{INTMOD} coefficients}70257026Those functions are mostly needed for interface reasons: \typ{INTMOD}s should7027not be used in library mode since the modular kernel is more flexible and more7028efficient, but GP users do not have access to the modular kernel.7029We document them for completeness:70307031\fun{GEN}{Fp_to_mod}{GEN z, GEN p}, \kbd{z} a \typ{INT}. Returns \kbd{z *7032Mod(1,p)}, normalized. Hence the returned value is a \typ{INTMOD}.70337034\fun{GEN}{FpX_to_mod}{GEN z, GEN p}, \kbd{z} a \kbd{ZX}. Returns \kbd{z *7035Mod(1,p)}, normalized. Hence the returned value has \typ{INTMOD} coefficients.70367037\fun{GEN}{FpC_to_mod}{GEN z, GEN p}, \kbd{z} a \kbd{ZC}. Returns \kbd{Col(z) *7038Mod(1,p)}, a \typ{COL} with \typ{INTMOD} coefficients.70397040\fun{GEN}{FpV_to_mod}{GEN z, GEN p}, \kbd{z} a \kbd{ZV}. Returns \kbd{Vec(z) *7041Mod(1,p)}, a \typ{VEC} with \typ{INTMOD} coefficients.70427043\fun{GEN}{FpVV_to_mod}{GEN z, GEN p}, \kbd{z} a \kbd{ZVV}. Returns \kbd{Vec(z) *7044Mod(1,p)}, a \typ{VEC} of \typ{VEC} with \typ{INTMOD} coefficients.70457046\fun{GEN}{FpM_to_mod}{GEN z, GEN p}, \kbd{z} a \kbd{ZM}. Returns \kbd{z *7047Mod(1,p)}, with \typ{INTMOD} coefficients.70487049\fun{GEN}{F2c_to_mod}{GEN x}70507051\fun{GEN}{F2m_to_mod}{GEN x}70527053\fun{GEN}{Flc_to_mod}{GEN z}70547055\fun{GEN}{Flm_to_mod}{GEN z}70567057\fun{GEN}{FqC_to_mod}{GEN z, GEN T, GEN p}70587059\fun{GEN}{FqM_to_mod}{GEN z, GEN T, GEN p}70607061\fun{GEN}{FpXC_to_mod}{GEN V, GEN p}70627063\fun{GEN}{FpXM_to_mod}{GEN V, GEN p}70647065\fun{GEN}{FpXQC_to_mod}{GEN V, GEN T, GEN p} $V$ being a vector of \kbd{FpXQ},7066converts each entry to a \typ{POLMOD} with \typ{INTMOD} coefficients, and return7067a \typ{COL}.70687069\fun{GEN}{FpXQX_to_mod}{GEN P, GEN T, GEN p}7070$P$ being a \kbd{FpXQX}, converts each coefficient to a \typ{POLMOD} with7071\typ{INTMOD} coefficients.70727073\fun{GEN}{FqX_to_mod}{GEN P, GEN T, GEN p} same but allow7074$\kbd{T} = \kbd{NULL}$.70757076\fun{GEN}{FqXC_to_mod}{GEN P, GEN T, GEN p}70777078\fun{GEN}{FqXM_to_mod}{GEN P, GEN T, GEN p}70797080\fun{GEN}{QXQ_to_mod_shallow}{GEN x, GEN T} $x$ a \kbd{QXQ}, which is7081a lifted representative of elements of $\Q[X]/(T)$ (number field elements7082in most applications) and $T$ is in $\Z[X]$. Convert it to a \typ{POLMOD}7083modulo $T$; no reduction mod $T$ is attempted: the representatives should be7084already reduced. Shallow function.70857086\fun{GEN}{QXQV_to_mod}{GEN V, GEN T} $V$ a vector of \kbd{QXQ}, which7087are lifted representatives of elements of $\Q[X]/(T)$ (number field elements7088in most applications) and $T$ is in $\Z[X]$. Return a vector where all7089nonrational entries are converted to \typ{POLMOD} modulo $T$; no reduction7090mod $T$ is attempted: the representatives should be already reduced. Used to7091normalize the output of \kbd{nfroots}.70927093\fun{GEN}{QXQX_to_mod_shallow}{GEN P, GEN T} $P$ a polynomial with \kbd{QXQ}7094coefficients; replace them by \kbd{mkpolmod(.,T)}. Shallow function.70957096\fun{GEN}{QXQC_to_mod_shallow}{GEN V, GEN T} $V$ a vector with \kbd{QXQ}7097coefficients; replace them by \kbd{mkpolmod(.,T)}. Shallow function.70987099\fun{GEN}{QXQM_to_mod_shallow}{GEN M, GEN T} $M$ a matrix with \kbd{QXQ}7100coefficients; replace them by \kbd{mkpolmod(.,T)}. Shallow function.71017102\fun{GEN}{QXQXV_to_mod}{GEN V, GEN T} $V$ a vector of polynomials whose7103coefficients are \kbd{QXQ}. Analogous to \kbd{QXQV\_to\_mod}.7104Used to normalize the output of \kbd{nffactor}.71057106The following functions are obsolete and should not be used: they receive a7107polynomial with arbitrary coefficients, apply a conversion function to map7108them to a finite field, a function from the modular kernel, then7109\kbd{*\_to\_mod}:71107111\fun{GEN}{rootmod}{GEN f, GEN p}, applies \kbd{FpX\_roots}.71127113\fun{GEN}{rootmod2}{GEN f, GEN p}, (now) identical to \kbd{rootmod}.71147115\fun{GEN}{rootmod0}{GEN f, GEN p, long flag}, (now) identical to7116\kbd{rootmod}; ignores \fl.71177118\fun{GEN}{factmod}{GEN f, GEN p} applies \kbd{*\_factor}.71197120\fun{GEN}{simplefactmod}{GEN f, GEN p} applies \kbd{*\_degfact}.71217122\subsec{Slow Chinese remainder theorem over $\Z$}7123The routines in this section have quadratic time complexity with respect to7124the input size; see the routines in the next two sections for quasi-linear7125time variants.71267127\fun{GEN}{Z_chinese}{GEN a, GEN b, GEN A, GEN B} returns the integer7128in $[0, \lcm(A,B)[$ congruent to $a$ mod $A$ and $b$ mod $B$, assuming it7129exists; in other words, that $a$ and $b$ are congruent mod $\gcd(A,B)$.71307131\fun{GEN}{Z_chinese_all}{GEN a, GEN b, GEN A, GEN B, GEN *pC} as7132\kbd{Z\_chinese}, setting \kbd{*pC} to the lcm of $A$ and $B$.71337134\fun{GEN}{Z_chinese_coprime}{GEN a, GEN b, GEN A, GEN B, GEN C}, as7135\kbd{Z\_chinese}, assuming that $\gcd(A,B) = 1$ and that $C = \lcm(A,B) = AB$.71367137\fun{ulong}{u_chinese_coprime}{ulong a, ulong b, ulong A, ulong B, ulong C}, as7138\kbd{Z\_chinese\_coprime} for \kbd{ulong} inputs and output.71397140\fun{void}{Z_chinese_pre}{GEN A, GEN B, GEN *pC, GEN *pU, GEN *pd}7141initializes chinese remainder computations modulo $A$ and $B$. Sets7142\kbd{*pC} to $\lcm(A,B)$, \kbd{*pd} to $\gcd(A,B)$,7143\kbd{*pU} to an integer congruent to $0$ mod $(A/d)$ and $1$ mod $(B/d)$.7144It is allowed to set \kbd{pd = NULL}, in which case, $d$ is still7145computed, but not saved.71467147\fun{GEN}{Z_chinese_post}{GEN a, GEN b, GEN C, GEN U, GEN d} returns7148the solution to the chinese remainder problem $x$ congruent7149to $a$ mod $A$ and $b$ mod $B$, where $C, U, d$ were set in7150\kbd{Z\_chinese\_pre}. If $d$ is \kbd{NULL}, assume the problem has a7151solution. Otherwise, return \kbd{NULL} if it has no solution.71527153\medskip71547155The following pair of functions is used in homomorphic imaging schemes,7156when reconstructing an integer from its images modulo pairwise coprime7157integers. The idea is as follows: we want to discover an integer $H$ which7158satisfies $|H| < B$ for some known bound $B$; we are given pairs $(H_p, p)$7159with $H$ congruent to $H_p$ mod $p$ and all $p$ pairwise coprime.71607161Given \kbd{H} congruent to $H_p$ modulo a number of $p$, whose product is7162$q$, and a new pair $(\kbd{Hp}, \kbd{p})$, \kbd{p} coprime to $q$, the7163following incremental functions use the chinese remainder theorem (CRT) to7164find a new \kbd{H}, congruent to the preceding one modulo $q$, but also to7165\kbd{Hp} modulo \kbd{p}. It is defined uniquely modulo $qp$, and we choose7166the centered representative. When $P$ is larger than $2B$, we have $\kbd{H} =7167H$, but of course, the value of \kbd{H} may stabilize sooner. In many7168applications it is possible to directly check that such a partial result is7169correct.71707171\fun{GEN}{Z_init_CRT}{ulong Hp, ulong p} given a \kbd{Fl} \kbd{Hp} in7172$[0, p-1]$, returns the centered representative \kbd{H} congruent to \kbd{Hp}7173modulo \kbd{p}.71747175\fun{int}{Z_incremental_CRT}{GEN *H, ulong Hp, GEN *q, ulong p}7176given a \typ{INT} \kbd{*H}, centered modulo \kbd{*q}, a new pair $(\kbd{Hp},7177\kbd{p})$ with \kbd{p} coprime to \kbd{q}, this function updates \kbd{*H} so7178that it also becomes congruent to $(\kbd{Hp}, \kbd{p})$, and \kbd{*q} to the7179product$\kbd{qp} = \kbd{p} \cdot \kbd{*q}$. It returns $1$ if the new value7180is equal to the old one, and $0$ otherwise.71817182\fun{GEN}{chinese1_coprime_Z}{GEN v} an alternative divide-and-conquer7183implementation: $v$ is a vector of \typ{INTMOD} with pairwise coprime moduli.7184Return the \typ{INTMOD} solving the corresponding chinese remainder problem.7185This is a streamlined version of71867187\fun{GEN}{chinese1}{GEN v}, which solves a general chinese remainder problem7188(not necessarily over $\Z$, moduli not assumed coprime).71897190As above, for $H$ a \kbd{ZM}: we assume that $H$ and all \kbd{Hp} have7191dimension $> 0$. The original \kbd{*H} is destroyed.71927193\fun{GEN}{ZM_init_CRT}{GEN Hp, ulong p}71947195\fun{int}{ZM_incremental_CRT}{GEN *H, GEN Hp, GEN *q, ulong p}71967197As above for $H$ a \kbd{ZX}: note that the degree may increase or decrease.7198The original \kbd{*H} is destroyed.71997200\fun{GEN}{ZX_init_CRT}{GEN Hp, ulong p, long v}72017202\fun{int}{ZX_incremental_CRT}{GEN *H, GEN Hp, GEN *q, ulong p}72037204As above, for $H$ a matrix whose coefficient are \kbd{ZX}.7205The original \kbd{*H} is destroyed.7206The entries of $H$ are not normalized, use \kbd{ZX\_renormalize}7207for this.72087209\fun{GEN}{ZXM_init_CRT}{GEN Hp, long deg, ulong p} where \kbd{deg}7210is the maximal degree of all the \kbd{Hp}72117212\fun{int}{ZXM_incremental_CRT}{GEN *H, GEN Hp, GEN *q, ulong p}72137214\subsec{Fast remainders}72157216The routines in these section are asymptotically fast (quasi-linear time in7217the input size).72187219\fun{GEN}{Z_ZV_mod}{GEN A, GEN P} given a \typ{INT} $A$ and a vector $P$ of7220positive pairwise coprime integers of length $n\ge 1$, return a vector $B$ of7221the same length such that $B[i] = A\pmod{P[i]}$ and $0\leq B[i] < P[i]$ for7222all $1\leq i\leq n$. The vector $P$ may be a \typ{VEC} or a \typ{VECSMALL}7223(treated as \kbd{ulong}s) and $B$ has the same type as $P$.72247225\fun{GEN}{Z_nv_mod}{GEN A, GEN P} given a \typ{INT} $A$ and a \typ{VECSMALL}7226$P$ of positive pairwise coprime integers of length $n\ge 1$, return a7227\typ{VECSMALL} $B$ of the same length such that $B[i]=A\pmod{P[i]}$ and7228$0\leq B[i] < P[i]$ for all $1\leq i\leq n$. The entries of $P$ and $B$ are7229treated as \kbd{ulong}s.72307231The following low level functions allow precomputations:72327233\fun{GEN}{ZV_producttree}{GEN P} where $P$ is a vector of integers (or7234\typ{VECSMALL}) of length $n\ge 1$, return the vector of \typ{VEC}s7235$[f(P),f^2(P),\ldots,f^k(P)]$ where $f$ is the transformation7236$[p_1,p_2,\ldots,p_m] \mapsto [p_1\*p_2,p_3\*p_4,\ldots,p_{m-1}\*p_m]$ if $m$7237is even and $[p_1\*p_2,p_3\*p_4,\ldots,p_{m-2}\*p_{m-1},p_m]$ if $m$ is odd,7238and $k = O(\log m)$ is minimal so that $f^k(P)$ has length $1$; in other7239words, $f^k(P) = [p_1\*p_2\*\ldots\*p_m]$.72407241\fun{GEN}{Z_ZV_mod_tree}{GEN A, GEN P, GEN T}7242as \kbd{Z\_ZV\_mod} where $T$ is the tree \kbd{ZV\_producttree(P)}.72437244\fun{GEN}{ZV_nv_mod_tree}{GEN A, GEN P, GEN T} $A$ being a \kbd{ZV}7245and $P$ a \typ{VECSMALL} of length $n\ge 1$, the elements of $P$ being7246pairwise coprime, return the vector of \kbd{Flv}7247$[A \pmod{P[1]},\ldots,A \pmod{P[n]}]$,7248where $T$ is the tree \kbd{ZV\_producttree(P)}.72497250\fun{GEN}{ZM_nv_mod_tree}{GEN A, GEN P, GEN T} $A$ being a \kbd{ZM}7251and $P$ a \typ{VECSMALL} of length $n\ge 1$, the elements of $P$ being7252pairwise coprime, return the vector of \kbd{Flm}7253$[A \pmod{P[1]},\ldots,A \pmod{P[n]}]$,7254where $T$ is the tree \kbd{ZV\_producttree(P)}.72557256\fun{GEN}{ZX_nv_mod_tree}{GEN A, GEN P, GEN T} $A$ being a \kbd{ZX}7257and $P$ a \typ{VECSMALL} of length $n\ge 1$, the elements of $P$ being7258pairwise coprime, return the vector of \kbd{Flx} polynomials7259$[A \pmod{P[1]},\ldots,A \pmod{P[n]}]$,7260where $T$ is the tree \kbd{ZV\_producttree(P)}.72617262\fun{GEN}{ZXC_nv_mod_tree}{GEN A, GEN P, GEN T} $A$ being a \kbd{ZXC}7263and $P$ a \typ{VECSMALL} of length $n\ge 1$, the elements of $P$ being7264pairwise coprime, return the vector of \kbd{FlxC}7265$[A \pmod{P[1]},\ldots,A \pmod{P[n]}]$,7266where $T$ is the tree \kbd{ZV\_producttree(P)}.72677268\fun{GEN}{ZXM_nv_mod_tree}{GEN A, GEN P, GEN T} $A$ being a \kbd{ZXM}7269and $P$ a \typ{VECSMALL} of length $n\ge 1$, the elements of $P$ being7270pairwise coprime, return the vector of \kbd{FlxM}7271$[A \pmod{P[1]},\ldots,A \pmod{P[n]}]$,7272where $T$ is the tree \kbd{ZV\_producttree(P)}.72737274\fun{GEN}{ZXX_nv_mod_tree}{GEN A, GEN P, GEN T, long v} $A$ being a \kbd{ZXX},7275and $P$ a \typ{VECSMALL} of length $n\ge 1$, the elements of $P$ being7276pairwise coprime, return the vector of \kbd{FlxX}7277$[A \pmod{P[1]},\ldots,A \pmod{P[n]}]$,7278where $T$ is assumed to be the tree created by \kbd{ZV\_producttree(P)}.72797280\medskip72817282\subsec{Fast Chinese remainder theorem over $\Z$}7283The routines in these section are asymptotically fast (quasi-linear time in7284the input size) and should be used whenever the moduli are known from7285the start.72867287The simplest function is72887289\fun{GEN}{ZV_chinese}{GEN A, GEN P, GEN *pM}7290let $P$ be a vector of positive pairwise coprime integers, let $A$ be a7291vector of integers of the same length $n\ge 1$ such that $0 \leq A[i] < P[i]$7292for all $i$, and let $M$ be the product of the elements of $P$. Returns the7293integer in $[0, M[$ congruent to $A[i]$ mod $P[i]$ for all $1\leq i\leq n$.7294If \kbd{pM} is not \kbd{NULL}, set \kbd{*pM} to $M$. We also allow7295\typ{VECSMALL}s for $A$ and $P$ (seen as vectors of unsigned integers).72967297\fun{GEN}{ZV_chinese_center}{GEN A, GEN P, GEN *pM}7298As \kbd{ZV\_chinese} but return integers in $[-M/2, M/2[$ instead.72997300The following functions allow to solve many Chinese remainder problems7301simultaneously, for a given set of moduli:73027303\fun{GEN}{nxV_chinese_center}{GEN A, GEN P, GEN *pt_mod}7304where $A$ is a vector of \kbd{nx}7305and $P$ a \typ{VECSMALL} of the same length $n\ge 1$, the elements of $P$7306being pairwise coprime, and $M$ being the product of the elements of $P$,7307returns the \typ{POL} whose entries are integers in $[-M/2, M/2[$ congruent to $A[i]$7308mod $P[i]$ for all $1\leq i\leq n$.7309If \kbd{pt\_mod} is not \kbd{NULL}, set \kbd{*pt\_mod} to $M$.73107311\fun{GEN}{ncV_chinese_center}{GEN A, GEN P, GEN *pM} where $A$ is a7312vector of \kbd{VECSMALL}s (seen as vectors of unsigned integers) and $P$ a7313\typ{VECSMALL} of the same length $n\ge 1$, the elements of $P$ being7314pairwise coprime, and $M$ being the product of the elements of $P$, returns7315the \typ{COL} whose entries are integers in $[-M/2, M/2[$ congruent to $A[i]$7316mod $P[i]$ for all $1\leq i\leq n$. If \kbd{pM} is not \kbd{NULL}, set7317\kbd{*pt\_mod} to $M$.73187319\fun{GEN}{nmV_chinese_center}{GEN A, GEN P, GEN *pM} where $A$ is a7320vector of \kbd{MATSMALL}s (seen as matrices of unsigned integers) and $P$ a7321\typ{VECSMALL} of the same length $n\ge 1$, the elements of $P$ being7322pairwise coprime, and $M$ being the product of the elements of $P$, returns7323the matrix whose entries are integers in $[-M/2, M/2[$ congruent to $A[i]$7324mod $P[i]$ for all $1\leq i\leq n$. If \kbd{pM} is not \kbd{NULL}, set7325\kbd{*pM} to $M$. N.B.: this function uses the parallel GP interface.73267327\fun{GEN}{nxCV_chinese_center}{GEN A, GEN P, GEN *pM} where $A$ is a7328vector of \kbd{nxC}s and $P$ a7329\typ{VECSMALL} of the same length $n\ge 1$, the elements of $P$ being7330pairwise coprime, and $M$ being the product of the elements of $P$, returns7331the \typ{COL} whose entries are integers in $[-M/2, M/2[$ congruent to $A[i]$7332mod $P[i]$ for all $1\leq i\leq n$. If \kbd{pM} is not \kbd{NULL}, set7333\kbd{*pt\_mod} to $M$.73347335\fun{GEN}{nxMV_chinese_center}{GEN A, GEN P, GEN *pM} where $A$ is a7336vector of \kbd{nxM}s and $P$ a7337\typ{VECSMALL} of the same length $n\ge 1$, the elements of $P$ being7338pairwise coprime, and $M$ being the product of the elements of $P$, returns7339the matrix whose entries are integers in $[-M/2, M/2[$ congruent to $A[i]$7340mod $P[i]$ for all $1\leq i\leq n$. If \kbd{pM} is not \kbd{NULL}, set7341\kbd{*pM} to $M$. N.B.: this function uses the parallel GP interface.73427343The other routines allow for various precomputations :73447345\fun{GEN}{ZV_chinesetree}{GEN P, GEN T} given $P$ a vector of integers7346(or \typ{VECSMALL}) and a product tree $T$ from \tet{ZV_producttree}$(P)$7347for the same $P$, return a ``chinese remainder tree'' $R$, preconditionning7348the solution of Chinese remainder problems modulo the $P[i]$.73497350\fun{GEN}{ZV_chinese_tree}{GEN A, GEN P, GEN T, GEN R}7351return \kbd{ZV\_chinese}$(A,P,\kbd{NULL})$, where $T$ is created by7352\kbd{ZV\_producttree}$(P)$ and $R$ by \kbd{ZV\_chinesetree}$(P,T)$.73537354\fun{GEN}{ncV_chinese_center_tree}{GEN A, GEN P, GEN T, GEN R}7355as \kbd{ncV\_chinese\_center} where $T$ is assumed to be the tree created by7356\kbd{ZV\_producttree(P)} and $R$ by \kbd{ZV\_chinesetree}$(P,T)$.73577358\fun{GEN}{nmV_chinese_center_tree}{GEN A, GEN P, GEN T, GEN R}7359as \kbd{nmV\_chinese\_center} where $T$ is assumed to be the tree created by7360\kbd{ZV\_producttree(P)} and $R$ by \kbd{ZV\_chinesetree}$(P,T)$.73617362\fun{GEN}{nxV_chinese_center_tree}{GEN A, GEN P, GEN T, GEN R}7363as \kbd{nxV\_chinese\_center} where $T$ is assumed to be the tree created by7364\kbd{ZV\_producttree(P)} and $R$ by \kbd{ZV\_chinesetree}$(P,T)$.73657366\fun{GEN}{nxCV_chinese_center_tree}{GEN A, GEN P, GEN T, GEN R}7367as \kbd{nxCV\_chinese\_center} where $T$ is assumed to be the tree created by7368\kbd{ZV\_producttree(P)} and $R$ by \kbd{ZV\_chinesetree}$(P,T)$.73697370\subsec{Rational reconstruction}73717372\fun{int}{Fp_ratlift}{GEN x, GEN m, GEN amax, GEN bmax, GEN *a, GEN *b}.7373Assuming that $0 \leq x < m$, $\kbd{amax} \geq 0$, and7374$\kbd{bmax} > 0$ are \typ{INT}s, and that $2 \kbd{amax} \kbd{bmax} < m$,7375attempts to recognize $x$ as a rational $a/b$, i.e. to find \typ{INT}s $a$7376and $b$ such that73777378\item $a \equiv b x$ modulo $m$,73797380\item $|a| \leq \kbd{amax}$, $0 < b \leq \kbd{bmax}$,73817382\item $\gcd(m,b) = \gcd(a,b)$.73837384\noindent If unsuccessful, the routine returns $0$ and leaves $a$, $b$7385unchanged; otherwise it returns $1$ and sets $a$ and $b$.73867387In almost all applications, we actually know that a solution exists, as well7388as a nonzero multiple $B$ of $b$, and $m = p^\ell$ is a prime power, for a7389prime $p$ chosen coprime to $B$ hence to $b$. Under the single assumption7390$\gcd(m,b) = 1$, if a solution $a,b$ exists satisfying the three conditions7391above, then it is unique.73927393\fun{GEN}{FpM_ratlift}{GEN M, GEN m, GEN amax, GEN bmax, GEN denom}7394given an \kbd{FpM} modulo $m$ with reduced or \kbd{Fp\_center}-ed entries,7395reconstructs a matrix with rational coefficients by applying \kbd{Fp\_ratlift}7396to all entries. Assume that all preconditions for \kbd{Fp\_ratlift} are7397satisfied, as well $\gcd(m,b) = 1$ (so that the solution is unique if it7398exists). Return \kbd{NULL} if the reconstruction fails, and the rational7399matrix otherwise. If \kbd{denom} is not \kbd{NULL} check further that all7400denominators divide \kbd{denom}.74017402The function is not stack clean if one of the coefficients of $M$ is negative7403(centered residues), but still suitable for \kbd{gerepileupto}.74047405\fun{GEN}{FpX_ratlift}{GEN P, GEN m, GEN amax, GEN bmax, GEN denom} as7406\kbd{FpM\_ratlift}, where $P$ is an \kbd{FpX}.74077408\fun{GEN}{FpC_ratlift}{GEN P, GEN m, GEN amax, GEN bmax, GEN denom} as7409\kbd{FpM\_ratlift}, where $P$ is an \kbd{FpC}.74107411\subsec{Zp}74127413\fun{GEN}{Zp_sqrt}{GEN b, GEN p, long e} $b$ and $p$ being \typ{INT}s, with $p$7414a prime (possibly $2$), returns a \typ{INT} $a$ such that $a^2 \equiv b \mod7415p^e$.74167417\fun{GEN}{Z2_sqrt}{GEN b, long e} $b$ being a \typ{INT}s7418returns a \typ{INT} $a$ such that $a^2 \equiv b \mod 2^e$.74197420\fun{GEN}{Zp_sqrtlift}{GEN b, GEN a, GEN p, long e} let7421$a,b,p$ be \typ{INT}s, with $p > 1$ odd, such that $a^2\equiv b\mod p$.7422Returns a \typ{INT} $A$ such that $A^2 \equiv b \mod p^e$. Special case7423of \tet{Zp_sqrtnlift}.74247425\fun{GEN}{Zp_sqrtnlift}{GEN b, GEN n, GEN a, GEN p, long e} let7426$a,b,n,p$ be \typ{INT}s, with $n,p > 1$, and $p$ coprime to $n$,7427such that $a^n \equiv b \mod p$. Returns a \typ{INT} $A$ such that7428$A^n \equiv b \mod p^e$. Special case of \tet{ZpX_liftroot}.74297430\fun{GEN}{Zp_teichmuller}{GEN x, GEN p, long e, GEN pe} for $p$ an odd prime,7431$x$ a \typ{INT} coprime to $p$, and $pe = p^e$, returns the $(p-1)$-th root of7432$1$ congruent to $x$ modulo $p$, modulo $p^e$. For convenience, $p = 2$ is7433also allowed and we return $1$ ($x$ is $1$ mod $4$) or $2^e - 1$ ($x$ is $3$7434mod $4$).74357436\fun{GEN}{teichmullerinit}{long p, long n} returns the values of7437\tet{Zp_teichmuller} at all $x = 1, \dots, p-1$.74387439\subsec{ZpM}74407441\fun{GEN}{ZpM_invlift}{GEN M, GEN Np, GEN p, long e} let7442$p$ be a prime \typ{INT}, $Np$ be a \kbd{FpM} (modulo $p$) and7443$M$ a \kbd{ZpM} such that $M\*Np \equiv 1 \mod p$.7444Returns an \kbd{ZpM} $N$ such that $N \equiv Np \pmod{p}$ and7445$M\*N \equiv 1 \mod p^e$.74467447\subsec{ZpX}74487449\fun{GEN}{ZpX_roots}{GEN f, GEN p, long e} $f$ a \kbd{ZX} with leading7450term prime to $p$, and without multiple roots mod $p$. Return a vector7451of \typ{INT}s which are the roots of $f$ mod $p^e$.74527453\fun{GEN}{ZpX_liftroot}{GEN f, GEN a, GEN p, long e} $f$ a \kbd{ZX} with7454leading term prime to $p$, and $a$ a root mod $p$ such that7455$v_p(f'(a))=0$. Return a \typ{INT} which is the root of $f$ mod $p^e$7456congruent to $a$ mod $p$.74577458\fun{GEN}{ZX_Zp_root}{GEN f, GEN a, GEN p, long e} same as \tet{ZpX_liftroot}7459without the assumption $v_p(f'(a)) = 0$. Return a \typ{VEC} of \typ{INT}s,7460which are the $p$-adic roots of $f$ congruent to $a$ mod $p$ (given modulo7461$p^e$). Assume that $0 \leq a < p$.74627463\fun{GEN}{ZpX_liftroots}{GEN f, GEN S, GEN p, long e} $f$ a \kbd{ZX} with7464leading term prime to $p$, and $S$ a vector of simple roots mod $p$. Return a7465vector of \typ{INT}s which are the root of $f$ mod $p^e$ congruent to the7466$S[i]$ mod $p$.74677468\fun{GEN}{ZpX_liftfact}{GEN A, GEN B, GEN pe, GEN p, long e} is7469the routine underlying \tet{polhensellift}. Here, $p$ is prime7470defines a finite field $\F_p$. $A$ is a polynomial in7471$\Z[X]$, whose leading coefficient is nonzero in $\F_q$. $B$ is a vector of7472monic \kbd{FpX}, pairwise coprime in $\F_p[X]$, whose product is congruent to7473$A/\text{lc}(A)$ in $\F_p[X]$. Lifts the elements of $B$ mod $\kbd{pe} = p^e$.74747475\fun{GEN}{ZpX_Frobenius}{GEN T, GEN p, ulong e} returns the $p$-adic lift7476of the Frobenius automorphism of $\F_p[X]/(T)$ to precision $e$.74777478\fun{long}{ZpX_disc_val}{GEN f, GEN p} returns the valuation at $p$ of the7479discriminant of $f$. Assume that $f$ is a monic \emph{separable} \kbd{ZX}7480and that $p$ is a prime number. Proceeds by dynamically increasing the7481$p$-adic accuracy; infinite loop if the discriminant of $f$ is7482$0$.74837484\fun{long}{ZpX_resultant_val}{GEN f, GEN g, GEN p, long M} returns the7485valuation at $p$ of $\text{Res}(f,g)$. Assume $f,g$ are both \kbd{ZX},7486and that $p$ is a prime number coprime to the leading coefficient of $f$.7487Proceeds by dynamically increasing the $p$-adic accuracy.7488To avoid an infinite loop when the resultant is $0$, we return $M$ if7489the Sylvester matrix mod $p^M$ still does not have maximal rank.74907491\fun{GEN}{ZpX_gcd}{GEN f,GEN g, GEN p, GEN pm} $f$ a monic \kbd{ZX},7492$g$ a \kbd{ZX}, $\kbd{pm} = p^m$ a prime power. There is a unique integer7493$r\geq 0$ and a monic $h\in \Q_p[X]$ such that7494$$p^rh\Z_p[X] + p^m\Z_p[X] = f\Z_p[X] + g\Z_p[X] + p^m\Z_p[X].$$7495Return the $0$ polynomial if $r\geq m$ and a monic $h\in\Z[1/p][X]$ otherwise7496(whose valuation at $p$ is $> -m$).74977498\fun{GEN}{ZpX_reduced_resultant}{GEN f, GEN g, GEN p, GEN pm} $f$ a monic7499\kbd{ZX}, $g$ a \kbd{ZX}, $\kbd{pm} = p^m$ a prime power. The $p$-adic7500\emph{reduced resultant}\varsidx{resultant (reduced)} of $f$ and $g$ is7501$0$ if $f$, $g$ not coprime in $\Z_p[X]$, and otherwise the generator of the7502form $p^d$ of7503$$ (f\Z_p[X] + g\Z_p[X])\cap \Z_p. $$7504Return the reduced resultant modulo $p^m$.75057506\fun{GEN}{ZpX_reduced_resultant_fast}{GEN f, GEN g, GEN p, long M} $f$7507a monic \kbd{ZX}, $g$ a \kbd{ZX}, $p$ a prime. Returns7508the $p$-adic reduced resultant of $f$ and $g$ modulo $p^M$. This function7509computes resultants for a sequence of increasing $p$-adic accuracies7510(up to $M$ $p$-adic digits), returning as soon as it obtains a nonzero7511result. It is very inefficient when the resultant is $0$, but otherwise7512usually more efficient than computations using a priori bounds.75137514\fun{GEN}{ZpX_monic_factor}{GEN f, GEN p, long M} $f$ a monic7515\kbd{ZX}, $p$ a prime, return the $p$-adic factorization of $f$, modulo7516$p^M$. This is the underlying low-level recursive function behind7517\kbd{factorpadic} (using a combination of Round 4 factorization and Hensel7518lifting); the factors are not sorted and the function is not7519\kbd{gerepile}-clean.75207521\fun{GEN}{ZpX_primedec}{GEN T, GEN p} $T$ a monic separable \kbd{ZX}, $p$ a7522prime, return as a factorization matrix the shape of the prime ideal7523decomposition of $(p)$ in $\Q[X]/(T)$: the first column contains inertia7524degrees, the second columns contains ramification degrees.75257526\subsec{ZpXQ}75277528\fun{GEN}{ZpXQ_invlift}{GEN b, GEN a, GEN T, GEN p, long e} let7529$p$ be a prime \typ{INT}, $a$ be a \kbd{FpXQ} (modulo $(p, T)$) and7530$b$ a \kbd{ZpXQ} such that $a\*b \equiv 1 \mod (p, T)$.7531Returns an \kbd{ZpXQ} $A$ such that $A \equiv a \pmod{p}$ and7532$A\*b \equiv 1 \mod (p^e, T)$.75337534\fun{GEN}{ZpXQ_inv}{GEN b, GEN T, GEN p, long e} let7535$p$ be a prime \typ{INT} and $b$ be a \kbd{FpXQ} (modulo $T, p^e$).7536Returns an \kbd{FpXQ} $A$ such that $A\*b \equiv 1 \mod (p^e, T)$.75377538\fun{GEN}{ZpXQ_div}{GEN a, GEN b, GEN T, GEN q, GEN p, long e} let7539$p$ be a prime \typ{INT} and $a$ and $b$ be a \kbd{FpXQ} (modulo $T, p^e$).7540Returns an \kbd{FpXQ} $c$ such that $c\*b \equiv a \mod (p^e, T)$.7541The parameter $q$ must be equal to $p^e$.75427543\fun{GEN}{ZpXQ_sqrtnlift}{GEN b, GEN n, GEN a, GEN T, GEN p, long e} let7544$n,p$ be \typ{INT}s, with $n,p > 1$ and $p$ coprime to $n$, and $a,b$7545be \kbd{FpXQ}s (modulo $T$) such that $a^n \equiv b \mod (p,T)$.7546Returns an \kbd{Fq} $A$ such that $A^n \equiv b \mod (p^e, T)$.75477548\fun{GEN}{ZpXQ_sqrt}{GEN b, GEN T, GEN p, long e} let7549$p$ being a odd prime and $b$ be a \kbd{FpXQ} (modulo $T, p^e$),7550returns $a$ such that $a^2 \equiv b \mod (p^e, T)$.75517552\fun{GEN}{ZpX_ZpXQ_liftroot}{GEN f, GEN a, GEN T, GEN p, long e}7553as \tet{ZpXQX_liftroot}, but $f$ is a polynomial in $\Z[X]$.75547555\fun{GEN}{ZpX_ZpXQ_liftroot_ea}{GEN f, GEN a, GEN T, GEN p, long e,7556void *E, GEN early(void *E, GEN x, GEN q)}7557as \tet{ZpX_ZpXQ_liftroot} with early abort: the function \kbd{early(E,x,q)}7558will be called with $x$ is a root of $f$ modulo $q=p^n$ for some $n$. If7559\kbd{early} returns a non-\kbd{NULL} value $z$, the function returns $z$7560immediately.75617562\fun{GEN}{ZpXQ_log}{GEN a, GEN T, GEN p, long e} $T$ being a \kbd{ZpX}7563irreducible modulo $p$, return the logarithm of $a$ in $\Z_p[X]/(T)$ to7564precision $e$, assuming that $a\equiv 1 \pmod{p\Z_p[X]}$ if $p$ odd or7565$a\equiv 1 \pmod{4\Z_2[X]}$ if $p=2$.75667567\subsec{Zq}75687569\fun{GEN}{Zq_sqrtnlift}{GEN b, GEN n, GEN a, GEN T, GEN p, long e}75707571\subsec{ZpXQM}75727573\fun{GEN}{ZpXQM_prodFrobenius}{GEN M, GEN T, GEN p, long e}7574returns the product of matrices $M\*\sigma(M)\*\sigma^2(M)\ldots\sigma^{n-1}(M)$7575to precision $e$ where $\sigma$ is the lift of the Frobenius automorphism7576over $\Z_p[X]/(T)$ and $n$ is the degree of $T$.75777578\subsec{ZpXQX}75797580\fun{GEN}{ZpXQX_liftfact}{GEN A, GEN B, GEN T, GEN pe, GEN p, long e} is the7581routine underlying \tet{polhensellift}. Here, $p$ is prime, $T(Y)$ defines a7582finite field $\F_q$. $A$ is a polynomial in $\Z[X,Y]$, whose leading7583coefficient is nonzero in $\F_q$. $B$ is a vector of monic or \kbd{FqX},7584pairwise coprime in $\F_q[X]$, whose product is congruent to $A/\text{lc}(A)$7585in $\F_q[X]$. Lifts the elements of $B$ mod $\kbd{pe} = p^e$, such that the7586congruence now holds mod $(T,p^e)$.75877588\fun{GEN}{ZpXQX_liftroot}{GEN f, GEN a, GEN T, GEN p, long e} as7589\tet{ZpX_liftroot}, but $f$ is now a polynomial in $\Z[X,Y]$ and lift the7590root $a$ in the unramified extension of $\Q_p$ with residue field $\F_p[Y]/(T)$,7591assuming $v_p(f(a))>0$ and $v_p(f'(a))=0$.75927593\fun{GEN}{ZpXQX_liftroot_vald}{GEN f, GEN a, long v, GEN T, GEN p, long e}7594returns the foots of $f$ as \tet{ZpXQX_liftroot}, where $v$ is the valuation7595of the content of $f'$ and it is required that $v_p(f(a))>v$ and7596$v_p(f'(a))=v$.75977598\fun{GEN}{ZpXQX_roots}{GEN F, GEN T, GEN p, long e}75997600\fun{GEN}{ZpXQX_liftroots}{GEN F, GEN S, GEN T, GEN p, long e}76017602\fun{GEN}{ZpXQX_divrem}{GEN x, GEN Sp, GEN T,GEN q,GEN p,long e, GEN *pr}7603as \kbd{FpXQX\_divrem}. The parameter $q$ must be equal to $p^e$.76047605\fun{GEN}{ZpXQX_digits}{GEN x, GEN B, GEN T, GEN q, GEN p, long e}7606As \kbd{FpXQX\_digits}. The parameter $q$ must be equal to $p^e$.76077608\subsec{ZqX}76097610\fun{GEN}{ZqX_roots}{GEN F, GEN T, GEN p, long e}76117612\fun{GEN}{ZqX_liftfact}{GEN A, GEN B, GEN T, GEN pe, GEN p, long e}76137614\fun{GEN}{ZqX_liftroot}{GEN f, GEN a, GEN T, GEN p, long e}76157616\subsec{Other $p$-adic functions}76177618\fun{GEN}{ZpM_echelon}{GEN M, long early_abort, GEN p, GEN pm} given a7619\kbd{ZM} $M$, a prime $p$ and $\kbd{pm} = p^m$, returns an echelon form7620$E$ for $M$ mod $p^m$. I.e. there exist a square integral matrix $U$ with7621$\det U$ coprime to $p$ such that $E = MU$ modulo $p^m$. I7622\kbd{early\_abort} is nonzero, return NULL as soon as one pivot in7623the echelon form is divisible by $p^m$. The echelon form is an upper7624triangular HNF, we do not waste time to reduce it to Gauss-Jordan form.76257626\fun{GEN}{zlm_echelon}{GEN M, long early_abort, ulong p, ulong pm}7627variant of \kbd{ZpM\_echelon}, for a \kbd{Zlm} $M$.76287629\fun{GEN}{ZlM_gauss}{GEN a, GEN b, ulong p, long e, GEN C} as \kbd{gauss}7630with the following peculiarities: $a$ and $b$ are \kbd{ZM}, such that $a$ is7631invertible modulo $p$. Optional $C$ is an \kbd{Flm} that is an inverse of7632$a\mod p$ or \kbd{NULL}. Return the matrix $x$ such that $ax=b\mod p^e$ and7633all elements of $x$ are in $[0,p^e-1]$. For efficiency, it is better7634to reduce $a$ and $b$ mod $p^e$ first.76357636\fun{GEN}{padic_to_Q}{GEN x} truncate the \typ{PADIC} to a \typ{INT} or7637\typ{FRAC}.76387639\fun{GEN}{padic_to_Q_shallow}{GEN x} shallow version of \tet{padic_to_Q}76407641\fun{GEN}{QpV_to_QV}{GEN v} apply \tet{padic_to_Q_shallow}76427643\fun{long}{padicprec}{GEN x, GEN p} returns the absolute $p$-adic precision of7644the object $x$, by definition the minimum precision of the components of $x$.7645For a nonzero \typ{PADIC}, this returns \kbd{valp(x) + precp(x)}.76467647\fun{long}{padicprec_relative}{GEN x} returns the relative $p$-adic7648precision of the \typ{INT}, \typ{FRAC}, or \typ{PADIC} $x$ (minimum precision7649of the components of $x$ for \typ{POL} or vector/matrices).7650For a \typ{PADIC}, this returns \kbd{precp(x)} if $x\neq0$, and $0$ for $x=0$.76517652\subsubsec{low-level}76537654The following technical function returns an optimal sequence of $p$-adic7655accuracies, for a given target accuracy:76567657\fun{ulong}{quadratic_prec_mask}{long n} we want to reach accuracy7658$n\geq 1$, starting from accuracy 1, using a quadratically convergent,7659self-correcting, algorithm; in other words, from inputs correct to accuracy7660$l$ one iteration outputs a result correct to accuracy $2l$.7661For instance, to reach $n = 9$, we want to use accuracies7662$[1,2,3,5,9]$ instead of $[1,2,4,8,9]$. The idea is to essentially double7663the accuracy at each step, and not overshoot in the end.76647665Let $a_0$ = 1, $a_1 = 2, \ldots, a_k = n$, be the desired sequence of7666accuracies. To obtain it, we work backwards and set7667$$ a_k = n,\quad a_{i-1} = (a_i + 1)\,\bs\, 2.$$7668This is in essence what the function returns.7669But we do not want to store the $a_i$ explicitly, even as a \typ{VECSMALL},7670since this would leave an object on the stack. Instead, we store $a_i$7671implicitly in a bitmask \kbd{MASK}: let $a_0 = 1$, if the $i$-th bit of the7672mask is set, set $a_{i+1} = 2a_i - 1$, and $2a_i$ otherwise; in short the7673bits indicate the places where we do something special and do not quite7674double the accuracy (which would be the straightforward thing to do).76757676In fact, to avoid returning separately the mask and the sequence length7677$k+1$, the function returns $\kbd{MASK} + 2^{k+1}$, so the highest bit of7678the mask indicates the length of the sequence, and the following ones give7679an algorithm to obtain the accuracies. This is much simpler than it sounds,7680here is what it looks like in practice:7681\bprog7682ulong mask = quadratic_prec_mask(n);7683long l = 1;7684while (mask > 1) { /* here, the result is known to accuracy l */7685l = 2*l; if (mask & 1) l--; /* new accuracy l for the iteration */7686mask >>= 1; /* pop low order bit */7687/* ... lift to the new accuracy ... */7688}7689/* we are done. At this point l = n */7690@eprog\noindent We just pop the bits in \kbd{mask} starting from the low7691order bits, stop when \kbd{mask} is $1$ (that last bit corresponds to the7692$2^{k+1}$ that we added to the mask proper). Note that there is nothing7693specific to Hensel lifts in that function: it would work equally well for7694an Archimedean Newton iteration.76957696Note that in practice, we rather use an infinite loop, and insert an7697\bprog7698if (mask == 1) break;7699@eprog\noindent in the middle of the loop: the loop body usually includes7700preparations for the next iterations (e.g. lifting Bezout coefficients7701in a quadratic Hensel lift), which are costly and useless in the \emph{last}7702iteration.77037704\subsec{Conversions involving single precision objects}77057706\subsubsec{To single precision}77077708\fun{ulong}{Rg_to_Fl}{GEN z, ulong p}, \kbd{z} which can be mapped to7709$\Z/p\Z$: a \typ{INT}, a \typ{INTMOD} whose modulus is divisible by $p$,7710a \typ{FRAC} whose denominator is coprime to $p$, or a \typ{PADIC} with7711underlying prime $\ell$ satisfying $p = \ell^n$ for some $n$ (less than the7712accuracy of the input). Returns \kbd{lift(z * Mod(1,p))}, normalized, as an7713\kbd{Fl}.77147715\fun{ulong}{Rg_to_F2}{GEN z}, as \tet{Rg_to_Fl} for $p = 2$.77167717\fun{ulong}{padic_to_Fl}{GEN x, ulong p} special case of \tet{Rg_to_Fl},7718for a $x$ a \typ{PADIC}.77197720\fun{GEN}{RgX_to_F2x}{GEN x}, \kbd{x} a \typ{POL}, returns the7721\kbd{F2x} obtained by applying \kbd{Rg\_to\_Fl} coefficientwise.77227723\fun{GEN}{RgX_to_Flx}{GEN x, ulong p}, \kbd{x} a \typ{POL}, returns the7724\kbd{Flx} obtained by applying \kbd{Rg\_to\_Fl} coefficientwise.77257726\fun{GEN}{RgXV_to_FlxV}{GEN x, ulong p}, \kbd{x} a vector, returns the7727\kbd{FlxV} obtained by applying \kbd{RgX\_to\_Flx} coefficientwise.77287729\fun{GEN}{Rg_to_F2xq}{GEN z, GEN T}, \kbd{z} a \kbd{GEN} which can be7730mapped to $\F_2[X]/(T)$: anything \kbd{Rg\_to\_Fl} can be applied to,7731a \typ{POL} to which \kbd{RgX\_to\_F2x} can be applied to, a \typ{POLMOD}7732whose modulus is divisible by $T$ (once mapped to a \kbd{F2x}), a suitable7733\typ{RFRAC}. Returns \kbd{z} as an \kbd{F2xq}, normalized.77347735\fun{GEN}{Rg_to_Flxq}{GEN z, GEN T, ulong p}, \kbd{z} a \kbd{GEN} which can be7736mapped to $\F_p[X]/(T)$: anything \kbd{Rg\_to\_Fl} can be applied to,7737a \typ{POL} to which \kbd{RgX\_to\_Flx} can be applied to, a \typ{POLMOD}7738whose modulus is divisible by $T$ (once mapped to a \kbd{Flx}), a suitable7739\typ{RFRAC}. Returns \kbd{z} as an \kbd{Flxq}, normalized.77407741\fun{GEN}{RgX_to_FlxqX}{GEN z, GEN T, ulong p}, \kbd{z} a \kbd{GEN} which can be7742mapped to $\F_p[x]/(T)[X]$: anything \kbd{Rg\_to\_Flxq} can be applied to,7743a \typ{POL} to which \kbd{RgX\_to\_Flx} can be applied to, a \typ{POLMOD}7744whose modulus is divisible by $T$ (once mapped to a \kbd{Flx}), a suitable7745\typ{RFRAC}. Returns \kbd{z} as an \kbd{FlxqX}, normalized.77467747\fun{GEN}{ZX_to_Flx}{GEN x, ulong p} reduce \kbd{ZX}~\kbd{x} modulo \kbd{p}7748(yielding an \kbd{Flx}). Faster than \kbd{RgX\_to\_Flx}.77497750\fun{GEN}{ZV_to_Flv}{GEN x, ulong p} reduce \kbd{ZV}~\kbd{x} modulo \kbd{p}7751(yielding an \kbd{Flv}).77527753\fun{GEN}{ZXV_to_FlxV}{GEN v, ulong p}, as \kbd{ZX\_to\_Flx}, repeatedly7754called on the vector's coefficients.77557756\fun{GEN}{ZXT_to_FlxT}{GEN v, ulong p}, as \kbd{ZX\_to\_Flx}, repeatedly7757called on the tree leaves.77587759\fun{GEN}{ZXX_to_FlxX}{GEN B, ulong p, long v}, as \kbd{ZX\_to\_Flx},7760repeatedly called on the polynomial's coefficients.77617762\fun{GEN}{zxX_to_FlxX}{GEN z, ulong p} as \kbd{zx\_to\_Flx},7763repeatedly called on the polynomial's coefficients.77647765\fun{GEN}{ZXXV_to_FlxXV}{GEN V, ulong p, long v}, as \kbd{ZXX\_to\_FlxX},7766repeatedly called on the vector's coefficients.77677768\fun{GEN}{ZXXT_to_FlxXT}{GEN V, ulong p, long v}, as \kbd{ZXX\_to\_FlxX},7769repeatedly called on the tree leaves.77707771\fun{GEN}{RgV_to_Flv}{GEN x, ulong p} reduce the \typ{VEC}/\typ{COL}7772$x$ modulo $p$, yielding a \typ{VECSMALL}.77737774\fun{GEN}{RgM_to_Flm}{GEN x, ulong p} reduce the \typ{MAT} $x$ modulo $p$.77757776\fun{GEN}{ZM_to_Flm}{GEN x, ulong p} reduce \kbd{ZM}~$x$ modulo $p$7777(yielding an \kbd{Flm}).77787779\fun{GEN}{ZXC_to_FlxC}{GEN x, ulong p, long sv} reduce \kbd{ZXC}~$x$ modulo $p$7780(yielding an \kbd{FlxC}). Assume that \kbd{sv = evalvarn(v)} where $v$ is7781the variable number of the entries of $x$. It is allowed for the entries of7782$x$ to be \typ{INT}.77837784\fun{GEN}{ZXM_to_FlxM}{GEN x, ulong p, long sv} reduce \kbd{ZXM}~$x$ modulo $p$7785(yielding an \kbd{FlxM}). Assume that \kbd{sv = evalvarn(v)} where $v$ is7786the variable number of the entries of $x$. It is allowed for the entries of $x$7787to be \typ{INT}.77887789\fun{GEN}{ZV_to_zv}{GEN z}, converts coefficients using \kbd{itos}77907791\fun{GEN}{ZV_to_nv}{GEN z}, converts coefficients using \kbd{itou}77927793\fun{GEN}{ZM_to_zm}{GEN z}, converts coefficients using \kbd{itos}77947795\subsubsec{From single precision}77967797\fun{GEN}{Flx_to_ZX}{GEN z}, converts to \kbd{ZX} (\typ{POL} of nonnegative7798\typ{INT}s in this case)77997800\fun{GEN}{Flx_to_FlxX}{GEN z}, converts to \kbd{FlxX} (\typ{POL} of constant7801\kbd{Flx} in this case).78027803\fun{GEN}{Flx_to_ZX_inplace}{GEN z}, same as \kbd{Flx\_to\_ZX}, in place7804(\kbd{z} is destroyed).78057806\fun{GEN}{FlxX_to_ZXX}{GEN B}, converts an \kbd{FlxX} to a polynomial with7807\kbd{ZX} or \typ{INT} coefficients (repeated calls to \kbd{Flx\_to\_ZX}).78087809\fun{GEN}{FlxXC_to_ZXXC}{GEN B}, converts an \kbd{FlxXC} to a \typ{COL} with7810\kbd{ZXX} coefficients (repeated calls to \kbd{FlxX\_to\_ZXX}).78117812\fun{GEN}{FlxXM_to_ZXXM}{GEN B}, converts an \kbd{FlxXM} to a \typ{MAT} with7813\kbd{ZXX} coefficients (repeated calls to \kbd{FlxX\_to\_ZXX}).78147815\fun{GEN}{FlxC_to_ZXC}{GEN x}, converts a vector of \kbd{Flx} to a column7816vector of polynomials with \typ{INT} coefficients (repeated calls to7817\kbd{Flx\_to\_ZX}).78187819\fun{GEN}{FlxV_to_ZXV}{GEN x}, as above but return a \typ{VEC}.78207821\fun{void}{F2xV_to_FlxV_inplace}{GEN v} v is destroyed.78227823\fun{void}{F2xV_to_ZXV_inplace}{GEN v} v is destroyed.78247825\fun{void}{FlxV_to_ZXV_inplace}{GEN v} v is destroyed.78267827\fun{GEN}{FlxM_to_ZXM}{GEN z}, converts a matrix of \kbd{Flx} to a matrix of7828polynomials with \typ{INT} coefficients (repeated calls to \kbd{Flx\_to\_ZX}).78297830\fun{GEN}{zx_to_ZX}{GEN z}, as \kbd{Flx\_to\_ZX}, without assuming7831the coefficients to be nonnegative.78327833\fun{GEN}{zx_to_Flx}{GEN z, ulong p} as \kbd{Flx\_red} without assuming7834the coefficients to be nonnegative.78357836\fun{GEN}{Flc_to_ZC}{GEN z}, converts to \kbd{ZC} (\typ{COL} of nonnegative7837\typ{INT}s in this case)78387839\fun{GEN}{Flc_to_ZC_inplace}{GEN z}, same as \kbd{Flc\_to\_ZC}, in place7840(\kbd{z} is destroyed).78417842\fun{GEN}{Flv_to_ZV}{GEN z}, converts to \kbd{ZV} (\typ{VEC} of nonnegative7843\typ{INT}s in this case)78447845\fun{GEN}{Flm_to_ZM}{GEN z}, converts to \kbd{ZM} (\typ{MAT} with7846nonnegative \typ{INT}s coefficients in this case)78477848\fun{GEN}{Flm_to_ZM_inplace}{GEN z}, same as \kbd{Flm\_to\_ZM}, in place7849(\kbd{z} is destroyed).78507851\fun{GEN}{zc_to_ZC}{GEN z} as \kbd{Flc\_to\_ZC}, without assuming7852coefficients are nonnegative.78537854\fun{GEN}{zv_to_ZV}{GEN z} as \kbd{Flv\_to\_ZV}, without assuming7855coefficients are nonnegative.78567857\fun{GEN}{zm_to_ZM}{GEN z} as \kbd{Flm\_to\_ZM}, without assuming7858coefficients are nonnegative.78597860\fun{GEN}{zv_to_Flv}{GEN z, ulong p}78617862\fun{GEN}{zm_to_Flm}{GEN z, ulong p}78637864\subsubsec{Mixed precision linear algebra} Assumes dimensions are compatible.7865Multiply a multiprecision object by a single-precision one.78667867\fun{GEN}{RgM_zc_mul}{GEN x, GEN y}78687869\fun{GEN}{RgMrow_zc_mul}{GEN x, GEN y, long i}78707871\fun{GEN}{RgM_zm_mul}{GEN x, GEN y}78727873\fun{GEN}{RgV_zc_mul}{GEN x, GEN y}78747875\fun{GEN}{RgV_zm_mul}{GEN x, GEN y}78767877\fun{GEN}{ZM_zc_mul}{GEN x, GEN y}78787879\fun{GEN}{zv_ZM_mul}{GEN x, GEN y}78807881\fun{GEN}{ZV_zc_mul}{GEN x, GEN y}78827883\fun{GEN}{ZM_zm_mul}{GEN x, GEN y}78847885\fun{GEN}{ZC_z_mul}{GEN x, long y}78867887\fun{GEN}{ZM_nm_mul}{GEN x, GEN y} the entries of $y$ are \kbd{ulong}s.78887889\fun{GEN}{nm_Z_mul}{GEN y, GEN c} the entries of $y$ are \kbd{ulong}s.78907891\subsubsec{Miscellaneous involving Fl}78927893\fun{GEN}{Fl_to_Flx}{ulong x, long evx} converts a \kbd{unsigned long} to a7894scalar \kbd{Flx}. Assume that \kbd{evx = evalvarn(vx)} for some variable7895number \kbd{vx}.78967897\fun{GEN}{Z_to_Flx}{GEN x, ulong p, long sv} converts a \typ{INT} to a scalar7898\kbd{Flx} polynomial. Assume that \kbd{sv = evalvarn(v)} for some variable7899number \kbd{v}.79007901\fun{GEN}{Flx_to_Flv}{GEN x, long n} converts from \kbd{Flx} to \kbd{Flv}7902with \kbd{n} components (assumed larger than the number of coefficients of7903\kbd{x}).79047905\fun{GEN}{zx_to_zv}{GEN x, long n} as \kbd{Flx\_to\_Flv}.79067907\fun{GEN}{Flv_to_Flx}{GEN x, long sv} converts from vector (coefficient7908array) to (normalized) polynomial in variable $v$.79097910\fun{GEN}{zv_to_zx}{GEN x, long n} as \kbd{Flv\_to\_Flx}.79117912\fun{GEN}{Flm_to_FlxV}{GEN x, long sv} converts the columns of7913\kbd{Flm}~\kbd{x} to an array of \kbd{Flx} in the variable $v$7914(repeated calls to \kbd{Flv\_to\_Flx}).79157916\fun{GEN}{zm_to_zxV}{GEN x, long n} as \kbd{Flm\_to\_FlxV}.79177918\fun{GEN}{Flm_to_FlxX}{GEN x, long sw, long sv} same as7919\kbd{Flm\_to\_FlxV(x,sv)} but returns the result as a (normalized) polynomial7920in variable $w$.79217922\fun{GEN}{FlxV_to_Flm}{GEN v, long n} reverse \kbd{Flm\_to\_FlxV}, to obtain7923an \kbd{Flm} with \kbd{n} rows (repeated calls to \kbd{Flx\_to\_Flv}).79247925\fun{GEN}{FlxX_to_Flx}{GEN P} Let $P(x,X)$ be a \kbd{FlxX}, return $P(0,X)$7926as a \kbd{Flx}.79277928\fun{GEN}{FlxX_to_Flm}{GEN v, long n} reverse \kbd{Flm\_to\_FlxX}, to obtain7929an \kbd{Flm} with \kbd{n} rows (repeated calls to \kbd{Flx\_to\_Flv}).79307931\fun{GEN}{FlxX_to_FlxC}{GEN B, long n, long sv} see \kbd{RgX\_to\_RgV}.7932The coefficients of \kbd{B} are assumed to be in the variable $v$.79337934\fun{GEN}{FlxV_to_FlxX}{GEN x, long v} see \kbd{RgV\_to\_RgX}.79357936\fun{GEN}{FlxXV_to_FlxM}{GEN V, long n, long sv} see \kbd{RgXV\_to\_RgM}.7937The coefficients of \kbd{V[i]} are assumed to be in the variable $v$.79387939\fun{GEN}{Fly_to_FlxY}{GEN a, long sv} convert coefficients of \kbd{a} to7940constant \kbd{Flx} in variable $v$.79417942\subsubsec{Miscellaneous involving \kbd{F2x}}79437944\fun{GEN}{F2x_to_F2v}{GEN x, long n} converts from \kbd{F2x} to \kbd{F2v}7945with \kbd{n} components (assumed larger than the number of coefficients of7946\kbd{x}).79477948\fun{GEN}{F2xC_to_ZXC}{GEN x}, converts a vector of \kbd{F2x} to a column7949vector of polynomials with \typ{INT} coefficients (repeated calls to7950\kbd{F2x\_to\_ZX}).79517952\fun{GEN}{F2xC_to_FlxC}{GEN x}79537954\fun{GEN}{FlxC_to_F2xC}{GEN x}79557956\fun{GEN}{F2xV_to_F2m}{GEN v, long n} \kbd{F2x\_to\_F2v} to each polynomial7957to get an \kbd{F2m} with \kbd{n} rows.79587959\section{Higher arithmetic over $\Z$: primes, factorization}79607961\subsec{Pure powers}79627963\fun{long}{Z_issquare}{GEN n} returns $1$ if the \typ{INT} $n$ is7964a square, and $0$ otherwise. This is tested first modulo small prime7965powers, then \kbd{sqrtremi} is called.79667967\fun{long}{Z_issquareall}{GEN n, GEN *sqrtn} as \kbd{Z\_issquare}. If7968$n$ is indeed a square, set \kbd{sqrtn} to its integer square root.7969Uses a fast congruence test mod $64\times 63\times 65\times 11$ before7970computing an integer square root.79717972\fun{long}{Z_ispow2}{GEN x} returns $1$ if the \typ{INT} $x$ is a power of7973$2$, and $0$ otherwise.79747975\fun{long}{uissquare}{ulong n} as \kbd{Z\_issquare},7976for an \kbd{ulong} operand \kbd{n}.79777978\fun{long}{uissquareall}{ulong n, ulong *sqrtn} as \kbd{Z\_issquareall},7979for an \kbd{ulong} operand \kbd{n}.79807981\fun{ulong}{usqrt}{ulong a} returns the floor of the square root of $a$.79827983\fun{ulong}{usqrtn}{ulong a, ulong n} returns the floor of the $n$-th root7984of $a$.79857986\fun{long}{Z_ispower}{GEN x, ulong k} returns $1$ if the \typ{INT} $n$ is a7987$k$-th power, and $0$ otherwise; assume that $k > 1$.79887989\fun{long}{Z_ispowerall}{GEN x, ulong k, GEN *pt} as \kbd{Z\_ispower}. If7990$n$ is indeed a $k$-th power, set \kbd{*pt} to its integer $k$-th root.79917992\fun{long}{Z_isanypower}{GEN x, GEN *ptn} returns the maximal $k\geq 2$ such7993that the \typ{INT} $x = n^k$ is a perfect power, or $0$ if no such $k$ exist;7994in particular \kbd{ispower(1)}, \kbd{ispower(0)}, \kbd{ispower(-1)} all7995return 0. If the return value $k$ is not $0$ (so that $x = n^k$) and7996\kbd{ptn} is not \kbd{NULL}, set \kbd{*ptn} to $n$.79977998The following low-level functions are called by \tet{Z_isanypower} but can7999be directly useful:80008001\fun{int}{is_357_power}{GEN x, GEN *ptn, ulong *pmask} tests whether the8002integer $x > 0$ is a $3$-rd, $5$-th or $7$-th power. The bits of \kbd{*mask}8003initially indicate which test is to be performed;8004bit $0$: $3$-rd,8005bit $1$: $5$-th,8006bit $2$: $7$-th (e.g.~$\kbd{*pmask} = 7$ performs all tests). They are8007updated during the call: if the ``$i$-th power'' bit is set to $0$8008then $x$ is not a $k$-th power. The function returns $0$8009(not a8010$3$-rd,8011$5$-th or8012$7$-th power),8013$3$8014($3$-rd power,8015not a $5$-th or8016$7$-th power),8017$5$8018($5$-th power,8019not a $7$-th power),8020or $7$8021($7$-th power); if an $i$-th power bit is initially set to $0$, we take it8022at face value and assume $x$ is not an $i$-th power without performing any8023test. If the return value $k$ is nonzero, set \kbd{*ptn} to $n$ such that $x8024= n^k$.80258026\fun{int}{is_pth_power}{GEN x, GEN *ptn, forprime_t *T, ulong cutoff}8027let $x > 0$ be an integer, $\kbd{cutoff} > 0$ and $T$ be an iterator over8028primes $\geq 11$, we look for the smallest prime $p$ such that $x = n^p$8029(advancing $T$ as we go along). The $11$ is due to the fact that8030\tet{is_357_power} and \kbd{issquare} are faster than the generic version for8031$p < 11$.80328033Fail and return $0$ when the existence of $p$ would imply $2^{\kbd{cutoff}} >8034x^{1/p}$, meaning that a possible $n$ is so small that it should have been8035found by trial division; for maximal speed, you should start by a round of8036trial division, but the cut-off may also be set to $1$ for a rigorous result8037without any trial division.80388039Otherwise returns the smallest suitable prime power $p^i$ and set \kbd{*ptn}8040to the $p^i$-th root of $x$ (which is now not a $p$-th power). We may8041immediately recall the function with the same parameters after setting $x =8042\kbd{*ptn}$: it will start at the next prime.80438044\subsec{Factorization}80458046\fun{GEN}{Z_factor}{GEN n} factors the \typ{INT} \kbd{n}. The ``primes''8047in the factorization are actually strong pseudoprimes.80488049\fun{GEN}{absZ_factor}{GEN n} returns \kbd{Z\_factor(absi(n))}.80508051\fun{long}{Z_issmooth}{GEN n, ulong lim} returns $1$ if all the8052prime factors of the \typ{INT} $n$ are less or equal to $lim$.80538054\fun{GEN}{Z_issmooth_fact}{GEN n, ulong lim} returns \kbd{NULL} if a prime8055factor of the \typ{INT} $n$ is $> lim$, and returns the factorization8056of $n$ otherwise, as a \typ{MAT} with \typ{VECSMALL} columns (word-size8057primes and exponents). Neither memory-clean nor suitable for8058\kbd{gerepileupto}.80598060\fun{GEN}{Z_factor_until}{GEN n, GEN lim} as \kbd{Z\_factor}, but stop the8061factorization process as soon as the unfactored part is smaller than \kbd{lim}.8062The resulting factorization matrix only contains the factors found. No other8063assumptions can be made on the remaining factors.80648065\fun{GEN}{Z_factor_limit}{GEN n, ulong lim} trial divide $n$ by all primes $p8066< \kbd{lim}$ in the precomputed list of prime numbers and the \kbd{addprimes}8067prime table. Return the corresponding factorization matrix. The first column8068of the factorization matrix may contain a single composite, which may8069or may not be the last entry in presence of a prime table.80708071If $\kbd{lim} = 0$, the effect is the same as setting $\kbd{lim} =8072\kbd{maxprime()} + 1$: use all precomputed primes.80738074\fun{GEN}{absZ_factor_limit}{GEN n, ulong all} returns8075\kbd{Z\_factor\_limit(absi(n))}.80768077\fun{GEN}{absZ_factor_limit_strict}{GEN n, ulong all, GEN *pU} analogous to8078\tet{absZ_factor_limit}, with a better interface: trial divide $n$ by all8079primes $p < \kbd{lim}$ in the precomputed list of prime numbers and the8080\kbd{addprimes} prime table. Return the corresponding factorization matrix.8081In this case, a composite cofactor is \emph{not} included.80828083If \kbd{pU} is not \kbd{NULL}, set it to the cofactor, which is either8084\kbd{NULL} (no cofactor) or $[q,k]$, where $k > 0$, $q$ is a composite8085whose prime divisors are greater than \kbd{all}, not a pure power8086and $q^k$ is the largest power of $q$ dividing $n$.80878088\fun{GEN}{boundfact}{GEN x, ulong lim} as \tet{Z_factor_limit}, applying to8089\typ{INT} or \typ{FRAC} inputs.80908091\fun{GEN}{Z_smoothen}{GEN n, GEN L, GEN *pP, GEN *pE} given a \typ{VEC}8092$L$ containing a list of primes and a \typ{INT} $n$, trial divide8093$n$ by the elements of $L$ and return the cofactor. Return \kbd{NULL} if the8094cofactor is $\pm 1$. \kbd{*P} and \kbd{*E} contain the list of prime divisors8095found and their exponents, as \typ{VECSMALL}s. Neither memory-clean, nor8096suitable for \tet{gerepileupto}.80978098\fun{GEN}{Z_lsmoothen}{GEN n, GEN L, GEN *pP, GEN *pE} as8099\kbd{Z\_smoothen} where $L$ is a \typ{VECSMALL} of small primes and both8100\kbd{*P} and \kbd{*E} are given as \typ{VECSMALL}.81018102\fun{GEN}{Z_factor_listP}{GEN N, GEN L} given a \typ{INT} $N$, a vector or8103primes $L$ containing all prime divisors of $N$ (and possibly others). Return8104\kbd{factor(N)}. Neither memory-clean, nor suitable for \tet{gerepileupto}.81058106\fun{GEN}{factor_pn_1}{GEN p, ulong n} returns the factorization of $p^n-1$,8107where $p$ is prime and $n$ is a positive integer.81088109\fun{GEN}{factor_pn_1_limit}{GEN p, ulong n, ulong B} returns a partial8110factorization of $p^n-1$, where $p$ is prime and $n$ is a positive integer.8111Don't actively search for prime divisors $p > B$, but we may find still find8112some due to Aurifeuillian factorizations. Any entry $> B^2$ in the output8113factorization matrix is \emph{a priori} not a prime (but may well be).81148115\fun{GEN}{factor_Aurifeuille_prime}{GEN p, long n} an Aurifeuillian factor8116of $\phi_n(p)$, assuming $p$ prime and an Aurifeuillian factor exists8117($p \zeta_n$ is a square in $\Q(\zeta_n)$).81188119\fun{GEN}{factor_Aurifeuille}{GEN a, long d} an Aurifeuillian factor of8120$\phi_n(a)$, assuming $a$ is a nonzero integer and $n > 2$. Returns $1$8121if no Aurifeuillian factor exists.81228123\fun{GEN}{odd_prime_divisors}{GEN a} \typ{VEC} of all prime divisors of the8124\typ{INT} $a$.81258126\fun{GEN}{factoru}{ulong n}, returns the factorization of $n$. The result8127is a $2$-component vector $[P,E]$, where $P$ and $E$ are \typ{VECSMALL}8128containing the prime divisors of $n$, and the $v_p(n)$.81298130\fun{GEN}{factoru_pow}{ulong n}, returns the factorization of $n$. The result8131is a $3$-component vector $[P,E,C]$, where $P$, $E$ and $C$ are8132\typ{VECSMALL} containing the prime divisors of $n$, the $v_p(n)$8133and the $p^{v_p(n)}$.81348135\fun{GEN}{vecfactoru}{ulong a, ulong b}, returns a \typ{VEC} $v$ containing8136the factorizations (\tet{factoru} format) of $a,\dots, b$; assume that $b8137\geq a > 0$. Uses a sieve with primes up to $\sqrt{b}$. For all8138$c$, $a \leq c \leq b$, the factorization of $c$ is given in $v[c-a+1]$.81398140\fun{GEN}{vecfactoroddu}{ulong a, ulong b}, returns a \typ{VEC} $v$ containing8141the factorizations (\tet{factoru} format) of odd integers in $a,\dots, b$;8142assume that $b \geq a > 0$ are odd. Uses a sieve with primes up to8143$\sqrt{b}$. For all odd $c$, $a \leq c \leq b$, the factorization of $c$ is8144given in in $v[(c-a)/2 + 1]$.81458146\fun{GEN}{vecfactoru_i}{ulong a, ulong b}, private version of8147\kbd{vecfactoru}, not memory clean.81488149\fun{GEN}{vecfactoroddu_i}{ulong a, ulong b}, private version of8150\kbd{vecfactoroddu}, not memory clean.81518152\fun{GEN}{vecfactorsquarefreeu}{ulong a, ulong b} return a \typ{VEC} $v$8153containing the prime divisors of squarefree integers in $a,\dots,b$; assume8154that $a \leq b$. Uses a sieve with primes up to $\sqrt{b}$.8155For all squarefree $c$, $a\leq c\leq b$, the prime divisors of $c$ (as a8156\typ{VECSMALL}) are given in $v[c-a+1]$, and the other entries are8157\kbd{NULL}. Note that because of these \kbd{NULL} markers, $v$ is not a valid8158\kbd{GEN}, it is not memory clean and cannot be used in garbage collection8159routines.81608161\fun{GEN}{vecfactorsquarefreeu_coprime}{ulong a, ulong b, GEN P}8162given a \emph{sorted} \typ{VECSMALL} of primes $P$, return a \typ{VEC} $v$8163containing the prime divisors of squarefree integers in $a,\dots,b$ coprime to8164the elements of $P$; assume that $a \leq b$. Uses a sieve with primes up to8165$\sqrt{b}$. For all squarefree $c$, $a\leq c\leq b$, the prime divisors of $c$8166(as a \typ{VECSMALL}) are given in $v[c-a+1]$, and the other entries are8167\kbd{NULL}. Note that because of these \kbd{NULL} markers, $v$ is not a valid8168\kbd{GEN}, it is not memory clean and cannot be used in garbage collection8169routines.81708171\fun{GEN}{vecsquarefreeu}{ulong a, ulong b} return a \typ{VECSMALL} $v$8172containing the squarefree integers in $a,\dots,b$. Assume that8173$a\leq b$. Uses a sieve with primes up to $\sqrt{b}$.81748175\fun{ulong}{tridiv_bound}{GEN n} returns the trial division bound used by8176\tet{Z_factor}$(n)$.81778178\fun{GEN}{Z_pollardbrent}{GEN N, long n, long seed} try to factor8179\typ{INT} $N$ using $n\geq 1$ rounds of Pollard iterations; \var{seed} is an8180integer whose value (mod $8$) selects the quadratic polynomial use to8181generate Pollard's (pseudo)random walk. Returns \kbd{NULL} on failure, else a8182vector of 2 (possibly 3) integers whose product is $N$.81838184\fun{GEN}{Z_ECM}{GEN N, long n, long seed, ulong B1} try to8185factor \typ{INT} $N$ using $n\geq 1$ rounds of ECM iterations (on $8$ to $64$8186curves simultaneously, depending on the size of $N$); \var{seed} is an8187integer whose value selects the curves to be used: increase it by $64n$ to8188make sure that a subsequent call with a factor of $N$ uses a disjoint set of8189curves.8190Finally $B_1 > 7$ determines the computations performed on the8191curves: we compute $[k]P$ for some point in $E(\Z/N\Z)$ and $k = q \prod8192p^{e_p}$ where $p^{e_p} \leq B_1$ and $q \leq B_2 := 110 B_1$; a higher value8193of $B_1$ means higher chances of hitting a factor and more time spent.8194The computation is deterministic for a given set of parameters. Returns8195\kbd{NULL} on failure, else a nontrivial factor or \kbd{N}.81968197\fun{GEN}{Q_factor}{GEN x} as \tet{Z_factor}, where $x$ is a \typ{INT} or8198a \typ{FRAC}.81998200\fun{GEN}{Q_factor_limit}{GEN x, ulong lim} as \tet{Z_factor_limit}, where8201$x$ is a \typ{INT} or a \typ{FRAC}.82028203\subsec{Coprime factorization}82048205Given $a$ and $b$ two nonzero integers, let \teb{ppi}$(a,b)$, \teb{ppo}$(a,b)$,8206\teb{ppg}$(a,b)$, \teb{pple}$(a,b)$ (powers in $a$ of primes inside $b$,8207outside $b$, greater than those in $b$, less than or equal to those in $b$) be8208the integers defined by82098210\item $v_p(\text{ppi}) = v_p(a) [v_p(b) > 0]$,82118212\item $v_p(\text{ppo}) = v_p(a) [v_p(b) = 0]$,82138214\item $v_p(\text{ppg}) = v_p(a) [v_p(a) > v_p(b)]$,82158216\item $v_p(\text{pple}) = v_p(a) [v_p(a) \leq v_p(b)]$.82178218\fun{GEN}{Z_ppo}{GEN a, GEN b} returns $\text{ppo}(a,b)$; shallow function.82198220\fun{ulong}{u_ppo}{ulong a, ulong b} returns $\text{ppo}(a,b)$.82218222\fun{GEN}{Z_ppgle}{GEN a, GEN b} returns $[\text{ppg}(a,b), \text{pple}(a,b)]$;8223shallow function.82248225\fun{GEN}{Z_ppio}{GEN a, GEN b} returns8226$[\gcd(a,b), \text{ppi}(a,b), \text{ppo}(a,b)]$; shallow function.82278228\fun{GEN}{Z_cba}{GEN a, GEN b} fast natural coprime base algorithm. Returns a8229vector of coprime divisors of $a$ and $b$ such that both $a$ and $b$ can8230be multiplicatively generated from this set. Perfect powers are not removed,8231use \tet{Z_isanypower} if needed; shallow function.82328233\fun{GEN}{ZV_cba_extend}{GEN P, GEN b} extend a coprime basis $P$ by the8234integer $b$, the result being a coprime basis for $P\cup \{b\}$.8235Perfect powers are not removed; shallow function.82368237\fun{GEN}{ZV_cba}{GEN v} given a vector of nonzero integers $v$, return8238a coprime basis for $v$. Perfect powers are not removed; shallow function.82398240\subsec{Checks attached to arithmetic functions}82418242Arithmetic functions accept arguments of the following kind: a plain positive8243integer $N$ (\typ{INT}), the factorization \var{fa} of a positive integer (a8244\typ{MAT} with two columns containing respectively primes and exponents), or8245a vector $[N,\var{fa}]$. A few functions accept nonzero8246integers (e.g.~\tet{omega}), and some others arbitrary integers8247(e.g.~\tet{factorint}, \dots).82488249\fun{int}{is_Z_factorpos}{GEN f} returns $1$ if $f$ looks like the8250factorization of a positive integer, and $0$ otherwise. Useful for sanity8251checks but not 100\% foolproof. Specifically, this routine checks that $f$ is8252a two-column matrix all of whose entries are positive integers. It does8253\emph{not} check that entries in the first column (``primes'') are prime,8254or even pairwise coprime, nor that they are stricly increasing.82558256\fun{int}{is_Z_factornon0}{GEN f} returns $1$ if $f$ looks like the8257factorization of a nonzero integer, and $0$ otherwise. Useful for sanity8258checks but not 100\% foolproof, analogous to \tet{is_Z_factorpos}. (Entries8259in the first column need only be nonzero integers.)82608261\fun{int}{is_Z_factor}{GEN f} returns $1$ if $f$ looks like the8262factorization of an integer, and $0$ otherwise. Useful for sanity8263checks but not 100\% foolproof. Specifically, this routine checks that $f$ is8264a two-column matrix all of whose entries are integers. Entries in the second8265column (``exponents'') are all positive. Either it encodes the8266``factorization'' $0^e$, $e > 0$, or entries in the first column (``primes'')8267are all nonzero.82688269\fun{GEN}{clean_Z_factor}{GEN f} assuming $f$ is the factorization of an8270integer $n$, return the factorization of $|n|$, i.e.~remove $-1$ from the8271factorization. Shallow function.82728273\fun{GEN}{fuse_Z_factor}{GEN f, GEN B} assuming $f$ is the8274factorization of an integer $n$, return \kbd{boundfact(n, B)}, i.e.8275return a factorization where all primary factors for $|p| \leq B$8276are preserved, and all others are ``fused'' into a single composite8277integer; if that remainder is trivial, i.e.~equal to 1, it is of course8278not included. Shallow function.82798280In the following three routines, $f$ is the name of an arithmetic function,8281and $n$ a supplied argument. They all raise exceptions if $n$ does not8282correspond to an integer or an integer factorization of the expected shape.82838284\fun{GEN}{check_arith_pos}{GEN n, const char *f} check whether $n$8285is attached to the factorization of a positive integer, and return8286\kbd{NULL} (plain \typ{INT}) or a factorization extracted from $n$ otherwise.8287May raise an \tet{e_DOMAIN} ($n \leq 0$) or an \tet{e_TYPE} exception (other8288failures).82898290\fun{GEN}{check_arith_non0}{GEN n, const char *f} check whether $n$8291is attached to the factorization of a nonzero integer, and return8292\kbd{NULL} (plain \typ{INT}) or a factorization extracted from $n$ otherwise.8293May raise an \tet{e_TYPE} exception.82948295\fun{GEN}{check_arith_all}{GEN n, const char *f}8296is attached to the factorization of an integer, and return \kbd{NULL}8297(plain \typ{INT}) or a factorization extracted from $n$ otherwise.82988299\subsec{Incremental integer factorization}83008301Routines attached to the dynamic factorization of an integer $n$, iterating8302over successive prime divisors. This is useful to implement high-level8303routines allowed to take shortcuts given enough partial information: e.g.8304\kbd{moebius}$(n)$ can be trivially computed if we hit $p$ such that $p^28305\mid n$. For efficiency, trial division by small primes should have already8306taken place. In any case, the functions below assume that no prime $< 2^{14}$8307divides $n$.83088309\fun{GEN}{ifac_start}{GEN n, int moebius} schedules a new factorization8310attempt for the integer $n$. If \kbd{moebius} is nonzero, the factorization8311will be aborted as soon as a repeated factor is detected (Moebius mode).8312The function assumes that $n > 1$ is a \emph{composite} \typ{INT} whose prime8313divisors satisfy $p > 2^{14}$ \emph{and} that one can write to $n$ in place.83148315This function stores data on the stack, no \kbd{gerepile} call should8316delete this data until the factorization is complete. Returns \kbd{partial},8317a data structure recording the partial factorization state.83188319\fun{int}{ifac_next}{GEN *partial, GEN *p, long *e} deletes a primary factor8320$p^e$ from \kbd{partial} and sets \kbd{p} (prime) and \kbd{e} (exponent), and8321normally returns $1$. Whatever remains in the \kbd{partial} structure is now8322coprime to $p$.83238324Returns $0$ if all primary factors have been used already, so we are done8325with the factorization. In this case $p$ is set to \kbd{NULL}. If we ran in8326Moebius mode and the factorization was in fact aborted, we have $e = 1$,8327otherwise $e = 0$.83288329\fun{int}{ifac_read}{GEN part, GEN *k, long *e} peeks at the next integer8330to be factored in the list $k^e$, where $k$ is not necessarily prime8331and can be a perfect power as well, but will be factored by the next call to8332\tet{ifac_next}. You can remove this factorization from the schedule by8333calling:83348335\fun{void}{ifac_skip}{GEN part} removes the next scheduled factorization.83368337\fun{int}{ifac_isprime}{GEN n} given $n$ whose prime divisors are $> 2^{14}$,8338returns the decision the factoring engine would take about the compositeness8339of $n$: $0$ if $n$ is a proven composite, and $1$ if we believe it to be8340prime; more precisely, $n$ is a proven prime if \tet{factor_proven} is8341set, and only a BPSW-pseudoprime otherwise.83428343\subsec{Integer core, squarefree factorization}83448345\fun{long}{Z_issquarefree}{GEN n} returns $1$ if the \typ{INT} \kbd{n}8346is square-free, and $0$ otherwise.83478348\fun{long}{Z_isfundamental}{GEN x} returns $1$ if the \typ{INT} \kbd{x}8349is a fundamental discriminant, and $0$ otherwise.83508351\fun{GEN}{core}{GEN n} unique squarefree integer $d$ dividing $n$ such that8352$n/d$ is a square. The core of $0$ is defined to be $0$.83538354\fun{GEN}{core2}{GEN n} return $[d,f]$ with $d$ squarefree and $n = df^2$.83558356\fun{GEN}{corepartial}{GEN n, long lim} as \kbd{core}, using8357\kbd{boundfact(n,lim)} to partially factor \kbd{n}. The result is not8358necessarily squarefree, but $p^2 \mid n$ implies $p > \kbd{lim}$.83598360\fun{GEN}{core2partial}{GEN n, long lim} as \kbd{core2}, using8361\kbd{boundfact(n,lim)} to partially factor \kbd{n}. The resulting $d$ is not8362necessarily squarefree, but $p^2 \mid n$ implies $p > \kbd{lim}$.83638364\subsec{Primes, primality and compositeness tests}83658366\subsubsec{Chebyshev's $\pi$ function, bounds}83678368\fun{ulong}{uprimepi}{ulong n}, returns the number of primes $p\leq n$8369(Chebyshev's $\pi$ function).83708371\fun{double}{primepi_upper_bound}{double x} return a quick upper bound for8372$\pi(x)$, using Dusart bounds.83738374\fun{GEN}{gprimepi_upper_bound}{GEN x} as \tet{primepi_upper_bound}, returns a8375\typ{REAL}.83768377\fun{double}{primepi_lower_bound}{double x} return a quick lower bound for8378$\pi(x)$, using Dusart bounds.83798380\fun{GEN}{gprimepi_lower_bound}{GEN x} as \tet{primepi_lower_bound}, returns8381a \typ{REAL} or \kbd{gen\_0}.83828383\subsubsec{Primes, primes in intervals}83848385\fun{ulong}{unextprime}{ulong n}, returns the smallest prime $\geq n$. Return8386$0$ if it cannot be represented as an \kbd{ulong} ($n$ bigger than $2^{64} -838759$ or $2^{32} - 5$ depending on the word size).83888389\fun{ulong}{uprecprime}{ulong n}, returns the largest prime $\leq n$. Return8390$0$ if $n\leq 1$.83918392\fun{ulong}{uprime}{long n} returns the $n$-th prime, assuming it fits in an8393\kbd{ulong} (overflow error otherwise).83948395\fun{GEN}{prime}{long n} same as \kbd{utoi(uprime(n))}.83968397\fun{GEN}{primes_zv}{long m} returns the first $m$ primes, in a8398\typ{VECSMALL}.83998400\fun{GEN}{primes}{long m} return the first $m$ primes, as a \typ{VEC} of8401\typ{INT}s.84028403\fun{GEN}{primes_interval}{GEN a, GEN b} return the primes in the interval8404$[a,b]$, as a \typ{VEC} of \typ{INT}s.84058406\fun{GEN}{primes_interval_zv}{ulong a, ulong b} return the primes in the8407interval $[a,b]$, as a \typ{VECSMALL} of \kbd{ulongs}s.84088409\fun{GEN}{primes_upto_zv}{ulong b} return the primes in the interval $[2,b]$,8410as a \typ{VECSMALL} of \kbd{ulongs}s.84118412\subsubsec{Tests}84138414\fun{int}{uisprime}{ulong p}, returns $1$ if \kbd{p} is a prime number and8415$0$ otherwise.84168417\fun{int}{uisprime_101}{ulong p}, assuming that $p$ has no divisor $\leq8418101$, returns $1$ if \kbd{p} is a prime number and $0$ otherwise.84198420\fun{int}{uisprime_661}{ulong p}, assuming that $p$ has no divisor $\leq8421661$, returns $1$ if \kbd{p} is a prime number and $0$ otherwise.84228423\fun{int}{isprime}{GEN n}, returns $1$ if the \typ{INT} \kbd{n} is a8424(fully proven) prime number and $0$ otherwise.84258426\fun{long}{isprimeAPRCL}{GEN n}, returns $1$ if the \typ{INT} \kbd{n} is a8427prime number and $0$ otherwise, using only the APRCL test --- not even trial8428division or compositeness tests. The workhorse \kbd{isprime} should be8429faster on average, especially if nonprimes are included!84308431\fun{long}{isprimeECPP}{GEN n}, returns $1$ if the \typ{INT} \kbd{n} is a8432prime number and $0$ otherwise, using only the ECPP test. The workhorse8433\kbd{isprime} should be faster on average.84348435\fun{long}{BPSW_psp}{GEN n}, returns $1$ if the \typ{INT} \kbd{n} is a8436Baillie-Pomerance-Selfridge-Wagstaff pseudoprime, and $0$ otherwise (proven8437composite).84388439\fun{int}{BPSW_isprime}{GEN x} assuming $x$ is a BPSW-pseudoprime, rigorously8440prove its primality. The function \tet{isprime} is currently implemented8441as8442\bprog8443BPSW_psp(x) && BPSW_isprime(x)8444@eprog84458446\fun{long}{millerrabin}{GEN n, long k} performs $k$ strong Rabin-Miller8447compositeness tests on the \typ{INT} $n$, using $k$ random bases. This8448function also caches square roots of $-1$ that are encountered during the8449successive tests and stops as soon as three distinct square roots have been8450produced; we have in principle factored $n$ at this point, but8451unfortunately, there is currently no way for the factoring machinery to8452become aware of it. (It is highly implausible that hard to find factors8453would be exhibited in this way, though.) This should be slower than8454\tet{BPSW_psp} for $k\geq 4$ and we expect it to be less reliable.84558456\fun{GEN}{ecpp}{GEN N} returns an ECPP certificate for \typ{INT} $N$;8457underlies \kbd{primecert}.84588459\fun{GEN}{ecpp0}{GEN N, long t} returns a (potentially)8460partial ECPP certificate for \typ{INT} $N$ where strong pseudo-primes $< 2^t$8461are included as primes in the certificate. Underlies \kbd{primecert} with8462$t$ set to the \kbd{partial} argument.84638464\fun{GEN}{ecppexport}{GEN cert, long flag} export a PARI ECPP certificate to8465MAGMA or Primo format; underlies \kbd{primecertexport}.84668467\fun{long}{ecppisvalid}{GEN cert} checks whether a PARI ECPP certificate8468is valid; underlies \kbd{primecertisvalid}.84698470\fun{long}{check_ecppcert}{GEN cert} checks whether \kbd{cert} looks8471like a PARI ECPP certificate, (valid or invalid) without doing any computation.84728473\subsec{Iterators over primes}84748475\fun{int}{forprime_init}{forprime_t *T, GEN a, GEN b} initialize an8476iterator $T$ over primes in $[a,b]$; over primes $\geq a$ if $b =8477\kbd{NULL}$. Return $0$ if the range is known to be empty from the start8478(as if $b < a$ or $b < 0$), and return $1$ otherwise. Use \tet{forprime_next}8479to iterate over the prime collection.84808481\fun{int}{forprimestep_init}{forprime_t *T, GEN a, GEN b, GEN q} initialize an8482iterator $T$ over primes in an arithmetic progression in $[a,b]$;8483over primes $\geq a$ if $b = \kbd{NULL}$. The argument $q$ is either a8484\typ{INT} ($p \equiv a \pmod{q}$) or a \typ{INTMOD} \kbd{Mod(c,N)}8485and we restrict to that congruence class. Return $0$ if the range is known to8486be empty from the start (as if $b < a$ or $b < 0$), and return $1$ otherwise.8487Use \tet{forprime_next} to iterate over the prime collection.84888489\fun{GEN}{forprime_next}{forprime_t *T} returns the next prime in the range,8490assuming that $T$ was initialized by \tet{forprime_init}.84918492\fun{int}{u_forprime_init}{forprime_t *T, ulong a, ulong b}84938494\fun{ulong}{u_forprime_next}{forprime_t *T}84958496\fun{void}{u_forprime_restrict}{forprime_t *T, ulong c} let $T$ an iterator8497over primes initialized via \kbd{u\_forprime\_init(\&T, a, b)}, possibly8498followed by a number of calls to \tet{u_forprime_next}, and $a \leq c \leq8499b$. Restrict the range of primes considered to $[a,c]$.85008501\fun{int}{u_forprime_arith_init}{forprime_t *T, ulong a,ulong b, ulong c,ulong q} initialize an iterator over primes in $[a,b]$, congruent to $c$8502modulo $q$. Subsequent calls to \tet{u_forprime_next} will only return primes8503congruent to $c$ modulo $q$. Note that unless $(c,q) = 1$ there will be at8504most one such prime.85058506\section{Integral, rational and generic linear algebra}8507\subsec{\kbd{ZC} / \kbd{ZV}, \kbd{ZM}} A \kbd{ZV} (resp.~a~\kbd{ZM},8508resp.~a~\kbd{ZX}) is a \typ{VEC} or \typ{COL} (resp.~\typ{MAT},8509resp.~\typ{POL}) with \typ{INT} coefficients.85108511\subsubsec{\kbd{ZC} / \kbd{ZV}}85128513\fun{void}{RgV_check_ZV}{GEN x, const char *s} Assuming \kbd{x} is a \typ{VEC}8514or \typ{COL} raise an error if it is not a \kbd{ZV} ($s$ should point to the8515name of the caller).85168517\fun{int}{RgV_is_ZV}{GEN x} Assuming \kbd{x} is a \typ{VEC}8518or \typ{COL} return $1$ if it is a \kbd{ZV}, and $0$ otherwise.85198520\fun{int}{RgV_is_ZVpos}{GEN x} Assuming \kbd{x} is a \typ{VEC}8521or \typ{COL} return $1$ if it is a \kbd{ZV} with positive entries, and $0$8522otherwise.85238524\fun{int}{RgV_is_ZVnon0}{GEN x} Assuming \kbd{x} is a \typ{VEC}8525or \typ{COL} return $1$ if it is a \kbd{ZV} with nonzero entries, and $0$8526otherwise.85278528\fun{int}{RgV_is_QV}{GEN P} return 1 if the \kbd{RgV}~$P$ has only8529\typ{INT} and \typ{FRAC} coefficients, and 0 otherwise.85308531\fun{int}{RgV_is_arithprog}{GEN v, GEN *a, GEN *b} assuming $x$ is a \typ{VEC}8532or \typ{COL} return $1$ if its entries follow an arithmetic progression8533of the form $a + b*n$, $n = 0, 1, \dots$ and set $a$ and $b$. Else return $0$.85348535\fun{int}{ZV_equal0}{GEN x} returns 1 if all entries of the \kbd{ZV} $x$ are8536zero, and $0$ otherwise.85378538\fun{int}{ZV_cmp}{GEN x, GEN y} compare two \kbd{ZV}, which we assume have8539the same length (lexicographic order, comparing absolute values).85408541\fun{int}{ZV_abscmp}{GEN x, GEN y} compare two \kbd{ZV}, which we assume have8542the same length (lexicographic order).85438544\fun{int}{ZV_equal}{GEN x, GEN y} returns $1$ if the two \kbd{ZV} are equal8545and $0$ otherwise. A \typ{COL} and a \typ{VEC} with the same entries are8546declared equal.85478548\fun{GEN}{identity_ZV}{long n} return the \typ{VEC} $[1, 2, \dots, n]$.85498550\fun{GEN}{ZC_add}{GEN x, GEN y} adds \kbd{x} and \kbd{y}.85518552\fun{GEN}{ZC_sub}{GEN x, GEN y} subtracts \kbd{x} and \kbd{y}.85538554\fun{GEN}{ZC_Z_add}{GEN x, GEN y} adds \kbd{y} to \kbd{x[1]}.85558556\fun{GEN}{ZC_Z_sub}{GEN x, GEN y} subtracts \kbd{y} to \kbd{x[1]}.85578558\fun{GEN}{Z_ZC_sub}{GEN a, GEN x} returns the vector $[a - x_1,8559-x_2,\dots,-x_n]$.85608561\fun{GEN}{ZC_copy}{GEN x} returns a (\typ{COL}) copy of \kbd{x}.85628563\fun{GEN}{ZC_neg}{GEN x} returns $-\kbd{x}$ as a \typ{COL}.85648565\fun{void}{ZV_neg_inplace}{GEN x} negates the \kbd{ZV} \kbd{x} in place, by8566replacing each component by its opposite (the type of \kbd{x} remains the8567same, \typ{COL} or \typ{COL}). If you want to save even more memory by8568avoiding the implicit component copies, use \kbd{ZV\_togglesign}.85698570\fun{void}{ZV_togglesign}{GEN x} negates \kbd{x} in place, by toggling the8571sign of its integer components. Universal constants \kbd{gen\_1},8572\kbd{gen\_m1}, \kbd{gen\_2} and \kbd{gen\_m2} are handled specially and will8573not be corrupted. (We use \tet{togglesign_safe}.)85748575\fun{GEN}{ZC_Z_mul}{GEN x, GEN y} multiplies the \kbd{ZC} or \kbd{ZV}~\kbd{x}8576(which can be a column or row vector) by the \typ{INT}~\kbd{y}, returning a8577\kbd{ZC}.85788579\fun{GEN}{ZC_Z_divexact}{GEN x, GEN y} returns $x/y$ assuming all divisions8580are exact.85818582\fun{GEN}{ZC_divexactu}{GEN x, ulong y} returns $x/y$ assuming all divisions8583are exact.85848585\fun{GEN}{ZC_Z_div}{GEN x, GEN y} returns $x/y$, where the resulting vector8586has rational entries.85878588\fun{GEN}{ZV_dotproduct}{GEN x,GEN y} as \kbd{RgV\_dotproduct} assuming $x$8589and $y$ have \typ{INT} entries.85908591\fun{GEN}{ZV_dotsquare}{GEN x} as \kbd{RgV\_dotsquare} assuming $x$8592has \typ{INT} entries.85938594\fun{GEN}{ZC_lincomb}{GEN u, GEN v, GEN x, GEN y} returns $ux + vy$, where8595$u$, $v$ are \typ{INT} and $x,y$ are \kbd{ZC} or \kbd{ZV}. Return a \kbd{ZC}85968597\fun{void}{ZC_lincomb1_inplace}{GEN X, GEN Y, GEN v} sets $X\leftarrow X +8598vY$, where $v$ is a \typ{INT} and $X,Y$ are \kbd{ZC} or \kbd{ZV}. (The result8599has the type of $X$.) Memory efficient (e.g. no-op if $v = 0$), but not8600gerepile-safe.86018602\fun{void}{ZC_lincomb1_inplace_i}{GEN X, GEN Y, GEN v, long n}8603variant of \tet{ZC_lincomb1_inplace}: only update $X[1], \dots, X[n]$,8604assuming that $n < \kbd{lg}(X)$.86058606\fun{GEN}{ZC_ZV_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{ZC}~\kbd{x}8607(seen as a column vector) by the \kbd{ZV}~\kbd{y} (seen as a row vector,8608assumed to have compatible dimensions).86098610\fun{GEN}{ZV_content}{GEN x} returns the GCD of all the components8611of~\kbd{x}.86128613\fun{GEN}{ZV_extgcd}{GEN A} given a vector of $n$ integers $A$, returns $[d,8614U]$, where $d$ is the content of $A$ and $U$ is a matrix8615in $\text{GL}_n(\Z)$ such that $AU = [D,0, \dots,0]$.86168617\fun{GEN}{ZV_prod}{GEN x} returns the product of all the components8618of~\kbd{x} ($1$ for the empty vector).86198620\fun{GEN}{ZV_sum}{GEN x} returns the sum of all the components8621of~\kbd{x} ($0$ for the empty vector).86228623\fun{long}{ZV_max_lg}{GEN x} returns the effective length of the longest8624entry in $x$.86258626\fun{int}{ZV_dvd}{GEN x, GEN y} assuming $x$, $y$ are two \kbd{ZV}s of the same8627length, return $1$ if $y[i]$ divides $x[i]$ for all $i$ and $0$ otherwise.8628Error if one of the $y[i]$ is $0$.86298630\fun{GEN}{ZV_sort}{GEN L} sort the \kbd{ZV} $L$.8631Returns a vector with the same type as $L$.86328633\fun{void}{ZV_sort_inplace}{GEN L} sort the \kbd{ZV} $L$, in place.86348635\fun{GEN}{ZV_sort_uniq}{GEN L} sort the \kbd{ZV} $L$, removing duplicate8636entries. Returns a vector with the same type as $L$.86378638\fun{long}{ZV_search}{GEN L, GEN y} look for the \typ{INT} $y$ in the sorted8639\kbd{ZV} $L$. Return an index $i$ such that $L[i] = y$, and $0$ otherwise.86408641\fun{GEN}{ZV_indexsort}{GEN L} returns the permutation which, applied to the8642\kbd{ZV} $L$, would sort the vector. The result is a \typ{VECSMALL}.86438644\fun{GEN}{ZV_union_shallow}{GEN x, GEN y} given two \emph{sorted} ZV (as per8645\tet{ZV_sort}, returns the union of $x$ and $y$. Shallow function. In case two8646entries are equal in $x$ and $y$, include the one from $x$.86478648\fun{GEN}{ZC_union_shallow}{GEN x, GEN y} as \kbd{ZV\_union\_shallow} but return8649a \typ{COL}.86508651\subsubsec{\kbd{ZM}}86528653\fun{void}{RgM_check_ZM}{GEN A, const char *s} Assuming \kbd{x} is a \typ{MAT}8654raise an error if it is not a \kbd{ZM} ($s$ should point to the name of the8655caller).86568657\fun{GEN}{RgM_rescale_to_int}{GEN x} given a matrix $x$ with real entries8658(\typ{INT}, \typ{FRAC} or \typ{REAL}), return a \kbd{ZM} wich is very close8659to $D x$ for some well-chosen integer $D$. More precisely, if the input is8660exact, $D$ is the denominator of $x$; else it is a power of $2$ chosen8661so that all inexact entries are correctly rounded to 1 ulp.86628663\fun{GEN}{ZM_copy}{GEN x} returns a copy of \kbd{x}.86648665\fun{int}{ZM_equal}{GEN A, GEN B} returns $1$ if the two \kbd{ZM} are equal8666and $0$ otherwise.86678668\fun{int}{ZM_equal0}{GEN A} returns $1$ if the \kbd{ZM} $A$ is identically8669equal to $0$.86708671\fun{GEN}{ZM_add}{GEN x, GEN y} returns $\kbd{x} + \kbd{y}$ (assumed to have8672compatible dimensions).86738674\fun{GEN}{ZM_sub}{GEN x, GEN y} returns $\kbd{x} - \kbd{y}$ (assumed to have8675compatible dimensions).86768677\fun{GEN}{ZM_neg}{GEN x} returns $-\kbd{x}$.86788679\fun{void}{ZM_togglesign}{GEN x} negates \kbd{x} in place, by toggling the8680sign of its integer components. Universal constants \kbd{gen\_1},8681\kbd{gen\_m1}, \kbd{gen\_2} and \kbd{gen\_m2} are handled specially and will8682not be corrupted. (We use \tet{togglesign_safe}.)86838684\fun{GEN}{ZM_mul}{GEN x, GEN y} multiplies \kbd{x} and \kbd{y} (assumed to8685have compatible dimensions).86868687\fun{GEN}{ZM2_mul}{GEN x, GEN y} multiplies the two-by-two \kbd{ZM} $x$ and $y$.86888689\fun{GEN}{ZM_sqr}{GEN x} returns $x^2$, where $x$ is a square \kbd{ZM}.86908691\fun{GEN}{ZM_Z_mul}{GEN x, GEN y} multiplies the \kbd{ZM}~\kbd{x}8692by the \typ{INT}~\kbd{y}.86938694\fun{GEN}{ZM_ZC_mul}{GEN x, GEN y} multiplies the \kbd{ZM}~\kbd{x}8695by the \kbd{ZC}~\kbd{y} (seen as a column vector, assumed to have compatible8696dimensions).86978698\fun{GEN}{ZM_ZX_mul}{GEN x, GEN T} returns $x \times y$, where $y$8699is \kbd{RgX\_to\_RgC}$(T, \kbd{lg}(x)-1)$.87008701\fun{GEN}{ZM_diag_mul}{GEN d, GEN m} given a vector $d$ with integer entries8702and a \kbd{ZM} $m$ of compatible dimensions, return \kbd{diagonal(d) * m}.87038704\fun{GEN}{ZM_mul_diag}{GEN m, GEN d} given a vector $d$ with integer entries8705and a \kbd{ZM} $m$ of compatible dimensions, return \kbd{m * diagonal(d)}.87068707\fun{GEN}{ZM_multosym}{GEN x, GEN y}87088709\fun{GEN}{ZM_transmultosym}{GEN x, GEN y}87108711\fun{GEN}{ZM_transmul}{GEN x, GEN y}87128713\fun{GEN}{ZMrow_ZC_mul}{GEN x, GEN y, long i} multiplies the $i$-th row8714of \kbd{ZM}~\kbd{x} by the \kbd{ZC}~\kbd{y} (seen as a column vector, assumed8715to have compatible dimensions). Assumes that $x$ is nonempty and8716$0 < i < \kbd{lg(x[1])}$.87178718\fun{int}{ZMrow_equal0}{GEN V, long i} returns $1$ if the $i$-th row of8719the \kbd{ZM}~\kbd{V} is zero, and $0$ otherwise.87208721\fun{GEN}{ZV_ZM_mul}{GEN x, GEN y} multiplies the \kbd{ZV}~\kbd{x}8722by the \kbd{ZM}~\kbd{y}. Returns a \typ{VEC}.87238724\fun{GEN}{ZM_Z_divexact}{GEN x, GEN y} returns $x/y$ assuming all divisions8725are exact.87268727\fun{GEN}{ZM_divexactu}{GEN x, ulong y} returns $x/y$ assuming all divisions8728are exact.87298730\fun{GEN}{ZM_Z_div}{GEN x, GEN y} returns $x/y$, where the resulting matrix8731has rational entries.87328733\fun{GEN}{ZC_Q_mul}{GEN x, GEN y} returns $x*y$, where $y$ is a rational number8734and the resulting \typ{COL} has rational entries.87358736\fun{GEN}{ZM_Q_mul}{GEN x, GEN y} returns $x*y$, where $y$ is a rational number8737and the resulting matrix has rational entries.87388739\fun{GEN}{ZM_pow}{GEN x, GEN n} returns $\kbd{x}^\kbd{n}$, assuming \kbd{x}8740is a square \kbd{ZM} and $\kbd{n}\geq 0$.87418742\fun{GEN}{ZM_powu}{GEN x, ulong n} returns $\kbd{x}^\kbd{n}$, assuming \kbd{x}8743is a square \kbd{ZM} and $\kbd{n}\geq 0$.87448745\fun{GEN}{ZM_det}{GEN M} if \kbd{M} is a \kbd{ZM}, returns the determinant of8746$M$. This is the function underlying \tet{matdet} whenever $M$ is a \kbd{ZM}.87478748\fun{GEN}{ZM_permanent}{GEN M} if \kbd{M} is a \kbd{ZM}, returns its8749permanent. This is the function underlying \tet{matpermanent} whenever $M$8750is a \kbd{ZM}. It assumes that the matrix is square of dimension $<8751\kbd{BITS\_IN\_LONG}$.87528753\fun{GEN}{ZM_detmult}{GEN M} if \kbd{M} is a \kbd{ZM}, returns a multiple of8754the determinant of the lattice generated by its columns. This is the function8755underlying \tet{detint}.87568757\fun{GEN}{ZM_supnorm}{GEN x} return the sup norm of the \kbd{ZM} $x$.87588759\fun{GEN}{ZM_charpoly}{GEN M} returns the characteristic polynomial (in8760variable $0$) of the \kbd{ZM} $M$.87618762\fun{GEN}{ZM_imagecompl}{GEN x} returns \kbd{matimagecompl(x)}.87638764\fun{long}{ZM_rank}{GEN x} returns \kbd{matrank(x)}.87658766\fun{GEN}{ZM_ker}{GEN x} returns the primitive part of \kbd{matker(x)}; in8767other words the $\Q$-basis vectors are made integral and primitive.87688769\fun{GEN}{ZM_indexrank}{GEN x} returns \kbd{matindexrank(x)}.87708771\fun{GEN}{ZM_indeximage}{GEN x} returns \kbd{gel(ZM\_indexrank(x), 2)}.87728773\fun{long}{ZM_max_lg}{GEN x} returns the effective length of the longest8774entry in $x$.87758776\fun{GEN}{ZM_inv}{GEN M, GEN *pd} if \kbd{M} is a \kbd{ZM}, return8777a primitive matrix $H$ such that $M H$ is $d$ times the identity8778and set \kbd{*pd} to $d$. Uses a multimodular algorithm up to Hadamard's bound.8779If you suspect that the denominator is much smaller than $\det M$, you may8780use \tet{ZM_inv_ratlift}.87818782\fun{GEN}{ZM_inv_ratlift}{GEN M, GEN *pd} if \kbd{M} is a \kbd{ZM},8783return a primitive matrix $H$ such that $M H$ is $d$ times the identity8784and set \kbd{*pd} to $d$. Uses a multimodular algorithm, attempting8785rational reconstruction along the way. To be used when you expect that the8786denominator of $M^{-1}$ is much smaller than $\det M$ else use \kbd{ZM\_inv}.87878788\fun{GEN}{SL2_inv_shallow}{GEN M} return the inverse of $M \in8789\text{SL}_2(\Z)$. Not gerepile-safe.87908791\fun{GEN}{ZM_pseudoinv}{GEN M, GEN *pv, GEN *pd} if \kbd{M} is a nonempty8792\kbd{ZM}, let $v = [y,z]$ returned by \kbd{indexrank} and8793let $M_1$ be the corresponding square invertible matrix.8794Return a primitive left-inverse $H$ such that $H M_1$ is8795$d$ times the identity and set \kbd{*pd} to $d$. If \kbd{pv} is not8796\kbd{NULL}, set \kbd{*pv} to $v$. Not gerepile-safe.87978798\fun{GEN}{ZM_gauss}{GEN a, GEN b} as \kbd{gauss}, where $a$ and $b$8799coefficients are \typ{INT}s.88008801\fun{GEN}{ZM_det_triangular}{GEN x} returns the product of the diagonal8802entries of $x$ (its determinant if it is indeed triangular).88038804\fun{int}{ZM_isidentity}{GEN x} return 1 if the \kbd{ZM} $x$ is the8805identity matrix, and 0 otherwise.88068807\fun{int}{ZM_isdiagonal}{GEN x} return 1 if the \kbd{ZM} $x$ is diagonal,8808and 0 otherwise.88098810\fun{int}{ZM_isscalar}{GEN x, GEN s} given a \kbd{ZM} $x$ and a8811\typ{INT} $s$, return 1 if $x$ is equal to $s$ times the identity, and 08812otherwise. If $s$ is \kbd{NULL}, test whether $x$ is an arbitrary scalar8813matrix.88148815\fun{long}{ZC_is_ei}{GEN x} return $i$ if the \kbd{ZC} $x$ has $0$ entries,8816but for a $1$ at position $i$.88178818\fun{int}{ZM_ishnf}{GEN x} return $1$ if $x$ is in HNF form, i.e. is upper8819triangular with positive diagonal coefficients, and for $j>i$,8820$x_{i,i}>x_{i,j} \ge 0$.88218822\subsec{\kbd{QM}}88238824\fun{GEN}{QM_charpoly_ZX}{GEN M} returns the characteristic polynomial8825(in variable $0$) of the \kbd{QM} $M$, assuming that the result has integer8826coefficients.88278828\fun{GEN}{QM_charpoly_ZX_bound}{GEN M, long b} as \tet{QM_charpoly_ZX}8829assuming that the sup norm of the (integral) result is $\leq 2^b$.88308831\fun{GEN}{QM_gauss}{GEN a, GEN b} as \kbd{gauss}, where $a$ and $b$8832coefficients are \typ{FRAC}s.88338834\fun{GEN}{QM_gauss_i}{GEN a, GEN b, long flag} as \kbd{QM\_gauss} if8835\kbd{flag} is $0$. Else, no longer assume that $a$ is left-invertible and8836return a solution of $P a x = P b$ where $P$ is a row-selection matrix8837such that $A = P a Q$ is square invertible of maximal rank, for some8838column-selection matrix $Q$; in particular, $x$ is a solution of8839the original equation $a x = b$ if and only if a solution exists.88408841\fun{GEN}{QM_indexrank}{GEN x} returns \kbd{matindexrank(x)}.88428843\fun{GEN}{QM_inv}{GEN M} return the inverse of the \kbd{QM} $M$.88448845\fun{long}{QM_rank}{GEN x} returns \kbd{matrank(x)}.88468847\fun{GEN}{QM_image}{GEN x} returns an integral matrix with primitive columns8848generating the image of $x$.88498850\fun{GEN}{QM_image_shallow}{GEN A} shallow version of the previous function,8851not suitable for \kbd{gerepile}.88528853\subsec{\kbd{Qevproj}}88548855\fun{GEN}{Qevproj_init}{GEN M} let $M$ be a $n\times d$ \kbd{ZM} of8856maximal rank $d \leq n$, representing the basis of a $\Q$-subspace8857$V$ of $\Q^n$. Return a projector on $V$, to be used by \tet{Qevproj_apply}.8858The interface details may change in the future, but this function currently8859returns $[M, B,D,p]$, where $p$ is a \typ{VECSMALL} with $d$ entries8860such that the submatrix $A = \kbd{rowpermute}(M,p)$ is invertible, $B$ is a8861\kbd{ZM} and $d$ a \typ{INT} such that $A B = D \Id_d$.88628863\fun{GEN}{Qevproj_apply}{GEN T, GEN pro} let $T$ be an $n\times n$8864\kbd{QM}, stabilizing a $\Q$-subspace $V\subset \Q^n$ of dimension $d$, and8865let \kbd{pro} be a projector on that subspace initialized by8866\tet{Qevproj_init}$(M)$. Return the $d\times d$ matrix representing $T_{|V}$8867on the basis given by the columns of $M$.88688869\fun{GEN}{Qevproj_apply_vecei}{GEN T, GEN pro, long k} as8870\tet{Qevproj_apply}, return only the image of the $k$-th basis vector $M[k]$8871(still on the basis given by the columns of $M$).88728873\fun{GEN}{Qevproj_down}{GEN T, GEN pro} given a \kbd{ZC} (resp.~a \kbd{ZM})8874$T$ representing an element (resp.~a vector of elements) in the subspace $V$8875return a \kbd{QC} (resp.~a \kbd{QM}) $U$ such that $T = MU$.88768877\subsec{\kbd{zv}, \kbd{zm}}88788879\fun{GEN}{identity_zv}{long n} return the \typ{VECSMALL} $[1, 2, \dots, n]$.88808881\fun{GEN}{random_zv}{long n} returns a random \kbd{zv} with $n$ components.88828883\fun{GEN}{zv_abs}{GEN x} return $[|x[1]|,\ldots,|x[n]|]$ as a \kbd{zv}.88848885\fun{GEN}{zv_neg}{GEN x} return $-x$. No check for overflow is done, which8886occurs in the fringe case where an entry is equal to $2^{\B-1}$.88878888\fun{GEN}{zv_neg_inplace}{GEN x} negates $x$ in place and return it. No check8889for overflow is done, which occurs in the fringe case where an entry is equal8890to $2^{\B-1}$.88918892\fun{GEN}{zm_zc_mul}{GEN x, GEN y}88938894\fun{GEN}{zm_mul}{GEN x, GEN y}88958896\fun{GEN}{zv_z_mul}{GEN x, long n} return $n\*x$. No check for overflow is8897done.88988899\fun{long}{zv_content}{GEN x} returns the gcd of the entries of $x$.89008901\fun{long}{zv_dotproduct}{GEN x, GEN y}89028903\fun{long}{zv_prod}{GEN x} returns the product of all the components8904of~\kbd{x} (assumes no overflow occurs).89058906\fun{GEN}{zv_prod_Z}{GEN x} returns the product of all the components8907of~\kbd{x}; consider all $x[i]$ as \kbd{ulong}s.89088909\fun{long}{zv_sum}{GEN x} returns the sum of all the components8910of~\kbd{x} (assumes no overflow occurs).89118912\fun{long}{zv_sumpart}{GEN v, long n} returns the sum $v[1] + \dots + v[n]$8913(assumes no overflow occurs and \kbd{lg}$(v) > n$).89148915\fun{int}{zv_cmp0}{GEN x} returns 1 if all entries of the \kbd{zv} $x$ are $0$,8916and $0$ otherwise.89178918\fun{int}{zv_equal}{GEN x, GEN y} returns $1$ if the two \kbd{zv} are equal8919and $0$ otherwise.89208921\fun{int}{zv_equal0}{GEN x} returns $1$ if all entries are $0$, and return8922$0$ otherwise.89238924\fun{long}{zv_search}{GEN L, long y} look for $y$ in the sorted8925\kbd{zv} $L$. Return an index $i$ such that $L[i] = y$, and $0$ otherwise.89268927\fun{GEN}{zv_copy}{GEN x} as \kbd{Flv\_copy}.89288929\fun{GEN}{zm_transpose}{GEN x} as \kbd{Flm\_transpose}.89308931\fun{GEN}{zm_copy}{GEN x} as \kbd{Flm\_copy}.89328933\fun{GEN}{zero_zm}{long m, long n} as \kbd{zero\_Flm}.89348935\fun{GEN}{zero_zv}{long n} as \kbd{zero\_Flv}.89368937\fun{GEN}{zm_row}{GEN A, long x0} as \kbd{Flm\_row}.89388939\fun{GEN}{zv_diagonal}{GEN v} return the square \kbd{zm} whose diagonal8940is given by the entries of $v$.89418942\fun{GEN}{zm_permanent}{GEN M} return the permanent of $M$.8943The function assumes that the matrix is square of dimension8944$< \kbd{BITS\_IN\_LONG}$.89458946\fun{int}{zvV_equal}{GEN x, GEN y} returns $1$ if the two \kbd{zvV} (vectors8947of \kbd{zv}) are equal and $0$ otherwise.89488949\subsec{\kbd{ZMV} / \kbd{zmV} (vectors of \kbd{ZM}/\kbd{zm})}89508951\fun{int}{RgV_is_ZMV}{GEN x} Assuming \kbd{x} is a \typ{VEC}8952or \typ{COL} return $1$ if its components are \kbd{ZM}, and $0$ otherwise.89538954\fun{GEN}{ZMV_to_zmV}{GEN z}89558956\fun{GEN}{zmV_to_ZMV}{GEN z}89578958\fun{GEN}{ZMV_to_FlmV}{GEN z, ulong m}89598960\subsec{\kbd{QC} / \kbd{QV}, \kbd{QM}}89618962\fun{GEN}{QM_mul}{GEN x, GEN y} multiplies \kbd{x} and \kbd{y} (assumed to8963have compatible dimensions).89648965\fun{GEN}{QM_sqr}{GEN x} returns the square of \kbd{x} (assumed to be square).89668967\fun{GEN}{QM_QC_mul}{GEN x, GEN y} multiplies \kbd{x} and \kbd{y} (assumed to8968have compatible dimensions).89698970\fun{GEN}{QM_det}{GEN M} returns the determinant of $M$.89718972\fun{GEN}{QM_ker}{GEN x} returns \kbd{matker(x)}.89738974\subsec{\kbd{RgC} / \kbd{RgV}, \kbd{RgM}}89758976\kbd{RgC} and \kbd{RgV} routines assume the inputs are \kbd{VEC} or \kbd{COL}8977of the same dimension. \kbd{RgM} assume the inputs are \kbd{MAT} of8978compatible dimensions.89798980\subsubsec{Matrix arithmetic}89818982\fun{void}{RgM_dimensions}{GEN x, long *m, long *n} sets $m$, resp.~$n$, to8983the number of rows, resp.~columns of the \typ{MAT} $x$.89848985\fun{GEN}{RgC_add}{GEN x, GEN y} returns $x + y$ as a \typ{COL}.89868987\fun{GEN}{RgC_neg}{GEN x} returns $-x$ as a \typ{COL}.89888989\fun{GEN}{RgC_sub}{GEN x, GEN y} returns $x - y$ as a \typ{COL}.89908991\fun{GEN}{RgV_add}{GEN x, GEN y} returns $x + y$ as a \typ{VEC}.89928993\fun{GEN}{RgV_neg}{GEN x} returns $-x$ as a \typ{VEC}.89948995\fun{GEN}{RgV_sub}{GEN x, GEN y} returns $x - y$ as a \typ{VEC}.89968997\fun{GEN}{RgM_add}{GEN x, GEN y} return $x+y$.89988999\fun{GEN}{RgM_neg}{GEN x} returns $-x$.90009001\fun{GEN}{RgM_sub}{GEN x, GEN y} returns $x-y$.90029003\fun{GEN}{RgM_Rg_add}{GEN x, GEN y} assuming $x$ is a square matrix9004and $y$ a scalar, returns the square matrix $x + y*\text{Id}$.90059006\fun{GEN}{RgM_Rg_add_shallow}{GEN x, GEN y} as \kbd{RgM\_Rg\_add} with much9007fewer copies. Not suitable for \kbd{gerepileupto}.90089009\fun{GEN}{RgM_Rg_sub}{GEN x, GEN y} assuming $x$ is a square matrix9010and $y$ a scalar, returns the square matrix $x - y*\text{Id}$.90119012\fun{GEN}{RgM_Rg_sub_shallow}{GEN x, GEN y} as \kbd{RgM\_Rg\_sub} with much9013fewer copies. Not suitable for \kbd{gerepileupto}.90149015\fun{GEN}{RgC_Rg_add}{GEN x, GEN y} assuming $x$ is a nonempty column vector9016and $y$ a scalar, returns the vector $[x_1 + y, x_2,\dots,x_n]$.90179018\fun{GEN}{RgC_Rg_sub}{GEN x, GEN y} assuming $x$ is a nonempty column vector9019and $y$ a scalar, returns the vector $[x_1 - y, x_2,\dots,x_n]$.90209021\fun{GEN}{Rg_RgC_sub}{GEN a, GEN x} assuming $x$ is a nonempty column vector9022and $a$ a scalar, returns the vector $[a - x_1, -x_2,\dots,-x_n]$.90239024\fun{GEN}{RgC_Rg_div}{GEN x, GEN y}90259026\fun{GEN}{RgM_Rg_div}{GEN x, GEN y} returns $x/y$ ($y$ treated as a scalar).90279028\fun{GEN}{RgC_Rg_mul}{GEN x, GEN y}90299030\fun{GEN}{RgV_Rg_mul}{GEN x, GEN y}90319032\fun{GEN}{RgM_Rg_mul}{GEN x, GEN y} returns $x\times y$ ($y$ treated as a9033scalar).90349035\fun{GEN}{RgV_RgC_mul}{GEN x, GEN y} returns $x\times y$.90369037\fun{GEN}{RgV_RgM_mul}{GEN x, GEN y} returns $x\times y$.90389039\fun{GEN}{RgM_RgC_mul}{GEN x, GEN y} returns $x\times y$.90409041\fun{GEN}{RgM_RgX_mul}{GEN x, GEN T} returns $x \times y$, where $y$9042is \kbd{RgX\_to\_RgC}$(T, \kbd{lg}(x)-1)$.90439044\fun{GEN}{RgM_mul}{GEN x, GEN y} returns $x\times y$.90459046\fun{GEN}{RgM_ZM_mul}{GEN x, GEN y} returns $x\times y$ assuming that $y$ is9047a \kbd{ZM}.90489049\fun{GEN}{RgM_transmul}{GEN x, GEN y} returns $x\til \times y$.90509051\fun{GEN}{RgM_multosym}{GEN x, GEN y} returns $x\times y$, assuming9052the result is a symmetric matrix (about twice faster than a generic matrix9053multiplication).90549055\fun{GEN}{RgM_transmultosym}{GEN x, GEN y} returns $x\til \times y$, assuming9056the result is a symmetric matrix (about twice faster than a generic matrix9057multiplication).90589059\fun{GEN}{RgMrow_RgC_mul}{GEN x, GEN y, long i} multiplies the $i$-th row of9060\kbd{RgM}~\kbd{x} by the \kbd{RgC}~\kbd{y} (seen as a column vector, assumed9061to have compatible dimensions). Assumes that $x$ is nonempty and $0 < i <9062\kbd{lg(x[1])}$.90639064\fun{GEN}{RgM_mulreal}{GEN x, GEN y} returns the real part of $x\times y$9065(whose entries are \typ{INT}, \typ{FRAC}, \typ{REAL} or \typ{COMPLEX}).90669067\fun{GEN}{RgM_sqr}{GEN x} returns $x^2$.90689069\fun{GEN}{RgC_RgV_mul}{GEN x, GEN y} returns $x\times y$ (the matrix9070$(x_iy_j)$).90719072\fun{GEN}{RgC_RgV_mulrealsym}{GEN x, GEN y} returns the real part of $x\times9073y$ (whose entries are \typ{INT}, \typ{FRAC}, \typ{REAL} or \typ{COMPLEX}),9074assuming the result is symetric.90759076The following two functions are not well defined in general and only provided9077for convenience in specific cases:90789079\fun{GEN}{RgC_RgM_mul}{GEN x, GEN y} returns $x\times y[1,]$ if $y$ is9080a row matrix $1\times n$, error otherwise.90819082\fun{GEN}{RgM_RgV_mul}{GEN x, GEN y} returns $x\times y[,1]$ if $y$ is9083a column matrix $n\times 1$, error otherwise.90849085\fun{GEN}{RgM_powers}{GEN x, long n} returns $[\kbd{x}^0,9086\dots, \kbd{x}^\kbd{n}]$ as a \typ{VEC} of \kbd{RgM}s.90879088\smallskip90899090\fun{GEN}{RgV_sum}{GEN v} sum of the entries of $v$90919092\fun{GEN}{RgV_prod}{GEN v} product of the entries of $v$, using9093a divide and conquer strategy90949095\fun{GEN}{RgV_sumpart}{GEN v, long n} returns the sum $v[1] + \dots + v[n]$9096(assumes that \kbd{lg}$(v) > n$).90979098\fun{GEN}{RgV_sumpart2}{GEN v, long m, long n} returns the sum $v[m] + \dots +9099v[n]$ (assumes that \kbd{lg}$(v) > n$ and $m > 0$). Returns \kbd{gen\_0}9100when $m > n$.91019102\fun{GEN}{RgM_sumcol}{GEN v} returns a \typ{COL}, sum of the columns of the9103\typ{MAT} $v$.91049105\fun{GEN}{RgV_dotproduct}{GEN x,GEN y} returns the scalar product of $x$ and $y$91069107\fun{GEN}{RgV_dotsquare}{GEN x} returns the scalar product of $x$ with itself.91089109\fun{GEN}{RgV_kill0}{GEN v} returns a shallow copy of $v$ where entries9110matched by \kbd{gequal0} are replaced by \kbd{NULL}. The return value9111is not a valid \kbd{GEN} and must be handled specially. The idea is9112to pre-treat a vector of coefficients to speed up later linear combinations9113or scalar products.91149115\fun{GEN}{gram_matrix}{GEN v} returns the \idx{Gram matrix} $(v_i\cdot v_j)$9116attached to the entries of $v$ (matrix, or vector of vectors).91179118\fun{GEN}{RgV_polint}{GEN X, GEN Y, long v} $X$ and $Y$ being two vectors of9119the same length, returns the polynomial $T$ in variable $v$ such that9120$T(X[i]) = Y[i]$ for all $i$. The special case $X = \kbd{NULL}$9121corresponds to $X = [1,2,\dots,n]$, where $n$ is the length of $Y$.91229123\fun{GEN}{polintspec}{GEN X, GEN Y, GEN t, long n, long *pe}9124return $P(t)$ where $P$ is the Lagrange interpolation polynomial9125attached to the $n$ points $(X[0],Y[0]), \dots, (X[n-1],Y[n-1])$.9126If \kbd{pe} is not \kbd{NULL} and $t$ is a complex numeric value, \kbd{*pe}9127contains an error estimate for the returned value (Neville's algorithm, see9128\kbd{polinterpolate}). In extrapolation algorithms, e.g., Romberg9129integration, this function is usually called on actual \kbd{GEN} vectors9130with offsets: $x + k$ and $y + k$ so as to interpolate on $x[k..k+n-1]$9131without having to use \kbd{vecslice}.91329133\fun{GEN}{polint_i}{GEN X, GEN Y, GEN t, long *pe} as \kbd{polintspec},9134where $X$ and $Y$ are actual \kbd{GEN} vectors.91359136\subsubsec{Special shapes}91379138The following routines check whether matrices or vectors have a special9139shape, using \kbd{gequal1} and \kbd{gequal0} to test components. (This makes9140a difference when components are inexact.)91419142\fun{int}{RgV_isscalar}{GEN x} return 1 if all the entries of $x$ are $0$9143(as per \kbd{gequal0}), except possibly the first one. The name comes from9144vectors expressing polynomials on the standard basis $1,T,\dots, T^{n-1}$, or9145on \kbd{nf.zk} (whose first element is $1$).91469147\fun{int}{QV_isscalar}{GEN x} as \kbd{RgV\_isscalar}, assuming $x$ is a9148\kbd{QV} (\typ{INT} and \typ{FRAC} entries only).91499150\fun{int}{ZV_isscalar}{GEN x} as \kbd{RgV\_isscalar}, assuming $x$ is a9151\kbd{ZV} (\typ{INT} entries only).91529153\fun{int}{RgM_isscalar}{GEN x, GEN s} return 1 if $x$ is the scalar matrix9154equal to $s$ times the identity, and 0 otherwise. If $s$ is \kbd{NULL}, test9155whether $x$ is an arbitrary scalar matrix.91569157\fun{int}{RgM_isidentity}{GEN x} return 1 if the \typ{MAT} $x$ is the9158identity matrix, and 0 otherwise.91599160\fun{int}{RgM_isdiagonal}{GEN x} return 1 if the \typ{MAT} $x$ is a9161diagonal matrix, and 0 otherwise.91629163\fun{long}{RgC_is_ei}{GEN x} return $i$ if the \typ{COL} $x$ has $0$ entries,9164but for a $1$ at position $i$.91659166\fun{int}{RgM_is_ZM}{GEN x} return 1 if the \typ{MAT}~$x$ has only9167\typ{INT} coefficients, and 0 otherwise.91689169\fun{long}{qfiseven}{GEN M} return 1 if the square symetric typ{ZM}~$x$9170is an even quadratic form (all diagonal coefficients are even), and 0 otherwise.91719172\fun{int}{RgM_is_QM}{GEN x} return 1 if the \typ{MAT}~$x$ has only9173\typ{INT} or \typ{FRAC} coefficients, and 0 otherwise.91749175\fun{long}{RgV_isin}{GEN v, GEN x} return the first index $i$ such that9176$v[i] = x$ if it exists, and $0$ otherwise. Naive search in linear time, does9177not assume that \kbd{v} is sorted.91789179\fun{long}{RgV_isin_i}{GEN v, GEN x, long n} return the first index $i\ leq n$9180such that $v[i] = x$ if it exists, and $0$ otherwise. Naive search in linear9181time, does not assume that \kbd{v} is sorted. Assume that $n < \kbd{lg(v)}$.91829183\fun{GEN}{RgM_diagonal}{GEN m} returns the diagonal of $m$ as a \typ{VEC}.91849185\fun{GEN}{RgM_diagonal_shallow}{GEN m} shallow version of \kbd{RgM\_diagonal}91869187\subsubsec{Conversion to floating point entries}91889189\fun{GEN}{RgC_gtofp}{GEN x, GEN prec} returns the \typ{COL} obtained by9190applying \kbd{gtofp(gel(x,i), prec)} to all coefficients of $x$.91919192\fun{GEN}{RgV_gtofp}{GEN x, GEN prec} returns the \typ{VEC} obtained by9193applying \kbd{gtofp(gel(x,i), prec)} to all coefficients of $x$.91949195\fun{GEN}{RgC_gtomp}{GEN x, long prec} returns the \typ{COL} obtained by9196applying \kbd{gtomp(gel(x,i), prec)} to all coefficients of $x$.91979198\fun{GEN}{RgC_fpnorml2}{GEN x, long prec} returns (a stack-clean variant of)9199\bprog9200gnorml2( RgC_gtofp(x, prec) )9201@eprog92029203\fun{GEN}{RgM_gtofp}{GEN x, GEN prec} returns the \typ{MAT} obtained by9204applying \kbd{gtofp(gel(x,i), prec)} to all coefficients of $x$.92059206\fun{GEN}{RgM_gtomp}{GEN x, long prec} returns the \typ{MAT} obtained by9207applying \kbd{gtomp(gel(x,i), prec)} to all coefficients of $x$.92089209\fun{GEN}{RgM_fpnorml2}{GEN x, long prec} returns (a stack-clean variant of)9210\bprog9211gnorml2( RgM_gtofp(x, prec) )9212@eprog92139214\subsubsec{Linear algebra, linear systems}92159216\fun{GEN}{RgM_inv}{GEN a} returns a left inverse of $a$ (which needs not be9217square), or \kbd{NULL} if this turns out to be impossible. The latter9218happens when the matrix does not have maximal rank (or when rounding errors9219make it appear so).92209221\fun{GEN}{RgM_inv_upper}{GEN a} as \kbd{RgM\_inv}, assuming that $a$ is a9222nonempty invertible upper triangular matrix, hence a little faster.92239224\fun{GEN}{RgM_RgC_invimage}{GEN A, GEN B} returns a \typ{COL} $X$ such that9225$A X = B$ if one such exists, and \kbd{NULL} otherwise.92269227\fun{GEN}{RgM_invimage}{GEN A, GEN B} returns a \typ{MAT} $X$ such that9228$A X = B$ if one such exists, and \kbd{NULL} otherwise.92299230\fun{GEN}{RgM_Hadamard}{GEN a} returns a upper bound for the absolute9231value of $\text{det}(a)$. The bound is a \typ{INT}.92329233\fun{GEN}{RgM_solve}{GEN a, GEN b} returns $a^{-1}b$ where $a$ is a square9234\typ{MAT} and $b$ is a \typ{COL} or \typ{MAT}. Returns \kbd{NULL} if $a^{-1}$9235cannot be computed, see \tet{RgM_inv}.92369237If $b = \kbd{NULL}$, the matrix $a$ need no longer be square, and we strive9238to return a left inverse for $a$ (\kbd{NULL} if it does not exist).92399240\fun{GEN}{RgM_solve_realimag}{GEN M, GEN b} $M$ being a \typ{MAT}9241with $r_1+r_2$ rows and $r_1+2r_2$ columns, $y$ a \typ{COL} or \typ{MAT}9242such that the equation $Mx = y$ makes sense, returns $x$ under the following9243simplifying assumptions: the first $r_1$ rows of $M$ and $y$ are real9244(the $r_2$ others are complex), and $x$ is real. This is stabler and faster9245than calling $\kbd{RgM\_solve}(M, b)$ over $\C$. In most applications,9246$M$ approximates the complex embeddings of an integer basis in a number9247field, and $x$ is actually rational.92489249\fun{GEN}{split_realimag}{GEN x, long r1, long r2} $x$ is a \typ{COL} or9250\typ{MAT} with $r_1 + r_2$ rows, whose first $r_1$ rows have real entries9251(the $r_2$ others are complex). Return an object of the same type as9252$x$ and $r_1 + 2r_2$ rows, such that the first $r_1 + r_2$ rows contain9253the real part of $x$, and the $r_2$ following ones contain the imaginary part9254of the last $r_2$ rows of $x$. Called by \tet{RgM_solve_realimag}.92559256\fun{GEN}{RgM_det_triangular}{GEN x} returns the product of the diagonal9257entries of $x$ (its determinant if it is indeed triangular).92589259\fun{GEN}{Frobeniusform}{GEN V, long n} given the vector $V$ of elementary9260divisors for $M - x\text{Id}$, where $M$ is an $n\times n$ square matrix.9261Returns the Frobenius form of $M$.92629263\fun{GEN}{RgM_Frobenius}{GEN A, long flag, GEN *pP, GEN *pv}9264given a square matrix $A$, returns the Frobenius form of A if \kbd{flag} is $0$,9265or a weak Frobenius form of $A$ if \kbd{flag} is $1$.9266If \kbd{pP} is nonzero, set \kbd{*pP} to the change of basis matrix.9267If \kbd{pv} is nonzero, set \kbd{*pv} to the \typ{VECSMALL} containing the column indices of the left column of each diagonal block.92689269\fun{int}{RgM_QR_init}{GEN x, GEN *pB, GEN *pQ, GEN *pL, long prec}9270QR-decomposition of a square invertible \typ{MAT} $x$ with real coefficients.9271Sets \kbd{*pB} to the vector of squared lengths of the $x[i]$, \kbd{*pL} to9272the Gram-Schmidt coefficients and \kbd{*pQ} to a vector of successive9273Householder transforms. If $R$ denotes the transpose of $L$ and $Q$ is the9274result of applying \kbd{*pQ} to the identity matrix, then $x = QR$ is the QR9275decomposition of $x$. Returns $0$ is $x$ is not invertible or we hit a9276precision problem, and $1$ otherwise.92779278\fun{int}{QR_init}{GEN x, GEN *pB, GEN *pQ, GEN *pL, long prec} as9279\kbd{RgM\_QR\_init}, assuming further that $x$ has \typ{INT} or \typ{REAL}9280coefficients.92819282\fun{GEN}{R_from_QR}{GEN x, long prec} assuming that $x$ is a square9283invertible \typ{MAT} with \typ{INT} or \typ{REAL} coefficients, return9284the upper triangular $R$ from the $QR$ docomposition of $x$. Not memory9285clean. If the matrix is not known to have \typ{INT} or \typ{REAL}9286coefficients, apply \tet{RgM_gtomp} first.92879288\fun{GEN}{gaussred_from_QR}{GEN x, long prec} assuming that $x$ is a square9289invertible \typ{MAT} with \typ{INT} or \typ{REAL} coefficients, returns9290\kbd{qfgaussred(x\til * x)}; this is essentially the upper triangular $R$9291matrix from the $QR$ decomposition of $x$, renormalized to accomodate9292\kbd{qfgaussred} conventions. Not memory clean.92939294\fun{GEN}{RgM_gram_schmidt}{GEN e, GEN *ptB} naive (unstable) Gram-Schmidt9295orthogonalization of the basis $(e_i)$ given by the columns of \typ{MAT} $e$.9296Return the $e_i^*$ (as columns of a \typ{MAT}) and set \kbd{*ptB} to the9297vector of squared lengths $|e_i^*|^2$.92989299\fun{GEN}{RgM_Babai}{GEN M, GEN y} given a \typ{MAT} $M$ of maximal rank $n$9300and a \typ{COL} $y$ of the same dimension, apply Babai's nearest plane9301algorithm to return an \emph{integral} $x$ such that $y - Mx$ has small $L_2$9302norm. This yields an approximate solution to the closest vector problem: if9303$M$ is LLL-reduced, then9304$$|| y - Mx ||_2 \leq 2 (2/\sqrt{3})^n || y - MX ||_2$$9305for all $X \in \Z^n$.93069307\subsec{\kbd{ZG}}93089309Let $G$ be a multiplicative group with neutral element $1_G$ whose9310multiplication is supported by \kbd{gmul} and where equality test is9311performed using \tet{gidentical}, e.g. a matrix group. The following9312routines implement basic computations in the group algebra $\Z[G]$. All of9313them are shallow for efficiency reasons. A \kbd{ZG} is either93149315\item a \typ{INT} $n$, representing $n[1_G]$93169317\item or a ``factorization matrix'' with two columns $[g,e]$: the first one9318contains group elements, sorted according to \tet{cmp_universal}, and the9319second one contains integer ``exponents'', representing $\sum e_i [g_i]$.93209321Note that \tet{to_famat} and \tet{to_famat_shallow}$(g,e)$ allow to build9322the \kbd{ZG} $e[g]$ from $e\in \Z$ and $g\in G$.93239324\fun{GEN}{ZG_normalize}{GEN x} given a \typ{INT} $x$ or a factorization9325matrix \emph{without} assuming that the first column is properly sorted.9326Return a valid (sorted) \kbd{ZG}. Shallow function.93279328\fun{GEN}{ZG_add}{GEN x, GEN y} return $x+y$; shallow function.93299330\fun{GEN}{ZG_neg}{GEN x} return $-x$; shallow function.93319332\fun{GEN}{ZG_sub}{GEN x, GEN y} return $x-y$; shallow function.93339334\fun{GEN}{ZG_mul}{GEN x, GEN y} return $xy$; shallow function.93359336\fun{GEN}{ZG_G_mul}{GEN x, GEN y} given a \kbd{ZG} $x$ and $y\in G$,9337return $xy$; shallow function.93389339\fun{GEN}{G_ZG_mul}{GEN x, GEN y} given a \kbd{ZG} $y$ and $x\in G$,9340return $xy$; shallow function.93419342\fun{GEN}{ZG_Z_mul}{GEN x, GEN n} given a \kbd{ZG} $x$ and $y\in \Z$,9343return $xy$; shallow function.93449345\fun{GEN}{ZGC_G_mul}{GEN v, GEN x} given $v$ a vector of \kbd{ZG} and $x\in9346G$ return the vector (with the same type as $v$ with entries $v[i]\cdot x$.9347Shallow function.93489349\fun{void}{ZGC_G_mul_inplace}{GEN v, GEN x} as \tet{ZGC_G_mul}, modifying9350$v$ in place.93519352\fun{GEN}{ZGC_Z_mul}{GEN v, GEN n} given $v$ a vector of \kbd{ZG} and $n\in9353Z$ return the vector (with the same type as $v$ with entries $n \cdot v[i]$.9354Shallow function.93559356\fun{GEN}{G_ZGC_mul}{GEN x, GEN v} given $v$ a vector of \kbd{ZG} and $x\in9357G$ return the vector of $x \cdot v[i]$. Shallow function.93589359\fun{GEN}{ZGCs_add}{GEN x, GEN y} add two sparse vectors of9360\kbd{ZG} elements (see Sparse linear algebra below).93619362\subsec{Sparse and blackbox linear algebra}93639364A sparse column \kbd{zCs} $v$ is a \typ{COL} with two components $C$ and $E$9365which are \typ{VECSMALL} of the same length, representing $\sum_i9366E[i]*e_{C[i]}$, where $(e_j)$ is the canonical basis. A sparse matrix9367(\kbd{zMs}) is a \typ{VEC} of \kbd{zCs}.93689369\kbd{FpCs} and \kbd{FpMs} are identical to the above, but $E[i]$ is now9370interpreted as a \emph{signed} C long integer representing an element of9371$\F_p$. This is important since $p$ can be so large that $p+E[i]$ would not9372fit in a C long.93739374\kbd{RgCs} and \kbd{RgMs} are similar, except that the type of the components9375of $E$ is now unspecified. Functions handling those later objects9376must not depend on the type of those components.93779378\kbd{F2Ms} are \typ{VEC} of \kbd{F2Cs}. \kbd{F2Cs} are \typ{VECSMALL} whoses9379entries are the nonzero coefficients ($1$).93809381It is not possible to derive the space dimension (number of rows) from the9382above data. Thus most functions take an argument \kbd{nbrow} which is the9383number of rows of the corresponding column/matrix in dense representation.93849385\fun{GEN}{F2Ms_to_F2m}{GEN M, long nbrow} convert a \kbd{F2m} to a \kbd{F2Ms}.93869387\fun{GEN}{F2m_to_F2Ms}{GEN M} convert a \kbd{F2m} to a \kbd{F2Ms}.93889389\fun{GEN}{zCs_to_ZC}{GEN C, long nbrow} convert the sparse vector $C$9390to a dense \kbd{ZC} of dimension \kbd{nbrow}.93919392\fun{GEN}{zMs_to_ZM}{GEN M, long nbrow} convert the sparse matrix $M$9393to a dense \kbd{ZM} whose columns have dimension \kbd{nbrow}.93949395\fun{GEN}{FpMs_FpC_mul}{GEN M, GEN B, GEN p} multiply the sparse matrix $M$9396(over $\F_p$) by the \kbd{FpC} $B$. The result is an \kbd{FpC}, i.e.~a9397dense vector.93989399\fun{GEN}{zMs_ZC_mul}{GEN M, GEN B, GEN p} multiply the sparse matrix $M$9400by the \kbd{ZC} $B$ (over $\Z$). The result is an \kbd{ZC}, i.e.~a9401dense vector.94029403\fun{GEN}{FpV_FpMs_mul}{GEN B, GEN M, GEN p} multiply the \kbd{FpV} $B$9404by the sparse matrix $M$ (over $\F_p$). The result is an \kbd{FpV}, i.e.~a9405dense vector.94069407\fun{GEN}{ZV_zMs_mul}{GEN B, GEN M, GEN p} multiply the \kbd{FpV} $B$ (over9408$\Z$) by the sparse matrix $M$. The result is an \kbd{ZV}, i.e.~a9409dense vector.94109411\fun{void}{RgMs_structelim}{GEN M, long nbrow, GEN A, GEN *p_col, GEN *p_row}9412$M$ being a RgMs with \kbd{nbrow} rows, $A$ being a list of row indices,9413Perform structured elimination on $M$ by removing some rows and columns until9414the number of effectively present rows is equal to the number of columns.9415the result is stored in two \typ{VECSMALL}s, \kbd{*p\_col} and \kbd{*p\_row}:9416\kbd{*p\_col} is a map from the new columns indices to the old one.9417\kbd{*p\_row} is a map from the old rows indices to the new one ($0$ if removed).94189419\fun{GEN}{F2Ms_colelim}{GEN M, long nbrow} returns some subset of the columns9420of $M$ as a \typ{VECSMALL} of indices, selected such that the dimension of the9421kernel of the matrix is preserved. The subset is not guaranteed to be minimal.94229423\fun{GEN}{F2Ms_ker}{GEN M, long nbrow} returns some kernel vectors of $M$9424using block Lanczos algorithm.94259426\fun{GEN}{FpMs_leftkernel_elt}{GEN M, long nbrow, GEN p}9427$M$ being a sparse matrix over $\F_p$, return a nonzero kbd{FpV} $X$ such9428that $X\*M$ components are almost all $0$.94299430\fun{GEN}{FpMs_FpCs_solve}{GEN M, GEN B, long nbrow, GEN p}9431solve the equation $M\*X = B$, where $M$ is a sparse matrix and $B$ is a sparse9432vector, both over $\F_p$. Return either a solution as a \typ{COL} (dense9433vector), the index of a column which is linearly dependent from the9434others as a \typ{VECSMALL} with a single component, or \kbd{NULL}9435(can happen if $B$ is not in the image of $M$).94369437\fun{GEN}{FpMs_FpCs_solve_safe}{GEN M, GEN B, long nbrow, GEN p}9438as above, but in the event that $p$ is not a prime and an impossible division9439occurs, return \kbd{NULL}.94409441\fun{GEN}{ZpMs_ZpCs_solve}{GEN M, GEN B, long nbrow, GEN p, long e}9442solve the equation $MX = B$, where $M$ is a sparse matrix and $B$ is a sparse9443vector, both over $\Z/p^e\Z$. Return either a solution as a \typ{COL} (dense9444vector), or the index of a column which is linearly dependent from the9445others as a \typ{VECSMALL} with a single component.94469447\fun{GEN}{gen_FpM_Wiedemann}{void *E, GEN (*f)(void*, GEN), GEN B, GEN p}9448solve the equation $f(X) = B$ over $\F_p$, where $B$ is a \kbd{FpV}, and $f$9449is a blackbox endomorphism, where $f(E, X)$ computes the value of $f$ at the9450(dense) column vector $X$. Returns either a solution \typ{COL}, or a kernel9451vector as a \typ{VEC}.94529453\fun{GEN}{gen_ZpM_Dixon_Wiedemann}{void *E, GEN (*f)(void*, GEN), GEN B, GEN p, long e}9454solve equation $f(X) = B$ over $\Z/p^e\Z$, where $B$ is a \kbd{ZV}, and $f$ is a9455blackbox endomorphism, where $f(E, X)$ computes the value of $f$ at the9456(dense) column vector $X$. Returns either a solution \typ{COL}, or a kernel9457vector as a \typ{VEC}.94589459\subsec{Obsolete functions}94609461The functions in this section are kept for backward compatibility only9462and will eventually disappear.94639464\fun{GEN}{image2}{GEN x} compute the image of $x$ using a very slow9465algorithm. Use \tet{image} instead.94669467\section{Integral, rational and generic polynomial arithmetic}94689469\subsec{\kbd{ZX}}94709471\fun{void}{RgX_check_ZX}{GEN x, const char *s} Assuming \kbd{x} is a \typ{POL}9472raise an error if it is not a \kbd{ZX} ($s$ should point to the name of the9473caller).94749475\fun{GEN}{ZX_copy}{GEN x,GEN p} returns a copy of \kbd{x}.94769477\fun{long}{ZX_max_lg}{GEN x} returns the effective length of the longest9478component in $x$.94799480\fun{GEN}{scalar_ZX}{GEN x, long v} returns the constant \kbd{ZX} in variable9481$v$ equal to the \typ{INT} $x$.94829483\fun{GEN}{scalar_ZX_shallow}{GEN x, long v} returns the constant \kbd{ZX} in9484variable $v$ equal to the \typ{INT} $x$. Shallow function not suitable for9485\kbd{gerepile} and friends.94869487\fun{GEN}{ZX_renormalize}{GEN x, long l}, as \kbd{normalizepol}, where9488$\kbd{l} = \kbd{lg(x)}$, in place.94899490\fun{int}{ZX_equal}{GEN x, GEN y} returns $1$ if the two \kbd{ZX} have9491the same \kbd{degpol} and their coefficients are equal. Variable numbers are9492not checked.94939494\fun{int}{ZX_equal1}{GEN x} returns $1$ if the \kbd{ZX} $x$ is equal to $1$9495and $0$ otherwise.94969497\fun{int}{ZX_is_monic}{GEN x} returns $1$ if the \kbd{ZX} $x$ is monic and $0$9498otherwise. The zero polynomial considered not monic.94999500\fun{GEN}{ZX_add}{GEN x,GEN y} adds \kbd{x} and \kbd{y}.95019502\fun{GEN}{ZX_sub}{GEN x,GEN y} subtracts \kbd{x} and \kbd{y}.95039504\fun{GEN}{ZX_neg}{GEN x} returns $-\kbd{x}$.95059506\fun{GEN}{ZX_Z_add}{GEN x,GEN y} adds the integer \kbd{y} to the9507\kbd{ZX}~\kbd{x}.95089509\fun{GEN}{ZX_Z_add_shallow}{GEN x,GEN y} shallow version of \tet{ZX_Z_add}.95109511\fun{GEN}{ZX_Z_sub}{GEN x,GEN y} subtracts the integer \kbd{y} to the9512\kbd{ZX}~\kbd{x}.95139514\fun{GEN}{Z_ZX_sub}{GEN x,GEN y} subtracts the \kbd{ZX} \kbd{y} to the9515integer \kbd{x}.95169517\fun{GEN}{ZX_Z_mul}{GEN x,GEN y} multiplies the \kbd{ZX} \kbd{x} by the9518integer \kbd{y}.95199520\fun{GEN}{ZX_mulu}{GEN x, ulong y} multiplies \kbd{x} by the integer \kbd{y}.95219522\fun{GEN}{ZX_shifti}{GEN x, long n} shifts all coefficients of \kbd{x} by $n$9523bits, which can be negative.95249525\fun{GEN}{ZX_Z_divexact}{GEN x, GEN y} returns $x/y$ assuming all divisions9526are exact.95279528\fun{GEN}{ZX_divuexact}{GEN x, ulong y} returns $x/y$ assuming all divisions9529are exact.95309531\fun{GEN}{ZX_remi2n}{GEN x, long n} reduces all coefficients of \kbd{x} to9532$n$ bits, using \tet{remi2n}.95339534\fun{GEN}{ZX_mul}{GEN x,GEN y} multiplies \kbd{x} and \kbd{y}.95359536\fun{GEN}{ZX_sqr}{GEN x,GEN p} returns $\kbd{x}^2$.95379538\fun{GEN}{ZX_mulspec}{GEN a, GEN b, long na, long nb}. Internal routine:9539\kbd{a} and \kbd{b} are arrays of coefficients representing polynomials9540$\sum_{i = 0}^{\kbd{na-1}} \kbd{a}[i] X^i$ and9541$\sum_{i = 0}^{\kbd{nb-1}} \kbd{b}[i] X^i$. Returns their product (as a true9542\kbd{GEN}) in variable $0$.95439544\fun{GEN}{ZX_sqrspec}{GEN a, long na}. Internal routine:9545\kbd{a} is an array of coefficients representing polynomial9546$\sum_{i = 0}^{\kbd{na-1}} \kbd{a}[i] X^i$. Return its square (as a true9547\kbd{GEN}) in variable $0$.95489549\fun{GEN}{ZX_rem}{GEN x, GEN y} returns the remainder of the Euclidean9550division of $x$ mod $y$. Assume that $x$, $y$ are two \kbd{ZX} and that9551$y$ is monic.95529553\fun{GEN}{ZX_mod_Xnm1}{GEN T, ulong n} return $T$ modulo $X^n - 1)$. Shallow9554function.95559556\fun{GEN}{ZX_div_by_X_1}{GEN T, GEN *r} return the quotient of $T$ by $X-1$.9557If $r$ is not \kbd{NULL} set it to $T(1)$.95589559\fun{GEN}{ZX_gcd}{GEN x,GEN y} returns a gcd of the \kbd{ZX} $x$ and $y$.9560Not memory-clean, but suitable for \kbd{gerepileupto}.95619562\fun{GEN}{ZX_gcd_all}{GEN x, GEN y, GEN *pX} returns a gcd $d$ of $x$ and9563$y$. If \kbd{pX} is not \kbd{NULL}, set $\kbd{*pX}$ to a (nonzero) integer9564multiple of $x/d$. If $x$ and $y$ are both monic, then $d$ is monic and9565\kbd{*pX} is exactly $x/d$. Not memory clean.95669567\fun{GEN}{ZX_radical}{GEN x} returns the largest squarefree divisor9568of the \kbd{ZX} $x$. Not memory clean.95699570\fun{GEN}{ZX_content}{GEN x} returns the content of the \kbd{ZX} $x$.95719572\fun{long}{ZX_val}{GEN P} as \kbd{RgX\_val}, but assumes \kbd{P} has \typ{INT}9573coefficients.95749575\fun{long}{ZX_valrem}{GEN P, GEN *z} as \kbd{RgX\_valrem}, but assumes9576\kbd{P} has \typ{INT} coefficients.95779578\fun{GEN}{ZX_to_monic}{GEN q GEN *L} given $q$ a nonzero \kbd{ZX},9579returns a monic integral polynomial $Q$ such that $Q(x) = C q(x/L)$, for some9580rational $C$ and positive integer $L > 0$. If $\kbd{L}$ is not \kbd{NULL},9581set \kbd{*L} to $L$; if $L = 1$, \kbd{*L} is set to \kbd{gen\_1}. Shallow9582function.95839584\fun{GEN}{ZX_primitive_to_monic}{GEN q, GEN *L} as \tet{ZX_to_monic} except9585$q$ is assumed to have trivial content, which avoids recomputing it.9586The result is suboptimal if $q$ is not primitive ($L$ larger than9587necessary), but remains correct. Shallow function.95889589\fun{GEN}{ZX_Z_normalize}{GEN q, GEN *L} a restricted version of9590\kbd{ZX\_primitive\_to\_monic}, where $q$ is a \emph{monic} \kbd{ZX}9591of degree $> 0$. Finds the largest integer $L > 0$ such that9592$Q(X) := L^{-\deg q} q(Lx)$ is integral and return $Q$; this is not9593well-defined if $q$ is a monomial, in that case, set $L=1$ and $Q = q$. If9594\kbd{L} is not \kbd{NULL}, set \kbd{*L} to $L$. Shallow function.95959596\fun{GEN}{ZX_Q_normalize}{GEN q, GEN *L} a variant of \tet{ZX_Z_normalize}9597where $L > 0$ is allowed to be rational, the monic $Q\in \Z[X]$ has possibly9598smaller coefficients. Shallow function.95999600\fun{GEN}{ZX_Q_mul}{GEN x, GEN y} returns $x*y$, where $y$ is a rational number9601and the resulting \typ{POL} has rational entries.96029603\fun{long}{ZX_deflate_order}{GEN P} given a nonconstant \kbd{ZX}9604$P$, returns the largest exponent $d$ such that $P$ is of the form $P(x^d)$.96059606\fun{long}{ZX_deflate_max}{GEN P, long *d}. Given a nonconstant9607polynomial with integer coefficients $P$, sets \kbd{d} to9608\kbd{ZX\_deflate\_order(P)} and returns \kbd{RgX\_deflate(P,d)}. Shallow9609function.96109611\fun{GEN}{ZX_rescale}{GEN P, GEN h} returns $h^{\deg(P)} P(x/h)$.9612\kbd{P} is a \kbd{ZX} and \kbd{h} is a nonzero integer. Neither memory-clean9613nor suitable for \kbd{gerepileupto}.96149615\fun{GEN}{ZX_rescale2n}{GEN P, long n} returns $2^{n\deg(P)} P(x>>n)$ where9616\kbd{P} is a \kbd{ZX}.96179618\fun{GEN}{ZX_rescale_lt}{GEN P} returns the monic integral polynomial9619$h^{\deg(P)-1} P(x/h)$, where \kbd{P} is a nonzero \kbd{ZX} and \kbd{h} is9620its leading coefficient. Neither memory-clean nor suitable for9621\kbd{gerepileupto}.96229623\fun{GEN}{ZX_translate}{GEN P, GEN c} assume $P$ is a \kbd{ZX} and $c$ an9624integer. Returns $P(X + c)$ (optimized for $c = \pm 1$).96259626\fun{GEN}{ZX_Z_eval}{GEN P,GEN x} evaluate the \kbd{ZX} $P$ at the integer $x$.96279628\fun{GEN}{ZX_unscale}{GEN P, GEN h} given a \kbd{ZX} $P$ and a \typ{INT} $h$,9629returns $P(hx)$. Not memory clean.96309631\fun{GEN}{ZX_z_unscale}{GEN P, long h} given a \kbd{ZX} $P$,9632returns $P(hx)$. Not memory clean.96339634\fun{GEN}{ZX_unscale2n}{GEN P, long n} given a \kbd{ZX} $P$, returns9635$P(x<<n)$. Not memory clean.96369637\fun{GEN}{ZX_unscale_div}{GEN P, GEN h} given a \kbd{ZX} $P$ and a \typ{INT} $h$9638such that $h \mid P(0)$, returns $P(hx)/h$. Not memory clean.96399640\fun{GEN}{ZX_eval1}{GEN P} returns the integer $P(1)$.96419642\fun{GEN}{ZX_graeffe}{GEN p} returns the Graeffe transform of $p$, i.e. the9643\kbd{ZX} $q$ such that $p(x)p(-x) = q(x^2)$.96449645\fun{GEN}{ZX_deriv}{GEN x} returns the derivative of \kbd{x}.96469647\fun{GEN}{ZX_resultant}{GEN A, GEN B} returns the resultant of the9648\kbd{ZX}~\kbd{A} and \kbd{B}.96499650\fun{GEN}{ZX_disc}{GEN T} returns the discriminant of the \kbd{ZX}9651\kbd{T}.96529653\fun{GEN}{ZX_factor}{GEN T} returns the factorization of the primitive part9654of \kbd{T} over $\Q[X]$ (the content is lost).96559656\fun{int}{ZX_is_squarefree}{GEN T} returns $1$ if the9657\kbd{ZX}~\kbd{T} is squarefree, $0$ otherwise.96589659\fun{long}{ZX_is_irred}{GEN T} returns 1 it \kbd{T} is irreducible, and96600 otherwise.96619662\fun{GEN}{ZX_squff}{GEN T, GEN *E} write $T$ as a product $\prod T_i^{e_i}$9663with the $e_1 < e_2 < \cdots$ all distinct and the $T_i$ pairwise coprime.9664Return the vector of the $T_i$, and set \kbd{*E} to the vector of the $e_i$,9665as a \typ{VECSMALL}.96669667\fun{GEN}{ZX_Uspensky}{GEN P, GEN ab, long flag, long bitprec} let \kbd{P} be a9668\kbd{ZX} polynomial whose real roots are simple and \kbd{bitprec} is the9669relative precision in bits. For efficiency reasons, \kbd{P} should not only9670have simple real roots but actually be primitive and squarefree, but the9671routine neither checks nor enforces this, and it returns correct results in9672this case as well.96739674\item If \kbd{flag} is 0 returns a list of intervals that isolate the real9675roots of \kbd{P}. The return value is a column of elements which are either9676vectors \kbd{[a,b]} of rational numbers meaning that there is a single9677nonrational root in the open interval \kbd{(a,b)} or elements \kbd{x0} such9678that \kbd{x0} is a rational root of \kbd{P}. Beware that the limits of the9679open intervals can be roots of the polynomial.96809681\item If \kbd{flag} is 1 returns an approximation of the real roots of \kbd{P}.96829683\item If \kbd{flag} is 2 returns the number of roots.96849685The argument \kbd{ab} specify the interval in which the roots9686are searched. The default interval is $(-\infty,\infty)$. If \kbd{ab} is an9687integer or fraction $a$ then the interval is $[a,\infty)$. If \kbd{ab} is9688a vector $[a,b]$, where \typ{INT}, \typ{FRAC} or \typ{INFINITY} are allowed9689for $a$ and $b$, the interval is $[a,b]$.96909691\fun{long}{ZX_sturm}{GEN P} number of real roots of the nonconstant9692squarefree \kbd{ZX} $P$. For efficiency, it is advised to make $P$ primitive9693first.96949695\fun{long}{ZX_sturmpart}{GEN P, GEN ab} number of real roots of the9696nonconstant squarefree \kbd{ZX} $P$ in the interval specified by \kbd{ab}:9697either \kbd{NULL} (no restriction) or a \typ{VEC} $[a,b]$ with two real9698components (of type \typ{INT}, \typ{FRAC} or \typ{INFINITY}). For efficiency,9699it is advised to make $P$ primitive first.97009701\fun{long}{ZX_sturm_irred}{GEN P} number of real roots of the \kbd{ZX} $P$,9702assumed irreducible over $\Q[X]$. For efficiency, it is advised to make $P$9703primitive first.97049705\fun{long}{ZX_realroots_irred}{GEN P, long prec} real roots of the \kbd{ZX}9706$P$, assumed irreducible over $\Q[X]$ to precision prec. For efficiency, it9707is advised to make $P$ primitive first.97089709\subsec{Resultants}97109711\fun{GEN}{ZX_ZXY_resultant}{GEN A, GEN B}9712under the assumption that \kbd{A} in $\Z[Y]$, \kbd{B} in $\Q[Y][X]$, and9713$R = \text{Res}_Y(A, B) \in \Z[X]$, returns the resultant $R$.97149715\fun{GEN}{ZX_compositum_disjoint}{GEN A, GEN B} given two irreducible \kbd{ZX}9716defining linearly disjoint extensions, returns a \kbd{ZX} defining their9717compositum.97189719\fun{GEN}{ZX_compositum}{GEN A, GEN B, long *lambda}9720given two irreducible \kbd{ZX}, returns an irreducible \kbd{ZX} $C$ defining9721their compositum and set \kbd{lambda} to a small integer $k$ such that if9722$\alpha$ is a root of $A$ and $\beta$ is a root of $B$, then $k\*\alpha +9723\beta$ is a root of $C$.97249725\fun{GEN}{ZX_ZXY_rnfequation}{GEN A, GEN B, long *lambda},9726assume \kbd{A} in $\Z[Y]$, \kbd{B} in $\Q[Y][X]$, and $R =9727\text{Res}_Y(A, B) \in \Z[X]$. If \kbd{lambda = NULL}, returns $R$9728as in \kbd{ZY\_ZXY\_resultant}. Otherwise, \kbd{lambda} must point to9729some integer, e.g. $0$ which is used as a seed. The function then finds a9730small $\lambda \in \Z$ (starting from \kbd{*lambda}) such that9731$R_\lambda(X) := \text{Res}_Y(A, B(X + \lambda Y))$ is squarefree, resets9732\kbd{*lambda} to the chosen value and returns $R_{\lambda}$.97339734\subsec{\kbd{ZXV}}97359736\fun{GEN}{ZXV_equal}{GEN x,GEN y} returns $1$ if the two vectors of \kbd{ZX}9737are equal, as per \tet{ZX_equal} (variables are not checked to be equal) and9738$0$ otherwise.97399740\fun{GEN}{ZXV_Z_mul}{GEN x,GEN y} multiplies the vector of \kbd{ZX} \kbd{x}9741by the integer \kbd{y}.97429743\fun{GEN}{ZXV_remi2n}{GEN x, long n} applies \kbd{ZX\_remi2n} to all9744coefficients of \kbd{x}.97459746\fun{GEN}{ZXV_dotproduct}{GEN x,GEN y} as \kbd{RgV\_dotproduct} assuming $x$9747and $y$ have \kbd{ZX} entries.97489749\subsec{\kbd{ZXT}}97509751\fun{GEN}{ZXT_remi2n}{GEN x, long n} applies \kbd{ZX\_remi2n} to all9752leaves of the tree \kbd{x}.97539754\subsec{\kbd{ZXQ}}97559756\fun{GEN}{ZXQ_mul}{GEN x,GEN y,GEN T} returns $x*y$ mod $T$, assuming9757that all inputs are \kbd{ZX}s and that $T$ is monic.97589759\fun{GEN}{ZXQ_sqr}{GEN x,GEN T} returns $x^2$ mod $T$, assuming9760that all inputs are \kbd{ZX}s and that $T$ is monic.97619762\fun{GEN}{ZXQ_powu}{GEN x,ulong n,GEN T} returns $x^n$ mod $T$, assuming9763that all inputs are \kbd{ZX}s and that $T$ is monic.97649765\fun{GEN}{ZXQ_powers}{GEN x, long n, GEN T} returns $[\kbd{x}^0,9766\dots, \kbd{x}^\kbd{n}]$ mod $T$ as a \typ{VEC}, assuming that all inputs are9767\kbd{ZX}s and that $T$ is monic.97689769\fun{GEN}{ZXQ_charpoly}{GEN A, GEN T, long v}: let \kbd{T} and \kbd{A} be9770\kbd{ZX}s, returns the characteristic polynomial of \kbd{Mod(A, T)}.9771More generally, \kbd{A} is allowed to be a \kbd{QX}, hence possibly has9772rational coefficients, \emph{assuming} the result is a \kbd{ZX}, i.e.~the9773algebraic number \kbd{Mod(A,T)} is integral over \kbd{Z}.97749775\fun{GEN}{ZXQ_minpoly}{GEN A, GEN B, long d, ulong bound}9776let \kbd{T} and \kbd{A} be9777\kbd{ZX}s, returns the minimal polynomial of \kbd{Mod(A, T)} assuming it has9778degree $d$ and its coefficients are less than $2^{\text{bound}}$.9779More generally, \kbd{A} is allowed to be a \kbd{QX}, hence possibly has9780rational coefficients, \emph{assuming} the result is a \kbd{ZX}, i.e.~the9781algebraic number \kbd{Mod(A,T)} is integral over \kbd{Z}.97829783\subsec{\kbd{ZXn}}97849785\fun{GEN}{ZXn_mul}{GEN x, GEN y, long n} return $x\*y\pmod{X^n}$.97869787\fun{GEN}{ZXn_sqr}{GEN x, long n} return $x^2\pmod{X^n}$.97889789\fun{GEN}{eta_ZXn}{long r, long n} return $\eta(X^r) = \prod_{i>0} (1-X^{ri})9790\pmod{X^n}$, $r > 0$.97919792\fun{GEN}{eta_product_ZXn}{GEN DR, long n}: $\kbd{DR}= [D,R]$ being a vector9793with two \typ{VECSMALL} components, return $\prod_i \eta(X^{d_i})^{r_i}$.9794Shallow function.97959796\subsec{\kbd{ZXQM}}97979798\kbd{ZXQM} are matrices of \kbd{ZXQ}. All entries must be integers or9799polynomials of degree strictly less than the degree of $T$.98009801\fun{GEN}{ZXQM_mul}{GEN x,GEN y,GEN T} returns $x*y$ mod $T$, assuming9802that all inputs are \kbd{ZX}s and that $T$ is monic.98039804\fun{GEN}{ZXQM_sqr}{GEN x,GEN T} returns $x^2$ mod $T$, assuming9805that all inputs are \kbd{ZX}s and that $T$ is monic.98069807\subsec{\kbd{ZXQX}}98089809\fun{GEN}{ZXQX_mul}{GEN x,GEN y,GEN T} returns $x*y$, assuming9810that all inputs are \kbd{ZXQX}s and that $T$ is monic.98119812\fun{GEN}{ZXQX_ZXQ_mul}{GEN x, GEN y, GEN T} returns $x*y$, assuming9813that $x$ is a \kbd{ZXQX}, $y$ is a \kbd{ZXQ} and $T$ is monic.98149815\fun{GEN}{ZXQX_sqr}{GEN x,GEN T} returns $x^2$, assuming9816that all inputs are \kbd{ZXQX}s and that $T$ is monic.98179818\fun{GEN}{ZXQX_gcd}{GEN x,GEN y,GEN T} returns the gcd of $x$ and $y$,9819assuming that all inputs are \kbd{ZXQX}s and that $T$ is monic.98209821\subsec{\kbd{ZXX}}98229823\fun{void}{RgX_check_ZXX}{GEN x, const char *s} Assuming \kbd{x} is a \typ{POL}9824raise an error if it one of its coefficients is not an integer or a \kbd{ZX}9825($s$ should point to the name of the caller).98269827\fun{GEN}{ZXX_renormalize}{GEN x, long l}, as \kbd{normalizepol}, where9828$\kbd{l} = \kbd{lg(x)}$, in place.98299830\fun{long}{ZXX_max_lg}{GEN x} returns the effective length of the longest9831component in $x$; assume all coefficients are \typ{INT} or \kbd{ZX}s.98329833\fun{GEN}{ZXX_evalx0}{GEN P} returns $P(X, 0)$.98349835\fun{GEN}{ZXX_Z_mul}{GEN x, GEN y} returns $x\*y$.98369837\fun{GEN}{ZXX_Q_mul}{GEN x, GEN y} returns $x*y$, where $y$ is a rational number9838and the resulting \typ{POL} has rational entries.98399840\fun{GEN}{ZXX_Z_add_shallow}{GEN x, GEN y} returns $x+y$. Shallow function.98419842\fun{GEN}{ZXX_Z_divexact}{GEN x, GEN y} returns $x/y$ assuming all integer9843divisions are exact.98449845\fun{GEN}{Kronecker_to_ZXX}{GEN z, long n, long v} recover $P(X,Y)$9846from its Kronecker form $P(X,X^{2\*n-1})$ (see \tet{RgXX_to_Kronecker}),9847$v$ is the variable number corresponding to $Y$. Shallow function.98489849\fun{GEN}{Kronecker_to_ZXQX}{GEN z, GEN T}. Let $n = \deg T$ and let9850$P(X,Y)\in \Z[X,Y]$ lift a polynomial in $K[Y]$, where $K := \Z[X]/(T)$ and9851$\deg_X P < 2n-1$ --- such as would result from multiplying minimal degree9852lifts of two polynomials in $K[Y]$. Let $z = P(t,t^{2*n-1})$ be a Kronecker9853form of $P$ (see \tet{RgXX_to_Kronecker}), this function returns $Q\in9854\Z[X,t]$ such that $Q$ is congruent to $P(X,t)$ mod $(T(X))$, $\deg_X Q <9855n$. Not stack-clean. Note that $t$ need not be the same variable as $Y$!98569857\fun{GEN}{ZXX_mul_Kronecker}{GEN P, GEN Q, long n} return \tet{ZX_mul}9858applied to the Kronecker forms $P(X,X^{2\*n-1})$ and $Q(X,X^{2\*n-1})$9859of $P$ and $Q$. Not memory clean.98609861\fun{GEN}{ZXX_sqr_Kronecker}{GEN P, long n} return \tet{ZX_sqr}9862applied to the Kronecker forms $P(X,X^{2\*n-1})$9863of $P$. Not memory clean.98649865\subsec{\kbd{QX}}98669867\fun{void}{RgX_check_QX}{GEN x, const char *s} Assuming \kbd{x} is a \typ{POL}9868raise an error if it is not a \kbd{QX} ($s$ should point to the name of the9869caller).98709871\fun{GEN}{QX_mul}{GEN x,GEN y}98729873\fun{GEN}{QX_sqr}{GEN x}98749875\fun{GEN}{QX_ZX_rem}{GEN x, GEN y} $y$ is assumed to be monic.98769877\fun{GEN}{QX_gcd}{GEN x,GEN y} returns a gcd of the \kbd{QX} $x$ and $y$.98789879\fun{GEN}{QX_disc}{GEN T} returns the discriminant of the \kbd{QX}9880\kbd{T}.98819882\fun{GEN}{QX_factor}{GEN T} as \kbd{ZX\_factor}.98839884\fun{GEN}{QX_resultant}{GEN A, GEN B} returns the resultant of the9885\kbd{QX}~\kbd{A} and \kbd{B}.98869887\fun{GEN}{QX_complex_roots}{GEN p, long l} returns the complex roots of the9888\kbd{QX} $p$ at accuracy $l$, where real roots are returned as \typ{REAL}s.9889More efficient when $p$ is irreducible and primitive. Special case9890of \tet{cleanroots}.98919892\subsec{\kbd{QXQ}}98939894\fun{GEN}{QXQ_norm}{GEN A, GEN B} $A$ being a \kbd{QX} and $B$ being a9895\kbd{ZX}, returns the norm of the algebraic number $A \mod B$, using a9896modular algorithm. To ensure that $B$ is a \kbd{ZX}, one may replace it by9897\kbd{Q\_primpart(B)}, which of course does not change the norm.98989899If $A$ is not a \kbd{ZX} --- it has a denominator ---, but the result is9900nevertheless known to be an integer, it is much more efficient to call9901\tet{QXQ_intnorm} instead.99029903\fun{GEN}{QXQ_intnorm}{GEN A, GEN B} $A$ being a \kbd{QX} and $B$9904being a \kbd{ZX}, returns the norm of the algebraic number $A \mod B$,9905\emph{assuming} that the result is an integer, which is for instance the case9906is $A\mod B$ is an algebraic integer, in particular if $A$ is a \kbd{ZX}. To9907ensure that $B$ is a \kbd{ZX}, one may replace it by \kbd{Q\_primpart(B)}9908(which of course does not change the norm).99099910If the result is not known to be an integer, you must use \tet{QXQ_norm}9911instead, which is slower.99129913\fun{GEN}{QXQ_mul}{GEN A, GEN B, GEN T} returns the product of $A$ and $B$9914modulo $T$ where both $A$ and $B$ are a \kbd{QX} and $T$ is a monic \kbd{ZX}.99159916\fun{GEN}{QXQ_sqr}{GEN A, GEN T} returns the square of $A$9917modulo $T$ where $A$ is a \kbd{QX} and $T$ is a monic \kbd{ZX}.99189919\fun{GEN}{QXQ_inv}{GEN A, GEN B} returns the inverse of $A$ modulo $B$9920where $A$ is a \kbd{QX} and $B$ is a \kbd{ZX}. Should you need this for9921a \kbd{QX} $B$, just use9922\bprog9923QXQ_inv(A, Q_primpart(B));9924@eprog\noindent But in all cases where modular arithmetic modulo $B$ is9925desired, it is much more efficient to replace $B$ by \kbd{Q\_primpart$(B)$}9926once and for all.99279928\fun{GEN}{QXQ_div}{GEN A, GEN B, GEN T} returns $A/B$ modulo $T$9929where $A$ and $B$ are \kbd{QX} and $T$ is a \kbd{ZX}. Use this function9930when the result is expected to be of the same size as $B^{-1}$ mod $T$9931or smaller.9932Otherwise, it will be faster to use \tet{QXQ_mul(A,QXQ_inv(B,T),T)}.99339934\fun{GEN}{QXQ_charpoly}{GEN A, GEN T, long v} where \kbd{A} is a \kbd{QX} and9935\kbd{T} is a \kbd{ZX}, returns the characteristic polynomial of \kbd{Mod(A, T)}.9936If the result is known to be a \kbd{ZX}, then calling \kbd{ZXQ\_charpoly} will9937be faster.99389939\fun{GEN}{QXQ_powers}{GEN x, long n, GEN T} returns $[\kbd{x}^0, \dots,9940\kbd{x}^\kbd{n}]$ as \kbd{RgXQ\_powers} would, but in a more efficient way when9941$x$ has a huge integer denominator (we start by removing that denominator).9942Meant to be used to precompute powers of algebraic integers in $\Q[t]/(T)$.9943The current implementation does not require $x$ to be a \kbd{QX}: any9944polynomial to which \kbd{Q\_remove\_denom} can be applied is fine.99459946\fun{GEN}{QXQ_reverse}{GEN f, GEN T} as \kbd{RgXQ\_reverse}, assuming $f$9947is a \kbd{QX}.99489949\fun{GEN}{QX_ZXQV_eval}{GEN f, GEN nV, GEN dV} as \kbd{RgX\_RgXQV\_eval},9950except that $f$ is assumed to be a \kbd{QX}, $V$ is given implicitly9951by a numerator \kbd{nV} (\kbd{ZV}) and denominator \kbd{dV} (a positive9952\typ{INT} or \kbd{NULL} for trivial denominator). Not memory clean, but9953suitable for \kbd{gerepileupto}.99549955\fun{GEN}{QXV_QXQ_eval}{GEN v, GEN a, GEN T} $v$ is a vector of \kbd{QX}s9956(possibly scalars, i.e.~rational numbers, for convenience), $a$ and $T$ both9957\kbd{QX}. Return the vector of evaluations at $a$ modulo $T$.9958Not memory clean, nor suitable for \kbd{gerepileupto}.99599960\fun{GEN}{QXY_QXQ_evalx}{GEN P, GEN a, GEN T} $P(X,Y)$ is a \typ{POL} with9961\kbd{QX} coefficients (possibly scalars, i.e.~rational numbers, for9962convenience) , $a$ and $T$ both \kbd{QX}. Return the \kbd{QX} $P(a \mod9963T, Y)$. Not memory clean, nor suitable for \kbd{gerepileupto}.99649965\subsec{\kbd{QXQX}}99669967\fun{GEN}{QXQX_mul}{GEN x, GEN y, GEN T} where $T$ is a monic \kbd{ZX}.99689969\fun{GEN}{QXQX_QXQ_mul}{GEN x, GEN y, GEN T} where $T$ is a monic \kbd{ZX}.99709971\fun{GEN}{QXQX_sqr}{GEN x, GEN T} where $T$ is a monic \kbd{ZX}99729973\fun{GEN}{QXQX_powers}{GEN x, long n, GEN T} where $T$ is a monic \kbd{ZX}99749975\fun{GEN}{nfgcd}{GEN P, GEN Q, GEN T, GEN den} given $P$ and $Q$ in9976$\Z[X,Y]$, $T$ monic irreducible in $\Z[Y]$, returns the primitive $d$ in9977$\Z[X,Y]$ which is a gcd of $P$, $Q$ in $K[X]$, where $K$ is the number field9978$\Q[Y]/(T)$. If not \kbd{NULL}, \kbd{den} is a multiple of the integral9979denominator of the (monic) gcd of $P,Q$ in $K[X]$.99809981\fun{GEN}{nfgcd_all}{GEN P, GEN Q, GEN T, GEN den, GEN *Pnew} as \kbd{nfgcd}.9982If \kbd{Pnew} is not \kbd{NULL}, set \kbd{*Pnew} to a nonzero integer9983multiple of $P/d$. If $P$ and $Q$ are both monic, then $d$ is monic and9984\kbd{*Pnew} is exactly $P/d$. Not memory clean if the gcd is $1$9985(in that case \kbd{*Pnew} is set to $P$).99869987\fun{GEN}{QXQX_gcd}{GEN x, GEN y, GEN T} returns the gcd of $x$ and $y$,9988assuming that $x$ and $y$ are \kbd{QXQX}s and that $T$ is a monic \kbd{ZX}.99899990\subsec{\kbd{QXQM}}99919992\kbd{QXQM} are matrices of \kbd{QXQ}. All entries must be \typ{INT}, \typ{FRAC} or9993polynomials of degree strictly less than the degree of $T$, which must be a monic9994\kbd{ZX}.99959996\fun{GEN}{QXQM_mul}{GEN x,GEN y,GEN T} returns $x*y$ mod $T$.99979998\fun{GEN}{QXQM_sqr}{GEN x,GEN T} returns $x^2$ mod $T$.999910000\subsec{\kbd{zx}}1000110002\fun{GEN}{zero_zx}{long sv} returns a zero \kbd{zx} in variable $v$.1000310004\fun{GEN}{polx_zx}{long sv} returns the variable $v$ as degree~1~\kbd{Flx}.1000510006\fun{GEN}{zx_renormalize}{GEN x, long l}, as \kbd{Flx\_renormalize}, where10007$\kbd{l} = \kbd{lg(x)}$, in place.1000810009\fun{GEN}{zx_shift}{GEN T, long n} return \kbd{T}10010multiplied by $\kbd{x}^n$, assuming $n\geq 0$.1001110012\fun{long}{zx_lval}{GEN f, long p} return the valuation of $f$ at $p$.1001310014\fun{GEN}{zx_z_divexact}{GEN x, long y} return \kbd{x/y} assuming all divisions10015are exact.1001610017\subsec{\kbd{RgX}}1001810019\subsubsec{Tests}1002010021\fun{long}{RgX_degree}{GEN x, long v} $x$ being a \typ{POL} and $v \geq 0$,10022returns the degree in $v$ of $x$. Error if $x$ is not a polynomial in $v$.1002310024\fun{int}{RgX_isscalar}{GEN x} return 1 if $x$ all the coefficients of10025$x$ of degree $> 0$ are $0$ (as per \kbd{gequal0}).1002610027\fun{int}{RgX_is_rational}{GEN P} return 1 if the \kbd{RgX}~$P$ has only10028rational coefficients (\typ{INT} and \typ{FRAC}), and 0 otherwise.1002910030\fun{int}{RgX_is_QX}{GEN P} return 1 if the \kbd{RgX}~$P$ has only10031\typ{INT} and \typ{FRAC} coefficients, and 0 otherwise.1003210033\fun{int}{RgX_is_ZX}{GEN P} return 1 if the \kbd{RgX}~$P$ has only10034\typ{INT} coefficients, and 0 otherwise.1003510036\fun{int}{RgX_is_monomial}{GEN x} returns 1 (true) if \kbd{x} is a nonzero10037monomial in its main variable, 0~otherwise.1003810039\fun{long}{RgX_equal}{GEN x, GEN y} returns $1$ if the \typ{POL}s $x$ and $y$10040have the same \kbd{degpol} and their coefficients are equal (as per10041\tet{gequal}). Variable numbers are not checked. Note that this is more10042stringent than \kbd{gequal(x,y)}, which only checks whether $x - y$ satisfies10043\kbd{gequal0}; in particular, they may have different apparent degrees provided10044the extra leading terms are $0$.1004510046\fun{long}{RgX_equal_var}{GEN x, GEN y} returns $1$ if $x$ and $y$10047have the same variable number and \kbd{RgX\_equal(x,y)} is $1$.1004810049\subsubsec{Coefficients, blocks}1005010051\fun{GEN}{RgX_coeff}{GEN P, long n} return the coefficient of $x^n$ in $P$,10052defined as \kbd{gen\_0} if $n < 0$ or $n > \kbd{degpol}(P)$. Shallow10053function.1005410055\fun{int}{RgX_blocks}{GEN P, long n, long m} writes10056$P(X)=a_0(X)+X^n*a_1(X)*X^n+\ldots+X^{n*(m-1)}\*a_{m-1}(X)$,10057where the $a_i$ are polynomial of degree at most $n-1$10058(except possibly for the last one) and returns10059$[a_0(X),a_1(X),\ldots,a_{m-1}(X)]$. Shallow function.1006010061\fun{void}{RgX_even_odd}{GEN p, GEN *pe, GEN *po} write $p(X) = E(X^2) +10062X O(X^2)$ and set \kbd{*pe = E}, \kbd{*po = O}. Shallow function.1006310064\fun{GEN}{RgX_splitting}{GEN P, long k} write10065$P(X)=a_0(X^k)+X\*a_1(X^k)+\ldots+X^{k-1}\*a_{k-1}(X^k)$ and return10066$[a_0(X),a_1(X),\ldots,a_{k-1}(X)]$. Shallow function.1006710068\fun{GEN}{RgX_copy}{GEN x} returns (a deep copy of) $\kbd{x}$.1006910070\fun{GEN}{RgX_renormalize}{GEN x} remove leading terms in \kbd{x} which are10071equal to (necessarily inexact) zeros.1007210073\fun{GEN}{RgX_renormalize_lg}{GEN x, long lx} as \kbd{setlg(x, lx)}10074followed by \kbd{RgX\_renormalize(x)}. Assumes that $\kbd{lx} \leq10075\kbd{lg(x)}$.1007610077\fun{GEN}{RgX_recip}{GEN P} returns the reverse of the polynomial10078$P$, i.e. $X^{\deg P} P(1/X)$.1007910080\fun{GEN}{RgX_recip_shallow}{GEN P} shallow function of \tet{RgX_recip}.1008110082\fun{GEN}{RgX_recip_i}{GEN P} shallow function of \tet{RgX_recip},10083where we further assume that $P(0)\neq 0$, so that the degree of the output10084is the degree of $P$.1008510086\fun{long}{rfracrecip}{GEN *a, GEN *b} let \kbd{*a} and \kbd{*b} be such that10087their quotient $F$ is a \typ{RFRAC} in variable $X$. Write $F(1/X) = X^v A/B$10088where $A$ and $B$ are coprime to $X$ and $v$ in $\Z$. Set \kbd{*a} to A,10089\kbd{*b} to $B$ and return $v$.1009010091\fun{GEN}{RgX_deflate}{GEN P, long d} assuming $P$ is a polynomial of the10092form $Q(X^d)$, return $Q$. Shallow function, not suitable for10093\kbd{gerepileupto}.1009410095\fun{long}{RgX_deflate_order}{GEN P} given a nonconstant polynomial10096$P$, returns the largest exponent $d$ such that $P$ is of the form $P(x^d)$10097(use \kbd{gequal0} to check whether coefficients are 0).1009810099\fun{long}{RgX_deflate_max}{GEN P, long *d} given a nonconstant polynomial10100$P$, sets \kbd{d} to \kbd{RgX\_deflate\_order(P)} and10101returns \kbd{RgX\_deflate(P,d)}. Shallow function.1010210103\fun{long}{rfrac_deflate_order}{GEN F}10104as \kbd{RgX\_deflate\_order} where $F$ is a nonconstant \typ{RFRAC}.1010510106\fun{long}{rfrac_deflate_max}{GEN F, long *d}10107as \kbd{RgX\_deflate\_max} where $F$ is a nonconstant \typ{RFRAC}.1010810109\fun{GEN}{rfrac_deflate}{GEN F, long m}10110as \kbd{RgX\_deflate} where $F$ is a \typ{RFRAC}.1011110112\fun{GEN}{RgX_inflate}{GEN P, long d} return $P(X^d)$. Shallow function, not10113suitable for \kbd{gerepileupto}.1011410115\fun{GEN}{RgX_rescale_to_int}{GEN x} given a polynomial $x$ with real entries10116(\typ{INT}, \typ{FRAC} or \typ{REAL}), return a \kbd{ZX} wich is very close10117to $D x$ for some well-chosen integer $D$. More precisely, if the input is10118exact, $D$ is the denominator of $x$; else it is a power of $2$ chosen10119so that all inexact entries are correctly rounded to 1 ulp.1012010121\fun{GEN}{RgXX_to_Kronecker}{GEN P, long n} Assuming $P(X,Y)$ is a polynomial10122of degree in $X$ strictly less than $n$, returns $P(X,X^{2*n-1})$, the10123Kronecker form of $P$. Shallow function.1012410125\fun{GEN}{RgXX_to_Kronecker_spec}{GEN Q, long lQ, long n} return10126\tet{RgXX_to_Kronecker}$(P, n)$, where $P$ is the polynomial10127$\sum_{i = 0}^{\kbd{lQ} - 1} Q[i] x^i$. To be used when splitting10128the coefficients of genuine polynomials into blocks. Shallow function.1012910130\subsubsec{Shifts, valuations}1013110132\fun{GEN}{RgX_shift}{GEN x, long n} returns $\kbd{x} * t^n$ if $n\geq 0$,10133and $\kbd{x} \bs t^{-n}$ otherwise.1013410135\fun{GEN}{RgX_shift_shallow}{GEN x, long n} as \kbd{RgX\_shift}, but10136shallow (coefficients are not copied).1013710138\fun{GEN}{RgX_rotate_shallow}{GEN P, long k, long p} returns $\kbd{P} * X^k10139\pmod {X^p-1}$, assuming the degree of $P$ is strictly less than $p$, and10140$k\geq 0$.1014110142\fun{void}{RgX_shift_inplace_init}{long v} $v \geq 0$, prepare for a later10143call to \tet{RgX_shift_inplace}. Reserves $v$ words on the stack.1014410145\fun{GEN}{RgX_shift_inplace}{GEN x, long v} $v \geq 0$, assume that10146\tet{RgX_shift_inplace_init}$(v)$ has been called (reserving $v$ words on the10147stack), immediately followed by a \typ{POL} $x$. Return \kbd{RgX\_shift}$(x,v)$10148by shifting $x$ in place. To be used as follows10149\bprog10150RgX_shift_inplace_init(v);10151av = avma;10152...10153x = gerepileupto(av, ...); /* a t_POL */10154return RgX_shift_inplace(x, v);10155@eprog1015610157\fun{long}{RgX_valrem}{GEN P, GEN *pz} returns the valuation $v$ of the10158\typ{POL}~\kbd{P} with respect to its main variable $X$. Check whether10159coefficients are $0$ using \kbd{isexactzero}. Set \kbd{*pz} to10160$\kbd{RgX\_shift\_shallow}(P,-v)$.1016110162\fun{long}{RgX_val}{GEN P} returns the valuation $v$ of the10163\typ{POL}~\kbd{P} with respect to its main variable $X$. Check whether10164coefficients are $0$ using \kbd{isexactzero}.1016510166\fun{long}{RgX_valrem_inexact}{GEN P, GEN *z} as \kbd{RgX\_valrem}, using10167\kbd{gequal0} instead of \kbd{isexactzero}.1016810169\fun{long}{RgXV_maxdegree}{GEN V} returns the maximum of the degrees of10170the components of the vector of \typ{POL}s $V$.1017110172\subsubsec{Basic arithmetic}1017310174\fun{GEN}{RgX_add}{GEN x,GEN y} adds \kbd{x} and \kbd{y}.1017510176\fun{GEN}{RgX_sub}{GEN x,GEN y} subtracts \kbd{x} and \kbd{y}.1017710178\fun{GEN}{RgX_neg}{GEN x} returns $-\kbd{x}$.1017910180\fun{GEN}{RgX_Rg_add}{GEN y, GEN x} returns $x+y$.1018110182\fun{GEN}{RgX_Rg_add_shallow}{GEN y, GEN x} returns $x+y$; shallow function.1018310184\fun{GEN}{Rg_RgX_sub}{GEN x, GEN y}1018510186\fun{GEN}{RgX_Rg_sub}{GEN y, GEN x} returns $x-y$1018710188\fun{GEN}{RgX_Rg_mul}{GEN y, GEN x} multiplies the \kbd{RgX} \kbd{y}10189by the scalar \kbd{x}.1019010191\fun{GEN}{RgX_muls}{GEN y, long s} multiplies the \kbd{RgX} \kbd{y}10192by the \kbd{long}~\kbd{s}.1019310194\fun{GEN}{RgX_Rg_div}{GEN y, GEN x} divides the \kbd{RgX} \kbd{y}10195by the scalar \kbd{x}.1019610197\fun{GEN}{RgX_divs}{GEN y, long s} divides the \kbd{RgX} \kbd{y}10198by the \kbd{long}~\kbd{s}.1019910200\fun{GEN}{RgX_Rg_divexact}{GEN x, GEN y} exact division of the \kbd{RgX}10201\kbd{y} by the scalar \kbd{x}.1020210203\fun{GEN}{RgX_Rg_eval_bk}{GEN f, GEN x} returns $\kbd{f}(\kbd{x})$ using10204Brent and Kung algorithm. (Use \tet{poleval} for Horner algorithm.)1020510206\fun{GEN}{RgX_RgV_eval}{GEN f, GEN V} as \kbd{RgX\_Rg\_eval\_bk(f, x)},10207assuming $V$ was output by \kbd{gpowers(x, n)} for some $n\geq 1$.1020810209\fun{GEN}{RgXV_RgV_eval}{GEN f, GEN V} apply \kbd{RgX\_RgV\_eval\_bk(, V)}10210to all the components of the vector $f$.1021110212\fun{GEN}{RgX_normalize}{GEN x} divides $x$ by its10213leading coefficient. If the latter is~$1$, $x$ itself is returned, not a10214copy. Leading coefficients equal to $0$ are stripped, e.g.10215\bprog102160.*t^3 + Mod(0,3)*t^2 + 2*t10217@eprog\noindent is normalized to $t$.1021810219\fun{GEN}{RgX_mul}{GEN x, GEN y} multiplies the two \typ{POL} (in the same10220variable) \kbd{x} and \kbd{y}. Detect the coefficient ring and use an10221appropriate algorithm.1022210223\fun{GEN}{RgX_mul_i}{GEN x, GEN y} multiplies the two \typ{POL} (in the same10224variable) \kbd{x} and \kbd{y}. Do not detect the coefficient ring.10225Use a generic Karatsuba algorithm.1022610227\fun{GEN}{RgX_mul_normalized}{GEN A, long a, GEN B, long b}10228returns $(X^a + A)(X^b + B) - X^(a+b)$, where we assume that $\deg A < a$10229and $\deg B < b$ are polynomials in the same variable $X$.1023010231\fun{GEN}{RgX_sqr}{GEN x} squares the \typ{POL} \kbd{x}. Detect the coefficient10232ring and use an appropriate algorithm.1023310234\fun{GEN}{RgX_sqr_i}{GEN x} squares the \typ{POL} \kbd{x}. Do not detect the10235coefficient ring. Use a generic Karatsuba algorithm.1023610237\fun{GEN}{RgX_divrem}{GEN x, GEN y, GEN *r} by default, returns the Euclidean10238quotient and store the remainder in $r$. Three special values of $r$ change10239that behavior10240\item \kbd{NULL}: do not store the remainder, used to implement \kbd{RgX\_div},1024110242\item \tet{ONLY_REM}: return the remainder, used to implement \kbd{RgX\_rem},1024310244\item \tet{ONLY_DIVIDES}: return the quotient if the division is exact, and10245\kbd{NULL} otherwise.1024610247\fun{GEN}{RgX_div}{GEN x, GEN y}1024810249\fun{GEN}{RgX_div_by_X_x}{GEN A, GEN a, GEN *r} returns the10250quotient of the \kbd{RgX}~\kbd{A} by $(X - \kbd{a})$, and sets \kbd{r} to the10251remainder $\kbd{A}(\kbd{a})$.1025210253\fun{GEN}{RgX_rem}{GEN x, GEN y}1025410255\fun{GEN}{RgX_pseudodivrem}{GEN x, GEN y, GEN *ptr} compute a pseudo-quotient10256$q$ and pseudo-remainder $r$ such that $\kbd{lc}(y)^{\deg(x) - \deg(y) + 1}x10257= qy + r$. Return $q$ and set \kbd{*ptr} to $r$.1025810259\fun{GEN}{RgX_pseudorem}{GEN x, GEN y} return the remainder10260in the pseudo-division of $x$ by $y$.1026110262\fun{GEN}{RgXQX_pseudorem}{GEN x, GEN y, GEN T} return the remainder10263in the pseudo-division of $x$ by $y$ over $R[X]/(T)$.1026410265\fun{int}{ZXQX_dvd}{GEN x, GEN y, GEN T} let $T$ be a monic irreducible10266\kbd{ZX}, let $x, y$ be \typ{POL} whose coefficients are either \typ{INT}s or10267\kbd{ZX} in the same variable as $T$. Assume further that the leading10268coefficient of $y$ is an integer. Return $1$ if $y | x$ in $(\Z[Y]/(T))[X]$,10269and $0$ otherwise.1027010271\fun{GEN}{RgXQX_pseudodivrem}{GEN x, GEN y, GEN T, GEN *ptr} compute10272a pseudo-quotient $q$ and pseudo-remainder $r$ such that10273$\kbd{lc}(y)^{\deg(x) - \deg(y) + 1}x = qy + r$ in $R[X]/(T)$. Return $q$ and10274set \kbd{*ptr} to $r$.1027510276\fun{GEN}{RgX_mulXn}{GEN a, long n} returns $a * X^n$. This may10277be a \typ{FRAC} if $n < 0$ and the valuation of $a$ is not large10278enough.1027910280\fun{GEN}{RgX_addmulXn}{GEN a, GEN b, long n} returns $a + b * X^n$, assuming10281that $n > 0$.1028210283\fun{GEN}{RgX_addmulXn_shallow}{GEN a, GEN b, long n} shallow10284variant of \tet{RgX_addmulXn}.1028510286\fun{GEN}{RgX_digits}{GEN x, GEN B} returns a vector of \kbd{RgX}10287$[c_0,\ldots,c_n]$ of degree less than the degree of $B$ and such that10288$x=\sum_{i=0}^{n}{c_i\*B^i}$.1028910290\subsubsec{Internal routines working on coefficient arrays}1029110292These routines operate on coefficient blocks which are invalid \kbd{GEN}s10293A \kbd{GEN} argument $a$ or $b$ in routines below is actually a coefficient10294arrays representing the polynomials10295$\sum_{i = 0}^{\kbd{na-1}} a[i] X^i$ and10296$\sum_{i = 0}^{\kbd{nb-1}} b[i] X^i$. Note that $a[0]$ and $b[0]$ contain10297coefficients and not the mandatory \kbd{GEN} codeword. This allows to implement10298divide-and-conquer methods directly, without needing to allocate wrappers10299around coefficient blocks.1030010301\fun{GEN}{RgX_mulspec}{GEN a, GEN b, long na, long nb}. Internal routine:10302given two coefficient arrays representing polynomials, return their product (as10303a true \kbd{GEN}) in variable $0$.1030410305\fun{GEN}{RgX_sqrspec}{GEN a, long na}. Internal routine:10306given a coefficient array representing a polynomial r eturn its square (as a10307true \kbd{GEN}) in variable $0$.1030810309\fun{GEN}{RgX_addspec}{GEN x, GEN y, long nx, long ny}10310given two coefficient arrays representing polynomials, return their sum (as a10311true \kbd{GEN}) in variable $0$.1031210313\fun{GEN}{RgX_addspec_shallow}{GEN x, GEN y, long nx, long ny} shallow10314variant of \tet{RgX_addspec}.1031510316\subsubsec{GCD, Resultant}1031710318\fun{GEN}{RgX_gcd}{GEN x, GEN y} returns the GCD of \kbd{x} and \kbd{y},10319assumed to be \typ{POL}s in the same variable.1032010321\fun{GEN}{RgX_gcd_simple}{GEN x, GEN y} as \tet{RgX_gcd} using a standard10322extended Euclidean algorithm. Usually slower than \tet{RgX_gcd}.1032310324\fun{GEN}{RgX_extgcd}{GEN x, GEN y, GEN *u, GEN *v} returns10325$d = \text{GCD}(\kbd{x},\kbd{y})$, and sets \kbd{*u}, \kbd{*v} to the Bezout10326coefficients such that $\kbd{*ux} + \kbd{*vy} = d$. Uses a generic10327subresultant algorithm.1032810329\fun{GEN}{RgX_extgcd_simple}{GEN x, GEN y, GEN *u, GEN *v} as10330\tet{RgX_extgcd} using a standard extended Euclidean algorithm. Usually10331slower than \tet{RgX_extgcd}.1033210333\fun{GEN}{RgX_halfgcd}{GEN x, GEN y}10334assuming \kbd{x} and \kbd{y} are \typ{POL}s in the same variable,10335returns a $2$-components \typ{VEC} $[M,V]$ where $M$ is a $2\times 2$10336\typ{MAT} and $V$ a $2$-component \typ{COL}, both with \typ{POL} entries,10337such that $M*[x,y]~==V$ and such that f $V=[a,b]~$, then10338$\deg a \geq \ceil{\max(\deg x,\deg y))/2} > \deg b$.1033910340\fun{GEN}{RgX_chinese_coprime}{GEN x, GEN y, GEN Tx, GEN Ty, GEN Tz}10341returns an \kbd{RgX}, congruent to \kbd{x} mod \kbd{Tx} and to \kbd{y} mod10342\kbd{Ty}. Assumes \kbd{Tx} and \kbd{Ty} are coprime, and \kbd{Tz = Tx * Ty}10343or \kbd{NULL} (in which case it is computed within).1034410345\fun{GEN}{RgX_disc}{GEN x} returns the discriminant of the \typ{POL} \kbd{x}10346with respect to its main variable.1034710348\fun{GEN}{RgX_resultant_all}{GEN x, GEN y, GEN *sol} returns10349\kbd{resultant(x,y)}. If \kbd{sol} is not \kbd{NULL}, sets it to the last10350nonconstant remainder in the polynomial remainder sequence if it exists and to10351\kbd{gen\_0} otherwise (e.g. one polynomial has degree 0).1035210353\subsubsec{Other operations}1035410355\fun{GEN}{RgX_gtofp}{GEN x, GEN prec} returns the polynomial obtained by10356applying10357\bprog10358gtofp(gel(x,i), prec)10359@eprog\noindent to all coefficients of $x$.1036010361\fun{GEN}{RgX_fpnorml2}{GEN x, long prec} returns (a stack-clean variant of)10362\bprog10363gnorml2( RgX_gtofp(x, prec) )10364@eprog1036510366\fun{GEN}{RgX_deriv}{GEN x} returns the derivative of \kbd{x} with respect to10367its main variable.1036810369\fun{GEN}{RgX_integ}{GEN x} returns the primitive of \kbd{x} vanishing at10370$0$, with respect to its main variable.1037110372\fun{GEN}{RgX_rescale}{GEN P, GEN h} returns $h^{\deg(P)} P(x/h)$.10373\kbd{P} is an \kbd{RgX} and \kbd{h} is nonzero. (Leaves small objects on the10374stack. Suitable but inefficient for \kbd{gerepileupto}.)1037510376\fun{GEN}{RgX_unscale}{GEN P, GEN h} returns $P(h x)$. (Leaves small objects10377on the stack. Suitable but inefficient for \kbd{gerepileupto}.)1037810379\fun{GEN}{RgXV_unscale}{GEN v, GEN h} apply \kbd{RgX\_unscale} to a vector10380of \kbd{RgX}.1038110382\fun{GEN}{RgX_translate}{GEN P, GEN c} assume $c$ is a scalar or10383a polynomials whose main variable has lower priority than the main variable10384$X$ of $P$. Returns $P(X + c)$ (optimized for $c = \pm 1$).1038510386\subsubsec{Function related to modular forms}1038710388\fun{GEN}{RgX_act_Gl2Q}{GEN g, long k} let $R$ be a commutative ring10389and $g = [a,b;c,d]$ be in $\text{GL}_2(\Q)$, $g$ acts (on the left)10390on homogeneous polynomials of degree $k-2$ in $V := R[X,Y]_{k-2}$ via10391$$ g\cdot P := P(dX-cY, -bX+aY) = (\det g)^{k-2} P((X,Y)\cdot g^{-1}).$$10392This function returns the matrix in $M_{k-1}(R)$ of $P\mapsto g\cdot P$ in10393the basis $(X^{k-2},\dots,Y^{k-2})$ of $V$.1039410395\fun{GEN}{RgX_act_ZGl2Q}{GEN z, long k} let $G:=\text{GL}_2(\Q)$, acting10396on $R[X,Y]_{k-2}$ and $z\in \Z[G]$. Return the matrix giving10397$P\mapsto z\cdot P$ in the basis $(X^{k-2},\dots,Y^{k-2})$.1039810399\subsec{\kbd{RgXn}}1040010401\fun{GEN}{RgXn_red_shallow}{GEN x, long n} return $\kbd{x \% } t^n$,10402where $n\geq 0$. Shallow function.1040310404\fun{GEN}{RgXn_recip_shallow}{GEN P} returns $X^n\*P(1/X)$. Shallow function.1040510406\fun{GEN}{RgXn_mul}{GEN a, GEN b, long n} returns $a b$ modulo $X^n$,10407where $a,b$ are two \typ{POL} in the same variable $X$ and $n \geq 0$. Uses10408Karatsuba algorithm (Mulders, Hanrot-Zimmermann variant).1040910410\fun{GEN}{RgXn_sqr}{GEN a, long n} returns $a^2$ modulo $X^n$,10411where $a$ is a \typ{POL} in the variable $X$ and $n \geq 0$. Uses10412Karatsuba algorithm (Mulders, Hanrot-Zimmermann variant).1041310414\fun{GEN}{RgX_mulhigh_i}{GEN f, GEN g, long n} return the Euclidean quotient10415of $f(x)*g(x)$ by $x^n$ (high product). Uses \tet{RgXn_mul} applied to10416the reciprocal polynomials of $f$ and $g$. Not suitable for \kbd{gerepile}.1041710418\fun{GEN}{RgX_sqrhigh_i}{GEN f, long n} return the Euclidean quotient10419of $f(x)^2$ by $x^n$ (high product). Uses \tet{RgXn_sqr} applied to10420the reciprocal polynomial of $f$. Not suitable for \kbd{gerepile}.1042110422\fun{GEN}{RgXn_inv}{GEN a, long n} returns $a^{-1}$ modulo $X^n$,10423where $a$ is a \typ{POL} in the variable $X$ and $n \geq 0$. Uses10424Newton-Raphson algorithm.1042510426\fun{GEN}{RgXn_inv_i}{GEN a, long n} as \tet{RgXn_inv} without final garbage10427collection (suitable for \kbd{gerepileupto}).1042810429\fun{GEN}{RgXn_powers}{GEN x, long m, long n} returns $[\kbd{x}^0,10430\dots, \kbd{x}^\kbd{m}]$ modulo $X^n$ as a \typ{VEC} of \kbd{RgXn}s.1043110432\fun{GEN}{RgXn_powu}{GEN x, ulong m, long n} returns $x^m$ modulo10433$X^n$.1043410435\fun{GEN}{RgXn_powu_i}{GEN x, ulong m, long n} as \tet{RgXn_powu},10436not memory clean.1043710438\fun{GEN}{RgXn_sqrt}{GEN a, long n} returns $a^{1/2}$ modulo $X^n$,10439where $a$ is a \typ{POL} in the variable $X$ and $n \geq 0$.10440Assume that $a = 1 \mod{X}$. Uses Newton algorithm.1044110442\fun{GEN}{RgXn_exp}{GEN a, long n} returns $exp(a)$ modulo $X^n$, assuming10443$a = 0 \mod{X}$.1044410445\fun{GEN}{RgXn_expint}{GEN f, long n} return $\exp(F)$10446where $F$ is the primitive of $f$ that vanishes at $0$.1044710448\fun{GEN}{RgXn_eval}{GEN Q, GEN x, long n} special case of10449\tet{RgX_RgXQ_eval}, when the modulus is a monomial:10450returns $\kbd{Q}(\kbd{x})$ modulo $t^n$, where $x \in R[t]$.1045110452\fun{GEN}{RgX_RgXn_eval}{GEN f, GEN x, long n} returns $\kbd{f}(\kbd{x})$ modulo10453$X^n$.1045410455\fun{GEN}{RgX_RgXnV_eval}{GEN f, GEN V, long n} as \kbd{RgX\_RgXn\_eval(f, x, n)},10456assuming $V$ was output by \kbd{RgXn\_powers(x, m, n)} for some $m\geq 1$.1045710458\fun{GEN}{RgXn_reverse}{GEN f, long n} assuming that $f = a\*x \mod{x^2}$10459with $a$ invertible, returns a \typ{POL} $g$ of degree $< n$ such that $(g10460\circ f)(x) = x$ modulo $x^n$.1046110462\subsec{\kbd{RgXnV}}1046310464\fun{GEN}{RgXnV_red_shallow}{GEN x, long n} apply \kbd{RgXn\_red\_shallow}10465to all the components of the vector $x$.1046610467\subsec{\kbd{RgXQ}}1046810469\fun{GEN}{RgXQ_mul}{GEN y, GEN x, GEN T} computes $xy$ mod $T$1047010471\fun{GEN}{RgXQ_sqr}{GEN x, GEN T} computes $x^2$ mod $T$1047210473\fun{GEN}{RgXQ_inv}{GEN x, GEN T} return the inverse of $x$ mod $T$.1047410475\fun{GEN}{RgXQ_pow}{GEN x, GEN n, GEN T} computes $x^n$ mod $T$1047610477\fun{GEN}{RgXQ_powu}{GEN x, ulong n, GEN T} computes $x^n$ mod $T$,10478$n$ being an \kbd{ulong}.1047910480\fun{GEN}{RgXQ_powers}{GEN x, long n, GEN T} returns $[\kbd{x}^0,10481\dots, \kbd{x}^\kbd{n}]$ as a \typ{VEC} of \kbd{RgXQ}s.1048210483\fun{GEN}{RgXQ_matrix_pow}{GEN y, long n, long m, GEN P} returns10484\kbd{RgXQ\_powers(y,m-1,P)}, as a matrix of dimension $n \geq \deg P$.1048510486\fun{GEN}{RgXQ_norm}{GEN x, GEN T} returns the norm of \kbd{Mod(x, T)}.1048710488\fun{GEN}{RgXQ_trace}{GEN x, GEN T} returns the trace of \kbd{Mod(x, T)}.1048910490\fun{GEN}{RgXQ_charpoly}{GEN x, GEN T, long v} returns the characteristic10491polynomial of \kbd{Mod(x, T)}, in variable $v$.1049210493\fun{GEN}{RgX_RgXQ_eval}{GEN f, GEN x, GEN T} returns $\kbd{f}(\kbd{x})$ modulo10494$T$.1049510496\fun{GEN}{RgX_RgXQV_eval}{GEN f, GEN V, GEN T} as \kbd{RgX\_RgXQ\_eval(f, x, T)},10497assuming $V$ was output by \kbd{RgXQ\_powers(x, n, T)} for some $n\geq 1$.1049810499\fun{int}{RgXQ_ratlift}{GEN x, GEN T, long amax, long bmax, GEN *P, GEN *Q}10500Assuming that $\kbd{amax}+\kbd{bmax}<\deg T$, attempts to recognize $x$ as a10501rational function $a/b$, i.e. to find \typ{POL}s $P$ and $Q$ such that1050210503\item $P \equiv Q x$ modulo $T$,1050410505\item $\deg P \leq \kbd{amax}$, $\deg Q \leq \kbd{bmax}$,1050610507\item $\gcd(T,P) = \gcd(P,Q)$.1050810509\noindent If unsuccessful, the routine returns $0$ and leaves $P$, $Q$10510unchanged; otherwise it returns $1$ and sets $P$ and $Q$.1051110512\fun{GEN}{RgXQ_reverse}{GEN f, GEN T} returns a \typ{POL} $g$ of degree $< n10513= \text{deg}~T$ such that $T(x)$ divides $(g \circ f)(x) - x$, by solving a10514linear system. Low-level function underlying \tet{modreverse}: it returns a10515lift of \kbd[modreverse(f,T)]; faster than the high-level function since it10516needs not compute the characteristic polynomial of $f$ mod $T$ (often already10517known in applications). In the trivial case where $n \leq 1$, returns a10518scalar, not a constant \typ{POL}.1051910520\subsec{\kbd{RgXQV, RgXQC}}1052110522\fun{GEN}{RgXQC_red}{GEN z, GEN T} \kbd{z} a vector whose10523coefficients are \kbd{RgX}s (arbitrary \kbd{GEN}s in fact), reduce them to10524\kbd{RgXQ}s (applying \kbd{grem} coefficientwise) in a \typ{COL}.1052510526\fun{GEN}{RgXQV_red}{GEN z, GEN T} \kbd{z} a vector whose10527coefficients are \kbd{RgX}s (arbitrary \kbd{GEN}s in fact), reduce them to10528\kbd{RgXQ}s (applying \kbd{grem} coefficientwise) in a \typ{VEC}.1052910530\fun{GEN}{RgXQV_RgXQ_mul}{GEN z, GEN x, GEN T} \kbd{z} multiplies the10531\kbd{RgXQV} \kbd{z} by the scalar (\kbd{RgXQ}) \kbd{x}.1053210533\fun{GEN}{RgXQV_factorback}{GEN L, GEN e, GEN T} returns10534$\prod_i L_i^{e_i}$ mod $T$ where $L$ is a vector of \kbd{RgXQ}s and10535$e$ a vector of \typ{INT}s.1053610537\subsec{\kbd{RgXQM}}1053810539\fun{GEN}{RgXQM_red}{GEN z, GEN T} \kbd{z} a matrix whose10540coefficients are \kbd{RgX}s (arbitrary \kbd{GEN}s in fact), reduce them to10541\kbd{RgXQ}s (applying \kbd{grem} coefficientwise).1054210543\fun{GEN}{RgXQM_mul}{GEN x, GEN y, GEN T}1054410545\subsec{\kbd{RgXQX}}1054610547\fun{GEN}{RgXQX_red}{GEN z, GEN T} \kbd{z} a \typ{POL} whose10548coefficients are \kbd{RgX}s (arbitrary \kbd{GEN}s in fact), reduce them to10549\kbd{RgXQ}s (applying \kbd{grem} coefficientwise).1055010551\fun{GEN}{RgXQX_mul}{GEN x, GEN y, GEN T}1055210553\fun{GEN}{RgXQX_RgXQ_mul}{GEN x, GEN y, GEN T} multiplies the \kbd{RgXQX}10554\kbd{y} by the scalar (\kbd{RgXQ}) \kbd{x}.1055510556\fun{GEN}{RgXQX_sqr}{GEN x, GEN T}1055710558\fun{GEN}{RgXQX_powers}{GEN x, long n, GEN T}1055910560\fun{GEN}{RgXQX_divrem}{GEN x, GEN y, GEN T, GEN *pr}1056110562\fun{GEN}{RgXQX_div}{GEN x, GEN y, GEN T, GEN *r}1056310564\fun{GEN}{RgXQX_rem}{GEN x, GEN y, GEN T, GEN *r}1056510566\fun{GEN}{RgXQX_translate}{GEN P, GEN c, GEN T} assume the main variable10567$X$ of $P$ has higher priority than the main variable $Y$ of $T$ and $c$.10568Return a lift of $P(X+\text{Mod}(c(Y), T(Y)))$.1056910570\fun{GEN}{Kronecker_to_mod}{GEN z, GEN T} $z\in R[X]$ represents an element10571$P(X,Y)$ in $R[X,Y]$ mod $T(Y)$ in Kronecker form, i.e. $z = P(X,X^{2*n-1})$1057210573Let $R$ be some commutative ring, $n = \deg T$ and let $P(X,Y)\in R[X,Y]$ lift10574a polynomial in $K[Y]$, where $K := R[X]/(T)$ and $\deg_X P < 2n-1$ --- such as10575would result from multiplying minimal degree lifts of two polynomials in10576$K[Y]$. Let $z = P(t,t^{2*n-1})$ be a Kronecker form of $P$, this function10577returns the image of $P(X,t)$ in $K[t]$, with \typ{POLMOD} coefficients.10578Not stack-clean. Note that $t$ need not be the same variable as $Y$!1057910580\chapter{Black box algebraic structures}1058110582The generic routines like \kbd{gmul} or \kbd{gadd} allow handling objects10583belonging to a fixed list of basic types, with some natural polymorphism10584(you can mix rational numbers and polynomials, etc.), at the expense of10585efficiency and sometimes of clarity when the recursive structure becomes10586complicated, e.g. a few levels of \typ{POLMOD}s attached to different10587polynomials and variable numbers for quotient structures. This10588is the only possibility in GP.1058910590On the other hand, the Level 2 Kernel allows dedicated routines to handle10591efficiently objects of a very specific type, e.g. polynomials with10592coefficients in the same finite field. This is more efficient, but imvolves a10593lot of code duplication since polymorphism is no longer possible.1059410595A third and final option, still restricted to library programming, is to10596define an arbitrary algebraic structure (currently groups, fields, rings,10597algebras and $\Z_p$-modules) by providing suitable methods, then using generic10598algorithms. For instance naive Gaussian pivoting applies over all base fields10599and need only be implemented once. The difference with the first solution10600is that we no longer depend on the way functions like \kbd{gmul} or10601\kbd{gadd} will guess what the user is trying to do. We can then implement10602independently various groups / fields / algebras in a clean way.1060310604\section{Black box groups}1060510606A black box group is defined by a \tet{bb_group} struct, describing methods10607available to handle group elements:10608\bprog10609struct bb_group10610{10611GEN (*mul)(void*, GEN, GEN);10612GEN (*pow)(void*, GEN, GEN);10613GEN (*rand)(void*);10614ulong (*hash)(GEN);10615int (*equal)(GEN, GEN);10616int (*equal1)(GEN);10617GEN (*easylog)(void *E, GEN, GEN, GEN);10618};10619@eprog10620\kbd{mul(E,x,y)} returns the product $x\*y$.1062110622\kbd{pow(E,x,n)} returns $x^n$ ($n$ integer, possibly negative or zero).1062310624\kbd{rand(E)} returns a random element in the group.1062510626\kbd{hash(x)} returns a hash value for $x$ (\kbd{hash\_GEN} is suitable for this field).1062710628\kbd{equal(x,y)} returns one if $x=y$ and zero otherwise.1062910630\kbd{equal1(x)} returns one if $x$ is the neutral element in the group,10631and zero otherwise.1063210633\kbd{easylog(E,a,g,o)} (optional) returns either NULL or the discrete logarithm10634$n$ such that $g^n=a$, the element $g$ being of order $o$. This provides a10635short-cut in situation where a better algorithm than the generic one is known.1063610637A group is thus described by a \kbd{struct bb\_group} as above and auxiliary10638data typecast to \kbd{void*}. The following functions operate on black box10639groups:1064010641\fun{GEN}{gen_Shanks_log}{GEN x, GEN g, GEN N, void *E, const struct bb_group10642*grp} \hbadness 10000\break10643Generic baby-step/giant-step algorithm (Shanks's method). Assuming10644that $g$ has order $N$, compute an integer $k$ such that $g^k = x$.10645Return \kbd{cgetg(1, t\_VEC)} if there are no solutions. This requires10646$O(\sqrt{N})$ group operations and uses an auxiliary table containing10647$O(\sqrt{N})$ group elements.1064810649The above is useful for a one-shot computation. If many discrete logs10650are desired:10651\fun{GEN}{gen_Shanks_init}{GEN g, long n, void *E, const struct bb_group *grp}10652return an auxiliary data structure $T$ required to compute a discrete log in10653base $g$. Compute and store all powers $g^i$, $i < n$.1065410655\fun{GEN}{gen_Shanks}{GEN T, GEN x, ulong N, void *E, const struct bb_group *grp}10656Let $T$ be computed by \tet{gen_Shanks_init}$(g,n,\dots)$.10657Return $k < n N$ such that $g^k = x$ or \kbd{NULL} if no such index exist.10658It uses $O(N)$ operation in the group and fast table lookups (in time10659$O(\log n)$). The interface is such that the function may be used when the10660order of the base $g$ is unknown, and hence compute it given only an upper10661bound $B$ for it: e.g. choose $n,N$ such that $nN \geq B$ and compute the10662discrete log $l$ of $g^{-1}$ in base $g$, then use \tet{gen_order}10663with multiple $N = l+1$.1066410665\fun{GEN}{gen_Pollard_log}{GEN x, GEN g, GEN N, void *E, const struct bb_group10666*grp} \hbadness 10000\break10667Generic Pollard rho algorithm. Assuming that $g$ has order $N$, compute an10668integer $k$ such that $g^k = x$. This requires $O(\sqrt{N})$ group operations10669in average and $O(1)$ storage. Will enter an infinite loop if there are no10670solutions.1067110672\fun{GEN}{gen_plog}{GEN x, GEN g, GEN N, void *E, const struct bb_group}10673Assuming that $g$ has prime order $N$, compute an integer $k$ such that10674$g^k = x$, using either \kbd{gen\_Shanks\_log} or \kbd{gen\_Pollard\_log}.10675Return \kbd{cgetg(1, t\_VEC)} if there are no solutions.1067610677\fun{GEN}{gen_Shanks_sqrtn}{GEN a, GEN n, GEN N, GEN *zetan, void *E, const10678struct bb_group *grp} \hbadness 10000 returns one solution of $x^n = a$ in a10679black box cyclic group of order $N$. Return \kbd{NULL} if no solution exists.10680If \kbd{zetan} is not \kbd{NULL} it is set to an element of exact order $n$.10681This function uses \kbd{gen\_plog} for all prime divisors of $\gcd(n,N)$.1068210683\fun{GEN}{gen_PH_log}{GEN a, GEN g, GEN N, void *E, const struct bb_group *grp}10684returns an integer $k$ such that $g^k = x$, assuming that the order of $g$10685divides $N$, using Pohlig-Hellman algorithm. Return \kbd{cgetg(1, t\_VEC)} if10686there are no solutions. This calls \tet{gen_plog} repeatedly for all prime10687divisors $p$ of $N$.1068810689In the following functions the integer parameter \kbd{ord} can be given10690in all the formats recognized for the argument of arithmetic functions,10691i.e.~either as a positive \typ{INT} $N$, or as its factorization matrix10692$\var{faN}$, or (preferred) as a pair $[N,\var{faN}]$.1069310694\fun{GEN}{gen_order}{GEN x, GEN ord, void *E, const struct bb_group *grp}10695computes the order of $x$; \kbd{ord} is a multiple of the order, for instance10696the group order.1069710698\fun{GEN}{gen_factored_order}{GEN x, GEN ord, void *E, const struct bb_group10699*grp} returns a pair $[o,F]$, where $o$ is the order of $x$ and $F$ is the10700factorization of $o$; \kbd{ord} is as in \tet{gen_order}.1070110702\fun{GEN}{gen_gener}{GEN ord, void *E, const struct bb_group *grp}10703returns a random generator of the group, assuming it is of order exactly10704\kbd{ord}.1070510706\fun{GEN}{get_arith_Z}{GEN ord} given \kbd{ord} as above in one of the10707formats recognized for arithmetic functions, i.e. a positive10708\typ{INT} $N$, its factorization \var{faN}, or the pair $[N, \var{faN}]$,10709return $N$.1071010711\fun{GEN}{get_arith_ZZM}{GEN ord} given \kbd{ord} as above,10712return the pair $[N, \var{faN}]$. This may require factoring $N$.1071310714\fun{GEN}{gen_select_order}{GEN v, void *E, const struct bb_group *grp}10715Let $v$ be a vector of possible orders for the group; try to find the true10716order by checking orders of random points. This will not terminate if there10717is an ambiguity.1071810719\subsec{Black box groups with pairing}1072010721These functions handle groups of rank at most $2$ equipped with a family of10722bilinear pairings which behave like the Weil pairing on elliptic curves over10723finite field. In the descriptions below, the function \kbd{pairorder(E, P, Q,10724m, F)} must return the order of the $m$-pairing of $P$ and $Q$, both of order10725dividing $m$, where $F$ is the factorization matrix of a multiple of $m$.1072610727\fun{GEN}{gen_ellgroup}{GEN o, GEN d, GEN *pt_m, void *E, const struct bb_group *grp,10728GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F)}10729returns the elementary divisors $[d_1, d_2]$ of the group, assuming it is of10730order exactly $o>1$, and that $d_2$ divides $d$. If $d_2=1$ then $[o]$ is10731returned, otherwise \kbd{m=*pt\_m} is set to the order of the pairing10732required to verify a generating set which is to be used with10733\kbd{gen\_ellgens}. For the parameter $o$, all formats recognized by10734arithmetic functions are allowed, preferably a factorization matrix or a pair10735$[n,\kbd{factor}(n)]$.1073610737\fun{GEN}{gen_ellgens}{GEN d1, GEN d2, GEN m, void *E, const struct bb_group *grp,10738GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F)}10739the parameters $d_1$, $d_2$, $m$ being as returned by \kbd{gen\_ellgroup},10740returns a pair of generators $[P,Q]$ such that $P$ is of order $d_1$ and the10741$m$-pairing of $P$ and $Q$ is of order $m$. (Note: $Q$ needs not be of order10742$d_2$). For the parameter $d_1$, all formats recognized by arithmetic10743functions are allowed, preferably a factorization matrix or a pair10744$[n,\kbd{factor}(n)]$.1074510746\subsec{Functions returning black box groups}1074710748\fun{const struct bb_group *}{get_Flxq_star}{void **E, GEN T, ulong p}1074910750\fun{const struct bb_group *}{get_FpXQ_star}{void **E, GEN T, GEN p}10751returns a pointer to the black box group $(\F_p[x]/(T))^*$.1075210753\fun{const struct bb_group *}{get_FpE_group}{void **pE, GEN a4, GEN a6, GEN p}10754returns a pointer to a black box group and set \kbd{*pE} to the necessary data for10755computing in the group $E(\F_p)$ where $E$ is the elliptic curve $E:y^2=x^3+a_4\*x+a_6$,10756with $a_4$ and $a_6$ in $\F_p$.1075710758\fun{const struct bb_group *}{get_FpXQE_group}{void **pE, GEN a4, GEN a6, GEN T, GEN p}10759returns a pointer to a black box group and set \kbd{*pE} to the necessary data for10760computing in the group $E(\F_p[X]/(T))$ where $E$ is the elliptic curve $E:y^2=x^3+a_4\*x+a_6$,10761with $a_4$ and $a_6$ in $\F_p[X]/(T)$.1076210763\fun{const struct bb_group *}{get_FlxqE_group}{void **pE, GEN a4, GEN a6, GEN10764T, ulong p} idem for small $p$.1076510766\fun{const struct bb_group *}{get_F2xqE_group}{void **pE, GEN a2, GEN a6, GEN T}10767idem for $p=2$.1076810769\section{Black box fields}1077010771A black box field is defined by a \tet{bb_field} struct, describing methods10772available to handle field elements:10773\bprog10774struct bb_field10775{10776GEN (*red)(void *E ,GEN);10777GEN (*add)(void *E ,GEN, GEN);10778GEN (*mul)(void *E ,GEN, GEN);10779GEN (*neg)(void *E ,GEN);10780GEN (*inv)(void *E ,GEN);10781int (*equal0)(GEN);10782GEN (*s)(void *E, long);10783};10784@eprog\noindent In contrast of black box group, elements can have10785non canonical forms, and only \kbd{red} is required to return a canonical form.10786For instance a black box implementation of finite fields, all methods10787except \kbd{red} may return arbitrary representatives in $\Z[X]$ of the10788correct congruence class modulo $(p,T(X))$.1078910790\kbd{red(E,x)} returns the canonical form of $x$.1079110792\kbd{add(E,x,y)} returns the sum $x+y$.1079310794\kbd{mul(E,x,y)} returns the product $x\*y$.1079510796\kbd{neg(E,x)} returns $-x$.1079710798\kbd{inv(E,x)} returns the inverse of $x$.1079910800\kbd{equal0(x)} $x$ being in canonical form, returns one if $x=0$ and zero10801otherwise.1080210803\kbd{s(n)} $n$ being a small signed integer, returns $n$ times the unit element.1080410805\noindent A field is thus described by a \kbd{struct bb\_field} as above and10806auxiliary data typecast to \kbd{void*}. The following functions operate on10807black box fields:1080810809\fun{GEN}{gen_Gauss}{GEN a, GEN b, void *E, const struct bb_field *ff}1081010811\fun{GEN}{gen_Gauss_pivot}{GEN x, long *rr, void *E, const struct bb_field *ff}1081210813\fun{GEN}{gen_det}{GEN a, void *E, const struct bb_field *ff}1081410815\fun{GEN}{gen_ker}{GEN x, long deplin, void *E, const struct bb_field *ff}1081610817\fun{GEN}{gen_matcolinvimage}{GEN a, GEN b, void *E, const struct bb_field *ff}1081810819\fun{GEN}{gen_matcolmul}{GEN a, GEN b, void *E, const struct bb_field *ff}1082010821\fun{GEN}{gen_matid}{long n, void *E, const struct bb_field *ff}1082210823\fun{GEN}{gen_matinvimage}{GEN a, GEN b, void *E, const struct bb_field *ff}1082410825\fun{GEN}{gen_matmul}{GEN a, GEN b, void *E, const struct bb_field *ff}1082610827\subsec{Functions returning black box fields}1082810829\fun{const struct bb_field *}{get_Fp_field}{void **pE, GEN p}1083010831\fun{const struct bb_field *}{get_Fq_field}{void **pE, GEN T, GEN p}1083210833\fun{const struct bb_field *}{get_Flxq_field}{void **pE, GEN T, ulong p}1083410835\fun{const struct bb_field *}{get_F2xq_field}{void **pE, GEN T}1083610837\fun{const struct bb_field *}{get_nf_field}{void **pE, GEN nf}1083810839\section{Black box algebra}1084010841A black box algebra is defined by a \tet{bb_algebra} struct, describing methods10842available to handle algebra elements:10843\bprog10844struct bb_algebra10845{10846GEN (*red)(void *E, GEN x);10847GEN (*add)(void *E, GEN x, GEN y);10848GEN (*sub)(void *E, GEN x, GEN y);10849GEN (*mul)(void *E, GEN x, GEN y);10850GEN (*sqr)(void *E, GEN x);10851GEN (*one)(void *E);10852GEN (*zero)(void *E);10853};10854@eprog\noindent In contrast with black box groups, elements can have non10855canonical forms, but only \kbd{add} is allowed to return a non canonical10856form.1085710858\kbd{red(E,x)} returns the canonical form of $x$.1085910860\kbd{add(E,x,y)} returns the sum $x+y$.1086110862\kbd{sub(E,x,y)} returns the difference $x-y$.1086310864\kbd{mul(E,x,y)} returns the product $x\*y$.1086510866\kbd{sqr(E,x)} returns the square $x^2$.1086710868\kbd{one(E)} returns the unit element.1086910870\kbd{zero(E)} returns the zero element.1087110872\noindent An algebra is thus described by a \kbd{struct bb\_algebra} as above10873and auxiliary data typecast to \kbd{void*}. The following functions operate10874on black box algebra:1087510876\fun{GEN}{gen_bkeval}{GEN P, long d, GEN x, int use_sqr, void *E,10877const struct bb_algebra *ff, GEN cmul(void *E, GEN P, long a, GEN x)}10878$x$ being an element of the black box algebra, and $P$ some black box10879polynomial of degree $d$ over the base field, returns $P(x)$. The function10880\kbd{cmul(E,P,a,y)} must return the coefficient of degree $a$ of $P$10881multiplied by $y$. \kbd{cmul} is allowed to return a non canonical form;10882it is also allowed to return \kbd{NULL} instead of an exact $0$.1088310884The flag \kbd{use\_sqr} has the same meaning as for \kbd{gen\_powers}. This10885implements an algorithm of Brent and Kung (1978).1088610887\fun{GEN}{gen_bkeval_powers}{GEN P, long d, GEN V, void *E,10888const struct bb_algebra *ff, GEN cmul(void *E, GEN P, long a, GEN x)}10889as \tet{gen_RgX_bkeval} assuming $V$ was output by10890\tet{gen_powers}$(x, l, E, \var{ff})$ for some $l\geq 1$. For optimal10891performance, $l$ should be computed by \tet{brent_kung_optpow}.1089210893\fun{long}{brent_kung_optpow}{long d, long n, long m} returns the optimal10894parameter $l$ for the evaluation of $n/m$ polynomials of degree $d$.10895Fractional values can be used if the evaluations are done with different10896accuracies, and thus have different weights.1089710898\subsec{Functions returning black box algebras}1089910900\fun{const struct bb_algebra *}{get_FpX_algebra}{void **E, GEN p, long v}10901return the algebra of polynomials over $\F_p$ in variable $v$.1090210903\fun{const struct bb_algebra *}{get_FpXQ_algebra}{void **E, GEN T, GEN p}10904return the algebra $\F_p[X]/(T(X))$.1090510906\fun{const struct bb_algebra *}{get_FpXQX_algebra}{void **E, GEN T, GEN p, long v}10907return the algebra of polynomials over $\F_p[X]/(T(X))$ in variable $v$.1090810909\fun{const struct bb_algebra *}{get_FlxqXQ_algebra}{void **E, GEN S, GEN T, ulong p}10910return the algebra $\F_p[X,Y]/(S(X,Y),T(X))$ (for \kbd{ulong} $p$).1091110912\fun{const struct bb_algebra *}{get_FpXQXQ_algebra}{void **E, GEN S, GEN T, GEN p}10913return the algebra $\F_p[X,Y]/(S(X,Y),T(X))$.1091410915\fun{const struct bb_algebra *}{get_Rg_algebra}{void}10916return the generic algebra.1091710918\section{Black box ring}1091910920A black box ring is defined by a \tet{bb_ring} struct, describing methods10921available to handle ring elements:10922\bprog10923struct bb_ring10924{10925GEN (*add)(void *E, GEN x, GEN y);10926GEN (*mul)(void *E, GEN x, GEN y);10927GEN (*sqr)(void *E, GEN x);10928};10929@eprog1093010931\kbd{add(E,x,y)} returns the sum $x+y$.1093210933\kbd{mul(E,x,y)} returns the product $x\*y$.1093410935\kbd{sqr(E,x)} returns the square $x^2$.1093610937\fun{GEN}{gen_fromdigits}{GEN v, GEN B, void *E, struct bb_ring *r}10938where $B$ is a ring element and $v=[c_0,\ldots,c_{n-1}]$ a vector of ring elements,10939return $\sum_{i=0}^n c_i\*B^i$ using binary splitting.1094010941\fun{GEN}{gen_digits}{GEN x, GEN B, long n, void *E, struct bb_ring *r,10942GEN (*div)(void *E, GEN x, GEN y, GEN *r)}1094310944(Require the ring to be Euclidean)1094510946\kbd{div(E,x,y,\&r)} performs the Euclidean division of $x$ by $y$ in the ring10947$R$, returning the quotient $q$ and setting $r$ to the residue so that10948$x=q\*y+r$ holds. The residue must belong to a fixed set of representatives of10949$R/(y)$.1095010951The argument $x$ being a ring element, \kbd{gen\_digits} returns a vector of10952ring elements $[c_0,\ldots,c_{n-1}]$ such that $x = \sum_{i=0}^n c_i\*B^i$.10953Furthermore for all $i\ne n-1$, the elements $c_i$ belonging to the fixed set10954of representatives of $R/(B)$.1095510956\section{Black box free $\Z_p$-modules}1095710958(Very experimental)1095910960\fun{GEN}{gen_ZpX_Dixon}{GEN F, GEN V, GEN q, GEN p, long N, void *E,10961GEN lin(void *E, GEN F, GEN z, GEN q),10962GEN invl(void *E, GEN z)}1096310964Let $F$ be a \kbd{ZpXT} representing the coefficients of some abstract10965linear mapping $f$ over $\Z_p[X]$ seen as a free $\Z_p$-module, let $V$ be10966an element of $\Z_p[X]$ and let $q = p^N$. Return $y\in\Z_p[X]$ such that10967$f(y)=V\pmod{p^N}$ assuming the following holds for $n\leq N$:1096810969\item $\kbd{lin}(E, \kbd{FpX\_red}(F, p^n), z, p^n) \equiv f(z) \pmod{p^n}$1097010971\item $f(\kbd{invl}(E, z)) \equiv z \pmod{p}$1097210973The rationale for the argument $F$ being that it allows \kbd{gen\_ZpX\_Dixon}10974to reduce it to the required $p$-adic precision.1097510976\fun{GEN}{gen_ZpX_Newton}{GEN x, GEN p, long n, void *E,10977GEN eval(void *E, GEN a, GEN q),10978GEN invd(void *E, GEN b, GEN v, GEN q, long N)}1097910980Let $x$ be an element of $\Z_p[X]$ seen as a free $\Z_p$-module, and $f$10981some differentiable function over $\Z_p[X]$ such that $f(x) \equiv 010982\pmod{p}$. Return $y$ such that $f(y) \equiv 0\pmod{p^n}$, assuming the10983following holds for all $a, b\in \Z_p[X]$ and $M\leq N$:1098410985\item $v = \kbd{eval}(E,a,p^N)$ is a vector of elements of $\Z_p[X]$,1098610987\item $w = \kbd{invd}(E,b,v,p^M,M)$ is an element in $\Z_p[X]$,1098810989\item $v[1] \equiv f(a) \pmod{p^N\Z_p[X]}$,1099010991\item $df_a(w) \equiv b \pmod{p^M\Z_p[X]}$1099210993\noindent and $df_a$ denotes the differential of $f$ at $a$. Motivation:10994\kbd{eval} allows to evaluate $f$ and \kbd{invd} allows to invert its10995differential. Frequently, data useful to compute the differential appear as a10996subproduct of computing the function. The vector $v$ allows \kbd{eval} to10997provide these to \kbd{invd}. The implementation of \kbd{invd} will generally10998involves the use of the function \kbd{gen\_ZpX\_Dixon}.1099911000\fun{GEN}{gen_ZpM_Newton}{GEN x, GEN p, long n, void *E,11001GEN eval(void *E, GEN a, GEN q),11002GEN invd(void *E, GEN b, GEN v, GEN q, long N)}11003as above, with polynomials replaced by matrices.1100411005\newpage11006\chapter{Operations on general PARI objects}1100711008\section{Assignment}1100911010It is in general easier to use a direct conversion,11011e.g.~\kbd{y = stoi(s)}, than to allocate a target of correct type and11012sufficient size, then assign to it:11013\bprog11014GEN y = cgeti(3); affsi(s, y);11015@eprog\noindent11016These functions can still be moderately useful in complicated garbage11017collecting scenarios but you will be better off not using them.1101811019\fun{void}{gaffsg}{long s, GEN x} assigns the \kbd{long}~\kbd{s} into the11020object~\kbd{x}.1102111022\fun{void}{gaffect}{GEN x, GEN y} assigns the object \kbd{x} into the11023object~\kbd{y}. Both \kbd{x} and \kbd{y} must be scalar types. Type11024conversions (e.g.~from \typ{INT} to \typ{REAL} or \typ{INTMOD}) occur if11025legitimate.1102611027\fun{int}{is_universal_constant}{GEN x} returns $1$ if $x$ is a global PARI11028constant you should never assign to (such as \kbd{gen\_1}), and $0$11029otherwise.1103011031\section{Conversions}1103211033\subsec{Scalars}1103411035\fun{double}{rtodbl}{GEN x} applied to a \typ{REAL}~\kbd{x}, converts \kbd{x}11036into a \kbd{double} if possible.1103711038\fun{GEN}{dbltor}{double x} converts the \kbd{double} \kbd{x} into a11039\typ{REAL}.1104011041\fun{long}{dblexpo}{double x} returns \kbd{expo(dbltor(x))}, but11042faster and without cluttering the stack.1104311044\fun{ulong}{dblmantissa}{double x} returns the most significant word11045in the mantissa of \kbd{dbltor(x)}.1104611047\fun{int}{gisdouble}{GEN x} if \kbd{x} is a real number (not necessarily11048a~\typ{REAL}), return $1$ if \kbd{x} can be converted to a \kbd{double},11049$0$ otherwise.1105011051\fun{double}{gtodouble}{GEN x} if \kbd{x} is a real number (not necessarily11052a~\typ{REAL}), converts \kbd{x} into a \kbd{double} if possible.1105311054\fun{long}{gtos}{GEN x} converts the \typ{INT} \kbd{x} to a small11055integer if possible, otherwise raise an exception. This function11056is similar to \tet{itos}, slightly slower since it checks the type of \kbd{x}.1105711058\fun{ulong}{gtou}{GEN x} converts the non-negative \typ{INT} \kbd{x} to11059an unsigned small integer if possible, otherwise raise an exception. This11060function is similar to \tet{itou}, slightly slower since it checks the type11061of \kbd{x}.1106211063\fun{double}{dbllog2r}{GEN x} assuming that \kbd{x} is a nonzero \typ{REAL},11064returns an approximation to \kbd{log2(|x|)}.1106511066\fun{double}{dblmodulus}{GEN x} return an approximation to \kbd{|x|}.1106711068\fun{long}{gtolong}{GEN x} if \kbd{x} is an integer (not necessarily11069a~\typ{INT}), converts \kbd{x} into a \kbd{long} if possible.1107011071\fun{GEN}{fractor}{GEN x, long l} applied to a \typ{FRAC}~\kbd{x}, converts11072\kbd{x} into a \typ{REAL} of length \kbd{prec}.1107311074\fun{GEN}{quadtofp}{GEN x, long l} applied to a \typ{QUAD}~\kbd{x}, converts11075\kbd{x} into a \typ{REAL} or \typ{COMPLEX} depending on the sign of the11076discriminant of~\kbd{x}, to precision \hbox{\kbd{l} \B-bit} words.11077% forbid line brk at hyphen here [GN]1107811079\fun{GEN}{upper_to_cx}{GEN x, long *prec} valid for a \typ{COMPLEX}11080or \typ{QUAD} belonging to the upper half-plane. If a \typ{QUAD}, convert it11081to \typ{COMPLEX} using accuracy \kbd{*prec}. If $x$ is inexact, sets11082\kbd{*prec} to the precision of $x$.1108311084\fun{GEN}{cxtofp}{GEN x, long prec} converts the \typ{COMPLEX}~\kbd{x} to a11085a complex whose real and imaginary parts are \typ{REAL} of length \kbd{prec}11086(special case of~\kbd{gtofp}.1108711088\fun{GEN}{cxcompotor}{GEN x, long prec} converts the11089\typ{INT}, \typ{REAL} or \typ{FRAC} $x$ to a \typ{REAL} of length \kbd{prec}.11090These are all the real types which may occur as components of a11091\typ{COMPLEX}; special case of~\kbd{gtofp} (introduced so that the latter is11092not recursive and can thus be inlined).1109311094\fun{GEN}{cxtoreal}{GEN x} converts the complex (\typ{INT}, \typ{REAL},11095\typ{FRAC} or \typ{COMPLEX}) $x$ to a real number if its imaginary part is 0.11096Shallow function.1109711098converts the \typ{COMPLEX}~\kbd{x} to a11099a complex whose real and imaginary parts are \typ{REAL} of length \kbd{prec}11100(special case of~\kbd{gtofp}.1110111102\fun{GEN}{gtofp}{GEN x, long prec} converts the complex number~\kbd{x}11103(\typ{INT}, \typ{REAL}, \typ{FRAC}, \typ{QUAD} or \typ{COMPLEX}) to either11104a \typ{REAL} or \typ{COMPLEX} whose components are \typ{REAL} of precision11105\kbd{prec}; not necessarily of \emph{length} \kbd{prec}: a real $0$ may be11106given as \kbd{real\_0(...)}). If the result is a \typ{COMPLEX} extra care is11107taken so that its modulus really has accuracy \kbd{prec}: there is a problem11108if the real part of the input is an exact $0$; indeed, converting it to11109\kbd{real\_0(prec)} would be wrong if the imaginary part is tiny, since the11110modulus would then become equal to $0$, as in $1.E-100 + 0.E-28 = 0.E-28$.1111111112\fun{GEN}{gtomp}{GEN z, long prec} converts the real number~\kbd{x}11113(\typ{INT}, \typ{REAL}, \typ{FRAC}, real \typ{QUAD}) to either11114a \typ{INT} or a \typ{REAL} of precision \kbd{prec}. Not memory clean11115if $x$ is a \typ{INT}: we return $x$ itself and not a copy.1111611117\fun{GEN}{gcvtop}{GEN x, GEN p, long l} converts $x$ into a \typ{PADIC}11118of precision~$l$. Works componentwise on recursive objects,11119e.g.~\typ{POL} or \typ{VEC}. Converting $0$ yields $O(p^l)$; converting a11120nonzero number yield a result well defined modulo $p^{v_p(x) + l}$.1112111122\fun{GEN}{cvtop}{GEN x, GEN p, long l} as \kbd{gcvtop}, assuming that $x$11123is a scalar.1112411125\fun{GEN}{cvtop2}{GEN x, GEN y} $y$ being a $p$-adic, converts the scalar $x$11126to a $p$-adic of the same accuracy. Shallow function.1112711128\fun{GEN}{cvstop2}{long s, GEN y} $y$ being a $p$-adic, converts the scalar $s$11129to a $p$-adic of the same accuracy. Shallow function.1113011131\fun{GEN}{gprec}{GEN x, long l} returns a copy of $x$ whose precision is11132changed to $l$ digits. The precision change is done recursively on all11133components of $x$. Digits means \emph{decimal}, $p$-adic and $X$-adic digits11134for \typ{REAL}, \typ{SER}, \typ{PADIC} components, respectively.1113511136\fun{GEN}{gprec_w}{GEN x, long l} returns a shallow copy of $x$ whose11137\typ{REAL} components have their precision changed to $l$ \emph{words}. This11138is often more useful than \kbd{gprec}.1113911140\fun{GEN}{gprec_wtrunc}{GEN x, long l} returns a shallow copy of $x$ whose11141\typ{REAL} components have their precision \emph{truncated} to $l$11142\emph{words}. Contrary to \kbd{gprec\_w}, this function may never increase11143the precision of~$x$.1114411145\fun{GEN}{gprec_wensure}{GEN x, long l} returns a shallow copy of $x$ whose11146\typ{REAL} components have their precision \emph{increased} to at least $l$11147\emph{words}. Contrary to \kbd{gprec\_w}, this function may never decrease11148the precision of~$x$.1114911150The following functions are obsolete and kept for backward compatibility only:1115111152\fun{GEN}{precision0}{GEN x, long n}1115311154\fun{GEN}{bitprecision0}{GEN x, long n}1115511156\subsec{Modular objects / lifts}1115711158\fun{GEN}{gmodulo}{GEN x, GEN y} creates the object \kbd{\key{Mod}(x,y)} on11159the PARI stack, where \kbd{x} and \kbd{y} are either both \typ{INT}s, and the11160result is a \typ{INTMOD}, or \kbd{x} is a scalar or a \typ{POL} and \kbd{y} a11161\typ{POL}, and the result is a \typ{POLMOD}.1116211163\fun{GEN}{gmodulgs}{GEN x, long y} same as \key{gmodulo} except \kbd{y} is a11164\kbd{long}.1116511166\fun{GEN}{gmodulsg}{long x, GEN y} same as \key{gmodulo} except \kbd{x} is a11167\kbd{long}.1116811169\fun{GEN}{gmodulss}{long x, long y} same as \key{gmodulo} except both11170\kbd{x} and \kbd{y} are \kbd{long}s.1117111172\fun{GEN}{lift_shallow}{GEN x} shallow version of \tet{lift}1117311174\fun{GEN}{liftall_shallow}{GEN x} shallow version of \tet{liftall}1117511176\fun{GEN}{liftint_shallow}{GEN x} shallow version of \tet{liftint}1117711178\fun{GEN}{liftpol_shallow}{GEN x} shallow version of \tet{liftpol}1117911180\fun{GEN}{centerlift0}{GEN x,long v} DEPRECATED, kept for backward11181compatibility only: use either \tet{lift0}$(x,v)$ or \tet{centerlift}$(x)$.1118211183\subsec{Between polynomials and coefficient arrays}1118411185\fun{GEN}{gtopoly}{GEN x, long v} converts or truncates the object~\kbd{x}11186into a \typ{POL} with main variable number~\kbd{v}. A common application11187would be the conversion of coefficient vectors (coefficients are given by11188decreasing degree). E.g.~\kbd{[2,3]} goes to \kbd{2*v + 3}1118911190\fun{GEN}{gtopolyrev}{GEN x, long v} converts or truncates the object~\kbd{x}11191into a \typ{POL} with main variable number~\kbd{v}, but vectors are converted11192in reverse order compared to \kbd{gtopoly} (coefficients are given by11193increasing degree). E.g.~\kbd{[2,3]} goes to \kbd{3*v + 2}. In other words11194the vector represents a polynomial in the basis $(1,v,v^2,v^3,\dots)$.1119511196\fun{GEN}{normalizepol}{GEN x} applied to an unnormalized \typ{POL}~\kbd{x}11197(with all coefficients correctly set except that \kbd{leading\_term(x)} might11198be zero), normalizes \kbd{x} correctly in place and returns~\kbd{x}. For11199internal use. Normalizing means deleting all leading \emph{exact} zeroes11200(as per \kbd{isexactzero}), except if the polynomial turns out to be $0$,11201in which case we try to find a coefficient $c$ which is a nonrational zero,11202and return the constant polynomial $c$. (We do this so that information11203about the base ring is not lost.)1120411205\fun{GEN}{normalizepol_lg}{GEN x, long l} applies \kbd{normalizepol} to11206\kbd{x}, pretending that \kbd{lg(x)} is $l$, which must be less than11207or equal to \kbd{lg(x)}. If equal, the function is equivalent to11208\kbd{normalizepol(x)}.1120911210\fun{GEN}{normalizepol_approx}{GEN x, long lx} as \kbd{normalizepol\_lg},11211with the difference that we just delete all leading zeroes (as per11212\kbd{gequal0}). This rougher normalization is used when we have no other11213choice, for instance before attempting a Euclidean division by $x$.1121411215The following routines do \emph{not} copy coefficients on the stack (they11216only move pointers around), hence are very fast but not suitable for11217\kbd{gerepile} calls. Recall that an \kbd{RgV} (resp.~an \kbd{RgX}, resp.~an11218\kbd{RgM}) is a \typ{VEC} or \typ{COL} (resp.~a \typ{POL}, resp.~a \typ{MAT})11219with arbitrary components. Similarly, an \kbd{RgXV} is a \typ{VEC} or11220\typ{COL} with \kbd{RgX} components, etc.1122111222\fun{GEN}{RgV_to_RgX}{GEN x, long v} converts the \kbd{RgV}~\kbd{x} to a11223(normalized) polynomial in variable~\kbd{v} (as \kbd{gtopolyrev}, without11224copy).1122511226\fun{GEN}{RgV_to_RgX_reverse}{GEN x, long v} converts the \kbd{RgV}~\kbd{x}11227to a (normalized) polynomial in variable~\kbd{v} (as \kbd{gtopoly},11228without copy).1122911230\fun{GEN}{RgX_to_RgC}{GEN x, long N} converts the \typ{POL}~\kbd{x} to a11231\typ{COL}~\kbd{v} with \kbd{N} components. Coefficients of \kbd{x} are listed11232by increasing degree, so that \kbd{y[i]} is the coefficient of the term of11233degree $i-1$ in \kbd{x}.1123411235\fun{GEN}{Rg_to_RgC}{GEN x, long N} as \tet{RgX_to_RgV}, except that other11236types than \typ{POL} are allowed for \kbd{x}, which is then considered as a11237constant polynomial.1123811239\fun{GEN}{RgM_to_RgXV}{GEN x, long v} converts the \kbd{RgM}~\kbd{x} to a11240\typ{VEC} of \kbd{RgX}, by repeated calls to \kbd{RgV\_to\_RgX}.1124111242\fun{GEN}{RgM_to_RgXV_reverse}{GEN x, long v} converts the \kbd{RgM}~\kbd{x}11243to a \typ{VEC} of \kbd{RgX}, by repeated calls to \kbd{RgV\_to\_RgX\_reverse}.1124411245\fun{GEN}{RgV_to_RgM}{GEN v, long N} converts the vector~\kbd{v} to11246a~\typ{MAT} with \kbd{N}~rows, by repeated calls to \kbd{Rg\_to\_RgV}.1124711248\fun{GEN}{RgXV_to_RgM}{GEN v, long N} converts the vector of \kbd{RgX}~\kbd{v}11249to a~\typ{MAT} with \kbd{N}~rows, by repeated calls to \kbd{RgX\_to\_RgV}.1125011251\fun{GEN}{RgM_to_RgXX}{GEN x, long v,long w} converts the \kbd{RgM}~\kbd{x} into11252a \typ{POL} in variable~\kbd{v}, whose coefficients are \typ{POL}s in11253variable~\kbd{w}. This is a shortcut for11254\bprog11255RgV_to_RgX( RgM_to_RgXV(x, w), v );11256@eprog\noindent11257There are no consistency checks with respect to variable11258priorities: the above is an invalid object if $\kbd{varncmp(v, w)} \geq 0$.1125911260\fun{GEN}{RgXX_to_RgM}{GEN x, long N} converts the \typ{POL}~\kbd{x} with11261\kbd{RgX} (or constant) coefficients to a matrix with \kbd{N} rows.1126211263\fun{long}{RgXY_degreex}{GEN P} return the degree of $P$ with respect to11264the secondary variable.1126511266\fun{GEN}{RgXY_derivx}{GEN P} return the derivative of $P$ with respect to11267the secondary variable.1126811269\fun{GEN}{RgXY_swap}{GEN P, long n, long w} converts the bivariate polynomial11270$\kbd{P}(u,v)$ (a \typ{POL} with \typ{POL} or scalar coefficients) to11271$P(\kbd{pol\_x[w]},u)$, assuming \kbd{n} is an upper bound for11272$\deg_v(\kbd{P})$.1127311274\fun{GEN}{RgXY_swapspec}{GEN C, long n, long w, long lP}11275as \kbd{RgXY\_swap} where the coefficients of $P$ are given by11276\kbd{gel(C,0),\dots,gel(C,lP-1)}.1127711278\fun{GEN}{RgX_to_ser}{GEN x, long l} convert the \typ{POL}~\kbd{x} to11279a \emph{shallow} \typ{SER} of length~$l\geq 2$.11280Unless the polynomial is an exact zero, the coefficient of lowest degree11281$T^d$ of the result is not an exact zero (as per \kbd{isexactzero}). The11282remainder is $O(T^{d+l-2})$.1128311284\fun{GEN}{RgX_to_ser_inexact}{GEN x, long l} convert the \typ{POL}~\kbd{x} to11285a \emph{shallow} \typ{SER} of length~$l\geq 2$.11286Unless the polynomial is zero, the coefficient of lowest degree11287$T^d$ of the result is not zero (as per \kbd{gequal0}). The11288remainder is $O(T^{d+l-2})$.1128911290\fun{GEN}{RgV_to_ser}{GEN x, long v, long l} convert the \typ{VEC}~\kbd{x},11291to a \emph{shallow} \typ{SER} of length~$l\geq 2$.1129211293\fun{GEN}{rfrac_to_ser}{GEN F, long l} applied to a \typ{RFRAC}~$F$,11294creates a \typ{SER} of length~$l\geq 2$ congruent to $F$. Not memory-clean11295but suitable for \kbd{gerepileupto}.1129611297\fun{GEN}{rfracrecip_to_ser_absolute}{GEN F, long d} applied to a11298\typ{RFRAC}~$F$, creates the \typ{SER} $F(1/t) + O(t^d)$. Note that11299we use absolute and not relative precision here.1130011301\fun{GEN}{gtoser}{GEN s, long v, long d}. This function is deprecated,11302kept for backward compatibility: it follows the semantic of \kbd{Ser(s,v)},11303with $d = \kbd{seriesprecision}$ implied and is hard to use as a general11304conversion function. Use \tet{gtoser_prec} instead.1130511306It converts the object~$s$ into a \typ{SER} with main variable number~\kbd{v}11307and $d > 0$ significant terms, but the argument $d$ is sometimes ignored.11308More precisely1130911310\item if $s$ is a scalar (with respect to variable $v$), we return a11311constant power series with $d$ significant terms;1131211313\item if $s$ is a \typ{POL} in variable $v$, it is truncated to $d$ terms if11314needed;1131511316\item if $s$ is a vector, the coefficients of the vector are understood to11317be the coefficients of the power series starting from the constant term (as11318in \tet{Polrev}), and the precision $d$ is \emph{ignored};1131911320\item if $s$ is already a power series in $v$, we return a copy, and11321the precision $d$ is again \emph{ignored}.1132211323\fun{GEN}{gtoser_prec}{GEN s, long v, long d} this function is a variant of11324\kbd{gtoser} following the semantic of \kbd{Ser(s,v,d)}: the precision $d$11325is always taken into account.1132611327\fun{GEN}{gtocol}{GEN x} converts the object~\kbd{x} into a \typ{COL}1132811329\fun{GEN}{gtomat}{GEN x} converts the object~\kbd{x} into a \typ{MAT}.1133011331\fun{GEN}{gtovec}{GEN x} converts the object~\kbd{x} into a \typ{VEC}.1133211333\fun{GEN}{gtovecsmall}{GEN x} converts the object~\kbd{x} into a11334\typ{VECSMALL}.1133511336\fun{GEN}{normalize}{GEN x} applied to an unnormalized \typ{SER}~\kbd{x}11337(i.e.~type \typ{SER} with all coefficients correctly set except that \kbd{x[2]}11338might be zero), normalizes \kbd{x} correctly in place. Returns~\kbd{x}.11339For internal use.1134011341\fun{GEN}{serchop0}{GEN s} given a \typ{SER} of the form $x^v s(x)$, with11342$s(0)\neq 0$, return $x^v(s - s(0))$. Shallow function.1134311344\fun{GEN}{serchop_i}{GEN x, long n} returns a shallow chopy of \typ{SER} $x$11345with all terms of degree strictly less than $n$ removed. Shallow11346version of \kbd{serchop}.1134711348\section{Constructors}1134911350\subsec{Clean constructors}\label{se:clean}1135111352\fun{GEN}{zeropadic}{GEN p, long n} creates a $0$ \typ{PADIC} equal to11353$O(\kbd{p}^\kbd{n})$.1135411355\fun{GEN}{zeroser}{long v, long n} creates a $0$ \typ{SER} in variable11356\kbd{v} equal to $O(X^\kbd{n})$.1135711358\fun{GEN}{scalarser}{GEN x, long v, long prec} creates a constant \typ{SER}11359in variable \kbd{v} and precision \kbd{prec}, whose constant coefficient is11360(a copy of) \kbd{x}, in other words $\kbd{x} + O(\kbd{v}^\kbd{prec})$.11361Assumes that $\kbd{prec}\geq 0$.1136211363\fun{GEN}{pol_0}{long v} Returns the constant polynomial $0$ in variable $v$.1136411365\fun{GEN}{pol_1}{long v} Returns the constant polynomial $1$ in variable $v$.1136611367\fun{GEN}{pol_x}{long v} Returns the monomial of degree $1$ in variable $v$.1136811369\fun{GEN}{pol_xn}{long n, long v} Returns the monomial of degree $n$11370in variable $v$; assume that $n \geq 0$.1137111372\fun{GEN}{pol_xnall}{long n, long v} Returns the Laurent monomial of degree $n$11373in variable $v$; $n < 0$ is allowed.1137411375\fun{GEN}{pol_x_powers}{long N, long v} returns the powers of11376\kbd{pol\_x(v)}, of degree $0$ to $N-1$, in a vector with $N$ components.1137711378\fun{GEN}{scalarpol}{GEN x, long v} creates a constant \typ{POL} in variable11379\kbd{v}, whose constant coefficient is (a copy of) \kbd{x}.1138011381\fun{GEN}{deg1pol}{GEN a, GEN b,long v} creates the degree 1 \typ{POL}11382$a \kbd{pol\_x}(v) + b$1138311384\fun{GEN}{zeropol}{long v} is identical \kbd{pol\_0}.1138511386\fun{GEN}{zerocol}{long n} creates a \typ{COL} with \kbd{n} components set to11387\kbd{gen\_0}.1138811389\fun{GEN}{zerovec}{long n} creates a \typ{VEC} with \kbd{n} components set to11390\kbd{gen\_0}.1139111392\fun{GEN}{zerovec_block}{long n} as \kbd{zerovec} but return a clone.1139311394\fun{GEN}{col_ei}{long n, long i} creates a \typ{COL} with \kbd{n} components11395set to \kbd{gen\_0}, but for the \kbd{i}-th one which is set to \kbd{gen\_1}11396(\kbd{i}-th vector in the canonical basis).1139711398\fun{GEN}{vec_ei}{long n, long i} creates a \typ{VEC} with \kbd{n} components11399set to \kbd{gen\_0}, but for the \kbd{i}-th one which is set to \kbd{gen\_1}11400(\kbd{i}-th vector in the canonical basis).1140111402\fun{GEN}{trivial_fact}{void} returns the trivial (empty) factorization11403\kbd{Mat([]\til,[]\til)}1140411405\fun{GEN}{prime_fact}{GEN x} returns the factorization11406\kbd{Mat([x]\til, [1]\til)}1140711408\fun{GEN}{Rg_col_ei}{GEN x, long n, long i} creates a \typ{COL} with \kbd{n}11409components set to \kbd{gen\_0}, but for the \kbd{i}-th one which is set to11410\kbd{x}.1141111412\fun{GEN}{vecsmall_ei}{long n, long i} creates a \typ{VECSMALL} with \kbd{n}11413components set to \kbd{0}, but for the \kbd{i}-th one which is set to11414\kbd{1} (\kbd{i}-th vector in the canonical basis).1141511416\fun{GEN}{scalarcol}{GEN x, long n} creates a \typ{COL} with \kbd{n}11417components set to \kbd{gen\_0}, but the first one which is set to a copy11418of \kbd{x}. (The name comes from \kbd{RgV\_isscalar}.)1141911420\smallskip1142111422\fun{GEN}{mkintmodu}{ulong x, ulong y} creates the \typ{INTMOD} \kbd{Mod(x, y)}.11423The inputs must satisfy $x < y$.1142411425\fun{GEN}{zeromat}{long m, long n} creates a \typ{MAT} with \kbd{m} x \kbd{n}11426components set to \kbd{gen\_0}. Note that the result allocates a11427\emph{single} column, so modifying an entry in one column modifies it in11428all columns. To fully allocate a matrix initialized with zero entries,11429use \kbd{zeromatcopy}.1143011431\fun{GEN}{zeromatcopy}{long m, long n} creates a \typ{MAT} with \kbd{m} x11432\kbd{n} components set to \kbd{gen\_0}.1143311434\fun{GEN}{matid}{long n} identity matrix in dimension \kbd{n} (with11435components \kbd{gen\_1} and\kbd{gen\_0}).1143611437\fun{GEN}{scalarmat}{GEN x, long n} scalar matrix, \kbd{x} times the identity.1143811439\fun{GEN}{scalarmat_s}{long x, long n} scalar matrix, \kbd{stoi(x)} times11440the identity.1144111442\fun{GEN}{vecrange}{GEN a, GEN b} returns the \typ{VEC} $[a..b]$.1144311444\fun{GEN}{vecrangess}{long a, long b} returns the \typ{VEC} $[a..b]$.1144511446\smallskip11447See also next section for analogs of the following functions:1144811449\fun{GEN}{mkfracss}{long x, long y} creates the \typ{FRAC} $x/y$. Assumes that11450$y > 1$ and $(x,y) = 1$.1145111452\fun{GEN}{sstoQ}{long x, long y} returns the \typ{INT} or \typ{FRAC} $x/y$;11453no assumptions.1145411455\fun{void}{Qtoss}{GEN q, long *n, long *d} given a \typ{INT} or \typ{FRAC} $q$,11456set $n$ and $d$ such that $q = n/d$ with $d \geq 1$ and $(n,d)$ = 1. Overflow11457error if numerator or denominator do not fit into a long integer.1145811459\fun{GEN}{mkfraccopy}{GEN x, GEN y} creates the \typ{FRAC} $x/y$. Assumes that11460$y > 1$ and $(x,y) = 1$.1146111462\fun{GEN}{mkrfraccopy}{GEN x, GEN y} creates the \typ{RFRAC} $x/y$.11463Assumes that $y$ is a \typ{POL}, $x$ a compatible type whose variable has11464lower or same priority, with $(x,y) = 1$.1146511466\fun{GEN}{mkcolcopy}{GEN x} creates a 1-dimensional \typ{COL} containing11467\kbd{x}.1146811469\fun{GEN}{mkmatcopy}{GEN x} creates a 1-by-1 \typ{MAT} wrapping the \typ{COL}11470\kbd{x}.1147111472\fun{GEN}{mkveccopy}{GEN x} creates a 1-dimensional \typ{VEC} containing11473\kbd{x}.1147411475\fun{GEN}{mkvec2copy}{GEN x, GEN y} creates a 2-dimensional \typ{VEC} equal11476to \kbd{[x,y]}.1147711478\fun{GEN}{mkcols}{long x} creates a 1-dimensional \typ{COL}11479containing \kbd{stoi(x)}.1148011481\fun{GEN}{mkcol2s}{long x, long y} creates a 2-dimensional \typ{COL}11482containing \kbd{[stoi(x), stoi(y)]~}.1148311484\fun{GEN}{mkcol3s}{long x, long y, long z} creates a 3-dimensional \typ{COL}11485containing \kbd{[stoi(x), stoi(y), stoi(z)]~}.1148611487\fun{GEN}{mkcol4s}{long x, long y, long z, long t} creates a 4-dimensional11488\typ{COL} containing \kbd{[stoi(x), stoi(y), stoi(z), stoi(t)]~}.1148911490\fun{GEN}{mkvecs}{long x} creates a 1-dimensional \typ{VEC}11491containing \kbd{stoi(x)}.1149211493\fun{GEN}{mkvec2s}{long x, long y} creates a 2-dimensional \typ{VEC}11494containing \kbd{[stoi(x), stoi(y)]}.1149511496\fun{GEN}{mkmat22s}{long a, long b, long c, long d} creates the $2$ by $2$11497\typ{MAT} with successive rows \kbd{[stoi(a), stoi(b)]} and11498\kbd{[stoi(c), stoi(d)]}.1149911500\fun{GEN}{mkvec3s}{long x, long y, long z} creates a 3-dimensional \typ{VEC}11501containing \kbd{[stoi(x), stoi(y), stoi(z)]}.1150211503\fun{GEN}{mkvec4s}{long x, long y, long z, long t} creates a 4-dimensional11504\typ{VEC} containing \kbd{[stoi(x), stoi(y), stoi(z), stoi(t)]}.1150511506\fun{GEN}{mkvecsmall}{long x} creates a 1-dimensional \typ{VECSMALL}11507containing \kbd{x}.1150811509\fun{GEN}{mkvecsmall2}{long x, long y} creates a 2-dimensional \typ{VECSMALL}11510containing \kbd{[x, y]}.1151111512\fun{GEN}{mkvecsmall3}{long x, long y, long z} creates a 3-dimensional11513\typ{VECSMALL} containing \kbd{[x, y, z]}.1151411515\fun{GEN}{mkvecsmall4}{long x, long y, long z, long t} creates a 4-dimensional11516\typ{VECSMALL} containing \kbd{[x, y, z, t]}.1151711518\fun{GEN}{mkvecsmall5}{long x, long y, long z, long t, long u} creates a115195-dimensional \typ{VECSMALL} containing \kbd{[x, y, z, t, u]}.1152011521\fun{GEN}{mkvecsmalln}{long n, ...} returns the \typ{VECSMALL} whose $n$11522coefficients (\kbd{long}) follow.11523\emph{Warning:} since this is a variadic function, C type promotion is not11524performed on the arguments by the compiler, thus you have to make sure that all11525the arguments are of type \kbd{long}, in particular integer constants need to11526be written with the \kbd{L} suffix: \kbd{mkvecsmalln(2, 1L, 2L)} is correct,11527but \kbd{mkvecsmalln(2, 1, 2)} is not.1152811529\subsec{Unclean constructors}\label{se:unclean}1153011531Contrary to the policy of general PARI functions, the functions in this11532subsection do \emph{not} copy their arguments, nor do they produce an object11533a priori suitable for \tet{gerepileupto}. In particular, they are11534faster than their clean equivalent (which may not exist). \emph{If} you11535restrict their arguments to universal objects (e.g \kbd{gen\_0}),11536then the above warning does not apply.1153711538\fun{GEN}{mkcomplex}{GEN x, GEN y} creates the \typ{COMPLEX} $x + iy$.1153911540\fun{GEN}{mulcxI}{GEN x} creates the \typ{COMPLEX} $ix$. The result in11541general contains data pointing back to the original $x$. Use \kbd{gcopy} if11542this is a problem. But in most cases, the result is to be used immediately,11543before $x$ is subject to garbage collection.1154411545\fun{GEN}{mulcxmI}{GEN x}, as \tet{mulcxI}, but returns $-ix$.1154611547\fun{GEN}{mulcxpowIs}{GEN x, long k}, as \tet{mulcxI}, but returns11548$x \cdot i^k$.1154911550\fun{GEN}{mkquad}{GEN n, GEN x, GEN y} creates the \typ{QUAD} $x + yw$,11551where $w$ is a root of $n$, which is of the form \kbd{quadpoly(D)}.1155211553\fun{GEN}{mkfrac}{GEN x, GEN y} creates the \typ{FRAC} $x/y$. Assumes that11554$y > 1$ and $(x,y) = 1$.1155511556\fun{GEN}{mkrfrac}{GEN x, GEN y} creates the \typ{RFRAC} $x/y$. Assumes11557that $y$ is a \typ{POL}, $x$ a compatible type whose variable has lower11558or same priority, with $(x,y) = 1$.1155911560\fun{GEN}{mkcol}{GEN x} creates a 1-dimensional \typ{COL} containing \kbd{x}.1156111562\fun{GEN}{mkcol2}{GEN x, GEN y} creates a 2-dimensional \typ{COL} equal to11563\kbd{[x,y]}.1156411565\fun{GEN}{mkcol3}{GEN x, GEN y, GEN z} creates a 3-dimensional \typ{COL}11566equal to \kbd{[x,y,z]}.1156711568\fun{GEN}{mkcol4}{GEN x, GEN y, GEN z, GEN t} creates a 4-dimensional \typ{COL}11569equal to \kbd{[x,y,z,t]}.1157011571\fun{GEN}{mkcol5}{GEN a1, GEN a2, GEN a3, GEN a4, GEN a5} creates the115725-dimensional \typ{COL} equal to $[a_1,a_2,a_3,a_4,a_5]$.1157311574\fun{GEN}{mkcol6}{GEN x, GEN y, GEN z, GEN t, GEN u, GEN v}11575creates the $6$-dimensional column vector \kbd{[x,y,z,t,u,v]~}.1157611577\fun{GEN}{mkintmod}{GEN x, GEN y} creates the \typ{INTMOD} \kbd{Mod(x, y)}.11578The inputs must be \typ{INT}s satisfying $0 \leq x < y$.1157911580\fun{GEN}{mkpolmod}{GEN x, GEN y} creates the \typ{POLMOD} \kbd{Mod(x, y)}.11581The input must satisfy $\deg x < \deg y$ with respect to the main variable of11582the \typ{POL} $y$. $x$ may be a scalar.1158311584\fun{GEN}{mkmat}{GEN x} creates a 1-column \typ{MAT} with column $x$11585(a \typ{COL}).1158611587\fun{GEN}{mkmat2}{GEN x, GEN y} creates a 2-column \typ{MAT} with columns11588$x$, $y$ (\typ{COL}s of the same length).1158911590\fun{GEN}{mkmat22}{GEN a, GEN b, GEN c, GEN d} creates the $2$ by $2$11591\typ{MAT} with successive rows $[a,b]$ and $[c,d]$.1159211593\fun{GEN}{mkmat3}{GEN x, GEN y, GEN z} creates a 3-column \typ{MAT} with columns11594$x$, $y$, $z$ (\typ{COL}s of the same length).1159511596\fun{GEN}{mkmat4}{GEN x, GEN y, GEN z, GEN t} creates a 4-column \typ{MAT}11597with columns $x$, $y$, $z$, $t$ (\typ{COL}s of the same length).1159811599\fun{GEN}{mkmat5}{GEN x, GEN y, GEN z, GEN t, GEN u} creates a 5-column11600\typ{MAT} with columns $x$, $y$, $z$, $t$, $u$ (\typ{COL}s of the same11601length).1160211603\fun{GEN}{mkvec}{GEN x} creates a 1-dimensional \typ{VEC} containing \kbd{x}.1160411605\fun{GEN}{mkvec2}{GEN x, GEN y} creates a 2-dimensional \typ{VEC} equal to11606\kbd{[x,y]}.1160711608\fun{GEN}{mkvec3}{GEN x, GEN y, GEN z} creates a 3-dimensional \typ{VEC}11609equal to \kbd{[x,y,z]}.1161011611\fun{GEN}{mkvec4}{GEN x, GEN y, GEN z, GEN t} creates a 4-dimensional \typ{VEC}11612equal to \kbd{[x,y,z,t]}.1161311614\fun{GEN}{mkvec5}{GEN a1, GEN a2, GEN a3, GEN a4, GEN a5} creates the116155-dimensional \typ{VEC} equal to $[a_1,a_2,a_3,a_4,a_5]$.1161611617\fun{GEN}{mkqfb}{GEN a, GEN b, GEN c, GEN D} creates \typ{QFB} equal11618to \kbd{Qfb(a,b,c)}, assuming that $D = b^2 - 4ac$.1161911620\fun{GEN}{mkerr}{long n} returns a \typ{ERROR} with error code $n$11621(\kbd{enum err\_list}).1162211623\smallskip1162411625It is sometimes useful to return such a container whose entries are not11626universal objects, but nonetheless suitable for \tet{gerepileupto}.11627If the entries can be computed at the time the result is returned, the11628following macros achieve this effect:1162911630\fun{GEN}{retmkvec}{GEN x} returns a vector containing the single entry $x$,11631where the vector root is created just before the function argument $x$ is11632evaluated. Expands to11633\bprog11634{11635GEN res = cgetg(2, t_VEC);11636gel(res, 1) = x; /* @Ccom or rather, the \emph{expansion} of $x$ */11637return res;11638}11639@eprog\noindent For instance, the \kbd{retmkvec(gcopy(x))} returns a clean11640object, just like \kbd{return mkveccopy(x)} would.1164111642\fun{GEN}{retmkvec2}{GEN x, GEN y}11643returns the $2$-dimensional \typ{VEC} \kbd{[x,y]}.1164411645\fun{GEN}{retmkvec3}{GEN x, GEN y, GEN z}11646returns the $3$-dimensional \typ{VEC} \kbd{[x,y,z]}.1164711648\fun{GEN}{retmkvec4}{GEN x, GEN y, GEN z, GEN t}11649returns the $4$-dimensional \typ{VEC} \kbd{[x,y,z,t]}.1165011651\fun{GEN}{retmkvec5}{GEN x, GEN y, GEN z, GEN t, GEN u}11652returns the $5$-dimensional row vector \kbd{[x,y,z,t,u]}.1165311654\fun{GEN}{retconst_vec}{long n, GEN x}11655returns the $n$-dimensional \typ{VEC} whose entries are constant and all11656equal to $x$.1165711658\fun{GEN}{retmkcol}{GEN x}11659returns the $1$-dimensional \typ{COL} \kbd{[x]~}.1166011661\fun{GEN}{retmkcol2}{GEN x, GEN y}11662returns the $2$-dimensional \typ{COL} \kbd{[x,y]~}.1166311664\fun{GEN}{retmkcol3}{GEN x, GEN y, GEN z}11665returns the $3$-dimensional \typ{COL} \kbd{[x,y,z]~}.1166611667\fun{GEN}{retmkcol4}{GEN x, GEN y, GEN z, GEN t}11668returns the $4$-dimensional \typ{COL} \kbd{[x,y,z,t]~}.1166911670\fun{GEN}{retmkcol5}{GEN x, GEN y, GEN z, GEN t, GEN u}11671returns the $5$-dimensional column vector \kbd{[x,y,z,t,u]~}.1167211673\fun{GEN}{retmkcol6}{GEN x, GEN y, GEN z, GEN t, GEN u, GEN v}11674returns the $6$-dimensional column vector \kbd{[x,y,z,t,u,v]~}.1167511676\fun{GEN}{retconst_col}{long n, GEN x}11677returns the $n$-dimensional \typ{COL} whose entries are constant and all11678equal to $x$.1167911680\fun{GEN}{retmkmat}{GEN x}11681returns the $1$-column \typ{MAT} with colum \kbd{x}.1168211683\fun{GEN}{retmkmat2}{GEN x, GEN y}11684returns the $2$-column \typ{MAT} with columns \kbd{x}, \kbd{y}.1168511686\fun{GEN}{retmkmat3}{GEN x, GEN y, GEN z}11687returns the $3$-dimensional \typ{MAT} with columns11688\kbd{x}, \kbd{y}, \kbd{z}.1168911690\fun{GEN}{retmkmat4}{GEN x, GEN y, GEN z, GEN t}11691returns the $4$-dimensional \typ{MAT} with columns11692\kbd{x}, \kbd{y}, \kbd{z}, \kbd{t}.1169311694\fun{GEN}{retmkmat5}{GEN x, GEN y, GEN z, GEN t, GEN u}11695returns the $5$-dimensional \typ{MAT} with columns11696\kbd{x}, \kbd{y}, \kbd{z}, \kbd{t}, \kbd{u}.1169711698\fun{GEN}{retmkcomplex}{GEN x, GEN y}11699returns the \typ{COMPLEX} \kbd{x + I*y}.1170011701\fun{GEN}{retmkfrac}{GEN x, GEN y}11702returns the \typ{FRAC} \kbd{x / y}. Assume $x$ and $y$ are coprime and $y > 1$.1170311704\fun{GEN}{retmkrfrac}{GEN x, GEN y}11705returns the \typ{RFRAC} \kbd{x / y}. Assume $x$ and $y$ are coprime and more11706generally that the rational function cannot be simplified.1170711708\fun{GEN}{retmkintmod}{GEN x, GEN y}11709returns the \typ{INTMOD} \kbd{Mod(x, y)}.1171011711\fun{GEN}{retmkquad}{GEN n, GEN a, GEN b}.1171211713\fun{GEN}{retmkpolmod}{GEN x, GEN y}11714returns the \typ{POLMOD} \kbd{Mod(x, y)}.1171511716\smallskip1171711718\fun{GEN}{mkintn}{long n, ...} returns the nonnegative \typ{INT} whose11719development in base $2^{32}$ is given by the following $n$ 32bit-words11720(\kbd{unsigned int}).11721\bprog11722mkintn(3, a2, a1, a0);11723@eprog11724\noindent returns $a_2 2^{64} + a_1 2^{32} + a_0$.1172511726\fun{GEN}{mkpoln}{long n, ...} Returns the \typ{POL} whose $n$11727coefficients (\kbd{GEN}) follow, in order of decreasing degree.11728\bprog11729mkpoln(3, gen_1, gen_2, gen_0);11730@eprog11731\noindent returns the polynomial $X^2 + 2X$ (in variable $0$, use11732\tet{setvarn} if you want other variable numbers). Beware that $n$ is the11733number of coefficients, hence \emph{one more} than the degree.1173411735\fun{GEN}{mkvecn}{long n, ...} returns the \typ{VEC} whose $n$11736coefficients (\kbd{GEN}) follow.1173711738\fun{GEN}{mkcoln}{long n, ...} returns the \typ{COL} whose $n$11739coefficients (\kbd{GEN}) follow.1174011741\fun{GEN}{scalarcol_shallow}{GEN x, long n} creates a \typ{COL} with \kbd{n}11742components set to \kbd{gen\_0}, but the first one which is set to a shallow11743copy of \kbd{x}. (The name comes from \kbd{RgV\_isscalar}.)1174411745\fun{GEN}{scalarmat_shallow}{GEN x, long n} creates an $n\times n$11746scalar matrix whose diagonal is set to shallow copies of the scalar \kbd{x}.1174711748\fun{GEN}{RgX_sylvestermatrix}{GEN f, GEN g} return the Sylvester matrix11749attached to the two \typ{POL} in the same variable $f$ and $g$.1175011751\fun{GEN}{diagonal_shallow}{GEN x} returns a diagonal matrix whose diagonal11752is given by the vector $x$. Shallow function.1175311754\fun{GEN}{scalarpol_shallow}{GEN a, long v} returns the degree $0$11755\typ{POL} $a \kbd{pol\_x}(v)^0$.1175611757\fun{GEN}{deg1pol_shallow}{GEN a, GEN b,long v} returns the degree $1$11758\typ{POL} $a\kbd{pol\_x}(v) + b$1175911760\fun{GEN}{deg2pol_shallow}{GEN a, GEN b, GEN c, long v} returns the degree $2$11761\typ{POL} $a\*x^2+b\*x+c$ where $x=\kbd{pol\_x}(v)$.1176211763\fun{GEN}{zeropadic_shallow}{GEN p, long n} returns a (shallow) $0$11764\typ{PADIC} equal to $O(\kbd{p}^\kbd{n})$.1176511766\subsec{From roots to polynomials}1176711768\fun{GEN}{deg1_from_roots}{GEN L, long v} given a vector $L$ of scalars,11769returns the vector of monic linear polynomials in variable $v$ whose roots11770are the $L[i]$, i.e. the $x - L[i]$.1177111772\fun{GEN}{roots_from_deg1}{GEN L} given a vector $L$ of monic linear11773polynomials, return their roots, i.e. the $- L[i](0)$.1177411775\fun{GEN}{roots_to_pol}{GEN L, long v} given a vector of scalars $L$,11776returns the monic polynomial in variable $v$ whose roots are the $L[i]$.11777Leaves some garbage on stack, but suitable for \kbd{gerepileupto}.1177811779\fun{GEN}{roots_to_pol_r1}{GEN L, long v, long r1} as \kbd{roots\_to\_pol}11780assuming the first $r_1$ roots are ``real'', and the following ones are11781representatives of conjugate pairs of ``complex'' roots. So if $L$ has $r_1 +11782r_2$ elements, we obtain a polynomial of degree $r_1 + 2r_2$. In most11783applications, the roots are indeed real and complex, but the implementation11784assumes only that each ``complex'' root $z$ introduces a quadratic11785factor $X^2 - \kbd{trace}(z) X + \kbd{norm}(z)$.11786Leaves some garbage on stack, but suitable for \kbd{gerepileupto}.1178711788\section{Integer parts}1178911790\fun{GEN}{gfloor}{GEN x} creates the floor of~\kbd{x}, i.e.\ the (true)11791integral part.1179211793\fun{GEN}{gfrac}{GEN x} creates the fractional part of~\kbd{x}, i.e.\ \kbd{x}11794minus the floor of~\kbd{x}.1179511796\fun{GEN}{gceil}{GEN x} creates the ceiling of~\kbd{x}.1179711798\fun{GEN}{ground}{GEN x} rounds towards~$+\infty$ the components of \kbd{x}11799to the nearest integers.1180011801\fun{GEN}{grndtoi}{GEN x, long *e} same as \kbd{ground}, but in addition sets11802\kbd{*e} to the binary exponent of $x - \kbd{ground}(x)$. If this is11803positive, all significant bits are lost. This kind of situation raises an11804error message in \key{ground} but not in \key{grndtoi}.1180511806\fun{GEN}{gtrunc}{GEN x} truncates~\kbd{x}. This is the false integer part11807if \kbd{x} is a real number (i.e.~the unique integer closest to \kbd{x} among11808those between 0 and~\kbd{x}). If \kbd{x} is a \typ{SER}, it is truncated11809to a \typ{POL}; if \kbd{x} is a \typ{RFRAC}, this takes the polynomial part.1181011811\fun{GEN}{gtrunc2n}{GEN x, long n} creates the floor of~$2^n$\kbd{x}, this is11812only implemented for \typ{INT}, \typ{REAL}, \typ{FRAC} and \typ{COMPLEX} of11813those.1181411815\fun{GEN}{gcvtoi}{GEN x, long *e} analogous to \key{grndtoi} for11816\typ{REAL} inputs except that rounding is replaced by truncation. Also applies11817componentwise for vector or matrix inputs; otherwise, sets \kbd{*e} to11818\kbd{-HIGHEXPOBIT} (infinite real accuracy) and return \kbd{gtrunc(x)}.1181911820\section{Valuation and shift}1182111822\fun{GEN}{gshift[z]}{GEN x, long n[, GEN z]} yields the result of shifting11823(the components of) \kbd{x} left by \kbd{n} (if \kbd{n} is nonnegative)11824or right by $-\kbd{n}$ (if \kbd{n} is negative). Applies only to \typ{INT}11825and vectors/matrices of such. For other types, it is simply multiplication11826by~$2^{\kbd{n}}$.1182711828\fun{GEN}{gmul2n[z]}{GEN x, long n[, GEN z]} yields the product of \kbd{x}11829and~$2^{\kbd{n}}$. This is different from \kbd{gshift} when \kbd{n} is negative11830and \kbd{x} is a \typ{INT}: \key{gshift} truncates, while \key{gmul2n}11831creates a fraction if necessary.1183211833\fun{long}{gvaluation}{GEN x, GEN p} returns the greatest exponent~$e$ such that11834$\kbd{p}^e$ divides~\kbd{x}, when this makes sense.1183511836\fun{long}{gval}{GEN x, long v} returns the highest power of the variable11837number \kbd{v} dividing the \typ{POL}~\kbd{x}.1183811839\section{Comparison operators}1184011841\subsec{Generic}1184211843\fun{long}{gcmp}{GEN x, GEN y} comparison of \kbd{x} with \kbd{y}: returns11844$1$ ($x > y$), $0$ ($x = y$) or $-1$ ($x < y$). Two \typ{STR}11845are compared using the standard lexicographic ordering; a \typ{STR}11846cannot be compared to any non-string type. If neither11847$x$ nor $y$ is a \typ{STR}, their allowed types are \typ{INT}, \typ{REAL},11848\typ{FRAC}, \typ{QUAD} with positive discriminant (use the canonical11849embedding $w \to \sqrt{D}/2$ or $w \to (1 + \sqrt{D})/2$) or \typ{INFINITY}.11850Use \tet{cmp_universal} to compare arbitrary \kbd{GEN}s.1185111852\fun{long}{lexcmp}{GEN x, GEN y} comparison of \kbd{x} with \kbd{y} for the11853lexicographic ordering; when comparing objects of different lengths whose11854components are all equal up to the smallest of their length, consider that11855the longest is largest. Consider scalars as $1$-component vectors. Return11856\kbd{gcmp}$(x,y)$ if both arguments are scalars.1185711858\fun{int}{gequalX}{GEN x} return 1 (true) if \kbd{x} is a variable11859(monomial of degree $1$ with \typ{INT} coefficients equal to $1$ and $0$),11860and $0$ otherwise1186111862\fun{long}{gequal}{GEN x, GEN y} returns 1 (true) if \kbd{x} is equal11863to~\kbd{y}, 0~otherwise. A priori, this makes sense only if \kbd{x} and11864\kbd{y} have the same type, in which case they are recursively compared11865componentwise. When the types are different, a \kbd{true} result11866means that \kbd{x - y} was successfully computed and that11867\kbd{gequal0} found it equal to $0$. In particular11868\bprog11869gequal(cgetg(1, t_VEC), gen_0)11870@eprog\noindent is true, and the relation is not transitive. E.g.~an empty11871\typ{COL} and an empty \typ{VEC} are not equal but are both equal to11872\kbd{gen\_0}.1187311874\fun{long}{gidentical}{GEN x, GEN y} returns 1 (true) if \kbd{x} is identical11875to~\kbd{y}, 0~otherwise. In particular, the types and length of \kbd{x} and11876\kbd{y} must be equal. This test is much stricter than \tet{gequal}, in11877particular, \typ{REAL} with different accuracies are tested different. This11878relation is transitive.1187911880\fun{GEN}{gmax}{GEN x, GEN y} returns a copy of the maximum of $x$ and $y$,11881compared using \kbd{gcmp}.1188211883\fun{GEN}{gmin}{GEN x, GEN y} returns a copy of the minimum of $x$ and $y$,11884compared using \kbd{gcmp}.1188511886\fun{GEN}{gmax_shallow}{GEN x, GEN y} shallow version of \kbd{gmax}.1188711888\fun{GEN}{gmin_shallow}{GEN x, GEN y} shallow version of \kbd{gmin}.1188911890\subsec{Comparison with a small integer}1189111892\fun{int}{isexactzero}{GEN x} returns 1 (true) if \kbd{x} is exactly equal11893to~0 (including \typ{INTMOD}s like \kbd{Mod(0,2)}), and 0~(false) otherwise.11894This includes recursive objects, for instance vectors, whose components are $0$.1189511896\fun{GEN}{gisexactzero}{GEN x} returns \kbd{NULL} unless \kbd{x} is exactly11897equal to~0 (as per \kbd{isexactzero}). When \kbd{x} is an exact zero11898return the attached scalar zero as a \typ{INT} (\kbd{gen\_0}),11899a \typ{INTMOD} (\kbd{Mod(0,$N$)} for the largest possible $N$) or a11900\typ{FFELT}.1190111902\fun{int}{isrationalzero}{GEN x} returns 1 (true) if \kbd{x} is equal11903to an integer~0 (excluding \typ{INTMOD}s like \kbd{Mod(0,2)}), and 0~(false)11904otherwise. Contrary to \kbd{isintzero}, this includes recursive objects, for11905instance vectors, whose components are $0$.1190611907\fun{int}{ismpzero}{GEN x} returns 1 (true) if \kbd{x} is a \typ{INT} or11908a \typ{REAL} equal to~0.1190911910\fun{int}{isintzero}{GEN x} returns 1 (true) if \kbd{x} is a \typ{INT}11911equal to~0.1191211913\fun{int}{isint1}{GEN x} returns 1 (true) if \kbd{x} is a \typ{INT}11914equal to~1.1191511916\fun{int}{isintm1}{GEN x} returns 1 (true) if \kbd{x} is a \typ{INT}11917equal to~$-1$.1191811919\fun{int}{equali1}{GEN n}11920Assuming that \kbd{x} is a \typ{INT}, return 1 (true) if \kbd{x} is equal to11921$1$, and return 0~(false) otherwise.1192211923\fun{int}{equalim1}{GEN n}11924Assuming that \kbd{x} is a \typ{INT}, return 1 (true) if \kbd{x} is equal to11925$-1$, and return 0~(false) otherwise.1192611927\fun{int}{is_pm1}{GEN x}. Assuming that \kbd{x} is a11928\emph{nonzero} \typ{INT}, return 1 (true) if \kbd{x} is equal to $-1$ or11929$1$, and return 0~(false) otherwise.1193011931\fun{int}{gequal0}{GEN x} returns 1 (true) if \kbd{x} is equal to~0, 0~(false)11932otherwise.1193311934\fun{int}{gequal1}{GEN x} returns 1 (true) if \kbd{x} is equal to~1, 0~(false)11935otherwise.1193611937\fun{int}{gequalm1}{GEN x} returns 1 (true) if \kbd{x} is equal to~$-1$,119380~(false) otherwise.119391194011941\fun{long}{gcmpsg}{long s, GEN x}1194211943\fun{long}{gcmpgs}{GEN x, long s} comparison of \kbd{x} with the11944\kbd{long}~\kbd{s}.1194511946\fun{GEN}{gmaxsg}{long s, GEN x}1194711948\fun{GEN}{gmaxgs}{GEN x, long s} returns the largest of \kbd{x} and11949the \kbd{long}~\kbd{s} (converted to \kbd{GEN})1195011951\fun{GEN}{gminsg}{long s, GEN x}1195211953\fun{GEN}{gmings}{GEN x, long s} returns the smallest of \kbd{x} and the11954\kbd{long}~\kbd{s} (converted to \kbd{GEN})1195511956\fun{long}{gequalsg}{long s, GEN x}1195711958\fun{long}{gequalgs}{GEN x, long s} returns 1 (true) if \kbd{x} is equal to11959the \kbd{long}~\kbd{s}, 0~otherwise.1196011961\section{Miscellaneous Boolean functions}1196211963\fun{int}{isrationalzeroscalar}{GEN x} equivalent to, but faster than,11964\bprog11965is_scalar_t(typ(x)) && isrationalzero(x)11966@eprog1196711968\fun{int}{isinexact}{GEN x} returns 1 (true) if $x$ has an inexact11969component, and 0 (false) otherwise.1197011971\fun{int}{isinexactreal}{GEN x} return 1 if $x$ has an inexact11972\typ{REAL} component, and 0 otherwise.1197311974\fun{int}{isrealappr}{GEN x, long e} applies (recursively) to complex inputs;11975returns $1$ if $x$ is approximately real to the bit accuracy $e$, and 011976otherwise. This means that any \typ{COMPLEX} component must have imaginary part11977$t$ satisfying $\kbd{gexpo}(t) < e$.1197811979\fun{int}{isint}{GEN x, GEN *n} returns 0 (false) if \kbd{x} does not round11980to an integer. Otherwise, returns 1 (true) and set \kbd{n} to the rounded11981value.1198211983\fun{int}{issmall}{GEN x, long *n} returns 0 (false) if \kbd{x} does not11984round to a small integer (suitable for \kbd{itos}). Otherwise, returns 111985(true) and set \kbd{n} to the rounded value.1198611987\fun{long}{iscomplex}{GEN x} returns 1 (true) if \kbd{x} is a complex number11988(of component types embeddable into the reals) but is not itself real, 0~if11989\kbd{x} is a real (not necessarily of type \typ{REAL}), or raises an error if11990\kbd{x} is not embeddable into the complex numbers.1199111992\subsec{Obsolete}1199311994The following less convenient comparison functions and Boolean operators were11995used by the historical GP interpreter. They are provided for backward11996compatibility only and should not be used:1199711998\fun{GEN}{gle}{GEN x, GEN y}1199912000\fun{GEN}{glt}{GEN x, GEN y}1200112002\fun{GEN}{gge}{GEN x, GEN y}1200312004\fun{GEN}{ggt}{GEN x, GEN y}1200512006\fun{GEN}{geq}{GEN x, GEN y}1200712008\fun{GEN}{gne}{GEN x, GEN y}1200912010\fun{GEN}{gor}{GEN x, GEN y}1201112012\fun{GEN}{gand}{GEN x, GEN y}1201312014\fun{GEN}{gnot}{GEN x, GEN y}1201512016\section{Sorting}1201712018\subsec{Basic sort}1201912020\fun{GEN}{sort}{GEN x} sorts the vector \kbd{x} in ascending order using a12021mergesort algorithm, and \kbd{gcmp} as the underlying comparison routine12022(returns the sorted vector). This routine copies all components of $x$, use12023\kbd{gen\_sort\_inplace} for a more memory-efficient function.1202412025\fun{GEN}{lexsort}{GEN x}, as \kbd{sort}, using \kbd{lexcmp} instead of12026\kbd{gcmp} as the underlying comparison routine.1202712028\fun{GEN}{vecsort}{GEN x, GEN k}, as \kbd{sort}, but sorts the12029vector \kbd{x} in ascending \emph{lexicographic} order, according to the12030entries of the \typ{VECSMALL} \kbd{k}. For example, if $\kbd{k} = [2,1,3]$,12031sorting will be done with respect to the second component, and when these12032are equal, with respect to the first, and when these are equal, with12033respect to the third.1203412035\subsec{Indirect sorting}1203612037\fun{GEN}{indexsort}{GEN x} as \kbd{sort}, but only returns the permutation12038which, applied to \kbd{x}, would sort the vector. The result is a12039\typ{VECSMALL}.1204012041\fun{GEN}{indexlexsort}{GEN x}, as \kbd{indexsort}, using \kbd{lexcmp}12042instead of \kbd{gcmp} as the underlying comparison routine.1204312044\fun{GEN}{indexvecsort}{GEN x, GEN k}, as \kbd{vecsort}, but only12045returns the permutation that would sort the vector \kbd{x}.1204612047\fun{long}{vecindexmin}{GEN x} returns the index for a maximal element of $x$12048(\typ{VEC}, \typ{COL} or \typ{VECSMALL}).1204912050\fun{long}{vecindexmax}{GEN x} returns the index for a maximal element of $x$12051(\typ{VEC}, \typ{COL} or \typ{VECSMALL}).1205212053\fun{long}{vecindexmax}{GEN x}1205412055\subsec{Generic sort and search} The following routines allow to use an12056arbitrary comparison function \kbd{int (*cmp)(void* data, GEN x, GEN y)},12057such that \kbd{cmp(data,x,y)} returns a negative result if $x12058< y$, a positive one if $x > y$ and 0 if $x = y$. The \kbd{data} argument is12059there in case your \kbd{cmp} requires additional context.1206012061\fun{GEN}{gen_sort}{GEN x, void *data, int (*cmp)(void *,GEN,GEN)}, as12062\kbd{sort}, with an explicit comparison routine.1206312064\fun{GEN}{gen_sort_shallow}{GEN x, void *data, int (*cmp)(void *,GEN,GEN)},12065shallow variant of \kbd{gen\_sort}.1206612067\fun{GEN}{gen_sort_uniq}{GEN x, void *data, int (*cmp)(void *,GEN,GEN)}, as12068\kbd{gen\_sort}, removing duplicate entries.1206912070\fun{GEN}{gen_indexsort}{GEN x, void *data, int (*cmp)(void*,GEN,GEN)},12071as \kbd{indexsort}.1207212073\fun{GEN}{gen_indexsort_uniq}{GEN x, void *data, int (*cmp)(void*,GEN,GEN)},12074as \kbd{indexsort}, removing duplicate entries.1207512076\fun{void}{gen_sort_inplace}{GEN x, void *data, int (*cmp)(void*,GEN,GEN), GEN12077*perm} sort \kbd{x} in place, without copying its components. If12078\kbd{perm} is not \kbd{NULL}, it is set to the permutation that would sort12079the original \kbd{x}.1208012081\fun{GEN}{gen_setminus}{GEN A, GEN B, int (*cmp)(GEN,GEN)} given two sorted12082vectors $A$ and $B$, returns the vector of elements of $A$ not belonging to12083$B$.1208412085\fun{GEN}{sort_factor}{GEN y, void *data, int (*cmp)(void *,GEN,GEN)}:12086assuming \kbd{y} is a factorization matrix, sorts its rows in place (no copy12087is made) according to the comparison function \kbd{cmp} applied to its first12088column.1208912090\fun{GEN}{merge_sort_uniq}{GEN x,GEN y, void *data, int (*cmp)(void *,GEN,GEN)}12091assuming \kbd{x} and \kbd{y} are sorted vectors, with respect to the \kbd{cmp}12092comparison function, return a sorted concatenation, with duplicates removed.12093Shallow function.1209412095\fun{GEN}{setunion_i}{GEN x,GEN y} shallow version of \kbd{setunion}, a12096simple alias for12097\bprog12098merge_sort_uniq(x,y, (void*)cmp_universal, cmp_nodata)12099@eprog1210012101\fun{GEN}{merge_factor}{GEN fx, GEN fy, void *data, int (*cmp)(void *,GEN,GEN)}12102let \kbd{fx} and \kbd{fy} be factorization matrices for $X$ and $Y$12103sorted with respect to the comparison function \kbd{cmp} (see12104\tet{sort_factor}), returns the factorization of $X * Y$.1210512106\fun{long}{gen_search}{GEN v, GEN y, void *data, int12107(*cmp)(void*,GEN,GEN)}.\hfil\break12108Let $v$ be a vector sorted according to \kbd{cmp(data,a,b)}; look for an12109index $i$ such that $v[i]$ is equal to $y$. If $y$ is found, return $i$12110(not necessarily the first occurence in case of multisets), else return $-i$12111where $i$ is the index where $y$ should be inserted.1211212113\fun{long}{tablesearch}{GEN T, GEN x, int (*cmp)(GEN,GEN)} is a faster12114implementation for the common case \kbd{gen\_search(T,x,cmp,cmp\_nodata)}12115when we have no need to insert missing elements; return $0$ in case $x$12116is not found.1211712118\subsec{Further useful comparison functions}1211912120\fun{int}{cmp_universal}{GEN x, GEN y} a somewhat arbitrary universal12121comparison function, devoid of sensible mathematical meaning. It is12122transitive, and returns 0 if and only if \kbd{gidentical(x,y)} is true.12123Useful to sort and search vectors of arbitrary data.1212412125\fun{int}{cmp_nodata}{void *data, GEN x, GEN y}. This function is a hack12126used to pass an existing basic comparison function lacking the \kbd{data}12127argument, i.e. with prototype \kbd{int (*cmp)(GEN x, GEN y)}. Instead of12128\kbd{gen\_sort(x, NULL, cmp)} which may or may not work depending on how your12129compiler handles typecasts between incompatible function pointers, one should12130use \kbd{gen\_sort(x, (void*)cmp, cmp\_nodata)}.1213112132Here are a few basic comparison functions, to be used with \kbd{cmp\_nodata}:1213312134\fun{int}{ZV_cmp}{GEN x, GEN y} compare two \kbd{ZV}, which we assume have12135the same length (lexicographic order).1213612137\fun{int}{cmp_Flx}{GEN x, GEN y} compare two \kbd{Flx}, which we assume12138have the same main variable (lexicographic order).1213912140\fun{int}{cmp_RgX}{GEN x, GEN y} compare two polynomials, which we assume12141have the same main variable (lexicographic order). The coefficients are12142compared using \kbd{gcmp}.1214312144\fun{int}{cmp_prime_over_p}{GEN x, GEN y} compare two prime ideals, which12145we assume divide the same prime number. The comparison is ad hoc but orders12146according to increasing residue degrees.1214712148\fun{int}{cmp_prime_ideal}{GEN x, GEN y} compare two prime ideals in the same12149\var{nf}. Orders by increasing primes, breaking ties using12150\kbd{cmp\_prime\_over\_p}.1215112152\fun{int}{cmp_padic}{GEN x, GEN y} compare two \typ{PADIC} (for the same12153prime $p$).1215412155Finally a more elaborate comparison function:1215612157\fun{int}{gen_cmp_RgX}{void *data, GEN x, GEN y} compare two polynomials,12158ordering first by increasing degree, then according to the coefficient12159comparison function:12160\bprog12161int (*cmp_coeff)(GEN,GEN) = (int(*)(GEN,GEN)) data;12162@eprog1216312164\section{Divisibility, Euclidean division}1216512166\fun{GEN}{gdivexact}{GEN x, GEN y} returns the quotient $\kbd{x} / \kbd{y}$,12167assuming $\kbd{y}$ divides $\kbd{x}$. Not stack clean if $y = 1$12168(we return $x$, not a copy).1216912170\fun{int}{gdvd}{GEN x, GEN y} returns 1 (true) if \kbd{y} divides~\kbd{x},121710~otherwise.1217212173\fun{GEN}{gdiventres}{GEN x, GEN y} creates a 2-component vertical12174vector whose components are the true Euclidean quotient and remainder12175of \kbd{x} and~\kbd{y}.1217612177\fun{GEN}{gdivent[z]}{GEN x, GEN y[, GEN z]} yields the true Euclidean12178quotient of \kbd{x} and the \typ{INT} or \typ{POL}~\kbd{y}, as per12179the \kbd{\bs} GP operator.1218012181\fun{GEN}{gdiventsg}{long s, GEN y[, GEN z]}, as \kbd{gdivent}12182except that \kbd{x} is a \kbd{long}.1218312184\fun{GEN}{gdiventgs[z]}{GEN x, long s[, GEN z]}, as \kbd{gdivent}12185except that \kbd{y} is a \kbd{long}.1218612187\fun{GEN}{gmod[z]}{GEN x, GEN y[, GEN z]} yields the remainder of \kbd{x}12188modulo the \typ{INT} or \typ{POL}~\kbd{y}, as per the \kbd{\%} GP operator.12189A \typ{REAL} or \typ{FRAC} \kbd{y} is also allowed, in which case the12190remainder is the unique real $r$ such that $0 \leq r < |\kbd{y}|$ and12191$\kbd{y} = q\kbd{x} + r$ for some (in fact unique) integer $q$.1219212193\fun{GEN}{gmodsg}{long s, GEN y[, GEN z]} as \kbd{gmod}, except \kbd{x} is12194a \kbd{long}.1219512196\fun{GEN}{gmodgs}{GEN x, long s[, GEN z]} as \kbd{gmod}, except \kbd{y} is12197a \kbd{long}.1219812199\fun{GEN}{gdivmod}{GEN x, GEN y, GEN *r} If \kbd{r} is not equal to12200\kbd{NULL} or \kbd{ONLY\_REM}, creates the (false) Euclidean quotient of12201\kbd{x} and~\kbd{y}, and puts (the address of) the remainder into~\kbd{*r}.12202If \kbd{r} is equal to \kbd{NULL}, do not create the remainder, and if12203\kbd{r} is equal to \kbd{ONLY\_REM}, create and output only the remainder.12204The remainder is created after the quotient and can be disposed of12205individually with a \kbd{cgiv(r)}.1220612207\fun{GEN}{poldivrem}{GEN x, GEN y, GEN *r} same as \key{gdivmod} but12208specifically for \typ{POL}s~\kbd{x} and~\kbd{y}, not necessarily in the same12209variable. Either of \kbd{x} and \kbd{y} may also be scalars, treated as12210polynomials of degree $0$.1221112212\fun{GEN}{gdeuc}{GEN x, GEN y} creates the Euclidean quotient of the12213\typ{POL}s~\kbd{x} and~\kbd{y}. Either of \kbd{x} and \kbd{y} may also be12214scalars, treated as polynomials of degree $0$.1221512216\fun{GEN}{grem}{GEN x, GEN y} creates the Euclidean remainder of the12217\typ{POL}~\kbd{x} divided by the \typ{POL}~\kbd{y}. Either of \kbd{x} and12218\kbd{y} may also be scalars, treated as polynomials of degree $0$.122191222012221\fun{GEN}{gdivround}{GEN x, GEN y} if \kbd{x} and \kbd{y} are real12222(\typ{INT}, \typ{REAL}, \typ{FRAC}), return the rounded Euclidean quotient of12223$x$ and $y$ as per the \kbd{\bs/} GP operator. Operate componentwise if12224\kbd{x} is a \typ{COL}, \typ{VEC} or \typ{MAT}. Otherwise as \key{gdivent}.1222512226\fun{GEN}{centermod_i}{GEN x, GEN y, GEN y2}, as \kbd{centermodii},12227componentwise.1222812229\fun{GEN}{centermod}{GEN x, GEN y}, as \kbd{centermod\_i}, except that12230\kbd{y2} is computed (and left on the stack for efficiency).1223112232\fun{GEN}{ginvmod}{GEN x, GEN y} creates the inverse of \kbd{x} modulo \kbd{y}12233when it exists. \kbd{y} must be of type \typ{INT} (in which case \kbd{x} is12234of type \typ{INT}) or \typ{POL} (in which case \kbd{x} is either a scalar12235type or a \typ{POL}).1223612237\section{GCD, content and primitive part}1223812239\subsec{Generic}1224012241\fun{GEN}{resultant}{GEN x, GEN y} creates the resultant of the \typ{POL}s12242\kbd{x} and~\kbd{y} computed using Sylvester's matrix (inexact inputs), a12243modular algorithm (inputs in $\Q[X]$) or the subresultant algorithm, as12244optimized by Lazard and Ducos. Either of \kbd{x} and \kbd{y} may also be12245scalars (treated as polynomials of degree $0$)1224612247\fun{GEN}{ggcd}{GEN x, GEN y} creates the GCD of \kbd{x} and~\kbd{y}.1224812249\fun{GEN}{glcm}{GEN x, GEN y} creates the LCM of \kbd{x} and~\kbd{y}.1225012251\fun{GEN}{gbezout}{GEN x,GEN y, GEN *u,GEN *v} returns the GCD of \kbd{x}12252and~\kbd{y}, and puts (the addresses of) objects $u$ and~$v$ such that12253$u\kbd{x}+v\kbd{y}=\gcd(\kbd{x},\kbd{y})$ into \kbd{*u} and~\kbd{*v}.1225412255\fun{GEN}{subresext}{GEN x, GEN y, GEN *U, GEN *V} returns the resultant12256of \kbd{x} and~\kbd{y}, and puts (the addresses of) polynomials $u$ and~$v$12257such that $u\kbd{x}+v\kbd{y}=\text{Res}(\kbd{x},\kbd{y})$ into \kbd{*U}12258and~\kbd{*V}.1225912260\fun{GEN}{content}{GEN x} returns the GCD of all the components of~\kbd{x}.1226112262\fun{GEN}{primitive_part}{GEN x, GEN *c} sets \kbd{c} to \kbd{content(x)}12263and returns the primitive part \kbd{x} / \kbd{c}. A trivial content is set to12264\kbd{NULL}.1226512266\fun{GEN}{primpart}{GEN x} as above but the content is lost.12267(For efficiency, the content remains on the stack.)1226812269\fun{GEN}{denom_i}{GEN x} shallow version of \kbd{denom}.1227012271\fun{GEN}{numer_i}{GEN x} shallow version of \kbd{numer}.1227212273\subsec{Over the rationals}1227412275\fun{long}{Q_pval}{GEN x, GEN p} valuation at the \typ{INT} \kbd{p}12276of the \typ{INT} or \typ{FRAC}~\kbd{x}.1227712278\fun{long}{Q_lval}{GEN x, ulong p} same for \kbd{ulong} $p$.1227912280\fun{long}{Q_pvalrem}{GEN x, GEN p, GEN *r} returns the valuation $e$ at the12281\typ{INT} \kbd{p} of the \typ{INT} or \typ{FRAC}~\kbd{x}. The quotient12282$\kbd{x}/\kbd{p}^{e}$ is returned in~\kbd{*r}.1228312284\fun{long}{Q_lvalrem}{GEN x, ulong p, GEN *r} same for \kbd{ulong} $p$.1228512286\fun{GEN}{Q_abs}{GEN x} absolute value of the \typ{INT} or12287\typ{FRAC}~\kbd{x}.1228812289\fun{GEN}{Qdivii}{GEN x, GEN y}, assuming $x$ and $y$12290are both of type \typ{INT}, return the quotient $x/y$ as a \typ{INT} or12291\typ{FRAC}; marginally faster than \kbd{gdiv}.1229212293\fun{GEN}{Qdivis}{GEN x, long y}, assuming $x$12294is an \typ{INT}, return the quotient $x/y$ as a \typ{INT} or12295\typ{FRAC}; marginally faster than \kbd{gdiv}.1229612297\fun{GEN}{Qdiviu}{GEN x, ulong y}, assuming $x$12298is an \typ{INT}, return the quotient $x/y$ as a \typ{INT} or12299\typ{FRAC}; marginally faster than \kbd{gdiv}.1230012301\fun{GEN}{Q_abs_shallow}{GEN x} $x$ being a \typ{INT} or a \typ{FRAC}, returns12302a shallow copy of $|x|$, in particular returns $x$ itself when $x \geq 0$, and12303\kbd{gneg($x$)} otherwise.1230412305\fun{GEN}{Q_gcd}{GEN x, GEN y} gcd of the \typ{INT} or \typ{FRAC}~\kbd{x}12306and~\kbd{y}.12307\smallskip1230812309In the following functions, arguments belong to a $M\otimes_\Z\Q$12310for some natural $\Z$-module $M$, e.g. multivariate polynomials with integer12311coefficients (or vectors/matrices recursively built from such objects), and12312an element of $M$ is said to be \emph{integral}.12313We are interested in contents, denominators, etc. with respect to this12314canonical integral structure; in particular, contents belong to $\Q$,12315denominators to $\Z$. For instance the $\Q$-content of $(1/2)xy$ is $(1/2)$,12316and its $\Q$-denominator is $2$, whereas \kbd{content} would return $y/2$ and12317\kbd{denom}~1.1231812319\fun{GEN}{Q_content}{GEN x} the $\Q$-content of $x$.1232012321\fun{GEN}{Z_content}{GEN x} as \kbd{Q\_content} but assume that all rationals12322are in fact \typ{INT}s and return \kbd{NULL} when the content is $1$. This12323function returns as soon as the content is found to equal $1$.1232412325\fun{GEN}{Q_content_safe}{GEN x} as \kbd{Q\_content}, returning12326\kbd{NULL} when the $\Q$-content is not defined (e.g. for a \typ{REAL}12327or \typ{INTMOD} component).1232812329\fun{GEN}{Q_denom}{GEN x} the $\Q$-denominator of $x$. Shallow function.12330Raises en \kbd{e\_TYPE} error out when the notion is meaningless, e.g. for12331a \typ{REAL} or \typ{INTMOD} component.1233212333\fun{GEN}{Q_denom_safe}{GEN x} the $\Q$-denominator of $x$. Shallow function.12334Return \kbd{NULL} when the notion is meaningless.1233512336\fun{GEN}{Q_primitive_part}{GEN x, GEN *c} sets \kbd{c} to the $\Q$-content12337of \kbd{x} and returns \kbd{x / c}, which is integral.1233812339\fun{GEN}{Q_primpart}{GEN x} as above but the content is lost. (For12340efficiency, the content remains on the stack.)1234112342\fun{GEN}{vec_Q_primpart}{GEN x} as above component-wise. Applied to a12343\typ{MAT}, the result has primitive columns.1234412345\fun{GEN}{row_Q_primpart}{GEN x} as above, applied to the rows of a12346\typ{MAT}, so that the result has primitive rows. Not \kbd{gerepile}-safe.1234712348\fun{GEN}{Q_remove_denom}{GEN x, GEN *ptd} sets \kbd{d} to the12349$\Q$-denominator of \kbd{x} and returns \kbd{x * d}, which is integral.12350Shallow function.1235112352\fun{GEN}{Q_div_to_int}{GEN x, GEN c} returns \kbd{x / c}, assuming $c$12353is a rational number (\typ{INT} or \typ{FRAC}) and the result is integral.1235412355\fun{GEN}{Q_mul_to_int}{GEN x, GEN c} returns \kbd{x * c}, assuming $c$12356is a rational number (\typ{INT} or \typ{FRAC}) and the result is integral.1235712358\fun{GEN}{Q_muli_to_int}{GEN x, GEN d} returns \kbd{x * c}, assuming $c$12359is a \typ{INT} and the result is integral.1236012361\fun{GEN}{mul_content}{GEN cx, GEN cy} \kbd{cx} and \kbd{cy} are12362as set by \kbd{primitive\_part}: either a \kbd{GEN} or \kbd{NULL}12363representing the trivial content $1$. Returns their product (either a12364\kbd{GEN} or \kbd{NULL}).1236512366\fun{GEN}{div_content}{GEN cx, GEN cy} \kbd{cx} and \kbd{cy} are12367as set by \kbd{primitive\_part}: either a \kbd{GEN} or \kbd{NULL}12368representing the trivial content $1$. Returns their quotient (either a12369\kbd{GEN} or \kbd{NULL}).1237012371\fun{GEN}{inv_content}{GEN c} $c$ is as set by \kbd{primitive\_part}: either12372a \kbd{GEN} or \kbd{NULL} representing the trivial content $1$. Returns its12373inverse (either a \kbd{GEN} or \kbd{NULL}).1237412375\fun{GEN}{mul_denom}{GEN dx, GEN dy} \kbd{dx} and \kbd{dy} are12376as set by \kbd{Q\_remove\_denom}: either a \typ{INT} or \kbd{NULL} representing12377the trivial denominator $1$. Returns their product (either a \typ{INT} or12378\kbd{NULL}).1237912380\section{Generic arithmetic operators}1238112382\subsec{Unary operators}1238312384\fun{GEN}{gneg[z]}{GEN x[, GEN z]} yields $-\kbd{x}$.1238512386\fun{GEN}{gneg_i}{GEN x} shallow function yielding $-\kbd{x}$.1238712388\fun{GEN}{gabs[z]}{GEN x[, GEN z]} yields $|\kbd{x}|$.1238912390\fun{GEN}{gsqr}{GEN x} creates the square of~\kbd{x}.1239112392\fun{GEN}{ginv}{GEN x} creates the inverse of~\kbd{x}.1239312394\subsec{Binary operators}1239512396Let ``\op'' be a binary operation among1239712398\op=\key{add}: addition (\kbd{x + y}).1239912400\op=\key{sub}: subtraction (\kbd{x - y}).1240112402\op=\key{mul}: multiplication (\kbd{x * y}).1240312404\op=\key{div}: division (\kbd{x / y}).1240512406\noindent The names and prototypes of the functions corresponding12407to \op\ are as follows:1240812409\funno{GEN}{g\op}{GEN x, GEN y}1241012411\funno{GEN}{g\op gs}{GEN x, long s}1241212413\funno{GEN}{g\op sg}{long s, GEN y}1241412415\noindent Explicitly1241612417\fun{GEN}{gadd}{GEN x, GEN y}, \fun{GEN}{gaddgs}{GEN x, long s},12418\fun{GEN}{gaddsg}{long s, GEN x}1241912420\fun{GEN}{gmul}{GEN x, GEN y}, \fun{GEN}{gmulgs}{GEN x, long s},12421\fun{GEN}{gmulsg}{long s, GEN x}1242212423\fun{GEN}{gsub}{GEN x, GEN y}, \fun{GEN}{gsubgs}{GEN x, long s},12424\fun{GEN}{gsubsg}{long s, GEN x}1242512426\fun{GEN}{gdiv}{GEN x, GEN y}, \fun{GEN}{gdivgs}{GEN x, long s},12427\fun{GEN}{gdivsg}{long s, GEN x}124281242912430\fun{GEN}{gpow}{GEN x, GEN y, long l} creates $\kbd{x}^{\kbd{y}}$. If12431\kbd{y} is a \typ{INT}, return \kbd{powgi(x,y)} (the precision \kbd{l} is not12432taken into account). Otherwise, the result is $\exp(\kbd{y}*\log(\kbd{x}))$12433where exact arguments are converted to floats of precision~\kbd{l} in case of12434need; if there is no need, for instance if $x$ is a \typ{REAL}, $l$ is12435ignored. Indeed, if $x$ is a \typ{REAL}, the accuracy of $\log x$ is12436determined from the accuracy of $x$, it is no problem to multiply by $y$,12437even if it is an exact type, and the accuracy of the exponential is12438determined, exactly as in the case of the initial $\log x$.1243912440\fun{GEN}{gpowgs}{GEN x, long n} creates $\kbd{x}^{\kbd{n}}$ using12441binary powering. To treat the special case $n = 0$, we consider12442\kbd{gpowgs} as a series of \kbd{gmul}, so we follow the rule of returning12443result which is as exact as possible given the input. More precisely,12444we return1244512446\item \kbd{gen\_1} if $x$ has type \typ{INT}, \typ{REAL}, \typ{FRAC}, or12447\typ{PADIC}1244812449\item \kbd{Mod(1,N)} if $x$ is a \typ{INTMOD} modulo $N$.1245012451\item \kbd{gen\_1} for \typ{COMPLEX}, \typ{QUAD} unless one component12452is a \typ{INTMOD}, in which case we return \kbd{Mod(1, N)} for a suitable12453$N$ (the gcd of the moduli that appear).1245412455\item \kbd{FF\_1}$(x)$ for a \typ{FFELT}.1245612457\item \kbd{qfb\_1}$(x)$ for \typ{QFB}.1245812459\item the identity permutation for \typ{VECSMALL}.1246012461\item \kbd{Rg\_get\_1}$(x)$ otherwise1246212463Of course, the only practical use of this routine for $n = 0$ is12464to obtain the multiplicative neutral element in the base ring (or to treat12465marginal cases that should be special cased anyway if there is the slightest12466doubt about what the result should be).1246712468\fun{GEN}{powgi}{GEN x, GEN y} creates $\kbd{x}^{\kbd{y}}$, where \kbd{y} is a12469\typ{INT}, using left-shift binary powering. The case where $y = 0$12470(as all cases where $y$ is small) is handled by \kbd{gpowgs(x, 0)}.1247112472\fun{GEN}{gpowers}{GEN x, long n} returns the vector $[1,x,\dots,x^n]$.1247312474\fun{GEN}{grootsof1}{long n, long prec} returns the vector12475$[1,x,\dots,x^{n-1}]$, where $x$ is the $n$-th root of unity $\exp(2i\pi/n)$.1247612477\fun{GEN}{gsqrpowers}{GEN x, long n} returns the vector $[x,x^4,\dots,x^{n^2}]$.1247812479In addition we also have the obsolete forms:1248012481\fun{void}{gaddz}{GEN x, GEN y, GEN z}1248212483\fun{void}{gsubz}{GEN x, GEN y, GEN z}1248412485\fun{void}{gmulz}{GEN x, GEN y, GEN z}1248612487\fun{void}{gdivz}{GEN x, GEN y, GEN z}1248812489\section{Generic operators: product, powering, factorback}1249012491To describe the following functions, we use the following private typedefs12492to simplify the description:12493\bprog12494typedef (*F0)(void *);12495typedef (*F1)(void *, GEN);12496typedef (*F2)(void *, GEN, GEN);12497@eprog12498\noindent They correspond to generic functions with one and two arguments12499respectively (the \kbd{void*} argument provides some arbitrary evaluation12500context).1250112502\fun{GEN}{gen_product}{GEN v, void *D, F2 op}12503Given two objects $x,y$, assume that \kbd{op(D, $x$, $y$)} implements an12504associative binary operator. If $v$ has $k$ entries, return12505$$v[1]~\var{op}~v[2]~\var{op}~\ldots ~\var{op}~v[k];$$12506returns \kbd{gen\_1} if $k = 0$ and a copy of $v[1]$ if $k = 1$.12507Use divide and conquer strategy. Leave some garbage on stack, but suitable for12508\kbd{gerepileupto} if \kbd{mul} is.1250912510\fun{GEN}{gen_pow}{GEN x, GEN n, void *D, F1 sqr, F2 mul} $n > 0$ a12511\typ{INT}, returns $x^n$; \kbd{mul(D, $x$, $y$)} implements the multiplication12512in the underlying monoid; \kbd{sqr} is a (presumably optimized) shortcut for12513\kbd{mul(D, $x$, $x$)}.1251412515\fun{GEN}{gen_powu}{GEN x, ulong n, void *D, F1 sqr, F2 mul} $n > 0$,12516returns $x^n$. See \tet{gen_pow}.1251712518\fun{GEN}{gen_pow_i}{GEN x, GEN n, void *E, F1 sqr, F2 mul}12519internal variant of \tet{gen_pow}, not memory-clean.1252012521\fun{GEN}{gen_powu_i}{GEN x, ulong n, void *E, F1 sqr, F2 mul}12522internal variant of \tet{gen_powu}, not memory-clean.1252312524\fun{GEN}{gen_pow_fold}{GEN x, GEN n, void *D, F1 sqr, F1 msqr} variant12525of \tet{gen_pow}, where \kbd{mul} is replaced by \kbd{msqr}, with12526\kbd{msqr(D, $y$)} returning $xy^2$. In particular \kbd{D} must implicitly12527contain $x$.1252812529\fun{GEN}{gen_pow_fold_i}{GEN x, GEN n, void *E, F1 sqr, F1 msqr}12530internal variant of the function \tet{gen_pow_fold}, not memory-clean.1253112532\fun{GEN}{gen_powu_fold}{GEN x, ulong n, void *D, F1 sqr, F1 msqr}, see12533\tet{gen_pow_fold}.1253412535\fun{GEN}{gen_powu_fold_i}{GEN x, ulong n, void *E, F1 sqr, F1 msqr}12536see \tet{gen_pow_fold_i}.1253712538\fun{GEN}{gen_pow_init}{GEN x, GEN n, long k, void *E, GEN (*sqr)(void*,GEN),12539GEN (*mul)(void*,GEN,GEN)}12540Return a table \kbd{R} that can be used with12541\kbd{gen\_pow\_table} to compute the powers of $x$ up to $n$.12542The table is of size $2^k\*\log_2(n)$.1254312544\fun{GEN}{gen_pow_table}{GEN R, GEN n, void *E, GEN (*one)(void*),12545GEN (*mul)(void*,GEN,GEN)}1254612547Return $x^n$, where $R$ is as given by \kbd{gen\_pow\_init(x,m,k,E,sqr,mul)}12548for some integer $m\geq n$.1254912550\fun{GEN}{gen_powers}{GEN x, long n, long usesqr, void *D, F1 sqr, F2 mul, F0 one}12551returns $[\kbd{x}^0, \dots, \kbd{x}^\kbd{n}]$ as a \typ{VEC}; \kbd{mul(D,12552$x$, $y$)} implements the multiplication in the underlying monoid; \kbd{sqr}12553is a (presumably optimized) shortcut for \kbd{mul(D, $x$, $x$)}; \kbd{one}12554returns the monoid unit. The flag \kbd{usesqr} should be set to $1$ if12555squaring are faster than multiplication by $x$.1255612557\fun{GEN}{gen_factorback}{GEN L, GEN e, void *D, F2 mul, F2 pow, GEN (*one)(void *)D}12558generic form of \tet{factorback}. The pair $[L,e]$ is of the form1255912560\item \kbd{[fa, NULL]}, \kbd{fa} a two-column factorization matrix: expand it.1256112562\item \kbd{[v, NULL]}, $v$ a vector of objects: return their product.1256312564\item or \kbd{[v, e]}, $v$ a vector of objects, $e$ a vector of integral12565exponents (a \kbd{ZV} or \kbd{zv}): return the product of the $v[i]^{e[i]}$.1256612567\noindent \kbd{mul(D, $x$, $y$)} and \kbd{pow(D, $x$, $n$)}12568return $xy$ and $x^n$ respectively.1256912570\noindent \kbd{one(D)} returns the neutral element. If \kbd{one} is \kbd{NULL},12571\kbd{gen\_1} is used instead.1257212573\section{Matrix and polynomial norms} This section concerns only standard norms12574of $\R$ and $\C$ vector spaces, not algebraic norms given by the determinant of12575some multiplication operator. We have already seen type-specific functions like12576\tet{ZM_supnorm} or \tet{RgM_fpnorml2} and limit ourselves to generic functions12577assuming nothing about their \kbd{GEN} argument; these functions allow12578the following scalar types: \typ{INT}, \typ{FRAC}, \typ{REAL}, \typ{COMPLEX},12579\typ{QUAD} and are defined recursively (in terms of norms of their components)12580for the following ``container'' types: \typ{POL}, \typ{VEC}, \typ{COL} and12581\typ{MAT}. They raise an error if some other type appears in the argument.1258212583\fun{GEN}{gnorml2}{GEN x} The norm of a scalar is the square of its complex12584modulus, the norm of a recursive type is the sum of the norms of its components.12585For polynomials, vectors or matrices of complex numbers one recovers the12586\emph{square} of the usual $L^2$ norm. In most applications, the missing square12587root computation can be skipped.1258812589\fun{GEN}{gnorml1}{GEN x, long prec} The norm of a scalar is its complex12590modulus, the norm of a recursive type is the sum of the norms of its components.12591For polynomials, vectors or matrices of complex numbers one recovers12592the usual $L^1$ norm. One must include a real precision \kbd{prec} in case12593the inputs include \typ{COMPLEX} or \typ{QUAD} with exact rational components:12594a square root must be computed and we must choose an accuracy.1259512596\fun{GEN}{gnorml1_fake}{GEN x} as \tet{gnorml1}, except that the norm12597of a \typ{QUAD} $x + wy$ or \typ{COMPLEX} $x + Iy$ is defined as12598$|x| + |y|$, where we use the ordinary real absolute value. This is still a norm12599of $\R$ vector spaces, which is easier to compute than12600\kbd{gnorml1} and can often be used in its place.1260112602\fun{GEN}{gsupnorm}{GEN x, long prec} The norm of a scalar is its complex12603modulus, the norm of a recursive type is the max of the norms of its12604components. A precision \kbd{prec} must be included for the same reason as in12605\kbd{gnorml1}.1260612607\fun{void}{gsupnorm_aux}{GEN x, GEN *m, GEN *m2, long prec}12608is the low-level function underlying12609\kbd{gsupnorm}, used as follows:12610\bprog12611GEN m = NULL, m2 = NULL;12612gsupnorm_aux(x, &m, &m2);12613@eprog12614After the call, the sup norm of $x$ is the min of \kbd{m} and the square root12615of \kbd{m2}; one or both of \kbd{m}, \kbd{m2} may be \kbd{NULL}, in12616which case it must be omitted. You may initially set \kbd{m} and \kbd{m2} to12617non-\kbd{NULL} values, in which case, the above procedure yields the max of12618(the initial) \kbd{m}, the square root of (the initial) \kbd{m2}, and the sup12619norm of $x$.1262012621The strange interface is due to the fact that $|z|^2$ is easier to compute12622than $|z|$ for a \typ{QUAD} or \typ{COMPLEX} $z$: \kbd{m2} is the max of12623those $|z|^2$, and \kbd{m} is the max of the other $|z|$.1262412625\section{Substitution and evaluation}1262612627\fun{GEN}{gsubst}{GEN x, long v, GEN y} substitutes the object \kbd{y}12628into~\kbd{x} for the variable number~\kbd{v}.1262912630\fun{GEN}{poleval}{GEN q, GEN x} evaluates the \typ{POL} or \typ{RFRAC}12631$q$ at $x$. For convenience, a \typ{VEC} or \typ{COL} is also recognized as12632the \typ{POL} \kbd{gtovecrev(q)}.1263312634\fun{GEN}{RgX_cxeval}{GEN T, GEN x, GEN xi} evaluate the \typ{POL} $T$12635at $x$ via Horner's scheme. If \var{xi} is not \kbd{NULL} it must be equal to12636$1/x$ and we evaluate $x^{\deg T}T(1/x)$ instead. This is useful when12637$|x| > 1$ is a \typ{REAL} or an inexact \typ{COMPLEX} and $T$ has12638``balanced'' coefficients, since the evaluation becomes numerically stable.1263912640\fun{GEN}{RgX_RgM_eval}{GEN q, GEN x} evaluates the \typ{POL} $q$ at the12641square matrix $x$.1264212643\fun{GEN}{RgX_RgMV_eval}{GEN f, GEN V} returns12644the evaluation $\kbd{f}(\kbd{x})$, assuming that \kbd{V} was computed by12645$\kbd{FpXQ\_powers}(\kbd{x}, n)$ for some $n>1$.1264612647\fun{GEN}{qfeval}{GEN q, GEN x} evaluates the quadratic form12648$q$ (symmetric matrix) at $x$ (column vector of compatible dimensions).1264912650\fun{GEN}{qfevalb}{GEN q, GEN x, GEN y} evaluates the polar bilinear form12651attached to the quadratic form $q$ (symmetric matrix) at $x$, $y$ (column12652vectors of compatible dimensions).1265312654\fun{GEN}{hqfeval}{GEN q, GEN x} evaluates the Hermitian form $q$12655(a Hermitian complex matrix) at $x$.1265612657\fun{GEN}{qf_apply_RgM}{GEN q, GEN M} $q$ is a symmetric $n\times n$ matrix,12658$M$ an $n\times k$ matrix, return $M' q M$.1265912660\fun{GEN}{qf_apply_ZM}{GEN q, GEN M} as above assuming that both12661$q$ and $M$ have integer entries.1266212663\newpage12664\chapter{Miscellaneous mathematical functions}1266512666\section{Fractions}1266712668\fun{GEN}{absfrac}{GEN x} returns the absolute value of the \typ{FRAC} $x$.1266912670\fun{GEN}{absfrac_shallow}{GEN x} $x$ being a \typ{FRAC}, returns a shallow12671copy of $|x|$, in particular returns $x$ itself when $x \geq 0$, and12672\kbd{gneg($x$)} otherwise.1267312674\fun{GEN}{sqrfrac}{GEN x} returns the square of the \typ{FRAC} $x$.1267512676\section{Binomials}1267712678\fun{GEN}{binomial}{GEN x, long k}1267912680\fun{GEN}{binomialuu}{ulong n, ulong k}1268112682\fun{GEN}{vecbinomial}{long n}, which returns a vector $v$ with $n+1$12683\typ{INT} components such that $v[k+1] = \kbd{binomial}(n,k)$ for $k$ from12684$0$ up to $n$.1268512686\section{Real numbers}1268712688\fun{GEN}{R_abs}{GEN x} $x$ being a \typ{INT}, a \typ{REAL} or a12689\typ{FRAC}, returns $|x|$.1269012691\fun{GEN}{R_abs_shallow}{GEN x} $x$ being a \typ{INT}, a \typ{REAL} or a12692\typ{FRAC}, returns a shallow copy of $|x|$, in particular returns $x$ itself12693when $x \geq 0$, and \kbd{gneg($x$)} otherwise.1269412695\fun{GEN}{modRr_safe}{GEN x, GEN y} let $x$ be a \typ{INT}, a \typ{REAL} or12696\typ{FRAC} and let $y$ be a \typ{REAL}. Return $x\% y$ unless the input12697accuracy is unsufficient to compute the floor or $x/y$ in which case we12698return \kbd{NULL}.1269912700\section{Complex numbers}1270112702\fun{GEN}{gimag}{GEN x} returns a copy of the imaginary part of \kbd{x}.1270312704\fun{GEN}{greal}{GEN x} returns a copy of the real part of \kbd{x}. If \kbd{x}12705is a \typ{QUAD}, returns the coefficient of $1$ in the ``canonical'' integral12706basis $(1,\omega)$.1270712708\fun{GEN}{gconj}{GEN x} returns $\kbd{greal}(x) - 2\kbd{gimag}(x)$, which is12709the ordinary complex conjugate except for a real \typ{QUAD}.1271012711\fun{GEN}{imag_i}{GEN x}, shallow variant of \kbd{gimag}.1271212713\fun{GEN}{real_i}{GEN x}, shallow variant of \kbd{greal}.1271412715\fun{GEN}{conj_i}{GEN x}, shallow variant of \kbd{gconj}.1271612717\fun{GEN}{mulreal}{GEN x, GEN y} returns the real part of $x\*y$;12718$x$, $y$ have type \typ{INT}, \typ{FRAC}, \typ{REAL} or \typ{COMPLEX}. See also12719\kbd{RgM\_mulreal}.1272012721\fun{GEN}{cxnorm}{GEN x} norm of the \typ{COMPLEX} $x$ (modulus squared).1272212723\fun{GEN}{cxexpm1}{GEN x} returns $\exp(x)-1$, for a \typ{COMPLEX} $x$.1272412725\fun{int}{cx_approx_equal}{GEN a, GEN b} test whether (\typ{INT}, \typ{FRAC},12726\typ{REAL}, or \typ{COMPLEX} of those) $a$ and $b$ are approximately equal.12727This returns $1$ if and only if the division by $a-b$ would produce a12728division by $0$ (which is a less stringent test than testing whether $a-b$12729evaluates to $0$).1273012731\section{Quadratic numbers and binary quadratic forms}1273212733\fun{GEN}{quad_disc}{GEN x} returns the discriminant of the \typ{QUAD} $x$.12734Not stack-clean but suitable for \kbd{gerepileupto}.1273512736\fun{GEN}{quadnorm}{GEN x} norm of the \typ{QUAD} $x$.1273712738\fun{GEN}{qfb_disc}{GEN x} returns the discriminant of the \typ{QFB}12739\kbd{x}.1274012741\fun{GEN}{qfb_disc3}{GEN x, GEN y, GEN z} returns $y^2 - 4xz$ assuming all12742inputs are \typ{INT}s. Not stack-clean.1274312744\fun{GEN}{qfb_apply_ZM}{GEN q, GEN g} returns $q \circ g$.1274512746\fun{GEN}{qfbforms}{GEN D} given a discriminant $D < 0$, return the list12747of reduced forms of discriminant $D$ as \typ{VECSMALL} with 3 components.12748The primitive forms in the list enumerate the class group of the quadratic12749order of discriminant $D$; if $D$ is fundamental, all returned forms12750are automatically primitive.1275112752\section{Polynomials}\label{se:polynomials}1275312754\fun{GEN}{truecoef}{GEN x, long n} returns \kbd{polcoef(x,n, -1)}, i.e.12755the coefficient of the term of degree \kbd{n} in the main variable. This is12756a safe but expensive function that must \emph{copy} its return value so that12757it be \kbd{gerepile}-safe. Use \kbd{polcoef\_i} for a fast internal variant.1275812759\fun{GEN}{polcoef_i}{GEN x, long n, long v} internal shallow function. Rewrite12760$x$ as a Laurent polynomial in the variable $v$ and returns its coefficient12761of degree $n$ (\kbd{gen\_0} if this falls outside the coefficient array).12762Allow \typ{POL}, \typ{SER}, \typ{RFRAC} and scalars.1276312764\fun{long}{degree}{GEN x} returns \kbd{poldegree(x, -1)}, the degree of12765\kbd{x} with respect to its main variable, with the usual meaning if the12766leading coefficient of $x$ is nonzero. If the sign of $x$ is $0$, this12767function always returns $-1$. Otherwise, we return the index of the leading12768coefficient of $x$, i.e. the coefficient of largest index stored in $x$.12769For instance the ``degrees'' of12770\bprog127710. E-38 * x^4 + 0.E-19 * x + 112772Mod(0,2) * x^0 \\ sign is 0 !12773@eprog\noindent are $4$ and $-1$ respectively.1277412775\fun{long}{degpol}{GEN x} is a simple macro returning \kbd{lg(x) - 3}.12776This is the degree of the \typ{POL}~\kbd{x} with respect to its main12777variable, \emph{if} its leading coefficient is nonzero (a rational $0$ is12778impossible, but an inexact $0$ is allowed, as well as an exact modular $0$,12779e.g. \kbd{Mod(0,2)}). If $x$ has no coefficients (rational $0$ polynomial),12780its length is $2$ and we return the expected $-1$.1278112782\fun{GEN}{characteristic}{GEN x} returns the characteristic of the12783base ring over which the polynomial is defined (as defined by \typ{INTMOD}12784and \typ{FFELT} components). The function raises an exception if incompatible12785primes arise from \typ{FFELT} and \typ{PADIC} components. Shallow function.1278612787\fun{GEN}{residual_characteristic}{GEN x} returns a kind of ``residual12788characteristic'' of the base ring over which the polynomial is defined. This12789is defined as the gcd of all moduli \typ{INTMOD}s occurring in the structure,12790as well as primes $p$ arising from \typ{PADIC}s or \typ{FFELT}s. The function12791raises an exception if incompatible primes arise from \typ{FFELT} and12792\typ{PADIC} components. Shallow function.1279312794\fun{GEN}{resultant}{GEN x,GEN y} resultant of \kbd{x} and \kbd{y}, with12795respect to the main variable of highest priority. Uses either the12796subresultant algorithm (generic case), a modular algorithm (inputs in12797$\Q[X]$) or Sylvester's matrix (inexact inputs).1279812799\fun{GEN}{resultant2}{GEN x, GEN y} resultant of \kbd{x} and \kbd{y}, with12800respect to the main variable of highest priority. Computes the determinant12801of Sylvester's matrix.1280212803\fun{GEN}{cleanroots}{GEN x, long prec} returns the complex roots of12804the complex polynomial $x$ (with coefficients \typ{INT}, \typ{FRAC},12805\typ{REAL} or \typ{COMPLEX} of the above). The roots are returned12806as \typ{REAL} or \typ{COMPLEX} of \typ{REAL}s of precision \kbd{prec}12807(guaranteeing a nonzero imaginary part). See \tet{QX_complex_roots}.1280812809\fun{double}{fujiwara_bound}{GEN x} return a quick upper bound for the12810logarithm in base $2$ of the modulus of the largest complex roots of12811the polynomial $x$ (complex coefficients).1281212813\fun{double}{fujiwara_bound_real}{GEN x, long sign} return a quick upper12814bound for the logarithm in base $2$ of the absolute value of the largest12815real root of sign \var{sign} ($1$ or $-1$), for the polynomial $x$ (real12816coefficients).1281712818\fun{GEN}{polmod_to_embed}{GEN x, long prec} return the vector of complex12819embeddings of the \typ{POLMOD} $x$ (with complex coefficients). Shallow12820function, simple complex variant of \tet{conjvec}.1282112822\fun{GEN}{pollegendre_reduced}{long n, long v} let $P_n(t)\in \Q[t]$12823be the $n$-th Legendre polynomial in variable $v$. Return $p\in \Z[t]$12824such that $2^n P_n(t) = p(t^2)$ ($n$ even) or $t p(t^2)$ ($n$ odd).1282512826\section{Power series}1282712828\fun{GEN}{sertoser}{GEN x, long prec} return the \typ{SER} $x$ truncated12829or extended (with zeros) to \kbd{prec} terms. Shallow function, assume12830that $\kbd{prec} \geq 0$.1283112832\fun{GEN}{derivser}{GEN x} returns the derivative of the \typ{SER} \kbd{x}12833with respect to its main variable.1283412835\fun{GEN}{integser}{GEN x} returns the primitive of the \typ{SER} \kbd{x}12836with respect to its main variable.1283712838\fun{GEN}{truecoef}{GEN x, long n} returns \kbd{polcoef(x,n, -1)}, i.e.12839the coefficient of the term of degree \kbd{n} in the main variable. This is a12840safe but expensive function that must \emph{copy} its return value so that it12841be \kbd{gerepile}-safe. Use \kbd{polcoef\_i} for a fast internal variant.1284212843\fun{GEN}{ser_unscale}{GEN P, GEN h} return $P(h x)$, not memory clean.1284412845\fun{GEN}{ser_normalize}{GEN x} divide $x$ by its ``leading term'' so that12846the series is either $0$ or equal to $t^v(1+O(t))$. Shallow function if the12847``leading term'' is $1$.1284812849\fun{int}{ser_isexactzero}{GEN x} return $1$ if $x$ is a zero series, all12850of whose known coefficients are exact zeroes; this implies that12851$\kbd{sign}(x) = 0$ and $\kbd{lg}(x) \leq 3$.1285212853\fun{GEN}{ser_inv}{GEN x} return the inverse of the \typ{SER} $x$ using12854Newton iteration. This is in general slower than \kbd{ginv} unless the12855precision is huge (hundreds of terms, where the threshold depends strongly12856on the base field).1285712858\fun{GEN}{psi1series}{long n, long v, long prec} creates the \typ{SER}12859$\psi(1 + x + O(x^n))$ in variable $v$.1286012861\section{Functions to handle \typ{FFELT}}12862These functions define the public interface of the \typ{FFELT} type to use in12863generic functions. However, in specific functions, it is better to use the12864functions class \kbd{FpXQ} and/or \kbd{Flxq} as appropriate.1286512866\fun{GEN}{FF_p}{GEN a} returns the characteristic of the definition field of the12867\typ{FFELT} element \kbd{a}.1286812869\fun{long}{FF_f}{GEN a} returns the dimension of the definition field over12870its prime field; the cardinality of the dimension field is thus $p^f$.1287112872\fun{GEN}{FF_p_i}{GEN a} shallow version of \kbd{FF\_p}.1287312874\fun{GEN}{FF_q}{GEN a} returns the cardinality of the definition field of the12875\typ{FFELT} element \kbd{a}.1287612877\fun{GEN}{FF_mod}{GEN a} returns the polynomial (with reduced \typ{INT}12878coefficients) defining the finite field, in the variable used to display $a$.1287912880\fun{long}{FF_var}{GEN a} returns the variable used to display $a$.1288112882\fun{GEN}{FF_gen}{GEN a} returns the standard generator of the definition field12883of the \typ{FFELT} element \kbd{a}, see \kbd{ffgen}, that is $x\pmod{T}$ where12884$T$ is the polynomial over the prime field that define the finite field.1288512886\fun{GEN}{FF_to_FpXQ}{GEN a} converts the \typ{FFELT} \kbd{a} to a polynomial12887$P$ with reduced \typ{INT} coefficients such that $a=P(g)$ where $g$ is the12888generator of the finite field returned by \kbd{ffgen}, in the variable used to12889display $g$.1289012891\fun{GEN}{FF_to_FpXQ_i}{GEN a} shallow version of \kbd{FF\_to\_FpXQ}.1289212893\fun{GEN}{FF_to_F2xq}{GEN a} converts the \typ{FFELT} \kbd{a} to a \kbd{F2x}12894$P$ such that $a=P(g)$ where $g$ is the generator of the finite field returned12895by \kbd{ffgen}, in the variable used to display $g$. This only work if the12896characteristic is $2$.1289712898\fun{GEN}{FF_to_F2xq_i}{GEN a} shallow version of \kbd{FF\_to\_F2xq}.1289912900\fun{GEN}{FF_to_Flxq}{GEN a} converts the \typ{FFELT} \kbd{a} to a \kbd{Flx}12901$P$ such that $a=P(g)$ where $g$ is the generator of the finite field returned12902by \kbd{ffgen}, in the variable used to display $g$. This only work if the12903characteristic is small enough.1290412905\fun{GEN}{FF_to_Flxq_i}{GEN a} shallow version of \kbd{FF\_to\_Flxq}.1290612907\fun{GEN}{p_to_FF}{GEN p, long v} returns a \typ{FFELT} equal to $1$ in the12908finite field $\Z/p\Z$. Useful for generic code that wants to handle12909(inefficiently) $\Z/p\Z$ as if it were not a prime field.1291012911\fun{GEN}{Tp_to_FF}{GEN T, GEN p} returns a \typ{FFELT} equal to $1$ in the12912finite field $\F_p/(T)$, where $T$ is a \kbd{ZX}, assumed to be irreducible12913modulo $p$, or \kbd{NULL} in which case the routine acts as \tet{p_to_FF(p,0)}.12914No checks.1291512916\fun{GEN}{Fq_to_FF}{GEN x, GEN ff} returns a \typ{FFELT} equal to $x$12917in the finite field defined by the \typ{FFELT} \kbd{ff}, where12918$x$ is an \kbd{Fq} (either a \typ{INT} or a \kbd{ZX}: a \typ{POL} with12919\typ{INT} coefficients). No checks.1292012921\fun{GEN}{FqX_to_FFX}{GEN x, GEN ff} given an \kbd{FqX} $x$,12922return the polynomial with \typ{FFELT} coefficients obtained by12923applying \tet{Fq_to_FF} coefficientwise. No checks, and no normalization12924if the leading coefficient maps to $0$.1292512926\fun{GEN}{FF_1}{GEN a} returns the unity in the definition field of the12927\typ{FFELT} element \kbd{a}.1292812929\fun{GEN}{FF_zero}{GEN a} returns the zero element of the definition field of12930the \typ{FFELT} element \kbd{a}.1293112932\fun{int}{FF_equal0}{GEN a} returns $1$ if the \typ{FFELT} \kbd{a} is equal12933to $0$ else returns $0$.1293412935\fun{int}{FF_equal1}{GEN a} returns $1$ if the \typ{FFELT} \kbd{a} is equal12936to $1$ else returns $0$.1293712938\fun{int}{FF_equalm1}{GEN a} returns $-1$ if the \typ{FFELT} \kbd{a} is equal12939to $1$ else returns $0$.1294012941\fun{int}{FF_equal}{GEN a, GEN b} return $1$ if the \typ{FFELT} \kbd{a} and12942\kbd{b} have the same definition field and are equal, else $0$.1294312944\fun{int}{FF_samefield}{GEN a, GEN b} return $1$ if the \typ{FFELT} \kbd{a} and12945\kbd{b} have the same definition field, else $0$.1294612947\fun{int}{Rg_is_FF}{GEN c, GEN *ff} to be called successively on many objects,12948setting \kbd{*ff = NULL} (unset) initially. Returns $1$ as long as $c$ is a12949\typ{FFELT} defined over the same field as \kbd{*ff} (setting \kbd{*ff = c}12950if unset), and $0$ otherwise.1295112952\fun{int}{RgC_is_FFC}{GEN x, GEN *ff} apply \tet{Rg_is_FF} successively to all12953components of the \typ{VEC} or \typ{COL} $x$. Return $0$ if one call fails,12954and $1$ otherwise.1295512956\fun{int}{RgM_is_FFM}{GEN x, GEN *ff} apply \tet{Rg_is_FF} to all components12957of the \typ{MAT}. Return $0$ if one call fails, and $1$ otherwise.1295812959\fun{GEN}{FF_add}{GEN a, GEN b} returns $a+b$ where \kbd{a} and \kbd{b} are12960\typ{FFELT} having the same definition field.1296112962\fun{GEN}{FF_Z_add}{GEN a, GEN x} returns $a+x$, where \kbd{a} is a12963\typ{FFELT}, and \kbd{x} is a \typ{INT}, the computation being12964performed in the definition field of \kbd{a}.1296512966\fun{GEN}{FF_Q_add}{GEN a, GEN x} returns $a+x$, where \kbd{a} is a12967\typ{FFELT}, and \kbd{x} is a \typ{RFRAC}, the computation being12968performed in the definition field of \kbd{a}.1296912970\fun{GEN}{FF_sub}{GEN a, GEN b} returns $a-b$ where \kbd{a} and \kbd{b} are12971\typ{FFELT} having the same definition field.1297212973\fun{GEN}{FF_mul}{GEN a, GEN b} returns $a\*b$ where \kbd{a} and \kbd{b} are12974\typ{FFELT} having the same definition field.1297512976\fun{GEN}{FF_Z_mul}{GEN a, GEN b} returns $a\*b$, where \kbd{a} is a12977\typ{FFELT}, and \kbd{b} is a \typ{INT}, the computation being12978performed in the definition field of \kbd{a}.1297912980\fun{GEN}{FF_div}{GEN a, GEN b} returns $a/b$ where \kbd{a} and \kbd{b} are12981\typ{FFELT} having the same definition field.1298212983\fun{GEN}{FF_neg}{GEN a} returns $-a$ where \kbd{a} is a \typ{FFELT}.1298412985\fun{GEN}{FF_neg_i}{GEN a} shallow function returning $-a$ where \kbd{a} is a12986\typ{FFELT}.1298712988\fun{GEN}{FF_inv}{GEN a} returns $a^{-1}$ where \kbd{a} is a \typ{FFELT}.1298912990\fun{GEN}{FF_sqr}{GEN a} returns $a^2$ where \kbd{a} is a \typ{FFELT}.1299112992\fun{GEN}{FF_mul2n}{GEN a, long n} returns $a\*2^n$ where \kbd{a} is a12993\typ{FFELT}.1299412995\fun{GEN}{FF_pow}{GEN a, GEN n} returns $a^n$ where \kbd{a} is a \typ{FFELT}12996and \kbd{n} is a \typ{INT}.1299712998\fun{GEN}{FF_Frobenius}{GEN a, GEN n} returns $x^{p^n}$ where \kbd{x} is12999the standard generator of the definition field of the \typ{FFELT} element13000\kbd{a}, \typ{FFELT}, \kbd{n} is a \typ{INT}, and $p$ is the characteristic13001of the definition field of $a$.1300213003\fun{GEN}{FF_Z_Z_muldiv}{GEN a, GEN x, GEN y} returns $a\*y/z$, where \kbd{a}13004is a \typ{FFELT}, and \kbd{x} and \kbd{y} are \typ{INT}, the computation being13005performed in the definition field of \kbd{a}.1300613007\fun{GEN}{Z_FF_div}{GEN x, GEN a} return $x/a$ where \kbd{a} is a13008\typ{FFELT}, and \kbd{x} is a \typ{INT}, the computation being13009performed in the definition field of \kbd{a}.1301013011\fun{GEN}{FF_norm}{GEN a} returns the norm of the \typ{FFELT} \kbd{a} with13012respect to its definition field.1301313014\fun{GEN}{FF_trace}{GEN a} returns the trace of the \typ{FFELT} \kbd{a} with13015respect to its definition field.1301613017\fun{GEN}{FF_conjvec}{GEN a} returns the vector of conjugates13018$[a,a^p,a^{p^2},\ldots,a^{p^{n-1}}]$ where the \typ{FFELT} \kbd{a} belong to a13019field with $p^n$ elements.1302013021\fun{GEN}{FF_charpoly}{GEN a} returns the characteristic polynomial) of the13022\typ{FFELT} \kbd{a} with respect to its definition field.1302313024\fun{GEN}{FF_minpoly}{GEN a} returns the minimal polynomial of13025the \typ{FFELT} \kbd{a}.1302613027\fun{GEN}{FF_sqrt}{GEN a} returns an \typ{FFELT} $b$ such that $a=b^2$ if13028it exist, where \kbd{a} is a \typ{FFELT}.1302913030\fun{long}{FF_issquareall}{GEN x, GEN *pt} returns $1$ if \kbd{x} is a13031square, and $0$ otherwise. If \kbd{x} is indeed a square, set \kbd{pt} to its13032square root.1303313034\fun{long}{FF_issquare}{GEN x} returns $1$ if \kbd{x} is a square and $0$13035otherwise.1303613037\fun{long}{FF_ispower}{GEN x, GEN K, GEN *pt} Given $K$ a positive integer,13038returns $1$ if \kbd{x} is a $K$-th power, and $0$ otherwise. If \kbd{x} is13039indeed a $K$-th power, set \kbd{pt} to its $K$-th root.1304013041\fun{GEN}{FF_sqrtn}{GEN a, GEN n, GEN *zn} returns an \kbd{n}-th root of13042$\kbd{a}$ if it exist. If \kbd{zn} is non-\kbd{NULL} set it to a primitive13043\kbd{n}-th root of the unity.1304413045\fun{GEN}{FF_log}{GEN a, GEN g, GEN o} the \typ{FFELT} \kbd{g} being a13046generator for the definition field of the \typ{FFELT} \kbd{a}, returns a13047\typ{INT} $e$ such that $a^e=g$. If $e$ does not exists, the result is13048currently undefined. If \kbd{o} is not \kbd{NULL} it is assumed to be a13049factorization of the multiplicative order of \kbd{g} (as set by13050\tet{FF_primroot})1305113052\fun{GEN}{FF_order}{GEN a, GEN o} returns the order of the \typ{FFELT} \kbd{a}.13053If \kbd{o} is non-\kbd{NULL}, it is assumed that \kbd{o} is a multiple of the13054order of \kbd{a}.1305513056\fun{GEN}{FF_primroot}{GEN a, GEN *o} returns a generator of the13057multiplicative group of the definition field of the \typ{FFELT} \kbd{a}.13058If \kbd{o} is not \kbd{NULL}, set it to the factorization of the order13059of the primitive root (to speed up \tet{FF_log}).1306013061\fun{GEN}{FF_map}{GEN m, GEN a} returns $A(m)$ where \kbd{A=a.pol} assuming13062$a$ and $m$ belongs to fields having the same characteristic.1306313064\subsec{FFX}1306513066The functions in this sections take polynomial arguments and a \typ{FFELT}13067$a$. The coefficients of the polynomials must be of type \typ{INT},13068\typ{INTMOD} or \typ{FFELT} and compatible with \kbd{a}.1306913070\fun{GEN}{FFX_add}{GEN P, GEN Q, GEN a} returns the sum of the polynomials13071\kbd{P} and \kbd{Q} defined over the definition field of the \typ{FFELT}13072\kbd{a}.1307313074\fun{GEN}{FFX_mul}{GEN P, GEN Q, GEN a} returns the product of the polynomials13075\kbd{P} and \kbd{Q} defined over the definition field of the \typ{FFELT}13076\kbd{a}.1307713078\fun{GEN}{FFX_sqr}{GEN P, GEN a} returns the square of the polynomial13079\kbd{P} defined over the definition field of the \typ{FFELT} \kbd{a}.1308013081\fun{GEN}{FFX_rem}{GEN P, GEN Q, GEN a} returns the remainder13082of the polynomial \kbd{P} modulo the polynomial \kbd{Q}, where \kbd{P} and13083\kbd{Q} are defined over the definition field of the \typ{FFELT} \kbd{a}.1308413085\fun{GEN}{FFX_gcd}{GEN P, GEN Q, GEN a} returns the GCD of the polynomials13086\kbd{P} and \kbd{Q} defined over the definition field of the \typ{FFELT}13087\kbd{a}.1308813089\fun{GEN}{FFX_extgcd}{GEN P, GEN Q, GEN a, GEN *U, GEN *V}13090returns the GCD of the polynomials \kbd{P} and \kbd{Q} defined over13091the definition field of the \typ{FFELT} \kbd{a} and sets \kbd{*U}, \kbd{*V} to13092the Bezout coefficients such that $\kbd{*UP} + \kbd{*VQ} = d$. If \kbd{*U} is13093set to \kbd{NULL}, it is not computed which is a bit faster.1309413095\fun{GEN}{FFX_halfgcd}{GEN x, GEN y, GEN a} returns a two-by-two matrix13096$M$ with determinant $\pm 1$ such that the image $(a,b)$ of $(x,y)$ by $M$13097has the property that $\deg a \geq {\deg x \over 2} > \deg b$.1309813099\fun{GEN}{FFX_resultant}{GEN P, GEN Q, GEN a} returns the resultant of the polynomials13100\kbd{P} and \kbd{Q} where \kbd{P} and \kbd{Q} are defined over the definition13101field of the \typ{FFELT} \kbd{a}.1310213103\fun{GEN}{FFX_disc}{GEN P, GEN a} returns the discriminant of the polynomial13104\kbd{P} where \kbd{P} is defined over the definition field of the \typ{FFELT}13105\kbd{a}.1310613107\fun{GEN}{FFX_ispower}{GEN P, ulong k, GEN a, GEN *py}13108return $1$ if the \kbd{FFX} $P$ is a $k$-th power, $0$ otherwise,13109where \kbd{P} is defined over the definition field of the \typ{FFELT} \kbd{a}.13110If \kbd{py} is not \kbd{NULL}, set it to $g$ such that $g^k = f$.1311113112\fun{GEN}{FFX_factor}{GEN f, GEN a} returns the factorization of the univariate13113polynomial \kbd{f} over the definition field of the \typ{FFELT} \kbd{a}. The13114coefficients of \kbd{f} must be of type \typ{INT}, \typ{INTMOD} or \typ{FFELT}13115and compatible with \kbd{a}.1311613117\fun{GEN}{FFX_factor_squarefree}{GEN f, GEN a} returns the squarefree13118factorization of the univariate polynomial \kbd{f} over the definition field of13119the \typ{FFELT} \kbd{a}. This is a vector $[u_1,\dots,u_k]$ of pairwise13120coprime \kbd{FFX} such that $u_k \neq 1$ and $f = \prod u_i^i$.1312113122\fun{GEN}{FFX_ddf}{GEN f, GEN a} assuming that $f$ is squarefree,13123returns the distinct degree factorization of $f$ modulo $p$.13124The returned value \kbd{v} is a \typ{VEC} with two13125components: \kbd{F=v[1]} is a vector of (\kbd{FFX})13126factors, and \kbd{E=v[2]} is a \typ{VECSMALL}, such that13127$f$ is equal to the product of the \kbd{F[i]} and each \kbd{F[i]}13128is a product of irreducible factors of degree \kbd{E[i]}.1312913130\fun{GEN}{FFX_degfact}{GEN f, GEN a}, as \kbd{FFX\_factor}, but the13131degrees of the irreducible factors are returned instead of the factors13132themselves (as a \typ{VECSMALL}).1313313134\fun{GEN}{FFX_roots}{GEN f, GEN a} returns the roots (\typ{FFELT})13135of the univariate polynomial \kbd{f} over the definition field of the13136\typ{FFELT} \kbd{a}. The coefficients of \kbd{f} must be of type \typ{INT},13137\typ{INTMOD} or \typ{FFELT} and compatible with \kbd{a}.1313813139\fun{GEN}{FFX_preimagerel}{GEN F, GEN x, GEN a} returns $P\%F$13140where \kbd{P=x.pol} assuming $a$ and $x$ belongs to fields having the same13141characteristic, and that the coefficients of $F$ belong to the definition13142field of $a$.1314313144\fun{GEN}{FFX_preimage}{GEN F, GEN x, GEN a} as \kbd{FFX\_preimagerel}13145but return \kbd{NULL} if the remainder is of degree greater or equal to $1$,13146the constant coefficient otherwise.1314713148\subsec{FFM}1314913150\fun{GEN}{FFM_FFC_gauss}{GEN M, GEN C, GEN ff} given a matrix \kbd{M}13151(\typ{MAT}) and a column vector \kbd{C} (\typ{COL}) over the finite13152field given by \kbd{ff} (\typ{FFELT}) such that $M$ is invertible,13153return the unique column vector $X$ such that $MX=C$.1315413155\fun{GEN}{FFM_FFC_invimage}{GEN M, GEN C, GEN ff} given a matrix13156\kbd{M} (\typ{MAT}) and a column vector \kbd{C} (\typ{COL}) over the13157finite field given by \kbd{ff} (\typ{FFELT}), return a column vector13158\kbd{X} such that $MX=C$, or \kbd{NULL} if no such vector exists.1315913160\fun{GEN}{FFM_FFC_mul}{GEN M, GEN C, GEN ff} returns the product of13161the matrix~\kbd{M} (\typ{MAT}) and the column vector~\kbd{C}13162(\typ{COL}) over the finite field given by \kbd{ff} (\typ{FFELT}).1316313164\fun{GEN}{FFM_deplin}{GEN M, GEN ff} returns a nonzero vector13165(\typ{COL}) in the kernel of the matrix~\kbd{M} over the finite field13166given by \kbd{ff}, or \kbd{NULL} if no such vector exists.1316713168\fun{GEN}{FFM_det}{GEN M, GEN ff} returns the determinant of the13169matrix~\kbd{M} over the finite field given by \kbd{ff}.1317013171\fun{GEN}{FFM_gauss}{GEN M, GEN N, GEN ff} given two matrices \kbd{M}13172and~\kbd{N} (\typ{MAT}) over the finite field given by \kbd{ff}13173(\typ{FFELT}) such that $M$ is invertible, return the unique matrix13174$X$ such that $MX=N$.1317513176\fun{GEN}{FFM_image}{GEN M, GEN ff} returns a matrix whose columns13177span the image of the matrix~\kbd{M} over the finite field given by13178\kbd{ff}.1317913180\fun{GEN}{FFM_indexrank}{GEN M, GEN ff} given a matrix \kbd{M} of13181rank~$r$ over the finite field given by \kbd{ff}, returns a vector13182with two \typ{VECSMALL} components $y$ and $z$ containing $r$ row and13183column indices, respectively, such that the $r\times r$-matrix formed13184by the \kbd{M[i,j]} for $i$ in $y$ and $j$ in $z$ is invertible.1318513186\fun{GEN}{FFM_inv}{GEN M, GEN ff} returns the inverse of the square13187matrix~\kbd{M} over the finite field given by \kbd{ff}, or \kbd{NULL}13188if \kbd{M} is not invertible.1318913190\fun{GEN}{FFM_invimage}{GEN M, GEN N, GEN ff} given two matrices13191\kbd{M} and~\kbd{N} (\typ{MAT}) over the finite field given by13192\kbd{ff} (\typ{FFELT}), return a matrix \kbd{X} such that $MX=N$, or13193\kbd{NULL} if no such matrix exists.1319413195\fun{GEN}{FFM_ker}{GEN M, GEN ff} returns the kernel of the \typ{MAT}13196\kbd{M} over the finite field given by the \typ{FFELT} \kbd{ff}.1319713198\fun{GEN}{FFM_mul}{GEN M, GEN N, GEN ff} returns the product of the13199matrices \kbd{M} and~\kbd{N} (\typ{MAT}) over the finite field given13200by \kbd{ff} (\typ{FFELT}).1320113202\fun{long}{FFM_rank}{GEN M, GEN ff} returns the rank of the13203matrix~\kbd{M} over the finite field given by \kbd{ff}.1320413205\fun{GEN}{FFM_suppl}{GEN M, GEN ff} given a matrix \kbd{M} over the13206finite field given by \kbd{ff} whose columns are linearly independent,13207returns a square invertible matrix whose first columns are those13208of~\kbd{M}.1320913210\subsec{FFXQ}1321113212\fun{GEN}{FFXQ_mul}{GEN P, GEN Q, GEN T, GEN a} returns the product13213of the polynomials \kbd{P} and \kbd{Q} modulo the polynomial \kbd{T}, where13214\kbd{P}, \kbd{Q} and \kbd{T} are defined over the definition field of the13215\typ{FFELT} \kbd{a}.1321613217\fun{GEN}{FFXQ_sqr}{GEN P, GEN T, GEN a} returns the square13218of the polynomial \kbd{P} modulo the polynomial \kbd{T}, where \kbd{P} and13219\kbd{T} are defined over the definition field of the \typ{FFELT} \kbd{a}.1322013221\fun{GEN}{FFXQ_inv}{GEN P, GEN Q, GEN a} returns the inverse13222of the polynomial \kbd{P} modulo the polynomial \kbd{Q}, where \kbd{P} and13223\kbd{Q} are defined over the definition field of the \typ{FFELT} \kbd{a}.1322413225\fun{GEN}{FFXQ_minpoly}{GEN Pf, GEN Qf, GEN a} returns the minimal13226polynomial of the polynomial \kbd{P} modulo the polynomial \kbd{Q}, where13227\kbd{P} and \kbd{Q} are defined over the definition field of the \typ{FFELT}13228\kbd{a}.1322913230\section{Transcendental functions}1323113232The following two functions are only useful when interacting with \kbd{gp},13233to manipulate its internal default precision (expressed as a number of13234decimal digits, not in words as used everywhere else):1323513236\fun{long}{getrealprecision}{void} returns \kbd{realprecision}.1323713238\fun{long}{setrealprecision}{long n, long *prec} sets the new13239\kbd{realprecision} to $n$, which is returned. As a side effect, set13240\kbd{prec} to the corresponding number of words \kbd{ndec2prec(n)}.1324113242\subsec{Transcendental functions with \typ{REAL} arguments}1324313244In the following routines, $x$ is assumed to be a \typ{REAL} and the result13245is a \typ{REAL} (sometimes a \typ{COMPLEX} with \typ{REAL} components), with13246the largest accuracy which can be deduced from the input. The naming scheme13247is inconsistent here, since we sometimes use the prefix \kbd{mp} even though13248\typ{INT} inputs are forbidden:1324913250\fun{GEN}{sqrtr}{GEN x} returns the square root of $x$.1325113252\fun{GEN}{cbrtr}{GEN x} returns the real cube root of $x$.1325313254\fun{GEN}{sqrtnr}{GEN x, long n} returns the $n$-th root of $x$, assuming13255$n\geq 1$ and $x \geq 0$.1325613257\fun{GEN}{sqrtnr_abs}{GEN x, long n} returns the $n$-th root of $|x|$,13258assuming $n\geq 1$ and $x \neq 0$.1325913260\fun{GEN}{mpcos[z]}{GEN x[, GEN z]} returns $\cos(x)$.1326113262\fun{GEN}{mpsin[z]}{GEN x[, GEN z]} returns $\sin(x)$.1326313264\fun{GEN}{mplog[z]}{GEN x[, GEN z]} returns $\log(x)$. We must have $x > 0$13265since the result must be a \typ{REAL}. Use \kbd{glog} for the general case,13266where you want such computations as $\log(-1) = I$.1326713268\fun{GEN}{mpexp[z]}{GEN x[, GEN z]} returns $\exp(x)$.1326913270\fun{GEN}{mpexpm1}{GEN x} returns $\exp(x)-1$, but is more accurate than13271\kbd{subrs(mpexp(x), 1)}, which suffers from catastrophic cancellation if13272$|x|$ is very small.1327313274\fun{void}{mpsincosm1}{GEN x, GEN *s, GEN *c} sets $s$ and $c$ to13275$\sin(x)$ and $\cos(x)-1$ respectively, where $x$ is a \typ{REAL}; the latter13276is more accurate than \kbd{subrs(mpcos(y), 1)}, which suffers from13277catastrophic cancellation if $|x|$ is very small.1327813279\fun{GEN}{mpveceint1}{GEN C, GEN eC, long n} as \kbd{veceint1}; assumes13280that $C > 0$ is a \typ{REAL} and that \kbd{eC} is \kbd{NULL} or \kbd{mpexp(C)}.1328113282\fun{GEN}{mpeint1}{GEN x, GEN expx} returns \kbd{eint1}$(x)$, for a \typ{REAL}13283$x\neq 0$, assuming that \kbd{expx} is \kbd{mpexp}$(x)$.1328413285\noindent A few variants on the Lambert function: they actually work when13286\kbd{gtofp} can map all \kbd{GEN} arguments to a \typ{REAL}.1328713288\fun{GEN}{mplambertW}{GEN y} solution $x = W_0(y)$ of the implicit equation13289$x \exp(x) = y$, for $y > -1/e$ a \typ{REAL}.1329013291\fun{GEN}{mplambertx_logx}{GEN a, GEN b, long bit}13292solve $x - a \log(x) = b$ with $a > 0$ and $b \geq a (1 - \log(a))$.1329313294\fun{GEN}{mplambertX}{GEN y, long bit} as \tet{mplambertx_logx}13295in the special case $a = 1$, $b = \log(y)$. In other words,13296solve $e^x / x = y$ with $y \geq e$.1329713298\fun{GEN}{mplambertxlogx_x}{GEN a, GEN b, long bit}13299solve $x \log(x) - a x = b$; if $b < 0$, assume $a \geq 1 + \log |b|$.1330013301\noindent Useful low-level functions which \emph{disregard} the sign of $x$:1330213303\fun{GEN}{sqrtr_abs}{GEN x} returns $\sqrt{|x|}$ assuming $x\neq 0$.1330413305\fun{GEN}{cbrtr_abs}{GEN x} returns $|x|^{1/3}$ assuming $x\neq 0$.1330613307\fun{GEN}{exp1r_abs}{GEN x} returns $\exp(|x|) - 1$, assuming $x \neq 0$.1330813309\fun{GEN}{logr_abs}{GEN x} returns $\log(|x|)$, assuming $x \neq 0$.1331013311\subsec{Other complex transcendental functions}1331213313\fun{GEN}{atanhuu}{ulong u, ulong v, long prec} computes \text{atanh}$(u/v)$13314using binary splitting, assuming $0 < u < v$. Not memory clean but suitable13315for \kbd{gerepileupto}.1331613317\fun{GEN}{atanhui}{ulong u, GEN v, long prec} computes \text{atanh}$(u/v)$13318using binary splitting, assuming $0 < u < v$. Not memory clean but suitable13319for \kbd{gerepileupto}.1332013321\fun{GEN}{szeta}{long s, long prec} returns the value of Riemann's zeta13322function at the (possibly negative) integer $s\neq 1$, in relative accuracy13323\kbd{prec}.1332413325\fun{GEN}{veczeta}{GEN a, GEN b, long N, long prec} returns in a vector13326all the $\zeta(aj + b)$, where $j = 0, 1, \dots, N-1$, where $a$ and $b$ are13327real numbers (of arbitrary type, although \typ{INT} is treated more13328efficiently) and $b > 1$. Assumes that $N \geq 1$.1332913330\fun{GEN}{ggamma1m1}{GEN x, long prec} return $\Gamma(1+x) - 1$ assuming13331$|x| < 1$. Guard against cancellation when $x$ is small.1333213333\noindent A few variants on sin and cos:1333413335\fun{void}{mpsincos}{GEN x, GEN *s, GEN *c} sets $s$ and $c$ to13336$\sin(x)$ and $\cos(x)$ respectively, where $x$ is a \typ{REAL}1333713338\fun{void}{mpsinhcosh}{GEN x, GEN *s, GEN *c} sets $s$ and $c$ to13339$\sinh(x)$ and $\cosh(x)$ respectively, where $x$ is a \typ{REAL}1334013341\fun{GEN}{expIr}{GEN x} returns $\exp(ix)$, where $x$ is a \typ{REAL}.13342The return type is \typ{COMPLEX} unless the imaginary part is equal to $0$13343to the current accuracy (its sign is $0$).1334413345\fun{GEN}{expIPiR}{GEN x, long prec} return $\exp(i \pi x)$, where $x$ is a13346real number (\typ{INT}, \typ{FRAC} or \typ{REAL}).1334713348\fun{GEN}{expIPiC}{GEN z, long prec} return $\exp(i \pi x)$, where $x$ is a13349complex number (\typ{INT}, \typ{FRAC}, \typ{REAL} or \typ{COMPLEX}).1335013351\fun{GEN}{expIxy}{GEN x, GEN y, long prec} returns $\exp(ixy)$. Efficient13352when $x$ is real and $y$ pure imaginary.1335313354\fun{GEN}{pow2Pis}{GEN s, long prec} returns $(2\pi)^s$. The intent of this13355function and the next ones is to be accurate even if $s$ has a huge imaginary13356part: $\pi$ is computed at an accuracy taking into account the cancellation13357induced by argument reduction when computing the sine or cosine of13358$\Im s \log 2\pi$.1335913360\fun{GEN}{powPis}{GEN s, long prec} returns $\pi^s$, as \kbd{pow2Pis}.1336113362\fun{long}{powcx_prec}{long e, GEN s, long prec} if $e \approx \log_2 |x|$13363return the precision at which $\log(x)$ must be computed to evaluate $x^s$13364reliably (taking into account argument reduction).1336513366\fun{GEN}{powcx}{GEN x, GEN logx, GEN s, long prec} assuming $s$ is a13367\typ{COMPLEX} and \kbd{logx} is $\log(x)$ computed to accuracy13368\kbd{powcx\_prec}, return $x^s$.1336913370\fun{void}{gsincos}{GEN x, GEN *s, GEN *c, long prec} general case.1337113372\fun{GEN}{rootsof1_cx}{GEN d, long prec} return $e(1/d)$ at precision13373\kbd{prec}, $e(x) = \exp(2i\pi x)$.1337413375\fun{GEN}{rootsof1u_cx}{ulong d, long prec} return $e(1/d)$ at13376precision \kbd{prec}.1337713378\fun{GEN}{rootsof1q_cx}{long a, long b, long prec} return $e(a/b)$ at13379precision \kbd{prec}.1338013381\fun{GEN}{rootsof1powinit}{long a, long b, long prec} precompute $b$-th13382roots of $1$ for \kbd{rootsof1pow}, i.e. to later compute $e(ac/b)$ for13383varying $c$.1338413385\fun{GEN}{rootsof1pow}{GEN T, long c} given13386$T = \kbd{rootsof1powinit}(a,b,\kbd{prec})$, return $e(ac/b)$.133871338813389\noindent A generalization of \tet{affrr_fixlg}1339013391\fun{GEN}{affc_fixlg}{GEN x, GEN res} assume \kbd{res} was allocated using13392\tet{cgetc}, and that $x$ is either a \typ{REAL} or a \typ{COMPLEX}13393with \typ{REAL} components. Assign $x$ to \kbd{res}, first shortening13394the components of \kbd{res} if needed (in a \kbd{gerepile}-safe way). Further13395convert \kbd{res} to a \typ{REAL} if $x$ is a \typ{REAL}.1339613397\fun{GEN}{trans_eval}{const char *fun, GEN (*f) (GEN, long), GEN x, long prec}13398evaluate the transcendental function $f$ (named \kbd{"fun"} at the argument13399$x$ and precision \kbd{prec}. This is a quick way to implement a transcendental13400function to be made available under GP, starting from a $C$ function13401handling only \typ{REAL} and \typ{COMPLEX} arguments. This routine first13402converts $x$ to a suitable type:1340313404\item \typ{INT}/\typ{FRAC} to \typ{REAL} of precision \kbd{prec}, \typ{QUAD} to13405\typ{REAL} or \typ{COMPLEX} of precision \kbd{prec}.1340613407\item \typ{POLMOD} to a \typ{COL} of complex embeddings (as in \tet{conjvec})1340813409Then evaluates the function at \typ{VEC}, \typ{COL}, \typ{MAT} arguments13410coefficientwise.1341113412\subsec{Modular functions}1341313414\fun{GEN}{cxredsl2}{GEN z, GEN *g} given $t$ a \typ{COMPLEX} belonging to the13415upper half-plane, find $\gamma \in \text{SL}_2(\Z)$ such that $\gamma \cdot z$13416belongs to the standard fundamental domain and set \kbd{*g} to $\gamma$.1341713418\fun{GEN}{cxredsl2_i}{GEN z, GEN *g, GEN *czd} as \kbd{cxredsl2}; also sets13419\kbd{*czd} to $cz + d$, if $\gamma = [a,b;c,d]$.1342013421\fun{GEN}{cxEk}{GEN tau, long k, long prec} returns $E_k(\tau)$ by direct13422evaluation of $1 + 2/\zeta(1-k) \sum_n n^{k-1} q^n/(1-q^n)$, $q = e(\tau)$.13423Assume that $\Im \tau > 0$ and $k$ even. Very slow unless $\tau$ is already13424reduced modulo $\text{SL}_2(\Z)$. Not \kbd{gerepile}-clean but suitable for13425\kbd{gerepileupto}.1342613427\subsec{Transcendental functions with \typ{PADIC} arguments}1342813429\fun{GEN}{Qp_exp}{GEN x} shortcut for \kbd{gexp(x, /*ignored*/prec)}1343013431\fun{GEN}{Qp_gamma}{GEN x} shortcut for \kbd{ggamma(x, /*ignored*/prec)}1343213433\fun{GEN}{Qp_log}{GEN x} shortcut for \kbd{glog(x, /*ignored*/prec)}1343413435\fun{GEN}{Qp_sqrt}{GEN x} shortcut for \kbd{gsqrt(x, /*ignored*/prec)}13436Return \kbd{NULL} if $x$ is not a square.1343713438\fun{GEN}{Qp_sqrtn}{GEN x, GEN n, GEN *z} shortcut for \kbd{gsqrtn(x, n, z,13439/*ignored*/prec)}. Return \kbd{NULL} if $x$ is not an $n$-th power.1344013441\fun{GEN}{Qp_agm2_sequence}{GEN a1, GEN b1} assume $a_1/b_1 = 1$ mod $p$13442if $p$ odd and mod $2^4$ if $p = 2$. Let $A_1 = a_1/p^v$ and $B_1 = b_1/p^v$13443with $v = v_p(a_1) = v_p(b_1)$; let further13444$A_{n+1} = (A_n + B_n + 2 B_{n+1}) / 4$,13445$B_{n+1} = B_n \sqrt{A_n / B_n}$ (the square root of $A_n B_n$ congruent to13446$B_n$ mod $p$) and $R_n = p^v(A_n - B_n)$. We stop when $R_n$ is $0$13447at the given $p$-adic accuracy. This function returns in a triplet \typ{VEC}13448the three sequences $(A_n)$, $(B_n)$ and $(R_n)$, corresponding to a sequence13449of $2$-isogenies on the Tate curve $y^2 = x(x-a_1)(x+a_1-b_1)$. The13450common limit of $A_n$ and $B_n$ is the $M_2(a_1,b_1)$, the square13451of the $p$-adic AGM of $\sqrt(a_1)$ and $\sqrt(b_1)$. This is given13452by \tet{ellQp_Ei} and is used by corresponding ascending and descending13453$p$-adic Landen transforms:1345413455\fun{void}{Qp_ascending_Landen}{GEN ABR, GEN *ptx, GEN *pty}1345613457\fun{void}{Qp_descending_Landen}{GEN ABR, GEN *ptx, GEN *pty}1345813459\subsec{Cached constants}1346013461The cached constant is returned at its current precision, which may be larger13462than \kbd{prec}. One should always use the \kbd{mp\var{xxx}} variant:13463\kbd{mppi}, \kbd{mpeuler}, or \kbd{mplog2}.1346413465\fun{GEN}{consteuler}{long prec} precomputes Euler-Mascheroni's constant13466at precision \kbd{prec}.1346713468\fun{GEN}{constcatalan}{long prec} precomputes Catalan's constant at precision13469\kbd{prec}.1347013471\fun{GEN}{constpi}{long prec} precomputes $\pi$ at precision \kbd{prec}.1347213473\fun{GEN}{constlog2}{long prec} precomputes $\log(2)$ at precision13474\kbd{prec}.1347513476\fun{void}{constbern}{long n} precomputes the $n$ even \idx{Bernoulli} numbers13477$B_2,\dots,B_{2n}$ as \typ{FRAC}. No more than $n$ Bernoulli numbers will13478ever be stored (by \tet{bernfrac} or \tet{bernreal}), unless a subsequent13479call to \kbd{constbern} increases the cache.1348013481\fun{GEN}{constzeta}{long n, long prec} ensures that the $n$ values13482$\gamma, \zeta(2),\dots, \zeta(n)$ are cached at accuracy bigger than or equal13483to \kbd{prec} and return a vector containing at least those value. Note that13484$\gamma = \lim_1 \zeta(s) - 1/(s-1)$. If the accuracy of cached data is too13485low or $n$ is greater than the cache length, the cache is recomputed at the13486given parameters.1348713488The following functions use cached data if \kbd{prec} is smaller than the13489precision of the cached value; otherwise the newly computed data replaces the13490old cache.1349113492\fun{GEN}{mppi}{long prec} returns $\pi$ at precision \kbd{prec}.1349313494\fun{GEN}{Pi2n}{long n, long prec} returns $2^n\pi$ at precision \kbd{prec}.1349513496\fun{GEN}{PiI2}{long n, long prec} returns the complex number $2\pi i$ at13497precision \kbd{prec}.1349813499\fun{GEN}{PiI2n}{long n, long prec} returns the complex number $2^n\pi i$ at13500precision \kbd{prec}.1350113502\fun{GEN}{mpeuler}{long prec} returns Euler-Mascheroni's constant at13503precision \kbd{prec}.1350413505\fun{GEN}{mpeuler}{long prec} returns Catalan's number at precision \kbd{prec}.1350613507\fun{GEN}{mplog2}{long prec} returns $\log 2$ at precision \kbd{prec}.1350813509The following functions use the \idx{Bernoulli} numbers cache initialized by13510\kbd{constbern}:1351113512\fun{GEN}{bernreal}{long i, long prec} returns the \idx{Bernoulli} number13513$B_i$ as a \typ{REAL} at precision \kbd{prec}. If \kbd{constbern(n)}13514was called previously with $n \geq i$, then the cached value is (converted to13515a \typ{REAL} of accuracy \kbd{prec} then) returned. Otherwise, the missing13516value is computed; the cache is not updated.1351713518\fun{GEN}{bernfrac}{long i} returns the \idx{Bernoulli} number $B_i$ as a13519rational number (\typ{FRAC} or \typ{INT}). If the \kbd{constbern} cache includes13520$B_i$, the latter is returned. Otherwise, the missing value is computed; the13521cache is not updated.1352213523\subsec{Obsolete functions}1352413525\fun{void}{mpbern}{long n, long prec}1352613527\section{Permutations }1352813529\noindent Permutations are represented in two different ways1353013531\item (\kbd{perm}) a \typ{VECSMALL} $p$ representing the bijection $i\mapsto13532p[i]$; unless mentioned otherwise, this is the form used in the functions13533below for both input and output,1353413535\item (\kbd{cyc}) a \typ{VEC} of \typ{VECSMALL}s representing a product of13536disjoint cycles.1353713538\fun{GEN}{identity_perm}{long n} return the identity permutation on $n$13539symbols.1354013541\fun{GEN}{cyclic_perm}{long n, long d} return the cyclic permutation mapping13542$i$ to $i+d$ (mod $n$) in $S_n$. Assume that $d \leq n$.1354313544\fun{GEN}{perm_mul}{GEN s, GEN t} multiply $s$ and $t$ (composition $s\circ t$)1354513546\fun{GEN}{perm_sqr}{GEN s} multiply $s$ by itself (composition $s\circ s$)1354713548\fun{GEN}{perm_conj}{GEN s, GEN t} return $sts^{-1}$.1354913550\fun{int}{perm_commute}{GEN p, GEN q} return $1$ if $p$ and $q$ commute, 013551otherwise.1355213553\fun{GEN}{perm_inv}{GEN p} returns the inverse of $p$.1355413555\fun{GEN}{perm_pow}{GEN p, GEN n} returns $p^n$1355613557\fun{GEN}{perm_powu}{GEN p, ulong n} returns $p^n$1355813559\fun{GEN}{cyc_pow_perm}{GEN p, long n} the permutation $p$ is given as13560a product of disjoint cycles (\kbd{cyc}); return $p^n$ (as a \kbd{perm}).1356113562\fun{GEN}{cyc_pow}{GEN p, long n} the permutation $p$ is given as13563a product of disjoint cycles (\kbd{cyc}); return $p^n$ (as a \kbd{cyc}).1356413565\fun{GEN}{perm_cycles}{GEN p} return the cyclic decomposition of $p$.1356613567\fun{GEN}{perm_order}{GEN p} returns the order of the permutation $p$13568(as the lcm of its cycle lengths).1356913570\fun{ulong}{perm_orderu}{GEN p} returns the order of the permutation $p$13571(as the lcm of its cycle lengths) assuming it fits in a \kbd{ulong}.1357213573\fun{long}{perm_sign}{GEN p} returns the sign of the permutation $p$.1357413575\fun{GEN}{vecperm_orbits}{GEN gen, long n} return the orbits of13576$\{1,2,\ldots,n\}$ under the action of the subgroup of $S_n$ generated by13577$gen$.1357813579\fun{GEN}{Z_to_perm}{long n, GEN x} as \kbd{numtoperm}, returning a13580\typ{VECSMALL}.1358113582\fun{GEN}{perm_to_Z}{GEN v} as \kbd{permtonum} for a \typ{VECSMALL} input.1358313584\fun{GEN}{perm_to_GAP}{GEN p} return a \typ{STR} which is a representation13585of $p$ comptatible with the GAP computer algebra system.1358613587\section{Small groups}1358813589The small (finite) groups facility is meant to deal with subgroups of Galois13590groups obtained by \tet{galoisinit} and thus is currently limited to weakly13591super-solvable groups.1359213593A group \var{grp} of order $n$ is represented by its regular representation13594(for an arbitrary ordering of its element) in $S_n$. A subgroup of such group13595is represented by the restriction of the representation to the subgroup.13596A \emph{small group} can be either a group or a subgroup. Thus it is embedded13597in some $S_n$, where $n$ is the multiple of the order. Such an $n$ is called13598the \emph{domain} of the small group. The domain of a trivial subgroup cannot13599be derived from the subgroup data, so some functions require the subgroup13600domain as argument.1360113602The small group \var{grp} is represented by a \typ{VEC} with two13603components:1360413605$\var{grp}[1]$ is a generating subset $[s_1,\ldots,s_g]$ of \var{grp}13606expressed as a vector of permutations of length~$n$.1360713608$\var{grp}[2]$ contains the relative orders $[o_1,\ldots,o_g]$ of13609the generators $\var{grp}[1]$.1361013611See \tet{galoisinit} for the technical details.1361213613\fun{GEN}{checkgroup}{GEN gal, GEN *elts} check whether \var{gal} is a13614small group or a Galois group. Returns the underlying small13615group and set \var{elts} to the list of elements or to \kbd{NULL} if it is not13616known.1361713618\fun{GEN}{checkgroupelts}{GEN gal}13619check whether \var{gal} is a small group or a Galois group, or a vector of13620permutations listing the group elements. Returns the list of group elements as13621permutations.1362213623\fun{GEN}{galois_group}{GEN gal} return the underlying small group of the13624Galois group \var{gal}.1362513626\fun{GEN}{cyclicgroup}{GEN g, long s} return the cyclic group with generator13627$g$ of order $s$.1362813629\fun{GEN}{trivialgroup}{void} return the trivial group.1363013631\fun{GEN}{dicyclicgroup}{GEN g1, GEN g2, long s1, long s2} returns the group13632with generators \var{g1}, \var{g2} with respecting relative orders \var{s1},13633\var{s2}.1363413635\fun{GEN}{abelian_group}{GEN v} let v be a \typ{VECSMALL} seen as the SNF of13636a small abelian group, return its regular representation.1363713638\fun{long}{group_domain}{GEN grp} returns the \kbd{domain} of the13639\emph{nontrivial} small group \var{grp}. Return an error if \var{grp} is13640trivial.1364113642\fun{GEN}{group_elts}{GEN grp, long n} returns the list of elements of the13643small group \var{grp} of domain \var{n} as permutations.1364413645\fun{GEN}{groupelts_to_group}{GEN elts}, where \var{elts} is the list of13646elements of a group, returns the corresponding small group, if it13647exists, otherwise return \kbd{NULL}.1364813649\fun{GEN}{group_set}{GEN grp, long n} returns a \var{F2v} $b$ such that13650$b[i]$ is set if and only if the small group \var{grp} of domain \var{n}13651contains a permutation sending $1$ to $i$.1365213653\fun{GEN}{groupelts_set}{GEN elts, long n}, where \var{elts} is the list of13654elements of a small group of domain \var{n}, returns a \var{F2v} $b$ such that13655$b[i]$ is set if and only if the small group contains a permutation sending $1$13656to $i$.1365713658\fun{GEN}{groupelts_conj_set}{GEN elts, GEN p}, where \var{elts} is the list of13659elements of a small group of domain \var{n}, returns a \var{F2v} $b$ such that13660$b[i]$ is set if and only if the small group contains a permutation sending13661$p^{-1}[1]$ to $p^{-1}[i]$.1366213663\fun{int}{group_subgroup_is_faithful}{GEN G, GEN H} return $1$ if the action13664of $G$ on $G/H$ by translation is faithful, $0$ otherwise.1366513666\fun{GEN}{groupelts_conjclasses}{GEN elts, long *pn}, where \var{elts} is13667the list of elements of a small group (sorted with respect to13668\kbd{vecsmall\_lexcmp}), return a \typ{VECSMALL} \kbd{conj} of13669the same length such that \kbd{conj[i]} is the index in $\{1,\cdots,n\}$ of13670the conjugacy class of \kbd{elts[i]} for some unspecified but deterministic13671ordering of the classes, where $n$ is the number of conjugacy classes. If13672\kbd{pn} is non \kbd{NULL}, \kbd{*pn} is set to $n$.1367313674\fun{GEN}{conjclasses_repr}{GEN conj, long nb},13675where \kbd{conj} and \kbd{nb} are as returned by the call13676\kbd{groupelts\_conjclasses(elts)}, return \typ{VECSMALL} of length \kbd{nb}13677which gives the indices in \kbd{elts} of a representative of each conjugacy13678class.1367913680\fun{GEN}{group_to_cc}{GEN G}, where $G$ is a small group or a Galois group,13681returns a \kbd{cc} (conjclasses) structure \kbd{[elts,conj,rep,flag]},13682as obtained by \kbd{alggroupcenter}, where \kbd{conj} is13683\kbd{groupelts\_conjclasses}$(\kbd{elts})$ and \kbd{rep} is the attached13684\kbd{conjclasses\_repr}. \kbd{flag} is 1 if the permutation representation13685is transitive (in which case an element $g$ of $G$ is characterized by $g[1]$),13686and 0 otherwise. Shallow function.1368713688\fun{long}{group_order}{GEN grp} returns the order of the small group13689\var{grp} (which is the product of the relative orders).1369013691\fun{long}{group_isabelian}{GEN grp} returns $1$ if the small group13692\var{grp} is Abelian, else $0$.1369313694\fun{GEN}{group_abelianHNF}{GEN grp, GEN elts} if \var{grp} is not Abelian,13695returns \kbd{NULL}, else returns the HNF matrix of \var{grp} with respect to13696the generating family $\var{grp}[1]$. If \var{elts} is no \kbd{NULL}, it must13697be the list of elements of \var{grp}.1369813699\fun{GEN}{group_abelianSNF}{GEN grp, GEN elts} if \var{grp} is not Abelian,13700returns \kbd{NULL}, else returns its cyclic decomposition. If \var{elts} is no13701\kbd{NULL}, it must be the list of elements of \var{grp}.1370213703\fun{long}{group_subgroup_isnormal}{GEN G, GEN H}, $H$ being a subgroup of the13704small group $G$, returns $1$ if $H$ is normal in $G$, else $0$.1370513706\fun{long}{group_isA4S4}{GEN grp} returns $1$ if the small group13707\var{grp} is isomorphic to $A_4$, $2$ if it is isomorphic to $S_4$,13708$3$ if it is isomorphic to $(3\times 3):4$ and13709$0$ else. This is mainly to deal with the idiosyncrasy of the format.1371013711\fun{GEN}{group_leftcoset}{GEN G, GEN g} where $G$ is a small group and $g$ a13712permutation of the same domain, the left coset $gG$ as a vector of13713permutations.1371413715\fun{GEN}{group_rightcoset}{GEN G, GEN g} where $G$ is a small group and $g$ a13716permutation of the same domain, the right coset $Gg$ as a vector of13717permutations.1371813719\fun{long}{group_perm_normalize}{GEN G, GEN g} where $G$ is a small group and13720$g$ a permutation of the same domain, return $1$ if $gGg^{-1}=G$, else $0$.1372113722\fun{GEN}{group_quotient}{GEN G, GEN H}, where $G$ is a small group and13723$H$ is a subgroup of $G$, returns the quotient map $G\rightarrow G/H$13724as an abstract data structure.1372513726\fun{GEN}{groupelts_quotient}{GEN elts, GEN H}, where \var{elts} is the list of13727elements of a small group $G$, $H$ is a subgroup of $G$, returns the quotient13728map $G\rightarrow G/H$ as an abstract data structure.1372913730\fun{GEN}{quotient_perm}{GEN C, GEN g} where $C$ is the quotient map13731$G\rightarrow G/H$ for some subgroup $H$ of $G$ and $g$ an element of $G$,13732return the image of $g$ by $C$ (i.e. the coset $gH$).1373313734\fun{GEN}{quotient_group}{GEN C, GEN G} where $C$ is the quotient map13735$G\rightarrow G/H$ for some \emph{normal} subgroup $H$ of $G$, return the13736quotient group $G/H$ as a small group.1373713738\fun{GEN}{quotient_groupelts}{GEN C} where $C$ is the quotient map13739$G\rightarrow G/H$ for some group $G$ and some \emph{normal} subgroup $H$ of $G$,13740return the list of elements of the quotient group $G/H$ (as permutations over13741corresponding to the regular representation).1374213743\fun{GEN}{quotient_subgroup_lift}{GEN C, GEN H, GEN S} where $C$ is the13744quotient map $G\rightarrow G/H$ for some group $G$ normalizing $H$ and $S$ is13745a subgroup of $G/H$, return the inverse image of $S$ by $C$.1374613747\fun{GEN}{group_subgroups}{GEN grp} returns the list of subgroups of the13748small group \var{grp} as a \typ{VEC}.1374913750\fun{GEN}{subgroups_tableset}{GEN S, long n} where $S$ is a vector of subgroups13751of domain $n$, returns a table which matchs the set of elements of the13752subgroups against the index of the subgroups.1375313754\fun{long}{tableset_find_index}{GEN tbl, GEN set} searchs the set \kbd{set} in13755the table \kbd{tbl} and returns its attached index, or $0$ if not found.1375613757\fun{GEN}{groupelts_abelian_group}{GEN elts} where \var{elts} is the list of13758elements of an \emph{Abelian} small group, returns the corresponding13759small group.1376013761\fun{long}{groupelts_exponent}{GEN elts} where \var{elts} is the list of13762elements of a small group, returns the exponent the group (the LCM of the order13763of the elements of the group).1376413765\fun{GEN}{groupelts_center}{GEN elts} where \var{elts} is the list of elements13766of a small group, returns the list of elements of the center of the13767group.1376813769\fun{GEN}{group_export}{GEN grp, long format} convert a small group13770to another format, as a \typ{STR} describing the group for the given syntax,13771see \tet{galoisexport}.1377213773\fun{GEN}{group_export_GAP}{GEN G} export a small group to GAP format.1377413775\fun{GEN}{group_export_MAGMA}{GEN G} export a small group to MAGMA format.1377613777\fun{long}{group_ident}{GEN grp, GEN elts} returns the index of the small group13778\var{grp} in the GAP4 Small Group library, see \tet{galoisidentify}. If13779\var{elts} is not \kbd{NULL}, it must be the list of elements of \var{grp}.1378013781\fun{long}{group_ident_trans}{GEN grp, GEN elts} returns the index of the13782regular representation of the small group \var{grp} in the GAP4 Transitive13783Group library, see \tet{polgalois}. If \var{elts} is no \kbd{NULL}, it must be13784the list of elements of \var{grp}.1378513786\newpage13787\chapter{Standard data structures}1378813789\section{Character strings}1379013791\subsec{Functions returning a \kbd{char *}}1379213793\fun{char*}{pari_strdup}{const char *s} returns a malloc'ed copy of $s$13794(uses \kbd{pari\_malloc}).1379513796\fun{char*}{pari_strndup}{const char *s, long n} returns a malloc'ed copy of13797at most $n$ chars from $s$ (uses \kbd{pari\_malloc}). If $s$ is longer than13798$n$, only $n$ characters are copied and a terminal null byte is added.1379913800\fun{char*}{stack_strdup}{const char *s} returns a copy of $s$, allocated13801on the PARI stack (uses \kbd{stack\_malloc}).1380213803\fun{char*}{stack_strcat}{const char *s, const char *t} returns the13804concatenation of $s$ and $t$, allocated on the PARI stack (uses13805\kbd{stack\_malloc}).1380613807\fun{char*}{stack_sprintf}{const char *fmt, ...} runs \kbd{pari\_sprintf}13808on the given arguments, returning a string allocated on the PARI stack.1380913810\fun{char*}{uordinal}{ulong x} return the ordinal number attached to $x$13811(i.e. $1$st, $2$nd, etc.) as a \tet{stack_malloc}'ed string.1381213813\fun{char*}{itostr}{GEN x} writes the \typ{INT} $x$ to a \tet{stack_malloc}'ed13814string.1381513816\fun{char*}{GENtostr}{GEN x}, using the current default output format13817(\kbd{GP\_DATA->fmt}, which contains the output style and the number of13818significant digits to print), converts $x$ to a malloc'ed string. Simple13819variant of \tet{pari_sprintf}.1382013821\fun{char*}{GENtostr_raw}{GEN x} as \tet{GENtostr} with the following13822differences: 1) the output format is \tet{f_RAW}; 2) the result is allocated13823on the stack and \emph{must not} be freed.1382413825\fun{char*}{GENtostr_unquoted}{GEN x} as \tet{GENtostr_raw} with the following13826additional difference: a \typ{STR} $x$ is printed without enclosing quotes13827(to be used by \kbd{print}.1382813829\fun{char*}{GENtoTeXstr}{GEN x}, as \kbd{GENtostr}, except that13830\tet{f_TEX} overrides the output format from \kbd{GP\_DATA->fmt}.1383113832\fun{char*}{RgV_to_str}{GEN g, long flag} $g$ being a vector of \kbd{GEN}s,13833returns a malloc'ed string, the concatenation of the \kbd{GENtostr} applied13834to its elements, except that \typ{STR} are printed without enclosing quotes.13835\kbd{flag} determines the output format: \tet{f_RAW}, \tet{f_PRETTYMAT}13836or \tet{f_TEX}.1383713838\subsec{Functions returning a \typ{STR}}1383913840\fun{GEN}{strtoGENstr}{const char *s} returns a \typ{STR} with content $s$.1384113842\fun{GEN}{strntoGENstr}{const char *s, long n}13843returns a \typ{STR} containing the first $n$ characters of $s$.1384413845\fun{GEN}{chartoGENstr}{char c} returns a \typ{STR} containing the character13846$c$.1384713848\fun{GEN}{GENtoGENstr}{GEN x} returns a \typ{STR} containing the printed13849form of $x$ (in \tet{raw} format). This is often easier to use that13850\tet{GENtostr} (which returns a malloc-ed \kbd{char*}) since there is no need13851to free the string after use.1385213853\fun{GEN}{GENtoGENstr_nospace}{GEN x} as \kbd{GENtoGENstr}, removing all13854spaces from the output.1385513856\fun{GEN}{Str}{GEN g} as \tet{RgV_to_str} with output format \tet{f_RAW},13857but returns a \typ{STR}, not a malloc'ed string.1385813859\fun{GEN}{strtex}{GEN g} as \tet{RgV_to_str} with output format \tet{f_TEX},13860but returns a \typ{STR}, not a malloc'ed string.1386113862\fun{GEN}{strexpand}{GEN g} as \tet{RgV_to_str} with output format \tet{f_RAW},13863performing tilde and environment expansion on the result. Returns a13864\typ{STR}, not a malloc'ed string.1386513866\fun{GEN}{gsprintf}{const char *fmt, ...} equivalent to13867\kbd{pari\_sprintf(fmt,...}, followed by \tet{strtoGENstr}. Returns a \typ{STR},13868not a malloc'ed string.1386913870\fun{GEN}{gvsprintf}{const char *fmt, va_list ap} variadic version of13871\tet{gsprintf}1387213873\subsec{Dynamic strings}1387413875A \tet{pari_str} is a dynamic string which grows dynamically as needed.13876This structure contains private data and two public members \kbd{char *string},13877which is the string itself and \kbd{use\_stack} which tells whether the13878string lives1387913880\item on the PARI stack (value $1$), meaning that it will be destroyed by any13881manipulation of the stack, e.g. a \kbd{gerepile} call or resetting13882\kbd{avma};1388313884\item in malloc'ed memory (value $0$), in which case it is impervious to13885stack manipulation but will need to be explicitly freed by the user13886after use, via \kbd{pari\_free(s.string)}.138871388813889\fun{void}{str_init}{pari_str *S, int use_stack} initializes a dynamic13890string; if \kbd{use\_stack} is 0, then the string is malloc'ed, else13891it lives on the PARI stack.1389213893\fun{void}{str_printf}{pari_str *S, const char *fmt, ...} write to the end13894of $S$ the remaining arguments according to PARI format \kbd{fmt}.1389513896\fun{void}{str_putc}{pari_str *S, char c} write the character $c$ to the end13897of $S$.1389813899\fun{void}{str_puts}{pari_str *S, const char *s} write the string $s$ to the13900end of $S$.1390113902\section{Output}1390313904\subsec{Output contexts}1390513906An output coutext, of type \tet{PariOUT}, is a \kbd{struct}13907that models a stream and contains the following function pointers:13908\bprog13909void (*putch)(char); /* fputc()-alike */13910void (*puts)(const char*); /* fputs()-alike */13911void (*flush)(void); /* fflush()-alike */13912@eprog\noindent13913The methods \tet{putch} and \tet{puts} are used to print a character13914or a string respectively. The method \tet{flush} is called to finalize a13915messages.1391613917The generic functions \tet{pari_putc}, \tet{pari_puts}, \tet{pari_flush} and13918\tet{pari_printf} print according to a \emph{default output context}, which13919should be sufficient for most purposes. Lower level functions are available,13920which take an explicit output context as first argument:1392113922\fun{void}{out_putc}{PariOUT *out, char c} essentially equivalent to13923\kbd{out->putc(c)}. In addition, registers whether the last character printed13924was a \kbd{\bs n}.1392513926\fun{void}{out_puts}{PariOUT *out, const char *s} essentially equivalent to13927\kbd{out->puts(s)}. In addition, registers whether the last character printed13928was a \kbd{\bs n}.1392913930\fun{void}{out_printf}{PariOUT *out, const char *fmt, ...}1393113932\fun{void}{out_vprintf}{PariOUT *out, const char *fmt, va_list ap}1393313934\noindent N.B. The function \kbd{out\_flush} does not exist since it would be13935identical to \kbd{out->flush()}1393613937\fun{int}{pari_last_was_newline}{void} returns a nonzero value if the last13938character printed via \tet{out_putc} or \tet{out_puts} was \kbd{\bs13939n}, and $0$ otherwise.1394013941\fun{void}{pari_set_last_newline}{int last} sets the boolean value13942to be returned by the function \tet{pari_last_was_newline} to \var{last}.1394313944\subsec{Default output context} They are defined by the global variables13945\tet{pariOut} and \tet{pariErr} for normal outputs and warnings/errors, and you13946probably do not want to change them. If you \emph{do} change them, diverting13947output in nontrivial ways, this probably means that you are rewriting13948\kbd{gp}. For completeness, we document in this section what the default13949output contexts do.1395013951\misctitle{pariOut} writes output to the \kbd{FILE*} \tet{pari_outfile},13952initialized to \tet{stdout}. The low-level methods are actually the standard13953\kbd{putc} / \kbd{fputs}, plus some magic to handle a log file if one is13954open.1395513956\misctitle{pariErr} prints to the \kbd{FILE*} \tet{pari_errfile}, initialized13957to \tet{stderr}. The low-level methods are as above.1395813959You can stick with the default \kbd{pariOut} output context and change PARI's13960standard output, redirecting \tet{pari_outfile} to another file, using1396113962\fun{void}{switchout}{const char *name} where \kbd{name} is a character string13963giving the name of the file you want to write to; the output is13964\emph{appended} at the end of the file. To close the file and revert to13965outputting to \kbd{stdout}, call \kbd{switchout(NULL)}.1396613967\subsec{PARI colors}13968In this section we describe the low-level functions used to implement GP's13969color scheme, attached to the \tet{colors} default. The following symbolic13970names are attached to gp's output strings:1397113972\item \tet{c_ERR} an error message1397313974\item \tet{c_HIST} a history number (as in \kbd{\%1 = ...})1397513976\item \tet{c_PROMPT} a prompt1397713978\item \tet{c_INPUT} an input line (minus the prompt part)1397913980\item \tet{c_OUTPUT} an output1398113982\item \tet{c_HELP} a help message1398313984\item \tet{c_TIME} a timer1398513986\item \tet{c_NONE} everything else1398713988\emph{If} the \tet{colors} default is set to a nonempty value, before gp13989outputs a string, it first outputs an ANSI colors escape sequence ---13990understood by most terminals ---, according to the \kbd{colors}13991specifications. As long as this is in effect, the following strings are13992rendered in color, possibly in bold or underlined.1399313994\fun{void}{term_color}{long c} prints (as if using \tet{pari_puts}) the ANSI13995color escape sequence attached to output object \kbd{c}. If \kbd{c} is13996\tet{c_NONE}, revert to default printing style.1399713998\fun{void}{out_term_color}{PariOUT *out, long c} as \tet{term_color},13999using output context \kbd{out}.1400014001\fun{char*}{term_get_color}{char *s, long c} returns as a character14002string the ANSI color escape sequence attached to output object \kbd{c}.14003If \kbd{c} is \tet{c_NONE}, the value used to revert to default printing14004style is returned. The argument \kbd{s} is either \kbd{NULL} (string14005allocated on the PARI stack), or preallocated storage (in which case, it must14006be able to hold at least 16 chars, including the final \kbd{\bs 0}).1400714008\subsec{Obsolete output functions}1400914010These variants of \fun{void}{output}{GEN x}, which prints \kbd{x}, followed by14011a newline and a buffer flush are complicated to use and less flexible14012than what we saw above, or than the \tet{pari_printf} variants. They are14013provided for backward compatibility and are scheduled to disappear.1401414015\fun{void}{brute}{GEN x, char format, long dec}1401614017\fun{void}{matbrute}{GEN x, char format, long dec}1401814019\fun{void}{texe}{GEN x, char format, long dec}1402014021\section{Files}1402214023The following routines are trivial wrappers around system functions14024(possibly around one of several functions depending on availability).14025They are usually integrated within PARI's diagnostics system, printing14026messages if \kbd{DEBUGFILES} is high enough.1402714028\fun{int}{pari_is_dir}{const char *name} returns $1$ if \kbd{name} points to14029a directory, $0$ otherwise.1403014031\fun{int}{pari_is_file}{const char *name} returns $1$ if \kbd{name} points to14032a directory, $0$ otherwise.1403314034\fun{int}{file_is_binary}{FILE *f} returns $1$ if the file $f$ is a binary14035file (in the \tet{writebin} sense), $0$ otherwise.1403614037\fun{void}{pari_unlink}{const char *s} deletes the file named $s$. Warn14038if the operation fails.1403914040\fun{void}{pari_fread_chars}{void *b, size_t n, FILE *f} read $n$ chars from14041stream $f$, storing the result in pre-allocated buffer $b$ (assumed to be14042large enough).1404314044\fun{char*}{path_expand}{const char *s} perform tilde and environment expansion14045on $s$. Returns a \kbd{malloc}'ed buffer.1404614047\fun{void}{strftime_expand}{const char *s, char *buf, long max} perform14048time expansion on $s$, storing the result (at most \kbd{max} chars) in14049buffer \kbd{buf}. Trivial wrapper around14050\bprog14051time_t t = time(NULL);14052strftime(but, max, s, localtime(&t);14053@eprog1405414055\fun{char*}{pari_get_homedir}{const char *user} expands \kbd{\til user}14056constructs, returning the home directory of user \kbd{user}, or \kbd{NULL} if14057it could not be determined (in particular if the operating system has no such14058concept). The return value may point to static area and may be overwritten14059by subsequent system calls: use immediately or \kbd{strdup} it.1406014061\fun{int}{pari_stdin_isatty}{void} returns $1$ if our standard input14062\kbd{stdin} is attached to a terminal. Trivial wrapper around \kbd{isatty}.1406314064\subsec{pariFILE}1406514066PARI maintains a linked list of open files, to reclaim resources14067(file descriptors) on error or interrupts. The corresponding data structure14068is a \kbd{pariFILE}, which is a wrapper around a standard \kbd{FILE*},14069containing further the file name, its type (regular file, pipe, input or14070output file, etc.). The following functions create and manipulate this14071structure; they are integrated within PARI's diagnostics system, printing14072messages if \kbd{DEBUGFILES} is high enough.1407314074\fun{pariFILE*}{pari_fopen}{const char *s, const char *mode} wrapper14075around \kbd{fopen(s, mode)}, return \kbd{NULL} on failure.1407614077\fun{pariFILE*}{pari_fopen_or_fail}{const char *s, const char *mode}14078simple wrapper around \kbd{fopen(s, mode)}; error on failure.1407914080\fun{pariFILE*}{pari_fopengz}{const char *s} opens the file whose name is14081$s$, and associates a (read-only) \kbd{pariFILE} with it. If $s$ is a14082compressed file (\kbd{.gz} suffix), it is uncompressed on the fly.14083If $s$ cannot be opened, also try to open \kbd{$s$.gz}. Returns \kbd{NULL}14084on failure.1408514086\fun{void}{pari_fclose}{pariFILE *f} closes14087the underlying file descriptor and deletes the \kbd{pariFILE} struct.1408814089\fun{pariFILE*}{pari_safefopen}{const char *s, const char *mode}14090creates a \emph{new} file $s$ (a priori for writing) with \kbd{600}14091permissions. Error if the file already exists. To avoid symlink attacks,14092a symbolic link exists, regardless of where it points to.1409314094\subsec{Temporary files}1409514096PARI has its own idea of the system temp directory derived from an14097environment variable (\kbd{\$GPTMPDIR}, else \kbd{\$TMPDIR}), or the first14098writable directory among \kbd{/tmp}, \kbd{/var/tmp} and \kbd{.}.1409914100\fun{char*}{pari_unique_dir}{const char *s} creates a ``unique directory''14101and return its name built from the string $s$, the user id and process pid14102(on Unix systems). This directory is itself located in the temp14103directory mentioned above. The name returned is \tet{malloc}'ed.1410414105\fun{char*}{pari_unique_filename}{const char *s} creates a \emph{new} empty14106file in the temp directory, whose name contains the id-string $s$ (truncated14107to its first $8$ chars), followed by a system-dependent suffix (incorporating14108the ids of both the user and the running process, for instance). The function14109returns the tempfile name and creates an empty file with that name. The name14110returned is \tet{malloc}'ed.1411114112\fun{char*}{pari_unique_filename_suffix}{const char *s, const char *suf}14113analogous to above \tet{pari_unique_filename}, creating a (previously14114nonexistent) tempfile whose name ends with suffix \kbd{suf}.1411514116\section{Errors}\label{se:errors}1411714118This section documents the various error classes, and the corresponding14119arguments to \tet{pari_err}. The general syntax is1412014121\fun{void}{pari_err}{numerr,...}1412214123\noindent In the sequel, we mostly use sequences of arguments of the form14124\bprog14125const char *s14126const char *fmt, ...14127@eprog\noindent where \kbd{fmt} is a PARI14128format, producing a string $s$ from the remaining arguments. Since14129providing the correct arguments to \tet{pari_err} is quite error-prone, we14130also provide specialized routines \kbd{pari\_err\_\var{ERRORCLASS}(\dots)}14131instead of \kbd{pari\_err(e\_\var{ERRORCLASS}, \dots)} so that the C compiler14132can check their arguments.1413314134\noindent We now inspect the list of valid keywords (error classes) for14135\kbd{numerr}, and the corresponding required arguments.1413614137\subsec{Internal errors, ``system'' errors}1413814139\subsubsec{e\_ARCH} A requested feature $s$ is not available on this14140architecture or operating system.14141\bprog14142pari_err(e_ARCH)14143@eprog\noindent prints the error message: \kbd{sorry, '$s$' not available on14144this system}.1414514146\subsubsec{e\_BUG} A bug in the PARI library, in function $s$.14147\bprog14148pari_err(e_BUG, const char *s)14149pari_err_BUG(const char *s)14150@eprog\noindent prints the error message: \kbd{Bug in $s$, please report}.1415114152\subsubsec{e\_FILE} Error while trying to open a file.14153\bprog14154pari_err(e_FILE, const char *what, const char *name)14155pari_err_FILE(const char *what, const char *name)14156@eprog\noindent prints the error message: \kbd{error opening14157\emph{what}: `\emph{name}'}.1415814159\subsubsec{e\_FILEDESC} Error while handling a file descriptor.14160\bprog14161pari_err(e_FILEDESC, const char *where, long n)14162pari_err_FILEDESC(const char *where, long n)14163@eprog\noindent prints the error message: \kbd{invalid file descriptor in14164\emph{where}: `\emph{name}'}.1416514166\subsubsec{e\_IMPL} A requested feature $s$ is not implemented.14167\bprog14168pari_err(e_IMPL, const char *s)14169pari_err_IMPL(const char *s)14170@eprog\noindent prints the error message: \kbd{sorry, $s$ is not yet14171implemented}.1417214173\subsubsec{e\_PACKAGE} Missing optional package $s$.14174\bprog14175pari_err(e_PACKAGE, const char *s)14176pari_err_PACKAGE(const char *s)14177@eprog\noindent prints the error message: \kbd{package $s$ is required,14178please install it}1417914180\subsec{Syntax errors, type errors}1418114182\subsubsec{e\_DIM} arguments submitted to function $s$ have inconsistent14183dimensions. E.g., when solving a linear system, or trying to compute the14184determinant of a nonsquare matrix.14185\bprog14186pari_err(e_DIM, const char *s)14187pari_err_DIM(const char *s)14188@eprog\noindent prints the error message: \kbd{inconsistent dimensions in $s$}.1418914190\subsubsec{e\_FLAG} A flag argument is out of bounds in function $s$.14191\bprog14192pari_err(e_FLAG, const char *s)14193pari_err_FLAG(const char *s)14194@eprog\noindent prints the error message: \kbd{invalid flag in $s$}.1419514196\subsubsec{e\_NOTFUNC} Generated by the PARI evaluator; tried to use a14197\kbd{GEN} which is not a \typ{CLOSURE} in a function call syntax (as in14198\kbd{f = 1; f(2);}).14199\bprog14200pari_err(e_NOTFUNC, GEN fun)14201@eprog\noindent prints the error message: \kbd{not a function in a function14202call}.1420314204\subsubsec{e\_OP} Impossible operation between two objects than cannot be14205typecast to a sensible common domain for deeper reasons than a type mismatch,14206usually for arithmetic reasons. As in \kbd{O(2) + O(3)}: it is valid to add14207two \typ{PADIC}s, provided the underlying prime is the same; so the addition14208is not forbidden a priori for type reasons, it only becomes so when14209inspecting the objects and trying to perform the operation.14210\bprog14211pari_err(e_OP, const char *op, GEN x, GEN y)14212pari_err_OP(const char *op, GEN x, GEN y)14213@eprog\noindent As \kbd{e\_TYPE2}, replacing \kbd{forbidden} by14214\kbd{inconsistent}.1421514216\subsubsec{e\_PRIORITY} object $o$ in function $s$ contains14217variables whose priority is incompatible with the expected operation.14218E.g.~\kbd{Pol([x,1], 'y)}: this raises an error because it's not possible to14219create a polynomial whose coefficients involve variables with higher priority14220than the main variable.14221\bprog14222pari_err(e_PRIORITY, const char *s, GEN o, const char *op, long v)14223pari_err_PRIORITY(const char *s, GEN o, const char *op, long v)14224@eprog\noindent prints the error message: \kbd{incorrect priority14225in $s$, variable $v_o$ \var{op} $v$}, were $v_o$ is \kbd{gvar(o)}.1422614227\subsubsec{e\_SYNTAX} Syntax error, generated by the PARI parser.14228\bprog14229pari_err(e_SYNTAX, const char *msg, const char *e, const char *entry)14230@eprog\noindent where \kbd{msg} is a complete error message, and \kbd{e} and14231\kbd{entry} point into the \emph{same} character string, which is the input14232that was incorrectly parsed: \kbd{e} points to the character where the parser14233failed, and $\kbd{entry}\leq \kbd{e}$ points somewhat before.1423414235\noindent Prints the error message: \kbd{msg}, followed by a colon, then14236a part of the input character string (in general \kbd{entry} itself, but an14237initial segment may be truncated if $\kbd{e}-\kbd{entry}$ is large); a caret14238points at \kbd{e}, indicating where the error took place.1423914240\subsubsec{e\_TYPE} An argument $x$ of function $s$ had an unexpected type.14241(As in \kbd{factor("blah")}.)14242\bprog14243pari_err(e_TYPE, const char *s, GEN x)14244pari_err_TYPE(const char *s, GEN x)14245@eprog\noindent prints the error message: \kbd{incorrect type in $s$14246(\typ{$x$})}, where \typ{$x$} is the type of $x$.1424714248\subsubsec{e\_TYPE2} Forbidden operation between two objects than cannot be14249typecast to a sensible common domain, because their types do not match up.14250(As in \kbd{Mod(1,2) + Pi}.)14251\bprog14252pari_err(e_TYPE2, const char *op, GEN x, GEN y)14253pari_err_TYPE2(const char *op, GEN x, GEN y)14254@eprog\noindent prints the error message: \kbd{forbidden} $s$14255\typ{$x$} \var{op} \typ{$y$}, where \typ{$z$} denotes the type of $z$.14256Here, $s$ denotes the spelled out name of the operator14257$\var{op}\in\{\kbd{+}, \kbd{*}, \kbd{/}, \kbd{\%}, \kbd{=}\}$, e.g.14258\emph{addition} for \kbd{"+"} or \emph{assignment} for \kbd{"="}. If \var{op}14259is not in the above operator, list, it is taken to be the already spelled out14260name of a function, e.g. \kbd{"gcd"}, and the error message becomes14261\kbd{forbidden} \var{op} \typ{$x$}, \typ{$y$}.1426214263\subsubsec{e\_VAR} polynomials $x$ and $y$ submitted to function $s$ have14264inconsistent variables. E.g., considering the algebraic number14265\kbd{Mod(t,t\pow2+1)} in \kbd{nfinit(x\pow2+1)}.14266\bprog14267pari_err(e_VAR, const char *s, GEN x, GEN y)14268pari_err_VAR(const char *s, GEN x, GEN y)14269@eprog\noindent prints the error message: \kbd{inconsistent variables in $s$14270$X$ != $Y$}, where $X$ and $Y$ are the names of the variables of $x$ and $y$,14271respectively.1427214273\subsec{Overflows}1427414275\subsubsec{e\_COMPONENT} Trying to access an inexistent component of a14276vector/matrix/list: the index is less than $1$ or greater14277than the allowed length.14278\bprog14279pari_err(e_COMPONENT, const char *f, const char *op, GEN lim, GEN x)14280pari_err_COMPONENT(const char *f, const char *op, GEN lim, GEN x)14281@eprog\noindent prints the error message: \kbd{nonexistent component in $f$:14282index \var{op} \var{lim}}. Special case: if $f$ is the empty string (no14283meaningful public function name can be used), we ignore it and print the14284message: \kbd{nonexistent component: index \var{op} \var{lim}}.1428514286\subsubsec{e\_DOMAIN} An argument $x$ is not in the function's domain (as in14287\kbd{moebius(0)} or \kbd{zeta(1)}).14288\bprog14289pari_err(e_DOMAIN, char *f, char *v, char *op, GEN lim, GEN x)14290pari_err_DOMAIN(char *f, char *v, char *op, GEN lim, GEN x)14291@eprog\noindent prints the error message: \kbd{domain error in $f$: $v$14292\var{op} \var{lim}}. Special case: if \var{op} is the empty string, we ignore14293\var{lim} and print the error message: \kbd{domain error in $f$: $v$ out of14294range}.1429514296\subsubsec{e\_MAXPRIME} A function using the precomputed list of prime numbers14297ran out of primes.14298\bprog14299pari_err(e_MAXPRIME, ulong c)14300pari_err_MAXPRIME(ulong c)14301@eprog\noindent prints the error message: \kbd{not enough precomputed primes,14302need primelimit \til $c$} if $c$ is nonzero. And simply \kbd{not enough14303precomputed primes} otherwise.1430414305\subsubsec{e\_MEM} A call to \tet{pari_malloc} or \tet{pari_realloc} failed.14306\bprog14307pari_err(e_MEM)14308@eprog\noindent prints the error message: \kbd{not enough memory}.1430914310\subsubsec{e\_OVERFLOW} An object in function $s$ becomes too large to be14311represented within PARI's hardcoded limits. (As in \kbd{2\pow2\pow2\pow10}14312or \kbd{exp(1e100)}, which overflow in \kbd{lg} and \kbd{expo}.)14313\bprog14314pari_err(e_OVERFLOW, const char *s)14315pari_err_OVERFLOW(const char *s)14316@eprog\noindent prints the error message: \kbd{overflow in $s$}.1431714318\subsubsec{e\_PREC} Function $s$ fails because input accuracy is too low.14319(As in \kbd{floor(1e100)} at default accuracy.)14320\bprog14321pari_err(e_PREC, const char *s)14322pari_err_PREC(const char *s)14323@eprog\noindent prints the error message: \kbd{precision too low in $s$}.1432414325\subsubsec{e\_STACK} The PARI stack overflows.14326\bprog14327pari_err(e_STACK)14328@eprog\noindent prints the error message: \kbd{the PARI stack overflows !}14329as well as some statistics concerning stack usage.1433014331\subsec{Errors triggered intentionally}1433214333\subsubsec{e\_ALARM} A timeout, generated by the \tet{alarm} function.14334\bprog14335pari_err(e_ALARM, const char *fmt, ...)14336@eprog\noindent prints the error message: $s$.1433714338\subsubsec{e\_USER} A user error, as triggered by \tet{error}($g_1,\dots,g_n)$14339in GP.14340\bprog14341pari_err(e_USER, GEN g)14342@eprog\noindent prints the error message: \kbd{user error:}, then the14343entries of the vector $g$.1434414345\subsec{Mathematical errors}1434614347\subsubsec{e\_CONSTPOL} An argument of function $s$ is a constant polynomial,14348which does not make sense. (As in \kbd{galoisinit(Pol(1))}.)14349\bprog14350pari_err(e_CONSTPOL, const char *s)14351pari_err_CONSTPOL(const char *s)14352@eprog\noindent prints the error message: \kbd{constant polynomial in $s$}.1435314354\subsubsec{e\_COPRIME} Function $s$ expected two coprime arguments, and did14355receive $x$, $y$ which were not.14356\bprog14357pari_err(e_COPRIME, const char *s, GEN x, GEN y)14358pari_err_COPRIME(const char *s, GEN x, GEN y)14359@eprog\noindent prints the error message: \kbd{elements not coprime in $s$:14360$x, y$}.1436114362\subsubsec{e\_INV} Tried to invert a noninvertible object $x$.14363\bprog14364pari_err(e_INV, const char *s, GEN x)14365pari_err_INV(const char *s, GEN x)14366@eprog\noindent prints the error message: \kbd{impossible inverse in $s$: $x$}.14367If $x = \kbd{Mod}(a,b)$ is a \typ{INTMOD} and $a$ is not $0$ mod $b$, this14368allows to factor the modulus, as \kbd{gcd}$(a,b)$ is a nontrivial divisor of14369$b$.1437014371\subsubsec{e\_IRREDPOL} Function $s$ expected an irreducible polynomial,14372and did not receive one. (As in \kbd{nfinit(x\pow2-1)}.)14373\bprog14374pari_err(e_IRREDPOL, const char *s, GEN x)14375pari_err_IRREDPOL(const char *s, GEN x)14376@eprog\noindent prints the error message: \kbd{not an irreducible polynomial14377in $s$: $x$}.1437814379\subsubsec{e\_MISC} Generic uncategorized error.14380\bprog14381pari_err(e_MISC, const char *fmt, ...)14382@eprog\noindent prints the error message: $s$.1438314384\subsubsec{e\_MODULUS} moduli $x$ and $y$ submitted to function $s$ are14385inconsistent. E.g., considering the algebraic number14386\kbd{Mod(t,t\pow2+1)} in \kbd{nfinit(t\pow3-2)}.14387\bprog14388pari_err(e_MODULUS, const char *s, GEN x, GEN y)14389pari_err_MODULUS(const char *s, GEN x, GEN y)14390@eprog\noindent prints the error message: \kbd{inconsistent moduli in $s$},14391then the moduli.1439214393\subsubsec{e\_PRIME} Function $s$ expected a prime number, and did receive $p$,14394which was not. (As in \kbd{idealprimedec(nf, 4)}.)14395\bprog14396pari_err(e_PRIME, const char *s, GEN x)14397pari_err_PRIME(const char *s, GEN x)14398@eprog\noindent prints the error message: \kbd{not a prime in $s$: $x$}.1439914400\subsubsec{e\_ROOTS0} An argument of function $s$ is a zero polynomial, and14401we need to consider its roots. (As in \kbd{polroots(0)}.)14402\bprog14403pari_err(e_ROOTS0, const char *s)14404pari_err_ROOTS0(const char *s)14405@eprog\noindent prints the error message: \kbd{zero polynomial in $s$}.1440614407\subsubsec{e\_SQRTN} Tried to compute an $n$-th root of $x$, which does not14408exist, in function $s$.14409(As in \kbd{sqrt(Mod(-1,3))}.)14410\bprog14411pari_err(e_SQRTN, GEN x)14412pari_err_SQRTN(GEN x)14413@eprog\noindent prints the error message: \kbd{not an n-th power residue in14414$s$: $x$}.1441514416\subsec{Miscellaneous functions}1441714418\fun{long}{name_numerr}{const char *s} return the error number corresponding to14419an error name. E.g. \kbd{name\_numerr("e\_DIM")} returns \kbd{e\_DIM}.1442014421\fun{const char*}{numerr_name}{long errnum} returns the error name14422corresponding to an error number. E.g. \kbd{name\_numerr(e\_DIM)} returns14423\kbd{"e\_DIM"}.1442414425\fun{char*}{pari_err2str}{GEN err} returns the error message that would be14426printed on \typ{ERROR} \kbd{err}. The name is allocated on the PARI stack and14427must not be freed.1442814429\section{Hashtables}14430A \tet{hashtable}, or associative array, is a set of pairs $(k,v)$ of keys14431and values. PARI implements general extensible hashtables for fast data14432retrieval: when creating a table, we may either choose to use the PARI stack,14433or \kbd{malloc} so as to be stack-independent. A hashtable is implemented as14434a table of linked lists, each list containing all entries sharing the same14435hash value. The table length is a prime number, which roughly doubles as the14436table overflows by gaining new entries; both the current number of entries14437and the threshold before the table grows are stored in the table. Finally the14438table remembers the functions used to hash the entries's keys and to test for14439equality two entries hashed to the same value.1444014441An entry, or \tet{hashentry}, contains1444214443\item a key/value pair $(k,v)$, both of type \kbd{void*} for maximal14444flexibility,1444514446\item the hash value of the key, for the table hash function. This hash is14447mapped to a table index (by reduction modulo the table length), but it14448contains more information, and is used to bypass costly general equality14449tests if possible,1445014451\item a link pointer to the next entry sharing the same table cell.1445214453\bprog14454typedef struct {14455void *key, *val;14456ulong hash; /* hash(key) */14457struct hashentry *next;14458} hashentry;1445914460typedef struct {14461ulong len; /* table length */14462hashentry **table; /* the table */14463ulong nb, maxnb; /* number of entries stored and max nb before enlarging */14464ulong pindex; /* prime index */14465ulong (*hash) (void *k); /* hash function */14466int (*eq) (void *k1, void *k2); /* equality test */14467int use_stack; /* use the PARI stack, resp. malloc */14468} hashtable;14469@eprog\noindent1447014471\fun{hashtable*}{hash_create}{size, hash, eq, use_stack}14472\vskip -0.5em % switch to K&R style to avoid atrocious line break14473\bprog14474ulong size;14475ulong (*hash)(void*);14476int (*eq)(void*,void*);14477int use_stack;14478@eprog\noindent14479creates a hashtable with enough room to contain14480\kbd{size} entries. The functions \kbd{hash} and \kbd{eq} compute the hash14481value of keys and test keys for equality, respectively. If \kbd{use\_stack}14482is non zero, the resulting table will use the PARI stack; otherwise, we use14483\kbd{malloc}.1448414485\fun{hashtable*}{hash_create_ulong}{ulong size, long stack} special case14486when the keys are \kbd{ulongs} with ordinary equality test.1448714488\fun{hashtable*}{hash_create_str}{ulong size, long stack} special case14489when the keys are character strings with string equality test (and14490\tet{hash_str} hash function).1449114492\fun{void}{hash_init}{hashtable *h, ulong size, ulong (*hash)(void*),14493int (*eq)(void*,void*), use_stack}14494Initialize \kbd{h} for an hashtable with enough room to contain14495\kbd{size} entries of type \kbd{void*}. The functions \kbd{eq} test keys for14496equality. If \kbd{use\_stack} is non zero, the resulting table will use the14497PARI stack; otherwise, we use \kbd{malloc}.1449814499\fun{void}{hash_init_GEN}{hashtable *h, ulong size, int (*eq)(GEN,GEN), use_stack}14500Initialize \kbd{h} for an hashtable with enough room to contain14501\kbd{size} entries of type \kbd{GEN}. The functions \kbd{eq} test keys for14502equality. If \kbd{use\_stack} is non zero, the resulting table will use the14503PARI stack; otherwise, we use \kbd{malloc}. The hash used is \kbd{hash\_GEN}.1450414505\fun{void}{hash_init_ulong}{hashtable *h, ulong size, use_stack}14506Initialize \kbd{h} for an hashtable with enough room to contain14507\kbd{size} entries of type \kbd{ulong}.14508If \kbd{use\_stack} is non zero, the resulting table will use the14509PARI stack; otherwise, we use \kbd{malloc}.1451014511\fun{void}{hash_insert}{hashtable *h, void *k, void *v} inserts $(k,v)$14512in hashtable $h$. No copy is made: $k$ and $v$ themselves are stored. The14513implementation does not prevent one to insert two entries with equal14514keys $k$, but which of the two is affected by later commands is undefined.1451514516\fun{void}{hash_insert2}{hashtable *h, void *k, void *v, ulong hash}14517as \kbd{hash\_insert}, assuming \kbd{h->hash(k)} is \kbd{hash}.1451814519\fun{void}{hash_insert_long}{hashtable *h, void *k, long v} as14520\kbd{hash\_insert} but \kbd{v} is a \kbd{long}.1452114522\fun{hashentry*}{hash_search}{hashtable *h, void *k} look for an entry14523with key $k$ in $h$. Return it if it one exists, and \kbd{NULL} if not.1452414525\fun{hashentry*}{hash_search2}{hashtable *h, void *k, ulong hash} as14526\kbd{hash\_search} assuming \kbd{h->hash(k)} is \kbd{hash}.1452714528\fun{GEN}{hash_haskey_GEN}{hashtable *h, void *k}14529returns the associate value if the key $k$ belongs to the hash,14530otherwise returns \kbd{NULL}.1453114532\fun{int}{hash_haskey_long}{hashtable *h, void *k, long *v}14533returns $1$ if the key $k$ belongs to the hash and set $v$ to its value,14534otherwise returns 0.1453514536\fun{hashentry *}{hash_select}{hashtable *h, void *k, void *E, int (*select)(void *, hashentry *)} variant of \tet{hash_search}, useful when entries14537with identical keys are inserted: among the entries attached to14538key $k$, return one satisfying the selection criterion (such that14539\kbd{select(E,e)} is nonzero), or \kbd{NULL} if none exist.1454014541\fun{hashentry*}{hash_remove}{hashtable *h, void *k} deletes an entry $(k,v)$14542with key $k$ from $h$ and return it. (Return \kbd{NULL} if none was found.)14543Only the linking structures are freed, memory attached to $k$ and $v$14544is not reclaimed.1454514546\fun{hashentry*}{hash_remove_select}{hashtable *h, void *k, void *E, int(*select)(void*, hashentry *)}14547a variant of \tet{hash_remove}, useful when entries with identical keys are14548inserted: among the entries attached to key $k$, return one satisfying the14549selection criterion (such that \kbd{select(E,e)} is nonzero) and delete it,14550or \kbd{NULL} if none exist. Only the linking structures are freed, memory14551attached to $k$ and $v$ is not reclaimed.1455214553\fun{GEN}{hash_keys}{hashtable *h} return in a \typ{VECSMALL} the keys14554stored in hashtable $h$.1455514556\fun{GEN}{hash_values}{hashtable *h} return in a \typ{VECSMALL}14557the values stored in hashtable $h$.1455814559\fun{void}{hash_destroy}{hashtable *h} deletes the hashtable, by removing all14560entries.1456114562\fun{void}{hash_dbg}{hashtable *h} print statistics for hashtable $h$, allows14563to evaluate the attached hash function performance on actual data.1456414565Some interesting hash functions are available:1456614567\fun{ulong}{hash_str}{const char *s}1456814569\fun{ulong}{hash_str_len}{const char *s, long len} hash the prefix string14570containing the first \kbd{len} characters (assume $\kbd{strlen}(s) \geq14571\kbd{len}$).1457214573\fun{ulong}{hash_GEN}{GEN x} generic hash function.1457414575\fun{ulong}{hash_zv}{GEN x} hash a \typ{VECSMALL}.1457614577\section{Dynamic arrays}1457814579A \teb{dynamic array} is a generic way to manage stacks of data that need14580to grow dynamically. It allocates memory using \kbd{pari\_malloc}, and is14581independent of the PARI stack; it even works before the \kbd{pari\_init} call.1458214583\subsec{Initialization}1458414585To create a stack of objects of type \kbd{foo}, we proceed as follows:14586\bprog14587foo *t_foo;14588pari_stack s_foo;14589pari_stack_init(&s_foo, sizeof(*t_foo), (void**)&t_foo);14590@eprog\noindent Think of \kbd{s\_foo} as the controlling interface, and14591\kbd{t\_foo} as the (dynamic) array tied to it. The value of \kbd{t\_foo}14592may be changed as you add more elements.1459314594\subsec{Adding elements}14595The following function pushes an element on the stack.14596\bprog14597/* access globals t_foo and s_foo */14598void push_foo(foo x)14599{14600long n = pari_stack_new(&s_foo);14601t_foo[n] = x;14602}14603@eprog1460414605\subsec{Accessing elements}1460614607Elements are accessed naturally through the \kbd{t\_foo} pointer.14608For example this function swaps two elements:14609\bprog14610void swapfoo(long a, long b)14611{14612foo x;14613if (a > s_foo.n || b > s_foo.n) pari_err_BUG("swapfoo");14614x = t_foo[a];14615t_foo[a] = t_foo[b];14616t_foo[b] = x;14617}14618@eprog1461914620\subsec{Stack of stacks}14621Changing the address of \kbd{t\_foo} is not supported in general.14622In particular \kbd{realloc()}'ed array of stacks and stack of stacks are not14623supported.1462414625\subsec{Public interface}14626Let \kbd{s} be a \kbd{pari\_stack} and \kbd{data} the data linked to it. The14627following public fields are defined:1462814629\item \kbd{s.alloc} is the number of elements allocated for \kbd{data}.1463014631\item \kbd{s.n} is the number of elements in the stack and \kbd{data[s.n-1]} is14632the topmost element of the stack. \kbd{s.n} can be changed as long as14633$0\leq\kbd{s.n}\leq\kbd{s.alloc}$ holds.1463414635\fun{void}{pari_stack_init}{pari_stack *s, size_t size, void **data} links14636\kbd{*s} to the data pointer \kbd{*data}, where \kbd{size} is the size of14637data element. The pointer \kbd{*data} is set to \kbd{NULL}, \kbd{s->n} and14638\kbd{s->alloc} are set to $0$: the array is empty.1463914640\fun{void}{pari_stack_alloc}{pari_stack *s, long nb} makes room for \kbd{nb}14641more elements, i.e.~makes sure that $\kbd{s.alloc}\geq\kbd{s.n} + \kbd{nb}$,14642possibly reallocating \kbd{data}.1464314644\fun{long}{pari_stack_new}{pari_stack *s} increases \kbd{s.n} by one unit,14645possibly reallocating \kbd{data}, and returns $\kbd{s.n}-1$.1464614647\misctitle{Caveat} The following construction is incorrect because14648\kbd{stack\_new} can change the value of \kbd{t\_foo}:14649\bprog14650t_foo[ pari_stack_new(&s_foo) ] = x;14651@eprog1465214653\fun{void}{pari_stack_delete}{pari_stack *s} frees \kbd{data} and resets the14654stack to the state immediately following \kbd{stack\_init} (\kbd{s->n} and14655\kbd{s->alloc} are set to $0$).1465614657\fun{void *}{pari_stack_pushp}{pari_stack *s, void *u} This function assumes14658that \kbd{*data} is of pointer type. Pushes the element \kbd{u} on the stack14659\kbd{s}.1466014661\fun{void **}{pari_stack_base}{pari_stack *s} returns the address of \kbd{data},14662typecast to a \kbd{void **}.1466314664\section{Vectors and Matrices}1466514666\subsec{Access and extract}14667See~\secref{se:clean} and~\secref{se:unclean} for various useful constructors.14668Coefficients are accessed and set using \tet{gel}, \tet{gcoeff},14669see~\secref{se:accessors}. There are many internal functions to extract or14670manipulate subvectors or submatrices but, like the accessors above, none of14671them are suitable for \tet{gerepileupto}. Worse, there are no type14672verification, nor bound checking, so use at your own risk.1467314674\fun{GEN}{shallowcopy}{GEN x} returns a \kbd{GEN} whose components are the14675components of $x$ (no copy is made). The result may now be used to compute in14676place without destroying $x$. This is essentially equivalent to14677\bprog14678GEN y = cgetg(lg(x), typ(x));14679for (i = 1; i < lg(x); i++) y[i] = x[i];14680return y;14681@eprog\noindent14682except that \typ{MAT} is treated specially since shallow copies of all columns14683are made. The function also works for nonrecursive types, but is useless14684in that case since it makes a deep copy. If $x$ is known to be a \typ{MAT}, you14685may call \tet{RgM_shallowcopy} directly; if $x$ is known not to be a \typ{MAT},14686you may call \tet{leafcopy} directly.1468714688\fun{GEN}{RgM_shallowcopy}{GEN x} returns \kbd{shallowcopy(x)}, where $x$14689is a \typ{MAT}.1469014691\fun{GEN}{shallowtrans}{GEN x} returns the transpose of $x$, \emph{without}14692copying its components, i.~e.,~it returns a \kbd{GEN} whose components are14693(physically) the components of $x$. This is the internal function underlying14694\tet{gtrans}.1469514696\fun{GEN}{shallowconcat}{GEN x, GEN y} concatenate $x$ and $y$, \emph{without}14697copying components, i.~e.,~it returns a \kbd{GEN} whose components are14698(physically) the components of $x$ and $y$.1469914700\fun{GEN}{shallowconcat1}{GEN x}14701$x$ must be \typ{VEC}, \typ{COL} or \typ{LIST}, concatenate14702its elements from left to right. Shallow version of \kbd{gconcat1}.1470314704\fun{GEN}{shallowmatconcat}{GEN v} shallow version of \kbd{matconcat}.1470514706\fun{GEN}{shallowextract}{GEN x, GEN y} extract components14707of the vector or matrix $x$ according to the selection parameter $y$.14708This is the shallow analog of \kbd{extract0(x, y, NULL)}, see \tet{vecextract}.14709\kbdsidx{extract0}1471014711\fun{GEN}{shallowmatextract}{GEN M, GEN l1, GEN l2} extract components of the14712matrix $M$ according to the \typ{VECSMALL} $l1$ (list of lines indices) and14713$l2$ (list of columns indices).14714This is the shallow analog of \kbd{extract0(x, l1, l2)}, see \tet{vecextract}.14715\kbdsidx{extract0}1471614717\fun{GEN}{RgM_minor}{GEN A, long i, long j} given a square \typ{MAT} A,14718return the matrix with $i$-th row and $j$-th column removed.1471914720\fun{GEN}{vconcat}{GEN A, GEN B} concatenate vertically the two \typ{MAT} $A$14721and $B$ of compatible dimensions. A \kbd{NULL} pointer is accepted for an14722empty matrix. See \tet{shallowconcat}.1472314724\fun{GEN}{matslice}{GEN A, long a, long b, long c, long d}14725returns the submatrix $A[a..b,c..d]$. Assume $a \leq b$ and $c \leq d$.1472614727\fun{GEN}{row}{GEN A, long i} return $A[i,]$, the $i$-th row of the \typ{MAT}14728$A$.1472914730\fun{GEN}{row_i}{GEN A, long i, long j1, long j2} return part of the $i$-th14731row of \typ{MAT}~$A$: $A[i,j_1]$, $A[i,j_1+1]\dots,A[i,j_2]$. Assume $j_114732\leq j_2$.1473314734\fun{GEN}{rowcopy}{GEN A, long i} return the row $A[i,]$ of14735the~\typ{MAT}~$A$. This function is memory clean and suitable for14736\kbd{gerepileupto}. See \kbd{row} for the shallow equivalent.1473714738\fun{GEN}{rowslice}{GEN A, long i1, long i2} return the \typ{MAT}14739formed by the $i_1$-th through $i_2$-th rows of \typ{MAT} $A$. Assume $i_114740\leq i_2$.1474114742\fun{GEN}{rowsplice}{GEN A, long i} return the \typ{MAT} formed from the14743coefficients of \typ{MAT} $A$ with $j$-th row removed.1474414745\fun{GEN}{rowpermute}{GEN A, GEN p}, $p$ being a \typ{VECSMALL}14746representing a list $[p_1,\dots,p_n]$ of rows of \typ{MAT} $A$, returns the14747matrix whose rows are $A[p_1,],\dots, A[p_n,]$.1474814749\fun{GEN}{rowslicepermute}{GEN A, GEN p, long x1, long x2}, short for14750\bprog14751rowslice(rowpermute(A,p), x1, x2)14752@eprog\noindent14753(more efficient).1475414755\fun{GEN}{vecslice}{GEN A, long j1, long j2}, return $A[j_1], \dots,14756A[j_2]$. If $A$ is a \typ{MAT}, these correspond to \emph{columns} of $A$.14757The object returned has the same type as $A$ (\typ{VEC}, \typ{COL} or14758\typ{MAT}). Assume $j_1 \leq j_2$.1475914760\fun{GEN}{vecsplice}{GEN A, long j} return $A$ with $j$-th entry removed14761(\typ{VEC}, \typ{COL}) or $j$-th column removed (\typ{MAT}).1476214763\fun{GEN}{vecreverse}{GEN A}. Returns a \kbd{GEN} which has the same14764type as $A$ (\typ{VEC}, \typ{COL} or \typ{MAT}), and whose components14765are the $A[n],\dots,A[1]$. If $A$ is a \typ{MAT}, these are the14766\emph{columns} of $A$.1476714768\fun{void}{vecreverse_inplace}{GEN A} as \kbd{vecreverse}, but reverse14769$A$ in place.1477014771\fun{GEN}{vecpermute}{GEN A, GEN p} $p$ is a \typ{VECSMALL} representing14772a list $[p_1,\dots,p_n]$ of indices. Returns a \kbd{GEN} which has the same14773type as $A$ (\typ{VEC}, \typ{COL} or \typ{MAT}), and whose components14774are $A[p_1],\dots,A[p_n]$. If $A$ is a \typ{MAT}, these are the14775\emph{columns} of $A$.1477614777\fun{GEN}{vecsmallpermute}{GEN A, GEN p} as \kbd{vecpermute} when \kbd{A} is a14778\typ{VECSMALL}.1477914780\fun{GEN}{vecslicepermute}{GEN A, GEN p, long y1, long y2} short for14781\bprog14782vecslice(vecpermute(A,p), y1, y2)14783@eprog\noindent14784(more efficient).1478514786\subsec{Componentwise operations}1478714788The following convenience routines automate trivial loops of the form14789\bprog14790for (i = 1; i < lg(a); i++) gel(v,i) = f(gel(a,i), gel(b,i))14791@eprog\noindent14792for suitable $f$:1479314794\fun{GEN}{vecinv}{GEN a}. Given a vector $a$,14795returns the vector whose $i$-th component is \kbd{ginv}$(a[i])$.1479614797\fun{GEN}{vecmul}{GEN a, GEN b}. Given $a$ and $b$ two vectors of the same14798length, returns the vector whose $i$-th component is \kbd{gmul}$(a[i], b[i])$.1479914800\fun{GEN}{vecdiv}{GEN a, GEN b}. Given $a$ and $b$ two vectors of the same14801length, returns the vector whose $i$-th component is \kbd{gdiv}$(a[i], b[i])$.1480214803\fun{GEN}{vecpow}{GEN a, GEN n}. Given $n$ a \typ{INT}, returns14804the vector whose $i$-th component is $a[i]^n$.1480514806\fun{GEN}{vecmodii}{GEN a, GEN b}. Assuming $a$ and $b$ are two \kbd{ZV}14807of the same length, returns the vector whose $i$-th component14808is \kbd{modii}$(a[i], b[i])$.1480914810\fun{GEN}{vecmoduu}{GEN a, GEN b}. Assuming $a$ and $b$ are two \typ{VECSMALL}14811of the same length, returns the vector whose $i$-th component14812is $a[i]~\kbd{\%}~b[i]$.1481314814Note that \kbd{vecadd} or \kbd{vecsub} do not exist since \kbd{gadd}14815and \kbd{gsub} have the expected behavior. On the other hand,14816\kbd{ginv} does not accept vector types, hence \kbd{vecinv}.1481714818\subsec{Low-level vectors and columns functions}1481914820These functions handle \typ{VEC} as an abstract container type of14821\kbd{GEN}s. No specific meaning is attached to the content. They accept both14822\typ{VEC} and \typ{COL} as input, but \kbd{col} functions always return14823\typ{COL} and \kbd{vec} functions always return \typ{VEC}.1482414825\misctitle{Note} All the functions below are shallow.1482614827\fun{GEN}{const_col}{long n, GEN x} returns a \typ{COL} of \kbd{n} components14828equal to \kbd{x}.1482914830\fun{GEN}{const_vec}{long n, GEN x} returns a \typ{VEC} of \kbd{n} components14831equal to \kbd{x}.1483214833\fun{int}{vec_isconst}{GEN v} Returns 1 if all the components of \kbd{v} are14834equal, else returns 0.1483514836\fun{void}{vec_setconst}{GEN v, GEN x} $v$ a pre-existing vector. Set all its14837components to $x$.1483814839\fun{int}{vec_is1to1}{GEN v} Returns 1 if the components of \kbd{v} are14840pair-wise distinct, i.e. if $i\mapsto v[i]$ is a 1-to-1 mapping, else returns148410.1484214843\fun{GEN}{vec_append}{GEN V, GEN s} append \kbd{s} to the vector \kbd{V}.1484414845\fun{GEN}{vec_prepend}{GEN V, GEN s} prepend \kbd{s} to the vector \kbd{V}.1484614847\fun{GEN}{vec_shorten}{GEN v, long n} shortens the vector \kbd{v} to \kbd{n}14848components.1484914850\fun{GEN}{vec_lengthen}{GEN v, long n} lengthens the vector \kbd{v}14851to \kbd{n} components. The extra components are not initialized.1485214853\fun{GEN}{vec_insert}{GEN v, long n, GEN x} inserts $x$ at position $n$ in the vector14854$v$.1485514856\fun{GEN}{vec_equiv}{GEN O} given a vector of objects $O$, return a vector14857with $n$ components where $n$ is the number of distinct objects in $O$. The14858$i$-th component is a \typ{VECSMALL} containing the indices of the elements14859in $O$ having the same value. Applied to the image of a function evaluated on14860some finite set, it computes the fibers of the function.1486114862\fun{GEN}{vec_reduce}{GEN O, GEN *pE} given a vector of objects $O$,14863return the vector $v$ (of the same type as $O$) of \emph{distinct} elements14864of $O$ and set a \typ{VECSMALL} $E$ with the same length as $v$, such14865that $E[i]$ is the multiplicity of object $v[i]$ in the original $O$.14866Shallow function.1486714868\section{Vectors of small integers}1486914870\subsec{\typ{VECSMALL}}1487114872These functions handle \typ{VECSMALL} as an abstract container type14873of small signed integers. No specific meaning is attached to the content.1487414875\fun{GEN}{const_vecsmall}{long n, long c} returns a \typ{VECSMALL}14876of \kbd{n} components equal to \kbd{c}.1487714878\fun{GEN}{vec_to_vecsmall}{GEN z} identical to \kbd{ZV\_to\_zv(z)}.1487914880\fun{GEN}{vecsmall_to_vec}{GEN z} identical to \kbd{zv\_to\_ZV(z)}.1488114882\fun{GEN}{vecsmall_to_col}{GEN z} identical to \kbd{zv\_to\_ZC(z)}.1488314884\fun{GEN}{vecsmall_to_vec_inplace}{GEN z} apply \kbd{stoi} to all entries14885of $z$ and set its type to \typ{VEC}.1488614887\fun{GEN}{vecsmall_copy}{GEN x} makes a copy of \kbd{x} on the stack.1488814889\fun{GEN}{vecsmall_shorten}{GEN v, long n} shortens the \typ{VECSMALL} \kbd{v}14890to \kbd{n} components.1489114892\fun{GEN}{vecsmall_lengthen}{GEN v, long n} lengthens the \typ{VECSMALL}14893\kbd{v} to \kbd{n} components. The extra components are not initialized.1489414895\fun{GEN}{vecsmall_indexsort}{GEN x} performs an indirect sort of the14896components of the \typ{VECSMALL} \kbd{x} and return a permutation stored in a14897\typ{VECSMALL}.1489814899\fun{void}{vecsmall_sort}{GEN v} sorts the \typ{VECSMALL} \kbd{v} in place.1490014901\fun{GEN}{vecsmall_reverse}{GEN v} as \kbd{vecreverse} for a \typ{VECSMALL}14902\kbd{v}.1490314904\fun{long}{vecsmall_max}{GEN v} returns the maximum of the elements of14905\typ{VECSMALL} \kbd{v}, assumed nonempty.1490614907\fun{long}{vecsmall_indexmax}{GEN v} returns the index of the largest14908element of \typ{VECSMALL} \kbd{v}, assumed nonempty.1490914910\fun{long}{vecsmall_min}{GEN v} returns the minimum of the elements of14911\typ{VECSMALL} \kbd{v}, assumed nonempty.1491214913\fun{long}{vecsmall_indexmin}{GEN v} returns the index of the smallest14914element of \typ{VECSMALL} \kbd{v}, assumed nonempty.1491514916\fun{int}{vecsmall_isconst}{GEN v} Returns 1 if all the components of \kbd{v}14917are equal, else returns 0.1491814919\fun{int}{vecsmall_is1to1}{GEN v} Returns 1 if the components of \kbd{v} are14920pair-wise distinct, i.e. if $i\mapsto v[i]$ is a 1-to-1 mapping, else returns149210.1492214923\fun{long}{vecsmall_isin}{GEN v, long x} returns the first index $i$14924such that \kbd{v[$i$]} is equal to \kbd{x}. Naive search in linear time, does14925not assume that \kbd{v} is sorted.1492614927\fun{GEN}{vecsmall_uniq}{GEN v} given a \typ{VECSMALL} \kbd{v}, return14928the vector of unique occurrences.1492914930\fun{GEN}{vecsmall_uniq_sorted}{GEN v} same as \kbd{vecsmall\_uniq}, but assumes14931\kbd{v} sorted.1493214933\fun{long}{vecsmall_duplicate}{GEN v} given a \typ{VECSMALL} \kbd{v}, return14934$0$ if there is no duplicates, or the index of the first duplicate14935(\kbd{vecsmall\_duplicate([1,1])} returns $2$).1493614937\fun{long}{vecsmall_duplicate_sorted}{GEN v} same as14938\kbd{vecsmall\_duplicate}, but assume \kbd{v} sorted.1493914940\fun{int}{vecsmall_lexcmp}{GEN x, GEN y} compares two \typ{VECSMALL} lexically.1494114942\fun{int}{vecsmall_prefixcmp}{GEN x, GEN y} truncate the longest \typ{VECSMALL}14943to the length of the shortest and compares them lexicographically.1494414945\fun{GEN}{vecsmall_prepend}{GEN V, long s} prepend \kbd{s} to the14946\typ{VECSMALL} \kbd{V}.1494714948\fun{GEN}{vecsmall_append}{GEN V, long s} append \kbd{s} to the14949\typ{VECSMALL} \kbd{V}.1495014951\fun{GEN}{vecsmall_concat}{GEN u, GEN v} concat the \typ{VECSMALL} \kbd{u}14952and \kbd{v}.1495314954\fun{long}{vecsmall_coincidence}{GEN u, GEN v} returns the numbers of indices14955where \kbd{u} and \kbd{v} agree.1495614957\fun{long}{vecsmall_pack}{GEN v, long base, long mod} handles the14958\typ{VECSMALL} \kbd{v} as the digit of a number in base \kbd{base} and return14959this number modulo \kbd{mod}. This can be used as an hash function.1496014961\fun{GEN}{vecsmall_prod}{GEN v} given a \typ{VECSMALL} \kbd{v}, return14962the product of its entries.1496314964\subsec{Vectors of \typ{VECSMALL}}14965These functions manipulate vectors of \typ{VECSMALL} (vecvecsmall).1496614967\fun{GEN}{vecvecsmall_sort}{GEN x} sorts lexicographically the components of14968the vector \kbd{x}.1496914970\fun{GEN}{vecvecsmall_sort_shallow}{GEN x}, shallow variant of14971\kbd{vecvecsmall\_sort}.1497214973\fun{void}{vecvecsmall_sort_inplace}{GEN x, GEN *perm} sort lexicographically14974\kbd{x} in place, without copying its components. If14975\kbd{perm} is not \kbd{NULL}, it is set to the permutation that would sort14976the original \kbd{x}.1497714978\fun{GEN}{vecvecsmall_sort_uniq}{GEN x} sorts lexicographically the components of14979the vector \kbd{x}, removing duplicates entries.1498014981\fun{GEN}{vecvecsmall_indexsort}{GEN x} performs an indirect lexicographic14982sorting of the components of the vector \kbd{x} and return a permutation14983stored in a \typ{VECSMALL}.1498414985\fun{long}{vecvecsmall_search}{GEN x, GEN y} \kbd{x} being a sorted14986vecvecsmall and \kbd{y} a \typ{VECSMALL}, search \kbd{y} inside \kbd{x}.1498714988\fun{GEN}{vecvecsmall_max}{GEN x} returns the largest entry in all $x[i]$,14989assumed nonempty. Shallow function.1499014991\newpage14992\chapter{Functions related to the GP interpreter}1499314994\section{Handling closures}\label{se:closure}1499514996\subsec{Functions to evaluate \typ{CLOSURE}}1499714998\fun{void}{closure_disassemble}{GEN C} print the \typ{CLOSURE} \kbd{C} in14999GP assembly format.1500015001\fun{GEN}{closure_callgenall}{GEN C, long n, ...} evaluate the \typ{CLOSURE}15002\kbd{C} with the \kbd{n} arguments (of type \kbd{GEN}) following \kbd{n} in15003the function call. Assumes \kbd{C} has arity $\geq \kbd{n}$.1500415005\fun{GEN}{closure_callgenvec}{GEN C, GEN args} evaluate the \typ{CLOSURE}15006\kbd{C} with the arguments supplied in the vector \kbd{args}. Assumes \kbd{C}15007has arity $\geq \kbd{lg(args)-1}$.1500815009\fun{GEN}{closure_callgenvecprec}{GEN C, GEN args, long prec} as15010\kbd{closure\_callgenvec} but set the precision locally to \kbd{prec}.1501115012\fun{GEN}{closure_callgenvecdef}{GEN C, GEN args, GEN def} evaluate the \typ{CLOSURE}15013\kbd{C} with the arguments supplied in the vector \kbd{args}, where the \typ{VECSMALL}15014\kbd{def} indicates which arguments are actually present.15015Assumes \kbd{C} has arity $\geq \kbd{lg(args)-1}$.1501615017\fun{GEN}{closure_callgenvecdefprec}{GEN C, GEN args, GEN def, long prec} as15018\kbd{closure\_callgenvecdef} but set the precision locally to \kbd{prec}.1501915020\fun{GEN}{closure_callgen0prec}{GEN C, long prec} evaluate the \typ{CLOSURE}15021\kbd{C} without arguments, but set the precision locally to \kbd{prec}.1502215023\fun{GEN}{closure_callgen1}{GEN C, GEN x} evaluate the \typ{CLOSURE}15024\kbd{C} with argument \kbd{x}. Assumes \kbd{C} has arity $\geq 1$.1502515026\fun{GEN}{closure_callgen1prec}{GEN C, GEN x, long prec} as15027\kbd{closure\_callgen1}, but set the precision locally to \kbd{prec}.1502815029\fun{GEN}{closure_callgen2}{GEN C, GEN x, GEN y} evaluate the \typ{CLOSURE}15030\kbd{C} with argument \kbd{x}, \kbd{y}. Assumes \kbd{C} has arity $\geq 2$.1503115032\fun{void}{closure_callvoid1}{GEN C, GEN x} evaluate the \typ{CLOSURE}15033\kbd{C} with argument \kbd{x} and discard the result. Assumes \kbd{C}15034has arity $\geq 1$.1503515036The following technical functions are used to evaluate \emph{inline}15037closures and closures of arity 0.1503815039The control flow statements (break, next and return) will cause the15040evaluation of the closure to be interrupted; this is called below a15041\emph{flow change}. When that occurs, the functions below generally15042return \kbd{NULL}. The caller can then adopt three positions:1504315044\item raises an exception (\kbd{closure\_evalnobrk}).1504515046\item passes through (by returning NULL itself).1504715048\item handles the flow change.1504915050\fun{GEN}{closure_evalgen}{GEN code} evaluates a closure and returns the result,15051or \kbd{NULL} if a flow change occurred.1505215053\fun{GEN}{closure_evalnobrk}{GEN code} as \kbd{closure\_evalgen} but raise15054an exception if a flow change occurs. Meant for iterators where15055interrupting the closure is meaningless, e.g.~\kbd{intnum} or \kbd{sumnum}.1505615057\fun{void}{closure_evalvoid}{GEN code} evaluates a closure whose return15058value is ignored. The caller has to deal with eventual flow changes by15059calling \kbd{loop\_break}.1506015061The remaining functions below are for exceptional situations:1506215063\fun{GEN}{closure_evalres}{GEN code} evaluates a closure and returns the result.15064The difference with \kbd{closure\_evalgen} being that, if the flow end by a15065\kbd{return} statement, the result will be the returned value instead of15066\kbd{NULL}. Used by the main GP loop.1506715068\fun{GEN}{closure_evalbrk}{GEN code, long *status} as \kbd{closure\_evalres}15069but set \kbd{status} to a nonzero value if a flow change occurred. This15070variant is not stack clean. Used by the break loop.1507115072\fun{GEN}{closure_trapgen}{long numerr, GEN code} evaluates closure, while15073trapping error \kbd{numerr}. Return \kbd{(GEN)1L} if error trapped, and the15074result otherwise, or \kbd{NULL} if a flow change occurred. Used by trap.150751507615077\subsec{Functions to handle control flow changes}1507815079\fun{long}{loop_break}{void} processes an eventual flow changes inside an15080iterator. If this function return $1$, the iterator should stop.1508115082\subsec{Functions to deal with lexical local variables}\label{se:pushlex}1508315084Function using the prototype code \kbd{`V'} need to manually create and delete a15085lexical variable for each code \kbd{`V'}, which will be given a number $-1, -2,15086\ldots$.1508715088\fun{void}{push_lex}{GEN a, GEN code} creates a new lexical variable whose15089initial value is $a$ on the top of the stack. This variable get the number15090$-1$, and the number of the other variables is decreased by one unit. When15091the first variable of a closure is created, the argument \kbd{code} must be the15092closure that references this lexical variable. The argument \kbd{code} must be15093\kbd{NULL} for all subsequent variables (if any). (The closure contains the15094debugging data for the variable).1509515096\fun{void}{pop_lex}{long n} deletes the $n$ topmost lexical variables,15097increasing the number of other variables by $n$. The argument $n$ must match15098the number of variables allocated through \kbd{push\_lex}.1509915100\fun{GEN}{get_lex}{long vn} get the value of the variable with number \kbd{vn}.1510115102\fun{void}{set_lex}{long vn, GEN x} set the value of the variable with number15103\kbd{vn}.1510415105\subsec{Functions returning new closures}1510615107\fun{GEN}{compile_str}{const char *s} returns the closure corresponding to the15108GP expression $s$.1510915110\fun{GEN}{closure_deriv}{GEN code} returns a closure corresponding to the15111numerical derivative of the closure \kbd{code}.1511215113\fun{GEN}{closure_derivn}{GEN code, long n} returns a closure corresponding to15114the numerical derivative of order $n > 0$ of the closure \kbd{code}.1511515116\fun{GEN}{snm_closure}{entree *ep, GEN data}15117Let \kbd{data} be a vector of length $m$, \kbd{ep} be an \kbd{entree}15118pointing to a C function $f$ of arity $n+m$, returns a \typ{CLOSURE} object15119$g$ of arity $n$ such that15120$g(x_1,\ldots,x_n)=f(x_1,\ldots,x_n,gel(data,1),...,gel(data,m))$. If15121\kbd{data} is \kbd{NULL}, then $m=0$ is assumed. Shallow function.1512215123\fun{GEN}{strtofunction}{char *str} returns a closure corresponding to the15124built-in or install'ed function named \kbd{str}.1512515126\fun{GEN}{strtoclosure}{char *str, long n, ...} returns a closure15127corresponding to the built-in or install'ed function named \kbd{str} with the15128$n$ last parameters set to the $n$ \kbd{GEN}s following $n$. This is15129analogous to \kbd{snm\_closure(isentry(str), mkvecn(...))} but the latter has15130lower overhead since it does not copy arguments, nor does it validate inputs.1513115132In the example code below, \kbd{agm1} is set to the function15133\kbd{x->agm(x,1)} and \kbd{res} is set to \kbd{agm(2,1)}.1513415135\bprog15136GEN agm1 = strtoclosure("agm",1, gen_1);15137GEN res = closure_callgen1(agm1, gen_2);15138@eprog1513915140\subsec{Functions used by the gp debugger (break loop)}15141\fun{long}{closure_context}{long s} restores the compilation context starting15142at frame \kbd{s+1}, and returns the index of the topmost frame. This allow to15143compile expressions in the topmost lexical scope.1514415145\fun{void}{closure_err}{long level} prints a backtrace of the last $20$ stack15146frames, starting at frame \kbd{level}, the numbering starting at $0$.1514715148\subsec{Standard wrappers for iterators}15149Two families of standard wrappers are provided to interface iterators like15150\kbd{intnum} or \kbd{sumnum} with GP.1515115152\subsubsec{Standard wrappers for inline closures}15153These wrappers are used to implement GP functions taking inline closures as15154input. The object \kbd{(GEN)E} must be an inline closure which is evaluated15155with the lexical variable number $-1$ set to $x$.1515615157\fun{GEN}{gp_eval}{void *E, GEN x} is used for the prototype code \kbd{`E'}.1515815159\fun{GEN}{gp_evalprec}{void *E, GEN x, long prec} as \kbd{gp\_eval}, but15160set the precision locally to \kbd{prec}.1516115162\fun{long}{gp_evalvoid}{void *E, GEN x} is used for the prototype code15163\kbd{`I'}. The resulting value is discarded. Return a nonzero value if a15164control-flow instruction request the iterator to terminate immediately.1516515166\fun{long}{gp_evalbool}{void *E, GEN x} returns the boolean15167\kbd{gp\_eval(E, x)} evaluates to (i.e. true iff the value is nonzero).1516815169\fun{GEN}{gp_evalupto}{void *E, GEN x} memory-safe version of \kbd{gp\_eval},15170\kbd{gcopy}-ing the result, when the evaluator returns components of15171previously allocated objects (e.g. member functions).1517215173\subsubsec{Standard wrappers for true closures}15174These wrappers are used to implement GP functions taking true closures as15175input.1517615177\fun{GEN}{gp_call}{void *E, GEN x} evaluates the closure \kbd{(GEN)E} on $x$.1517815179\fun{GEN}{gp_callprec}{void *E, GEN x, long prec} as \kbd{gp\_call},15180but set the precision locally to \kbd{prec}.1518115182\fun{GEN}{gp_call2}{void *E, GEN x, GEN y} evaluates the closure \kbd{(GEN)E}15183on $(x,y)$.1518415185\fun{long}{gp_callbool}{void *E, GEN x} evaluates the closure \kbd{(GEN)E} on15186$x$, returns \kbd{1} if its result is nonzero, and \kbd{0} otherwise.1518715188\fun{long}{gp_callvoid}{void *E, GEN x} evaluates the closure \kbd{(GEN)E} on15189$x$, discarding the result. Return a nonzero value if a control-flow15190instruction request the iterator to terminate immediately.1519115192\section{Defaults}1519315194\fun{entree*}{pari_is_default}{const char *s} return the \kbd{entree}15195structure attached to $s$ if it is the name of a default, \kbd{NULL}15196otherwise.1519715198\fun{GEN}{setdefault}{const char *s, const char *v, long flag} is the15199low-level function underlying \kbd{default0}. If $s$ is \kbd{NULL}, call all15200default setting functions with string argument \kbd{NULL} and flag15201\tet{d_ACKNOWLEDGE}. Otherwise, check whether $s$ corresponds to a default15202and call the corresponding default setting function with arguments $v$ and15203\fl.1520415205We shall describe these functions below: if $v$ is \kbd{NULL}, we only look15206at the default value (and possibly print or return it, depending on15207\kbd{flag}); otherwise the value of the default to $v$, possibly after some15208translation work. The flag is one of1520915210\item \tet{d_INITRC} called while reading the \kbd{gprc}: print and return15211\kbd{gnil}, possibly defer until \kbd{gp} actually starts.1521215213\item \tet{d_RETURN} return the current value, as a \typ{INT} if possible, as15214a \typ{STR} otherwise.1521515216\item \tet{d_ACKNOWLEDGE} print the current value, return \kbd{gnil}.1521715218\item \tet{d_SILENT} print nothing, return \kbd{gnil}.1521915220\noindent Low-level functions called by \kbd{setdefault}:152211522215223\fun{GEN}{sd_TeXstyle}{const char *v, long flag}1522415225\fun{GEN}{sd_breakloop}{const char *v, long flag}1522615227\fun{GEN}{sd_colors}{const char *v, long flag}1522815229\fun{GEN}{sd_compatible}{const char *v, long flag}1523015231\fun{GEN}{sd_datadir}{const char *v, long flag}1523215233\fun{GEN}{sd_debug}{const char *v, long flag}1523415235\fun{GEN}{sd_debugfiles}{const char *v, long flag}1523615237\fun{GEN}{sd_debugmem}{const char *v, long flag}1523815239\fun{GEN}{sd_echo}{const char *v, long flag}1524015241\fun{GEN}{sd_factor_add_primes}{const char *v, long flag}1524215243\fun{GEN}{sd_factor_proven}{const char *v, long flag}1524415245\fun{GEN}{sd_format}{const char *v, long flag}1524615247\fun{GEN}{sd_graphcolormap}{const char *v, long flag}1524815249\fun{GEN}{sd_graphcolors}{const char *v, long flag}1525015251\fun{GEN}{sd_help}{const char *v, long flag}1525215253\fun{GEN}{sd_histfile}{const char *v, long flag}1525415255\fun{GEN}{sd_histsize}{const char *v, long flag}1525615257\fun{GEN}{sd_lines}{const char *v, long flag}1525815259\fun{GEN}{sd_linewrap}{const char *v, long flag}1526015261\fun{GEN}{sd_log}{const char *v, long flag}1526215263\fun{GEN}{sd_logfile}{const char *v, long flag}1526415265\fun{GEN}{sd_nbthreads}{const char *v, long flag}1526615267\fun{GEN}{sd_new_galois_format}{const char *v, long flag}1526815269\fun{GEN}{sd_output}{const char *v, long flag}1527015271\fun{GEN}{sd_parisize}{const char *v, long flag}1527215273\fun{GEN}{sd_parisizemax}{const char *v, long flag}1527415275\fun{GEN}{sd_path}{const char *v, long flag}1527615277\fun{GEN}{sd_plothsizes}{const char *v, long flag}1527815279\fun{GEN}{sd_prettyprinter}{const char *v, long flag}1528015281\fun{GEN}{sd_primelimit}{const char *v, long flag}1528215283\fun{GEN}{sd_prompt}{const char *v, long flag}1528415285\fun{GEN}{sd_prompt_cont}{const char *v, long flag}1528615287\fun{GEN}{sd_psfile}{const char *v, long flag} The \kbd{psfile} default15288is obsolete, don't use this function.1528915290\fun{GEN}{sd_readline}{const char *v, long flag}1529115292\fun{GEN}{sd_realbitprecision}{const char *v, long flag}1529315294\fun{GEN}{sd_realprecision}{const char *v, long flag}1529515296\fun{GEN}{sd_recover}{const char *v, long flag}1529715298\fun{GEN}{sd_secure}{const char *v, long flag}1529915300\fun{GEN}{sd_seriesprecision}{const char *v, long flag}1530115302\fun{GEN}{sd_simplify}{const char *v, long flag}1530315304\fun{GEN}{sd_sopath}{const char *v, int flag}1530515306\fun{GEN}{sd_strictargs}{const char *v, long flag}1530715308\fun{GEN}{sd_strictmatch}{const char *v, long flag}1530915310\fun{GEN}{sd_timer}{const char *v, long flag}1531115312\fun{GEN}{sd_threadsize}{const char *v, long flag}1531315314\fun{GEN}{sd_threadsizemax}{const char *v, long flag}1531515316\noindent Generic functions used to implement defaults: most of the above15317routines are implemented in terms of the following generic ones. In all15318routines below1531915320\item \kbd{v} and \kbd{flag} are the arguments passed to \kbd{default}:15321\kbd{v} is a new value (or the empty string: no change), and \kbd{flag} is one15322of \tet{d_INITRC}, \tet{d_RETURN}, etc.1532315324\item \kbd{s} is the name of the default being changed, used to display error15325messages or acknowledgements.1532615327\fun{GEN}{sd_toggle}{const char *v, long flag, const char *s, int *ptn}1532815329\item if \kbd{v} is neither \kbd{"0"} nor \kbd{"1"}, an error is raised using15330\tet{pari_err}.1533115332\item \kbd{ptn} points to the current numerical value of the toggle (1 or 0),15333and is set to the new value (when \kbd{v} is nonempty).1533415335For instance, here is how the timer default is implemented internally:15336\bprog15337GEN15338sd_timer(const char *v, long flag)15339{ return sd_toggle(v,flag,"timer", &(GP_DATA->chrono)); }15340@eprog1534115342The exact behavior and return value depends on \kbd{flag}:1534315344\item \tet{d_RETURN}: returns the new toggle value, as a \kbd{GEN}.1534515346\item \tet{d_ACKNOWLEDGE}: prints a message indicating the new toggle value15347and return \kbd{gnil}.1534815349\item other cases: print nothing and return \kbd{gnil}.153501535115352\fun{GEN}{sd_ulong}{const char *v, long flag, const char *s, ulong *ptn,15353ulong Min, ulong Max, const char **msg}\hbadness 100001535415355\item \kbd{ptn} points to the current numerical value of the toggle, and is set15356to the new value (when \kbd{v} is nonempty).1535715358\item \kbd{Min} and \kbd{Max} point to the minimum and maximum values allowed15359for the default.1536015361\item \kbd{v} must translate to an integer in the allowed ranger, a suffix15362among15363\kbd{k}/\kbd{K} ($\times 10^3$),15364\kbd{m}/\kbd{M} ($\times 10^6$),15365or15366\kbd{g}/\kbd{G} ($\times 10^9$) is allowed, but no arithmetic expression.1536715368\item \kbd{msg} is a \kbd[NULL]-terminated array of messages or \kbd{NULL}15369(ignored). If \kbd{msg} is not \kbd{NULL}, \kbd{msg}$[i]$ contains15370a message attached to the value $i$ of the default. The last entry in the15371\kbd{msg} array is used as a message attached to all subsequent ones.1537215373The exact behavior and return value depends on \kbd{flag}:1537415375\item \tet{d_RETURN}: returns the new value, as a \kbd{GEN}.1537615377\item \tet{d_ACKNOWLEDGE}: prints a message indicating the new value,15378possibly a message attached to it via the \kbd{msg} argument, and return15379\kbd{gnil}.1538015381\item other cases: print nothing and return \kbd{gnil}.1538215383\fun{GEN}{sd_intarray}{const char *v, long flag, const char *s, GEN *pz}1538415385\item records a \typ{VECSMALL} array of nonnegative integers.1538615387\item \kbd{pz} points to the current \typ{VECSMALL} value, and is set to the15388new value (when \kbd{v} is nonempty).1538915390The exact return value depends on \kbd{flag}:1539115392\item \tet{d_RETURN}: returns the new value, as a \typ{VEC} (converted via15393\kbd{zv\_to\_ZV})1539415395\item \tet{d_ACKNOWLEDGE}: prints a message indicating the new value,15396(as a \typ{VEC}) and return \kbd{gnil}.1539715398\item other cases: print nothing and return \kbd{gnil}.1539915400\fun{GEN}{sd_string}{const char *v, long flag, const char *s, char **pstr}15401\item \kbd{v} is subjet to environment expansion, then time expansion.1540215403\item \kbd{pstr} points to the current string value, and is set to the new15404value (when \kbd{v} is nonempty).1540515406\section{Records and Lazy vectors}15407The functions in this section are used to implement \kbd{ell} structures and15408analogous objects, which are vectors some of whose components are initialized15409to dummy values, later computed on demand. We start by initializing the15410structure:1541115412\fun{GEN}{obj_init}{long d, long n} returns an \tev{obj} $S$, a \typ{VEC}15413with $d$ regular components, accessed as \kbd{gel(S,1)}, \dots,15414\kbd{gel(S,d)}; together with a record of $n$ members, all initialized to15415$0$. The arguments $d$ and $n$ must be nonnegative.1541615417After \kbd{S = obj\_init(d, n)}, the prototype of our other functions are of15418the form15419\bprog15420GEN obj_do(GEN S, long tag, ...)15421@eprog\noindent The first argument $S$ holds the structure to be managed.15422The second argument \var{tag} is the index of the struct member (from $1$ to15423$n$) we operate on. We recommend to define an \kbd{enum} and use descriptive15424names instead of hardcoded numbers. For instance, if $n = 3$, after defining15425\bprog15426enum { TAG_p = 1, TAG_list, TAG_data };15427@eprog\noindent one may use \kbd{TAG\_list} or $2$ indifferently as a tag.15428The former being preferred, of course.1542915430\misctitle{Technical note}15431In the current implementation, $S$ is a \typ{VEC} with $d+1$ entries.15432The first $d$ components are ordinary \typ{GEN} entries, which you can15433read or assign to in the customary way. But the last component $\kbd{gel(S,15434d+1)}$, a \typ{VEC} of length $n$ initialized to \kbd{zerovec}$(n)$, must15435be handled in a special way: you should never access or modify its components15436directly, only through the API we are about to describe. Indeed, its entries15437are meant to contain dynamic data, which will be stored, retrieved and15438replaced (for instance by a value computed to a higher accuracy), while15439interacting safely with intermediate \kbd{gerepile} calls. This mechanism15440allows to simulate C \kbd{struct}s, in a simpler way than with general15441hashtables, while remaining compatible with the GP language, which knows15442neither structs nor hashtables. It also serialize the structure in an15443ordinary \kbd{GEN}, which facilitates copies and garbage collection (use15444\kbd{gcopy} or \kbd{gerepile}), rather than having to deal with individual15445components of actual C \kbd{struct}s.1544615447\fun{GEN}{obj_reinit}{GEN S} make a shallow copy of $S$, re-initializing15448all dynamic components. This allows ``forking'' a lazy vector while15449avoiding both a memory leak, and storing pointers to the same data15450in different objects (with risks of a double free later).1545115452\fun{GEN}{obj_check}{GEN S, long tag} if the \emph{tag}-component in $S$15453is non empty, return it. Otherwise return \kbd{NULL}. The \typ{INT} $0$15454(initial value) is used as a sentinel to indicated an empty component.1545515456\fun{GEN}{obj_insert}{GEN S, long tag, GEN O} insert (a clone of) $O$15457as \emph{tag}-component of $S$. Any previous value is deleted, and15458data pointing to it become invalid.1545915460\fun{GEN}{obj_insert_shallow}{GEN S, long K, GEN O} as \tet{obj_insert},15461inserting $O$ as-is, not via a clone.1546215463\fun{GEN}{obj_checkbuild}{GEN S, long tag, GEN (*build)(GEN)} if the15464\emph{tag}-component of $S$ is non empty, return it. Otherwise insert15465(a clone of) \kbd{build(S)} as \emph{tag}-component in $S$, and return it.1546615467\fun{GEN}{obj_checkbuild_padicprec}{GEN S, long tag, GEN (*build)(GEN,long),15468long prec}15469if the \emph{tag}-component of $S$ is non empty \emph{and} has relative15470$p$-adic precision $\geq \kbd{prec}$, return it. Otherwise insert (a clone15471of) \kbd{build(S, prec)} as \emph{tag}-component in $S$, and return it.1547215473\fun{GEN}{obj_checkbuild_realprec}{GEN S, long tag, GEN (*build)(GEN, long),15474long prec} if the \emph{tag}-component of $S$ is non empty \emph{and}15475satisfies \kbd{gprecision} $\geq \kbd{prec}$, return it. Otherwise insert (a15476clone of) \kbd{build(S, prec)} as \emph{tag}-component in $S$, and return it.1547715478\fun{GEN}{obj_checkbuild_prec}{GEN S, long tag, GEN (*build)(GEN,long), GEN15479(*gpr)(GEN), long prec} if the \emph{tag}-component of $S$ is non empty15480\emph{and} has precision $\kbd{gpr}(x)\geq \kbd{prec}$, return it. Otherwise15481insert (a clone of) \kbd{build(S, prec)} as \emph{tag}-component in $S$,15482and return it.1548315484\fun{void}{obj_free}{GEN S} destroys all clones stored in the $n$ tagged15485components, and replace them by the initial value $0$. The regular entries of15486$S$ are unaffected, and $S$ remains a valid object. This is used to15487avoid memory leaks.154881548915490