~logan/ubuntu/trusty/suitesparse/4.2.1-3ubuntu1

« back to all changes in this revision

Viewing changes to LINFACTOR/lintest.m

  • Committer: Bazaar Package Importer
  • Author(s): Rafael Laboissiere, Rafael Laboissiere, Ondrej Certik
  • Date: 2008-02-21 14:46:50 UTC
  • mfrom: (1.1.2 upstream) (5.1.1 hardy)
  • Revision ID: james.westby@ubuntu.com-20080221144650-tgeppgj0t7s759i8
Tags: 3.1.0-3
[ Rafael Laboissiere ]
* Upload to unstable

[ Ondrej Certik ]
* XS-DM-Upload-Allowed: yes field added
* Ondrej Certik added to uploaders

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function lintest (A,b)
 
2
%LINTEST test A*x=b, using linfactor, x=A\b, and (ack!) the explicit inv(A).
 
3
% The results printed include the breakeven point, which is the number of
 
4
% systems Ax=b that must be solved with the same A but different b for the
 
5
% inv(A) method to be faster than linfactor.  Using inv(A) is always
 
6
% numerically dubious, and typically slower.  However because of MATLAB's
 
7
% interpretive overhead, linfactor can be slightly slower in its
 
8
% forward/backsolves.  A true linfactor mexFunction would probably be just as
 
9
% fast as inv(A)*b when A is full.  You should never ever use inv(A) to solve
 
10
% a linear system.
 
11
%
 
12
% Example:
 
13
%   load west0479
 
14
%   b = rand (size (west0479,1), 1) ;
 
15
%   lintest (west0479, b) ;
 
16
%
 
17
% See also linfactor, lintests, mldivide
 
18
 
 
19
% Copyright 2007, Timothy A. Davis
 
20
 
 
21
%-------------------------------------------------------------------------------
 
22
% warmup, to make sure functions are loaded, for accurate timings
 
23
%-------------------------------------------------------------------------------
 
24
 
 
25
F = linfactor (1) ;
 
26
x = linfactor (F, 1) ;                                                      %#ok
 
27
F = linfactor (sparse (1)) ;
 
28
x = linfactor (F, 1) ;                                                      %#ok
 
29
F = linfactor (sparse (-1)) ;
 
30
x = linfactor (F, 1) ;                                                      %#ok
 
31
S = inv (1) ;
 
32
x = S*1 ;                                                                   %#ok
 
33
S = inv (sparse (1)) ;
 
34
x = S*1 ;                                                                   %#ok
 
35
S = inv (sparse (-1)) ;
 
36
x = S*1 ;                                                                   %#ok
 
37
x = rand (2) \ rand (2,1) ;                                                 %#ok
 
38
x = sparse (rand (2)) \ rand (2,1) ;                                        %#ok
 
39
clear x F S
 
40
 
 
41
%-------------------------------------------------------------------------------
 
42
% linfactor
 
43
%-------------------------------------------------------------------------------
 
44
 
 
45
% do this several times for accurate timings
 
46
t1 = 0 ;
 
47
trials = 0 ;
 
48
while (t1 < 1)
 
49
    [F, t] = linfactor (A) ;                % factorize A
 
50
    t1 = t1 + t ;
 
51
    trials = trials + 1 ;
 
52
end
 
53
t1 = t1 / trials ;
 
54
 
 
55
% do this several times for accurate timings
 
56
t2 = 0 ;
 
57
trials = 0 ;
 
58
while (t2 < 1)
 
59
    [x, t] = linfactor (F, b) ;             % use the factors to solve Ax=b
 
60
    t2 = t2 + t ;
 
61
    trials = trials + 1 ;
 
62
end
 
63
t2 = t2 / trials ;
 
64
 
 
65
resid = norm (A*x-b,1) / (norm (A,1) * norm (x,1) + norm (b,1)) ;
 
66
 
 
67
fprintf ('%-16s factor time: %10.6f solve time: %10.6f resid: %8.2e\n', ...
 
68
    F.kind (1:(find(F.kind == ':', 1, 'first'))), t1, t2, resid) ;
 
