]> Creatis software - CreaPhase.git/blob - octave_packages/linear-algebra-2.2.0/@kronprod/mldivide.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / linear-algebra-2.2.0 / @kronprod / mldivide.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} mldivide (@var{M1}, @var{M2})
18 ## XXX: Write documentation
19 ## @end deftypefn
20
21 function retval = mldivide (M1, M2)
22   ## Check input
23   if (nargin != 2)
24     print_usage ();
25   endif
26   
27   if (!ismatrix (M1) || !ismatrix (M2))
28     error ("mldivide: both input arguments must be matrices");
29   endif
30   
31   if (rows (M1) != rows (M2))
32     error ("mldivide: nonconformant arguments (op1 is %dx%d, op2 is %dx%d)",
33            rows (M1), columns (M1), rows (M2), columns (M2));
34   endif
35
36   ## Take action depending on types
37   M1_is_KP = isa (M1, "kronprod");
38   M2_is_KP = isa (M2, "kronprod");
39   
40   if (M1_is_KP && M2_is_KP) # Left division of Kronecker Products
41     error ("mldividide: this part not yet implemented as I'm lazy...");
42
43   elseif (M1_is_KP) # Left division of Kronecker Product and Matrix
44     ## XXX: Does this give the same minimum-norm solution as when using
45     ## XXX:   full (M1) \ M2
46     ## XXX: ? It is the same when M1 is invertible.
47     retval = zeros (columns (M1), columns (M2));
48     for n = 1:columns (M2)
49       M = reshape (M2 (:, n), [rows(M1.B), rows(M1.A)]);
50       retval (:, n) = vec ((M1.A \ (M1.B \ M)')');
51     endfor
52
53   elseif (M2_is_KP) # Left division of Matrix and Kronecker Product
54     error ("mldividide: this part not yet implemented as I'm lazy...");
55       
56   else
57     error ("mldivide: internal error for 'kronprod'");
58   endif
59 endfunction