\chapter{A GP tutorial}

This chapter is intended to be a guided tour and a tutorial to the GP calculator.
Many examples will be given, but each time a new function is used, the reader
should look at the appropriate section in the manual for detailed explanations.
Hence although this chapter can be read independently of the rest (for example
to get rapidly acquainted with the possibilities of GP without having to read
the whole reference manual), the reader will profit most from it by reading it
in conjunction with the reference manual.

\section{Greetings!}

So you are sitting in front of your workstation (or terminal, or PC,\dots), and you
type {\bf gp} to get the program started (remember to always hit the {\tt enter} key
and not the {\tt return} key on a Macintosh computer). 

It says hello in its particular manner, and then waits for you after its {\tt prompt},
initially {\bf ?}.

Type $2+2$. What happens? Maybe not what you expect. First of all,
of course, you should tell GP that your input is finished, and this is done by hitting
the {\tt return} (or {\tt newline}) key, or the {\tt enter} key on the Mac. If you
do exactly this, you will get the expected answer. However some of you may be used
to other systems like Macsyma or Maple. In this case, you will have subconsciously
ended the line with a semicolon {\bf ;} before hitting {\tt return}, since this is
how it is done on those systems. In that case, you will simply see GP answering
you with a smug expression, i.e. a new prompt and no answer!
This is because a semicolon at the
end of a line in GP tells it to keep the result, but not to print it (you will
certainly want to use this feature if the output is several pages long).

Try $27*37$. Wow! even multiplication works.

Now this session is getting lengthy, so the second thing one needs to learn is to
quit. Each system has its quit signal (to name a few: {\tt quit}, {\tt quit();},
{\tt exit}, {\tt system},\dots). In GP it is {\tt \bs q} (backslash), the
{\tt q} being of course for quit. Try it.

Now you've done it! You're out of GP, so how do you want to continue studying this
tutorial? Get back in please (see above).

Let's get to more serious stuff. Let's see, I seem to remember that the decimal
expansion of $1/7$ has some interesting properties. Let's see what GP has to say
about this. Type $1/7$. What? This computer is making fun of me, it just spits back
to me my own input, that's not what I want! 

Now stop complaining, and think a little. This system has been written mainly for
pure mathematicians, and not for physicists (although they are welcome to use it :-).
And mathematically, $1/7$ is an element of the field $\Bbb Q$ of rational numbers,
so how else but $1/7$ can the computer give the answer to you? (well maybe $2/14$, but
why complicate matters?). Seriously, the basic point here is that PARI (hence GP)
will almost always try to give you a result which is as precise as possible
(we will se why ``almost'' later), hence since here you gave an operation whose
result can be represented exactly, that's what it gives you.

OK, but I still want the decimal expansion of $1/7$. No problem. Type one of the
following: $1./7$, $1/7.$, $1./7.$, $0.+1/7$,\dots. Immediately 28 decimals of this
fraction appear, and the repeating pattern is $142857$. The reason is that you have
included in the operations numbers like $0.$, $1.$ or $7.$ which are {\sl imprecise\/}
real numbers, hence GP cannot give you an exact result.

Why 28 decimals by the way? well, it is the default initial precision, as indicated
when you launch GP. This has been chosen so that the computations are very fast,
and give already 12 decimals more accuracy than conventional double precision
floating point operations. Only large mainframes or supercomputers have 28 digits
of precision in their standard libraries, and that is the absolute limit.

Not here of course. You can extend the precision (almost) as much as you like as
we will see in a moment.

I'm getting bored, why don't we get on with some more exciting stuff?
Well, try {\tt exp(1)}. Presto, comes out the value of $e$ to 28 digits.
Try {\tt log(exp(1))}. Well, it's not exactly equal to 1, but pretty close!
That's what you lose by working numerically.

