![](https://csdnimg.cn/release/download_crawler_static/13959625/bg9.jpg)
Of course PARI knows about vectors (rows and columns are distinguished, even though math-
ematically there is no difference) and matrices. Type for example [1,2,3,4]. This gives the row
vector whose coordinates are 1, 2, 3 and 4. If you want a column vector, type [1,2,3,4]
~
, the
tilde me aning of course transpose. You don’t see much difference in the output, except for the
tilde at the end. However, now type \b: lo and behold, the vector does become a column. The \b
command is used mainly for this purpose.
Type 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 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
\a, or if you want this type of printing to be the permanent default typ e default(output, 1).
Type default(output, 0) if you want to come back to the original default.
Now type m[1,2], m[1,], m[,2]. Are explanations necessary? (In an expression such as
m[j,k], the j always refers to the row number, and the k to the column number, and the first
index is always 1, never 0. This default cannot be changed.)
Even b etter, type m[1,2] = 5; m (the semicolon also allows us to put several instructions on
the same line. The final result will be the output of the last statement on the line). Now typ e
m[1,] = [15,-17,8]. No problem. Finally type m[,2] = [j,k]. You have an error message since
you have typed a row vector, while m[,2] is a column vector. If you type instead m[,2] = [j,k]
~
it works.
Type now h = mathilbert(20). You get the so-called “Hilbert matrix” whose coefficient of
row i and column j is equal to (i +j −1)
−1
. Incidentally, the matrix h takes too much room. If you
don’t want to see it, simply type a semi-colon “;” at the end of the li ne (h = mathilbert(20);).
This is an example of a “precomputed” matrix, built into 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 d = matdet(h) (you have to b e 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 1.* d. The
result is now in exponential format, and the exponent gives us the answer. Another, more natural,
way would be to simply type sizedigit(1/d).
Now type hr = 1.* h; (do not forget the semicolon, we don’t want to see all the junk!), then
dr = matdet(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
1.∗d computed earlier, which is correct, you will see that only 2 decimals agree! This catastrophic
instability is as already mentioned one of the characteristics of Hilber t matrices. In fact, the
situation is much worse than that. Type norml2(1/h - 1/hr) (the function norml2 gives the
square of the L
2
norm, i.e. the sum of the squares of the coefficients). Again be patient since
computing 1/h will take even more time (not much) than computing matdet(h). The result is
9