]> Creatis software - CreaPhase.git/blob - octave_packages/linear-algebra-2.2.0/@kronprod/not_done/svd.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / linear-algebra-2.2.0 / @kronprod / not_done / svd.m
1 ## Copyright (C) 2010  Soren Hauberg
2 ## 
3 ## This program is free software; you can redistribute it and/or modify
4 ## it under the terms of the GNU General Public License as published by
5 ## the Free Software Foundation; either version 3, or (at your option)
6 ## any later version.
7 ## 
8 ## This program is distributed in the hope that it will be useful, but
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 ## General Public License for more details. 
12 ## 
13 ## You should have received a copy of the GNU General Public License
14 ## along with this file.  If not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} svd (@var{KP})
18 ## XXX: Write documentation
19 ## @end deftypefn
20
21 function [U, S, V] = svd (KP)
22   if (nargin < 1)
23     print_usage ();
24   endif
25   
26   if (!isa (KP, "kronprod"))
27     error ("svd: input must be of class 'kronprod'");
28   endif
29   
30   ## XXX: I don't think this works properly for non-square A and B
31   
32   if (nargout <= 1)
33     ## Only singular values were requested
34     S_A = svd (KP.A);
35     S_B = svd (KP.B);
36     U = sort (kron (S_A, S_B), "descend");
37   elseif (nargout == 3)
38     ## The full SVD was requested
39     [U_A, S_A, V_A] = svd (KP.A);
40     [U_B, S_B, V_B] = svd (KP.B);
41
42     ## Compute and sort singular values
43     [sv, idx] = sort (kron (diag (S_A), diag (S_B)), "descend");
44     
45     ## Form matrices
46     S = resize (diag (sv), [rows(KP), columns(KP)]);
47     #Pu = eye (rows (KP)) (idx, :);
48     U = kronprod (U_A, U_B, idx);
49     #Pv = eye (columns (KP)) (idx, :);
50     V = kronprod (V_A, V_B, idx);
51   else
52     print_usage ();
53   endif
54 endfunction