Now try {\tt exp(pi*sqrt(163))}. Hmmm, since from our last example we suspect
that the last digit may be wrong, can this really be an integer?
This is the time to change precision. Type {\tt \bs precision=50},
then try {\tt exp(pi*sqrt(163))} again. We were right to suspect that the last
decimal was incorrect, since we get even more nines than before, but it is now
convincingly clear that this is not an integer. Maybe it's a bug in PARI, and
the result is really an integer? Type {\tt sqr(log(\%)/pi)} immediately after
the preceding computation
({\tt \%} means the result of the last computed expression. More generally, the
results are numbered {\tt \%1, \%2, \dots} {\bf including} the results that you
do not want to see printed by putting a semicolon at the end of the line, and
you can evidently use all these quantities in any further computations. {\tt sqr}
is the square function ({\tt sqr(x)=x*x}), not to be confused with {\tt sqrt}
which is the square root function). The result seems to be indistinguishable
from 163, hence it does not seem to be a bug.

In fact it is a known fact that {\tt exp(pi*sqrt(n))} not only is not an integer
or a rational number, but is even a transcendental number when $n$ is a positive
rational number.

So GP is just a fancy calculator, able to give me more decimals than I will ever
need? Not so, GP is incredibly more powerful than an ordinary calculator, even
independently of its arbitrary precision possibilities.
\medskip
{\bf Additional comments}

You are supposed to skip this at first, and come back later.
\smallskip
1) It seems that the text implicitly says that as soon as an imprecise number
is entered, the result will be imprecise. Is this always true? There is a unique
exception: when you multiply an imprecise number by the exact number 0, you will
get the exact 0. Try: $0*1.4$, and compare with $0.*1.4$.
\smallskip
2) Not only can the number of decimal places of real numbers be large, but the
number of digits of integers also. Try $100!$. It is never necessary to tell
in advance to GP the size of the integers that it will encounter, since this is
adjusted automatically. On the other hand, for many computations with
real numbers, it is necessary to specify a default precision (initially 28 digits).
\smallskip
3) Come back to 28 digits of precision ({\tt \bs precision=28}), and type
{\tt exp(24*pi)}. What is this $*$ that you get at the end of the result?

Well, GP never wants you to believe that a result is correct when it is not.
We are working with 28 digits of precision, but the integer part of {\tt exp(24*pi)}
has 33 decimal digits. Hence GP dutifully prints out 33 digits, but it prints a
$*$ to warn you that the last few digits are wrong (without telling you how
many). The $*$ can thus be interpreted as a decimal point with a warning message.
\smallskip
4) To avoid this $*$, there are two ways. One is of course to increase the precision
to more than 33 decimals. Let's try it. To give it a wide margin, we set the
precision to 40 decimals. Then we recall our last result ({\tt \%} or {\tt \%n}
where {\tt n} is the number of the result). What? We still have the $*$! Do you
understand why?

Again let's try to see what's happening. The number you recalled had been computed only
to 28 decimals, and even if you set the precision to 1000 decimals, GP knows that
your number has only 28 digits of accuracy but an integral part with 33 digits. So
you haven't improved things by increasing the precision. Or have you? What if we
retype {\tt exp(24*pi)} now that we have 40 digits? Try it. Now we do not have
any warning, and we see the beginning of what's after the decimal point. We also
see that at 28 decimals the last 6 digits were wrong.
\smallskip
5) There is still another method to avoid the warning: put yourself back into
precision 28. Now type {\tt \bs format=e0.28}. The $e$ instead of the $g$
which is printed when GP greets you, means that real numbers (by opposition to
integers or rational numbers) will be printed in {\sl exponential\/} format, i.e.
with a E xxx denoting powers of 10. Try again {\tt exp(24*pi)} in precision 28.
Now we don't have any warning, and the reason is clear: GP has to print only a
single digit for the integer part, and then it prints out as many decimals as
was asked by the format (here 28).
\smallskip
6) {\sl Warning\/}. Try the following: starting in precision 28, type 
successively {\tt \bs format=e0.50}, then {\tt exp(24*pi)}. Do you
understand why the result is so bad, and why there are lots of zeros at the end?
Convince yourself by typing {\tt log(exp(1))}. The moral is that the {\tt format}
command changes only the output format, but {\sl not\/} the default precision.
On the other hand, the {\tt precision} command changes both the precision and
the output format.