69
 
 
70
%-------------------------------------------------------------------------------
 
71
% inv
 
72
%-------------------------------------------------------------------------------
 
73
 
 
74
% Try again using inv(A)*b.  This is a really horrible way to solve Ax=b.  I'm 
 
75
% doing it here precisely to show that it is typically slower, except when there
 
76
% are a huge number of right-hand-sides to solve and A is small.  inv(A) also
 
77
% tends to be less accurate, but random matrices do not trigger that problem.
 
78
% It fails hopelessly when A is large, sparse, and where max(diff(r)) is large
 
79
% where [p,q,r,s] = dmperm(A).  Never, ever use inv(A) to solve a linear system.
 
80
% Oh, did I tell you never to use inv(A) to solve Ax=b?
 
81
 
 
82
try
 
83
 
 
84
    % do this several times for accurate timings
 
85
    t3 = 0 ;
 
86
    trials = 0 ;
 
87
    while (t3 < 1)
 
88
        tic ;
 
89
        S = inv (A) ;   %#ok
 
90
        t = toc ;
 
91
        t3 = t3 + t ;
 
92
        trials = trials + 1 ;
 
93
    end
 
94
    t3 = t3 / trials ;
 
95
 
 
96
    % do this several times for accurate timings
 
97
    t4 = 0 ;
 
98
    trials = 0 ;
 
99
    while (t4 < 1)
 
100
        tic ;
 
101
        x = S*b ;
 
102
        t = toc ;
 
103
        t4 = t4 + t ;
 
104
        trials = trials + 1 ;
 
105
    end
 
106
    t4 = t4 / trials ;
 
107
 
 
108
    resid = norm (A*x-b,1) / (norm (A,1) * norm (x,1) + norm (b,1)) ;
 
109
 
 
110
    fprintf ('%-16s factor time: %10.6f solve time: %10.6f resid: %8.2e\n', ...
 
111
        'inv(A)', t3, t4, resid) ;
 
112
 
 
113
    % determine the breakeven point where using inv(A) is faster
 
114
    nrhs = max (1, ceil ((t3 - t1) / (t2 - t4))) ;
 
115
    if (t1 < t3 & t2 < t4)                                      %#ok
 
116
        fprintf ('inv(A) breakeven: never\n') ;
 
117
    elseif (t3 < t1 & t4 < t2)                                 %#ok
 
118
        fprintf ('inv(A) breakeven: > 0\n') ;
 
119
    elseif (t1 < t3 & t4 < t2)                                 %#ok
 
120
        fprintf ('inv(A) breakeven: > %d\n', nrhs) ;
 
121
    else
 
122
        fprintf ('inv(A) breakeven: < %d\n', nrhs) ;
 
123
    end
 
124
 
 
125
catch
 
126
 
 
127
    % inv(A) probably ran out of memory
 
128
    fprintf ('inv(A) failed\n') ;
 
129
    fprintf ('inv(A) breakeven: never\n') ;
 
130
end
 
131
 
 
132
%-------------------------------------------------------------------------------
 
133
% backslash
 
134
%-------------------------------------------------------------------------------
 
135
 
 
136
% do this several times for accurate timings
 
137
t0 = 0 ;
 
138
trials = 0 ;
 
139
while (t0 < 1)
 
140
    tic ;
 
141
    x = A\b ;                               % solve Ax=b
 
142
    t = toc ;
 
143
    t0 = t0 + t ;
 
144
    trials = trials + 1 ;
 
145
end
 
146
t0 = t0 / trials ;
 
147
 
 
148
resid = norm (A*x-b,1) / (norm (A,1) * norm (x,1) + norm (b,1)) ;
 
149
 
 
150
fprintf ('%-16s  total time: %10.6f                        resid: %8.2e\n', ...
 
151
    'x = A\b', t0, resid) ;
 
152
fprintf ('\n') ;
 
153