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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
|
function F = factorize (A,strategy,burble)
%FACTORIZE an object-oriented method for solving linear systems
% and least-squares problems, and for representing operations with the
% inverse of a square matrix or the pseudo-inverse of a rectangular matrix.
%
% F = factorize(A) returns an object F that holds the factorization of A.
% x=F\b then solves a linear system or a least-squares problem. S=inverse(F)
% or S=inverse(A) returns a factorized representation of the inverse of A so
% that inverse(A)*b is mathematically equivalent to pinv(A)*b, but the former
% does not actually compute the inverse or pseudo-inverse of A.
%
% Example
%
% F = factorize(A) ; % LU, QR, or Cholesky factorization of A
% x = F\b ; % solve A*x=b; same as x=A\b
% S = inverse (F) ; % S represents the factorization of inv(A)
% x = S*b ; % same as x = A\b.
% E = A-B*inverse(D)*C % efficiently computes the Schur complement
% E = A-B*inv(D)*C % bad method for computing the Schur complement
% S = inverse(A) ; S(:,1) % compute just the first column of inv(A),
% % without computing inv(A)
%
% F = factorize (A, strategy, burble) ; % optional 2nd and 3rd inputs
%
% A string can be specified as a second input parameter to select the strategy
% used to factorize the matrix. The first two are meta-strategies:
%
% 'default' if rectangular
% use QR for sparse A or A' (whichever is tall and thin);
% use COD for full A
% else
% if symmetric
% if positive real diagonal: try CHOL
% else (or if CHOL fails): try LDL
% end
% if not yet factorized: try LU (fails if rank-deficient)
% end
% if all else fails, or if QR or LU report that the matrix
% is singular (or nearly so): use COD
% This strategy mimics backslash, except that backslash never
% uses COD. Backslash also exploits other solvers, such as
% specialized tridiagonal and banded solvers.
%
% 'symmetric' as 'default' above, but assumes A is symmetric without
% checking, which is faster if you already know A is symmetric.
% Uses tril(A) and assumes triu(A) is its transpose. Results
% will be incorrect if A is not symmetric. If A is rectangular,
% the 'default' strategy is used instead.
%
% 'unsymmetric' as 'default', but assumes A is unsymmetric.
%
% The next "strategies" just select a single method, listed in decreasing order
% of generality and increasing order of speed and memory efficiency. All of
% them except the SVD can exploit sparsity.
%
% 'svd' use SVD. Never fails ... unless it runs out of time or memory.
% Coerces a sparse matrix A to full.
%
% 'cod' use COD. Almost as accurate as SVD, and much faster. Based
% on dense or sparse QR with rank estimation. Handles
% rank-deficient matrices, as long as it correctly estimates
% the rank. If the rank is ill-defined, use the SVD instead.
% Sparse COD requires the SPQR package to be installed
% (see http://www.suitesparse.com).
%
% 'qr' use QR. Reports a warning if A is singular.
%
% 'lu' use LU. Fails if A is rectangular; warning if A singular.
%
% 'ldl' use LDL. Fails if A is rank-deficient or not symmetric, or if
% A is sparse and complex. Uses tril(A) and assumes triu(A)
% is the transpose of tril(A).
%
% 'chol' use CHOL. Fails if A is rank-deficient or not symmetric
% positive definite. If A is sparse, it uses tril(A) and
% assumes triu(A) is the transpose of tril(A). If A is dense,
% triu(A) is used instead.
%
% A third input, burble, can be provided to tell this function to print what
% methods it tries (if burble is nonzero).
%
% For a demo type "fdemo" in the Demo directory or see the Doc/ directory.
%
% See also inverse, slash, linsolve, spqr.
% Copyright 2011-2012, Timothy A. Davis, http://www.suitesparse.com
assert (ndims (A) == 2, 'Matrix must be 2D.') ;
if (nargin < 2 || isempty (strategy))
strategy = 'default' ;
end
if (nargin < 3)
burble = 0 ;
end
if (burble)
fprintf ('\nfactorize: strategy %s, A has size %d-by-%d, ', ...
strategy, size (A)) ;
if (issparse (A))
fprintf ('sparse with %d nonzeros.\n', nnz (A)) ;
else
fprintf ('full.\n') ;
end
end
switch strategy
case 'default'
[F, me] = backslash_mimic (A, burble, 0) ;
case 'symmetric'
[F, me] = backslash_mimic (A, burble, 1) ;
case 'unsymmetric'
[F, me] = backslash_mimic (A, burble, 2) ;
case 'svd'
[F, me] = factorize_svd (A, burble) ;
case 'cod'
[F, me] = factorize_cod (A, burble) ;
case 'qr'
[F, me] = factorize_qr (A, burble, 0) ;
case 'lu'
% do not report a failure if the matrix is singular
[F, me] = factorize_lu (A, burble, 0) ;
case 'ldl'
[F, me] = factorize_ldl (A, burble) ;
case 'chol'
[F, me] = factorize_chol (A, burble) ;
otherwise
error ('FACTORIZE:invalidStrategy', 'unrecognized strategy.') ;
end
if (~isobject (F))
throw (me) ;
end
%-------------------------------------------------------------------------------
function [F, me] = backslash_mimic (A, burble, strategy)
%BACKSLASH_MIMIC automatically select a method to factorize A.
F = [ ] ;
me = [ ] ;
[m, n] = size (A) ;
% If the following condition is true, then the QR, QRT, or LU factorizations
% will report a failure if A is singular (or nearly so). This allows COD
% or COD_SPARSE to then be used instead. COD_SPARSE relies on the SPQR
% mexFunction in SuiteSparse, which might not be installed. In this case,
% QR, QRT, and LU do not report failures for sparse matrices that are singular
% (or nearly so), since there is no COD_SPARSE to fall back on.
fail_if_singular = ~issparse (A) || (exist ('spqr') == 3) ; %#ok
try_cod = true ;
if (m ~= n)
if (issparse (A))
% Use QR for the sparse rectangular case (ignore 'strategy' argument).
[F, me] = factorize_qr (A, burble, fail_if_singular) ;
else
% Use COD for the full rectangular case (ignore 'strategy' argument).
% If this fails, there's no reason to retry the COD below. If A has
% full rank, then COD is the same as QR with column pivoting (with the
% same cost in terms of run time and memory). Backslash in MATLAB uses
% QR with column pivoting alone, so this is just as fast as x=A\b in
% the full-rank case, but gives a more reliable result in the rank-
% deficient case.
try_cod = false ;
[F, me] = factorize_cod (A, burble) ;
end
else
% square case: Cholesky, LDL, or LU factorization of A
switch strategy
case 0
is_symmetric = (nnz (A-A') == 0) ;
case 1
is_symmetric = true ;
case 2
is_symmetric = false ;
end
if (is_symmetric)
% A is symmetric (or assumed to be so)
d = diag (A) ;
if (all (d > 0) && nnz (imag (d)) == 0)
% try a Cholesky factorization
[F, me] = factorize_chol (A, burble) ;
end
if (~isobject (F) && (~issparse (A) || isreal (A)))
% try an LDL factorization.
% complex sparse LDL does not yet exist in MATLAB
[F, me] = factorize_ldl (A, burble) ;
end
end
if (~isobject (F))
% use LU if Cholesky and/or LDL failed, or were skipped.
[F, me] = factorize_lu (A, burble, fail_if_singular) ;
end
end
if (~isobject (F) && try_cod)
% everything else failed, matrix is rank-deficient. Use COD
[F, me] = factorize_cod (A, burble) ;
end
%-------------------------------------------------------------------------------
function [F, me] = factorize_qr (A, burble, fail_if_singular)
% QR fails if the matrix is rank-deficient.
F = [ ] ;
me = [ ] ;
try
[m, n] = size (A) ;
if (m >= n)
if (burble)
fprintf ('factorize: try QR of A ... ') ;
end
if (issparse (A))
F = factorization_qr_sparse (A, fail_if_singular) ;
else
F = factorization_qr_dense (A, fail_if_singular) ;
end
else
if (burble)
fprintf ('factorize: try QR of A'' ... ') ;
end
if (issparse (A))
F = factorization_qrt_sparse (A, fail_if_singular) ;
else
F = factorization_qrt_dense (A, fail_if_singular) ;
end
end
if (burble)
fprintf ('OK.\n') ;
end
catch me
if (burble)
fprintf ('failed.\nfactorize: %s\n', me.message) ;
end
end
%-------------------------------------------------------------------------------
function [F, me] = factorize_chol (A, burble)
% LDL fails if the matrix is rectangular, rank-deficient, or not positive
% definite. Only the lower triangular part of A is used.
F = [ ] ;
me = [ ] ;
try
if (burble)
fprintf ('factorize: try CHOL ... ') ;
end
if (issparse (A))
F = factorization_chol_sparse (A) ;
else
F = factorization_chol_dense (A) ;
end
if (burble)
fprintf ('OK.\n') ;
end
catch me
if (burble)
fprintf ('failed.\nfactorize: %s\n', me.message) ;
end
end
%-------------------------------------------------------------------------------
function [F, me] = factorize_ldl (A, burble)
% LDL fails if the matrix is rectangular or rank-deficient.
% As of MATLAB R2012a, ldl does not work for complex sparse matrices.
% Only the lower triangular part of A is used.
F = [ ] ;
me = [ ] ;
try
if (burble)
fprintf ('factorize: try LDL ... ') ;
end
if (issparse (A))
F = factorization_ldl_sparse (A) ;
else
F = factorization_ldl_dense (A) ;
end
if (burble)
fprintf ('OK.\n') ;
end
catch me
if (burble)
fprintf ('failed.\nfactorize: %s\n', me.message) ;
end
end
%-------------------------------------------------------------------------------
function [F, me] = factorize_lu (A, burble, fail_if_singular)
% LU fails if the matrix is rectangular or rank-deficient.
F = [ ] ;
me = [ ] ;
try
if (burble)
fprintf ('factorize: try LU ... ') ;
end
if (issparse (A))
F = factorization_lu_sparse (A, fail_if_singular) ;
else
F = factorization_lu_dense (A, fail_if_singular) ;
end
if (burble)
fprintf ('OK.\n') ;
end
catch me
if (burble)
fprintf ('failed.\nfactorize: %s\n', me.message) ;
end
end
%-------------------------------------------------------------------------------
function [F, me] = factorize_cod (A, burble)
% COD only fails when it runs out of memory.
F = [ ] ;
me = [ ] ;
try
if (burble)
fprintf ('factorize: try COD ... ') ;
end
if (issparse (A))
F = factorization_cod_sparse (A) ;
else
F = factorization_cod_dense (A) ;
end
if (burble)
fprintf ('OK.\n') ;
end
catch me
if (burble)
fprintf ('failed.\nfactorize: %s\n', me.message) ;
end
end
%-------------------------------------------------------------------------------
function [F, me] = factorize_svd (A, burble)
% SVD only fails when it runs out of memory.
F = [ ] ;
me = [ ] ;
try
if (burble)
fprintf ('factorize: try SVD ... ') ;
end
F = factorization_svd (A) ;
if (burble)
fprintf ('OK.\n') ;
end
catch me
if (burble)
fprintf ('failed.\nfactorize: %s\n', me.message) ;
end
end
|