\section{Warming up}

Another thing you better get used to pretty fast is error messages. Try typing
$1/0$. Could'nt be clearer. Taking again our universal example in precision 28,
type {\tt floor(exp(24*pi))} ({\tt floor} is the mathematicians' integer part, not
to be confused with {\tt trunc}, which is the computer scientists': 
{\tt floor$(-3.4)$} is equal to $-4$ while {\tt trunc$(-3.4)$} is equal to $-3$).
You get a more cryptic error message, which you would immediately understand if you
had read the additional comments of the preceding section. Since I told you not
to read them, the explananation is simply that GP is unable to compute the integer
part of {\tt exp(24*pi)} given only 28 decimals of accuracy, since it has 33 digits.

Some error messages are even much more cryptic than that and are sometimes not so
easy to understand (well, it's nothing compared to \TeX's error messages! :-).

For instance, try {\tt log(x)}. Not really clear, is it? But no matter, it simply
tells you that GP simply does not understand what {\tt log(x)} is (although it
does know the {\tt log} function).

Now let's try {\tt sqrt(-1)} to see what error message we get now. Haha! GP even
knows about complex numbers, so impossible to trick it that way. Similarly, try
typing {\tt log(-2)}, {\tt exp(i*pi)}, {\tt i\^{}i},\dots. So we have a lot of real and complex
analysis at our disposal (note that there is always a specific branch of
multivalued complex transcendental functions which is taken, specified in the manual).

