File: ldltest.m

package info (click to toggle)
ufsparse 1.2-7
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 27,536 kB
  • ctags: 5,848
  • sloc: ansic: 89,328; makefile: 4,721; fortran: 1,991; csh: 207; sed: 162; awk: 33; java: 30; sh: 8
file content (147 lines) | stat: -rw-r--r-- 3,439 bytes parent folder | download
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
%
% ldltest.m:  compile and test the LDL mexFunction
%
% LDL Version 1.2, Copyright (c) 2005 by Timothy A Davis,
% University of Florida.  All Rights Reserved.  See README for the License.

clear
help ldl
mex -inline ldl.c ldlmex.c

A = sparse ([ ]) ;

[L, D, Parent, fl] = ldl (A) ;
[L, D, Parent, fl] = ldl (A, [ ]) ;

try
    [L, D, Parent, fl] = ldl (A, [1 2])
    ok = 0 ;
catch
    ok = 1 ;
end
if (~ok) error ('?'), end

try
    ldl
    ok = 0 ;
catch
    ok = 1 ;
end
if (~ok) error ('?'), end

try
    [L, D, Parent, fl] = ldl (1)
    ok = 0 ;
catch
    ok = 1 ;
end
if (~ok) error ('?'), end

try
    x = ldl (1,2) ;
    ok = 0 ;
catch
    ok = 1 ;
end
if (~ok) error ('?'), end

A =[ ...
1.7     0     0     0     0     0     0     0   .13     0
  0   1.0     0     0   .02     0     0     0     0   .01
  0     0   1.5     0     0     0     0     0     0     0
  0     0     0   1.1     0     0     0     0     0     0
  0   .02     0     0   2.6     0   .16   .09   .52   .53
  0     0     0     0     0   1.2     0     0     0     0
  0     0     0     0   .16     0   1.3     0     0   .56
  0     0     0     0   .09     0     0   1.6   .11     0
.13     0     0     0   .52     0     0   .11   1.4     0
  0   .01     0     0   .53     0   .56     0     0   3.1 ] ;
A = sparse (A) ;
b = [ ...
 .98 .64 .05
 .58 .20 .44
 .42 .37 .30
 .51 .78 .84
 .33 .68 .01
 .43 .46 .76
 .22 .56 .97
 .57 .79 .99
 .76 .05 .78
 .52 .60 .43 ] ;
P = [3 10 2 5 8 6 9 7 1 4] ;
I = speye (10) ;

[L, D, Parent, fl] = ldl (A) ;
err = norm ((L+I)*D*(L+I)'-A, 1) ;
fprintf ('err: %g fl: %g\n', err, fl) ;
if (err > 1e-14) error ('?'), end ;

Parent2 = etree (A) ;
if (any (Parent2 ~= Parent)) error ('?'), end

[L, D, Parent] = ldl (A) ;
err = norm ((L+I)*D*(L+I)'-A, 1) ;
fprintf ('err: %g\n', err) ;
if (err > 1e-14) error ('?'), end ;

[L, D] = ldl (A) ;
err = norm ((L+I)*D*(L+I)'-A, 1) ;
fprintf ('err: %g\n', err) ;
if (err > 1e-14) error ('?'), end ;

L2 = ldl (A) ;
err = norm (L - L2, 1) ;
fprintf ('err: %g\n', err) ;
if (err > 1e-14) error ('?'), end ;

x = ldl (A, [ ], b) ;
err = norm (A*x-b, 1) ;
fprintf ('err: %g\n', err) ;
if (err > 1e-14) error ('?'), end ;

[x, fl] = ldl (A, [ ], b) ;
err = norm (A*x-b, 1) ;
fprintf ('err: %g fl: %g\n', err, fl) ;
if (err > 1e-14) error ('?'), end ;

[L, D, Parent, fl] = ldl (A, P) ;
err = norm ((L+I)*D*(L+I)'-A(P,P), 1) ;
fprintf ('err: %g fl: %g\n', err, fl) ;
if (err > 1e-14) error ('?'), end ;

Parent2 = etree (A (P,P)) ;
if (any (Parent2 ~= Parent)) error ('?'), end

figure (1)
clf
subplot (2,2,1), spy (A),           title ('original matrix') ;
subplot (2,2,2), spy (A (P,P)),     title ('permuted matrix') ;
subplot (2,2,3), spy (L+D+L'),      title ('L+D+L''') ;
subplot (2,2,4), treeplot (Parent), title ('elimination tree') ;

[L, D, Parent] = ldl (A, P) ;
err = norm ((L+I)*D*(L+I)'-A(P,P), 1) ;
fprintf ('err: %g\n', err) ;
if (err > 1e-14) error ('?'), end ;

[L, D] = ldl (A, P) ;
err = norm ((L+I)*D*(L+I)'-A(P,P), 1) ;
fprintf ('err: %g\n', err) ;
if (err > 1e-14) error ('?'), end ;

L2 = ldl (A, P) ;
err = norm (L - L2, 1) ;
fprintf ('err: %g\n', err) ;
if (err > 1e-14) error ('?'), end ;

x = ldl (A, P, b) ;
err = norm (A*x-b, 1) ;
fprintf ('err: %g\n', err) ;
if (err > 1e-14) error ('?'), end ;

[x, fl] = ldl (A, P, b) ;
err = norm (A*x-b, 1) ;
fprintf ('err: %g fl: %g\n', err, fl) ;
if (err > 1e-14) error ('?'), end ;

fprintf ('\nldl: all tests passed\n') ;