Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Testing latest pari + WASM + node.js... and it works?! Wow.

28507 views
License: GPL3
ubuntu2004
1
% Copyright (c) 2000 The PARI Group
2
%
3
% This file is part of the PARI/GP documentation
4
%
5
% Permission is granted to copy, distribute and/or modify this document
6
% under the terms of the GNU General Public License
7
\chapter{Technical Reference Guide: the basics}
8
9
In the following chapters, we describe all public low-level functions of the
10
PARI library. These include specialized functions for handling all the PARI
11
types. Simple higher level functions, such as arithmetic or transcendental
12
functions, are described in Chapter~3 of the GP user's manual; we will
13
eventually see more general or flexible versions in the chapters to come. A
14
general introduction to the major concepts of PARI programming can be found
15
in Chapter~4, which you should really read first.
16
17
We shall now study specialized functions, more efficient than the library
18
wrappers, but sloppier on argument checking and damage control; besides
19
speed, their main advantage is to give finer control about the inner
20
workings of generic routines, offering more options to the programmer.
21
22
\misctitle{Important advice} Generic routines eventually call lower level
23
functions. Optimize your algorithms first, not overhead and conversion costs
24
between PARI routines. For generic operations, use generic routines first;
25
do not waste time looking for the most specialized one available unless you
26
identify a genuine bottleneck, or you need some special behavior the generic
27
routine does not offer. The PARI source code is part of the documentation;
28
look for inspiration there.\smallskip
29
30
The type \kbd{long} denotes a \tet{BITS_IN_LONG}-bit signed long integer (32
31
or 64 bits). The type \tet{ulong} is defined as \kbd{unsigned long}. The word
32
\emph{stack} always refer to the PARI stack, allocated through an initial
33
\kbd{pari\_init} call. Refer to Chapters 1--2 and~4 for general background.
34
\kbdsidx{BIL}
35
36
We shall often refer to the notion of \tev{shallow} function, which means that
37
some components of the result may point to components of the input, which is
38
more efficient than a \emph{deep} copy (full recursive copy of the object
39
tree). Such outputs are not suitable for \kbd{gerepileupto} and particular
40
care must be taken when garbage collecting objects which have been input to
41
shallow functions: corresponding outputs also become invalid and should no
42
longer be accessed.
43
44
A function is \emph{not stack clean} if it leaves intermediate data on the
45
stack besides its output, for efficiency reasons.
46
47
\section{Initializing the library}
48
49
The following functions enable you to start using the PARI functions
50
in a program, and cleanup without exiting the whole program.
51
52
\subsec{General purpose}
53
54
\fun{void}{pari_init}{size_t size, ulong maxprime} initialize the
55
library, with a stack of \kbd{size} bytes and a prime table
56
up to the maximum of \kbd{maxprime} and $2^{16}$. Unless otherwise
57
mentioned, no PARI function will function properly before such an
58
initialization.
59
60
\fun{void}{pari_close}{void} stop using the library (assuming it was
61
initialized with \kbd{pari\_init}) and frees all allocated objects.
62
63
\subsec{Technical functions}\label{se:pari_init_tech}
64
65
\fun{void}{pari_init_opts}{size_t size, ulong maxprime, ulong opts} as
66
\kbd{pari\_init}, more flexible. \kbd{opts} is a mask of flags
67
among the following:
68
69
\kbd{INIT\_JMPm}: install PARI error handler. When an exception is
70
raised, the program is terminated with \kbd{exit(1)}.
71
72
\kbd{INIT\_SIGm}: install PARI signal handler.
73
74
\kbd{INIT\_DFTm}: initialize the \kbd{GP\_DATA} environment structure.
75
This one \emph{must} be enabled once. If you close pari, then restart it,
76
you need not reinitialize \kbd{GP\_DATA}; if you do not, then old values are
77
restored.
78
79
\kbd{INIT\_noPRIMEm}: do not compute the prime table (ignore the
80
\kbd{maxprime} argument). The user \emph{must} call
81
\tet{pari_init_primes} later.
82
83
\kbd{INIT\_noIMTm}: (technical, see \kbd{pari\_mt\_init} in the Developer's
84
Guide for detail). Do not call \tet{pari_mt_init} to initialize the
85
multi-thread engine. If this flag is set, \kbd{pari\_mt\_init()} will need to
86
be called manually. See \kbd{examples/pari-mt.c} for an example.
87
88
\kbd{INIT\_noINTGMPm}: do not install PARI-specific GMP memory functions.
89
This option is ignored when the GMP library is not in use. You may
90
install PARI-specific GMP memory functions later by calling
91
92
\fun{void}{pari_kernel_init}{void}
93
94
\noindent and restore the previous values using
95
96
\fun{void}{pari_kernel_close}{void}
97
98
This option should not be used without a thorough understanding of the
99
problem you are trying to solve. The GMP memory functions are global
100
variables used by the GMP library. If your program is linked with two
101
libraries that require these variables to be set to different values,
102
conflict ensues. To avoid a conflict, the proper solution is to record
103
their values with \kbd{mp\_get\_memory\_functions} and to call
104
\kbd{mp\_set\_memory\_functions} to restore the expected values each time the
105
code switches from using one library to the other. Here is an example:
106
\bprog
107
void *(*pari_alloc_ptr) (size_t);
108
void *(*pari_realloc_ptr) (void *, size_t, size_t);
109
void (*pari_free_ptr) (void *, size_t);
110
void *(*otherlib_alloc_ptr) (size_t);
111
void *(*otherlib_realloc_ptr) (void *, size_t, size_t);
112
void (*otherlib_free_ptr) (void *, size_t);
113
114
void init(void)
115
{
116
pari_init(8000000, 500000);
117
mp_get_memory_functions(&pari_alloc_ptr,&pari_realloc_ptr,
118
&pari_free_ptr);
119
otherlib_init();
120
mp_get_memory_functions(&otherlib_alloc_ptr,&otherlib_realloc_ptr,
121
&otherlib_free_ptr);
122
}
123
void function_that_use_pari(void)
124
{
125
mp_set_memory_functions(pari_alloc_ptr,pari_realloc_ptr,
126
pari_free_ptr);
127
/*use PARI functions*/
128
}
129
void function_that_use_otherlib(void)
130
{
131
mp_set_memory_functions(otherlib_alloc_ptr,otherlib_realloc_ptr,
132
otherlib_free_ptr);
133
/*use OTHERLIB functions*/
134
}
135
@eprog
136
137
\fun{void}{pari_close_opts}{ulong init_opts} as \kbd{pari\_close},
138
for a library initialized with a mask of options using
139
\kbd{pari\_init\_opts}. \kbd{opts} is a mask of flags among
140
141
\kbd{INIT\_SIGm}: restore \kbd{SIG\_DFL} default action for signals
142
tampered with by PARI signal handler.
143
144
\kbd{INIT\_DFTm}: frees the \kbd{GP\_DATA} environment structure.
145
146
\kbd{INIT\_noIMTm}: (technical, see \kbd{pari\_mt\_init} in the Developer's
147
Guide for detail). Do not call \tet{pari_mt_close} to close the multi-thread
148
engine.
149
\kbd{INIT\_noINTGMPm}: do not restore GMP memory functions.
150
151
\fun{void}{pari_sig_init}{void (*f)(int)} install the signal handler \kbd{f}
152
(see \kbd{signal(2)}): the signals \kbd{SIGBUS}, \kbd{SIGFPE}, \kbd{SIGINT},
153
\kbd{SIGBREAK}, \kbd{SIGPIPE} and \kbd{SIGSEGV} are concerned.
154
155
\fun{void}{pari_init_primes}{ulong maxprime} Initialize the PARI
156
primes. This function is called by \kbd{pari\_init(\dots,maxprime)}.
157
It is provided for users calling \kbd{pari\_init\_opts} with the
158
flag \kbd{INIT\_noPRIMEm}.
159
160
\fun{void}{pari_sighandler}{int signum} the actual signal handler that
161
PARI uses. This can be used as argument to \kbd{pari\_sig\_init} or
162
\kbd{signal(2)}.
163
164
\fun{void}{pari_stackcheck_init}{void *stackbase} controls the system stack
165
exhaustion checking code in the GP interpreter. This should be used when the
166
system stack base address change or when the address seen by \kbd{pari\_init}
167
is too far from the base address. If \kbd{stackbase} is \kbd{NULL}, disable the
168
check, else set the base address to \kbd{stackbase}. It is normally used this
169
way
170
\bprog
171
int thread_start (...)
172
{
173
long first_item_on_the_stack;
174
...
175
pari_stackcheck_init(&first_item_on_the_stack);
176
}
177
@eprog
178
179
\fun{int}{pari_daemon}{void} forks a PARI daemon, detaching from the main
180
process group. The function returns 1 in the parent, and 0 in the
181
forked son.
182
183
\fun{void}{paristack_setsize}{size_t rsize, size_t vsize}
184
sets the default \kbd{parisize} to \kbd{rsize} and the
185
default \kbd{parisizemax} to \kbd{vsize}, and reallocate the
186
stack to match these value, destroying its content.
187
Generally used just after \kbd{pari\_init}.
188
189
\fun{void}{paristack_resize}{ulong newsize}
190
changes the current stack size to \kbd{newsize}
191
(double it if \kbd{newsize} is 0).
192
The new size is clipped to be at least the current stack size and
193
at most \kbd{parisizemax}. The stack content is not affected
194
by this operation.
195
196
\fun{void}{parivstack_reset}{void}
197
resets the current stack to its default size \kbd{parisize}. This is
198
used to recover memory after a computation that enlarged the stack.
199
This function destroys the content of the enlarged stack (between
200
the old and the new bottom of the stack).
201
Before calling this function, you must ensure that \kbd{avma} lies
202
within the new smaller stack.
203
204
\fun{void}{paristack_newrsize}{ulong newsize}
205
\emph{(does not return)}. Library version of
206
\bprog
207
default(parisize, "newsize")
208
@eprog\noindent Set the default \kbd{parisize} to \kbd{newsize}, or double
209
\kbd{parisize} if \kbd{newsize} is equal to 0, then call
210
\kbd{cb\_pari\_err\_recover(-1)}.
211
212
\fun{void}{parivstack_resize}{ulong newsize}
213
\emph{(does not return)}. Library version of
214
\bprog
215
default(parisizemax, "newsize")
216
@eprog\noindent Set the default \kbd{parisizemax} to \kbd{newsize} and call
217
\kbd{cb\_pari\_err\_recover(-1)}.
218
219
\subsec{Notions specific to the GP interpreter}
220
221
An \kbd{entree} is the generic object attached to an identifier (a name)
222
in GP's interpreter, be it a built-in or user function, or a variable. For
223
a function, it has at least the following fields:
224
225
\kbd{char *name}: the name under which the interpreter knows us.
226
227
\kbd{void *value}: a pointer to the C function to call.
228
229
\kbd{long menu}: a small integer $\geq 1$ (to which group of function
230
help do we belong, for the \kbd{?$n$} help menu).
231
232
\kbd{char *code}: the prototype code.
233
234
\kbd{char *help}: the help text for the function.
235
236
A routine in GP is described to the analyzer by an \kbd{entree}
237
structure. Built-in PARI routines are grouped in \emph{modules}, which
238
are arrays of \kbd{entree} structs, the last of which satisfy
239
\kbd{name = NULL} (sentinel). There are currently four modules in PARI/GP:
240
241
\item general functions (\tet{functions_basic}, known to \kbd{libpari}),
242
243
\item gp-specific functions (\tet{functions_gp}),
244
245
\noindent and two modules of obsolete functions. The function
246
\kbd{pari\_init} initializes the interpreter and declares all symbols in
247
\kbd{functions\_basic}. You may declare further functions on a case by case
248
basis or as a whole module using
249
250
\fun{void}{pari_add_function}{entree *ep} adds a single routine to the
251
table of symbols in the interpreter. It assumes \kbd{pari\_init} has been
252
called.
253
254
\fun{void}{pari_add_module}{entree *mod} adds all the routines in module
255
\kbd{mod} to the table of symbols in the interpreter. It assumes
256
\kbd{pari\_init} has been called.
257
258
\noindent For instance, gp implements a number of private routines, which
259
it adds to the default set via the calls
260
\bprog
261
pari_add_module(functions_gp);
262
@eprog
263
264
A GP \kbd{default} is likewise attached to a helper routine, that is run
265
when the value is consulted, or changed by \tet{default0} or \tet{setdefault}.
266
Such routines are grouped in the module \tet{functions_default}.
267
268
\fun{void}{pari_add_defaults_module}{entree *mod} adds all the defaults in
269
module \kbd{mod} to the interpreter. It assumes that \kbd{pari\_init} has
270
been called. From this point on, all defaults in module \kbd{mod} are known
271
to \tet{setdefault} and friends.
272
273
\subsec{Public callbacks}
274
275
The \kbd{gp} calculator associates elaborate functions (for instance the
276
break loop handler) to the following callbacks, and so can you:
277
278
\doc{cb_pari_ask_confirm}{void (*cb_pari_ask_confirm)(const char *s)}
279
initialized to \kbd{NULL}. Called with argument $s$ whenever PARI wants
280
confirmation for action $s$, for instance in \tet{secure} mode.
281
282
\doc{cb_pari_init_histfile}{void (*cb_pari_init_histfile)(void)}
283
initialized to \kbd{NULL}. Called when the \kbd{histfile} default
284
is changed. The intent is for that callback to read the file content, append
285
it to history in memory, then dump the expanded history to the new
286
\kbd{histfile}.
287
288
\doc{cb_pari_is_interactive}{int (*cb_pari_is_interactive)(void)};
289
initialized to \kbd{NULL}.
290
291
\doc{cb_pari_quit}{void (*cb_pari_quit)(long)}
292
initialized to a no-op. Called when \kbd{gp} must evaluate the \kbd{quit}
293
command.
294
295
\doc{cb_pari_start_output}{void (*cb_pari_start_output)(void)}
296
initialized to \kbd{NULL}.
297
298
\doc{cb_pari_handle_exception}{int (*cb_pari_handle_exception)(long)}
299
initialized to \kbd{NULL}. If not \kbd{NULL}, this routine is called with
300
argument $-1$ on \kbd{SIGINT}, and argument \kbd{err} on error \kbd{err}. If
301
it returns a nonzero value, the error or signal handler returns, in effect
302
further ignoring the error or signal, otherwise it raises a fatal error.
303
A possible simple-minded handler, used by the \kbd{gp} interpreter, is
304
305
\fun{int}{gp_handle_exception}{long err} if the \kbd{breakloop}
306
default is enabled (set to $1$) and \tet{cb_pari_break_loop} is not
307
\kbd{NULL}, we call this routine with \kbd{err} argument and return the
308
result.
309
310
\doc{cb_pari_err_handle}{int (*cb_pari_err_handle)(GEN)}
311
If not \kbd{NULL}, this routine is called with a \typ{ERROR} argument
312
from \kbd{pari\_err}. If it returns a nonzero value, the error returns, in
313
effect further ignoring the error, otherwise it raises a fatal error.
314
315
The default behavior is to print a descriptive error
316
message (display the error), then return 0, thereby raising a fatal error.
317
This differs from \tet{cb_pari_handle_exception} in that the
318
function is not called on \kbd{SIGINT} (which do not generate a \typ{ERROR}),
319
only from \kbd{pari\_err}. Use \tet{cb_pari_sigint} if you need to handle
320
\kbd{SIGINT} as well.
321
322
The following function can be used by \kbd{cb\_pari\_err\_handle} to display
323
the error message.
324
325
\fun{const char*}{closure_func_err}{} return a statically allocated string
326
holding the name of the function that triggered the error. Return NULL if the
327
error was not caused by a function.
328
329
\doc{cb_pari_break_loop}{int (*cb_pari_break_loop)(int)}
330
initialized to \kbd{NULL}.
331
332
\doc{cb_pari_sigint}{void (*cb_pari_sigint)(void)}.
333
Function called when we receive \kbd{SIGINT}. By default, raises
334
\bprog
335
pari_err(e_MISC, "user interrupt");
336
@eprog\noindent A possible simple-minded variant, used by the
337
\kbd{gp} interpreter, is
338
339
\fun{void}{gp_sigint_fun}{void}
340
341
\doc{cb_pari_pre_recover}{void (*cb_pari_pre_recover)(long)}
342
initialized to \kbd{NULL}. If not \kbd{NULL}, this routine is called just
343
before PARI cleans up from an error. It is not required to return. The error
344
number is passed as argument.
345
346
\doc{cb_pari_err_recover}{void (*cb_pari_err_recover)(long)}
347
initialized to \kbd{pari\_exit()}. This callback must not return.
348
It is called after PARI has cleaned-up from an error. The error number is
349
passed as argument, unless the PARI stack has been destroyed, in which case
350
it is called with argument $-1$.
351
352
\doc{cb_pari_whatnow}{int (*cb_pari_whatnow)(PariOUT *out, const char *s, int
353
flag)} initialized to \kbd{NULL}. If not \kbd{NULL}, must check whether $s$
354
existed in older versions of \kbd{pari} (the \kbd{gp} callback checks against
355
\kbd{pari-1.39.15}). All output must be done via \kbd{out} methods.
356
357
\item $\fl = 0$: should print verbosely the answer, including help text if
358
available.
359
360
\item $\fl = 1$: must return $0$ if the function did not change, and a
361
nonzero result otherwise. May print a help message.
362
363
\subsec{Configuration variables}
364
365
\tet{pari_library_path}: If set, It should be a path to the libpari library.
366
It is used by the function \tet{gpinstall} to locate the PARI library when
367
searching for symbols. This should only be useful on Windows.
368
369
\subsec{Utility functions}
370
371
\fun{void}{pari_ask_confirm}{const char *s} raise an error if the
372
callback \tet{cb_pari_ask_confirm} is \kbd{NULL}. Otherwise
373
calls
374
\bprog
375
cb_pari_ask_confirm(s);
376
@eprog
377
378
\fun{char*}{gp_filter}{const char *s} pre-processor for the GP
379
parser: filter out whitespace and GP comments from $s$. The returned string
380
is allocated on the PARI stack and must not be freed.
381
382
\fun{GEN}{pari_compile_str}{const char *s} low-level form of
383
\tet{compile_str}: assumes that $s$ does not contain spaces or GP comments and
384
returns the closure attached to the GP expression $s$. Note
385
that GP metacommands are not recognized.
386
387
\fun{int}{gp_meta}{const char *s, int ismain} low-level component of
388
\tet{gp_read_str}: assumes that $s$ does not contain spaces or GP comments and
389
try to interpret $s$ as a GP metacommand (e.g. starting by \kbd{\bs} or
390
\kbd{?}). If successful, execute the metacommand and return $1$; otherwise
391
return $0$. The \kbd{ismain} parameter modifies the way \kbd{\bs r} commands
392
are handled: if nonzero, act as if the file contents were entered via
393
standard input (i.e. call \tet{switchin} and divert \tet{pari_infile});
394
otherwise, simply call \tet{gp_read_file}.
395
396
\fun{void}{pari_hit_return}{void} wait for the use to enter \kbd{\bs n}
397
via standard input.
398
399
\fun{void}{gp_load_gprc}{void} read and execute the user's \kbd{GPRC} file.
400
401
\fun{void}{pari_center}{const char *s} print $s$, centered.
402
403
\fun{void}{pari_print_version}{void} print verbose version information.
404
405
\fun{long}{pari_community}{void} return the index of the support section
406
n the help.
407
408
\fun{const char*}{gp_format_time}{long t} format a delay of $t$ ms
409
suitable for \kbd{gp} output, with \kbd{timer} set. The string is allocated
410
in the PARI stack via \kbd{stack\_malloc}.
411
412
\fun{const char*}{gp_format_prompt}{const char *p} format a prompt $p$
413
suitable for \kbd{gp} prompting (includes colors and protecting ANSI escape
414
sequences for readline).
415
416
\fun{void}{pari_alarm}{long s} set an alarm after $s$ seconds (raise an
417
\tet{e_ALARM} exception).
418
419
\fun{void}{gp_help}{const char *s, long flag} print help for $s$, depending
420
on the value of \fl:
421
422
\item \tet{h_REGULAR}, basic help (\kbd{?});
423
424
\item \tet{h_LONG}, extended help (\kbd{??});
425
426
\item \tet{h_APROPOS}, a propos help (\kbd{??}).
427
428
\fun{const char **}{gphelp_keyword_list}{void} return a
429
\kbd{NULL}-terminated array a strings, containing keywords known to
430
\kbd{gphelp} besides GP functions (e.g. \kbd{modulus} or \kbd{operator}).
431
Used by the online help system and the contextual completion engine.
432
433
\fun{void}{gp_echo_and_log}{const char *p, const char *s} given a prompt
434
$p$ and attached input command $s$, update logfile and possibly
435
print on standard output if \tet{echo} is set and we are not in interactive
436
mode. The callback \tet{cb_pari_is_interactive} must be set to a sensible
437
value.
438
439
\fun{void}{gp_alarm_handler}{int sig} the \kbd{SIGALRM} handler
440
set by the \kbd{gp} interpreter.
441
442
\fun{void}{print_fun_list}{char **list, long n}
443
print all elements of \kbd{list} in columns, pausing (hit return)
444
every $n$ lines. \kbd{list} is \kbd{NULL} terminated.
445
446
\subsec{Saving and restoring the GP context}
447
448
\fun{void}{gp_context_save}{struct gp_context* rec} save the current GP
449
context.
450
451
\fun{void}{gp_context_restore}{struct gp_context* rec} restore a GP context.
452
The new context must be an ancestor of the current context.
453
454
\subsec{GP history}
455
456
These functions allow to control the GP history (the \kbd{\%} operator).
457
458
\fun{void}{pari_add_hist}{GEN x, long t, long r} adds \kbd{x} as the last history
459
entry; $t$ (resp. r) is the cpu (resp. real) time used to compute it.
460
461
\fun{GEN}{pari_get_hist}{long p}, if $p>0$ returns entry of index $p$
462
(i.e. \kbd{\%p}), else returns entry of index $n+p$ where $n$ is the
463
index of the last entry (used for \kbd{\%}, \kbd{\%`}, \kbd{\%``}, etc.).
464
465
\fun{long}{pari_get_histtime}{long p} as \tet{pari_get_hist},
466
returning the cpu time used to compute the history entry, instead of the entry
467
itself.
468
469
\fun{long}{pari_get_histrtime}{long p} as \tet{pari_get_hist},
470
returning the real time used to compute the history entry, instead of the entry
471
itself.
472
473
\fun{GEN}{pari_histtime}{long p} return the vector \kbd{[cpu, real]} where
474
\kbd{cpu} and \kbd{real} are as above.
475
476
\fun{ulong}{pari_nb_hist}{void} return the index of the last entry.
477
478
\section{Handling \kbd{GEN}s}
479
\noindent Almost all these functions are either macros or inlined. Unless
480
mentioned otherwise, they do not evaluate their arguments twice. Most of them
481
are specific to a set of types, although no consistency checks are made:
482
e.g.~one may access the \kbd{sign} of a \typ{PADIC}, but the result is
483
meaningless.
484
485
\subsec{Allocation}
486
487
\fun{GEN}{cgetg}{long l, long t} allocates (the root of) a \kbd{GEN}
488
of type $t$ and length $l$. Sets $z[0]$.
489
490
\fun{GEN}{cgeti}{long l} allocates a \typ{INT} of length $l$ (including the
491
2 codewords). Sets $z[0]$ only.
492
493
\fun{GEN}{cgetr}{long l} allocates a \typ{REAL} of length $l$ (including the
494
2 codewords). Sets $z[0]$ only.
495
496
\fun{GEN}{cgetc}{long prec} allocates a \typ{COMPLEX} whose real and
497
imaginary parts are \typ{REAL}s of length \kbd{prec}.
498
499
\fun{GEN}{cgetg_copy}{GEN x, long *lx} fast version of \kbd{cgetg}:
500
allocate a \kbd{GEN} with the same type and length as $x$, setting \kbd{*lx}
501
to \kbd{lg(x)} as a side-effect. (Only sets the first codeword.) This is
502
a little faster than \kbd{cgetg} since we may reuse the bitmask in
503
$x[0]$ instead of recomputing it, and we do not need to check that the
504
length does not overflow the possibilities of the
505
implementation (since an object with that length already exists). Note that
506
\kbd{cgetg} with arguments known at compile time, as in
507
\bprog
508
cgetg(3, t_INTMOD)
509
@eprog\noindent will be even faster since the compiler will directly perform
510
all computations and checks.
511
512
\fun{GEN}{vectrunc_init}{long l} perform \kbd{cgetg(l,t\_VEC)}, then
513
set the length to $1$ and return the result. This is used to implement
514
vectors whose final length is easily bounded at creation time, that we intend
515
to fill gradually using:
516
517
\fun{void}{vectrunc_append}{GEN x, GEN y} assuming $x$ was allocated using
518
\tet{vectrunc_init}, appends $y$ as the last element of $x$, which
519
grows in the process. The function is shallow: we append $y$, not a copy;
520
it is equivalent to
521
\bprog
522
long lx = lg(x); gel(x,lx) = y; setlg(x, lx+1);
523
@eprog\noindent
524
Beware that the maximal size of $x$ (the $l$ argument to \tet{vectrunc_init})
525
is unknown, hence unchecked, and stack corruption will occur if we append
526
more than $l-1$ elements to $x$. Use the safer (but slower)
527
\kbd{shallowconcat} when $l$ is not easy to bound in advance.
528
529
An other possibility is simply to allocate using \kbd{cgetg(l, t)} then fill
530
the components as they become available: this time the downside is that we do
531
not obtain a correct \kbd{GEN} until the vector is complete. Almost no PARI
532
function will be able to operate on it.
533
534
\fun{void}{vectrunc_append_batch}{GEN x, GEN y} successively apply
535
\bprog
536
vectrunc_append(x, gel(y, i))
537
@eprog
538
for all elements of the vector $y$.
539
540
\fun{GEN}{coltrunc_init}{long l} as \kbd{vectrunc\_init} but perform
541
\kbd{cgetg(l,t\_COL)}.
542
543
\fun{GEN}{vecsmalltrunc_init}{long l}
544
545
\fun{void}{vecsmalltrunc_append}{GEN x, long t} analog to the above for a
546
\typ{VECSMALL} container.
547
548
\subsec{Length conversions}
549
550
These routines convert a nonnegative length to different units. Their
551
behavior is undefined at negative integers.
552
553
\fun{long}{ndec2nlong}{long x} converts a number of decimal digits to a number
554
of words. Returns $ 1 + \kbd{floor}(x \times \B \log_2 10)$.
555
556
\fun{long}{ndec2prec}{long x} converts a number of decimal digits to a number
557
of codewords. This is equal to 2 + \kbd{ndec2nlong(x)}.
558
559
\fun{long}{ndec2nbits}{long x} convers a number of decimal digits to a
560
number of bits.
561
562
\fun{long}{prec2ndec}{long x} converts a number of codewords to a
563
number of decimal digits.
564
565
\fun{long}{nbits2nlong}{long x} converts a number of bits to a number of
566
words. Returns the smallest word count containing $x$ bits, i.e $
567
\kbd{ceil}(x / \B)$.
568
569
\fun{long}{nbits2ndec}{long x} converts a number of bits to a number of
570
decimal digits.
571
572
\fun{long}{nbits2lg}{long x} converts a number of bits to a length
573
in code words. Currently an alias for \kbd{nbits2nlong}.
574
575
\fun{long}{nbits2prec}{long x} converts a number of bits to a number of
576
codewords. This is equal to 2 + \kbd{nbits2nlong(x)}.
577
578
\fun{long}{nbits2extraprec}{long x} converts a number of bits to the mantissa
579
length of a \typ{REAL} in codewords. This is currently an alias to
580
\kbd{nbits2nlong(x)}.
581
582
\fun{long}{nchar2nlong}{long x} converts a number of bytes to number of
583
words. Returns the smallest word count containing $x$ bytes, i.e
584
$\kbd{ceil}(x / \kbd{sizeof(long)})$.
585
586
\fun{long}{prec2nbits}{long x} converts a \typ{REAL} length into a number
587
of significant bits; returns $(x - 2)\B$.
588
589
\fun{double}{prec2nbits_mul}{long x, double y} returns
590
\kbd{prec2nbits}$(x)\times y$.
591
592
\fun{long}{bit_accuracy}{long x} converts a length into a number
593
of significant bits; currently an alias for \kbd{prec2nbits}.
594
595
\fun{double}{bit_accuracy_mul}{long x, double y} returns
596
\kbd{bit\_accuracy}$(x)\times y$.
597
598
\fun{long}{realprec}{GEN x} length of a \typ{REAL} in words; currently an alias
599
for \kbd{lg}.
600
601
\fun{long}{bit_prec}{GEN x} length of a \typ{REAL} in bits.
602
603
\fun{long}{precdbl}{long prec} given a length in words corresponding to a
604
\typ{REAL} precision, return the length corresponding to doubling the
605
precision. Due to the presence of 2 code words, this is
606
$2(\kbd{prec} - 2) + 2$.
607
608
\subsec{Read type-dependent information}
609
610
\fun{long}{typ}{GEN x} returns the type number of~\kbd{x}. The header files
611
included through \kbd{pari.h} define symbolic constants for the \kbd{GEN}
612
types: \typ{INT} etc. Never use their actual numerical values. E.g to determine
613
whether \kbd{x} is a \typ{INT}, simply check
614
\bprog
615
if (typ(x) == t_INT) { }
616
@eprog\noindent
617
The types are internally ordered and this simplifies the implementation of
618
commutative binary operations (e.g addition, gcd). Avoid using the ordering
619
directly, as it may change in the future; use type grouping functions
620
instead (\secref{se:typegroup}).
621
622
\fun{const char*}{type_name}{long t} given a type number \kbd{t} this routine
623
returns a string containing its symbolic name. E.g \kbd{type\_name(\typ{INT})}
624
returns \kbd{"\typ{INT}"}. The return value is read-only.
625
626
\fun{long}{lg}{GEN x} returns the length of~\kbd{x} in \B-bit words.
627
628
\fun{long}{lgefint}{GEN x} returns the effective length of the \typ{INT}
629
\kbd{x} in \B-bit words.
630
631
\fun{long}{signe}{GEN x} returns the sign ($-1$, 0 or 1) of~\kbd{x}. Can be
632
used for \typ{INT}, \typ{REAL}, \typ{POL} and \typ{SER} (for the last two
633
types, only 0 or 1 are possible).
634
635
\fun{long}{gsigne}{GEN x} returns the sign of a real number $x$,
636
valid for \typ{INT}, \typ{REAL} as \kbd{signe}, but also for \typ{FRAC}
637
and \typ{QUAD} of positive discriminants. Raise a type error if \kbd{typ(x)}
638
is not among those.
639
640
\fun{long}{expi}{GEN x} returns the binary exponent of the real number equal
641
to the \typ{INT}~\kbd{x}. This is a special case of \kbd{gexpo}.
642
643
\fun{long}{expo}{GEN x} returns the binary exponent of the
644
\typ{REAL}~\kbd{x}.
645
646
\fun{long}{mpexpo}{GEN x} returns the binary exponent of the \typ{INT}
647
or \typ{REAL}~\kbd{x}.
648
649
\fun{long}{gexpo}{GEN x} same as \kbd{expo}, but also valid when \kbd{x}
650
is not a \typ{REAL} (returns the largest exponent found among the components
651
of \kbd{x}). When \kbd{x} is an exact~0, this returns
652
\hbox{\kbd{-HIGHEXPOBIT}}, which is lower than any valid exponent.
653
654
\fun{long}{gexpo_safe}{GEN x} same as \kbd{gexpo}, but returns a value
655
strictly less than \hbox{\kbd{-HIGHEXPOBIT}} when the exponent is not defined
656
(e.g. for a \typ{PADIC} or \typ{INTMOD} component).
657
658
\fun{long}{valp}{GEN x} returns the $p$-adic valuation (for
659
a \typ{PADIC}) or $X$-adic valuation (for a \typ{SER}, taken with respect to
660
the main variable) of~\kbd{x}.
661
662
\fun{long}{precp}{GEN x} returns the precision of the \typ{PADIC}~\kbd{x}.
663
664
\fun{long}{varn}{GEN x} returns the variable number of the
665
\typ{POL} or \typ{SER}~\kbd{x} (between 0 and \kbd{MAXVARN}).
666
667
\fun{long}{gvar}{GEN x} returns the main variable number when any variable
668
at all occurs in the composite object~\kbd{x} (the smallest variable number
669
which occurs), and \tet{NO_VARIABLE} otherwise.
670
671
\fun{long}{gvar2}{GEN x} returns the variable number for the ring over which
672
$x$ is defined, e.g. if $x\in \Z[a][b]$ return (the variable number for)
673
$a$. Return \tet{NO_VARIABLE} if $x$ has no variable or is not defined over a
674
polynomial ring.
675
676
\fun{long}{degpol}{GEN x} is a simple macro returning \kbd{lg(x) - 3}.
677
This is the degree of the \typ{POL}~\kbd{x} with respect to its main
678
variable, \emph{if} its leading coefficient is nonzero (a rational $0$ is
679
impossible, but an inexact $0$ is allowed, as well as an exact modular $0$,
680
e.g. \kbd{Mod(0,2)}). If $x$ has no coefficients (rational $0$ polynomial),
681
its length is $2$ and we return the expected $-1$.
682
683
\fun{long}{lgpol}{GEN x} is equal to \kbd{degpol(x) + 1}. Used to loop over
684
the coefficients of a \typ{POL} in the following situation:
685
\bprog
686
GEN xd = x + 2;
687
long i, l = lgpol(x);
688
for (i = 0; i < l; i++) foo( xd[i] ).
689
@eprog
690
691
\fun{long}{precision}{GEN x} If \kbd{x} is of type \typ{REAL}, returns the
692
precision of~\kbd{x}, namely the length of \kbd{x} in \B-bit words if \kbd{x}
693
is not zero, and a reasonable quantity obtained from the exponent of \kbd{x}
694
if \kbd{x} is numerically equal to zero. If \kbd{x} is of type
695
\typ{COMPLEX}, returns the minimum of the precisions of the real and
696
imaginary part. Otherwise, returns~0 (which stands for infinite precision).
697
698
\fun{long}{lgcols}{GEN x} is equal to \kbd{lg(gel(x,1))}. This is the length
699
of the columns of a \typ{MAT} with at least one column.
700
701
\fun{long}{nbrows}{GEN x} is equal to \kbd{lg(gel(x,1))-1}. This is the number
702
of rows of a \typ{MAT} with at least one column.
703
704
\fun{long}{gprecision}{GEN x} as \kbd{precision} for scalars. Returns the
705
lowest precision encountered among the components otherwise.
706
707
\fun{long}{sizedigit}{GEN x} returns 0 if \kbd{x} is exactly~0. Otherwise,
708
returns \kbd{\key{gexpo}(x)} multiplied by $\log_{10}(2)$. This gives a crude
709
estimate for the maximal number of decimal digits of the components
710
of~\kbd{x}.
711
712
\subsec{Eval type-dependent information}
713
These routines convert type-dependent information to bitmask to fill the
714
codewords of \kbd{GEN} objects (see \secref{se:impl}). E.g for a
715
\typ{REAL}~\kbd{z}:
716
\bprog
717
z[1] = evalsigne(-1) | evalexpo(2)
718
@eprog
719
Compatible components of a codeword for a given type can be OR-ed as above.
720
721
\fun{ulong}{evaltyp}{long x} convert type~\kbd{x} to bitmask (first
722
codeword of all \kbd{GEN}s)
723
724
\fun{long}{evallg}{long x} convert length~\kbd{x} to bitmask (first
725
codeword of all \kbd{GEN}s). Raise overflow error if \kbd{x} is so large that
726
the corresponding length cannot be represented
727
728
\fun{long}{_evallg}{long x} as \kbd{evallg} \emph{without} the overflow
729
check.
730
731
\fun{ulong}{evalvarn}{long x} convert variable number~\kbd{x} to bitmask
732
(second codeword of \typ{POL} and \typ{SER})
733
734
\fun{long}{evalsigne}{long x} convert sign~\kbd{x} (in $-1,0,1$) to bitmask
735
(second codeword of \typ{INT}, \typ{REAL}, \typ{POL}, \typ{SER})
736
737
\fun{long}{evalprecp}{long x} convert $p$-adic ($X$-adic) precision~\kbd{x}
738
to bitmask (second codeword of \typ{PADIC}, \typ{SER}). Raise overflow error
739
if \kbd{x} is so large that the corresponding precision cannot be
740
represented.
741
742
\fun{long}{_evalprecp}{long x} same as \kbd{evalprecp} \emph{without} the
743
overflow check.
744
745
\fun{long}{evalvalp}{long x} convert $p$-adic ($X$-adic) valuation~\kbd{x} to
746
bitmask (second codeword of \typ{PADIC}, \typ{SER}). Raise overflow error if
747
\kbd{x} is so large that the corresponding valuation cannot be represented.
748
749
\fun{long}{_evalvalp}{long x} same as \kbd{evalvalp} \emph{without} the
750
overflow check.
751
752
\fun{long}{evalexpo}{long x} convert exponent~\kbd{x} to bitmask (second
753
codeword of \typ{REAL}). Raise overflow error if \kbd{x} is so
754
large that the corresponding exponent cannot be represented
755
756
\fun{long}{_evalexpo}{long x} same as \kbd{evalexpo} \emph{without} the
757
overflow check.
758
759
\fun{long}{evallgefint}{long x} convert effective length~\kbd{x} to bitmask
760
(second codeword \typ{INT}). This should be less or equal than the length
761
of the \typ{INT}, hence there is no overflow check for the effective length.
762
763
\subsec{Set type-dependent information}
764
Use these functions and macros with extreme care since usually the
765
corresponding information is set otherwise, and the components and further
766
codeword fields (which are left unchanged) may not be compatible with the new
767
information.
768
769
\fun{void}{settyp}{GEN x, long s} sets the type number of~\kbd{x} to~\kbd{s}.
770
771
\fun{void}{setlg}{GEN x, long s} sets the length of~\kbd{x} to~\kbd{s}. This
772
is an efficient way of truncating vectors, matrices or polynomials.
773
774
\fun{void}{setlgefint}{GEN x, long s} sets the effective length
775
of the \typ{INT} \kbd{x} to~\kbd{s}. The number \kbd{s} must be less than or
776
equal to the length of~\kbd{x}.
777
778
\fun{void}{setsigne}{GEN x, long s} sets the sign of~\kbd{x} to~\kbd{s}.
779
If \kbd{x} is a \typ{INT} or \typ{REAL}, \kbd{s} must be equal to $-1$, 0
780
or~1, and if \kbd{x} is a \typ{POL} or \typ{SER}, \kbd{s} must be equal to 0
781
or~1. No sanity check is made; in particular, setting the sign of a
782
$0$ \typ{INT} to $\pm1$ creates an invalid object.
783
784
\fun{void}{togglesign}{GEN x} sets the sign $s$ of~\kbd{x} to $-s$, in place.
785
786
\fun{void}{togglesign_safe}{GEN *x} sets the $s$ sign of~\kbd{*x} to $-s$, in
787
place, unless \kbd{*x} is one of the integer universal constants in which case
788
replace \kbd{*x} by its negation (e.g.~replace \kbd{gen\_1} by \kbd{gen\_m1}).
789
790
\fun{void}{setabssign}{GEN x} sets the sign $s$ of~\kbd{x} to $|s|$, in place.
791
792
\fun{void}{affectsign}{GEN x, GEN y} shortcut for \kbd{setsigne(y, signe(x))}.
793
No sanity check is made; in particular, setting the sign of a
794
$0$ \typ{INT} to $\pm1$ creates an invalid object.
795
796
\fun{void}{affectsign_safe}{GEN x, GEN *y} sets the sign of~\kbd{*y} to that
797
of~\kbd{x}, in place, unless \kbd{*y} is one of the integer universal
798
constants in which case replace \kbd{*y} by its negation if needed
799
(e.g.~replace \kbd{gen\_1} by \kbd{gen\_m1} if \kbd{x} is negative). No other
800
sanity check is made; in particular, setting the sign of a $0$
801
\typ{INT} to $\pm1$ creates an invalid object.
802
803
\fun{void}{normalize_frac}{GEN z} assuming $z$ is of the form \kbd{mkfrac(a,b)}
804
with $b\neq 0$, make sure that $b > 0$ by changing the sign of $a$ in place if
805
needed (use \kbd{togglesign}).
806
807
\fun{void}{setexpo}{GEN x, long s} sets the binary exponent of the
808
\typ{REAL}~\kbd{x} to \kbd{s}. The value \kbd{s} must be a 24-bit signed
809
number.
810
811
\fun{void}{setvalp}{GEN x, long s} sets the $p$-adic or $X$-adic valuation
812
of~\kbd{x} to~\kbd{s}, if \kbd{x} is a \typ{PADIC} or a \typ{SER},
813
respectively.
814
815
\fun{void}{setprecp}{GEN x, long s} sets the $p$-adic precision of the
816
\typ{PADIC}~\kbd{x} to~\kbd{s}.
817
818
\fun{void}{setvarn}{GEN x, long s} sets the variable number of the \typ{POL}
819
or \typ{SER}~\kbd{x} to~\kbd{s} (where $0\le \kbd{s}\le\kbd{MAXVARN}$).
820
821
\subsec{Type groups}\label{se:typegroup}
822
In the following functions, \kbd{t} denotes the type of a \kbd{GEN}.
823
They used to be implemented as macros, which could evaluate their argument
824
twice; \emph{no longer}: it is not inefficient to write
825
\bprog
826
is_intreal_t(typ(x))
827
@eprog
828
829
\fun{int}{is_recursive_t}{long t} \kbd{true} iff \kbd{t} is a recursive
830
type (the nonrecursive types are \typ{INT}, \typ{REAL},
831
\typ{STR}, \typ{VECSMALL}). Somewhat contrary to intuition, \typ{LIST} is
832
also nonrecursive, ; see the Developer's guide for details.
833
834
\fun{int}{is_intreal_t}{long t} \kbd{true} iff \kbd{t} is \typ{INT}
835
or \typ{REAL}.
836
837
\fun{int}{is_rational_t}{long t} \kbd{true} iff \kbd{t} is \typ{INT}
838
or \typ{FRAC}.
839
840
\fun{int}{is_real_t}{long t} \kbd{true} iff \kbd{t} is \typ{INT}
841
or \typ{REAL} or \typ{FRAC}.
842
843
\fun{int}{is_qfb_t}{long t} \kbd{true} iff \kbd{t} is \typ{QFB}.
844
845
\fun{int}{is_vec_t}{long t} \kbd{true} iff \kbd{t} is \typ{VEC}
846
or \typ{COL}.
847
848
\fun{int}{is_matvec_t}{long t} \kbd{true} iff \kbd{t} is \typ{MAT}, \typ{VEC}
849
or \typ{COL}.
850
851
\fun{int}{is_scalar_t}{long t} \kbd{true} iff \kbd{t} is a scalar, i.e
852
a \typ{INT},
853
a \typ{REAL},
854
a \typ{INTMOD},
855
a \typ{FRAC},
856
a \typ{COMPLEX},
857
a \typ{PADIC},
858
a \typ{QUAD},
859
or
860
a \typ{POLMOD}.
861
862
\fun{int}{is_extscalar_t}{long t} \kbd{true} iff \kbd{t} is a scalar (see
863
\kbd{is\_scalar\_t}) or \kbd{t} is \typ{POL}.
864
865
\fun{int}{is_const_t}{long t} \kbd{true} iff \kbd{t} is a scalar which is not
866
\typ{POLMOD}.
867
868
\fun{int}{is_noncalc_t}{long t} true if generic operations (\kbd{gadd},
869
\kbd{gmul}) do not make sense for $t$: corresponds to types
870
\typ{LIST}, \typ{STR}, \typ{VECSMALL}, \typ{CLOSURE}
871
872
\subsec{Accessors and components}\label{se:accessors}
873
The first two functions return \kbd{GEN} components as copies on the stack:
874
875
\fun{GEN}{compo}{GEN x, long n} creates a copy of the \kbd{n}-th true
876
component (i.e.\ not counting the codewords) of the object~\kbd{x}.
877
878
\fun{GEN}{truecoeff}{GEN x, long n} creates a copy of the coefficient of
879
degree~\kbd{n} of~\kbd{x} if \kbd{x} is a scalar, \typ{POL} or \typ{SER},
880
and otherwise of the \kbd{n}-th component of~\kbd{x}.
881
\smallskip
882
883
\noindent On the contrary, the following routines return the address of a
884
\kbd{GEN} component. No copy is made on the stack:
885
886
\fun{GEN}{constant_coeff}{GEN x} returns the address of the constant
887
coefficient of \typ{POL}~\kbd{x}. By convention, a $0$ polynomial (whose
888
\kbd{sign} is $0$) has \kbd{gen\_0} constant term.
889
890
\fun{GEN}{leading_coeff}{GEN x} returns the address of the leading coefficient
891
of \typ{POL}~\kbd{x}, i.e. the coefficient of largest index stored in the
892
array representing $x$. This may be an inexact $0$. By convention, return
893
\kbd{gen\_0} if the coefficient array is empty.
894
895
\fun{GEN}{gel}{GEN x, long i} returns the address of the
896
\kbd{x[i]} entry of~\kbd{x}. (\kbd{el} stands for element.)
897
898
\fun{GEN}{gcoeff}{GEN x, long i, long j} returns the address of the
899
\kbd{x[i,j]} entry of \typ{MAT}~\kbd{x}, i.e.~the coefficient at row~\kbd{i}
900
and column~\kbd{j}.
901
902
\fun{GEN}{gmael}{GEN x, long i, long j} returns the address of the
903
\kbd{x[i][j]} entry of~\kbd{x}. (\kbd{mael} stands for multidimensional array
904
element.)
905
906
\fun{GEN}{gmael2}{GEN A, long x1, long x2} is an alias for \kbd{gmael}.
907
Similar macros \tet{gmael3}, \tet{gmael4}, \tet{gmael5} are available.
908
909
\section{Global numerical constants}
910
These are defined in the various public PARI headers.
911
912
\subsec{Constants related to word size}
913
914
\noindent \kbd{long} $\tet{BITS_IN_LONG} = 2^{\tet{TWOPOTBITS_IN_LONG}}$:
915
number of bits in a \kbd{long} (32 or 64).
916
917
\noindent \kbd{long} \tet{BITS_IN_HALFULONG}: \kbd{BITS\_IN\_LONG} divided by
918
$2$.
919
920
\noindent \kbd{long} \tet{LONG_MAX}: the largest positive \kbd{long}.
921
922
\noindent \kbd{ulong} \tet{ULONG_MAX}: the largest \kbd{ulong}.
923
924
\noindent \kbd{long} \tet{DEFAULTPREC}: the length (\kbd{lg}) of a
925
\typ{REAL} with 64 bits of accuracy
926
927
\noindent \kbd{long} \tet{MEDDEFAULTPREC}: the length (\kbd{lg}) of a
928
\typ{REAL} with 128 bits of accuracy
929
930
\noindent \kbd{long} \tet{BIGDEFAULTPREC}: the length (\kbd{lg}) of a
931
\typ{REAL} with 192 bits of accuracy
932
933
\noindent \kbd{ulong} \tet{HIGHBIT}: the largest power of $2$ fitting in an
934
\kbd{ulong}.
935
936
\noindent \kbd{ulong} \tet{LOWMASK}: bitmask yielding the least significant
937
bits.
938
939
\noindent \kbd{ulong} \tet{HIGHMASK}: bitmask yielding the most significant
940
bits.
941
942
\noindent The last two are used to implement the following convenience macros,
943
returning half the bits of their operand:
944
945
\fun{ulong}{LOWWORD}{ulong a} returns least significant bits.
946
947
\fun{ulong}{HIGHWORD}{ulong a} returns most significant bits.
948
949
\noindent Finally
950
951
\fun{long}{divsBIL}{long n} returns the Euclidean quotient of $n$ by
952
\kbd{BITS\_IN\_LONG} (with nonnegative remainder).
953
954
\fun{long}{remsBIL}{n} returns the (nonnegative) Euclidean remainder of $n$
955
by \kbd{BITS\_IN\_LONG}
956
957
\fun{long}{dvmdsBIL}{long n, long *r}
958
959
\fun{ulong}{dvmduBIL}{ulong n, ulong *r} sets $r$ to \kbd{remsBIL(n)}
960
and returns \kbd{divsBIL(n)}.
961
962
\subsec{Masks used to implement the \kbd{GEN} type}
963
964
These constants are used by higher level macros, like \kbd{typ} or \kbd{lg}:
965
966
\noindent \tet{EXPOnumBITS},
967
\tet{LGnumBITS},
968
\tet{SIGNnumBITS},
969
\tet{TYPnumBITS},
970
\tet{VALPnumBITS},
971
\tet{VARNnumBITS}:
972
number of bits used to encode \kbd{expo}, \kbd{lg}, \kbd{signe},
973
\kbd{typ}, \kbd{valp}, \kbd{varn}.
974
975
\noindent \tet{PRECPSHIFT},
976
\tet{SIGNSHIFT},
977
\tet{TYPSHIFT},
978
\tet{VARNSHIFT}: shifts used to recover or encode \kbd{precp}, \kbd{varn},
979
\kbd{typ}, \kbd{signe}
980
981
\noindent \tet{CLONEBIT},
982
\tet{EXPOBITS},
983
\tet{LGBITS},
984
\tet{PRECPBITS},
985
\tet{SIGNBITS},
986
\tet{TYPBITS},
987
\tet{VALPBITS},
988
\tet{VARNBITS}: bitmasks used to extract \kbd{isclone}, \kbd{expo}, \kbd{lg},
989
\kbd{precp}, \kbd{signe}, \kbd{typ}, \kbd{valp}, \kbd{varn} from \kbd{GEN}
990
codewords.
991
992
\noindent \tet{MAXVARN}: the largest possible variable number.
993
994
\noindent \tet{NO_VARIABLE}: sentinel returned by \kbd{gvar(x)} when \kbd{x}
995
does not contain any polynomial; has a lower priority than any valid variable
996
number.
997
998
\noindent \tet{HIGHEXPOBIT}: a power of $2$, one more that the largest possible
999
exponent for a \typ{REAL}.
1000
1001
\noindent \tet{HIGHVALPBIT}: a power of $2$, one more that the largest possible
1002
valuation for a \typ{PADIC} or a \typ{SER}.
1003
1004
\subsec{$\log 2$, $\pi$}
1005
1006
These are \kbd{double} approximations to useful constants:
1007
1008
\noindent \tet{M_PI}: $\pi$.
1009
1010
\noindent \tet{M_LN2}: $\log 2$.
1011
1012
\noindent \tet{LOG10_2}: $\log 2 / \log 10$.
1013
1014
\noindent \tet{LOG2_10}: $\log 10 / \log 2$.
1015
1016
\section{Iterating over small primes, low-level interface}
1017
\label{se:primetable}
1018
1019
One of the methods used by the high-level prime iterator (see
1020
\secref{se:primeiter}), is a precomputed table. Its direct use is deprecated,
1021
but documented here.
1022
1023
After \kbd{pari\_init(size, maxprime)}, a ``prime table'' is
1024
initialized with the successive \emph{differences} of primes up to (possibly
1025
just a little beyond) \kbd{maxprime}. The prime table occupies roughly
1026
$\kbd{maxprime}/\log(\kbd{maxprime})$ bytes in memory, so be sensible when
1027
choosing \kbd{maxprime}; it is $500000$ by default under \kbd{gp} and there
1028
is no real benefit in choosing a much larger value: the high-level
1029
iterator provide \emph{fast} access to primes up to the \emph{square}
1030
of \kbd{maxprime}. In any case, the implementation requires that
1031
$\tet{maxprime} < 2^{\B} - 2048$, whatever memory is available.
1032
1033
PARI currently guarantees that the first 6547 primes, up to and including
1034
65557, are present in the table, even if you set \kbd{maxprime} to zero.
1035
in the \kbd{pari\_init} call.
1036
1037
\noindent Some convenience functions:
1038
1039
\fun{ulong}{maxprime}{} the largest prime computable using our prime table.
1040
1041
\fun{ulong}{maxprimeN}{} the index $N$ of the largest prime computable using
1042
the prime table. I.e., $p_N = \kbd{maxprime()}$.
1043
1044
\fun{void}{maxprime_check}{ulong B} raise an error if \kbd{maxprime()} is $< B$.
1045
1046
After the following initializations (the names $p$ and \var{ptr} are
1047
arbitrary of course)
1048
\bprog
1049
byteptr ptr = diffptr;
1050
ulong p = 0;
1051
@eprog
1052
\noindent calling the macro \tet{NEXT_PRIME_VIADIFF_CHECK}$(p, \var{ptr})$
1053
repeatedly will assign the successive prime numbers to $p$. Overrunning the
1054
prime table boundary will raise the error \tet{e_MAXPRIME}, which just
1055
prints the error message:
1056
1057
\kbd{*** not enough precomputed primes, need primelimit \til $c$}
1058
1059
\noindent (for some numerical value $c$), then the macro aborts the
1060
computation. The alternative macro \tet{NEXT_PRIME_VIADIFF} operates in the
1061
same way, but will omit that check, and is slightly faster. It should be used
1062
in the following way:
1063
%
1064
\bprog
1065
byteptr ptr = diffptr;
1066
ulong p = 0;
1067
1068
if (maxprime() < goal) pari_err_MAXPRIME(goal); /*@Ccom not enough primes */
1069
while (p <= goal) /*@Ccom run through all primes up to \kbd{goal} */
1070
{
1071
NEXT_PRIME_VIADIFF(p, ptr);
1072
...
1073
}
1074
@eprog\noindent
1075
Here, we use the general error handling function \kbd{pari\_err} (see
1076
\secref{se:err}), with the codeword \kbd{e\_MAXPRIME}, raising the ``not enough
1077
primes'' error. This could be rewritten as
1078
\bprog
1079
maxprime_check(goal);
1080
while (p <= goal) /*@Ccom run through all primes up to \kbd{goal} */
1081
{
1082
NEXT_PRIME_VIADIFF(p, ptr);
1083
...
1084
}
1085
@eprog
1086
1087
\fun{bytepr}{initprimes}{ulong maxprime, long *L, ulong *lastp}
1088
computes a (malloc'ed) ``prime table'', in fact a table of all prime
1089
differences for $p < \kbd{maxprime}$ (and possibly a little beyond). Set $L$
1090
to the table length (argument to \kbd{malloc}), and \var{lastp} to the last
1091
prime in the table.
1092
1093
\fun{void}{initprimetable}{ulong maxprime} computes a prime table (of all prime
1094
differences for $p < \kbd{maxprime}$) and assign it to the global variable
1095
\kbd{diffptr}. Don't change \kbd{diffptr} directly, call this function
1096
instead. This calls \kbd{initprimes} and updates internal data recording the
1097
table size.
1098
1099
\fun{ulong}{init_primepointer_geq}{ulong a, byteptr *pd}
1100
returns the smallest prime $p \geq a$, and sets \kbd{*pd} to the proper offset
1101
of \kbd{diffptr} so that \kbd{NEXT\_PRIME\_VIADIFF(p, *pd)} correctly
1102
returns \kbd{unextprime(p + 1)}.
1103
1104
\fun{ulong}{init_primepointer_gt}{ulong a, byteptr *pd} returns the smallest
1105
prime $p > a$.
1106
1107
\fun{ulong}{init_primepointer_leq}{ulong a, byteptr *pd} returns the largest
1108
prime $p \leq a$.
1109
1110
\fun{ulong}{init_primepointer_lt}{ulong a, byteptr *pd} returns the largest
1111
prime $p < a$.
1112
1113
\section{Handling the PARI stack}
1114
1115
\subsec{Allocating memory on the stack}
1116
1117
\fun{GEN}{cgetg}{long n, long t} allocates memory on the stack for
1118
an object of length \kbd{n} and type~\kbd{t}, and initializes its first
1119
codeword.
1120
1121
\fun{GEN}{cgeti}{long n} allocates memory on the stack for a \typ{INT}
1122
of length~\kbd{n}, and initializes its first codeword. Identical to
1123
\kbd{cgetg(n,\typ{INT})}.
1124
1125
\fun{GEN}{cgetr}{long n} allocates memory on the stack for a \typ{REAL}
1126
of length~\kbd{n}, and initializes its first codeword. Identical to
1127
\kbd{cgetg(n,\typ{REAL})}.
1128
1129
\fun{GEN}{cgetc}{long n} allocates memory on the stack for a
1130
\typ{COMPLEX}, whose real and imaginary parts are \typ{REAL}s
1131
of length~\kbd{n}.
1132
1133
\fun{GEN}{cgetp}{GEN x} creates space sufficient to hold the
1134
\typ{PADIC}~\kbd{x}, and sets the prime $p$ and the $p$-adic precision to
1135
those of~\kbd{x}, but does not copy (the $p$-adic unit or zero representative
1136
and the modulus of)~\kbd{x}.
1137
1138
\fun{GEN}{new_chunk}{size_t n} allocates a \kbd{GEN} with $n$ components,
1139
\emph{without} filling the required code words. This is the low-level
1140
constructor underlying \kbd{cgetg}, which calls \kbd{new\_chunk} then sets
1141
the first code word. It works by simply returning the address
1142
\kbd{((GEN)avma) - n}, after checking that it is larger than \kbd{(GEN)bot}.
1143
1144
\fun{void}{new_chunk_resize}{size_t x} this function is called by
1145
\kbd{new\_chunk} when the PARI stack overflows. There is no need to call it
1146
manually. It will either extend the stack or report an \kbd{e\_STACK} error.
1147
1148
\fun{char*}{stack_malloc}{size_t n} allocates memory on the stack for $n$
1149
chars (\emph{not} $n$ \kbd{GEN}s). This is faster than using \kbd{malloc},
1150
and easier to use in most situations when temporary storage is needed. In
1151
particular there is no need to \kbd{free} individually all variables thus
1152
allocated: a simple \kbd{set\_avma(oldavma)} might be enough. On the other hand,
1153
beware that this is not permanent independent storage, but part of the stack.
1154
The memory is aligned on \kbd{sizeof(long)} bytes boundaries.
1155
1156
\fun{char*}{stack_malloc_align}{size_t n, long k} as \kbd{stack\_malloc},
1157
but the memory is aligned on \kbd{k} bytes boundaries. The number\kbd{k} must
1158
be a multiple of the \kbd{sizeof(long)}.
1159
1160
\fun{char*}{stack_calloc}{size_t n} as \kbd{stack\_malloc}, setting the memory
1161
to zero.
1162
1163
\fun{char*}{stack_calloc_align}{size_t n, long k} as
1164
\kbd{stack\_malloc\_align}, setting the memory to zero.
1165
1166
\noindent Objects allocated through these last three functions cannot be
1167
\kbd{gerepile}'d, since they are not yet valid \kbd{GEN}s: their codewords
1168
must be filled first.
1169
1170
\fun{GEN}{cgetalloc}{long t, size_t l}, same as \kbd{cgetg(t, l)}, except
1171
that the result is allocated using \tet{pari_malloc} instead of the PARI
1172
stack. The resulting \kbd{GEN} is now impervious to garbage collecting
1173
routines, but should be freed using \tet{pari_free}.
1174
1175
\subsec{Stack-independent binary objects}
1176
1177
\fun{GENbin*}{copy_bin}{GEN x} copies $x$ into a malloc'ed structure suitable
1178
for stack-independent binary transmission or storage. The object obtained
1179
is architecture independent provided, \kbd{sizeof(long)} remains the same
1180
on all PARI instances involved, as well as the multiprecision kernel (either
1181
native or GMP).
1182
1183
\fun{GENbin*}{copy_bin_canon}{GEN x} as \kbd{copy\_bin}, ensuring furthermore
1184
that the binary object is independent of the multiprecision kernel. Slower
1185
than \kbd{copy\_bin}.
1186
1187
\fun{GEN}{bin_copy}{GENbin *p} assuming $p$ was created by \kbd{copy\_bin(x)}
1188
(not necessarily by the same PARI instance: transmission or external storage
1189
may be involved), restores $x$ on the PARI stack.
1190
1191
\noindent The routine \kbd{bin\_copy} transparently encapsulate the following
1192
functions:
1193
1194
\fun{GEN}{GENbinbase}{GENbin *p} the \kbd{GEN} data actually stored in $p$.
1195
All addresses are stored as offsets with respect to a common reference point,
1196
so the resulting \kbd{GEN} is unusable unless it is a nonrecursive type;
1197
private low-level routines must be called first to restore absolute addresses.
1198
1199
\fun{void}{shiftaddress}{GEN x, long dec} converts relative addresses to
1200
absolute ones.
1201
1202
\fun{void}{shiftaddress_canon}{GEN x, long dec} converts relative addresses to
1203
absolute ones, and converts leaves from a canonical form to the one
1204
specific to the multiprecision kernel in use. The \kbd{GENbin} type stores
1205
whether leaves are stored in canonical form, so \kbd{bin\_copy} can call
1206
the right variant.
1207
1208
\noindent Objects containing closures are harder to e.g. copy and save to disk,
1209
since closures contain pointers to libpari functions that will not be valid in
1210
another gp instance: there is little chance for them to be loaded at the exact
1211
same address in memory. Such objects must be saved along with a linking table.
1212
1213
\fun{GEN}{copybin_unlink}{GEN C} returns a linking table allowing to safely
1214
store and transmit \typ{CLOSURE} objects in $C$. If $C = \kbd{NULL}$ return a
1215
linking table corresponding to the content of all gp variables. $C$ may then be
1216
dumped to disk in binary form, for instance.
1217
1218
\fun{void}{bincopy_relink}{GEN C, GEN V} given a binary object $C$, as dumped
1219
by writebin and read back into a session, and a linking table $V$, restore all
1220
closures contained in $C$ (function pointers are translated to their current
1221
value).
1222
1223
\subsec{Garbage collection}
1224
See \secref{se:garbage} for a detailed explanation and many examples.
1225
1226
\fun{void}{set_avma}{ulong av} reset \kbd{avma} to \kbd{av}.
1227
1228
\fun{GEN}{gc_NULL}{pari_sp av} reset \kbd{avma} to \kbd{av} and return
1229
\kbd{NULL}.
1230
1231
The following 6 functions reset \kbd{avma} to \kbd{av} and return $x$:
1232
1233
\fun{int}{gc_bool}{pari_sp av, int x}
1234
1235
\fun{double}{gc_double}{pari_sp av, double x}
1236
1237
\fun{int}{gc_int}{pari_sp av, int x}
1238
1239
\fun{long}{gc_long}{pari_sp av, long x}
1240
1241
\fun{ulong}{gc_ulong}{pari_sp av, ulong x} This allows for instance
1242
to return \kbd{gc\_ulong(av, itou(z))}, whereas
1243
\bprog
1244
pari_sp av = avma;
1245
GEN z = ...
1246
set_avma(av);
1247
return itou(z);
1248
@eprog should be frowned upon since \kbd{set\_avma(av)} conceptually
1249
destroys everything from the reference point on, including $z$.
1250
1251
\fun{long}{gc_const}{pari_sp av, GEN x} assumes that $x$ is either not on the
1252
stack (clone, universal constant such as \kbd{gen\_0}) or was defined
1253
before \kbd{av}.
1254
1255
\fun{void}{cgiv}{GEN x} frees object \kbd{x}, assuming it is the last created
1256
on the stack.
1257
1258
\fun{GEN}{gerepile}{pari_sp p, pari_sp q, GEN x} general garbage collector
1259
for the stack.
1260
1261
\fun{void}{gerepileall}{pari_sp av, int n, ...} cleans up the stack from
1262
\kbd{av} on (i.e from \kbd{avma} to \kbd{av}), preserving the \kbd{n} objects
1263
which follow in the argument list (of type \kbd{GEN*}). For instance,
1264
\kbd{gerepileall(av, 2, \&x, \&y)} preserves \kbd{x} and \kbd{y}.
1265
1266
\fun{void}{gerepileallsp}{pari_sp av, pari_sp ltop, int n, ...}
1267
cleans up the stack between \kbd{av} and \kbd{ltop}, updating
1268
the \kbd{n} elements which follow \kbd{n} in the argument list (of type
1269
\kbd{GEN*}). Check that the elements of \kbd{g} have no component between
1270
\kbd{av} and \kbd{ltop}, and assumes that no garbage is present between
1271
\kbd{avma} and \kbd{ltop}. Analogous to (but faster than) \kbd{gerepileall}
1272
otherwise.
1273
1274
\fun{GEN}{gerepilecopy}{pari_sp av, GEN x} cleans up the stack from
1275
\kbd{av} on, preserving the object \kbd{x}. Special case of \kbd{gerepileall}
1276
(case $\kbd{n} = 1$), except that the routine returns the preserved \kbd{GEN}
1277
instead of updating its address through a pointer.
1278
1279
\fun{void}{gerepilemany}{pari_sp av, GEN* g[], int n} alternative interface
1280
to \kbd{gerepileall}. The preserved \kbd{GEN}s are the elements of the array
1281
\kbd{g} of length $n$: \kbd{g[0]}, \kbd{g[1]}, \dots,
1282
\kbd{g[$n$-1]}. Obsolete: no more efficient than \kbd{gerepileall},
1283
error-prone, and clumsy (need to declare an extra \kbd{GEN *g}).
1284
1285
\fun{void}{gerepilemanysp}{pari_sp av, pari_sp ltop, GEN* g[], int n}
1286
alternative interface to \kbd{gerepileallsp}. Obsolete.
1287
1288
\fun{void}{gerepilecoeffs}{pari_sp av, GEN x, int n} cleans up the stack
1289
from \kbd{av} on, preserving \kbd{x[0]}, \dots, \kbd{x[n-1]} (which are
1290
\kbd{GEN}s).
1291
1292
\fun{void}{gerepilecoeffssp}{pari_sp av, pari_sp ltop, GEN x, int n}
1293
cleans up the stack from \kbd{av} to \kbd{ltop}, preserving \kbd{x[0]},
1294
\dots, \kbd{x[n-1]} (which are \kbd{GEN}s). Same assumptions as in
1295
\kbd{gerepilemanysp}, of which this is a variant. For instance
1296
\bprog
1297
z = cgetg(3, t_COMPLEX);
1298
av = avma; garbage(); ltop = avma;
1299
z[1] = fun1();
1300
z[2] = fun2();
1301
gerepilecoeffssp(av, ltop, z + 1, 2);
1302
return z;
1303
@eprog\noindent
1304
cleans up the garbage between \kbd{av} and \kbd{ltop}, and connects \kbd{z}
1305
and its two components. This is marginally more efficient than the standard
1306
\bprog
1307
av = avma; garbage(); ltop = avma;
1308
z = cgetg(3, t_COMPLEX);
1309
z[1] = fun1();
1310
z[2] = fun2(); return gerepile(av, ltop, z);
1311
@eprog\noindent
1312
1313
\fun{GEN}{gerepileupto}{pari_sp av, GEN q} analogous to (but faster than)
1314
\kbd{gerepilecopy}. Assumes that \kbd{q} is connected and that its root was
1315
created before any component. If \kbd{q} is not on the stack, this is
1316
equivalent to \kbd{set\_avma(av)}; in particular, sentinels which are not even
1317
proper \kbd{GEN}s such as \kbd{q = NULL} are allowed.
1318
1319
\fun{GEN}{gerepileuptoint}{pari_sp av, GEN q} analogous to (but faster than)
1320
\kbd{gerepileupto}. Assumes further that \kbd{q} is a \typ{INT}. The
1321
length and effective length of the resulting \typ{INT} are equal.
1322
1323
\fun{GEN}{gerepileuptoleaf}{pari_sp av, GEN q} analogous to (but faster than)
1324
\kbd{gerepileupto}. Assumes further that \kbd{q} is a leaf, i.e a
1325
nonrecursive type (\kbd{is\_recursive\_t(typ(q))} is nonzero). Contrary to
1326
\kbd{gerepileuptoint} and \kbd{gerepileupto}, \kbd{gerepileuptoleaf} leaves
1327
length and effective length of a \typ{INT} unchanged.
1328
1329
\subsec{Garbage collection: advanced use}
1330
1331
\fun{void}{stackdummy}{pari_sp av, pari_sp ltop} inhibits the memory area
1332
between \kbd{av} \emph{included} and \kbd{ltop} \emph{excluded} with respect to
1333
\kbd{gerepile}, in order to avoid a call to \kbd{gerepile(av, ltop,...)}.
1334
The stack space is not reclaimed though.
1335
1336
More precisely, this routine assumes that \kbd{av} is recorded earlier
1337
than \kbd{ltop}, then marks the specified stack segment as a
1338
nonrecursive type of the correct length. Thus gerepile will not inspect
1339
the zone, at most copy it. To be used in the following situation:
1340
\bprog
1341
av0 = avma; z = cgetg(t_VEC, 3);
1342
gel(z,1) = HUGE(); av = avma; garbage(); ltop = avma;
1343
gel(z,2) = HUGE(); stackdummy(av, ltop);
1344
@eprog\noindent
1345
Compared to the orthodox
1346
\bprog
1347
gel(z,2) = gerepile(av, ltop, gel(z,2));
1348
@eprog\noindent
1349
or even more wasteful
1350
\bprog
1351
z = gerepilecopy(av0, z);
1352
@eprog\noindent
1353
we temporarily lose $(\kbd{av} - \kbd{ltop})$ words but save a costly
1354
\kbd{gerepile}. In principle, a garbage collection higher up the call
1355
chain should reclaim this later anyway.
1356
1357
Without the \kbd{stackdummy}, if the $[\kbd{av}, \kbd{ltop}]$ zone is
1358
arbitrary (not even valid \kbd{GEN}s as could happen after direct
1359
truncation via \kbd{setlg}), we would leave dangerous data in the middle
1360
of~\kbd{z}, which would be a problem for a later
1361
\bprog
1362
gerepile(..., ... , z);
1363
@eprog\noindent
1364
And even if it were made of valid \kbd{GEN}s, inhibiting the area makes sure
1365
\kbd{gerepile} will not inspect their components, saving time.
1366
1367
Another natural use in low-level routines is to ``shorten'' an existing
1368
\kbd{GEN} \kbd{z} to its first $\kbd{n}-1$ components:
1369
\bprog
1370
setlg(z, n);
1371
stackdummy((pari_sp)(z + lg(z)), (pari_sp)(z + n));
1372
@eprog\noindent
1373
or to its last \kbd{n} components:
1374
\bprog
1375
long L = lg(z) - n, tz = typ(z);
1376
stackdummy((pari_sp)(z + L), (pari_sp)z);
1377
z += L; z[0] = evaltyp(tz) | evallg(L);
1378
@eprog
1379
1380
The first scenario (safe shortening an existing \kbd{GEN}) is in fact so
1381
common, that we provide a function for this:
1382
1383
\fun{void}{fixlg}{GEN z, long ly} a safe variant of \kbd{setlg(z, ly)}. If
1384
\kbd{ly} is larger than \kbd{lg(z)} do nothing. Otherwise, shorten $z$ in
1385
place, using \kbd{stackdummy} to avoid later \kbd{gerepile} problems.
1386
1387
\fun{GEN}{gcopy_avma}{GEN x, pari_sp *AVMA} return a copy of $x$ as from
1388
\kbd{gcopy}, except that we pretend that initially \kbd{avma} is \kbd{*AVMA},
1389
and that \kbd{*AVMA} is updated accordingly (so that the total size of $x$ is
1390
the difference between the two successive values of \kbd{*AVMA}). It is not
1391
necessary for \kbd{*AVMA} to initially point on the stack: \tet{gclone} is
1392
implemented using this mechanism.
1393
1394
\fun{GEN}{icopy_avma}{GEN x, pari_sp av} analogous to \kbd{gcopy\_avma} but
1395
simpler: assume $x$ is a \typ{INT} and return a copy allocated as if
1396
initially we had \kbd{avma} equal to \kbd{av}. There is no need to pass a
1397
pointer and update the value of the second argument: the new (fictitious)
1398
\kbd{avma} is just the return value (typecast to \kbd{pari\_sp}).
1399
1400
\subsec{Debugging the PARI stack}
1401
1402
\fun{int}{chk_gerepileupto}{GEN x} returns 1 if \kbd{x} is suitable for
1403
\kbd{gerepileupto}, and 0 otherwise. In the latter case, print a warning
1404
explaining the problem.
1405
1406
\fun{void}{dbg_gerepile}{pari_sp ltop} outputs the list of all objects on the
1407
stack between \kbd{avma} and \kbd{ltop}, i.e. the ones that would be inspected
1408
in a call to \kbd{gerepile(...,ltop,...)}.
1409
1410
\fun{void}{dbg_gerepileupto}{GEN q} outputs the list of all objects on the
1411
stack that would be inspected in a call to \kbd{gerepileupto(...,q)}.
1412
1413
\subsec{Copies}
1414
1415
\fun{GEN}{gcopy}{GEN x} creates a new copy of $x$ on the stack.
1416
1417
\fun{GEN}{gcopy_lg}{GEN x, long l} creates a new copy of $x$
1418
on the stack, pretending that \kbd{lg(x)} is $l$, which must be less than or
1419
equal to \kbd{lg(x)}. If equal, the function is equivalent to \kbd{gcopy(x)}.
1420
1421
\fun{int}{isonstack}{GEN x} \kbd{true} iff $x$ belongs to the stack.
1422
1423
\fun{void}{copyifstack}{GEN x, GEN y} sets \kbd{y = gcopy(x)} if
1424
$x$ belongs to the stack, and \kbd{y = x} otherwise. This macro evaluates
1425
its arguments once, contrary to
1426
\bprog
1427
y = isonstack(x)? gcopy(x): x;
1428
@eprog
1429
1430
\fun{void}{icopyifstack}{GEN x, GEN y} as \kbd{copyifstack} assuming \kbd{x}
1431
is a \typ{INT}.
1432
1433
\subsec{Simplify}
1434
1435
\fun{GEN}{simplify}{GEN x} you should not need that function in library mode.
1436
One rather uses:
1437
1438
\fun{GEN}{simplify_shallow}{GEN x} shallow, faster, version of \tet{simplify}.
1439
1440
\section{The PARI heap}
1441
\subsec{Introduction}
1442
1443
It is implemented as a doubly-linked list of \kbd{malloc}'ed blocks of
1444
memory, equipped with reference counts. Each block has type \kbd{GEN} but need
1445
not be a valid \kbd{GEN}: it is a chunk of data preceded by a hidden header
1446
(meaning that we allocate $x$ and return $x + \kbd{header size}$). A
1447
\tev{clone}, created by \tet{gclone}, is a block which is a valid \kbd{GEN}
1448
and whose \emph{clone bit} is set.
1449
1450
\subsec{Public interface}
1451
1452
\fun{GEN}{newblock}{size_t n} allocates a block of $n$ \emph{words} (not bytes).
1453
1454
\fun{void}{killblock}{GEN x} deletes the block~$x$ created by \kbd{newblock}.
1455
Fatal error if $x$ not a block.
1456
1457
\fun{GEN}{gclone}{GEN x} creates a new permanent copy of $x$ on the heap
1458
(allocated using \kbd{newblock}). The \emph{clone bit} of the result is set.
1459
1460
\fun{GEN}{gcloneref}{GEN x} if $x$ is not a clone, clone it and return the
1461
result; otherwise, increase the clone reference count and return $x$.
1462
1463
\fun{void}{gunclone}{GEN x} deletes a clone. Deletion at first only decreases
1464
the reference count by $1$. If the count remains positive, no further action is
1465
taken; if the count becomes zero, then the clone is actually deleted. In the
1466
current implementation, this is an alias for \kbd{killblock}, but it is cleaner
1467
to kill clones (valid \kbd{GEN}s) using this function, and other blocks using
1468
\kbd{killblock}.
1469
1470
\fun{void}{guncloneNULL}{GEN x} same as \kbd{gunclone}, first checking
1471
whether $x$ is \kbd{NULL} (and doing nothing in this case).
1472
1473
\fun{void}{gunclone_deep}{GEN x} is only useful in the context of the GP
1474
interpreter which may replace arbitrary components of container types
1475
(\typ{VEC}, \typ{COL}, \typ{MAT}, \typ{LIST}) by clones. If $x$ is such
1476
a container, the function recursively deletes all clones among the components
1477
of $x$, then unclones $x$. Useless in library mode: simply use
1478
\kbd{gunclone}.
1479
1480
\fun{void}{guncloneNULL_deep}{GEN x} same as \kbd{gunclone\_deep}, first
1481
checking whether $x$ is \kbd{NULL} (and doing nothing in this case).
1482
1483
\fun{void}{traverseheap}{void(*f)(GEN, void *), void *data} this applies
1484
\kbd{f($x$, data)} to each object $x$ on the PARI heap, most recent
1485
first. Mostly for debugging purposes.
1486
1487
\fun{GEN}{getheap}{} a simple wrapper around \kbd{traverseheap}. Returns a
1488
two-component row vector giving the number of objects on the heap and the
1489
amount of memory they occupy in long words.
1490
1491
\fun{GEN}{cgetg_block}{long x, long y} as \kbd{cgetg(x,y)}, creating the return
1492
value as a \kbd{block}, not on the PARI stack.
1493
1494
\fun{GEN}{cgetr_block}{long prec} as \kbd{cgetr(prec)}, creating the return
1495
value as a \kbd{block}, not on the PARI stack.
1496
1497
\subsec{Implementation note} The hidden block header is manipulated using the
1498
following private functions:
1499
1500
\fun{void*}{bl_base}{GEN x} returns the pointer that was actually allocated
1501
by \kbd{malloc} (can be freed).
1502
1503
\fun{long}{bl_refc}{GEN x} the reference count of $x$: the number of pointers
1504
to this block. Decremented in \kbd{killblock}, incremented by the private
1505
function \fun{void}{gclone_refc}{GEN x}; block is freed when the reference
1506
count reaches $0$.
1507
1508
\fun{long}{bl_num}{GEN x} the index of this block in the list of all blocks
1509
allocated so far (including freed blocks). Uniquely identifies a block until
1510
$2^\B$ blocks have been allocated and this wraps around.
1511
1512
\fun{GEN}{bl_next}{GEN x} the block \emph{after} $x$ in the linked list of
1513
blocks (\kbd{NULL} if $x$ is the last block allocated not yet killed).
1514
1515
\fun{GEN}{bl_prev}{GEN x} the block allocated \emph{before} $x$ (never
1516
\kbd{NULL}).
1517
1518
We documented the last four routines as functions for clarity (and type
1519
checking) but they are actually macros yielding valid lvalues. It is allowed
1520
to write \kbd{bl\_refc(x)++} for instance.
1521
1522
\section{Handling user and temp variables}
1523
Low-level implementation of user / temporary variables is liable to change. We
1524
describe it nevertheless for completeness. Currently variables are
1525
implemented by a single array of values divided in 3 zones: 0--\kbd{nvar}
1526
(user variables), \kbd{max\_avail}--\kbd{MAXVARN} (temporary variables),
1527
and \kbd{nvar+1}--\kbd{max\_avail-1} (pool of free variable numbers).
1528
1529
\subsec{Low-level}
1530
1531
\fun{void}{pari_var_init}{}: a small part of \kbd{pari\_init}. Resets
1532
variable counters \kbd{nvar} and \kbd{max\_avail}, notwithstanding existing
1533
variables! In effect, this even deletes \kbd{x}. Don't use it.
1534
1535
\fun{void}{pari_var_close}{void} attached destructor, called by
1536
\kbd{pari\_close}.
1537
1538
\fun{long}{pari_var_next}{}: returns \kbd{nvar}, the number of the next user
1539
variable we can create.
1540
1541
\fun{long}{pari_var_next_temp}{} returns \kbd{max\_avail}, the number of the
1542
next temp variable we can create.
1543
1544
\fun{long}{pari_var_create}{entree *ep} low-level initialization of an
1545
\kbd{EpVAR}. Return the attached (new) variable number.
1546
1547
\fun{GEN}{vars_sort_inplace}{GEN z} given a \typ{VECSMALL} $z$ of variable
1548
numbers, sort $z$ in place according to variable priorities (highest priority
1549
comes first).
1550
1551
\fun{GEN}{vars_to_RgXV}{GEN h} given a \typ{VECSMALL} $z$ of variable numbers,
1552
return the \typ{VEC} of \kbd{pol\_x}$(z[i])$.
1553
1554
\subsec{User variables}
1555
1556
\fun{long}{fetch_user_var}{char *s} returns a user variable whose name
1557
is \kbd{s}, creating it is needed (and using an existing variable otherwise).
1558
Returns its variable number.
1559
1560
\fun{GEN}{fetch_var_value}{long v} returns a shallow copy of the
1561
current value of the variable numbered $v$. Return \kbd{NULL} for a temporary
1562
variable.
1563
1564
\fun{entree*}{is_entry}{const char *s} returns the \kbd{entree*} attached
1565
to an identifier \kbd{s} (variable or function), from the interpreter
1566
hashtables. Return \kbd{NULL} is the identifier is unknown.
1567
1568
\subsec{Temporary variables}
1569
1570
\fun{long}{fetch_var}{void} returns the number of a new temporary variable
1571
(decreasing \kbd{max\_avail}).
1572
1573
\fun{long}{delete_var}{void} delete latest temp variable created and return
1574
the number of previous one.
1575
1576
\fun{void}{name_var}{long n, char *s} rename temporary variable number
1577
\kbd{n} to \kbd{s}; mostly useful for nicer printout. Error when trying to
1578
rename a user variable.
1579
1580
\section{Adding functions to PARI}
1581
\subsec{Nota Bene}
1582
%
1583
As mentioned in the \kbd{COPYING} file, modified versions of the PARI package
1584
can be distributed under the conditions of the GNU General Public License. If
1585
you do modify PARI, however, it is certainly for a good reason, and we
1586
would like to know about it, so that everyone can benefit from your changes.
1587
There is then a good chance that your improvements are incorporated into the
1588
next release.
1589
1590
We classify changes to PARI into four rough classes, where changes of the
1591
first three types are almost certain to be accepted. The first type includes
1592
all improvements to the documentation, in a broad sense. This includes
1593
correcting typos or inaccuracies of course, but also items which are not
1594
really covered in this document, e.g.~if you happen to write a tutorial,
1595
or pieces of code exemplifying fine points unduly omitted in the present
1596
manual.
1597
1598
The second type is to expand or modify the configuration routines and skeleton
1599
files (the \kbd{Configure} script and anything in the \kbd{config/}
1600
subdirectory) so that compilation is possible (or easier, or more efficient)
1601
on an operating system previously not catered for. This includes discovering
1602
and removing idiosyncrasies in the code that would hinder its portability.
1603
1604
The third type is to modify existing (mathematical) code, either to correct
1605
bugs, to add new functionality to existing functions, or to improve their
1606
efficiency.
1607
1608
Finally the last type is to add new functions to PARI. We explain here how
1609
to do this, so that in particular the new function can be called from \kbd{gp}.
1610
1611
\subsec{Coding guidelines}\label{se:coding_guidelines}
1612
\noindent
1613
Code your function in a file of its own, using as a guide other functions
1614
in the PARI sources. One important thing to remember is to clean the stack
1615
before exiting your main function, since otherwise successive calls to
1616
the function clutters the stack with unnecessary garbage, and stack
1617
overflow occurs sooner. Also, if it returns a \kbd{GEN} and you want it
1618
to be accessible to \kbd{gp}, you have to make sure this \kbd{GEN} is
1619
suitable for \kbd{gerepileupto} (see \secref{se:garbage}).
1620
1621
If error messages or warnings are to be generated in your function, use
1622
\kbd{pari\_err} and \kbd{pari\_warn} respectively.
1623
Recall that \kbd{pari\_err} does not return but ends with a \kbd{longjmp}
1624
statement. As well, instead of explicit \kbd{printf}~/ \kbd{fprintf}
1625
statements, use the following encapsulated variants:
1626
1627
\fun{void}{pari_putc}{char c}: write character \kbd{c} to the output stream.
1628
1629
\fun{void}{pari_puts}{char *s}: write \kbd{s} to the output stream.
1630
1631
\fun{void}{pari_printf}{const char *fmt, ...}: write following arguments to the
1632
output stream, according to the conversion specifications in format \kbd{fmt}
1633
(see \tet{printf}).
1634
1635
\fun{void}{err_printf}{const char *fmt, ...}: as \tet{pari_printf}, writing to
1636
PARI's current error stream.
1637
1638
\fun{void}{err_flush}{void} flush error stream.
1639
1640
Declare all public functions in an appropriate header file, if you
1641
want to access them from C. The other functions should be declared
1642
\kbd{static} in your file.
1643
1644
Your function is now ready to be used in library mode after compilation and
1645
creation of the library. If possible, compile it as a shared library (see
1646
the \kbd{Makefile} coming with the \kbd{extgcd} example in the
1647
distribution). It is however still inaccessible from \kbd{gp}.\smallskip
1648
1649
\subsec{GP prototypes, parser codes}
1650
\label{se:gp.interface}
1651
A \tev{GP prototype} is a character string describing all the GP parser needs
1652
to know about the function prototype. It contains a sequence of the following
1653
atoms:
1654
1655
\settabs\+\indent&\kbd{Dxxx}\quad&\cr
1656
1657
\noindent\item Return type: \kbd{GEN} by default (must be valid for
1658
\kbd{gerepileupto}), otherwise the following can appear as the \emph{first}
1659
char of the code string:
1660
%
1661
\+& \kbd{i} & return \kbd{int}\cr
1662
\+& \kbd{l} & return \kbd{long}\cr
1663
\+& \kbd{u} & return \kbd{ulong}\cr
1664
\+& \kbd{v} & return \kbd{void}\cr
1665
\+& \kbd{m} & return a \kbd{GEN} which is not \kbd{gerepile}-safe.\cr
1666
1667
The \kbd{m} code is used for member functions, to avoid unnecessary copies. A
1668
copy opcode is generated by the compiler if the result needs to be kept safe
1669
for later use.
1670
1671
\noindent\item Mandatory arguments, appearing in the same order as the
1672
input arguments they describe:
1673
%
1674
\+& \kbd{G} & \kbd{GEN}\cr
1675
\+& \kbd{\&}& \kbd{*GEN}\cr
1676
\+& \kbd{L} & \kbd{long} (we implicitly typecast \kbd{int} to \kbd{long})\cr
1677
\+& \kbd{U} & \kbd{ulong} \cr
1678
\+& \kbd{V} & loop variable\cr
1679
\+& \kbd{n} & variable, expects a \idx{variable number} (a \kbd{long}, not an
1680
\kbd{*entree})\cr
1681
\+& \kbd{W} & a \kbd{GEN} which is a lvalue to be modified in place
1682
(for \typ{LIST})\cr
1683
\+& \kbd{r} & raw input (treated as a string without quotes). Quoted
1684
args are copied as strings\cr
1685
\+&&\quad Stops at first unquoted \kbd{')'} or \kbd{','}. Special chars can
1686
be quoted using \kbd{'\bs'}\cr
1687
\+&&\quad Example: \kbd{aa"b\bs n)"c} yields the string \kbd{"aab\bs{n})c"}\cr
1688
\+& \kbd{s} & expanded string. Example: \kbd{Pi"x"2} yields \kbd{"3.142x2"}\cr
1689
\+&&\quad Unquoted components can be of any PARI type, converted to string
1690
following\cr
1691
\+&&\quad current output format\cr
1692
\+& \kbd{I} & closure whose value is ignored, as in \kbd{for} loops,\cr
1693
\+&&\quad to be processed by \fun{void}{closure_evalvoid}{GEN C}\cr
1694
\+& \kbd{E} & closure whose value is used, as in \kbd{sum} loops,\cr
1695
\+&&\quad to be processed by \fun{void}{closure_evalgen}{GEN C}\cr
1696
\+& \kbd{J} & implicit function of arity $1$, as in \kbd{parsum} loops,\cr
1697
\+&&\quad to be processed by \fun{void}{closure_callgen1}{GEN C}\cr
1698
1699
\noindent A \tev{closure} is a GP function in compiled (bytecode) form. It
1700
can be efficiently evaluated using the \kbd{closure\_eval}$xxx$ functions.
1701
1702
\noindent\item Automatic arguments:
1703
%
1704
\+& \kbd{f} & Fake \kbd{*long}. C function requires a pointer but we
1705
do not use the resulting \kbd{long}\cr
1706
\+& \kbd{b} & current real precision in bits \cr
1707
\+& \kbd{p} & current real precision in words \cr
1708
\+& \kbd{P} & series precision (default \kbd{seriesprecision},
1709
global variable \kbd{precdl} for the library)\cr
1710
\+& \kbd{C} & lexical context (internal, for \kbd{eval},
1711
see \kbd{localvars\_read\_str})\cr
1712
1713
\noindent\item Syntax requirements, used by functions like
1714
\kbd{for}, \kbd{sum}, etc.:
1715
%
1716
\+& \kbd{=} & separator \kbd{=} required at this point (between two
1717
arguments)\cr
1718
1719
\noindent\item Optional arguments and default values:
1720
%
1721
\+& \kbd{E*} & any number of expressions, possibly 0 (see \kbd{E})\cr
1722
\+& \kbd{s*} & any number of strings, possibly 0 (see \kbd{s})\cr
1723
\+& \kbd{D\var{xxx}} & argument can be omitted and has a default value\cr
1724
1725
The \kbd{E*} code reads all remaining arguments in closure context and passes
1726
them as a single \typ{VEC}.
1727
The \kbd{s*} code reads all remaining arguments in \tev{string context} and
1728
passes the list of strings as a single \typ{VEC}. The automatic concatenation
1729
rules in string context are implemented so that adjacent strings
1730
are read as different arguments, as if they had been comma-separated. For
1731
instance, if the remaining argument sequence is: \kbd{"xx" 1, "yy"}, the
1732
\kbd{s*} atom sends \kbd{[a, b, c]}, where
1733
$a$, $b$, $c$ are \kbd{GEN}s of type \typ{STR} (content \kbd{"xx"}),
1734
\typ{INT} (equal to $1$) and \typ{STR} (content \kbd{"yy"}).
1735
1736
The format to indicate a default value (atom starts with a \kbd{D}) is
1737
``\kbd{D\var{value},\var{type},}'', where \var{type} is the code for any
1738
mandatory atom (previous group), \var{value} is any valid GP expression
1739
which is converted according to \var{type}, and the ending comma is
1740
mandatory. For instance \kbd{D0,L,} stands for ``this optional argument is
1741
converted to a \kbd{long}, and is \kbd{0} by default''. So if the
1742
user-given argument reads \kbd{1 + 3} at this point, \kbd{4L} is sent to
1743
the function; and \kbd{0L} if the argument is omitted. The following
1744
special notations are available:
1745
1746
\settabs\+\indent\indent&\kbd{Dxxx}\quad& optional \kbd{*GEN},&\cr
1747
\+&\kbd{DG}& optional \kbd{GEN}, & send \kbd{NULL} if argument omitted.\cr
1748
1749
\+&\kbd{D\&}& optional \kbd{*GEN}, send \kbd{NULL} if argument omitted.\cr
1750
\+&&\quad The argument must be prefixed by \kbd{\&}.\cr
1751
1752
\+&\kbd{DI}, \kbd{DE}& optional closure, send \kbd{NULL} if argument omitted.\cr
1753
1754
\+&\kbd{DP}& optional \kbd{long}, send \kbd{precdl} if argument omitted.\cr
1755
1756
\+&\kbd{DV}& optional \kbd{*entree}, send \kbd{NULL} if argument omitted.\cr
1757
1758
\+&\kbd{Dn}& optional variable number, $-1$ if omitted.\cr
1759
1760
\+&\kbd{Dr}& optional raw string, send \kbd{NULL} if argument omitted.\cr
1761
1762
\+&\kbd{Ds}& optional \kbd{char *}, send \kbd{NULL} if argument omitted.\cr
1763
1764
\misctitle{Hardcoded limit} C functions using more than 20 arguments are not
1765
supported. Use vectors if you really need that many parameters.
1766
1767
When the function is called under \kbd{gp}, the prototype is scanned and each
1768
time an atom corresponding to a mandatory argument is met, a user-given
1769
argument is read (\kbd{gp} outputs an error message it the argument was
1770
missing). Each time an optional atom is met, a default value is inserted if the
1771
user omits the argument. The ``automatic'' atoms fill in the argument list
1772
transparently, supplying the current value of the corresponding variable (or a
1773
dummy pointer).
1774
1775
For instance, here is how you would code the following prototypes, which
1776
do not involve default values:
1777
\bprog
1778
GEN f(GEN x, GEN y, long prec) ----> "GGp"
1779
void f(GEN x, GEN y, long prec) ----> "vGGp"
1780
void f(GEN x, long y, long prec) ----> "vGLp"
1781
long f(GEN x) ----> "lG"
1782
int f(long x) ----> "iL"
1783
@eprog\noindent
1784
If you want more examples, \kbd{gp} gives you easy access to the parser codes
1785
attached to all GP functions: just type \kbd{\b{h} \var{function}}. You
1786
can then compare with the C prototypes as they stand in \kbd{paridecl.h}.
1787
1788
\misctitle{Remark} If you need to implement complicated control statements
1789
(probably for some improved summation functions), you need to know
1790
how the parser implements closures and lexicals and how the evaluator lets
1791
you deal with them, in particular the \tet{push_lex} and \tet{pop_lex}
1792
functions. Check their descriptions and adapt the source code in
1793
\kbd{language/sumiter.c} and \kbd{language/intnum.c}.
1794
1795
\subsec{Integration with \kbd{gp} as a shared module}
1796
1797
In this section we assume that your Operating System is supported by
1798
\tet{install}. You have written a function in C following the guidelines is
1799
\secref{se:coding_guidelines}; in case the function returns a \kbd{GEN}, it
1800
must satisfy \kbd{gerepileupto} assumptions (see \secref{se:garbage}).
1801
1802
You then succeeded in building it as part of a shared library and want to
1803
finally tell \kbd{gp} about your function. First, find a name for it. It does
1804
not have to match the one used in library mode, but consistency is nice. It
1805
has to be a valid GP identifier, i.e.~use only alphabetic characters, digits
1806
and the underscore character (\kbd{\_}), the first character being
1807
alphabetic.
1808
1809
Then figure out the correct \idx{parser code} corresponding to the function
1810
prototype (as explained in~\secref{se:gp.interface}) and write a GP script
1811
like the following:
1812
\bprog
1813
install(libname, code, gpname, library)
1814
addhelp(gpname, "some help text")
1815
@eprog
1816
\noindent The \idx{addhelp} part is not mandatory, but very useful if you
1817
want others to use your module. \kbd{libname} is how the function is named in
1818
the library, usually the same name as one visible from C.
1819
1820
Read that file from your \kbd{gp} session, for instance from your
1821
\idx{preferences file} (or \kbd{gprc}), and that's it. You
1822
can now use the new function \var{gpname} under \kbd{gp}, and we would very
1823
much like to hear about it!
1824
\smallskip
1825
1826
\misctitle{Example}
1827
A complete description could look like this:
1828
\bprog
1829
{
1830
install(bnfinit0, "GD0,L,DGp", ClassGroupInit, "libpari.so");
1831
addhelp(ClassGroupInit, "ClassGroupInit(P,{flag=0},{data=[]}):
1832
compute the necessary data for ...");
1833
}
1834
@eprog\noindent which means we have a function \kbd{ClassGroupInit} under
1835
\kbd{gp}, which calls the library function \kbd{bnfinit0} . The function has
1836
one mandatory argument, and possibly two more (two \kbd{'D'} in the code),
1837
plus the current real precision. More precisely, the first argument is a
1838
\kbd{GEN}, the second one is converted to a \kbd{long} using \kbd{itos}
1839
(\kbd{0} is passed if it is omitted), and the third one is also a \kbd{GEN},
1840
but we pass \kbd{NULL} if no argument was supplied by the user. This matches
1841
the C prototype (from \kbd{paridecl.h}):
1842
%
1843
\bprog
1844
GEN bnfinit0(GEN P, long flag, GEN data, long prec)
1845
@eprog\noindent
1846
This function is in fact coded in \kbd{basemath/buch2.c}, and is in this case
1847
completely identical to the GP function \kbd{bnfinit} but \kbd{gp} does not
1848
need to know about this, only that it can be found somewhere in the shared
1849
library \kbd{libpari.so}.
1850
1851
\misctitle{Important note} You see in this example that it is the
1852
function's responsibility to correctly interpret its operands: \kbd{data =
1853
NULL} is interpreted \emph{by the function} as an empty vector. Note that
1854
since \kbd{NULL} is never a valid \kbd{GEN} pointer, this trick always
1855
enables you to distinguish between a default value and actual input: the
1856
user could explicitly supply an empty vector!
1857
1858
\subsec{Library interface for \kbd{install}}
1859
1860
There is a corresponding library interface for this \kbd{install}
1861
functionality, letting you expand the GP parser/evaluator available in the
1862
library with new functions from your C source code. Functions such as
1863
\tet{gp_read_str} may then evaluate a GP expression sequence involving calls
1864
to these new function!
1865
1866
\fun{entree *}{install}{void *f, const char *gpname, const char *code}
1867
1868
\noindent where \kbd{f} is the (address of the) function (cast to
1869
\kbd{void*}), \kbd{gpname} is the name by which you want to access your
1870
function from within your GP expressions, and \kbd{code} is as above.
1871
1872
1873
\subsec{Integration by patching \kbd{gp}}
1874
1875
If \tet{install} is not available, and installing Linux or a BSD operating
1876
system is not an option (why?), you have to hardcode your function in the
1877
\kbd{gp} binary. Here is what needs to be done:
1878
1879
\item Fetch the complete sources of the PARI distribution.
1880
1881
\item Drop the function source code module in an appropriate directory
1882
(a priori \kbd{src/modules}), and declare all public functions
1883
in \kbd{src/headers/paridecl.h}.
1884
1885
\item Choose a help section and add a file
1886
\kbd{src/functions/\var{section}/\var{gpname}}
1887
containing the following, keeping the notation above:
1888
\bprog
1889
Function: @com\var{gpname}
1890
Section: @com\var{section}
1891
C-Name: @com\var{libname}
1892
Prototype: @com\var{code}
1893
Help: @com\var{some help text}
1894
@eprog\noindent
1895
(If the help text does not fit on a single line, continuation lines must
1896
start by a whitespace character.) Two GP2C-related fields (\kbd{Description}
1897
and \kbd{Wrapper}) are also available to improve the code GP2C generates when
1898
compiling scripts involving your function. See the GP2C documentation for
1899
details.
1900
1901
\item Launch \kbd{Configure}, which should pick up your C files and build an
1902
appropriate \kbd{Makefile}. At this point you can recompile \kbd{gp}, which
1903
will first rebuild the functions database.
1904
1905
\misctitle{Example} We reuse the \kbd{ClassGroupInit} / \kbd{bnfinit0}
1906
from the preceding section. Since the C source code is already part
1907
of PARI, we only need to add a file
1908
1909
\kbd{functions/number\_fields/ClassGroupInit}
1910
1911
\noindent containing the following:
1912
\bprog
1913
Function: ClassGroupInit
1914
Section: number_fields
1915
C-Name: bnfinit0
1916
Prototype: GD0,L,DGp
1917
Help: ClassGroupInit(P,{flag=0},{tech=[]}): this routine does @com\dots
1918
@eprog\noindent
1919
and recompile \kbd{gp}.
1920
1921
\section{Globals related to PARI configuration}
1922
\subsec{PARI version numbers}
1923
1924
\noindent \tet{paricfg_version_code} encodes in a single \kbd{long}, the Major
1925
and minor version numbers as well as the patchlevel.
1926
1927
\fun{long}{PARI_VERSION}{long M, long m, long p} produces the version code
1928
attached to release $M.m.p$. Each code identifies a unique PARI release,
1929
and corresponds to the natural total order on the set of releases (bigger
1930
code number means more recent release).
1931
1932
\noindent \tet{PARI_VERSION_SHIFT} is the number of bits used to store each of
1933
the integers $M$, $m$, $p$ in the version code.
1934
1935
\noindent \tet{paricfg_vcsversion} is a version string related to the
1936
revision control system used to handle your sources, if any. For instance
1937
\kbd{git-}\emph{commit hash} if compiled from a git repository.
1938
1939
The two character strings \tet{paricfg_version} and \tet{paricfg_buildinfo},
1940
correspond to the first two lines printed by \kbd{gp} just before the
1941
Copyright message. The character string \tet{paricfg_compiledate} is the
1942
date of compilation which appears on the next line. The character string
1943
\tet{paricfg_mt_engine} is the name of the threading engine on the next line.
1944
1945
In the string \kbd{paricfg\_buildinfo}, the substring \kbd{"\%s"} needs
1946
to be substituted by the output of the function \kbd{pari\_kernel\_version}.
1947
1948
\fun{const char *}{pari_kernel_version}{void}
1949
1950
\fun{GEN}{pari_version}{} returns the version number as a PARI object, a
1951
\typ{VEC} with three \typ{INT} and one \typ{STR} components.
1952
1953
\subsec{Miscellaneous}
1954
1955
\tet{paricfg_datadir}: character string. The location of PARI's \tet{datadir}.
1956
1957
\tet{paricfg_gphelp}: character string. The name of an external help command
1958
for \kbd{??} (such as the \kbd{gphelp} script)
1959
1960
\newpage
1961
\chapter{Arithmetic kernel: Level 0 and 1}
1962
1963
\section{Level 0 kernel (operations on ulongs)}
1964
1965
\subsec{Micro-kernel}
1966
The Level 0 kernel simulates basic operations of the 68020 processor on which
1967
PARI was originally implemented. They need ``global'' \kbd{ulong} variables
1968
\kbd{overflow} (which will contain only 0 or 1) and \kbd{hiremainder} to
1969
function properly. A routine using one of these lowest-level functions
1970
where the description mentions either \kbd{hiremainder} or \kbd{overflow}
1971
must declare the corresponding
1972
\bprog
1973
LOCAL_HIREMAINDER; /* provides 'hiremainder' */
1974
LOCAL_OVERFLOW; /* provides 'overflow' */
1975
@eprog\noindent
1976
in a declaration block. Variables \kbd{hiremainder} and \kbd{overflow} then
1977
become available in the enclosing block. For instance a loop over the powers
1978
of an \kbd{ulong}~\kbd{p} protected from overflows could read
1979
\bprog
1980
while (pk < lim)
1981
{
1982
LOCAL_HIREMAINDER;
1983
...
1984
pk = mulll(pk, p); if (hiremainder) break;
1985
}
1986
@eprog\noindent
1987
For most architectures, the functions mentioned below are really chunks of
1988
inlined assembler code, and the above `global' variables are actually
1989
local register values.
1990
1991
\fun{ulong}{addll}{ulong x, ulong y} adds \kbd{x} and \kbd{y}, returns the
1992
lower \B\ bits and puts the carry bit into \kbd{overflow}.
1993
1994
\fun{ulong}{addllx}{ulong x, ulong y} adds \kbd{overflow} to the sum of the
1995
\kbd{x} and \kbd{y}, returns the lower \B\ bits and puts the carry bit into
1996
\kbd{overflow}.
1997
1998
\fun{ulong}{subll}{ulong x, ulong y} subtracts \kbd{x} and \kbd{y}, returns
1999
the lower \B\ bits and put the carry (borrow) bit into \kbd{overflow}.
2000
2001
\fun{ulong}{subllx}{ulong x, ulong y} subtracts \kbd{overflow} from the
2002
difference of \kbd{x} and \kbd{y}, returns the lower \B\ bits and puts the
2003
carry (borrow) bit into \kbd{overflow}.
2004
2005
\fun{int}{bfffo}{ulong x} returns the number of leading zero bits in \kbd{x}.
2006
That is, the number of bit positions by which it would have to be shifted
2007
left until its leftmost bit first becomes equal to~1, which can be between 0
2008
and $\B-1$ for nonzero \kbd{x}. When \kbd{x} is~0, the result is undefined.
2009
2010
\fun{ulong}{mulll}{ulong x, ulong y} multiplies \kbd{x} by \kbd{y}, returns
2011
the lower \B\ bits and stores the high-order \B\ bits into \kbd{hiremainder}.
2012
2013
\fun{ulong}{addmul}{ulong x, ulong y} adds \kbd{hiremainder} to the product
2014
of \kbd{x} and \kbd{y}, returns the lower \B\ bits and stores the high-order
2015
\B\ bits into \kbd{hiremainder}.
2016
2017
\fun{ulong}{divll}{ulong x, ulong y} returns the quotient of
2018
$ \left(\kbd{hiremainder} * 2^{\B}\right) + \kbd{x} $
2019
by \kbd{y} and stores the remainder into \kbd{hiremainder}. An error occurs
2020
if the quotient cannot be represented by an \kbd{ulong}, i.e.~if initially
2021
$\kbd{hiremainder}\ge\kbd{y}$.
2022
2023
\fun{long}{hammingl}{ulong x)} returns the Hamming weight of $x$, i.e.~the
2024
number of nonzero bits in its binary expansion.
2025
2026
\misctitle{Obsolete routines} Those functions are awkward and no longer used;
2027
they are only provided for backward compatibility:
2028
2029
\fun{ulong}{shiftl}{ulong x, ulong y} returns $x$ shifted left by $y$ bits,
2030
i.e.~\kbd{$x$ << $y$}, where we assume that $0\leq y\leq\B$. The global variable
2031
\kbd{hiremainder} receives the bits that were shifted out,
2032
i.e.~\kbd{$x$ >> $(\B - y)$}.
2033
2034
\fun{ulong}{shiftlr}{ulong x, ulong y} returns $x$ shifted right by $y$ bits,
2035
i.e.~\kbd{$x$ >> $y$}, where we assume that $0\leq y\leq\B$. The global variable
2036
\kbd{hiremainder} receives the bits that were shifted out,
2037
i.e.~\kbd{$x$ << $(\B - y)$}.
2038
2039
\subsec{Modular kernel}
2040
The following routines are not part of the level 0 kernel per se, but
2041
implement modular operations on words in terms of the above. They are written
2042
so that no overflow may occur. Let $m \geq 1$ be the modulus; all operands
2043
representing classes modulo $m$ are assumed to belong to $[0,m-1]$. The
2044
result may be wrong for a number of reasons otherwise: it may not be reduced,
2045
overflow can occur, etc.
2046
2047
\fun{int}{odd}{ulong x} returns 1 if $x$ is odd, and 0 otherwise.
2048
2049
\fun{int}{both_odd}{ulong x, ulong y} returns 1 if $x$ and $y$ are both odd,
2050
and 0 otherwise.
2051
2052
\fun{ulong}{invmod2BIL}{ulong x} returns the smallest
2053
positive representative of $x^{-1}$ mod $2^\B$, assuming $x$ is odd.
2054
2055
\fun{ulong}{Fl_add}{ulong x, ulong y, ulong m} returns the smallest
2056
nonnegative representative of $x + y$ modulo $m$.
2057
2058
\fun{ulong}{Fl_neg}{ulong x, ulong m} returns the smallest
2059
nonnegative representative of $-x$ modulo $m$.
2060
2061
\fun{ulong}{Fl_sub}{ulong x, ulong y, ulong m} returns the smallest
2062
nonnegative representative of $x - y$ modulo $m$.
2063
2064
\fun{long}{Fl_center}{ulong x, ulong m, ulong mo2} returns the representative
2065
in $]-m/2,m/2]$ of $x$ modulo $m$. Assume $0 \leq x < m$ and
2066
$\kbd{mo2} = m >> 1$.
2067
2068
\fun{ulong}{Fl_mul}{ulong x, ulong y, ulong m} returns the smallest
2069
nonnegative representative of $x y$ modulo $m$.
2070
2071
\fun{ulong}{Fl_double}{ulong x, ulong m} returns $2x$ modulo $m$.
2072
2073
\fun{ulong}{Fl_triple}{ulong x, ulong m} returns $3x$ modulo $m$.
2074
2075
\fun{ulong}{Fl_halve}{ulong x, ulong m} returns $z$ such that $2\*z = x$ modulo
2076
$m$ assuming such $z$ exists.
2077
2078
\fun{ulong}{Fl_sqr}{ulong x, ulong m} returns the smallest nonnegative
2079
representative of $x^2$ modulo $m$.
2080
2081
\fun{ulong}{Fl_inv}{ulong x, ulong m} returns the smallest
2082
positive representative of $x^{-1}$ modulo $m$. If $x$ is not invertible
2083
mod~$m$, raise an exception.
2084
2085
\fun{ulong}{Fl_invsafe}{ulong x, ulong m} returns the smallest
2086
positive representative of $x^{-1}$ modulo $m$. If $x$ is not invertible
2087
mod~$m$, return $0$ (which is ambiguous if $m=1$).
2088
2089
\fun{ulong}{Fl_invgen}{ulong x, ulong m, ulong *pg} set \kbd{*pg} to
2090
$g = \gcd(x,m)$ and return $u$ in $(\Z/m\Z)^*$ such that $x u = g$ modulo $m$.
2091
We have $g = 1$ if and only if $x$ is invertible, and in this case $u$
2092
is its inverse.
2093
2094
\fun{ulong}{Fl_div}{ulong x, ulong y, ulong m} returns the smallest
2095
nonnegative representative of $x y^{-1}$ modulo $m$. If $y$ is not invertible
2096
mod $m$, raise an exception.
2097
2098
\fun{ulong}{Fl_powu}{ulong x, ulong n, ulong m} returns the smallest
2099
nonnegative representative of $x^n$ modulo $m$.
2100
2101
\fun{GEN}{Fl_powers}{ulong x, long n, ulong p} returns
2102
$[\kbd{x}^0, \dots, \kbd{x}^\kbd{n}]$ modulo $m$, as a \typ{VECSMALL}.
2103
2104
\fun{ulong}{Fl_sqrt}{ulong x, ulong p} returns the square root of \kbd{x}
2105
modulo \kbd{p} (smallest nonnegative representative). Assumes \kbd{p} to be
2106
prime, and \kbd{x} to be a square modulo \kbd{p}.
2107
2108
\fun{ulong}{Fl_sqrtl}{ulong x, ulong l, ulong p} returns a $l$-the root of \kbd{x}
2109
modulo \kbd{p}. Assumes \kbd{p} to be prime and $p \equiv 1 \pmod{l}$, and
2110
\kbd{x} to be a $l$-th power modulo \kbd{p}.
2111
2112
\fun{ulong}{Fl_sqrtn}{ulong a, ulong n, ulong p, ulong *zn}
2113
returns \kbd{ULONG\_MAX} if $a$ is not an $n$-th power residue mod $p$.
2114
Otherwise, returns an $n$-th root of $a$; if \kbd{zn} is not \kbd{NULL}
2115
set it to a primitive $m$-th root of 1, $m = \gcd(p-1,n)$ allowing to compute
2116
all $m$ solutions in $\F_p$ of the equation $x^n = a$.
2117
2118
\fun{ulong}{Fl_log}{ulong a, ulong g, ulong ord, ulong p} Let $g$ such that
2119
$g^{ord} \equiv 1 \pmod{p}$. Return an integer $e$ such that
2120
$a^e \equiv g \pmod{p}$. If $e$ does not exist, the result is undefined.
2121
2122
\fun{ulong}{Fl_order}{ulong a, ulong o, ulong p} returns the order of the
2123
\kbd{Fp} \kbd{a}. It is assumed that \kbd{o} is a multiple of the order of
2124
\kbd{a}, $0$ being allowed (no nontrivial information).
2125
2126
\fun{ulong}{random_Fl}{ulong p} returns a pseudo-random integer uniformly
2127
distributed in $0, 1, \dots p-1$.
2128
2129
\fun{ulong}{nonsquare_Fl}{ulong p} return a quadratic nonresidue modulo $p$,
2130
assuming $p$ is an odd prime. If $p$ is $3$ mod $4$, return $p-1$, else
2131
return the smallest (prime) nonresidue.
2132
2133
\fun{ulong}{pgener_Fl}{ulong p} returns the smallest \idx{primitive root}
2134
modulo \kbd{p}, assuming \kbd{p} is prime.
2135
2136
\fun{ulong}{pgener_Zl}{ulong p} returns the smallest primitive root modulo
2137
$p^k$, $k > 1$, assuming $p$ is an odd prime.
2138
2139
\fun{ulong}{pgener_Fl_local}{ulong p, GEN L}, see \kbd{gener\_Fp\_local},
2140
\kbd{L} is an \kbd{Flv}.
2141
2142
\fun{ulong}{factorial_Fl}{long n, ulong p} return $n!$ mod $p$.
2143
2144
\subsec{Modular kernel with ``precomputed inverse''}
2145
2146
This is based on an algorithm by T. Grandlund and N. M\"{o}ller in
2147
``Improved division by invariant integers''
2148
\url{http://gmplib.org/~tege/division-paper.pdf}.
2149
2150
In the following, we set $B=\B$.
2151
2152
\fun{ulong}{get_Fl_red}{ulong p} returns a pseudo inverse \var{pi} for $p$
2153
2154
\fun{ulong}{divll_pre}{ulong x, ulong p, ulong yi}
2155
as divll, where $yi$ is the pseudo inverse of $y$.
2156
2157
\fun{ulong}{remll_pre}{ulong u1, ulong u0, ulong p, ulong pi} returns
2158
the Euclidean remainder of $u_1\*2^B+u_0$ modulo $p$, assuming $pi$ is the
2159
pseudo inverse of $p$. This function is faster if $u_1 < p$.
2160
2161
\fun{ulong}{remlll_pre}{ulong u2, ulong u1, ulong u0, ulong p, ulong pi}
2162
returns the Euclidean remainder of $u_2\*2^{2\*B}+u_1\*2^{B}+u_0$ modulo $p$,
2163
assuming $pi$ is the pseudo inverse of $p$.
2164
2165
\fun{ulong}{Fl_sqr_pre}{ulong x, ulong p, ulong pi} returns $x^2$ modulo $p$,
2166
assuming $pi$ is the pseudo inverse of $p$.
2167
2168
\fun{ulong}{Fl_mul_pre}{ulong x, ulong y, ulong p, ulong pi} returns $x\*y$
2169
modulo $p$, assuming $pi$ is the pseudo inverse of $p$.
2170
2171
\fun{ulong}{Fl_addmul_pre}{ulong a, ulong b, ulong c, ulong p, ulong pi}
2172
returns $a+b\*c$ modulo $p$, assuming $pi$ is the pseudo inverse of $p$.
2173
2174
\fun{ulong}{Fl_addmulmul_pre}{ulong a,ulong b, ulong c,ulong d, ulong p, ulong pi}
2175
returns $a\*b+c\*d$ modulo $p$, assuming $pi$ is the pseudo inverse of $p$.
2176
2177
\fun{ulong}{Fl_powu_pre}{ulong x, ulong n, ulong p, ulong pi} returns
2178
$x^n$ modulo $p$, assuming $pi$ is the pseudo inverse of $p$.
2179
2180
\fun{GEN}{Fl_powers_pre}{ulong x, long n, ulong p, ulong pi} returns
2181
the vector (\typ{VECSMALL}) $(x^0, \dots, x^n)$, assuming $pi$ is
2182
the pseudo inverse of $p$.
2183
2184
\fun{ulong}{Fl_log_pre}{ulong a, ulong g, ulong ord, ulong p, ulong pi}
2185
as \kbd{Fl\_log}, assuming $pi$ is the pseudo inverse of $p$.
2186
2187
\fun{ulong}{Fl_sqrt_pre}{ulong x, ulong p, ulong pi} returns a square root
2188
of $x$ modulo $p$, assuming $pi$ is the pseudo inverse of $p$.
2189
See \kbd{Fl\_sqrt}.
2190
2191
\fun{ulong}{Fl_sqrtl_pre}{ulong x, ulong l, ulong p, ulong pi}
2192
returns a $l$-the root of \kbd{x}
2193
modulo \kbd{p}, assuming $pi$ is the pseudo inverse of $p$,
2194
$p$ prime and $p \equiv 1 \pmod{l}$, and \kbd{x} to be a $l$-th power modulo
2195
\kbd{p}.
2196
2197
\fun{ulong}{Fl_sqrtn_pre}{ulong x, ulong n, ulong p, ulong *zn}
2198
See \kbd{Fl\_sqrtn}, assuming $pi$ is the pseudo inverse of $p$.
2199
2200
\fun{ulong}{Fl_2gener_pre}{ulong p, ulong pi} return a generator of
2201
the $2$-Sylow subgroup of $\F_p^*$. To use with \kbd{Fl\_sqrt\_pre\_i}.
2202
2203
\fun{ulong}{Fl_sqrt_pre_i}{ulong x, ulong s2, ulong p, ulong pi}
2204
as \kbd{Fl\_sqrt\_pre} where \kbd{s2} is the element returned by
2205
\kbd{Fl\_2gener\_pre}.
2206
2207
\subsec{Switching between Fl\_xxx and standard operators}
2208
2209
Even though the \kbd{Fl\_xxx} routines are efficient, they are slower than
2210
ordinary \kbd{long} operations, using the standard \kbd{+}, \kbd{\%}, etc.
2211
operators.
2212
The following macro is used to choose in a portable way the most efficient
2213
functions for given operands:
2214
2215
\fun{int}{SMALL_ULONG}{ulong p} true if $2p^2 <2^\B$. In that case, it is
2216
possible to use ordinary operators efficiently. If $p < 2^\B$, one
2217
may still use the \kbd{Fl\_xxx} routines. Otherwise, one must use generic
2218
routines. For instance, the scalar product of the \kbd{GEN}s $x$ and $y$ mod
2219
$p$ could be computed as follows.
2220
\bprog
2221
long i, l = lg(x);
2222
if (lgefint(p) > 3)
2223
{ /* arbitrary */
2224
GEN s = gen_0;
2225
for (i = 1; i < l; i++) s = addii(s, mulii(gel(x,i), gel(y,i)));
2226
return modii(s, p).
2227
}
2228
else
2229
{
2230
ulong s = 0, pp = itou(p);
2231
x = ZV_to_Flv(x, pp);
2232
y = ZV_to_Flv(y, pp);
2233
if (SMALL_ULONG(pp))
2234
{ /* very small */
2235
for (i = 1; i < l; i++)
2236
{
2237
s += x[i] * y[i];
2238
if (s & HIGHBIT) s %= pp;
2239
}
2240
s %= pp;
2241
}
2242
else
2243
{ /* small */
2244
for (i = 1; i < l; i++)
2245
s = Fl_add(s, Fl_mul(x[i], y[i], pp), pp);
2246
}
2247
return utoi(s);
2248
}
2249
@eprog\noindent
2250
In effect, we have three versions of the same code: very small, small, and
2251
arbitrary inputs. The very small and arbitrary variants use lazy reduction
2252
and reduce only when it becomes necessary: when overflow might occur (very
2253
small), and at the very end (very small, arbitrary).
2254
2255
\section{Level 1 kernel (operations on longs, integers and reals)}
2256
2257
\misctitle{Note} Some functions consist of an elementary operation,
2258
immediately followed by an assignment statement. They will be introduced as
2259
in the following example:
2260
2261
\fun{GEN}{gadd[z]}{GEN x, GEN y[, GEN z]} followed by the explicit
2262
description of the function
2263
2264
\fun{GEN}{gadd}{GEN x, GEN y}
2265
2266
\noindent which creates its result on the stack, returning a \kbd{GEN} pointer
2267
to it, and the parts in brackets indicate that there exists also a function
2268
2269
\fun{void}{gaddz}{GEN x, GEN y, GEN z}
2270
2271
\noindent which assigns its result to the pre-existing object
2272
\kbd{z}, leaving the stack unchanged. These assignment variants are kept for
2273
backward compatibility but are inefficient: don't use them.
2274
2275
\subsec{Creation}
2276
2277
\fun{GEN}{cgeti}{long n} allocates memory on the PARI stack for a \typ{INT}
2278
of length~\kbd{n}, and initializes its first codeword. Identical to
2279
\kbd{cgetg(n,\typ{INT})}.
2280
2281
\fun{GEN}{cgetipos}{long n} allocates memory on the PARI stack for a
2282
\typ{INT} of length~\kbd{n}, and initializes its two codewords. The sign
2283
of \kbd{n} is set to $1$.
2284
2285
\fun{GEN}{cgetineg}{long n} allocates memory on the PARI stack for a negative
2286
\typ{INT} of length~\kbd{n}, and initializes its two codewords. The sign
2287
of \kbd{n} is set to $-1$.
2288
2289
\fun{GEN}{cgetr}{long n} allocates memory on the PARI stack for a \typ{REAL}
2290
of length~\kbd{n}, and initializes its first codeword. Identical to
2291
\kbd{cgetg(n,\typ{REAL})}.
2292
2293
\fun{GEN}{cgetc}{long n} allocates memory on the PARI stack for a
2294
\typ{COMPLEX}, whose real and imaginary parts are \typ{REAL}s
2295
of length~\kbd{n}.
2296
2297
\fun{GEN}{real_1}{long prec} create a \typ{REAL} equal to $1$ to \kbd{prec}
2298
words of accuracy.
2299
2300
\fun{GEN}{real_1_bit}{long bitprec} create a \typ{REAL} equal to $1$ to
2301
\kbd{bitprec} bits of accuracy.
2302
2303
\fun{GEN}{real_m1}{long prec} create a \typ{REAL} equal to $-1$ to \kbd{prec}
2304
words of accuracy.
2305
2306
\fun{GEN}{real_0_bit}{long bit} create a \typ{REAL} equal to $0$ with
2307
exponent $-\kbd{bit}$.
2308
2309
\fun{GEN}{real_0}{long prec} is a shorthand for
2310
\bprog
2311
real_0_bit( -prec2nbits(prec) )
2312
@eprog
2313
2314
\fun{GEN}{int2n}{long n} creates a \typ{INT} equal to \kbd{1<<n} (i.e
2315
$2^n$ if $n \geq 0$, and $0$ otherwise).
2316
2317
\fun{GEN}{int2u}{ulong n} creates a \typ{INT} equal to $2^n$.
2318
2319
\fun{GEN}{int2um1}{long n} creates a \typ{INT} equal to $2^n - 1$.
2320
2321
\fun{GEN}{real2n}{long n, long prec} create a \typ{REAL} equal to $2^n$
2322
to \kbd{prec} words of accuracy.
2323
2324
\fun{GEN}{real_m2n}{long n, long prec} create a \typ{REAL} equal to $-2^n$
2325
to \kbd{prec} words of accuracy.
2326
2327
\fun{GEN}{strtoi}{char *s} convert the character string \kbd{s} to a
2328
nonnegative \typ{INT}.
2329
Decimal numbers, hexadecimal numbers prefixed by \kbd{0x} and binary numbers prefixed
2330
by \kbd{0b} are allowed. The string \kbd{s} consists exclusively of digits:
2331
no leading sign, no whitespace. Leading zeroes are discarded.
2332
2333
\fun{GEN}{strtor}{char *s, long prec} convert the character string \kbd{s} to
2334
a nonnegative \typ{REAL} of precision \kbd{prec}. The string \kbd{s}
2335
consists exclusively of digits and optional decimal point and exponent
2336
(\kbd{e} or \kbd{E}): no leading sign, no whitespace. Leading zeroes are
2337
discarded.
2338
2339
\subsec{Assignment}
2340
In this section, the \kbd{z} argument in the \kbd{z}-functions must be of type
2341
\typ{INT} or~\typ{REAL}.
2342
2343
\fun{void}{mpaff}{GEN x, GEN z} assigns \kbd{x} into~\kbd{z} (where \kbd{x}
2344
and \kbd{z} are \typ{INT} or \typ{REAL}).
2345
Assumes that $\kbd{lg(z)} > 2$.
2346
2347
\fun{void}{affii}{GEN x, GEN z} assigns the \typ{INT} \kbd{x} into the
2348
\typ{INT}~\kbd{z}.
2349
2350
\fun{void}{affir}{GEN x, GEN z} assigns the \typ{INT} \kbd{x} into the
2351
\typ{REAL}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.
2352
2353
\fun{void}{affiz}{GEN x, GEN z} assigns \typ{INT}~\kbd{x} into \typ{INT} or
2354
\typ{REAL}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.
2355
2356
\fun{void}{affsi}{long s, GEN z} assigns the \kbd{long}~\kbd{s} into the
2357
\typ{INT}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.
2358
2359
\fun{void}{affsr}{long s, GEN z} assigns the \kbd{long}~\kbd{s} into the
2360
\typ{REAL}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.
2361
2362
\fun{void}{affsz}{long s, GEN z} assigns the \kbd{long}~\kbd{s} into the
2363
\typ{INT} or \typ{REAL}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.
2364
2365
\fun{void}{affui}{ulong u, GEN z} assigns the \kbd{ulong}~\kbd{u} into the
2366
\typ{INT}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.
2367
2368
\fun{void}{affur}{ulong u, GEN z} assigns the \kbd{ulong}~\kbd{u} into the
2369
\typ{REAL}~\kbd{z}. Assumes that $\kbd{lg(z)} > 2$.
2370
2371
\fun{void}{affrr}{GEN x, GEN z} assigns the \typ{REAL}~\kbd{x} into the
2372
\typ{REAL}~\kbd{z}.
2373
2374
\fun{void}{affgr}{GEN x, GEN z} assigns the scalar \kbd{x} into the
2375
\typ{REAL}~\kbd{z}, if possible.
2376
2377
\noindent The function \kbd{affrs} and \kbd{affri} do not exist. So don't use
2378
them.
2379
2380
\fun{void}{affrr_fixlg}{GEN y, GEN z} a variant of \kbd{affrr}. First shorten
2381
$z$ so that it is no longer than $y$, then assigns $y$ to $z$. This is used
2382
in the following scenario: room is reserved for the result but, due to
2383
cancellation, fewer words of accuracy are available than had been
2384
anticipated; instead of appending meaningless $0$s to the mantissa, we store
2385
what was actually computed.
2386
2387
Note that shortening $z$ is not quite straightforward, since \kbd{setlg(z, ly)}
2388
would leave garbage on the stack, which \kbd{gerepile} might later inspect.
2389
It is done using
2390
2391
\fun{void}{fixlg}{GEN z, long ly} see \tet{stackdummy} and the examples that
2392
follow.
2393
2394
\subsec{Copy}
2395
2396
\fun{GEN}{icopy}{GEN x} copy relevant words of the \typ{INT}~\kbd{x} on the
2397
stack: the length and effective length of the copy are equal.
2398
2399
\fun{GEN}{rcopy}{GEN x} copy the \typ{REAL}~\kbd{x} on the stack.
2400
2401
\fun{GEN}{leafcopy}{GEN x} copy the leaf~\kbd{x} on the
2402
stack (works in particular for \typ{INT}s and \typ{REAL}s).
2403
Contrary to \kbd{icopy}, \kbd{leafcopy} preserves the original
2404
length of a \typ{INT}. The obsolete form \fun{GEN}{mpcopy}{GEN x}
2405
is still provided for backward compatibility.
2406
2407
This function also works on recursive types, copying them as if they were
2408
leaves, i.e.~making a shallow copy in that case: the components of the copy
2409
point to the same data as the component of the source; see also
2410
\kbd{shallowcopy}.
2411
2412
\fun{GEN}{leafcopy_avma}{GEN x, pari_sp av} analogous to \kbd{gcopy\_avma}
2413
but simpler: assume $x$ is a leaf and return a copy allocated as if
2414
initially we had \kbd{avma} equal to \kbd{av}. There is no need to pass a
2415
pointer and update the value of the second argument: the new (fictitious)
2416
\kbd{avma} is just the return value (typecast to \kbd{pari\_sp}).
2417
2418
\fun{GEN}{icopyspec}{GEN x, long nx} copy the \kbd{nx} words
2419
\kbd{x[2]}, \dots, \kbd{x[nx+1]} to make up a new \typ{INT}. Set the sign
2420
to $1$.
2421
2422
\subsec{Conversions}
2423
2424
\fun{GEN}{itor}{GEN x, long prec} converts the \typ{INT}~\kbd{x} to a
2425
\typ{REAL} of length \kbd{prec} and return the latter.
2426
Assumes that $\kbd{prec} > 2$.
2427
2428
\fun{long}{itos}{GEN x} converts the \typ{INT}~\kbd{x} to a \kbd{long} if
2429
possible, otherwise raise an exception. We consider the conversion
2430
to be possible if and only if $|x| \leq \kbd{LONG\_MAX}$, i.e. $|x| < 2^{63}$
2431
on a 64-bit architecture. Since the range is symetric, the output of
2432
\kbd{itos} can safely be negated.
2433
2434
\fun{long}{itos_or_0}{GEN x} converts the \typ{INT}~\kbd{x} to a \kbd{long} if
2435
possible, otherwise return $0$.
2436
2437
\fun{int}{is_bigint}{GEN n} true if \kbd{itos(n)} would give an error.
2438
2439
\fun{ulong}{itou}{GEN x} converts the \typ{INT}~\kbd{|x|} to an \kbd{ulong} if
2440
possible, otherwise raise an exception. The conversion is possible if
2441
and only if $\kbd{lgefint}(x) \leq 3$.
2442
2443
\fun{long}{itou_or_0}{GEN x} converts the \typ{INT}~\kbd{|x|} to an
2444
\kbd{ulong} if possible, otherwise return $0$.
2445
2446
\fun{GEN}{stoi}{long s} creates the \typ{INT} corresponding to the
2447
\kbd{long}~\kbd{s}.
2448
2449
\fun{GEN}{stor}{long s, long prec} converts the \kbd{long}~\kbd{s} into a
2450
\typ{REAL} of length \kbd{prec} and return the latter. Assumes that
2451
$\kbd{prec} > 2$.
2452
2453
\fun{GEN}{utoi}{ulong s} converts the \kbd{ulong}~\kbd{s} into a \typ{INT}
2454
and return the latter.
2455
2456
\fun{GEN}{utoipos}{ulong s} converts the \emph{nonzero} \kbd{ulong}~\kbd{s}
2457
into a \typ{INT} and return the latter.
2458
2459
\fun{GEN}{utoineg}{ulong s} converts the \emph{nonzero} \kbd{ulong}~\kbd{s}
2460
into the \typ{INT} $-s$ and return the latter.
2461
2462
\fun{GEN}{utor}{ulong s, long prec} converts the \kbd{ulong}~\kbd{s} into a
2463
\typ{REAL} of length \kbd{prec} and return the latter. Assumes that
2464
$\kbd{prec} > 2$.
2465
2466
\fun{GEN}{rtor}{GEN x, long prec} converts the \typ{REAL}~\kbd{x} to a
2467
\typ{REAL} of length \kbd{prec} and return the latter. If
2468
$\kbd{prec} < \kbd{lg(x)}$, round properly. If $\kbd{prec} > \kbd{lg(x)}$,
2469
pad with zeroes. Assumes that $\kbd{prec} > 2$.
2470
2471
\noindent The following function is also available as a special case of
2472
\tet{mkintn}:
2473
2474
\fun{GEN}{uu32toi}{ulong a, ulong b} returns the \kbd{GEN} equal to $2^{32} a +
2475
b$, \emph{assuming} that $a,b < 2^{32}$. This does not depend on
2476
\kbd{sizeof(long)}: the behavior is as above on both $32$ and $64$-bit
2477
machines.
2478
2479
\fun{GEN}{uu32toineg}{ulong a, ulong b} returns the \kbd{GEN} equal to
2480
$- (2^{32} a + b)$, \emph{assuming} that $a,b < 2^{32}$ and that one of $a$
2481
or $b$ is positive. This does not depend on \kbd{sizeof(long)}: the behavior
2482
is as above on both $32$ and $64$-bit machines.
2483
2484
\fun{GEN}{uutoi}{ulong a, ulong b} returns the \kbd{GEN} equal to
2485
$2^{\B} a + b$.
2486
2487
\fun{GEN}{uutoineg}{ulong a, ulong b} returns the \kbd{GEN} equal to
2488
$-(2^{\B} a + b)$.
2489
2490
\subsec{Integer parts}
2491
The following four functions implement the conversion from \typ{REAL} to
2492
\typ{INT} using standard rounding modes. Contrary to usual semantics
2493
(complement the mantissa with an infinite number of 0), they will raise an
2494
error \emph{precision loss in truncation} if the \typ{REAL} represents a
2495
range containing more than one integer.
2496
2497
\fun{GEN}{ceilr}{GEN x} smallest integer larger or equal
2498
to the \typ{REAL}~\kbd{x} (i.e.~the \kbd{ceil} function).
2499
2500
\fun{GEN}{floorr}{GEN x} largest integer smaller or equal to the
2501
\typ{REAL}~\kbd{x} (i.e.~the \kbd{floor} function).
2502
2503
\fun{GEN}{roundr}{GEN x} rounds the \typ{REAL} \kbd{x} to the nearest integer
2504
(towards~$+\infty$ in case of tie).
2505
2506
\fun{GEN}{truncr}{GEN x} truncates the \typ{REAL}~\kbd{x} (not the same as
2507
\kbd{floorr} if \kbd{x} is negative).
2508
2509
The following four function are analogous, but can also treat the trivial
2510
case when the argument is a \typ{INT}:
2511
2512
\fun{GEN}{mpceil}{GEN x}
2513
as \kbd{ceilr} except that \kbd{x} may be a \typ{INT}.
2514
2515
\fun{GEN}{mpfloor}{GEN x}
2516
as \kbd{floorr} except that \kbd{x} may be a \typ{INT}.
2517
2518
\fun{GEN}{mpround}{GEN x}
2519
as \kbd{roundr} except that \kbd{x} may be a \typ{INT}.
2520
2521
\fun{GEN}{mptrunc}{GEN x}
2522
as \kbd{truncr} except that \kbd{x} may be a \typ{INT}.
2523
2524
\fun{GEN}{diviiround}{GEN x, GEN y} if \kbd{x} and \kbd{y} are \typ{INT}s,
2525
returns the quotient $\kbd{x}/\kbd{y}$ of \kbd{x} and~\kbd{y}, rounded to
2526
the nearest integer. If $\kbd{x}/\kbd{y}$ falls exactly halfway between
2527
two consecutive integers, then it is rounded towards~$+\infty$ (as for
2528
\tet{roundr}).
2529
2530
\fun{GEN}{ceil_safe}{GEN x}, \kbd{x} being a real number (not necessarily a
2531
\typ{REAL}) returns the smallest integer which is larger than any possible
2532
incarnation of \kbd{x}. (Recall that a \typ{REAL} represents an interval of
2533
possible values.) Note that \kbd{gceil} raises an exception if the input
2534
accuracy is too low compared to its magnitude.
2535
2536
\fun{GEN}{floor_safe}{GEN x}, \kbd{x} being a real number (not necessarily a
2537
\typ{REAL}) returns the largest integer which is smaller than any possible
2538
incarnation of \kbd{x}. (Recall that a \typ{REAL} represents an interval of
2539
possible values.) Note that \kbd{gfloor} raises an exception if the input
2540
accuracy is too low compared to its magnitude.
2541
2542
\fun{GEN}{trunc_safe}{GEN x}, \kbd{x} being a real number (not necessarily a
2543
\typ{REAL}) returns the integer with the largest absolute value, which is closer
2544
to $0$ than any possible incarnation of \kbd{x}. (Recall that a \typ{REAL}
2545
represents an interval of possible values.)
2546
2547
\fun{GEN}{roundr_safe}{GEN x} rounds the \typ{REAL} \kbd{x} to the nearest
2548
integer (towards~$+\infty$). Complement the mantissa with an infinite number
2549
of $0$ before rounding, hence never raise an exception.
2550
2551
\subsec{$2$-adic valuations and shifts}
2552
2553
\fun{long}{vals}{long s} 2-adic valuation of the \kbd{long}~\kbd{s}. Returns
2554
$-1$ if \kbd{s} is equal to 0.
2555
2556
\fun{long}{vali}{GEN x} 2-adic valuation of the \typ{INT}~\kbd{x}. Returns $-1$
2557
if \kbd{x} is equal to 0.
2558
2559
\fun{GEN}{mpshift}{GEN x, long n} shifts the~\typ{INT} or
2560
\typ{REAL} \kbd{x} by~\kbd{n}. If \kbd{n} is positive, this is a left shift,
2561
i.e.~multiplication by $2^{\kbd{n}}$. If \kbd{n} is negative, it is a right
2562
shift by~$-\kbd{n}$, which amounts to the truncation of the quotient of \kbd{x}
2563
by~$2^{-\kbd{n}}$.
2564
2565
\fun{GEN}{shifti}{GEN x, long n} shifts the \typ{INT}~$x$ by~$n$.
2566
2567
\fun{GEN}{shiftr}{GEN x, long n} shifts the \typ{REAL}~$x$ by~$n$.
2568
2569
\fun{void}{shiftr_inplace}{GEN x, long n} shifts the \typ{REAL}~$x$ by~$n$,
2570
in place.
2571
2572
\fun{GEN}{trunc2nr}{GEN x, long n} given a \typ{REAL} $x$, returns
2573
\kbd{truncr(shiftr(x,n))}, but faster, without leaving garbage on the stack
2574
and never raising a \emph{precision loss in truncation} error.
2575
Called by \tet{gtrunc2n}.
2576
2577
\fun{GEN}{mantissa2nr}{GEN x, long n} given a \typ{REAL} $x$, returns
2578
the mantissa of $x 2^n$ (disregards the exponent of $x$). Equivalent to
2579
\bprog
2580
trunc2nr(x, n-expo(x)+bit_prec(x)-1)
2581
@eprog
2582
2583
\fun{GEN}{mantissa_real}{GEN z, long *e} returns the mantissa $m$ of $z$, and
2584
sets \kbd{*e} to the exponent $\kbd{bit\_accuracy(lg(z))}-1-\kbd{expo}(z)$,
2585
so that $z = m / 2^e$.
2586
2587
\misctitle{Low-level} In the following two functions, $s$(ource) and $t$(arget)
2588
need not be valid \kbd{GEN}s (in practice, they usually point to some part of a
2589
\typ{REAL} mantissa): they are considered as arrays of words representing some
2590
mantissa, and we shift globally $s$ by $n > 0$ bits, storing the result in
2591
$t$. We assume that $m\leq M$ and only access $s[m], s[m+1],\ldots s[M]$
2592
(read) and likewise for $t$ (write); we may have $s = t$ but more general
2593
overlaps are not allowed. The word $f$ is concatenated to $s$ to supply extra
2594
bits.
2595
2596
\fun{void}{shift_left}{GEN t, GEN s, long m, long M, ulong f, ulong n}
2597
shifts the mantissa
2598
$$s[m], s[m+1],\ldots s[M], f$$
2599
left by $n$ bits.
2600
2601
\fun{void}{shift_right}{GEN t, GEN s, long m, long M, ulong f, ulong n}
2602
shifts the mantissa
2603
$$f, s[m], s[m+1],\ldots s[M]$$
2604
right by $n$ bits.
2605
2606
\subsec{From \typ{INT} to bits or digits in base $2^k$ and back}
2607
2608
\fun{GEN}{binary_zv}{GEN x} given a \typ{INT} $x$, return a \typ{VECSMALL} of
2609
bits, from most significant to least significant.
2610
2611
\fun{GEN}{binary_2k}{GEN x, long k} given a \typ{INT} $x$, and
2612
$k > 0$, return a \typ{VEC} of digits of $x$ in base $2^k$, as \typ{INT}s, from
2613
most significant to least significant.
2614
2615
\fun{GEN}{binary_2k_nv}{GEN x, long k} given a \typ{INT} $x$, and $0 < k <
2616
\tet{BITS_IN_LONG}$, return a \typ{VECSMALL} of digits of $x$ in base $2^k$, as
2617
\kbd{ulong}s, from most significant to least significant.
2618
2619
\fun{GEN}{bits_to_int}{GEN x, long l} given a vector $x$ of $l$ bits (as a
2620
\typ{VECSMALL} or even a pointer to a part of a larger vector, so not a
2621
proper \kbd{GEN}), return the integer $\sum_{i = 1}^l x[i] 2^{l-i}$, as a
2622
\typ{INT}.
2623
2624
\fun{ulong}{bits_to_u}{GEN v, long l} same as \tet{bits_to_int}, where
2625
$l < \tet{BITS_IN_LONG}$, so we can return an \kbd{ulong}.
2626
2627
\fun{GEN}{fromdigitsu}{GEN x, GEN B}
2628
given a \typ{VECSMALL} $x$ of length $l$ and a \typ{INT} $B$,
2629
return the integer $\sum_{i = 1}^l x[i] B^{i-1}$, as a \typ{INT},
2630
where the \kbd{x[i]} are seen as unsigned integers.
2631
2632
\fun{GEN}{fromdigits_2k}{GEN x, long k} converse of \tet{binary_2k};
2633
given a \typ{VEC} $x$ of length $l$ and a positive \kbd{long} $k$,
2634
where each $x[i]$ is a \typ{INT} with $0\leq x[i] < 2^k$, return the
2635
integer $\sum_{i = 1}^l x[i] 2^{k(l-i)}$, as a \typ{INT}.
2636
2637
\fun{GEN}{nv_fromdigits_2k}{GEN x, long k} as \tet{fromdigits_2k}, but
2638
with $x$ being a \typ{VECSMALL} and each $x[i]$ being a \kbd{ulong}
2639
with $0\leq x[i] < 2^{\min\{k,\tet{BITS_IN_LONG}\}}$. Here $k$ may be
2640
any positive \kbd{long}, and the $x[i]$ are regarded as $k$-bit
2641
integers by truncating or extending with zeroes.
2642
2643
\subsec{Integer valuation}
2644
For integers $x$ and $p$, such that $x\neq 0$ and $|p| > 1$, we define
2645
$v_p(x)$ to be the largest integer exponent $e$ such that $p^e$ divides $x$.
2646
If $p$ is prime, this is the ordinary valuation of $x$ at $p$.
2647
2648
\fun{long}{Z_pvalrem}{GEN x, GEN p, GEN *r} applied to \typ{INT}s
2649
$\kbd{x}\neq 0$ and~\kbd{p}, $|\kbd{p}| > 1$, returns $e := v_p(x)$
2650
The quotient $\kbd{x}/\kbd{p}^e$ is returned in~\kbd{*r}. If
2651
$|\kbd{p}|$ is a prime, \kbd{*r} is the prime-to-\kbd{p} part of~\kbd{x}.
2652
2653
\fun{long}{Z_pval}{GEN x, GEN p} as \kbd{Z\_pvalrem} but only returns
2654
$v_p(x)$.
2655
2656
\fun{long}{Z_lvalrem}{GEN x, ulong p, GEN *r} as \kbd{Z\_pvalrem},
2657
except that \kbd{p} is an \kbd{ulong} ($\kbd{p} > 1$).
2658
2659
\fun{long}{Z_lvalrem_stop}{GEN *x, ulong p, int *stop} assume $x > 0$;
2660
returns $e := v_p(x)$ and replaces $x$ by $x / p^e$. Set \kbd{stop} to $1$ if
2661
the new value of $x$ is $ < p^2$ (and $0$ otherwise). To be used when trial
2662
dividing $x$ by successive primes: the \kbd{stop} condition is cheaply tested
2663
while testing whether $p$ divides $x$ (is the quotient less than $p$?), and
2664
allows to decide that $n$ is prime if no prime $< p$ divides $n$. Not
2665
memory-clean.
2666
2667
\fun{long}{Z_lval}{GEN x, ulong p} as \kbd{Z\_pval},
2668
except that \kbd{p} is an \kbd{ulong} ($\kbd{p} > 1$).
2669
2670
\fun{long}{u_lvalrem}{ulong x, ulong p, ulong *r} as \kbd{Z\_pvalrem},
2671
except the inputs/outputs are now \kbd{ulong}s.
2672
2673
\fun{long}{u_lvalrem_stop}{ulong *n, ulong p, int *stop} as
2674
\kbd{Z\_pvalrem\_stop}.
2675
2676
\fun{long}{u_pvalrem}{ulong x, GEN p, ulong *r} as \kbd{Z\_pvalrem},
2677
except \kbd{x} and \kbd{r} are now \kbd{ulong}s.
2678
2679
\fun{long}{u_lval}{ulong x, ulong p} as \kbd{Z\_pval},
2680
except the inputs are now \kbd{ulong}s.
2681
2682
\fun{long}{u_pval}{ulong x, GEN p} as \kbd{Z\_pval},
2683
except \kbd{x} is now an \kbd{ulong}.
2684
2685
\fun{long}{z_lval}{long x, ulong p} as \kbd{u\_lval}, for signed \kbd{x}.
2686
2687
\fun{long}{z_lvalrem}{long x, ulong p} as \kbd{u\_lvalrem}, for signed \kbd{x}.
2688
2689
\fun{long}{z_pval}{long x, GEN p} as \kbd{Z\_pval},
2690
except \kbd{x} is now a \kbd{long}.
2691
2692
\fun{long}{z_pvalrem}{long x, GEN p} as \kbd{Z\_pvalrem},
2693
except \kbd{x} is now a \kbd{long}.
2694
2695
\fun{long}{factorial_lval}{ulong n, ulong p} returns $v_p(n!)$, assuming
2696
$p$ is prime.
2697
2698
The following convenience functions generalize \kbd{Z\_pval} and its variants
2699
to ``containers'' (\kbd{ZV} and \kbd{ZX}):
2700
2701
2702
\fun{long}{ZV_pvalrem}{GEN x, GEN p, GEN *r} $x$ being a \kbd{ZV} (a vector
2703
of \typ{INT}s), return the min $v$ of the valuations of its components and
2704
set \kbd{*r} to $x/p^v$. Infinite loop if $x$ is the zero vector.
2705
This function is not stack clean.
2706
2707
\fun{long}{ZV_pval}{GEN x, GEN p} as \kbd{ZV\_pvalrem} but only returns the
2708
``valuation''.
2709
2710
\fun{int}{ZV_Z_dvd}{GEN x, GEN p} returns $1$ if $p$ divides all components
2711
of $x$ and $0$ otherwise. Faster than testing \kbd{ZV\_pval(x,p) >= 1}.
2712
2713
\fun{long}{ZV_lvalrem}{GEN x, ulong p, GEN *px} as \kbd{ZV\_pvalrem},
2714
except that \kbd{p} is an \kbd{ulong} ($\kbd{p} > 1$).
2715
This function is not stack-clean.
2716
2717
\fun{long}{ZV_lval}{GEN x, ulong p} as \kbd{ZV\_pval},
2718
except that \kbd{p} is an \kbd{ulong} ($\kbd{p} > 1$).
2719
2720
2721
\fun{long}{ZX_pvalrem}{GEN x, GEN p, GEN *r} as \kbd{ZV\_pvalrem}, for
2722
a \kbd{ZX} $x$ (a \typ{POL} with \typ{INT} coefficients).
2723
This function is not stack-clean.
2724
2725
\fun{long}{ZX_pval}{GEN x, GEN p} as \kbd{ZV\_pval} for a \kbd{ZX} $x$.
2726
2727
\fun{long}{ZX_lvalrem}{GEN x, ulong p, GEN *px} as \kbd{ZV\_lvalrem},
2728
a \kbd{ZX} $x$.
2729
This function is not stack-clean.
2730
2731
\fun{long}{ZX_lval}{GEN x, ulong p} as \kbd{ZX\_pval},
2732
except that \kbd{p} is an \kbd{ulong} ($\kbd{p} > 1$).
2733
2734
\subsec{Generic unary operators} Let ``\op'' be a unary operation among
2735
2736
\item \key{neg}: negation ($-x$).
2737
2738
\item \key{abs}: absolute value ($|x|$).
2739
2740
\item \key{sqr}: square ($x^2$).
2741
2742
\noindent The names and prototypes of the low-level functions corresponding
2743
to \op\ are as follows. The result is of the same type as~\kbd{x}.
2744
2745
\funno{GEN}{\op i}{GEN x} creates the result of \op\ applied to the
2746
\typ{INT}~\kbd{x}.
2747
2748
\funno{GEN}{\op r}{GEN x} creates the result of \op\ applied to the
2749
\typ{REAL}~\kbd{x}.
2750
2751
\funno{GEN}{mp\op}{GEN x} creates the result of \op\ applied to the
2752
\typ{INT} or \typ{REAL}~\kbd{x}.
2753
2754
\noindent Complete list of available functions:
2755
2756
\fun{GEN}{absi}{GEN x}, \fun{GEN}{absr}{GEN x}, \fun{GEN}{mpabs}{GEN x}
2757
2758
\fun{GEN}{negi}{GEN x}, \fun{GEN}{negr}{GEN x}, \fun{GEN}{mpneg}{GEN x}
2759
2760
\fun{GEN}{sqri}{GEN x}, \fun{GEN}{sqrr}{GEN x}, \fun{GEN}{mpsqr}{GEN x}
2761
2762
\fun{GEN}{absi_shallow}{GEN x} $x$ being a \typ{INT}, returns a shallow copy of
2763
$|x|$, in particular returns $x$ itself when $x \geq 0$, and \kbd{negi($x$)}
2764
otherwise.
2765
2766
\fun{GEN}{mpabs_shallow}{GEN x} $x$ being a \typ{INT} or a \typ{REAL}, returns
2767
a shallow copy of $|x|$, in particular returns $x$ itself when $x \geq 0$, and
2768
\kbd{mpneg($x$)} otherwise.
2769
2770
2771
\noindent Some miscellaneous routines:
2772
2773
\fun{GEN}{sqrs}{long x} returns $x^2$.
2774
2775
\fun{GEN}{sqru}{ulong x} returns $x^2$.
2776
2777
\subsec{Comparison operators}
2778
2779
\fun{int}{cmpss}{long s, long t} compares the \kbd{long}~\kbd{s} to the
2780
\typ{long}~\kbd{t}.
2781
2782
\fun{int}{cmpuu}{ulong u, ulong v} compares the \kbd{ulong}~\kbd{u} to the
2783
\typ{ulong}~\kbd{v}.
2784
2785
\fun{long}{minss}{long x, long y}
2786
2787
\fun{ulong}{minuu}{ulong x, ulong y}
2788
2789
\fun{double}{mindd}{double x, double y} returns the \kbd{min} of $x$ and $y$.
2790
2791
2792
\fun{long}{maxss}{long x, long y}
2793
2794
\fun{ulong}{maxuu}{ulong x, ulong y}
2795
2796
\fun{double}{maxdd}{double x, double y} returns the \kbd{max} of $x$ and $y$.
2797
2798
\smallskip
2799
2800
\fun{int}{mpcmp}{GEN x, GEN y} compares the \typ{INT} or \typ{REAL}~\kbd{x}
2801
to the \typ{INT} or \typ{REAL}~\kbd{y}. The result is the sign of
2802
$\kbd{x}-\kbd{y}$.
2803
2804
\fun{int}{cmpii}{GEN x, GEN y} compares the \typ{INT} \kbd{x} to the
2805
\typ{INT}~\kbd{y}.
2806
2807
\fun{int}{cmpir}{GEN x, GEN y} compares the \typ{INT} \kbd{x} to the
2808
\typ{REAL}~\kbd{y}.
2809
2810
\fun{int}{cmpis}{GEN x, long s} compares the \typ{INT}~\kbd{x} to the
2811
\kbd{long}~\kbd{s}.
2812
2813
\fun{int}{cmpiu}{GEN x, ulong s} compares the \typ{INT}~\kbd{x} to the
2814
\kbd{ulong}~\kbd{s}.
2815
2816
\fun{int}{cmpsi}{long s, GEN x} compares the \kbd{long}~\kbd{s} to the
2817
\typ{INT}~\kbd{x}.
2818
2819
\fun{int}{cmpui}{ulong s, GEN x} compares the \kbd{ulong}~\kbd{s} to the
2820
\typ{INT}~\kbd{x}.
2821
2822
\fun{int}{cmpsr}{long s, GEN x} compares the \kbd{long}~\kbd{s} to the
2823
\typ{REAL}~\kbd{x}.
2824
2825
\fun{int}{cmpri}{GEN x, GEN y} compares the \typ{REAL}~\kbd{x} to the
2826
\typ{INT}~\kbd{y}.
2827
2828
\fun{int}{cmprr}{GEN x, GEN y} compares the \typ{REAL}~\kbd{x} to the
2829
\typ{REAL}~\kbd{y}.
2830
2831
\fun{int}{cmprs}{GEN x, long s} compares the \typ{REAL}~\kbd{x} to the
2832
\kbd{long}~\kbd{s}.
2833
2834
\fun{int}{equalii}{GEN x, GEN y} compares the \typ{INT}s \kbd{x} and~\kbd{y}.
2835
The result is $1$ if $\kbd{x} = \kbd{y}$, $0$ otherwise.
2836
2837
\fun{int}{equalrr}{GEN x, GEN y} compares the \typ{REAL}s \kbd{x} and~\kbd{y}.
2838
The result is $1$ if $\kbd{x} = \kbd{y}$, $0$ otherwise. Equality is decided
2839
according to the following rules: all real zeroes are equal, and
2840
different from a nonzero real; two nonzero reals are equal if all their
2841
digits coincide up to the length of the shortest of the two, and the
2842
remaining words in the mantissa of the longest are all $0$.
2843
2844
\fun{int}{equalis}{GEN x, long s} compare the \typ{INT} \kbd{x} and
2845
the \kbd{long}~\kbd{s}. The result is $1$ if $\kbd{x} = \kbd{y}$, $0$ otherwise.
2846
2847
\fun{int}{equalsi}{long s, GEN x}
2848
2849
\fun{int}{equaliu}{GEN x, ulong s} compare the \typ{INT} \kbd{x} and
2850
the \kbd{ulong}~\kbd{s}. The result is $1$ if $\kbd{x} = \kbd{y}$, $0$
2851
otherwise.
2852
2853
\fun{int}{equalui}{ulong s, GEN x}
2854
2855
The remaining comparison operators disregard the sign of their operands
2856
2857
\fun{int}{absequaliu}{GEN x, ulong u} compare the absolute value of the
2858
\typ{INT} \kbd{x} and the \kbd{ulong}~\kbd{s}. The result is $1$ if
2859
$|\kbd{x}| = \kbd{y}$, $0$ otherwise. This is marginally more efficient
2860
than \kbd{equalis} even when \kbd{x} is known to be nonnegative.
2861
2862
\fun{int}{absequalui}{ulong u, GEN x}
2863
2864
\fun{int}{abscmpiu}{GEN x, ulong u} compare the absolute value of the
2865
\typ{INT} \kbd{x} and the \kbd{ulong}~\kbd{u}.
2866
2867
\fun{int}{abscmpui}{ulong u, GEN x}
2868
2869
2870
\fun{int}{abscmpii}{GEN x, GEN y} compares the \typ{INT}s \kbd{x} and~\kbd{y}.
2871
The result is the sign of $|\kbd{x}| - |\kbd{y}|$.
2872
2873
\fun{int}{absequalii}{GEN x, GEN y} compares the \typ{INT}s \kbd{x}
2874
and~\kbd{y}. The result is $1$ if $|\kbd{x}| = |\kbd{y}|$, $0$ otherwise.
2875
2876
\fun{int}{abscmprr}{GEN x, GEN y} compares the \typ{REAL}s \kbd{x} and~\kbd{y}.
2877
The result is the sign of $|\kbd{x}| - |\kbd{y}|$.
2878
2879
\fun{int}{absrnz_equal2n}{GEN x} tests whether a nonzero \typ{REAL} \kbd{x}
2880
is equal to $\pm 2^e$ for some integer $e$.
2881
2882
\fun{int}{absrnz_equal1}{GEN x} tests whether a nonzero \typ{REAL} \kbd{x}
2883
is equal to $\pm 1$.
2884
2885
\subsec{Generic binary operators}\label{se:genbinop} The operators in this
2886
section have arguments of C-type \kbd{GEN}, \kbd{long}, and \kbd{ulong}, and
2887
only \typ{INT} and \typ{REAL} \kbd{GEN}s are allowed. We say an argument is a
2888
real type if it is a \typ{REAL} \kbd{GEN}, and an integer type otherwise. The
2889
result is always a \typ{REAL} unless both \kbd{x} and \kbd{y} are integer
2890
types.
2891
2892
Let ``\op'' be a binary operation among
2893
2894
\item \key{add}: addition (\kbd{x + y}).
2895
2896
\item \key{sub}: subtraction (\kbd{x - y}).
2897
2898
\item \key{mul}: multiplication (\kbd{x * y}).
2899
2900
\item \key{div}: division (\kbd{x / y}). In the case where \kbd{x} and \kbd{y}
2901
are both integer types, the result is the Euclidean quotient, where the
2902
remainder has the same sign as the dividend~\kbd{x}. It is the ordinary
2903
division otherwise. A division-by-$0$ error occurs if \kbd{y} is equal to
2904
$0$.
2905
2906
The last two generic operations are defined only when arguments have integer
2907
types; and the result is a \typ{INT}:
2908
2909
\item \key{rem}: remainder (``\kbd{x \% y}''). The result is the Euclidean
2910
remainder corresponding to \kbd{div},~i.e. its sign is that of the
2911
dividend~\kbd{x}.
2912
2913
\item \key{mod}: true remainder (\kbd{x \% y}). The result is the true
2914
Euclidean remainder, i.e.~nonnegative and less than the absolute value
2915
of~\kbd{y}.
2916
2917
\misctitle{Important technical note} The rules given above fixing the output
2918
type (to \typ{REAL} unless both inputs are integer types) are subtly
2919
incompatible with the general rules obeyed by PARI's generic functions, such
2920
as \kbd{gmul} or \kbd{gdiv} for instance: the latter return a result
2921
containing as much information as could be deduced from the inputs, so it is
2922
not true that if $x$ is a \typ{INT} and $y$ a \typ{REAL}, then
2923
\kbd{gmul(x,y)} is always the same as \kbd{mulir(x,y)}. The exception
2924
is $x = 0$, in that case we can deduce that the result is an exact $0$,
2925
so \kbd{gmul} returns \kbd{gen\_0}, while \kbd{mulir} returns a
2926
\typ{REAL} $0$. Specifically, the one resulting from the conversion of
2927
\kbd{gen\_0} to a \typ{REAL} of precision \kbd{precision(y)}, multiplied by
2928
$y$; this determines the exponent of the real $0$ we obtain.
2929
2930
The reason for the discrepancy between the two rules is that we use the two
2931
sets of functions in different contexts: generic functions allow to write
2932
high-level code forgetting about types, letting PARI return results which are
2933
sensible and as simple as possible; type specific functions are used in
2934
kernel programming, where we do care about types and need to maintain strict
2935
consistency: it is much easier to compute the types of results when they are
2936
determined from the types of the inputs only (without taking into account
2937
further arithmetic properties, like being nonzero).
2938
\smallskip
2939
2940
The names and prototypes of the low-level functions corresponding
2941
to \op\ are as follows. In this section, the \kbd{z} argument in the
2942
\kbd{z}-functions must be of type \typ{INT} when no \kbd{r} or \kbd{mp}
2943
appears in the argument code (no \typ{REAL} operand is involved, only integer
2944
types), and of type \typ{REAL} otherwise.
2945
2946
\funno{GEN}{mp\op[z]}{GEN x, GEN y[, GEN z]} applies \op\ to
2947
the \typ{INT} or \typ{REAL} \kbd{x} and~\kbd{y}. The function
2948
\kbd{mpdivz} does not exist (its semantic would change drastically
2949
depending on the type of the \kbd{z} argument), and neither do
2950
\kbd{mprem[z]} nor \kbd{mpmod[z]} (specific to integers).
2951
2952
\funno{GEN}{\op si[z]}{long s, GEN x[, GEN z]} applies \op\ to the
2953
\kbd{long}~\kbd{s} and the \typ{INT}~\kbd{x}.
2954
These functions always return the global constant
2955
\kbd{gen\_0} (not a copy) when the sign of the result is $0$.
2956
2957
\funno{GEN}{\op sr[z]}{long s, GEN x[, GEN z]} applies \op\ to the
2958
\kbd{long}~\kbd{s} and the \typ{REAL}~\kbd{x}.
2959
2960
\funno{GEN}{\op ss[z]}{long s, long t[, GEN z]} applies \op\ to the longs
2961
\kbd{s} and~\kbd{t}. These functions always return the global constant
2962
\kbd{gen\_0} (not a copy) when the sign of the result is $0$.
2963
2964
\funno{GEN}{\op ii[z]}{GEN x, GEN y[, GEN z]} applies \op\ to the
2965
\typ{INT}s \kbd{x} and~\kbd{y}. These functions always return the global
2966
constant \kbd{gen\_0} (not a copy) when the sign of the result is $0$.
2967
2968
\funno{GEN}{\op ir[z]}{GEN x, GEN y[, GEN z]} applies \op\ to the
2969
\typ{INT} \kbd{x} and the \typ{REAL}~\kbd{y}.
2970
2971
\funno{GEN}{\op is[z]}{GEN x, long s[, GEN z]} applies \op\ to the
2972
\typ{INT}~\kbd{x} and the \kbd{long}~\kbd{s}. These functions always return
2973
the global constant \kbd{gen\_0} (not a copy) when the sign of the result
2974
is $0$.
2975
2976
\funno{GEN}{\op ri[z]}{GEN x, GEN y[, GEN z]} applies \op\ to the
2977
\typ{REAL}~\kbd{x} and the \typ{INT}~\kbd{y}.
2978
2979
\funno{GEN}{\op rr[z]}{GEN x, GEN y[, GEN z]} applies \op\ to the
2980
\typ{REAL}s~\kbd{x} and~\kbd{y}.
2981
2982
\funno{GEN}{\op rs[z]}{GEN x, long s[, GEN z]} applies \op\ to the
2983
\typ{REAL}~\kbd{x} and the \kbd{long}~\kbd{s}.
2984
2985
\noindent Some miscellaneous routines:
2986
2987
\fun{long}{expu}{ulong x} assuming $x > 0$, returns the binary exponent of
2988
the real number equal to $x$. This is a special case of \kbd{gexpo}.
2989
2990
\fun{GEN}{adduu}{ulong x, ulong y}
2991
2992
\fun{GEN}{addiu}{GEN x, ulong y}
2993
2994
\fun{GEN}{addui}{ulong x, GEN y} adds \kbd{x} and \kbd{y}.
2995
2996
\fun{GEN}{subuu}{ulong x, ulong y}
2997
2998
\fun{GEN}{subiu}{GEN x, ulong y}
2999
3000
\fun{GEN}{subui}{ulong x, GEN y} subtracts \kbd{x} by \kbd{y}.
3001
3002
\fun{GEN}{muluu}{ulong x, ulong y} multiplies \kbd{x} by \kbd{y}.
3003
3004
\fun{ulong}{umuluu_le}{ulong x, ulong y, ulong n} multiplies \kbd{x} by \kbd{y}.
3005
Return $xy$ if $xy \leq n$ and $0$ otherwise (in particular if $xy$ does not
3006
fit in an \kbd{ulong}).
3007
3008
\fun{ulong}{umuluu_or_0}{ulong x, ulong y} multiplies \kbd{x} by \kbd{y}.
3009
Return $0$ if $xy$ does not fit in an \kbd{ulong}.
3010
3011
\fun{GEN}{mului}{ulong x, GEN y} multiplies \kbd{x} by \kbd{y}.
3012
3013
\fun{GEN}{muluui}{ulong x, ulong y, GEN z} return $xyz$.
3014
3015
\fun{GEN}{muliu}{GEN x, ulong y} multiplies \kbd{x} by \kbd{y}.
3016
3017
\fun{void}{addumului}{ulong a, ulong b, GEN x} return $a + b|X|$.
3018
3019
\fun{GEN}{addmuliu}{GEN x, GEN y, ulong u} returns $x +yu$.
3020
3021
\fun{GEN}{addmulii}{GEN x, GEN y, GEN z} returns $x + yz$.
3022
3023
\fun{GEN}{addmulii_inplace}{GEN x, GEN y, GEN z} returns $x + yz$, but
3024
returns $x$ itself and not a copy if $yz = 0$. Not suitable for
3025
\tet{gerepile} or \tet{gerepileupto}.
3026
3027
\fun{GEN}{addmuliu_inplace}{GEN x, GEN y, ulong u} returns $x +yu$, but
3028
returns $x$ itself and not a copy if $yu = 0$. Not suitable for
3029
\tet{gerepile} or \tet{gerepileupto}.
3030
3031
\fun{GEN}{submuliu_inplace}{GEN x, GEN y, ulong u} returns $x- yu$, but
3032
returns $x$ itself and not a copy if $yu = 0$. Not suitable for
3033
\tet{gerepile} or \tet{gerepileupto}.
3034
3035
\fun{GEN}{lincombii}{GEN u, GEN v, GEN x, GEN y} returns $ux + vy$.
3036
3037
\fun{GEN}{mulsubii}{GEN y, GEN z, GEN x} returns $yz - x$.
3038
3039
\fun{GEN}{submulii}{GEN x, GEN y, GEN z} returns $x - yz$.
3040
3041
\fun{GEN}{submuliu}{GEN x, GEN y, ulong u} returns $x -yu$.
3042
3043
\fun{GEN}{mulu_interval}{ulong a, ulong b} returns $a(a+1)\cdots b$, assuming
3044
that $a \leq b$.
3045
3046
\fun{GEN}{mulu_interval_step}{ulong a, ulong b, ulong s} returns the product
3047
of all integers in $[a,b]$ congruent to $a$ modulo $s$. Assume $a\leq b$ and
3048
$s > 0$;
3049
3050
\fun{GEN}{muls_interval}{long a, long b} returns $a(a+1)\cdots b$, assuming
3051
that $a \leq b$.
3052
3053
\fun{GEN}{invr}{GEN x} returns the inverse of the nonzero \typ{REAL}~$x$.
3054
3055
\fun{GEN}{truedivii}{GEN x, GEN y} returns the true Euclidean quotient
3056
(with nonnegative remainder less than $|y|$).
3057
3058
\fun{GEN}{truedivis}{GEN x, long y} returns the true Euclidean quotient
3059
(with nonnegative remainder less than $|y|$).
3060
3061
\fun{GEN}{truedivsi}{long x, GEN y} returns the true Euclidean quotient
3062
(with nonnegative remainder less than $|y|$).
3063
3064
\fun{GEN}{centermodii}{GEN x, GEN y, GEN y2}, given
3065
\typ{INT}s \kbd{x}, \kbd{y}, returns $z$ congruent to \kbd{x} modulo \kbd{y},
3066
such that $-\kbd{y}/2 \leq z < \kbd{y}/2$. The function requires an extra
3067
argument \kbd{y2}, such that \kbd{y2 = shifti(y, -1)}. (In most cases, \kbd{y}
3068
is constant for many reductions and \kbd{y2} need only be computed once.)
3069
3070
\fun{GEN}{remi2n}{GEN x, long n} returns \kbd{x} mod $2^n$.
3071
3072
\fun{GEN}{addii_sign}{GEN x, long sx, GEN y, long sy} add the \typ{INT}s
3073
$x$ and $y$ as if their signs were \kbd{sx} and \kbd{sy}.
3074
3075
\fun{GEN}{addir_sign}{GEN x, long sx, GEN y, long sy}
3076
add the \typ{INT} $x$ and the \typ{REAL} $y$ as if their signs were \kbd{sx}
3077
and \kbd{sy}.
3078
3079
\fun{GEN}{addrr_sign}{GEN x, long sx, GEN y, long sy} add the \typ{REAL}s $x$
3080
and $y$ as if their signs were \kbd{sx} and \kbd{sy}.
3081
3082
\fun{GEN}{addsi_sign}{long x, GEN y, long sy} add $x$ and the \typ{INT} $y$
3083
as if its sign was \kbd{sy}.
3084
3085
\fun{GEN}{addui_sign}{ulong x, GEN y, long sy} add $x$ and the \typ{INT} $y$
3086
as if its sign was \kbd{sy}.
3087
3088
\subsec{Exact division and divisibility}
3089
3090
\fun{GEN}{diviiexact}{GEN x, GEN y} returns the Euclidean quotient
3091
$\kbd{x} / \kbd{y}$, assuming $\kbd{y}$ divides $\kbd{x}$. Uses Jebelean
3092
algorithm (Jebelean-Krandick bidirectional exact division is not
3093
implemented).
3094
3095
\fun{GEN}{diviuexact}{GEN x, ulong y} returns the Euclidean quotient
3096
$\kbd{x} / \kbd{y}$, assuming $\kbd{y}$ divides
3097
$\kbd{x}$ and $\kbd{y}$ is nonzero.
3098
3099
\fun{GEN}{diviuuexact}{GEN x, ulong y, ulong z} returns the Euclidean
3100
quotient $x/(yz)$, assuming $yz$ divides $x$ and $yz \neq 0$.
3101
3102
The following routines return 1 (true) if \kbd{y} divides \kbd{x}, and
3103
0 otherwise. All \kbd{GEN} are assumed to be \typ{INT}s:
3104
3105
\fun{int}{dvdii}{GEN x, GEN y},
3106
\fun{int}{dvdis}{GEN x, long y},
3107
\fun{int}{dvdiu}{GEN x, ulong y},
3108
3109
\fun{int}{dvdsi}{long x, GEN y},
3110
\fun{int}{dvdui}{ulong x, GEN y}.
3111
3112
The following routines return 1 (true) if \kbd{y} divides \kbd{x}, and in
3113
that case assign the quotient to \kbd{z}; otherwise they return 0. All
3114
\kbd{GEN} are assumed to be \typ{INT}s:
3115
3116
\fun{int}{dvdiiz}{GEN x, GEN y, GEN z},
3117
\fun{int}{dvdisz}{GEN x, long y, GEN z}.
3118
3119
\fun{int}{dvdiuz}{GEN x, ulong y, GEN z} if \kbd{y} divides \kbd{x}, assigns
3120
the quotient $|\kbd{x}|/\kbd{y}$ to \kbd{z} and returns 1 (true), otherwise
3121
returns 0 (false).
3122
3123
\subsec{Division with integral operands and \typ{REAL} result}
3124
3125
\fun{GEN}{rdivii}{GEN x, GEN y, long prec}, assuming $x$ and $y$
3126
are both of type \typ{INT}, return the quotient $x/y$ as a \typ{REAL} of
3127
precision \kbd{prec}.
3128
3129
\fun{GEN}{rdiviiz}{GEN x, GEN y, GEN z}, assuming $x$ and $y$
3130
are both of type \typ{INT}, and $z$ is a \typ{REAL},
3131
assign the quotient $x/y$ to $z$.
3132
3133
\fun{GEN}{rdivis}{GEN x, long y, long prec}, assuming \kbd{x}
3134
is of type \typ{INT}, return the quotient x/y as a \typ{REAL} of
3135
precision \kbd{prec}.
3136
3137
\fun{GEN}{rdivsi}{long x, GEN y, long prec}, assuming \kbd{y}
3138
is of type \typ{INT}, return the quotient x/y as a \typ{REAL} of
3139
precision \kbd{prec}.
3140
3141
\fun{GEN}{rdivss}{long x, long y, long prec}, return the quotient x/y as a
3142
\typ{REAL} of precision \kbd{prec}.
3143
3144
3145
\subsec{Division with remainder} The following functions return two objects,
3146
unless specifically asked for only one of them~--- a quotient and a remainder.
3147
The quotient is returned and the remainder is returned through the variable
3148
whose address is passed as the \kbd{r} argument. The term \emph{true
3149
Euclidean remainder} refers to the nonnegative one (\kbd{mod}), and
3150
\emph{Euclidean remainder} by itself to the one with the same sign as the
3151
dividend (\kbd{rem}). All \kbd{GEN}s, whether returned directly or through a
3152
pointer, are created on the stack.
3153
3154
\fun{GEN}{dvmdii}{GEN x, GEN y, GEN *r} returns the Euclidean quotient of the
3155
\typ{INT}~\kbd{x} by a \typ{INT}~\kbd{y} and puts the remainder
3156
into~\kbd{*r}. If \kbd{r} is equal to \kbd{NULL}, the remainder is not
3157
created, and if \kbd{r} is equal to \kbd{ONLY\_REM}, only the remainder is
3158
created and returned. In the generic case, the remainder is created after the
3159
quotient and can be disposed of individually with a \kbd{cgiv(r)}. The
3160
remainder is always of the sign of the dividend~\kbd{x}. If the remainder
3161
is $0$ set \kbd{r = gen\_0}.
3162
3163
\fun{void}{dvmdiiz}{GEN x, GEN y, GEN z, GEN t} assigns the Euclidean
3164
quotient of the \typ{INT}s \kbd{x} and \kbd{y} into the \typ{INT}~\kbd{z},
3165
and the Euclidean remainder into the \typ{INT}~\kbd{t}.
3166
3167
\noindent Analogous routines \tet{dvmdis}\kbd{[z]}, \tet{dvmdsi}\kbd{[z]},
3168
\tet{dvmdss}\kbd{[z]} are available, where \kbd{s} denotes a \kbd{long}
3169
argument. But the following routines are in general more flexible:
3170
3171
\fun{long}{sdivss_rem}{long s, long t, long *r} computes the Euclidean
3172
quotient and remainder of the longs \kbd{s} and~\kbd{t}. Puts the remainder
3173
into \kbd{*r}, and returns the quotient. The remainder is of the sign of the
3174
dividend~\kbd{s}, and has strictly smaller absolute value than~\kbd{t}.
3175
3176
\fun{long}{sdivsi_rem}{long s, GEN x, long *r} computes the Euclidean
3177
quotient and remainder of the \kbd{long}~\kbd{s} by the \typ{INT}~\kbd{x}. As
3178
\kbd{sdivss\_rem} otherwise.
3179
3180
\fun{long}{sdivsi}{long s, GEN x} as \kbd{sdivsi\_rem}, without
3181
remainder.
3182
3183
\fun{GEN}{divis_rem}{GEN x, long s, long *r} computes the Euclidean quotient
3184
and remainder of the \typ{INT}~\kbd{x} by the \kbd{long}~\kbd{s}. As
3185
\kbd{sdivss\_rem} otherwise.
3186
3187
\fun{GEN}{absdiviu_rem}{GEN x, ulong s, ulong *r} computes the Euclidean quotient
3188
and remainder of \emph{absolute value} of the \typ{INT}~\kbd{x} by the
3189
\kbd{ulong}~\kbd{s}. As \kbd{sdivss\_rem} otherwise.
3190
3191
\fun{ulong}{uabsdiviu_rem}{GEN n, ulong d, ulong *r} as \tet{absdiviu_rem}, assuming
3192
that $|n|/d$ fits into an \kbd{ulong}.
3193
3194
\fun{ulong}{uabsdivui_rem}{ulong x, GEN y, ulong *rem}
3195
computes the Euclidean quotient and remainder of $x$ by $|y|$. As
3196
\kbd{sdivss\_rem} otherwise.
3197
3198
\fun{ulong}{udivuu_rem}{ulong x, ulong y, ulong *rem}
3199
computes the Euclidean quotient and remainder of $x$ by $y$. As
3200
\kbd{sdivss\_rem} otherwise.
3201
3202
\fun{ulong}{ceildivuu}{ulong x, ulong y} return the ceiling of $x / y$.
3203
3204
\fun{GEN}{divsi_rem}{long s, GEN y, long *r} computes the Euclidean quotient
3205
and remainder of the \kbd{long}~\kbd{s} by the \kbd{GEN}~\kbd{y}. As
3206
\kbd{sdivss\_rem} otherwise.
3207
3208
\fun{GEN}{divss_rem}{long x, long y, long *r} computes the Euclidean quotient
3209
and remainder of the \kbd{long}~\kbd{x} by the \kbd{long}~\kbd{y}. As
3210
\kbd{sdivss\_rem} otherwise.
3211
\smallskip
3212
\fun{GEN}{truedvmdii}{GEN x, GEN y, GEN *r}, as \kbd{dvmdii} but with a
3213
nonnegative remainder.
3214
3215
\fun{GEN}{truedvmdis}{GEN x, long y, GEN *z}, as \kbd{dvmdis} but with a
3216
nonnegative remainder.
3217
3218
\fun{GEN}{truedvmdsi}{long x, GEN y, GEN *z}, as \kbd{dvmdsi} but with a
3219
nonnegative remainder.
3220
3221
\subsec{Modulo to longs} The following variants of \kbd{modii} do not
3222
clutter the stack:
3223
3224
\fun{long}{smodis}{GEN x, long y} computes the true Euclidean
3225
remainder of the \typ{INT}~\kbd{x} by the \kbd{long}~\kbd{y}. This is the
3226
nonnegative remainder, not the one whose sign is the sign of \kbd{x}
3227
as in the \kbd{div} functions.
3228
3229
\fun{long}{smodss}{long x, long y} computes the true Euclidean
3230
remainder of the \kbd{long}~\kbd{x} by a \kbd{long}~\kbd{y}.
3231
3232
\fun{ulong}{umodsu}{long x, ulong y} computes the true Euclidean
3233
remainder of the \kbd{long}~\kbd{x} by a \kbd{ulong}~\kbd{y}.
3234
3235
\fun{ulong}{umodiu}{GEN x, ulong y} computes the true Euclidean
3236
remainder of the \typ{INT}~\kbd{x} by the \kbd{ulong}~\kbd{y}.
3237
3238
\fun{ulong}{umodui}{ulong x, GEN y} computes the true Euclidean
3239
remainder of the \kbd{ulong}~\kbd{x} by the \typ{INT}~\kbd{|y|}.
3240
3241
The routine \tet{smodsi} does not exist, since it would not always be
3242
defined: for a \emph{negative} \kbd{x}, if the quotient is $\pm1$, the result
3243
\kbd{x + |y|} would in general not fit into a \kbd{long}. Use either
3244
\kbd{umodui} or \kbd{modsi}.
3245
3246
These functions directly access the binary data and are thus much faster than
3247
the generic modulo functions:
3248
3249
\fun{int}{mpodd}{GEN x} which is 1 if \kbd{x} is odd, and 0 otherwise.
3250
3251
\fun{ulong}{Mod2}{GEN x}
3252
3253
\fun{ulong}{Mod4}{GEN x}
3254
3255
\fun{ulong}{Mod8}{GEN x}
3256
3257
\fun{ulong}{Mod16}{GEN x}
3258
3259
\fun{ulong}{Mod32}{GEN x}
3260
3261
\fun{ulong}{Mod64}{GEN x} give the residue class of $x$ modulo the
3262
corresponding power of $2$.
3263
3264
\fun{ulong}{umodi2n}{GEN x, long n} give the residue class of $x$ modulo
3265
$2^n$, $0 \leq n < BITS\_IN\_LONG$.
3266
3267
The following functions assume that $x\neq 0$ and in fact disregard the
3268
sign of $x$. There are about $10\%$ faster than the safer variants above:
3269
3270
\fun{long}{mod2}{GEN x}
3271
3272
\fun{long}{mod4}{GEN x}
3273
3274
\fun{long}{mod8}{GEN x}
3275
3276
\fun{long}{mod16}{GEN x}
3277
3278
\fun{long}{mod32}{GEN x}
3279
3280
\fun{long}{mod64}{GEN x} give the residue class of $|x|$ modulo the
3281
corresponding power of 2, for \emph{nonzero}~\kbd{x}. As well,
3282
3283
\fun{ulong}{mod2BIL}{GEN x} returns the least significant word of $|x|$, still
3284
assuming that $x\neq 0$.
3285
3286
\subsec{Powering, Square root}
3287
3288
\fun{GEN}{powii}{GEN x, GEN n}, assumes $x$ and $n$ are \typ{INT}s and
3289
returns $x^n$.
3290
3291
\fun{GEN}{powuu}{ulong x, ulong n}, returns $x^n$.
3292
3293
\fun{GEN}{powiu}{GEN x, ulong n}, assumes $x$ is a \typ{INT} and returns $x^n$.
3294
3295
\fun{GEN}{powis}{GEN x, long n}, assumes $x$ is a \typ{INT} and returns $x^n$
3296
(possibly a \typ{FRAC} if $n < 0$).
3297
3298
\fun{GEN}{powrs}{GEN x, long n}, assumes $x$ is a \typ{REAL} and returns
3299
$x^n$. This is considered as a sequence of \kbd{mulrr}, possibly empty:
3300
as such the result has type \typ{REAL}, even if $n = 0$.
3301
Note that the generic function \kbd{gpowgs(x,0)} would return \kbd{gen\_1},
3302
see the technical note in \secref{se:genbinop}.
3303
3304
\fun{GEN}{powru}{GEN x, ulong n}, assumes $x$ is a \typ{REAL} and returns $x^n$
3305
(always a \typ{REAL}, even if $n = 0$).
3306
3307
\fun{GEN}{powersr}{GEN e, long n}. Given a \typ{REAL} $e$, return the vector
3308
$v$ of all $e^i$, $0 \leq i \leq n$, where $v[i] = e^{i-1}$.
3309
3310
\fun{GEN}{powrshalf}{GEN x, long n}, assumes $x$ is a \typ{REAL} and returns
3311
$x^{n/2}$ (always a \typ{REAL}, even if $n = 0$).
3312
3313
\fun{GEN}{powruhalf}{GEN x, ulong n}, assumes $x$ is a \typ{REAL} and returns
3314
$x^{n/2}$ (always a \typ{REAL}, even if $n = 0$).
3315
3316
\fun{GEN}{powrfrac}{GEN x, long n, long d}, assumes $x$ is a \typ{REAL} and
3317
returns $x^{n/d}$ (always a \typ{REAL}, even if $n = 0$).
3318
3319
\fun{GEN}{powIs}{long n} returns $I^n\in\{1,I,-1,-I\}$ (\typ{INT} for even $n$,
3320
\typ{COMPLEX} otherwise).
3321
3322
\fun{ulong}{upowuu}{ulong x, ulong n}, returns $x^n$ when $< 2^\B$, and $0$
3323
otherwise (overflow).
3324
3325
\fun{ulong}{upowers}{ulong x, long n}, returns $[1,x,\ldots,x^n]$ as a
3326
\typ{VECSMALL}. Assume there is no overflow.
3327
3328
\fun{GEN}{sqrtremi}{GEN N, GEN *r}, returns the integer square root $S$ of
3329
the nonnegative \typ{INT}~\kbd{N} (rounded towards 0) and puts the remainder
3330
$R$ into~\kbd{*r}. Precisely, $N = S^2 + R$ with $0\leq R \leq 2S$. If
3331
\kbd{r} is equal to \kbd{NULL}, the remainder is not created. In the generic
3332
case, the remainder is created after the quotient and can be disposed of
3333
individually with \kbd{cgiv(R)}. If the remainder is $0$ set \kbd{R = gen\_0}.
3334
3335
Uses a divide and conquer algorithm (discrete variant of Newton iteration)
3336
due to Paul Zimmermann (``Karatsuba Square Root'', INRIA Research Report 3805
3337
(1999)).
3338
3339
\fun{GEN}{sqrti}{GEN N}, returns the integer square root $S$ of
3340
the nonnegative \typ{INT}~\kbd{N} (rounded towards 0). This is identical
3341
to \kbd{sqrtremi(N, NULL)}.
3342
3343
\fun{long}{logintall}{GEN B, GEN y, GEN *ptq}
3344
returns the floor $e$ of $\log_y B$, where $B > 0$ and $y > 1$ are integers.
3345
If \kbd{ptq} is not \kbd{NULL}, set it to $y^e$. (Analogous to \kbd{logint0},
3346
whithout sanity checks.)
3347
3348
\fun{ulong}{ulogintall}{ulong B, ulong y, ulong *ptq} as \kbd{logintall} for
3349
\kbd{ulong} arguments.
3350
3351
\fun{long}{logint}{GEN B, GEN y} returns the floor $e$ of $\log_y B$, where
3352
$B > 0$ and $y > 1$ are integers.
3353
3354
\fun{ulong}{ulogint}{ulong B, ulong y} as \kbd{logint} for
3355
\kbd{ulong} arguments.
3356
3357
\fun{GEN}{vecpowuu}{long N, ulong a} return the vector of $n^a$, $n = 1,
3358
\dots, N$. Not memory clean.
3359
3360
\fun{GEN}{vecpowug}{long N, GEN a, long prec} return the vector of $n^a$, $n
3361
= 1, \dots, N$, where the powers are computed at precision \kbd{prec}. Not
3362
memory clean.
3363
3364
\subsec{GCD, extended GCD and LCM}
3365
3366
\fun{long}{cgcd}{long x, long y} returns the GCD of \kbd{x} and \kbd{y}.
3367
3368
\fun{ulong}{ugcd}{ulong x, ulong y} returns the GCD of \kbd{x} and \kbd{y}.
3369
3370
\fun{ulong}{ugcdiu}{GEN x, ulong y} returns the GCD of \kbd{x} and \kbd{y}.
3371
3372
\fun{ulong}{ugcdui}{ulong x, GEN y} returns the GCD of \kbd{x} and \kbd{y}.
3373
3374
\fun{GEN}{coprimes_zv}{ulong N} return a \typ{VECSMALL} $T$ with $N$ entries
3375
such that $T[i] = 1$ iff $(i,N) = 1$ and $0$ otherwise.
3376
3377
\fun{long}{clcm}{long x, long y} returns the LCM of \kbd{x} and \kbd{y},
3378
provided it fits into a \kbd{long}. Silently overflows otherwise.
3379
3380
\fun{ulong}{ulcm}{ulong x, ulong y} returns the LCM of \kbd{x} and \kbd{y},
3381
provided it fits into an \kbd{ulong}. Silently overflows otherwise.
3382
3383
\fun{GEN}{gcdii}{GEN x, GEN y}, returns the GCD of the \typ{INT}s \kbd{x} and
3384
\kbd{y}.
3385
3386
\fun{GEN}{lcmii}{GEN x, GEN y}, returns the LCM of the \typ{INT}s \kbd{x} and
3387
\kbd{y}.
3388
3389
\fun{GEN}{bezout}{GEN a,GEN b, GEN *u,GEN *v}, returns the GCD $d$ of
3390
\typ{INT}s \kbd{a} and \kbd{b} and sets \kbd{u}, \kbd{v} to the Bezout
3391
coefficients such that $\kbd{au} + \kbd{bv} = d$.
3392
3393
\fun{long}{cbezout}{long a,long b, long *u,long *v}, returns the GCD
3394
$d$ of \kbd{a} and \kbd{b} and sets \kbd{u}, \kbd{v} to the Bezout coefficients
3395
such that $\kbd{au} + \kbd{bv} = d$.
3396
3397
\fun{GEN}{halfgcdii}{GEN x, GEN y}
3398
assuming \kbd{x} and \kbd{y} are \typ{INT}s,
3399
returns a $2$-components \typ{VEC} $[M,V]$ where $M$ is a $2\times 2$
3400
\typ{MAT} and $V$ a $2$-component \typ{COL}, both with \typ{INT} entries,
3401
such that $M*[x,y]~==V$ and such that if $V=[a,b]~$, then
3402
$a \geq \ceil{\sqrt{\max(|x|,|y|)}} > b$.
3403
3404
\fun{GEN}{ZV_extgcd}{GEN A} given a vector of $n$ integers $A$, returns $[d,
3405
U]$, where $d$ is the GCD of the $A[i]$ and $U$ is a matrix
3406
in $\text{GL}_n(\Z)$ such that $AU = [0,\dots,0,D]$.
3407
3408
\fun{GEN}{ZV_lcm}{GEN v} given a vector $v$ of integers
3409
returns the LCM of its entries.
3410
3411
\fun{GEN}{ZV_snf_gcd}{GEN v, GEN N} given a vector $v$ of integers and a
3412
positive integer $N$, return the vector whose entries are the gcds
3413
$(v[i],N)$. Use case: if $v$ gives the cyclic components for some Abelian
3414
group $G$ of finite type, then this returns the structure of the finite
3415
groupe $G/G^N$.
3416
3417
\subsec{Continued fractions and convergents}
3418
3419
\fun{GEN}{ZV_allpnqn}{GEN x} given $x = [a_0, ..., a_n]$ a
3420
continued fraction from \tet{gboundcf}, $n\geq0$, return all
3421
convergents as $[P,Q]$, where $P = [p_0,\dots,p_n]$ and $Q =
3422
[q_0,\dots,q_n]$.
3423
3424
\subsec{Pseudo-random integers}
3425
These routine return pseudo-random integers uniformly distributed in some
3426
interval. The all use the same underlying generator which can be seeded and
3427
restarted using \tet{getrand} and \tet{setrand}.
3428
3429
\fun{void}{setrand}{GEN seed} reseeds the random number generator using the
3430
seed $n$. The seed is either a technical array output by \kbd{getrand}
3431
or a small positive integer, used to generate deterministically a suitable
3432
state array. For instance, running a randomized computation starting by
3433
\kbd{setrand(1)} twice will generate the exact same output.
3434
3435
\fun{GEN}{getrand}{void} returns the current value of the seed used by the
3436
pseudo-random number generator \tet{random}. Useful mainly for debugging
3437
purposes, to reproduce a specific chain of computations. The returned value
3438
is technical (reproduces an internal state array of type \typ{VECSMALL}),
3439
and can only be used as an argument to \tet{setrand}.
3440
3441
\fun{ulong}{pari_rand}{void} returns a random $0 \leq x < 2^\B$.
3442
3443
\fun{long}{random_bits}{long k} returns a random $0 \leq x < 2^k$. Assumes
3444
that $0 \leq k \leq \B$.
3445
3446
\fun{ulong}{random_Fl}{ulong p} returns a pseudo-random integer
3447
in $0, 1, \dots p-1$.
3448
3449
\fun{GEN}{randomi}{GEN n} returns a random \typ{INT} between $0$ and $\kbd{n}
3450
- 1$.
3451
3452
\fun{GEN}{randomr}{long prec} returns a random \typ{REAL} in $[0,1[$, with
3453
precision \kbd{prec}.
3454
3455
\subsec{Modular operations} In this subsection, all \kbd{GEN}s are
3456
\typ{INT}.
3457
3458
\fun{GEN}{Fp_red}{GEN a, GEN m} returns \kbd{a} modulo \kbd{m} (smallest
3459
nonnegative residue). (This is identical to modii).
3460
3461
\fun{GEN}{Fp_neg}{GEN a, GEN m} returns $-$\kbd{a} modulo \kbd{m} (smallest
3462
nonnegative residue).
3463
3464
\fun{GEN}{Fp_add}{GEN a, GEN b, GEN m} returns the sum of \kbd{a} and
3465
\kbd{b} modulo \kbd{m} (smallest nonnegative residue).
3466
3467
\fun{GEN}{Fp_sub}{GEN a, GEN b, GEN m} returns the difference of \kbd{a} and
3468
\kbd{b} modulo \kbd{m} (smallest nonnegative residue).
3469
3470
\fun{GEN}{Fp_center}{GEN a, GEN p, GEN pov2} assuming that \kbd{pov2} is
3471
\kbd{shifti(p,-1)} and that $-p/2 < a < p$, returns the representative of
3472
\kbd{a} in the symmetric residue system $]-p/2,p/2]$.
3473
3474
\fun{GEN}{Fp_center_i}{GEN a, GEN p, GEN pov2} internal variant of
3475
\tet{Fp_center}, not \kbd{gerepile}-safe: when $a$ is already in the
3476
proper interval, it is returned as is, without a copy.
3477
3478
\fun{GEN}{Fp_mul}{GEN a, GEN b, GEN m} returns the product of \kbd{a} by
3479
\kbd{b} modulo \kbd{m} (smallest nonnegative residue).
3480
3481
\fun{GEN}{Fp_addmul}{GEN x, GEN y, GEN z, GEN p} returns $x + y\*z$.
3482
3483
\fun{GEN}{Fp_mulu}{GEN a, ulong b, GEN m} returns the product of \kbd{a} by
3484
\kbd{b} modulo \kbd{m} (smallest nonnegative residue).
3485
3486
\fun{GEN}{Fp_muls}{GEN a, long b, GEN m} returns the product of \kbd{a} by
3487
\kbd{b} modulo \kbd{m} (smallest nonnegative residue).
3488
3489
\fun{GEN}{Fp_halve}{GEN x, GEN m} returns $z$ such that $2\*z = x$ modulo
3490
$m$ assuming such $z$ exists.
3491
3492
\fun{GEN}{Fp_sqr}{GEN a, GEN m} returns $\kbd{a}^2$ modulo \kbd{m} (smallest
3493
nonnegative residue).
3494
3495
\fun{ulong}{Fp_powu}{GEN x, ulong n, GEN m} raises \kbd{x} to the \kbd{n}-th
3496
power modulo \kbd{m} (smallest nonnegative residue). Not memory-clean, but
3497
suitable for \kbd{gerepileupto}.
3498
3499
\fun{ulong}{Fp_pows}{GEN x, long n, GEN m} raises \kbd{x} to the \kbd{n}-th
3500
power modulo \kbd{m} (smallest nonnegative residue). A negative \kbd{n} is
3501
allowed Not memory-clean, but suitable for \kbd{gerepileupto}.
3502
3503
\fun{GEN}{Fp_pow}{GEN x, GEN n, GEN m} returns $\kbd{x}^\kbd{n}$
3504
modulo \kbd{m} (smallest nonnegative residue).
3505
3506
\fun{GEN}{Fp_pow_init}{GEN x, GEN n, long k, GEN p}
3507
Return a table \kbd{R} that can be used with
3508
\kbd{Fp\_pow\_table} to compute the powers of $x$ up to $n$.
3509
The table is of size $2^k\*\log_2(n)$.
3510
3511
\fun{GEN}{Fp_pow_table}{GEN R, GEN n, GEN p}
3512
return $x^n$, where $R$ is given by \kbd{Fp\_pow\_init(x,m,k,p)}
3513
for some integer $m\geq n$.
3514
3515
\fun{GEN}{Fp_powers}{GEN x, long n, GEN m} returns
3516
$[\kbd{x}^0, \dots, \kbd{x}^\kbd{n}]$ modulo \kbd{m} as a \typ{VEC}
3517
(smallest nonnegative residue).
3518
3519
\fun{GEN}{Fp_inv}{GEN a, GEN m} returns an inverse of \kbd{a} modulo \kbd{m}
3520
(smallest nonnegative residue). Raise an error if \kbd{a} is not invertible.
3521
3522
\fun{GEN}{Fp_invsafe}{GEN a, GEN m} as \kbd{Fp\_inv}, but return
3523
\kbd{NULL} if \kbd{a} is not invertible.
3524
3525
\fun{GEN}{Fp_invgen}{GEN x, GEN m, GEN *pg} set \kbd{*pg} to
3526
$g = \gcd(x,m)$ and return $u$ in $(\Z/m\Z)^*$ such that $x u = g$ modulo $m$.
3527
We have $g = 1$ if and only if $x$ is invertible, and in this case $u$
3528
is its inverse.
3529
3530
\fun{GEN}{FpV_prod}{GEN x, GEN p} returns the product of the components of
3531
$x$.
3532
3533
\fun{GEN}{FpV_inv}{GEN x, GEN m} $x$ being a vector of \typ{INT}s, return
3534
the vector of inverses of the $x[i]$ mod $m$. The routine uses Montgomery's
3535
trick, and involves a single inversion mod $m$, plus $3(N-1)$ multiplications
3536
for $N$ entries. The routine is not stack-clean: $2N$ integers mod $m$
3537
are left on stack, besides the $N$ in the result.
3538
3539
\fun{GEN}{Fp_div}{GEN a, GEN b, GEN m} returns the quotient of \kbd{a} by
3540
\kbd{b} modulo \kbd{m} (smallest nonnegative residue). Raise an error if
3541
\kbd{b} is not invertible.
3542
3543
\fun{GEN}{Fp_divu}{GEN a, ulong b, GEN m} returns the quotient of \kbd{a} by
3544
\kbd{b} modulo \kbd{m} (smallest nonnegative residue). Raise an error if
3545
\kbd{b} is not invertible.
3546
3547
\fun{int}{invmod}{GEN a, GEN m, GEN *g}, return $1$ if \kbd{a}
3548
modulo \kbd{m} is invertible, else return $0$ and set
3549
$\kbd{g} = \gcd(\kbd{a},\kbd{m})$.
3550
3551
In the following three functions the integer parameter \kbd{ord} can be given
3552
either as a positive \typ{INT} $N$, or as its factorization matrix $\var{faN}$,
3553
or as a pair $[N,\var{faN}]$. The parameter may be omitted by setting it to
3554
\kbd{NULL} (the value is then $p-1$).
3555
3556
\fun{GEN}{Fp_log}{GEN a, GEN g, GEN ord, GEN p} Let $g$ such that
3557
$g^{ord} \equiv 1 \pmod{p}$. Return an integer $e$ such that
3558
$a^e \equiv g \pmod{p}$. If $e$ does not exist, the result is undefined.
3559
3560
\fun{GEN}{Fp_order}{GEN a, GEN ord, GEN p} returns the order of the
3561
\kbd{Fp} \kbd{a}. Assume that \kbd{ord} is a multiple of the order of
3562
\kbd{a}.
3563
3564
\fun{GEN}{Fp_factored_order}{GEN a, GEN ord, GEN p} returns $[o,F]$, where $o$
3565
is the multiplicative order of the \kbd{Fp} $a$ in $\F_p^*$, and $F$ is the
3566
factorization of $o$. Assume that \kbd{ord} is a multiple of the order of
3567
\kbd{a}.
3568
3569
\fun{int}{Fp_issquare}{GEN x, GEN p} returns $1$ if \kbd{x} is a square
3570
modulo \kbd{p}, and $0$ otherwise.
3571
3572
\fun{int}{Fp_ispower}{GEN x, GEN n, GEN p} returns $1$ if \kbd{x} is an
3573
$n$-th power modulo \kbd{p}, and $0$ otherwise.
3574
3575
\fun{GEN}{Fp_sqrt}{GEN x, GEN p} returns a square root of \kbd{x} modulo
3576
\kbd{p} (the smallest nonnegative residue), where \kbd{x}, \kbd{p} are
3577
\typ{INT}s, and \kbd{p} is assumed to be prime. Return \kbd{NULL}
3578
if \kbd{x} is not a quadratic residue modulo \kbd{p}.
3579
3580
\fun{GEN}{Fp_2gener}{GEN p} return a generator of
3581
the $2$-Sylow subgroup of $\F_p^*$. To use with \kbd{Fp\_sqrt\_i}.
3582
3583
\fun{GEN}{Fp_sqrt_i}{GEN x, GEN s2, GEN p}
3584
as \kbd{Fp\_sqrt} where \kbd{s2} is the element returned by
3585
\kbd{Fp\_2gener}.
3586
3587
\fun{GEN}{Fp_sqrtn}{GEN a, GEN n, GEN p, GEN *zn}
3588
returns \kbd{NULL} if $a$ is not an $n$-th power residue mod $p$.
3589
Otherwise, returns an $n$-th root of $a$; if \kbd{zn} is not \kbd{NULL}
3590
set it to a primitive $m$-th root of 1, $m = \gcd(p-1,n)$ allowing to compute
3591
all $m$ solutions in $\F_p$ of the equation $x^n = a$.
3592
3593
\fun{GEN}{Zn_sqrt}{GEN x, GEN n} returns one of the square roots of \kbd{x}
3594
modulo \kbd{n} (possibly not prime), where \kbd{x} is a \typ{INT} and \kbd{n}
3595
is either a \typ{INT} or is given by its factorization matrix. Return
3596
\kbd{NULL} if no such square root exist.
3597
3598
\fun{GEN}{Zn_quad_roots}{GEN N, GEN B, GEN C} solves the equation $X^2 + B X
3599
+ C$ modulo $N$. Return \kbd{NULL} if there are no solutions. Else returns
3600
$[v, M]$ where $M \mid N$ and the \kbd{FpV} $v$ of distinct integers
3601
(reduced, implicitly modulo $M$) is such that $x$ modulo $N$ is a solution to
3602
the equation if and only if $x$ modulo $M$ belongs to $v$. If the
3603
discriminant $B^2-4C$ is coprime to $N$, we have $M = N$ but in general $M$
3604
can be a strict divisor of $N$.
3605
3606
\fun{long}{kross}{long x, long y} returns the \idx{Kronecker symbol} $(x|y)$,
3607
i.e.$-1$, $0$ or $1$. If \kbd{y} is an odd prime, this is the \idx{Legendre
3608
symbol}. (Contrary to \kbd{krouu}, \kbd{kross} also supports $\kbd{y} = 0$)
3609
3610
\fun{long}{krouu}{ulong x, ulong y} returns the \idx{Kronecker symbol}
3611
$(x|y)$, i.e.~$-1$, $0$ or $1$. Assumes \kbd{y} is nonzero. If \kbd{y} is an
3612
odd prime, this is the \idx{Legendre symbol}.
3613
3614
\fun{long}{krois}{GEN x, long y} returns the \idx{Kronecker symbol} $(x|y)$
3615
of \typ{INT}~x and \kbd{long}~\kbd{y}. As \kbd{kross} otherwise.
3616
3617
\fun{long}{kroiu}{GEN x, ulong y} returns the \idx{Kronecker symbol} $(x|y)$
3618
of \typ{INT}~x and nonzero \kbd{ulong}~\kbd{y}. As \kbd{krouu} otherwise.
3619
3620
\fun{long}{krosi}{long x, GEN y} returns the \idx{Kronecker symbol} $(x|y)$
3621
of \kbd{long}~x and \typ{INT}~\kbd{y}. As \kbd{kross} otherwise.
3622
3623
\fun{long}{kroui}{ulong x, GEN y} returns the \idx{Kronecker symbol} $(x|y)$
3624
of \kbd{long}~x and \typ{INT}~\kbd{y}. As \kbd{kross} otherwise.
3625
3626
\fun{long}{kronecker}{GEN x, GEN y} returns the \idx{Kronecker symbol} $(x|y)$
3627
of \typ{INT}s~x and~\kbd{y}. As \kbd{kross} otherwise.
3628
3629
\fun{GEN}{factorial_Fp}{long n, GEN p} return $n!$ mod $p$.
3630
3631
\fun{GEN}{pgener_Fp}{GEN p} returns the smallest primitive root modulo
3632
\kbd{p}, assuming \kbd{p} is prime.
3633
3634
\fun{GEN}{pgener_Zp}{GEN p} returns the smallest primitive root modulo $p^k$,
3635
$k > 1$, assuming \kbd{p} is an odd prime.
3636
3637
\fun{long}{Zp_issquare}{GEN x, GEN p} returns 1 if the \typ{INT} $x$ is
3638
a $p$-adic square, $0$ otherwise.
3639
3640
\fun{long}{Zn_issquare}{GEN x, GEN n} returns 1 if \typ{INT} $x$ is
3641
a square modulo \kbd{n} (possibly not prime), where $n$ is either a \typ{INT}
3642
or is given by its factorization matrix. Return $0$ otherwise.
3643
3644
\fun{long}{Zn_ispower}{GEN x, GEN n, GEN K, GEN *py} returns 1 if \typ{INT}
3645
$x$ is a $K$-th power modulo \kbd{n} (possibly not prime), where $n$ is
3646
either a \typ{INT} or is given by its factorization matrix. Return $0$
3647
otherwise. If \kbd{py} is not \kbd{NULL}, set it to $y$ such that $y^K = x$
3648
modulo $n$.
3649
3650
\fun{GEN}{pgener_Fp_local}{GEN p, GEN L}, \kbd{L} being a vector of
3651
primes dividing $p - 1$, returns the smallest integer $x > 1$ which is a
3652
generator of the $\ell$-Sylow of $\F_p^*$ for every $\ell$ in \kbd{L}. In
3653
other words, $x^{(p-1)/\ell} \neq 1$ for all such $\ell$. In particular,
3654
returns \kbd{pgener\_Fp(p)} if \kbd{L} contains all primes dividing $p - 1$.
3655
It is not necessary, and in fact slightly inefficient, to include $\ell=2$,
3656
since 2 is treated separately in any case, i.e. the generator obtained is
3657
never a square.
3658
3659
\fun{GEN}{rootsof1_Fp}{GEN n, GEN p} returns a primitive $n$-th root modulo
3660
the prime $p$.
3661
3662
\fun{GEN}{rootsof1u_Fp}{ulong n, GEN p} returns a primitive $n$-th root modulo
3663
the prime $p$.
3664
3665
\fun{ulong}{rootsof1_Fl}{ulong n, ulong p} returns a primitive $n$-th root
3666
modulo the prime $p$.
3667
3668
\subsec{Extending functions to vector inputs}
3669
3670
The following functions apply $f$ to the given arguments, recursively
3671
if they are of vector / matrix type:
3672
3673
\fun{GEN}{map_proto_G}{GEN (*f)(GEN), GEN x} For instance, if $x$ is a
3674
\typ{VEC}, return a \typ{VEC} whose components are the $f(x[i])$.
3675
3676
\fun{GEN}{map_proto_lG}{long (*f)(GEN), GEN x} As above, applying the
3677
function \kbd{stoi( f() )}.
3678
3679
\fun{GEN}{map_proto_GL}{GEN (*f)(GEN,long), GEN x, long y}
3680
3681
\fun{GEN}{map_proto_lGL}{long (*f)(GEN,long), GEN x, long y}
3682
3683
In the last function, $f$ implements an associative binary operator, which we
3684
extend naturally to an $n$-ary operator $f_n$ for any $n$: by convention,
3685
$f_0() = 1$, $f_1(x) = x$, and
3686
$$ f_n(x_1,\dots,x_n) = f( f_{n-1}(x_1,\dots,x_{n-1}), x_n)),$$
3687
for $n \geq 2$.
3688
3689
\fun{GEN}{gassoc_proto}{GEN (*f)(GEN,GEN),GEN x, GEN y} If $y$ is not
3690
\kbd{NULL}, return $f(x,y)$. Otherwise, $x$ must be of vector type, and we
3691
return the result of $f$ applied to its components, computed using a
3692
divide-and-conquer algorithm. More precisely, return
3693
$$f( f(x_1,\kbd{NULL}), f(x_2,\kbd{NULL}) ),$$
3694
where $x_1$, $x_2$ are the two halves of $x$.
3695
3696
\subsec{Miscellaneous arithmetic functions}
3697
3698
\fun{long}{bigomegau}{ulong n} returns the number of prime divisors of $n >
3699
0$, counted with multiplicity.
3700
3701
\fun{ulong}{coreu}{ulong n}, unique squarefree integer $d$ dividing $n$ such
3702
that $n/d$ is a square.
3703
3704
\fun{ulong}{coreu_fact}{GEN fa} same, where \kbd{fa} is \kbd{factoru(n)}.
3705
3706
\fun{ulong}{corediscs}{long d, ulong *pt_f}, $d$ (possibly negative)
3707
being congruent to $0$ or $1$ modulo $4$, return the fundamental
3708
discriminant $D$ such that $d=D*f^2$ and set \kbd{*pt\_f} to $f$
3709
(if \kbd{*pt\_f} not \kbd{NULL}).
3710
3711
\fun{ulong}{eulerphiu}{ulong n}, Euler's totient function of $n$.
3712
3713
\fun{ulong}{eulerphiu_fact}{GEN fa} same, where \kbd{fa} is \kbd{factoru(n)}.
3714
3715
\fun{long}{moebiusu}{ulong n}, Moebius $\mu$-function of $n$.
3716
3717
\fun{long}{moebiusu_fact}{GEN fa} same, where \kbd{fa} is \kbd{factoru(n)}.
3718
3719
\fun{ulong}{radicalu}{ulong n}, product of primes dividing $n$.
3720
3721
\fun{GEN}{divisorsu}{ulong n}, returns the divisors of $n$ in a
3722
\typ{VECSMALL}, sorted by increasing order.
3723
3724
\fun{GEN}{divisorsu_fact}{GEN fa} same, where \kbd{fa} is \kbd{factoru(n)}.
3725
3726
\fun{GEN}{divisorsu_fact_factored}{GEN fa} where \kbd{fa} is \kbd{factoru(n)}.
3727
Return a vector $[D,F]$, where $D$ is a a \typ{VECSMALL} containing the
3728
divisors of $u$ and $F[i]$ contains \kbd{factoru}$(D[i])$.
3729
3730
\fun{GEN}{divisorsu_moebius}{GEN P} returns the divisors of $n$ of the form
3731
$\prod_{p\in S} (-p)$, $S\subset P$ in a \typ{VECSMALL}. The vector is not
3732
sorted but its first element is guaranteed to be $1$. If $P$ is
3733
\kbd{factoru(n)[1]}, this returns the set of $\mu(d) d$ where $d$ runs
3734
through the squarefree divisors of $n$.
3735
3736
\fun{long}{numdivu}{ulong n}, returns the number of positive divisors of $n>0$.
3737
3738
\fun{long}{numdivu_fact}{GEN fa} same, where \kbd{fa} is \kbd{factoru(n)}.
3739
3740
\fun{long}{omegau}{ulong n} returns the number of prime divisors of $n > 0$.
3741
3742
\fun{long}{uissquarefree}{ulong n} returns $1$ if \kbd{n}
3743
is square-free, and $0$ otherwise.
3744
3745
\fun{long}{uissquarefree_fact}{GEN fa} same, where \kbd{fa} is
3746
\kbd{factoru(n)}.
3747
3748
\fun{long}{uposisfundamental}{ulong x} return $1$ if $x$ is a fundamental
3749
discriminant, and $0$ otherwise.
3750
3751
\fun{long}{unegisfundamental}{ulong x} return $1$ if $-x$ is a fundamental
3752
discriminant, and $0$ otherwise.
3753
3754
\fun{long}{sisfundamental}{long x} return $1$ if $x$ is a fundamental
3755
discriminant, and $0$ otherwise.
3756
3757
\fun{int}{uis_357_power}{ulong x, ulong *pt, ulong *mask} as \tet{is_357_power}
3758
for \kbd{ulong} $x$.
3759
3760
\fun{int}{uis_357_powermod}{ulong x, ulong *mask} as \tet{uis_357_power}, but
3761
only check for 3rd, 5th or 7th powers modulo
3762
$211\times209\times61\times203\times117\times31\times43\times71$.
3763
3764
\fun{long}{uisprimepower}{ulong n, ulong *p} as \tet{isprimepower}, for
3765
\kbd{ulong} $n$.
3766
3767
\fun{int}{uislucaspsp}{ulong n} returns $1$ if the \kbd{ulong} $n$ fails Lucas
3768
compositeness test (it thus may be prime or composite), and $0$ otherwise
3769
(proving that $n$ is composite).
3770
3771
\fun{int}{uis2psp}{ulong n} returns $1$ if the odd \kbd{ulong} $n > 1$ fails a
3772
strong Rabin-Miller test for the base $2$ (it thus may be prime or
3773
composite), and $0$ otherwise (proving that $n$ is composite).
3774
3775
\fun{int}{uispsp}{ulong a, ulong n} returns $1$ if the odd \kbd{ulong} $n > 1$
3776
fails a strong Rabin-Miller test for the base $1 < a < n$ (it thus may be
3777
prime or composite), and $0$ otherwise (proving that $n$ is composite).
3778
3779
\fun{ulong}{sumdigitsu}{ulong n} returns the sum of decimal digits of $u$.
3780
3781
\fun{GEN}{usumdiv_fact}{GEN fa}, sum of divisors of \kbd{ulong} $n$, where
3782
\kbd{fa} is \kbd{factoru(n)}.
3783
3784
\fun{GEN}{usumdivk_fact}{GEN fa, ulong k}, sum of $k$-th powers of divisors
3785
of \kbd{ulong} $n$, where \kbd{fa} is \kbd{factoru(n)}.
3786
3787
\fun{GEN}{hilbertii}{GEN x, GEN y, GEN p}, returns the Hilbert symbol
3788
$(x,y)$ at the prime $p$ (\kbd{NULL} for the place at infinity); $x$ and $y$
3789
are \typ{INT}s.
3790
3791
\fun{GEN}{sumdedekind}{GEN h, GEN k} returns the Dedekind sum attached to
3792
the \typ{INT} $h$ and $k$, $k > 0$.
3793
3794
\fun{GEN}{sumdedekind_coprime}{GEN h, GEN k} as \kbd{sumdedekind}, except
3795
that $h$ and $k$ are assumed to be coprime \typ{INT}s.
3796
3797
\fun{GEN}{u_sumdedekind_coprime}{long h, long k}
3798
Let $k > 0$, $0 \leq h < k$, $(h,k) = 1$. Returns $[s_1,s_2]$
3799
in a \typ{VECSMALL}, such that $s(h,k) = (s_2 + k s_1) / (12k)$.
3800
Requires $\max(h + k/2, k) < \kbd{LONG\_MAX}$
3801
to avoid overflow, in particular $k \leq (2/3)\kbd{LONG\_MAX}$ is fine.
3802
3803
\newpage
3804
\chapter{Level 2 kernel}
3805
3806
These functions deal with modular arithmetic, linear algebra and polynomials
3807
where assumptions can be made about the types of the coefficients.
3808
3809
\section{Naming scheme}\label{se:level2names}
3810
A function name is built in the following way:
3811
$A_1\kbd{\_}\dots\kbd{\_}A_n\var{fun}$ for an operation \var{fun} with $n$
3812
arguments of class $A_1$,\dots, $A_n$. A class name is given by a base ring
3813
followed by a number of code letters. Base rings are among
3814
3815
\kbd{Fl}: $\Z/l\Z$ where $l < 2^{\B}$ is not necessarily prime. Implemented
3816
using \kbd{ulong}s
3817
3818
\kbd{Fp}: $\Z/p\Z$ where $p$ is a \typ{INT}, not necessarily prime.
3819
Implemented as \typ{INT}s $z$, preferably satisfying $0 \leq z < p$.
3820
More precisely, any \typ{INT} can be used as an \kbd{Fp}, but reduced
3821
inputs are treated more efficiently. Outputs from \kbd{Fp}xxx routines are
3822
reduced.
3823
3824
\kbd{Fq}: $\Z[X]/(p,T(X))$, $p$ a \typ{INT}, $T$ a \typ{POL} with \kbd{Fp}
3825
coefficients or \kbd{NULL} (in which case no reduction modulo \kbd{T} is
3826
performed). Implemented as \typ{POL}s $z$ with \kbd{Fp} coefficients,
3827
$\deg(z) < \deg \kbd{T}$, although $z$ a \typ{INT} is allowed for elements in
3828
the prime field.
3829
3830
\kbd{Z}: the integers $\Z$, implemented as \typ{INT}s.
3831
3832
\kbd{Zp}: the $p$-adic integers $\Z_p$, implemented as \typ{INT}s, for arbitrary $p$
3833
3834
\kbd{Zl}: the $p$-adic integers $\Z_p$, implemented as \typ{INT}s, for $p< 2^{\B}$
3835
3836
\kbd{z}: the integers $\Z$, implemented using (signed) \kbd{long}s.
3837
3838
\kbd{Q}: the rational numbers $\Q$, implemented as \typ{INT}s and
3839
\typ{FRAC}s.
3840
3841
\kbd{Rg}: a commutative ring, whose elements can be
3842
\kbd{gadd}-ed, \kbd{gmul}-ed, etc.
3843
3844
\noindent Possible letters are:
3845
3846
\kbd{X}: polynomial in $X$ (\typ{POL} in a fixed variable), e.g. \kbd{FpX}
3847
means $\Z/p\Z[X]$
3848
3849
\kbd{Y}: polynomial in $Y\neq X$. This is used to resolve ambiguities.
3850
E.g. \kbd{FpXY} means $((\Z/p\Z)[X])[Y]$.
3851
3852
\kbd{V}: vector (\typ{VEC} or \typ{COL}), treated as a line vector
3853
(independently of the actual type). E.g. \kbd{ZV} means $\Z^k$ for some $k$.
3854
3855
\kbd{C}: vector (\typ{VEC} or \typ{COL}), treated as a column vector
3856
(independently of the actual type). The difference with \kbd{V} is purely
3857
semantic: if the result is a vector, it will be of type \typ{COL} unless
3858
mentioned otherwise. For instance the function \kbd{ZC\_add} receives two
3859
integral vectors (\typ{COL} or \typ{VEC}, possibly different types) of the
3860
same length and returns a \typ{COL} whose entries are the sums of the input
3861
coefficients.
3862
3863
\kbd{M}: matrix (\typ{MAT}). E.g. \kbd{QM} means a matrix with rational
3864
entries
3865
3866
\kbd{T}: Trees. Either a leaf or a \typ{VEC} of trees.
3867
3868
\kbd{E}: point over an elliptic curve, represented
3869
as two-component vectors \kbd{[x,y]}, except for the represented by the
3870
one-component vector \kbd{[0]}. Not all curve models are supported.
3871
3872
\kbd{Q}: representative (\typ{POL}) of a class in a polynomial quotient ring.
3873
E.g.~an \kbd{FpXQ} belongs to $(\Z/p\Z)[X]/(T(X))$, \kbd{FpXQV} means a
3874
vector of such elements, etc.
3875
3876
\kbd{n}: a polynomial representative (\typ{POL}) for a truncated power
3877
series modulo $X^n$. E.g.~an \kbd{FpXn} belongs to $(\Z/p\Z)[X]/(X^n)$,
3878
\kbd{FpXnV} means a vector of such elements, etc.
3879
3880
\kbd{x}, \kbd{y}, \kbd{m}, \kbd{v}, \kbd{c}, \kbd{q}: as their uppercase
3881
counterpart, but coefficient arrays are implemented using \typ{VECSMALL}s,
3882
which coefficient understood as \kbd{ulong}s.
3883
3884
\kbd{x} and \kbd{y} (and \kbd{q}) are implemented by a \typ{VECSMALL} whose
3885
first coefficient is used as a code-word and the following are the
3886
coefficients , similarly to a \typ{POL}. This is known as a 'POLSMALL'.
3887
3888
\kbd{m} are implemented by a \typ{MAT} whose components (columns) are
3889
\typ{VECSMALL}s. This is known as a 'MATSMALL'.
3890
3891
\kbd{v} and \kbd{c} are regular \typ{VECSMALL}s. Difference between the
3892
two is purely semantic.
3893
3894
\noindent Omitting the letter means the argument is a scalar in the base
3895
ring. Standard functions \var{fun} are
3896
3897
\kbd{add}: add
3898
3899
\kbd{sub}: subtract
3900
3901
\kbd{mul}: multiply
3902
3903
\kbd{sqr}: square
3904
3905
\kbd{div}: divide (Euclidean quotient)
3906
3907
\kbd{rem}: Euclidean remainder
3908
3909
\kbd{divrem}: return Euclidean quotient, store remainder in a pointer
3910
argument. Three special values of that pointer argument modify the default
3911
behavior: \kbd{NULL} (do not store the remainder, used to implement
3912
\kbd{div}), \tet{ONLY_REM} (return the remainder, used to implement
3913
\kbd{rem}), \tet{ONLY_DIVIDES} (return the quotient if the division is exact,
3914
and \kbd{NULL} otherwise).
3915
3916
\kbd{gcd}: GCD
3917
3918
\kbd{extgcd}: return GCD, store Bezout coefficients in pointer arguments
3919
3920
\kbd{pow}: exponentiate
3921
3922
\kbd{eval}: evaluation / composition
3923
3924
\section{Coefficient ring}
3925
3926
\fun{long}{Rg_type}{GEN x, GEN *ptp, GEN *ptpol, long *ptprec} returns
3927
the ``natural'' base ring over which the object $x$ is defined.
3928
3929
Raise an error if it detects consistency problems in modular objects:
3930
incompatible rings (e.g. $\F_p$ and $\F_q$ for primes $p\neq q$,
3931
$\F_p[X]/(T)$ and $\F_p[X]/(U)$ for $T\neq U$). Minor discrepancies are
3932
supported if they make general sense (e.g. $\F_p$ and $\F_{p^k}$, but not
3933
$\F_p$ and $\Q_p$); \typ{FFELT} and \typ{POLMOD} of \typ{INTMOD}s are
3934
considered inconsistent, even if they define the same field: if you need to
3935
use simultaneously these different finite field implementations, multiply the
3936
polynomial by a \typ{FFELT} equal to $1$ first.
3937
3938
\item 0: none of the others (presumably multivariate, possibly inconsistent).
3939
3940
\item \typ{INT}: defined over $\Z$.
3941
3942
\item \typ{FRAC}: defined over $\Q$.
3943
3944
\item \typ{INTMOD}: defined over $\Z/p\Z$, where \kbd{*ptp} is set to $p$.
3945
It is not checked whether $p$ is prime.
3946
3947
\item \typ{COMPLEX}: defined over $\C$ (at least one \typ{COMPLEX} with at
3948
least one inexact floating point \typ{REAL} component). Set \kbd{*ptprec}
3949
to the minimal accuracy (as per \kbd{precision}) of inexact components.
3950
3951
\item \typ{REAL}: defined over $\R$ (at least one inexact floating point
3952
\typ{REAL} component). Set \kbd{*ptprec} to the minimal accuracy (as per
3953
\kbd{precision}) of inexact components.
3954
3955
\item \typ{PADIC}: defined over $\Q_p$, where \kbd{*ptp} is set to $p$ and
3956
\kbd{*ptprec} to the $p$-adic accuracy.
3957
3958
\item \typ{FFELT}: defined over a finite field $\F_{p^k}$, where \kbd{*ptp}
3959
is set to the field characteristic $p$ and \kbd{*ptpol} is set to a
3960
\typ{FFELT} belonging to the field.
3961
3962
\item \typ{POL}: defined over a polynomial ring.
3963
3964
\item other values are composite corresponding to quotients $R[X]/(T)$, with
3965
one primary type \kbd{t1}, describing the form of the quotient,
3966
and a secondary type \kbd{t2}, describing $R$. If \kbd{t} is the
3967
\kbd{RgX\_type}, \kbd{t1} and \kbd{t2} are recovered using
3968
3969
\fun{void}{RgX_type_decode}{long t, long *t1, long *t2}
3970
3971
\kbd{t1} is one of
3972
3973
\typ{POLMOD}: at least one \typ{POLMOD} component,
3974
set \kbd{*ppol} to the modulus,
3975
3976
\typ{QUAD}: no \typ{POLMOD}, at least one \typ{QUAD} component,
3977
set \kbd{*ppol} to the modulus (\kbd{$-$.pol}) of the \typ{QUAD},
3978
3979
\typ{COMPLEX}: no \typ{POLMOD} or \typ{QUAD}, at least one \typ{COMPLEX}
3980
component, set \kbd{*ppol} to $y^2 + 1$.
3981
3982
and the underlying base ring $R$ is given by \kbd{t2}, which
3983
is one of \typ{INT}, \typ{INTMOD} (set \kbd{*ptp}) or \typ{PADIC}
3984
(set \kbd{*ptp} and \kbd{*ptprec}), with the same meaning
3985
as above.
3986
3987
\fun{int}{RgX_type_is_composite}{long t} $t$ as returned by \kbd{RgX\_type},
3988
return 1 if $t$ is a composite type, and 0 otherwise.
3989
3990
\fun{GEN}{Rg_get_0}{GEN x} returns $0$ in the base ring over which $x$
3991
is defined, to the proper accuracy (e.g. \kbd{0}, \kbd{Mod(0,3)},
3992
\kbd{O(5\pow 10)}).
3993
3994
\fun{GEN}{Rg_get_1}{GEN x} returns $1$ in the base ring over which $x$
3995
is defined, to the proper accuracy (e.g. \kbd{0}, \kbd{Mod(0,3)},
3996
3997
\fun{long}{RgX_type}{GEN x, GEN *ptp, GEN *ptpol, long *ptprec} returns
3998
the ``natural'' base ring over which the polynomial $x$ is defined,
3999
otherwise as \kbd{Rg\_type}.
4000
4001
\fun{long}{RgX_Rg_type}{GEN x, GEN y, GEN *ptp, GEN *ptpol, long *ptprec}
4002
returns the ``natural'' base ring over which the polynomial $x$ and the element
4003
$y$ are defined, otherwise as \kbd{Rg\_type}.
4004
4005
\fun{long}{RgX_type2}{GEN x, GEN y, GEN *ptp, GEN *ptpol, long *ptprec} returns
4006
the ``natural'' base ring over which the polynomials $x$ and $y$ are defined,
4007
otherwise as \kbd{Rg\_type}.
4008
4009
\fun{long}{RgX_type3}{GEN x, GEN y, GNE z, GEN *ptp, GEN *ptpol, long *ptprec}
4010
returns the ``natural'' base ring over which the polynomials $x$, $y$ and $z$
4011
are defined, otherwise as \kbd{Rg\_type}.
4012
4013
\fun{long}{RgM_type}{GEN x, GEN *ptp, GEN *ptpol, long *ptprec} returns
4014
the ``natural'' base ring over which the matrix $x$ is defined,
4015
otherwise as \kbd{Rg\_type}.
4016
4017
\fun{long}{RgM_type2}{GEN x, GEN y, GEN *ptp, GEN *ptpol, long *ptprec} returns
4018
the ``natural'' base ring over which the matrices $x$ and $y$ are defined,
4019
otherwise as \kbd{Rg\_type}.
4020
4021
\fun{long}{RgV_type}{GEN x, GEN *ptp, GEN *ptpol, long *ptprec} returns
4022
the ``natural'' base ring over which the vector $x$ is defined,
4023
otherwise as \kbd{Rg\_type}.
4024
4025
\fun{long}{RgV_type2}{GEN x, GEN y, GEN *ptp, GEN *ptpol, long *ptprec} returns
4026
the ``natural'' base ring over which the vectors $x$ and $y$ are defined,
4027
otherwise as \kbd{Rg\_type}.
4028
4029
\fun{long}{RgM_RgC_type}{GEN x, GEN y, GEN *ptp, GEN *ptpol, long *ptprec}
4030
returns the ``natural'' base ring over which the matrix $x$ and the vector
4031
$y$ are defined, otherwise as \kbd{Rg\_type}.
4032
4033
\section{Modular arithmetic}
4034
4035
\noindent These routines implement univariate polynomial arithmetic and
4036
linear algebra over finite fields, in fact over finite rings of the form
4037
$(\Z/p\Z)[X]/(T)$, where $p$ is not necessarily prime and $T\in(\Z/p\Z)[X]$ is
4038
possibly reducible; and finite extensions thereof. All this can be emulated
4039
with \typ{INTMOD} and \typ{POLMOD} coefficients and using generic routines,
4040
at a considerable loss of efficiency. Also, specialized routines are
4041
available that have no obvious generic equivalent.
4042
4043
\subsec{\kbd{FpC} / \kbd{FpV}, \kbd{FpM}} A \kbd{ZV}
4044
(resp.~a~\kbd{ZM}) is a \typ{VEC} or \typ{COL} (resp.~\typ{MAT}) with
4045
\typ{INT} coefficients. An \kbd{FpV} or \kbd{FpM}, with respect to a given
4046
\typ{INT}~\kbd{p}, is the same with \kbd{Fp} coordinates; operations are
4047
understood over $\Z/p\Z$.
4048
4049
\subsubsec{Conversions}
4050
4051
\fun{int}{Rg_is_Fp}{GEN z, GEN *p}, checks if \kbd{z} can be mapped to
4052
$\Z/p\Z$: a \typ{INT} or a \typ{INTMOD} whose modulus is equal to \kbd{*p},
4053
(if \kbd{*p} not \kbd{NULL}), in that case return $1$, else $0$. If a modulus
4054
is found it is put in \kbd{*p}, else \kbd{*p} is left unchanged.
4055
4056
\fun{int}{RgV_is_FpV}{GEN z, GEN *p}, \kbd{z} a \typ{VEC} (resp. \typ{COL}),
4057
checks if it can be mapped to a \kbd{FpV} (resp. \kbd{FpC}), by checking
4058
\kbd{Rg\_is\_Fp} coefficientwise.
4059
4060
\fun{int}{RgM_is_FpM}{GEN z, GEN *p}, \kbd{z} a \typ{MAT},
4061
checks if it can be mapped to a \kbd{FpM}, by checking \kbd{RgV\_is\_FpV}
4062
columnwise.
4063
4064
\fun{GEN}{Rg_to_Fp}{GEN z, GEN p}, \kbd{z} a scalar which can be mapped to
4065
$\Z/p\Z$: a \typ{INT}, a \typ{INTMOD} whose modulus is divisible by $p$,
4066
a \typ{FRAC} whose denominator is coprime to $p$, or a \typ{PADIC} with
4067
underlying prime $\ell$ satisfying $p = \ell^n$ for some $n$ (less than the
4068
accuracy of the input). Returns \kbd{lift(z * Mod(1,p))}, normalized.
4069
4070
\fun{GEN}{padic_to_Fp}{GEN x, GEN p} special case of \tet{Rg_to_Fp},
4071
for a $x$ a \typ{PADIC}.
4072
4073
\fun{GEN}{RgV_to_FpV}{GEN z, GEN p}, \kbd{z} a \typ{VEC} or \typ{COL},
4074
returns the \kbd{FpV} (as a \typ{VEC}) obtained by applying \kbd{Rg\_to\_Fp}
4075
coefficientwise.
4076
4077
\fun{GEN}{RgC_to_FpC}{GEN z, GEN p}, \kbd{z} a \typ{VEC} or \typ{COL},
4078
returns the \kbd{FpC} (as a \typ{COL}) obtained by applying \kbd{Rg\_to\_Fp}
4079
coefficientwise.
4080
4081
\fun{GEN}{RgM_to_FpM}{GEN z, GEN p}, \kbd{z} a \typ{MAT},
4082
returns the \kbd{FpM} obtained by applying \kbd{RgC\_to\_FpC}
4083
columnwise.
4084
4085
\fun{GEN}{RgM_Fp_init}{GEN z, GEN p, ulong *pp}, given an \kbd{RgM} $z$,
4086
whose entries can be mapped to $\F_p$ (as per \tet{Rg_to_Fp}), and a prime
4087
number $p$. This routine returns a normal form of $z$: either an
4088
\kbd{F2m} ($p = 2$), an \kbd{Flm} ($p$ fits into an \kbd{ulong})
4089
or an \kbd{FpM}. In the first two cases, \kbd{pp} is set to \kbd{itou}$(p)$,
4090
and to $0$ in the last.
4091
4092
The functions above are generally used as follows:
4093
\bprog
4094
GEN add(GEN x, GEN y)
4095
{
4096
GEN p = NULL;
4097
if (Rg_is_Fp(x, &p) && Rg_is_Fp(y, &p) && p)
4098
{
4099
x = Rg_to_Fp(x, p); y = Rg_to_Fp(y, p);
4100
z = Fp_add(x, y, p);
4101
return Fp_to_mod(z);
4102
}
4103
else return gadd(x, y);
4104
}
4105
@eprog
4106
4107
\fun{GEN}{FpC_red}{GEN z, GEN p}, \kbd{z} a \kbd{ZC}. Returns \kbd{lift(Col(z) *
4108
Mod(1,p))}, hence a \typ{COL}.
4109
4110
\fun{GEN}{FpV_red}{GEN z, GEN p}, \kbd{z} a \kbd{ZV}. Returns \kbd{lift(Vec(z) *
4111
Mod(1,p))}, hence a \typ{VEC}
4112
4113
\fun{GEN}{FpM_red}{GEN z, GEN p}, \kbd{z} a \kbd{ZM}. Returns \kbd{lift(z *
4114
Mod(1,p))}, which is an \kbd{FpM}.
4115
4116
\subsubsec{Basic operations}
4117
4118
\fun{GEN}{random_FpC}{long n, GEN p} returns a random \kbd{FpC} with $n$
4119
components.
4120
4121
\fun{GEN}{random_FpV}{long n, GEN p} returns a random \kbd{FpV} with $n$
4122
components.
4123
4124
\fun{GEN}{FpC_center}{GEN z, GEN p, GEN pov2} returns a \typ{COL} whose
4125
entries are the \kbd{Fp\_center} of the \kbd{gel(z,i)}.
4126
4127
\fun{GEN}{FpM_center}{GEN z, GEN p, GEN pov2} returns a matrix whose
4128
entries are the \kbd{Fp\_center} of the \kbd{gcoeff(z,i,j)}.
4129
4130
\fun{void}{FpC_center_inplace}{GEN z, GEN p, GEN pov2}
4131
in-place version of \kbd{FpC\_center}, using \kbd{affii}.
4132
4133
\fun{void}{FpM_center_inplace}{GEN z, GEN p, GEN pov2}
4134
in-place version of \kbd{FpM\_center}, using \kbd{affii}.
4135
4136
\fun{GEN}{FpC_add}{GEN x, GEN y, GEN p} adds the \kbd{ZC} $x$ and $y$
4137
and reduce modulo $p$ to obtain an \kbd{FpC}.
4138
4139
\fun{GEN}{FpV_add}{GEN x, GEN y, GEN p} same as \kbd{FpC\_add}, returning and
4140
\kbd{FpV}.
4141
4142
\fun{GEN}{FpM_add}{GEN x, GEN y, GEN p} adds the two \kbd{ZM}s~\kbd{x}
4143
and \kbd{y} (assumed to have compatible dimensions), and reduce modulo
4144
\kbd{p} to obtain an \kbd{FpM}.
4145
4146
\fun{GEN}{FpC_sub}{GEN x, GEN y, GEN p} subtracts the \kbd{ZC} $y$ to
4147
the \kbd{ZC} $x$ and reduce modulo $p$ to obtain an \kbd{FpC}.
4148
4149
\fun{GEN}{FpV_sub}{GEN x, GEN y, GEN p} same as \kbd{FpC\_sub}, returning and
4150
\kbd{FpV}.
4151
4152
\fun{GEN}{FpM_sub}{GEN x, GEN y, GEN p} subtracts the two \kbd{ZM}s~\kbd{x}
4153
and \kbd{y} (assumed to have compatible dimensions), and reduce modulo
4154
\kbd{p} to obtain an \kbd{FpM}.
4155
4156
\fun{GEN}{FpC_Fp_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{ZC}~\kbd{x}
4157
(seen as a column vector) by the \typ{INT}~\kbd{y} and reduce modulo \kbd{p} to
4158
obtain an \kbd{FpC}.
4159
4160
\fun{GEN}{FpM_Fp_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{ZM}~\kbd{x}
4161
(seen as a column vector) by the \typ{INT}~\kbd{y} and reduce modulo \kbd{p} to
4162
obtain an \kbd{FpM}.
4163
4164
\fun{GEN}{FpC_FpV_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{ZC}~\kbd{x}
4165
(seen as a column vector) by the \kbd{ZV}~\kbd{y} (seen as a row vector,
4166
assumed to have compatible dimensions), and reduce modulo \kbd{p} to obtain
4167
an \kbd{FpM}.
4168
4169
\fun{GEN}{FpM_mul}{GEN x, GEN y, GEN p} multiplies the two \kbd{ZM}s~\kbd{x}
4170
and \kbd{y} (assumed to have compatible dimensions), and reduce modulo
4171
\kbd{p} to obtain an \kbd{FpM}.
4172
4173
\fun{GEN}{FpM_powu}{GEN x, ulong n, GEN p} computes $x^n$ where $x$ is a
4174
square \kbd{FpM}.
4175
4176
\fun{GEN}{FpM_FpC_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{ZM}~\kbd{x}
4177
by the \kbd{ZC}~\kbd{y} (seen as a column vector, assumed to have compatible
4178
dimensions), and reduce modulo \kbd{p} to obtain an \kbd{FpC}.
4179
4180
\fun{GEN}{FpM_FpC_mul_FpX}{GEN x, GEN y, GEN p, long v} is a memory-clean
4181
version of
4182
\bprog
4183
GEN tmp = FpM_FpC_mul(x,y,p);
4184
return RgV_to_RgX(tmp, v);
4185
@eprog
4186
4187
\fun{GEN}{FpV_FpC_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{ZV}~\kbd{x}
4188
(seen as a row vector) by the \kbd{ZC}~\kbd{y} (seen as a column vector,
4189
assumed to have compatible dimensions), and reduce modulo \kbd{p} to obtain
4190
an \kbd{Fp}.
4191
4192
\fun{GEN}{FpV_dotproduct}{GEN x,GEN y,GEN p} scalar product of
4193
$x$ and $y$ (assumed to have the same length).
4194
4195
\fun{GEN}{FpV_dotsquare}{GEN x, GEN p} scalar product of $x$ with itself.
4196
has \typ{INT} entries.
4197
4198
\fun{GEN}{FpV_factorback}{GEN L, GEN e, GEN p} given an \kbd{FpV} $L$
4199
and a \kbd{ZV} or \kbd{zv} $e$ of the same length, return $\prod_i L_i^{e_i}$
4200
modulo $p$.
4201
4202
\subsubsec{\kbd{Fp}-linear algebra} The implementations are not
4203
asymptotically efficient ($O(n^3)$ standard algorithms).
4204
4205
\fun{GEN}{FpM_deplin}{GEN x, GEN p} returns a nontrivial kernel vector,
4206
or \kbd{NULL} if none exist.
4207
4208
\fun{GEN}{FpM_det}{GEN x, GEN p} as \kbd{det}
4209
4210
\fun{GEN}{FpM_gauss}{GEN a, GEN b, GEN p} as \kbd{gauss}, where $a$ and
4211
$b$ are \kbd{FpM}.
4212
4213
\fun{GEN}{FpM_FpC_gauss}{GEN a, GEN b, GEN p} as \kbd{gauss}, where $a$
4214
is a \kbd{FpM} and $b$ a \kbd{FpC}.
4215
4216
\fun{GEN}{FpM_image}{GEN x, GEN p} as \kbd{image}
4217
4218
\fun{GEN}{FpM_intersect}{GEN x, GEN y, GEN p} as \kbd{intersect}
4219
4220
\fun{GEN}{FpM_intersect_i}{GEN x, GEN y, GEN p} internal variant of
4221
\kbd{FpM\_intersect} but the result is only a generating set, not
4222
necessarily an $\F_p$-basis. It is not \kbd{gerepile}-clean either, but
4223
suitable for \kbd{gerepileupto}.
4224
4225
\fun{GEN}{FpM_inv}{GEN x, GEN p} returns a left inverse of \kbd{x}
4226
(the inverse if $x$ is square), or \kbd{NULL} if \kbd{x} is not invertible.
4227
4228
\fun{GEN}{FpM_FpC_invimage}{GEN A, GEN y, GEN p}
4229
given an \kbd{FpM} $A$ and an \kbd{FpC} $y$, returns an $x$ such that $Ax =
4230
y$, or \kbd{NULL} if no such vector exist.
4231
4232
\fun{GEN}{FpM_invimage}{GEN A, GEN y, GEN p}
4233
given two \kbd{FpM} $A$ and $y$, returns $x$ such that $Ax = y$, or \kbd{NULL}
4234
if no such matrix exist.
4235
4236
\fun{GEN}{FpM_ker}{GEN x, GEN p} as \kbd{ker}
4237
4238
\fun{long}{FpM_rank}{GEN x, GEN p} as \kbd{rank}
4239
4240
\fun{GEN}{FpM_indexrank}{GEN x, GEN p} as \kbd{indexrank}
4241
4242
\fun{GEN}{FpM_suppl}{GEN x, GEN p} as \kbd{suppl}
4243
4244
\fun{GEN}{FpM_hess}{GEN x, GEN p} upper Hessenberg form of $x$ over $\F_p$.
4245
4246
\fun{GEN}{FpM_charpoly}{GEN x, GEN p} characteristic polynomial of $x$.
4247
4248
\subsubsec{\kbd{FqC}, \kbd{FqM} and \kbd{Fq}-linear algebra}
4249
4250
An \kbd{FqM} (resp. \kbd{FqC}) is a matrix (resp a \typ{COL}) with
4251
\kbd{Fq} coefficients (with respect to given \kbd{T}, \kbd{p}), not necessarily
4252
reduced (i.e arbitrary \typ{INT}s and \kbd{ZX}s in the same variable as
4253
\kbd{T}).
4254
4255
\fun{GEN}{RgC_to_FqC}{GEN z, GEN T, GEN p}
4256
4257
\fun{GEN}{RgM_to_FqM}{GEN z, GEN T, GEN p}
4258
4259
\fun{GEN}{FqC_add}{GEN a, GEN b, GEN T, GEN p}
4260
4261
\fun{GEN}{FqC_sub}{GEN a, GEN b, GEN T, GEN p}
4262
4263
\fun{GEN}{FqC_Fq_mul}{GEN a, GEN b, GEN T, GEN p}
4264
4265
\fun{GEN}{FqC_FqV_mul}{GEN a, GEN b, GEN T, GEN p}
4266
4267
\fun{GEN}{FqM_FqC_gauss}{GEN a, GEN b, GEN T, GEN p}
4268
as \kbd{gauss}, where $b$ is a \kbd{FqC}.
4269
4270
\fun{GEN}{FqM_FqC_invimage}{GEN a, GEN b, GEN T, GEN p}
4271
4272
\fun{GEN}{FqM_FqC_mul}{GEN a, GEN b, GEN T, GEN p}
4273
4274
\fun{GEN}{FqM_deplin}{GEN x, GEN T, GEN p} returns a nontrivial
4275
kernel vector, or \kbd{NULL} if none exist.
4276
4277
\fun{GEN}{FqM_det}{GEN x, GEN T, GEN p} as \kbd{det}
4278
4279
\fun{GEN}{FqM_gauss}{GEN a, GEN b, GEN T, GEN p}
4280
as \kbd{gauss}, where $b$ is a \kbd{FqM}.
4281
4282
\fun{GEN}{FqM_image}{GEN x, GEN T, GEN p} as \kbd{image}
4283
4284
\fun{GEN}{FqM_indexrank}{GEN x, GEN T, GEN p} as \kbd{indexrank}
4285
4286
\fun{GEN}{FqM_inv}{GEN x, GEN T, GEN p} returns the inverse of \kbd{x}, or
4287
\kbd{NULL} if \kbd{x} is not invertible.
4288
4289
\fun{GEN}{FqM_invimage}{GEN a, GEN b, GEN T, GEN p} as \kbd{invimage}
4290
4291
\fun{GEN}{FqM_ker}{GEN x, GEN T, GEN p} as \kbd{ker}
4292
4293
\fun{GEN}{FqM_mul}{GEN a, GEN b, GEN T, GEN p}
4294
4295
\fun{long}{FqM_rank}{GEN x, GEN T, GEN p} as \kbd{rank}
4296
4297
\fun{GEN}{FqM_suppl}{GEN x, GEN T, GEN p} as \kbd{suppl}
4298
4299
4300
\subsec{\kbd{Flc} / \kbd{Flv}, \kbd{Flm}} See \kbd{FpV}, \kbd{FpM}
4301
operations.
4302
4303
\fun{GEN}{Flv_copy}{GEN x} returns a copy of \kbd{x}.
4304
4305
\fun{GEN}{Flv_center}{GEN z, ulong p, ulong ps2}
4306
4307
\fun{GEN}{random_Flv}{long n, ulong p} returns a random \kbd{Flv} with $n$
4308
components.
4309
4310
\fun{GEN}{Flm_copy}{GEN x} returns a copy of \kbd{x}.
4311
4312
\fun{GEN}{matid_Flm}{long n} returns an \kbd{Flm} which is an $n \times n$
4313
identity matrix.
4314
4315
\fun{GEN}{scalar_Flm}{long s, long n} returns an \kbd{Flm} which is $s$ times
4316
the $n \times n$ identity matrix.
4317
4318
\fun{GEN}{Flm_center}{GEN z, ulong p, ulong ps2}
4319
4320
\fun{GEN}{Flm_Fl_add}{GEN x, ulong y, ulong p} returns $x + y*\text{Id}$
4321
($x$ must be square).
4322
4323
\fun{GEN}{Flm_Fl_sub}{GEN x, ulong y, ulong p} returns $x - y*\text{Id}$
4324
($x$ must be square).
4325
4326
\fun{GEN}{Flm_Flc_mul}{GEN x, GEN y, ulong p} multiplies \kbd{x} and \kbd{y}
4327
(assumed to have compatible dimensions).
4328
4329
\fun{GEN}{Flm_Flc_mul_pre}{GEN x, GEN y, ulong p, ulong pi} multiplies \kbd{x}
4330
and \kbd{y} (assumed to have compatible dimensions), assuming $pi$ is the
4331
pseudo inverse of $p$.
4332
4333
\fun{GEN}{Flc_Flv_mul}{GEN x, GEN y, ulong p} multiplies the column vector $x$
4334
by the row vector $y$. The result is a matrix.
4335
4336
\fun{GEN}{Flm_Flc_mul_pre_Flx}{GEN x, GEN y, ulong p, ulong pi, long sv}
4337
return \kbd{Flv\_to\_Flx(Flm\_Flc\_mul\_pre(x, y, p, pi), sv)}.
4338
4339
\fun{GEN}{Flm_Fl_mul}{GEN x, ulong y, ulong p} multiplies the \kbd{Flm}
4340
\kbd{x} by \kbd{y}.
4341
4342
\fun{GEN}{Flm_Fl_mul_pre}{GEN x, ulong y, ulong p, ulong pi} multiplies the
4343
\kbd{Flm} \kbd{x} by \kbd{y} assuming $pi$ is the pseudo inverse of $p$.
4344
4345
\fun{GEN}{Flm_neg}{GEN x, ulong p} negates the \kbd{Flm} \kbd{x}.
4346
4347
\fun{void}{Flm_Fl_mul_inplace}{GEN x, ulong y, ulong p} replaces
4348
the \kbd{Flm} \kbd{x} by $\kbd{x}*\kbd{y}$.
4349
4350
\fun{GEN}{Flv_Fl_mul}{GEN x, ulong y, ulong p} multiplies the \kbd{Flv}
4351
\kbd{x} by \kbd{y}.
4352
4353
\fun{void}{Flv_Fl_mul_inplace}{GEN x, ulong y, ulong p} replaces
4354
the \kbd{Flc} \kbd{x} by $\kbd{x}*\kbd{y}$.
4355
4356
\fun{void}{Flv_Fl_mul_part_inplace}{GEN x, ulong y, ulong p, long l}
4357
multiplies $x[1..l]$ by $y$ modulo $p$. In place.
4358
4359
\fun{GEN}{Flv_Fl_div}{GEN x, ulong y, ulong p} divides the \kbd{Flv}
4360
\kbd{x} by \kbd{y}.
4361
4362
\fun{void}{Flv_Fl_div_inplace}{GEN x, ulong y, ulong p} replaces
4363
the \kbd{Flv} \kbd{x} by $\kbd{x}/\kbd{y}$.
4364
4365
\fun{void}{Flc_lincomb1_inplace}{GEN X, GEN Y, ulong v, ulong q}
4366
sets $X\leftarrow X + vY$, where $X,Y$ are \kbd{Flc}. Memory efficient (e.g.
4367
no-op if $v = 0$), and gerepile-safe.
4368
4369
\fun{GEN}{Flv_add}{GEN x, GEN y, ulong p} adds two \kbd{Flv}.
4370
4371
\fun{void}{Flv_add_inplace}{GEN x, GEN y, ulong p} replaces
4372
$x$ by $x+y$.
4373
4374
\fun{GEN}{Flv_neg}{GEN x, ulong p} returns $-x$.
4375
4376
\fun{void}{Flv_neg_inplace}{GEN x, ulong p} replaces $x$ by $-x$.
4377
4378
\fun{GEN}{Flv_sub}{GEN x, GEN y, ulong p} subtracts \kbd{y} to \kbd{x}.
4379
4380
\fun{void}{Flv_sub_inplace}{GEN x, GEN y, ulong p} replaces $x$ by $x-y$.
4381
4382
\fun{ulong}{Flv_dotproduct}{GEN x, GEN y, ulong p} returns the scalar product
4383
of \kbd{x} and \kbd{y}
4384
4385
\fun{ulong}{Flv_dotproduct_pre}{GEN x, GEN y, ulong p, ulong pi} returns the
4386
scalar product of \kbd{x} and \kbd{y} assuming $pi$ is the pseudo inverse of
4387
$p$.
4388
4389
\fun{GEN}{Flv_factorback}{GEN L, GEN e, ulong p} given an \kbd{Flv} $L$
4390
and a \kbd{zv} $e$ of the same length, return $\prod_i L_i^{e_i}$ modulo $p$.
4391
4392
\fun{ulong}{Flv_sum}{GEN x, ulong p} returns the sum of the components of $x$.
4393
4394
\fun{ulong}{Flv_prod}{GEN x, ulong p} returns the product of the components of
4395
$x$.
4396
4397
\fun{ulong}{Flv_prod_pre}{GEN x, ulong p, ulong pi} as \kbd{Flv\_prod}
4398
assuming $pi$ is the pseudo inverse of $p$.
4399
4400
\fun{GEN}{Flv_inv}{GEN x, ulong p} returns the vector of inverses of the elements
4401
of $x$ (as a \kbd{Flv}). Use Montgomery trick.
4402
4403
\fun{void}{Flv_inv_inplace}{GEN x, ulong p} in place variant of \kbd{Flv\_inv}.
4404
4405
\fun{GEN}{Flv_inv_pre}{GEN x, ulong p, ulong pi} as \kbd{Flv\_inv}
4406
assuming $pi$ is the pseudo inverse of $p$.
4407
4408
\fun{void}{Flv_inv_pre_inplace}{GEN x, ulong p, ulong pi} in place variant of
4409
\kbd{Flv\_inv}.
4410
4411
\fun{GEN}{Flc_FpV_mul}{GEN x, GEN y, GEN p} multiplies $x$
4412
(seen as a column vector) by $y$ (seen as a row vector,
4413
assumed to have compatible dimensions) to obtain an \kbd{Flm}.
4414
4415
\fun{GEN}{zero_Flm}{long m, long n} creates a \kbd{Flm} with \kbd{m} x \kbd{n}
4416
components set to $0$. Note that the result allocates a
4417
\emph{single} column, so modifying an entry in one column modifies it in
4418
all columns.
4419
4420
\fun{GEN}{zero_Flm_copy}{long m, long n} creates a \kbd{Flm} with \kbd{m} x
4421
\kbd{n} components set to $0$.
4422
4423
\fun{GEN}{zero_Flv}{long n} creates a \kbd{Flv} with \kbd{n} components set to
4424
$0$.
4425
4426
\fun{GEN}{Flm_row}{GEN A, long x0} return $A[i,]$, the $i$-th row of the
4427
\kbd{Flm} $A$.
4428
4429
\fun{GEN}{Flm_add}{GEN x, GEN y, ulong p} adds \kbd{x} and \kbd{y}
4430
(assumed to have compatible dimensions).
4431
4432
\fun{GEN}{Flm_sub}{GEN x, GEN y, ulong p} subtracts \kbd{x} and \kbd{y}
4433
(assumed to have compatible dimensions).
4434
4435
\fun{GEN}{Flm_mul}{GEN x, GEN y, ulong p} multiplies \kbd{x} and \kbd{y}
4436
(assumed to have compatible dimensions).
4437
4438
\fun{GEN}{Flm_mul_pre}{GEN x, GEN y, ulong p, ulong pi} multiplies \kbd{x} and
4439
\kbd{y} (assumed to have compatible dimensions), assuming $pi$ is the pseudo
4440
inverse of $p$.
4441
4442
\fun{GEN}{Flm_powers}{GEN x, ulong n, ulong p} returns
4443
$[\kbd{x}^0, \dots, \kbd{x}^\kbd{n}]$ as a \typ{VEC} of \kbd{Flm}s.
4444
4445
\fun{GEN}{Flm_powu}{GEN x, ulong n, ulong p} computes $x^n$ where $x$ is a
4446
square \kbd{Flm}.
4447
4448
\fun{GEN}{Flm_charpoly}{GEN x, ulong p} return the characteristic polynomial of
4449
the square \kbd{Flm} $x$, as a \kbd{Flx}.
4450
4451
\fun{GEN}{Flm_deplin}{GEN x, ulong p}
4452
4453
\fun{ulong}{Flm_det}{GEN x, ulong p}
4454
4455
\fun{ulong}{Flm_det_sp}{GEN x, ulong p}, as \kbd{Flm\_det}, in place
4456
(destroys~\kbd{x}).
4457
4458
\fun{GEN}{Flm_gauss}{GEN a, GEN b, ulong p} as \kbd{gauss}, where $b$ is a
4459
\kbd{Flm}.
4460
4461
\fun{GEN}{Flm_Flc_gauss}{GEN a, GEN b, ulong p} as \kbd{gauss}, where $b$ is
4462
a \kbd{Flc}.
4463
4464
\fun{GEN}{Flm_indexrank}{GEN x, ulong p}
4465
4466
\fun{GEN}{Flm_inv}{GEN x, ulong p}
4467
4468
\fun{GEN}{Flm_adjoint}{GEN x, ulong p} as \kbd{matadjoint}.
4469
4470
\fun{GEN}{Flm_Flc_invimage}{GEN A, GEN y, ulong p} given an \kbd{Flm}
4471
$A$ and an \kbd{Flc} $y$, returns an $x$ such that $Ax = y$, or \kbd{NULL}
4472
if no such vector exist.
4473
4474
\fun{GEN}{Flm_invimage}{GEN A, GEN y, ulong p}
4475
given two \kbd{Flm} $A$ and $y$, returns $x$ such that $Ax = y$, or \kbd{NULL}
4476
if no such matrix exist.
4477
4478
\fun{GEN}{Flm_ker}{GEN x, ulong p}
4479
4480
\fun{GEN}{Flm_ker_sp}{GEN x, ulong p, long deplin}, as \kbd{Flm\_ker} (if
4481
\kbd{deplin=0}) or \kbd{Flm\_deplin} (if \kbd{deplin=1}) , in place
4482
(destroys~\kbd{x}).
4483
4484
\fun{long}{Flm_rank}{GEN x, ulong p}
4485
4486
\fun{long}{Flm_suppl}{GEN x, ulong p}
4487
4488
\fun{GEN}{Flm_image}{GEN x, ulong p}
4489
4490
\fun{GEN}{Flm_intersect}{GEN x, GEN y, ulong p}
4491
4492
\fun{GEN}{Flm_intersect_i}{GEN x, GEN y, GEN p} internal variant of
4493
\kbd{Flm\_intersect} but the result is only a generating set, not
4494
necessarily an $\F_p$-basis. It \emph{is} a basis if both $x$ and $y$
4495
have independent columns. It is not \kbd{gerepile}-clean either, but
4496
suitable for \kbd{gerepileupto}.
4497
4498
\fun{GEN}{Flm_transpose}{GEN x}
4499
4500
\fun{GEN}{Flm_hess}{GEN x, ulong p} upper Hessenberg form of $x$ over $\F_p$.
4501
4502
\subsec{\kbd{F2c} / \kbd{F2v}, \kbd{F2m}} An \kbd{F2v}~\kbd{v} is a
4503
\typ{VECSMALL} representing a vector over $\F_2$. Specifically \kbd{z[0]} is
4504
the usual codeword, \kbd{z[1]} is the number of components of $v$ and the
4505
coefficients are given by the bits of remaining words by increasing indices.
4506
4507
\fun{ulong}{F2v_coeff}{GEN x, long i} returns the coefficient $i\ge 1$ of $x$.
4508
4509
\fun{void}{F2v_clear}{GEN x, long i} sets the coefficient $i\ge 1$ of $x$ to
4510
$0$.
4511
4512
\fun{int}{F2v_equal0}{GEN x} returns $1$ if all entries are $0$, and return
4513
$0$ otherwise.
4514
4515
\fun{void}{F2v_flip}{GEN x, long i} adds $1$ to the coefficient $i\ge 1$ of $x$.
4516
4517
\fun{void}{F2v_set}{GEN x, long i} sets the coefficient $i\ge 1$ of $x$ to $1$.
4518
4519
\fun{void}{F2v_copy}{GEN x} returns a copy of $x$.
4520
4521
\fun{GEN}{F2v_slice}{GEN x, long a, long b} returns the \kbd{F2v} with
4522
entries $x[a]$, \dots, $x[b]$. Assumes $a \leq b$.
4523
4524
\fun{ulong}{F2m_coeff}{GEN x, long i, long j} returns the coefficient $(i,j)$
4525
of $x$.
4526
4527
\fun{void}{F2m_clear}{GEN x, long i, long j} sets the coefficient $(i,j)$ of $x$
4528
to $0$.
4529
4530
\fun{void}{F2m_flip}{GEN x, long i, long j} adds $1$ to the coefficient $(i,j)$
4531
of $x$.
4532
4533
\fun{void}{F2m_set}{GEN x, long i, long j} sets the coefficient $(i,j)$ of $x$
4534
to $1$.
4535
4536
\fun{GEN}{F2m_copy}{GEN x} returns a copy of $x$.
4537
4538
\fun{GEN}{F2m_transpose}{GEN x} returns the transpose of $x$.
4539
4540
\fun{GEN}{F2m_row}{GEN x, long j} returns the \kbd{F2v} which corresponds
4541
to the $j$-th row of the \kbd{F2m} $x$.
4542
4543
\fun{GEN}{F2m_rowslice}{GEN x, long a, long b} returns the \kbd{F2m} built
4544
from the $a$-th to $b$-th rows of the \kbd{F2m} $x$. Assumes $a \leq b$.
4545
4546
\fun{GEN}{F2m_F2c_mul}{GEN x, GEN y} multiplies \kbd{x} and \kbd{y} (assumed
4547
to have compatible dimensions).
4548
4549
\fun{GEN}{F2m_image}{GEN x} gives a subset of the columns of $x$ that generate
4550
the image of $x$.
4551
4552
\fun{GEN}{F2m_invimage}{GEN A, GEN B}
4553
4554
\fun{GEN}{F2m_F2c_invimage}{GEN A, GEN y}
4555
4556
\fun{GEN}{F2m_gauss}{GEN a, GEN b}
4557
as \kbd{gauss}, where $b$ is a \kbd{F2m}.
4558
4559
\fun{GEN}{F2m_F2c_gauss}{GEN a, GEN b}
4560
as \kbd{gauss}, where $b$ is a \kbd{F2c}.
4561
4562
4563
\fun{GEN}{F2m_indexrank}{GEN x} $x$ being a matrix of rank $r$, returns a
4564
vector with two \typ{VECSMALL} components $y$ and $z$ of length $r$ giving a
4565
list of rows and columns respectively (starting from 1) such that the extracted
4566
matrix obtained from these two vectors using \kbd{vecextract}$(x,y,z)$ is
4567
invertible.
4568
4569
\fun{GEN}{F2m_mul}{GEN x, GEN y} multiplies \kbd{x} and \kbd{y} (assumed to
4570
have compatible dimensions).
4571
4572
\fun{GEN}{F2m_powu}{GEN x, ulong n} computes $x^n$ where $x$ is a square
4573
\kbd{F2m}.
4574
4575
\fun{long}{F2m_rank}{GEN x} as \kbd{rank}.
4576
4577
\fun{long}{F2m_suppl}{GEN x} as \kbd{suppl}.
4578
4579
\fun{GEN}{matid_F2m}{long n} returns an \kbd{F2m} which is an $n \times n$
4580
identity matrix.
4581
4582
\fun{GEN}{zero_F2v}{long n} creates a \kbd{F2v} with \kbd{n} components set to
4583
$0$.
4584
4585
\fun{GEN}{const_F2v}{long n} creates a \kbd{F2v} with \kbd{n} components set to
4586
$1$.
4587
4588
\fun{GEN}{F2v_ei}{long n, long i} creates a \kbd{F2v} with \kbd{n} components
4589
set to $0$, but for the $i$-th one, which is set to $1$ ($i$-th vector in the
4590
canonical basis).
4591
4592
\fun{GEN}{zero_F2m}{long m, long n} creates a \kbd{Flm} with \kbd{m} x \kbd{n}
4593
components set to $0$. Note that the result allocates a
4594
\emph{single} column, so modifying an entry in one column modifies it in
4595
all columns.
4596
4597
\fun{GEN}{zero_F2m_copy}{long m, long n} creates a \kbd{F2m} with \kbd{m} x
4598
\kbd{n} components set to $0$.
4599
4600
\fun{GEN}{F2v_to_Flv}{GEN x}
4601
4602
\fun{GEN}{F2c_to_ZC}{GEN x}
4603
4604
\fun{GEN}{ZV_to_F2v}{GEN x}
4605
4606
\fun{GEN}{RgV_to_F2v}{GEN x}
4607
4608
\fun{GEN}{F2m_to_Flm}{GEN x}
4609
4610
\fun{GEN}{F2m_to_ZM}{GEN x}
4611
4612
\fun{GEN}{Flv_to_F2v}{GEN x}
4613
4614
\fun{GEN}{Flm_to_F2m}{GEN x}
4615
4616
\fun{GEN}{ZM_to_F2m}{GEN x}
4617
4618
\fun{GEN}{RgM_to_F2m}{GEN x}
4619
4620
\fun{void}{F2v_add_inplace}{GEN x, GEN y} replaces $x$ by $x+y$. It is
4621
allowed for $y$ to be shorter than $x$.
4622
4623
\fun{void}{F2v_and_inplace}{GEN x, GEN y} replaces $x$ by the term-by term product
4624
of $x$ and $y$ (which is the logical \kbd{and}). It is allowed for $y$ to be
4625
shorter than $x$.
4626
4627
\fun{void}{F2v_negimply_inplace}{GEN x, GEN y} replaces $x$ by the term-by term
4628
logical \kbd{and not} of $x$ and $y$. It is allowed for $y$ to be
4629
shorter than $x$.
4630
4631
\fun{void}{F2v_or_inplace}{GEN x, GEN y} replaces $x$ by the term-by term
4632
logical \kbd{or} of $x$ and $y$. It is allowed for $y$ to be
4633
shorter than $x$.
4634
4635
\fun{ulong}{F2v_hamming}{GEN x} returns the Hamming weight of \kbd{x}, that is
4636
the number of nonzero entries.
4637
4638
\fun{ulong}{F2m_det}{GEN x}
4639
4640
\fun{ulong}{F2m_det_sp}{GEN x}, as \kbd{F2m\_det}, in place (destroys~\kbd{x}).
4641
4642
\fun{GEN}{F2m_deplin}{GEN x}
4643
4644
\fun{ulong}{F2v_dotproduct}{GEN x, GEN y} returns the scalar product of \kbd{x}
4645
and \kbd{y}
4646
4647
\fun{GEN}{F2m_inv}{GEN x}
4648
4649
\fun{GEN}{F2m_ker}{GEN x}
4650
4651
\fun{GEN}{F2m_ker_sp}{GEN x, long deplin}, as \kbd{F2m\_ker} (if
4652
\kbd{deplin=0}) or \kbd{F2m\_deplin} (if \kbd{deplin=1}), in place
4653
(destroys~\kbd{x}).
4654
4655
\subsec{\kbd{F3c} / \kbd{F3v}, \kbd{F3m}} An \kbd{F3v}~\kbd{v} is a
4656
\typ{VECSMALL} representing a vector over $\F_3$. Specifically \kbd{z[0]} is
4657
the usual codeword, \kbd{z[1]} is the number of components of $v$ and the
4658
coefficients are given by pair of adjacent bits of remaining words by
4659
increasing indices, with the coding $00\mapsto 0, 01\mapsto 1, 10\mapsto 2$ and
4660
$11$ is undefined.
4661
4662
\fun{ulong}{F3v_coeff}{GEN x, long i} returns the coefficient $i\ge 1$ of $x$.
4663
4664
\fun{void}{F3v_clear}{GEN x, long i} sets the coefficient $i\ge 1$ of $x$ to
4665
$0$.
4666
4667
\fun{void}{F3v_set}{GEN x, long i, ulong n} sets the coefficient $i\ge 1$ of
4668
$x$ to $n<3$,
4669
4670
\fun{ulong}{F3m_coeff}{GEN x, long i, long j} returns the coefficient $(i,j)$
4671
of $x$.
4672
4673
\fun{void}{F3m_set}{GEN x, long i, long j, ulong n} sets the coefficient
4674
$(i,j)$ of $x$ to $n<3$.
4675
4676
\fun{GEN}{F3m_copy}{GEN x} returns a copy of $x$.
4677
4678
\fun{GEN}{F3m_transpose}{GEN x} returns the transpose of $x$.
4679
4680
\fun{GEN}{F3m_row}{GEN x, long j} returns the \kbd{F3v} which corresponds
4681
to the $j$-th row of the \kbd{F3m} $x$.
4682
4683
\fun{GEN}{F3m_ker}{GEN x}
4684
4685
\fun{GEN}{F3m_ker_sp}{GEN x, long deplin}, as \kbd{F3m\_ker} (if
4686
\kbd{deplin=0}) or \kbd{F2m\_deplin} (if \kbd{deplin=1}), in place
4687
(destroys~\kbd{x}).
4688
4689
\fun{GEN}{F3m_mul}{GEN x, GEN y} multiplies \kbd{x} and \kbd{y} (assumed to
4690
have compatible dimensions).
4691
4692
\fun{GEN}{zero_F3v}{long n} creates a \kbd{F3v} with \kbd{n} components set to
4693
$0$.
4694
4695
\fun{GEN}{zero_F2m_copy}{long m, long n} creates a \kbd{F3m} with \kbd{m} x
4696
\kbd{n} components set to $0$.
4697
4698
\fun{GEN}{F3v_to_Flv}{GEN x}
4699
4700
\fun{GEN}{ZV_to_F3v}{GEN x}
4701
4702
\fun{GEN}{RgV_to_F3v}{GEN x}
4703
4704
\fun{GEN}{F3m_to_Flm}{GEN x}
4705
4706
\fun{GEN}{F3m_to_ZM}{GEN x}
4707
4708
\fun{GEN}{Flv_to_F3v}{GEN x}
4709
4710
\fun{GEN}{Flm_to_F3m}{GEN x}
4711
4712
\fun{GEN}{ZM_to_F3m}{GEN x}
4713
4714
\fun{GEN}{RgM_to_F3m}{GEN x}
4715
4716
\subsec{\kbd{FlxqV}, \kbd{FlxqC}, \kbd{FlxqM}}
4717
See \kbd{FqV}, \kbd{FqC}, \kbd{FqM} operations.
4718
4719
\fun{GEN}{FlxqV_dotproduct}{GEN x, GEN y, GEN T, ulong p} as
4720
\kbd{FpV\_dotproduct}.
4721
4722
\fun{GEN}{FlxM_Flx_add_shallow}{GEN x, GEN y, ulong p} as
4723
\kbd{RgM\_Rg\_add\_shallow}.
4724
4725
\fun{GEN}{FlxqC_Flxq_mul}{GEN x, GEN y, GEN T, ulong p}
4726
4727
\fun{GEN}{FlxqM_Flxq_mul}{GEN x, GEN y, GEN T, ulong p}
4728
4729
\fun{GEN}{FlxqM_FlxqC_gauss}{GEN a, GEN b, GEN T, ulong p}
4730
4731
\fun{GEN}{FlxqM_FlxqC_invimage}{GEN a, GEN b, GEN T, ulong p}
4732
4733
\fun{GEN}{FlxqM_FlxqC_mul}{GEN a, GEN b, GEN T, ulong p}
4734
4735
\fun{GEN}{FlxqM_deplin}{GEN x, GEN T, ulong p}
4736
4737
\fun{GEN}{FlxqM_det}{GEN x, GEN T, ulong p}
4738
4739
\fun{GEN}{FlxqM_gauss}{GEN a, GEN b, GEN T, ulong p}
4740
4741
\fun{GEN}{FlxqM_image}{GEN x, GEN T, ulong p}
4742
4743
\fun{GEN}{FlxqM_indexrank}{GEN x, GEN T, ulong p}
4744
4745
\fun{GEN}{FlxqM_inv}{GEN x, GEN T, ulong p}
4746
4747
\fun{GEN}{FlxqM_invimage}{GEN a, GEN b, GEN T, ulong p}
4748
4749
\fun{GEN}{FlxqM_ker}{GEN x, GEN T, ulong p}
4750
4751
\fun{GEN}{FlxqM_mul}{GEN a, GEN b, GEN T, ulong p}
4752
4753
\fun{long}{FlxqM_rank}{GEN x, GEN T, ulong p}
4754
4755
\fun{GEN}{FlxqM_suppl}{GEN x, GEN T, ulong p}
4756
4757
\fun{GEN}{matid_FlxqM}{long n, GEN T, ulong p}
4758
4759
\subsec{\kbd{FpX}} Let \kbd{p} an understood \typ{INT}, to be given in
4760
the function arguments; in practice \kbd{p} is not assumed to be prime, but
4761
be wary. Recall than an \kbd{Fp} object is a \typ{INT}, preferably belonging
4762
to $[0, \kbd{p}-1]$; an \kbd{FpX} is a \typ{POL} in a fixed variable whose
4763
coefficients are \kbd{Fp} objects. Unless mentioned otherwise, all outputs in
4764
this section are \kbd{FpX}s. All operations are understood to take place in
4765
$(\Z/\kbd{p}\Z)[X]$.
4766
4767
\subsubsec{Conversions} In what follows \kbd{p} is always a \typ{INT},
4768
not necessarily prime.
4769
4770
\fun{int}{RgX_is_FpX}{GEN z, GEN *p}, \kbd{z} a \typ{POL},
4771
checks if it can be mapped to a \kbd{FpX}, by checking \kbd{Rg\_is\_Fp}
4772
coefficientwise.
4773
4774
\fun{GEN}{RgX_to_FpX}{GEN z, GEN p}, \kbd{z} a \typ{POL}, returns the
4775
\kbd{FpX} obtained by applying \kbd{Rg\_to\_Fp} coefficientwise.
4776
4777
\fun{GEN}{FpX_red}{GEN z, GEN p}, \kbd{z} a \kbd{ZX}, returns \kbd{lift(z *
4778
Mod(1,p))}, normalized.
4779
4780
\fun{GEN}{FpXV_red}{GEN z, GEN p}, \kbd{z} a \typ{VEC} of \kbd{ZX}. Applies
4781
\kbd{FpX\_red} componentwise and returns the result (and we obtain a vector
4782
of \kbd{FpX}s).
4783
4784
\fun{GEN}{FpXT_red}{GEN z, GEN p}, \kbd{z} a tree of \kbd{ZX}. Applies
4785
\kbd{FpX\_red} to each leaf and returns the result (and we obtain a tree
4786
of \kbd{FpX}s).
4787
4788
\subsubsec{Basic operations} In what follows \kbd{p} is always a \typ{INT},
4789
not necessarily prime.
4790
4791
\noindent Now, except for \kbd{p}, the operands and outputs are all \kbd{FpX}
4792
objects. Results are undefined on other inputs.
4793
4794
\fun{GEN}{FpX_add}{GEN x,GEN y, GEN p} adds \kbd{x} and \kbd{y}.
4795
4796
\fun{GEN}{FpX_neg}{GEN x,GEN p} returns $-\kbd{x}$, the components are
4797
between $0$ and $p$ if this is the case for the components of $x$.
4798
4799
\fun{GEN}{FpX_renormalize}{GEN x, long l}, as \kbd{normalizepol}, where
4800
$\kbd{l} = \kbd{lg(x)}$, in place.
4801
4802
\fun{GEN}{FpX_sub}{GEN x,GEN y,GEN p} returns $x-y$.
4803
4804
\fun{GEN}{FpX_halve}{GEN x, GEN p} returns $z$ such that $2\*z = x$ modulo
4805
$p$ assuming such $z$ exists.
4806
4807
\fun{GEN}{FpX_mul}{GEN x,GEN y,GEN p} returns $x\*y$.
4808
4809
\fun{GEN}{FpX_mulspec}{GEN a, GEN b, GEN p, long na, long nb}
4810
see \kbd{ZX\_mulspec}
4811
4812
\fun{GEN}{FpX_sqr}{GEN x,GEN p} returns $\kbd{x}^2$.
4813
4814
\fun{GEN}{FpX_powu}{GEN x, ulong n, GEN p} returns $x^n$.
4815
4816
\fun{GEN}{FpX_convol}{GEN x, GEN y, GEN p} return the-term by-term product of $x$
4817
and $y$.
4818
4819
\fun{GEN}{FpX_divrem}{GEN x, GEN y, GEN p, GEN *pr} returns the quotient
4820
of \kbd{x} by \kbd{y}, and sets \kbd{pr} to the remainder.
4821
4822
\fun{GEN}{FpX_div}{GEN x, GEN y, GEN p} returns the quotient of \kbd{x} by
4823
\kbd{y}.
4824
4825
\fun{GEN}{FpX_div_by_X_x}{GEN A, GEN a, GEN p, GEN *r} returns the
4826
quotient of the \kbd{FpX}~\kbd{A} by $(X - \kbd{a})$, and sets \kbd{r} to the
4827
remainder $\kbd{A}(\kbd{a})$.
4828
4829
\fun{GEN}{FpX_rem}{GEN x, GEN y, GEN p} returns the remainder \kbd{x} mod
4830
\kbd{y}.
4831
4832
\fun{long}{FpX_valrem}{GEN x, GEN t, GEN p, GEN *r} The arguments \kbd{x} and
4833
\kbd{e} being nonzero \kbd{FpX} returns the highest exponent $e$ such that
4834
$\kbd{t}^{e}$ divides~\kbd{x}. The quotient $\kbd{x}/\kbd{t}^{e}$ is returned
4835
in~\kbd{*r}. In particular, if \kbd{t} is irreducible, this returns the
4836
valuation at \kbd{t} of~\kbd{x}, and \kbd{*r} is the prime-to-\kbd{t} part
4837
of~\kbd{x}.
4838
4839
\fun{GEN}{FpX_deriv}{GEN x, GEN p} returns the derivative of \kbd{x}.
4840
This function is not memory-clean, but nevertheless suitable for
4841
\kbd{gerepileupto}.
4842
4843
\fun{GEN}{FpX_integ}{GEN x, GEN p} returns the primitive of \kbd{x} whose
4844
constant term is $0$.
4845
4846
\fun{GEN}{FpX_digits}{GEN x, GEN B, GEN p} returns a vector of \kbd{FpX}
4847
$[c_0,\ldots,c_n]$ of degree less than the degree of $B$ and such that
4848
$x=\sum_{i=0}^{n}{c_i\*B^i}$.
4849
4850
\fun{GEN}{FpXV_FpX_fromdigits}{GEN v, GEN B, GEN p} where $v=[c_0,\ldots,c_n]$
4851
is a vector of \kbd{FpX}, returns $\sum_{i=0}^{n}{c_i\*B^i}$.
4852
4853
\fun{GEN}{FpX_translate}{GEN P, GEN c, GEN p} let $c$ be an \kbd{Fp} and let
4854
$P$ be an \kbd{FpX}; returns the translated \kbd{FpX} of $P(X+c)$.
4855
4856
\fun{GEN}{FpX_gcd}{GEN x, GEN y, GEN p} returns a (not necessarily monic)
4857
greatest common divisor of $x$ and $y$.
4858
4859
\fun{GEN}{FpX_halfgcd}{GEN x, GEN y, GEN p} returns a two-by-two \kbd{FpXM}
4860
$M$ with determinant $\pm 1$ such that the image $(a,b)$ of $(x,y)$ by $M$
4861
has the property that $\deg a \geq {\deg x \over 2} > \deg b$.
4862
4863
\fun{GEN}{FpX_extgcd}{GEN x, GEN y, GEN p, GEN *u, GEN *v} returns
4864
$d = \text{GCD}(\kbd{x},\kbd{y})$ (not necessarily monic), and sets \kbd{*u},
4865
\kbd{*v} to the Bezout coefficients such that $\kbd{*ux} + \kbd{*vy} = d$.
4866
If \kbd{*u} is set to \kbd{NULL}, it is not computed which is a bit faster.
4867
This is useful when computing the inverse of $y$ modulo $x$.
4868
4869
\fun{GEN}{FpX_center}{GEN z, GEN p, GEN pov2} returns the polynomial whose
4870
coefficient belong to the symmetric residue system. Assumes the coefficients
4871
already belong to $]-p/2, p[$ and that \kbd{pov2} is \kbd{shifti(p,-1)}.
4872
4873
\fun{GEN}{FpX_center_i}{GEN z, GEN p, GEN pov2} internal variant of
4874
\tet{FpX_center}, not \kbd{gerepile}-safe.
4875
4876
\fun{GEN}{FpX_Frobenius}{GEN T, GEN p} returns $X^{p}\pmod{T(X)}$.
4877
4878
\fun{GEN}{FpX_matFrobenius}{GEN T, GEN p} returns the matrix of the
4879
Frobenius automorphism $x\mapsto x^p$ over the power basis of $\F_p[X]/(T)$.
4880
4881
\subsubsec{Mixed operations}
4882
The following functions implement arithmetic operations between \kbd{FpX}
4883
and \kbd{Fp} operands, the result being of type \kbd{FpX}. The integer
4884
\kbd{p} need not be prime.
4885
4886
\fun{GEN}{Z_to_FpX}{GEN x, GEN p, long v} converts a \typ{INT} to a scalar
4887
polynomial in variable $v$, reduced modulo $p$.
4888
4889
\fun{GEN}{FpX_Fp_add}{GEN y, GEN x, GEN p} add the \kbd{Fp}~\kbd{x} to the
4890
\kbd{FpX}~\kbd{y}.
4891
4892
\fun{GEN}{FpX_Fp_add_shallow}{GEN y, GEN x, GEN p} add the \kbd{Fp}~\kbd{x}
4893
to the \kbd{FpX}~\kbd{y}, using a shallow copy (result not suitable for
4894
\kbd{gerepileupto})
4895
4896
\fun{GEN}{FpX_Fp_sub}{GEN y, GEN x, GEN p} subtract the \kbd{Fp}~\kbd{x} from
4897
the \kbd{FpX}~\kbd{y}.
4898
4899
\fun{GEN}{FpX_Fp_sub_shallow}{GEN y, GEN x, GEN p} subtract the
4900
\kbd{Fp}~\kbd{x} from the \kbd{FpX}~\kbd{y}, using a shallow copy (result not
4901
suitable for \kbd{gerepileupto})
4902
4903
\fun{GEN}{Fp_FpX_sub}{GEN x,GEN y,GEN p} returns $x - y$, where $x$ is
4904
a \typ{INT} and $y$ an \kbd{FpX}.
4905
4906
\fun{GEN}{FpX_Fp_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{FpX}~\kbd{x}
4907
by the \kbd{Fp}~\kbd{y}.
4908
4909
\fun{GEN}{FpX_Fp_mulspec}{GEN x, GEN y, GEN p, long lx} see \kbd{ZX\_mulspec}
4910
4911
\fun{GEN}{FpX_mulu}{GEN x, ulong y, GEN p} multiplies the \kbd{FpX}~\kbd{x}
4912
by \kbd{y}.
4913
4914
\fun{GEN}{FpX_Fp_mul_to_monic}{GEN y,GEN x,GEN p} returns $y\*x$ assuming the
4915
result is monic of the same degree as $y$ (in particular $x\neq 0$).
4916
4917
\fun{GEN}{FpX_Fp_div}{GEN x, GEN y, GEN p} divides the \kbd{FpX}~\kbd{x}
4918
by the \kbd{Fp}~\kbd{y}.
4919
4920
\fun{GEN}{FpX_divu}{GEN x, ulong y, GEN p} divides the \kbd{FpX}~\kbd{x}
4921
by \kbd{y}.
4922
4923
\subsubsec{Miscellaneous operations}
4924
4925
\fun{GEN}{FpX_normalize}{GEN z, GEN p} divides the \kbd{FpX}~\kbd{z} by its
4926
leading coefficient. If the latter is~$1$, \kbd{z} itself is returned, not a
4927
copy. If not, the inverse remains uncollected on the stack.
4928
4929
\fun{GEN}{FpX_invBarrett}{GEN T, GEN p}, returns the Barrett inverse
4930
$M$ of $T$ defined by $M(x)\*x^n\*T(1/x)\equiv 1\pmod{x^{n-1}}$ where $n$ is
4931
the degree of $T$.
4932
4933
\fun{GEN}{FpX_rescale}{GEN P, GEN h, GEN p} returns $h^{\deg(P)} P(x/h)$.
4934
\kbd{P} is an \kbd{FpX} and \kbd{h} is a nonzero \kbd{Fp} (the routine would
4935
work with any nonzero \typ{INT} but is not efficient in this case).
4936
Neither memory-clean nor suitable for \kbd{gerepileupto}.
4937
4938
\fun{GEN}{FpX_eval}{GEN x, GEN y, GEN p} evaluates the \kbd{FpX}~\kbd{x}
4939
at the \kbd{Fp}~\kbd{y}. The result is an~\kbd{Fp}.
4940
4941
\fun{GEN}{FpX_FpV_multieval}{GEN P, GEN v, GEN p} returns the vector
4942
$[P(v[1]),\ldots,P(v[n])]$ as a \kbd{FpV}.
4943
4944
\fun{GEN}{FpX_dotproduct}{GEN x, GEN y, GEN p} return the scalar product
4945
$\sum_{i\geq 0} x_i\*y_i$ of the coefficients of $x$ and $y$.
4946
4947
\fun{GEN}{FpXV_FpC_mul}{GEN V, GEN W, GEN p} multiplies a nonempty line
4948
vector of\kbd{FpX} by a column vector of \kbd{Fp} of compatible dimensions.
4949
The result is an~\kbd{FpX}.
4950
4951
\fun{GEN}{FpXV_prod}{GEN V, GEN p}, \kbd{V} being a vector of \kbd{FpX},
4952
returns their product.
4953
4954
\fun{GEN}{FpXV_factorback}{GEN L, GEN e, GEN p, long v} returns
4955
$\prod_i L_i^{e_i}$ where $L$ is a vector of \kbd{FpX}s in the variable \kbd{v}
4956
and $e$ a vector of non-negative \typ{INT}s or a \typ{VECSMALL}.
4957
4958
\fun{GEN}{FpV_roots_to_pol}{GEN V, GEN p, long v}, \kbd{V} being a vector
4959
of \kbd{INT}s, returns the monic \kbd{FpX}
4960
$\prod_i (\kbd{pol\_x[v]} - \kbd{V[i]})$.
4961
4962
\fun{GEN}{FpX_chinese_coprime}{GEN x, GEN y, GEN Tx, GEN Ty, GEN Tz, GEN p}:
4963
returns an \kbd{FpX}, congruent to \kbd{x} mod \kbd{Tx} and to \kbd{y} mod
4964
\kbd{Ty}. Assumes \kbd{Tx} and \kbd{Ty} are coprime, and \kbd{Tz = Tx * Ty}
4965
or \kbd{NULL} (in which case it is computed within).
4966
4967
\fun{GEN}{FpV_polint}{GEN x, GEN y, GEN p, long v} returns the \kbd{FpX}
4968
interpolation polynomial with value \kbd{y[i]} at \kbd{x[i]}. Assumes lengths
4969
are the same, components are \typ{INT}s, and the \kbd{x[i]} are distinct
4970
modulo \kbd{p}.
4971
4972
\fun{GEN}{FpV_FpM_polint}{GEN x, GEN V, GEN p, long v} equivalent (but
4973
faster) to applying \kbd{FpV\_polint(x,$\ldots$)} to all the elements of the
4974
vector $V$ (thus, returns a \kbd{FpXV}).
4975
4976
\fun{GEN}{FpX_FpXV_multirem}{GEN A, GEN P, GEN p}
4977
given a \kbd{FpX} $A$ and a vector $P$ of pairwise coprime \kbd{FpX} of length
4978
$n\ge 1$, return a vector $B$ of the same length such that $B[i] =
4979
A\pmod{P[i]}$ and $B[i]$ of minimal degree for all $1\leq i\leq n$.
4980
4981
\fun{GEN}{FpXV_chinese}{GEN A, GEN P, GEN p, GEN *pM}
4982
let $P$ be a vector of pairwise coprime \kbd{FpX}, let $A$ be a vector of
4983
\kbd{FpX} of the same length $n\ge 1$ and let $M$ be the product of the
4984
elements of $P$. Returns a \kbd{FpX} of minimal degree congruent to $A[i]$ mod
4985
$P[i]$ for all $1\leq i\leq n$.
4986
If \kbd{pM} is not \kbd{NULL}, set \kbd{*pM} to $M$.
4987
4988
\fun{GEN}{FpV_invVandermonde}{GEN L, GEN d, GEN p} $L$ being a \kbd{FpV}
4989
of length $n$, return the inverse $M$ of the Vandermonde matrix attached to
4990
the elements of $L$, eventually multiplied by \kbd{d} if it is not
4991
\kbd{NULL}. If $A$ is a \kbd{FpV} and $B=M\*A$, then the polynomial
4992
$P=\sum_{i=1}^n B[i]\*X^{i-1}$ verifies $P(L[i])=d\*A[i]$ for
4993
$1 \leq i \leq n$.
4994
4995
\fun{int}{FpX_is_squarefree}{GEN f, GEN p} returns $1$ if the
4996
\kbd{FpX}~\kbd{f} is squarefree, $0$ otherwise.
4997
4998
\fun{int}{FpX_is_irred}{GEN f, GEN p} returns $1$ if the \kbd{FpX}~\kbd{f}
4999
is irreducible, $0$ otherwise. Assumes that \kbd{p} is prime. If~\kbd{f} has
5000
few factors, \kbd{FpX\_nbfact(f,p) == 1} is much faster.
5001
5002
\fun{int}{FpX_is_totally_split}{GEN f, GEN p} returns $1$ if the
5003
\kbd{FpX}~\kbd{f} splits into a product of distinct linear factors, $0$
5004
otherwise. Assumes that \kbd{p} is prime. The $0$ polynomial is not
5005
totally split.
5006
5007
\fun{long}{FpX_ispower}{GEN f, ulong k, GEN p, GEN *pt}
5008
return $1$ if the \kbd{FpX} $f$ is a $k$-th power, $0$ otherwise.
5009
If \kbd{pt} is not \kbd{NULL}, set it to $g$ such that $g^k = f$.
5010
5011
\fun{GEN}{FpX_factor}{GEN f, GEN p}, factors the \kbd{FpX}~\kbd{f}. Assumes
5012
that \kbd{p} is prime. The returned value \kbd{v} is a \typ{VEC} with two
5013
components: \kbd{v[1]} is a vector of distinct irreducible (\kbd{FpX})
5014
factors, and \kbd{v[2]} is a \typ{VECSMALL} of corresponding exponents. The
5015
order of the factors is deterministic (the computation is not).
5016
5017
\fun{GEN}{FpX_factor_squarefree}{GEN f, GEN p} returns the squarefree
5018
factorization of $f$ modulo $p$. This is a vector $[u_1,\dots,u_k]$
5019
of squarefree and pairwise coprime \kbd{FpX} such that $u_k \neq 1$ and $f =
5020
\prod u_i^i$. The other~$u_i$ may equal~$1$.
5021
Shallow function.
5022
5023
\fun{GEN}{FpX_ddf}{GEN f, GEN p} assuming that $f$ is squarefree,
5024
returns the distinct degree factorization of $f$ modulo $p$.
5025
The returned value \kbd{v} is a \typ{VEC} with two
5026
components: \kbd{F=v[1]} is a vector of (\kbd{FpX})
5027
factors, and \kbd{E=v[2]} is a \typ{VECSMALL}, such that
5028
$f$ is equal to the product of the \kbd{F[i]} and each \kbd{F[i]}
5029
is a product of irreducible factors of degree \kbd{E[i]}.
5030
5031
\fun{long}{FpX_ddf_degree}{GEN f, GEN XP, GEN p} assuming that $f$ is squarefree
5032
and that all its factors have the same degree, return the common degree,
5033
where \kbd{XP} is \kbd{FpX\_Frobenius(f, p)}.
5034
5035
\fun{long}{FpX_nbfact}{GEN f, GEN p}, assuming the \kbd{FpX}~f is squarefree,
5036
returns the number of its irreducible factors. Assumes that \kbd{p} is prime.
5037
5038
\fun{long}{FpX_nbfact_Frobenius}{GEN f, GEN XP, GEN p}, as
5039
\kbd{FpX\_nbfact(f, p)} but faster,
5040
where \kbd{XP} is \kbd{FpX\_Frobenius(f, p)}.
5041
5042
\fun{GEN}{FpX_degfact}{GEN f, GEN p}, as \kbd{FpX\_factor}, but the
5043
degrees of the irreducible factors are returned instead of the factors
5044
themselves (as a \typ{VECSMALL}). Assumes that \kbd{p} is prime.
5045
5046
\fun{long}{FpX_nbroots}{GEN f, GEN p} returns the number of distinct
5047
roots in \kbd{\Z/p\Z} of the \kbd{FpX}~\kbd{f}. Assumes that \kbd{p} is prime.
5048
5049
\fun{GEN}{FpX_oneroot}{GEN f, GEN p} returns one root in \kbd{\Z/p\Z} of
5050
the \kbd{FpX}~\kbd{f}. Return \kbd{NULL} if no root exists.
5051
Assumes that \kbd{p} is prime.
5052
5053
\fun{GEN}{FpX_oneroot_split}{GEN f, GEN p} as \kbd{FpX\_oneroot}.
5054
Faster when $f$ is close to be totally split.
5055
5056
\fun{GEN}{FpX_roots}{GEN f, GEN p} returns the roots in \kbd{\Z/p\Z} of
5057
the \kbd{FpX}~\kbd{f} (without multiplicity, as a vector of \kbd{Fp}s).
5058
Assumes that \kbd{p} is prime.
5059
5060
\fun{GEN}{FpX_split_part}{GEN f, GEN p} returns the largest totally split
5061
squarefree factor of $f$.
5062
5063
\fun{GEN}{random_FpX}{long d, long v, GEN p} returns a random \kbd{FpX}
5064
in variable \kbd{v}, of degree less than~\kbd{d}.
5065
5066
\fun{GEN}{FpX_resultant}{GEN x, GEN y, GEN p} returns the resultant
5067
of \kbd{x} and \kbd{y}, both \kbd{FpX}. The result is a \typ{INT}
5068
belonging to $[0,p-1]$.
5069
5070
\fun{GEN}{FpX_disc}{GEN x, GEN p} returns the discriminant
5071
of the \kbd{FpX} \kbd{x}. The result is a \typ{INT} belonging to $[0,p-1]$.
5072
5073
\fun{GEN}{FpX_FpXY_resultant}{GEN a, GEN b, GEN p}, \kbd{a} a \typ{POL} of
5074
\typ{INT}s (say in variable $X$), \kbd{b} a \typ{POL} (say in variable $X$)
5075
whose coefficients are either \typ{POL}s in $\Z[Y]$ or \typ{INT}s.
5076
Returns $\text{Res}_X(a, b)$ in $\F_p[Y]$ as an \kbd{FpY}. The function
5077
assumes that $X$ has lower priority than $Y$.
5078
5079
\fun{GEN}{FpX_Newton}{GEN x, long n, GEN p} return
5080
$\sum{i=0}^{n-1} \pi_i\*X^i$ where $\pi_i$ is the sum of the $i$th-power
5081
of the roots of $x$ in an algebraic closure.
5082
5083
\fun{GEN}{FpX_fromNewton}{GEN x, GEN p} recover a polynomial from it
5084
s Newton sums given by the coefficients of $x$.
5085
This function assumes that $p$ and the accuracy of $x$ as a \kbd{FpXn} is
5086
larger than the degree of the solution.
5087
5088
\fun{GEN}{FpX_Laplace}{GEN x, GEN p} return
5089
$\sum_{i=0}^{n-1} x_i\*i!\*X^i$.
5090
5091
\fun{GEN}{FpX_invLaplace}{GEN x, GEN p} return
5092
$\sum_{i=0}^{n-1} x_i/{i!}\*X^i$.
5093
5094
\subsec{\kbd{FpXQ}, \kbd{Fq}} Let \kbd{p} a \typ{INT} and \kbd{T} an
5095
\kbd{FpX} for \kbd{p}, both to be given in the function arguments; an \kbd{FpXQ}
5096
object is an \kbd{FpX} whose degree is strictly less than the degree of
5097
\kbd{T}. An \kbd{Fq} is either an \kbd{FpXQ} or an \kbd{Fp}. Both represent
5098
a class in $(\Z/\kbd{p}\Z)[X] / (T)$, in which all operations below take
5099
place. In addition, \kbd{Fq} routines also allow $\kbd{T} = \kbd{NULL}$, in
5100
which case no reduction mod \kbd{T} is performed on the result.
5101
5102
For efficiency, the routines in this section may leave small unused objects
5103
behind on the stack (their output is still suitable for \kbd{gerepileupto}).
5104
Besides \kbd{T} and \kbd{p}, arguments are either \kbd{FpXQ} or \kbd{Fq}
5105
depending on the function name. (All \kbd{Fq} routines accept \kbd{FpXQ}s by
5106
definition, not the other way round.)
5107
5108
\subsubsec{Preconditioned reduction}
5109
5110
For faster reduction, the modulus $T$ can be replaced by an extended modulus
5111
in all \kbd{FpXQ}- and \kbd{Fq}-classes functions, and in \kbd{FpX\_rem} and
5112
\kbd{FpX\_divrem}. An extended modulus(\kbd{FpXT}, which is a tree whose leaves are \kbd{FpX})In
5113
current implementation, an extended modulus is either a plain modulus (an
5114
\kbd{FpX}) or a pair of polynomials, one being the plain modulus $T$ and the
5115
other being \tet{FpX_invBarret}$(T,p)$.
5116
5117
\fun{GEN}{FpX_get_red}{GEN T, GEN p} returns the extended modulus \kbd{eT}.
5118
5119
To write code that works both with plain and extended moduli, the following
5120
accessors are defined:
5121
5122
\fun{GEN}{get_FpX_mod}{GEN eT} returns the underlying modulus $T$.
5123
5124
\fun{GEN}{get_FpX_var}{GEN eT} returns the variable number \kbd{varn}$(T)$.
5125
5126
\fun{GEN}{get_FpX_degree}{GEN eT} returns the degree \kbd{degpol}$(T)$.
5127
5128
\subsubsec{Conversions}
5129
5130
\fun{int}{ff_parse_Tp}{GEN Tp, GEN *T, GEN *p, long red} \kbd{Tp} is either
5131
a prime number $p$ or a \typ{VEC} with 2 entries $T$ (an irreducible
5132
polynomial mod $p$) and $p$ (a prime number). Sets \kbd{*p} and \kbd{*T}
5133
to the corresponding \kbd{GEN}s (\kbd{NULL} if undefined). If \kbd{red}
5134
is nonzero, normalize \kbd{*T} as an \kbd{FpX}; on the other hand,
5135
to initialize a $p$-adic function, set \kbd{red} to $0$ and \kbd{*T} is left
5136
as is and must be a \kbd{ZX} to start with. Return $1$ on success, and $0$ on
5137
failure. This helper routine is used by GP functions such as \kbd{factormod}
5138
where a single user argument defines a finite field. \typ{FFELT} is not
5139
supported.
5140
5141
\fun{GEN}{Rg_is_FpXQ}{GEN z, GEN *T, GEN *p}, checks if \kbd{z} is a \kbd{GEN}
5142
which can be mapped to $\F_p[X]/(T)$: anything for which \kbd{Rg\_is\_Fp} return
5143
$1$, a \typ{POL} for which \kbd{RgX\_to\_FpX} return $1$, a \typ{POLMOD}
5144
whose modulus is equal to \kbd{*T} if \kbd{*T} is not \kbd{NULL} (once mapped
5145
to a \kbd{FpX}), or a \typ{FFELT} $z$ with the same definition field as \kbd{*T}
5146
if \kbd{*T} is not \kbd{NULL} and is a \typ{FFELT}.
5147
5148
If an integer modulus is found it is put in \kbd{*p}, else \kbd{*p} is left
5149
unchanged. If a polynomial modulus is found it is put in \kbd{*T},
5150
if a \typ{FFELT} $z$ is found, $z$ is put in \kbd{*T}, else
5151
\kbd{*T} is left unchanged.
5152
5153
\fun{int}{RgX_is_FpXQX}{GEN z, GEN *T, GEN *p}, \kbd{z} a \typ{POL},
5154
checks if it can be mapped to a \kbd{FpXQX}, by checking \kbd{Rg\_is\_FpXQ}
5155
coefficientwise.
5156
5157
\fun{GEN}{Rg_to_FpXQ}{GEN z, GEN T, GEN p}, \kbd{z} a \kbd{GEN} which can be
5158
mapped to $\F_p[X]/(T)$: anything \kbd{Rg\_to\_Fp} can be applied to,
5159
a \typ{POL} to which \kbd{RgX\_to\_FpX} can be applied to, a \typ{POLMOD}
5160
whose modulus is divisible by $T$ (once mapped to a \kbd{FpX}), a suitable
5161
\typ{RFRAC}. Returns \kbd{z} as an \kbd{FpXQ}, normalized.
5162
5163
\fun{GEN}{Rg_to_Fq}{GEN z, GEN T, GEN p}, applies \kbd{Rg\_to\_Fp} if $T$
5164
is \kbd{NULL} and \kbd{Rg\_to\_FpXQ} otherwise.
5165
5166
\fun{GEN}{RgX_to_FpXQX}{GEN z, GEN T, GEN p}, \kbd{z} a \typ{POL}, returns the
5167
\kbd{FpXQ} obtained by applying \kbd{Rg\_to\_FpXQ} coefficientwise.
5168
5169
\fun{GEN}{RgX_to_FqX}{GEN z, GEN T, GEN p}: let \kbd{z} be a \typ{POL};
5170
returns the \kbd{FqX} obtained by applying \kbd{Rg\_to\_Fq} coefficientwise.
5171
5172
\fun{GEN}{Fq_to_FpXQ}{GEN z, GEN T, GEN p /*unused*/}
5173
if $z$ is a \typ{INT}, convert it to a constant polynomial in the variable of
5174
$T$, otherwise return $z$ (shallow function).
5175
5176
\fun{GEN}{Fq_red}{GEN x, GEN T, GEN p}, \kbd{x} a \kbd{ZX} or \typ{INT},
5177
reduce it to an \kbd{Fq} ($\kbd{T} = \kbd{NULL}$ is allowed iff \kbd{x} is a
5178
\typ{INT}).
5179
5180
\fun{GEN}{FqX_red}{GEN x, GEN T, GEN p}, \kbd{x} a \typ{POL}
5181
whose coefficients are \kbd{ZX}s or \typ{INT}s, reduce them to \kbd{Fq}s. (If
5182
$\kbd{T} = \kbd{NULL}$, as \kbd{FpXX\_red(x, p)}.)
5183
5184
\fun{GEN}{FqV_red}{GEN x, GEN T, GEN p}, \kbd{x} a vector of \kbd{ZX}s or
5185
\typ{INT}s, reduce them to \kbd{Fq}s. (If $\kbd{T} = \kbd{NULL}$, only
5186
reduce components mod \kbd{p} to \kbd{FpX}s or \kbd{Fp}s.)
5187
5188
\fun{GEN}{FpXQ_red}{GEN x, GEN T,GEN p} \kbd{x} a \typ{POL}
5189
whose coefficients are \typ{INT}s, reduce them to \kbd{FpXQ}s.
5190
5191
\subsec{\kbd{FpXQ}}
5192
5193
\fun{GEN}{FpXQ_add}{GEN x, GEN y, GEN T,GEN p}
5194
5195
\fun{GEN}{FpXQ_sub}{GEN x, GEN y, GEN T,GEN p}
5196
5197
\fun{GEN}{FpXQ_mul}{GEN x, GEN y, GEN T,GEN p}
5198
5199
\fun{GEN}{FpXQ_sqr}{GEN x, GEN T, GEN p}
5200
5201
\fun{GEN}{FpXQ_div}{GEN x, GEN y, GEN T,GEN p}
5202
5203
\fun{GEN}{FpXQ_inv}{GEN x, GEN T, GEN p} computes the inverse of \kbd{x}
5204
5205
\fun{GEN}{FpXQ_invsafe}{GEN x,GEN T,GEN p}, as \kbd{FpXQ\_inv}, returning
5206
\kbd{NULL} if \kbd{x} is not invertible.
5207
5208
\fun{GEN}{FpXQ_pow}{GEN x, GEN n, GEN T, GEN p} computes $\kbd{x}^\kbd{n}$.
5209
5210
\fun{GEN}{FpXQ_powu}{GEN x, ulong n, GEN T, GEN p} computes $\kbd{x}^\kbd{n}$
5211
for small $n$.
5212
5213
In the following three functions the integer parameter \kbd{ord} can be given
5214
either as a positive \typ{INT} $N$, or as its factorization matrix $\var{faN}$,
5215
or as a pair $[N,\var{faN}]$. The parameter may be omitted by setting it to
5216
\kbd{NULL} (the value is then $p^d-1$, $d = \deg T$).
5217
5218
\fun{GEN}{FpXQ_log}{GEN a, GEN g, GEN ord, GEN T, GEN p} Let \kbd{g} be of
5219
order dividing \kbd{ord} in the finite field $\F_p[X]/(T)$, return $e$ such that
5220
$a^e=g$. If $e$ does not exists, the result is undefined. Assumes
5221
that \kbd{T} is irreducible mod \kbd{p}.
5222
5223
\fun{GEN}{Fp_FpXQ_log}{GEN a, GEN g, GEN ord, GEN T, GEN p} As
5224
\kbd{FpXQ\_log}, \kbd{a} being a \kbd{Fp}.
5225
5226
\fun{GEN}{FpXQ_order}{GEN a, GEN ord, GEN T, GEN p} returns the order of the
5227
\kbd{FpXQ} \kbd{a}. Assume that \kbd{ord} is a multiple of the order of
5228
\kbd{a}. Assume that \kbd{T} is irreducible mod \kbd{p}.
5229
5230
\fun{int}{FpXQ_issquare}{GEN x, GEN T, GEN p} returns $1$ if $x$ is a square
5231
and $0$ otherwise. Assumes that \kbd{T} is irreducible mod \kbd{p}.
5232
5233
\fun{GEN}{FpXQ_sqrt}{GEN x, GEN T, GEN p} returns a square root of \kbd{x}.
5234
Return \kbd{NULL} if \kbd{x} is not a square.
5235
5236
\fun{GEN}{FpXQ_sqrtn}{GEN x, GEN n, GEN T, GEN p, GEN *zn}
5237
Let $T$be irreducible mod $p$ and $q = p^{\deg T}$; returns \kbd{NULL} if $a$
5238
is not an $n$-th power residue mod $p$. Otherwise, returns an $n$-th root of
5239
$a$; if \kbd{zn} is not \kbd{NULL} set it to a primitive $m$-th root of $1$
5240
in $\F_q$, $m = \gcd(q-1,n)$ allowing to compute all $m$ solutions in $\F_q$
5241
of the equation $x^n = a$.
5242
5243
\subsec{\kbd{Fq}}
5244
5245
\fun{GEN}{Fq_add}{GEN x, GEN y, GEN T/*unused*/, GEN p}
5246
5247
\fun{GEN}{Fq_sub}{GEN x, GEN y, GEN T/*unused*/, GEN p}
5248
5249
\fun{GEN}{Fq_mul}{GEN x, GEN y, GEN T, GEN p}
5250
5251
\fun{GEN}{Fq_Fp_mul}{GEN x, GEN y, GEN T, GEN p} multiplies the \kbd{Fq} $x$
5252
by the \typ{INT} $y$.
5253
5254
\fun{GEN}{Fq_mulu}{GEN x, ulong y, GEN T, GEN p} multiplies the \kbd{Fq} $x$
5255
by the scalar $y$.
5256
5257
\fun{GEN}{Fq_halve}{GEN x, GEN T, GEN p} returns $z$ such that $2\*z = x$
5258
assuming such $z$ exists.
5259
5260
\fun{GEN}{Fq_sqr}{GEN x, GEN T, GEN p}
5261
5262
\fun{GEN}{Fq_neg}{GEN x, GEN T, GEN p}
5263
5264
\fun{GEN}{Fq_neg_inv}{GEN x, GEN T, GEN p} computes $-\kbd{x}^{-1}$
5265
5266
\fun{GEN}{Fq_inv}{GEN x, GEN pol, GEN p} computes $\kbd{x}^{-1}$, raising an
5267
error if \kbd{x} is not invertible.
5268
5269
\fun{GEN}{Fq_invsafe}{GEN x, GEN pol, GEN p} as \kbd{Fq\_inv}, but returns
5270
\kbd{NULL} if \kbd{x} is not invertible.
5271
5272
\fun{GEN}{Fq_div}{GEN x, GEN y, GEN T, GEN p}
5273
5274
\fun{GEN}{FqV_inv}{GEN x, GEN T, GEN p} $x$ being a vector of \kbd{Fq}s,
5275
return the vector of inverses of the $x[i]$. The routine uses Montgomery's
5276
trick, and involves a single inversion, plus $3(N-1)$ multiplications for
5277
$N$ entries. The routine is not stack-clean: $2N$ \kbd{FpXQ} are left on
5278
stack, besides the $N$ in the result.
5279
5280
\fun{GEN}{FqV_factorback}{GEN L, GEN e, GEN T, GEN p} given an \kbd{FqV} $L$
5281
and a \kbd{ZV} or \kbd{zv} $e$ of the same length, return $\prod_i L_i^{e_i}$
5282
modulo $p$.
5283
5284
\fun{GEN}{Fq_pow}{GEN x, GEN n, GEN pol, GEN p} returns $\kbd{x}^\kbd{n}$.
5285
5286
\fun{GEN}{Fq_powu}{GEN x, ulong n, GEN pol, GEN p} returns $\kbd{x}^\kbd{n}$
5287
for small $n$.
5288
5289
\fun{GEN}{Fq_log}{GEN a, GEN g, GEN ord, GEN T, GEN p} as
5290
\tet{Fp_log} or \tet{FpXQ_log}.
5291
5292
\fun{int}{Fq_issquare}{GEN x, GEN T, GEN p} returns $1$ if $x$ is a square
5293
and $0$ otherwise. Assumes that \kbd{T} is irreducible mod \kbd{p} and that
5294
$p$ is prime; $T = \kbd{NULL}$ is forbidden unless $x$ is an \kbd{Fp}.
5295
5296
\fun{long}{Fq_ispower}{GEN x, GEN n, GEN T, GEN p} returns $1$ if $x$
5297
is a $n$-th power and $0$ otherwise. Assumes that \kbd{T} is irreducible mod
5298
\kbd{p} and that $p$ is prime; $T = \kbd{NULL}$ is forbidden unless $x$ is an
5299
\kbd{Fp}.
5300
5301
\fun{GEN}{Fq_sqrt}{GEN x, GEN T, GEN p} returns a square root of \kbd{x}.
5302
Return \kbd{NULL} if \kbd{x} is not a square.
5303
5304
\fun{GEN}{Fq_sqrtn}{GEN a, GEN n, GEN T, GEN p, GEN *zn}
5305
as \tet{FpXQ_sqrtn}.
5306
5307
\fun{GEN}{FpXQ_charpoly}{GEN x, GEN T, GEN p} returns the characteristic
5308
polynomial of \kbd{x}
5309
5310
\fun{GEN}{FpXQ_minpoly}{GEN x, GEN T, GEN p} returns the minimal polynomial
5311
of \kbd{x}
5312
5313
\fun{GEN}{FpXQ_norm}{GEN x, GEN T, GEN p} returns the norm of \kbd{x}
5314
5315
\fun{GEN}{FpXQ_trace}{GEN x, GEN T, GEN p} returns the trace of \kbd{x}
5316
5317
\fun{GEN}{FpXQ_conjvec}{GEN x, GEN T, GEN p} returns the vector of conjugates
5318
$[x,x^p,x^{p^2},\ldots,x^{p^{n-1}}]$ where $n$ is the degree of $T$.
5319
5320
\fun{GEN}{gener_FpXQ}{GEN T, GEN p, GEN *po} returns a primitive root modulo
5321
$(T,p)$. $T$ is an \kbd{FpX} assumed to be irreducible modulo the prime
5322
$p$. If \kbd{po} is not \kbd{NULL} it is set to $[o,\var{fa}]$, where $o$ is
5323
the order of the multiplicative group of the finite field, and \var{fa} is
5324
its factorization.
5325
5326
\fun{GEN}{gener_FpXQ_local}{GEN T, GEN p, GEN L}, \kbd{L} being a vector of
5327
primes dividing $p^{\deg T} - 1$, returns an element of $G:=\F_p[x]/(T)$
5328
which is a generator of the $\ell$-Sylow of $G$ for every $\ell$ in
5329
\kbd{L}. It is not necessary, and in fact slightly inefficient, to include
5330
$\ell=2$, since 2 is treated separately in any case, i.e. the generator
5331
obtained is never a square if $p$ is odd.
5332
5333
\fun{GEN}{gener_Fq_local}{GEN T, GEN p, GEN L} as
5334
\kbd{pgener\_Fp\_local(p, L)} if $T$ is \kbd{NULL},
5335
or \kbd{gener\_FpXQ\_local} (otherwise).
5336
5337
5338
\fun{GEN}{FpXQ_powers}{GEN x, long n, GEN T, GEN p} returns $[\kbd{x}^0,
5339
\dots, \kbd{x}^\kbd{n}]$ as a \typ{VEC} of \kbd{FpXQ}s.
5340
5341
\fun{GEN}{FpXQ_matrix_pow}{GEN x, long m, long n, GEN T, GEN p}, as
5342
\kbd{FpXQ\_powers}$(x, n-1, T, p)$, but returns the powers as a an
5343
$m\times n$ matrix. Usually, we have $m = n = \deg T$.
5344
5345
\fun{GEN}{FpXQ_autpow}{GEN a, ulong n, GEN T, GEN p} computes $\sigma^n(X)$
5346
assuming $a=\sigma(X)$ where $\sigma$ is an automorphism of the algebra
5347
$\F_p[X]/T(X)$.
5348
5349
\fun{GEN}{FpXQ_autsum}{GEN a, ulong n, GEN T, GEN p}
5350
$a$ being a two-component vector,
5351
$\sigma$ being the automorphism defined by $\sigma(X)=a[1]\pmod{T(X)}$,
5352
returns the vector $[\sigma^n(X),b\sigma(b)\ldots\sigma^{n-1}(b)]$
5353
where $b=a[2]$.
5354
5355
\fun{GEN}{FpXQ_auttrace}{GEN a, ulong n, GEN T, GEN p}
5356
$a$ being a two-component vector,
5357
$\sigma$ being the automorphism defined by $\sigma(X)=a[1]\pmod{T(X)}$,
5358
returns the vector $[\sigma^n(X),b+\sigma(b)+\ldots+\sigma^{n-1}(b)]$
5359
where $b=a[2]$.
5360
5361
\fun{GEN}{FpXQ_autpowers}{GEN S, long n, GEN T, GEN p} returns
5362
$[x,S(x),S(S(x)),\dots,S^{(n)}(x)]$ as a \typ{VEC} of \kbd{FpXQ}s.
5363
5364
\fun{GEN}{FpXQM_autsum}{GEN a, long n, GEN T, GEN p}
5365
$\sigma$ being the automorphism defined by $\sigma(X)=a[1]\pmod{T(X)}$,
5366
returns the vector $[\sigma^n(X),b\sigma(b)\ldots\sigma^{n-1}(b)]$
5367
where $b=a[2]$ is a square matrix.
5368
5369
\fun{GEN}{FpX_FpXQ_eval}{GEN f, GEN x, GEN T, GEN p} returns
5370
$\kbd{f}(\kbd{x})$.
5371
5372
\fun{GEN}{FpX_FpXQV_eval}{GEN f, GEN V, GEN T, GEN p} returns
5373
$\kbd{f}(\kbd{x})$, assuming that \kbd{V} was computed by
5374
$\kbd{FpXQ\_powers}(\kbd{x}, n, \kbd{T}, \kbd{p})$.
5375
5376
\fun{GEN}{FpXC_FpXQ_eval}{GEN C, GEN x, GEN T,GEN p} applies
5377
\kbd{FpX\_FpXQV\_eval} to all elements of the vector $C$
5378
and returns a \typ{COL}.
5379
5380
\fun{GEN}{FpXC_FpXQV_eval}{GEN C, GEN V,GEN T,GEN p} applies
5381
\kbd{FpX\_FpXQV\_eval} to all elements of the vector $C$
5382
and returns a \typ{COL}.
5383
5384
\fun{GEN}{FpXM_FpXQV_eval}{GEN M, GEN V,GEN T,GEN p} applies
5385
\kbd{FpX\_FpXQV\_eval} to all elements of the matrix $M$.
5386
5387
\subsec{\kbd{FpXn}} Let \kbd{p} a \typ{INT} and \kbd{T} an
5388
\kbd{FpX} for \kbd{p}, both to be given in the function arguments; an \kbd{FpXn}
5389
object is an \kbd{FpX} whose degree is strictly less than \kbd{n}.
5390
They represent a class in $(\Z/\kbd{p}\Z)[X] / (X^n)$, in which all operations
5391
below take place. They can be seen as truncated power series.
5392
5393
\fun{GEN}{FpXn_mul}{GEN x, GEN y, long n, GEN p} return $x\*y\pmod{X^n}$.
5394
5395
\fun{GEN}{FpXn_sqr}{GEN x, long n, GEN p} return $x^2\pmod{X^n}$.
5396
5397
\fun{GEN}{FpXn_inv}{GEN x, long n, GEN p} return $1/x\pmod{X^n}$.
5398
5399
\fun{GEN}{FpXn_exp}{GEN f, long n, GEN p} return $\exp(f)$
5400
as a composition of formal power series.
5401
It is required that the valuation of $f$ is positive and that $p>n$.
5402
5403
\fun{GEN}{FpXn_expint}{GEN f, long n, GEN p} return $\exp(F)$
5404
where $F$ is the primitive of $f$ that vanishes at $0$.
5405
It is required that $p>n$.
5406
5407
\subsec{\kbd{FpXC}, \kbd{FpXM}}
5408
5409
\fun{GEN}{FpXC_center}{GEN C, GEN p, GEN pov2}
5410
5411
\fun{GEN}{FpXM_center}{GEN M, GEN p, GEN pov2}
5412
5413
\subsec{\kbd{FpXX}, \kbd{FpXY}}
5414
Contrary to what the name implies, an \kbd{FpXX} is a \typ{POL} whose
5415
coefficients are either \typ{INT}s or \kbd{FpX}s. This reduces memory
5416
overhead at the expense of consistency. The prefix \kbd{FpXY} is an
5417
alias for \kbd{FpXX} when variables matters.
5418
5419
\fun{GEN}{FpXX_red}{GEN z, GEN p}, \kbd{z} a \typ{POL} whose coefficients are
5420
either \kbd{ZX}s or \typ{INT}s. Returns the \typ{POL} equal to \kbd{z} with
5421
all components reduced modulo \kbd{p}.
5422
5423
\fun{GEN}{FpXX_renormalize}{GEN x, long l}, as \kbd{normalizepol}, where
5424
$\kbd{l} = \kbd{lg(x)}$, in place.
5425
5426
\fun{GEN}{FpXX_add}{GEN x, GEN y, GEN p} adds \kbd{x} and \kbd{y}.
5427
5428
\fun{GEN}{FpXX_sub}{GEN x, GEN y, GEN p} returns $\kbd{x}-\kbd{y}$.
5429
5430
\fun{GEN}{FpXX_neg}{GEN x, GEN p} returns $-\kbd{x}$.
5431
5432
\fun{GEN}{FpXX_Fp_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{FpXX}~\kbd{x}
5433
by the \kbd{Fp}~\kbd{y}.
5434
5435
\fun{GEN}{FpXX_FpX_mul}{GEN x, GEN y, GEN p} multiplies the coefficients of the
5436
\kbd{FpXX}~\kbd{x} by the \kbd{FpX}~\kbd{y}.
5437
5438
\fun{GEN}{FpXX_mulu}{GEN x, GEN y, GEN p} multiplies the \kbd{FpXX}~\kbd{x}
5439
by the scalar \kbd{y}.
5440
5441
\fun{GEN}{FpXX_halve}{GEN x, GEN p} returns $z$ such that $2\*z = x$
5442
assuming such $z$ exists.
5443
5444
\fun{GEN}{FpXX_deriv}{GEN P, GEN p} differentiates \kbd{P} with respect to
5445
the main variable.
5446
5447
\fun{GEN}{FpXX_integ}{GEN P, GEN p} returns the primitive of \kbd{P} with
5448
respect to the main variable whose constant term is $0$.
5449
5450
\fun{GEN}{FpXY_eval}{GEN Q, GEN y, GEN x, GEN p} $Q$ being an \kbd{FpXY},
5451
i.e.~a \typ{POL} with \kbd{Fp} or \kbd{FpX} coefficients representing an
5452
element of $\F_p[X][Y]$. Returns the \kbd{Fp} $Q(x,y)$.
5453
5454
\fun{GEN}{FpXY_evalx}{GEN Q, GEN x, GEN p} $Q$ being an \kbd{FpXY}, returns the
5455
\kbd{FpX} $Q(x,Y)$, where $Y$ is the main variable of $Q$.
5456
5457
\fun{GEN}{FpXY_evaly}{GEN Q, GEN y, GEN p, long vx} $Q$ an \kbd{FpXY}, returns
5458
the \kbd{FpX} $Q(X,y)$, where $X$ is the second variable of $Q$, and \kbd{vx}
5459
is the variable number of $X$.
5460
5461
\fun{GEN}{FpXY_FpXQ_evaly}{GEN Q, GEN y, GEN T, GEN p, long vx} $Q$ an \kbd{FpXY}
5462
and $y$ being an \kbd{FpXQ}, returns the \kbd{FpXQX} $Q(X,y)$, where $X$ is the
5463
second variable of $Q$, and \kbd{vx} is the variable number of $X$.
5464
5465
\fun{GEN}{FpXY_Fq_evaly}{GEN Q, GEN y, GEN T, GEN p, long vx} $Q$ an \kbd{FpXY}
5466
and $y$ being an \kbd{Fq}, returns the \kbd{FqX} $Q(X,y)$, where $X$ is the
5467
second variable of $Q$, and \kbd{vx} is the variable number of $X$.
5468
5469
\fun{GEN}{FpXY_FpXQ_evalx}{GEN Q, GEN x, ulong p} $Q$ an \kbd{FpXY} and
5470
$x$ being an \kbd{FpXQ}, returns the \kbd{FpXQX} $Q(x,Y)$, where $Y$ is the
5471
first variable of $Q$.
5472
5473
\fun{GEN}{FpXY_FpXQV_evalx}{GEN Q, GEN V, ulong p} $Q$ an \kbd{FpXY} and
5474
$x$ being an \kbd{FpXQ}, returns the \kbd{FpXQX} $Q(x,Y)$, where $Y$ is the
5475
first variable of $Q$, assuming that \kbd{V} was computed by
5476
$\kbd{FpXQ\_powers}(\kbd{x}, n, \kbd{T}, \kbd{p})$.
5477
5478
\fun{GEN}{FpXYQQ_pow}{GEN x, GEN n, GEN S, GEN T, GEN p}, \kbd{x} being a
5479
\kbd{FpXY}, \kbd{T} being a \kbd{FpX} and \kbd{S} being a \kbd{FpY},
5480
return $x^n \pmod{S,T,p}$.
5481
5482
\subsec{\kbd{FpXQX}, \kbd{FqX}}
5483
Contrary to what the name implies, an \kbd{FpXQX} is a \typ{POL} whose
5484
coefficients are \kbd{Fq}s. So the only difference between \kbd{FqX} and
5485
\kbd{FpXQX} routines is that $\kbd{T} = \kbd{NULL}$ is not allowed in the
5486
latter. (It was thought more useful to allow \typ{INT} components than to
5487
enforce strict consistency, which would not imply any efficiency gain.)
5488
5489
\subsubsec{Basic operations}
5490
5491
\fun{GEN}{FqX_add}{GEN x,GEN y,GEN T,GEN p}
5492
5493
\fun{GEN}{FqX_Fq_add}{GEN x, GEN y, GEN T, GEN p} adds the
5494
\kbd{Fq}~\kbd{y} to the \kbd{FqX}~\kbd{x}.
5495
5496
\fun{GEN}{FqX_Fq_sub}{GEN x, GEN y, GEN T, GEN p} substracts the
5497
\kbd{Fq}~\kbd{y} to the \kbd{FqX}~\kbd{x}.
5498
5499
\fun{GEN}{FqX_neg}{GEN x,GEN T,GEN p}
5500
5501
\fun{GEN}{FqX_sub}{GEN x,GEN y,GEN T,GEN p}
5502
5503
\fun{GEN}{FqX_mul}{GEN x, GEN y, GEN T, GEN p}
5504
5505
\fun{GEN}{FqX_Fq_mul}{GEN x, GEN y, GEN T, GEN p} multiplies the
5506
\kbd{FqX}~\kbd{x} by the \kbd{Fq}~\kbd{y}.
5507
5508
\fun{GEN}{FqX_mulu}{GEN x, ulong y, GEN T, GEN p} multiplies the
5509
\kbd{FqX}~\kbd{x} by the scalar~\kbd{y}.
5510
5511
\fun{GEN}{FqX_halve}{GEN x, GEN T, GEN p} returns $z$ such that $2\*z = x$
5512
assuming such $z$ exists.
5513
5514
\fun{GEN}{FqX_Fp_mul}{GEN x, GEN y, GEN T, GEN p} multiplies the
5515
\kbd{FqX}~\kbd{x} by the \typ{INT}~\kbd{y}.
5516
5517
\fun{GEN}{FqX_Fq_mul_to_monic}{GEN x, GEN y, GEN T, GEN p}
5518
returns $x\*y$ assuming the result is monic of the same degree as $x$ (in
5519
particular $y\neq 0$).
5520
5521
\fun{GEN}{FpXQX_normalize}{GEN z, GEN T, GEN p}
5522
5523
\fun{GEN}{FqX_normalize}{GEN z, GEN T, GEN p} divides the \kbd{FqX}~\kbd{z}
5524
by its leading term. The leading coefficient becomes $1$ as a \typ{INT}.
5525
5526
\fun{GEN}{FqX_sqr}{GEN x, GEN T, GEN p}
5527
5528
\fun{GEN}{FqX_powu}{GEN x, ulong n, GEN T, GEN p}
5529
5530
\fun{GEN}{FqX_divrem}{GEN x, GEN y, GEN T, GEN p, GEN *z}
5531
5532
\fun{GEN}{FqX_div}{GEN x, GEN y, GEN T, GEN p}
5533
5534
\fun{GEN}{FqX_div_by_X_x}{GEN a, GEN x, GEN T, GEN p, GEN *r}
5535
5536
\fun{GEN}{FqX_rem}{GEN x, GEN y, GEN T, GEN p}
5537
5538
\fun{GEN}{FqX_deriv}{GEN x, GEN T, GEN p} returns the derivative of \kbd{x}.
5539
(This function is suitable for \kbd{gerepilupto} but not memory-clean.)
5540
5541
\fun{GEN}{FqX_integ}{GEN x, GEN T, GEN p} returns the primitive of \kbd{x}.
5542
whose constant term is $0$.
5543
5544
\fun{GEN}{FqX_translate}{GEN P, GEN c, GEN T, GEN p} let $c$ be an \kbd{Fq}
5545
defined modulo $(p, T)$, and let $P$ be an \kbd{FqX}; returns the translated
5546
\kbd{FqX} of $P(X+c)$.
5547
5548
\fun{GEN}{FqX_gcd}{GEN P, GEN Q, GEN T, GEN p} returns a (not necessarily
5549
monic) greatest common divisor of $x$ and $y$.
5550
5551
\fun{GEN}{FqX_extgcd}{GEN x, GEN y, GEN T, GEN p, GEN *ptu, GEN *ptv}
5552
returns $d = \text{GCD}(\kbd{x},\kbd{y})$ (not necessarily monic), and sets
5553
\kbd{*u}, \kbd{*v} to the Bezout coefficients such that $\kbd{*ux} +
5554
\kbd{*vy} = d$.
5555
5556
\fun{GEN}{FqX_halfgcd}{GEN x, GEN y, GEN T, GEN p} returns a two-by-two
5557
\kbd{FqXM} $M$ with determinant $\pm 1$ such that the image $(a,b)$ of $(x,y)$
5558
by $M$ has the property that $\deg a \geq {\deg x \over 2} > \deg b$.
5559
5560
\fun{GEN}{FqX_eval}{GEN x, GEN y, GEN T, GEN p} evaluates the \kbd{FqX}~\kbd{x}
5561
at the \kbd{Fq}~\kbd{y}. The result is an~\kbd{Fq}.
5562
5563
\fun{GEN}{FqXY_eval}{GEN Q, GEN y, GEN x, GEN T, GEN p} $Q$ an \kbd{FqXY},
5564
i.e.~a \typ{POL} with \kbd{Fq} or \kbd{FqX} coefficients representing an
5565
element of $\F_q[X][Y]$. Returns the \kbd{Fq} $Q(x,y)$.
5566
5567
\fun{GEN}{FqXY_evalx}{GEN Q, GEN x, GEN T, GEN p} $Q$ being an \kbd{FqXY},
5568
returns the \kbd{FqX} $Q(x,Y)$, where $Y$ is the main variable of $Q$.
5569
5570
\fun{GEN}{random_FpXQX}{long d, long v, GEN T, GEN p} returns a random
5571
\kbd{FpXQX} in variable \kbd{v}, of degree less than~\kbd{d}.
5572
5573
\fun{GEN}{FpXQX_renormalize}{GEN x, long lx}
5574
5575
\fun{GEN}{FpXQX_red}{GEN z, GEN T, GEN p} \kbd{z} a \typ{POL} whose
5576
coefficients are \kbd{ZX}s or \typ{INT}s, reduce them to \kbd{FpXQ}s.
5577
5578
\fun{GEN}{FpXQX_mul}{GEN x, GEN y, GEN T, GEN p}
5579
5580
\fun{GEN}{Kronecker_to_FpXQX}{GEN z, GEN T, GEN p}. Let $n = \deg T$ and let
5581
$P(X,Y)\in \Z[X,Y]$ lift a polynomial in $K[Y]$, where $K := \F_p[X]/(T)$ and
5582
$\deg_X P < 2n-1$ --- such as would result from multiplying minimal degree
5583
lifts of two polynomials in $K[Y]$. Let $z = P(t,t^{2*n-1})$ be a Kronecker
5584
form of $P$ (see \tet{RgXX_to_Kronecker}), this function returns $Q\in
5585
\Z[X,t]$ such that $Q$ is congruent to $P(X,t)$ mod $(p, T(X))$, $\deg_X Q <
5586
n$, and all coefficients are in $[0,p[$. Not stack-clean. Note that $t$ need
5587
not be the same variable as $Y$!
5588
5589
\fun{GEN}{FpXQX_FpXQ_mul}{GEN x, GEN y, GEN T, GEN p}
5590
5591
\fun{GEN}{FpXQX_sqr}{GEN x, GEN T, GEN p}
5592
5593
\fun{GEN}{FpXQX_divrem}{GEN x, GEN y, GEN T, GEN p, GEN *pr}
5594
5595
\fun{GEN}{FpXQX_div}{GEN x, GEN y, GEN T, GEN p}
5596
5597
\fun{GEN}{FpXQX_div_by_X_x}{GEN a, GEN x, GEN T, GEN p, GEN *r}
5598
5599
\fun{GEN}{FpXQX_rem}{GEN x, GEN y, GEN T, GEN p}
5600
5601
\fun{GEN}{FpXQX_powu}{GEN x, ulong n, GEN T, GEN p} returns $x^n$.
5602
5603
\fun{GEN}{FpXQX_digits}{GEN x, GEN B, GEN T, GEN p}
5604
5605
\fun{GEN}{FpXQX_dotproduct}{GEN x, GEN y, GEN T, GEN p} returns the scalar
5606
product of the coefficients of $x$ and $y$.
5607
5608
\fun{GEN}{FpXQXV_FpXQX_fromdigits}{GEN v, GEN B, GEN T, GEN p}
5609
5610
\fun{GEN}{FpXQX_invBarrett}{GEN y, GEN T, GEN p} returns the Barrett inverse of
5611
the \kbd{FpXQX} $y$, namely a lift of $1/\kbd{polrecip}(y)+O(x^{\deg(y)-1})$.
5612
5613
\fun{GEN}{FpXQXV_prod}{GEN V, GEN T, GEN p}, \kbd{V} being a vector of
5614
\kbd{FpXQX}, returns their product.
5615
5616
\fun{GEN}{FpXQX_gcd}{GEN x, GEN y, GEN T, GEN p}
5617
5618
\fun{GEN}{FpXQX_extgcd}{GEN x, GEN y, GEN T, GEN p, GEN *ptu, GEN *ptv}
5619
5620
\fun{GEN}{FpXQX_halfgcd}{GEN x, GEN y, GEN T, GEN p}
5621
5622
\fun{GEN}{FpXQX_resultant}{GEN x, GEN y, GEN T, GEN p} returns the resultant
5623
of \kbd{x} and \kbd{y}.
5624
5625
\fun{GEN}{FpXQX_disc}{GEN x, GEN T, GEN p} returns the discriminant
5626
of \kbd{x}.
5627
5628
\fun{GEN}{FpXQX_FpXQXQ_eval}{GEN f,GEN x,GEN S, GEN T,GEN p} returns
5629
$\kbd{f}(\kbd{x})$.
5630
5631
\subsec{\kbd{FpXQXn}, \kbd{FqXn}}
5632
5633
A \kbd{FpXQXn} is a \typ{FpXQX} which represents an element of the ring
5634
$(Fp[X]/T(X))[Y]/(Y^n)$, where $T$ is a \kbd{FpX}.
5635
5636
\fun{GEN}{FpXQXn_sqr}{GEN x, long n, GEN T, GEN p}
5637
5638
\fun{GEN}{FqXn_sqr}{GEN x, long n, GEN T, GEN p}
5639
5640
\fun{GEN}{FpXQXn_mul}{GEN x, GEN y, long n, GEN T, GEN p}
5641
5642
\fun{GEN}{FqXn_mul}{GEN x, GEN y, long n, GEN T, GEN p}
5643
5644
\fun{GEN}{FpXQXn_inv}{GEN x, long n, GEN T, GEN p}
5645
5646
\fun{GEN}{FqXn_inv}{GEN x, long n, GEN T, GEN p}
5647
5648
\fun{GEN}{FpXQXn_exp}{GEN x, long n, GEN T, GEN p} return $\exp(x)$
5649
as a composition of formal power series.
5650
It is required that the valuation of $x$ is positive and that $p>n$.
5651
5652
\fun{GEN}{FqXn_exp}{GEN x, long n, GEN T, GEN p}
5653
5654
\fun{GEN}{FpXQXn_expint}{GEN f, long n, GEN p} return $\exp(F)$
5655
where $F$ is the primitive of $f$ that vanishes at $0$.
5656
It is required that $p>n$.
5657
5658
\fun{GEN}{FqXn_expint}{GEN x, long n, GEN T, GEN p}
5659
5660
\subsec{\kbd{FpXQXQ}, \kbd{FqXQ}}
5661
5662
A \kbd{FpXQXQ} is a \typ{FpXQX} which represents an element of the ring
5663
$(Fp[X]/T(X))[Y]/S(X,Y)$, where $T$ is a \kbd{FpX} and $S$ a \kbd{FpXQX}
5664
modulo $T$. A \kbd{FqXQ} is identical except that $T$ is allowed to be
5665
\kbd{NULL} in which case $S$ must be a \kbd{FpX}.
5666
5667
\subsubsec{Preconditioned reduction}
5668
5669
For faster reduction, the modulus \kbd{S} can be replaced by an extended
5670
modulus, which is an \kbd{FpXQXT}, in all \kbd{FpXQXQ}- and \kbd{FqXQ}-classes
5671
functions, and in \kbd{FpXQX\_rem} and \kbd{FpXQX\_divrem}.
5672
5673
\fun{GEN}{FpXQX_get_red}{GEN S, GEN T, GEN p} returns the extended modulus
5674
\kbd{eS}.
5675
5676
\fun{GEN}{FqX_get_red}{GEN S, GEN T, GEN p} identical, but allow $T$ to
5677
be \kbd{NULL}, in which case it returns \kbd{FpX\_get\_red(S,p)}.
5678
5679
To write code that works both with plain and extended moduli, the following
5680
accessors are defined:
5681
5682
\fun{GEN}{get_FpXQX_mod}{GEN eS} returns the underlying modulus \kbd{S}.
5683
5684
\fun{GEN}{get_FpXQX_var}{GEN eS} returns the variable number of the modulus.
5685
5686
\fun{GEN}{get_FpXQX_degree}{GEN eS} returns the degree of the modulus.
5687
5688
Furthermore, \kbd{ZXXT\_to\_FlxXT} allows to convert an extended modulus for
5689
a \kbd{FpXQX} to an extended modulus for the corresponding \kbd{FlxqX}.
5690
5691
\subsubsec{basic operations}
5692
5693
\fun{GEN}{FpXQX_FpXQXQV_eval}{GEN f,GEN V,GEN S,GEN T,GEN p} returns
5694
$\kbd{f}(\kbd{x})$, assuming that \kbd{V} was computed by
5695
$\kbd{FpXQXQ\_powers}(\kbd{x}, n, \kbd{S}, \kbd{T}, \kbd{p})$.
5696
5697
\fun{GEN}{FpXQXQ_div}{GEN x, GEN y, GEN S, GEN T, GEN p}, \kbd{x}, \kbd{y} and
5698
\kbd{S} being \kbd{FpXQX}s, returns $\kbd{x}*\kbd{y}^{-1}$ modulo \kbd{S}.
5699
5700
\fun{GEN}{FpXQXQ_inv}{GEN x, GEN S, GEN T, GEN p}, \kbd{x} and
5701
\kbd{S} being \kbd{FpXQX}s, returns $\kbd{x}^{-1}$ modulo \kbd{S}.
5702
5703
\fun{GEN}{FpXQXQ_invsafe}{GEN x, GEN S, GEN T,GEN p}, as \kbd{FpXQXQ\_inv},
5704
returning \kbd{NULL} if \kbd{x} is not invertible.
5705
5706
\fun{GEN}{FpXQXQ_mul}{GEN x, GEN y, GEN S, GEN T, GEN p}, \kbd{x}, \kbd{y} and
5707
\kbd{S} being \kbd{FpXQX}s, returns $\kbd{x}\*\kbd{y}$ modulo \kbd{S}.
5708
5709
\fun{GEN}{FpXQXQ_sqr}{GEN x, GEN S, GEN T, GEN p}, \kbd{x} and
5710
\kbd{S} being \kbd{FpXQX}s, returns $\kbd{x}^2$ modulo \kbd{S}.
5711
5712
\fun{GEN}{FpXQXQ_pow}{GEN x, GEN n, GEN S, GEN T, GEN p}, \kbd{x} and
5713
\kbd{S} being \kbd{FpXQX}s, returns $\kbd{x}^\kbd{n}$ modulo \kbd{S}.
5714
5715
\fun{GEN}{FpXQXQ_powers}{GEN x, long n, GEN S, GEN T, GEN p}, \kbd{x} and
5716
\kbd{S} being \kbd{FpXQX}s, returns $[\kbd{x}^0, \dots, \kbd{x}^\kbd{n}]$ as a
5717
\typ{VEC} of \kbd{FpXQXQ}s.
5718
5719
\fun{GEN}{FpXQXQ_halfFrobenius}{GEN A, GEN S, GEN T, GEN p} returns
5720
$A(X)^{(q-1)/2}\pmod{S(X)}$ over the finite field $\F_q$ defined by $T$
5721
and $p$, thus $q=p^n$ where $n$ is the degree of $T$.
5722
5723
\fun{GEN}{FpXQXQ_minpoly}{GEN x, GEN S, GEN T, GEN p}, as
5724
\kbd{FpXQ\_minpoly}
5725
5726
\fun{GEN}{FpXQXQ_matrix_pow}{GEN x, long m, long n, GEN S, GEN T, GEN p}
5727
returns the same powers of \kbd{x} as \kbd{FpXQXQ\_powers}$(x, n-1,S, T, p)$,
5728
but as an $m\times n$ matrix.
5729
5730
\fun{GEN}{FpXQXQ_autpow}{GEN a, long n, GEN S, GEN T, GEN p}
5731
$\sigma$ being the automorphism defined by $\sigma(X)=a[1]\pmod{T(X)}$,
5732
$\sigma(Y)=a[2]\pmod{S(X,Y),T(X)}$, returns $[\sigma^n(X),\sigma^n(Y)]$.
5733
5734
\fun{GEN}{FpXQXQ_autsum}{GEN a, long n, GEN S, GEN T, GEN p}
5735
$\sigma$ being the automorphism defined by $\sigma(X)=a[1]\pmod{T(X)}$,
5736
$\sigma(Y)=a[2]\pmod{S(X,Y),T(X)}$, returns the vector
5737
$[\sigma^n(X),\sigma^n(Y),b\sigma(b)\ldots\sigma^{n-1}(b)]$
5738
where $b=a[3]$.
5739
5740
\fun{GEN}{FpXQXQ_auttrace}{GEN a, long n, GEN S, GEN T, GEN p}
5741
$\sigma$ being the automorphism defined by $\sigma(X)=X\pmod{T(X)}$,
5742
$\sigma(Y)=a[1]\pmod{S(X,Y),T(X)}$, returns the vector
5743
$[\sigma^n(X),\sigma^n(Y),b+\sigma(b)+\ldots+\sigma^{n-1}(b)]$
5744
where $b=a[2]$.
5745
5746
% FqXQ
5747
5748
\fun{GEN}{FqXQ_add}{GEN x, GEN y, GEN S, GEN T, GEN p}, \kbd{x}, \kbd{y} and
5749
\kbd{S} being \kbd{FqX}s, returns $\kbd{x} + \kbd{y}$ modulo \kbd{S}.
5750
5751
\fun{GEN}{FqXQ_sub}{GEN x, GEN y, GEN S, GEN T, GEN p}, \kbd{x}, \kbd{y} and
5752
\kbd{S} being \kbd{FqX}s, returns $\kbd{x} - \kbd{y}$ modulo \kbd{S}.
5753
5754
\fun{GEN}{FqXQ_mul}{GEN x, GEN y, GEN S, GEN T, GEN p}, \kbd{x}, \kbd{y} and
5755
\kbd{S} being \kbd{FqX}s, returns $\kbd{x}\*\kbd{y}$ modulo \kbd{S}.
5756
5757
\fun{GEN}{FqXQ_div}{GEN x, GEN y, GEN S, GEN T, GEN p}, \kbd{x} and
5758
\kbd{S} being \kbd{FqX}s, returns $\kbd{x}/\kbd{y}$ modulo \kbd{S}.
5759
5760
\fun{GEN}{FqXQ_inv}{GEN x, GEN S, GEN T, GEN p}, \kbd{x} and
5761
\kbd{S} being \kbd{FqX}s, returns $\kbd{x}^{-1}$ modulo \kbd{S}.
5762
5763
\fun{GEN}{FqXQ_invsafe}{GEN x, GEN S, GEN T, GEN p} , as \kbd{FqXQ\_inv},
5764
returning \kbd{NULL} if \kbd{x} is not invertible.
5765
5766
\fun{GEN}{FqXQ_sqr}{GEN x, GEN S, GEN T, GEN p}, \kbd{x} and
5767
\kbd{S} being \kbd{FqX}s, returns $\kbd{x}^2$ modulo \kbd{S}.
5768
5769
\fun{GEN}{FqXQ_pow}{GEN x, GEN n, GEN S, GEN T, GEN p}, \kbd{x} and
5770
\kbd{S} being \kbd{FqX}s, returns $\kbd{x}^\kbd{n}$ modulo \kbd{S}.
5771
5772
\fun{GEN}{FqXQ_powers}{GEN x, long n, GEN S, GEN T, GEN p}, \kbd{x} and
5773
\kbd{S} being \kbd{FqX}s, returns $[\kbd{x}^0, \dots, \kbd{x}^\kbd{n}]$ as a
5774
\typ{VEC} of \kbd{FqXQ}s.
5775
5776
\fun{GEN}{FqXQ_matrix_pow}{GEN x, long m, long n, GEN S, GEN T, GEN p}
5777
returns the same powers of \kbd{x} as \kbd{FqXQ\_powers}$(x, n-1,S, T, p)$,
5778
but as an $m\times n$ matrix.
5779
5780
\fun{GEN}{FqV_roots_to_pol}{GEN V, GEN T, GEN p, long v},
5781
\kbd{V} being a vector of \kbd{Fq}s, returns the monic \kbd{FqX}
5782
$\prod_i (\kbd{pol\_x[v]} - \kbd{V[i]})$.
5783
5784
\subsubsec{Miscellaneous operations}
5785
5786
\fun{GEN}{init_Fq}{GEN p, long n, long v} returns an irreducible polynomial
5787
of degree $\kbd{n} > 0$ over $\F_p$, in variable \kbd{v}.
5788
5789
\fun{int}{FqX_is_squarefree}{GEN P, GEN T, GEN p}
5790
5791
\fun{GEN}{FpXQX_roots}{GEN f, GEN T, GEN p} return the roots of $f$ in
5792
$\F_p[X]/(T)$. Assumes \kbd{p} is prime and \kbd{T} irreducible in $\F_p[X]$.
5793
5794
\fun{GEN}{FqX_roots}{GEN f, GEN T, GEN p} same but allow $\kbd{T} = \kbd{NULL}$.
5795
5796
\fun{GEN}{FpXQX_factor}{GEN f, GEN T, GEN p} same output convention as
5797
\kbd{FpX\_factor}. Assumes \kbd{p} is prime and \kbd{T} irreducible
5798
in $\F_p[X]$.
5799
5800
\fun{GEN}{FqX_factor}{GEN f, GEN T, GEN p} same but allow $\kbd{T} = \kbd{NULL}$.
5801
5802
\fun{GEN}{FpXQX_factor_squarefree}{GEN f, GEN T, GEN p} squarefree
5803
factorization of $f$ modulo $(T,p)$; same output convention as
5804
\kbd{FpX\_factor\_squarefree}. Assumes \kbd{p} is prime and \kbd{T}
5805
irreducible in $\F_p[X]$.
5806
5807
\fun{GEN}{FqX_factor_squarefree}{GEN f, GEN T, GEN p} same but allow
5808
$\kbd{T} = \kbd{NULL}$.
5809
5810
\fun{GEN}{FpXQX_ddf}{GEN f, GEN T, GEN p} as \kbd{FpX\_ddf}.
5811
5812
\fun{GEN}{FqX_ddf}{GEN f, GEN T, GEN p} same but allow $\kbd{T} = \kbd{NULL}$.
5813
5814
\fun{long}{FpXQX_ddf_degree}{GEN f, GEN XP, GEN T, GEN p}, as
5815
\kbd{FpX\_ddf\_degree}.
5816
5817
\fun{GEN}{FpXQX_degfact}{GEN f, GEN T, GEN p}, as \kbd{FpX\_degfact}.
5818
5819
\fun{GEN}{FqX_degfact}{GEN f, GEN T, GEN p} same but allow
5820
$\kbd{T} = \kbd{NULL}$.
5821
5822
\fun{GEN}{FpXQX_split_part}{GEN f, GEN T, GEN p} returns the largest totally
5823
split squarefree factor of $f$.
5824
5825
\fun{long}{FpXQX_ispower}{GEN f, ulong k, GEN T, GEN p, GEN *pt}
5826
return $1$ if the \kbd{FpXQX} $f$ is a $k$-th power, $0$ otherwise.
5827
If \kbd{pt} is not \kbd{NULL}, set it to $g$ such that $g^k = f$.
5828
5829
\fun{long}{FqX_ispower}{GEN f, ulong k, GEN T, GEN p, GEN *pt}
5830
same but allow $\kbd{T} = \kbd{NULL}$.
5831
5832
\fun{GEN}{FpX_factorff}{GEN P, GEN T, GEN p}. Assumes \kbd{p} prime
5833
and \kbd{T} irreducible in $\F_p[X]$. Factor the \kbd{FpX} \kbd{P}
5834
over the finite field $\F_p[Y]/(T(Y))$. See \kbd{FpX\_factorff\_irred}
5835
if \kbd{P} is known to be irreducible of $\F_p$.
5836
5837
\fun{GEN}{FpX_rootsff}{GEN P, GEN T, GEN p}. Assumes \kbd{p} prime
5838
and \kbd{T} irreducible in $\F_p[X]$. Returns the roots of the \kbd{FpX}
5839
\kbd{P} belonging to the finite field $\F_p[Y]/(T(Y))$.
5840
5841
\fun{GEN}{FpX_factorff_irred}{GEN P, GEN T, GEN p}. Assumes \kbd{p} prime
5842
and \kbd{T} irreducible in $\F_p[X]$. Factors the \emph{irreducible}
5843
\kbd{FpX} \kbd{P} over the finite field $\F_p[Y]/(T(Y))$ and returns the
5844
vector of irreducible \kbd{FqX}s factors (the exponents, being all equal to
5845
$1$, are not included).
5846
5847
\fun{GEN}{FpX_ffisom}{GEN P, GEN Q, GEN p}. Assumes \kbd{p} prime,
5848
\kbd{P}, \kbd{Q} are \kbd{ZX}s, both irreducible mod \kbd{p}, and
5849
$\deg(P) \mid \deg Q$. Outputs a monomorphism between $\F_p[X]/(P)$ and
5850
$\F_p[X]/(Q)$, as a polynomial $R$ such that $\kbd{Q} \mid \kbd{P}(R)$ in
5851
$\F_p[X]$. If \kbd{P} and \kbd{Q} have the same degree, it is of course an
5852
isomorphism.
5853
5854
\fun{void}{FpX_ffintersect}{GEN P, GEN Q, long n, GEN p, GEN *SP,GEN *SQ, GEN
5855
MA,GEN MB}\hfil\break
5856
Assumes \kbd{p} is prime, \kbd{P}, \kbd{Q} are \kbd{ZX}s, both
5857
irreducible mod \kbd{p}, and \kbd{n} divides both the degree of \kbd{P} and
5858
\kbd{Q}. Compute \kbd{SP} and \kbd{SQ} such that the subfield of
5859
$\F_p[X]/(P)$ generated by \kbd{SP} and the subfield of $\F_p[X]/(Q)$
5860
generated by \kbd{SQ} are isomorphic of degree \kbd{n}. The polynomials
5861
\kbd{P} and \kbd{Q} do not need to be of the same variable. If \kbd{MA}
5862
(resp. \kbd{MB}) is not \kbd{NULL}, it must be the matrix of the Frobenius
5863
map in $\F_p[X]/(P)$ (resp.~$\F_p[X]/(Q)$).
5864
5865
\fun{GEN}{FpXQ_ffisom_inv}{GEN S, GEN T, GEN p}. Assumes \kbd{p} is prime,
5866
\kbd{T} a \kbd{ZX}, which is irreducible modulo \kbd{p}, \kbd{S} a
5867
\kbd{ZX} representing an automorphism of $\F_q := \F_p[X]/(\kbd{T})$.
5868
($\kbd{S}(X)$ is the image of $X$ by the automorphism.) Returns the
5869
inverse automorphism of \kbd{S}, in the same format, i.e.~an \kbd{FpX}~$H$
5870
such that $H(\kbd{S}) \equiv X$ modulo $(\kbd{T}, \kbd{p})$.
5871
5872
\fun{long}{FpXQX_nbfact}{GEN S, GEN T, GEN p} returns the number of
5873
irreducible factors of the polynomial $S$ over the finite field $\F_q$
5874
defined by $T$ and $p$.
5875
5876
\fun{long}{FpXQX_nbfact_Frobenius}{GEN S, GEN Xq, GEN T, GEN p} as
5877
\kbd{FpXQX\_nbfact} where \kbd{Xq} is \kbd{FpXQX\_Frobenius(S, T, p)}.
5878
5879
\fun{long}{FqX_nbfact}{GEN S, GEN T, GEN p} as above but accept \kbd{T=NULL}.
5880
5881
\fun{long}{FpXQX_nbroots}{GEN S, GEN T, GEN p} returns the number of roots of
5882
the polynomial $S$ over the finite field $\F_q$ defined by $T$ and $p$.
5883
5884
\fun{long}{FqX_nbroots}{GEN S, GEN T, GEN p} as above but accept \kbd{T=NULL}.
5885
5886
\fun{GEN}{FpXQX_Frobenius}{GEN S, GEN T, GEN p} returns
5887
$X^{q}\pmod{S(X)}$ over the finite field $\F_q$ defined by $T$ and $p$, thus
5888
$q=p^n$ where $n$ is the degree of $T$.
5889
5890
\subsec{\kbd{Flx}} Let \kbd{p} be an \kbd{ulong}, not assumed to be
5891
prime unless mentioned otherwise (e.g., all functions involving Euclidean
5892
divisions and factorizations), to be given the function arguments; an
5893
\kbd{Fl} is an \kbd{ulong} belonging to $[0,\kbd{p}-1]$, an \kbd{Flx}~\kbd{z}
5894
is a \typ{VECSMALL} representing a polynomial with small integer
5895
coefficients. Specifically \kbd{z[0]} is the usual codeword, \kbd{z[1] =
5896
evalvarn($v$)} for some variable $v$, then the coefficients by increasing
5897
degree. An \kbd{FlxX} is a \typ{POL} whose coefficients are \kbd{Flx}s.
5898
5899
\noindent In the following, an argument called \kbd{sv} is of the form
5900
\kbd{evalvarn}$(v)$ for some variable number~$v$.
5901
5902
\subsubsec{Preconditioned reduction}
5903
5904
For faster reduction, the modulus \kbd{T} can be replaced by an extended
5905
modulus (\kbd{FlxT}) in all \kbd{Flxq}-classes functions, and in
5906
\kbd{Flx\_divrem}.
5907
5908
\fun{GEN}{Flx_get_red}{GEN T, ulong p} returns the extended modulus \kbd{eT}.
5909
5910
To write code that works both with plain and extended moduli, the following
5911
accessors are defined:
5912
5913
\fun{GEN}{get_Flx_mod}{GEN eT} returns the underlying modulus \kbd{T}.
5914
5915
\fun{GEN}{get_Flx_var}{GEN eT} returns the variable number of the modulus.
5916
5917
\fun{GEN}{get_Flx_degree}{GEN eT} returns the degree of the modulus.
5918
5919
Furthermore, \kbd{ZXT\_to\_FlxT} allows to convert an extended modulus for
5920
a \kbd{FpX} to an extended modulus for the corresponding \kbd{Flx}.
5921
5922
\subsubsec{Basic operations}
5923
5924
\fun{ulong}{Flx_lead}{GEN x} returns the leading coefficient of $x$ as a
5925
\kbd{ulong} (return $0$ for the zero polynomial).
5926
5927
\fun{ulong}{Flx_constant}{GEN x} returns the constant coefficient of $x$ as a
5928
\kbd{ulong} (return $0$ for the zero polynomial).
5929
5930
\fun{GEN}{Flx_red}{GEN z, ulong p} converts from \kbd{zx} with
5931
nonnegative coefficients to \kbd{Flx} (by reducing them mod \kbd{p}).
5932
5933
\fun{int}{Flx_equal1}{GEN x} returns 1 (true) if the \kbd{Flx} $x$ is equal
5934
to~1, 0~(false) otherwise.
5935
5936
\fun{int}{Flx_equal}{GEN x, GEN y} returns 1 (true) if the \kbd{Flx} $x$
5937
and $y$ are equal, and 0~(false) otherwise.
5938
5939
\fun{GEN}{Flx_copy}{GEN x} returns a copy of \kbd{x}.
5940
5941
\fun{GEN}{Flx_add}{GEN x, GEN y, ulong p}
5942
5943
\fun{GEN}{Flx_Fl_add}{GEN y, ulong x, ulong p}
5944
5945
\fun{GEN}{Flx_neg}{GEN x, ulong p}
5946
5947
\fun{GEN}{Flx_neg_inplace}{GEN x, ulong p}, same as \kbd{Flx\_neg}, in place
5948
(\kbd{x} is destroyed).
5949
5950
\fun{GEN}{Flx_sub}{GEN x, GEN y, ulong p}
5951
5952
\fun{GEN}{Flx_Fl_sub}{GEN y, ulong x, ulong p}
5953
5954
\fun{GEN}{Flx_halve}{GEN x, ulong p} returns $z$ such that $2\*z = x$ modulo
5955
$p$ assuming such $z$ exists.
5956
5957
\fun{GEN}{Flx_mul}{GEN x, GEN y, ulong p}
5958
5959
\fun{GEN}{Flx_Fl_mul}{GEN y, ulong x, ulong p}
5960
5961
\fun{GEN}{Flx_double}{GEN y, ulong p} returns $2\*y$.
5962
5963
\fun{GEN}{Flx_triple}{GEN y, ulong p} returns $3\*y$.
5964
5965
\fun{GEN}{Flx_mulu}{GEN y, ulong x, ulong p} as \kbd{Flx\_Fl\_mul} but do not
5966
assume that $x<p$.
5967
5968
\fun{GEN}{Flx_Fl_mul_to_monic}{GEN y, ulong x, ulong p} returns $y\*x$
5969
assuming the result is monic of the same degree as $y$ (in particular $x\neq
5970
0$).
5971
5972
\fun{GEN}{Flx_sqr}{GEN x, ulong p}
5973
5974
\fun{GEN}{Flx_powu}{GEN x, ulong n, ulong p} return $x^n$.
5975
5976
\fun{GEN}{Flx_divrem}{GEN x, GEN y, ulong p, GEN *pr},
5977
here $p$ must be prime.
5978
5979
\fun{GEN}{Flx_div}{GEN x, GEN y, ulong p},
5980
here $p$ must be prime.
5981
5982
\fun{GEN}{Flx_rem}{GEN x, GEN y, ulong p},
5983
here $p$ must be prime.
5984
5985
\fun{GEN}{Flx_deriv}{GEN z, ulong p}
5986
5987
\fun{GEN}{Flx_integ}{GEN z, ulong p},
5988
here $p$ must be prime.
5989
5990
\fun{GEN}{Flx_translate1}{GEN P, ulong p} return $P(x+1)$, $p$ must be prime.
5991
Asymptotically fast (quasi-linear in the degree of $P$).
5992
5993
\fun{GEN}{Flx_translate1_basecase}{GEN P, ulong p} return $P(x+1)$, $p$
5994
need not be prime. Not asymptotically fast (quadratic in the degree of $P$).
5995
5996
\fun{GEN}{zlx_translate1}{GEN P, ulong p, long e} return $P(x+1)$
5997
modulo $p^e$ for prime $p$. Asymptotically fast (quasi-linear in the degree of
5998
$P$).
5999
6000
\fun{GEN}{Flx_diff1}{GEN P, ulong p} return $P(x+1)-P(x)$; $p$ must be prime.
6001
6002
\fun{GEN}{Flx_digits}{GEN x, GEN B, ulong p} returns a vector of \kbd{Flx}
6003
$[c_0,\ldots,c_n]$ of degree less than the degree of $B$ and such that
6004
$x=\sum_{i=0}^{n}{c_i\*B^i}$.
6005
6006
\fun{GEN}{FlxV_Flx_fromdigits}{GEN v, GEN B, ulong p} where $v=[c_0,\ldots,c_n]$
6007
is a vector of \kbd{Flx}, returns $\sum_{i=0}^{n}{c_i\*B^i}$.
6008
6009
\fun{GEN}{Flx_Frobenius}{GEN T, ulong p}
6010
here $p$ must be prime.
6011
6012
\fun{GEN}{Flx_matFrobenius}{GEN T, ulong p}
6013
here $p$ must be prime.
6014
6015
\fun{GEN}{Flx_gcd}{GEN a, GEN b, ulong p} returns a (not necessarily monic)
6016
greatest common divisor of $x$ and $y$. Here $p$ must be prime.
6017
6018
\fun{GEN}{Flx_halfgcd}{GEN x, GEN y, ulong p} returns a two-by-two \kbd{FlxM}
6019
$M$ with determinant $\pm 1$ such that the image $(a,b)$ of $(x,y)$ by $M$
6020
has the property that $\deg a \geq {\deg x \over 2} > \deg b$.
6021
Assumes that $p$ is prime.
6022
6023
\fun{GEN}{Flx_extgcd}{GEN a, GEN b, ulong p, GEN *ptu, GEN *ptv},
6024
here $p$ must be prime.
6025
6026
\fun{GEN}{Flx_roots}{GEN f, ulong p} returns the vector of roots
6027
of $f$ (without multiplicity, as a \typ{VECSMALL}). Assumes that $p$ is
6028
prime.
6029
6030
\fun{ulong}{Flx_oneroot}{GEN f, ulong p} returns one root $0 \leq r < p$ of
6031
the \kbd{Flx}~\kbd{f} in \kbd{\Z/p\Z}. Return $p$ if no root exists.
6032
Assumes that $p$ is prime.
6033
6034
\fun{ulong}{Flx_oneroot_split}{GEN f, ulong p} as \kbd{Flx\_oneroot} but
6035
assume $f$ is totally split. Assumes that $p$ is prime.
6036
6037
\fun{long}{Flx_ispower}{GEN f, ulong k, ulong p, GEN *pt}
6038
return $1$ if the \kbd{Flx} $f$ is a $k$-th power, $0$ otherwise.
6039
If \kbd{pt} is not \kbd{NULL}, set it to $g$ such that $g^k = f$.
6040
6041
\fun{GEN}{Flx_factor}{GEN f, ulong p} Assumes that $p$ is prime.
6042
6043
\fun{GEN}{Flx_ddf}{GEN f, ulong p} Assumes that $p$ is prime.
6044
6045
\fun{GEN}{Flx_factor_squarefree}{GEN f, ulong p} returns the squarefree
6046
factorization of $f$ modulo $p$. This is a vector $[u_1,\dots,u_k]$
6047
of pairwise coprime \kbd{Flx} such that $u_k \neq 1$ and $f = \prod u_i^i$.
6048
Shallow function. Assumes that $p$ is prime.
6049
6050
\fun{GEN}{Flx_mod_Xn1}{GEN T, ulong n, ulong p} return $T$ modulo
6051
$(X^n + 1, p)$. Shallow function.
6052
6053
\fun{GEN}{Flx_mod_Xnm1}{GEN T, ulong n, ulong p} return $T$ modulo
6054
$(X^n - 1, p)$. Shallow function.
6055
6056
\fun{GEN}{Flx_degfact}{GEN f, ulong p} as \tet{FpX_degfact}. Assumes that $p$
6057
is prime.
6058
6059
\fun{GEN}{Flx_factorff_irred}{GEN P, GEN Q, ulong p} as
6060
\tet{FpX_factorff_irred}. Assumes that $p$ is prime.
6061
6062
\fun{GEN}{Flx_rootsff}{GEN P, GEN T, ulong p} as \tet{FpX_rootsff}.
6063
Assumes that $p$ is prime.
6064
6065
\fun{GEN}{Flx_ffisom}{GEN P,GEN Q,ulong l} as \tet{FpX_ffisom}.
6066
Assumes that $p$ is prime.
6067
6068
\subsubsec{Miscellaneous operations}
6069
6070
\fun{GEN}{pol0_Flx}{long sv} returns a zero \kbd{Flx} in variable $v$.
6071
6072
\fun{GEN}{zero_Flx}{long sv} alias for \kbd{pol0\_Flx}
6073
6074
\fun{GEN}{pol1_Flx}{long sv} returns the unit \kbd{Flx} in variable $v$.
6075
6076
\fun{GEN}{polx_Flx}{long sv} returns the variable $v$ as degree~1~\kbd{Flx}.
6077
6078
\fun{GEN}{polxn_Flx}{long n, long sv} Returns the monomial of degree $n$ as
6079
a \kbd{Flx} in variable $v$; assume that $n \geq 0$.
6080
6081
\fun{GEN}{monomial_Flx}{ulong a, long d, long sv} returns the \kbd{Flx}
6082
$a\*X^d$ in variable $v$.
6083
6084
\fun{GEN}{init_Flxq}{ulong p, long n, long sv} returns an irreducible
6085
polynomial of degree $\kbd{n} > 0$ over $\F_p$, in variable \kbd{v}.
6086
6087
\fun{GEN}{Flx_normalize}{GEN z, ulong p}, as \kbd{FpX\_normalize}.
6088
6089
\fun{GEN}{Flx_rescale}{GEN P, ulong h, ulong p} returns $h^{\deg(P)} P(x/h)$,
6090
\kbd{P} is a \kbd{Flx} and \kbd{h} is a nonzero integer.
6091
6092
\fun{GEN}{random_Flx}{long d, long sv, ulong p} returns a random \kbd{Flx}
6093
in variable \kbd{v}, of degree less than~\kbd{d}.
6094
6095
\fun{GEN}{Flx_recip}{GEN x}, returns the reciprocal polynomial
6096
6097
\fun{ulong}{Flx_resultant}{GEN a, GEN b, ulong p}, returns the resultant
6098
of \kbd{a} and \kbd{b}. Assumes that $p$ is prime.
6099
6100
\fun{ulong}{Flx_extresultant}{GEN a, GEN b, ulong p, GEN *ptU, GEN *ptV}
6101
given two \kbd{Flx} \kbd{a} and \kbd{b},
6102
returns their resultant and sets Bezout coefficients (if the resultant is 0,
6103
the latter are not set). Assumes that $p$ is prime.
6104
6105
\fun{GEN}{Flx_invBarrett}{GEN T, ulong p}, returns the Barrett inverse
6106
$M$ of $T$ defined by $M(x)\*x^n\*T(1/x)\equiv 1\pmod{x^{n-1}}$ where $n$ is
6107
the degree of $T$. Assumes that $p$ is prime.
6108
6109
\fun{GEN}{Flx_renormalize}{GEN x, long l}, as \kbd{FpX\_renormalize}, where
6110
$\kbd{l} = \kbd{lg(x)}$, in place.
6111
6112
\fun{GEN}{Flx_shift}{GEN T, long n} returns $\kbd{T} * x^n$ if $n\geq 0$,
6113
and $\kbd{T} \bs x^{-n}$ otherwise.
6114
6115
\fun{long}{Flx_val}{GEN x} returns the valuation of \kbd{x}, i.e. the
6116
multiplicity of the $0$ root.
6117
6118
\fun{long}{Flx_valrem}{GEN x, GEN *Z} as \kbd{RgX\_valrem}, returns the
6119
valuation of \kbd{x}. In particular, if the valuation is $0$, set \kbd{*Z}
6120
to $x$, not a copy.
6121
6122
\fun{GEN}{Flx_div_by_X_x}{GEN A, ulong a, ulong p, ulong *rem}, returns the
6123
Euclidean quotient of the \kbd{Flx}~\kbd{A} by $X - \kbd{a}$, and sets
6124
\kbd{rem} to the remainder $ \kbd{A}(\kbd{a})$.
6125
6126
\fun{ulong}{Flx_eval}{GEN x, ulong y, ulong p}, as \kbd{FpX\_eval}.
6127
6128
\fun{ulong}{Flx_eval_pre}{GEN x, ulong y, ulong p, ulong pi}, as \kbd{Flx\_eval},
6129
assuming $pi$ is the pseudo inverse of $p$.
6130
6131
\fun{ulong}{Flx_eval_powers_pre}{GEN P, GEN y, ulong p, ulong pi}. Let $y$ be
6132
the \typ{VECSMALL} $(1,a,\dots,a^n)$, where $n$ is the degree of the
6133
\kbd{Flx} $P$, return $P(a)$, assuming $pi$ is the pseudo inverse of $p$.
6134
6135
\fun{GEN}{Flx_Flv_multieval}{GEN P, GEN v, ulong p} returns the vector
6136
$[P(v[1]),\ldots,P(v[n])]$ as a \kbd{Flv}.
6137
6138
\fun{ulong}{Flx_dotproduct}{GEN x, GEN y, ulong p} returns the scalar product
6139
of the coefficients of $x$ and $y$.
6140
6141
\fun{GEN}{Flx_deflate}{GEN P, long d} assuming $P$ is a polynomial of the
6142
form $Q(X^d)$, return $Q$.
6143
6144
\fun{GEN}{Flx_inflate}{GEN P, long d} returns $P(X^d)$.
6145
6146
\fun{GEN}{Flx_splitting}{GEN P, long k}, as \tet{RgX_splitting}.
6147
6148
\fun{GEN}{Flx_blocks}{GEN P, long n, long m}, as \tet{RgX_blocks}.
6149
6150
\fun{int}{Flx_is_squarefree}{GEN z, ulong p}. Assumes that $p$ is prime.
6151
6152
\fun{int}{Flx_is_irred}{GEN f, ulong p}, as \kbd{FpX\_is\_irred}.
6153
Assumes that $p$ is prime.
6154
6155
\fun{int}{Flx_is_totally_split}{GEN f, ulong p} returns $1$ if the
6156
\kbd{Flx}~\kbd{f} splits into a product of distinct linear factors, $0$
6157
otherwise. Assumes that \kbd{p} is prime.
6158
6159
\fun{int}{Flx_is_smooth}{GEN f, long r, ulong p} return $1$ if all
6160
irreducible factors of $f$ are of degree at most $r$, $0$ otherwise.
6161
Assumes that $p$ is prime.
6162
6163
\fun{long}{Flx_nbroots}{GEN f, ulong p}, as \kbd{FpX\_nbroots}.
6164
Assumes that $p$ is prime.
6165
6166
\fun{long}{Flx_nbfact}{GEN z, ulong p}, as \kbd{FpX\_nbfact}.
6167
Assumes that $p$ is prime.
6168
6169
\fun{long}{Flx_nbfact_Frobenius}{GEN f, GEN XP, ulong p},
6170
as \kbd{FpX\_nbfact\_Frobenius}. Assumes that $p$ is prime.
6171
6172
\fun{GEN}{Flx_degfact}{GEN f, ulong p}, as \kbd{FpX\_degfact}.
6173
Assumes that $p$ is prime.
6174
6175
\fun{GEN}{Flx_nbfact_by_degree}{GEN z, long *nb, ulong p} Assume
6176
that the \kbd{Flx} $z$ is squarefree mod the prime $p$. Returns a
6177
\typ{VECSMALL} $D$ with $\deg z$ entries, such that $D[i]$ is the number of
6178
irreducible factors of degree $i$. Set \kbd{nb} to the total number of
6179
irreducible factors (the sum of the $D[i]$).
6180
Assumes that $p$ is prime.
6181
6182
\fun{void}{Flx_ffintersect}{GEN P,GEN Q, long n, ulong p, GEN*SP, GEN*SQ, GEN
6183
MA,GEN MB},\hfil\break
6184
as \kbd{FpX\_ffintersect}. Assumes that $p$ is prime.
6185
6186
\fun{GEN}{Flx_Laplace}{GEN x, ulong p}
6187
6188
\fun{GEN}{Flx_invLaplace}{GEN x, ulong p}
6189
6190
\fun{GEN}{Flx_Newton}{GEN x, long n, ulong p}
6191
6192
\fun{GEN}{Flx_fromNewton}{GEN x, ulong p}
6193
6194
\fun{GEN}{Flx_Teichmuller}{GEN P, ulong p, long n}
6195
Return a \kbd{ZX} $Q$ such that $P\equiv Q\pmod{p}$ and
6196
$Q(X^p)=0\pmod{Q,p^n}$. Assumes that $p$ is prime.
6197
6198
\fun{GEN}{Flv_polint}{GEN x, GEN y, ulong p, long sv} as \kbd{FpV\_polint},
6199
returning an \kbd{Flx} in variable $v$. Assumes that $p$ is prime.
6200
6201
\fun{GEN}{Flv_Flm_polint}{GEN x, GEN V, ulong p, long sv} equivalent (but
6202
faster) to applying \kbd{Flv\_polint(x,$\ldots$)} to all the elements of the
6203
vector $V$ (thus, returns a \kbd{FlxV}). Assumes that $p$ is prime.
6204
6205
\fun{GEN}{Flv_invVandermonde}{GEN L, ulong d, ulong p} $L$ being a \kbd{Flv}
6206
of length $n$, return the inverse $M$ of the Vandermonde matrix attached to
6207
the elements of $L$, multiplied by \kbd{d}.
6208
If $A$ is a \kbd{Flv} and $B=M\*A$, then the polynomial
6209
$P=\sum_{i=1}^n B[i]\*X^{i-1}$ verifies $P(L[i])=d\*A[i]$ for
6210
$1 \leq i \leq n$. Assumes that $p$ is prime.
6211
6212
\fun{GEN}{Flv_roots_to_pol}{GEN a, ulong p, long sv} as
6213
\kbd{FpV\_roots\_to\_pol} returning an \kbd{Flx} in variable $v$.
6214
6215
\subsec{\kbd{FlxV}} See \kbd{FpXV} operations.
6216
6217
\fun{GEN}{FlxV_Flc_mul}{GEN V, GEN W, ulong p}, as \kbd{FpXV\_FpC\_mul}.
6218
6219
\fun{GEN}{FlxV_red}{GEN V, ulong p} reduces each components with \kbd{Flx\_red}.
6220
6221
\fun{GEN}{FlxV_prod}{GEN V, ulong p}, \kbd{V} being a vector of \kbd{Flx},
6222
returns their product.
6223
6224
\fun{ulong}{FlxC_eval_powers_pre}{GEN x, GEN y, ulong p, ulong pi}
6225
apply \kbd{Flx\_eval\_powers\_pre} to all elements of \kbd{x}.
6226
6227
\fun{GEN}{FlxV_Flv_multieval}{GEN F, GEN v, ulong p} returns the vector
6228
$[[F[1](v[1]),\ldots,F[1](v[n])],\ldots,[F[m](v[1]),\ldots,F[m](v[n])]]$
6229
as a \kbd{FlvV}.
6230
6231
\fun{GEN}{FlxC_neg}{GEN x, ulong p}
6232
6233
\fun{GEN}{FlxC_sub}{GEN x, GEN y, ulong p}
6234
6235
\fun{GEN}{zero_FlxC}{long n, long sv}
6236
6237
\subsec{\kbd{FlxM}} See \kbd{FpXM} operations.
6238
6239
\fun{ulong}{FlxM_eval_powers_pre}{GEN M, GEN y, ulong p, ulong pi}
6240
this function applies \kbd{FlxC\_eval\_powers\_pre} to all entries of \kbd{M}.
6241
6242
\fun{GEN}{FlxM_neg}{GEN x, ulong p}
6243
6244
\fun{GEN}{FlxM_sub}{GEN x, GEN y, ulong p}
6245
6246
\fun{GEN}{zero_FlxM}{long r, long c, long sv}
6247
6248
\subsec{\kbd{FlxT}} See \kbd{FpXT} operations.
6249
6250
\fun{GEN}{FlxT_red}{GEN V, ulong p} reduces each leaf with \kbd{Flx\_red}.
6251
6252
\subsec{\kbd{Flxn}} See \kbd{FpXn} operations.
6253
6254
\fun{GEN}{Flxn_inv}{GEN a, long n, ulong p}
6255
returns $1/a$ modulo $X^n$.
6256
6257
\fun{GEN}{Flxn_mul}{GEN a, GEN b, long n, ulong p}
6258
returns $a\*b$ modulo $X^n$.
6259
6260
\fun{GEN}{Flxn_sqr}{GEN a, long n, ulong p}
6261
returns $a^2$ modulo $X^n$.
6262
6263
\fun{GEN}{Flxn_red}{GEN a, long n}
6264
returns $a$ modulo $X^n$.
6265
6266
\fun{GEN}{Flxn_exp}{GEN x, long n, ulong p} return $\exp(x)$
6267
as a composition of formal power series.
6268
It is required that the valuation of $x$ is positive and that $p>n$.
6269
6270
\fun{GEN}{Flxn_expint}{GEN f, long n, ulong p} return $\exp(F)$
6271
where $F$ is the primitive of $f$ that vanishes at $0$.
6272
It is required that $p>n$.
6273
6274
\subsec{\kbd{Flxq}} See \kbd{FpXQ} operations.
6275
6276
\fun{GEN}{Flxq_add}{GEN x, GEN y, GEN T, ulong p}
6277
6278
\fun{GEN}{Flxq_sub}{GEN x, GEN y, GEN T, ulong p}
6279
6280
\fun{GEN}{Flxq_mul}{GEN x, GEN y, GEN T, ulong p}
6281
6282
\fun{GEN}{Flxq_sqr}{GEN y, GEN T, ulong p}
6283
6284
\fun{GEN}{Flxq_inv}{GEN x, GEN T, ulong p}
6285
6286
\fun{GEN}{Flxq_invsafe}{GEN x, GEN T, ulong p}
6287
6288
\fun{GEN}{Flxq_div}{GEN x, GEN y, GEN T, ulong p}
6289
6290
\fun{GEN}{Flxq_pow}{GEN x, GEN n, GEN T, ulong p}
6291
6292
\fun{GEN}{Flxq_powu}{GEN x, ulong n, GEN T, ulong p}
6293
6294
\fun{GEN}{FlxqV_factorback}{GEN L, GEN e, GEN Tp, ulong p}
6295
6296
\fun{GEN}{Flxq_pow_init}{GEN x, GEN n, long k, GEN T, ulong p}
6297
6298
\fun{GEN}{Flxq_pow_table}{GEN R, GEN n, GEN T, ulong p}
6299
6300
\fun{GEN}{Flxq_powers}{GEN x, long n, GEN T, ulong p}
6301
6302
\fun{GEN}{Flxq_matrix_pow}{GEN x, long m, long n, GEN T, ulong p},
6303
see \kbd{FpXQ\_matrix\_pow}.
6304
6305
\fun{GEN}{Flxq_autpow}{GEN a, long n, GEN T, ulong p}
6306
see \kbd{FpXQ\_autpow}.
6307
6308
\fun{GEN}{Flxq_autpowers}{GEN a, long n, GEN T, ulong p}
6309
return $[X,\sigma(X),\ldots,\sigma^n(X)]$,
6310
assuming $a=\sigma(X)$ where $\sigma$ is an automorphism of the algebra
6311
$\F_p[X]/T(X)$.
6312
6313
\fun{GEN}{Flxq_autsum}{GEN a, long n, GEN T, ulong p}
6314
see \kbd{FpXQ\_autsum}.
6315
6316
\fun{GEN}{Flxq_auttrace}{GEN a, ulong n, GEN T, ulong p}
6317
see \kbd{FpXQ\_auttrace}.
6318
6319
\fun{GEN}{Flxq_ffisom_inv}{GEN S, GEN T, ulong p}, as \kbd{FpXQ\_ffisom\_inv}.
6320
6321
\fun{GEN}{Flx_Flxq_eval}{GEN f, GEN x, GEN T, ulong p} returns
6322
$\kbd{f}(\kbd{x})$.
6323
6324
\fun{GEN}{Flx_FlxqV_eval}{GEN f, GEN x, GEN T, ulong p},
6325
see \kbd{FpX\_FpXQV\_eval}.
6326
6327
\fun{GEN}{FlxC_Flxq_eval}{GEN C, GEN x, GEN T, ulong p},
6328
see \kbd{FpXC\_FpXQ\_eval}.
6329
6330
\fun{GEN}{FlxC_FlxqV_eval}{GEN C, GEN V, GEN T, ulong p}
6331
see \kbd{FpXC\_FpXQV\_eval}.
6332
6333
\fun{GEN}{FlxqV_roots_to_pol}{GEN V, GEN T, ulong p, long v} as
6334
\kbd{FqV\_roots\_to\_pol} returning an \kbd{FlxqX} in variable $v$.
6335
6336
\fun{int}{Flxq_issquare}{GEN x, GEN T, ulong p} returns $1$ if $x$ is a square
6337
and $0$ otherwise. Assume that \kbd{T} is irreducible mod \kbd{p}.
6338
6339
\fun{int}{Flxq_is2npower}{GEN x, long n, GEN T, ulong p} returns $1$ if $x$ is
6340
a $2^n$-th power and $0$ otherwise. Assume that \kbd{T} is irreducible mod
6341
\kbd{p}.
6342
6343
\fun{GEN}{Flxq_order}{GEN a, GEN ord, GEN T, ulong p}
6344
as \tet{FpXQ_order}.
6345
6346
\fun{GEN}{Flxq_log}{GEN a, GEN g, GEN ord, GEN T, ulong p}
6347
as \tet{FpXQ_log}
6348
6349
\fun{GEN}{Flxq_sqrtn}{GEN x, GEN n, GEN T, ulong p, GEN *zn} as
6350
\tet{FpXQ_sqrtn}.
6351
6352
\fun{GEN}{Flxq_sqrt}{GEN x, GEN T, ulong p} returns a square root of \kbd{x}.
6353
Return \kbd{NULL} if \kbd{x} is not a square.
6354
6355
\fun{GEN}{Flxq_lroot}{GEN a, GEN T, ulong p} returns $x$ such that $x^p = a$.
6356
6357
\fun{GEN}{Flxq_lroot_fast}{GEN a, GEN V, GEN T, ulong p} assuming that
6358
\kbd{V=Flxq\_powers(s,p-1,T,p)} where $s(x)^p \equiv x\pmod{T(x),p}$,
6359
returns $b$ such that $b^p=a$. Only useful if $p$ is less than the degree of
6360
$T$.
6361
6362
\fun{GEN}{Flxq_charpoly}{GEN x, GEN T, ulong p} returns the characteristic
6363
polynomial of \kbd{x}
6364
6365
\fun{GEN}{Flxq_minpoly}{GEN x, GEN T, ulong p} returns the minimal polynomial
6366
of \kbd{x}
6367
6368
\fun{ulong}{Flxq_norm}{GEN x, GEN T, ulong p} returns the norm of \kbd{x}
6369
6370
\fun{ulong}{Flxq_trace}{GEN x, GEN T, ulong p} returns the trace of \kbd{x}
6371
6372
\fun{GEN}{Flxq_conjvec}{GEN x, GEN T, ulong p} returns the conjugates
6373
$[x,x^p,x^{p^2},\ldots,x^{p^{n-1}}]$ where $n$ is the degree of $T$.
6374
6375
\fun{GEN}{gener_Flxq}{GEN T, ulong p, GEN *po} returns a primitive root modulo
6376
$(T,p)$. $T$ is an \kbd{Flx} assumed to be irreducible modulo the prime
6377
$p$. If \kbd{po} is not \kbd{NULL} it is set to $[o,\var{fa}]$, where $o$ is the
6378
order of the multiplicative group of the finite field, and \var{fa} is
6379
its factorization.
6380
6381
\subsec{\kbd{FlxX}} See \kbd{FpXX} operations.
6382
6383
\fun{GEN}{pol1_FlxX}{long vX, long sx} returns the unit \kbd{FlxX} as a
6384
\typ{POL} in variable \kbd{vX} which only coefficient is \kbd{pol1\_Flx(sx)}.
6385
6386
\fun{GEN}{polx_FlxX}{long vX, long sx} returns the variable $X$ as a
6387
degree~1~\typ{POL} with \kbd{Flx} coefficients in the variable $x$.
6388
6389
\fun{long}{FlxY_degreex}{GEN P} return the degree of $P$ with respect to
6390
the secondary variable.
6391
6392
\fun{GEN}{FlxX_add}{GEN P, GEN Q, ulong p}
6393
6394
\fun{GEN}{FlxX_sub}{GEN P, GEN Q, ulong p}
6395
6396
\fun{GEN}{FlxX_Fl_mul}{GEN x, ulong y, ulong p}
6397
6398
\fun{GEN}{FlxX_double}{GEN x, ulong p}
6399
6400
\fun{GEN}{FlxX_triple}{GEN x, ulong p}
6401
6402
\fun{GEN}{FlxX_neg}{GEN x, ulong p}
6403
6404
\fun{GEN}{FlxX_Flx_add}{GEN x, GEN y, ulong p}
6405
6406
\fun{GEN}{FlxX_Flx_sub}{GEN x, GEN y, ulong p}
6407
6408
\fun{GEN}{FlxX_Flx_mul}{GEN x, GEN y, ulong p}
6409
6410
\fun{GEN}{FlxY_Flx_div}{GEN x, GEN y, ulong p} divides the coefficients of $x$
6411
by $y$ using \kbd{Flx\_div}.
6412
6413
\fun{GEN}{FlxX_deriv}{GEN P, ulong p} returns the derivative of \kbd{P} with
6414
respect to the main variable.
6415
6416
\fun{GEN}{FlxX_Laplace}{GEN x, ulong p}
6417
6418
\fun{GEN}{FlxX_invLaplace}{GEN x, ulong p}
6419
6420
\fun{GEN}{FlxY_evalx}{GEN P, ulong z, ulong p} $P$ being an \kbd{FlxY}, returns
6421
the \kbd{Flx} $P(z,Y)$, where $Y$ is the main variable of $P$.
6422
6423
\fun{GEN}{FlxX_translate1}{GEN P, ulong p, long n} $P$ being an \kbd{FlxX} with
6424
all coefficients of degree at most $n$, return $(P(x,Y+1)$, where $Y$ is the
6425
main variable of $P$.
6426
6427
\fun{GEN}{zlxX_translate1}{GEN P, ulong p, long e, long n}
6428
$P$ being an \kbd{zlxX} with all coefficients of degree at most $n$, return
6429
$(P(x,Y+1)$ modulo $p^e$ for prime $p$ , where $Y$ is the main variable of
6430
$P$.
6431
6432
\fun{GEN}{FlxY_Flx_translate}{GEN P, GEN f, ulong p} $P$ being an \kbd{FlxY} and $f$
6433
being an \kbd{Flx}, return $(P(x,Y+f(x))$, where $Y$ is the main variable of $P$.
6434
6435
\fun{ulong}{FlxY_evalx_powers_pre}{GEN P, GEN xp, ulong p, ulong pi}, \kbd{xp}
6436
being the vector $[1,x,\dots,x^n]$, where $n$ is larger or equal to the degree
6437
of $P$ in $X$, return $P(x,Y)$, where $Y$ is the main variable of $Q$, assuming
6438
$pi$ is the pseudo inverse of $p$.
6439
6440
\fun{ulong}{FlxY_eval_powers_pre}{GEN P, GEN xp, GEN yp, ulong p, ulong pi},
6441
\kbd{xp} being the vector $[1,x,\dots,x^n]$, where $n$ is larger or equal to the degree
6442
of $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)$, assuming
6443
$pi$ is the pseudo inverse of $p$.
6444
6445
\fun{GEN}{FlxY_Flxq_evalx}{GEN x, GEN y, GEN T, ulong p} as \kbd{FpXY\_FpXQ\_evalx}.
6446
6447
\fun{GEN}{FlxY_FlxqV_evalx}{GEN x, GEN V, GEN T, ulong p} as \kbd{FpXY\_FpXQV\_evalx}.
6448
6449
\fun{GEN}{FlxX_renormalize}{GEN x, long l}, as \kbd{normalizepol}, where
6450
$\kbd{l} = \kbd{lg(x)}$, in place.
6451
6452
\fun{GEN}{FlxX_resultant}{GEN u, GEN v, ulong p, long sv} Returns
6453
$\text{Res}_X(u, v)$, which is an \kbd{Flx}. The coefficients of \kbd{u}
6454
and \kbd{v} are assumed to be in the variable $v$.
6455
6456
\fun{GEN}{Flx_FlxY_resultant}{GEN a, GEN b, ulong p}
6457
Returns $\text{Res}_x(a, b)$, which is an \kbd{Flx}
6458
in the main variable of \kbd{b}.
6459
6460
\fun{GEN}{FlxX_blocks}{GEN P, long n, long m, long sv}, as \tet{RgX_blocks},
6461
where $v$ is the secondary variable.
6462
6463
\fun{GEN}{FlxX_shift}{GEN a, long n, long sv}, as \kbd{RgX\_shift\_shallow},
6464
where $v$ is the secondary variable.
6465
6466
\fun{GEN}{FlxX_swap}{GEN x, long n, long ws}, as \kbd{RgXY\_swap}.
6467
6468
\fun{GEN}{FlxYqq_pow}{GEN x, GEN n, GEN S, GEN T, ulong p}, as
6469
\kbd{FpXYQQ\_pow}.
6470
6471
\subsec{\kbd{FlxqX}} See \kbd{FpXQX} operations.
6472
6473
\subsubsec{Preconditioned reduction}
6474
6475
For faster reduction, the modulus \kbd{S} can be replaced by an extended
6476
modulus, which is an \kbd{FlxqXT}, in all \kbd{FlxqXQ}-classes
6477
functions, and in \kbd{FlxqX\_rem} and \kbd{FlxqX\_divrem}.
6478
6479
\fun{GEN}{FlxqX_get_red}{GEN S, GEN T, ulong p} returns the extended modulus
6480
\kbd{eS}.
6481
6482
To write code that works both with plain and extended moduli, the following
6483
accessors are defined:
6484
6485
\fun{GEN}{get_FlxqX_mod}{GEN eS} returns the underlying modulus \kbd{S}.
6486
6487
\fun{GEN}{get_FlxqX_var}{GEN eS} returns the variable number of the modulus.
6488
6489
\fun{GEN}{get_FlxqX_degree}{GEN eS} returns the degree of the modulus.
6490
6491
\subsubsec{basic functions}
6492
6493
\fun{GEN}{random_FlxqX}{long d, long v, GEN T, ulong p} returns a random
6494
\kbd{FlxqX} in variable \kbd{v}, of degree less than~\kbd{d}.
6495
6496
\fun{GEN}{zxX_to_Kronecker}{GEN P, GEN Q} assuming $P(X,Y)$ is a polynomial
6497
of degree in $X$ strictly less than $n$, returns $P(X,X^{2*n-1})$, the
6498
Kronecker form of $P$.
6499
6500
\fun{GEN}{Kronecker_to_FlxqX}{GEN z, GEN T, ulong p}. Let $n = \deg T$ and let
6501
$P(X,Y)\in \Z[X,Y]$ lift a polynomial in $K[Y]$, where $K := \F_p[X]/(T)$ and
6502
$\deg_X P < 2n-1$ --- such as would result from multiplying minimal degree
6503
lifts of two polynomials in $K[Y]$. Let $z = P(t,t^{2*n-1})$ be a Kronecker
6504
form of $P$, this function returns $Q\in \Z[X,t]$ such that $Q$ is congruent to
6505
$P(X,t)$ mod $(p, T(X))$, $\deg_X Q < n$, and all coefficients are in $[0,p[$.
6506
Not stack-clean. Note that $t$ need not be the same variable as $Y$!
6507
6508
\fun{GEN}{FlxqX_red}{GEN z, GEN T, ulong p}
6509
6510
\fun{GEN}{FlxqX_normalize}{GEN z, GEN T, ulong p}
6511
6512
\fun{GEN}{FlxqX_mul}{GEN x, GEN y, GEN T, ulong p}
6513
6514
\fun{GEN}{FlxqX_Flxq_mul}{GEN P, GEN U, GEN T, ulong p}
6515
6516
\fun{GEN}{FlxqX_Flxq_mul_to_monic}{GEN P, GEN U, GEN T, ulong p}
6517
returns $P*U$ assuming the result is monic of the same degree as $P$ (in
6518
particular $U\neq 0$).
6519
6520
\fun{GEN}{FlxqX_sqr}{GEN x, GEN T, ulong p}
6521
6522
\fun{GEN}{FlxqX_powu}{GEN x, ulong n, GEN T, ulong p}
6523
6524
\fun{GEN}{FlxqX_divrem}{GEN x, GEN y, GEN T, ulong p, GEN *pr}
6525
6526
\fun{GEN}{FlxqX_div}{GEN x, GEN y, GEN T, ulong p}
6527
6528
\fun{GEN}{FlxqX_rem}{GEN x, GEN y, GEN T, ulong p}
6529
6530
\fun{GEN}{FlxqX_invBarrett}{GEN T, GEN Q, ulong p}
6531
6532
\fun{GEN}{FlxqX_gcd}{GEN x, GEN y, ulong p} returns a (not necessarily monic)
6533
greatest common divisor of $x$ and $y$.
6534
6535
\fun{GEN}{FlxqX_extgcd}{GEN x, GEN y, GEN T, ulong p, GEN *ptu, GEN *ptv}
6536
6537
\fun{GEN}{FlxqX_halfgcd}{GEN x, GEN y, GEN T, ulong p}, see \kbd{FpX\_halfgcd}.
6538
6539
\fun{GEN}{FlxqX_resultant}{GEN x, GEN y, GEN T, ulong p}
6540
6541
\fun{GEN}{FlxqX_saferesultant}{GEN P, GEN Q, GEN T, ulong p} Returns the
6542
resultant of $P$ and $Q$ if Euclid's algorithm succeeds and \kbd{NULL} otherwise.
6543
In particular, if $p$ is not prime or $T$ is not irreducible over $\F_p[X]$, the
6544
routine may still be used (but will fail if noninvertible leading terms
6545
occur).
6546
6547
\fun{GEN}{FlxqX_disc}{GEN x, GEN T, ulong p}
6548
6549
\fun{GEN}{FlxqXV_prod}{GEN V, GEN T, ulong p}
6550
6551
\fun{GEN}{FlxqX_safegcd}{GEN P, GEN Q, GEN T, ulong p} Returns the \emph{monic}
6552
GCD of $P$ and $Q$ if Euclid's algorithm succeeds and \kbd{NULL} otherwise. In
6553
particular, if $p$ is not prime or $T$ is not irreducible over $\F_p[X]$, the
6554
routine may still be used (but will fail if noninvertible leading terms
6555
occur).
6556
6557
\fun{GEN}{FlxqX_dotproduct}{GEN x, GEN y, GEN T, ulong p} returns the scalar
6558
product of the coefficients of $x$ and $y$.
6559
6560
\fun{GEN}{FlxqX_Newton}{GEN x, long n, GEN T, ulong p}
6561
6562
\fun{GEN}{FlxqX_fromNewton}{GEN x, GEN T, ulong p}
6563
6564
\fun{long}{FlxqX_is_squarefree}{GEN S, GEN T, ulong p}, as
6565
\kbd{FpX\_is\_squarefree}.
6566
6567
\fun{long}{FlxqX_ispower}{GEN f, ulong k, GEN T, ulong p, GEN *pt}
6568
return $1$ if the \kbd{FlxqX} $f$ is a $k$-th power, $0$ otherwise.
6569
If \kbd{pt} is not \kbd{NULL}, set it to $g$ such that $g^k = f$.
6570
6571
\fun{GEN}{FlxqX_Frobenius}{GEN S, GEN T, ulong p}, as \kbd{FpXQX\_Frobenius}
6572
6573
\fun{GEN}{FlxqX_roots}{GEN f, GEN T, ulong p} return the roots of \kbd{f} in
6574
$\F_p[X]/(T)$. Assumes \kbd{p} is prime and \kbd{T} irreducible in $\F_p[X]$.
6575
6576
\fun{GEN}{FlxqX_factor}{GEN f, GEN T, ulong p} return the factorization of
6577
\kbd{f} over $\F_p[X]/(T)$. Assumes \kbd{p} is prime and \kbd{T} irreducible
6578
in $\F_p[X]$.
6579
6580
\fun{GEN}{FlxqX_factor_squarefree}{GEN f, GEN T, ulong p} returns the squarefree
6581
factorization of $f$, see \kbd{FpX\_factor\_squarefree}.
6582
6583
\fun{GEN}{FlxqX_ddf}{GEN f, GEN T, ulong p} as \kbd{FpX\_ddf}.
6584
6585
\fun{long}{FlxqX_ddf_degree}{GEN f, GEN XP, GEN T, GEN p},
6586
as \kbd{FpX\_ddf\_degree}.
6587
6588
\fun{GEN}{FlxqX_degfact}{GEN f, GEN T, ulong p}, as \kbd{FpX\_degfact}.
6589
6590
\fun{long}{FlxqX_nbroots}{GEN S, GEN T, ulong p}, as \kbd{FpX\_nbroots}.
6591
6592
\fun{long}{FlxqX_nbfact}{GEN S, GEN T, ulong p}, as \kbd{FpX\_nbfact}.
6593
6594
\fun{long}{FlxqX_nbfact_Frobenius}{GEN S, GEN Xq, GEN T, ulong p},
6595
as \kbd{FpX\_nbfact\_Frobenius}.
6596
6597
\fun{GEN}{FlxqX_FlxqXQ_eval}{GEN Q, GEN x, GEN S, GEN T, ulong p} as
6598
\kbd{FpX\_FpXQ\_eval}.
6599
6600
\fun{GEN}{FlxqX_FlxqXQV_eval}{GEN P, GEN V, GEN S, GEN T, ulong p} as
6601
\kbd{FpX\_FpXQV\_eval}.
6602
6603
\subsec{\kbd{FlxqXQ}} See \kbd{FpXQXQ} operations.
6604
6605
\fun{GEN}{FlxqXQ_mul}{GEN x, GEN y, GEN S, GEN T, ulong p}
6606
6607
\fun{GEN}{FlxqXQ_sqr}{GEN x, GEN S, GEN T, ulong p}
6608
6609
\fun{GEN}{FlxqXQ_inv}{GEN x, GEN S, GEN T, ulong p}
6610
6611
\fun{GEN}{FlxqXQ_invsafe}{GEN x, GEN S, GEN T, ulong p}
6612
6613
\fun{GEN}{FlxqXQ_div}{GEN x, GEN y, GEN S, GEN T, ulong p}
6614
6615
\fun{GEN}{FlxqXQ_pow}{GEN x, GEN n, GEN S, GEN T, ulong p}
6616
6617
\fun{GEN}{FlxqXQ_powu}{GEN x, ulong n, GEN S, GEN T, ulong p}
6618
6619
\fun{GEN}{FlxqXQ_powers}{GEN x, long n, GEN S, GEN T, ulong p}
6620
6621
\fun{GEN}{FlxqXQ_matrix_pow}{GEN x, long n, long m, GEN S, GEN T, ulong p}
6622
6623
\fun{GEN}{FlxqXQ_autpow}{GEN a, long n, GEN S, GEN T, ulong p}
6624
as \kbd{FpXQXQ\_autpow}
6625
6626
\fun{GEN}{FlxqXQ_autsum}{GEN a, long n, GEN S, GEN T, ulong p}
6627
as \kbd{FpXQXQ\_autsum}
6628
6629
\fun{GEN}{FlxqXQ_auttrace}{GEN a, long n, GEN S, GEN T, ulong p}
6630
as \kbd{FpXQXQ\_auttrace}
6631
6632
\fun{GEN}{FlxqXQ_halfFrobenius}{GEN A, GEN S, GEN T, ulong p}, as
6633
\kbd{FpXQXQ\_halfFrobenius}
6634
6635
\fun{GEN}{FlxqXQ_minpoly}{GEN x, GEN S, GEN T, ulong p}, as
6636
\kbd{FpXQ\_minpoly}
6637
6638
\subsec{\kbd{FlxqXn}} See \kbd{FpXn} operations.
6639
6640
\fun{GEN}{FlxXn_red}{GEN a, long n} returns $a$ modulo $X^n$.
6641
6642
\fun{GEN}{FlxqXn_mul}{GEN a, GEN b, long n, GEN T, ulong p}
6643
6644
\fun{GEN}{FlxqXn_sqr}{GEN a, long n, GEN T, ulong p}
6645
6646
\fun{GEN}{FlxqXn_inv}{GEN a, long n, GEN T, ulong p}
6647
6648
\fun{GEN}{FlxqXn_expint}{GEN a, long n, GEN T, ulong p}
6649
6650
\subsec{\kbd{F2x}} An \kbd{F2x}~\kbd{z} is a \typ{VECSMALL}
6651
representing a polynomial over $\F_2[X]$. Specifically
6652
\kbd{z[0]} is the usual codeword, \kbd{z[1] = evalvarn($v$)} for some
6653
variable $v$ and the coefficients are given by the bits of remaining
6654
words by increasing degree.
6655
6656
\subsubsec{Preconditioned reduction}
6657
6658
For faster reduction, the modulus \kbd{T} can be replaced by an extended
6659
modulus (\kbd{FlxT}) in all \kbd{Flxq}-classes functions, and in
6660
\kbd{Flx\_divrem}.
6661
6662
\fun{GEN}{F2x_get_red}{GEN T} returns the extended modulus \kbd{eT}.
6663
6664
To write code that works both with plain and extended moduli, the following
6665
accessors are defined:
6666
6667
\fun{GEN}{get_F2x_mod}{GEN eT} returns the underlying modulus \kbd{T}.
6668
6669
\fun{GEN}{get_F2x_var}{GEN eT} returns the variable number of the modulus.
6670
6671
\fun{GEN}{get_F2x_degree}{GEN eT} returns the degree of the modulus.
6672
6673
\subsubsec{Basic operations}
6674
6675
\fun{ulong}{F2x_coeff}{GEN x, long i} returns the coefficient $i\ge 0$ of $x$.
6676
6677
\fun{void}{F2x_clear}{GEN x, long i} sets the coefficient $i\ge 0$ of $x$ to
6678
$0$.
6679
6680
\fun{void}{F2x_flip}{GEN x, long i} adds $1$ to the coefficient $i\ge 0$ of $x$.
6681
6682
\fun{void}{F2x_set}{GEN x, long i} sets the coefficient $i\ge 0$ of $x$ to $1$.
6683
6684
\fun{GEN}{F2x_copy}{GEN x}
6685
6686
\fun{GEN}{Flx_to_F2x}{GEN x}
6687
6688
\fun{GEN}{Z_to_F2x}{GEN x, long sv}
6689
6690
\fun{GEN}{ZX_to_F2x}{GEN x}
6691
6692
\fun{GEN}{F2v_to_F2x}{GEN x, long sv}
6693
6694
\fun{GEN}{F2x_to_Flx}{GEN x}
6695
6696
\fun{GEN}{F2x_to_F2xX}{GEN x, long sv}
6697
6698
\fun{GEN}{F2x_to_ZX}{GEN x}
6699
6700
\fun{GEN}{pol0_F2x}{long sv} returns a zero \kbd{F2x} in variable $v$.
6701
6702
\fun{GEN}{zero_F2x}{long sv} alias for \kbd{pol0\_F2x}.
6703
6704
\fun{GEN}{pol1_F2x}{long sv} returns the \kbd{F2x} in variable $v$ constant to
6705
$1$.
6706
6707
\fun{GEN}{polx_F2x}{long sv} returns the variable $v$ as degree~1~\kbd{F2x}.
6708
6709
\fun{GEN}{monomial_F2x}{long d, long sv} returns the \kbd{F2x}
6710
$X^d$ in variable $v$.
6711
6712
\fun{GEN}{random_F2x}{long d, long sv} returns a random \kbd{F2x}
6713
in variable \kbd{v}, of degree less than~\kbd{d}.
6714
6715
\fun{long}{F2x_degree}{GEN x} returns the degree of the \kbd{F2x x}. The
6716
degree of $0$ is defined as $-1$.
6717
6718
\fun{GEN}{F2x_recip}{GEN x}
6719
6720
\fun{int}{F2x_equal1}{GEN x}
6721
6722
\fun{int}{F2x_equal}{GEN x, GEN y}
6723
6724
\fun{GEN}{F2x_1_add}{GEN y} returns \kbd{y+1} where \kbd{y} is a \kbd{Flx}.
6725
6726
\fun{GEN}{F2x_add}{GEN x, GEN y}
6727
6728
\fun{GEN}{F2x_mul}{GEN x, GEN y}
6729
6730
\fun{GEN}{F2x_sqr}{GEN x}
6731
6732
\fun{GEN}{F2x_divrem}{GEN x, GEN y, GEN *pr}
6733
6734
\fun{GEN}{F2x_rem}{GEN x, GEN y}
6735
6736
\fun{GEN}{F2x_div}{GEN x, GEN y}
6737
6738
\fun{GEN}{F2x_renormalize}{GEN x, long lx}
6739
6740
\fun{GEN}{F2x_deriv}{GEN x}
6741
6742
\fun{GEN}{F2x_deflate}{GEN x, long d}
6743
6744
\fun{ulong}{F2x_eval}{GEN P, ulong u} returns $P(u)$.
6745
6746
\fun{void}{F2x_shift}{GEN x, long d} as \tet{RgX_shift}
6747
6748
\fun{void}{F2x_even_odd}{GEN P, GEN *pe, GEN *po} as \tet{RgX_even_odd}
6749
6750
\fun{long}{F2x_valrem}{GEN x, GEN *Z}
6751
6752
\fun{GEN}{F2x_extgcd}{GEN a, GEN b, GEN *ptu, GEN *ptv}
6753
6754
\fun{GEN}{F2x_gcd}{GEN a, GEN b}
6755
6756
\fun{GEN}{F2x_halfgcd}{GEN a, GEN b}
6757
6758
\fun{int}{F2x_issquare}{GEN x} returns $1$ if $x$ is a square of a \kbd{F2x}
6759
and $0$ otherwise.
6760
6761
\fun{int}{F2x_is_irred}{GEN f}, as \tet{FpX_is_irred}.
6762
6763
\fun{GEN}{F2x_degfact}{GEN f} as \tet{FpX_degfact}.
6764
6765
\fun{GEN}{F2x_sqrt}{GEN x} returns the squareroot of $x$, assuming $x$ is a
6766
square of a \kbd{F2x}.
6767
6768
\fun{GEN}{F2x_Frobenius}{GEN T}
6769
6770
\fun{GEN}{F2x_matFrobenius}{GEN T}
6771
6772
\fun{GEN}{F2x_factor}{GEN f}
6773
6774
\fun{GEN}{F2x_factor_squarefree}{GEN f}
6775
6776
\fun{GEN}{F2x_ddf}{GEN f}
6777
6778
\fun{GEN}{F2x_Teichmuller}{GEN P, long n}
6779
Return a \kbd{ZX} $Q$ such that $P\equiv Q\pmod{2}$ and
6780
$Q(X^p)=0\pmod{Q,2^n}$.
6781
6782
\subsec{\kbd{F2xq}} See \kbd{FpXQ} operations.
6783
6784
\fun{GEN}{F2xq_mul}{GEN x, GEN y, GEN T}
6785
6786
\fun{GEN}{F2xq_sqr}{GEN x, GEN T}
6787
6788
\fun{GEN}{F2xq_div}{GEN x,GEN y,GEN T}
6789
6790
\fun{GEN}{F2xq_inv}{GEN x, GEN T}
6791
6792
\fun{GEN}{F2xq_invsafe}{GEN x, GEN T}
6793
6794
\fun{GEN}{F2xq_pow}{GEN x, GEN n, GEN T}
6795
6796
\fun{GEN}{F2xq_powu}{GEN x, ulong n, GEN T}
6797
6798
\fun{GEN}{F2xq_pow_init}{GEN x, GEN n, long k, GEN T}
6799
6800
\fun{GEN}{F2xq_pow_table}{GEN R, GEN n, GEN T}
6801
6802
\fun{ulong}{F2xq_trace}{GEN x, GEN T}
6803
6804
\fun{GEN}{F2xq_conjvec}{GEN x, GEN T} returns the vector of conjugates
6805
$[x,x^2,x^{2^2},\ldots,x^{2^{n-1}}]$ where $n$ is the degree of $T$.
6806
6807
\fun{GEN}{F2xq_log}{GEN a, GEN g, GEN ord, GEN T}
6808
6809
\fun{GEN}{F2xq_order}{GEN a, GEN ord, GEN T}
6810
6811
\fun{GEN}{F2xq_Artin_Schreier}{GEN a, GEN T} returns a solution of $x^2+x=a$,
6812
assuming it exists.
6813
6814
\fun{GEN}{F2xq_sqrt}{GEN a, GEN T}
6815
6816
\fun{GEN}{F2xq_sqrt_fast}{GEN a, GEN s, GEN T} assuming that
6817
$s^2 \equiv x\pmod{T(x)}$, computes $b \equiv a(s)\pmod{T}$ so that $b^2=a$.
6818
6819
\fun{GEN}{F2xq_sqrtn}{GEN a, GEN n, GEN T, GEN *zeta}
6820
6821
\fun{GEN}{gener_F2xq}{GEN T, GEN *po}
6822
6823
\fun{GEN}{F2xq_powers}{GEN x, long n, GEN T}
6824
6825
\fun{GEN}{F2xq_matrix_pow}{GEN x, long m, long n, GEN T}
6826
6827
\fun{GEN}{F2x_F2xq_eval}{GEN f, GEN x, GEN T}
6828
6829
\fun{GEN}{F2x_F2xqV_eval}{GEN f, GEN x, GEN T}, see \kbd{FpX\_FpXQV\_eval}.
6830
6831
\fun{GEN}{F2xq_autpow}{GEN a, long n, GEN T} computes $\sigma^n(X)$ assuming
6832
$a=\sigma(X)$ where $\sigma$ is an automorphism of the algebra $\F_2[X]/T(X)$.
6833
6834
\subsec{\kbd{F2xn}} See \kbd{FpXn} operations.
6835
6836
\fun{GEN}{F2xn_red}{GEN a, long n}
6837
6838
\fun{GEN}{F2xn_inv}{GEN f, long e}
6839
6840
\subsec{\kbd{F2xqV}, \kbd{F2xqM}}. See \kbd{FqV}, \kbd{FqM} operations.
6841
6842
\fun{GEN}{F2xqM_F2xqC_gauss}{GEN a, GEN b, GEN T}
6843
6844
\fun{GEN}{F2xqM_F2xqC_invimage}{GEN a, GEN b, GEN T}
6845
6846
\fun{GEN}{F2xqM_F2xqC_mul}{GEN a, GEN b, GEN T}
6847
6848
\fun{GEN}{F2xqM_deplin}{GEN x, GEN T}
6849
6850
\fun{GEN}{F2xqM_det}{GEN a, GEN T}
6851
6852
\fun{GEN}{F2xqM_gauss}{GEN a, GEN b, GEN T}
6853
6854
\fun{GEN}{F2xqM_image}{GEN x, GEN T}
6855
6856
\fun{GEN}{F2xqM_indexrank}{GEN x, GEN T}
6857
6858
\fun{GEN}{F2xqM_inv}{GEN a, GEN T}
6859
6860
\fun{GEN}{F2xqM_invimage}{GEN a, GEN b, GEN T}
6861
6862
\fun{GEN}{F2xqM_ker}{GEN x, GEN T}
6863
6864
\fun{GEN}{F2xqM_mul}{GEN a, GEN b, GEN T}
6865
6866
\fun{long}{F2xqM_rank}{GEN x, GEN T}
6867
6868
\fun{GEN}{F2xqM_suppl}{GEN x, GEN T}
6869
6870
\fun{GEN}{matid_F2xqM}{long n, GEN T}
6871
6872
\subsec{\kbd{F2xX}}. See \kbd{FpXX} operations.
6873
6874
\fun{GEN}{ZXX_to_F2xX}{GEN x, long v}
6875
6876
\fun{GEN}{FlxX_to_F2xX}{GEN x}
6877
6878
\fun{GEN}{F2xX_to_FlxX}{GEN B}
6879
6880
\fun{GEN}{F2xX_to_F2xC}{GEN B, long N, long sv}
6881
6882
\fun{GEN}{F2xXV_to_F2xM}{GEN B, long N, long sv}
6883
6884
\fun{GEN}{F2xX_to_ZXX}{GEN B}
6885
6886
\fun{GEN}{F2xX_renormalize}{GEN x, long lx}
6887
6888
\fun{long}{F2xY_degreex}{GEN P} return the degree of $P$ with respect to
6889
the secondary variable.
6890
6891
\fun{GEN}{pol1_F2xX}{long v, long sv}
6892
6893
\fun{GEN}{polx_F2xX}{long v, long sv}
6894
6895
\fun{GEN}{F2xX_add}{GEN x, GEN y}
6896
6897
\fun{GEN}{F2xX_F2x_add}{GEN x, GEN y}
6898
6899
\fun{GEN}{F2xX_F2x_mul}{GEN x, GEN y}
6900
6901
\fun{GEN}{F2xX_deriv}{GEN P} returns the derivative of \kbd{P} with respect to
6902
the main variable.
6903
6904
\fun{GEN}{Kronecker_to_F2xqX}{GEN z, GEN T}
6905
6906
\fun{GEN}{F2xX_to_Kronecker}{GEN z, GEN T}
6907
6908
\fun{GEN}{F2xY_F2xq_evalx}{GEN x, GEN y, GEN T} as \kbd{FpXY\_FpXQ\_evalx}.
6909
6910
\fun{GEN}{F2xY_F2xqV_evalx}{GEN x, GEN V, GEN T} as \kbd{FpXY\_FpXQV\_evalx}.
6911
6912
\subsec{\kbd{F2xXV/F2xXC}}. See \kbd{FpXXV} operations.
6913
6914
\fun{GEN}{FlxXC_to_F2xXC}{GEN B}
6915
6916
\fun{GEN}{F2xXC_to_ZXXC}{GEN B}
6917
6918
\subsec{\kbd{F2xqX}}. See \kbd{FlxqX} operations.
6919
6920
\subsubsec{Preconditioned reduction}
6921
6922
For faster reduction, the modulus \kbd{S} can be replaced by an extended
6923
modulus, which is an \kbd{F2xqXT}, in all \kbd{F2xqXQ}-classes
6924
functions, and in \kbd{F2xqX\_rem} and \kbd{F2xqX\_divrem}.
6925
6926
\fun{GEN}{F2xqX_get_red}{GEN S, GEN T} returns the extended modulus
6927
\kbd{eS}.
6928
6929
To write code that works both with plain and extended moduli, the following
6930
accessors are defined:
6931
6932
\fun{GEN}{get_F2xqX_mod}{GEN eS} returns the underlying modulus \kbd{S}.
6933
6934
\fun{GEN}{get_F2xqX_var}{GEN eS} returns the variable number of the modulus.
6935
6936
\fun{GEN}{get_F2xqX_degree}{GEN eS} returns the degree of the modulus.
6937
6938
\subsubsec{basic functions}
6939
6940
\fun{GEN}{random_F2xqX}{long d, long v, GEN T, ulong p} returns a random
6941
\kbd{F2xqX} in variable \kbd{v}, of degree less than~\kbd{d}.
6942
6943
\fun{GEN}{F2xqX_red}{GEN z, GEN T}
6944
6945
\fun{GEN}{F2xqX_normalize}{GEN z, GEN T}
6946
6947
\fun{GEN}{F2xqX_F2xq_mul}{GEN P, GEN U, GEN T}
6948
6949
\fun{GEN}{F2xqX_F2xq_mul_to_monic}{GEN P, GEN U, GEN T}
6950
6951
\fun{GEN}{F2xqX_mul}{GEN x, GEN y, GEN T}
6952
6953
\fun{GEN}{F2xqX_sqr}{GEN x, GEN T}
6954
6955
\fun{GEN}{F2xqX_powu}{GEN x, ulong n, GEN T}
6956
6957
\fun{GEN}{F2xqX_rem}{GEN x, GEN y, GEN T}
6958
6959
\fun{GEN}{F2xqX_div}{GEN x, GEN y, GEN T}
6960
6961
\fun{GEN}{F2xqX_divrem}{GEN x, GEN y, GEN T, GEN *pr}
6962
6963
\fun{GEN}{F2xqXQ_inv}{GEN x, GEN S, GEN T}
6964
6965
\fun{GEN}{F2xqXQ_invsafe}{GEN x, GEN S, GEN T}
6966
6967
\fun{GEN}{F2xqX_invBarrett}{GEN T, GEN Q}
6968
6969
\fun{GEN}{F2xqX_extgcd}{GEN x, GEN y, GEN T, GEN *ptu, GEN *ptv}
6970
6971
\fun{GEN}{F2xqX_gcd}{GEN x, GEN y, GEN T}
6972
6973
\fun{GEN}{F2xqX_halfgcd}{GEN x, GEN y, GEN T}
6974
6975
\fun{GEN}{F2xqX_resultant}{GEN x, GEN y, GEN T}
6976
6977
\fun{GEN}{F2xqX_disc}{GEN x, GEN T}
6978
6979
\fun{long}{F2xqX_ispower}{GEN f, ulong k, GEN T, GEN *pt}
6980
6981
\fun{GEN}{F2xqX_F2xqXQ_eval}{GEN Q, GEN x, GEN S, GEN T} as
6982
\kbd{FpX\_FpXQ\_eval}.
6983
6984
\fun{GEN}{F2xqX_F2xqXQV_eval}{GEN P, GEN V, GEN S, GEN T} as
6985
\kbd{FpX\_FpXQV\_eval}.
6986
6987
\fun{GEN}{F2xqX_roots}{GEN f, GEN T} return the roots of \kbd{f} in
6988
$\F_2[X]/(T)$. Assumes \kbd{T} irreducible in $\F_2[X]$.
6989
6990
\fun{GEN}{F2xqX_factor}{GEN f, GEN T} return the factorization of \kbd{f} over
6991
$\F_2[X]/(T)$. Assumes \kbd{T} irreducible in $\F_2[X]$.
6992
6993
\fun{GEN}{F2xqX_factor_squarefree}{GEN f, GEN T} as
6994
\kbd{FlxqX\_factor\_squarefree}.
6995
6996
\fun{GEN}{F2xqX_ddf}{GEN f, GEN T} as \kbd{FpX\_ddf}.
6997
6998
\fun{GEN}{F2xqX_degfact}{GEN f, GEN T} as \kbd{FpX\_degfact}.
6999
7000
\subsec{\kbd{F2xqXQ}}. See \kbd{FlxqXQ} operations.
7001
7002
\fun{GEN}{FlxqXQ_inv}{GEN x, GEN S, GEN T}
7003
7004
\fun{GEN}{FlxqXQ_invsafe}{GEN x, GEN S, GEN T}
7005
7006
\fun{GEN}{F2xqXQ_mul}{GEN x, GEN y, GEN S, GEN T}
7007
7008
\fun{GEN}{F2xqXQ_sqr}{GEN x, GEN S, GEN T}
7009
7010
\fun{GEN}{F2xqXQ_pow}{GEN x, GEN n, GEN S, GEN T}
7011
7012
\fun{GEN}{F2xqXQ_powers}{GEN x, long n, GEN S, GEN T}
7013
7014
\fun{GEN}{F2xqXQ_autpow}{GEN a, long n, GEN S, GEN T}
7015
as \kbd{FpXQXQ\_autpow}
7016
7017
\fun{GEN}{F2xqXQ_auttrace}{GEN a, long n, GEN S, GEN T}. Let
7018
$\sigma$ be the automorphism defined by $\sigma(X)=a[1]\pmod{T(X)}$ and
7019
$\sigma(Y)=a[2]\pmod{S(X,Y),T(X)}$; returns the vector
7020
$[\sigma^n(X),\sigma^n(Y),b+\sigma(b)+\ldots+\sigma^{n-1}(b)]$
7021
where $b=a[3]$.
7022
7023
\fun{GEN}{F2xqXQV_red}{GEN x, GEN S, GEN T}
7024
7025
\subsec{Functions returning objects with \typ{INTMOD} coefficients}
7026
7027
Those functions are mostly needed for interface reasons: \typ{INTMOD}s should
7028
not be used in library mode since the modular kernel is more flexible and more
7029
efficient, but GP users do not have access to the modular kernel.
7030
We document them for completeness:
7031
7032
\fun{GEN}{Fp_to_mod}{GEN z, GEN p}, \kbd{z} a \typ{INT}. Returns \kbd{z *
7033
Mod(1,p)}, normalized. Hence the returned value is a \typ{INTMOD}.
7034
7035
\fun{GEN}{FpX_to_mod}{GEN z, GEN p}, \kbd{z} a \kbd{ZX}. Returns \kbd{z *
7036
Mod(1,p)}, normalized. Hence the returned value has \typ{INTMOD} coefficients.
7037
7038
\fun{GEN}{FpC_to_mod}{GEN z, GEN p}, \kbd{z} a \kbd{ZC}. Returns \kbd{Col(z) *
7039
Mod(1,p)}, a \typ{COL} with \typ{INTMOD} coefficients.
7040
7041
\fun{GEN}{FpV_to_mod}{GEN z, GEN p}, \kbd{z} a \kbd{ZV}. Returns \kbd{Vec(z) *
7042
Mod(1,p)}, a \typ{VEC} with \typ{INTMOD} coefficients.
7043
7044
\fun{GEN}{FpVV_to_mod}{GEN z, GEN p}, \kbd{z} a \kbd{ZVV}. Returns \kbd{Vec(z) *
7045
Mod(1,p)}, a \typ{VEC} of \typ{VEC} with \typ{INTMOD} coefficients.
7046
7047
\fun{GEN}{FpM_to_mod}{GEN z, GEN p}, \kbd{z} a \kbd{ZM}. Returns \kbd{z *
7048
Mod(1,p)}, with \typ{INTMOD} coefficients.
7049
7050
\fun{GEN}{F2c_to_mod}{GEN x}
7051
7052
\fun{GEN}{F2m_to_mod}{GEN x}
7053
7054
\fun{GEN}{Flc_to_mod}{GEN z}
7055
7056
\fun{GEN}{Flm_to_mod}{GEN z}
7057
7058
\fun{GEN}{FqC_to_mod}{GEN z, GEN T, GEN p}
7059
7060
\fun{GEN}{FqM_to_mod}{GEN z, GEN T, GEN p}
7061
7062
\fun{GEN}{FpXC_to_mod}{GEN V, GEN p}
7063
7064
\fun{GEN}{FpXM_to_mod}{GEN V, GEN p}
7065
7066
\fun{GEN}{FpXQC_to_mod}{GEN V, GEN T, GEN p} $V$ being a vector of \kbd{FpXQ},
7067
converts each entry to a \typ{POLMOD} with \typ{INTMOD} coefficients, and return
7068
a \typ{COL}.
7069
7070
\fun{GEN}{FpXQX_to_mod}{GEN P, GEN T, GEN p}
7071
$P$ being a \kbd{FpXQX}, converts each coefficient to a \typ{POLMOD} with
7072
\typ{INTMOD} coefficients.
7073
7074
\fun{GEN}{FqX_to_mod}{GEN P, GEN T, GEN p} same but allow
7075
$\kbd{T} = \kbd{NULL}$.
7076
7077
\fun{GEN}{FqXC_to_mod}{GEN P, GEN T, GEN p}
7078
7079
\fun{GEN}{FqXM_to_mod}{GEN P, GEN T, GEN p}
7080
7081
\fun{GEN}{QXQ_to_mod_shallow}{GEN x, GEN T} $x$ a \kbd{QXQ}, which is
7082
a lifted representative of elements of $\Q[X]/(T)$ (number field elements
7083
in most applications) and $T$ is in $\Z[X]$. Convert it to a \typ{POLMOD}
7084
modulo $T$; no reduction mod $T$ is attempted: the representatives should be
7085
already reduced. Shallow function.
7086
7087
\fun{GEN}{QXQV_to_mod}{GEN V, GEN T} $V$ a vector of \kbd{QXQ}, which
7088
are lifted representatives of elements of $\Q[X]/(T)$ (number field elements
7089
in most applications) and $T$ is in $\Z[X]$. Return a vector where all
7090
nonrational entries are converted to \typ{POLMOD} modulo $T$; no reduction
7091
mod $T$ is attempted: the representatives should be already reduced. Used to
7092
normalize the output of \kbd{nfroots}.
7093
7094
\fun{GEN}{QXQX_to_mod_shallow}{GEN P, GEN T} $P$ a polynomial with \kbd{QXQ}
7095
coefficients; replace them by \kbd{mkpolmod(.,T)}. Shallow function.
7096
7097
\fun{GEN}{QXQC_to_mod_shallow}{GEN V, GEN T} $V$ a vector with \kbd{QXQ}
7098
coefficients; replace them by \kbd{mkpolmod(.,T)}. Shallow function.
7099
7100
\fun{GEN}{QXQM_to_mod_shallow}{GEN M, GEN T} $M$ a matrix with \kbd{QXQ}
7101
coefficients; replace them by \kbd{mkpolmod(.,T)}. Shallow function.
7102
7103
\fun{GEN}{QXQXV_to_mod}{GEN V, GEN T} $V$ a vector of polynomials whose
7104
coefficients are \kbd{QXQ}. Analogous to \kbd{QXQV\_to\_mod}.
7105
Used to normalize the output of \kbd{nffactor}.
7106
7107
The following functions are obsolete and should not be used: they receive a
7108
polynomial with arbitrary coefficients, apply a conversion function to map
7109
them to a finite field, a function from the modular kernel, then
7110
\kbd{*\_to\_mod}:
7111
7112
\fun{GEN}{rootmod}{GEN f, GEN p}, applies \kbd{FpX\_roots}.
7113
7114
\fun{GEN}{rootmod2}{GEN f, GEN p}, (now) identical to \kbd{rootmod}.
7115
7116
\fun{GEN}{rootmod0}{GEN f, GEN p, long flag}, (now) identical to
7117
\kbd{rootmod}; ignores \fl.
7118
7119
\fun{GEN}{factmod}{GEN f, GEN p} applies \kbd{*\_factor}.
7120
7121
\fun{GEN}{simplefactmod}{GEN f, GEN p} applies \kbd{*\_degfact}.
7122
7123
\subsec{Slow Chinese remainder theorem over $\Z$}
7124
The routines in this section have quadratic time complexity with respect to
7125
the input size; see the routines in the next two sections for quasi-linear
7126
time variants.
7127
7128
\fun{GEN}{Z_chinese}{GEN a, GEN b, GEN A, GEN B} returns the integer
7129
in $[0, \lcm(A,B)[$ congruent to $a$ mod $A$ and $b$ mod $B$, assuming it
7130
exists; in other words, that $a$ and $b$ are congruent mod $\gcd(A,B)$.
7131
7132
\fun{GEN}{Z_chinese_all}{GEN a, GEN b, GEN A, GEN B, GEN *pC} as
7133
\kbd{Z\_chinese}, setting \kbd{*pC} to the lcm of $A$ and $B$.
7134
7135
\fun{GEN}{Z_chinese_coprime}{GEN a, GEN b, GEN A, GEN B, GEN C}, as
7136
\kbd{Z\_chinese}, assuming that $\gcd(A,B) = 1$ and that $C = \lcm(A,B) = AB$.
7137
7138
\fun{ulong}{u_chinese_coprime}{ulong a, ulong b, ulong A, ulong B, ulong C}, as
7139
\kbd{Z\_chinese\_coprime} for \kbd{ulong} inputs and output.
7140
7141
\fun{void}{Z_chinese_pre}{GEN A, GEN B, GEN *pC, GEN *pU, GEN *pd}
7142
initializes chinese remainder computations modulo $A$ and $B$. Sets
7143
\kbd{*pC} to $\lcm(A,B)$, \kbd{*pd} to $\gcd(A,B)$,
7144
\kbd{*pU} to an integer congruent to $0$ mod $(A/d)$ and $1$ mod $(B/d)$.
7145
It is allowed to set \kbd{pd = NULL}, in which case, $d$ is still
7146
computed, but not saved.
7147
7148
\fun{GEN}{Z_chinese_post}{GEN a, GEN b, GEN C, GEN U, GEN d} returns
7149
the solution to the chinese remainder problem $x$ congruent
7150
to $a$ mod $A$ and $b$ mod $B$, where $C, U, d$ were set in
7151
\kbd{Z\_chinese\_pre}. If $d$ is \kbd{NULL}, assume the problem has a
7152
solution. Otherwise, return \kbd{NULL} if it has no solution.
7153
7154
\medskip
7155
7156
The following pair of functions is used in homomorphic imaging schemes,
7157
when reconstructing an integer from its images modulo pairwise coprime
7158
integers. The idea is as follows: we want to discover an integer $H$ which
7159
satisfies $|H| < B$ for some known bound $B$; we are given pairs $(H_p, p)$
7160
with $H$ congruent to $H_p$ mod $p$ and all $p$ pairwise coprime.
7161
7162
Given \kbd{H} congruent to $H_p$ modulo a number of $p$, whose product is
7163
$q$, and a new pair $(\kbd{Hp}, \kbd{p})$, \kbd{p} coprime to $q$, the
7164
following incremental functions use the chinese remainder theorem (CRT) to
7165
find a new \kbd{H}, congruent to the preceding one modulo $q$, but also to
7166
\kbd{Hp} modulo \kbd{p}. It is defined uniquely modulo $qp$, and we choose
7167
the centered representative. When $P$ is larger than $2B$, we have $\kbd{H} =
7168
H$, but of course, the value of \kbd{H} may stabilize sooner. In many
7169
applications it is possible to directly check that such a partial result is
7170
correct.
7171
7172
\fun{GEN}{Z_init_CRT}{ulong Hp, ulong p} given a \kbd{Fl} \kbd{Hp} in
7173
$[0, p-1]$, returns the centered representative \kbd{H} congruent to \kbd{Hp}
7174
modulo \kbd{p}.
7175
7176
\fun{int}{Z_incremental_CRT}{GEN *H, ulong Hp, GEN *q, ulong p}
7177
given a \typ{INT} \kbd{*H}, centered modulo \kbd{*q}, a new pair $(\kbd{Hp},
7178
\kbd{p})$ with \kbd{p} coprime to \kbd{q}, this function updates \kbd{*H} so
7179
that it also becomes congruent to $(\kbd{Hp}, \kbd{p})$, and \kbd{*q} to the
7180
product$\kbd{qp} = \kbd{p} \cdot \kbd{*q}$. It returns $1$ if the new value
7181
is equal to the old one, and $0$ otherwise.
7182
7183
\fun{GEN}{chinese1_coprime_Z}{GEN v} an alternative divide-and-conquer
7184
implementation: $v$ is a vector of \typ{INTMOD} with pairwise coprime moduli.
7185
Return the \typ{INTMOD} solving the corresponding chinese remainder problem.
7186
This is a streamlined version of
7187
7188
\fun{GEN}{chinese1}{GEN v}, which solves a general chinese remainder problem
7189
(not necessarily over $\Z$, moduli not assumed coprime).
7190
7191
As above, for $H$ a \kbd{ZM}: we assume that $H$ and all \kbd{Hp} have
7192
dimension $> 0$. The original \kbd{*H} is destroyed.
7193
7194
\fun{GEN}{ZM_init_CRT}{GEN Hp, ulong p}
7195
7196
\fun{int}{ZM_incremental_CRT}{GEN *H, GEN Hp, GEN *q, ulong p}
7197
7198
As above for $H$ a \kbd{ZX}: note that the degree may increase or decrease.
7199
The original \kbd{*H} is destroyed.
7200
7201
\fun{GEN}{ZX_init_CRT}{GEN Hp, ulong p, long v}
7202
7203
\fun{int}{ZX_incremental_CRT}{GEN *H, GEN Hp, GEN *q, ulong p}
7204
7205
As above, for $H$ a matrix whose coefficient are \kbd{ZX}.
7206
The original \kbd{*H} is destroyed.
7207
The entries of $H$ are not normalized, use \kbd{ZX\_renormalize}
7208
for this.
7209
7210
\fun{GEN}{ZXM_init_CRT}{GEN Hp, long deg, ulong p} where \kbd{deg}
7211
is the maximal degree of all the \kbd{Hp}
7212
7213
\fun{int}{ZXM_incremental_CRT}{GEN *H, GEN Hp, GEN *q, ulong p}
7214
7215
\subsec{Fast remainders}
7216
7217
The routines in these section are asymptotically fast (quasi-linear time in
7218
the input size).
7219
7220
\fun{GEN}{Z_ZV_mod}{GEN A, GEN P} given a \typ{INT} $A$ and a vector $P$ of
7221
positive pairwise coprime integers of length $n\ge 1$, return a vector $B$ of
7222
the same length such that $B[i] = A\pmod{P[i]}$ and $0\leq B[i] < P[i]$ for
7223
all $1\leq i\leq n$. The vector $P$ may be a \typ{VEC} or a \typ{VECSMALL}
7224
(treated as \kbd{ulong}s) and $B$ has the same type as $P$.
7225
7226
\fun{GEN}{Z_nv_mod}{GEN A, GEN P} given a \typ{INT} $A$ and a \typ{VECSMALL}
7227
$P$ of positive pairwise coprime integers of length $n\ge 1$, return a
7228
\typ{VECSMALL} $B$ of the same length such that $B[i]=A\pmod{P[i]}$ and
7229
$0\leq B[i] < P[i]$ for all $1\leq i\leq n$. The entries of $P$ and $B$ are
7230
treated as \kbd{ulong}s.
7231
7232
The following low level functions allow precomputations:
7233
7234
\fun{GEN}{ZV_producttree}{GEN P} where $P$ is a vector of integers (or
7235
\typ{VECSMALL}) of length $n\ge 1$, return the vector of \typ{VEC}s
7236
$[f(P),f^2(P),\ldots,f^k(P)]$ where $f$ is the transformation
7237
$[p_1,p_2,\ldots,p_m] \mapsto [p_1\*p_2,p_3\*p_4,\ldots,p_{m-1}\*p_m]$ if $m$
7238
is even and $[p_1\*p_2,p_3\*p_4,\ldots,p_{m-2}\*p_{m-1},p_m]$ if $m$ is odd,
7239
and $k = O(\log m)$ is minimal so that $f^k(P)$ has length $1$; in other
7240
words, $f^k(P) = [p_1\*p_2\*\ldots\*p_m]$.
7241
7242
\fun{GEN}{Z_ZV_mod_tree}{GEN A, GEN P, GEN T}
7243
as \kbd{Z\_ZV\_mod} where $T$ is the tree \kbd{ZV\_producttree(P)}.
7244
7245
\fun{GEN}{ZV_nv_mod_tree}{GEN A, GEN P, GEN T} $A$ being a \kbd{ZV}
7246
and $P$ a \typ{VECSMALL} of length $n\ge 1$, the elements of $P$ being
7247
pairwise coprime, return the vector of \kbd{Flv}
7248
$[A \pmod{P[1]},\ldots,A \pmod{P[n]}]$,
7249
where $T$ is the tree \kbd{ZV\_producttree(P)}.
7250
7251
\fun{GEN}{ZM_nv_mod_tree}{GEN A, GEN P, GEN T} $A$ being a \kbd{ZM}
7252
and $P$ a \typ{VECSMALL} of length $n\ge 1$, the elements of $P$ being
7253
pairwise coprime, return the vector of \kbd{Flm}
7254
$[A \pmod{P[1]},\ldots,A \pmod{P[n]}]$,
7255
where $T$ is the tree \kbd{ZV\_producttree(P)}.
7256
7257
\fun{GEN}{ZX_nv_mod_tree}{GEN A, GEN P, GEN T} $A$ being a \kbd{ZX}
7258
and $P$ a \typ{VECSMALL} of length $n\ge 1$, the elements of $P$ being
7259
pairwise coprime, return the vector of \kbd{Flx} polynomials
7260
$[A \pmod{P[1]},\ldots,A \pmod{P[n]}]$,
7261
where $T$ is the tree \kbd{ZV\_producttree(P)}.
7262
7263
\fun{GEN}{ZXC_nv_mod_tree}{GEN A, GEN P, GEN T} $A$ being a \kbd{ZXC}
7264
and $P$ a \typ{VECSMALL} of length $n\ge 1$, the elements of $P$ being
7265
pairwise coprime, return the vector of \kbd{FlxC}
7266
$[A \pmod{P[1]},\ldots,A \pmod{P[n]}]$,
7267
where $T$ is the tree \kbd{ZV\_producttree(P)}.
7268
7269
\fun{GEN}{ZXM_nv_mod_tree}{GEN A, GEN P, GEN T} $A$ being a \kbd{ZXM}
7270
and $P$ a \typ{VECSMALL} of length $n\ge 1$, the elements of $P$ being
7271
pairwise coprime, return the vector of \kbd{FlxM}
7272
$[A \pmod{P[1]},\ldots,A \pmod{P[n]}]$,
7273
where $T$ is the tree \kbd{ZV\_producttree(P)}.
7274
7275
\fun{GEN}{ZXX_nv_mod_tree}{GEN A, GEN P, GEN T, long v} $A$ being a \kbd{ZXX},
7276
and $P$ a \typ{VECSMALL} of length $n\ge 1$, the elements of $P$ being
7277
pairwise coprime, return the vector of \kbd{FlxX}
7278
$[A \pmod{P[1]},\ldots,A \pmod{P[n]}]$,
7279
where $T$ is assumed to be the tree created by \kbd{ZV\_producttree(P)}.
7280
7281
\medskip
7282
7283
\subsec{Fast Chinese remainder theorem over $\Z$}
7284
The routines in these section are asymptotically fast (quasi-linear time in
7285
the input size) and should be used whenever the moduli are known from
7286
the start.
7287
7288
The simplest function is
7289
7290
\fun{GEN}{ZV_chinese}{GEN A, GEN P, GEN *pM}
7291
let $P$ be a vector of positive pairwise coprime integers, let $A$ be a
7292
vector of integers of the same length $n\ge 1$ such that $0 \leq A[i] < P[i]$
7293
for all $i$, and let $M$ be the product of the elements of $P$. Returns the
7294
integer in $[0, M[$ congruent to $A[i]$ mod $P[i]$ for all $1\leq i\leq n$.
7295
If \kbd{pM} is not \kbd{NULL}, set \kbd{*pM} to $M$. We also allow
7296
\typ{VECSMALL}s for $A$ and $P$ (seen as vectors of unsigned integers).
7297
7298
\fun{GEN}{ZV_chinese_center}{GEN A, GEN P, GEN *pM}
7299
As \kbd{ZV\_chinese} but return integers in $[-M/2, M/2[$ instead.
7300
7301
The following functions allow to solve many Chinese remainder problems
7302
simultaneously, for a given set of moduli:
7303
7304
\fun{GEN}{nxV_chinese_center}{GEN A, GEN P, GEN *pt_mod}
7305
where $A$ is a vector of \kbd{nx}
7306
and $P$ a \typ{VECSMALL} of the same length $n\ge 1$, the elements of $P$
7307
being pairwise coprime, and $M$ being the product of the elements of $P$,
7308
returns the \typ{POL} whose entries are integers in $[-M/2, M/2[$ congruent to $A[i]$
7309
mod $P[i]$ for all $1\leq i\leq n$.
7310
If \kbd{pt\_mod} is not \kbd{NULL}, set \kbd{*pt\_mod} to $M$.
7311
7312
\fun{GEN}{ncV_chinese_center}{GEN A, GEN P, GEN *pM} where $A$ is a
7313
vector of \kbd{VECSMALL}s (seen as vectors of unsigned integers) and $P$ a
7314
\typ{VECSMALL} of the same length $n\ge 1$, the elements of $P$ being
7315
pairwise coprime, and $M$ being the product of the elements of $P$, returns
7316
the \typ{COL} whose entries are integers in $[-M/2, M/2[$ congruent to $A[i]$
7317
mod $P[i]$ for all $1\leq i\leq n$. If \kbd{pM} is not \kbd{NULL}, set
7318
\kbd{*pt\_mod} to $M$.
7319
7320
\fun{GEN}{nmV_chinese_center}{GEN A, GEN P, GEN *pM} where $A$ is a
7321
vector of \kbd{MATSMALL}s (seen as matrices of unsigned integers) and $P$ a
7322
\typ{VECSMALL} of the same length $n\ge 1$, the elements of $P$ being
7323
pairwise coprime, and $M$ being the product of the elements of $P$, returns
7324
the matrix whose entries are integers in $[-M/2, M/2[$ congruent to $A[i]$
7325
mod $P[i]$ for all $1\leq i\leq n$. If \kbd{pM} is not \kbd{NULL}, set
7326
\kbd{*pM} to $M$. N.B.: this function uses the parallel GP interface.
7327
7328
\fun{GEN}{nxCV_chinese_center}{GEN A, GEN P, GEN *pM} where $A$ is a
7329
vector of \kbd{nxC}s and $P$ a
7330
\typ{VECSMALL} of the same length $n\ge 1$, the elements of $P$ being
7331
pairwise coprime, and $M$ being the product of the elements of $P$, returns
7332
the \typ{COL} whose entries are integers in $[-M/2, M/2[$ congruent to $A[i]$
7333
mod $P[i]$ for all $1\leq i\leq n$. If \kbd{pM} is not \kbd{NULL}, set
7334
\kbd{*pt\_mod} to $M$.
7335
7336
\fun{GEN}{nxMV_chinese_center}{GEN A, GEN P, GEN *pM} where $A$ is a
7337
vector of \kbd{nxM}s and $P$ a
7338
\typ{VECSMALL} of the same length $n\ge 1$, the elements of $P$ being
7339
pairwise coprime, and $M$ being the product of the elements of $P$, returns
7340
the matrix whose entries are integers in $[-M/2, M/2[$ congruent to $A[i]$
7341
mod $P[i]$ for all $1\leq i\leq n$. If \kbd{pM} is not \kbd{NULL}, set
7342
\kbd{*pM} to $M$. N.B.: this function uses the parallel GP interface.
7343
7344
The other routines allow for various precomputations :
7345
7346
\fun{GEN}{ZV_chinesetree}{GEN P, GEN T} given $P$ a vector of integers
7347
(or \typ{VECSMALL}) and a product tree $T$ from \tet{ZV_producttree}$(P)$
7348
for the same $P$, return a ``chinese remainder tree'' $R$, preconditionning
7349
the solution of Chinese remainder problems modulo the $P[i]$.
7350
7351
\fun{GEN}{ZV_chinese_tree}{GEN A, GEN P, GEN T, GEN R}
7352
return \kbd{ZV\_chinese}$(A,P,\kbd{NULL})$, where $T$ is created by
7353
\kbd{ZV\_producttree}$(P)$ and $R$ by \kbd{ZV\_chinesetree}$(P,T)$.
7354
7355
\fun{GEN}{ncV_chinese_center_tree}{GEN A, GEN P, GEN T, GEN R}
7356
as \kbd{ncV\_chinese\_center} where $T$ is assumed to be the tree created by
7357
\kbd{ZV\_producttree(P)} and $R$ by \kbd{ZV\_chinesetree}$(P,T)$.
7358
7359
\fun{GEN}{nmV_chinese_center_tree}{GEN A, GEN P, GEN T, GEN R}
7360
as \kbd{nmV\_chinese\_center} where $T$ is assumed to be the tree created by
7361
\kbd{ZV\_producttree(P)} and $R$ by \kbd{ZV\_chinesetree}$(P,T)$.
7362
7363
\fun{GEN}{nxV_chinese_center_tree}{GEN A, GEN P, GEN T, GEN R}
7364
as \kbd{nxV\_chinese\_center} where $T$ is assumed to be the tree created by
7365
\kbd{ZV\_producttree(P)} and $R$ by \kbd{ZV\_chinesetree}$(P,T)$.
7366
7367
\fun{GEN}{nxCV_chinese_center_tree}{GEN A, GEN P, GEN T, GEN R}
7368
as \kbd{nxCV\_chinese\_center} where $T$ is assumed to be the tree created by
7369
\kbd{ZV\_producttree(P)} and $R$ by \kbd{ZV\_chinesetree}$(P,T)$.
7370
7371
\subsec{Rational reconstruction}
7372
7373
\fun{int}{Fp_ratlift}{GEN x, GEN m, GEN amax, GEN bmax, GEN *a, GEN *b}.
7374
Assuming that $0 \leq x < m$, $\kbd{amax} \geq 0$, and
7375
$\kbd{bmax} > 0$ are \typ{INT}s, and that $2 \kbd{amax} \kbd{bmax} < m$,
7376
attempts to recognize $x$ as a rational $a/b$, i.e. to find \typ{INT}s $a$
7377
and $b$ such that
7378
7379
\item $a \equiv b x$ modulo $m$,
7380
7381
\item $|a| \leq \kbd{amax}$, $0 < b \leq \kbd{bmax}$,
7382
7383
\item $\gcd(m,b) = \gcd(a,b)$.
7384
7385
\noindent If unsuccessful, the routine returns $0$ and leaves $a$, $b$
7386
unchanged; otherwise it returns $1$ and sets $a$ and $b$.
7387
7388
In almost all applications, we actually know that a solution exists, as well
7389
as a nonzero multiple $B$ of $b$, and $m = p^\ell$ is a prime power, for a
7390
prime $p$ chosen coprime to $B$ hence to $b$. Under the single assumption
7391
$\gcd(m,b) = 1$, if a solution $a,b$ exists satisfying the three conditions
7392
above, then it is unique.
7393
7394
\fun{GEN}{FpM_ratlift}{GEN M, GEN m, GEN amax, GEN bmax, GEN denom}
7395
given an \kbd{FpM} modulo $m$ with reduced or \kbd{Fp\_center}-ed entries,
7396
reconstructs a matrix with rational coefficients by applying \kbd{Fp\_ratlift}
7397
to all entries. Assume that all preconditions for \kbd{Fp\_ratlift} are
7398
satisfied, as well $\gcd(m,b) = 1$ (so that the solution is unique if it
7399
exists). Return \kbd{NULL} if the reconstruction fails, and the rational
7400
matrix otherwise. If \kbd{denom} is not \kbd{NULL} check further that all
7401
denominators divide \kbd{denom}.
7402
7403
The function is not stack clean if one of the coefficients of $M$ is negative
7404
(centered residues), but still suitable for \kbd{gerepileupto}.
7405
7406
\fun{GEN}{FpX_ratlift}{GEN P, GEN m, GEN amax, GEN bmax, GEN denom} as
7407
\kbd{FpM\_ratlift}, where $P$ is an \kbd{FpX}.
7408
7409
\fun{GEN}{FpC_ratlift}{GEN P, GEN m, GEN amax, GEN bmax, GEN denom} as
7410
\kbd{FpM\_ratlift}, where $P$ is an \kbd{FpC}.
7411
7412
\subsec{Zp}
7413
7414
\fun{GEN}{Zp_sqrt}{GEN b, GEN p, long e} $b$ and $p$ being \typ{INT}s, with $p$
7415
a prime (possibly $2$), returns a \typ{INT} $a$ such that $a^2 \equiv b \mod
7416
p^e$.
7417
7418
\fun{GEN}{Z2_sqrt}{GEN b, long e} $b$ being a \typ{INT}s
7419
returns a \typ{INT} $a$ such that $a^2 \equiv b \mod 2^e$.
7420
7421
\fun{GEN}{Zp_sqrtlift}{GEN b, GEN a, GEN p, long e} let
7422
$a,b,p$ be \typ{INT}s, with $p > 1$ odd, such that $a^2\equiv b\mod p$.
7423
Returns a \typ{INT} $A$ such that $A^2 \equiv b \mod p^e$. Special case
7424
of \tet{Zp_sqrtnlift}.
7425
7426
\fun{GEN}{Zp_sqrtnlift}{GEN b, GEN n, GEN a, GEN p, long e} let
7427
$a,b,n,p$ be \typ{INT}s, with $n,p > 1$, and $p$ coprime to $n$,
7428
such that $a^n \equiv b \mod p$. Returns a \typ{INT} $A$ such that
7429
$A^n \equiv b \mod p^e$. Special case of \tet{ZpX_liftroot}.
7430
7431
\fun{GEN}{Zp_teichmuller}{GEN x, GEN p, long e, GEN pe} for $p$ an odd prime,
7432
$x$ a \typ{INT} coprime to $p$, and $pe = p^e$, returns the $(p-1)$-th root of
7433
$1$ congruent to $x$ modulo $p$, modulo $p^e$. For convenience, $p = 2$ is
7434
also allowed and we return $1$ ($x$ is $1$ mod $4$) or $2^e - 1$ ($x$ is $3$
7435
mod $4$).
7436
7437
\fun{GEN}{teichmullerinit}{long p, long n} returns the values of
7438
\tet{Zp_teichmuller} at all $x = 1, \dots, p-1$.
7439
7440
\subsec{ZpM}
7441
7442
\fun{GEN}{ZpM_invlift}{GEN M, GEN Np, GEN p, long e} let
7443
$p$ be a prime \typ{INT}, $Np$ be a \kbd{FpM} (modulo $p$) and
7444
$M$ a \kbd{ZpM} such that $M\*Np \equiv 1 \mod p$.
7445
Returns an \kbd{ZpM} $N$ such that $N \equiv Np \pmod{p}$ and
7446
$M\*N \equiv 1 \mod p^e$.
7447
7448
\subsec{ZpX}
7449
7450
\fun{GEN}{ZpX_roots}{GEN f, GEN p, long e} $f$ a \kbd{ZX} with leading
7451
term prime to $p$, and without multiple roots mod $p$. Return a vector
7452
of \typ{INT}s which are the roots of $f$ mod $p^e$.
7453
7454
\fun{GEN}{ZpX_liftroot}{GEN f, GEN a, GEN p, long e} $f$ a \kbd{ZX} with
7455
leading term prime to $p$, and $a$ a root mod $p$ such that
7456
$v_p(f'(a))=0$. Return a \typ{INT} which is the root of $f$ mod $p^e$
7457
congruent to $a$ mod $p$.
7458
7459
\fun{GEN}{ZX_Zp_root}{GEN f, GEN a, GEN p, long e} same as \tet{ZpX_liftroot}
7460
without the assumption $v_p(f'(a)) = 0$. Return a \typ{VEC} of \typ{INT}s,
7461
which are the $p$-adic roots of $f$ congruent to $a$ mod $p$ (given modulo
7462
$p^e$). Assume that $0 \leq a < p$.
7463
7464
\fun{GEN}{ZpX_liftroots}{GEN f, GEN S, GEN p, long e} $f$ a \kbd{ZX} with
7465
leading term prime to $p$, and $S$ a vector of simple roots mod $p$. Return a
7466
vector of \typ{INT}s which are the root of $f$ mod $p^e$ congruent to the
7467
$S[i]$ mod $p$.
7468
7469
\fun{GEN}{ZpX_liftfact}{GEN A, GEN B, GEN pe, GEN p, long e} is
7470
the routine underlying \tet{polhensellift}. Here, $p$ is prime
7471
defines a finite field $\F_p$. $A$ is a polynomial in
7472
$\Z[X]$, whose leading coefficient is nonzero in $\F_q$. $B$ is a vector of
7473
monic \kbd{FpX}, pairwise coprime in $\F_p[X]$, whose product is congruent to
7474
$A/\text{lc}(A)$ in $\F_p[X]$. Lifts the elements of $B$ mod $\kbd{pe} = p^e$.
7475
7476
\fun{GEN}{ZpX_Frobenius}{GEN T, GEN p, ulong e} returns the $p$-adic lift
7477
of the Frobenius automorphism of $\F_p[X]/(T)$ to precision $e$.
7478
7479
\fun{long}{ZpX_disc_val}{GEN f, GEN p} returns the valuation at $p$ of the
7480
discriminant of $f$. Assume that $f$ is a monic \emph{separable} \kbd{ZX}
7481
and that $p$ is a prime number. Proceeds by dynamically increasing the
7482
$p$-adic accuracy; infinite loop if the discriminant of $f$ is
7483
$0$.
7484
7485
\fun{long}{ZpX_resultant_val}{GEN f, GEN g, GEN p, long M} returns the
7486
valuation at $p$ of $\text{Res}(f,g)$. Assume $f,g$ are both \kbd{ZX},
7487
and that $p$ is a prime number coprime to the leading coefficient of $f$.
7488
Proceeds by dynamically increasing the $p$-adic accuracy.
7489
To avoid an infinite loop when the resultant is $0$, we return $M$ if
7490
the Sylvester matrix mod $p^M$ still does not have maximal rank.
7491
7492
\fun{GEN}{ZpX_gcd}{GEN f,GEN g, GEN p, GEN pm} $f$ a monic \kbd{ZX},
7493
$g$ a \kbd{ZX}, $\kbd{pm} = p^m$ a prime power. There is a unique integer
7494
$r\geq 0$ and a monic $h\in \Q_p[X]$ such that
7495
$$p^rh\Z_p[X] + p^m\Z_p[X] = f\Z_p[X] + g\Z_p[X] + p^m\Z_p[X].$$
7496
Return the $0$ polynomial if $r\geq m$ and a monic $h\in\Z[1/p][X]$ otherwise
7497
(whose valuation at $p$ is $> -m$).
7498
7499
\fun{GEN}{ZpX_reduced_resultant}{GEN f, GEN g, GEN p, GEN pm} $f$ a monic
7500
\kbd{ZX}, $g$ a \kbd{ZX}, $\kbd{pm} = p^m$ a prime power. The $p$-adic
7501
\emph{reduced resultant}\varsidx{resultant (reduced)} of $f$ and $g$ is
7502
$0$ if $f$, $g$ not coprime in $\Z_p[X]$, and otherwise the generator of the
7503
form $p^d$ of
7504
$$ (f\Z_p[X] + g\Z_p[X])\cap \Z_p. $$
7505
Return the reduced resultant modulo $p^m$.
7506
7507
\fun{GEN}{ZpX_reduced_resultant_fast}{GEN f, GEN g, GEN p, long M} $f$
7508
a monic \kbd{ZX}, $g$ a \kbd{ZX}, $p$ a prime. Returns
7509
the $p$-adic reduced resultant of $f$ and $g$ modulo $p^M$. This function
7510
computes resultants for a sequence of increasing $p$-adic accuracies
7511
(up to $M$ $p$-adic digits), returning as soon as it obtains a nonzero
7512
result. It is very inefficient when the resultant is $0$, but otherwise
7513
usually more efficient than computations using a priori bounds.
7514
7515
\fun{GEN}{ZpX_monic_factor}{GEN f, GEN p, long M} $f$ a monic
7516
\kbd{ZX}, $p$ a prime, return the $p$-adic factorization of $f$, modulo
7517
$p^M$. This is the underlying low-level recursive function behind
7518
\kbd{factorpadic} (using a combination of Round 4 factorization and Hensel
7519
lifting); the factors are not sorted and the function is not
7520
\kbd{gerepile}-clean.
7521
7522
\fun{GEN}{ZpX_primedec}{GEN T, GEN p} $T$ a monic separable \kbd{ZX}, $p$ a
7523
prime, return as a factorization matrix the shape of the prime ideal
7524
decomposition of $(p)$ in $\Q[X]/(T)$: the first column contains inertia
7525
degrees, the second columns contains ramification degrees.
7526
7527
\subsec{ZpXQ}
7528
7529
\fun{GEN}{ZpXQ_invlift}{GEN b, GEN a, GEN T, GEN p, long e} let
7530
$p$ be a prime \typ{INT}, $a$ be a \kbd{FpXQ} (modulo $(p, T)$) and
7531
$b$ a \kbd{ZpXQ} such that $a\*b \equiv 1 \mod (p, T)$.
7532
Returns an \kbd{ZpXQ} $A$ such that $A \equiv a \pmod{p}$ and
7533
$A\*b \equiv 1 \mod (p^e, T)$.
7534
7535
\fun{GEN}{ZpXQ_inv}{GEN b, GEN T, GEN p, long e} let
7536
$p$ be a prime \typ{INT} and $b$ be a \kbd{FpXQ} (modulo $T, p^e$).
7537
Returns an \kbd{FpXQ} $A$ such that $A\*b \equiv 1 \mod (p^e, T)$.
7538
7539
\fun{GEN}{ZpXQ_div}{GEN a, GEN b, GEN T, GEN q, GEN p, long e} let
7540
$p$ be a prime \typ{INT} and $a$ and $b$ be a \kbd{FpXQ} (modulo $T, p^e$).
7541
Returns an \kbd{FpXQ} $c$ such that $c\*b \equiv a \mod (p^e, T)$.
7542
The parameter $q$ must be equal to $p^e$.
7543
7544
\fun{GEN}{ZpXQ_sqrtnlift}{GEN b, GEN n, GEN a, GEN T, GEN p, long e} let
7545
$n,p$ be \typ{INT}s, with $n,p > 1$ and $p$ coprime to $n$, and $a,b$
7546
be \kbd{FpXQ}s (modulo $T$) such that $a^n \equiv b \mod (p,T)$.
7547
Returns an \kbd{Fq} $A$ such that $A^n \equiv b \mod (p^e, T)$.
7548
7549
\fun{GEN}{ZpXQ_sqrt}{GEN b, GEN T, GEN p, long e} let
7550
$p$ being a odd prime and $b$ be a \kbd{FpXQ} (modulo $T, p^e$),
7551
returns $a$ such that $a^2 \equiv b \mod (p^e, T)$.
7552
7553
\fun{GEN}{ZpX_ZpXQ_liftroot}{GEN f, GEN a, GEN T, GEN p, long e}
7554
as \tet{ZpXQX_liftroot}, but $f$ is a polynomial in $\Z[X]$.
7555
7556
\fun{GEN}{ZpX_ZpXQ_liftroot_ea}{GEN f, GEN a, GEN T, GEN p, long e,
7557
void *E, GEN early(void *E, GEN x, GEN q)}
7558
as \tet{ZpX_ZpXQ_liftroot} with early abort: the function \kbd{early(E,x,q)}
7559
will be called with $x$ is a root of $f$ modulo $q=p^n$ for some $n$. If
7560
\kbd{early} returns a non-\kbd{NULL} value $z$, the function returns $z$
7561
immediately.
7562
7563
\fun{GEN}{ZpXQ_log}{GEN a, GEN T, GEN p, long e} $T$ being a \kbd{ZpX}
7564
irreducible modulo $p$, return the logarithm of $a$ in $\Z_p[X]/(T)$ to
7565
precision $e$, assuming that $a\equiv 1 \pmod{p\Z_p[X]}$ if $p$ odd or
7566
$a\equiv 1 \pmod{4\Z_2[X]}$ if $p=2$.
7567
7568
\subsec{Zq}
7569
7570
\fun{GEN}{Zq_sqrtnlift}{GEN b, GEN n, GEN a, GEN T, GEN p, long e}
7571
7572
\subsec{ZpXQM}
7573
7574
\fun{GEN}{ZpXQM_prodFrobenius}{GEN M, GEN T, GEN p, long e}
7575
returns the product of matrices $M\*\sigma(M)\*\sigma^2(M)\ldots\sigma^{n-1}(M)$
7576
to precision $e$ where $\sigma$ is the lift of the Frobenius automorphism
7577
over $\Z_p[X]/(T)$ and $n$ is the degree of $T$.
7578
7579
\subsec{ZpXQX}
7580
7581
\fun{GEN}{ZpXQX_liftfact}{GEN A, GEN B, GEN T, GEN pe, GEN p, long e} is the
7582
routine underlying \tet{polhensellift}. Here, $p$ is prime, $T(Y)$ defines a
7583
finite field $\F_q$. $A$ is a polynomial in $\Z[X,Y]$, whose leading
7584
coefficient is nonzero in $\F_q$. $B$ is a vector of monic or \kbd{FqX},
7585
pairwise coprime in $\F_q[X]$, whose product is congruent to $A/\text{lc}(A)$
7586
in $\F_q[X]$. Lifts the elements of $B$ mod $\kbd{pe} = p^e$, such that the
7587
congruence now holds mod $(T,p^e)$.
7588
7589
\fun{GEN}{ZpXQX_liftroot}{GEN f, GEN a, GEN T, GEN p, long e} as
7590
\tet{ZpX_liftroot}, but $f$ is now a polynomial in $\Z[X,Y]$ and lift the
7591
root $a$ in the unramified extension of $\Q_p$ with residue field $\F_p[Y]/(T)$,
7592
assuming $v_p(f(a))>0$ and $v_p(f'(a))=0$.
7593
7594
\fun{GEN}{ZpXQX_liftroot_vald}{GEN f, GEN a, long v, GEN T, GEN p, long e}
7595
returns the foots of $f$ as \tet{ZpXQX_liftroot}, where $v$ is the valuation
7596
of the content of $f'$ and it is required that $v_p(f(a))>v$ and
7597
$v_p(f'(a))=v$.
7598
7599
\fun{GEN}{ZpXQX_roots}{GEN F, GEN T, GEN p, long e}
7600
7601
\fun{GEN}{ZpXQX_liftroots}{GEN F, GEN S, GEN T, GEN p, long e}
7602
7603
\fun{GEN}{ZpXQX_divrem}{GEN x, GEN Sp, GEN T,GEN q,GEN p,long e, GEN *pr}
7604
as \kbd{FpXQX\_divrem}. The parameter $q$ must be equal to $p^e$.
7605
7606
\fun{GEN}{ZpXQX_digits}{GEN x, GEN B, GEN T, GEN q, GEN p, long e}
7607
As \kbd{FpXQX\_digits}. The parameter $q$ must be equal to $p^e$.
7608
7609
\subsec{ZqX}
7610
7611
\fun{GEN}{ZqX_roots}{GEN F, GEN T, GEN p, long e}
7612
7613
\fun{GEN}{ZqX_liftfact}{GEN A, GEN B, GEN T, GEN pe, GEN p, long e}
7614
7615
\fun{GEN}{ZqX_liftroot}{GEN f, GEN a, GEN T, GEN p, long e}
7616
7617
\subsec{Other $p$-adic functions}
7618
7619
\fun{GEN}{ZpM_echelon}{GEN M, long early_abort, GEN p, GEN pm} given a
7620
\kbd{ZM} $M$, a prime $p$ and $\kbd{pm} = p^m$, returns an echelon form
7621
$E$ for $M$ mod $p^m$. I.e. there exist a square integral matrix $U$ with
7622
$\det U$ coprime to $p$ such that $E = MU$ modulo $p^m$. I
7623
\kbd{early\_abort} is nonzero, return NULL as soon as one pivot in
7624
the echelon form is divisible by $p^m$. The echelon form is an upper
7625
triangular HNF, we do not waste time to reduce it to Gauss-Jordan form.
7626
7627
\fun{GEN}{zlm_echelon}{GEN M, long early_abort, ulong p, ulong pm}
7628
variant of \kbd{ZpM\_echelon}, for a \kbd{Zlm} $M$.
7629
7630
\fun{GEN}{ZlM_gauss}{GEN a, GEN b, ulong p, long e, GEN C} as \kbd{gauss}
7631
with the following peculiarities: $a$ and $b$ are \kbd{ZM}, such that $a$ is
7632
invertible modulo $p$. Optional $C$ is an \kbd{Flm} that is an inverse of
7633
$a\mod p$ or \kbd{NULL}. Return the matrix $x$ such that $ax=b\mod p^e$ and
7634
all elements of $x$ are in $[0,p^e-1]$. For efficiency, it is better
7635
to reduce $a$ and $b$ mod $p^e$ first.
7636
7637
\fun{GEN}{padic_to_Q}{GEN x} truncate the \typ{PADIC} to a \typ{INT} or
7638
\typ{FRAC}.
7639
7640
\fun{GEN}{padic_to_Q_shallow}{GEN x} shallow version of \tet{padic_to_Q}
7641
7642
\fun{GEN}{QpV_to_QV}{GEN v} apply \tet{padic_to_Q_shallow}
7643
7644
\fun{long}{padicprec}{GEN x, GEN p} returns the absolute $p$-adic precision of
7645
the object $x$, by definition the minimum precision of the components of $x$.
7646
For a nonzero \typ{PADIC}, this returns \kbd{valp(x) + precp(x)}.
7647
7648
\fun{long}{padicprec_relative}{GEN x} returns the relative $p$-adic
7649
precision of the \typ{INT}, \typ{FRAC}, or \typ{PADIC} $x$ (minimum precision
7650
of the components of $x$ for \typ{POL} or vector/matrices).
7651
For a \typ{PADIC}, this returns \kbd{precp(x)} if $x\neq0$, and $0$ for $x=0$.
7652
7653
\subsubsec{low-level}
7654
7655
The following technical function returns an optimal sequence of $p$-adic
7656
accuracies, for a given target accuracy:
7657
7658
\fun{ulong}{quadratic_prec_mask}{long n} we want to reach accuracy
7659
$n\geq 1$, starting from accuracy 1, using a quadratically convergent,
7660
self-correcting, algorithm; in other words, from inputs correct to accuracy
7661
$l$ one iteration outputs a result correct to accuracy $2l$.
7662
For instance, to reach $n = 9$, we want to use accuracies
7663
$[1,2,3,5,9]$ instead of $[1,2,4,8,9]$. The idea is to essentially double
7664
the accuracy at each step, and not overshoot in the end.
7665
7666
Let $a_0$ = 1, $a_1 = 2, \ldots, a_k = n$, be the desired sequence of
7667
accuracies. To obtain it, we work backwards and set
7668
$$ a_k = n,\quad a_{i-1} = (a_i + 1)\,\bs\, 2.$$
7669
This is in essence what the function returns.
7670
But we do not want to store the $a_i$ explicitly, even as a \typ{VECSMALL},
7671
since this would leave an object on the stack. Instead, we store $a_i$
7672
implicitly in a bitmask \kbd{MASK}: let $a_0 = 1$, if the $i$-th bit of the
7673
mask is set, set $a_{i+1} = 2a_i - 1$, and $2a_i$ otherwise; in short the
7674
bits indicate the places where we do something special and do not quite
7675
double the accuracy (which would be the straightforward thing to do).
7676
7677
In fact, to avoid returning separately the mask and the sequence length
7678
$k+1$, the function returns $\kbd{MASK} + 2^{k+1}$, so the highest bit of
7679
the mask indicates the length of the sequence, and the following ones give
7680
an algorithm to obtain the accuracies. This is much simpler than it sounds,
7681
here is what it looks like in practice:
7682
\bprog
7683
ulong mask = quadratic_prec_mask(n);
7684
long l = 1;
7685
while (mask > 1) { /* here, the result is known to accuracy l */
7686
l = 2*l; if (mask & 1) l--; /* new accuracy l for the iteration */
7687
mask >>= 1; /* pop low order bit */
7688
/* ... lift to the new accuracy ... */
7689
}
7690
/* we are done. At this point l = n */
7691
@eprog\noindent We just pop the bits in \kbd{mask} starting from the low
7692
order bits, stop when \kbd{mask} is $1$ (that last bit corresponds to the
7693
$2^{k+1}$ that we added to the mask proper). Note that there is nothing
7694
specific to Hensel lifts in that function: it would work equally well for
7695
an Archimedean Newton iteration.
7696
7697
Note that in practice, we rather use an infinite loop, and insert an
7698
\bprog
7699
if (mask == 1) break;
7700
@eprog\noindent in the middle of the loop: the loop body usually includes
7701
preparations for the next iterations (e.g. lifting Bezout coefficients
7702
in a quadratic Hensel lift), which are costly and useless in the \emph{last}
7703
iteration.
7704
7705
\subsec{Conversions involving single precision objects}
7706
7707
\subsubsec{To single precision}
7708
7709
\fun{ulong}{Rg_to_Fl}{GEN z, ulong p}, \kbd{z} which can be mapped to
7710
$\Z/p\Z$: a \typ{INT}, a \typ{INTMOD} whose modulus is divisible by $p$,
7711
a \typ{FRAC} whose denominator is coprime to $p$, or a \typ{PADIC} with
7712
underlying prime $\ell$ satisfying $p = \ell^n$ for some $n$ (less than the
7713
accuracy of the input). Returns \kbd{lift(z * Mod(1,p))}, normalized, as an
7714
\kbd{Fl}.
7715
7716
\fun{ulong}{Rg_to_F2}{GEN z}, as \tet{Rg_to_Fl} for $p = 2$.
7717
7718
\fun{ulong}{padic_to_Fl}{GEN x, ulong p} special case of \tet{Rg_to_Fl},
7719
for a $x$ a \typ{PADIC}.
7720
7721
\fun{GEN}{RgX_to_F2x}{GEN x}, \kbd{x} a \typ{POL}, returns the
7722
\kbd{F2x} obtained by applying \kbd{Rg\_to\_Fl} coefficientwise.
7723
7724
\fun{GEN}{RgX_to_Flx}{GEN x, ulong p}, \kbd{x} a \typ{POL}, returns the
7725
\kbd{Flx} obtained by applying \kbd{Rg\_to\_Fl} coefficientwise.
7726
7727
\fun{GEN}{RgXV_to_FlxV}{GEN x, ulong p}, \kbd{x} a vector, returns the
7728
\kbd{FlxV} obtained by applying \kbd{RgX\_to\_Flx} coefficientwise.
7729
7730
\fun{GEN}{Rg_to_F2xq}{GEN z, GEN T}, \kbd{z} a \kbd{GEN} which can be
7731
mapped to $\F_2[X]/(T)$: anything \kbd{Rg\_to\_Fl} can be applied to,
7732
a \typ{POL} to which \kbd{RgX\_to\_F2x} can be applied to, a \typ{POLMOD}
7733
whose modulus is divisible by $T$ (once mapped to a \kbd{F2x}), a suitable
7734
\typ{RFRAC}. Returns \kbd{z} as an \kbd{F2xq}, normalized.
7735
7736
\fun{GEN}{Rg_to_Flxq}{GEN z, GEN T, ulong p}, \kbd{z} a \kbd{GEN} which can be
7737
mapped to $\F_p[X]/(T)$: anything \kbd{Rg\_to\_Fl} can be applied to,
7738
a \typ{POL} to which \kbd{RgX\_to\_Flx} can be applied to, a \typ{POLMOD}
7739
whose modulus is divisible by $T$ (once mapped to a \kbd{Flx}), a suitable
7740
\typ{RFRAC}. Returns \kbd{z} as an \kbd{Flxq}, normalized.
7741
7742
\fun{GEN}{RgX_to_FlxqX}{GEN z, GEN T, ulong p}, \kbd{z} a \kbd{GEN} which can be
7743
mapped to $\F_p[x]/(T)[X]$: anything \kbd{Rg\_to\_Flxq} can be applied to,
7744
a \typ{POL} to which \kbd{RgX\_to\_Flx} can be applied to, a \typ{POLMOD}
7745
whose modulus is divisible by $T$ (once mapped to a \kbd{Flx}), a suitable
7746
\typ{RFRAC}. Returns \kbd{z} as an \kbd{FlxqX}, normalized.
7747
7748
\fun{GEN}{ZX_to_Flx}{GEN x, ulong p} reduce \kbd{ZX}~\kbd{x} modulo \kbd{p}
7749
(yielding an \kbd{Flx}). Faster than \kbd{RgX\_to\_Flx}.
7750
7751
\fun{GEN}{ZV_to_Flv}{GEN x, ulong p} reduce \kbd{ZV}~\kbd{x} modulo \kbd{p}
7752
(yielding an \kbd{Flv}).
7753
7754
\fun{GEN}{ZXV_to_FlxV}{GEN v, ulong p}, as \kbd{ZX\_to\_Flx}, repeatedly
7755
called on the vector's coefficients.
7756
7757
\fun{GEN}{ZXT_to_FlxT}{GEN v, ulong p}, as \kbd{ZX\_to\_Flx}, repeatedly
7758
called on the tree leaves.
7759
7760
\fun{GEN}{ZXX_to_FlxX}{GEN B, ulong p, long v}, as \kbd{ZX\_to\_Flx},
7761
repeatedly called on the polynomial's coefficients.
7762
7763
\fun{GEN}{zxX_to_FlxX}{GEN z, ulong p} as \kbd{zx\_to\_Flx},
7764
repeatedly called on the polynomial's coefficients.
7765
7766
\fun{GEN}{ZXXV_to_FlxXV}{GEN V, ulong p, long v}, as \kbd{ZXX\_to\_FlxX},
7767
repeatedly called on the vector's coefficients.
7768
7769
\fun{GEN}{ZXXT_to_FlxXT}{GEN V, ulong p, long v}, as \kbd{ZXX\_to\_FlxX},
7770
repeatedly called on the tree leaves.
7771
7772
\fun{GEN}{RgV_to_Flv}{GEN x, ulong p} reduce the \typ{VEC}/\typ{COL}
7773
$x$ modulo $p$, yielding a \typ{VECSMALL}.
7774
7775
\fun{GEN}{RgM_to_Flm}{GEN x, ulong p} reduce the \typ{MAT} $x$ modulo $p$.
7776
7777
\fun{GEN}{ZM_to_Flm}{GEN x, ulong p} reduce \kbd{ZM}~$x$ modulo $p$
7778
(yielding an \kbd{Flm}).
7779
7780
\fun{GEN}{ZXC_to_FlxC}{GEN x, ulong p, long sv} reduce \kbd{ZXC}~$x$ modulo $p$
7781
(yielding an \kbd{FlxC}). Assume that \kbd{sv = evalvarn(v)} where $v$ is
7782
the variable number of the entries of $x$. It is allowed for the entries of
7783
$x$ to be \typ{INT}.
7784
7785
\fun{GEN}{ZXM_to_FlxM}{GEN x, ulong p, long sv} reduce \kbd{ZXM}~$x$ modulo $p$
7786
(yielding an \kbd{FlxM}). Assume that \kbd{sv = evalvarn(v)} where $v$ is
7787
the variable number of the entries of $x$. It is allowed for the entries of $x$
7788
to be \typ{INT}.
7789
7790
\fun{GEN}{ZV_to_zv}{GEN z}, converts coefficients using \kbd{itos}
7791
7792
\fun{GEN}{ZV_to_nv}{GEN z}, converts coefficients using \kbd{itou}
7793
7794
\fun{GEN}{ZM_to_zm}{GEN z}, converts coefficients using \kbd{itos}
7795
7796
\subsubsec{From single precision}
7797
7798
\fun{GEN}{Flx_to_ZX}{GEN z}, converts to \kbd{ZX} (\typ{POL} of nonnegative
7799
\typ{INT}s in this case)
7800
7801
\fun{GEN}{Flx_to_FlxX}{GEN z}, converts to \kbd{FlxX} (\typ{POL} of constant
7802
\kbd{Flx} in this case).
7803
7804
\fun{GEN}{Flx_to_ZX_inplace}{GEN z}, same as \kbd{Flx\_to\_ZX}, in place
7805
(\kbd{z} is destroyed).
7806
7807
\fun{GEN}{FlxX_to_ZXX}{GEN B}, converts an \kbd{FlxX} to a polynomial with
7808
\kbd{ZX} or \typ{INT} coefficients (repeated calls to \kbd{Flx\_to\_ZX}).
7809
7810
\fun{GEN}{FlxXC_to_ZXXC}{GEN B}, converts an \kbd{FlxXC} to a \typ{COL} with
7811
\kbd{ZXX} coefficients (repeated calls to \kbd{FlxX\_to\_ZXX}).
7812
7813
\fun{GEN}{FlxXM_to_ZXXM}{GEN B}, converts an \kbd{FlxXM} to a \typ{MAT} with
7814
\kbd{ZXX} coefficients (repeated calls to \kbd{FlxX\_to\_ZXX}).
7815
7816
\fun{GEN}{FlxC_to_ZXC}{GEN x}, converts a vector of \kbd{Flx} to a column
7817
vector of polynomials with \typ{INT} coefficients (repeated calls to
7818
\kbd{Flx\_to\_ZX}).
7819
7820
\fun{GEN}{FlxV_to_ZXV}{GEN x}, as above but return a \typ{VEC}.
7821
7822
\fun{void}{F2xV_to_FlxV_inplace}{GEN v} v is destroyed.
7823
7824
\fun{void}{F2xV_to_ZXV_inplace}{GEN v} v is destroyed.
7825
7826
\fun{void}{FlxV_to_ZXV_inplace}{GEN v} v is destroyed.
7827
7828
\fun{GEN}{FlxM_to_ZXM}{GEN z}, converts a matrix of \kbd{Flx} to a matrix of
7829
polynomials with \typ{INT} coefficients (repeated calls to \kbd{Flx\_to\_ZX}).
7830
7831
\fun{GEN}{zx_to_ZX}{GEN z}, as \kbd{Flx\_to\_ZX}, without assuming
7832
the coefficients to be nonnegative.
7833
7834
\fun{GEN}{zx_to_Flx}{GEN z, ulong p} as \kbd{Flx\_red} without assuming
7835
the coefficients to be nonnegative.
7836
7837
\fun{GEN}{Flc_to_ZC}{GEN z}, converts to \kbd{ZC} (\typ{COL} of nonnegative
7838
\typ{INT}s in this case)
7839
7840
\fun{GEN}{Flc_to_ZC_inplace}{GEN z}, same as \kbd{Flc\_to\_ZC}, in place
7841
(\kbd{z} is destroyed).
7842
7843
\fun{GEN}{Flv_to_ZV}{GEN z}, converts to \kbd{ZV} (\typ{VEC} of nonnegative
7844
\typ{INT}s in this case)
7845
7846
\fun{GEN}{Flm_to_ZM}{GEN z}, converts to \kbd{ZM} (\typ{MAT} with
7847
nonnegative \typ{INT}s coefficients in this case)
7848
7849
\fun{GEN}{Flm_to_ZM_inplace}{GEN z}, same as \kbd{Flm\_to\_ZM}, in place
7850
(\kbd{z} is destroyed).
7851
7852
\fun{GEN}{zc_to_ZC}{GEN z} as \kbd{Flc\_to\_ZC}, without assuming
7853
coefficients are nonnegative.
7854
7855
\fun{GEN}{zv_to_ZV}{GEN z} as \kbd{Flv\_to\_ZV}, without assuming
7856
coefficients are nonnegative.
7857
7858
\fun{GEN}{zm_to_ZM}{GEN z} as \kbd{Flm\_to\_ZM}, without assuming
7859
coefficients are nonnegative.
7860
7861
\fun{GEN}{zv_to_Flv}{GEN z, ulong p}
7862
7863
\fun{GEN}{zm_to_Flm}{GEN z, ulong p}
7864
7865
\subsubsec{Mixed precision linear algebra} Assumes dimensions are compatible.
7866
Multiply a multiprecision object by a single-precision one.
7867
7868
\fun{GEN}{RgM_zc_mul}{GEN x, GEN y}
7869
7870
\fun{GEN}{RgMrow_zc_mul}{GEN x, GEN y, long i}
7871
7872
\fun{GEN}{RgM_zm_mul}{GEN x, GEN y}
7873
7874
\fun{GEN}{RgV_zc_mul}{GEN x, GEN y}
7875
7876
\fun{GEN}{RgV_zm_mul}{GEN x, GEN y}
7877
7878
\fun{GEN}{ZM_zc_mul}{GEN x, GEN y}
7879
7880
\fun{GEN}{zv_ZM_mul}{GEN x, GEN y}
7881
7882
\fun{GEN}{ZV_zc_mul}{GEN x, GEN y}
7883
7884
\fun{GEN}{ZM_zm_mul}{GEN x, GEN y}
7885
7886
\fun{GEN}{ZC_z_mul}{GEN x, long y}
7887
7888
\fun{GEN}{ZM_nm_mul}{GEN x, GEN y} the entries of $y$ are \kbd{ulong}s.
7889
7890
\fun{GEN}{nm_Z_mul}{GEN y, GEN c} the entries of $y$ are \kbd{ulong}s.
7891
7892
\subsubsec{Miscellaneous involving Fl}
7893
7894
\fun{GEN}{Fl_to_Flx}{ulong x, long evx} converts a \kbd{unsigned long} to a
7895
scalar \kbd{Flx}. Assume that \kbd{evx = evalvarn(vx)} for some variable
7896
number \kbd{vx}.
7897
7898
\fun{GEN}{Z_to_Flx}{GEN x, ulong p, long sv} converts a \typ{INT} to a scalar
7899
\kbd{Flx} polynomial. Assume that \kbd{sv = evalvarn(v)} for some variable
7900
number \kbd{v}.
7901
7902
\fun{GEN}{Flx_to_Flv}{GEN x, long n} converts from \kbd{Flx} to \kbd{Flv}
7903
with \kbd{n} components (assumed larger than the number of coefficients of
7904
\kbd{x}).
7905
7906
\fun{GEN}{zx_to_zv}{GEN x, long n} as \kbd{Flx\_to\_Flv}.
7907
7908
\fun{GEN}{Flv_to_Flx}{GEN x, long sv} converts from vector (coefficient
7909
array) to (normalized) polynomial in variable $v$.
7910
7911
\fun{GEN}{zv_to_zx}{GEN x, long n} as \kbd{Flv\_to\_Flx}.
7912
7913
\fun{GEN}{Flm_to_FlxV}{GEN x, long sv} converts the columns of
7914
\kbd{Flm}~\kbd{x} to an array of \kbd{Flx} in the variable $v$
7915
(repeated calls to \kbd{Flv\_to\_Flx}).
7916
7917
\fun{GEN}{zm_to_zxV}{GEN x, long n} as \kbd{Flm\_to\_FlxV}.
7918
7919
\fun{GEN}{Flm_to_FlxX}{GEN x, long sw, long sv} same as
7920
\kbd{Flm\_to\_FlxV(x,sv)} but returns the result as a (normalized) polynomial
7921
in variable $w$.
7922
7923
\fun{GEN}{FlxV_to_Flm}{GEN v, long n} reverse \kbd{Flm\_to\_FlxV}, to obtain
7924
an \kbd{Flm} with \kbd{n} rows (repeated calls to \kbd{Flx\_to\_Flv}).
7925
7926
\fun{GEN}{FlxX_to_Flx}{GEN P} Let $P(x,X)$ be a \kbd{FlxX}, return $P(0,X)$
7927
as a \kbd{Flx}.
7928
7929
\fun{GEN}{FlxX_to_Flm}{GEN v, long n} reverse \kbd{Flm\_to\_FlxX}, to obtain
7930
an \kbd{Flm} with \kbd{n} rows (repeated calls to \kbd{Flx\_to\_Flv}).
7931
7932
\fun{GEN}{FlxX_to_FlxC}{GEN B, long n, long sv} see \kbd{RgX\_to\_RgV}.
7933
The coefficients of \kbd{B} are assumed to be in the variable $v$.
7934
7935
\fun{GEN}{FlxV_to_FlxX}{GEN x, long v} see \kbd{RgV\_to\_RgX}.
7936
7937
\fun{GEN}{FlxXV_to_FlxM}{GEN V, long n, long sv} see \kbd{RgXV\_to\_RgM}.
7938
The coefficients of \kbd{V[i]} are assumed to be in the variable $v$.
7939
7940
\fun{GEN}{Fly_to_FlxY}{GEN a, long sv} convert coefficients of \kbd{a} to
7941
constant \kbd{Flx} in variable $v$.
7942
7943
\subsubsec{Miscellaneous involving \kbd{F2x}}
7944
7945
\fun{GEN}{F2x_to_F2v}{GEN x, long n} converts from \kbd{F2x} to \kbd{F2v}
7946
with \kbd{n} components (assumed larger than the number of coefficients of
7947
\kbd{x}).
7948
7949
\fun{GEN}{F2xC_to_ZXC}{GEN x}, converts a vector of \kbd{F2x} to a column
7950
vector of polynomials with \typ{INT} coefficients (repeated calls to
7951
\kbd{F2x\_to\_ZX}).
7952
7953
\fun{GEN}{F2xC_to_FlxC}{GEN x}
7954
7955
\fun{GEN}{FlxC_to_F2xC}{GEN x}
7956
7957
\fun{GEN}{F2xV_to_F2m}{GEN v, long n} \kbd{F2x\_to\_F2v} to each polynomial
7958
to get an \kbd{F2m} with \kbd{n} rows.
7959
7960
\section{Higher arithmetic over $\Z$: primes, factorization}
7961
7962
\subsec{Pure powers}
7963
7964
\fun{long}{Z_issquare}{GEN n} returns $1$ if the \typ{INT} $n$ is
7965
a square, and $0$ otherwise. This is tested first modulo small prime
7966
powers, then \kbd{sqrtremi} is called.
7967
7968
\fun{long}{Z_issquareall}{GEN n, GEN *sqrtn} as \kbd{Z\_issquare}. If
7969
$n$ is indeed a square, set \kbd{sqrtn} to its integer square root.
7970
Uses a fast congruence test mod $64\times 63\times 65\times 11$ before
7971
computing an integer square root.
7972
7973
\fun{long}{Z_ispow2}{GEN x} returns $1$ if the \typ{INT} $x$ is a power of
7974
$2$, and $0$ otherwise.
7975
7976
\fun{long}{uissquare}{ulong n} as \kbd{Z\_issquare},
7977
for an \kbd{ulong} operand \kbd{n}.
7978
7979
\fun{long}{uissquareall}{ulong n, ulong *sqrtn} as \kbd{Z\_issquareall},
7980
for an \kbd{ulong} operand \kbd{n}.
7981
7982
\fun{ulong}{usqrt}{ulong a} returns the floor of the square root of $a$.
7983
7984
\fun{ulong}{usqrtn}{ulong a, ulong n} returns the floor of the $n$-th root
7985
of $a$.
7986
7987
\fun{long}{Z_ispower}{GEN x, ulong k} returns $1$ if the \typ{INT} $n$ is a
7988
$k$-th power, and $0$ otherwise; assume that $k > 1$.
7989
7990
\fun{long}{Z_ispowerall}{GEN x, ulong k, GEN *pt} as \kbd{Z\_ispower}. If
7991
$n$ is indeed a $k$-th power, set \kbd{*pt} to its integer $k$-th root.
7992
7993
\fun{long}{Z_isanypower}{GEN x, GEN *ptn} returns the maximal $k\geq 2$ such
7994
that the \typ{INT} $x = n^k$ is a perfect power, or $0$ if no such $k$ exist;
7995
in particular \kbd{ispower(1)}, \kbd{ispower(0)}, \kbd{ispower(-1)} all
7996
return 0. If the return value $k$ is not $0$ (so that $x = n^k$) and
7997
\kbd{ptn} is not \kbd{NULL}, set \kbd{*ptn} to $n$.
7998
7999
The following low-level functions are called by \tet{Z_isanypower} but can
8000
be directly useful:
8001
8002
\fun{int}{is_357_power}{GEN x, GEN *ptn, ulong *pmask} tests whether the
8003
integer $x > 0$ is a $3$-rd, $5$-th or $7$-th power. The bits of \kbd{*mask}
8004
initially indicate which test is to be performed;
8005
bit $0$: $3$-rd,
8006
bit $1$: $5$-th,
8007
bit $2$: $7$-th (e.g.~$\kbd{*pmask} = 7$ performs all tests). They are
8008
updated during the call: if the ``$i$-th power'' bit is set to $0$
8009
then $x$ is not a $k$-th power. The function returns $0$
8010
(not a
8011
$3$-rd,
8012
$5$-th or
8013
$7$-th power),
8014
$3$
8015
($3$-rd power,
8016
not a $5$-th or
8017
$7$-th power),
8018
$5$
8019
($5$-th power,
8020
not a $7$-th power),
8021
or $7$
8022
($7$-th power); if an $i$-th power bit is initially set to $0$, we take it
8023
at face value and assume $x$ is not an $i$-th power without performing any
8024
test. If the return value $k$ is nonzero, set \kbd{*ptn} to $n$ such that $x
8025
= n^k$.
8026
8027
\fun{int}{is_pth_power}{GEN x, GEN *ptn, forprime_t *T, ulong cutoff}
8028
let $x > 0$ be an integer, $\kbd{cutoff} > 0$ and $T$ be an iterator over
8029
primes $\geq 11$, we look for the smallest prime $p$ such that $x = n^p$
8030
(advancing $T$ as we go along). The $11$ is due to the fact that
8031
\tet{is_357_power} and \kbd{issquare} are faster than the generic version for
8032
$p < 11$.
8033
8034
Fail and return $0$ when the existence of $p$ would imply $2^{\kbd{cutoff}} >
8035
x^{1/p}$, meaning that a possible $n$ is so small that it should have been
8036
found by trial division; for maximal speed, you should start by a round of
8037
trial division, but the cut-off may also be set to $1$ for a rigorous result
8038
without any trial division.
8039
8040
Otherwise returns the smallest suitable prime power $p^i$ and set \kbd{*ptn}
8041
to the $p^i$-th root of $x$ (which is now not a $p$-th power). We may
8042
immediately recall the function with the same parameters after setting $x =
8043
\kbd{*ptn}$: it will start at the next prime.
8044
8045
\subsec{Factorization}
8046
8047
\fun{GEN}{Z_factor}{GEN n} factors the \typ{INT} \kbd{n}. The ``primes''
8048
in the factorization are actually strong pseudoprimes.
8049
8050
\fun{GEN}{absZ_factor}{GEN n} returns \kbd{Z\_factor(absi(n))}.
8051
8052
\fun{long}{Z_issmooth}{GEN n, ulong lim} returns $1$ if all the
8053
prime factors of the \typ{INT} $n$ are less or equal to $lim$.
8054
8055
\fun{GEN}{Z_issmooth_fact}{GEN n, ulong lim} returns \kbd{NULL} if a prime
8056
factor of the \typ{INT} $n$ is $> lim$, and returns the factorization
8057
of $n$ otherwise, as a \typ{MAT} with \typ{VECSMALL} columns (word-size
8058
primes and exponents). Neither memory-clean nor suitable for
8059
\kbd{gerepileupto}.
8060
8061
\fun{GEN}{Z_factor_until}{GEN n, GEN lim} as \kbd{Z\_factor}, but stop the
8062
factorization process as soon as the unfactored part is smaller than \kbd{lim}.
8063
The resulting factorization matrix only contains the factors found. No other
8064
assumptions can be made on the remaining factors.
8065
8066
\fun{GEN}{Z_factor_limit}{GEN n, ulong lim} trial divide $n$ by all primes $p
8067
< \kbd{lim}$ in the precomputed list of prime numbers and the \kbd{addprimes}
8068
prime table. Return the corresponding factorization matrix. The first column
8069
of the factorization matrix may contain a single composite, which may
8070
or may not be the last entry in presence of a prime table.
8071
8072
If $\kbd{lim} = 0$, the effect is the same as setting $\kbd{lim} =
8073
\kbd{maxprime()} + 1$: use all precomputed primes.
8074
8075
\fun{GEN}{absZ_factor_limit}{GEN n, ulong all} returns
8076
\kbd{Z\_factor\_limit(absi(n))}.
8077
8078
\fun{GEN}{absZ_factor_limit_strict}{GEN n, ulong all, GEN *pU} analogous to
8079
\tet{absZ_factor_limit}, with a better interface: trial divide $n$ by all
8080
primes $p < \kbd{lim}$ in the precomputed list of prime numbers and the
8081
\kbd{addprimes} prime table. Return the corresponding factorization matrix.
8082
In this case, a composite cofactor is \emph{not} included.
8083
8084
If \kbd{pU} is not \kbd{NULL}, set it to the cofactor, which is either
8085
\kbd{NULL} (no cofactor) or $[q,k]$, where $k > 0$, $q$ is a composite
8086
whose prime divisors are greater than \kbd{all}, not a pure power
8087
and $q^k$ is the largest power of $q$ dividing $n$.
8088
8089
\fun{GEN}{boundfact}{GEN x, ulong lim} as \tet{Z_factor_limit}, applying to
8090
\typ{INT} or \typ{FRAC} inputs.
8091
8092
\fun{GEN}{Z_smoothen}{GEN n, GEN L, GEN *pP, GEN *pE} given a \typ{VEC}
8093
$L$ containing a list of primes and a \typ{INT} $n$, trial divide
8094
$n$ by the elements of $L$ and return the cofactor. Return \kbd{NULL} if the
8095
cofactor is $\pm 1$. \kbd{*P} and \kbd{*E} contain the list of prime divisors
8096
found and their exponents, as \typ{VECSMALL}s. Neither memory-clean, nor
8097
suitable for \tet{gerepileupto}.
8098
8099
\fun{GEN}{Z_lsmoothen}{GEN n, GEN L, GEN *pP, GEN *pE} as
8100
\kbd{Z\_smoothen} where $L$ is a \typ{VECSMALL} of small primes and both
8101
\kbd{*P} and \kbd{*E} are given as \typ{VECSMALL}.
8102
8103
\fun{GEN}{Z_factor_listP}{GEN N, GEN L} given a \typ{INT} $N$, a vector or
8104
primes $L$ containing all prime divisors of $N$ (and possibly others). Return
8105
\kbd{factor(N)}. Neither memory-clean, nor suitable for \tet{gerepileupto}.
8106
8107
\fun{GEN}{factor_pn_1}{GEN p, ulong n} returns the factorization of $p^n-1$,
8108
where $p$ is prime and $n$ is a positive integer.
8109
8110
\fun{GEN}{factor_pn_1_limit}{GEN p, ulong n, ulong B} returns a partial
8111
factorization of $p^n-1$, where $p$ is prime and $n$ is a positive integer.
8112
Don't actively search for prime divisors $p > B$, but we may find still find
8113
some due to Aurifeuillian factorizations. Any entry $> B^2$ in the output
8114
factorization matrix is \emph{a priori} not a prime (but may well be).
8115
8116
\fun{GEN}{factor_Aurifeuille_prime}{GEN p, long n} an Aurifeuillian factor
8117
of $\phi_n(p)$, assuming $p$ prime and an Aurifeuillian factor exists
8118
($p \zeta_n$ is a square in $\Q(\zeta_n)$).
8119
8120
\fun{GEN}{factor_Aurifeuille}{GEN a, long d} an Aurifeuillian factor of
8121
$\phi_n(a)$, assuming $a$ is a nonzero integer and $n > 2$. Returns $1$
8122
if no Aurifeuillian factor exists.
8123
8124
\fun{GEN}{odd_prime_divisors}{GEN a} \typ{VEC} of all prime divisors of the
8125
\typ{INT} $a$.
8126
8127
\fun{GEN}{factoru}{ulong n}, returns the factorization of $n$. The result
8128
is a $2$-component vector $[P,E]$, where $P$ and $E$ are \typ{VECSMALL}
8129
containing the prime divisors of $n$, and the $v_p(n)$.
8130
8131
\fun{GEN}{factoru_pow}{ulong n}, returns the factorization of $n$. The result
8132
is a $3$-component vector $[P,E,C]$, where $P$, $E$ and $C$ are
8133
\typ{VECSMALL} containing the prime divisors of $n$, the $v_p(n)$
8134
and the $p^{v_p(n)}$.
8135
8136
\fun{GEN}{vecfactoru}{ulong a, ulong b}, returns a \typ{VEC} $v$ containing
8137
the factorizations (\tet{factoru} format) of $a,\dots, b$; assume that $b
8138
\geq a > 0$. Uses a sieve with primes up to $\sqrt{b}$. For all
8139
$c$, $a \leq c \leq b$, the factorization of $c$ is given in $v[c-a+1]$.
8140
8141
\fun{GEN}{vecfactoroddu}{ulong a, ulong b}, returns a \typ{VEC} $v$ containing
8142
the factorizations (\tet{factoru} format) of odd integers in $a,\dots, b$;
8143
assume that $b \geq a > 0$ are odd. Uses a sieve with primes up to
8144
$\sqrt{b}$. For all odd $c$, $a \leq c \leq b$, the factorization of $c$ is
8145
given in in $v[(c-a)/2 + 1]$.
8146
8147
\fun{GEN}{vecfactoru_i}{ulong a, ulong b}, private version of
8148
\kbd{vecfactoru}, not memory clean.
8149
8150
\fun{GEN}{vecfactoroddu_i}{ulong a, ulong b}, private version of
8151
\kbd{vecfactoroddu}, not memory clean.
8152
8153
\fun{GEN}{vecfactorsquarefreeu}{ulong a, ulong b} return a \typ{VEC} $v$
8154
containing the prime divisors of squarefree integers in $a,\dots,b$; assume
8155
that $a \leq b$. Uses a sieve with primes up to $\sqrt{b}$.
8156
For all squarefree $c$, $a\leq c\leq b$, the prime divisors of $c$ (as a
8157
\typ{VECSMALL}) are given in $v[c-a+1]$, and the other entries are
8158
\kbd{NULL}. Note that because of these \kbd{NULL} markers, $v$ is not a valid
8159
\kbd{GEN}, it is not memory clean and cannot be used in garbage collection
8160
routines.
8161
8162
\fun{GEN}{vecfactorsquarefreeu_coprime}{ulong a, ulong b, GEN P}
8163
given a \emph{sorted} \typ{VECSMALL} of primes $P$, return a \typ{VEC} $v$
8164
containing the prime divisors of squarefree integers in $a,\dots,b$ coprime to
8165
the elements of $P$; assume that $a \leq b$. Uses a sieve with primes up to
8166
$\sqrt{b}$. For all squarefree $c$, $a\leq c\leq b$, the prime divisors of $c$
8167
(as a \typ{VECSMALL}) are given in $v[c-a+1]$, and the other entries are
8168
\kbd{NULL}. Note that because of these \kbd{NULL} markers, $v$ is not a valid
8169
\kbd{GEN}, it is not memory clean and cannot be used in garbage collection
8170
routines.
8171
8172
\fun{GEN}{vecsquarefreeu}{ulong a, ulong b} return a \typ{VECSMALL} $v$
8173
containing the squarefree integers in $a,\dots,b$. Assume that
8174
$a\leq b$. Uses a sieve with primes up to $\sqrt{b}$.
8175
8176
\fun{ulong}{tridiv_bound}{GEN n} returns the trial division bound used by
8177
\tet{Z_factor}$(n)$.
8178
8179
\fun{GEN}{Z_pollardbrent}{GEN N, long n, long seed} try to factor
8180
\typ{INT} $N$ using $n\geq 1$ rounds of Pollard iterations; \var{seed} is an
8181
integer whose value (mod $8$) selects the quadratic polynomial use to
8182
generate Pollard's (pseudo)random walk. Returns \kbd{NULL} on failure, else a
8183
vector of 2 (possibly 3) integers whose product is $N$.
8184
8185
\fun{GEN}{Z_ECM}{GEN N, long n, long seed, ulong B1} try to
8186
factor \typ{INT} $N$ using $n\geq 1$ rounds of ECM iterations (on $8$ to $64$
8187
curves simultaneously, depending on the size of $N$); \var{seed} is an
8188
integer whose value selects the curves to be used: increase it by $64n$ to
8189
make sure that a subsequent call with a factor of $N$ uses a disjoint set of
8190
curves.
8191
Finally $B_1 > 7$ determines the computations performed on the
8192
curves: we compute $[k]P$ for some point in $E(\Z/N\Z)$ and $k = q \prod
8193
p^{e_p}$ where $p^{e_p} \leq B_1$ and $q \leq B_2 := 110 B_1$; a higher value
8194
of $B_1$ means higher chances of hitting a factor and more time spent.
8195
The computation is deterministic for a given set of parameters. Returns
8196
\kbd{NULL} on failure, else a nontrivial factor or \kbd{N}.
8197
8198
\fun{GEN}{Q_factor}{GEN x} as \tet{Z_factor}, where $x$ is a \typ{INT} or
8199
a \typ{FRAC}.
8200
8201
\fun{GEN}{Q_factor_limit}{GEN x, ulong lim} as \tet{Z_factor_limit}, where
8202
$x$ is a \typ{INT} or a \typ{FRAC}.
8203
8204
\subsec{Coprime factorization}
8205
8206
Given $a$ and $b$ two nonzero integers, let \teb{ppi}$(a,b)$, \teb{ppo}$(a,b)$,
8207
\teb{ppg}$(a,b)$, \teb{pple}$(a,b)$ (powers in $a$ of primes inside $b$,
8208
outside $b$, greater than those in $b$, less than or equal to those in $b$) be
8209
the integers defined by
8210
8211
\item $v_p(\text{ppi}) = v_p(a) [v_p(b) > 0]$,
8212
8213
\item $v_p(\text{ppo}) = v_p(a) [v_p(b) = 0]$,
8214
8215
\item $v_p(\text{ppg}) = v_p(a) [v_p(a) > v_p(b)]$,
8216
8217
\item $v_p(\text{pple}) = v_p(a) [v_p(a) \leq v_p(b)]$.
8218
8219
\fun{GEN}{Z_ppo}{GEN a, GEN b} returns $\text{ppo}(a,b)$; shallow function.
8220
8221
\fun{ulong}{u_ppo}{ulong a, ulong b} returns $\text{ppo}(a,b)$.
8222
8223
\fun{GEN}{Z_ppgle}{GEN a, GEN b} returns $[\text{ppg}(a,b), \text{pple}(a,b)]$;
8224
shallow function.
8225
8226
\fun{GEN}{Z_ppio}{GEN a, GEN b} returns
8227
$[\gcd(a,b), \text{ppi}(a,b), \text{ppo}(a,b)]$; shallow function.
8228
8229
\fun{GEN}{Z_cba}{GEN a, GEN b} fast natural coprime base algorithm. Returns a
8230
vector of coprime divisors of $a$ and $b$ such that both $a$ and $b$ can
8231
be multiplicatively generated from this set. Perfect powers are not removed,
8232
use \tet{Z_isanypower} if needed; shallow function.
8233
8234
\fun{GEN}{ZV_cba_extend}{GEN P, GEN b} extend a coprime basis $P$ by the
8235
integer $b$, the result being a coprime basis for $P\cup \{b\}$.
8236
Perfect powers are not removed; shallow function.
8237
8238
\fun{GEN}{ZV_cba}{GEN v} given a vector of nonzero integers $v$, return
8239
a coprime basis for $v$. Perfect powers are not removed; shallow function.
8240
8241
\subsec{Checks attached to arithmetic functions}
8242
8243
Arithmetic functions accept arguments of the following kind: a plain positive
8244
integer $N$ (\typ{INT}), the factorization \var{fa} of a positive integer (a
8245
\typ{MAT} with two columns containing respectively primes and exponents), or
8246
a vector $[N,\var{fa}]$. A few functions accept nonzero
8247
integers (e.g.~\tet{omega}), and some others arbitrary integers
8248
(e.g.~\tet{factorint}, \dots).
8249
8250
\fun{int}{is_Z_factorpos}{GEN f} returns $1$ if $f$ looks like the
8251
factorization of a positive integer, and $0$ otherwise. Useful for sanity
8252
checks but not 100\% foolproof. Specifically, this routine checks that $f$ is
8253
a two-column matrix all of whose entries are positive integers. It does
8254
\emph{not} check that entries in the first column (``primes'') are prime,
8255
or even pairwise coprime, nor that they are stricly increasing.
8256
8257
\fun{int}{is_Z_factornon0}{GEN f} returns $1$ if $f$ looks like the
8258
factorization of a nonzero integer, and $0$ otherwise. Useful for sanity
8259
checks but not 100\% foolproof, analogous to \tet{is_Z_factorpos}. (Entries
8260
in the first column need only be nonzero integers.)
8261
8262
\fun{int}{is_Z_factor}{GEN f} returns $1$ if $f$ looks like the
8263
factorization of an integer, and $0$ otherwise. Useful for sanity
8264
checks but not 100\% foolproof. Specifically, this routine checks that $f$ is
8265
a two-column matrix all of whose entries are integers. Entries in the second
8266
column (``exponents'') are all positive. Either it encodes the
8267
``factorization'' $0^e$, $e > 0$, or entries in the first column (``primes'')
8268
are all nonzero.
8269
8270
\fun{GEN}{clean_Z_factor}{GEN f} assuming $f$ is the factorization of an
8271
integer $n$, return the factorization of $|n|$, i.e.~remove $-1$ from the
8272
factorization. Shallow function.
8273
8274
\fun{GEN}{fuse_Z_factor}{GEN f, GEN B} assuming $f$ is the
8275
factorization of an integer $n$, return \kbd{boundfact(n, B)}, i.e.
8276
return a factorization where all primary factors for $|p| \leq B$
8277
are preserved, and all others are ``fused'' into a single composite
8278
integer; if that remainder is trivial, i.e.~equal to 1, it is of course
8279
not included. Shallow function.
8280
8281
In the following three routines, $f$ is the name of an arithmetic function,
8282
and $n$ a supplied argument. They all raise exceptions if $n$ does not
8283
correspond to an integer or an integer factorization of the expected shape.
8284
8285
\fun{GEN}{check_arith_pos}{GEN n, const char *f} check whether $n$
8286
is attached to the factorization of a positive integer, and return
8287
\kbd{NULL} (plain \typ{INT}) or a factorization extracted from $n$ otherwise.
8288
May raise an \tet{e_DOMAIN} ($n \leq 0$) or an \tet{e_TYPE} exception (other
8289
failures).
8290
8291
\fun{GEN}{check_arith_non0}{GEN n, const char *f} check whether $n$
8292
is attached to the factorization of a nonzero integer, and return
8293
\kbd{NULL} (plain \typ{INT}) or a factorization extracted from $n$ otherwise.
8294
May raise an \tet{e_TYPE} exception.
8295
8296
\fun{GEN}{check_arith_all}{GEN n, const char *f}
8297
is attached to the factorization of an integer, and return \kbd{NULL}
8298
(plain \typ{INT}) or a factorization extracted from $n$ otherwise.
8299
8300
\subsec{Incremental integer factorization}
8301
8302
Routines attached to the dynamic factorization of an integer $n$, iterating
8303
over successive prime divisors. This is useful to implement high-level
8304
routines allowed to take shortcuts given enough partial information: e.g.
8305
\kbd{moebius}$(n)$ can be trivially computed if we hit $p$ such that $p^2
8306
\mid n$. For efficiency, trial division by small primes should have already
8307
taken place. In any case, the functions below assume that no prime $< 2^{14}$
8308
divides $n$.
8309
8310
\fun{GEN}{ifac_start}{GEN n, int moebius} schedules a new factorization
8311
attempt for the integer $n$. If \kbd{moebius} is nonzero, the factorization
8312
will be aborted as soon as a repeated factor is detected (Moebius mode).
8313
The function assumes that $n > 1$ is a \emph{composite} \typ{INT} whose prime
8314
divisors satisfy $p > 2^{14}$ \emph{and} that one can write to $n$ in place.
8315
8316
This function stores data on the stack, no \kbd{gerepile} call should
8317
delete this data until the factorization is complete. Returns \kbd{partial},
8318
a data structure recording the partial factorization state.
8319
8320
\fun{int}{ifac_next}{GEN *partial, GEN *p, long *e} deletes a primary factor
8321
$p^e$ from \kbd{partial} and sets \kbd{p} (prime) and \kbd{e} (exponent), and
8322
normally returns $1$. Whatever remains in the \kbd{partial} structure is now
8323
coprime to $p$.
8324
8325
Returns $0$ if all primary factors have been used already, so we are done
8326
with the factorization. In this case $p$ is set to \kbd{NULL}. If we ran in
8327
Moebius mode and the factorization was in fact aborted, we have $e = 1$,
8328
otherwise $e = 0$.
8329
8330
\fun{int}{ifac_read}{GEN part, GEN *k, long *e} peeks at the next integer
8331
to be factored in the list $k^e$, where $k$ is not necessarily prime
8332
and can be a perfect power as well, but will be factored by the next call to
8333
\tet{ifac_next}. You can remove this factorization from the schedule by
8334
calling:
8335
8336
\fun{void}{ifac_skip}{GEN part} removes the next scheduled factorization.
8337
8338
\fun{int}{ifac_isprime}{GEN n} given $n$ whose prime divisors are $> 2^{14}$,
8339
returns the decision the factoring engine would take about the compositeness
8340
of $n$: $0$ if $n$ is a proven composite, and $1$ if we believe it to be
8341
prime; more precisely, $n$ is a proven prime if \tet{factor_proven} is
8342
set, and only a BPSW-pseudoprime otherwise.
8343
8344
\subsec{Integer core, squarefree factorization}
8345
8346
\fun{long}{Z_issquarefree}{GEN n} returns $1$ if the \typ{INT} \kbd{n}
8347
is square-free, and $0$ otherwise.
8348
8349
\fun{long}{Z_isfundamental}{GEN x} returns $1$ if the \typ{INT} \kbd{x}
8350
is a fundamental discriminant, and $0$ otherwise.
8351
8352
\fun{GEN}{core}{GEN n} unique squarefree integer $d$ dividing $n$ such that
8353
$n/d$ is a square. The core of $0$ is defined to be $0$.
8354
8355
\fun{GEN}{core2}{GEN n} return $[d,f]$ with $d$ squarefree and $n = df^2$.
8356
8357
\fun{GEN}{corepartial}{GEN n, long lim} as \kbd{core}, using
8358
\kbd{boundfact(n,lim)} to partially factor \kbd{n}. The result is not
8359
necessarily squarefree, but $p^2 \mid n$ implies $p > \kbd{lim}$.
8360
8361
\fun{GEN}{core2partial}{GEN n, long lim} as \kbd{core2}, using
8362
\kbd{boundfact(n,lim)} to partially factor \kbd{n}. The resulting $d$ is not
8363
necessarily squarefree, but $p^2 \mid n$ implies $p > \kbd{lim}$.
8364
8365
\subsec{Primes, primality and compositeness tests}
8366
8367
\subsubsec{Chebyshev's $\pi$ function, bounds}
8368
8369
\fun{ulong}{uprimepi}{ulong n}, returns the number of primes $p\leq n$
8370
(Chebyshev's $\pi$ function).
8371
8372
\fun{double}{primepi_upper_bound}{double x} return a quick upper bound for
8373
$\pi(x)$, using Dusart bounds.
8374
8375
\fun{GEN}{gprimepi_upper_bound}{GEN x} as \tet{primepi_upper_bound}, returns a
8376
\typ{REAL}.
8377
8378
\fun{double}{primepi_lower_bound}{double x} return a quick lower bound for
8379
$\pi(x)$, using Dusart bounds.
8380
8381
\fun{GEN}{gprimepi_lower_bound}{GEN x} as \tet{primepi_lower_bound}, returns
8382
a \typ{REAL} or \kbd{gen\_0}.
8383
8384
\subsubsec{Primes, primes in intervals}
8385
8386
\fun{ulong}{unextprime}{ulong n}, returns the smallest prime $\geq n$. Return
8387
$0$ if it cannot be represented as an \kbd{ulong} ($n$ bigger than $2^{64} -
8388
59$ or $2^{32} - 5$ depending on the word size).
8389
8390
\fun{ulong}{uprecprime}{ulong n}, returns the largest prime $\leq n$. Return
8391
$0$ if $n\leq 1$.
8392
8393
\fun{ulong}{uprime}{long n} returns the $n$-th prime, assuming it fits in an
8394
\kbd{ulong} (overflow error otherwise).
8395
8396
\fun{GEN}{prime}{long n} same as \kbd{utoi(uprime(n))}.
8397
8398
\fun{GEN}{primes_zv}{long m} returns the first $m$ primes, in a
8399
\typ{VECSMALL}.
8400
8401
\fun{GEN}{primes}{long m} return the first $m$ primes, as a \typ{VEC} of
8402
\typ{INT}s.
8403
8404
\fun{GEN}{primes_interval}{GEN a, GEN b} return the primes in the interval
8405
$[a,b]$, as a \typ{VEC} of \typ{INT}s.
8406
8407
\fun{GEN}{primes_interval_zv}{ulong a, ulong b} return the primes in the
8408
interval $[a,b]$, as a \typ{VECSMALL} of \kbd{ulongs}s.
8409
8410
\fun{GEN}{primes_upto_zv}{ulong b} return the primes in the interval $[2,b]$,
8411
as a \typ{VECSMALL} of \kbd{ulongs}s.
8412
8413
\subsubsec{Tests}
8414
8415
\fun{int}{uisprime}{ulong p}, returns $1$ if \kbd{p} is a prime number and
8416
$0$ otherwise.
8417
8418
\fun{int}{uisprime_101}{ulong p}, assuming that $p$ has no divisor $\leq
8419
101$, returns $1$ if \kbd{p} is a prime number and $0$ otherwise.
8420
8421
\fun{int}{uisprime_661}{ulong p}, assuming that $p$ has no divisor $\leq
8422
661$, returns $1$ if \kbd{p} is a prime number and $0$ otherwise.
8423
8424
\fun{int}{isprime}{GEN n}, returns $1$ if the \typ{INT} \kbd{n} is a
8425
(fully proven) prime number and $0$ otherwise.
8426
8427
\fun{long}{isprimeAPRCL}{GEN n}, returns $1$ if the \typ{INT} \kbd{n} is a
8428
prime number and $0$ otherwise, using only the APRCL test --- not even trial
8429
division or compositeness tests. The workhorse \kbd{isprime} should be
8430
faster on average, especially if nonprimes are included!
8431
8432
\fun{long}{isprimeECPP}{GEN n}, returns $1$ if the \typ{INT} \kbd{n} is a
8433
prime number and $0$ otherwise, using only the ECPP test. The workhorse
8434
\kbd{isprime} should be faster on average.
8435
8436
\fun{long}{BPSW_psp}{GEN n}, returns $1$ if the \typ{INT} \kbd{n} is a
8437
Baillie-Pomerance-Selfridge-Wagstaff pseudoprime, and $0$ otherwise (proven
8438
composite).
8439
8440
\fun{int}{BPSW_isprime}{GEN x} assuming $x$ is a BPSW-pseudoprime, rigorously
8441
prove its primality. The function \tet{isprime} is currently implemented
8442
as
8443
\bprog
8444
BPSW_psp(x) && BPSW_isprime(x)
8445
@eprog
8446
8447
\fun{long}{millerrabin}{GEN n, long k} performs $k$ strong Rabin-Miller
8448
compositeness tests on the \typ{INT} $n$, using $k$ random bases. This
8449
function also caches square roots of $-1$ that are encountered during the
8450
successive tests and stops as soon as three distinct square roots have been
8451
produced; we have in principle factored $n$ at this point, but
8452
unfortunately, there is currently no way for the factoring machinery to
8453
become aware of it. (It is highly implausible that hard to find factors
8454
would be exhibited in this way, though.) This should be slower than
8455
\tet{BPSW_psp} for $k\geq 4$ and we expect it to be less reliable.
8456
8457
\fun{GEN}{ecpp}{GEN N} returns an ECPP certificate for \typ{INT} $N$;
8458
underlies \kbd{primecert}.
8459
8460
\fun{GEN}{ecpp0}{GEN N, long t} returns a (potentially)
8461
partial ECPP certificate for \typ{INT} $N$ where strong pseudo-primes $< 2^t$
8462
are included as primes in the certificate. Underlies \kbd{primecert} with
8463
$t$ set to the \kbd{partial} argument.
8464
8465
\fun{GEN}{ecppexport}{GEN cert, long flag} export a PARI ECPP certificate to
8466
MAGMA or Primo format; underlies \kbd{primecertexport}.
8467
8468
\fun{long}{ecppisvalid}{GEN cert} checks whether a PARI ECPP certificate
8469
is valid; underlies \kbd{primecertisvalid}.
8470
8471
\fun{long}{check_ecppcert}{GEN cert} checks whether \kbd{cert} looks
8472
like a PARI ECPP certificate, (valid or invalid) without doing any computation.
8473
8474
\subsec{Iterators over primes}
8475
8476
\fun{int}{forprime_init}{forprime_t *T, GEN a, GEN b} initialize an
8477
iterator $T$ over primes in $[a,b]$; over primes $\geq a$ if $b =
8478
\kbd{NULL}$. Return $0$ if the range is known to be empty from the start
8479
(as if $b < a$ or $b < 0$), and return $1$ otherwise. Use \tet{forprime_next}
8480
to iterate over the prime collection.
8481
8482
\fun{int}{forprimestep_init}{forprime_t *T, GEN a, GEN b, GEN q} initialize an
8483
iterator $T$ over primes in an arithmetic progression in $[a,b]$;
8484
over primes $\geq a$ if $b = \kbd{NULL}$. The argument $q$ is either a
8485
\typ{INT} ($p \equiv a \pmod{q}$) or a \typ{INTMOD} \kbd{Mod(c,N)}
8486
and we restrict to that congruence class. Return $0$ if the range is known to
8487
be empty from the start (as if $b < a$ or $b < 0$), and return $1$ otherwise.
8488
Use \tet{forprime_next} to iterate over the prime collection.
8489
8490
\fun{GEN}{forprime_next}{forprime_t *T} returns the next prime in the range,
8491
assuming that $T$ was initialized by \tet{forprime_init}.
8492
8493
\fun{int}{u_forprime_init}{forprime_t *T, ulong a, ulong b}
8494
8495
\fun{ulong}{u_forprime_next}{forprime_t *T}
8496
8497
\fun{void}{u_forprime_restrict}{forprime_t *T, ulong c} let $T$ an iterator
8498
over primes initialized via \kbd{u\_forprime\_init(\&T, a, b)}, possibly
8499
followed by a number of calls to \tet{u_forprime_next}, and $a \leq c \leq
8500
b$. Restrict the range of primes considered to $[a,c]$.
8501
8502
\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$
8503
modulo $q$. Subsequent calls to \tet{u_forprime_next} will only return primes
8504
congruent to $c$ modulo $q$. Note that unless $(c,q) = 1$ there will be at
8505
most one such prime.
8506
8507
\section{Integral, rational and generic linear algebra}
8508
\subsec{\kbd{ZC} / \kbd{ZV}, \kbd{ZM}} A \kbd{ZV} (resp.~a~\kbd{ZM},
8509
resp.~a~\kbd{ZX}) is a \typ{VEC} or \typ{COL} (resp.~\typ{MAT},
8510
resp.~\typ{POL}) with \typ{INT} coefficients.
8511
8512
\subsubsec{\kbd{ZC} / \kbd{ZV}}
8513
8514
\fun{void}{RgV_check_ZV}{GEN x, const char *s} Assuming \kbd{x} is a \typ{VEC}
8515
or \typ{COL} raise an error if it is not a \kbd{ZV} ($s$ should point to the
8516
name of the caller).
8517
8518
\fun{int}{RgV_is_ZV}{GEN x} Assuming \kbd{x} is a \typ{VEC}
8519
or \typ{COL} return $1$ if it is a \kbd{ZV}, and $0$ otherwise.
8520
8521
\fun{int}{RgV_is_ZVpos}{GEN x} Assuming \kbd{x} is a \typ{VEC}
8522
or \typ{COL} return $1$ if it is a \kbd{ZV} with positive entries, and $0$
8523
otherwise.
8524
8525
\fun{int}{RgV_is_ZVnon0}{GEN x} Assuming \kbd{x} is a \typ{VEC}
8526
or \typ{COL} return $1$ if it is a \kbd{ZV} with nonzero entries, and $0$
8527
otherwise.
8528
8529
\fun{int}{RgV_is_QV}{GEN P} return 1 if the \kbd{RgV}~$P$ has only
8530
\typ{INT} and \typ{FRAC} coefficients, and 0 otherwise.
8531
8532
\fun{int}{RgV_is_arithprog}{GEN v, GEN *a, GEN *b} assuming $x$ is a \typ{VEC}
8533
or \typ{COL} return $1$ if its entries follow an arithmetic progression
8534
of the form $a + b*n$, $n = 0, 1, \dots$ and set $a$ and $b$. Else return $0$.
8535
8536
\fun{int}{ZV_equal0}{GEN x} returns 1 if all entries of the \kbd{ZV} $x$ are
8537
zero, and $0$ otherwise.
8538
8539
\fun{int}{ZV_cmp}{GEN x, GEN y} compare two \kbd{ZV}, which we assume have
8540
the same length (lexicographic order, comparing absolute values).
8541
8542
\fun{int}{ZV_abscmp}{GEN x, GEN y} compare two \kbd{ZV}, which we assume have
8543
the same length (lexicographic order).
8544
8545
\fun{int}{ZV_equal}{GEN x, GEN y} returns $1$ if the two \kbd{ZV} are equal
8546
and $0$ otherwise. A \typ{COL} and a \typ{VEC} with the same entries are
8547
declared equal.
8548
8549
\fun{GEN}{identity_ZV}{long n} return the \typ{VEC} $[1, 2, \dots, n]$.
8550
8551
\fun{GEN}{ZC_add}{GEN x, GEN y} adds \kbd{x} and \kbd{y}.
8552
8553
\fun{GEN}{ZC_sub}{GEN x, GEN y} subtracts \kbd{x} and \kbd{y}.
8554
8555
\fun{GEN}{ZC_Z_add}{GEN x, GEN y} adds \kbd{y} to \kbd{x[1]}.
8556
8557
\fun{GEN}{ZC_Z_sub}{GEN x, GEN y} subtracts \kbd{y} to \kbd{x[1]}.
8558
8559
\fun{GEN}{Z_ZC_sub}{GEN a, GEN x} returns the vector $[a - x_1,
8560
-x_2,\dots,-x_n]$.
8561
8562
\fun{GEN}{ZC_copy}{GEN x} returns a (\typ{COL}) copy of \kbd{x}.
8563
8564
\fun{GEN}{ZC_neg}{GEN x} returns $-\kbd{x}$ as a \typ{COL}.
8565
8566
\fun{void}{ZV_neg_inplace}{GEN x} negates the \kbd{ZV} \kbd{x} in place, by
8567
replacing each component by its opposite (the type of \kbd{x} remains the
8568
same, \typ{COL} or \typ{COL}). If you want to save even more memory by
8569
avoiding the implicit component copies, use \kbd{ZV\_togglesign}.
8570
8571
\fun{void}{ZV_togglesign}{GEN x} negates \kbd{x} in place, by toggling the
8572
sign of its integer components. Universal constants \kbd{gen\_1},
8573
\kbd{gen\_m1}, \kbd{gen\_2} and \kbd{gen\_m2} are handled specially and will
8574
not be corrupted. (We use \tet{togglesign_safe}.)
8575
8576
\fun{GEN}{ZC_Z_mul}{GEN x, GEN y} multiplies the \kbd{ZC} or \kbd{ZV}~\kbd{x}
8577
(which can be a column or row vector) by the \typ{INT}~\kbd{y}, returning a
8578
\kbd{ZC}.
8579
8580
\fun{GEN}{ZC_Z_divexact}{GEN x, GEN y} returns $x/y$ assuming all divisions
8581
are exact.
8582
8583
\fun{GEN}{ZC_divexactu}{GEN x, ulong y} returns $x/y$ assuming all divisions
8584
are exact.
8585
8586
\fun{GEN}{ZC_Z_div}{GEN x, GEN y} returns $x/y$, where the resulting vector
8587
has rational entries.
8588
8589
\fun{GEN}{ZV_dotproduct}{GEN x,GEN y} as \kbd{RgV\_dotproduct} assuming $x$
8590
and $y$ have \typ{INT} entries.
8591
8592
\fun{GEN}{ZV_dotsquare}{GEN x} as \kbd{RgV\_dotsquare} assuming $x$
8593
has \typ{INT} entries.
8594
8595
\fun{GEN}{ZC_lincomb}{GEN u, GEN v, GEN x, GEN y} returns $ux + vy$, where
8596
$u$, $v$ are \typ{INT} and $x,y$ are \kbd{ZC} or \kbd{ZV}. Return a \kbd{ZC}
8597
8598
\fun{void}{ZC_lincomb1_inplace}{GEN X, GEN Y, GEN v} sets $X\leftarrow X +
8599
vY$, where $v$ is a \typ{INT} and $X,Y$ are \kbd{ZC} or \kbd{ZV}. (The result
8600
has the type of $X$.) Memory efficient (e.g. no-op if $v = 0$), but not
8601
gerepile-safe.
8602
8603
\fun{void}{ZC_lincomb1_inplace_i}{GEN X, GEN Y, GEN v, long n}
8604
variant of \tet{ZC_lincomb1_inplace}: only update $X[1], \dots, X[n]$,
8605
assuming that $n < \kbd{lg}(X)$.
8606
8607
\fun{GEN}{ZC_ZV_mul}{GEN x, GEN y, GEN p} multiplies the \kbd{ZC}~\kbd{x}
8608
(seen as a column vector) by the \kbd{ZV}~\kbd{y} (seen as a row vector,
8609
assumed to have compatible dimensions).
8610
8611
\fun{GEN}{ZV_content}{GEN x} returns the GCD of all the components
8612
of~\kbd{x}.
8613
8614
\fun{GEN}{ZV_extgcd}{GEN A} given a vector of $n$ integers $A$, returns $[d,
8615
U]$, where $d$ is the content of $A$ and $U$ is a matrix
8616
in $\text{GL}_n(\Z)$ such that $AU = [D,0, \dots,0]$.
8617
8618
\fun{GEN}{ZV_prod}{GEN x} returns the product of all the components
8619
of~\kbd{x} ($1$ for the empty vector).
8620
8621
\fun{GEN}{ZV_sum}{GEN x} returns the sum of all the components
8622
of~\kbd{x} ($0$ for the empty vector).
8623
8624
\fun{long}{ZV_max_lg}{GEN x} returns the effective length of the longest
8625
entry in $x$.
8626
8627
\fun{int}{ZV_dvd}{GEN x, GEN y} assuming $x$, $y$ are two \kbd{ZV}s of the same
8628
length, return $1$ if $y[i]$ divides $x[i]$ for all $i$ and $0$ otherwise.
8629
Error if one of the $y[i]$ is $0$.
8630
8631
\fun{GEN}{ZV_sort}{GEN L} sort the \kbd{ZV} $L$.
8632
Returns a vector with the same type as $L$.
8633
8634
\fun{void}{ZV_sort_inplace}{GEN L} sort the \kbd{ZV} $L$, in place.
8635
8636
\fun{GEN}{ZV_sort_uniq}{GEN L} sort the \kbd{ZV} $L$, removing duplicate
8637
entries. Returns a vector with the same type as $L$.
8638
8639
\fun{long}{ZV_search}{GEN L, GEN y} look for the \typ{INT} $y$ in the sorted
8640
\kbd{ZV} $L$. Return an index $i$ such that $L[i] = y$, and $0$ otherwise.
8641
8642
\fun{GEN}{ZV_indexsort}{GEN L} returns the permutation which, applied to the
8643
\kbd{ZV} $L$, would sort the vector. The result is a \typ{VECSMALL}.
8644
8645
\fun{GEN}{ZV_union_shallow}{GEN x, GEN y} given two \emph{sorted} ZV (as per
8646
\tet{ZV_sort}, returns the union of $x$ and $y$. Shallow function. In case two
8647
entries are equal in $x$ and $y$, include the one from $x$.
8648
8649
\fun{GEN}{ZC_union_shallow}{GEN x, GEN y} as \kbd{ZV\_union\_shallow} but return
8650
a \typ{COL}.
8651
8652
\subsubsec{\kbd{ZM}}
8653
8654
\fun{void}{RgM_check_ZM}{GEN A, const char *s} Assuming \kbd{x} is a \typ{MAT}
8655
raise an error if it is not a \kbd{ZM} ($s$ should point to the name of the
8656
caller).
8657
8658
\fun{GEN}{RgM_rescale_to_int}{GEN x} given a matrix $x$ with real entries
8659
(\typ{INT}, \typ{FRAC} or \typ{REAL}), return a \kbd{ZM} wich is very close
8660
to $D x$ for some well-chosen integer $D$. More precisely, if the input is
8661
exact, $D$ is the denominator of $x$; else it is a power of $2$ chosen
8662
so that all inexact entries are correctly rounded to 1 ulp.
8663
8664
\fun{GEN}{ZM_copy}{GEN x} returns a copy of \kbd{x}.
8665
8666
\fun{int}{ZM_equal}{GEN A, GEN B} returns $1$ if the two \kbd{ZM} are equal
8667
and $0$ otherwise.
8668
8669
\fun{int}{ZM_equal0}{GEN A} returns $1$ if the \kbd{ZM} $A$ is identically
8670
equal to $0$.
8671
8672
\fun{GEN}{ZM_add}{GEN x, GEN y} returns $\kbd{x} + \kbd{y}$ (assumed to have
8673
compatible dimensions).
8674
8675
\fun{GEN}{ZM_sub}{GEN x, GEN y} returns $\kbd{x} - \kbd{y}$ (assumed to have
8676
compatible dimensions).
8677
8678
\fun{GEN}{ZM_neg}{GEN x} returns $-\kbd{x}$.
8679
8680
\fun{void}{ZM_togglesign}{GEN x} negates \kbd{x} in place, by toggling the
8681
sign of its integer components. Universal constants \kbd{gen\_1},
8682
\kbd{gen\_m1}, \kbd{gen\_2} and \kbd{gen\_m2} are handled specially and will
8683
not be corrupted. (We use \tet{togglesign_safe}.)
8684
8685
\fun{GEN}{ZM_mul}{GEN x, GEN y} multiplies \kbd{x} and \kbd{y} (assumed to
8686
have compatible dimensions).
8687
8688
\fun{GEN}{ZM2_mul}{GEN x, GEN y} multiplies the two-by-two \kbd{ZM} $x$ and $y$.
8689
8690
\fun{GEN}{ZM_sqr}{GEN x} returns $x^2$, where $x$ is a square \kbd{ZM}.
8691
8692
\fun{GEN}{ZM_Z_mul}{GEN x, GEN y} multiplies the \kbd{ZM}~\kbd{x}
8693
by the \typ{INT}~\kbd{y}.
8694
8695
\fun{GEN}{ZM_ZC_mul}{GEN x, GEN y} multiplies the \kbd{ZM}~\kbd{x}
8696
by the \kbd{ZC}~\kbd{y} (seen as a column vector, assumed to have compatible
8697
dimensions).
8698
8699
\fun{GEN}{ZM_ZX_mul}{GEN x, GEN T} returns $x \times y$, where $y$
8700
is \kbd{RgX\_to\_RgC}$(T, \kbd{lg}(x)-1)$.
8701
8702
\fun{GEN}{ZM_diag_mul}{GEN d, GEN m} given a vector $d$ with integer entries
8703
and a \kbd{ZM} $m$ of compatible dimensions, return \kbd{diagonal(d) * m}.
8704
8705
\fun{GEN}{ZM_mul_diag}{GEN m, GEN d} given a vector $d$ with integer entries
8706
and a \kbd{ZM} $m$ of compatible dimensions, return \kbd{m * diagonal(d)}.
8707
8708
\fun{GEN}{ZM_multosym}{GEN x, GEN y}
8709
8710
\fun{GEN}{ZM_transmultosym}{GEN x, GEN y}
8711
8712
\fun{GEN}{ZM_transmul}{GEN x, GEN y}
8713
8714
\fun{GEN}{ZMrow_ZC_mul}{GEN x, GEN y, long i} multiplies the $i$-th row
8715
of \kbd{ZM}~\kbd{x} by the \kbd{ZC}~\kbd{y} (seen as a column vector, assumed
8716
to have compatible dimensions). Assumes that $x$ is nonempty and
8717
$0 < i < \kbd{lg(x[1])}$.
8718
8719
\fun{int}{ZMrow_equal0}{GEN V, long i} returns $1$ if the $i$-th row of
8720
the \kbd{ZM}~\kbd{V} is zero, and $0$ otherwise.
8721
8722
\fun{GEN}{ZV_ZM_mul}{GEN x, GEN y} multiplies the \kbd{ZV}~\kbd{x}
8723
by the \kbd{ZM}~\kbd{y}. Returns a \typ{VEC}.
8724
8725
\fun{GEN}{ZM_Z_divexact}{GEN x, GEN y} returns $x/y$ assuming all divisions
8726
are exact.
8727
8728
\fun{GEN}{ZM_divexactu}{GEN x, ulong y} returns $x/y$ assuming all divisions
8729
are exact.
8730
8731
\fun{GEN}{ZM_Z_div}{GEN x, GEN y} returns $x/y$, where the resulting matrix
8732
has rational entries.
8733
8734
\fun{GEN}{ZC_Q_mul}{GEN x, GEN y} returns $x*y$, where $y$ is a rational number
8735
and the resulting \typ{COL} has rational entries.
8736
8737
\fun{GEN}{ZM_Q_mul}{GEN x, GEN y} returns $x*y$, where $y$ is a rational number
8738
and the resulting matrix has rational entries.
8739
8740
\fun{GEN}{ZM_pow}{GEN x, GEN n} returns $\kbd{x}^\kbd{n}$, assuming \kbd{x}
8741
is a square \kbd{ZM} and $\kbd{n}\geq 0$.
8742
8743
\fun{GEN}{ZM_powu}{GEN x, ulong n} returns $\kbd{x}^\kbd{n}$, assuming \kbd{x}
8744
is a square \kbd{ZM} and $\kbd{n}\geq 0$.
8745
8746
\fun{GEN}{ZM_det}{GEN M} if \kbd{M} is a \kbd{ZM}, returns the determinant of
8747
$M$. This is the function underlying \tet{matdet} whenever $M$ is a \kbd{ZM}.
8748
8749
\fun{GEN}{ZM_permanent}{GEN M} if \kbd{M} is a \kbd{ZM}, returns its
8750
permanent. This is the function underlying \tet{matpermanent} whenever $M$
8751
is a \kbd{ZM}. It assumes that the matrix is square of dimension $<
8752
\kbd{BITS\_IN\_LONG}$.
8753
8754
\fun{GEN}{ZM_detmult}{GEN M} if \kbd{M} is a \kbd{ZM}, returns a multiple of
8755
the determinant of the lattice generated by its columns. This is the function
8756
underlying \tet{detint}.
8757
8758
\fun{GEN}{ZM_supnorm}{GEN x} return the sup norm of the \kbd{ZM} $x$.
8759
8760
\fun{GEN}{ZM_charpoly}{GEN M} returns the characteristic polynomial (in
8761
variable $0$) of the \kbd{ZM} $M$.
8762
8763
\fun{GEN}{ZM_imagecompl}{GEN x} returns \kbd{matimagecompl(x)}.
8764
8765
\fun{long}{ZM_rank}{GEN x} returns \kbd{matrank(x)}.
8766
8767
\fun{GEN}{ZM_ker}{GEN x} returns the primitive part of \kbd{matker(x)}; in
8768
other words the $\Q$-basis vectors are made integral and primitive.
8769
8770
\fun{GEN}{ZM_indexrank}{GEN x} returns \kbd{matindexrank(x)}.
8771
8772
\fun{GEN}{ZM_indeximage}{GEN x} returns \kbd{gel(ZM\_indexrank(x), 2)}.
8773
8774
\fun{long}{ZM_max_lg}{GEN x} returns the effective length of the longest
8775
entry in $x$.
8776
8777
\fun{GEN}{ZM_inv}{GEN M, GEN *pd} if \kbd{M} is a \kbd{ZM}, return
8778
a primitive matrix $H$ such that $M H$ is $d$ times the identity
8779
and set \kbd{*pd} to $d$. Uses a multimodular algorithm up to Hadamard's bound.
8780
If you suspect that the denominator is much smaller than $\det M$, you may
8781
use \tet{ZM_inv_ratlift}.
8782
8783
\fun{GEN}{ZM_inv_ratlift}{GEN M, GEN *pd} if \kbd{M} is a \kbd{ZM},
8784
return a primitive matrix $H$ such that $M H$ is $d$ times the identity
8785
and set \kbd{*pd} to $d$. Uses a multimodular algorithm, attempting
8786
rational reconstruction along the way. To be used when you expect that the
8787
denominator of $M^{-1}$ is much smaller than $\det M$ else use \kbd{ZM\_inv}.
8788
8789
\fun{GEN}{SL2_inv_shallow}{GEN M} return the inverse of $M \in
8790
\text{SL}_2(\Z)$. Not gerepile-safe.
8791
8792
\fun{GEN}{ZM_pseudoinv}{GEN M, GEN *pv, GEN *pd} if \kbd{M} is a nonempty
8793
\kbd{ZM}, let $v = [y,z]$ returned by \kbd{indexrank} and
8794
let $M_1$ be the corresponding square invertible matrix.
8795
Return a primitive left-inverse $H$ such that $H M_1$ is
8796
$d$ times the identity and set \kbd{*pd} to $d$. If \kbd{pv} is not
8797
\kbd{NULL}, set \kbd{*pv} to $v$. Not gerepile-safe.
8798
8799
\fun{GEN}{ZM_gauss}{GEN a, GEN b} as \kbd{gauss}, where $a$ and $b$
8800
coefficients are \typ{INT}s.
8801
8802
\fun{GEN}{ZM_det_triangular}{GEN x} returns the product of the diagonal
8803
entries of $x$ (its determinant if it is indeed triangular).
8804
8805
\fun{int}{ZM_isidentity}{GEN x} return 1 if the \kbd{ZM} $x$ is the
8806
identity matrix, and 0 otherwise.
8807
8808
\fun{int}{ZM_isdiagonal}{GEN x} return 1 if the \kbd{ZM} $x$ is diagonal,
8809
and 0 otherwise.
8810
8811
\fun{int}{ZM_isscalar}{GEN x, GEN s} given a \kbd{ZM} $x$ and a
8812
\typ{INT} $s$, return 1 if $x$ is equal to $s$ times the identity, and 0
8813
otherwise. If $s$ is \kbd{NULL}, test whether $x$ is an arbitrary scalar
8814
matrix.
8815
8816
\fun{long}{ZC_is_ei}{GEN x} return $i$ if the \kbd{ZC} $x$ has $0$ entries,
8817
but for a $1$ at position $i$.
8818
8819
\fun{int}{ZM_ishnf}{GEN x} return $1$ if $x$ is in HNF form, i.e. is upper
8820
triangular with positive diagonal coefficients, and for $j>i$,
8821
$x_{i,i}>x_{i,j} \ge 0$.
8822
8823
\subsec{\kbd{QM}}
8824
8825
\fun{GEN}{QM_charpoly_ZX}{GEN M} returns the characteristic polynomial
8826
(in variable $0$) of the \kbd{QM} $M$, assuming that the result has integer
8827
coefficients.
8828
8829
\fun{GEN}{QM_charpoly_ZX_bound}{GEN M, long b} as \tet{QM_charpoly_ZX}
8830
assuming that the sup norm of the (integral) result is $\leq 2^b$.
8831
8832
\fun{GEN}{QM_gauss}{GEN a, GEN b} as \kbd{gauss}, where $a$ and $b$
8833
coefficients are \typ{FRAC}s.
8834
8835
\fun{GEN}{QM_gauss_i}{GEN a, GEN b, long flag} as \kbd{QM\_gauss} if
8836
\kbd{flag} is $0$. Else, no longer assume that $a$ is left-invertible and
8837
return a solution of $P a x = P b$ where $P$ is a row-selection matrix
8838
such that $A = P a Q$ is square invertible of maximal rank, for some
8839
column-selection matrix $Q$; in particular, $x$ is a solution of
8840
the original equation $a x = b$ if and only if a solution exists.
8841
8842
\fun{GEN}{QM_indexrank}{GEN x} returns \kbd{matindexrank(x)}.
8843
8844
\fun{GEN}{QM_inv}{GEN M} return the inverse of the \kbd{QM} $M$.
8845
8846
\fun{long}{QM_rank}{GEN x} returns \kbd{matrank(x)}.
8847
8848
\fun{GEN}{QM_image}{GEN x} returns an integral matrix with primitive columns
8849
generating the image of $x$.
8850
8851
\fun{GEN}{QM_image_shallow}{GEN A} shallow version of the previous function,
8852
not suitable for \kbd{gerepile}.
8853
8854
\subsec{\kbd{Qevproj}}
8855
8856
\fun{GEN}{Qevproj_init}{GEN M} let $M$ be a $n\times d$ \kbd{ZM} of
8857
maximal rank $d \leq n$, representing the basis of a $\Q$-subspace
8858
$V$ of $\Q^n$. Return a projector on $V$, to be used by \tet{Qevproj_apply}.
8859
The interface details may change in the future, but this function currently
8860
returns $[M, B,D,p]$, where $p$ is a \typ{VECSMALL} with $d$ entries
8861
such that the submatrix $A = \kbd{rowpermute}(M,p)$ is invertible, $B$ is a
8862
\kbd{ZM} and $d$ a \typ{INT} such that $A B = D \Id_d$.
8863
8864
\fun{GEN}{Qevproj_apply}{GEN T, GEN pro} let $T$ be an $n\times n$
8865
\kbd{QM}, stabilizing a $\Q$-subspace $V\subset \Q^n$ of dimension $d$, and
8866
let \kbd{pro} be a projector on that subspace initialized by
8867
\tet{Qevproj_init}$(M)$. Return the $d\times d$ matrix representing $T_{|V}$
8868
on the basis given by the columns of $M$.
8869
8870
\fun{GEN}{Qevproj_apply_vecei}{GEN T, GEN pro, long k} as
8871
\tet{Qevproj_apply}, return only the image of the $k$-th basis vector $M[k]$
8872
(still on the basis given by the columns of $M$).
8873
8874
\fun{GEN}{Qevproj_down}{GEN T, GEN pro} given a \kbd{ZC} (resp.~a \kbd{ZM})
8875
$T$ representing an element (resp.~a vector of elements) in the subspace $V$
8876
return a \kbd{QC} (resp.~a \kbd{QM}) $U$ such that $T = MU$.
8877
8878
\subsec{\kbd{zv}, \kbd{zm}}
8879
8880
\fun{GEN}{identity_zv}{long n} return the \typ{VECSMALL} $[1, 2, \dots, n]$.
8881
8882
\fun{GEN}{random_zv}{long n} returns a random \kbd{zv} with $n$ components.
8883
8884
\fun{GEN}{zv_abs}{GEN x} return $[|x[1]|,\ldots,|x[n]|]$ as a \kbd{zv}.
8885
8886
\fun{GEN}{zv_neg}{GEN x} return $-x$. No check for overflow is done, which
8887
occurs in the fringe case where an entry is equal to $2^{\B-1}$.
8888
8889
\fun{GEN}{zv_neg_inplace}{GEN x} negates $x$ in place and return it. No check
8890
for overflow is done, which occurs in the fringe case where an entry is equal
8891
to $2^{\B-1}$.
8892
8893
\fun{GEN}{zm_zc_mul}{GEN x, GEN y}
8894
8895
\fun{GEN}{zm_mul}{GEN x, GEN y}
8896
8897
\fun{GEN}{zv_z_mul}{GEN x, long n} return $n\*x$. No check for overflow is
8898
done.
8899
8900
\fun{long}{zv_content}{GEN x} returns the gcd of the entries of $x$.
8901
8902
\fun{long}{zv_dotproduct}{GEN x, GEN y}
8903
8904
\fun{long}{zv_prod}{GEN x} returns the product of all the components
8905
of~\kbd{x} (assumes no overflow occurs).
8906
8907
\fun{GEN}{zv_prod_Z}{GEN x} returns the product of all the components
8908
of~\kbd{x}; consider all $x[i]$ as \kbd{ulong}s.
8909
8910
\fun{long}{zv_sum}{GEN x} returns the sum of all the components
8911
of~\kbd{x} (assumes no overflow occurs).
8912
8913
\fun{long}{zv_sumpart}{GEN v, long n} returns the sum $v[1] + \dots + v[n]$
8914
(assumes no overflow occurs and \kbd{lg}$(v) > n$).
8915
8916
\fun{int}{zv_cmp0}{GEN x} returns 1 if all entries of the \kbd{zv} $x$ are $0$,
8917
and $0$ otherwise.
8918
8919
\fun{int}{zv_equal}{GEN x, GEN y} returns $1$ if the two \kbd{zv} are equal
8920
and $0$ otherwise.
8921
8922
\fun{int}{zv_equal0}{GEN x} returns $1$ if all entries are $0$, and return
8923
$0$ otherwise.
8924
8925
\fun{long}{zv_search}{GEN L, long y} look for $y$ in the sorted
8926
\kbd{zv} $L$. Return an index $i$ such that $L[i] = y$, and $0$ otherwise.
8927
8928
\fun{GEN}{zv_copy}{GEN x} as \kbd{Flv\_copy}.
8929
8930
\fun{GEN}{zm_transpose}{GEN x} as \kbd{Flm\_transpose}.
8931
8932
\fun{GEN}{zm_copy}{GEN x} as \kbd{Flm\_copy}.
8933
8934
\fun{GEN}{zero_zm}{long m, long n} as \kbd{zero\_Flm}.
8935
8936
\fun{GEN}{zero_zv}{long n} as \kbd{zero\_Flv}.
8937
8938
\fun{GEN}{zm_row}{GEN A, long x0} as \kbd{Flm\_row}.
8939
8940
\fun{GEN}{zv_diagonal}{GEN v} return the square \kbd{zm} whose diagonal
8941
is given by the entries of $v$.
8942
8943
\fun{GEN}{zm_permanent}{GEN M} return the permanent of $M$.
8944
The function assumes that the matrix is square of dimension
8945
$< \kbd{BITS\_IN\_LONG}$.
8946
8947
\fun{int}{zvV_equal}{GEN x, GEN y} returns $1$ if the two \kbd{zvV} (vectors
8948
of \kbd{zv}) are equal and $0$ otherwise.
8949
8950
\subsec{\kbd{ZMV} / \kbd{zmV} (vectors of \kbd{ZM}/\kbd{zm})}
8951
8952
\fun{int}{RgV_is_ZMV}{GEN x} Assuming \kbd{x} is a \typ{VEC}
8953
or \typ{COL} return $1$ if its components are \kbd{ZM}, and $0$ otherwise.
8954
8955
\fun{GEN}{ZMV_to_zmV}{GEN z}
8956
8957
\fun{GEN}{zmV_to_ZMV}{GEN z}
8958
8959
\fun{GEN}{ZMV_to_FlmV}{GEN z, ulong m}
8960
8961
\subsec{\kbd{QC} / \kbd{QV}, \kbd{QM}}
8962
8963
\fun{GEN}{QM_mul}{GEN x, GEN y} multiplies \kbd{x} and \kbd{y} (assumed to
8964
have compatible dimensions).
8965
8966
\fun{GEN}{QM_sqr}{GEN x} returns the square of \kbd{x} (assumed to be square).
8967
8968
\fun{GEN}{QM_QC_mul}{GEN x, GEN y} multiplies \kbd{x} and \kbd{y} (assumed to
8969
have compatible dimensions).
8970
8971
\fun{GEN}{QM_det}{GEN M} returns the determinant of $M$.
8972
8973
\fun{GEN}{QM_ker}{GEN x} returns \kbd{matker(x)}.
8974
8975
\subsec{\kbd{RgC} / \kbd{RgV}, \kbd{RgM}}
8976
8977
\kbd{RgC} and \kbd{RgV} routines assume the inputs are \kbd{VEC} or \kbd{COL}
8978
of the same dimension. \kbd{RgM} assume the inputs are \kbd{MAT} of
8979
compatible dimensions.
8980
8981
\subsubsec{Matrix arithmetic}
8982
8983
\fun{void}{RgM_dimensions}{GEN x, long *m, long *n} sets $m$, resp.~$n$, to
8984
the number of rows, resp.~columns of the \typ{MAT} $x$.
8985
8986
\fun{GEN}{RgC_add}{GEN x, GEN y} returns $x + y$ as a \typ{COL}.
8987
8988
\fun{GEN}{RgC_neg}{GEN x} returns $-x$ as a \typ{COL}.
8989
8990
\fun{GEN}{RgC_sub}{GEN x, GEN y} returns $x - y$ as a \typ{COL}.
8991
8992
\fun{GEN}{RgV_add}{GEN x, GEN y} returns $x + y$ as a \typ{VEC}.
8993
8994
\fun{GEN}{RgV_neg}{GEN x} returns $-x$ as a \typ{VEC}.
8995
8996
\fun{GEN}{RgV_sub}{GEN x, GEN y} returns $x - y$ as a \typ{VEC}.
8997
8998
\fun{GEN}{RgM_add}{GEN x, GEN y} return $x+y$.
8999
9000
\fun{GEN}{RgM_neg}{GEN x} returns $-x$.
9001
9002
\fun{GEN}{RgM_sub}{GEN x, GEN y} returns $x-y$.
9003
9004
\fun{GEN}{RgM_Rg_add}{GEN x, GEN y} assuming $x$ is a square matrix
9005
and $y$ a scalar, returns the square matrix $x + y*\text{Id}$.
9006
9007
\fun{GEN}{RgM_Rg_add_shallow}{GEN x, GEN y} as \kbd{RgM\_Rg\_add} with much
9008
fewer copies. Not suitable for \kbd{gerepileupto}.
9009
9010
\fun{GEN}{RgM_Rg_sub}{GEN x, GEN y} assuming $x$ is a square matrix
9011
and $y$ a scalar, returns the square matrix $x - y*\text{Id}$.
9012
9013
\fun{GEN}{RgM_Rg_sub_shallow}{GEN x, GEN y} as \kbd{RgM\_Rg\_sub} with much
9014
fewer copies. Not suitable for \kbd{gerepileupto}.
9015
9016
\fun{GEN}{RgC_Rg_add}{GEN x, GEN y} assuming $x$ is a nonempty column vector
9017
and $y$ a scalar, returns the vector $[x_1 + y, x_2,\dots,x_n]$.
9018
9019
\fun{GEN}{RgC_Rg_sub}{GEN x, GEN y} assuming $x$ is a nonempty column vector
9020
and $y$ a scalar, returns the vector $[x_1 - y, x_2,\dots,x_n]$.
9021
9022
\fun{GEN}{Rg_RgC_sub}{GEN a, GEN x} assuming $x$ is a nonempty column vector
9023
and $a$ a scalar, returns the vector $[a - x_1, -x_2,\dots,-x_n]$.
9024
9025
\fun{GEN}{RgC_Rg_div}{GEN x, GEN y}
9026
9027
\fun{GEN}{RgM_Rg_div}{GEN x, GEN y} returns $x/y$ ($y$ treated as a scalar).
9028
9029
\fun{GEN}{RgC_Rg_mul}{GEN x, GEN y}
9030
9031
\fun{GEN}{RgV_Rg_mul}{GEN x, GEN y}
9032
9033
\fun{GEN}{RgM_Rg_mul}{GEN x, GEN y} returns $x\times y$ ($y$ treated as a
9034
scalar).
9035
9036
\fun{GEN}{RgV_RgC_mul}{GEN x, GEN y} returns $x\times y$.
9037
9038
\fun{GEN}{RgV_RgM_mul}{GEN x, GEN y} returns $x\times y$.
9039
9040
\fun{GEN}{RgM_RgC_mul}{GEN x, GEN y} returns $x\times y$.
9041
9042
\fun{GEN}{RgM_RgX_mul}{GEN x, GEN T} returns $x \times y$, where $y$
9043
is \kbd{RgX\_to\_RgC}$(T, \kbd{lg}(x)-1)$.
9044
9045
\fun{GEN}{RgM_mul}{GEN x, GEN y} returns $x\times y$.
9046
9047
\fun{GEN}{RgM_ZM_mul}{GEN x, GEN y} returns $x\times y$ assuming that $y$ is
9048
a \kbd{ZM}.
9049
9050
\fun{GEN}{RgM_transmul}{GEN x, GEN y} returns $x\til \times y$.
9051
9052
\fun{GEN}{RgM_multosym}{GEN x, GEN y} returns $x\times y$, assuming
9053
the result is a symmetric matrix (about twice faster than a generic matrix
9054
multiplication).
9055
9056
\fun{GEN}{RgM_transmultosym}{GEN x, GEN y} returns $x\til \times y$, assuming
9057
the result is a symmetric matrix (about twice faster than a generic matrix
9058
multiplication).
9059
9060
\fun{GEN}{RgMrow_RgC_mul}{GEN x, GEN y, long i} multiplies the $i$-th row of
9061
\kbd{RgM}~\kbd{x} by the \kbd{RgC}~\kbd{y} (seen as a column vector, assumed
9062
to have compatible dimensions). Assumes that $x$ is nonempty and $0 < i <
9063
\kbd{lg(x[1])}$.
9064
9065
\fun{GEN}{RgM_mulreal}{GEN x, GEN y} returns the real part of $x\times y$
9066
(whose entries are \typ{INT}, \typ{FRAC}, \typ{REAL} or \typ{COMPLEX}).
9067
9068
\fun{GEN}{RgM_sqr}{GEN x} returns $x^2$.
9069
9070
\fun{GEN}{RgC_RgV_mul}{GEN x, GEN y} returns $x\times y$ (the matrix
9071
$(x_iy_j)$).
9072
9073
\fun{GEN}{RgC_RgV_mulrealsym}{GEN x, GEN y} returns the real part of $x\times
9074
y$ (whose entries are \typ{INT}, \typ{FRAC}, \typ{REAL} or \typ{COMPLEX}),
9075
assuming the result is symetric.
9076
9077
The following two functions are not well defined in general and only provided
9078
for convenience in specific cases:
9079
9080
\fun{GEN}{RgC_RgM_mul}{GEN x, GEN y} returns $x\times y[1,]$ if $y$ is
9081
a row matrix $1\times n$, error otherwise.
9082
9083
\fun{GEN}{RgM_RgV_mul}{GEN x, GEN y} returns $x\times y[,1]$ if $y$ is
9084
a column matrix $n\times 1$, error otherwise.
9085
9086
\fun{GEN}{RgM_powers}{GEN x, long n} returns $[\kbd{x}^0,
9087
\dots, \kbd{x}^\kbd{n}]$ as a \typ{VEC} of \kbd{RgM}s.
9088
9089
\smallskip
9090
9091
\fun{GEN}{RgV_sum}{GEN v} sum of the entries of $v$
9092
9093
\fun{GEN}{RgV_prod}{GEN v} product of the entries of $v$, using
9094
a divide and conquer strategy
9095
9096
\fun{GEN}{RgV_sumpart}{GEN v, long n} returns the sum $v[1] + \dots + v[n]$
9097
(assumes that \kbd{lg}$(v) > n$).
9098
9099
\fun{GEN}{RgV_sumpart2}{GEN v, long m, long n} returns the sum $v[m] + \dots +
9100
v[n]$ (assumes that \kbd{lg}$(v) > n$ and $m > 0$). Returns \kbd{gen\_0}
9101
when $m > n$.
9102
9103
\fun{GEN}{RgM_sumcol}{GEN v} returns a \typ{COL}, sum of the columns of the
9104
\typ{MAT} $v$.
9105
9106
\fun{GEN}{RgV_dotproduct}{GEN x,GEN y} returns the scalar product of $x$ and $y$
9107
9108
\fun{GEN}{RgV_dotsquare}{GEN x} returns the scalar product of $x$ with itself.
9109
9110
\fun{GEN}{RgV_kill0}{GEN v} returns a shallow copy of $v$ where entries
9111
matched by \kbd{gequal0} are replaced by \kbd{NULL}. The return value
9112
is not a valid \kbd{GEN} and must be handled specially. The idea is
9113
to pre-treat a vector of coefficients to speed up later linear combinations
9114
or scalar products.
9115
9116
\fun{GEN}{gram_matrix}{GEN v} returns the \idx{Gram matrix} $(v_i\cdot v_j)$
9117
attached to the entries of $v$ (matrix, or vector of vectors).
9118
9119
\fun{GEN}{RgV_polint}{GEN X, GEN Y, long v} $X$ and $Y$ being two vectors of
9120
the same length, returns the polynomial $T$ in variable $v$ such that
9121
$T(X[i]) = Y[i]$ for all $i$. The special case $X = \kbd{NULL}$
9122
corresponds to $X = [1,2,\dots,n]$, where $n$ is the length of $Y$.
9123
9124
\fun{GEN}{polintspec}{GEN X, GEN Y, GEN t, long n, long *pe}
9125
return $P(t)$ where $P$ is the Lagrange interpolation polynomial
9126
attached to the $n$ points $(X[0],Y[0]), \dots, (X[n-1],Y[n-1])$.
9127
If \kbd{pe} is not \kbd{NULL} and $t$ is a complex numeric value, \kbd{*pe}
9128
contains an error estimate for the returned value (Neville's algorithm, see
9129
\kbd{polinterpolate}). In extrapolation algorithms, e.g., Romberg
9130
integration, this function is usually called on actual \kbd{GEN} vectors
9131
with offsets: $x + k$ and $y + k$ so as to interpolate on $x[k..k+n-1]$
9132
without having to use \kbd{vecslice}.
9133
9134
\fun{GEN}{polint_i}{GEN X, GEN Y, GEN t, long *pe} as \kbd{polintspec},
9135
where $X$ and $Y$ are actual \kbd{GEN} vectors.
9136
9137
\subsubsec{Special shapes}
9138
9139
The following routines check whether matrices or vectors have a special
9140
shape, using \kbd{gequal1} and \kbd{gequal0} to test components. (This makes
9141
a difference when components are inexact.)
9142
9143
\fun{int}{RgV_isscalar}{GEN x} return 1 if all the entries of $x$ are $0$
9144
(as per \kbd{gequal0}), except possibly the first one. The name comes from
9145
vectors expressing polynomials on the standard basis $1,T,\dots, T^{n-1}$, or
9146
on \kbd{nf.zk} (whose first element is $1$).
9147
9148
\fun{int}{QV_isscalar}{GEN x} as \kbd{RgV\_isscalar}, assuming $x$ is a
9149
\kbd{QV} (\typ{INT} and \typ{FRAC} entries only).
9150
9151
\fun{int}{ZV_isscalar}{GEN x} as \kbd{RgV\_isscalar}, assuming $x$ is a
9152
\kbd{ZV} (\typ{INT} entries only).
9153
9154
\fun{int}{RgM_isscalar}{GEN x, GEN s} return 1 if $x$ is the scalar matrix
9155
equal to $s$ times the identity, and 0 otherwise. If $s$ is \kbd{NULL}, test
9156
whether $x$ is an arbitrary scalar matrix.
9157
9158
\fun{int}{RgM_isidentity}{GEN x} return 1 if the \typ{MAT} $x$ is the
9159
identity matrix, and 0 otherwise.
9160
9161
\fun{int}{RgM_isdiagonal}{GEN x} return 1 if the \typ{MAT} $x$ is a
9162
diagonal matrix, and 0 otherwise.
9163
9164
\fun{long}{RgC_is_ei}{GEN x} return $i$ if the \typ{COL} $x$ has $0$ entries,
9165
but for a $1$ at position $i$.
9166
9167
\fun{int}{RgM_is_ZM}{GEN x} return 1 if the \typ{MAT}~$x$ has only
9168
\typ{INT} coefficients, and 0 otherwise.
9169
9170
\fun{long}{qfiseven}{GEN M} return 1 if the square symetric typ{ZM}~$x$
9171
is an even quadratic form (all diagonal coefficients are even), and 0 otherwise.
9172
9173
\fun{int}{RgM_is_QM}{GEN x} return 1 if the \typ{MAT}~$x$ has only
9174
\typ{INT} or \typ{FRAC} coefficients, and 0 otherwise.
9175
9176
\fun{long}{RgV_isin}{GEN v, GEN x} return the first index $i$ such that
9177
$v[i] = x$ if it exists, and $0$ otherwise. Naive search in linear time, does
9178
not assume that \kbd{v} is sorted.
9179
9180
\fun{long}{RgV_isin_i}{GEN v, GEN x, long n} return the first index $i\ leq n$
9181
such that $v[i] = x$ if it exists, and $0$ otherwise. Naive search in linear
9182
time, does not assume that \kbd{v} is sorted. Assume that $n < \kbd{lg(v)}$.
9183
9184
\fun{GEN}{RgM_diagonal}{GEN m} returns the diagonal of $m$ as a \typ{VEC}.
9185
9186
\fun{GEN}{RgM_diagonal_shallow}{GEN m} shallow version of \kbd{RgM\_diagonal}
9187
9188
\subsubsec{Conversion to floating point entries}
9189
9190
\fun{GEN}{RgC_gtofp}{GEN x, GEN prec} returns the \typ{COL} obtained by
9191
applying \kbd{gtofp(gel(x,i), prec)} to all coefficients of $x$.
9192
9193
\fun{GEN}{RgV_gtofp}{GEN x, GEN prec} returns the \typ{VEC} obtained by
9194
applying \kbd{gtofp(gel(x,i), prec)} to all coefficients of $x$.
9195
9196
\fun{GEN}{RgC_gtomp}{GEN x, long prec} returns the \typ{COL} obtained by
9197
applying \kbd{gtomp(gel(x,i), prec)} to all coefficients of $x$.
9198
9199
\fun{GEN}{RgC_fpnorml2}{GEN x, long prec} returns (a stack-clean variant of)
9200
\bprog
9201
gnorml2( RgC_gtofp(x, prec) )
9202
@eprog
9203
9204
\fun{GEN}{RgM_gtofp}{GEN x, GEN prec} returns the \typ{MAT} obtained by
9205
applying \kbd{gtofp(gel(x,i), prec)} to all coefficients of $x$.
9206
9207
\fun{GEN}{RgM_gtomp}{GEN x, long prec} returns the \typ{MAT} obtained by
9208
applying \kbd{gtomp(gel(x,i), prec)} to all coefficients of $x$.
9209
9210
\fun{GEN}{RgM_fpnorml2}{GEN x, long prec} returns (a stack-clean variant of)
9211
\bprog
9212
gnorml2( RgM_gtofp(x, prec) )
9213
@eprog
9214
9215
\subsubsec{Linear algebra, linear systems}
9216
9217
\fun{GEN}{RgM_inv}{GEN a} returns a left inverse of $a$ (which needs not be
9218
square), or \kbd{NULL} if this turns out to be impossible. The latter
9219
happens when the matrix does not have maximal rank (or when rounding errors
9220
make it appear so).
9221
9222
\fun{GEN}{RgM_inv_upper}{GEN a} as \kbd{RgM\_inv}, assuming that $a$ is a
9223
nonempty invertible upper triangular matrix, hence a little faster.
9224
9225
\fun{GEN}{RgM_RgC_invimage}{GEN A, GEN B} returns a \typ{COL} $X$ such that
9226
$A X = B$ if one such exists, and \kbd{NULL} otherwise.
9227
9228
\fun{GEN}{RgM_invimage}{GEN A, GEN B} returns a \typ{MAT} $X$ such that
9229
$A X = B$ if one such exists, and \kbd{NULL} otherwise.
9230
9231
\fun{GEN}{RgM_Hadamard}{GEN a} returns a upper bound for the absolute
9232
value of $\text{det}(a)$. The bound is a \typ{INT}.
9233
9234
\fun{GEN}{RgM_solve}{GEN a, GEN b} returns $a^{-1}b$ where $a$ is a square
9235
\typ{MAT} and $b$ is a \typ{COL} or \typ{MAT}. Returns \kbd{NULL} if $a^{-1}$
9236
cannot be computed, see \tet{RgM_inv}.
9237
9238
If $b = \kbd{NULL}$, the matrix $a$ need no longer be square, and we strive
9239
to return a left inverse for $a$ (\kbd{NULL} if it does not exist).
9240
9241
\fun{GEN}{RgM_solve_realimag}{GEN M, GEN b} $M$ being a \typ{MAT}
9242
with $r_1+r_2$ rows and $r_1+2r_2$ columns, $y$ a \typ{COL} or \typ{MAT}
9243
such that the equation $Mx = y$ makes sense, returns $x$ under the following
9244
simplifying assumptions: the first $r_1$ rows of $M$ and $y$ are real
9245
(the $r_2$ others are complex), and $x$ is real. This is stabler and faster
9246
than calling $\kbd{RgM\_solve}(M, b)$ over $\C$. In most applications,
9247
$M$ approximates the complex embeddings of an integer basis in a number
9248
field, and $x$ is actually rational.
9249
9250
\fun{GEN}{split_realimag}{GEN x, long r1, long r2} $x$ is a \typ{COL} or
9251
\typ{MAT} with $r_1 + r_2$ rows, whose first $r_1$ rows have real entries
9252
(the $r_2$ others are complex). Return an object of the same type as
9253
$x$ and $r_1 + 2r_2$ rows, such that the first $r_1 + r_2$ rows contain
9254
the real part of $x$, and the $r_2$ following ones contain the imaginary part
9255
of the last $r_2$ rows of $x$. Called by \tet{RgM_solve_realimag}.
9256
9257
\fun{GEN}{RgM_det_triangular}{GEN x} returns the product of the diagonal
9258
entries of $x$ (its determinant if it is indeed triangular).
9259
9260
\fun{GEN}{Frobeniusform}{GEN V, long n} given the vector $V$ of elementary
9261
divisors for $M - x\text{Id}$, where $M$ is an $n\times n$ square matrix.
9262
Returns the Frobenius form of $M$.
9263
9264
\fun{GEN}{RgM_Frobenius}{GEN A, long flag, GEN *pP, GEN *pv}
9265
given a square matrix $A$, returns the Frobenius form of A if \kbd{flag} is $0$,
9266
or a weak Frobenius form of $A$ if \kbd{flag} is $1$.
9267
If \kbd{pP} is nonzero, set \kbd{*pP} to the change of basis matrix.
9268
If \kbd{pv} is nonzero, set \kbd{*pv} to the \typ{VECSMALL} containing the column indices of the left column of each diagonal block.
9269
9270
\fun{int}{RgM_QR_init}{GEN x, GEN *pB, GEN *pQ, GEN *pL, long prec}
9271
QR-decomposition of a square invertible \typ{MAT} $x$ with real coefficients.
9272
Sets \kbd{*pB} to the vector of squared lengths of the $x[i]$, \kbd{*pL} to
9273
the Gram-Schmidt coefficients and \kbd{*pQ} to a vector of successive
9274
Householder transforms. If $R$ denotes the transpose of $L$ and $Q$ is the
9275
result of applying \kbd{*pQ} to the identity matrix, then $x = QR$ is the QR
9276
decomposition of $x$. Returns $0$ is $x$ is not invertible or we hit a
9277
precision problem, and $1$ otherwise.
9278
9279
\fun{int}{QR_init}{GEN x, GEN *pB, GEN *pQ, GEN *pL, long prec} as
9280
\kbd{RgM\_QR\_init}, assuming further that $x$ has \typ{INT} or \typ{REAL}
9281
coefficients.
9282
9283
\fun{GEN}{R_from_QR}{GEN x, long prec} assuming that $x$ is a square
9284
invertible \typ{MAT} with \typ{INT} or \typ{REAL} coefficients, return
9285
the upper triangular $R$ from the $QR$ docomposition of $x$. Not memory
9286
clean. If the matrix is not known to have \typ{INT} or \typ{REAL}
9287
coefficients, apply \tet{RgM_gtomp} first.
9288
9289
\fun{GEN}{gaussred_from_QR}{GEN x, long prec} assuming that $x$ is a square
9290
invertible \typ{MAT} with \typ{INT} or \typ{REAL} coefficients, returns
9291
\kbd{qfgaussred(x\til * x)}; this is essentially the upper triangular $R$
9292
matrix from the $QR$ decomposition of $x$, renormalized to accomodate
9293
\kbd{qfgaussred} conventions. Not memory clean.
9294
9295
\fun{GEN}{RgM_gram_schmidt}{GEN e, GEN *ptB} naive (unstable) Gram-Schmidt
9296
orthogonalization of the basis $(e_i)$ given by the columns of \typ{MAT} $e$.
9297
Return the $e_i^*$ (as columns of a \typ{MAT}) and set \kbd{*ptB} to the
9298
vector of squared lengths $|e_i^*|^2$.
9299
9300
\fun{GEN}{RgM_Babai}{GEN M, GEN y} given a \typ{MAT} $M$ of maximal rank $n$
9301
and a \typ{COL} $y$ of the same dimension, apply Babai's nearest plane
9302
algorithm to return an \emph{integral} $x$ such that $y - Mx$ has small $L_2$
9303
norm. This yields an approximate solution to the closest vector problem: if
9304
$M$ is LLL-reduced, then
9305
$$|| y - Mx ||_2 \leq 2 (2/\sqrt{3})^n || y - MX ||_2$$
9306
for all $X \in \Z^n$.
9307
9308
\subsec{\kbd{ZG}}
9309
9310
Let $G$ be a multiplicative group with neutral element $1_G$ whose
9311
multiplication is supported by \kbd{gmul} and where equality test is
9312
performed using \tet{gidentical}, e.g. a matrix group. The following
9313
routines implement basic computations in the group algebra $\Z[G]$. All of
9314
them are shallow for efficiency reasons. A \kbd{ZG} is either
9315
9316
\item a \typ{INT} $n$, representing $n[1_G]$
9317
9318
\item or a ``factorization matrix'' with two columns $[g,e]$: the first one
9319
contains group elements, sorted according to \tet{cmp_universal}, and the
9320
second one contains integer ``exponents'', representing $\sum e_i [g_i]$.
9321
9322
Note that \tet{to_famat} and \tet{to_famat_shallow}$(g,e)$ allow to build
9323
the \kbd{ZG} $e[g]$ from $e\in \Z$ and $g\in G$.
9324
9325
\fun{GEN}{ZG_normalize}{GEN x} given a \typ{INT} $x$ or a factorization
9326
matrix \emph{without} assuming that the first column is properly sorted.
9327
Return a valid (sorted) \kbd{ZG}. Shallow function.
9328
9329
\fun{GEN}{ZG_add}{GEN x, GEN y} return $x+y$; shallow function.
9330
9331
\fun{GEN}{ZG_neg}{GEN x} return $-x$; shallow function.
9332
9333
\fun{GEN}{ZG_sub}{GEN x, GEN y} return $x-y$; shallow function.
9334
9335
\fun{GEN}{ZG_mul}{GEN x, GEN y} return $xy$; shallow function.
9336
9337
\fun{GEN}{ZG_G_mul}{GEN x, GEN y} given a \kbd{ZG} $x$ and $y\in G$,
9338
return $xy$; shallow function.
9339
9340
\fun{GEN}{G_ZG_mul}{GEN x, GEN y} given a \kbd{ZG} $y$ and $x\in G$,
9341
return $xy$; shallow function.
9342
9343
\fun{GEN}{ZG_Z_mul}{GEN x, GEN n} given a \kbd{ZG} $x$ and $y\in \Z$,
9344
return $xy$; shallow function.
9345
9346
\fun{GEN}{ZGC_G_mul}{GEN v, GEN x} given $v$ a vector of \kbd{ZG} and $x\in
9347
G$ return the vector (with the same type as $v$ with entries $v[i]\cdot x$.
9348
Shallow function.
9349
9350
\fun{void}{ZGC_G_mul_inplace}{GEN v, GEN x} as \tet{ZGC_G_mul}, modifying
9351
$v$ in place.
9352
9353
\fun{GEN}{ZGC_Z_mul}{GEN v, GEN n} given $v$ a vector of \kbd{ZG} and $n\in
9354
Z$ return the vector (with the same type as $v$ with entries $n \cdot v[i]$.
9355
Shallow function.
9356
9357
\fun{GEN}{G_ZGC_mul}{GEN x, GEN v} given $v$ a vector of \kbd{ZG} and $x\in
9358
G$ return the vector of $x \cdot v[i]$. Shallow function.
9359
9360
\fun{GEN}{ZGCs_add}{GEN x, GEN y} add two sparse vectors of
9361
\kbd{ZG} elements (see Sparse linear algebra below).
9362
9363
\subsec{Sparse and blackbox linear algebra}
9364
9365
A sparse column \kbd{zCs} $v$ is a \typ{COL} with two components $C$ and $E$
9366
which are \typ{VECSMALL} of the same length, representing $\sum_i
9367
E[i]*e_{C[i]}$, where $(e_j)$ is the canonical basis. A sparse matrix
9368
(\kbd{zMs}) is a \typ{VEC} of \kbd{zCs}.
9369
9370
\kbd{FpCs} and \kbd{FpMs} are identical to the above, but $E[i]$ is now
9371
interpreted as a \emph{signed} C long integer representing an element of
9372
$\F_p$. This is important since $p$ can be so large that $p+E[i]$ would not
9373
fit in a C long.
9374
9375
\kbd{RgCs} and \kbd{RgMs} are similar, except that the type of the components
9376
of $E$ is now unspecified. Functions handling those later objects
9377
must not depend on the type of those components.
9378
9379
\kbd{F2Ms} are \typ{VEC} of \kbd{F2Cs}. \kbd{F2Cs} are \typ{VECSMALL} whoses
9380
entries are the nonzero coefficients ($1$).
9381
9382
It is not possible to derive the space dimension (number of rows) from the
9383
above data. Thus most functions take an argument \kbd{nbrow} which is the
9384
number of rows of the corresponding column/matrix in dense representation.
9385
9386
\fun{GEN}{F2Ms_to_F2m}{GEN M, long nbrow} convert a \kbd{F2m} to a \kbd{F2Ms}.
9387
9388
\fun{GEN}{F2m_to_F2Ms}{GEN M} convert a \kbd{F2m} to a \kbd{F2Ms}.
9389
9390
\fun{GEN}{zCs_to_ZC}{GEN C, long nbrow} convert the sparse vector $C$
9391
to a dense \kbd{ZC} of dimension \kbd{nbrow}.
9392
9393
\fun{GEN}{zMs_to_ZM}{GEN M, long nbrow} convert the sparse matrix $M$
9394
to a dense \kbd{ZM} whose columns have dimension \kbd{nbrow}.
9395
9396
\fun{GEN}{FpMs_FpC_mul}{GEN M, GEN B, GEN p} multiply the sparse matrix $M$
9397
(over $\F_p$) by the \kbd{FpC} $B$. The result is an \kbd{FpC}, i.e.~a
9398
dense vector.
9399
9400
\fun{GEN}{zMs_ZC_mul}{GEN M, GEN B, GEN p} multiply the sparse matrix $M$
9401
by the \kbd{ZC} $B$ (over $\Z$). The result is an \kbd{ZC}, i.e.~a
9402
dense vector.
9403
9404
\fun{GEN}{FpV_FpMs_mul}{GEN B, GEN M, GEN p} multiply the \kbd{FpV} $B$
9405
by the sparse matrix $M$ (over $\F_p$). The result is an \kbd{FpV}, i.e.~a
9406
dense vector.
9407
9408
\fun{GEN}{ZV_zMs_mul}{GEN B, GEN M, GEN p} multiply the \kbd{FpV} $B$ (over
9409
$\Z$) by the sparse matrix $M$. The result is an \kbd{ZV}, i.e.~a
9410
dense vector.
9411
9412
\fun{void}{RgMs_structelim}{GEN M, long nbrow, GEN A, GEN *p_col, GEN *p_row}
9413
$M$ being a RgMs with \kbd{nbrow} rows, $A$ being a list of row indices,
9414
Perform structured elimination on $M$ by removing some rows and columns until
9415
the number of effectively present rows is equal to the number of columns.
9416
the result is stored in two \typ{VECSMALL}s, \kbd{*p\_col} and \kbd{*p\_row}:
9417
\kbd{*p\_col} is a map from the new columns indices to the old one.
9418
\kbd{*p\_row} is a map from the old rows indices to the new one ($0$ if removed).
9419
9420
\fun{GEN}{F2Ms_colelim}{GEN M, long nbrow} returns some subset of the columns
9421
of $M$ as a \typ{VECSMALL} of indices, selected such that the dimension of the
9422
kernel of the matrix is preserved. The subset is not guaranteed to be minimal.
9423
9424
\fun{GEN}{F2Ms_ker}{GEN M, long nbrow} returns some kernel vectors of $M$
9425
using block Lanczos algorithm.
9426
9427
\fun{GEN}{FpMs_leftkernel_elt}{GEN M, long nbrow, GEN p}
9428
$M$ being a sparse matrix over $\F_p$, return a nonzero kbd{FpV} $X$ such
9429
that $X\*M$ components are almost all $0$.
9430
9431
\fun{GEN}{FpMs_FpCs_solve}{GEN M, GEN B, long nbrow, GEN p}
9432
solve the equation $M\*X = B$, where $M$ is a sparse matrix and $B$ is a sparse
9433
vector, both over $\F_p$. Return either a solution as a \typ{COL} (dense
9434
vector), the index of a column which is linearly dependent from the
9435
others as a \typ{VECSMALL} with a single component, or \kbd{NULL}
9436
(can happen if $B$ is not in the image of $M$).
9437
9438
\fun{GEN}{FpMs_FpCs_solve_safe}{GEN M, GEN B, long nbrow, GEN p}
9439
as above, but in the event that $p$ is not a prime and an impossible division
9440
occurs, return \kbd{NULL}.
9441
9442
\fun{GEN}{ZpMs_ZpCs_solve}{GEN M, GEN B, long nbrow, GEN p, long e}
9443
solve the equation $MX = B$, where $M$ is a sparse matrix and $B$ is a sparse
9444
vector, both over $\Z/p^e\Z$. Return either a solution as a \typ{COL} (dense
9445
vector), or the index of a column which is linearly dependent from the
9446
others as a \typ{VECSMALL} with a single component.
9447
9448
\fun{GEN}{gen_FpM_Wiedemann}{void *E, GEN (*f)(void*, GEN), GEN B, GEN p}
9449
solve the equation $f(X) = B$ over $\F_p$, where $B$ is a \kbd{FpV}, and $f$
9450
is a blackbox endomorphism, where $f(E, X)$ computes the value of $f$ at the
9451
(dense) column vector $X$. Returns either a solution \typ{COL}, or a kernel
9452
vector as a \typ{VEC}.
9453
9454
\fun{GEN}{gen_ZpM_Dixon_Wiedemann}{void *E, GEN (*f)(void*, GEN), GEN B, GEN p, long e}
9455
solve equation $f(X) = B$ over $\Z/p^e\Z$, where $B$ is a \kbd{ZV}, and $f$ is a
9456
blackbox endomorphism, where $f(E, X)$ computes the value of $f$ at the
9457
(dense) column vector $X$. Returns either a solution \typ{COL}, or a kernel
9458
vector as a \typ{VEC}.
9459
9460
\subsec{Obsolete functions}
9461
9462
The functions in this section are kept for backward compatibility only
9463
and will eventually disappear.
9464
9465
\fun{GEN}{image2}{GEN x} compute the image of $x$ using a very slow
9466
algorithm. Use \tet{image} instead.
9467
9468
\section{Integral, rational and generic polynomial arithmetic}
9469
9470
\subsec{\kbd{ZX}}
9471
9472
\fun{void}{RgX_check_ZX}{GEN x, const char *s} Assuming \kbd{x} is a \typ{POL}
9473
raise an error if it is not a \kbd{ZX} ($s$ should point to the name of the
9474
caller).
9475
9476
\fun{GEN}{ZX_copy}{GEN x,GEN p} returns a copy of \kbd{x}.
9477
9478
\fun{long}{ZX_max_lg}{GEN x} returns the effective length of the longest
9479
component in $x$.
9480
9481
\fun{GEN}{scalar_ZX}{GEN x, long v} returns the constant \kbd{ZX} in variable
9482
$v$ equal to the \typ{INT} $x$.
9483
9484
\fun{GEN}{scalar_ZX_shallow}{GEN x, long v} returns the constant \kbd{ZX} in
9485
variable $v$ equal to the \typ{INT} $x$. Shallow function not suitable for
9486
\kbd{gerepile} and friends.
9487
9488
\fun{GEN}{ZX_renormalize}{GEN x, long l}, as \kbd{normalizepol}, where
9489
$\kbd{l} = \kbd{lg(x)}$, in place.
9490
9491
\fun{int}{ZX_equal}{GEN x, GEN y} returns $1$ if the two \kbd{ZX} have
9492
the same \kbd{degpol} and their coefficients are equal. Variable numbers are
9493
not checked.
9494
9495
\fun{int}{ZX_equal1}{GEN x} returns $1$ if the \kbd{ZX} $x$ is equal to $1$
9496
and $0$ otherwise.
9497
9498
\fun{int}{ZX_is_monic}{GEN x} returns $1$ if the \kbd{ZX} $x$ is monic and $0$
9499
otherwise. The zero polynomial considered not monic.
9500
9501
\fun{GEN}{ZX_add}{GEN x,GEN y} adds \kbd{x} and \kbd{y}.
9502
9503
\fun{GEN}{ZX_sub}{GEN x,GEN y} subtracts \kbd{x} and \kbd{y}.
9504
9505
\fun{GEN}{ZX_neg}{GEN x} returns $-\kbd{x}$.
9506
9507
\fun{GEN}{ZX_Z_add}{GEN x,GEN y} adds the integer \kbd{y} to the
9508
\kbd{ZX}~\kbd{x}.
9509
9510
\fun{GEN}{ZX_Z_add_shallow}{GEN x,GEN y} shallow version of \tet{ZX_Z_add}.
9511
9512
\fun{GEN}{ZX_Z_sub}{GEN x,GEN y} subtracts the integer \kbd{y} to the
9513
\kbd{ZX}~\kbd{x}.
9514
9515
\fun{GEN}{Z_ZX_sub}{GEN x,GEN y} subtracts the \kbd{ZX} \kbd{y} to the
9516
integer \kbd{x}.
9517
9518
\fun{GEN}{ZX_Z_mul}{GEN x,GEN y} multiplies the \kbd{ZX} \kbd{x} by the
9519
integer \kbd{y}.
9520
9521
\fun{GEN}{ZX_mulu}{GEN x, ulong y} multiplies \kbd{x} by the integer \kbd{y}.
9522
9523
\fun{GEN}{ZX_shifti}{GEN x, long n} shifts all coefficients of \kbd{x} by $n$
9524
bits, which can be negative.
9525
9526
\fun{GEN}{ZX_Z_divexact}{GEN x, GEN y} returns $x/y$ assuming all divisions
9527
are exact.
9528
9529
\fun{GEN}{ZX_divuexact}{GEN x, ulong y} returns $x/y$ assuming all divisions
9530
are exact.
9531
9532
\fun{GEN}{ZX_remi2n}{GEN x, long n} reduces all coefficients of \kbd{x} to
9533
$n$ bits, using \tet{remi2n}.
9534
9535
\fun{GEN}{ZX_mul}{GEN x,GEN y} multiplies \kbd{x} and \kbd{y}.
9536
9537
\fun{GEN}{ZX_sqr}{GEN x,GEN p} returns $\kbd{x}^2$.
9538
9539
\fun{GEN}{ZX_mulspec}{GEN a, GEN b, long na, long nb}. Internal routine:
9540
\kbd{a} and \kbd{b} are arrays of coefficients representing polynomials
9541
$\sum_{i = 0}^{\kbd{na-1}} \kbd{a}[i] X^i$ and
9542
$\sum_{i = 0}^{\kbd{nb-1}} \kbd{b}[i] X^i$. Returns their product (as a true
9543
\kbd{GEN}) in variable $0$.
9544
9545
\fun{GEN}{ZX_sqrspec}{GEN a, long na}. Internal routine:
9546
\kbd{a} is an array of coefficients representing polynomial
9547
$\sum_{i = 0}^{\kbd{na-1}} \kbd{a}[i] X^i$. Return its square (as a true
9548
\kbd{GEN}) in variable $0$.
9549
9550
\fun{GEN}{ZX_rem}{GEN x, GEN y} returns the remainder of the Euclidean
9551
division of $x$ mod $y$. Assume that $x$, $y$ are two \kbd{ZX} and that
9552
$y$ is monic.
9553
9554
\fun{GEN}{ZX_mod_Xnm1}{GEN T, ulong n} return $T$ modulo $X^n - 1)$. Shallow
9555
function.
9556
9557
\fun{GEN}{ZX_div_by_X_1}{GEN T, GEN *r} return the quotient of $T$ by $X-1$.
9558
If $r$ is not \kbd{NULL} set it to $T(1)$.
9559
9560
\fun{GEN}{ZX_gcd}{GEN x,GEN y} returns a gcd of the \kbd{ZX} $x$ and $y$.
9561
Not memory-clean, but suitable for \kbd{gerepileupto}.
9562
9563
\fun{GEN}{ZX_gcd_all}{GEN x, GEN y, GEN *pX} returns a gcd $d$ of $x$ and
9564
$y$. If \kbd{pX} is not \kbd{NULL}, set $\kbd{*pX}$ to a (nonzero) integer
9565
multiple of $x/d$. If $x$ and $y$ are both monic, then $d$ is monic and
9566
\kbd{*pX} is exactly $x/d$. Not memory clean.
9567
9568
\fun{GEN}{ZX_radical}{GEN x} returns the largest squarefree divisor
9569
of the \kbd{ZX} $x$. Not memory clean.
9570
9571
\fun{GEN}{ZX_content}{GEN x} returns the content of the \kbd{ZX} $x$.
9572
9573
\fun{long}{ZX_val}{GEN P} as \kbd{RgX\_val}, but assumes \kbd{P} has \typ{INT}
9574
coefficients.
9575
9576
\fun{long}{ZX_valrem}{GEN P, GEN *z} as \kbd{RgX\_valrem}, but assumes
9577
\kbd{P} has \typ{INT} coefficients.
9578
9579
\fun{GEN}{ZX_to_monic}{GEN q GEN *L} given $q$ a nonzero \kbd{ZX},
9580
returns a monic integral polynomial $Q$ such that $Q(x) = C q(x/L)$, for some
9581
rational $C$ and positive integer $L > 0$. If $\kbd{L}$ is not \kbd{NULL},
9582
set \kbd{*L} to $L$; if $L = 1$, \kbd{*L} is set to \kbd{gen\_1}. Shallow
9583
function.
9584
9585
\fun{GEN}{ZX_primitive_to_monic}{GEN q, GEN *L} as \tet{ZX_to_monic} except
9586
$q$ is assumed to have trivial content, which avoids recomputing it.
9587
The result is suboptimal if $q$ is not primitive ($L$ larger than
9588
necessary), but remains correct. Shallow function.
9589
9590
\fun{GEN}{ZX_Z_normalize}{GEN q, GEN *L} a restricted version of
9591
\kbd{ZX\_primitive\_to\_monic}, where $q$ is a \emph{monic} \kbd{ZX}
9592
of degree $> 0$. Finds the largest integer $L > 0$ such that
9593
$Q(X) := L^{-\deg q} q(Lx)$ is integral and return $Q$; this is not
9594
well-defined if $q$ is a monomial, in that case, set $L=1$ and $Q = q$. If
9595
\kbd{L} is not \kbd{NULL}, set \kbd{*L} to $L$. Shallow function.
9596
9597
\fun{GEN}{ZX_Q_normalize}{GEN q, GEN *L} a variant of \tet{ZX_Z_normalize}
9598
where $L > 0$ is allowed to be rational, the monic $Q\in \Z[X]$ has possibly
9599
smaller coefficients. Shallow function.
9600
9601
\fun{GEN}{ZX_Q_mul}{GEN x, GEN y} returns $x*y$, where $y$ is a rational number
9602
and the resulting \typ{POL} has rational entries.
9603
9604
\fun{long}{ZX_deflate_order}{GEN P} given a nonconstant \kbd{ZX}
9605
$P$, returns the largest exponent $d$ such that $P$ is of the form $P(x^d)$.
9606
9607
\fun{long}{ZX_deflate_max}{GEN P, long *d}. Given a nonconstant
9608
polynomial with integer coefficients $P$, sets \kbd{d} to
9609
\kbd{ZX\_deflate\_order(P)} and returns \kbd{RgX\_deflate(P,d)}. Shallow
9610
function.
9611
9612
\fun{GEN}{ZX_rescale}{GEN P, GEN h} returns $h^{\deg(P)} P(x/h)$.
9613
\kbd{P} is a \kbd{ZX} and \kbd{h} is a nonzero integer. Neither memory-clean
9614
nor suitable for \kbd{gerepileupto}.
9615
9616
\fun{GEN}{ZX_rescale2n}{GEN P, long n} returns $2^{n\deg(P)} P(x>>n)$ where
9617
\kbd{P} is a \kbd{ZX}.
9618
9619
\fun{GEN}{ZX_rescale_lt}{GEN P} returns the monic integral polynomial
9620
$h^{\deg(P)-1} P(x/h)$, where \kbd{P} is a nonzero \kbd{ZX} and \kbd{h} is
9621
its leading coefficient. Neither memory-clean nor suitable for
9622
\kbd{gerepileupto}.
9623
9624
\fun{GEN}{ZX_translate}{GEN P, GEN c} assume $P$ is a \kbd{ZX} and $c$ an
9625
integer. Returns $P(X + c)$ (optimized for $c = \pm 1$).
9626
9627
\fun{GEN}{ZX_Z_eval}{GEN P,GEN x} evaluate the \kbd{ZX} $P$ at the integer $x$.
9628
9629
\fun{GEN}{ZX_unscale}{GEN P, GEN h} given a \kbd{ZX} $P$ and a \typ{INT} $h$,
9630
returns $P(hx)$. Not memory clean.
9631
9632
\fun{GEN}{ZX_z_unscale}{GEN P, long h} given a \kbd{ZX} $P$,
9633
returns $P(hx)$. Not memory clean.
9634
9635
\fun{GEN}{ZX_unscale2n}{GEN P, long n} given a \kbd{ZX} $P$, returns
9636
$P(x<<n)$. Not memory clean.
9637
9638
\fun{GEN}{ZX_unscale_div}{GEN P, GEN h} given a \kbd{ZX} $P$ and a \typ{INT} $h$
9639
such that $h \mid P(0)$, returns $P(hx)/h$. Not memory clean.
9640
9641
\fun{GEN}{ZX_eval1}{GEN P} returns the integer $P(1)$.
9642
9643
\fun{GEN}{ZX_graeffe}{GEN p} returns the Graeffe transform of $p$, i.e. the
9644
\kbd{ZX} $q$ such that $p(x)p(-x) = q(x^2)$.
9645
9646
\fun{GEN}{ZX_deriv}{GEN x} returns the derivative of \kbd{x}.
9647
9648
\fun{GEN}{ZX_resultant}{GEN A, GEN B} returns the resultant of the
9649
\kbd{ZX}~\kbd{A} and \kbd{B}.
9650
9651
\fun{GEN}{ZX_disc}{GEN T} returns the discriminant of the \kbd{ZX}
9652
\kbd{T}.
9653
9654
\fun{GEN}{ZX_factor}{GEN T} returns the factorization of the primitive part
9655
of \kbd{T} over $\Q[X]$ (the content is lost).
9656
9657
\fun{int}{ZX_is_squarefree}{GEN T} returns $1$ if the
9658
\kbd{ZX}~\kbd{T} is squarefree, $0$ otherwise.
9659
9660
\fun{long}{ZX_is_irred}{GEN T} returns 1 it \kbd{T} is irreducible, and
9661
0 otherwise.
9662
9663
\fun{GEN}{ZX_squff}{GEN T, GEN *E} write $T$ as a product $\prod T_i^{e_i}$
9664
with the $e_1 < e_2 < \cdots$ all distinct and the $T_i$ pairwise coprime.
9665
Return the vector of the $T_i$, and set \kbd{*E} to the vector of the $e_i$,
9666
as a \typ{VECSMALL}.
9667
9668
\fun{GEN}{ZX_Uspensky}{GEN P, GEN ab, long flag, long bitprec} let \kbd{P} be a
9669
\kbd{ZX} polynomial whose real roots are simple and \kbd{bitprec} is the
9670
relative precision in bits. For efficiency reasons, \kbd{P} should not only
9671
have simple real roots but actually be primitive and squarefree, but the
9672
routine neither checks nor enforces this, and it returns correct results in
9673
this case as well.
9674
9675
\item If \kbd{flag} is 0 returns a list of intervals that isolate the real
9676
roots of \kbd{P}. The return value is a column of elements which are either
9677
vectors \kbd{[a,b]} of rational numbers meaning that there is a single
9678
nonrational root in the open interval \kbd{(a,b)} or elements \kbd{x0} such
9679
that \kbd{x0} is a rational root of \kbd{P}. Beware that the limits of the
9680
open intervals can be roots of the polynomial.
9681
9682
\item If \kbd{flag} is 1 returns an approximation of the real roots of \kbd{P}.
9683
9684
\item If \kbd{flag} is 2 returns the number of roots.
9685
9686
The argument \kbd{ab} specify the interval in which the roots
9687
are searched. The default interval is $(-\infty,\infty)$. If \kbd{ab} is an
9688
integer or fraction $a$ then the interval is $[a,\infty)$. If \kbd{ab} is
9689
a vector $[a,b]$, where \typ{INT}, \typ{FRAC} or \typ{INFINITY} are allowed
9690
for $a$ and $b$, the interval is $[a,b]$.
9691
9692
\fun{long}{ZX_sturm}{GEN P} number of real roots of the nonconstant
9693
squarefree \kbd{ZX} $P$. For efficiency, it is advised to make $P$ primitive
9694
first.
9695
9696
\fun{long}{ZX_sturmpart}{GEN P, GEN ab} number of real roots of the
9697
nonconstant squarefree \kbd{ZX} $P$ in the interval specified by \kbd{ab}:
9698
either \kbd{NULL} (no restriction) or a \typ{VEC} $[a,b]$ with two real
9699
components (of type \typ{INT}, \typ{FRAC} or \typ{INFINITY}). For efficiency,
9700
it is advised to make $P$ primitive first.
9701
9702
\fun{long}{ZX_sturm_irred}{GEN P} number of real roots of the \kbd{ZX} $P$,
9703
assumed irreducible over $\Q[X]$. For efficiency, it is advised to make $P$
9704
primitive first.
9705
9706
\fun{long}{ZX_realroots_irred}{GEN P, long prec} real roots of the \kbd{ZX}
9707
$P$, assumed irreducible over $\Q[X]$ to precision prec. For efficiency, it
9708
is advised to make $P$ primitive first.
9709
9710
\subsec{Resultants}
9711
9712
\fun{GEN}{ZX_ZXY_resultant}{GEN A, GEN B}
9713
under the assumption that \kbd{A} in $\Z[Y]$, \kbd{B} in $\Q[Y][X]$, and
9714
$R = \text{Res}_Y(A, B) \in \Z[X]$, returns the resultant $R$.
9715
9716
\fun{GEN}{ZX_compositum_disjoint}{GEN A, GEN B} given two irreducible \kbd{ZX}
9717
defining linearly disjoint extensions, returns a \kbd{ZX} defining their
9718
compositum.
9719
9720
\fun{GEN}{ZX_compositum}{GEN A, GEN B, long *lambda}
9721
given two irreducible \kbd{ZX}, returns an irreducible \kbd{ZX} $C$ defining
9722
their compositum and set \kbd{lambda} to a small integer $k$ such that if
9723
$\alpha$ is a root of $A$ and $\beta$ is a root of $B$, then $k\*\alpha +
9724
\beta$ is a root of $C$.
9725
9726
\fun{GEN}{ZX_ZXY_rnfequation}{GEN A, GEN B, long *lambda},
9727
assume \kbd{A} in $\Z[Y]$, \kbd{B} in $\Q[Y][X]$, and $R =
9728
\text{Res}_Y(A, B) \in \Z[X]$. If \kbd{lambda = NULL}, returns $R$
9729
as in \kbd{ZY\_ZXY\_resultant}. Otherwise, \kbd{lambda} must point to
9730
some integer, e.g. $0$ which is used as a seed. The function then finds a
9731
small $\lambda \in \Z$ (starting from \kbd{*lambda}) such that
9732
$R_\lambda(X) := \text{Res}_Y(A, B(X + \lambda Y))$ is squarefree, resets
9733
\kbd{*lambda} to the chosen value and returns $R_{\lambda}$.
9734
9735
\subsec{\kbd{ZXV}}
9736
9737
\fun{GEN}{ZXV_equal}{GEN x,GEN y} returns $1$ if the two vectors of \kbd{ZX}
9738
are equal, as per \tet{ZX_equal} (variables are not checked to be equal) and
9739
$0$ otherwise.
9740
9741
\fun{GEN}{ZXV_Z_mul}{GEN x,GEN y} multiplies the vector of \kbd{ZX} \kbd{x}
9742
by the integer \kbd{y}.
9743
9744
\fun{GEN}{ZXV_remi2n}{GEN x, long n} applies \kbd{ZX\_remi2n} to all
9745
coefficients of \kbd{x}.
9746
9747
\fun{GEN}{ZXV_dotproduct}{GEN x,GEN y} as \kbd{RgV\_dotproduct} assuming $x$
9748
and $y$ have \kbd{ZX} entries.
9749
9750
\subsec{\kbd{ZXT}}
9751
9752
\fun{GEN}{ZXT_remi2n}{GEN x, long n} applies \kbd{ZX\_remi2n} to all
9753
leaves of the tree \kbd{x}.
9754
9755
\subsec{\kbd{ZXQ}}
9756
9757
\fun{GEN}{ZXQ_mul}{GEN x,GEN y,GEN T} returns $x*y$ mod $T$, assuming
9758
that all inputs are \kbd{ZX}s and that $T$ is monic.
9759
9760
\fun{GEN}{ZXQ_sqr}{GEN x,GEN T} returns $x^2$ mod $T$, assuming
9761
that all inputs are \kbd{ZX}s and that $T$ is monic.
9762
9763
\fun{GEN}{ZXQ_powu}{GEN x,ulong n,GEN T} returns $x^n$ mod $T$, assuming
9764
that all inputs are \kbd{ZX}s and that $T$ is monic.
9765
9766
\fun{GEN}{ZXQ_powers}{GEN x, long n, GEN T} returns $[\kbd{x}^0,
9767
\dots, \kbd{x}^\kbd{n}]$ mod $T$ as a \typ{VEC}, assuming that all inputs are
9768
\kbd{ZX}s and that $T$ is monic.
9769
9770
\fun{GEN}{ZXQ_charpoly}{GEN A, GEN T, long v}: let \kbd{T} and \kbd{A} be
9771
\kbd{ZX}s, returns the characteristic polynomial of \kbd{Mod(A, T)}.
9772
More generally, \kbd{A} is allowed to be a \kbd{QX}, hence possibly has
9773
rational coefficients, \emph{assuming} the result is a \kbd{ZX}, i.e.~the
9774
algebraic number \kbd{Mod(A,T)} is integral over \kbd{Z}.
9775
9776
\fun{GEN}{ZXQ_minpoly}{GEN A, GEN B, long d, ulong bound}
9777
let \kbd{T} and \kbd{A} be
9778
\kbd{ZX}s, returns the minimal polynomial of \kbd{Mod(A, T)} assuming it has
9779
degree $d$ and its coefficients are less than $2^{\text{bound}}$.
9780
More generally, \kbd{A} is allowed to be a \kbd{QX}, hence possibly has
9781
rational coefficients, \emph{assuming} the result is a \kbd{ZX}, i.e.~the
9782
algebraic number \kbd{Mod(A,T)} is integral over \kbd{Z}.
9783
9784
\subsec{\kbd{ZXn}}
9785
9786
\fun{GEN}{ZXn_mul}{GEN x, GEN y, long n} return $x\*y\pmod{X^n}$.
9787
9788
\fun{GEN}{ZXn_sqr}{GEN x, long n} return $x^2\pmod{X^n}$.
9789
9790
\fun{GEN}{eta_ZXn}{long r, long n} return $\eta(X^r) = \prod_{i>0} (1-X^{ri})
9791
\pmod{X^n}$, $r > 0$.
9792
9793
\fun{GEN}{eta_product_ZXn}{GEN DR, long n}: $\kbd{DR}= [D,R]$ being a vector
9794
with two \typ{VECSMALL} components, return $\prod_i \eta(X^{d_i})^{r_i}$.
9795
Shallow function.
9796
9797
\subsec{\kbd{ZXQM}}
9798
9799
\kbd{ZXQM} are matrices of \kbd{ZXQ}. All entries must be integers or
9800
polynomials of degree strictly less than the degree of $T$.
9801
9802
\fun{GEN}{ZXQM_mul}{GEN x,GEN y,GEN T} returns $x*y$ mod $T$, assuming
9803
that all inputs are \kbd{ZX}s and that $T$ is monic.
9804
9805
\fun{GEN}{ZXQM_sqr}{GEN x,GEN T} returns $x^2$ mod $T$, assuming
9806
that all inputs are \kbd{ZX}s and that $T$ is monic.
9807
9808
\subsec{\kbd{ZXQX}}
9809
9810
\fun{GEN}{ZXQX_mul}{GEN x,GEN y,GEN T} returns $x*y$, assuming
9811
that all inputs are \kbd{ZXQX}s and that $T$ is monic.
9812
9813
\fun{GEN}{ZXQX_ZXQ_mul}{GEN x, GEN y, GEN T} returns $x*y$, assuming
9814
that $x$ is a \kbd{ZXQX}, $y$ is a \kbd{ZXQ} and $T$ is monic.
9815
9816
\fun{GEN}{ZXQX_sqr}{GEN x,GEN T} returns $x^2$, assuming
9817
that all inputs are \kbd{ZXQX}s and that $T$ is monic.
9818
9819
\fun{GEN}{ZXQX_gcd}{GEN x,GEN y,GEN T} returns the gcd of $x$ and $y$,
9820
assuming that all inputs are \kbd{ZXQX}s and that $T$ is monic.
9821
9822
\subsec{\kbd{ZXX}}
9823
9824
\fun{void}{RgX_check_ZXX}{GEN x, const char *s} Assuming \kbd{x} is a \typ{POL}
9825
raise an error if it one of its coefficients is not an integer or a \kbd{ZX}
9826
($s$ should point to the name of the caller).
9827
9828
\fun{GEN}{ZXX_renormalize}{GEN x, long l}, as \kbd{normalizepol}, where
9829
$\kbd{l} = \kbd{lg(x)}$, in place.
9830
9831
\fun{long}{ZXX_max_lg}{GEN x} returns the effective length of the longest
9832
component in $x$; assume all coefficients are \typ{INT} or \kbd{ZX}s.
9833
9834
\fun{GEN}{ZXX_evalx0}{GEN P} returns $P(X, 0)$.
9835
9836
\fun{GEN}{ZXX_Z_mul}{GEN x, GEN y} returns $x\*y$.
9837
9838
\fun{GEN}{ZXX_Q_mul}{GEN x, GEN y} returns $x*y$, where $y$ is a rational number
9839
and the resulting \typ{POL} has rational entries.
9840
9841
\fun{GEN}{ZXX_Z_add_shallow}{GEN x, GEN y} returns $x+y$. Shallow function.
9842
9843
\fun{GEN}{ZXX_Z_divexact}{GEN x, GEN y} returns $x/y$ assuming all integer
9844
divisions are exact.
9845
9846
\fun{GEN}{Kronecker_to_ZXX}{GEN z, long n, long v} recover $P(X,Y)$
9847
from its Kronecker form $P(X,X^{2\*n-1})$ (see \tet{RgXX_to_Kronecker}),
9848
$v$ is the variable number corresponding to $Y$. Shallow function.
9849
9850
\fun{GEN}{Kronecker_to_ZXQX}{GEN z, GEN T}. Let $n = \deg T$ and let
9851
$P(X,Y)\in \Z[X,Y]$ lift a polynomial in $K[Y]$, where $K := \Z[X]/(T)$ and
9852
$\deg_X P < 2n-1$ --- such as would result from multiplying minimal degree
9853
lifts of two polynomials in $K[Y]$. Let $z = P(t,t^{2*n-1})$ be a Kronecker
9854
form of $P$ (see \tet{RgXX_to_Kronecker}), this function returns $Q\in
9855
\Z[X,t]$ such that $Q$ is congruent to $P(X,t)$ mod $(T(X))$, $\deg_X Q <
9856
n$. Not stack-clean. Note that $t$ need not be the same variable as $Y$!
9857
9858
\fun{GEN}{ZXX_mul_Kronecker}{GEN P, GEN Q, long n} return \tet{ZX_mul}
9859
applied to the Kronecker forms $P(X,X^{2\*n-1})$ and $Q(X,X^{2\*n-1})$
9860
of $P$ and $Q$. Not memory clean.
9861
9862
\fun{GEN}{ZXX_sqr_Kronecker}{GEN P, long n} return \tet{ZX_sqr}
9863
applied to the Kronecker forms $P(X,X^{2\*n-1})$
9864
of $P$. Not memory clean.
9865
9866
\subsec{\kbd{QX}}
9867
9868
\fun{void}{RgX_check_QX}{GEN x, const char *s} Assuming \kbd{x} is a \typ{POL}
9869
raise an error if it is not a \kbd{QX} ($s$ should point to the name of the
9870
caller).
9871
9872
\fun{GEN}{QX_mul}{GEN x,GEN y}
9873
9874
\fun{GEN}{QX_sqr}{GEN x}
9875
9876
\fun{GEN}{QX_ZX_rem}{GEN x, GEN y} $y$ is assumed to be monic.
9877
9878
\fun{GEN}{QX_gcd}{GEN x,GEN y} returns a gcd of the \kbd{QX} $x$ and $y$.
9879
9880
\fun{GEN}{QX_disc}{GEN T} returns the discriminant of the \kbd{QX}
9881
\kbd{T}.
9882
9883
\fun{GEN}{QX_factor}{GEN T} as \kbd{ZX\_factor}.
9884
9885
\fun{GEN}{QX_resultant}{GEN A, GEN B} returns the resultant of the
9886
\kbd{QX}~\kbd{A} and \kbd{B}.
9887
9888
\fun{GEN}{QX_complex_roots}{GEN p, long l} returns the complex roots of the
9889
\kbd{QX} $p$ at accuracy $l$, where real roots are returned as \typ{REAL}s.
9890
More efficient when $p$ is irreducible and primitive. Special case
9891
of \tet{cleanroots}.
9892
9893
\subsec{\kbd{QXQ}}
9894
9895
\fun{GEN}{QXQ_norm}{GEN A, GEN B} $A$ being a \kbd{QX} and $B$ being a
9896
\kbd{ZX}, returns the norm of the algebraic number $A \mod B$, using a
9897
modular algorithm. To ensure that $B$ is a \kbd{ZX}, one may replace it by
9898
\kbd{Q\_primpart(B)}, which of course does not change the norm.
9899
9900
If $A$ is not a \kbd{ZX} --- it has a denominator ---, but the result is
9901
nevertheless known to be an integer, it is much more efficient to call
9902
\tet{QXQ_intnorm} instead.
9903
9904
\fun{GEN}{QXQ_intnorm}{GEN A, GEN B} $A$ being a \kbd{QX} and $B$
9905
being a \kbd{ZX}, returns the norm of the algebraic number $A \mod B$,
9906
\emph{assuming} that the result is an integer, which is for instance the case
9907
is $A\mod B$ is an algebraic integer, in particular if $A$ is a \kbd{ZX}. To
9908
ensure that $B$ is a \kbd{ZX}, one may replace it by \kbd{Q\_primpart(B)}
9909
(which of course does not change the norm).
9910
9911
If the result is not known to be an integer, you must use \tet{QXQ_norm}
9912
instead, which is slower.
9913
9914
\fun{GEN}{QXQ_mul}{GEN A, GEN B, GEN T} returns the product of $A$ and $B$
9915
modulo $T$ where both $A$ and $B$ are a \kbd{QX} and $T$ is a monic \kbd{ZX}.
9916
9917
\fun{GEN}{QXQ_sqr}{GEN A, GEN T} returns the square of $A$
9918
modulo $T$ where $A$ is a \kbd{QX} and $T$ is a monic \kbd{ZX}.
9919
9920
\fun{GEN}{QXQ_inv}{GEN A, GEN B} returns the inverse of $A$ modulo $B$
9921
where $A$ is a \kbd{QX} and $B$ is a \kbd{ZX}. Should you need this for
9922
a \kbd{QX} $B$, just use
9923
\bprog
9924
QXQ_inv(A, Q_primpart(B));
9925
@eprog\noindent But in all cases where modular arithmetic modulo $B$ is
9926
desired, it is much more efficient to replace $B$ by \kbd{Q\_primpart$(B)$}
9927
once and for all.
9928
9929
\fun{GEN}{QXQ_div}{GEN A, GEN B, GEN T} returns $A/B$ modulo $T$
9930
where $A$ and $B$ are \kbd{QX} and $T$ is a \kbd{ZX}. Use this function
9931
when the result is expected to be of the same size as $B^{-1}$ mod $T$
9932
or smaller.
9933
Otherwise, it will be faster to use \tet{QXQ_mul(A,QXQ_inv(B,T),T)}.
9934
9935
\fun{GEN}{QXQ_charpoly}{GEN A, GEN T, long v} where \kbd{A} is a \kbd{QX} and
9936
\kbd{T} is a \kbd{ZX}, returns the characteristic polynomial of \kbd{Mod(A, T)}.
9937
If the result is known to be a \kbd{ZX}, then calling \kbd{ZXQ\_charpoly} will
9938
be faster.
9939
9940
\fun{GEN}{QXQ_powers}{GEN x, long n, GEN T} returns $[\kbd{x}^0, \dots,
9941
\kbd{x}^\kbd{n}]$ as \kbd{RgXQ\_powers} would, but in a more efficient way when
9942
$x$ has a huge integer denominator (we start by removing that denominator).
9943
Meant to be used to precompute powers of algebraic integers in $\Q[t]/(T)$.
9944
The current implementation does not require $x$ to be a \kbd{QX}: any
9945
polynomial to which \kbd{Q\_remove\_denom} can be applied is fine.
9946
9947
\fun{GEN}{QXQ_reverse}{GEN f, GEN T} as \kbd{RgXQ\_reverse}, assuming $f$
9948
is a \kbd{QX}.
9949
9950
\fun{GEN}{QX_ZXQV_eval}{GEN f, GEN nV, GEN dV} as \kbd{RgX\_RgXQV\_eval},
9951
except that $f$ is assumed to be a \kbd{QX}, $V$ is given implicitly
9952
by a numerator \kbd{nV} (\kbd{ZV}) and denominator \kbd{dV} (a positive
9953
\typ{INT} or \kbd{NULL} for trivial denominator). Not memory clean, but
9954
suitable for \kbd{gerepileupto}.
9955
9956
\fun{GEN}{QXV_QXQ_eval}{GEN v, GEN a, GEN T} $v$ is a vector of \kbd{QX}s
9957
(possibly scalars, i.e.~rational numbers, for convenience), $a$ and $T$ both
9958
\kbd{QX}. Return the vector of evaluations at $a$ modulo $T$.
9959
Not memory clean, nor suitable for \kbd{gerepileupto}.
9960
9961
\fun{GEN}{QXY_QXQ_evalx}{GEN P, GEN a, GEN T} $P(X,Y)$ is a \typ{POL} with
9962
\kbd{QX} coefficients (possibly scalars, i.e.~rational numbers, for
9963
convenience) , $a$ and $T$ both \kbd{QX}. Return the \kbd{QX} $P(a \mod
9964
T, Y)$. Not memory clean, nor suitable for \kbd{gerepileupto}.
9965
9966
\subsec{\kbd{QXQX}}
9967
9968
\fun{GEN}{QXQX_mul}{GEN x, GEN y, GEN T} where $T$ is a monic \kbd{ZX}.
9969
9970
\fun{GEN}{QXQX_QXQ_mul}{GEN x, GEN y, GEN T} where $T$ is a monic \kbd{ZX}.
9971
9972
\fun{GEN}{QXQX_sqr}{GEN x, GEN T} where $T$ is a monic \kbd{ZX}
9973
9974
\fun{GEN}{QXQX_powers}{GEN x, long n, GEN T} where $T$ is a monic \kbd{ZX}
9975
9976
\fun{GEN}{nfgcd}{GEN P, GEN Q, GEN T, GEN den} given $P$ and $Q$ in
9977
$\Z[X,Y]$, $T$ monic irreducible in $\Z[Y]$, returns the primitive $d$ in
9978
$\Z[X,Y]$ which is a gcd of $P$, $Q$ in $K[X]$, where $K$ is the number field
9979
$\Q[Y]/(T)$. If not \kbd{NULL}, \kbd{den} is a multiple of the integral
9980
denominator of the (monic) gcd of $P,Q$ in $K[X]$.
9981
9982
\fun{GEN}{nfgcd_all}{GEN P, GEN Q, GEN T, GEN den, GEN *Pnew} as \kbd{nfgcd}.
9983
If \kbd{Pnew} is not \kbd{NULL}, set \kbd{*Pnew} to a nonzero integer
9984
multiple of $P/d$. If $P$ and $Q$ are both monic, then $d$ is monic and
9985
\kbd{*Pnew} is exactly $P/d$. Not memory clean if the gcd is $1$
9986
(in that case \kbd{*Pnew} is set to $P$).
9987
9988
\fun{GEN}{QXQX_gcd}{GEN x, GEN y, GEN T} returns the gcd of $x$ and $y$,
9989
assuming that $x$ and $y$ are \kbd{QXQX}s and that $T$ is a monic \kbd{ZX}.
9990
9991
\subsec{\kbd{QXQM}}
9992
9993
\kbd{QXQM} are matrices of \kbd{QXQ}. All entries must be \typ{INT}, \typ{FRAC} or
9994
polynomials of degree strictly less than the degree of $T$, which must be a monic
9995
\kbd{ZX}.
9996
9997
\fun{GEN}{QXQM_mul}{GEN x,GEN y,GEN T} returns $x*y$ mod $T$.
9998
9999
\fun{GEN}{QXQM_sqr}{GEN x,GEN T} returns $x^2$ mod $T$.
10000
10001
\subsec{\kbd{zx}}
10002
10003
\fun{GEN}{zero_zx}{long sv} returns a zero \kbd{zx} in variable $v$.
10004
10005
\fun{GEN}{polx_zx}{long sv} returns the variable $v$ as degree~1~\kbd{Flx}.
10006
10007
\fun{GEN}{zx_renormalize}{GEN x, long l}, as \kbd{Flx\_renormalize}, where
10008
$\kbd{l} = \kbd{lg(x)}$, in place.
10009
10010
\fun{GEN}{zx_shift}{GEN T, long n} return \kbd{T}
10011
multiplied by $\kbd{x}^n$, assuming $n\geq 0$.
10012
10013
\fun{long}{zx_lval}{GEN f, long p} return the valuation of $f$ at $p$.
10014
10015
\fun{GEN}{zx_z_divexact}{GEN x, long y} return \kbd{x/y} assuming all divisions
10016
are exact.
10017
10018
\subsec{\kbd{RgX}}
10019
10020
\subsubsec{Tests}
10021
10022
\fun{long}{RgX_degree}{GEN x, long v} $x$ being a \typ{POL} and $v \geq 0$,
10023
returns the degree in $v$ of $x$. Error if $x$ is not a polynomial in $v$.
10024
10025
\fun{int}{RgX_isscalar}{GEN x} return 1 if $x$ all the coefficients of
10026
$x$ of degree $> 0$ are $0$ (as per \kbd{gequal0}).
10027
10028
\fun{int}{RgX_is_rational}{GEN P} return 1 if the \kbd{RgX}~$P$ has only
10029
rational coefficients (\typ{INT} and \typ{FRAC}), and 0 otherwise.
10030
10031
\fun{int}{RgX_is_QX}{GEN P} return 1 if the \kbd{RgX}~$P$ has only
10032
\typ{INT} and \typ{FRAC} coefficients, and 0 otherwise.
10033
10034
\fun{int}{RgX_is_ZX}{GEN P} return 1 if the \kbd{RgX}~$P$ has only
10035
\typ{INT} coefficients, and 0 otherwise.
10036
10037
\fun{int}{RgX_is_monomial}{GEN x} returns 1 (true) if \kbd{x} is a nonzero
10038
monomial in its main variable, 0~otherwise.
10039
10040
\fun{long}{RgX_equal}{GEN x, GEN y} returns $1$ if the \typ{POL}s $x$ and $y$
10041
have the same \kbd{degpol} and their coefficients are equal (as per
10042
\tet{gequal}). Variable numbers are not checked. Note that this is more
10043
stringent than \kbd{gequal(x,y)}, which only checks whether $x - y$ satisfies
10044
\kbd{gequal0}; in particular, they may have different apparent degrees provided
10045
the extra leading terms are $0$.
10046
10047
\fun{long}{RgX_equal_var}{GEN x, GEN y} returns $1$ if $x$ and $y$
10048
have the same variable number and \kbd{RgX\_equal(x,y)} is $1$.
10049
10050
\subsubsec{Coefficients, blocks}
10051
10052
\fun{GEN}{RgX_coeff}{GEN P, long n} return the coefficient of $x^n$ in $P$,
10053
defined as \kbd{gen\_0} if $n < 0$ or $n > \kbd{degpol}(P)$. Shallow
10054
function.
10055
10056
\fun{int}{RgX_blocks}{GEN P, long n, long m} writes
10057
$P(X)=a_0(X)+X^n*a_1(X)*X^n+\ldots+X^{n*(m-1)}\*a_{m-1}(X)$,
10058
where the $a_i$ are polynomial of degree at most $n-1$
10059
(except possibly for the last one) and returns
10060
$[a_0(X),a_1(X),\ldots,a_{m-1}(X)]$. Shallow function.
10061
10062
\fun{void}{RgX_even_odd}{GEN p, GEN *pe, GEN *po} write $p(X) = E(X^2) +
10063
X O(X^2)$ and set \kbd{*pe = E}, \kbd{*po = O}. Shallow function.
10064
10065
\fun{GEN}{RgX_splitting}{GEN P, long k} write
10066
$P(X)=a_0(X^k)+X\*a_1(X^k)+\ldots+X^{k-1}\*a_{k-1}(X^k)$ and return
10067
$[a_0(X),a_1(X),\ldots,a_{k-1}(X)]$. Shallow function.
10068
10069
\fun{GEN}{RgX_copy}{GEN x} returns (a deep copy of) $\kbd{x}$.
10070
10071
\fun{GEN}{RgX_renormalize}{GEN x} remove leading terms in \kbd{x} which are
10072
equal to (necessarily inexact) zeros.
10073
10074
\fun{GEN}{RgX_renormalize_lg}{GEN x, long lx} as \kbd{setlg(x, lx)}
10075
followed by \kbd{RgX\_renormalize(x)}. Assumes that $\kbd{lx} \leq
10076
\kbd{lg(x)}$.
10077
10078
\fun{GEN}{RgX_recip}{GEN P} returns the reverse of the polynomial
10079
$P$, i.e. $X^{\deg P} P(1/X)$.
10080
10081
\fun{GEN}{RgX_recip_shallow}{GEN P} shallow function of \tet{RgX_recip}.
10082
10083
\fun{GEN}{RgX_recip_i}{GEN P} shallow function of \tet{RgX_recip},
10084
where we further assume that $P(0)\neq 0$, so that the degree of the output
10085
is the degree of $P$.
10086
10087
\fun{long}{rfracrecip}{GEN *a, GEN *b} let \kbd{*a} and \kbd{*b} be such that
10088
their quotient $F$ is a \typ{RFRAC} in variable $X$. Write $F(1/X) = X^v A/B$
10089
where $A$ and $B$ are coprime to $X$ and $v$ in $\Z$. Set \kbd{*a} to A,
10090
\kbd{*b} to $B$ and return $v$.
10091
10092
\fun{GEN}{RgX_deflate}{GEN P, long d} assuming $P$ is a polynomial of the
10093
form $Q(X^d)$, return $Q$. Shallow function, not suitable for
10094
\kbd{gerepileupto}.
10095
10096
\fun{long}{RgX_deflate_order}{GEN P} given a nonconstant polynomial
10097
$P$, returns the largest exponent $d$ such that $P$ is of the form $P(x^d)$
10098
(use \kbd{gequal0} to check whether coefficients are 0).
10099
10100
\fun{long}{RgX_deflate_max}{GEN P, long *d} given a nonconstant polynomial
10101
$P$, sets \kbd{d} to \kbd{RgX\_deflate\_order(P)} and
10102
returns \kbd{RgX\_deflate(P,d)}. Shallow function.
10103
10104
\fun{long}{rfrac_deflate_order}{GEN F}
10105
as \kbd{RgX\_deflate\_order} where $F$ is a nonconstant \typ{RFRAC}.
10106
10107
\fun{long}{rfrac_deflate_max}{GEN F, long *d}
10108
as \kbd{RgX\_deflate\_max} where $F$ is a nonconstant \typ{RFRAC}.
10109
10110
\fun{GEN}{rfrac_deflate}{GEN F, long m}
10111
as \kbd{RgX\_deflate} where $F$ is a \typ{RFRAC}.
10112
10113
\fun{GEN}{RgX_inflate}{GEN P, long d} return $P(X^d)$. Shallow function, not
10114
suitable for \kbd{gerepileupto}.
10115
10116
\fun{GEN}{RgX_rescale_to_int}{GEN x} given a polynomial $x$ with real entries
10117
(\typ{INT}, \typ{FRAC} or \typ{REAL}), return a \kbd{ZX} wich is very close
10118
to $D x$ for some well-chosen integer $D$. More precisely, if the input is
10119
exact, $D$ is the denominator of $x$; else it is a power of $2$ chosen
10120
so that all inexact entries are correctly rounded to 1 ulp.
10121
10122
\fun{GEN}{RgXX_to_Kronecker}{GEN P, long n} Assuming $P(X,Y)$ is a polynomial
10123
of degree in $X$ strictly less than $n$, returns $P(X,X^{2*n-1})$, the
10124
Kronecker form of $P$. Shallow function.
10125
10126
\fun{GEN}{RgXX_to_Kronecker_spec}{GEN Q, long lQ, long n} return
10127
\tet{RgXX_to_Kronecker}$(P, n)$, where $P$ is the polynomial
10128
$\sum_{i = 0}^{\kbd{lQ} - 1} Q[i] x^i$. To be used when splitting
10129
the coefficients of genuine polynomials into blocks. Shallow function.
10130
10131
\subsubsec{Shifts, valuations}
10132
10133
\fun{GEN}{RgX_shift}{GEN x, long n} returns $\kbd{x} * t^n$ if $n\geq 0$,
10134
and $\kbd{x} \bs t^{-n}$ otherwise.
10135
10136
\fun{GEN}{RgX_shift_shallow}{GEN x, long n} as \kbd{RgX\_shift}, but
10137
shallow (coefficients are not copied).
10138
10139
\fun{GEN}{RgX_rotate_shallow}{GEN P, long k, long p} returns $\kbd{P} * X^k
10140
\pmod {X^p-1}$, assuming the degree of $P$ is strictly less than $p$, and
10141
$k\geq 0$.
10142
10143
\fun{void}{RgX_shift_inplace_init}{long v} $v \geq 0$, prepare for a later
10144
call to \tet{RgX_shift_inplace}. Reserves $v$ words on the stack.
10145
10146
\fun{GEN}{RgX_shift_inplace}{GEN x, long v} $v \geq 0$, assume that
10147
\tet{RgX_shift_inplace_init}$(v)$ has been called (reserving $v$ words on the
10148
stack), immediately followed by a \typ{POL} $x$. Return \kbd{RgX\_shift}$(x,v)$
10149
by shifting $x$ in place. To be used as follows
10150
\bprog
10151
RgX_shift_inplace_init(v);
10152
av = avma;
10153
...
10154
x = gerepileupto(av, ...); /* a t_POL */
10155
return RgX_shift_inplace(x, v);
10156
@eprog
10157
10158
\fun{long}{RgX_valrem}{GEN P, GEN *pz} returns the valuation $v$ of the
10159
\typ{POL}~\kbd{P} with respect to its main variable $X$. Check whether
10160
coefficients are $0$ using \kbd{isexactzero}. Set \kbd{*pz} to
10161
$\kbd{RgX\_shift\_shallow}(P,-v)$.
10162
10163
\fun{long}{RgX_val}{GEN P} returns the valuation $v$ of the
10164
\typ{POL}~\kbd{P} with respect to its main variable $X$. Check whether
10165
coefficients are $0$ using \kbd{isexactzero}.
10166
10167
\fun{long}{RgX_valrem_inexact}{GEN P, GEN *z} as \kbd{RgX\_valrem}, using
10168
\kbd{gequal0} instead of \kbd{isexactzero}.
10169
10170
\fun{long}{RgXV_maxdegree}{GEN V} returns the maximum of the degrees of
10171
the components of the vector of \typ{POL}s $V$.
10172
10173
\subsubsec{Basic arithmetic}
10174
10175
\fun{GEN}{RgX_add}{GEN x,GEN y} adds \kbd{x} and \kbd{y}.
10176
10177
\fun{GEN}{RgX_sub}{GEN x,GEN y} subtracts \kbd{x} and \kbd{y}.
10178
10179
\fun{GEN}{RgX_neg}{GEN x} returns $-\kbd{x}$.
10180
10181
\fun{GEN}{RgX_Rg_add}{GEN y, GEN x} returns $x+y$.
10182
10183
\fun{GEN}{RgX_Rg_add_shallow}{GEN y, GEN x} returns $x+y$; shallow function.
10184
10185
\fun{GEN}{Rg_RgX_sub}{GEN x, GEN y}
10186
10187
\fun{GEN}{RgX_Rg_sub}{GEN y, GEN x} returns $x-y$
10188
10189
\fun{GEN}{RgX_Rg_mul}{GEN y, GEN x} multiplies the \kbd{RgX} \kbd{y}
10190
by the scalar \kbd{x}.
10191
10192
\fun{GEN}{RgX_muls}{GEN y, long s} multiplies the \kbd{RgX} \kbd{y}
10193
by the \kbd{long}~\kbd{s}.
10194
10195
\fun{GEN}{RgX_Rg_div}{GEN y, GEN x} divides the \kbd{RgX} \kbd{y}
10196
by the scalar \kbd{x}.
10197
10198
\fun{GEN}{RgX_divs}{GEN y, long s} divides the \kbd{RgX} \kbd{y}
10199
by the \kbd{long}~\kbd{s}.
10200
10201
\fun{GEN}{RgX_Rg_divexact}{GEN x, GEN y} exact division of the \kbd{RgX}
10202
\kbd{y} by the scalar \kbd{x}.
10203
10204
\fun{GEN}{RgX_Rg_eval_bk}{GEN f, GEN x} returns $\kbd{f}(\kbd{x})$ using
10205
Brent and Kung algorithm. (Use \tet{poleval} for Horner algorithm.)
10206
10207
\fun{GEN}{RgX_RgV_eval}{GEN f, GEN V} as \kbd{RgX\_Rg\_eval\_bk(f, x)},
10208
assuming $V$ was output by \kbd{gpowers(x, n)} for some $n\geq 1$.
10209
10210
\fun{GEN}{RgXV_RgV_eval}{GEN f, GEN V} apply \kbd{RgX\_RgV\_eval\_bk(, V)}
10211
to all the components of the vector $f$.
10212
10213
\fun{GEN}{RgX_normalize}{GEN x} divides $x$ by its
10214
leading coefficient. If the latter is~$1$, $x$ itself is returned, not a
10215
copy. Leading coefficients equal to $0$ are stripped, e.g.
10216
\bprog
10217
0.*t^3 + Mod(0,3)*t^2 + 2*t
10218
@eprog\noindent is normalized to $t$.
10219
10220
\fun{GEN}{RgX_mul}{GEN x, GEN y} multiplies the two \typ{POL} (in the same
10221
variable) \kbd{x} and \kbd{y}. Detect the coefficient ring and use an
10222
appropriate algorithm.
10223
10224
\fun{GEN}{RgX_mul_i}{GEN x, GEN y} multiplies the two \typ{POL} (in the same
10225
variable) \kbd{x} and \kbd{y}. Do not detect the coefficient ring.
10226
Use a generic Karatsuba algorithm.
10227
10228
\fun{GEN}{RgX_mul_normalized}{GEN A, long a, GEN B, long b}
10229
returns $(X^a + A)(X^b + B) - X^(a+b)$, where we assume that $\deg A < a$
10230
and $\deg B < b$ are polynomials in the same variable $X$.
10231
10232
\fun{GEN}{RgX_sqr}{GEN x} squares the \typ{POL} \kbd{x}. Detect the coefficient
10233
ring and use an appropriate algorithm.
10234
10235
\fun{GEN}{RgX_sqr_i}{GEN x} squares the \typ{POL} \kbd{x}. Do not detect the
10236
coefficient ring. Use a generic Karatsuba algorithm.
10237
10238
\fun{GEN}{RgX_divrem}{GEN x, GEN y, GEN *r} by default, returns the Euclidean
10239
quotient and store the remainder in $r$. Three special values of $r$ change
10240
that behavior
10241
\item \kbd{NULL}: do not store the remainder, used to implement \kbd{RgX\_div},
10242
10243
\item \tet{ONLY_REM}: return the remainder, used to implement \kbd{RgX\_rem},
10244
10245
\item \tet{ONLY_DIVIDES}: return the quotient if the division is exact, and
10246
\kbd{NULL} otherwise.
10247
10248
\fun{GEN}{RgX_div}{GEN x, GEN y}
10249
10250
\fun{GEN}{RgX_div_by_X_x}{GEN A, GEN a, GEN *r} returns the
10251
quotient of the \kbd{RgX}~\kbd{A} by $(X - \kbd{a})$, and sets \kbd{r} to the
10252
remainder $\kbd{A}(\kbd{a})$.
10253
10254
\fun{GEN}{RgX_rem}{GEN x, GEN y}
10255
10256
\fun{GEN}{RgX_pseudodivrem}{GEN x, GEN y, GEN *ptr} compute a pseudo-quotient
10257
$q$ and pseudo-remainder $r$ such that $\kbd{lc}(y)^{\deg(x) - \deg(y) + 1}x
10258
= qy + r$. Return $q$ and set \kbd{*ptr} to $r$.
10259
10260
\fun{GEN}{RgX_pseudorem}{GEN x, GEN y} return the remainder
10261
in the pseudo-division of $x$ by $y$.
10262
10263
\fun{GEN}{RgXQX_pseudorem}{GEN x, GEN y, GEN T} return the remainder
10264
in the pseudo-division of $x$ by $y$ over $R[X]/(T)$.
10265
10266
\fun{int}{ZXQX_dvd}{GEN x, GEN y, GEN T} let $T$ be a monic irreducible
10267
\kbd{ZX}, let $x, y$ be \typ{POL} whose coefficients are either \typ{INT}s or
10268
\kbd{ZX} in the same variable as $T$. Assume further that the leading
10269
coefficient of $y$ is an integer. Return $1$ if $y | x$ in $(\Z[Y]/(T))[X]$,
10270
and $0$ otherwise.
10271
10272
\fun{GEN}{RgXQX_pseudodivrem}{GEN x, GEN y, GEN T, GEN *ptr} compute
10273
a pseudo-quotient $q$ and pseudo-remainder $r$ such that
10274
$\kbd{lc}(y)^{\deg(x) - \deg(y) + 1}x = qy + r$ in $R[X]/(T)$. Return $q$ and
10275
set \kbd{*ptr} to $r$.
10276
10277
\fun{GEN}{RgX_mulXn}{GEN a, long n} returns $a * X^n$. This may
10278
be a \typ{FRAC} if $n < 0$ and the valuation of $a$ is not large
10279
enough.
10280
10281
\fun{GEN}{RgX_addmulXn}{GEN a, GEN b, long n} returns $a + b * X^n$, assuming
10282
that $n > 0$.
10283
10284
\fun{GEN}{RgX_addmulXn_shallow}{GEN a, GEN b, long n} shallow
10285
variant of \tet{RgX_addmulXn}.
10286
10287
\fun{GEN}{RgX_digits}{GEN x, GEN B} returns a vector of \kbd{RgX}
10288
$[c_0,\ldots,c_n]$ of degree less than the degree of $B$ and such that
10289
$x=\sum_{i=0}^{n}{c_i\*B^i}$.
10290
10291
\subsubsec{Internal routines working on coefficient arrays}
10292
10293
These routines operate on coefficient blocks which are invalid \kbd{GEN}s
10294
A \kbd{GEN} argument $a$ or $b$ in routines below is actually a coefficient
10295
arrays representing the polynomials
10296
$\sum_{i = 0}^{\kbd{na-1}} a[i] X^i$ and
10297
$\sum_{i = 0}^{\kbd{nb-1}} b[i] X^i$. Note that $a[0]$ and $b[0]$ contain
10298
coefficients and not the mandatory \kbd{GEN} codeword. This allows to implement
10299
divide-and-conquer methods directly, without needing to allocate wrappers
10300
around coefficient blocks.
10301
10302
\fun{GEN}{RgX_mulspec}{GEN a, GEN b, long na, long nb}. Internal routine:
10303
given two coefficient arrays representing polynomials, return their product (as
10304
a true \kbd{GEN}) in variable $0$.
10305
10306
\fun{GEN}{RgX_sqrspec}{GEN a, long na}. Internal routine:
10307
given a coefficient array representing a polynomial r eturn its square (as a
10308
true \kbd{GEN}) in variable $0$.
10309
10310
\fun{GEN}{RgX_addspec}{GEN x, GEN y, long nx, long ny}
10311
given two coefficient arrays representing polynomials, return their sum (as a
10312
true \kbd{GEN}) in variable $0$.
10313
10314
\fun{GEN}{RgX_addspec_shallow}{GEN x, GEN y, long nx, long ny} shallow
10315
variant of \tet{RgX_addspec}.
10316
10317
\subsubsec{GCD, Resultant}
10318
10319
\fun{GEN}{RgX_gcd}{GEN x, GEN y} returns the GCD of \kbd{x} and \kbd{y},
10320
assumed to be \typ{POL}s in the same variable.
10321
10322
\fun{GEN}{RgX_gcd_simple}{GEN x, GEN y} as \tet{RgX_gcd} using a standard
10323
extended Euclidean algorithm. Usually slower than \tet{RgX_gcd}.
10324
10325
\fun{GEN}{RgX_extgcd}{GEN x, GEN y, GEN *u, GEN *v} returns
10326
$d = \text{GCD}(\kbd{x},\kbd{y})$, and sets \kbd{*u}, \kbd{*v} to the Bezout
10327
coefficients such that $\kbd{*ux} + \kbd{*vy} = d$. Uses a generic
10328
subresultant algorithm.
10329
10330
\fun{GEN}{RgX_extgcd_simple}{GEN x, GEN y, GEN *u, GEN *v} as
10331
\tet{RgX_extgcd} using a standard extended Euclidean algorithm. Usually
10332
slower than \tet{RgX_extgcd}.
10333
10334
\fun{GEN}{RgX_halfgcd}{GEN x, GEN y}
10335
assuming \kbd{x} and \kbd{y} are \typ{POL}s in the same variable,
10336
returns a $2$-components \typ{VEC} $[M,V]$ where $M$ is a $2\times 2$
10337
\typ{MAT} and $V$ a $2$-component \typ{COL}, both with \typ{POL} entries,
10338
such that $M*[x,y]~==V$ and such that f $V=[a,b]~$, then
10339
$\deg a \geq \ceil{\max(\deg x,\deg y))/2} > \deg b$.
10340
10341
\fun{GEN}{RgX_chinese_coprime}{GEN x, GEN y, GEN Tx, GEN Ty, GEN Tz}
10342
returns an \kbd{RgX}, congruent to \kbd{x} mod \kbd{Tx} and to \kbd{y} mod
10343
\kbd{Ty}. Assumes \kbd{Tx} and \kbd{Ty} are coprime, and \kbd{Tz = Tx * Ty}
10344
or \kbd{NULL} (in which case it is computed within).
10345
10346
\fun{GEN}{RgX_disc}{GEN x} returns the discriminant of the \typ{POL} \kbd{x}
10347
with respect to its main variable.
10348
10349
\fun{GEN}{RgX_resultant_all}{GEN x, GEN y, GEN *sol} returns
10350
\kbd{resultant(x,y)}. If \kbd{sol} is not \kbd{NULL}, sets it to the last
10351
nonconstant remainder in the polynomial remainder sequence if it exists and to
10352
\kbd{gen\_0} otherwise (e.g. one polynomial has degree 0).
10353
10354
\subsubsec{Other operations}
10355
10356
\fun{GEN}{RgX_gtofp}{GEN x, GEN prec} returns the polynomial obtained by
10357
applying
10358
\bprog
10359
gtofp(gel(x,i), prec)
10360
@eprog\noindent to all coefficients of $x$.
10361
10362
\fun{GEN}{RgX_fpnorml2}{GEN x, long prec} returns (a stack-clean variant of)
10363
\bprog
10364
gnorml2( RgX_gtofp(x, prec) )
10365
@eprog
10366
10367
\fun{GEN}{RgX_deriv}{GEN x} returns the derivative of \kbd{x} with respect to
10368
its main variable.
10369
10370
\fun{GEN}{RgX_integ}{GEN x} returns the primitive of \kbd{x} vanishing at
10371
$0$, with respect to its main variable.
10372
10373
\fun{GEN}{RgX_rescale}{GEN P, GEN h} returns $h^{\deg(P)} P(x/h)$.
10374
\kbd{P} is an \kbd{RgX} and \kbd{h} is nonzero. (Leaves small objects on the
10375
stack. Suitable but inefficient for \kbd{gerepileupto}.)
10376
10377
\fun{GEN}{RgX_unscale}{GEN P, GEN h} returns $P(h x)$. (Leaves small objects
10378
on the stack. Suitable but inefficient for \kbd{gerepileupto}.)
10379
10380
\fun{GEN}{RgXV_unscale}{GEN v, GEN h} apply \kbd{RgX\_unscale} to a vector
10381
of \kbd{RgX}.
10382
10383
\fun{GEN}{RgX_translate}{GEN P, GEN c} assume $c$ is a scalar or
10384
a polynomials whose main variable has lower priority than the main variable
10385
$X$ of $P$. Returns $P(X + c)$ (optimized for $c = \pm 1$).
10386
10387
\subsubsec{Function related to modular forms}
10388
10389
\fun{GEN}{RgX_act_Gl2Q}{GEN g, long k} let $R$ be a commutative ring
10390
and $g = [a,b;c,d]$ be in $\text{GL}_2(\Q)$, $g$ acts (on the left)
10391
on homogeneous polynomials of degree $k-2$ in $V := R[X,Y]_{k-2}$ via
10392
$$ g\cdot P := P(dX-cY, -bX+aY) = (\det g)^{k-2} P((X,Y)\cdot g^{-1}).$$
10393
This function returns the matrix in $M_{k-1}(R)$ of $P\mapsto g\cdot P$ in
10394
the basis $(X^{k-2},\dots,Y^{k-2})$ of $V$.
10395
10396
\fun{GEN}{RgX_act_ZGl2Q}{GEN z, long k} let $G:=\text{GL}_2(\Q)$, acting
10397
on $R[X,Y]_{k-2}$ and $z\in \Z[G]$. Return the matrix giving
10398
$P\mapsto z\cdot P$ in the basis $(X^{k-2},\dots,Y^{k-2})$.
10399
10400
\subsec{\kbd{RgXn}}
10401
10402
\fun{GEN}{RgXn_red_shallow}{GEN x, long n} return $\kbd{x \% } t^n$,
10403
where $n\geq 0$. Shallow function.
10404
10405
\fun{GEN}{RgXn_recip_shallow}{GEN P} returns $X^n\*P(1/X)$. Shallow function.
10406
10407
\fun{GEN}{RgXn_mul}{GEN a, GEN b, long n} returns $a b$ modulo $X^n$,
10408
where $a,b$ are two \typ{POL} in the same variable $X$ and $n \geq 0$. Uses
10409
Karatsuba algorithm (Mulders, Hanrot-Zimmermann variant).
10410
10411
\fun{GEN}{RgXn_sqr}{GEN a, long n} returns $a^2$ modulo $X^n$,
10412
where $a$ is a \typ{POL} in the variable $X$ and $n \geq 0$. Uses
10413
Karatsuba algorithm (Mulders, Hanrot-Zimmermann variant).
10414
10415
\fun{GEN}{RgX_mulhigh_i}{GEN f, GEN g, long n} return the Euclidean quotient
10416
of $f(x)*g(x)$ by $x^n$ (high product). Uses \tet{RgXn_mul} applied to
10417
the reciprocal polynomials of $f$ and $g$. Not suitable for \kbd{gerepile}.
10418
10419
\fun{GEN}{RgX_sqrhigh_i}{GEN f, long n} return the Euclidean quotient
10420
of $f(x)^2$ by $x^n$ (high product). Uses \tet{RgXn_sqr} applied to
10421
the reciprocal polynomial of $f$. Not suitable for \kbd{gerepile}.
10422
10423
\fun{GEN}{RgXn_inv}{GEN a, long n} returns $a^{-1}$ modulo $X^n$,
10424
where $a$ is a \typ{POL} in the variable $X$ and $n \geq 0$. Uses
10425
Newton-Raphson algorithm.
10426
10427
\fun{GEN}{RgXn_inv_i}{GEN a, long n} as \tet{RgXn_inv} without final garbage
10428
collection (suitable for \kbd{gerepileupto}).
10429
10430
\fun{GEN}{RgXn_powers}{GEN x, long m, long n} returns $[\kbd{x}^0,
10431
\dots, \kbd{x}^\kbd{m}]$ modulo $X^n$ as a \typ{VEC} of \kbd{RgXn}s.
10432
10433
\fun{GEN}{RgXn_powu}{GEN x, ulong m, long n} returns $x^m$ modulo
10434
$X^n$.
10435
10436
\fun{GEN}{RgXn_powu_i}{GEN x, ulong m, long n} as \tet{RgXn_powu},
10437
not memory clean.
10438
10439
\fun{GEN}{RgXn_sqrt}{GEN a, long n} returns $a^{1/2}$ modulo $X^n$,
10440
where $a$ is a \typ{POL} in the variable $X$ and $n \geq 0$.
10441
Assume that $a = 1 \mod{X}$. Uses Newton algorithm.
10442
10443
\fun{GEN}{RgXn_exp}{GEN a, long n} returns $exp(a)$ modulo $X^n$, assuming
10444
$a = 0 \mod{X}$.
10445
10446
\fun{GEN}{RgXn_expint}{GEN f, long n} return $\exp(F)$
10447
where $F$ is the primitive of $f$ that vanishes at $0$.
10448
10449
\fun{GEN}{RgXn_eval}{GEN Q, GEN x, long n} special case of
10450
\tet{RgX_RgXQ_eval}, when the modulus is a monomial:
10451
returns $\kbd{Q}(\kbd{x})$ modulo $t^n$, where $x \in R[t]$.
10452
10453
\fun{GEN}{RgX_RgXn_eval}{GEN f, GEN x, long n} returns $\kbd{f}(\kbd{x})$ modulo
10454
$X^n$.
10455
10456
\fun{GEN}{RgX_RgXnV_eval}{GEN f, GEN V, long n} as \kbd{RgX\_RgXn\_eval(f, x, n)},
10457
assuming $V$ was output by \kbd{RgXn\_powers(x, m, n)} for some $m\geq 1$.
10458
10459
\fun{GEN}{RgXn_reverse}{GEN f, long n} assuming that $f = a\*x \mod{x^2}$
10460
with $a$ invertible, returns a \typ{POL} $g$ of degree $< n$ such that $(g
10461
\circ f)(x) = x$ modulo $x^n$.
10462
10463
\subsec{\kbd{RgXnV}}
10464
10465
\fun{GEN}{RgXnV_red_shallow}{GEN x, long n} apply \kbd{RgXn\_red\_shallow}
10466
to all the components of the vector $x$.
10467
10468
\subsec{\kbd{RgXQ}}
10469
10470
\fun{GEN}{RgXQ_mul}{GEN y, GEN x, GEN T} computes $xy$ mod $T$
10471
10472
\fun{GEN}{RgXQ_sqr}{GEN x, GEN T} computes $x^2$ mod $T$
10473
10474
\fun{GEN}{RgXQ_inv}{GEN x, GEN T} return the inverse of $x$ mod $T$.
10475
10476
\fun{GEN}{RgXQ_pow}{GEN x, GEN n, GEN T} computes $x^n$ mod $T$
10477
10478
\fun{GEN}{RgXQ_powu}{GEN x, ulong n, GEN T} computes $x^n$ mod $T$,
10479
$n$ being an \kbd{ulong}.
10480
10481
\fun{GEN}{RgXQ_powers}{GEN x, long n, GEN T} returns $[\kbd{x}^0,
10482
\dots, \kbd{x}^\kbd{n}]$ as a \typ{VEC} of \kbd{RgXQ}s.
10483
10484
\fun{GEN}{RgXQ_matrix_pow}{GEN y, long n, long m, GEN P} returns
10485
\kbd{RgXQ\_powers(y,m-1,P)}, as a matrix of dimension $n \geq \deg P$.
10486
10487
\fun{GEN}{RgXQ_norm}{GEN x, GEN T} returns the norm of \kbd{Mod(x, T)}.
10488
10489
\fun{GEN}{RgXQ_trace}{GEN x, GEN T} returns the trace of \kbd{Mod(x, T)}.
10490
10491
\fun{GEN}{RgXQ_charpoly}{GEN x, GEN T, long v} returns the characteristic
10492
polynomial of \kbd{Mod(x, T)}, in variable $v$.
10493
10494
\fun{GEN}{RgX_RgXQ_eval}{GEN f, GEN x, GEN T} returns $\kbd{f}(\kbd{x})$ modulo
10495
$T$.
10496
10497
\fun{GEN}{RgX_RgXQV_eval}{GEN f, GEN V, GEN T} as \kbd{RgX\_RgXQ\_eval(f, x, T)},
10498
assuming $V$ was output by \kbd{RgXQ\_powers(x, n, T)} for some $n\geq 1$.
10499
10500
\fun{int}{RgXQ_ratlift}{GEN x, GEN T, long amax, long bmax, GEN *P, GEN *Q}
10501
Assuming that $\kbd{amax}+\kbd{bmax}<\deg T$, attempts to recognize $x$ as a
10502
rational function $a/b$, i.e. to find \typ{POL}s $P$ and $Q$ such that
10503
10504
\item $P \equiv Q x$ modulo $T$,
10505
10506
\item $\deg P \leq \kbd{amax}$, $\deg Q \leq \kbd{bmax}$,
10507
10508
\item $\gcd(T,P) = \gcd(P,Q)$.
10509
10510
\noindent If unsuccessful, the routine returns $0$ and leaves $P$, $Q$
10511
unchanged; otherwise it returns $1$ and sets $P$ and $Q$.
10512
10513
\fun{GEN}{RgXQ_reverse}{GEN f, GEN T} returns a \typ{POL} $g$ of degree $< n
10514
= \text{deg}~T$ such that $T(x)$ divides $(g \circ f)(x) - x$, by solving a
10515
linear system. Low-level function underlying \tet{modreverse}: it returns a
10516
lift of \kbd[modreverse(f,T)]; faster than the high-level function since it
10517
needs not compute the characteristic polynomial of $f$ mod $T$ (often already
10518
known in applications). In the trivial case where $n \leq 1$, returns a
10519
scalar, not a constant \typ{POL}.
10520
10521
\subsec{\kbd{RgXQV, RgXQC}}
10522
10523
\fun{GEN}{RgXQC_red}{GEN z, GEN T} \kbd{z} a vector whose
10524
coefficients are \kbd{RgX}s (arbitrary \kbd{GEN}s in fact), reduce them to
10525
\kbd{RgXQ}s (applying \kbd{grem} coefficientwise) in a \typ{COL}.
10526
10527
\fun{GEN}{RgXQV_red}{GEN z, GEN T} \kbd{z} a vector whose
10528
coefficients are \kbd{RgX}s (arbitrary \kbd{GEN}s in fact), reduce them to
10529
\kbd{RgXQ}s (applying \kbd{grem} coefficientwise) in a \typ{VEC}.
10530
10531
\fun{GEN}{RgXQV_RgXQ_mul}{GEN z, GEN x, GEN T} \kbd{z} multiplies the
10532
\kbd{RgXQV} \kbd{z} by the scalar (\kbd{RgXQ}) \kbd{x}.
10533
10534
\fun{GEN}{RgXQV_factorback}{GEN L, GEN e, GEN T} returns
10535
$\prod_i L_i^{e_i}$ mod $T$ where $L$ is a vector of \kbd{RgXQ}s and
10536
$e$ a vector of \typ{INT}s.
10537
10538
\subsec{\kbd{RgXQM}}
10539
10540
\fun{GEN}{RgXQM_red}{GEN z, GEN T} \kbd{z} a matrix whose
10541
coefficients are \kbd{RgX}s (arbitrary \kbd{GEN}s in fact), reduce them to
10542
\kbd{RgXQ}s (applying \kbd{grem} coefficientwise).
10543
10544
\fun{GEN}{RgXQM_mul}{GEN x, GEN y, GEN T}
10545
10546
\subsec{\kbd{RgXQX}}
10547
10548
\fun{GEN}{RgXQX_red}{GEN z, GEN T} \kbd{z} a \typ{POL} whose
10549
coefficients are \kbd{RgX}s (arbitrary \kbd{GEN}s in fact), reduce them to
10550
\kbd{RgXQ}s (applying \kbd{grem} coefficientwise).
10551
10552
\fun{GEN}{RgXQX_mul}{GEN x, GEN y, GEN T}
10553
10554
\fun{GEN}{RgXQX_RgXQ_mul}{GEN x, GEN y, GEN T} multiplies the \kbd{RgXQX}
10555
\kbd{y} by the scalar (\kbd{RgXQ}) \kbd{x}.
10556
10557
\fun{GEN}{RgXQX_sqr}{GEN x, GEN T}
10558
10559
\fun{GEN}{RgXQX_powers}{GEN x, long n, GEN T}
10560
10561
\fun{GEN}{RgXQX_divrem}{GEN x, GEN y, GEN T, GEN *pr}
10562
10563
\fun{GEN}{RgXQX_div}{GEN x, GEN y, GEN T, GEN *r}
10564
10565
\fun{GEN}{RgXQX_rem}{GEN x, GEN y, GEN T, GEN *r}
10566
10567
\fun{GEN}{RgXQX_translate}{GEN P, GEN c, GEN T} assume the main variable
10568
$X$ of $P$ has higher priority than the main variable $Y$ of $T$ and $c$.
10569
Return a lift of $P(X+\text{Mod}(c(Y), T(Y)))$.
10570
10571
\fun{GEN}{Kronecker_to_mod}{GEN z, GEN T} $z\in R[X]$ represents an element
10572
$P(X,Y)$ in $R[X,Y]$ mod $T(Y)$ in Kronecker form, i.e. $z = P(X,X^{2*n-1})$
10573
10574
Let $R$ be some commutative ring, $n = \deg T$ and let $P(X,Y)\in R[X,Y]$ lift
10575
a polynomial in $K[Y]$, where $K := R[X]/(T)$ and $\deg_X P < 2n-1$ --- such as
10576
would result from multiplying minimal degree lifts of two polynomials in
10577
$K[Y]$. Let $z = P(t,t^{2*n-1})$ be a Kronecker form of $P$, this function
10578
returns the image of $P(X,t)$ in $K[t]$, with \typ{POLMOD} coefficients.
10579
Not stack-clean. Note that $t$ need not be the same variable as $Y$!
10580
10581
\chapter{Black box algebraic structures}
10582
10583
The generic routines like \kbd{gmul} or \kbd{gadd} allow handling objects
10584
belonging to a fixed list of basic types, with some natural polymorphism
10585
(you can mix rational numbers and polynomials, etc.), at the expense of
10586
efficiency and sometimes of clarity when the recursive structure becomes
10587
complicated, e.g. a few levels of \typ{POLMOD}s attached to different
10588
polynomials and variable numbers for quotient structures. This
10589
is the only possibility in GP.
10590
10591
On the other hand, the Level 2 Kernel allows dedicated routines to handle
10592
efficiently objects of a very specific type, e.g. polynomials with
10593
coefficients in the same finite field. This is more efficient, but imvolves a
10594
lot of code duplication since polymorphism is no longer possible.
10595
10596
A third and final option, still restricted to library programming, is to
10597
define an arbitrary algebraic structure (currently groups, fields, rings,
10598
algebras and $\Z_p$-modules) by providing suitable methods, then using generic
10599
algorithms. For instance naive Gaussian pivoting applies over all base fields
10600
and need only be implemented once. The difference with the first solution
10601
is that we no longer depend on the way functions like \kbd{gmul} or
10602
\kbd{gadd} will guess what the user is trying to do. We can then implement
10603
independently various groups / fields / algebras in a clean way.
10604
10605
\section{Black box groups}
10606
10607
A black box group is defined by a \tet{bb_group} struct, describing methods
10608
available to handle group elements:
10609
\bprog
10610
struct bb_group
10611
{
10612
GEN (*mul)(void*, GEN, GEN);
10613
GEN (*pow)(void*, GEN, GEN);
10614
GEN (*rand)(void*);
10615
ulong (*hash)(GEN);
10616
int (*equal)(GEN, GEN);
10617
int (*equal1)(GEN);
10618
GEN (*easylog)(void *E, GEN, GEN, GEN);
10619
};
10620
@eprog
10621
\kbd{mul(E,x,y)} returns the product $x\*y$.
10622
10623
\kbd{pow(E,x,n)} returns $x^n$ ($n$ integer, possibly negative or zero).
10624
10625
\kbd{rand(E)} returns a random element in the group.
10626
10627
\kbd{hash(x)} returns a hash value for $x$ (\kbd{hash\_GEN} is suitable for this field).
10628
10629
\kbd{equal(x,y)} returns one if $x=y$ and zero otherwise.
10630
10631
\kbd{equal1(x)} returns one if $x$ is the neutral element in the group,
10632
and zero otherwise.
10633
10634
\kbd{easylog(E,a,g,o)} (optional) returns either NULL or the discrete logarithm
10635
$n$ such that $g^n=a$, the element $g$ being of order $o$. This provides a
10636
short-cut in situation where a better algorithm than the generic one is known.
10637
10638
A group is thus described by a \kbd{struct bb\_group} as above and auxiliary
10639
data typecast to \kbd{void*}. The following functions operate on black box
10640
groups:
10641
10642
\fun{GEN}{gen_Shanks_log}{GEN x, GEN g, GEN N, void *E, const struct bb_group
10643
*grp} \hbadness 10000\break
10644
Generic baby-step/giant-step algorithm (Shanks's method). Assuming
10645
that $g$ has order $N$, compute an integer $k$ such that $g^k = x$.
10646
Return \kbd{cgetg(1, t\_VEC)} if there are no solutions. This requires
10647
$O(\sqrt{N})$ group operations and uses an auxiliary table containing
10648
$O(\sqrt{N})$ group elements.
10649
10650
The above is useful for a one-shot computation. If many discrete logs
10651
are desired:
10652
\fun{GEN}{gen_Shanks_init}{GEN g, long n, void *E, const struct bb_group *grp}
10653
return an auxiliary data structure $T$ required to compute a discrete log in
10654
base $g$. Compute and store all powers $g^i$, $i < n$.
10655
10656
\fun{GEN}{gen_Shanks}{GEN T, GEN x, ulong N, void *E, const struct bb_group *grp}
10657
Let $T$ be computed by \tet{gen_Shanks_init}$(g,n,\dots)$.
10658
Return $k < n N$ such that $g^k = x$ or \kbd{NULL} if no such index exist.
10659
It uses $O(N)$ operation in the group and fast table lookups (in time
10660
$O(\log n)$). The interface is such that the function may be used when the
10661
order of the base $g$ is unknown, and hence compute it given only an upper
10662
bound $B$ for it: e.g. choose $n,N$ such that $nN \geq B$ and compute the
10663
discrete log $l$ of $g^{-1}$ in base $g$, then use \tet{gen_order}
10664
with multiple $N = l+1$.
10665
10666
\fun{GEN}{gen_Pollard_log}{GEN x, GEN g, GEN N, void *E, const struct bb_group
10667
*grp} \hbadness 10000\break
10668
Generic Pollard rho algorithm. Assuming that $g$ has order $N$, compute an
10669
integer $k$ such that $g^k = x$. This requires $O(\sqrt{N})$ group operations
10670
in average and $O(1)$ storage. Will enter an infinite loop if there are no
10671
solutions.
10672
10673
\fun{GEN}{gen_plog}{GEN x, GEN g, GEN N, void *E, const struct bb_group}
10674
Assuming that $g$ has prime order $N$, compute an integer $k$ such that
10675
$g^k = x$, using either \kbd{gen\_Shanks\_log} or \kbd{gen\_Pollard\_log}.
10676
Return \kbd{cgetg(1, t\_VEC)} if there are no solutions.
10677
10678
\fun{GEN}{gen_Shanks_sqrtn}{GEN a, GEN n, GEN N, GEN *zetan, void *E, const
10679
struct bb_group *grp} \hbadness 10000 returns one solution of $x^n = a$ in a
10680
black box cyclic group of order $N$. Return \kbd{NULL} if no solution exists.
10681
If \kbd{zetan} is not \kbd{NULL} it is set to an element of exact order $n$.
10682
This function uses \kbd{gen\_plog} for all prime divisors of $\gcd(n,N)$.
10683
10684
\fun{GEN}{gen_PH_log}{GEN a, GEN g, GEN N, void *E, const struct bb_group *grp}
10685
returns an integer $k$ such that $g^k = x$, assuming that the order of $g$
10686
divides $N$, using Pohlig-Hellman algorithm. Return \kbd{cgetg(1, t\_VEC)} if
10687
there are no solutions. This calls \tet{gen_plog} repeatedly for all prime
10688
divisors $p$ of $N$.
10689
10690
In the following functions the integer parameter \kbd{ord} can be given
10691
in all the formats recognized for the argument of arithmetic functions,
10692
i.e.~either as a positive \typ{INT} $N$, or as its factorization matrix
10693
$\var{faN}$, or (preferred) as a pair $[N,\var{faN}]$.
10694
10695
\fun{GEN}{gen_order}{GEN x, GEN ord, void *E, const struct bb_group *grp}
10696
computes the order of $x$; \kbd{ord} is a multiple of the order, for instance
10697
the group order.
10698
10699
\fun{GEN}{gen_factored_order}{GEN x, GEN ord, void *E, const struct bb_group
10700
*grp} returns a pair $[o,F]$, where $o$ is the order of $x$ and $F$ is the
10701
factorization of $o$; \kbd{ord} is as in \tet{gen_order}.
10702
10703
\fun{GEN}{gen_gener}{GEN ord, void *E, const struct bb_group *grp}
10704
returns a random generator of the group, assuming it is of order exactly
10705
\kbd{ord}.
10706
10707
\fun{GEN}{get_arith_Z}{GEN ord} given \kbd{ord} as above in one of the
10708
formats recognized for arithmetic functions, i.e. a positive
10709
\typ{INT} $N$, its factorization \var{faN}, or the pair $[N, \var{faN}]$,
10710
return $N$.
10711
10712
\fun{GEN}{get_arith_ZZM}{GEN ord} given \kbd{ord} as above,
10713
return the pair $[N, \var{faN}]$. This may require factoring $N$.
10714
10715
\fun{GEN}{gen_select_order}{GEN v, void *E, const struct bb_group *grp}
10716
Let $v$ be a vector of possible orders for the group; try to find the true
10717
order by checking orders of random points. This will not terminate if there
10718
is an ambiguity.
10719
10720
\subsec{Black box groups with pairing}
10721
10722
These functions handle groups of rank at most $2$ equipped with a family of
10723
bilinear pairings which behave like the Weil pairing on elliptic curves over
10724
finite field. In the descriptions below, the function \kbd{pairorder(E, P, Q,
10725
m, F)} must return the order of the $m$-pairing of $P$ and $Q$, both of order
10726
dividing $m$, where $F$ is the factorization matrix of a multiple of $m$.
10727
10728
\fun{GEN}{gen_ellgroup}{GEN o, GEN d, GEN *pt_m, void *E, const struct bb_group *grp,
10729
GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F)}
10730
returns the elementary divisors $[d_1, d_2]$ of the group, assuming it is of
10731
order exactly $o>1$, and that $d_2$ divides $d$. If $d_2=1$ then $[o]$ is
10732
returned, otherwise \kbd{m=*pt\_m} is set to the order of the pairing
10733
required to verify a generating set which is to be used with
10734
\kbd{gen\_ellgens}. For the parameter $o$, all formats recognized by
10735
arithmetic functions are allowed, preferably a factorization matrix or a pair
10736
$[n,\kbd{factor}(n)]$.
10737
10738
\fun{GEN}{gen_ellgens}{GEN d1, GEN d2, GEN m, void *E, const struct bb_group *grp,
10739
GEN pairorder(void *E, GEN P, GEN Q, GEN m, GEN F)}
10740
the parameters $d_1$, $d_2$, $m$ being as returned by \kbd{gen\_ellgroup},
10741
returns a pair of generators $[P,Q]$ such that $P$ is of order $d_1$ and the
10742
$m$-pairing of $P$ and $Q$ is of order $m$. (Note: $Q$ needs not be of order
10743
$d_2$). For the parameter $d_1$, all formats recognized by arithmetic
10744
functions are allowed, preferably a factorization matrix or a pair
10745
$[n,\kbd{factor}(n)]$.
10746
10747
\subsec{Functions returning black box groups}
10748
10749
\fun{const struct bb_group *}{get_Flxq_star}{void **E, GEN T, ulong p}
10750
10751
\fun{const struct bb_group *}{get_FpXQ_star}{void **E, GEN T, GEN p}
10752
returns a pointer to the black box group $(\F_p[x]/(T))^*$.
10753
10754
\fun{const struct bb_group *}{get_FpE_group}{void **pE, GEN a4, GEN a6, GEN p}
10755
returns a pointer to a black box group and set \kbd{*pE} to the necessary data for
10756
computing in the group $E(\F_p)$ where $E$ is the elliptic curve $E:y^2=x^3+a_4\*x+a_6$,
10757
with $a_4$ and $a_6$ in $\F_p$.
10758
10759
\fun{const struct bb_group *}{get_FpXQE_group}{void **pE, GEN a4, GEN a6, GEN T, GEN p}
10760
returns a pointer to a black box group and set \kbd{*pE} to the necessary data for
10761
computing in the group $E(\F_p[X]/(T))$ where $E$ is the elliptic curve $E:y^2=x^3+a_4\*x+a_6$,
10762
with $a_4$ and $a_6$ in $\F_p[X]/(T)$.
10763
10764
\fun{const struct bb_group *}{get_FlxqE_group}{void **pE, GEN a4, GEN a6, GEN
10765
T, ulong p} idem for small $p$.
10766
10767
\fun{const struct bb_group *}{get_F2xqE_group}{void **pE, GEN a2, GEN a6, GEN T}
10768
idem for $p=2$.
10769
10770
\section{Black box fields}
10771
10772
A black box field is defined by a \tet{bb_field} struct, describing methods
10773
available to handle field elements:
10774
\bprog
10775
struct bb_field
10776
{
10777
GEN (*red)(void *E ,GEN);
10778
GEN (*add)(void *E ,GEN, GEN);
10779
GEN (*mul)(void *E ,GEN, GEN);
10780
GEN (*neg)(void *E ,GEN);
10781
GEN (*inv)(void *E ,GEN);
10782
int (*equal0)(GEN);
10783
GEN (*s)(void *E, long);
10784
};
10785
@eprog\noindent In contrast of black box group, elements can have
10786
non canonical forms, and only \kbd{red} is required to return a canonical form.
10787
For instance a black box implementation of finite fields, all methods
10788
except \kbd{red} may return arbitrary representatives in $\Z[X]$ of the
10789
correct congruence class modulo $(p,T(X))$.
10790
10791
\kbd{red(E,x)} returns the canonical form of $x$.
10792
10793
\kbd{add(E,x,y)} returns the sum $x+y$.
10794
10795
\kbd{mul(E,x,y)} returns the product $x\*y$.
10796
10797
\kbd{neg(E,x)} returns $-x$.
10798
10799
\kbd{inv(E,x)} returns the inverse of $x$.
10800
10801
\kbd{equal0(x)} $x$ being in canonical form, returns one if $x=0$ and zero
10802
otherwise.
10803
10804
\kbd{s(n)} $n$ being a small signed integer, returns $n$ times the unit element.
10805
10806
\noindent A field is thus described by a \kbd{struct bb\_field} as above and
10807
auxiliary data typecast to \kbd{void*}. The following functions operate on
10808
black box fields:
10809
10810
\fun{GEN}{gen_Gauss}{GEN a, GEN b, void *E, const struct bb_field *ff}
10811
10812
\fun{GEN}{gen_Gauss_pivot}{GEN x, long *rr, void *E, const struct bb_field *ff}
10813
10814
\fun{GEN}{gen_det}{GEN a, void *E, const struct bb_field *ff}
10815
10816
\fun{GEN}{gen_ker}{GEN x, long deplin, void *E, const struct bb_field *ff}
10817
10818
\fun{GEN}{gen_matcolinvimage}{GEN a, GEN b, void *E, const struct bb_field *ff}
10819
10820
\fun{GEN}{gen_matcolmul}{GEN a, GEN b, void *E, const struct bb_field *ff}
10821
10822
\fun{GEN}{gen_matid}{long n, void *E, const struct bb_field *ff}
10823
10824
\fun{GEN}{gen_matinvimage}{GEN a, GEN b, void *E, const struct bb_field *ff}
10825
10826
\fun{GEN}{gen_matmul}{GEN a, GEN b, void *E, const struct bb_field *ff}
10827
10828
\subsec{Functions returning black box fields}
10829
10830
\fun{const struct bb_field *}{get_Fp_field}{void **pE, GEN p}
10831
10832
\fun{const struct bb_field *}{get_Fq_field}{void **pE, GEN T, GEN p}
10833
10834
\fun{const struct bb_field *}{get_Flxq_field}{void **pE, GEN T, ulong p}
10835
10836
\fun{const struct bb_field *}{get_F2xq_field}{void **pE, GEN T}
10837
10838
\fun{const struct bb_field *}{get_nf_field}{void **pE, GEN nf}
10839
10840
\section{Black box algebra}
10841
10842
A black box algebra is defined by a \tet{bb_algebra} struct, describing methods
10843
available to handle algebra elements:
10844
\bprog
10845
struct bb_algebra
10846
{
10847
GEN (*red)(void *E, GEN x);
10848
GEN (*add)(void *E, GEN x, GEN y);
10849
GEN (*sub)(void *E, GEN x, GEN y);
10850
GEN (*mul)(void *E, GEN x, GEN y);
10851
GEN (*sqr)(void *E, GEN x);
10852
GEN (*one)(void *E);
10853
GEN (*zero)(void *E);
10854
};
10855
@eprog\noindent In contrast with black box groups, elements can have non
10856
canonical forms, but only \kbd{add} is allowed to return a non canonical
10857
form.
10858
10859
\kbd{red(E,x)} returns the canonical form of $x$.
10860
10861
\kbd{add(E,x,y)} returns the sum $x+y$.
10862
10863
\kbd{sub(E,x,y)} returns the difference $x-y$.
10864
10865
\kbd{mul(E,x,y)} returns the product $x\*y$.
10866
10867
\kbd{sqr(E,x)} returns the square $x^2$.
10868
10869
\kbd{one(E)} returns the unit element.
10870
10871
\kbd{zero(E)} returns the zero element.
10872
10873
\noindent An algebra is thus described by a \kbd{struct bb\_algebra} as above
10874
and auxiliary data typecast to \kbd{void*}. The following functions operate
10875
on black box algebra:
10876
10877
\fun{GEN}{gen_bkeval}{GEN P, long d, GEN x, int use_sqr, void *E,
10878
const struct bb_algebra *ff, GEN cmul(void *E, GEN P, long a, GEN x)}
10879
$x$ being an element of the black box algebra, and $P$ some black box
10880
polynomial of degree $d$ over the base field, returns $P(x)$. The function
10881
\kbd{cmul(E,P,a,y)} must return the coefficient of degree $a$ of $P$
10882
multiplied by $y$. \kbd{cmul} is allowed to return a non canonical form;
10883
it is also allowed to return \kbd{NULL} instead of an exact $0$.
10884
10885
The flag \kbd{use\_sqr} has the same meaning as for \kbd{gen\_powers}. This
10886
implements an algorithm of Brent and Kung (1978).
10887
10888
\fun{GEN}{gen_bkeval_powers}{GEN P, long d, GEN V, void *E,
10889
const struct bb_algebra *ff, GEN cmul(void *E, GEN P, long a, GEN x)}
10890
as \tet{gen_RgX_bkeval} assuming $V$ was output by
10891
\tet{gen_powers}$(x, l, E, \var{ff})$ for some $l\geq 1$. For optimal
10892
performance, $l$ should be computed by \tet{brent_kung_optpow}.
10893
10894
\fun{long}{brent_kung_optpow}{long d, long n, long m} returns the optimal
10895
parameter $l$ for the evaluation of $n/m$ polynomials of degree $d$.
10896
Fractional values can be used if the evaluations are done with different
10897
accuracies, and thus have different weights.
10898
10899
\subsec{Functions returning black box algebras}
10900
10901
\fun{const struct bb_algebra *}{get_FpX_algebra}{void **E, GEN p, long v}
10902
return the algebra of polynomials over $\F_p$ in variable $v$.
10903
10904
\fun{const struct bb_algebra *}{get_FpXQ_algebra}{void **E, GEN T, GEN p}
10905
return the algebra $\F_p[X]/(T(X))$.
10906
10907
\fun{const struct bb_algebra *}{get_FpXQX_algebra}{void **E, GEN T, GEN p, long v}
10908
return the algebra of polynomials over $\F_p[X]/(T(X))$ in variable $v$.
10909
10910
\fun{const struct bb_algebra *}{get_FlxqXQ_algebra}{void **E, GEN S, GEN T, ulong p}
10911
return the algebra $\F_p[X,Y]/(S(X,Y),T(X))$ (for \kbd{ulong} $p$).
10912
10913
\fun{const struct bb_algebra *}{get_FpXQXQ_algebra}{void **E, GEN S, GEN T, GEN p}
10914
return the algebra $\F_p[X,Y]/(S(X,Y),T(X))$.
10915
10916
\fun{const struct bb_algebra *}{get_Rg_algebra}{void}
10917
return the generic algebra.
10918
10919
\section{Black box ring}
10920
10921
A black box ring is defined by a \tet{bb_ring} struct, describing methods
10922
available to handle ring elements:
10923
\bprog
10924
struct bb_ring
10925
{
10926
GEN (*add)(void *E, GEN x, GEN y);
10927
GEN (*mul)(void *E, GEN x, GEN y);
10928
GEN (*sqr)(void *E, GEN x);
10929
};
10930
@eprog
10931
10932
\kbd{add(E,x,y)} returns the sum $x+y$.
10933
10934
\kbd{mul(E,x,y)} returns the product $x\*y$.
10935
10936
\kbd{sqr(E,x)} returns the square $x^2$.
10937
10938
\fun{GEN}{gen_fromdigits}{GEN v, GEN B, void *E, struct bb_ring *r}
10939
where $B$ is a ring element and $v=[c_0,\ldots,c_{n-1}]$ a vector of ring elements,
10940
return $\sum_{i=0}^n c_i\*B^i$ using binary splitting.
10941
10942
\fun{GEN}{gen_digits}{GEN x, GEN B, long n, void *E, struct bb_ring *r,
10943
GEN (*div)(void *E, GEN x, GEN y, GEN *r)}
10944
10945
(Require the ring to be Euclidean)
10946
10947
\kbd{div(E,x,y,\&r)} performs the Euclidean division of $x$ by $y$ in the ring
10948
$R$, returning the quotient $q$ and setting $r$ to the residue so that
10949
$x=q\*y+r$ holds. The residue must belong to a fixed set of representatives of
10950
$R/(y)$.
10951
10952
The argument $x$ being a ring element, \kbd{gen\_digits} returns a vector of
10953
ring elements $[c_0,\ldots,c_{n-1}]$ such that $x = \sum_{i=0}^n c_i\*B^i$.
10954
Furthermore for all $i\ne n-1$, the elements $c_i$ belonging to the fixed set
10955
of representatives of $R/(B)$.
10956
10957
\section{Black box free $\Z_p$-modules}
10958
10959
(Very experimental)
10960
10961
\fun{GEN}{gen_ZpX_Dixon}{GEN F, GEN V, GEN q, GEN p, long N, void *E,
10962
GEN lin(void *E, GEN F, GEN z, GEN q),
10963
GEN invl(void *E, GEN z)}
10964
10965
Let $F$ be a \kbd{ZpXT} representing the coefficients of some abstract
10966
linear mapping $f$ over $\Z_p[X]$ seen as a free $\Z_p$-module, let $V$ be
10967
an element of $\Z_p[X]$ and let $q = p^N$. Return $y\in\Z_p[X]$ such that
10968
$f(y)=V\pmod{p^N}$ assuming the following holds for $n\leq N$:
10969
10970
\item $\kbd{lin}(E, \kbd{FpX\_red}(F, p^n), z, p^n) \equiv f(z) \pmod{p^n}$
10971
10972
\item $f(\kbd{invl}(E, z)) \equiv z \pmod{p}$
10973
10974
The rationale for the argument $F$ being that it allows \kbd{gen\_ZpX\_Dixon}
10975
to reduce it to the required $p$-adic precision.
10976
10977
\fun{GEN}{gen_ZpX_Newton}{GEN x, GEN p, long n, void *E,
10978
GEN eval(void *E, GEN a, GEN q),
10979
GEN invd(void *E, GEN b, GEN v, GEN q, long N)}
10980
10981
Let $x$ be an element of $\Z_p[X]$ seen as a free $\Z_p$-module, and $f$
10982
some differentiable function over $\Z_p[X]$ such that $f(x) \equiv 0
10983
\pmod{p}$. Return $y$ such that $f(y) \equiv 0\pmod{p^n}$, assuming the
10984
following holds for all $a, b\in \Z_p[X]$ and $M\leq N$:
10985
10986
\item $v = \kbd{eval}(E,a,p^N)$ is a vector of elements of $\Z_p[X]$,
10987
10988
\item $w = \kbd{invd}(E,b,v,p^M,M)$ is an element in $\Z_p[X]$,
10989
10990
\item $v[1] \equiv f(a) \pmod{p^N\Z_p[X]}$,
10991
10992
\item $df_a(w) \equiv b \pmod{p^M\Z_p[X]}$
10993
10994
\noindent and $df_a$ denotes the differential of $f$ at $a$. Motivation:
10995
\kbd{eval} allows to evaluate $f$ and \kbd{invd} allows to invert its
10996
differential. Frequently, data useful to compute the differential appear as a
10997
subproduct of computing the function. The vector $v$ allows \kbd{eval} to
10998
provide these to \kbd{invd}. The implementation of \kbd{invd} will generally
10999
involves the use of the function \kbd{gen\_ZpX\_Dixon}.
11000
11001
\fun{GEN}{gen_ZpM_Newton}{GEN x, GEN p, long n, void *E,
11002
GEN eval(void *E, GEN a, GEN q),
11003
GEN invd(void *E, GEN b, GEN v, GEN q, long N)}
11004
as above, with polynomials replaced by matrices.
11005
11006
\newpage
11007
\chapter{Operations on general PARI objects}
11008
11009
\section{Assignment}
11010
11011
It is in general easier to use a direct conversion,
11012
e.g.~\kbd{y = stoi(s)}, than to allocate a target of correct type and
11013
sufficient size, then assign to it:
11014
\bprog
11015
GEN y = cgeti(3); affsi(s, y);
11016
@eprog\noindent
11017
These functions can still be moderately useful in complicated garbage
11018
collecting scenarios but you will be better off not using them.
11019
11020
\fun{void}{gaffsg}{long s, GEN x} assigns the \kbd{long}~\kbd{s} into the
11021
object~\kbd{x}.
11022
11023
\fun{void}{gaffect}{GEN x, GEN y} assigns the object \kbd{x} into the
11024
object~\kbd{y}. Both \kbd{x} and \kbd{y} must be scalar types. Type
11025
conversions (e.g.~from \typ{INT} to \typ{REAL} or \typ{INTMOD}) occur if
11026
legitimate.
11027
11028
\fun{int}{is_universal_constant}{GEN x} returns $1$ if $x$ is a global PARI
11029
constant you should never assign to (such as \kbd{gen\_1}), and $0$
11030
otherwise.
11031
11032
\section{Conversions}
11033
11034
\subsec{Scalars}
11035
11036
\fun{double}{rtodbl}{GEN x} applied to a \typ{REAL}~\kbd{x}, converts \kbd{x}
11037
into a \kbd{double} if possible.
11038
11039
\fun{GEN}{dbltor}{double x} converts the \kbd{double} \kbd{x} into a
11040
\typ{REAL}.
11041
11042
\fun{long}{dblexpo}{double x} returns \kbd{expo(dbltor(x))}, but
11043
faster and without cluttering the stack.
11044
11045
\fun{ulong}{dblmantissa}{double x} returns the most significant word
11046
in the mantissa of \kbd{dbltor(x)}.
11047
11048
\fun{int}{gisdouble}{GEN x} if \kbd{x} is a real number (not necessarily
11049
a~\typ{REAL}), return $1$ if \kbd{x} can be converted to a \kbd{double},
11050
$0$ otherwise.
11051
11052
\fun{double}{gtodouble}{GEN x} if \kbd{x} is a real number (not necessarily
11053
a~\typ{REAL}), converts \kbd{x} into a \kbd{double} if possible.
11054
11055
\fun{long}{gtos}{GEN x} converts the \typ{INT} \kbd{x} to a small
11056
integer if possible, otherwise raise an exception. This function
11057
is similar to \tet{itos}, slightly slower since it checks the type of \kbd{x}.
11058
11059
\fun{ulong}{gtou}{GEN x} converts the non-negative \typ{INT} \kbd{x} to
11060
an unsigned small integer if possible, otherwise raise an exception. This
11061
function is similar to \tet{itou}, slightly slower since it checks the type
11062
of \kbd{x}.
11063
11064
\fun{double}{dbllog2r}{GEN x} assuming that \kbd{x} is a nonzero \typ{REAL},
11065
returns an approximation to \kbd{log2(|x|)}.
11066
11067
\fun{double}{dblmodulus}{GEN x} return an approximation to \kbd{|x|}.
11068
11069
\fun{long}{gtolong}{GEN x} if \kbd{x} is an integer (not necessarily
11070
a~\typ{INT}), converts \kbd{x} into a \kbd{long} if possible.
11071
11072
\fun{GEN}{fractor}{GEN x, long l} applied to a \typ{FRAC}~\kbd{x}, converts
11073
\kbd{x} into a \typ{REAL} of length \kbd{prec}.
11074
11075
\fun{GEN}{quadtofp}{GEN x, long l} applied to a \typ{QUAD}~\kbd{x}, converts
11076
\kbd{x} into a \typ{REAL} or \typ{COMPLEX} depending on the sign of the
11077
discriminant of~\kbd{x}, to precision \hbox{\kbd{l} \B-bit} words.
11078
% forbid line brk at hyphen here [GN]
11079
11080
\fun{GEN}{upper_to_cx}{GEN x, long *prec} valid for a \typ{COMPLEX}
11081
or \typ{QUAD} belonging to the upper half-plane. If a \typ{QUAD}, convert it
11082
to \typ{COMPLEX} using accuracy \kbd{*prec}. If $x$ is inexact, sets
11083
\kbd{*prec} to the precision of $x$.
11084
11085
\fun{GEN}{cxtofp}{GEN x, long prec} converts the \typ{COMPLEX}~\kbd{x} to a
11086
a complex whose real and imaginary parts are \typ{REAL} of length \kbd{prec}
11087
(special case of~\kbd{gtofp}.
11088
11089
\fun{GEN}{cxcompotor}{GEN x, long prec} converts the
11090
\typ{INT}, \typ{REAL} or \typ{FRAC} $x$ to a \typ{REAL} of length \kbd{prec}.
11091
These are all the real types which may occur as components of a
11092
\typ{COMPLEX}; special case of~\kbd{gtofp} (introduced so that the latter is
11093
not recursive and can thus be inlined).
11094
11095
\fun{GEN}{cxtoreal}{GEN x} converts the complex (\typ{INT}, \typ{REAL},
11096
\typ{FRAC} or \typ{COMPLEX}) $x$ to a real number if its imaginary part is 0.
11097
Shallow function.
11098
11099
converts the \typ{COMPLEX}~\kbd{x} to a
11100
a complex whose real and imaginary parts are \typ{REAL} of length \kbd{prec}
11101
(special case of~\kbd{gtofp}.
11102
11103
\fun{GEN}{gtofp}{GEN x, long prec} converts the complex number~\kbd{x}
11104
(\typ{INT}, \typ{REAL}, \typ{FRAC}, \typ{QUAD} or \typ{COMPLEX}) to either
11105
a \typ{REAL} or \typ{COMPLEX} whose components are \typ{REAL} of precision
11106
\kbd{prec}; not necessarily of \emph{length} \kbd{prec}: a real $0$ may be
11107
given as \kbd{real\_0(...)}). If the result is a \typ{COMPLEX} extra care is
11108
taken so that its modulus really has accuracy \kbd{prec}: there is a problem
11109
if the real part of the input is an exact $0$; indeed, converting it to
11110
\kbd{real\_0(prec)} would be wrong if the imaginary part is tiny, since the
11111
modulus would then become equal to $0$, as in $1.E-100 + 0.E-28 = 0.E-28$.
11112
11113
\fun{GEN}{gtomp}{GEN z, long prec} converts the real number~\kbd{x}
11114
(\typ{INT}, \typ{REAL}, \typ{FRAC}, real \typ{QUAD}) to either
11115
a \typ{INT} or a \typ{REAL} of precision \kbd{prec}. Not memory clean
11116
if $x$ is a \typ{INT}: we return $x$ itself and not a copy.
11117
11118
\fun{GEN}{gcvtop}{GEN x, GEN p, long l} converts $x$ into a \typ{PADIC}
11119
of precision~$l$. Works componentwise on recursive objects,
11120
e.g.~\typ{POL} or \typ{VEC}. Converting $0$ yields $O(p^l)$; converting a
11121
nonzero number yield a result well defined modulo $p^{v_p(x) + l}$.
11122
11123
\fun{GEN}{cvtop}{GEN x, GEN p, long l} as \kbd{gcvtop}, assuming that $x$
11124
is a scalar.
11125
11126
\fun{GEN}{cvtop2}{GEN x, GEN y} $y$ being a $p$-adic, converts the scalar $x$
11127
to a $p$-adic of the same accuracy. Shallow function.
11128
11129
\fun{GEN}{cvstop2}{long s, GEN y} $y$ being a $p$-adic, converts the scalar $s$
11130
to a $p$-adic of the same accuracy. Shallow function.
11131
11132
\fun{GEN}{gprec}{GEN x, long l} returns a copy of $x$ whose precision is
11133
changed to $l$ digits. The precision change is done recursively on all
11134
components of $x$. Digits means \emph{decimal}, $p$-adic and $X$-adic digits
11135
for \typ{REAL}, \typ{SER}, \typ{PADIC} components, respectively.
11136
11137
\fun{GEN}{gprec_w}{GEN x, long l} returns a shallow copy of $x$ whose
11138
\typ{REAL} components have their precision changed to $l$ \emph{words}. This
11139
is often more useful than \kbd{gprec}.
11140
11141
\fun{GEN}{gprec_wtrunc}{GEN x, long l} returns a shallow copy of $x$ whose
11142
\typ{REAL} components have their precision \emph{truncated} to $l$
11143
\emph{words}. Contrary to \kbd{gprec\_w}, this function may never increase
11144
the precision of~$x$.
11145
11146
\fun{GEN}{gprec_wensure}{GEN x, long l} returns a shallow copy of $x$ whose
11147
\typ{REAL} components have their precision \emph{increased} to at least $l$
11148
\emph{words}. Contrary to \kbd{gprec\_w}, this function may never decrease
11149
the precision of~$x$.
11150
11151
The following functions are obsolete and kept for backward compatibility only:
11152
11153
\fun{GEN}{precision0}{GEN x, long n}
11154
11155
\fun{GEN}{bitprecision0}{GEN x, long n}
11156
11157
\subsec{Modular objects / lifts}
11158
11159
\fun{GEN}{gmodulo}{GEN x, GEN y} creates the object \kbd{\key{Mod}(x,y)} on
11160
the PARI stack, where \kbd{x} and \kbd{y} are either both \typ{INT}s, and the
11161
result is a \typ{INTMOD}, or \kbd{x} is a scalar or a \typ{POL} and \kbd{y} a
11162
\typ{POL}, and the result is a \typ{POLMOD}.
11163
11164
\fun{GEN}{gmodulgs}{GEN x, long y} same as \key{gmodulo} except \kbd{y} is a
11165
\kbd{long}.
11166
11167
\fun{GEN}{gmodulsg}{long x, GEN y} same as \key{gmodulo} except \kbd{x} is a
11168
\kbd{long}.
11169
11170
\fun{GEN}{gmodulss}{long x, long y} same as \key{gmodulo} except both
11171
\kbd{x} and \kbd{y} are \kbd{long}s.
11172
11173
\fun{GEN}{lift_shallow}{GEN x} shallow version of \tet{lift}
11174
11175
\fun{GEN}{liftall_shallow}{GEN x} shallow version of \tet{liftall}
11176
11177
\fun{GEN}{liftint_shallow}{GEN x} shallow version of \tet{liftint}
11178
11179
\fun{GEN}{liftpol_shallow}{GEN x} shallow version of \tet{liftpol}
11180
11181
\fun{GEN}{centerlift0}{GEN x,long v} DEPRECATED, kept for backward
11182
compatibility only: use either \tet{lift0}$(x,v)$ or \tet{centerlift}$(x)$.
11183
11184
\subsec{Between polynomials and coefficient arrays}
11185
11186
\fun{GEN}{gtopoly}{GEN x, long v} converts or truncates the object~\kbd{x}
11187
into a \typ{POL} with main variable number~\kbd{v}. A common application
11188
would be the conversion of coefficient vectors (coefficients are given by
11189
decreasing degree). E.g.~\kbd{[2,3]} goes to \kbd{2*v + 3}
11190
11191
\fun{GEN}{gtopolyrev}{GEN x, long v} converts or truncates the object~\kbd{x}
11192
into a \typ{POL} with main variable number~\kbd{v}, but vectors are converted
11193
in reverse order compared to \kbd{gtopoly} (coefficients are given by
11194
increasing degree). E.g.~\kbd{[2,3]} goes to \kbd{3*v + 2}. In other words
11195
the vector represents a polynomial in the basis $(1,v,v^2,v^3,\dots)$.
11196
11197
\fun{GEN}{normalizepol}{GEN x} applied to an unnormalized \typ{POL}~\kbd{x}
11198
(with all coefficients correctly set except that \kbd{leading\_term(x)} might
11199
be zero), normalizes \kbd{x} correctly in place and returns~\kbd{x}. For
11200
internal use. Normalizing means deleting all leading \emph{exact} zeroes
11201
(as per \kbd{isexactzero}), except if the polynomial turns out to be $0$,
11202
in which case we try to find a coefficient $c$ which is a nonrational zero,
11203
and return the constant polynomial $c$. (We do this so that information
11204
about the base ring is not lost.)
11205
11206
\fun{GEN}{normalizepol_lg}{GEN x, long l} applies \kbd{normalizepol} to
11207
\kbd{x}, pretending that \kbd{lg(x)} is $l$, which must be less than
11208
or equal to \kbd{lg(x)}. If equal, the function is equivalent to
11209
\kbd{normalizepol(x)}.
11210
11211
\fun{GEN}{normalizepol_approx}{GEN x, long lx} as \kbd{normalizepol\_lg},
11212
with the difference that we just delete all leading zeroes (as per
11213
\kbd{gequal0}). This rougher normalization is used when we have no other
11214
choice, for instance before attempting a Euclidean division by $x$.
11215
11216
The following routines do \emph{not} copy coefficients on the stack (they
11217
only move pointers around), hence are very fast but not suitable for
11218
\kbd{gerepile} calls. Recall that an \kbd{RgV} (resp.~an \kbd{RgX}, resp.~an
11219
\kbd{RgM}) is a \typ{VEC} or \typ{COL} (resp.~a \typ{POL}, resp.~a \typ{MAT})
11220
with arbitrary components. Similarly, an \kbd{RgXV} is a \typ{VEC} or
11221
\typ{COL} with \kbd{RgX} components, etc.
11222
11223
\fun{GEN}{RgV_to_RgX}{GEN x, long v} converts the \kbd{RgV}~\kbd{x} to a
11224
(normalized) polynomial in variable~\kbd{v} (as \kbd{gtopolyrev}, without
11225
copy).
11226
11227
\fun{GEN}{RgV_to_RgX_reverse}{GEN x, long v} converts the \kbd{RgV}~\kbd{x}
11228
to a (normalized) polynomial in variable~\kbd{v} (as \kbd{gtopoly},
11229
without copy).
11230
11231
\fun{GEN}{RgX_to_RgC}{GEN x, long N} converts the \typ{POL}~\kbd{x} to a
11232
\typ{COL}~\kbd{v} with \kbd{N} components. Coefficients of \kbd{x} are listed
11233
by increasing degree, so that \kbd{y[i]} is the coefficient of the term of
11234
degree $i-1$ in \kbd{x}.
11235
11236
\fun{GEN}{Rg_to_RgC}{GEN x, long N} as \tet{RgX_to_RgV}, except that other
11237
types than \typ{POL} are allowed for \kbd{x}, which is then considered as a
11238
constant polynomial.
11239
11240
\fun{GEN}{RgM_to_RgXV}{GEN x, long v} converts the \kbd{RgM}~\kbd{x} to a
11241
\typ{VEC} of \kbd{RgX}, by repeated calls to \kbd{RgV\_to\_RgX}.
11242
11243
\fun{GEN}{RgM_to_RgXV_reverse}{GEN x, long v} converts the \kbd{RgM}~\kbd{x}
11244
to a \typ{VEC} of \kbd{RgX}, by repeated calls to \kbd{RgV\_to\_RgX\_reverse}.
11245
11246
\fun{GEN}{RgV_to_RgM}{GEN v, long N} converts the vector~\kbd{v} to
11247
a~\typ{MAT} with \kbd{N}~rows, by repeated calls to \kbd{Rg\_to\_RgV}.
11248
11249
\fun{GEN}{RgXV_to_RgM}{GEN v, long N} converts the vector of \kbd{RgX}~\kbd{v}
11250
to a~\typ{MAT} with \kbd{N}~rows, by repeated calls to \kbd{RgX\_to\_RgV}.
11251
11252
\fun{GEN}{RgM_to_RgXX}{GEN x, long v,long w} converts the \kbd{RgM}~\kbd{x} into
11253
a \typ{POL} in variable~\kbd{v}, whose coefficients are \typ{POL}s in
11254
variable~\kbd{w}. This is a shortcut for
11255
\bprog
11256
RgV_to_RgX( RgM_to_RgXV(x, w), v );
11257
@eprog\noindent
11258
There are no consistency checks with respect to variable
11259
priorities: the above is an invalid object if $\kbd{varncmp(v, w)} \geq 0$.
11260
11261
\fun{GEN}{RgXX_to_RgM}{GEN x, long N} converts the \typ{POL}~\kbd{x} with
11262
\kbd{RgX} (or constant) coefficients to a matrix with \kbd{N} rows.
11263
11264
\fun{long}{RgXY_degreex}{GEN P} return the degree of $P$ with respect to
11265
the secondary variable.
11266
11267
\fun{GEN}{RgXY_derivx}{GEN P} return the derivative of $P$ with respect to
11268
the secondary variable.
11269
11270
\fun{GEN}{RgXY_swap}{GEN P, long n, long w} converts the bivariate polynomial
11271
$\kbd{P}(u,v)$ (a \typ{POL} with \typ{POL} or scalar coefficients) to
11272
$P(\kbd{pol\_x[w]},u)$, assuming \kbd{n} is an upper bound for
11273
$\deg_v(\kbd{P})$.
11274
11275
\fun{GEN}{RgXY_swapspec}{GEN C, long n, long w, long lP}
11276
as \kbd{RgXY\_swap} where the coefficients of $P$ are given by
11277
\kbd{gel(C,0),\dots,gel(C,lP-1)}.
11278
11279
\fun{GEN}{RgX_to_ser}{GEN x, long l} convert the \typ{POL}~\kbd{x} to
11280
a \emph{shallow} \typ{SER} of length~$l\geq 2$.
11281
Unless the polynomial is an exact zero, the coefficient of lowest degree
11282
$T^d$ of the result is not an exact zero (as per \kbd{isexactzero}). The
11283
remainder is $O(T^{d+l-2})$.
11284
11285
\fun{GEN}{RgX_to_ser_inexact}{GEN x, long l} convert the \typ{POL}~\kbd{x} to
11286
a \emph{shallow} \typ{SER} of length~$l\geq 2$.
11287
Unless the polynomial is zero, the coefficient of lowest degree
11288
$T^d$ of the result is not zero (as per \kbd{gequal0}). The
11289
remainder is $O(T^{d+l-2})$.
11290
11291
\fun{GEN}{RgV_to_ser}{GEN x, long v, long l} convert the \typ{VEC}~\kbd{x},
11292
to a \emph{shallow} \typ{SER} of length~$l\geq 2$.
11293
11294
\fun{GEN}{rfrac_to_ser}{GEN F, long l} applied to a \typ{RFRAC}~$F$,
11295
creates a \typ{SER} of length~$l\geq 2$ congruent to $F$. Not memory-clean
11296
but suitable for \kbd{gerepileupto}.
11297
11298
\fun{GEN}{rfracrecip_to_ser_absolute}{GEN F, long d} applied to a
11299
\typ{RFRAC}~$F$, creates the \typ{SER} $F(1/t) + O(t^d)$. Note that
11300
we use absolute and not relative precision here.
11301
11302
\fun{GEN}{gtoser}{GEN s, long v, long d}. This function is deprecated,
11303
kept for backward compatibility: it follows the semantic of \kbd{Ser(s,v)},
11304
with $d = \kbd{seriesprecision}$ implied and is hard to use as a general
11305
conversion function. Use \tet{gtoser_prec} instead.
11306
11307
It converts the object~$s$ into a \typ{SER} with main variable number~\kbd{v}
11308
and $d > 0$ significant terms, but the argument $d$ is sometimes ignored.
11309
More precisely
11310
11311
\item if $s$ is a scalar (with respect to variable $v$), we return a
11312
constant power series with $d$ significant terms;
11313
11314
\item if $s$ is a \typ{POL} in variable $v$, it is truncated to $d$ terms if
11315
needed;
11316
11317
\item if $s$ is a vector, the coefficients of the vector are understood to
11318
be the coefficients of the power series starting from the constant term (as
11319
in \tet{Polrev}), and the precision $d$ is \emph{ignored};
11320
11321
\item if $s$ is already a power series in $v$, we return a copy, and
11322
the precision $d$ is again \emph{ignored}.
11323
11324
\fun{GEN}{gtoser_prec}{GEN s, long v, long d} this function is a variant of
11325
\kbd{gtoser} following the semantic of \kbd{Ser(s,v,d)}: the precision $d$
11326
is always taken into account.
11327
11328
\fun{GEN}{gtocol}{GEN x} converts the object~\kbd{x} into a \typ{COL}
11329
11330
\fun{GEN}{gtomat}{GEN x} converts the object~\kbd{x} into a \typ{MAT}.
11331
11332
\fun{GEN}{gtovec}{GEN x} converts the object~\kbd{x} into a \typ{VEC}.
11333
11334
\fun{GEN}{gtovecsmall}{GEN x} converts the object~\kbd{x} into a
11335
\typ{VECSMALL}.
11336
11337
\fun{GEN}{normalize}{GEN x} applied to an unnormalized \typ{SER}~\kbd{x}
11338
(i.e.~type \typ{SER} with all coefficients correctly set except that \kbd{x[2]}
11339
might be zero), normalizes \kbd{x} correctly in place. Returns~\kbd{x}.
11340
For internal use.
11341
11342
\fun{GEN}{serchop0}{GEN s} given a \typ{SER} of the form $x^v s(x)$, with
11343
$s(0)\neq 0$, return $x^v(s - s(0))$. Shallow function.
11344
11345
\fun{GEN}{serchop_i}{GEN x, long n} returns a shallow chopy of \typ{SER} $x$
11346
with all terms of degree strictly less than $n$ removed. Shallow
11347
version of \kbd{serchop}.
11348
11349
\section{Constructors}
11350
11351
\subsec{Clean constructors}\label{se:clean}
11352
11353
\fun{GEN}{zeropadic}{GEN p, long n} creates a $0$ \typ{PADIC} equal to
11354
$O(\kbd{p}^\kbd{n})$.
11355
11356
\fun{GEN}{zeroser}{long v, long n} creates a $0$ \typ{SER} in variable
11357
\kbd{v} equal to $O(X^\kbd{n})$.
11358
11359
\fun{GEN}{scalarser}{GEN x, long v, long prec} creates a constant \typ{SER}
11360
in variable \kbd{v} and precision \kbd{prec}, whose constant coefficient is
11361
(a copy of) \kbd{x}, in other words $\kbd{x} + O(\kbd{v}^\kbd{prec})$.
11362
Assumes that $\kbd{prec}\geq 0$.
11363
11364
\fun{GEN}{pol_0}{long v} Returns the constant polynomial $0$ in variable $v$.
11365
11366
\fun{GEN}{pol_1}{long v} Returns the constant polynomial $1$ in variable $v$.
11367
11368
\fun{GEN}{pol_x}{long v} Returns the monomial of degree $1$ in variable $v$.
11369
11370
\fun{GEN}{pol_xn}{long n, long v} Returns the monomial of degree $n$
11371
in variable $v$; assume that $n \geq 0$.
11372
11373
\fun{GEN}{pol_xnall}{long n, long v} Returns the Laurent monomial of degree $n$
11374
in variable $v$; $n < 0$ is allowed.
11375
11376
\fun{GEN}{pol_x_powers}{long N, long v} returns the powers of
11377
\kbd{pol\_x(v)}, of degree $0$ to $N-1$, in a vector with $N$ components.
11378
11379
\fun{GEN}{scalarpol}{GEN x, long v} creates a constant \typ{POL} in variable
11380
\kbd{v}, whose constant coefficient is (a copy of) \kbd{x}.
11381
11382
\fun{GEN}{deg1pol}{GEN a, GEN b,long v} creates the degree 1 \typ{POL}
11383
$a \kbd{pol\_x}(v) + b$
11384
11385
\fun{GEN}{zeropol}{long v} is identical \kbd{pol\_0}.
11386
11387
\fun{GEN}{zerocol}{long n} creates a \typ{COL} with \kbd{n} components set to
11388
\kbd{gen\_0}.
11389
11390
\fun{GEN}{zerovec}{long n} creates a \typ{VEC} with \kbd{n} components set to
11391
\kbd{gen\_0}.
11392
11393
\fun{GEN}{zerovec_block}{long n} as \kbd{zerovec} but return a clone.
11394
11395
\fun{GEN}{col_ei}{long n, long i} creates a \typ{COL} with \kbd{n} components
11396
set to \kbd{gen\_0}, but for the \kbd{i}-th one which is set to \kbd{gen\_1}
11397
(\kbd{i}-th vector in the canonical basis).
11398
11399
\fun{GEN}{vec_ei}{long n, long i} creates a \typ{VEC} with \kbd{n} components
11400
set to \kbd{gen\_0}, but for the \kbd{i}-th one which is set to \kbd{gen\_1}
11401
(\kbd{i}-th vector in the canonical basis).
11402
11403
\fun{GEN}{trivial_fact}{void} returns the trivial (empty) factorization
11404
\kbd{Mat([]\til,[]\til)}
11405
11406
\fun{GEN}{prime_fact}{GEN x} returns the factorization
11407
\kbd{Mat([x]\til, [1]\til)}
11408
11409
\fun{GEN}{Rg_col_ei}{GEN x, long n, long i} creates a \typ{COL} with \kbd{n}
11410
components set to \kbd{gen\_0}, but for the \kbd{i}-th one which is set to
11411
\kbd{x}.
11412
11413
\fun{GEN}{vecsmall_ei}{long n, long i} creates a \typ{VECSMALL} with \kbd{n}
11414
components set to \kbd{0}, but for the \kbd{i}-th one which is set to
11415
\kbd{1} (\kbd{i}-th vector in the canonical basis).
11416
11417
\fun{GEN}{scalarcol}{GEN x, long n} creates a \typ{COL} with \kbd{n}
11418
components set to \kbd{gen\_0}, but the first one which is set to a copy
11419
of \kbd{x}. (The name comes from \kbd{RgV\_isscalar}.)
11420
11421
\smallskip
11422
11423
\fun{GEN}{mkintmodu}{ulong x, ulong y} creates the \typ{INTMOD} \kbd{Mod(x, y)}.
11424
The inputs must satisfy $x < y$.
11425
11426
\fun{GEN}{zeromat}{long m, long n} creates a \typ{MAT} with \kbd{m} x \kbd{n}
11427
components set to \kbd{gen\_0}. Note that the result allocates a
11428
\emph{single} column, so modifying an entry in one column modifies it in
11429
all columns. To fully allocate a matrix initialized with zero entries,
11430
use \kbd{zeromatcopy}.
11431
11432
\fun{GEN}{zeromatcopy}{long m, long n} creates a \typ{MAT} with \kbd{m} x
11433
\kbd{n} components set to \kbd{gen\_0}.
11434
11435
\fun{GEN}{matid}{long n} identity matrix in dimension \kbd{n} (with
11436
components \kbd{gen\_1} and\kbd{gen\_0}).
11437
11438
\fun{GEN}{scalarmat}{GEN x, long n} scalar matrix, \kbd{x} times the identity.
11439
11440
\fun{GEN}{scalarmat_s}{long x, long n} scalar matrix, \kbd{stoi(x)} times
11441
the identity.
11442
11443
\fun{GEN}{vecrange}{GEN a, GEN b} returns the \typ{VEC} $[a..b]$.
11444
11445
\fun{GEN}{vecrangess}{long a, long b} returns the \typ{VEC} $[a..b]$.
11446
11447
\smallskip
11448
See also next section for analogs of the following functions:
11449
11450
\fun{GEN}{mkfracss}{long x, long y} creates the \typ{FRAC} $x/y$. Assumes that
11451
$y > 1$ and $(x,y) = 1$.
11452
11453
\fun{GEN}{sstoQ}{long x, long y} returns the \typ{INT} or \typ{FRAC} $x/y$;
11454
no assumptions.
11455
11456
\fun{void}{Qtoss}{GEN q, long *n, long *d} given a \typ{INT} or \typ{FRAC} $q$,
11457
set $n$ and $d$ such that $q = n/d$ with $d \geq 1$ and $(n,d)$ = 1. Overflow
11458
error if numerator or denominator do not fit into a long integer.
11459
11460
\fun{GEN}{mkfraccopy}{GEN x, GEN y} creates the \typ{FRAC} $x/y$. Assumes that
11461
$y > 1$ and $(x,y) = 1$.
11462
11463
\fun{GEN}{mkrfraccopy}{GEN x, GEN y} creates the \typ{RFRAC} $x/y$.
11464
Assumes that $y$ is a \typ{POL}, $x$ a compatible type whose variable has
11465
lower or same priority, with $(x,y) = 1$.
11466
11467
\fun{GEN}{mkcolcopy}{GEN x} creates a 1-dimensional \typ{COL} containing
11468
\kbd{x}.
11469
11470
\fun{GEN}{mkmatcopy}{GEN x} creates a 1-by-1 \typ{MAT} wrapping the \typ{COL}
11471
\kbd{x}.
11472
11473
\fun{GEN}{mkveccopy}{GEN x} creates a 1-dimensional \typ{VEC} containing
11474
\kbd{x}.
11475
11476
\fun{GEN}{mkvec2copy}{GEN x, GEN y} creates a 2-dimensional \typ{VEC} equal
11477
to \kbd{[x,y]}.
11478
11479
\fun{GEN}{mkcols}{long x} creates a 1-dimensional \typ{COL}
11480
containing \kbd{stoi(x)}.
11481
11482
\fun{GEN}{mkcol2s}{long x, long y} creates a 2-dimensional \typ{COL}
11483
containing \kbd{[stoi(x), stoi(y)]~}.
11484
11485
\fun{GEN}{mkcol3s}{long x, long y, long z} creates a 3-dimensional \typ{COL}
11486
containing \kbd{[stoi(x), stoi(y), stoi(z)]~}.
11487
11488
\fun{GEN}{mkcol4s}{long x, long y, long z, long t} creates a 4-dimensional
11489
\typ{COL} containing \kbd{[stoi(x), stoi(y), stoi(z), stoi(t)]~}.
11490
11491
\fun{GEN}{mkvecs}{long x} creates a 1-dimensional \typ{VEC}
11492
containing \kbd{stoi(x)}.
11493
11494
\fun{GEN}{mkvec2s}{long x, long y} creates a 2-dimensional \typ{VEC}
11495
containing \kbd{[stoi(x), stoi(y)]}.
11496
11497
\fun{GEN}{mkmat22s}{long a, long b, long c, long d} creates the $2$ by $2$
11498
\typ{MAT} with successive rows \kbd{[stoi(a), stoi(b)]} and
11499
\kbd{[stoi(c), stoi(d)]}.
11500
11501
\fun{GEN}{mkvec3s}{long x, long y, long z} creates a 3-dimensional \typ{VEC}
11502
containing \kbd{[stoi(x), stoi(y), stoi(z)]}.
11503
11504
\fun{GEN}{mkvec4s}{long x, long y, long z, long t} creates a 4-dimensional
11505
\typ{VEC} containing \kbd{[stoi(x), stoi(y), stoi(z), stoi(t)]}.
11506
11507
\fun{GEN}{mkvecsmall}{long x} creates a 1-dimensional \typ{VECSMALL}
11508
containing \kbd{x}.
11509
11510
\fun{GEN}{mkvecsmall2}{long x, long y} creates a 2-dimensional \typ{VECSMALL}
11511
containing \kbd{[x, y]}.
11512
11513
\fun{GEN}{mkvecsmall3}{long x, long y, long z} creates a 3-dimensional
11514
\typ{VECSMALL} containing \kbd{[x, y, z]}.
11515
11516
\fun{GEN}{mkvecsmall4}{long x, long y, long z, long t} creates a 4-dimensional
11517
\typ{VECSMALL} containing \kbd{[x, y, z, t]}.
11518
11519
\fun{GEN}{mkvecsmall5}{long x, long y, long z, long t, long u} creates a
11520
5-dimensional \typ{VECSMALL} containing \kbd{[x, y, z, t, u]}.
11521
11522
\fun{GEN}{mkvecsmalln}{long n, ...} returns the \typ{VECSMALL} whose $n$
11523
coefficients (\kbd{long}) follow.
11524
\emph{Warning:} since this is a variadic function, C type promotion is not
11525
performed on the arguments by the compiler, thus you have to make sure that all
11526
the arguments are of type \kbd{long}, in particular integer constants need to
11527
be written with the \kbd{L} suffix: \kbd{mkvecsmalln(2, 1L, 2L)} is correct,
11528
but \kbd{mkvecsmalln(2, 1, 2)} is not.
11529
11530
\subsec{Unclean constructors}\label{se:unclean}
11531
11532
Contrary to the policy of general PARI functions, the functions in this
11533
subsection do \emph{not} copy their arguments, nor do they produce an object
11534
a priori suitable for \tet{gerepileupto}. In particular, they are
11535
faster than their clean equivalent (which may not exist). \emph{If} you
11536
restrict their arguments to universal objects (e.g \kbd{gen\_0}),
11537
then the above warning does not apply.
11538
11539
\fun{GEN}{mkcomplex}{GEN x, GEN y} creates the \typ{COMPLEX} $x + iy$.
11540
11541
\fun{GEN}{mulcxI}{GEN x} creates the \typ{COMPLEX} $ix$. The result in
11542
general contains data pointing back to the original $x$. Use \kbd{gcopy} if
11543
this is a problem. But in most cases, the result is to be used immediately,
11544
before $x$ is subject to garbage collection.
11545
11546
\fun{GEN}{mulcxmI}{GEN x}, as \tet{mulcxI}, but returns $-ix$.
11547
11548
\fun{GEN}{mulcxpowIs}{GEN x, long k}, as \tet{mulcxI}, but returns
11549
$x \cdot i^k$.
11550
11551
\fun{GEN}{mkquad}{GEN n, GEN x, GEN y} creates the \typ{QUAD} $x + yw$,
11552
where $w$ is a root of $n$, which is of the form \kbd{quadpoly(D)}.
11553
11554
\fun{GEN}{mkfrac}{GEN x, GEN y} creates the \typ{FRAC} $x/y$. Assumes that
11555
$y > 1$ and $(x,y) = 1$.
11556
11557
\fun{GEN}{mkrfrac}{GEN x, GEN y} creates the \typ{RFRAC} $x/y$. Assumes
11558
that $y$ is a \typ{POL}, $x$ a compatible type whose variable has lower
11559
or same priority, with $(x,y) = 1$.
11560
11561
\fun{GEN}{mkcol}{GEN x} creates a 1-dimensional \typ{COL} containing \kbd{x}.
11562
11563
\fun{GEN}{mkcol2}{GEN x, GEN y} creates a 2-dimensional \typ{COL} equal to
11564
\kbd{[x,y]}.
11565
11566
\fun{GEN}{mkcol3}{GEN x, GEN y, GEN z} creates a 3-dimensional \typ{COL}
11567
equal to \kbd{[x,y,z]}.
11568
11569
\fun{GEN}{mkcol4}{GEN x, GEN y, GEN z, GEN t} creates a 4-dimensional \typ{COL}
11570
equal to \kbd{[x,y,z,t]}.
11571
11572
\fun{GEN}{mkcol5}{GEN a1, GEN a2, GEN a3, GEN a4, GEN a5} creates the
11573
5-dimensional \typ{COL} equal to $[a_1,a_2,a_3,a_4,a_5]$.
11574
11575
\fun{GEN}{mkcol6}{GEN x, GEN y, GEN z, GEN t, GEN u, GEN v}
11576
creates the $6$-dimensional column vector \kbd{[x,y,z,t,u,v]~}.
11577
11578
\fun{GEN}{mkintmod}{GEN x, GEN y} creates the \typ{INTMOD} \kbd{Mod(x, y)}.
11579
The inputs must be \typ{INT}s satisfying $0 \leq x < y$.
11580
11581
\fun{GEN}{mkpolmod}{GEN x, GEN y} creates the \typ{POLMOD} \kbd{Mod(x, y)}.
11582
The input must satisfy $\deg x < \deg y$ with respect to the main variable of
11583
the \typ{POL} $y$. $x$ may be a scalar.
11584
11585
\fun{GEN}{mkmat}{GEN x} creates a 1-column \typ{MAT} with column $x$
11586
(a \typ{COL}).
11587
11588
\fun{GEN}{mkmat2}{GEN x, GEN y} creates a 2-column \typ{MAT} with columns
11589
$x$, $y$ (\typ{COL}s of the same length).
11590
11591
\fun{GEN}{mkmat22}{GEN a, GEN b, GEN c, GEN d} creates the $2$ by $2$
11592
\typ{MAT} with successive rows $[a,b]$ and $[c,d]$.
11593
11594
\fun{GEN}{mkmat3}{GEN x, GEN y, GEN z} creates a 3-column \typ{MAT} with columns
11595
$x$, $y$, $z$ (\typ{COL}s of the same length).
11596
11597
\fun{GEN}{mkmat4}{GEN x, GEN y, GEN z, GEN t} creates a 4-column \typ{MAT}
11598
with columns $x$, $y$, $z$, $t$ (\typ{COL}s of the same length).
11599
11600
\fun{GEN}{mkmat5}{GEN x, GEN y, GEN z, GEN t, GEN u} creates a 5-column
11601
\typ{MAT} with columns $x$, $y$, $z$, $t$, $u$ (\typ{COL}s of the same
11602
length).
11603
11604
\fun{GEN}{mkvec}{GEN x} creates a 1-dimensional \typ{VEC} containing \kbd{x}.
11605
11606
\fun{GEN}{mkvec2}{GEN x, GEN y} creates a 2-dimensional \typ{VEC} equal to
11607
\kbd{[x,y]}.
11608
11609
\fun{GEN}{mkvec3}{GEN x, GEN y, GEN z} creates a 3-dimensional \typ{VEC}
11610
equal to \kbd{[x,y,z]}.
11611
11612
\fun{GEN}{mkvec4}{GEN x, GEN y, GEN z, GEN t} creates a 4-dimensional \typ{VEC}
11613
equal to \kbd{[x,y,z,t]}.
11614
11615
\fun{GEN}{mkvec5}{GEN a1, GEN a2, GEN a3, GEN a4, GEN a5} creates the
11616
5-dimensional \typ{VEC} equal to $[a_1,a_2,a_3,a_4,a_5]$.
11617
11618
\fun{GEN}{mkqfb}{GEN a, GEN b, GEN c, GEN D} creates \typ{QFB} equal
11619
to \kbd{Qfb(a,b,c)}, assuming that $D = b^2 - 4ac$.
11620
11621
\fun{GEN}{mkerr}{long n} returns a \typ{ERROR} with error code $n$
11622
(\kbd{enum err\_list}).
11623
11624
\smallskip
11625
11626
It is sometimes useful to return such a container whose entries are not
11627
universal objects, but nonetheless suitable for \tet{gerepileupto}.
11628
If the entries can be computed at the time the result is returned, the
11629
following macros achieve this effect:
11630
11631
\fun{GEN}{retmkvec}{GEN x} returns a vector containing the single entry $x$,
11632
where the vector root is created just before the function argument $x$ is
11633
evaluated. Expands to
11634
\bprog
11635
{
11636
GEN res = cgetg(2, t_VEC);
11637
gel(res, 1) = x; /* @Ccom or rather, the \emph{expansion} of $x$ */
11638
return res;
11639
}
11640
@eprog\noindent For instance, the \kbd{retmkvec(gcopy(x))} returns a clean
11641
object, just like \kbd{return mkveccopy(x)} would.
11642
11643
\fun{GEN}{retmkvec2}{GEN x, GEN y}
11644
returns the $2$-dimensional \typ{VEC} \kbd{[x,y]}.
11645
11646
\fun{GEN}{retmkvec3}{GEN x, GEN y, GEN z}
11647
returns the $3$-dimensional \typ{VEC} \kbd{[x,y,z]}.
11648
11649
\fun{GEN}{retmkvec4}{GEN x, GEN y, GEN z, GEN t}
11650
returns the $4$-dimensional \typ{VEC} \kbd{[x,y,z,t]}.
11651
11652
\fun{GEN}{retmkvec5}{GEN x, GEN y, GEN z, GEN t, GEN u}
11653
returns the $5$-dimensional row vector \kbd{[x,y,z,t,u]}.
11654
11655
\fun{GEN}{retconst_vec}{long n, GEN x}
11656
returns the $n$-dimensional \typ{VEC} whose entries are constant and all
11657
equal to $x$.
11658
11659
\fun{GEN}{retmkcol}{GEN x}
11660
returns the $1$-dimensional \typ{COL} \kbd{[x]~}.
11661
11662
\fun{GEN}{retmkcol2}{GEN x, GEN y}
11663
returns the $2$-dimensional \typ{COL} \kbd{[x,y]~}.
11664
11665
\fun{GEN}{retmkcol3}{GEN x, GEN y, GEN z}
11666
returns the $3$-dimensional \typ{COL} \kbd{[x,y,z]~}.
11667
11668
\fun{GEN}{retmkcol4}{GEN x, GEN y, GEN z, GEN t}
11669
returns the $4$-dimensional \typ{COL} \kbd{[x,y,z,t]~}.
11670
11671
\fun{GEN}{retmkcol5}{GEN x, GEN y, GEN z, GEN t, GEN u}
11672
returns the $5$-dimensional column vector \kbd{[x,y,z,t,u]~}.
11673
11674
\fun{GEN}{retmkcol6}{GEN x, GEN y, GEN z, GEN t, GEN u, GEN v}
11675
returns the $6$-dimensional column vector \kbd{[x,y,z,t,u,v]~}.
11676
11677
\fun{GEN}{retconst_col}{long n, GEN x}
11678
returns the $n$-dimensional \typ{COL} whose entries are constant and all
11679
equal to $x$.
11680
11681
\fun{GEN}{retmkmat}{GEN x}
11682
returns the $1$-column \typ{MAT} with colum \kbd{x}.
11683
11684
\fun{GEN}{retmkmat2}{GEN x, GEN y}
11685
returns the $2$-column \typ{MAT} with columns \kbd{x}, \kbd{y}.
11686
11687
\fun{GEN}{retmkmat3}{GEN x, GEN y, GEN z}
11688
returns the $3$-dimensional \typ{MAT} with columns
11689
\kbd{x}, \kbd{y}, \kbd{z}.
11690
11691
\fun{GEN}{retmkmat4}{GEN x, GEN y, GEN z, GEN t}
11692
returns the $4$-dimensional \typ{MAT} with columns
11693
\kbd{x}, \kbd{y}, \kbd{z}, \kbd{t}.
11694
11695
\fun{GEN}{retmkmat5}{GEN x, GEN y, GEN z, GEN t, GEN u}
11696
returns the $5$-dimensional \typ{MAT} with columns
11697
\kbd{x}, \kbd{y}, \kbd{z}, \kbd{t}, \kbd{u}.
11698
11699
\fun{GEN}{retmkcomplex}{GEN x, GEN y}
11700
returns the \typ{COMPLEX} \kbd{x + I*y}.
11701
11702
\fun{GEN}{retmkfrac}{GEN x, GEN y}
11703
returns the \typ{FRAC} \kbd{x / y}. Assume $x$ and $y$ are coprime and $y > 1$.
11704
11705
\fun{GEN}{retmkrfrac}{GEN x, GEN y}
11706
returns the \typ{RFRAC} \kbd{x / y}. Assume $x$ and $y$ are coprime and more
11707
generally that the rational function cannot be simplified.
11708
11709
\fun{GEN}{retmkintmod}{GEN x, GEN y}
11710
returns the \typ{INTMOD} \kbd{Mod(x, y)}.
11711
11712
\fun{GEN}{retmkquad}{GEN n, GEN a, GEN b}.
11713
11714
\fun{GEN}{retmkpolmod}{GEN x, GEN y}
11715
returns the \typ{POLMOD} \kbd{Mod(x, y)}.
11716
11717
\smallskip
11718
11719
\fun{GEN}{mkintn}{long n, ...} returns the nonnegative \typ{INT} whose
11720
development in base $2^{32}$ is given by the following $n$ 32bit-words
11721
(\kbd{unsigned int}).
11722
\bprog
11723
mkintn(3, a2, a1, a0);
11724
@eprog
11725
\noindent returns $a_2 2^{64} + a_1 2^{32} + a_0$.
11726
11727
\fun{GEN}{mkpoln}{long n, ...} Returns the \typ{POL} whose $n$
11728
coefficients (\kbd{GEN}) follow, in order of decreasing degree.
11729
\bprog
11730
mkpoln(3, gen_1, gen_2, gen_0);
11731
@eprog
11732
\noindent returns the polynomial $X^2 + 2X$ (in variable $0$, use
11733
\tet{setvarn} if you want other variable numbers). Beware that $n$ is the
11734
number of coefficients, hence \emph{one more} than the degree.
11735
11736
\fun{GEN}{mkvecn}{long n, ...} returns the \typ{VEC} whose $n$
11737
coefficients (\kbd{GEN}) follow.
11738
11739
\fun{GEN}{mkcoln}{long n, ...} returns the \typ{COL} whose $n$
11740
coefficients (\kbd{GEN}) follow.
11741
11742
\fun{GEN}{scalarcol_shallow}{GEN x, long n} creates a \typ{COL} with \kbd{n}
11743
components set to \kbd{gen\_0}, but the first one which is set to a shallow
11744
copy of \kbd{x}. (The name comes from \kbd{RgV\_isscalar}.)
11745
11746
\fun{GEN}{scalarmat_shallow}{GEN x, long n} creates an $n\times n$
11747
scalar matrix whose diagonal is set to shallow copies of the scalar \kbd{x}.
11748
11749
\fun{GEN}{RgX_sylvestermatrix}{GEN f, GEN g} return the Sylvester matrix
11750
attached to the two \typ{POL} in the same variable $f$ and $g$.
11751
11752
\fun{GEN}{diagonal_shallow}{GEN x} returns a diagonal matrix whose diagonal
11753
is given by the vector $x$. Shallow function.
11754
11755
\fun{GEN}{scalarpol_shallow}{GEN a, long v} returns the degree $0$
11756
\typ{POL} $a \kbd{pol\_x}(v)^0$.
11757
11758
\fun{GEN}{deg1pol_shallow}{GEN a, GEN b,long v} returns the degree $1$
11759
\typ{POL} $a\kbd{pol\_x}(v) + b$
11760
11761
\fun{GEN}{deg2pol_shallow}{GEN a, GEN b, GEN c, long v} returns the degree $2$
11762
\typ{POL} $a\*x^2+b\*x+c$ where $x=\kbd{pol\_x}(v)$.
11763
11764
\fun{GEN}{zeropadic_shallow}{GEN p, long n} returns a (shallow) $0$
11765
\typ{PADIC} equal to $O(\kbd{p}^\kbd{n})$.
11766
11767
\subsec{From roots to polynomials}
11768
11769
\fun{GEN}{deg1_from_roots}{GEN L, long v} given a vector $L$ of scalars,
11770
returns the vector of monic linear polynomials in variable $v$ whose roots
11771
are the $L[i]$, i.e. the $x - L[i]$.
11772
11773
\fun{GEN}{roots_from_deg1}{GEN L} given a vector $L$ of monic linear
11774
polynomials, return their roots, i.e. the $- L[i](0)$.
11775
11776
\fun{GEN}{roots_to_pol}{GEN L, long v} given a vector of scalars $L$,
11777
returns the monic polynomial in variable $v$ whose roots are the $L[i]$.
11778
Leaves some garbage on stack, but suitable for \kbd{gerepileupto}.
11779
11780
\fun{GEN}{roots_to_pol_r1}{GEN L, long v, long r1} as \kbd{roots\_to\_pol}
11781
assuming the first $r_1$ roots are ``real'', and the following ones are
11782
representatives of conjugate pairs of ``complex'' roots. So if $L$ has $r_1 +
11783
r_2$ elements, we obtain a polynomial of degree $r_1 + 2r_2$. In most
11784
applications, the roots are indeed real and complex, but the implementation
11785
assumes only that each ``complex'' root $z$ introduces a quadratic
11786
factor $X^2 - \kbd{trace}(z) X + \kbd{norm}(z)$.
11787
Leaves some garbage on stack, but suitable for \kbd{gerepileupto}.
11788
11789
\section{Integer parts}
11790
11791
\fun{GEN}{gfloor}{GEN x} creates the floor of~\kbd{x}, i.e.\ the (true)
11792
integral part.
11793
11794
\fun{GEN}{gfrac}{GEN x} creates the fractional part of~\kbd{x}, i.e.\ \kbd{x}
11795
minus the floor of~\kbd{x}.
11796
11797
\fun{GEN}{gceil}{GEN x} creates the ceiling of~\kbd{x}.
11798
11799
\fun{GEN}{ground}{GEN x} rounds towards~$+\infty$ the components of \kbd{x}
11800
to the nearest integers.
11801
11802
\fun{GEN}{grndtoi}{GEN x, long *e} same as \kbd{ground}, but in addition sets
11803
\kbd{*e} to the binary exponent of $x - \kbd{ground}(x)$. If this is
11804
positive, all significant bits are lost. This kind of situation raises an
11805
error message in \key{ground} but not in \key{grndtoi}.
11806
11807
\fun{GEN}{gtrunc}{GEN x} truncates~\kbd{x}. This is the false integer part
11808
if \kbd{x} is a real number (i.e.~the unique integer closest to \kbd{x} among
11809
those between 0 and~\kbd{x}). If \kbd{x} is a \typ{SER}, it is truncated
11810
to a \typ{POL}; if \kbd{x} is a \typ{RFRAC}, this takes the polynomial part.
11811
11812
\fun{GEN}{gtrunc2n}{GEN x, long n} creates the floor of~$2^n$\kbd{x}, this is
11813
only implemented for \typ{INT}, \typ{REAL}, \typ{FRAC} and \typ{COMPLEX} of
11814
those.
11815
11816
\fun{GEN}{gcvtoi}{GEN x, long *e} analogous to \key{grndtoi} for
11817
\typ{REAL} inputs except that rounding is replaced by truncation. Also applies
11818
componentwise for vector or matrix inputs; otherwise, sets \kbd{*e} to
11819
\kbd{-HIGHEXPOBIT} (infinite real accuracy) and return \kbd{gtrunc(x)}.
11820
11821
\section{Valuation and shift}
11822
11823
\fun{GEN}{gshift[z]}{GEN x, long n[, GEN z]} yields the result of shifting
11824
(the components of) \kbd{x} left by \kbd{n} (if \kbd{n} is nonnegative)
11825
or right by $-\kbd{n}$ (if \kbd{n} is negative). Applies only to \typ{INT}
11826
and vectors/matrices of such. For other types, it is simply multiplication
11827
by~$2^{\kbd{n}}$.
11828
11829
\fun{GEN}{gmul2n[z]}{GEN x, long n[, GEN z]} yields the product of \kbd{x}
11830
and~$2^{\kbd{n}}$. This is different from \kbd{gshift} when \kbd{n} is negative
11831
and \kbd{x} is a \typ{INT}: \key{gshift} truncates, while \key{gmul2n}
11832
creates a fraction if necessary.
11833
11834
\fun{long}{gvaluation}{GEN x, GEN p} returns the greatest exponent~$e$ such that
11835
$\kbd{p}^e$ divides~\kbd{x}, when this makes sense.
11836
11837
\fun{long}{gval}{GEN x, long v} returns the highest power of the variable
11838
number \kbd{v} dividing the \typ{POL}~\kbd{x}.
11839
11840
\section{Comparison operators}
11841
11842
\subsec{Generic}
11843
11844
\fun{long}{gcmp}{GEN x, GEN y} comparison of \kbd{x} with \kbd{y}: returns
11845
$1$ ($x > y$), $0$ ($x = y$) or $-1$ ($x < y$). Two \typ{STR}
11846
are compared using the standard lexicographic ordering; a \typ{STR}
11847
cannot be compared to any non-string type. If neither
11848
$x$ nor $y$ is a \typ{STR}, their allowed types are \typ{INT}, \typ{REAL},
11849
\typ{FRAC}, \typ{QUAD} with positive discriminant (use the canonical
11850
embedding $w \to \sqrt{D}/2$ or $w \to (1 + \sqrt{D})/2$) or \typ{INFINITY}.
11851
Use \tet{cmp_universal} to compare arbitrary \kbd{GEN}s.
11852
11853
\fun{long}{lexcmp}{GEN x, GEN y} comparison of \kbd{x} with \kbd{y} for the
11854
lexicographic ordering; when comparing objects of different lengths whose
11855
components are all equal up to the smallest of their length, consider that
11856
the longest is largest. Consider scalars as $1$-component vectors. Return
11857
\kbd{gcmp}$(x,y)$ if both arguments are scalars.
11858
11859
\fun{int}{gequalX}{GEN x} return 1 (true) if \kbd{x} is a variable
11860
(monomial of degree $1$ with \typ{INT} coefficients equal to $1$ and $0$),
11861
and $0$ otherwise
11862
11863
\fun{long}{gequal}{GEN x, GEN y} returns 1 (true) if \kbd{x} is equal
11864
to~\kbd{y}, 0~otherwise. A priori, this makes sense only if \kbd{x} and
11865
\kbd{y} have the same type, in which case they are recursively compared
11866
componentwise. When the types are different, a \kbd{true} result
11867
means that \kbd{x - y} was successfully computed and that
11868
\kbd{gequal0} found it equal to $0$. In particular
11869
\bprog
11870
gequal(cgetg(1, t_VEC), gen_0)
11871
@eprog\noindent is true, and the relation is not transitive. E.g.~an empty
11872
\typ{COL} and an empty \typ{VEC} are not equal but are both equal to
11873
\kbd{gen\_0}.
11874
11875
\fun{long}{gidentical}{GEN x, GEN y} returns 1 (true) if \kbd{x} is identical
11876
to~\kbd{y}, 0~otherwise. In particular, the types and length of \kbd{x} and
11877
\kbd{y} must be equal. This test is much stricter than \tet{gequal}, in
11878
particular, \typ{REAL} with different accuracies are tested different. This
11879
relation is transitive.
11880
11881
\fun{GEN}{gmax}{GEN x, GEN y} returns a copy of the maximum of $x$ and $y$,
11882
compared using \kbd{gcmp}.
11883
11884
\fun{GEN}{gmin}{GEN x, GEN y} returns a copy of the minimum of $x$ and $y$,
11885
compared using \kbd{gcmp}.
11886
11887
\fun{GEN}{gmax_shallow}{GEN x, GEN y} shallow version of \kbd{gmax}.
11888
11889
\fun{GEN}{gmin_shallow}{GEN x, GEN y} shallow version of \kbd{gmin}.
11890
11891
\subsec{Comparison with a small integer}
11892
11893
\fun{int}{isexactzero}{GEN x} returns 1 (true) if \kbd{x} is exactly equal
11894
to~0 (including \typ{INTMOD}s like \kbd{Mod(0,2)}), and 0~(false) otherwise.
11895
This includes recursive objects, for instance vectors, whose components are $0$.
11896
11897
\fun{GEN}{gisexactzero}{GEN x} returns \kbd{NULL} unless \kbd{x} is exactly
11898
equal to~0 (as per \kbd{isexactzero}). When \kbd{x} is an exact zero
11899
return the attached scalar zero as a \typ{INT} (\kbd{gen\_0}),
11900
a \typ{INTMOD} (\kbd{Mod(0,$N$)} for the largest possible $N$) or a
11901
\typ{FFELT}.
11902
11903
\fun{int}{isrationalzero}{GEN x} returns 1 (true) if \kbd{x} is equal
11904
to an integer~0 (excluding \typ{INTMOD}s like \kbd{Mod(0,2)}), and 0~(false)
11905
otherwise. Contrary to \kbd{isintzero}, this includes recursive objects, for
11906
instance vectors, whose components are $0$.
11907
11908
\fun{int}{ismpzero}{GEN x} returns 1 (true) if \kbd{x} is a \typ{INT} or
11909
a \typ{REAL} equal to~0.
11910
11911
\fun{int}{isintzero}{GEN x} returns 1 (true) if \kbd{x} is a \typ{INT}
11912
equal to~0.
11913
11914
\fun{int}{isint1}{GEN x} returns 1 (true) if \kbd{x} is a \typ{INT}
11915
equal to~1.
11916
11917
\fun{int}{isintm1}{GEN x} returns 1 (true) if \kbd{x} is a \typ{INT}
11918
equal to~$-1$.
11919
11920
\fun{int}{equali1}{GEN n}
11921
Assuming that \kbd{x} is a \typ{INT}, return 1 (true) if \kbd{x} is equal to
11922
$1$, and return 0~(false) otherwise.
11923
11924
\fun{int}{equalim1}{GEN n}
11925
Assuming that \kbd{x} is a \typ{INT}, return 1 (true) if \kbd{x} is equal to
11926
$-1$, and return 0~(false) otherwise.
11927
11928
\fun{int}{is_pm1}{GEN x}. Assuming that \kbd{x} is a
11929
\emph{nonzero} \typ{INT}, return 1 (true) if \kbd{x} is equal to $-1$ or
11930
$1$, and return 0~(false) otherwise.
11931
11932
\fun{int}{gequal0}{GEN x} returns 1 (true) if \kbd{x} is equal to~0, 0~(false)
11933
otherwise.
11934
11935
\fun{int}{gequal1}{GEN x} returns 1 (true) if \kbd{x} is equal to~1, 0~(false)
11936
otherwise.
11937
11938
\fun{int}{gequalm1}{GEN x} returns 1 (true) if \kbd{x} is equal to~$-1$,
11939
0~(false) otherwise.
11940
11941
11942
\fun{long}{gcmpsg}{long s, GEN x}
11943
11944
\fun{long}{gcmpgs}{GEN x, long s} comparison of \kbd{x} with the
11945
\kbd{long}~\kbd{s}.
11946
11947
\fun{GEN}{gmaxsg}{long s, GEN x}
11948
11949
\fun{GEN}{gmaxgs}{GEN x, long s} returns the largest of \kbd{x} and
11950
the \kbd{long}~\kbd{s} (converted to \kbd{GEN})
11951
11952
\fun{GEN}{gminsg}{long s, GEN x}
11953
11954
\fun{GEN}{gmings}{GEN x, long s} returns the smallest of \kbd{x} and the
11955
\kbd{long}~\kbd{s} (converted to \kbd{GEN})
11956
11957
\fun{long}{gequalsg}{long s, GEN x}
11958
11959
\fun{long}{gequalgs}{GEN x, long s} returns 1 (true) if \kbd{x} is equal to
11960
the \kbd{long}~\kbd{s}, 0~otherwise.
11961
11962
\section{Miscellaneous Boolean functions}
11963
11964
\fun{int}{isrationalzeroscalar}{GEN x} equivalent to, but faster than,
11965
\bprog
11966
is_scalar_t(typ(x)) && isrationalzero(x)
11967
@eprog
11968
11969
\fun{int}{isinexact}{GEN x} returns 1 (true) if $x$ has an inexact
11970
component, and 0 (false) otherwise.
11971
11972
\fun{int}{isinexactreal}{GEN x} return 1 if $x$ has an inexact
11973
\typ{REAL} component, and 0 otherwise.
11974
11975
\fun{int}{isrealappr}{GEN x, long e} applies (recursively) to complex inputs;
11976
returns $1$ if $x$ is approximately real to the bit accuracy $e$, and 0
11977
otherwise. This means that any \typ{COMPLEX} component must have imaginary part
11978
$t$ satisfying $\kbd{gexpo}(t) < e$.
11979
11980
\fun{int}{isint}{GEN x, GEN *n} returns 0 (false) if \kbd{x} does not round
11981
to an integer. Otherwise, returns 1 (true) and set \kbd{n} to the rounded
11982
value.
11983
11984
\fun{int}{issmall}{GEN x, long *n} returns 0 (false) if \kbd{x} does not
11985
round to a small integer (suitable for \kbd{itos}). Otherwise, returns 1
11986
(true) and set \kbd{n} to the rounded value.
11987
11988
\fun{long}{iscomplex}{GEN x} returns 1 (true) if \kbd{x} is a complex number
11989
(of component types embeddable into the reals) but is not itself real, 0~if
11990
\kbd{x} is a real (not necessarily of type \typ{REAL}), or raises an error if
11991
\kbd{x} is not embeddable into the complex numbers.
11992
11993
\subsec{Obsolete}
11994
11995
The following less convenient comparison functions and Boolean operators were
11996
used by the historical GP interpreter. They are provided for backward
11997
compatibility only and should not be used:
11998
11999
\fun{GEN}{gle}{GEN x, GEN y}
12000
12001
\fun{GEN}{glt}{GEN x, GEN y}
12002
12003
\fun{GEN}{gge}{GEN x, GEN y}
12004
12005
\fun{GEN}{ggt}{GEN x, GEN y}
12006
12007
\fun{GEN}{geq}{GEN x, GEN y}
12008
12009
\fun{GEN}{gne}{GEN x, GEN y}
12010
12011
\fun{GEN}{gor}{GEN x, GEN y}
12012
12013
\fun{GEN}{gand}{GEN x, GEN y}
12014
12015
\fun{GEN}{gnot}{GEN x, GEN y}
12016
12017
\section{Sorting}
12018
12019
\subsec{Basic sort}
12020
12021
\fun{GEN}{sort}{GEN x} sorts the vector \kbd{x} in ascending order using a
12022
mergesort algorithm, and \kbd{gcmp} as the underlying comparison routine
12023
(returns the sorted vector). This routine copies all components of $x$, use
12024
\kbd{gen\_sort\_inplace} for a more memory-efficient function.
12025
12026
\fun{GEN}{lexsort}{GEN x}, as \kbd{sort}, using \kbd{lexcmp} instead of
12027
\kbd{gcmp} as the underlying comparison routine.
12028
12029
\fun{GEN}{vecsort}{GEN x, GEN k}, as \kbd{sort}, but sorts the
12030
vector \kbd{x} in ascending \emph{lexicographic} order, according to the
12031
entries of the \typ{VECSMALL} \kbd{k}. For example, if $\kbd{k} = [2,1,3]$,
12032
sorting will be done with respect to the second component, and when these
12033
are equal, with respect to the first, and when these are equal, with
12034
respect to the third.
12035
12036
\subsec{Indirect sorting}
12037
12038
\fun{GEN}{indexsort}{GEN x} as \kbd{sort}, but only returns the permutation
12039
which, applied to \kbd{x}, would sort the vector. The result is a
12040
\typ{VECSMALL}.
12041
12042
\fun{GEN}{indexlexsort}{GEN x}, as \kbd{indexsort}, using \kbd{lexcmp}
12043
instead of \kbd{gcmp} as the underlying comparison routine.
12044
12045
\fun{GEN}{indexvecsort}{GEN x, GEN k}, as \kbd{vecsort}, but only
12046
returns the permutation that would sort the vector \kbd{x}.
12047
12048
\fun{long}{vecindexmin}{GEN x} returns the index for a maximal element of $x$
12049
(\typ{VEC}, \typ{COL} or \typ{VECSMALL}).
12050
12051
\fun{long}{vecindexmax}{GEN x} returns the index for a maximal element of $x$
12052
(\typ{VEC}, \typ{COL} or \typ{VECSMALL}).
12053
12054
\fun{long}{vecindexmax}{GEN x}
12055
12056
\subsec{Generic sort and search} The following routines allow to use an
12057
arbitrary comparison function \kbd{int (*cmp)(void* data, GEN x, GEN y)},
12058
such that \kbd{cmp(data,x,y)} returns a negative result if $x
12059
< y$, a positive one if $x > y$ and 0 if $x = y$. The \kbd{data} argument is
12060
there in case your \kbd{cmp} requires additional context.
12061
12062
\fun{GEN}{gen_sort}{GEN x, void *data, int (*cmp)(void *,GEN,GEN)}, as
12063
\kbd{sort}, with an explicit comparison routine.
12064
12065
\fun{GEN}{gen_sort_shallow}{GEN x, void *data, int (*cmp)(void *,GEN,GEN)},
12066
shallow variant of \kbd{gen\_sort}.
12067
12068
\fun{GEN}{gen_sort_uniq}{GEN x, void *data, int (*cmp)(void *,GEN,GEN)}, as
12069
\kbd{gen\_sort}, removing duplicate entries.
12070
12071
\fun{GEN}{gen_indexsort}{GEN x, void *data, int (*cmp)(void*,GEN,GEN)},
12072
as \kbd{indexsort}.
12073
12074
\fun{GEN}{gen_indexsort_uniq}{GEN x, void *data, int (*cmp)(void*,GEN,GEN)},
12075
as \kbd{indexsort}, removing duplicate entries.
12076
12077
\fun{void}{gen_sort_inplace}{GEN x, void *data, int (*cmp)(void*,GEN,GEN), GEN
12078
*perm} sort \kbd{x} in place, without copying its components. If
12079
\kbd{perm} is not \kbd{NULL}, it is set to the permutation that would sort
12080
the original \kbd{x}.
12081
12082
\fun{GEN}{gen_setminus}{GEN A, GEN B, int (*cmp)(GEN,GEN)} given two sorted
12083
vectors $A$ and $B$, returns the vector of elements of $A$ not belonging to
12084
$B$.
12085
12086
\fun{GEN}{sort_factor}{GEN y, void *data, int (*cmp)(void *,GEN,GEN)}:
12087
assuming \kbd{y} is a factorization matrix, sorts its rows in place (no copy
12088
is made) according to the comparison function \kbd{cmp} applied to its first
12089
column.
12090
12091
\fun{GEN}{merge_sort_uniq}{GEN x,GEN y, void *data, int (*cmp)(void *,GEN,GEN)}
12092
assuming \kbd{x} and \kbd{y} are sorted vectors, with respect to the \kbd{cmp}
12093
comparison function, return a sorted concatenation, with duplicates removed.
12094
Shallow function.
12095
12096
\fun{GEN}{setunion_i}{GEN x,GEN y} shallow version of \kbd{setunion}, a
12097
simple alias for
12098
\bprog
12099
merge_sort_uniq(x,y, (void*)cmp_universal, cmp_nodata)
12100
@eprog
12101
12102
\fun{GEN}{merge_factor}{GEN fx, GEN fy, void *data, int (*cmp)(void *,GEN,GEN)}
12103
let \kbd{fx} and \kbd{fy} be factorization matrices for $X$ and $Y$
12104
sorted with respect to the comparison function \kbd{cmp} (see
12105
\tet{sort_factor}), returns the factorization of $X * Y$.
12106
12107
\fun{long}{gen_search}{GEN v, GEN y, void *data, int
12108
(*cmp)(void*,GEN,GEN)}.\hfil\break
12109
Let $v$ be a vector sorted according to \kbd{cmp(data,a,b)}; look for an
12110
index $i$ such that $v[i]$ is equal to $y$. If $y$ is found, return $i$
12111
(not necessarily the first occurence in case of multisets), else return $-i$
12112
where $i$ is the index where $y$ should be inserted.
12113
12114
\fun{long}{tablesearch}{GEN T, GEN x, int (*cmp)(GEN,GEN)} is a faster
12115
implementation for the common case \kbd{gen\_search(T,x,cmp,cmp\_nodata)}
12116
when we have no need to insert missing elements; return $0$ in case $x$
12117
is not found.
12118
12119
\subsec{Further useful comparison functions}
12120
12121
\fun{int}{cmp_universal}{GEN x, GEN y} a somewhat arbitrary universal
12122
comparison function, devoid of sensible mathematical meaning. It is
12123
transitive, and returns 0 if and only if \kbd{gidentical(x,y)} is true.
12124
Useful to sort and search vectors of arbitrary data.
12125
12126
\fun{int}{cmp_nodata}{void *data, GEN x, GEN y}. This function is a hack
12127
used to pass an existing basic comparison function lacking the \kbd{data}
12128
argument, i.e. with prototype \kbd{int (*cmp)(GEN x, GEN y)}. Instead of
12129
\kbd{gen\_sort(x, NULL, cmp)} which may or may not work depending on how your
12130
compiler handles typecasts between incompatible function pointers, one should
12131
use \kbd{gen\_sort(x, (void*)cmp, cmp\_nodata)}.
12132
12133
Here are a few basic comparison functions, to be used with \kbd{cmp\_nodata}:
12134
12135
\fun{int}{ZV_cmp}{GEN x, GEN y} compare two \kbd{ZV}, which we assume have
12136
the same length (lexicographic order).
12137
12138
\fun{int}{cmp_Flx}{GEN x, GEN y} compare two \kbd{Flx}, which we assume
12139
have the same main variable (lexicographic order).
12140
12141
\fun{int}{cmp_RgX}{GEN x, GEN y} compare two polynomials, which we assume
12142
have the same main variable (lexicographic order). The coefficients are
12143
compared using \kbd{gcmp}.
12144
12145
\fun{int}{cmp_prime_over_p}{GEN x, GEN y} compare two prime ideals, which
12146
we assume divide the same prime number. The comparison is ad hoc but orders
12147
according to increasing residue degrees.
12148
12149
\fun{int}{cmp_prime_ideal}{GEN x, GEN y} compare two prime ideals in the same
12150
\var{nf}. Orders by increasing primes, breaking ties using
12151
\kbd{cmp\_prime\_over\_p}.
12152
12153
\fun{int}{cmp_padic}{GEN x, GEN y} compare two \typ{PADIC} (for the same
12154
prime $p$).
12155
12156
Finally a more elaborate comparison function:
12157
12158
\fun{int}{gen_cmp_RgX}{void *data, GEN x, GEN y} compare two polynomials,
12159
ordering first by increasing degree, then according to the coefficient
12160
comparison function:
12161
\bprog
12162
int (*cmp_coeff)(GEN,GEN) = (int(*)(GEN,GEN)) data;
12163
@eprog
12164
12165
\section{Divisibility, Euclidean division}
12166
12167
\fun{GEN}{gdivexact}{GEN x, GEN y} returns the quotient $\kbd{x} / \kbd{y}$,
12168
assuming $\kbd{y}$ divides $\kbd{x}$. Not stack clean if $y = 1$
12169
(we return $x$, not a copy).
12170
12171
\fun{int}{gdvd}{GEN x, GEN y} returns 1 (true) if \kbd{y} divides~\kbd{x},
12172
0~otherwise.
12173
12174
\fun{GEN}{gdiventres}{GEN x, GEN y} creates a 2-component vertical
12175
vector whose components are the true Euclidean quotient and remainder
12176
of \kbd{x} and~\kbd{y}.
12177
12178
\fun{GEN}{gdivent[z]}{GEN x, GEN y[, GEN z]} yields the true Euclidean
12179
quotient of \kbd{x} and the \typ{INT} or \typ{POL}~\kbd{y}, as per
12180
the \kbd{\bs} GP operator.
12181
12182
\fun{GEN}{gdiventsg}{long s, GEN y[, GEN z]}, as \kbd{gdivent}
12183
except that \kbd{x} is a \kbd{long}.
12184
12185
\fun{GEN}{gdiventgs[z]}{GEN x, long s[, GEN z]}, as \kbd{gdivent}
12186
except that \kbd{y} is a \kbd{long}.
12187
12188
\fun{GEN}{gmod[z]}{GEN x, GEN y[, GEN z]} yields the remainder of \kbd{x}
12189
modulo the \typ{INT} or \typ{POL}~\kbd{y}, as per the \kbd{\%} GP operator.
12190
A \typ{REAL} or \typ{FRAC} \kbd{y} is also allowed, in which case the
12191
remainder is the unique real $r$ such that $0 \leq r < |\kbd{y}|$ and
12192
$\kbd{y} = q\kbd{x} + r$ for some (in fact unique) integer $q$.
12193
12194
\fun{GEN}{gmodsg}{long s, GEN y[, GEN z]} as \kbd{gmod}, except \kbd{x} is
12195
a \kbd{long}.
12196
12197
\fun{GEN}{gmodgs}{GEN x, long s[, GEN z]} as \kbd{gmod}, except \kbd{y} is
12198
a \kbd{long}.
12199
12200
\fun{GEN}{gdivmod}{GEN x, GEN y, GEN *r} If \kbd{r} is not equal to
12201
\kbd{NULL} or \kbd{ONLY\_REM}, creates the (false) Euclidean quotient of
12202
\kbd{x} and~\kbd{y}, and puts (the address of) the remainder into~\kbd{*r}.
12203
If \kbd{r} is equal to \kbd{NULL}, do not create the remainder, and if
12204
\kbd{r} is equal to \kbd{ONLY\_REM}, create and output only the remainder.
12205
The remainder is created after the quotient and can be disposed of
12206
individually with a \kbd{cgiv(r)}.
12207
12208
\fun{GEN}{poldivrem}{GEN x, GEN y, GEN *r} same as \key{gdivmod} but
12209
specifically for \typ{POL}s~\kbd{x} and~\kbd{y}, not necessarily in the same
12210
variable. Either of \kbd{x} and \kbd{y} may also be scalars, treated as
12211
polynomials of degree $0$.
12212
12213
\fun{GEN}{gdeuc}{GEN x, GEN y} creates the Euclidean quotient of the
12214
\typ{POL}s~\kbd{x} and~\kbd{y}. Either of \kbd{x} and \kbd{y} may also be
12215
scalars, treated as polynomials of degree $0$.
12216
12217
\fun{GEN}{grem}{GEN x, GEN y} creates the Euclidean remainder of the
12218
\typ{POL}~\kbd{x} divided by the \typ{POL}~\kbd{y}. Either of \kbd{x} and
12219
\kbd{y} may also be scalars, treated as polynomials of degree $0$.
12220
12221
12222
\fun{GEN}{gdivround}{GEN x, GEN y} if \kbd{x} and \kbd{y} are real
12223
(\typ{INT}, \typ{REAL}, \typ{FRAC}), return the rounded Euclidean quotient of
12224
$x$ and $y$ as per the \kbd{\bs/} GP operator. Operate componentwise if
12225
\kbd{x} is a \typ{COL}, \typ{VEC} or \typ{MAT}. Otherwise as \key{gdivent}.
12226
12227
\fun{GEN}{centermod_i}{GEN x, GEN y, GEN y2}, as \kbd{centermodii},
12228
componentwise.
12229
12230
\fun{GEN}{centermod}{GEN x, GEN y}, as \kbd{centermod\_i}, except that
12231
\kbd{y2} is computed (and left on the stack for efficiency).
12232
12233
\fun{GEN}{ginvmod}{GEN x, GEN y} creates the inverse of \kbd{x} modulo \kbd{y}
12234
when it exists. \kbd{y} must be of type \typ{INT} (in which case \kbd{x} is
12235
of type \typ{INT}) or \typ{POL} (in which case \kbd{x} is either a scalar
12236
type or a \typ{POL}).
12237
12238
\section{GCD, content and primitive part}
12239
12240
\subsec{Generic}
12241
12242
\fun{GEN}{resultant}{GEN x, GEN y} creates the resultant of the \typ{POL}s
12243
\kbd{x} and~\kbd{y} computed using Sylvester's matrix (inexact inputs), a
12244
modular algorithm (inputs in $\Q[X]$) or the subresultant algorithm, as
12245
optimized by Lazard and Ducos. Either of \kbd{x} and \kbd{y} may also be
12246
scalars (treated as polynomials of degree $0$)
12247
12248
\fun{GEN}{ggcd}{GEN x, GEN y} creates the GCD of \kbd{x} and~\kbd{y}.
12249
12250
\fun{GEN}{glcm}{GEN x, GEN y} creates the LCM of \kbd{x} and~\kbd{y}.
12251
12252
\fun{GEN}{gbezout}{GEN x,GEN y, GEN *u,GEN *v} returns the GCD of \kbd{x}
12253
and~\kbd{y}, and puts (the addresses of) objects $u$ and~$v$ such that
12254
$u\kbd{x}+v\kbd{y}=\gcd(\kbd{x},\kbd{y})$ into \kbd{*u} and~\kbd{*v}.
12255
12256
\fun{GEN}{subresext}{GEN x, GEN y, GEN *U, GEN *V} returns the resultant
12257
of \kbd{x} and~\kbd{y}, and puts (the addresses of) polynomials $u$ and~$v$
12258
such that $u\kbd{x}+v\kbd{y}=\text{Res}(\kbd{x},\kbd{y})$ into \kbd{*U}
12259
and~\kbd{*V}.
12260
12261
\fun{GEN}{content}{GEN x} returns the GCD of all the components of~\kbd{x}.
12262
12263
\fun{GEN}{primitive_part}{GEN x, GEN *c} sets \kbd{c} to \kbd{content(x)}
12264
and returns the primitive part \kbd{x} / \kbd{c}. A trivial content is set to
12265
\kbd{NULL}.
12266
12267
\fun{GEN}{primpart}{GEN x} as above but the content is lost.
12268
(For efficiency, the content remains on the stack.)
12269
12270
\fun{GEN}{denom_i}{GEN x} shallow version of \kbd{denom}.
12271
12272
\fun{GEN}{numer_i}{GEN x} shallow version of \kbd{numer}.
12273
12274
\subsec{Over the rationals}
12275
12276
\fun{long}{Q_pval}{GEN x, GEN p} valuation at the \typ{INT} \kbd{p}
12277
of the \typ{INT} or \typ{FRAC}~\kbd{x}.
12278
12279
\fun{long}{Q_lval}{GEN x, ulong p} same for \kbd{ulong} $p$.
12280
12281
\fun{long}{Q_pvalrem}{GEN x, GEN p, GEN *r} returns the valuation $e$ at the
12282
\typ{INT} \kbd{p} of the \typ{INT} or \typ{FRAC}~\kbd{x}. The quotient
12283
$\kbd{x}/\kbd{p}^{e}$ is returned in~\kbd{*r}.
12284
12285
\fun{long}{Q_lvalrem}{GEN x, ulong p, GEN *r} same for \kbd{ulong} $p$.
12286
12287
\fun{GEN}{Q_abs}{GEN x} absolute value of the \typ{INT} or
12288
\typ{FRAC}~\kbd{x}.
12289
12290
\fun{GEN}{Qdivii}{GEN x, GEN y}, assuming $x$ and $y$
12291
are both of type \typ{INT}, return the quotient $x/y$ as a \typ{INT} or
12292
\typ{FRAC}; marginally faster than \kbd{gdiv}.
12293
12294
\fun{GEN}{Qdivis}{GEN x, long y}, assuming $x$
12295
is an \typ{INT}, return the quotient $x/y$ as a \typ{INT} or
12296
\typ{FRAC}; marginally faster than \kbd{gdiv}.
12297
12298
\fun{GEN}{Qdiviu}{GEN x, ulong y}, assuming $x$
12299
is an \typ{INT}, return the quotient $x/y$ as a \typ{INT} or
12300
\typ{FRAC}; marginally faster than \kbd{gdiv}.
12301
12302
\fun{GEN}{Q_abs_shallow}{GEN x} $x$ being a \typ{INT} or a \typ{FRAC}, returns
12303
a shallow copy of $|x|$, in particular returns $x$ itself when $x \geq 0$, and
12304
\kbd{gneg($x$)} otherwise.
12305
12306
\fun{GEN}{Q_gcd}{GEN x, GEN y} gcd of the \typ{INT} or \typ{FRAC}~\kbd{x}
12307
and~\kbd{y}.
12308
\smallskip
12309
12310
In the following functions, arguments belong to a $M\otimes_\Z\Q$
12311
for some natural $\Z$-module $M$, e.g. multivariate polynomials with integer
12312
coefficients (or vectors/matrices recursively built from such objects), and
12313
an element of $M$ is said to be \emph{integral}.
12314
We are interested in contents, denominators, etc. with respect to this
12315
canonical integral structure; in particular, contents belong to $\Q$,
12316
denominators to $\Z$. For instance the $\Q$-content of $(1/2)xy$ is $(1/2)$,
12317
and its $\Q$-denominator is $2$, whereas \kbd{content} would return $y/2$ and
12318
\kbd{denom}~1.
12319
12320
\fun{GEN}{Q_content}{GEN x} the $\Q$-content of $x$.
12321
12322
\fun{GEN}{Z_content}{GEN x} as \kbd{Q\_content} but assume that all rationals
12323
are in fact \typ{INT}s and return \kbd{NULL} when the content is $1$. This
12324
function returns as soon as the content is found to equal $1$.
12325
12326
\fun{GEN}{Q_content_safe}{GEN x} as \kbd{Q\_content}, returning
12327
\kbd{NULL} when the $\Q$-content is not defined (e.g. for a \typ{REAL}
12328
or \typ{INTMOD} component).
12329
12330
\fun{GEN}{Q_denom}{GEN x} the $\Q$-denominator of $x$. Shallow function.
12331
Raises en \kbd{e\_TYPE} error out when the notion is meaningless, e.g. for
12332
a \typ{REAL} or \typ{INTMOD} component.
12333
12334
\fun{GEN}{Q_denom_safe}{GEN x} the $\Q$-denominator of $x$. Shallow function.
12335
Return \kbd{NULL} when the notion is meaningless.
12336
12337
\fun{GEN}{Q_primitive_part}{GEN x, GEN *c} sets \kbd{c} to the $\Q$-content
12338
of \kbd{x} and returns \kbd{x / c}, which is integral.
12339
12340
\fun{GEN}{Q_primpart}{GEN x} as above but the content is lost. (For
12341
efficiency, the content remains on the stack.)
12342
12343
\fun{GEN}{vec_Q_primpart}{GEN x} as above component-wise. Applied to a
12344
\typ{MAT}, the result has primitive columns.
12345
12346
\fun{GEN}{row_Q_primpart}{GEN x} as above, applied to the rows of a
12347
\typ{MAT}, so that the result has primitive rows. Not \kbd{gerepile}-safe.
12348
12349
\fun{GEN}{Q_remove_denom}{GEN x, GEN *ptd} sets \kbd{d} to the
12350
$\Q$-denominator of \kbd{x} and returns \kbd{x * d}, which is integral.
12351
Shallow function.
12352
12353
\fun{GEN}{Q_div_to_int}{GEN x, GEN c} returns \kbd{x / c}, assuming $c$
12354
is a rational number (\typ{INT} or \typ{FRAC}) and the result is integral.
12355
12356
\fun{GEN}{Q_mul_to_int}{GEN x, GEN c} returns \kbd{x * c}, assuming $c$
12357
is a rational number (\typ{INT} or \typ{FRAC}) and the result is integral.
12358
12359
\fun{GEN}{Q_muli_to_int}{GEN x, GEN d} returns \kbd{x * c}, assuming $c$
12360
is a \typ{INT} and the result is integral.
12361
12362
\fun{GEN}{mul_content}{GEN cx, GEN cy} \kbd{cx} and \kbd{cy} are
12363
as set by \kbd{primitive\_part}: either a \kbd{GEN} or \kbd{NULL}
12364
representing the trivial content $1$. Returns their product (either a
12365
\kbd{GEN} or \kbd{NULL}).
12366
12367
\fun{GEN}{div_content}{GEN cx, GEN cy} \kbd{cx} and \kbd{cy} are
12368
as set by \kbd{primitive\_part}: either a \kbd{GEN} or \kbd{NULL}
12369
representing the trivial content $1$. Returns their quotient (either a
12370
\kbd{GEN} or \kbd{NULL}).
12371
12372
\fun{GEN}{inv_content}{GEN c} $c$ is as set by \kbd{primitive\_part}: either
12373
a \kbd{GEN} or \kbd{NULL} representing the trivial content $1$. Returns its
12374
inverse (either a \kbd{GEN} or \kbd{NULL}).
12375
12376
\fun{GEN}{mul_denom}{GEN dx, GEN dy} \kbd{dx} and \kbd{dy} are
12377
as set by \kbd{Q\_remove\_denom}: either a \typ{INT} or \kbd{NULL} representing
12378
the trivial denominator $1$. Returns their product (either a \typ{INT} or
12379
\kbd{NULL}).
12380
12381
\section{Generic arithmetic operators}
12382
12383
\subsec{Unary operators}
12384
12385
\fun{GEN}{gneg[z]}{GEN x[, GEN z]} yields $-\kbd{x}$.
12386
12387
\fun{GEN}{gneg_i}{GEN x} shallow function yielding $-\kbd{x}$.
12388
12389
\fun{GEN}{gabs[z]}{GEN x[, GEN z]} yields $|\kbd{x}|$.
12390
12391
\fun{GEN}{gsqr}{GEN x} creates the square of~\kbd{x}.
12392
12393
\fun{GEN}{ginv}{GEN x} creates the inverse of~\kbd{x}.
12394
12395
\subsec{Binary operators}
12396
12397
Let ``\op'' be a binary operation among
12398
12399
\op=\key{add}: addition (\kbd{x + y}).
12400
12401
\op=\key{sub}: subtraction (\kbd{x - y}).
12402
12403
\op=\key{mul}: multiplication (\kbd{x * y}).
12404
12405
\op=\key{div}: division (\kbd{x / y}).
12406
12407
\noindent The names and prototypes of the functions corresponding
12408
to \op\ are as follows:
12409
12410
\funno{GEN}{g\op}{GEN x, GEN y}
12411
12412
\funno{GEN}{g\op gs}{GEN x, long s}
12413
12414
\funno{GEN}{g\op sg}{long s, GEN y}
12415
12416
\noindent Explicitly
12417
12418
\fun{GEN}{gadd}{GEN x, GEN y}, \fun{GEN}{gaddgs}{GEN x, long s},
12419
\fun{GEN}{gaddsg}{long s, GEN x}
12420
12421
\fun{GEN}{gmul}{GEN x, GEN y}, \fun{GEN}{gmulgs}{GEN x, long s},
12422
\fun{GEN}{gmulsg}{long s, GEN x}
12423
12424
\fun{GEN}{gsub}{GEN x, GEN y}, \fun{GEN}{gsubgs}{GEN x, long s},
12425
\fun{GEN}{gsubsg}{long s, GEN x}
12426
12427
\fun{GEN}{gdiv}{GEN x, GEN y}, \fun{GEN}{gdivgs}{GEN x, long s},
12428
\fun{GEN}{gdivsg}{long s, GEN x}
12429
12430
12431
\fun{GEN}{gpow}{GEN x, GEN y, long l} creates $\kbd{x}^{\kbd{y}}$. If
12432
\kbd{y} is a \typ{INT}, return \kbd{powgi(x,y)} (the precision \kbd{l} is not
12433
taken into account). Otherwise, the result is $\exp(\kbd{y}*\log(\kbd{x}))$
12434
where exact arguments are converted to floats of precision~\kbd{l} in case of
12435
need; if there is no need, for instance if $x$ is a \typ{REAL}, $l$ is
12436
ignored. Indeed, if $x$ is a \typ{REAL}, the accuracy of $\log x$ is
12437
determined from the accuracy of $x$, it is no problem to multiply by $y$,
12438
even if it is an exact type, and the accuracy of the exponential is
12439
determined, exactly as in the case of the initial $\log x$.
12440
12441
\fun{GEN}{gpowgs}{GEN x, long n} creates $\kbd{x}^{\kbd{n}}$ using
12442
binary powering. To treat the special case $n = 0$, we consider
12443
\kbd{gpowgs} as a series of \kbd{gmul}, so we follow the rule of returning
12444
result which is as exact as possible given the input. More precisely,
12445
we return
12446
12447
\item \kbd{gen\_1} if $x$ has type \typ{INT}, \typ{REAL}, \typ{FRAC}, or
12448
\typ{PADIC}
12449
12450
\item \kbd{Mod(1,N)} if $x$ is a \typ{INTMOD} modulo $N$.
12451
12452
\item \kbd{gen\_1} for \typ{COMPLEX}, \typ{QUAD} unless one component
12453
is a \typ{INTMOD}, in which case we return \kbd{Mod(1, N)} for a suitable
12454
$N$ (the gcd of the moduli that appear).
12455
12456
\item \kbd{FF\_1}$(x)$ for a \typ{FFELT}.
12457
12458
\item \kbd{qfb\_1}$(x)$ for \typ{QFB}.
12459
12460
\item the identity permutation for \typ{VECSMALL}.
12461
12462
\item \kbd{Rg\_get\_1}$(x)$ otherwise
12463
12464
Of course, the only practical use of this routine for $n = 0$ is
12465
to obtain the multiplicative neutral element in the base ring (or to treat
12466
marginal cases that should be special cased anyway if there is the slightest
12467
doubt about what the result should be).
12468
12469
\fun{GEN}{powgi}{GEN x, GEN y} creates $\kbd{x}^{\kbd{y}}$, where \kbd{y} is a
12470
\typ{INT}, using left-shift binary powering. The case where $y = 0$
12471
(as all cases where $y$ is small) is handled by \kbd{gpowgs(x, 0)}.
12472
12473
\fun{GEN}{gpowers}{GEN x, long n} returns the vector $[1,x,\dots,x^n]$.
12474
12475
\fun{GEN}{grootsof1}{long n, long prec} returns the vector
12476
$[1,x,\dots,x^{n-1}]$, where $x$ is the $n$-th root of unity $\exp(2i\pi/n)$.
12477
12478
\fun{GEN}{gsqrpowers}{GEN x, long n} returns the vector $[x,x^4,\dots,x^{n^2}]$.
12479
12480
In addition we also have the obsolete forms:
12481
12482
\fun{void}{gaddz}{GEN x, GEN y, GEN z}
12483
12484
\fun{void}{gsubz}{GEN x, GEN y, GEN z}
12485
12486
\fun{void}{gmulz}{GEN x, GEN y, GEN z}
12487
12488
\fun{void}{gdivz}{GEN x, GEN y, GEN z}
12489
12490
\section{Generic operators: product, powering, factorback}
12491
12492
To describe the following functions, we use the following private typedefs
12493
to simplify the description:
12494
\bprog
12495
typedef (*F0)(void *);
12496
typedef (*F1)(void *, GEN);
12497
typedef (*F2)(void *, GEN, GEN);
12498
@eprog
12499
\noindent They correspond to generic functions with one and two arguments
12500
respectively (the \kbd{void*} argument provides some arbitrary evaluation
12501
context).
12502
12503
\fun{GEN}{gen_product}{GEN v, void *D, F2 op}
12504
Given two objects $x,y$, assume that \kbd{op(D, $x$, $y$)} implements an
12505
associative binary operator. If $v$ has $k$ entries, return
12506
$$v[1]~\var{op}~v[2]~\var{op}~\ldots ~\var{op}~v[k];$$
12507
returns \kbd{gen\_1} if $k = 0$ and a copy of $v[1]$ if $k = 1$.
12508
Use divide and conquer strategy. Leave some garbage on stack, but suitable for
12509
\kbd{gerepileupto} if \kbd{mul} is.
12510
12511
\fun{GEN}{gen_pow}{GEN x, GEN n, void *D, F1 sqr, F2 mul} $n > 0$ a
12512
\typ{INT}, returns $x^n$; \kbd{mul(D, $x$, $y$)} implements the multiplication
12513
in the underlying monoid; \kbd{sqr} is a (presumably optimized) shortcut for
12514
\kbd{mul(D, $x$, $x$)}.
12515
12516
\fun{GEN}{gen_powu}{GEN x, ulong n, void *D, F1 sqr, F2 mul} $n > 0$,
12517
returns $x^n$. See \tet{gen_pow}.
12518
12519
\fun{GEN}{gen_pow_i}{GEN x, GEN n, void *E, F1 sqr, F2 mul}
12520
internal variant of \tet{gen_pow}, not memory-clean.
12521
12522
\fun{GEN}{gen_powu_i}{GEN x, ulong n, void *E, F1 sqr, F2 mul}
12523
internal variant of \tet{gen_powu}, not memory-clean.
12524
12525
\fun{GEN}{gen_pow_fold}{GEN x, GEN n, void *D, F1 sqr, F1 msqr} variant
12526
of \tet{gen_pow}, where \kbd{mul} is replaced by \kbd{msqr}, with
12527
\kbd{msqr(D, $y$)} returning $xy^2$. In particular \kbd{D} must implicitly
12528
contain $x$.
12529
12530
\fun{GEN}{gen_pow_fold_i}{GEN x, GEN n, void *E, F1 sqr, F1 msqr}
12531
internal variant of the function \tet{gen_pow_fold}, not memory-clean.
12532
12533
\fun{GEN}{gen_powu_fold}{GEN x, ulong n, void *D, F1 sqr, F1 msqr}, see
12534
\tet{gen_pow_fold}.
12535
12536
\fun{GEN}{gen_powu_fold_i}{GEN x, ulong n, void *E, F1 sqr, F1 msqr}
12537
see \tet{gen_pow_fold_i}.
12538
12539
\fun{GEN}{gen_pow_init}{GEN x, GEN n, long k, void *E, GEN (*sqr)(void*,GEN),
12540
GEN (*mul)(void*,GEN,GEN)}
12541
Return a table \kbd{R} that can be used with
12542
\kbd{gen\_pow\_table} to compute the powers of $x$ up to $n$.
12543
The table is of size $2^k\*\log_2(n)$.
12544
12545
\fun{GEN}{gen_pow_table}{GEN R, GEN n, void *E, GEN (*one)(void*),
12546
GEN (*mul)(void*,GEN,GEN)}
12547
12548
Return $x^n$, where $R$ is as given by \kbd{gen\_pow\_init(x,m,k,E,sqr,mul)}
12549
for some integer $m\geq n$.
12550
12551
\fun{GEN}{gen_powers}{GEN x, long n, long usesqr, void *D, F1 sqr, F2 mul, F0 one}
12552
returns $[\kbd{x}^0, \dots, \kbd{x}^\kbd{n}]$ as a \typ{VEC}; \kbd{mul(D,
12553
$x$, $y$)} implements the multiplication in the underlying monoid; \kbd{sqr}
12554
is a (presumably optimized) shortcut for \kbd{mul(D, $x$, $x$)}; \kbd{one}
12555
returns the monoid unit. The flag \kbd{usesqr} should be set to $1$ if
12556
squaring are faster than multiplication by $x$.
12557
12558
\fun{GEN}{gen_factorback}{GEN L, GEN e, void *D, F2 mul, F2 pow, GEN (*one)(void *)D}
12559
generic form of \tet{factorback}. The pair $[L,e]$ is of the form
12560
12561
\item \kbd{[fa, NULL]}, \kbd{fa} a two-column factorization matrix: expand it.
12562
12563
\item \kbd{[v, NULL]}, $v$ a vector of objects: return their product.
12564
12565
\item or \kbd{[v, e]}, $v$ a vector of objects, $e$ a vector of integral
12566
exponents (a \kbd{ZV} or \kbd{zv}): return the product of the $v[i]^{e[i]}$.
12567
12568
\noindent \kbd{mul(D, $x$, $y$)} and \kbd{pow(D, $x$, $n$)}
12569
return $xy$ and $x^n$ respectively.
12570
12571
\noindent \kbd{one(D)} returns the neutral element. If \kbd{one} is \kbd{NULL},
12572
\kbd{gen\_1} is used instead.
12573
12574
\section{Matrix and polynomial norms} This section concerns only standard norms
12575
of $\R$ and $\C$ vector spaces, not algebraic norms given by the determinant of
12576
some multiplication operator. We have already seen type-specific functions like
12577
\tet{ZM_supnorm} or \tet{RgM_fpnorml2} and limit ourselves to generic functions
12578
assuming nothing about their \kbd{GEN} argument; these functions allow
12579
the following scalar types: \typ{INT}, \typ{FRAC}, \typ{REAL}, \typ{COMPLEX},
12580
\typ{QUAD} and are defined recursively (in terms of norms of their components)
12581
for the following ``container'' types: \typ{POL}, \typ{VEC}, \typ{COL} and
12582
\typ{MAT}. They raise an error if some other type appears in the argument.
12583
12584
\fun{GEN}{gnorml2}{GEN x} The norm of a scalar is the square of its complex
12585
modulus, the norm of a recursive type is the sum of the norms of its components.
12586
For polynomials, vectors or matrices of complex numbers one recovers the
12587
\emph{square} of the usual $L^2$ norm. In most applications, the missing square
12588
root computation can be skipped.
12589
12590
\fun{GEN}{gnorml1}{GEN x, long prec} The norm of a scalar is its complex
12591
modulus, the norm of a recursive type is the sum of the norms of its components.
12592
For polynomials, vectors or matrices of complex numbers one recovers
12593
the usual $L^1$ norm. One must include a real precision \kbd{prec} in case
12594
the inputs include \typ{COMPLEX} or \typ{QUAD} with exact rational components:
12595
a square root must be computed and we must choose an accuracy.
12596
12597
\fun{GEN}{gnorml1_fake}{GEN x} as \tet{gnorml1}, except that the norm
12598
of a \typ{QUAD} $x + wy$ or \typ{COMPLEX} $x + Iy$ is defined as
12599
$|x| + |y|$, where we use the ordinary real absolute value. This is still a norm
12600
of $\R$ vector spaces, which is easier to compute than
12601
\kbd{gnorml1} and can often be used in its place.
12602
12603
\fun{GEN}{gsupnorm}{GEN x, long prec} The norm of a scalar is its complex
12604
modulus, the norm of a recursive type is the max of the norms of its
12605
components. A precision \kbd{prec} must be included for the same reason as in
12606
\kbd{gnorml1}.
12607
12608
\fun{void}{gsupnorm_aux}{GEN x, GEN *m, GEN *m2, long prec}
12609
is the low-level function underlying
12610
\kbd{gsupnorm}, used as follows:
12611
\bprog
12612
GEN m = NULL, m2 = NULL;
12613
gsupnorm_aux(x, &m, &m2);
12614
@eprog
12615
After the call, the sup norm of $x$ is the min of \kbd{m} and the square root
12616
of \kbd{m2}; one or both of \kbd{m}, \kbd{m2} may be \kbd{NULL}, in
12617
which case it must be omitted. You may initially set \kbd{m} and \kbd{m2} to
12618
non-\kbd{NULL} values, in which case, the above procedure yields the max of
12619
(the initial) \kbd{m}, the square root of (the initial) \kbd{m2}, and the sup
12620
norm of $x$.
12621
12622
The strange interface is due to the fact that $|z|^2$ is easier to compute
12623
than $|z|$ for a \typ{QUAD} or \typ{COMPLEX} $z$: \kbd{m2} is the max of
12624
those $|z|^2$, and \kbd{m} is the max of the other $|z|$.
12625
12626
\section{Substitution and evaluation}
12627
12628
\fun{GEN}{gsubst}{GEN x, long v, GEN y} substitutes the object \kbd{y}
12629
into~\kbd{x} for the variable number~\kbd{v}.
12630
12631
\fun{GEN}{poleval}{GEN q, GEN x} evaluates the \typ{POL} or \typ{RFRAC}
12632
$q$ at $x$. For convenience, a \typ{VEC} or \typ{COL} is also recognized as
12633
the \typ{POL} \kbd{gtovecrev(q)}.
12634
12635
\fun{GEN}{RgX_cxeval}{GEN T, GEN x, GEN xi} evaluate the \typ{POL} $T$
12636
at $x$ via Horner's scheme. If \var{xi} is not \kbd{NULL} it must be equal to
12637
$1/x$ and we evaluate $x^{\deg T}T(1/x)$ instead. This is useful when
12638
$|x| > 1$ is a \typ{REAL} or an inexact \typ{COMPLEX} and $T$ has
12639
``balanced'' coefficients, since the evaluation becomes numerically stable.
12640
12641
\fun{GEN}{RgX_RgM_eval}{GEN q, GEN x} evaluates the \typ{POL} $q$ at the
12642
square matrix $x$.
12643
12644
\fun{GEN}{RgX_RgMV_eval}{GEN f, GEN V} returns
12645
the evaluation $\kbd{f}(\kbd{x})$, assuming that \kbd{V} was computed by
12646
$\kbd{FpXQ\_powers}(\kbd{x}, n)$ for some $n>1$.
12647
12648
\fun{GEN}{qfeval}{GEN q, GEN x} evaluates the quadratic form
12649
$q$ (symmetric matrix) at $x$ (column vector of compatible dimensions).
12650
12651
\fun{GEN}{qfevalb}{GEN q, GEN x, GEN y} evaluates the polar bilinear form
12652
attached to the quadratic form $q$ (symmetric matrix) at $x$, $y$ (column
12653
vectors of compatible dimensions).
12654
12655
\fun{GEN}{hqfeval}{GEN q, GEN x} evaluates the Hermitian form $q$
12656
(a Hermitian complex matrix) at $x$.
12657
12658
\fun{GEN}{qf_apply_RgM}{GEN q, GEN M} $q$ is a symmetric $n\times n$ matrix,
12659
$M$ an $n\times k$ matrix, return $M' q M$.
12660
12661
\fun{GEN}{qf_apply_ZM}{GEN q, GEN M} as above assuming that both
12662
$q$ and $M$ have integer entries.
12663
12664
\newpage
12665
\chapter{Miscellaneous mathematical functions}
12666
12667
\section{Fractions}
12668
12669
\fun{GEN}{absfrac}{GEN x} returns the absolute value of the \typ{FRAC} $x$.
12670
12671
\fun{GEN}{absfrac_shallow}{GEN x} $x$ being a \typ{FRAC}, returns a shallow
12672
copy of $|x|$, in particular returns $x$ itself when $x \geq 0$, and
12673
\kbd{gneg($x$)} otherwise.
12674
12675
\fun{GEN}{sqrfrac}{GEN x} returns the square of the \typ{FRAC} $x$.
12676
12677
\section{Binomials}
12678
12679
\fun{GEN}{binomial}{GEN x, long k}
12680
12681
\fun{GEN}{binomialuu}{ulong n, ulong k}
12682
12683
\fun{GEN}{vecbinomial}{long n}, which returns a vector $v$ with $n+1$
12684
\typ{INT} components such that $v[k+1] = \kbd{binomial}(n,k)$ for $k$ from
12685
$0$ up to $n$.
12686
12687
\section{Real numbers}
12688
12689
\fun{GEN}{R_abs}{GEN x} $x$ being a \typ{INT}, a \typ{REAL} or a
12690
\typ{FRAC}, returns $|x|$.
12691
12692
\fun{GEN}{R_abs_shallow}{GEN x} $x$ being a \typ{INT}, a \typ{REAL} or a
12693
\typ{FRAC}, returns a shallow copy of $|x|$, in particular returns $x$ itself
12694
when $x \geq 0$, and \kbd{gneg($x$)} otherwise.
12695
12696
\fun{GEN}{modRr_safe}{GEN x, GEN y} let $x$ be a \typ{INT}, a \typ{REAL} or
12697
\typ{FRAC} and let $y$ be a \typ{REAL}. Return $x\% y$ unless the input
12698
accuracy is unsufficient to compute the floor or $x/y$ in which case we
12699
return \kbd{NULL}.
12700
12701
\section{Complex numbers}
12702
12703
\fun{GEN}{gimag}{GEN x} returns a copy of the imaginary part of \kbd{x}.
12704
12705
\fun{GEN}{greal}{GEN x} returns a copy of the real part of \kbd{x}. If \kbd{x}
12706
is a \typ{QUAD}, returns the coefficient of $1$ in the ``canonical'' integral
12707
basis $(1,\omega)$.
12708
12709
\fun{GEN}{gconj}{GEN x} returns $\kbd{greal}(x) - 2\kbd{gimag}(x)$, which is
12710
the ordinary complex conjugate except for a real \typ{QUAD}.
12711
12712
\fun{GEN}{imag_i}{GEN x}, shallow variant of \kbd{gimag}.
12713
12714
\fun{GEN}{real_i}{GEN x}, shallow variant of \kbd{greal}.
12715
12716
\fun{GEN}{conj_i}{GEN x}, shallow variant of \kbd{gconj}.
12717
12718
\fun{GEN}{mulreal}{GEN x, GEN y} returns the real part of $x\*y$;
12719
$x$, $y$ have type \typ{INT}, \typ{FRAC}, \typ{REAL} or \typ{COMPLEX}. See also
12720
\kbd{RgM\_mulreal}.
12721
12722
\fun{GEN}{cxnorm}{GEN x} norm of the \typ{COMPLEX} $x$ (modulus squared).
12723
12724
\fun{GEN}{cxexpm1}{GEN x} returns $\exp(x)-1$, for a \typ{COMPLEX} $x$.
12725
12726
\fun{int}{cx_approx_equal}{GEN a, GEN b} test whether (\typ{INT}, \typ{FRAC},
12727
\typ{REAL}, or \typ{COMPLEX} of those) $a$ and $b$ are approximately equal.
12728
This returns $1$ if and only if the division by $a-b$ would produce a
12729
division by $0$ (which is a less stringent test than testing whether $a-b$
12730
evaluates to $0$).
12731
12732
\section{Quadratic numbers and binary quadratic forms}
12733
12734
\fun{GEN}{quad_disc}{GEN x} returns the discriminant of the \typ{QUAD} $x$.
12735
Not stack-clean but suitable for \kbd{gerepileupto}.
12736
12737
\fun{GEN}{quadnorm}{GEN x} norm of the \typ{QUAD} $x$.
12738
12739
\fun{GEN}{qfb_disc}{GEN x} returns the discriminant of the \typ{QFB}
12740
\kbd{x}.
12741
12742
\fun{GEN}{qfb_disc3}{GEN x, GEN y, GEN z} returns $y^2 - 4xz$ assuming all
12743
inputs are \typ{INT}s. Not stack-clean.
12744
12745
\fun{GEN}{qfb_apply_ZM}{GEN q, GEN g} returns $q \circ g$.
12746
12747
\fun{GEN}{qfbforms}{GEN D} given a discriminant $D < 0$, return the list
12748
of reduced forms of discriminant $D$ as \typ{VECSMALL} with 3 components.
12749
The primitive forms in the list enumerate the class group of the quadratic
12750
order of discriminant $D$; if $D$ is fundamental, all returned forms
12751
are automatically primitive.
12752
12753
\section{Polynomials}\label{se:polynomials}
12754
12755
\fun{GEN}{truecoef}{GEN x, long n} returns \kbd{polcoef(x,n, -1)}, i.e.
12756
the coefficient of the term of degree \kbd{n} in the main variable. This is
12757
a safe but expensive function that must \emph{copy} its return value so that
12758
it be \kbd{gerepile}-safe. Use \kbd{polcoef\_i} for a fast internal variant.
12759
12760
\fun{GEN}{polcoef_i}{GEN x, long n, long v} internal shallow function. Rewrite
12761
$x$ as a Laurent polynomial in the variable $v$ and returns its coefficient
12762
of degree $n$ (\kbd{gen\_0} if this falls outside the coefficient array).
12763
Allow \typ{POL}, \typ{SER}, \typ{RFRAC} and scalars.
12764
12765
\fun{long}{degree}{GEN x} returns \kbd{poldegree(x, -1)}, the degree of
12766
\kbd{x} with respect to its main variable, with the usual meaning if the
12767
leading coefficient of $x$ is nonzero. If the sign of $x$ is $0$, this
12768
function always returns $-1$. Otherwise, we return the index of the leading
12769
coefficient of $x$, i.e. the coefficient of largest index stored in $x$.
12770
For instance the ``degrees'' of
12771
\bprog
12772
0. E-38 * x^4 + 0.E-19 * x + 1
12773
Mod(0,2) * x^0 \\ sign is 0 !
12774
@eprog\noindent are $4$ and $-1$ respectively.
12775
12776
\fun{long}{degpol}{GEN x} is a simple macro returning \kbd{lg(x) - 3}.
12777
This is the degree of the \typ{POL}~\kbd{x} with respect to its main
12778
variable, \emph{if} its leading coefficient is nonzero (a rational $0$ is
12779
impossible, but an inexact $0$ is allowed, as well as an exact modular $0$,
12780
e.g. \kbd{Mod(0,2)}). If $x$ has no coefficients (rational $0$ polynomial),
12781
its length is $2$ and we return the expected $-1$.
12782
12783
\fun{GEN}{characteristic}{GEN x} returns the characteristic of the
12784
base ring over which the polynomial is defined (as defined by \typ{INTMOD}
12785
and \typ{FFELT} components). The function raises an exception if incompatible
12786
primes arise from \typ{FFELT} and \typ{PADIC} components. Shallow function.
12787
12788
\fun{GEN}{residual_characteristic}{GEN x} returns a kind of ``residual
12789
characteristic'' of the base ring over which the polynomial is defined. This
12790
is defined as the gcd of all moduli \typ{INTMOD}s occurring in the structure,
12791
as well as primes $p$ arising from \typ{PADIC}s or \typ{FFELT}s. The function
12792
raises an exception if incompatible primes arise from \typ{FFELT} and
12793
\typ{PADIC} components. Shallow function.
12794
12795
\fun{GEN}{resultant}{GEN x,GEN y} resultant of \kbd{x} and \kbd{y}, with
12796
respect to the main variable of highest priority. Uses either the
12797
subresultant algorithm (generic case), a modular algorithm (inputs in
12798
$\Q[X]$) or Sylvester's matrix (inexact inputs).
12799
12800
\fun{GEN}{resultant2}{GEN x, GEN y} resultant of \kbd{x} and \kbd{y}, with
12801
respect to the main variable of highest priority. Computes the determinant
12802
of Sylvester's matrix.
12803
12804
\fun{GEN}{cleanroots}{GEN x, long prec} returns the complex roots of
12805
the complex polynomial $x$ (with coefficients \typ{INT}, \typ{FRAC},
12806
\typ{REAL} or \typ{COMPLEX} of the above). The roots are returned
12807
as \typ{REAL} or \typ{COMPLEX} of \typ{REAL}s of precision \kbd{prec}
12808
(guaranteeing a nonzero imaginary part). See \tet{QX_complex_roots}.
12809
12810
\fun{double}{fujiwara_bound}{GEN x} return a quick upper bound for the
12811
logarithm in base $2$ of the modulus of the largest complex roots of
12812
the polynomial $x$ (complex coefficients).
12813
12814
\fun{double}{fujiwara_bound_real}{GEN x, long sign} return a quick upper
12815
bound for the logarithm in base $2$ of the absolute value of the largest
12816
real root of sign \var{sign} ($1$ or $-1$), for the polynomial $x$ (real
12817
coefficients).
12818
12819
\fun{GEN}{polmod_to_embed}{GEN x, long prec} return the vector of complex
12820
embeddings of the \typ{POLMOD} $x$ (with complex coefficients). Shallow
12821
function, simple complex variant of \tet{conjvec}.
12822
12823
\fun{GEN}{pollegendre_reduced}{long n, long v} let $P_n(t)\in \Q[t]$
12824
be the $n$-th Legendre polynomial in variable $v$. Return $p\in \Z[t]$
12825
such that $2^n P_n(t) = p(t^2)$ ($n$ even) or $t p(t^2)$ ($n$ odd).
12826
12827
\section{Power series}
12828
12829
\fun{GEN}{sertoser}{GEN x, long prec} return the \typ{SER} $x$ truncated
12830
or extended (with zeros) to \kbd{prec} terms. Shallow function, assume
12831
that $\kbd{prec} \geq 0$.
12832
12833
\fun{GEN}{derivser}{GEN x} returns the derivative of the \typ{SER} \kbd{x}
12834
with respect to its main variable.
12835
12836
\fun{GEN}{integser}{GEN x} returns the primitive of the \typ{SER} \kbd{x}
12837
with respect to its main variable.
12838
12839
\fun{GEN}{truecoef}{GEN x, long n} returns \kbd{polcoef(x,n, -1)}, i.e.
12840
the coefficient of the term of degree \kbd{n} in the main variable. This is a
12841
safe but expensive function that must \emph{copy} its return value so that it
12842
be \kbd{gerepile}-safe. Use \kbd{polcoef\_i} for a fast internal variant.
12843
12844
\fun{GEN}{ser_unscale}{GEN P, GEN h} return $P(h x)$, not memory clean.
12845
12846
\fun{GEN}{ser_normalize}{GEN x} divide $x$ by its ``leading term'' so that
12847
the series is either $0$ or equal to $t^v(1+O(t))$. Shallow function if the
12848
``leading term'' is $1$.
12849
12850
\fun{int}{ser_isexactzero}{GEN x} return $1$ if $x$ is a zero series, all
12851
of whose known coefficients are exact zeroes; this implies that
12852
$\kbd{sign}(x) = 0$ and $\kbd{lg}(x) \leq 3$.
12853
12854
\fun{GEN}{ser_inv}{GEN x} return the inverse of the \typ{SER} $x$ using
12855
Newton iteration. This is in general slower than \kbd{ginv} unless the
12856
precision is huge (hundreds of terms, where the threshold depends strongly
12857
on the base field).
12858
12859
\fun{GEN}{psi1series}{long n, long v, long prec} creates the \typ{SER}
12860
$\psi(1 + x + O(x^n))$ in variable $v$.
12861
12862
\section{Functions to handle \typ{FFELT}}
12863
These functions define the public interface of the \typ{FFELT} type to use in
12864
generic functions. However, in specific functions, it is better to use the
12865
functions class \kbd{FpXQ} and/or \kbd{Flxq} as appropriate.
12866
12867
\fun{GEN}{FF_p}{GEN a} returns the characteristic of the definition field of the
12868
\typ{FFELT} element \kbd{a}.
12869
12870
\fun{long}{FF_f}{GEN a} returns the dimension of the definition field over
12871
its prime field; the cardinality of the dimension field is thus $p^f$.
12872
12873
\fun{GEN}{FF_p_i}{GEN a} shallow version of \kbd{FF\_p}.
12874
12875
\fun{GEN}{FF_q}{GEN a} returns the cardinality of the definition field of the
12876
\typ{FFELT} element \kbd{a}.
12877
12878
\fun{GEN}{FF_mod}{GEN a} returns the polynomial (with reduced \typ{INT}
12879
coefficients) defining the finite field, in the variable used to display $a$.
12880
12881
\fun{long}{FF_var}{GEN a} returns the variable used to display $a$.
12882
12883
\fun{GEN}{FF_gen}{GEN a} returns the standard generator of the definition field
12884
of the \typ{FFELT} element \kbd{a}, see \kbd{ffgen}, that is $x\pmod{T}$ where
12885
$T$ is the polynomial over the prime field that define the finite field.
12886
12887
\fun{GEN}{FF_to_FpXQ}{GEN a} converts the \typ{FFELT} \kbd{a} to a polynomial
12888
$P$ with reduced \typ{INT} coefficients such that $a=P(g)$ where $g$ is the
12889
generator of the finite field returned by \kbd{ffgen}, in the variable used to
12890
display $g$.
12891
12892
\fun{GEN}{FF_to_FpXQ_i}{GEN a} shallow version of \kbd{FF\_to\_FpXQ}.
12893
12894
\fun{GEN}{FF_to_F2xq}{GEN a} converts the \typ{FFELT} \kbd{a} to a \kbd{F2x}
12895
$P$ such that $a=P(g)$ where $g$ is the generator of the finite field returned
12896
by \kbd{ffgen}, in the variable used to display $g$. This only work if the
12897
characteristic is $2$.
12898
12899
\fun{GEN}{FF_to_F2xq_i}{GEN a} shallow version of \kbd{FF\_to\_F2xq}.
12900
12901
\fun{GEN}{FF_to_Flxq}{GEN a} converts the \typ{FFELT} \kbd{a} to a \kbd{Flx}
12902
$P$ such that $a=P(g)$ where $g$ is the generator of the finite field returned
12903
by \kbd{ffgen}, in the variable used to display $g$. This only work if the
12904
characteristic is small enough.
12905
12906
\fun{GEN}{FF_to_Flxq_i}{GEN a} shallow version of \kbd{FF\_to\_Flxq}.
12907
12908
\fun{GEN}{p_to_FF}{GEN p, long v} returns a \typ{FFELT} equal to $1$ in the
12909
finite field $\Z/p\Z$. Useful for generic code that wants to handle
12910
(inefficiently) $\Z/p\Z$ as if it were not a prime field.
12911
12912
\fun{GEN}{Tp_to_FF}{GEN T, GEN p} returns a \typ{FFELT} equal to $1$ in the
12913
finite field $\F_p/(T)$, where $T$ is a \kbd{ZX}, assumed to be irreducible
12914
modulo $p$, or \kbd{NULL} in which case the routine acts as \tet{p_to_FF(p,0)}.
12915
No checks.
12916
12917
\fun{GEN}{Fq_to_FF}{GEN x, GEN ff} returns a \typ{FFELT} equal to $x$
12918
in the finite field defined by the \typ{FFELT} \kbd{ff}, where
12919
$x$ is an \kbd{Fq} (either a \typ{INT} or a \kbd{ZX}: a \typ{POL} with
12920
\typ{INT} coefficients). No checks.
12921
12922
\fun{GEN}{FqX_to_FFX}{GEN x, GEN ff} given an \kbd{FqX} $x$,
12923
return the polynomial with \typ{FFELT} coefficients obtained by
12924
applying \tet{Fq_to_FF} coefficientwise. No checks, and no normalization
12925
if the leading coefficient maps to $0$.
12926
12927
\fun{GEN}{FF_1}{GEN a} returns the unity in the definition field of the
12928
\typ{FFELT} element \kbd{a}.
12929
12930
\fun{GEN}{FF_zero}{GEN a} returns the zero element of the definition field of
12931
the \typ{FFELT} element \kbd{a}.
12932
12933
\fun{int}{FF_equal0}{GEN a} returns $1$ if the \typ{FFELT} \kbd{a} is equal
12934
to $0$ else returns $0$.
12935
12936
\fun{int}{FF_equal1}{GEN a} returns $1$ if the \typ{FFELT} \kbd{a} is equal
12937
to $1$ else returns $0$.
12938
12939
\fun{int}{FF_equalm1}{GEN a} returns $-1$ if the \typ{FFELT} \kbd{a} is equal
12940
to $1$ else returns $0$.
12941
12942
\fun{int}{FF_equal}{GEN a, GEN b} return $1$ if the \typ{FFELT} \kbd{a} and
12943
\kbd{b} have the same definition field and are equal, else $0$.
12944
12945
\fun{int}{FF_samefield}{GEN a, GEN b} return $1$ if the \typ{FFELT} \kbd{a} and
12946
\kbd{b} have the same definition field, else $0$.
12947
12948
\fun{int}{Rg_is_FF}{GEN c, GEN *ff} to be called successively on many objects,
12949
setting \kbd{*ff = NULL} (unset) initially. Returns $1$ as long as $c$ is a
12950
\typ{FFELT} defined over the same field as \kbd{*ff} (setting \kbd{*ff = c}
12951
if unset), and $0$ otherwise.
12952
12953
\fun{int}{RgC_is_FFC}{GEN x, GEN *ff} apply \tet{Rg_is_FF} successively to all
12954
components of the \typ{VEC} or \typ{COL} $x$. Return $0$ if one call fails,
12955
and $1$ otherwise.
12956
12957
\fun{int}{RgM_is_FFM}{GEN x, GEN *ff} apply \tet{Rg_is_FF} to all components
12958
of the \typ{MAT}. Return $0$ if one call fails, and $1$ otherwise.
12959
12960
\fun{GEN}{FF_add}{GEN a, GEN b} returns $a+b$ where \kbd{a} and \kbd{b} are
12961
\typ{FFELT} having the same definition field.
12962
12963
\fun{GEN}{FF_Z_add}{GEN a, GEN x} returns $a+x$, where \kbd{a} is a
12964
\typ{FFELT}, and \kbd{x} is a \typ{INT}, the computation being
12965
performed in the definition field of \kbd{a}.
12966
12967
\fun{GEN}{FF_Q_add}{GEN a, GEN x} returns $a+x$, where \kbd{a} is a
12968
\typ{FFELT}, and \kbd{x} is a \typ{RFRAC}, the computation being
12969
performed in the definition field of \kbd{a}.
12970
12971
\fun{GEN}{FF_sub}{GEN a, GEN b} returns $a-b$ where \kbd{a} and \kbd{b} are
12972
\typ{FFELT} having the same definition field.
12973
12974
\fun{GEN}{FF_mul}{GEN a, GEN b} returns $a\*b$ where \kbd{a} and \kbd{b} are
12975
\typ{FFELT} having the same definition field.
12976
12977
\fun{GEN}{FF_Z_mul}{GEN a, GEN b} returns $a\*b$, where \kbd{a} is a
12978
\typ{FFELT}, and \kbd{b} is a \typ{INT}, the computation being
12979
performed in the definition field of \kbd{a}.
12980
12981
\fun{GEN}{FF_div}{GEN a, GEN b} returns $a/b$ where \kbd{a} and \kbd{b} are
12982
\typ{FFELT} having the same definition field.
12983
12984
\fun{GEN}{FF_neg}{GEN a} returns $-a$ where \kbd{a} is a \typ{FFELT}.
12985
12986
\fun{GEN}{FF_neg_i}{GEN a} shallow function returning $-a$ where \kbd{a} is a
12987
\typ{FFELT}.
12988
12989
\fun{GEN}{FF_inv}{GEN a} returns $a^{-1}$ where \kbd{a} is a \typ{FFELT}.
12990
12991
\fun{GEN}{FF_sqr}{GEN a} returns $a^2$ where \kbd{a} is a \typ{FFELT}.
12992
12993
\fun{GEN}{FF_mul2n}{GEN a, long n} returns $a\*2^n$ where \kbd{a} is a
12994
\typ{FFELT}.
12995
12996
\fun{GEN}{FF_pow}{GEN a, GEN n} returns $a^n$ where \kbd{a} is a \typ{FFELT}
12997
and \kbd{n} is a \typ{INT}.
12998
12999
\fun{GEN}{FF_Frobenius}{GEN a, GEN n} returns $x^{p^n}$ where \kbd{x} is
13000
the standard generator of the definition field of the \typ{FFELT} element
13001
\kbd{a}, \typ{FFELT}, \kbd{n} is a \typ{INT}, and $p$ is the characteristic
13002
of the definition field of $a$.
13003
13004
\fun{GEN}{FF_Z_Z_muldiv}{GEN a, GEN x, GEN y} returns $a\*y/z$, where \kbd{a}
13005
is a \typ{FFELT}, and \kbd{x} and \kbd{y} are \typ{INT}, the computation being
13006
performed in the definition field of \kbd{a}.
13007
13008
\fun{GEN}{Z_FF_div}{GEN x, GEN a} return $x/a$ where \kbd{a} is a
13009
\typ{FFELT}, and \kbd{x} is a \typ{INT}, the computation being
13010
performed in the definition field of \kbd{a}.
13011
13012
\fun{GEN}{FF_norm}{GEN a} returns the norm of the \typ{FFELT} \kbd{a} with
13013
respect to its definition field.
13014
13015
\fun{GEN}{FF_trace}{GEN a} returns the trace of the \typ{FFELT} \kbd{a} with
13016
respect to its definition field.
13017
13018
\fun{GEN}{FF_conjvec}{GEN a} returns the vector of conjugates
13019
$[a,a^p,a^{p^2},\ldots,a^{p^{n-1}}]$ where the \typ{FFELT} \kbd{a} belong to a
13020
field with $p^n$ elements.
13021
13022
\fun{GEN}{FF_charpoly}{GEN a} returns the characteristic polynomial) of the
13023
\typ{FFELT} \kbd{a} with respect to its definition field.
13024
13025
\fun{GEN}{FF_minpoly}{GEN a} returns the minimal polynomial of
13026
the \typ{FFELT} \kbd{a}.
13027
13028
\fun{GEN}{FF_sqrt}{GEN a} returns an \typ{FFELT} $b$ such that $a=b^2$ if
13029
it exist, where \kbd{a} is a \typ{FFELT}.
13030
13031
\fun{long}{FF_issquareall}{GEN x, GEN *pt} returns $1$ if \kbd{x} is a
13032
square, and $0$ otherwise. If \kbd{x} is indeed a square, set \kbd{pt} to its
13033
square root.
13034
13035
\fun{long}{FF_issquare}{GEN x} returns $1$ if \kbd{x} is a square and $0$
13036
otherwise.
13037
13038
\fun{long}{FF_ispower}{GEN x, GEN K, GEN *pt} Given $K$ a positive integer,
13039
returns $1$ if \kbd{x} is a $K$-th power, and $0$ otherwise. If \kbd{x} is
13040
indeed a $K$-th power, set \kbd{pt} to its $K$-th root.
13041
13042
\fun{GEN}{FF_sqrtn}{GEN a, GEN n, GEN *zn} returns an \kbd{n}-th root of
13043
$\kbd{a}$ if it exist. If \kbd{zn} is non-\kbd{NULL} set it to a primitive
13044
\kbd{n}-th root of the unity.
13045
13046
\fun{GEN}{FF_log}{GEN a, GEN g, GEN o} the \typ{FFELT} \kbd{g} being a
13047
generator for the definition field of the \typ{FFELT} \kbd{a}, returns a
13048
\typ{INT} $e$ such that $a^e=g$. If $e$ does not exists, the result is
13049
currently undefined. If \kbd{o} is not \kbd{NULL} it is assumed to be a
13050
factorization of the multiplicative order of \kbd{g} (as set by
13051
\tet{FF_primroot})
13052
13053
\fun{GEN}{FF_order}{GEN a, GEN o} returns the order of the \typ{FFELT} \kbd{a}.
13054
If \kbd{o} is non-\kbd{NULL}, it is assumed that \kbd{o} is a multiple of the
13055
order of \kbd{a}.
13056
13057
\fun{GEN}{FF_primroot}{GEN a, GEN *o} returns a generator of the
13058
multiplicative group of the definition field of the \typ{FFELT} \kbd{a}.
13059
If \kbd{o} is not \kbd{NULL}, set it to the factorization of the order
13060
of the primitive root (to speed up \tet{FF_log}).
13061
13062
\fun{GEN}{FF_map}{GEN m, GEN a} returns $A(m)$ where \kbd{A=a.pol} assuming
13063
$a$ and $m$ belongs to fields having the same characteristic.
13064
13065
\subsec{FFX}
13066
13067
The functions in this sections take polynomial arguments and a \typ{FFELT}
13068
$a$. The coefficients of the polynomials must be of type \typ{INT},
13069
\typ{INTMOD} or \typ{FFELT} and compatible with \kbd{a}.
13070
13071
\fun{GEN}{FFX_add}{GEN P, GEN Q, GEN a} returns the sum of the polynomials
13072
\kbd{P} and \kbd{Q} defined over the definition field of the \typ{FFELT}
13073
\kbd{a}.
13074
13075
\fun{GEN}{FFX_mul}{GEN P, GEN Q, GEN a} returns the product of the polynomials
13076
\kbd{P} and \kbd{Q} defined over the definition field of the \typ{FFELT}
13077
\kbd{a}.
13078
13079
\fun{GEN}{FFX_sqr}{GEN P, GEN a} returns the square of the polynomial
13080
\kbd{P} defined over the definition field of the \typ{FFELT} \kbd{a}.
13081
13082
\fun{GEN}{FFX_rem}{GEN P, GEN Q, GEN a} returns the remainder
13083
of the polynomial \kbd{P} modulo the polynomial \kbd{Q}, where \kbd{P} and
13084
\kbd{Q} are defined over the definition field of the \typ{FFELT} \kbd{a}.
13085
13086
\fun{GEN}{FFX_gcd}{GEN P, GEN Q, GEN a} returns the GCD of the polynomials
13087
\kbd{P} and \kbd{Q} defined over the definition field of the \typ{FFELT}
13088
\kbd{a}.
13089
13090
\fun{GEN}{FFX_extgcd}{GEN P, GEN Q, GEN a, GEN *U, GEN *V}
13091
returns the GCD of the polynomials \kbd{P} and \kbd{Q} defined over
13092
the definition field of the \typ{FFELT} \kbd{a} and sets \kbd{*U}, \kbd{*V} to
13093
the Bezout coefficients such that $\kbd{*UP} + \kbd{*VQ} = d$. If \kbd{*U} is
13094
set to \kbd{NULL}, it is not computed which is a bit faster.
13095
13096
\fun{GEN}{FFX_halfgcd}{GEN x, GEN y, GEN a} returns a two-by-two matrix
13097
$M$ with determinant $\pm 1$ such that the image $(a,b)$ of $(x,y)$ by $M$
13098
has the property that $\deg a \geq {\deg x \over 2} > \deg b$.
13099
13100
\fun{GEN}{FFX_resultant}{GEN P, GEN Q, GEN a} returns the resultant of the polynomials
13101
\kbd{P} and \kbd{Q} where \kbd{P} and \kbd{Q} are defined over the definition
13102
field of the \typ{FFELT} \kbd{a}.
13103
13104
\fun{GEN}{FFX_disc}{GEN P, GEN a} returns the discriminant of the polynomial
13105
\kbd{P} where \kbd{P} is defined over the definition field of the \typ{FFELT}
13106
\kbd{a}.
13107
13108
\fun{GEN}{FFX_ispower}{GEN P, ulong k, GEN a, GEN *py}
13109
return $1$ if the \kbd{FFX} $P$ is a $k$-th power, $0$ otherwise,
13110
where \kbd{P} is defined over the definition field of the \typ{FFELT} \kbd{a}.
13111
If \kbd{py} is not \kbd{NULL}, set it to $g$ such that $g^k = f$.
13112
13113
\fun{GEN}{FFX_factor}{GEN f, GEN a} returns the factorization of the univariate
13114
polynomial \kbd{f} over the definition field of the \typ{FFELT} \kbd{a}. The
13115
coefficients of \kbd{f} must be of type \typ{INT}, \typ{INTMOD} or \typ{FFELT}
13116
and compatible with \kbd{a}.
13117
13118
\fun{GEN}{FFX_factor_squarefree}{GEN f, GEN a} returns the squarefree
13119
factorization of the univariate polynomial \kbd{f} over the definition field of
13120
the \typ{FFELT} \kbd{a}. This is a vector $[u_1,\dots,u_k]$ of pairwise
13121
coprime \kbd{FFX} such that $u_k \neq 1$ and $f = \prod u_i^i$.
13122
13123
\fun{GEN}{FFX_ddf}{GEN f, GEN a} assuming that $f$ is squarefree,
13124
returns the distinct degree factorization of $f$ modulo $p$.
13125
The returned value \kbd{v} is a \typ{VEC} with two
13126
components: \kbd{F=v[1]} is a vector of (\kbd{FFX})
13127
factors, and \kbd{E=v[2]} is a \typ{VECSMALL}, such that
13128
$f$ is equal to the product of the \kbd{F[i]} and each \kbd{F[i]}
13129
is a product of irreducible factors of degree \kbd{E[i]}.
13130
13131
\fun{GEN}{FFX_degfact}{GEN f, GEN a}, as \kbd{FFX\_factor}, but the
13132
degrees of the irreducible factors are returned instead of the factors
13133
themselves (as a \typ{VECSMALL}).
13134
13135
\fun{GEN}{FFX_roots}{GEN f, GEN a} returns the roots (\typ{FFELT})
13136
of the univariate polynomial \kbd{f} over the definition field of the
13137
\typ{FFELT} \kbd{a}. The coefficients of \kbd{f} must be of type \typ{INT},
13138
\typ{INTMOD} or \typ{FFELT} and compatible with \kbd{a}.
13139
13140
\fun{GEN}{FFX_preimagerel}{GEN F, GEN x, GEN a} returns $P\%F$
13141
where \kbd{P=x.pol} assuming $a$ and $x$ belongs to fields having the same
13142
characteristic, and that the coefficients of $F$ belong to the definition
13143
field of $a$.
13144
13145
\fun{GEN}{FFX_preimage}{GEN F, GEN x, GEN a} as \kbd{FFX\_preimagerel}
13146
but return \kbd{NULL} if the remainder is of degree greater or equal to $1$,
13147
the constant coefficient otherwise.
13148
13149
\subsec{FFM}
13150
13151
\fun{GEN}{FFM_FFC_gauss}{GEN M, GEN C, GEN ff} given a matrix \kbd{M}
13152
(\typ{MAT}) and a column vector \kbd{C} (\typ{COL}) over the finite
13153
field given by \kbd{ff} (\typ{FFELT}) such that $M$ is invertible,
13154
return the unique column vector $X$ such that $MX=C$.
13155
13156
\fun{GEN}{FFM_FFC_invimage}{GEN M, GEN C, GEN ff} given a matrix
13157
\kbd{M} (\typ{MAT}) and a column vector \kbd{C} (\typ{COL}) over the
13158
finite field given by \kbd{ff} (\typ{FFELT}), return a column vector
13159
\kbd{X} such that $MX=C$, or \kbd{NULL} if no such vector exists.
13160
13161
\fun{GEN}{FFM_FFC_mul}{GEN M, GEN C, GEN ff} returns the product of
13162
the matrix~\kbd{M} (\typ{MAT}) and the column vector~\kbd{C}
13163
(\typ{COL}) over the finite field given by \kbd{ff} (\typ{FFELT}).
13164
13165
\fun{GEN}{FFM_deplin}{GEN M, GEN ff} returns a nonzero vector
13166
(\typ{COL}) in the kernel of the matrix~\kbd{M} over the finite field
13167
given by \kbd{ff}, or \kbd{NULL} if no such vector exists.
13168
13169
\fun{GEN}{FFM_det}{GEN M, GEN ff} returns the determinant of the
13170
matrix~\kbd{M} over the finite field given by \kbd{ff}.
13171
13172
\fun{GEN}{FFM_gauss}{GEN M, GEN N, GEN ff} given two matrices \kbd{M}
13173
and~\kbd{N} (\typ{MAT}) over the finite field given by \kbd{ff}
13174
(\typ{FFELT}) such that $M$ is invertible, return the unique matrix
13175
$X$ such that $MX=N$.
13176
13177
\fun{GEN}{FFM_image}{GEN M, GEN ff} returns a matrix whose columns
13178
span the image of the matrix~\kbd{M} over the finite field given by
13179
\kbd{ff}.
13180
13181
\fun{GEN}{FFM_indexrank}{GEN M, GEN ff} given a matrix \kbd{M} of
13182
rank~$r$ over the finite field given by \kbd{ff}, returns a vector
13183
with two \typ{VECSMALL} components $y$ and $z$ containing $r$ row and
13184
column indices, respectively, such that the $r\times r$-matrix formed
13185
by the \kbd{M[i,j]} for $i$ in $y$ and $j$ in $z$ is invertible.
13186
13187
\fun{GEN}{FFM_inv}{GEN M, GEN ff} returns the inverse of the square
13188
matrix~\kbd{M} over the finite field given by \kbd{ff}, or \kbd{NULL}
13189
if \kbd{M} is not invertible.
13190
13191
\fun{GEN}{FFM_invimage}{GEN M, GEN N, GEN ff} given two matrices
13192
\kbd{M} and~\kbd{N} (\typ{MAT}) over the finite field given by
13193
\kbd{ff} (\typ{FFELT}), return a matrix \kbd{X} such that $MX=N$, or
13194
\kbd{NULL} if no such matrix exists.
13195
13196
\fun{GEN}{FFM_ker}{GEN M, GEN ff} returns the kernel of the \typ{MAT}
13197
\kbd{M} over the finite field given by the \typ{FFELT} \kbd{ff}.
13198
13199
\fun{GEN}{FFM_mul}{GEN M, GEN N, GEN ff} returns the product of the
13200
matrices \kbd{M} and~\kbd{N} (\typ{MAT}) over the finite field given
13201
by \kbd{ff} (\typ{FFELT}).
13202
13203
\fun{long}{FFM_rank}{GEN M, GEN ff} returns the rank of the
13204
matrix~\kbd{M} over the finite field given by \kbd{ff}.
13205
13206
\fun{GEN}{FFM_suppl}{GEN M, GEN ff} given a matrix \kbd{M} over the
13207
finite field given by \kbd{ff} whose columns are linearly independent,
13208
returns a square invertible matrix whose first columns are those
13209
of~\kbd{M}.
13210
13211
\subsec{FFXQ}
13212
13213
\fun{GEN}{FFXQ_mul}{GEN P, GEN Q, GEN T, GEN a} returns the product
13214
of the polynomials \kbd{P} and \kbd{Q} modulo the polynomial \kbd{T}, where
13215
\kbd{P}, \kbd{Q} and \kbd{T} are defined over the definition field of the
13216
\typ{FFELT} \kbd{a}.
13217
13218
\fun{GEN}{FFXQ_sqr}{GEN P, GEN T, GEN a} returns the square
13219
of the polynomial \kbd{P} modulo the polynomial \kbd{T}, where \kbd{P} and
13220
\kbd{T} are defined over the definition field of the \typ{FFELT} \kbd{a}.
13221
13222
\fun{GEN}{FFXQ_inv}{GEN P, GEN Q, GEN a} returns the inverse
13223
of the polynomial \kbd{P} modulo the polynomial \kbd{Q}, where \kbd{P} and
13224
\kbd{Q} are defined over the definition field of the \typ{FFELT} \kbd{a}.
13225
13226
\fun{GEN}{FFXQ_minpoly}{GEN Pf, GEN Qf, GEN a} returns the minimal
13227
polynomial of the polynomial \kbd{P} modulo the polynomial \kbd{Q}, where
13228
\kbd{P} and \kbd{Q} are defined over the definition field of the \typ{FFELT}
13229
\kbd{a}.
13230
13231
\section{Transcendental functions}
13232
13233
The following two functions are only useful when interacting with \kbd{gp},
13234
to manipulate its internal default precision (expressed as a number of
13235
decimal digits, not in words as used everywhere else):
13236
13237
\fun{long}{getrealprecision}{void} returns \kbd{realprecision}.
13238
13239
\fun{long}{setrealprecision}{long n, long *prec} sets the new
13240
\kbd{realprecision} to $n$, which is returned. As a side effect, set
13241
\kbd{prec} to the corresponding number of words \kbd{ndec2prec(n)}.
13242
13243
\subsec{Transcendental functions with \typ{REAL} arguments}
13244
13245
In the following routines, $x$ is assumed to be a \typ{REAL} and the result
13246
is a \typ{REAL} (sometimes a \typ{COMPLEX} with \typ{REAL} components), with
13247
the largest accuracy which can be deduced from the input. The naming scheme
13248
is inconsistent here, since we sometimes use the prefix \kbd{mp} even though
13249
\typ{INT} inputs are forbidden:
13250
13251
\fun{GEN}{sqrtr}{GEN x} returns the square root of $x$.
13252
13253
\fun{GEN}{cbrtr}{GEN x} returns the real cube root of $x$.
13254
13255
\fun{GEN}{sqrtnr}{GEN x, long n} returns the $n$-th root of $x$, assuming
13256
$n\geq 1$ and $x \geq 0$.
13257
13258
\fun{GEN}{sqrtnr_abs}{GEN x, long n} returns the $n$-th root of $|x|$,
13259
assuming $n\geq 1$ and $x \neq 0$.
13260
13261
\fun{GEN}{mpcos[z]}{GEN x[, GEN z]} returns $\cos(x)$.
13262
13263
\fun{GEN}{mpsin[z]}{GEN x[, GEN z]} returns $\sin(x)$.
13264
13265
\fun{GEN}{mplog[z]}{GEN x[, GEN z]} returns $\log(x)$. We must have $x > 0$
13266
since the result must be a \typ{REAL}. Use \kbd{glog} for the general case,
13267
where you want such computations as $\log(-1) = I$.
13268
13269
\fun{GEN}{mpexp[z]}{GEN x[, GEN z]} returns $\exp(x)$.
13270
13271
\fun{GEN}{mpexpm1}{GEN x} returns $\exp(x)-1$, but is more accurate than
13272
\kbd{subrs(mpexp(x), 1)}, which suffers from catastrophic cancellation if
13273
$|x|$ is very small.
13274
13275
\fun{void}{mpsincosm1}{GEN x, GEN *s, GEN *c} sets $s$ and $c$ to
13276
$\sin(x)$ and $\cos(x)-1$ respectively, where $x$ is a \typ{REAL}; the latter
13277
is more accurate than \kbd{subrs(mpcos(y), 1)}, which suffers from
13278
catastrophic cancellation if $|x|$ is very small.
13279
13280
\fun{GEN}{mpveceint1}{GEN C, GEN eC, long n} as \kbd{veceint1}; assumes
13281
that $C > 0$ is a \typ{REAL} and that \kbd{eC} is \kbd{NULL} or \kbd{mpexp(C)}.
13282
13283
\fun{GEN}{mpeint1}{GEN x, GEN expx} returns \kbd{eint1}$(x)$, for a \typ{REAL}
13284
$x\neq 0$, assuming that \kbd{expx} is \kbd{mpexp}$(x)$.
13285
13286
\noindent A few variants on the Lambert function: they actually work when
13287
\kbd{gtofp} can map all \kbd{GEN} arguments to a \typ{REAL}.
13288
13289
\fun{GEN}{mplambertW}{GEN y} solution $x = W_0(y)$ of the implicit equation
13290
$x \exp(x) = y$, for $y > -1/e$ a \typ{REAL}.
13291
13292
\fun{GEN}{mplambertx_logx}{GEN a, GEN b, long bit}
13293
solve $x - a \log(x) = b$ with $a > 0$ and $b \geq a (1 - \log(a))$.
13294
13295
\fun{GEN}{mplambertX}{GEN y, long bit} as \tet{mplambertx_logx}
13296
in the special case $a = 1$, $b = \log(y)$. In other words,
13297
solve $e^x / x = y$ with $y \geq e$.
13298
13299
\fun{GEN}{mplambertxlogx_x}{GEN a, GEN b, long bit}
13300
solve $x \log(x) - a x = b$; if $b < 0$, assume $a \geq 1 + \log |b|$.
13301
13302
\noindent Useful low-level functions which \emph{disregard} the sign of $x$:
13303
13304
\fun{GEN}{sqrtr_abs}{GEN x} returns $\sqrt{|x|}$ assuming $x\neq 0$.
13305
13306
\fun{GEN}{cbrtr_abs}{GEN x} returns $|x|^{1/3}$ assuming $x\neq 0$.
13307
13308
\fun{GEN}{exp1r_abs}{GEN x} returns $\exp(|x|) - 1$, assuming $x \neq 0$.
13309
13310
\fun{GEN}{logr_abs}{GEN x} returns $\log(|x|)$, assuming $x \neq 0$.
13311
13312
\subsec{Other complex transcendental functions}
13313
13314
\fun{GEN}{atanhuu}{ulong u, ulong v, long prec} computes \text{atanh}$(u/v)$
13315
using binary splitting, assuming $0 < u < v$. Not memory clean but suitable
13316
for \kbd{gerepileupto}.
13317
13318
\fun{GEN}{atanhui}{ulong u, GEN v, long prec} computes \text{atanh}$(u/v)$
13319
using binary splitting, assuming $0 < u < v$. Not memory clean but suitable
13320
for \kbd{gerepileupto}.
13321
13322
\fun{GEN}{szeta}{long s, long prec} returns the value of Riemann's zeta
13323
function at the (possibly negative) integer $s\neq 1$, in relative accuracy
13324
\kbd{prec}.
13325
13326
\fun{GEN}{veczeta}{GEN a, GEN b, long N, long prec} returns in a vector
13327
all the $\zeta(aj + b)$, where $j = 0, 1, \dots, N-1$, where $a$ and $b$ are
13328
real numbers (of arbitrary type, although \typ{INT} is treated more
13329
efficiently) and $b > 1$. Assumes that $N \geq 1$.
13330
13331
\fun{GEN}{ggamma1m1}{GEN x, long prec} return $\Gamma(1+x) - 1$ assuming
13332
$|x| < 1$. Guard against cancellation when $x$ is small.
13333
13334
\noindent A few variants on sin and cos:
13335
13336
\fun{void}{mpsincos}{GEN x, GEN *s, GEN *c} sets $s$ and $c$ to
13337
$\sin(x)$ and $\cos(x)$ respectively, where $x$ is a \typ{REAL}
13338
13339
\fun{void}{mpsinhcosh}{GEN x, GEN *s, GEN *c} sets $s$ and $c$ to
13340
$\sinh(x)$ and $\cosh(x)$ respectively, where $x$ is a \typ{REAL}
13341
13342
\fun{GEN}{expIr}{GEN x} returns $\exp(ix)$, where $x$ is a \typ{REAL}.
13343
The return type is \typ{COMPLEX} unless the imaginary part is equal to $0$
13344
to the current accuracy (its sign is $0$).
13345
13346
\fun{GEN}{expIPiR}{GEN x, long prec} return $\exp(i \pi x)$, where $x$ is a
13347
real number (\typ{INT}, \typ{FRAC} or \typ{REAL}).
13348
13349
\fun{GEN}{expIPiC}{GEN z, long prec} return $\exp(i \pi x)$, where $x$ is a
13350
complex number (\typ{INT}, \typ{FRAC}, \typ{REAL} or \typ{COMPLEX}).
13351
13352
\fun{GEN}{expIxy}{GEN x, GEN y, long prec} returns $\exp(ixy)$. Efficient
13353
when $x$ is real and $y$ pure imaginary.
13354
13355
\fun{GEN}{pow2Pis}{GEN s, long prec} returns $(2\pi)^s$. The intent of this
13356
function and the next ones is to be accurate even if $s$ has a huge imaginary
13357
part: $\pi$ is computed at an accuracy taking into account the cancellation
13358
induced by argument reduction when computing the sine or cosine of
13359
$\Im s \log 2\pi$.
13360
13361
\fun{GEN}{powPis}{GEN s, long prec} returns $\pi^s$, as \kbd{pow2Pis}.
13362
13363
\fun{long}{powcx_prec}{long e, GEN s, long prec} if $e \approx \log_2 |x|$
13364
return the precision at which $\log(x)$ must be computed to evaluate $x^s$
13365
reliably (taking into account argument reduction).
13366
13367
\fun{GEN}{powcx}{GEN x, GEN logx, GEN s, long prec} assuming $s$ is a
13368
\typ{COMPLEX} and \kbd{logx} is $\log(x)$ computed to accuracy
13369
\kbd{powcx\_prec}, return $x^s$.
13370
13371
\fun{void}{gsincos}{GEN x, GEN *s, GEN *c, long prec} general case.
13372
13373
\fun{GEN}{rootsof1_cx}{GEN d, long prec} return $e(1/d)$ at precision
13374
\kbd{prec}, $e(x) = \exp(2i\pi x)$.
13375
13376
\fun{GEN}{rootsof1u_cx}{ulong d, long prec} return $e(1/d)$ at
13377
precision \kbd{prec}.
13378
13379
\fun{GEN}{rootsof1q_cx}{long a, long b, long prec} return $e(a/b)$ at
13380
precision \kbd{prec}.
13381
13382
\fun{GEN}{rootsof1powinit}{long a, long b, long prec} precompute $b$-th
13383
roots of $1$ for \kbd{rootsof1pow}, i.e. to later compute $e(ac/b)$ for
13384
varying $c$.
13385
13386
\fun{GEN}{rootsof1pow}{GEN T, long c} given
13387
$T = \kbd{rootsof1powinit}(a,b,\kbd{prec})$, return $e(ac/b)$.
13388
13389
13390
\noindent A generalization of \tet{affrr_fixlg}
13391
13392
\fun{GEN}{affc_fixlg}{GEN x, GEN res} assume \kbd{res} was allocated using
13393
\tet{cgetc}, and that $x$ is either a \typ{REAL} or a \typ{COMPLEX}
13394
with \typ{REAL} components. Assign $x$ to \kbd{res}, first shortening
13395
the components of \kbd{res} if needed (in a \kbd{gerepile}-safe way). Further
13396
convert \kbd{res} to a \typ{REAL} if $x$ is a \typ{REAL}.
13397
13398
\fun{GEN}{trans_eval}{const char *fun, GEN (*f) (GEN, long), GEN x, long prec}
13399
evaluate the transcendental function $f$ (named \kbd{"fun"} at the argument
13400
$x$ and precision \kbd{prec}. This is a quick way to implement a transcendental
13401
function to be made available under GP, starting from a $C$ function
13402
handling only \typ{REAL} and \typ{COMPLEX} arguments. This routine first
13403
converts $x$ to a suitable type:
13404
13405
\item \typ{INT}/\typ{FRAC} to \typ{REAL} of precision \kbd{prec}, \typ{QUAD} to
13406
\typ{REAL} or \typ{COMPLEX} of precision \kbd{prec}.
13407
13408
\item \typ{POLMOD} to a \typ{COL} of complex embeddings (as in \tet{conjvec})
13409
13410
Then evaluates the function at \typ{VEC}, \typ{COL}, \typ{MAT} arguments
13411
coefficientwise.
13412
13413
\subsec{Modular functions}
13414
13415
\fun{GEN}{cxredsl2}{GEN z, GEN *g} given $t$ a \typ{COMPLEX} belonging to the
13416
upper half-plane, find $\gamma \in \text{SL}_2(\Z)$ such that $\gamma \cdot z$
13417
belongs to the standard fundamental domain and set \kbd{*g} to $\gamma$.
13418
13419
\fun{GEN}{cxredsl2_i}{GEN z, GEN *g, GEN *czd} as \kbd{cxredsl2}; also sets
13420
\kbd{*czd} to $cz + d$, if $\gamma = [a,b;c,d]$.
13421
13422
\fun{GEN}{cxEk}{GEN tau, long k, long prec} returns $E_k(\tau)$ by direct
13423
evaluation of $1 + 2/\zeta(1-k) \sum_n n^{k-1} q^n/(1-q^n)$, $q = e(\tau)$.
13424
Assume that $\Im \tau > 0$ and $k$ even. Very slow unless $\tau$ is already
13425
reduced modulo $\text{SL}_2(\Z)$. Not \kbd{gerepile}-clean but suitable for
13426
\kbd{gerepileupto}.
13427
13428
\subsec{Transcendental functions with \typ{PADIC} arguments}
13429
13430
\fun{GEN}{Qp_exp}{GEN x} shortcut for \kbd{gexp(x, /*ignored*/prec)}
13431
13432
\fun{GEN}{Qp_gamma}{GEN x} shortcut for \kbd{ggamma(x, /*ignored*/prec)}
13433
13434
\fun{GEN}{Qp_log}{GEN x} shortcut for \kbd{glog(x, /*ignored*/prec)}
13435
13436
\fun{GEN}{Qp_sqrt}{GEN x} shortcut for \kbd{gsqrt(x, /*ignored*/prec)}
13437
Return \kbd{NULL} if $x$ is not a square.
13438
13439
\fun{GEN}{Qp_sqrtn}{GEN x, GEN n, GEN *z} shortcut for \kbd{gsqrtn(x, n, z,
13440
/*ignored*/prec)}. Return \kbd{NULL} if $x$ is not an $n$-th power.
13441
13442
\fun{GEN}{Qp_agm2_sequence}{GEN a1, GEN b1} assume $a_1/b_1 = 1$ mod $p$
13443
if $p$ odd and mod $2^4$ if $p = 2$. Let $A_1 = a_1/p^v$ and $B_1 = b_1/p^v$
13444
with $v = v_p(a_1) = v_p(b_1)$; let further
13445
$A_{n+1} = (A_n + B_n + 2 B_{n+1}) / 4$,
13446
$B_{n+1} = B_n \sqrt{A_n / B_n}$ (the square root of $A_n B_n$ congruent to
13447
$B_n$ mod $p$) and $R_n = p^v(A_n - B_n)$. We stop when $R_n$ is $0$
13448
at the given $p$-adic accuracy. This function returns in a triplet \typ{VEC}
13449
the three sequences $(A_n)$, $(B_n)$ and $(R_n)$, corresponding to a sequence
13450
of $2$-isogenies on the Tate curve $y^2 = x(x-a_1)(x+a_1-b_1)$. The
13451
common limit of $A_n$ and $B_n$ is the $M_2(a_1,b_1)$, the square
13452
of the $p$-adic AGM of $\sqrt(a_1)$ and $\sqrt(b_1)$. This is given
13453
by \tet{ellQp_Ei} and is used by corresponding ascending and descending
13454
$p$-adic Landen transforms:
13455
13456
\fun{void}{Qp_ascending_Landen}{GEN ABR, GEN *ptx, GEN *pty}
13457
13458
\fun{void}{Qp_descending_Landen}{GEN ABR, GEN *ptx, GEN *pty}
13459
13460
\subsec{Cached constants}
13461
13462
The cached constant is returned at its current precision, which may be larger
13463
than \kbd{prec}. One should always use the \kbd{mp\var{xxx}} variant:
13464
\kbd{mppi}, \kbd{mpeuler}, or \kbd{mplog2}.
13465
13466
\fun{GEN}{consteuler}{long prec} precomputes Euler-Mascheroni's constant
13467
at precision \kbd{prec}.
13468
13469
\fun{GEN}{constcatalan}{long prec} precomputes Catalan's constant at precision
13470
\kbd{prec}.
13471
13472
\fun{GEN}{constpi}{long prec} precomputes $\pi$ at precision \kbd{prec}.
13473
13474
\fun{GEN}{constlog2}{long prec} precomputes $\log(2)$ at precision
13475
\kbd{prec}.
13476
13477
\fun{void}{constbern}{long n} precomputes the $n$ even \idx{Bernoulli} numbers
13478
$B_2,\dots,B_{2n}$ as \typ{FRAC}. No more than $n$ Bernoulli numbers will
13479
ever be stored (by \tet{bernfrac} or \tet{bernreal}), unless a subsequent
13480
call to \kbd{constbern} increases the cache.
13481
13482
\fun{GEN}{constzeta}{long n, long prec} ensures that the $n$ values
13483
$\gamma, \zeta(2),\dots, \zeta(n)$ are cached at accuracy bigger than or equal
13484
to \kbd{prec} and return a vector containing at least those value. Note that
13485
$\gamma = \lim_1 \zeta(s) - 1/(s-1)$. If the accuracy of cached data is too
13486
low or $n$ is greater than the cache length, the cache is recomputed at the
13487
given parameters.
13488
13489
The following functions use cached data if \kbd{prec} is smaller than the
13490
precision of the cached value; otherwise the newly computed data replaces the
13491
old cache.
13492
13493
\fun{GEN}{mppi}{long prec} returns $\pi$ at precision \kbd{prec}.
13494
13495
\fun{GEN}{Pi2n}{long n, long prec} returns $2^n\pi$ at precision \kbd{prec}.
13496
13497
\fun{GEN}{PiI2}{long n, long prec} returns the complex number $2\pi i$ at
13498
precision \kbd{prec}.
13499
13500
\fun{GEN}{PiI2n}{long n, long prec} returns the complex number $2^n\pi i$ at
13501
precision \kbd{prec}.
13502
13503
\fun{GEN}{mpeuler}{long prec} returns Euler-Mascheroni's constant at
13504
precision \kbd{prec}.
13505
13506
\fun{GEN}{mpeuler}{long prec} returns Catalan's number at precision \kbd{prec}.
13507
13508
\fun{GEN}{mplog2}{long prec} returns $\log 2$ at precision \kbd{prec}.
13509
13510
The following functions use the \idx{Bernoulli} numbers cache initialized by
13511
\kbd{constbern}:
13512
13513
\fun{GEN}{bernreal}{long i, long prec} returns the \idx{Bernoulli} number
13514
$B_i$ as a \typ{REAL} at precision \kbd{prec}. If \kbd{constbern(n)}
13515
was called previously with $n \geq i$, then the cached value is (converted to
13516
a \typ{REAL} of accuracy \kbd{prec} then) returned. Otherwise, the missing
13517
value is computed; the cache is not updated.
13518
13519
\fun{GEN}{bernfrac}{long i} returns the \idx{Bernoulli} number $B_i$ as a
13520
rational number (\typ{FRAC} or \typ{INT}). If the \kbd{constbern} cache includes
13521
$B_i$, the latter is returned. Otherwise, the missing value is computed; the
13522
cache is not updated.
13523
13524
\subsec{Obsolete functions}
13525
13526
\fun{void}{mpbern}{long n, long prec}
13527
13528
\section{Permutations }
13529
13530
\noindent Permutations are represented in two different ways
13531
13532
\item (\kbd{perm}) a \typ{VECSMALL} $p$ representing the bijection $i\mapsto
13533
p[i]$; unless mentioned otherwise, this is the form used in the functions
13534
below for both input and output,
13535
13536
\item (\kbd{cyc}) a \typ{VEC} of \typ{VECSMALL}s representing a product of
13537
disjoint cycles.
13538
13539
\fun{GEN}{identity_perm}{long n} return the identity permutation on $n$
13540
symbols.
13541
13542
\fun{GEN}{cyclic_perm}{long n, long d} return the cyclic permutation mapping
13543
$i$ to $i+d$ (mod $n$) in $S_n$. Assume that $d \leq n$.
13544
13545
\fun{GEN}{perm_mul}{GEN s, GEN t} multiply $s$ and $t$ (composition $s\circ t$)
13546
13547
\fun{GEN}{perm_sqr}{GEN s} multiply $s$ by itself (composition $s\circ s$)
13548
13549
\fun{GEN}{perm_conj}{GEN s, GEN t} return $sts^{-1}$.
13550
13551
\fun{int}{perm_commute}{GEN p, GEN q} return $1$ if $p$ and $q$ commute, 0
13552
otherwise.
13553
13554
\fun{GEN}{perm_inv}{GEN p} returns the inverse of $p$.
13555
13556
\fun{GEN}{perm_pow}{GEN p, GEN n} returns $p^n$
13557
13558
\fun{GEN}{perm_powu}{GEN p, ulong n} returns $p^n$
13559
13560
\fun{GEN}{cyc_pow_perm}{GEN p, long n} the permutation $p$ is given as
13561
a product of disjoint cycles (\kbd{cyc}); return $p^n$ (as a \kbd{perm}).
13562
13563
\fun{GEN}{cyc_pow}{GEN p, long n} the permutation $p$ is given as
13564
a product of disjoint cycles (\kbd{cyc}); return $p^n$ (as a \kbd{cyc}).
13565
13566
\fun{GEN}{perm_cycles}{GEN p} return the cyclic decomposition of $p$.
13567
13568
\fun{GEN}{perm_order}{GEN p} returns the order of the permutation $p$
13569
(as the lcm of its cycle lengths).
13570
13571
\fun{ulong}{perm_orderu}{GEN p} returns the order of the permutation $p$
13572
(as the lcm of its cycle lengths) assuming it fits in a \kbd{ulong}.
13573
13574
\fun{long}{perm_sign}{GEN p} returns the sign of the permutation $p$.
13575
13576
\fun{GEN}{vecperm_orbits}{GEN gen, long n} return the orbits of
13577
$\{1,2,\ldots,n\}$ under the action of the subgroup of $S_n$ generated by
13578
$gen$.
13579
13580
\fun{GEN}{Z_to_perm}{long n, GEN x} as \kbd{numtoperm}, returning a
13581
\typ{VECSMALL}.
13582
13583
\fun{GEN}{perm_to_Z}{GEN v} as \kbd{permtonum} for a \typ{VECSMALL} input.
13584
13585
\fun{GEN}{perm_to_GAP}{GEN p} return a \typ{STR} which is a representation
13586
of $p$ comptatible with the GAP computer algebra system.
13587
13588
\section{Small groups}
13589
13590
The small (finite) groups facility is meant to deal with subgroups of Galois
13591
groups obtained by \tet{galoisinit} and thus is currently limited to weakly
13592
super-solvable groups.
13593
13594
A group \var{grp} of order $n$ is represented by its regular representation
13595
(for an arbitrary ordering of its element) in $S_n$. A subgroup of such group
13596
is represented by the restriction of the representation to the subgroup.
13597
A \emph{small group} can be either a group or a subgroup. Thus it is embedded
13598
in some $S_n$, where $n$ is the multiple of the order. Such an $n$ is called
13599
the \emph{domain} of the small group. The domain of a trivial subgroup cannot
13600
be derived from the subgroup data, so some functions require the subgroup
13601
domain as argument.
13602
13603
The small group \var{grp} is represented by a \typ{VEC} with two
13604
components:
13605
13606
$\var{grp}[1]$ is a generating subset $[s_1,\ldots,s_g]$ of \var{grp}
13607
expressed as a vector of permutations of length~$n$.
13608
13609
$\var{grp}[2]$ contains the relative orders $[o_1,\ldots,o_g]$ of
13610
the generators $\var{grp}[1]$.
13611
13612
See \tet{galoisinit} for the technical details.
13613
13614
\fun{GEN}{checkgroup}{GEN gal, GEN *elts} check whether \var{gal} is a
13615
small group or a Galois group. Returns the underlying small
13616
group and set \var{elts} to the list of elements or to \kbd{NULL} if it is not
13617
known.
13618
13619
\fun{GEN}{checkgroupelts}{GEN gal}
13620
check whether \var{gal} is a small group or a Galois group, or a vector of
13621
permutations listing the group elements. Returns the list of group elements as
13622
permutations.
13623
13624
\fun{GEN}{galois_group}{GEN gal} return the underlying small group of the
13625
Galois group \var{gal}.
13626
13627
\fun{GEN}{cyclicgroup}{GEN g, long s} return the cyclic group with generator
13628
$g$ of order $s$.
13629
13630
\fun{GEN}{trivialgroup}{void} return the trivial group.
13631
13632
\fun{GEN}{dicyclicgroup}{GEN g1, GEN g2, long s1, long s2} returns the group
13633
with generators \var{g1}, \var{g2} with respecting relative orders \var{s1},
13634
\var{s2}.
13635
13636
\fun{GEN}{abelian_group}{GEN v} let v be a \typ{VECSMALL} seen as the SNF of
13637
a small abelian group, return its regular representation.
13638
13639
\fun{long}{group_domain}{GEN grp} returns the \kbd{domain} of the
13640
\emph{nontrivial} small group \var{grp}. Return an error if \var{grp} is
13641
trivial.
13642
13643
\fun{GEN}{group_elts}{GEN grp, long n} returns the list of elements of the
13644
small group \var{grp} of domain \var{n} as permutations.
13645
13646
\fun{GEN}{groupelts_to_group}{GEN elts}, where \var{elts} is the list of
13647
elements of a group, returns the corresponding small group, if it
13648
exists, otherwise return \kbd{NULL}.
13649
13650
\fun{GEN}{group_set}{GEN grp, long n} returns a \var{F2v} $b$ such that
13651
$b[i]$ is set if and only if the small group \var{grp} of domain \var{n}
13652
contains a permutation sending $1$ to $i$.
13653
13654
\fun{GEN}{groupelts_set}{GEN elts, long n}, where \var{elts} is the list of
13655
elements of a small group of domain \var{n}, returns a \var{F2v} $b$ such that
13656
$b[i]$ is set if and only if the small group contains a permutation sending $1$
13657
to $i$.
13658
13659
\fun{GEN}{groupelts_conj_set}{GEN elts, GEN p}, where \var{elts} is the list of
13660
elements of a small group of domain \var{n}, returns a \var{F2v} $b$ such that
13661
$b[i]$ is set if and only if the small group contains a permutation sending
13662
$p^{-1}[1]$ to $p^{-1}[i]$.
13663
13664
\fun{int}{group_subgroup_is_faithful}{GEN G, GEN H} return $1$ if the action
13665
of $G$ on $G/H$ by translation is faithful, $0$ otherwise.
13666
13667
\fun{GEN}{groupelts_conjclasses}{GEN elts, long *pn}, where \var{elts} is
13668
the list of elements of a small group (sorted with respect to
13669
\kbd{vecsmall\_lexcmp}), return a \typ{VECSMALL} \kbd{conj} of
13670
the same length such that \kbd{conj[i]} is the index in $\{1,\cdots,n\}$ of
13671
the conjugacy class of \kbd{elts[i]} for some unspecified but deterministic
13672
ordering of the classes, where $n$ is the number of conjugacy classes. If
13673
\kbd{pn} is non \kbd{NULL}, \kbd{*pn} is set to $n$.
13674
13675
\fun{GEN}{conjclasses_repr}{GEN conj, long nb},
13676
where \kbd{conj} and \kbd{nb} are as returned by the call
13677
\kbd{groupelts\_conjclasses(elts)}, return \typ{VECSMALL} of length \kbd{nb}
13678
which gives the indices in \kbd{elts} of a representative of each conjugacy
13679
class.
13680
13681
\fun{GEN}{group_to_cc}{GEN G}, where $G$ is a small group or a Galois group,
13682
returns a \kbd{cc} (conjclasses) structure \kbd{[elts,conj,rep,flag]},
13683
as obtained by \kbd{alggroupcenter}, where \kbd{conj} is
13684
\kbd{groupelts\_conjclasses}$(\kbd{elts})$ and \kbd{rep} is the attached
13685
\kbd{conjclasses\_repr}. \kbd{flag} is 1 if the permutation representation
13686
is transitive (in which case an element $g$ of $G$ is characterized by $g[1]$),
13687
and 0 otherwise. Shallow function.
13688
13689
\fun{long}{group_order}{GEN grp} returns the order of the small group
13690
\var{grp} (which is the product of the relative orders).
13691
13692
\fun{long}{group_isabelian}{GEN grp} returns $1$ if the small group
13693
\var{grp} is Abelian, else $0$.
13694
13695
\fun{GEN}{group_abelianHNF}{GEN grp, GEN elts} if \var{grp} is not Abelian,
13696
returns \kbd{NULL}, else returns the HNF matrix of \var{grp} with respect to
13697
the generating family $\var{grp}[1]$. If \var{elts} is no \kbd{NULL}, it must
13698
be the list of elements of \var{grp}.
13699
13700
\fun{GEN}{group_abelianSNF}{GEN grp, GEN elts} if \var{grp} is not Abelian,
13701
returns \kbd{NULL}, else returns its cyclic decomposition. If \var{elts} is no
13702
\kbd{NULL}, it must be the list of elements of \var{grp}.
13703
13704
\fun{long}{group_subgroup_isnormal}{GEN G, GEN H}, $H$ being a subgroup of the
13705
small group $G$, returns $1$ if $H$ is normal in $G$, else $0$.
13706
13707
\fun{long}{group_isA4S4}{GEN grp} returns $1$ if the small group
13708
\var{grp} is isomorphic to $A_4$, $2$ if it is isomorphic to $S_4$,
13709
$3$ if it is isomorphic to $(3\times 3):4$ and
13710
$0$ else. This is mainly to deal with the idiosyncrasy of the format.
13711
13712
\fun{GEN}{group_leftcoset}{GEN G, GEN g} where $G$ is a small group and $g$ a
13713
permutation of the same domain, the left coset $gG$ as a vector of
13714
permutations.
13715
13716
\fun{GEN}{group_rightcoset}{GEN G, GEN g} where $G$ is a small group and $g$ a
13717
permutation of the same domain, the right coset $Gg$ as a vector of
13718
permutations.
13719
13720
\fun{long}{group_perm_normalize}{GEN G, GEN g} where $G$ is a small group and
13721
$g$ a permutation of the same domain, return $1$ if $gGg^{-1}=G$, else $0$.
13722
13723
\fun{GEN}{group_quotient}{GEN G, GEN H}, where $G$ is a small group and
13724
$H$ is a subgroup of $G$, returns the quotient map $G\rightarrow G/H$
13725
as an abstract data structure.
13726
13727
\fun{GEN}{groupelts_quotient}{GEN elts, GEN H}, where \var{elts} is the list of
13728
elements of a small group $G$, $H$ is a subgroup of $G$, returns the quotient
13729
map $G\rightarrow G/H$ as an abstract data structure.
13730
13731
\fun{GEN}{quotient_perm}{GEN C, GEN g} where $C$ is the quotient map
13732
$G\rightarrow G/H$ for some subgroup $H$ of $G$ and $g$ an element of $G$,
13733
return the image of $g$ by $C$ (i.e. the coset $gH$).
13734
13735
\fun{GEN}{quotient_group}{GEN C, GEN G} where $C$ is the quotient map
13736
$G\rightarrow G/H$ for some \emph{normal} subgroup $H$ of $G$, return the
13737
quotient group $G/H$ as a small group.
13738
13739
\fun{GEN}{quotient_groupelts}{GEN C} where $C$ is the quotient map
13740
$G\rightarrow G/H$ for some group $G$ and some \emph{normal} subgroup $H$ of $G$,
13741
return the list of elements of the quotient group $G/H$ (as permutations over
13742
corresponding to the regular representation).
13743
13744
\fun{GEN}{quotient_subgroup_lift}{GEN C, GEN H, GEN S} where $C$ is the
13745
quotient map $G\rightarrow G/H$ for some group $G$ normalizing $H$ and $S$ is
13746
a subgroup of $G/H$, return the inverse image of $S$ by $C$.
13747
13748
\fun{GEN}{group_subgroups}{GEN grp} returns the list of subgroups of the
13749
small group \var{grp} as a \typ{VEC}.
13750
13751
\fun{GEN}{subgroups_tableset}{GEN S, long n} where $S$ is a vector of subgroups
13752
of domain $n$, returns a table which matchs the set of elements of the
13753
subgroups against the index of the subgroups.
13754
13755
\fun{long}{tableset_find_index}{GEN tbl, GEN set} searchs the set \kbd{set} in
13756
the table \kbd{tbl} and returns its attached index, or $0$ if not found.
13757
13758
\fun{GEN}{groupelts_abelian_group}{GEN elts} where \var{elts} is the list of
13759
elements of an \emph{Abelian} small group, returns the corresponding
13760
small group.
13761
13762
\fun{long}{groupelts_exponent}{GEN elts} where \var{elts} is the list of
13763
elements of a small group, returns the exponent the group (the LCM of the order
13764
of the elements of the group).
13765
13766
\fun{GEN}{groupelts_center}{GEN elts} where \var{elts} is the list of elements
13767
of a small group, returns the list of elements of the center of the
13768
group.
13769
13770
\fun{GEN}{group_export}{GEN grp, long format} convert a small group
13771
to another format, as a \typ{STR} describing the group for the given syntax,
13772
see \tet{galoisexport}.
13773
13774
\fun{GEN}{group_export_GAP}{GEN G} export a small group to GAP format.
13775
13776
\fun{GEN}{group_export_MAGMA}{GEN G} export a small group to MAGMA format.
13777
13778
\fun{long}{group_ident}{GEN grp, GEN elts} returns the index of the small group
13779
\var{grp} in the GAP4 Small Group library, see \tet{galoisidentify}. If
13780
\var{elts} is not \kbd{NULL}, it must be the list of elements of \var{grp}.
13781
13782
\fun{long}{group_ident_trans}{GEN grp, GEN elts} returns the index of the
13783
regular representation of the small group \var{grp} in the GAP4 Transitive
13784
Group library, see \tet{polgalois}. If \var{elts} is no \kbd{NULL}, it must be
13785
the list of elements of \var{grp}.
13786
13787
\newpage
13788
\chapter{Standard data structures}
13789
13790
\section{Character strings}
13791
13792
\subsec{Functions returning a \kbd{char *}}
13793
13794
\fun{char*}{pari_strdup}{const char *s} returns a malloc'ed copy of $s$
13795
(uses \kbd{pari\_malloc}).
13796
13797
\fun{char*}{pari_strndup}{const char *s, long n} returns a malloc'ed copy of
13798
at most $n$ chars from $s$ (uses \kbd{pari\_malloc}). If $s$ is longer than
13799
$n$, only $n$ characters are copied and a terminal null byte is added.
13800
13801
\fun{char*}{stack_strdup}{const char *s} returns a copy of $s$, allocated
13802
on the PARI stack (uses \kbd{stack\_malloc}).
13803
13804
\fun{char*}{stack_strcat}{const char *s, const char *t} returns the
13805
concatenation of $s$ and $t$, allocated on the PARI stack (uses
13806
\kbd{stack\_malloc}).
13807
13808
\fun{char*}{stack_sprintf}{const char *fmt, ...} runs \kbd{pari\_sprintf}
13809
on the given arguments, returning a string allocated on the PARI stack.
13810
13811
\fun{char*}{uordinal}{ulong x} return the ordinal number attached to $x$
13812
(i.e. $1$st, $2$nd, etc.) as a \tet{stack_malloc}'ed string.
13813
13814
\fun{char*}{itostr}{GEN x} writes the \typ{INT} $x$ to a \tet{stack_malloc}'ed
13815
string.
13816
13817
\fun{char*}{GENtostr}{GEN x}, using the current default output format
13818
(\kbd{GP\_DATA->fmt}, which contains the output style and the number of
13819
significant digits to print), converts $x$ to a malloc'ed string. Simple
13820
variant of \tet{pari_sprintf}.
13821
13822
\fun{char*}{GENtostr_raw}{GEN x} as \tet{GENtostr} with the following
13823
differences: 1) the output format is \tet{f_RAW}; 2) the result is allocated
13824
on the stack and \emph{must not} be freed.
13825
13826
\fun{char*}{GENtostr_unquoted}{GEN x} as \tet{GENtostr_raw} with the following
13827
additional difference: a \typ{STR} $x$ is printed without enclosing quotes
13828
(to be used by \kbd{print}.
13829
13830
\fun{char*}{GENtoTeXstr}{GEN x}, as \kbd{GENtostr}, except that
13831
\tet{f_TEX} overrides the output format from \kbd{GP\_DATA->fmt}.
13832
13833
\fun{char*}{RgV_to_str}{GEN g, long flag} $g$ being a vector of \kbd{GEN}s,
13834
returns a malloc'ed string, the concatenation of the \kbd{GENtostr} applied
13835
to its elements, except that \typ{STR} are printed without enclosing quotes.
13836
\kbd{flag} determines the output format: \tet{f_RAW}, \tet{f_PRETTYMAT}
13837
or \tet{f_TEX}.
13838
13839
\subsec{Functions returning a \typ{STR}}
13840
13841
\fun{GEN}{strtoGENstr}{const char *s} returns a \typ{STR} with content $s$.
13842
13843
\fun{GEN}{strntoGENstr}{const char *s, long n}
13844
returns a \typ{STR} containing the first $n$ characters of $s$.
13845
13846
\fun{GEN}{chartoGENstr}{char c} returns a \typ{STR} containing the character
13847
$c$.
13848
13849
\fun{GEN}{GENtoGENstr}{GEN x} returns a \typ{STR} containing the printed
13850
form of $x$ (in \tet{raw} format). This is often easier to use that
13851
\tet{GENtostr} (which returns a malloc-ed \kbd{char*}) since there is no need
13852
to free the string after use.
13853
13854
\fun{GEN}{GENtoGENstr_nospace}{GEN x} as \kbd{GENtoGENstr}, removing all
13855
spaces from the output.
13856
13857
\fun{GEN}{Str}{GEN g} as \tet{RgV_to_str} with output format \tet{f_RAW},
13858
but returns a \typ{STR}, not a malloc'ed string.
13859
13860
\fun{GEN}{strtex}{GEN g} as \tet{RgV_to_str} with output format \tet{f_TEX},
13861
but returns a \typ{STR}, not a malloc'ed string.
13862
13863
\fun{GEN}{strexpand}{GEN g} as \tet{RgV_to_str} with output format \tet{f_RAW},
13864
performing tilde and environment expansion on the result. Returns a
13865
\typ{STR}, not a malloc'ed string.
13866
13867
\fun{GEN}{gsprintf}{const char *fmt, ...} equivalent to
13868
\kbd{pari\_sprintf(fmt,...}, followed by \tet{strtoGENstr}. Returns a \typ{STR},
13869
not a malloc'ed string.
13870
13871
\fun{GEN}{gvsprintf}{const char *fmt, va_list ap} variadic version of
13872
\tet{gsprintf}
13873
13874
\subsec{Dynamic strings}
13875
13876
A \tet{pari_str} is a dynamic string which grows dynamically as needed.
13877
This structure contains private data and two public members \kbd{char *string},
13878
which is the string itself and \kbd{use\_stack} which tells whether the
13879
string lives
13880
13881
\item on the PARI stack (value $1$), meaning that it will be destroyed by any
13882
manipulation of the stack, e.g. a \kbd{gerepile} call or resetting
13883
\kbd{avma};
13884
13885
\item in malloc'ed memory (value $0$), in which case it is impervious to
13886
stack manipulation but will need to be explicitly freed by the user
13887
after use, via \kbd{pari\_free(s.string)}.
13888
13889
13890
\fun{void}{str_init}{pari_str *S, int use_stack} initializes a dynamic
13891
string; if \kbd{use\_stack} is 0, then the string is malloc'ed, else
13892
it lives on the PARI stack.
13893
13894
\fun{void}{str_printf}{pari_str *S, const char *fmt, ...} write to the end
13895
of $S$ the remaining arguments according to PARI format \kbd{fmt}.
13896
13897
\fun{void}{str_putc}{pari_str *S, char c} write the character $c$ to the end
13898
of $S$.
13899
13900
\fun{void}{str_puts}{pari_str *S, const char *s} write the string $s$ to the
13901
end of $S$.
13902
13903
\section{Output}
13904
13905
\subsec{Output contexts}
13906
13907
An output coutext, of type \tet{PariOUT}, is a \kbd{struct}
13908
that models a stream and contains the following function pointers:
13909
\bprog
13910
void (*putch)(char); /* fputc()-alike */
13911
void (*puts)(const char*); /* fputs()-alike */
13912
void (*flush)(void); /* fflush()-alike */
13913
@eprog\noindent
13914
The methods \tet{putch} and \tet{puts} are used to print a character
13915
or a string respectively. The method \tet{flush} is called to finalize a
13916
messages.
13917
13918
The generic functions \tet{pari_putc}, \tet{pari_puts}, \tet{pari_flush} and
13919
\tet{pari_printf} print according to a \emph{default output context}, which
13920
should be sufficient for most purposes. Lower level functions are available,
13921
which take an explicit output context as first argument:
13922
13923
\fun{void}{out_putc}{PariOUT *out, char c} essentially equivalent to
13924
\kbd{out->putc(c)}. In addition, registers whether the last character printed
13925
was a \kbd{\bs n}.
13926
13927
\fun{void}{out_puts}{PariOUT *out, const char *s} essentially equivalent to
13928
\kbd{out->puts(s)}. In addition, registers whether the last character printed
13929
was a \kbd{\bs n}.
13930
13931
\fun{void}{out_printf}{PariOUT *out, const char *fmt, ...}
13932
13933
\fun{void}{out_vprintf}{PariOUT *out, const char *fmt, va_list ap}
13934
13935
\noindent N.B. The function \kbd{out\_flush} does not exist since it would be
13936
identical to \kbd{out->flush()}
13937
13938
\fun{int}{pari_last_was_newline}{void} returns a nonzero value if the last
13939
character printed via \tet{out_putc} or \tet{out_puts} was \kbd{\bs
13940
n}, and $0$ otherwise.
13941
13942
\fun{void}{pari_set_last_newline}{int last} sets the boolean value
13943
to be returned by the function \tet{pari_last_was_newline} to \var{last}.
13944
13945
\subsec{Default output context} They are defined by the global variables
13946
\tet{pariOut} and \tet{pariErr} for normal outputs and warnings/errors, and you
13947
probably do not want to change them. If you \emph{do} change them, diverting
13948
output in nontrivial ways, this probably means that you are rewriting
13949
\kbd{gp}. For completeness, we document in this section what the default
13950
output contexts do.
13951
13952
\misctitle{pariOut} writes output to the \kbd{FILE*} \tet{pari_outfile},
13953
initialized to \tet{stdout}. The low-level methods are actually the standard
13954
\kbd{putc} / \kbd{fputs}, plus some magic to handle a log file if one is
13955
open.
13956
13957
\misctitle{pariErr} prints to the \kbd{FILE*} \tet{pari_errfile}, initialized
13958
to \tet{stderr}. The low-level methods are as above.
13959
13960
You can stick with the default \kbd{pariOut} output context and change PARI's
13961
standard output, redirecting \tet{pari_outfile} to another file, using
13962
13963
\fun{void}{switchout}{const char *name} where \kbd{name} is a character string
13964
giving the name of the file you want to write to; the output is
13965
\emph{appended} at the end of the file. To close the file and revert to
13966
outputting to \kbd{stdout}, call \kbd{switchout(NULL)}.
13967
13968
\subsec{PARI colors}
13969
In this section we describe the low-level functions used to implement GP's
13970
color scheme, attached to the \tet{colors} default. The following symbolic
13971
names are attached to gp's output strings:
13972
13973
\item \tet{c_ERR} an error message
13974
13975
\item \tet{c_HIST} a history number (as in \kbd{\%1 = ...})
13976
13977
\item \tet{c_PROMPT} a prompt
13978
13979
\item \tet{c_INPUT} an input line (minus the prompt part)
13980
13981
\item \tet{c_OUTPUT} an output
13982
13983
\item \tet{c_HELP} a help message
13984
13985
\item \tet{c_TIME} a timer
13986
13987
\item \tet{c_NONE} everything else
13988
13989
\emph{If} the \tet{colors} default is set to a nonempty value, before gp
13990
outputs a string, it first outputs an ANSI colors escape sequence ---
13991
understood by most terminals ---, according to the \kbd{colors}
13992
specifications. As long as this is in effect, the following strings are
13993
rendered in color, possibly in bold or underlined.
13994
13995
\fun{void}{term_color}{long c} prints (as if using \tet{pari_puts}) the ANSI
13996
color escape sequence attached to output object \kbd{c}. If \kbd{c} is
13997
\tet{c_NONE}, revert to default printing style.
13998
13999
\fun{void}{out_term_color}{PariOUT *out, long c} as \tet{term_color},
14000
using output context \kbd{out}.
14001
14002
\fun{char*}{term_get_color}{char *s, long c} returns as a character
14003
string the ANSI color escape sequence attached to output object \kbd{c}.
14004
If \kbd{c} is \tet{c_NONE}, the value used to revert to default printing
14005
style is returned. The argument \kbd{s} is either \kbd{NULL} (string
14006
allocated on the PARI stack), or preallocated storage (in which case, it must
14007
be able to hold at least 16 chars, including the final \kbd{\bs 0}).
14008
14009
\subsec{Obsolete output functions}
14010
14011
These variants of \fun{void}{output}{GEN x}, which prints \kbd{x}, followed by
14012
a newline and a buffer flush are complicated to use and less flexible
14013
than what we saw above, or than the \tet{pari_printf} variants. They are
14014
provided for backward compatibility and are scheduled to disappear.
14015
14016
\fun{void}{brute}{GEN x, char format, long dec}
14017
14018
\fun{void}{matbrute}{GEN x, char format, long dec}
14019
14020
\fun{void}{texe}{GEN x, char format, long dec}
14021
14022
\section{Files}
14023
14024
The following routines are trivial wrappers around system functions
14025
(possibly around one of several functions depending on availability).
14026
They are usually integrated within PARI's diagnostics system, printing
14027
messages if \kbd{DEBUGFILES} is high enough.
14028
14029
\fun{int}{pari_is_dir}{const char *name} returns $1$ if \kbd{name} points to
14030
a directory, $0$ otherwise.
14031
14032
\fun{int}{pari_is_file}{const char *name} returns $1$ if \kbd{name} points to
14033
a directory, $0$ otherwise.
14034
14035
\fun{int}{file_is_binary}{FILE *f} returns $1$ if the file $f$ is a binary
14036
file (in the \tet{writebin} sense), $0$ otherwise.
14037
14038
\fun{void}{pari_unlink}{const char *s} deletes the file named $s$. Warn
14039
if the operation fails.
14040
14041
\fun{void}{pari_fread_chars}{void *b, size_t n, FILE *f} read $n$ chars from
14042
stream $f$, storing the result in pre-allocated buffer $b$ (assumed to be
14043
large enough).
14044
14045
\fun{char*}{path_expand}{const char *s} perform tilde and environment expansion
14046
on $s$. Returns a \kbd{malloc}'ed buffer.
14047
14048
\fun{void}{strftime_expand}{const char *s, char *buf, long max} perform
14049
time expansion on $s$, storing the result (at most \kbd{max} chars) in
14050
buffer \kbd{buf}. Trivial wrapper around
14051
\bprog
14052
time_t t = time(NULL);
14053
strftime(but, max, s, localtime(&t);
14054
@eprog
14055
14056
\fun{char*}{pari_get_homedir}{const char *user} expands \kbd{\til user}
14057
constructs, returning the home directory of user \kbd{user}, or \kbd{NULL} if
14058
it could not be determined (in particular if the operating system has no such
14059
concept). The return value may point to static area and may be overwritten
14060
by subsequent system calls: use immediately or \kbd{strdup} it.
14061
14062
\fun{int}{pari_stdin_isatty}{void} returns $1$ if our standard input
14063
\kbd{stdin} is attached to a terminal. Trivial wrapper around \kbd{isatty}.
14064
14065
\subsec{pariFILE}
14066
14067
PARI maintains a linked list of open files, to reclaim resources
14068
(file descriptors) on error or interrupts. The corresponding data structure
14069
is a \kbd{pariFILE}, which is a wrapper around a standard \kbd{FILE*},
14070
containing further the file name, its type (regular file, pipe, input or
14071
output file, etc.). The following functions create and manipulate this
14072
structure; they are integrated within PARI's diagnostics system, printing
14073
messages if \kbd{DEBUGFILES} is high enough.
14074
14075
\fun{pariFILE*}{pari_fopen}{const char *s, const char *mode} wrapper
14076
around \kbd{fopen(s, mode)}, return \kbd{NULL} on failure.
14077
14078
\fun{pariFILE*}{pari_fopen_or_fail}{const char *s, const char *mode}
14079
simple wrapper around \kbd{fopen(s, mode)}; error on failure.
14080
14081
\fun{pariFILE*}{pari_fopengz}{const char *s} opens the file whose name is
14082
$s$, and associates a (read-only) \kbd{pariFILE} with it. If $s$ is a
14083
compressed file (\kbd{.gz} suffix), it is uncompressed on the fly.
14084
If $s$ cannot be opened, also try to open \kbd{$s$.gz}. Returns \kbd{NULL}
14085
on failure.
14086
14087
\fun{void}{pari_fclose}{pariFILE *f} closes
14088
the underlying file descriptor and deletes the \kbd{pariFILE} struct.
14089
14090
\fun{pariFILE*}{pari_safefopen}{const char *s, const char *mode}
14091
creates a \emph{new} file $s$ (a priori for writing) with \kbd{600}
14092
permissions. Error if the file already exists. To avoid symlink attacks,
14093
a symbolic link exists, regardless of where it points to.
14094
14095
\subsec{Temporary files}
14096
14097
PARI has its own idea of the system temp directory derived from an
14098
environment variable (\kbd{\$GPTMPDIR}, else \kbd{\$TMPDIR}), or the first
14099
writable directory among \kbd{/tmp}, \kbd{/var/tmp} and \kbd{.}.
14100
14101
\fun{char*}{pari_unique_dir}{const char *s} creates a ``unique directory''
14102
and return its name built from the string $s$, the user id and process pid
14103
(on Unix systems). This directory is itself located in the temp
14104
directory mentioned above. The name returned is \tet{malloc}'ed.
14105
14106
\fun{char*}{pari_unique_filename}{const char *s} creates a \emph{new} empty
14107
file in the temp directory, whose name contains the id-string $s$ (truncated
14108
to its first $8$ chars), followed by a system-dependent suffix (incorporating
14109
the ids of both the user and the running process, for instance). The function
14110
returns the tempfile name and creates an empty file with that name. The name
14111
returned is \tet{malloc}'ed.
14112
14113
\fun{char*}{pari_unique_filename_suffix}{const char *s, const char *suf}
14114
analogous to above \tet{pari_unique_filename}, creating a (previously
14115
nonexistent) tempfile whose name ends with suffix \kbd{suf}.
14116
14117
\section{Errors}\label{se:errors}
14118
14119
This section documents the various error classes, and the corresponding
14120
arguments to \tet{pari_err}. The general syntax is
14121
14122
\fun{void}{pari_err}{numerr,...}
14123
14124
\noindent In the sequel, we mostly use sequences of arguments of the form
14125
\bprog
14126
const char *s
14127
const char *fmt, ...
14128
@eprog\noindent where \kbd{fmt} is a PARI
14129
format, producing a string $s$ from the remaining arguments. Since
14130
providing the correct arguments to \tet{pari_err} is quite error-prone, we
14131
also provide specialized routines \kbd{pari\_err\_\var{ERRORCLASS}(\dots)}
14132
instead of \kbd{pari\_err(e\_\var{ERRORCLASS}, \dots)} so that the C compiler
14133
can check their arguments.
14134
14135
\noindent We now inspect the list of valid keywords (error classes) for
14136
\kbd{numerr}, and the corresponding required arguments.
14137
14138
\subsec{Internal errors, ``system'' errors}
14139
14140
\subsubsec{e\_ARCH} A requested feature $s$ is not available on this
14141
architecture or operating system.
14142
\bprog
14143
pari_err(e_ARCH)
14144
@eprog\noindent prints the error message: \kbd{sorry, '$s$' not available on
14145
this system}.
14146
14147
\subsubsec{e\_BUG} A bug in the PARI library, in function $s$.
14148
\bprog
14149
pari_err(e_BUG, const char *s)
14150
pari_err_BUG(const char *s)
14151
@eprog\noindent prints the error message: \kbd{Bug in $s$, please report}.
14152
14153
\subsubsec{e\_FILE} Error while trying to open a file.
14154
\bprog
14155
pari_err(e_FILE, const char *what, const char *name)
14156
pari_err_FILE(const char *what, const char *name)
14157
@eprog\noindent prints the error message: \kbd{error opening
14158
\emph{what}: `\emph{name}'}.
14159
14160
\subsubsec{e\_FILEDESC} Error while handling a file descriptor.
14161
\bprog
14162
pari_err(e_FILEDESC, const char *where, long n)
14163
pari_err_FILEDESC(const char *where, long n)
14164
@eprog\noindent prints the error message: \kbd{invalid file descriptor in
14165
\emph{where}: `\emph{name}'}.
14166
14167
\subsubsec{e\_IMPL} A requested feature $s$ is not implemented.
14168
\bprog
14169
pari_err(e_IMPL, const char *s)
14170
pari_err_IMPL(const char *s)
14171
@eprog\noindent prints the error message: \kbd{sorry, $s$ is not yet
14172
implemented}.
14173
14174
\subsubsec{e\_PACKAGE} Missing optional package $s$.
14175
\bprog
14176
pari_err(e_PACKAGE, const char *s)
14177
pari_err_PACKAGE(const char *s)
14178
@eprog\noindent prints the error message: \kbd{package $s$ is required,
14179
please install it}
14180
14181
\subsec{Syntax errors, type errors}
14182
14183
\subsubsec{e\_DIM} arguments submitted to function $s$ have inconsistent
14184
dimensions. E.g., when solving a linear system, or trying to compute the
14185
determinant of a nonsquare matrix.
14186
\bprog
14187
pari_err(e_DIM, const char *s)
14188
pari_err_DIM(const char *s)
14189
@eprog\noindent prints the error message: \kbd{inconsistent dimensions in $s$}.
14190
14191
\subsubsec{e\_FLAG} A flag argument is out of bounds in function $s$.
14192
\bprog
14193
pari_err(e_FLAG, const char *s)
14194
pari_err_FLAG(const char *s)
14195
@eprog\noindent prints the error message: \kbd{invalid flag in $s$}.
14196
14197
\subsubsec{e\_NOTFUNC} Generated by the PARI evaluator; tried to use a
14198
\kbd{GEN} which is not a \typ{CLOSURE} in a function call syntax (as in
14199
\kbd{f = 1; f(2);}).
14200
\bprog
14201
pari_err(e_NOTFUNC, GEN fun)
14202
@eprog\noindent prints the error message: \kbd{not a function in a function
14203
call}.
14204
14205
\subsubsec{e\_OP} Impossible operation between two objects than cannot be
14206
typecast to a sensible common domain for deeper reasons than a type mismatch,
14207
usually for arithmetic reasons. As in \kbd{O(2) + O(3)}: it is valid to add
14208
two \typ{PADIC}s, provided the underlying prime is the same; so the addition
14209
is not forbidden a priori for type reasons, it only becomes so when
14210
inspecting the objects and trying to perform the operation.
14211
\bprog
14212
pari_err(e_OP, const char *op, GEN x, GEN y)
14213
pari_err_OP(const char *op, GEN x, GEN y)
14214
@eprog\noindent As \kbd{e\_TYPE2}, replacing \kbd{forbidden} by
14215
\kbd{inconsistent}.
14216
14217
\subsubsec{e\_PRIORITY} object $o$ in function $s$ contains
14218
variables whose priority is incompatible with the expected operation.
14219
E.g.~\kbd{Pol([x,1], 'y)}: this raises an error because it's not possible to
14220
create a polynomial whose coefficients involve variables with higher priority
14221
than the main variable.
14222
\bprog
14223
pari_err(e_PRIORITY, const char *s, GEN o, const char *op, long v)
14224
pari_err_PRIORITY(const char *s, GEN o, const char *op, long v)
14225
@eprog\noindent prints the error message: \kbd{incorrect priority
14226
in $s$, variable $v_o$ \var{op} $v$}, were $v_o$ is \kbd{gvar(o)}.
14227
14228
\subsubsec{e\_SYNTAX} Syntax error, generated by the PARI parser.
14229
\bprog
14230
pari_err(e_SYNTAX, const char *msg, const char *e, const char *entry)
14231
@eprog\noindent where \kbd{msg} is a complete error message, and \kbd{e} and
14232
\kbd{entry} point into the \emph{same} character string, which is the input
14233
that was incorrectly parsed: \kbd{e} points to the character where the parser
14234
failed, and $\kbd{entry}\leq \kbd{e}$ points somewhat before.
14235
14236
\noindent Prints the error message: \kbd{msg}, followed by a colon, then
14237
a part of the input character string (in general \kbd{entry} itself, but an
14238
initial segment may be truncated if $\kbd{e}-\kbd{entry}$ is large); a caret
14239
points at \kbd{e}, indicating where the error took place.
14240
14241
\subsubsec{e\_TYPE} An argument $x$ of function $s$ had an unexpected type.
14242
(As in \kbd{factor("blah")}.)
14243
\bprog
14244
pari_err(e_TYPE, const char *s, GEN x)
14245
pari_err_TYPE(const char *s, GEN x)
14246
@eprog\noindent prints the error message: \kbd{incorrect type in $s$
14247
(\typ{$x$})}, where \typ{$x$} is the type of $x$.
14248
14249
\subsubsec{e\_TYPE2} Forbidden operation between two objects than cannot be
14250
typecast to a sensible common domain, because their types do not match up.
14251
(As in \kbd{Mod(1,2) + Pi}.)
14252
\bprog
14253
pari_err(e_TYPE2, const char *op, GEN x, GEN y)
14254
pari_err_TYPE2(const char *op, GEN x, GEN y)
14255
@eprog\noindent prints the error message: \kbd{forbidden} $s$
14256
\typ{$x$} \var{op} \typ{$y$}, where \typ{$z$} denotes the type of $z$.
14257
Here, $s$ denotes the spelled out name of the operator
14258
$\var{op}\in\{\kbd{+}, \kbd{*}, \kbd{/}, \kbd{\%}, \kbd{=}\}$, e.g.
14259
\emph{addition} for \kbd{"+"} or \emph{assignment} for \kbd{"="}. If \var{op}
14260
is not in the above operator, list, it is taken to be the already spelled out
14261
name of a function, e.g. \kbd{"gcd"}, and the error message becomes
14262
\kbd{forbidden} \var{op} \typ{$x$}, \typ{$y$}.
14263
14264
\subsubsec{e\_VAR} polynomials $x$ and $y$ submitted to function $s$ have
14265
inconsistent variables. E.g., considering the algebraic number
14266
\kbd{Mod(t,t\pow2+1)} in \kbd{nfinit(x\pow2+1)}.
14267
\bprog
14268
pari_err(e_VAR, const char *s, GEN x, GEN y)
14269
pari_err_VAR(const char *s, GEN x, GEN y)
14270
@eprog\noindent prints the error message: \kbd{inconsistent variables in $s$
14271
$X$ != $Y$}, where $X$ and $Y$ are the names of the variables of $x$ and $y$,
14272
respectively.
14273
14274
\subsec{Overflows}
14275
14276
\subsubsec{e\_COMPONENT} Trying to access an inexistent component of a
14277
vector/matrix/list: the index is less than $1$ or greater
14278
than the allowed length.
14279
\bprog
14280
pari_err(e_COMPONENT, const char *f, const char *op, GEN lim, GEN x)
14281
pari_err_COMPONENT(const char *f, const char *op, GEN lim, GEN x)
14282
@eprog\noindent prints the error message: \kbd{nonexistent component in $f$:
14283
index \var{op} \var{lim}}. Special case: if $f$ is the empty string (no
14284
meaningful public function name can be used), we ignore it and print the
14285
message: \kbd{nonexistent component: index \var{op} \var{lim}}.
14286
14287
\subsubsec{e\_DOMAIN} An argument $x$ is not in the function's domain (as in
14288
\kbd{moebius(0)} or \kbd{zeta(1)}).
14289
\bprog
14290
pari_err(e_DOMAIN, char *f, char *v, char *op, GEN lim, GEN x)
14291
pari_err_DOMAIN(char *f, char *v, char *op, GEN lim, GEN x)
14292
@eprog\noindent prints the error message: \kbd{domain error in $f$: $v$
14293
\var{op} \var{lim}}. Special case: if \var{op} is the empty string, we ignore
14294
\var{lim} and print the error message: \kbd{domain error in $f$: $v$ out of
14295
range}.
14296
14297
\subsubsec{e\_MAXPRIME} A function using the precomputed list of prime numbers
14298
ran out of primes.
14299
\bprog
14300
pari_err(e_MAXPRIME, ulong c)
14301
pari_err_MAXPRIME(ulong c)
14302
@eprog\noindent prints the error message: \kbd{not enough precomputed primes,
14303
need primelimit \til $c$} if $c$ is nonzero. And simply \kbd{not enough
14304
precomputed primes} otherwise.
14305
14306
\subsubsec{e\_MEM} A call to \tet{pari_malloc} or \tet{pari_realloc} failed.
14307
\bprog
14308
pari_err(e_MEM)
14309
@eprog\noindent prints the error message: \kbd{not enough memory}.
14310
14311
\subsubsec{e\_OVERFLOW} An object in function $s$ becomes too large to be
14312
represented within PARI's hardcoded limits. (As in \kbd{2\pow2\pow2\pow10}
14313
or \kbd{exp(1e100)}, which overflow in \kbd{lg} and \kbd{expo}.)
14314
\bprog
14315
pari_err(e_OVERFLOW, const char *s)
14316
pari_err_OVERFLOW(const char *s)
14317
@eprog\noindent prints the error message: \kbd{overflow in $s$}.
14318
14319
\subsubsec{e\_PREC} Function $s$ fails because input accuracy is too low.
14320
(As in \kbd{floor(1e100)} at default accuracy.)
14321
\bprog
14322
pari_err(e_PREC, const char *s)
14323
pari_err_PREC(const char *s)
14324
@eprog\noindent prints the error message: \kbd{precision too low in $s$}.
14325
14326
\subsubsec{e\_STACK} The PARI stack overflows.
14327
\bprog
14328
pari_err(e_STACK)
14329
@eprog\noindent prints the error message: \kbd{the PARI stack overflows !}
14330
as well as some statistics concerning stack usage.
14331
14332
\subsec{Errors triggered intentionally}
14333
14334
\subsubsec{e\_ALARM} A timeout, generated by the \tet{alarm} function.
14335
\bprog
14336
pari_err(e_ALARM, const char *fmt, ...)
14337
@eprog\noindent prints the error message: $s$.
14338
14339
\subsubsec{e\_USER} A user error, as triggered by \tet{error}($g_1,\dots,g_n)$
14340
in GP.
14341
\bprog
14342
pari_err(e_USER, GEN g)
14343
@eprog\noindent prints the error message: \kbd{user error:}, then the
14344
entries of the vector $g$.
14345
14346
\subsec{Mathematical errors}
14347
14348
\subsubsec{e\_CONSTPOL} An argument of function $s$ is a constant polynomial,
14349
which does not make sense. (As in \kbd{galoisinit(Pol(1))}.)
14350
\bprog
14351
pari_err(e_CONSTPOL, const char *s)
14352
pari_err_CONSTPOL(const char *s)
14353
@eprog\noindent prints the error message: \kbd{constant polynomial in $s$}.
14354
14355
\subsubsec{e\_COPRIME} Function $s$ expected two coprime arguments, and did
14356
receive $x$, $y$ which were not.
14357
\bprog
14358
pari_err(e_COPRIME, const char *s, GEN x, GEN y)
14359
pari_err_COPRIME(const char *s, GEN x, GEN y)
14360
@eprog\noindent prints the error message: \kbd{elements not coprime in $s$:
14361
$x, y$}.
14362
14363
\subsubsec{e\_INV} Tried to invert a noninvertible object $x$.
14364
\bprog
14365
pari_err(e_INV, const char *s, GEN x)
14366
pari_err_INV(const char *s, GEN x)
14367
@eprog\noindent prints the error message: \kbd{impossible inverse in $s$: $x$}.
14368
If $x = \kbd{Mod}(a,b)$ is a \typ{INTMOD} and $a$ is not $0$ mod $b$, this
14369
allows to factor the modulus, as \kbd{gcd}$(a,b)$ is a nontrivial divisor of
14370
$b$.
14371
14372
\subsubsec{e\_IRREDPOL} Function $s$ expected an irreducible polynomial,
14373
and did not receive one. (As in \kbd{nfinit(x\pow2-1)}.)
14374
\bprog
14375
pari_err(e_IRREDPOL, const char *s, GEN x)
14376
pari_err_IRREDPOL(const char *s, GEN x)
14377
@eprog\noindent prints the error message: \kbd{not an irreducible polynomial
14378
in $s$: $x$}.
14379
14380
\subsubsec{e\_MISC} Generic uncategorized error.
14381
\bprog
14382
pari_err(e_MISC, const char *fmt, ...)
14383
@eprog\noindent prints the error message: $s$.
14384
14385
\subsubsec{e\_MODULUS} moduli $x$ and $y$ submitted to function $s$ are
14386
inconsistent. E.g., considering the algebraic number
14387
\kbd{Mod(t,t\pow2+1)} in \kbd{nfinit(t\pow3-2)}.
14388
\bprog
14389
pari_err(e_MODULUS, const char *s, GEN x, GEN y)
14390
pari_err_MODULUS(const char *s, GEN x, GEN y)
14391
@eprog\noindent prints the error message: \kbd{inconsistent moduli in $s$},
14392
then the moduli.
14393
14394
\subsubsec{e\_PRIME} Function $s$ expected a prime number, and did receive $p$,
14395
which was not. (As in \kbd{idealprimedec(nf, 4)}.)
14396
\bprog
14397
pari_err(e_PRIME, const char *s, GEN x)
14398
pari_err_PRIME(const char *s, GEN x)
14399
@eprog\noindent prints the error message: \kbd{not a prime in $s$: $x$}.
14400
14401
\subsubsec{e\_ROOTS0} An argument of function $s$ is a zero polynomial, and
14402
we need to consider its roots. (As in \kbd{polroots(0)}.)
14403
\bprog
14404
pari_err(e_ROOTS0, const char *s)
14405
pari_err_ROOTS0(const char *s)
14406
@eprog\noindent prints the error message: \kbd{zero polynomial in $s$}.
14407
14408
\subsubsec{e\_SQRTN} Tried to compute an $n$-th root of $x$, which does not
14409
exist, in function $s$.
14410
(As in \kbd{sqrt(Mod(-1,3))}.)
14411
\bprog
14412
pari_err(e_SQRTN, GEN x)
14413
pari_err_SQRTN(GEN x)
14414
@eprog\noindent prints the error message: \kbd{not an n-th power residue in
14415
$s$: $x$}.
14416
14417
\subsec{Miscellaneous functions}
14418
14419
\fun{long}{name_numerr}{const char *s} return the error number corresponding to
14420
an error name. E.g. \kbd{name\_numerr("e\_DIM")} returns \kbd{e\_DIM}.
14421
14422
\fun{const char*}{numerr_name}{long errnum} returns the error name
14423
corresponding to an error number. E.g. \kbd{name\_numerr(e\_DIM)} returns
14424
\kbd{"e\_DIM"}.
14425
14426
\fun{char*}{pari_err2str}{GEN err} returns the error message that would be
14427
printed on \typ{ERROR} \kbd{err}. The name is allocated on the PARI stack and
14428
must not be freed.
14429
14430
\section{Hashtables}
14431
A \tet{hashtable}, or associative array, is a set of pairs $(k,v)$ of keys
14432
and values. PARI implements general extensible hashtables for fast data
14433
retrieval: when creating a table, we may either choose to use the PARI stack,
14434
or \kbd{malloc} so as to be stack-independent. A hashtable is implemented as
14435
a table of linked lists, each list containing all entries sharing the same
14436
hash value. The table length is a prime number, which roughly doubles as the
14437
table overflows by gaining new entries; both the current number of entries
14438
and the threshold before the table grows are stored in the table. Finally the
14439
table remembers the functions used to hash the entries's keys and to test for
14440
equality two entries hashed to the same value.
14441
14442
An entry, or \tet{hashentry}, contains
14443
14444
\item a key/value pair $(k,v)$, both of type \kbd{void*} for maximal
14445
flexibility,
14446
14447
\item the hash value of the key, for the table hash function. This hash is
14448
mapped to a table index (by reduction modulo the table length), but it
14449
contains more information, and is used to bypass costly general equality
14450
tests if possible,
14451
14452
\item a link pointer to the next entry sharing the same table cell.
14453
14454
\bprog
14455
typedef struct {
14456
void *key, *val;
14457
ulong hash; /* hash(key) */
14458
struct hashentry *next;
14459
} hashentry;
14460
14461
typedef struct {
14462
ulong len; /* table length */
14463
hashentry **table; /* the table */
14464
ulong nb, maxnb; /* number of entries stored and max nb before enlarging */
14465
ulong pindex; /* prime index */
14466
ulong (*hash) (void *k); /* hash function */
14467
int (*eq) (void *k1, void *k2); /* equality test */
14468
int use_stack; /* use the PARI stack, resp. malloc */
14469
} hashtable;
14470
@eprog\noindent
14471
14472
\fun{hashtable*}{hash_create}{size, hash, eq, use_stack}
14473
\vskip -0.5em % switch to K&R style to avoid atrocious line break
14474
\bprog
14475
ulong size;
14476
ulong (*hash)(void*);
14477
int (*eq)(void*,void*);
14478
int use_stack;
14479
@eprog\noindent
14480
creates a hashtable with enough room to contain
14481
\kbd{size} entries. The functions \kbd{hash} and \kbd{eq} compute the hash
14482
value of keys and test keys for equality, respectively. If \kbd{use\_stack}
14483
is non zero, the resulting table will use the PARI stack; otherwise, we use
14484
\kbd{malloc}.
14485
14486
\fun{hashtable*}{hash_create_ulong}{ulong size, long stack} special case
14487
when the keys are \kbd{ulongs} with ordinary equality test.
14488
14489
\fun{hashtable*}{hash_create_str}{ulong size, long stack} special case
14490
when the keys are character strings with string equality test (and
14491
\tet{hash_str} hash function).
14492
14493
\fun{void}{hash_init}{hashtable *h, ulong size, ulong (*hash)(void*),
14494
int (*eq)(void*,void*), use_stack}
14495
Initialize \kbd{h} for an hashtable with enough room to contain
14496
\kbd{size} entries of type \kbd{void*}. The functions \kbd{eq} test keys for
14497
equality. If \kbd{use\_stack} is non zero, the resulting table will use the
14498
PARI stack; otherwise, we use \kbd{malloc}.
14499
14500
\fun{void}{hash_init_GEN}{hashtable *h, ulong size, int (*eq)(GEN,GEN), use_stack}
14501
Initialize \kbd{h} for an hashtable with enough room to contain
14502
\kbd{size} entries of type \kbd{GEN}. The functions \kbd{eq} test keys for
14503
equality. If \kbd{use\_stack} is non zero, the resulting table will use the
14504
PARI stack; otherwise, we use \kbd{malloc}. The hash used is \kbd{hash\_GEN}.
14505
14506
\fun{void}{hash_init_ulong}{hashtable *h, ulong size, use_stack}
14507
Initialize \kbd{h} for an hashtable with enough room to contain
14508
\kbd{size} entries of type \kbd{ulong}.
14509
If \kbd{use\_stack} is non zero, the resulting table will use the
14510
PARI stack; otherwise, we use \kbd{malloc}.
14511
14512
\fun{void}{hash_insert}{hashtable *h, void *k, void *v} inserts $(k,v)$
14513
in hashtable $h$. No copy is made: $k$ and $v$ themselves are stored. The
14514
implementation does not prevent one to insert two entries with equal
14515
keys $k$, but which of the two is affected by later commands is undefined.
14516
14517
\fun{void}{hash_insert2}{hashtable *h, void *k, void *v, ulong hash}
14518
as \kbd{hash\_insert}, assuming \kbd{h->hash(k)} is \kbd{hash}.
14519
14520
\fun{void}{hash_insert_long}{hashtable *h, void *k, long v} as
14521
\kbd{hash\_insert} but \kbd{v} is a \kbd{long}.
14522
14523
\fun{hashentry*}{hash_search}{hashtable *h, void *k} look for an entry
14524
with key $k$ in $h$. Return it if it one exists, and \kbd{NULL} if not.
14525
14526
\fun{hashentry*}{hash_search2}{hashtable *h, void *k, ulong hash} as
14527
\kbd{hash\_search} assuming \kbd{h->hash(k)} is \kbd{hash}.
14528
14529
\fun{GEN}{hash_haskey_GEN}{hashtable *h, void *k}
14530
returns the associate value if the key $k$ belongs to the hash,
14531
otherwise returns \kbd{NULL}.
14532
14533
\fun{int}{hash_haskey_long}{hashtable *h, void *k, long *v}
14534
returns $1$ if the key $k$ belongs to the hash and set $v$ to its value,
14535
otherwise returns 0.
14536
14537
\fun{hashentry *}{hash_select}{hashtable *h, void *k, void *E, int (*select)(void *, hashentry *)} variant of \tet{hash_search}, useful when entries
14538
with identical keys are inserted: among the entries attached to
14539
key $k$, return one satisfying the selection criterion (such that
14540
\kbd{select(E,e)} is nonzero), or \kbd{NULL} if none exist.
14541
14542
\fun{hashentry*}{hash_remove}{hashtable *h, void *k} deletes an entry $(k,v)$
14543
with key $k$ from $h$ and return it. (Return \kbd{NULL} if none was found.)
14544
Only the linking structures are freed, memory attached to $k$ and $v$
14545
is not reclaimed.
14546
14547
\fun{hashentry*}{hash_remove_select}{hashtable *h, void *k, void *E, int(*select)(void*, hashentry *)}
14548
a variant of \tet{hash_remove}, useful when entries with identical keys are
14549
inserted: among the entries attached to key $k$, return one satisfying the
14550
selection criterion (such that \kbd{select(E,e)} is nonzero) and delete it,
14551
or \kbd{NULL} if none exist. Only the linking structures are freed, memory
14552
attached to $k$ and $v$ is not reclaimed.
14553
14554
\fun{GEN}{hash_keys}{hashtable *h} return in a \typ{VECSMALL} the keys
14555
stored in hashtable $h$.
14556
14557
\fun{GEN}{hash_values}{hashtable *h} return in a \typ{VECSMALL}
14558
the values stored in hashtable $h$.
14559
14560
\fun{void}{hash_destroy}{hashtable *h} deletes the hashtable, by removing all
14561
entries.
14562
14563
\fun{void}{hash_dbg}{hashtable *h} print statistics for hashtable $h$, allows
14564
to evaluate the attached hash function performance on actual data.
14565
14566
Some interesting hash functions are available:
14567
14568
\fun{ulong}{hash_str}{const char *s}
14569
14570
\fun{ulong}{hash_str_len}{const char *s, long len} hash the prefix string
14571
containing the first \kbd{len} characters (assume $\kbd{strlen}(s) \geq
14572
\kbd{len}$).
14573
14574
\fun{ulong}{hash_GEN}{GEN x} generic hash function.
14575
14576
\fun{ulong}{hash_zv}{GEN x} hash a \typ{VECSMALL}.
14577
14578
\section{Dynamic arrays}
14579
14580
A \teb{dynamic array} is a generic way to manage stacks of data that need
14581
to grow dynamically. It allocates memory using \kbd{pari\_malloc}, and is
14582
independent of the PARI stack; it even works before the \kbd{pari\_init} call.
14583
14584
\subsec{Initialization}
14585
14586
To create a stack of objects of type \kbd{foo}, we proceed as follows:
14587
\bprog
14588
foo *t_foo;
14589
pari_stack s_foo;
14590
pari_stack_init(&s_foo, sizeof(*t_foo), (void**)&t_foo);
14591
@eprog\noindent Think of \kbd{s\_foo} as the controlling interface, and
14592
\kbd{t\_foo} as the (dynamic) array tied to it. The value of \kbd{t\_foo}
14593
may be changed as you add more elements.
14594
14595
\subsec{Adding elements}
14596
The following function pushes an element on the stack.
14597
\bprog
14598
/* access globals t_foo and s_foo */
14599
void push_foo(foo x)
14600
{
14601
long n = pari_stack_new(&s_foo);
14602
t_foo[n] = x;
14603
}
14604
@eprog
14605
14606
\subsec{Accessing elements}
14607
14608
Elements are accessed naturally through the \kbd{t\_foo} pointer.
14609
For example this function swaps two elements:
14610
\bprog
14611
void swapfoo(long a, long b)
14612
{
14613
foo x;
14614
if (a > s_foo.n || b > s_foo.n) pari_err_BUG("swapfoo");
14615
x = t_foo[a];
14616
t_foo[a] = t_foo[b];
14617
t_foo[b] = x;
14618
}
14619
@eprog
14620
14621
\subsec{Stack of stacks}
14622
Changing the address of \kbd{t\_foo} is not supported in general.
14623
In particular \kbd{realloc()}'ed array of stacks and stack of stacks are not
14624
supported.
14625
14626
\subsec{Public interface}
14627
Let \kbd{s} be a \kbd{pari\_stack} and \kbd{data} the data linked to it. The
14628
following public fields are defined:
14629
14630
\item \kbd{s.alloc} is the number of elements allocated for \kbd{data}.
14631
14632
\item \kbd{s.n} is the number of elements in the stack and \kbd{data[s.n-1]} is
14633
the topmost element of the stack. \kbd{s.n} can be changed as long as
14634
$0\leq\kbd{s.n}\leq\kbd{s.alloc}$ holds.
14635
14636
\fun{void}{pari_stack_init}{pari_stack *s, size_t size, void **data} links
14637
\kbd{*s} to the data pointer \kbd{*data}, where \kbd{size} is the size of
14638
data element. The pointer \kbd{*data} is set to \kbd{NULL}, \kbd{s->n} and
14639
\kbd{s->alloc} are set to $0$: the array is empty.
14640
14641
\fun{void}{pari_stack_alloc}{pari_stack *s, long nb} makes room for \kbd{nb}
14642
more elements, i.e.~makes sure that $\kbd{s.alloc}\geq\kbd{s.n} + \kbd{nb}$,
14643
possibly reallocating \kbd{data}.
14644
14645
\fun{long}{pari_stack_new}{pari_stack *s} increases \kbd{s.n} by one unit,
14646
possibly reallocating \kbd{data}, and returns $\kbd{s.n}-1$.
14647
14648
\misctitle{Caveat} The following construction is incorrect because
14649
\kbd{stack\_new} can change the value of \kbd{t\_foo}:
14650
\bprog
14651
t_foo[ pari_stack_new(&s_foo) ] = x;
14652
@eprog
14653
14654
\fun{void}{pari_stack_delete}{pari_stack *s} frees \kbd{data} and resets the
14655
stack to the state immediately following \kbd{stack\_init} (\kbd{s->n} and
14656
\kbd{s->alloc} are set to $0$).
14657
14658
\fun{void *}{pari_stack_pushp}{pari_stack *s, void *u} This function assumes
14659
that \kbd{*data} is of pointer type. Pushes the element \kbd{u} on the stack
14660
\kbd{s}.
14661
14662
\fun{void **}{pari_stack_base}{pari_stack *s} returns the address of \kbd{data},
14663
typecast to a \kbd{void **}.
14664
14665
\section{Vectors and Matrices}
14666
14667
\subsec{Access and extract}
14668
See~\secref{se:clean} and~\secref{se:unclean} for various useful constructors.
14669
Coefficients are accessed and set using \tet{gel}, \tet{gcoeff},
14670
see~\secref{se:accessors}. There are many internal functions to extract or
14671
manipulate subvectors or submatrices but, like the accessors above, none of
14672
them are suitable for \tet{gerepileupto}. Worse, there are no type
14673
verification, nor bound checking, so use at your own risk.
14674
14675
\fun{GEN}{shallowcopy}{GEN x} returns a \kbd{GEN} whose components are the
14676
components of $x$ (no copy is made). The result may now be used to compute in
14677
place without destroying $x$. This is essentially equivalent to
14678
\bprog
14679
GEN y = cgetg(lg(x), typ(x));
14680
for (i = 1; i < lg(x); i++) y[i] = x[i];
14681
return y;
14682
@eprog\noindent
14683
except that \typ{MAT} is treated specially since shallow copies of all columns
14684
are made. The function also works for nonrecursive types, but is useless
14685
in that case since it makes a deep copy. If $x$ is known to be a \typ{MAT}, you
14686
may call \tet{RgM_shallowcopy} directly; if $x$ is known not to be a \typ{MAT},
14687
you may call \tet{leafcopy} directly.
14688
14689
\fun{GEN}{RgM_shallowcopy}{GEN x} returns \kbd{shallowcopy(x)}, where $x$
14690
is a \typ{MAT}.
14691
14692
\fun{GEN}{shallowtrans}{GEN x} returns the transpose of $x$, \emph{without}
14693
copying its components, i.~e.,~it returns a \kbd{GEN} whose components are
14694
(physically) the components of $x$. This is the internal function underlying
14695
\tet{gtrans}.
14696
14697
\fun{GEN}{shallowconcat}{GEN x, GEN y} concatenate $x$ and $y$, \emph{without}
14698
copying components, i.~e.,~it returns a \kbd{GEN} whose components are
14699
(physically) the components of $x$ and $y$.
14700
14701
\fun{GEN}{shallowconcat1}{GEN x}
14702
$x$ must be \typ{VEC}, \typ{COL} or \typ{LIST}, concatenate
14703
its elements from left to right. Shallow version of \kbd{gconcat1}.
14704
14705
\fun{GEN}{shallowmatconcat}{GEN v} shallow version of \kbd{matconcat}.
14706
14707
\fun{GEN}{shallowextract}{GEN x, GEN y} extract components
14708
of the vector or matrix $x$ according to the selection parameter $y$.
14709
This is the shallow analog of \kbd{extract0(x, y, NULL)}, see \tet{vecextract}.
14710
\kbdsidx{extract0}
14711
14712
\fun{GEN}{shallowmatextract}{GEN M, GEN l1, GEN l2} extract components of the
14713
matrix $M$ according to the \typ{VECSMALL} $l1$ (list of lines indices) and
14714
$l2$ (list of columns indices).
14715
This is the shallow analog of \kbd{extract0(x, l1, l2)}, see \tet{vecextract}.
14716
\kbdsidx{extract0}
14717
14718
\fun{GEN}{RgM_minor}{GEN A, long i, long j} given a square \typ{MAT} A,
14719
return the matrix with $i$-th row and $j$-th column removed.
14720
14721
\fun{GEN}{vconcat}{GEN A, GEN B} concatenate vertically the two \typ{MAT} $A$
14722
and $B$ of compatible dimensions. A \kbd{NULL} pointer is accepted for an
14723
empty matrix. See \tet{shallowconcat}.
14724
14725
\fun{GEN}{matslice}{GEN A, long a, long b, long c, long d}
14726
returns the submatrix $A[a..b,c..d]$. Assume $a \leq b$ and $c \leq d$.
14727
14728
\fun{GEN}{row}{GEN A, long i} return $A[i,]$, the $i$-th row of the \typ{MAT}
14729
$A$.
14730
14731
\fun{GEN}{row_i}{GEN A, long i, long j1, long j2} return part of the $i$-th
14732
row of \typ{MAT}~$A$: $A[i,j_1]$, $A[i,j_1+1]\dots,A[i,j_2]$. Assume $j_1
14733
\leq j_2$.
14734
14735
\fun{GEN}{rowcopy}{GEN A, long i} return the row $A[i,]$ of
14736
the~\typ{MAT}~$A$. This function is memory clean and suitable for
14737
\kbd{gerepileupto}. See \kbd{row} for the shallow equivalent.
14738
14739
\fun{GEN}{rowslice}{GEN A, long i1, long i2} return the \typ{MAT}
14740
formed by the $i_1$-th through $i_2$-th rows of \typ{MAT} $A$. Assume $i_1
14741
\leq i_2$.
14742
14743
\fun{GEN}{rowsplice}{GEN A, long i} return the \typ{MAT} formed from the
14744
coefficients of \typ{MAT} $A$ with $j$-th row removed.
14745
14746
\fun{GEN}{rowpermute}{GEN A, GEN p}, $p$ being a \typ{VECSMALL}
14747
representing a list $[p_1,\dots,p_n]$ of rows of \typ{MAT} $A$, returns the
14748
matrix whose rows are $A[p_1,],\dots, A[p_n,]$.
14749
14750
\fun{GEN}{rowslicepermute}{GEN A, GEN p, long x1, long x2}, short for
14751
\bprog
14752
rowslice(rowpermute(A,p), x1, x2)
14753
@eprog\noindent
14754
(more efficient).
14755
14756
\fun{GEN}{vecslice}{GEN A, long j1, long j2}, return $A[j_1], \dots,
14757
A[j_2]$. If $A$ is a \typ{MAT}, these correspond to \emph{columns} of $A$.
14758
The object returned has the same type as $A$ (\typ{VEC}, \typ{COL} or
14759
\typ{MAT}). Assume $j_1 \leq j_2$.
14760
14761
\fun{GEN}{vecsplice}{GEN A, long j} return $A$ with $j$-th entry removed
14762
(\typ{VEC}, \typ{COL}) or $j$-th column removed (\typ{MAT}).
14763
14764
\fun{GEN}{vecreverse}{GEN A}. Returns a \kbd{GEN} which has the same
14765
type as $A$ (\typ{VEC}, \typ{COL} or \typ{MAT}), and whose components
14766
are the $A[n],\dots,A[1]$. If $A$ is a \typ{MAT}, these are the
14767
\emph{columns} of $A$.
14768
14769
\fun{void}{vecreverse_inplace}{GEN A} as \kbd{vecreverse}, but reverse
14770
$A$ in place.
14771
14772
\fun{GEN}{vecpermute}{GEN A, GEN p} $p$ is a \typ{VECSMALL} representing
14773
a list $[p_1,\dots,p_n]$ of indices. Returns a \kbd{GEN} which has the same
14774
type as $A$ (\typ{VEC}, \typ{COL} or \typ{MAT}), and whose components
14775
are $A[p_1],\dots,A[p_n]$. If $A$ is a \typ{MAT}, these are the
14776
\emph{columns} of $A$.
14777
14778
\fun{GEN}{vecsmallpermute}{GEN A, GEN p} as \kbd{vecpermute} when \kbd{A} is a
14779
\typ{VECSMALL}.
14780
14781
\fun{GEN}{vecslicepermute}{GEN A, GEN p, long y1, long y2} short for
14782
\bprog
14783
vecslice(vecpermute(A,p), y1, y2)
14784
@eprog\noindent
14785
(more efficient).
14786
14787
\subsec{Componentwise operations}
14788
14789
The following convenience routines automate trivial loops of the form
14790
\bprog
14791
for (i = 1; i < lg(a); i++) gel(v,i) = f(gel(a,i), gel(b,i))
14792
@eprog\noindent
14793
for suitable $f$:
14794
14795
\fun{GEN}{vecinv}{GEN a}. Given a vector $a$,
14796
returns the vector whose $i$-th component is \kbd{ginv}$(a[i])$.
14797
14798
\fun{GEN}{vecmul}{GEN a, GEN b}. Given $a$ and $b$ two vectors of the same
14799
length, returns the vector whose $i$-th component is \kbd{gmul}$(a[i], b[i])$.
14800
14801
\fun{GEN}{vecdiv}{GEN a, GEN b}. Given $a$ and $b$ two vectors of the same
14802
length, returns the vector whose $i$-th component is \kbd{gdiv}$(a[i], b[i])$.
14803
14804
\fun{GEN}{vecpow}{GEN a, GEN n}. Given $n$ a \typ{INT}, returns
14805
the vector whose $i$-th component is $a[i]^n$.
14806
14807
\fun{GEN}{vecmodii}{GEN a, GEN b}. Assuming $a$ and $b$ are two \kbd{ZV}
14808
of the same length, returns the vector whose $i$-th component
14809
is \kbd{modii}$(a[i], b[i])$.
14810
14811
\fun{GEN}{vecmoduu}{GEN a, GEN b}. Assuming $a$ and $b$ are two \typ{VECSMALL}
14812
of the same length, returns the vector whose $i$-th component
14813
is $a[i]~\kbd{\%}~b[i]$.
14814
14815
Note that \kbd{vecadd} or \kbd{vecsub} do not exist since \kbd{gadd}
14816
and \kbd{gsub} have the expected behavior. On the other hand,
14817
\kbd{ginv} does not accept vector types, hence \kbd{vecinv}.
14818
14819
\subsec{Low-level vectors and columns functions}
14820
14821
These functions handle \typ{VEC} as an abstract container type of
14822
\kbd{GEN}s. No specific meaning is attached to the content. They accept both
14823
\typ{VEC} and \typ{COL} as input, but \kbd{col} functions always return
14824
\typ{COL} and \kbd{vec} functions always return \typ{VEC}.
14825
14826
\misctitle{Note} All the functions below are shallow.
14827
14828
\fun{GEN}{const_col}{long n, GEN x} returns a \typ{COL} of \kbd{n} components
14829
equal to \kbd{x}.
14830
14831
\fun{GEN}{const_vec}{long n, GEN x} returns a \typ{VEC} of \kbd{n} components
14832
equal to \kbd{x}.
14833
14834
\fun{int}{vec_isconst}{GEN v} Returns 1 if all the components of \kbd{v} are
14835
equal, else returns 0.
14836
14837
\fun{void}{vec_setconst}{GEN v, GEN x} $v$ a pre-existing vector. Set all its
14838
components to $x$.
14839
14840
\fun{int}{vec_is1to1}{GEN v} Returns 1 if the components of \kbd{v} are
14841
pair-wise distinct, i.e. if $i\mapsto v[i]$ is a 1-to-1 mapping, else returns
14842
0.
14843
14844
\fun{GEN}{vec_append}{GEN V, GEN s} append \kbd{s} to the vector \kbd{V}.
14845
14846
\fun{GEN}{vec_prepend}{GEN V, GEN s} prepend \kbd{s} to the vector \kbd{V}.
14847
14848
\fun{GEN}{vec_shorten}{GEN v, long n} shortens the vector \kbd{v} to \kbd{n}
14849
components.
14850
14851
\fun{GEN}{vec_lengthen}{GEN v, long n} lengthens the vector \kbd{v}
14852
to \kbd{n} components. The extra components are not initialized.
14853
14854
\fun{GEN}{vec_insert}{GEN v, long n, GEN x} inserts $x$ at position $n$ in the vector
14855
$v$.
14856
14857
\fun{GEN}{vec_equiv}{GEN O} given a vector of objects $O$, return a vector
14858
with $n$ components where $n$ is the number of distinct objects in $O$. The
14859
$i$-th component is a \typ{VECSMALL} containing the indices of the elements
14860
in $O$ having the same value. Applied to the image of a function evaluated on
14861
some finite set, it computes the fibers of the function.
14862
14863
\fun{GEN}{vec_reduce}{GEN O, GEN *pE} given a vector of objects $O$,
14864
return the vector $v$ (of the same type as $O$) of \emph{distinct} elements
14865
of $O$ and set a \typ{VECSMALL} $E$ with the same length as $v$, such
14866
that $E[i]$ is the multiplicity of object $v[i]$ in the original $O$.
14867
Shallow function.
14868
14869
\section{Vectors of small integers}
14870
14871
\subsec{\typ{VECSMALL}}
14872
14873
These functions handle \typ{VECSMALL} as an abstract container type
14874
of small signed integers. No specific meaning is attached to the content.
14875
14876
\fun{GEN}{const_vecsmall}{long n, long c} returns a \typ{VECSMALL}
14877
of \kbd{n} components equal to \kbd{c}.
14878
14879
\fun{GEN}{vec_to_vecsmall}{GEN z} identical to \kbd{ZV\_to\_zv(z)}.
14880
14881
\fun{GEN}{vecsmall_to_vec}{GEN z} identical to \kbd{zv\_to\_ZV(z)}.
14882
14883
\fun{GEN}{vecsmall_to_col}{GEN z} identical to \kbd{zv\_to\_ZC(z)}.
14884
14885
\fun{GEN}{vecsmall_to_vec_inplace}{GEN z} apply \kbd{stoi} to all entries
14886
of $z$ and set its type to \typ{VEC}.
14887
14888
\fun{GEN}{vecsmall_copy}{GEN x} makes a copy of \kbd{x} on the stack.
14889
14890
\fun{GEN}{vecsmall_shorten}{GEN v, long n} shortens the \typ{VECSMALL} \kbd{v}
14891
to \kbd{n} components.
14892
14893
\fun{GEN}{vecsmall_lengthen}{GEN v, long n} lengthens the \typ{VECSMALL}
14894
\kbd{v} to \kbd{n} components. The extra components are not initialized.
14895
14896
\fun{GEN}{vecsmall_indexsort}{GEN x} performs an indirect sort of the
14897
components of the \typ{VECSMALL} \kbd{x} and return a permutation stored in a
14898
\typ{VECSMALL}.
14899
14900
\fun{void}{vecsmall_sort}{GEN v} sorts the \typ{VECSMALL} \kbd{v} in place.
14901
14902
\fun{GEN}{vecsmall_reverse}{GEN v} as \kbd{vecreverse} for a \typ{VECSMALL}
14903
\kbd{v}.
14904
14905
\fun{long}{vecsmall_max}{GEN v} returns the maximum of the elements of
14906
\typ{VECSMALL} \kbd{v}, assumed nonempty.
14907
14908
\fun{long}{vecsmall_indexmax}{GEN v} returns the index of the largest
14909
element of \typ{VECSMALL} \kbd{v}, assumed nonempty.
14910
14911
\fun{long}{vecsmall_min}{GEN v} returns the minimum of the elements of
14912
\typ{VECSMALL} \kbd{v}, assumed nonempty.
14913
14914
\fun{long}{vecsmall_indexmin}{GEN v} returns the index of the smallest
14915
element of \typ{VECSMALL} \kbd{v}, assumed nonempty.
14916
14917
\fun{int}{vecsmall_isconst}{GEN v} Returns 1 if all the components of \kbd{v}
14918
are equal, else returns 0.
14919
14920
\fun{int}{vecsmall_is1to1}{GEN v} Returns 1 if the components of \kbd{v} are
14921
pair-wise distinct, i.e. if $i\mapsto v[i]$ is a 1-to-1 mapping, else returns
14922
0.
14923
14924
\fun{long}{vecsmall_isin}{GEN v, long x} returns the first index $i$
14925
such that \kbd{v[$i$]} is equal to \kbd{x}. Naive search in linear time, does
14926
not assume that \kbd{v} is sorted.
14927
14928
\fun{GEN}{vecsmall_uniq}{GEN v} given a \typ{VECSMALL} \kbd{v}, return
14929
the vector of unique occurrences.
14930
14931
\fun{GEN}{vecsmall_uniq_sorted}{GEN v} same as \kbd{vecsmall\_uniq}, but assumes
14932
\kbd{v} sorted.
14933
14934
\fun{long}{vecsmall_duplicate}{GEN v} given a \typ{VECSMALL} \kbd{v}, return
14935
$0$ if there is no duplicates, or the index of the first duplicate
14936
(\kbd{vecsmall\_duplicate([1,1])} returns $2$).
14937
14938
\fun{long}{vecsmall_duplicate_sorted}{GEN v} same as
14939
\kbd{vecsmall\_duplicate}, but assume \kbd{v} sorted.
14940
14941
\fun{int}{vecsmall_lexcmp}{GEN x, GEN y} compares two \typ{VECSMALL} lexically.
14942
14943
\fun{int}{vecsmall_prefixcmp}{GEN x, GEN y} truncate the longest \typ{VECSMALL}
14944
to the length of the shortest and compares them lexicographically.
14945
14946
\fun{GEN}{vecsmall_prepend}{GEN V, long s} prepend \kbd{s} to the
14947
\typ{VECSMALL} \kbd{V}.
14948
14949
\fun{GEN}{vecsmall_append}{GEN V, long s} append \kbd{s} to the
14950
\typ{VECSMALL} \kbd{V}.
14951
14952
\fun{GEN}{vecsmall_concat}{GEN u, GEN v} concat the \typ{VECSMALL} \kbd{u}
14953
and \kbd{v}.
14954
14955
\fun{long}{vecsmall_coincidence}{GEN u, GEN v} returns the numbers of indices
14956
where \kbd{u} and \kbd{v} agree.
14957
14958
\fun{long}{vecsmall_pack}{GEN v, long base, long mod} handles the
14959
\typ{VECSMALL} \kbd{v} as the digit of a number in base \kbd{base} and return
14960
this number modulo \kbd{mod}. This can be used as an hash function.
14961
14962
\fun{GEN}{vecsmall_prod}{GEN v} given a \typ{VECSMALL} \kbd{v}, return
14963
the product of its entries.
14964
14965
\subsec{Vectors of \typ{VECSMALL}}
14966
These functions manipulate vectors of \typ{VECSMALL} (vecvecsmall).
14967
14968
\fun{GEN}{vecvecsmall_sort}{GEN x} sorts lexicographically the components of
14969
the vector \kbd{x}.
14970
14971
\fun{GEN}{vecvecsmall_sort_shallow}{GEN x}, shallow variant of
14972
\kbd{vecvecsmall\_sort}.
14973
14974
\fun{void}{vecvecsmall_sort_inplace}{GEN x, GEN *perm} sort lexicographically
14975
\kbd{x} in place, without copying its components. If
14976
\kbd{perm} is not \kbd{NULL}, it is set to the permutation that would sort
14977
the original \kbd{x}.
14978
14979
\fun{GEN}{vecvecsmall_sort_uniq}{GEN x} sorts lexicographically the components of
14980
the vector \kbd{x}, removing duplicates entries.
14981
14982
\fun{GEN}{vecvecsmall_indexsort}{GEN x} performs an indirect lexicographic
14983
sorting of the components of the vector \kbd{x} and return a permutation
14984
stored in a \typ{VECSMALL}.
14985
14986
\fun{long}{vecvecsmall_search}{GEN x, GEN y} \kbd{x} being a sorted
14987
vecvecsmall and \kbd{y} a \typ{VECSMALL}, search \kbd{y} inside \kbd{x}.
14988
14989
\fun{GEN}{vecvecsmall_max}{GEN x} returns the largest entry in all $x[i]$,
14990
assumed nonempty. Shallow function.
14991
14992
\newpage
14993
\chapter{Functions related to the GP interpreter}
14994
14995
\section{Handling closures}\label{se:closure}
14996
14997
\subsec{Functions to evaluate \typ{CLOSURE}}
14998
14999
\fun{void}{closure_disassemble}{GEN C} print the \typ{CLOSURE} \kbd{C} in
15000
GP assembly format.
15001
15002
\fun{GEN}{closure_callgenall}{GEN C, long n, ...} evaluate the \typ{CLOSURE}
15003
\kbd{C} with the \kbd{n} arguments (of type \kbd{GEN}) following \kbd{n} in
15004
the function call. Assumes \kbd{C} has arity $\geq \kbd{n}$.
15005
15006
\fun{GEN}{closure_callgenvec}{GEN C, GEN args} evaluate the \typ{CLOSURE}
15007
\kbd{C} with the arguments supplied in the vector \kbd{args}. Assumes \kbd{C}
15008
has arity $\geq \kbd{lg(args)-1}$.
15009
15010
\fun{GEN}{closure_callgenvecprec}{GEN C, GEN args, long prec} as
15011
\kbd{closure\_callgenvec} but set the precision locally to \kbd{prec}.
15012
15013
\fun{GEN}{closure_callgenvecdef}{GEN C, GEN args, GEN def} evaluate the \typ{CLOSURE}
15014
\kbd{C} with the arguments supplied in the vector \kbd{args}, where the \typ{VECSMALL}
15015
\kbd{def} indicates which arguments are actually present.
15016
Assumes \kbd{C} has arity $\geq \kbd{lg(args)-1}$.
15017
15018
\fun{GEN}{closure_callgenvecdefprec}{GEN C, GEN args, GEN def, long prec} as
15019
\kbd{closure\_callgenvecdef} but set the precision locally to \kbd{prec}.
15020
15021
\fun{GEN}{closure_callgen0prec}{GEN C, long prec} evaluate the \typ{CLOSURE}
15022
\kbd{C} without arguments, but set the precision locally to \kbd{prec}.
15023
15024
\fun{GEN}{closure_callgen1}{GEN C, GEN x} evaluate the \typ{CLOSURE}
15025
\kbd{C} with argument \kbd{x}. Assumes \kbd{C} has arity $\geq 1$.
15026
15027
\fun{GEN}{closure_callgen1prec}{GEN C, GEN x, long prec} as
15028
\kbd{closure\_callgen1}, but set the precision locally to \kbd{prec}.
15029
15030
\fun{GEN}{closure_callgen2}{GEN C, GEN x, GEN y} evaluate the \typ{CLOSURE}
15031
\kbd{C} with argument \kbd{x}, \kbd{y}. Assumes \kbd{C} has arity $\geq 2$.
15032
15033
\fun{void}{closure_callvoid1}{GEN C, GEN x} evaluate the \typ{CLOSURE}
15034
\kbd{C} with argument \kbd{x} and discard the result. Assumes \kbd{C}
15035
has arity $\geq 1$.
15036
15037
The following technical functions are used to evaluate \emph{inline}
15038
closures and closures of arity 0.
15039
15040
The control flow statements (break, next and return) will cause the
15041
evaluation of the closure to be interrupted; this is called below a
15042
\emph{flow change}. When that occurs, the functions below generally
15043
return \kbd{NULL}. The caller can then adopt three positions:
15044
15045
\item raises an exception (\kbd{closure\_evalnobrk}).
15046
15047
\item passes through (by returning NULL itself).
15048
15049
\item handles the flow change.
15050
15051
\fun{GEN}{closure_evalgen}{GEN code} evaluates a closure and returns the result,
15052
or \kbd{NULL} if a flow change occurred.
15053
15054
\fun{GEN}{closure_evalnobrk}{GEN code} as \kbd{closure\_evalgen} but raise
15055
an exception if a flow change occurs. Meant for iterators where
15056
interrupting the closure is meaningless, e.g.~\kbd{intnum} or \kbd{sumnum}.
15057
15058
\fun{void}{closure_evalvoid}{GEN code} evaluates a closure whose return
15059
value is ignored. The caller has to deal with eventual flow changes by
15060
calling \kbd{loop\_break}.
15061
15062
The remaining functions below are for exceptional situations:
15063
15064
\fun{GEN}{closure_evalres}{GEN code} evaluates a closure and returns the result.
15065
The difference with \kbd{closure\_evalgen} being that, if the flow end by a
15066
\kbd{return} statement, the result will be the returned value instead of
15067
\kbd{NULL}. Used by the main GP loop.
15068
15069
\fun{GEN}{closure_evalbrk}{GEN code, long *status} as \kbd{closure\_evalres}
15070
but set \kbd{status} to a nonzero value if a flow change occurred. This
15071
variant is not stack clean. Used by the break loop.
15072
15073
\fun{GEN}{closure_trapgen}{long numerr, GEN code} evaluates closure, while
15074
trapping error \kbd{numerr}. Return \kbd{(GEN)1L} if error trapped, and the
15075
result otherwise, or \kbd{NULL} if a flow change occurred. Used by trap.
15076
15077
15078
\subsec{Functions to handle control flow changes}
15079
15080
\fun{long}{loop_break}{void} processes an eventual flow changes inside an
15081
iterator. If this function return $1$, the iterator should stop.
15082
15083
\subsec{Functions to deal with lexical local variables}\label{se:pushlex}
15084
15085
Function using the prototype code \kbd{`V'} need to manually create and delete a
15086
lexical variable for each code \kbd{`V'}, which will be given a number $-1, -2,
15087
\ldots$.
15088
15089
\fun{void}{push_lex}{GEN a, GEN code} creates a new lexical variable whose
15090
initial value is $a$ on the top of the stack. This variable get the number
15091
$-1$, and the number of the other variables is decreased by one unit. When
15092
the first variable of a closure is created, the argument \kbd{code} must be the
15093
closure that references this lexical variable. The argument \kbd{code} must be
15094
\kbd{NULL} for all subsequent variables (if any). (The closure contains the
15095
debugging data for the variable).
15096
15097
\fun{void}{pop_lex}{long n} deletes the $n$ topmost lexical variables,
15098
increasing the number of other variables by $n$. The argument $n$ must match
15099
the number of variables allocated through \kbd{push\_lex}.
15100
15101
\fun{GEN}{get_lex}{long vn} get the value of the variable with number \kbd{vn}.
15102
15103
\fun{void}{set_lex}{long vn, GEN x} set the value of the variable with number
15104
\kbd{vn}.
15105
15106
\subsec{Functions returning new closures}
15107
15108
\fun{GEN}{compile_str}{const char *s} returns the closure corresponding to the
15109
GP expression $s$.
15110
15111
\fun{GEN}{closure_deriv}{GEN code} returns a closure corresponding to the
15112
numerical derivative of the closure \kbd{code}.
15113
15114
\fun{GEN}{closure_derivn}{GEN code, long n} returns a closure corresponding to
15115
the numerical derivative of order $n > 0$ of the closure \kbd{code}.
15116
15117
\fun{GEN}{snm_closure}{entree *ep, GEN data}
15118
Let \kbd{data} be a vector of length $m$, \kbd{ep} be an \kbd{entree}
15119
pointing to a C function $f$ of arity $n+m$, returns a \typ{CLOSURE} object
15120
$g$ of arity $n$ such that
15121
$g(x_1,\ldots,x_n)=f(x_1,\ldots,x_n,gel(data,1),...,gel(data,m))$. If
15122
\kbd{data} is \kbd{NULL}, then $m=0$ is assumed. Shallow function.
15123
15124
\fun{GEN}{strtofunction}{char *str} returns a closure corresponding to the
15125
built-in or install'ed function named \kbd{str}.
15126
15127
\fun{GEN}{strtoclosure}{char *str, long n, ...} returns a closure
15128
corresponding to the built-in or install'ed function named \kbd{str} with the
15129
$n$ last parameters set to the $n$ \kbd{GEN}s following $n$. This is
15130
analogous to \kbd{snm\_closure(isentry(str), mkvecn(...))} but the latter has
15131
lower overhead since it does not copy arguments, nor does it validate inputs.
15132
15133
In the example code below, \kbd{agm1} is set to the function
15134
\kbd{x->agm(x,1)} and \kbd{res} is set to \kbd{agm(2,1)}.
15135
15136
\bprog
15137
GEN agm1 = strtoclosure("agm",1, gen_1);
15138
GEN res = closure_callgen1(agm1, gen_2);
15139
@eprog
15140
15141
\subsec{Functions used by the gp debugger (break loop)}
15142
\fun{long}{closure_context}{long s} restores the compilation context starting
15143
at frame \kbd{s+1}, and returns the index of the topmost frame. This allow to
15144
compile expressions in the topmost lexical scope.
15145
15146
\fun{void}{closure_err}{long level} prints a backtrace of the last $20$ stack
15147
frames, starting at frame \kbd{level}, the numbering starting at $0$.
15148
15149
\subsec{Standard wrappers for iterators}
15150
Two families of standard wrappers are provided to interface iterators like
15151
\kbd{intnum} or \kbd{sumnum} with GP.
15152
15153
\subsubsec{Standard wrappers for inline closures}
15154
These wrappers are used to implement GP functions taking inline closures as
15155
input. The object \kbd{(GEN)E} must be an inline closure which is evaluated
15156
with the lexical variable number $-1$ set to $x$.
15157
15158
\fun{GEN}{gp_eval}{void *E, GEN x} is used for the prototype code \kbd{`E'}.
15159
15160
\fun{GEN}{gp_evalprec}{void *E, GEN x, long prec} as \kbd{gp\_eval}, but
15161
set the precision locally to \kbd{prec}.
15162
15163
\fun{long}{gp_evalvoid}{void *E, GEN x} is used for the prototype code
15164
\kbd{`I'}. The resulting value is discarded. Return a nonzero value if a
15165
control-flow instruction request the iterator to terminate immediately.
15166
15167
\fun{long}{gp_evalbool}{void *E, GEN x} returns the boolean
15168
\kbd{gp\_eval(E, x)} evaluates to (i.e. true iff the value is nonzero).
15169
15170
\fun{GEN}{gp_evalupto}{void *E, GEN x} memory-safe version of \kbd{gp\_eval},
15171
\kbd{gcopy}-ing the result, when the evaluator returns components of
15172
previously allocated objects (e.g. member functions).
15173
15174
\subsubsec{Standard wrappers for true closures}
15175
These wrappers are used to implement GP functions taking true closures as
15176
input.
15177
15178
\fun{GEN}{gp_call}{void *E, GEN x} evaluates the closure \kbd{(GEN)E} on $x$.
15179
15180
\fun{GEN}{gp_callprec}{void *E, GEN x, long prec} as \kbd{gp\_call},
15181
but set the precision locally to \kbd{prec}.
15182
15183
\fun{GEN}{gp_call2}{void *E, GEN x, GEN y} evaluates the closure \kbd{(GEN)E}
15184
on $(x,y)$.
15185
15186
\fun{long}{gp_callbool}{void *E, GEN x} evaluates the closure \kbd{(GEN)E} on
15187
$x$, returns \kbd{1} if its result is nonzero, and \kbd{0} otherwise.
15188
15189
\fun{long}{gp_callvoid}{void *E, GEN x} evaluates the closure \kbd{(GEN)E} on
15190
$x$, discarding the result. Return a nonzero value if a control-flow
15191
instruction request the iterator to terminate immediately.
15192
15193
\section{Defaults}
15194
15195
\fun{entree*}{pari_is_default}{const char *s} return the \kbd{entree}
15196
structure attached to $s$ if it is the name of a default, \kbd{NULL}
15197
otherwise.
15198
15199
\fun{GEN}{setdefault}{const char *s, const char *v, long flag} is the
15200
low-level function underlying \kbd{default0}. If $s$ is \kbd{NULL}, call all
15201
default setting functions with string argument \kbd{NULL} and flag
15202
\tet{d_ACKNOWLEDGE}. Otherwise, check whether $s$ corresponds to a default
15203
and call the corresponding default setting function with arguments $v$ and
15204
\fl.
15205
15206
We shall describe these functions below: if $v$ is \kbd{NULL}, we only look
15207
at the default value (and possibly print or return it, depending on
15208
\kbd{flag}); otherwise the value of the default to $v$, possibly after some
15209
translation work. The flag is one of
15210
15211
\item \tet{d_INITRC} called while reading the \kbd{gprc}: print and return
15212
\kbd{gnil}, possibly defer until \kbd{gp} actually starts.
15213
15214
\item \tet{d_RETURN} return the current value, as a \typ{INT} if possible, as
15215
a \typ{STR} otherwise.
15216
15217
\item \tet{d_ACKNOWLEDGE} print the current value, return \kbd{gnil}.
15218
15219
\item \tet{d_SILENT} print nothing, return \kbd{gnil}.
15220
15221
\noindent Low-level functions called by \kbd{setdefault}:
15222
15223
15224
\fun{GEN}{sd_TeXstyle}{const char *v, long flag}
15225
15226
\fun{GEN}{sd_breakloop}{const char *v, long flag}
15227
15228
\fun{GEN}{sd_colors}{const char *v, long flag}
15229
15230
\fun{GEN}{sd_compatible}{const char *v, long flag}
15231
15232
\fun{GEN}{sd_datadir}{const char *v, long flag}
15233
15234
\fun{GEN}{sd_debug}{const char *v, long flag}
15235
15236
\fun{GEN}{sd_debugfiles}{const char *v, long flag}
15237
15238
\fun{GEN}{sd_debugmem}{const char *v, long flag}
15239
15240
\fun{GEN}{sd_echo}{const char *v, long flag}
15241
15242
\fun{GEN}{sd_factor_add_primes}{const char *v, long flag}
15243
15244
\fun{GEN}{sd_factor_proven}{const char *v, long flag}
15245
15246
\fun{GEN}{sd_format}{const char *v, long flag}
15247
15248
\fun{GEN}{sd_graphcolormap}{const char *v, long flag}
15249
15250
\fun{GEN}{sd_graphcolors}{const char *v, long flag}
15251
15252
\fun{GEN}{sd_help}{const char *v, long flag}
15253
15254
\fun{GEN}{sd_histfile}{const char *v, long flag}
15255
15256
\fun{GEN}{sd_histsize}{const char *v, long flag}
15257
15258
\fun{GEN}{sd_lines}{const char *v, long flag}
15259
15260
\fun{GEN}{sd_linewrap}{const char *v, long flag}
15261
15262
\fun{GEN}{sd_log}{const char *v, long flag}
15263
15264
\fun{GEN}{sd_logfile}{const char *v, long flag}
15265
15266
\fun{GEN}{sd_nbthreads}{const char *v, long flag}
15267
15268
\fun{GEN}{sd_new_galois_format}{const char *v, long flag}
15269
15270
\fun{GEN}{sd_output}{const char *v, long flag}
15271
15272
\fun{GEN}{sd_parisize}{const char *v, long flag}
15273
15274
\fun{GEN}{sd_parisizemax}{const char *v, long flag}
15275
15276
\fun{GEN}{sd_path}{const char *v, long flag}
15277
15278
\fun{GEN}{sd_plothsizes}{const char *v, long flag}
15279
15280
\fun{GEN}{sd_prettyprinter}{const char *v, long flag}
15281
15282
\fun{GEN}{sd_primelimit}{const char *v, long flag}
15283
15284
\fun{GEN}{sd_prompt}{const char *v, long flag}
15285
15286
\fun{GEN}{sd_prompt_cont}{const char *v, long flag}
15287
15288
\fun{GEN}{sd_psfile}{const char *v, long flag} The \kbd{psfile} default
15289
is obsolete, don't use this function.
15290
15291
\fun{GEN}{sd_readline}{const char *v, long flag}
15292
15293
\fun{GEN}{sd_realbitprecision}{const char *v, long flag}
15294
15295
\fun{GEN}{sd_realprecision}{const char *v, long flag}
15296
15297
\fun{GEN}{sd_recover}{const char *v, long flag}
15298
15299
\fun{GEN}{sd_secure}{const char *v, long flag}
15300
15301
\fun{GEN}{sd_seriesprecision}{const char *v, long flag}
15302
15303
\fun{GEN}{sd_simplify}{const char *v, long flag}
15304
15305
\fun{GEN}{sd_sopath}{const char *v, int flag}
15306
15307
\fun{GEN}{sd_strictargs}{const char *v, long flag}
15308
15309
\fun{GEN}{sd_strictmatch}{const char *v, long flag}
15310
15311
\fun{GEN}{sd_timer}{const char *v, long flag}
15312
15313
\fun{GEN}{sd_threadsize}{const char *v, long flag}
15314
15315
\fun{GEN}{sd_threadsizemax}{const char *v, long flag}
15316
15317
\noindent Generic functions used to implement defaults: most of the above
15318
routines are implemented in terms of the following generic ones. In all
15319
routines below
15320
15321
\item \kbd{v} and \kbd{flag} are the arguments passed to \kbd{default}:
15322
\kbd{v} is a new value (or the empty string: no change), and \kbd{flag} is one
15323
of \tet{d_INITRC}, \tet{d_RETURN}, etc.
15324
15325
\item \kbd{s} is the name of the default being changed, used to display error
15326
messages or acknowledgements.
15327
15328
\fun{GEN}{sd_toggle}{const char *v, long flag, const char *s, int *ptn}
15329
15330
\item if \kbd{v} is neither \kbd{"0"} nor \kbd{"1"}, an error is raised using
15331
\tet{pari_err}.
15332
15333
\item \kbd{ptn} points to the current numerical value of the toggle (1 or 0),
15334
and is set to the new value (when \kbd{v} is nonempty).
15335
15336
For instance, here is how the timer default is implemented internally:
15337
\bprog
15338
GEN
15339
sd_timer(const char *v, long flag)
15340
{ return sd_toggle(v,flag,"timer", &(GP_DATA->chrono)); }
15341
@eprog
15342
15343
The exact behavior and return value depends on \kbd{flag}:
15344
15345
\item \tet{d_RETURN}: returns the new toggle value, as a \kbd{GEN}.
15346
15347
\item \tet{d_ACKNOWLEDGE}: prints a message indicating the new toggle value
15348
and return \kbd{gnil}.
15349
15350
\item other cases: print nothing and return \kbd{gnil}.
15351
15352
15353
\fun{GEN}{sd_ulong}{const char *v, long flag, const char *s, ulong *ptn,
15354
ulong Min, ulong Max, const char **msg}\hbadness 10000
15355
15356
\item \kbd{ptn} points to the current numerical value of the toggle, and is set
15357
to the new value (when \kbd{v} is nonempty).
15358
15359
\item \kbd{Min} and \kbd{Max} point to the minimum and maximum values allowed
15360
for the default.
15361
15362
\item \kbd{v} must translate to an integer in the allowed ranger, a suffix
15363
among
15364
\kbd{k}/\kbd{K} ($\times 10^3$),
15365
\kbd{m}/\kbd{M} ($\times 10^6$),
15366
or
15367
\kbd{g}/\kbd{G} ($\times 10^9$) is allowed, but no arithmetic expression.
15368
15369
\item \kbd{msg} is a \kbd[NULL]-terminated array of messages or \kbd{NULL}
15370
(ignored). If \kbd{msg} is not \kbd{NULL}, \kbd{msg}$[i]$ contains
15371
a message attached to the value $i$ of the default. The last entry in the
15372
\kbd{msg} array is used as a message attached to all subsequent ones.
15373
15374
The exact behavior and return value depends on \kbd{flag}:
15375
15376
\item \tet{d_RETURN}: returns the new value, as a \kbd{GEN}.
15377
15378
\item \tet{d_ACKNOWLEDGE}: prints a message indicating the new value,
15379
possibly a message attached to it via the \kbd{msg} argument, and return
15380
\kbd{gnil}.
15381
15382
\item other cases: print nothing and return \kbd{gnil}.
15383
15384
\fun{GEN}{sd_intarray}{const char *v, long flag, const char *s, GEN *pz}
15385
15386
\item records a \typ{VECSMALL} array of nonnegative integers.
15387
15388
\item \kbd{pz} points to the current \typ{VECSMALL} value, and is set to the
15389
new value (when \kbd{v} is nonempty).
15390
15391
The exact return value depends on \kbd{flag}:
15392
15393
\item \tet{d_RETURN}: returns the new value, as a \typ{VEC} (converted via
15394
\kbd{zv\_to\_ZV})
15395
15396
\item \tet{d_ACKNOWLEDGE}: prints a message indicating the new value,
15397
(as a \typ{VEC}) and return \kbd{gnil}.
15398
15399
\item other cases: print nothing and return \kbd{gnil}.
15400
15401
\fun{GEN}{sd_string}{const char *v, long flag, const char *s, char **pstr}
15402
\item \kbd{v} is subjet to environment expansion, then time expansion.
15403
15404
\item \kbd{pstr} points to the current string value, and is set to the new
15405
value (when \kbd{v} is nonempty).
15406
15407
\section{Records and Lazy vectors}
15408
The functions in this section are used to implement \kbd{ell} structures and
15409
analogous objects, which are vectors some of whose components are initialized
15410
to dummy values, later computed on demand. We start by initializing the
15411
structure:
15412
15413
\fun{GEN}{obj_init}{long d, long n} returns an \tev{obj} $S$, a \typ{VEC}
15414
with $d$ regular components, accessed as \kbd{gel(S,1)}, \dots,
15415
\kbd{gel(S,d)}; together with a record of $n$ members, all initialized to
15416
$0$. The arguments $d$ and $n$ must be nonnegative.
15417
15418
After \kbd{S = obj\_init(d, n)}, the prototype of our other functions are of
15419
the form
15420
\bprog
15421
GEN obj_do(GEN S, long tag, ...)
15422
@eprog\noindent The first argument $S$ holds the structure to be managed.
15423
The second argument \var{tag} is the index of the struct member (from $1$ to
15424
$n$) we operate on. We recommend to define an \kbd{enum} and use descriptive
15425
names instead of hardcoded numbers. For instance, if $n = 3$, after defining
15426
\bprog
15427
enum { TAG_p = 1, TAG_list, TAG_data };
15428
@eprog\noindent one may use \kbd{TAG\_list} or $2$ indifferently as a tag.
15429
The former being preferred, of course.
15430
15431
\misctitle{Technical note}
15432
In the current implementation, $S$ is a \typ{VEC} with $d+1$ entries.
15433
The first $d$ components are ordinary \typ{GEN} entries, which you can
15434
read or assign to in the customary way. But the last component $\kbd{gel(S,
15435
d+1)}$, a \typ{VEC} of length $n$ initialized to \kbd{zerovec}$(n)$, must
15436
be handled in a special way: you should never access or modify its components
15437
directly, only through the API we are about to describe. Indeed, its entries
15438
are meant to contain dynamic data, which will be stored, retrieved and
15439
replaced (for instance by a value computed to a higher accuracy), while
15440
interacting safely with intermediate \kbd{gerepile} calls. This mechanism
15441
allows to simulate C \kbd{struct}s, in a simpler way than with general
15442
hashtables, while remaining compatible with the GP language, which knows
15443
neither structs nor hashtables. It also serialize the structure in an
15444
ordinary \kbd{GEN}, which facilitates copies and garbage collection (use
15445
\kbd{gcopy} or \kbd{gerepile}), rather than having to deal with individual
15446
components of actual C \kbd{struct}s.
15447
15448
\fun{GEN}{obj_reinit}{GEN S} make a shallow copy of $S$, re-initializing
15449
all dynamic components. This allows ``forking'' a lazy vector while
15450
avoiding both a memory leak, and storing pointers to the same data
15451
in different objects (with risks of a double free later).
15452
15453
\fun{GEN}{obj_check}{GEN S, long tag} if the \emph{tag}-component in $S$
15454
is non empty, return it. Otherwise return \kbd{NULL}. The \typ{INT} $0$
15455
(initial value) is used as a sentinel to indicated an empty component.
15456
15457
\fun{GEN}{obj_insert}{GEN S, long tag, GEN O} insert (a clone of) $O$
15458
as \emph{tag}-component of $S$. Any previous value is deleted, and
15459
data pointing to it become invalid.
15460
15461
\fun{GEN}{obj_insert_shallow}{GEN S, long K, GEN O} as \tet{obj_insert},
15462
inserting $O$ as-is, not via a clone.
15463
15464
\fun{GEN}{obj_checkbuild}{GEN S, long tag, GEN (*build)(GEN)} if the
15465
\emph{tag}-component of $S$ is non empty, return it. Otherwise insert
15466
(a clone of) \kbd{build(S)} as \emph{tag}-component in $S$, and return it.
15467
15468
\fun{GEN}{obj_checkbuild_padicprec}{GEN S, long tag, GEN (*build)(GEN,long),
15469
long prec}
15470
if the \emph{tag}-component of $S$ is non empty \emph{and} has relative
15471
$p$-adic precision $\geq \kbd{prec}$, return it. Otherwise insert (a clone
15472
of) \kbd{build(S, prec)} as \emph{tag}-component in $S$, and return it.
15473
15474
\fun{GEN}{obj_checkbuild_realprec}{GEN S, long tag, GEN (*build)(GEN, long),
15475
long prec} if the \emph{tag}-component of $S$ is non empty \emph{and}
15476
satisfies \kbd{gprecision} $\geq \kbd{prec}$, return it. Otherwise insert (a
15477
clone of) \kbd{build(S, prec)} as \emph{tag}-component in $S$, and return it.
15478
15479
\fun{GEN}{obj_checkbuild_prec}{GEN S, long tag, GEN (*build)(GEN,long), GEN
15480
(*gpr)(GEN), long prec} if the \emph{tag}-component of $S$ is non empty
15481
\emph{and} has precision $\kbd{gpr}(x)\geq \kbd{prec}$, return it. Otherwise
15482
insert (a clone of) \kbd{build(S, prec)} as \emph{tag}-component in $S$,
15483
and return it.
15484
15485
\fun{void}{obj_free}{GEN S} destroys all clones stored in the $n$ tagged
15486
components, and replace them by the initial value $0$. The regular entries of
15487
$S$ are unaffected, and $S$ remains a valid object. This is used to
15488
avoid memory leaks.
15489
15490