Just for fun, let's try {\tt 6*zeta(2)/pi\^{}2}. Pretty good? (If you don't know what the Riemann
zeta function is don't worry, we won't use it).
\medskip
Now GP didn't seem to know what {\tt log(x)} was, although it did know how to compute
numerical values of {\tt log}. This is annoying. Maybe it knows the exponential
function? Let's give it a try. Type {\tt exp(x)}. What's this? If you had had any experience
with other systems, the answer should have simply been {\tt exp(x)} again. But here the
answer is the Taylor expansion of the function around {\tt x=0}, to 16 terms (16 is the
default {\tt serieslength}, which can be changed by typing {\tt \bs serieslength=n},
where {\tt n} is the number of terms that you want in your power series (note the {\tt O(x\^{}16)}
which ends the series, and which is trademark of this type of object in GP. It is the familiar
``bigoh'' notation of analysis).

You will thus automatically get the Taylor expansion of any function that can be expanded
around {\tt x=0}, and incidentally this explains
why we weren't able to do anything with {\tt log(x)} which is not defined at 0. By if we try
{\tt log(1+x)}, then it works. But what if we wanted the expansion around a point different
from 0? Well, you're able to change {\tt x} into {\tt x-a}, aren't you? So for instance
you can type {\tt log(x+2)} to have the expansion of {\tt log} around {\tt x=2}. 

As exercises, try {\tt cos(x)}, {\tt cos(x)\^{}2+sin(x)\^{}2}, {\tt exp(cos(x))},
{\tt exp(exp(x)-1)}, {\tt gamma(1+x)}, with different values of {\tt serieslength} if you like.

Let's try something else: type {\tt (1+x)\^{}3}. No bigoh here, since the result is a polynomial.
Haha, but I have learnt that if you do not take exponents which are integers greater or equal
to 0, you obtain a power series with an infinite number of nonzero terms. Let's try.
Type {\tt (1+x)\^{}($-3$)} (the parentheses around $-3$ are not necessary but make things 
easier to read). Surprise! Contrary to what we expected, we don't get a power series but a
rational function. Again this is for the same reason that $1/7$ just gave you $1/7$: the result
being exact, PARI doesn't see any reason to make it nonexact.

But I still want that power series. To obtain it, just do as in the $1/7$ example:
type {\tt (1+x)\^{}(-3)+O(x\^{}{16})}, {\tt (1+O(x\^{}{16}))*(1+x)\^{}(-3)},
{\tt (1+x+O(x\^{}{16}))\^{}(-3)},etc\dots

Now try {\tt (1+x)\^{}(1/2)}. Now we obtain a power series, since the result is an object
which PARI does not know how to represent exactly (we could teach PARI about algebraic functions,
but then take {\tt (1+x)\^{}pi} as another example). This gives us still another solution to
our preceding exercise: we can type {\tt (1+x)\^{}($-3.$)}. Since $-3.$ is not an exact
quantity, PARI has no means to know that we are dealing with a rational function, and
will instead give you the power series.

Finally, if you want to be really fancy, you can type {\tt taylor((1+x)\^{}($-3$),x)}
(look at the entry for {\tt taylor} for the description of the syntax), but this is in fact
almost never used.
\smallskip
To summarize, in this section we have seen that in addition to integers,
real numbers and rational numbers, PARI can handle complex numbers,
polynomials, power series, rational functions. A large number of functions 
exist which handle these types, but in this tutorial we will only look at
a few. \medskip
{\bf Additional comments}

As before, you are supposed to skip this at first, and come back later.
\smallskip
1) A complex number has a real part and an imaginary part (who would have guessed?).
However, beware that when the imaginary part is the exact integer zero, it is not printed,
but the complex number is not converted to a real number. Hence it may {\sl look\/} like
a real number without being one, and this may cause some confusion in programs which expect
real numbers. For example, type {\tt 3+i-i}. The answer is, as expected 3. But it is still
a complex number. Hence if you then type {\tt (1+x)\^{}\%}, instead of getting the polynomial
{\tt 1+3*x+3*x\^{}2+x\^{}3} as expected, you obtain a power series. Worse, when you try to
apply an arithmetic function, say the Euler {\tt phi} function, you get the error message
which says that ``arithmetic functions want integer arguments'', which you would have
guessed yourself, but the message is difficult to understand since 3 looks like a genuine
integer!!! (Please read again if this is not clear. It is a common source of incomprehension).

Similarly, {\tt 3+x-x} is not the integer 3 but the constant polynomial equal
to 3.

If you want the final expression to be in the simplest form possible (for
example before applying an arithmetic function, or simply because things
will go faster afterwards), apply the function {\tt simplify} to the 
result.

2) As already stated, power series expansions are always implicitly around {\tt x=0}. If you
want them around {\tt x=a}, replace {\tt x} by {\tt z+a} in the function that you want to
expand. For example, to have the expansion of {\tt exp(x)} around {\tt x=1}, we simply type
{\tt exp(z+1)}, where {\tt z} stands for {\tt x-a}. For complicated functions, it may be
simpler to use the substitution function {\tt subst}. For example, if 
{\tt p=1/(x\^{}4+3*x\^{}3+5*x\^{}2-6*x+7)}, you may not want to retype this, replacing {\tt x} by {\tt z+a},
so you can write {\tt subst(p,x,z+a)} (look up the exact description of the {\tt subst} function).

Now try typing {\tt p=1+x+x\^{}2+O(x\^{}10)}, then {\tt subst(p,x,z+1)}. Do you 
understand why you get an error message?

\section{The rest of the PARI types}

Let's handle first some more the basic PARI types.

Type {\tt p=x*exp(-x)}. As expected, you get the power series expansion
to 16 terms (if you have not changed the default). Now type
{\tt pr=reverse(p)}. You are asking here for the {\sl reversion\/} of the
power series $p$, in other words the inverse function. This is possible only
for power series whose first non-zero coefficient is that of $x^1$.
To check the correctness of the result, you can type {\tt subst(p,x,pr)}
or {\tt subst(pr,x,p)} and you should get back {\tt x+O(x\^{}17)}.

