umfpack_test.m
3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
%UMFPACK_TEST for testing umfpack2 (requires UFget)
%
% Example:
% umfpack_test
% See also umfpack2
% Copyright 1995-2007 by Timothy A. Davis.
index = UFget ;
f = find (index.nrows == index.ncols) ;
[ignore, i] = sort (index.nrows (f)) ;
f = f (i) ;
Control = umfpack2 ;
Control (1) = 0 ;
figure (1)
clf
for i = f
fprintf ('\nmatrix: %s %s %d\n', index.Group{i}, index.Name{i}, index.nrows(i)) ;
Prob = UFget (i) ;
A = Prob.A ;
n = size (A,1) ;
b = rand (1,n) ;
c = b' ;
try
%-----------------------------------------------------------------------
% symbolic factorization
%-----------------------------------------------------------------------
[P1, Q1, Fr, Ch, Info] = umfpack2 (A, 'symbolic') ;
subplot (2,2,1)
spy (A)
title ('A')
subplot (2,2,2)
treeplot (Fr (1:end-1,2)') ;
title ('supercolumn etree')
%-----------------------------------------------------------------------
% P(R\A)Q = LU
%-----------------------------------------------------------------------
[L,U,P,Q,R,Info] = umfpack2 (A) ;
err = lu_normest (P*(R\A)*Q, L, U) ;
fprintf ('norm est PR\\AQ-LU: %g relative: %g\n', ...
err, err / norm (A,1)) ;
subplot (2,2,3)
spy (P*A*Q)
title ('PAQ') ;
cs = Info (57) ;
rs = Info (58) ;
subplot (2,2,4)
hold off
spy (L|U)
hold on
if (cs > 0)
plot ([0 cs n n 0] + .5, [0 cs cs 0 0]+.5, 'c') ;
end
if (rs > 0)
plot ([0 rs rs 0 0] + cs +.5, [cs cs+rs n n cs]+.5, 'r') ;
end
title ('LU factors')
drawnow
%-----------------------------------------------------------------------
% PAQ = LU
%-----------------------------------------------------------------------
[L,U,P,Q] = umfpack2 (A) ;
err = lu_normest (P*A*Q, L, U) ;
fprintf ('norm est PAQ-LU: %g relative: %g\n', ...
err, err / norm (A,1)) ;
%-----------------------------------------------------------------------
% solve
%-----------------------------------------------------------------------
x1 = b/A ;
y1 = A\c ;
m1 = norm (b-x1*A) ;
m2 = norm (A*y1-c) ;
% factor the transpose
Control (8) = 2 ;
[x, info] = umfpack2 (A', '\', c, Control) ;
lunz0 = info (44) + info (45) - info (67) ;
r = norm (A'*x-c) ;
fprintf (':: %8.2e matlab: %8.2e %8.2e\n', r, m1, m2) ;
% factor the original matrix and solve xA=b
for ir = 0:4
Control (8) = ir ;
[x, info] = umfpack2 (b, '/', A, Control) ;
r = norm (b-x*A) ;
if (ir == 0)
lunz1 = info (44) + info (45) - info (67) ;
end
fprintf ('%d: %8.2e %d %d\n', ir, r, info (81), info (82)) ;
end
% factor the original matrix and solve Ax=b
for ir = 0:4
Control (8) = ir ;
[x, info] = umfpack2 (A, '\', c, Control) ;
r = norm (A*x-c) ;
fprintf ('%d: %8.2e %d %d\n', ir, r, info (81), info (82)) ;
end
fprintf (...
'lunz trans %12d no trans: %12d trans/notrans: %10.4f\n', ...
lunz0, lunz1, lunz0 / lunz1) ;
%-----------------------------------------------------------------------
% get the determinant
%-----------------------------------------------------------------------
det1 = det (A) ;
det2 = umfpack2 (A, 'det') ;
[det3 dexp3] = umfpack2 (A, 'det') ;
err = abs (det1-det2) ;
err3 = abs (det1 - (det3 * 10^dexp3)) ;
denom = det1 ;
if (denom == 0)
denom = 1 ;
end
err = err / denom ;
err3 = err3 / denom ;
fprintf ('det: %20.12e + (%20.12e)i MATLAB\n', ...
real(det1), imag(det1)) ;
fprintf ('det: %20.12e + (%20.12e)i umfpack2\n', ...
real(det2), imag(det2)) ;
fprintf ('det: (%20.12e + (%20.12e)i) * 10^(%g) umfpack2\n', ...
real(det3), imag(det3), dexp3) ;
fprintf ('diff %g %g\n', err, err3) ;
catch
fprintf ('failed\n') ;
end
end