File: sparseinv_test.m

package info (click to toggle)
suitesparse 1%3A5.12.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 176,720 kB
  • sloc: ansic: 1,193,914; cpp: 31,704; makefile: 6,638; fortran: 1,927; java: 1,826; csh: 765; ruby: 725; sh: 529; python: 333; perl: 225; sed: 164; awk: 35
file content (149 lines) | stat: -rw-r--r-- 4,023 bytes parent folder | download | duplicates (3)
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
function sparseinv_test (extensive)
%SPARSEINV_TEST tests the sparseinv function.
%
% Example
%   sparseinv_test ;        % basic test
%   sparseinv_test (1) ;    % extensive test (requires ssget)
%
% See also sparseinv, sparseinv_install, ssget.

% Copyright 2011, Timothy A. Davis, http://www.suitesparse.com

if (nargin < 1)
    extensive = 0 ;
end

load west0479 ;
A = west0479 ;

for k = 1:2

    [Z, Zpattern, L, D, U, P, Q, stats] = sparseinv (A) ;

    n = size (A,1) ;
    I = speye (n) ;

    S = inv (A) ;
    Snorm = norm (S, 1) ;
    e1 = norm (Zpattern.*(Z-S), 1) / Snorm ;
    e2 = norm ((L+I)*D*(U+I) - P*A*Q, 1) / norm (A,1) ;
    c = condest (A) ;

    fprintf ('west0479: errors %g %g condest %g : ', e1, e2, c) ;
    if (e1 > 1e-10 || e2 > 1e-10)
        error ('west0479 test failed') ;
    end
    fprintf ('ok\n') ;
    disp (stats) ;

    % create a symmetric positive definite matrix to test with
    if (k == 1)
        A = A+A' + 1e6*I ;
    end
end

% check error-handling
fprintf ('testing error handling (errors below are expected)\n') ;
ok = 1 ;
A = ones (2) ;
try
    Z1 = sparseinv (A) ;                                                    %#ok
    ok = 0 ;
catch me
    fprintf ('    expected error: %s\n', me.message) ;
end
A = sparse (ones (3,2)) ;
try
    Z2 = sparseinv (A) ;                                                    %#ok
    ok = 0 ;
catch me
    fprintf ('    expected error: %s\n', me.message) ;
end
A = sparse (ones (3)) ;
try
    Z3 = sparseinv (A) ;                                                    %#ok
    ok = 0 ;
catch me
    fprintf ('    expected error: %s\n', me.message) ;
end
A = 1i * sparse (ones (3)) ;
try
    Z4 = sparseinv (A) ;                                                    %#ok
    ok = 0 ;
catch me
    fprintf ('    expected error: %s\n', me.message) ;
end

if (~ok)
    error ('error-handling tests failed') ;
end

% now try with lots of matrices from the SuiteSparse Matrix Collection
if (extensive && exist ('ssget', 'file') == 2)

    fprintf ('Now doing extensive tests with SuiteSparse Matrix Collection:\n') ;
    dofigures = (exist ('cspy', 'file') == 2) ;
    if (dofigures)
        clf ;
    end

    index = ssget ;
    f = find ((index.nrows == index.ncols) & (index.isReal == 1)) ;
    [ignore,i] = sort (index.nrows (f)) ;   %#ok
    f = f (i) ;
    nmat = length (f) ;

    s = warning ('off', 'MATLAB:nearlySingularMatrix') ;

    for k = 1:nmat
        id = f (k) ;
        Prob = ssget (id, index) ;
        A = Prob.A ;
        n = size (A,1) ;
        I = speye (n) ;
        fprintf ('id: %4d  n: %4d : %-30s', id, n, Prob.name) ;

        Z = [ ] ; 
        try
            [Z, Zpattern, L, D, U, P, Q] = sparseinv (A) ;
        catch me
            fprintf ('%s', me.message) ;
        end

        if (~isempty (Z))
            e = norm ((L+I) * D * (U+I) - P*A*Q, 1) / norm (A,1) ;
            fprintf ('errs:  %12.3e ', e) ;
            if (e > 1e-10)
                error ('error in factorization too high') ;
            end
            S = inv (A) ;           % normally S has MANY nonzero entries
            Snorm = norm (S,1) ;
            E = abs (Zpattern .* (Z-S)) / Snorm ;
            e = norm (E, 1) ;
            c = condest (A) ;
            fprintf (' %12.3e  condest: %12.2e', e, c) ;
            if (e/c  > 1e-8)
                error ('error in sparseinv too high') ;
            end
            fprintf (' ok') ;

            if (dofigures)
                subplot (2,2,1) ; cspy (A) ;
                title (Prob.name, 'Interpreter', 'none') ;
                subplot (2,2,2) ; cspy (P*A*Q) ;  title ('P*A*Q') ;
                subplot (2,2,3) ; cspy (Z) ;      title ('sparse inverse') ;
                subplot (2,2,4) ; cspy (S) ;      title ('inverse') ;
                drawnow
            end

        end
        fprintf ('\n') ;
        if (n >= 300)
            break ;
        end
    end

    warning (s) ;       % restore warning status
end

fprintf ('All sparseinv tests passed.\n') ;