Now the coefficients of {\tt pr} obey a very simple formula. First, we would
like to multiply the coefficient of {\tt x\^{}n} by {\tt n!} (in the case of the
exponential function, this would simplify things considerably!). The PARI
function {\tt laplace} does just that. So type {\tt ps=laplace(pr)}. The
coefficients now become integers, which can be immediately recognized by
inspection. The coefficient of {\tt x\^{}n} is now equal to $\text{n}^{\text{n-1}}$. In other words, we have 
\def\n{{\rm n}}
$$\text{pr}=\sum_{\n\ge1}\dfrac{\n^{\n-1}}{\n!}X^{\n}.$$Do you know how to
prove this? (If you have never seen this, the proof is difficult.)
\smallskip
Of course PARI knows about vectors (rows and columns are distinguished, even
though mathematically there is no difference) and matrices. Type for example
{\tt [1,2,3,4]}. This gives the row vector whose coordinates are 1, 2, 3 and 4.
If you want a column vector, type {\tt [1,2,3,4]\~{}}, the tilde meaning of
course transpose. You don't see much difference in the output, except for the
tilde at the end. However, now type {\tt \bs b}: lo and behold, the vector
does become a column. This command is used mainly for this purpose.

Type {\tt m=[a,b,c;d,e,f]}. You have just entered a matrix with 2 rows and 3
columns. Note that the matrix is entered by {\sl rows\/} and the rows are
separated by semicolons ``;''. The matrix is printed naturally in a rectangle
shape. If you want it printed horizontally just as you typed it, type
{\tt \bs r}, or if you want this type of printing to be the permanent default
type {\tt \bs p} twice (the first time puts GP in a mode which in the present
version you do not want to use). Type {\tt \bs p} a third time if you want
to come back to the original default.

Now type {\tt m[1,2]}, {\tt m[1,]}, {\tt m[,2]}. Are explanations necessary?
(In an expression such as {\tt m[j,k]}, the {\tt j} always refers to the
row number, and the {\tt k} to the column number, and the first index is
always 1, never 0. This default cannot be changed.)

Even better, type {\tt m[1,2]=5;m} (the semicolon allows us to put several
instructions on the same line. It is an absolute necessity when we program
in GP). Now type {\tt m[1,]=[15,-17]}. No problem. Finally type
{\tt m[,2]=[j,k]}. You have an error message since you have typed a row
vector, while {\tt m[,2]} is a column vector. If you type instead
{\tt m[,2]=[j,k]\~{}} it works.
\smallskip
Type now {\tt h=hilbert(20)}. You get the so-called ``Hilbert matrix'' whose 
coefficient of row {\tt i} and column {\tt j} is equal to
{\tt 1$/$(i$+$j$-$1)}. Incidentally, the matrix {\tt h} takes too
much room. If you don't want to see it, simply type a semi-colon ``;'' at the
end of the line ({\tt h=hilbert(20);}). This is an example of a ``precomputed''
matrix, built-in PARI. There are only a few. We will see later an example
of a much more general construction.

What is interesting about Hilbert matrices is that first their inverses and
determinants can be computed explicitly (and the inverse has integer
coefficients), and second they are numerically very unstable, which make
them a severe test for linear algebra packages in numerical analysis.
Of course with PARI, no such problem can occur: since the coefficients are
given as rational numbers, the computation will be done exactly, so there
cannot be any numerical error. Try it. Type {\tt d=det(h)} (you have to be
a little patient, this is quite a complicated computation). The result is
a rational number (of course) of numerator equal to 1 and denominator having
226 decimal digits. How do I know, by the way? I did not count! Instead,
simply type {\tt 1.*d}. The result is now in exponential format, and the
exponent gives us the answer.

