]> Creatis software - CreaPhase.git/blob - octave_packages/linear-algebra-2.2.0/@kronprod/inv.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / linear-algebra-2.2.0 / @kronprod / inv.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} inv (@var{KP})
18 ## Return the inverse of the Kronecker product @var{KP}.
19 ##
20 ## If @var{KP} is the Kronecker product of two square matrices @var{A} and @var{B},
21 ## the inverse will be computed as the Kronecker product of the inverse of
22 ## @var{A} and @var{B}.
23 ##
24 ## If @var{KP} is square but not a Kronecker product of square matrices, the
25 ## inverse will be computed using the SVD
26 ## @seealso{@@kronprod/sparse}
27 ## @end deftypefn
28
29 function retval = inv (KP)
30   ## Check input
31   if (nargin != 1)
32     print_usage ();
33   endif
34   
35   if (!isa (KP, "kronprod"))
36     error ("inv: input argument must be of class 'kronprod'");
37   endif
38   
39   ## Do the computations
40   [n, m] = size (KP.A);
41   [q, r] = size (KP.B);
42   if (n == m && q == r) # A and B are both square
43     retval = kronprod (inv (KP.A), inv (KP.B));
44   elseif (n*q == m*r) # kron (A, B) is square
45     ## We use the SVD to compute the inverse.
46     ## XXX: Should we use 'eig' instead?
47     [U, S, V] = svd (KP);
48     retval = U * (1./S) * V';
49   else
50     error ("inv: argument must be a square matrix");
51   endif
52 endfunction