1 ## Copyright (C) 2010 Soren Hauberg
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)
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.
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/>.
17 ## @deftypefn {Function File} inv (@var{KP})
18 ## Return the inverse of the Kronecker product @var{KP}.
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}.
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}
29 function retval = inv (KP)
35 if (!isa (KP, "kronprod"))
36 error ("inv: input argument must be of class 'kronprod'");
39 ## Do the computations
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?
48 retval = U * (1./S) * V';
50 error ("inv: argument must be a square matrix");