|
NOTES Matrices for testing of numeric calculus routines |
By Foxes & C |
As we
have finished same numeric routines for linear algebra - such as matrix
inversion, linear system, etc. - we need something to perform our tests. Better
if we know the exact solution in order to evaluate the approximation errors.
We have
collected here same useful matrices used in our testing activity. Hope these
help same other developers or mathematics
Example
1:
Hilbert's matrices
Very easy
to generate: a(i,j) = 1/(i+j-1)
|
1 |
1/2 |
1/3 |
1/4 |
1/5 |
1/6 |
|
1/2 |
1/3 |
1/4 |
1/5 |
1/6 |
1/7 |
|
1/3 |
1/4 |
1/5 |
1/6 |
1/7 |
1/8 |
|
1/4 |
1/5 |
1/6 |
1/7 |
1/8 |
1/9 |
|
1/5 |
1/6 |
1/7 |
1/8 |
1/9 |
1/10 |
|
1/6 |
1/7 |
1/8 |
1/9 |
1/10 |
1/11 |
Despite
of their clean aspect this kind of matrices are very ill-conditioned also for
moderate dimension. The exact determinants and inverse matrices for order 6,7,8
are listed below:
6°
order Hilbert matrix: Determinant of = 1/186313420339200000 @ 5.367E-18
|
36 |
-630 |
3360 |
-7560 |
7560 |
-2772 |
|
-630 |
14700 |
-88200 |
211680 |
-220500 |
83160 |
|
3360 |
-88200 |
564480 |
-1411200 |
1512000 |
-582120 |
|
-7560 |
211680 |
-1411200 |
3628800 |
-3969000 |
1552320 |
|
7560 |
-220500 |
1512000 |
-3969000 |
4410000 |
-1746360 |
|
-2772 |
83160 |
-582120 |
1552320 |
-1746360 |
698544 |
7°
order Hilbert matrix: Determinant of = 1/2067909047925770649600000 @ 4.835E-25
|
49 |
-1176 |
8820 |
-29400 |
48510 |
-38808 |
12012 |
|
-1176 |
37632 |
-317520 |
1128960 |
-1940400 |
1596672 |
-504504 |
|
8820 |
-317520 |
2857680 |
-10584000 |
18711000 |
-15717240 |
5045040 |
|
-29400 |
1128960 |
-10584000 |
40320000 |
-72765000 |
62092800 |
-20180160 |
|
48510 |
-1940400 |
18711000 |
-72765000 |
133402500 |
-115259760 |
37837800 |
|
-38808 |
1596672 |
-15717240 |
62092800 |
-115259760 |
100590336 |
-33297264 |
|
12012 |
-504504 |
5045040 |
-20180160 |
37837800 |
-33297264 |
11099088 |
8°
order Hilbert matrix: Determinant of = 1/365356847125734485878112256000000
@ 2.737E-33
|
64 |
-2016 |
20160 |
-92400 |
221760 |
-288288 |
192192 |
-51480 |
|
-2016 |
84672 |
-952560 |
4656960 |
-11642400 |
15567552 |
-10594584 |
2882880 |
|
20160 |
-952560 |
11430720 |
-58212000 |
149688000 |
-204324120 |
141261120 |
-38918880 |
|
-92400 |
4656960 |
-58212000 |
304920000 |
-800415000 |
1109908800 |
-776936160 |
216216000 |
|
221760 |
-11642400 |
149688000 |
-800415000 |
2134440000 |
-2996753760 |
2118916800 |
-594594000 |
|
-288288 |
15567552 |
-204324120 |
1109908800 |
-2996753760 |
4249941696 |
-3030051024 |
856215360 |
|
192192 |
-10594584 |
141261120 |
-776936160 |
2118916800 |
-3030051024 |
2175421248 |
-618377760 |
|
-51480 |
2882880 |
-38918880 |
216216000 |
-594594000 |
856215360 |
-618377760 |
176679360 |
Example
2:
Combination's matrices
This
class of matrices is very useful because it can be easy generate but - this is
very important - we can predict the exact results of the system Ax=b for any
dimension. Another features very important is that they have lots of decimal
values, very useful for testing the round-off error. Moreover the exact results
are always integer and this facilitates the comparing.
They are
defined with:
|
a(1,j) = 1 a(i,1) = 1/i |
for j =1 ...n for i=1 ..n |
|
|
a(i,j) = a(i,j-1)+a(i-1,j-1) |
for i=2 ..n , j=2 ..n |
|
The
matrix can be built starting from the first row - all 1 - and the first column
- inverses of natural numbers.
After that, each cells is the sum of the two left and left-upper cells
|
1 |
1 |
1 |
1 |
1 |
1 |
|
1/2 |
a21+a11 |
a22+a12 |
a23+a13 |
.... |
|
|
1/3 |
a31+a21 |
a32+a22 |
a33+a23 |
.... |
|
|
1/4 |
a41+a31 |
a42+a32 |
a43+a33 |
... |
|
|
1/5 |
a51+a41 |
... |
.... |
|
|
|
1/6 |
a61+a51 |
|
|
|
|
These matrices are unitary: that is,
DET=1. Here is a matrix of 7° order
|
1 |
1 |
1 |
1 |
1 |
1 |
1 |
|
1/2 |
1.5 |
2.5 |
3.5 |
4.5 |
5.5 |
6.5 |
|
1/3 |
0.83333 |
2.33333 |
4.83333 |
8.33333 |
12.8333 |
18.3333 |
|
1/4 |
0.58333 |
1.41667 |
3.75 |
8.58333 |
16.9167 |
29.75 |
|
1/5 |
0.45 |
1.03333 |
2.45 |
6.2 |
14.7833 |
31.7 |
|
1/6 |
0.36667 |
0.81667 |
1.85 |
4.3 |
10.5 |
25.2833 |
|
1/7 |
0.30952 |
0.67619 |
1.49286 |
3.34286 |
7.64286 |
18.1429 |
If we
solve the linear system A x = b , where b is the vector: b = [0,
0, .....0, 1]T
The exact
solution is given by the following simple formula
|
x(i) = (-1)n+i *Comb(n-1,i-1) |
for
i=1 ..n |
In this
case the values of the solution are:
|
1 |
-6 |
15 |
-20 |
15 |
-6 |
1 |
For a 10° order we have:
|
-1 |
9 |
-36 |
84 |
-126 |
126 |
-84 |
36 |
-9 |
1 |
For a 15° order we have:
|
1 |
-14 |
91 |
-364 |
1001 |
-2002 |
3003 |
-3432 |
3003 |
-2002 |
1001 |
-364 |
91 |
-14 |
1 |
And so on...
We can
use also this results to measure the general accuracy of the invert matrix; we
have only to check the combination's vector in the last column.
This
matrices are suitable to build test-case for high dimension > 20.
The
round-off error measured for several matrix dimension is show in the following
table
|
Dim |
Rel.Error |
|
7 |
1.05E-14 |
|
13 |
8.26E-12 |
|
20 |
3.41E-08 |
|
30 |
6.14E-05 |
Example 3: Sparse
matrices
A useful
class is generate as following:
|
a11 |
-1 |
- 1/2 |
- 1/3 |
- 1/4 |
- 1/5 |
|
-1 |
1 |
0 |
0 |
0 |
0 |
|
- 1/2 |
0 |
1/2 |
0 |
0 |
0 |
|
- 1/3 |
0 |
0 |
1/3 |
0 |
0 |
|
- 1/4 |
0 |
0 |
0 |
1/4 |
0 |
|
- 1/5 |
0 |
0 |
0 |
0 |
1/5 |
The first
element a11 is computed at the end by the formula:
|
a11 = 1+Si aii |
for
i= 2...n |
for n = 6
we have a11 = 3+2/7 = 3.283333...
Determinant
of this class can be easy computed as DET = (n-1)! . for n = 6 , DET = 120
Also the
inverse is always note, being this integer, simply matrix:
|
1 |
1 |
1 |
1 |
1 |
1 |
|
1 |
2 |
1 |
1 |
1 |
1 |
|
1 |
1 |
3 |
1 |
1 |
1 |
|
1 |
1 |
1 |
4 |
1 |
1 |
|
1 |
1 |
1 |
1 |
5 |
1 |
|
1 |
1 |
1 |
1 |
1 |
6 |
Example
4:
Wandermonde's matrices
This is a
note class of difficult matrices. An interesting feature about building test
matrices is that round-off error can be modulate by a single parameter.
|
1 |
1 |
1 |
1 |
... |
|
x |
x-1 |
x-2 |
x-3 |
... |
|
x2 |
(x-1)2 |
(x-2)2 |
(x-3)2 |
... |
|
x3 |
(x-1)3 |
(x-2)3 |
(x-3)3 |
... |
|
... |
... |
... |
... |
... |
For
example, giving x = 0 and n= 6 we get the following matrix
|
1 |
1 |
1 |
1 |
1 |
1 |
|
0 |
1 |
2 |
3 |
4 |
5 |
|
0 |
1 |
4 |
9 |
16 |
25 |
|
0 |
1 |
8 |
27 |
64 |
125 |
|
0 |
1 |
16 |
81 |
256 |
625 |
|
0 |
1 |
32 |
243 |
1024 |
3125 |
This
matrix can be used as test for solving linear system Ax = b , where b is
given by the sum of each row
|
bi
= Sj aij |
for
j= 1...n, for i= 1...n |
In our
example: bT = [ 6 , 15 , 55 , 225 , 979 ,
4425 ]
With this
values, the exact solution x is always unitary; that is x = [ 1,
1 , 1 , 1 , 1 , 1 ]
The
difference between a numerical solution and the exact one, gives the general
accuracy of the algorithm used. If we want to increase the difficult without
change the matrix order, we simply increase the parameter x . The
following table shows the round-off error for several values of x
|
x |
error |
|
1 |
8.3E-12 |
|
10 |
6.6E-10 |
|
20 |
8.8E-09 |
|
50 |
1.8E-06 |
|
100 |
2.8E-05 |
Example
5:
Singular matrices
|
127 |
-507 |
245 |
|
-507 |
2025 |
-987 |
|
245 |
-987 |
553 |
This
simple 3 x 3 matrix is singular; DET = 0
Probably,
with any 32bit numeric program you can't prove it! You will get a (wrong!)
inverse
See also Limit in matrix computation
Example
6:
Tartaglia's matrices
This
class of matrices is very useful because it can be easy generate but - this is
very important - we can predict the exact results of the inverse for any
dimension. Another features very important is that they both Tartaglia’s matrix
and its inverse have always integer values, very useful for testing the
round-off error.
They are
defined with:
|
a(1,j) =
1 a(i,1) = 1 |
for j =1 ...n for i=1 ..n |
(all 1
in the first row) (all 1
in the first column) |
|
|
a(i,j) = Sj a(i-1, j) |
for j=2 ..n |
||
The
matrix can be built starting from the first row - all 1 - and the first column –
all 1.
After that, each cells is the sum of all left cells of the upper row. Example
a(4,4) = a(3,4)+a(3,3)+a(3,2)+a(3,1)
= 10+6+3+1
Here is a
6x6 Tartaglia’s matrix
|
1 |
1 |
1 |
1 |
1 |
1 |
|
1 |
2 |
3 |
4 |
5 |
6 |
|
1 |
3 |
6 |
10 |
15 |
21 |
|
1 |
4 |
10 |
20 |
35 |
56 |
|
1 |
5 |
15 |
35 |
70 |
126 |
|
1 |
6 |
21 |
56 |
126 |
252 |
And here
is its inverse
|
6 |
-15 |
20 |
-15 |
6 |
-1 |
|
-15 |
55 |
-85 |
69 |
-29 |
5 |
|
20 |
-85 |
146 |
-127 |
56 |
-10 |
|
-15 |
69 |
-127 |
117 |
-54 |
10 |
|
6 |
-29 |
56 |
-54 |
26 |
-5 |
|
-1 |
5 |
-10 |
10 |
-5 |
1 |
As we can
see both matrices are integer.
Any round-off error of the inverse matrix must be regard as a round-off error
and immediately detected.
This trick can be used to evaluate the round-off errors introduced by an
algorithm for inverting matrices without to know exactely the result. For
example, assume that in the inversion of a Tartaglia’s matrix you find an
element like this
A(1,1) = 5.99999999999985
Clearly,
the exact value is 6 end the error will
easily compute in the following way:
Err(1,1)
= | A(1,1) – Round(A(1,1),0)| = 0.000000003456 @ 1.518E-13
If we
take the average of errors of all matrix elements, repeating for different
dimension of Tartaglia’s matrices, we have te following result:
|
Dim |
Err |
Err.rel |
|
5 |
5.00E-16 |
6.00E-17 |
|
6 |
4.96E-12 |
6.27E-15 |
|
8 |
7.80E-10 |
9.62E-12 |
|
9 |
9.06E-09 |
1.94E-10 |
|
10 |
1.11E-05 |
2.85E-09 |
|
11 |
0.000286 |
3.23E-08 |
|
13 |
0.14 |
2.29E-06 |
As we can
see the round-off errors became evident also for moderate dimension.