Now type {\tt hr=1.*h;} (do not forget the semicolon, we don't want to see
all the junk!), then {\tt dr=det(hr)}. You notice two things. First the
computation, although not instantaneous, is much faster than in the rational
case. The reason for this is that PARI is handling real numbers with 28 digits
of accuracy, while in the rational case it is handling integers having up to
226 decimal digits.

The second more important fact is that the result is terribly wrong. If you
compare with {\tt 1.*d} computed earlier, which is correct, you will see that
only 2 decimals agree! This catastrophic unstability is as already 
mentioned one of the characteristics of Hilbert matrices. In fact, the 
situation is much worse than that. Type {\tt norml2(1/h-1/(h*1.))}
(the function {\tt norml2} gives the square of the $L^2$ norm, i.e. the sum of
the squares of the coefficients). Again be patient since computing {\tt 1/h}
will take even more time (not much) than computing {\tt det(h)}. The result
is larger than $10^{50}$, showing that some coefficients of {\tt 1/(h*1.)}
are wrong by as much as $10^{24}$ (the largest error is in fact equal
to $7.644 10^{24}$ for the coefficient of row 15 and column 14, which is a
28 digit integer).

To obtain the correct result after rounding for the inverse, we have to use
a default precision of 56 digits (try it).
\smallskip
Although vectors and matrices can be entered manually, by typing explicitly
their elements, very often the elements satisfy a simple law and one uses a
different syntax. For example, assume that you want a vector whose {\tt k}-th
coordinate is equal to {\tt k\^{}2}. No problem, type for example
{\tt vector(10,k,k\^{}2)} if you want a vector of length 10. Similarly, if
you type 

\centerline{{\tt matrix(5,5,j,k,1$/$(j$+$k$-$1))}}

\noindent you will get the Hilbert
matrix of order 5 (hence the {\tt hilbert} function is unnecessary).
The {\tt j} and {\tt k} represent dummy variables which are used to number
the rows and columns respectively (in the case of a vector only one is present
of course). You must not forget, in addition to the dimensions of the vector
or matrix, to indicate explicitly the names of these variables.

{\bf Important warning.} The letter {\tt i} is reserved for the complex number
equal to the square root of $-1$. Hence it is absolutely forbidden to use it
as a variable. Try typing {\tt vector(10,i,i\^{}2)}, the error message that you
get clearly indicates that GP does not consider {\tt i} as a variable.
%XXX More examples
\medskip
The last PARI types which we have not yet played with are types which are
closely linked to number theory (hence people not interested in number theory
can skip).

The first is the type ``integermod''. Let us see an example. Type
{\tt n=10\^{}15+3}. We want to know whether {\tt n} is prime or not. Of course
we could make use of the builtin facilities of PARI, but let us do otherwise.
We first trial divide by the builtin table of primes, by default up to
500000. We slightly cheat here and use the function {\tt smallfact} which
does exactly this. So type {\tt smallfact(n)}. The result is a 2 column
matrix (as all factoring programs), the first column giving the primes and
the second their exponents. Here we get a single row, telling us that {\tt n}
is not divisible by any prime up to the default (500000). We could now
trial divide further, or even cheat completely and call the PARI function
{\tt factor}, but before we do this let us see how to get an answer ourselves.

By Fermat's little theorem, if {\tt n} is prime we must have 
$\text{a}^{\text{n-1}}\equiv1\pmod{\text{n}}$ for all {\tt a} not divisible by
{\tt n}. Hence we could try this with {\tt a=2} for example. But 
$2^{\text{n-1}}$ is a number with approximately $3\cdot10^{14}$ digits, hence
impossible to write down, even less to compute. But instead type
{\tt a=mod(2,n)}. This creates the number 2 considered now as an element of
the ring $R=\Z/\text{n}\Z$. The elements of $R$, called integermods, can
always be represented by numbers smaller than {\tt n}, hence very small.
Fermat's theorem can be rewritten {\tt $\text{a}^{\text{n-1}}=$mod(1,n)} in
the ring $R$, and this can be computed very efficiently. Type
{\tt a\^{}(n-1)}. The result is definitely {\sl not\/} equal to {\tt mod(1,n)},
thus {\sl proving\/} that {\tt n} is not a prime (if we had obtained 
{\tt mod(1,n)} on the other hand, it would have given us a hint that {\tt n}
is maybe prime, but never a proof).

