2
%LDLDEMO demo program for LDL
9
% Copyright 2006-2007 by Timothy A. Davis, Univ. of Florida
11
% compile the LDLSPARSE and LDLSYMBOL mexFunctions
14
fprintf ('\nTesting ldlsparse and ldlsymbol:\n') ;
16
% create a small random symmetric positive definite sparse matrix
21
A = sprandn (n, n, d) ;
22
A = speye (n) + A*A' ;
29
title ('original matrix') ;
31
% permute for sparsity
37
title ('permuted matrix') ;
40
% factorize, without using ldlsparse's internal permutation
41
[L, D, Parent, fl] = ldlsparse (C) ;
43
err = norm (L*D*L' - C, 1) ;
44
fprintf ('norm (LDL''-PAP'') = %g\n', err) ;
47
x = L' \ (D \ (L \ (b (p)))) ;
49
resid = norm (A*x-b) ;
50
fprintf ('residual %g for ldlsparse, flops %10.1f\n', resid, fl) ;
52
% solve Ax=b with one call to ldlsparse
53
x = ldlsparse (C, [ ], b (p)) ;
55
resid = norm (A*x-b) ;
56
fprintf ('residual %g for ldlsparse solve\n', resid) ;
64
title ('elimination tree') ;
66
% try ldlrow (this will be slow)
68
x = L' \ (D \ (L \ (b (p)))) ;
70
resid = norm (A*x-b) ;
71
fprintf ('residual %g for ldlrow.m\n', resid) ;
73
% factorize, using ldlsparse's internal permutation
74
[L, D, Parent, fl] = ldlsparse (A, p) ;
76
err = norm (L*D*L' - C, 1) ;
77
fprintf ('norm (LDL''-PAP'') = %g\n', err) ;
80
x = L' \ (D \ (L \ (b (p)))) ;
82
resid = norm (A*x-b) ;
83
fprintf ('residual %g for ldlsparse, flops %10.1f\n', resid, fl) ;
85
% solve Ax=b with one call to ldlsparse
86
x = ldlsparse (A, p, b) ;
87
resid = norm (A*x-b) ;
88
fprintf ('residual %g for ldlsparse solve\n\n', resid) ;
90
% compare ldlsymbol and symbfact
91
[Lnz, Parent, fl] = ldlsymbol (A) ;
92
fprintf ('Original matrix: nz in L: %5d flop count: %g\n', sum (Lnz), fl) ;
94
Lnz2 = symbfact (A) - 1 ;
96
fl2 = sum (Lnz2 .* (Lnz2 + 2)) ;
97
if (any (Lnz ~= Lnz2))
98
error ('Lnz mismatch') ;
100
if (any (Parent ~= Parent2))
101
error ('Parent mismatch') ;
104
error ('fl mismatch') ;
107
[Lnz, Parent, fl] = ldlsymbol (A, p) ;
108
fprintf ('Permuted matrix: nz in L: %5d flop count: %g\n', sum (Lnz), fl) ;
110
Lnz2 = symbfact (A (p,p)) - 1 ;
111
Parent2 = etree (A (p,p)) ;
112
fl2 = sum (Lnz2 .* (Lnz2 + 2)) ;
113
if (any (Lnz ~= Lnz2))
114
error ('Lnz mismatch') ;
116
if (any (Parent ~= Parent2))
117
error ('Parent mismatch') ;
120
error ('fl mismatch') ;
124
fprintf ('\nldldemo: all tests passed\n') ;