1 ## Copyright (C) 2009 VZLU Prague, a.s., Czech Republic
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn{Function File} {[@var{q}, @var{r}, @var{z}] =} cod (@var{a})
18 ## @deftypefnx{Function File} {[@var{q}, @var{r}, @var{z}, @var{p}] =} cod (@var{a})
19 ## @deftypefnx{Function File} {[@dots{}] =} cod (@var{a}, '0')
20 ## Computes the complete orthogonal decomposition (COD) of the matrix @var{a}:
22 ## @var{a} = @var{q}*@var{r}*@var{z}'
24 ## Let @var{a} be an M-by-N matrix, and let @code{K = min(M, N)}.
25 ## Then @var{q} is M-by-M orthogonal, @var{z} is N-by-N orthogonal,
26 ## and @var{r} is M-by-N such that @code{@var{r}(:,1:K)} is upper
27 ## trapezoidal and @code{@var{r}(:,K+1:N)} is zero.
28 ## The additional @var{p} output argument specifies that pivoting should be used in
29 ## the first step (QR decomposition). In this case,
31 ## @var{a}*@var{p} = @var{q}*@var{r}*@var{z}'
33 ## If a second argument of '0' is given, an economy-sized factorization is returned
34 ## so that @var{r} is K-by-K.
36 ## @emph{NOTE}: This is currently implemented by double QR factorization plus some
37 ## tricky manipulations, and is not as efficient as using xRZTZF from LAPACK.
41 ## Author: Jaroslav Hajek <highegg@gmail.com>
43 function [q, r, z, p] = cod (a, varargin)
45 if (nargin < 1 || nargin > 2 || nargout > 4 || ! ismatrix (a))
51 economy = nargin == 2;
52 pivoted = nargout == 4;
54 ## Compute the initial QR decomposition
56 [q, r, p] = qr (a, varargin{:});
58 [q, r] = qr (a, varargin{:});
62 ## In this case, Z is identity, and we're finished.
63 z = eye (n, class (a));
65 ## Permutation inverts row order.
66 pr = eye (m) (m:-1:1, :);
67 ## Permutation inverts first m columns order.
68 pc = eye (n) ([m:-1:1, m+1:n], :);
69 ## Make n-by-m matrix, invert first m columns
71 ## QR factorize again.
72 [z, r] = qr (r, varargin{:});
73 ## Recover final R and Z
87 %! [q, r, z] = cod (a);
88 %! assert (norm (q*r*z' - a) / norm (a) < 1e-10);
90 %! a = rand (5, 10) + i * rand (5, 10);
91 %! [q, r, z] = cod (a);
92 %! assert (norm (q*r*z' - a) / norm (a) < 1e-10);
95 %! [q, r, z, p] = cod (a);
96 %! assert (norm (q*r*z' - a*p) / norm (a) < 1e-10);