To find the factors is another story. One must use less naive techniques than
trial division (or be very patient). To finish this example, type 
{\tt factor(n)} to see the factors. Since the smallest factor is 14902357,
you would have had to be very patient with trial division!
\smallskip
The second specifically number-theoretic type is the p-adic numbers. I have
no room for definitions, so please skip if you have no use for such beasts.
A p-adic number is entered as a rational or integer valued expression to which
is added {\tt O(p\^{}n)} (or simply {\tt O(p)} if {\tt n=1}) where {\tt p} is
the prime and {\tt n} the p-adic precision. Apart from the usual arithmetic
operations, you can apply a number of transcendental functions.
For example, type {\tt n=569+O(7\^{}8)}. Then type {\tt s=sqrt(n)}, you
obtain one of the square roots of {\tt n} (if you want to check, type 
{\tt s$*$s$-$n}). Type now {\tt l=log(n)}, then {\tt e=exp(l)}. If you
know about p-adic logarithms, you will not be surprised that {\tt e} is not
equal to {\tt n}. Type {\tt (n/e)\^{}6}: {\tt e} is in fact equal to {\tt n}
times a (p$-$1)-st root of unity.

Incidentally, if you want to get back the integer 569 from the p-adic number
{\tt n}, type {\tt trunc(n)}.
\smallskip
The third number-theoretic type is the type ``quadratic number''. This type
is specially tailored so that we can easily work in a quadratic extension
of a base field (usually $\Bbb Q$). It is a generalization of the type
``complex''. To start, we must specify which quadratic field we want to work
in. For this, we use the function {\tt quadgen} applied to the 
{\sl discriminant\/} {\tt d} (as opposed to the radicand) of the quadratic
field. This returns a number (always printed as {\tt w}) equal to
{\tt (d$+$a)$/$2} where {\tt a} is equal to 0 or 1 according to whether {\tt d}
is even or odd. The behavior of {\tt quadgen} is a little special: although
its result is always printed as {\tt w}, the variable {\tt w} itself is not
set to that value. Hence it is necessary to write systematically
{\tt w=quadgen(d)} using the variable name {\tt w} (or {\tt w1} etc.. if you
have several quadratic fields), otherwise you will be confused.

So type {\tt w=quadgen($-163$)}, then {\tt char(w,x)} which asks for the
characteristic polynomial of {\tt w} expressed with the variable {\tt x}.
The result shows what {\tt w} will represent. You can also ask for {\tt 1.*w}
to see which root of the quadratic has been taken, but this is rarely 
necessary. We can now play in the field $\Bbb Q(\sqrt{-163})$. Type for example
{\tt w\^{}10, norm(3$+$4$*$w), 1$/$(4$+$w)}. More interesting, type
{\tt a=mod(1,23)$*$w} then {\tt b=a\^{}264}. This is a generalization of Fermat's
theorem to quadratic fields. If you do not want to see the 23 all the time,
\def\x{{\tt x}}
type {\tt lift(b)}. Another example, type 
{\tt p$=\x^2+\text{w}*\x+5*\text{w}+7$}, then {\tt norm(p)} (or even
better {\tt simplify(norm(p))}, see above). We thus
obtain the quartic equation over $\Q$ corresponding to the relative quadratic
extension over $\Q(\text{w})$ defined by {\tt p}.

On the other hand, if you type {\tt wr=sqrt(w\^{}2)}, do not expect to get back
{\tt w}. Instead, you get the numerical value, the function {\tt sqrt} being
considered as a ``transcendental'' function, even though it is algebraic.
Type {\tt algdep(wr,2)}. This is one way to get {\tt w} back. Similarly,
type {\tt algdep(sqrt(3$*$w$+$5),4)}. See the users manual for the function
{\tt algdep}.
\smallskip
The fourth number-theoretic type is the type ``polymod'', i.e. polynomial
modulo another polynomial. This type is used to work in algebraic extensions,
for example elements of number fields (if the base field is $\Bbb Q$), or
elements of finite fields (if the base field is $\Z/p\Z$ for a prime $p$,
defined by an integermod). In a sense it is a generalization of the type
quadratic number. The syntax used is the same as for integermods.
For example, instead of typing {\tt w=quadgen($-$163)}, you can type
{\tt w=mod(x,x\^{}2$-$x$+$41)}. Then exactly as in the quadratic case you can
type {\tt w\^{}10, norm(3$+$4$*$w), 1$/$(4$+$w), a=mod(1,23)$*$w, b=a\^{}264},
obtaining of course the same results (type {\tt lift(...)} if you don't want
to see the polynomial {\tt x\^{}2$-$x$+$41} repeated all the time). Of course,
the basic interest is that you can work in any degree, not only quadratic.

There is however a slight difference in behavior. Keeping our {\tt w},
type {\tt 1.$*$}. As you can see, the result is not the same. Type
{\tt sqrt(w)}. Here, we obtain a vector with 2 components, the two components
being the principal branch of the square root of all the possible embeddings
of {\tt w} in $\Bbb C$ (NOT the two square roots). More generally, if {\tt w}
was of degree n, we would get an n-component vector, and similarly for
transcendental functions.

We have at our disposal the usual arithmetic functions, plus a few others.
For example, type {\tt a=mod($\x,\x^3-\x-1$)} defining a cubic extension.
We can for example ask for {\tt b=a\^{}7}. But now assume we want to express
{\tt a} as a polynomial in {\tt b}. This is possible since {\tt b} is also a
generator of the same field. No problem, type {\tt modreverse(b)}. This gives
a new defining polynomial for the same field (i.e. the characteristic 
polynomial of {\tt b}), and expresses {\tt a} in terms of this new polymod, i.e.
in terms of {\tt a}.

\section{Arithmetic functions}

Since PARI is aimed at number theorists, it is not surprising that there
exists a large number of arithmetic functions (see the list in the 
corresponding section of the users manual). We have already seen several, such
as {\tt factor}. Note that {\tt factor} factors not only integers, but also
polynomials. Type for example {\tt factor(x\^{}15$-$1)}. You can also ask to
factor a polynomial modulo a prime p ({\tt factmod}) and even in a finite
field which is not the prime field ({\tt factfq}).

Evidently, you have functions for computing GCD's, extended GCD's, solving
the Chinese remainder theorem and so on.

In addition to the factoring facilities, you have a few functions related to
primality tesing such as {\tt isprime}, {\tt ispsp} and {\tt nextprime}.
Note that in all these cases, only strong pseudoprimes are produced, there
is no sophisticated primality test.

The usual multiplicative arithmetic functions exist (the M\"obius $\mu$ 
function ({\tt mu}) the Euler $\phi$ function ({\tt phi}), the $\omega$
and $\Omega$ functions ({\tt omega} and {\tt bigomega}), the sum of divisors
functions ({\tt sigma} and {\tt sigmak}), etc\dots

You can compute continued fractions. For example, if you type 
{\tt\bs precision=1000} then {\tt cf(exp(1))} you obtain the continued fraction
of the base of natural logarithms, which as you can see obeys a very simple
pattern (can you prove it?).

A number of more sophisticated functions relative to quadratic fields
are also available, such as class numbers ({\tt classno} and {\tt hclassno}),
fundamental units ({\tt unit}) and regulator ({\tt regula}), as well as
functions for handling binary quadratic forms.
%XXXExamples
%Then functions for polynomials
%Fns for elliptic curves
%Section on transc functs and sumalt etc...
%Final section: programming.


\vfill\eject