]> Creatis software - CreaPhase.git/blob - octave_packages/linear-algebra-2.2.0/@kronprod/times.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / linear-algebra-2.2.0 / @kronprod / times.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} times (@var{KP})
18 ## XXX: Write documentation
19 ## @end deftypefn
20
21 function retval = times (M1, M2)
22   ## Check input
23   if (nargin == 0)
24     print_usage ();
25   elseif (nargin == 1)
26     ## This seems to be what happens for full and sparse matrices, so we copy this behaviour
27     retval = M1;
28     return;
29   endif
30   
31   if (!ismatrix (M1) || !ismatrix (M2))
32     error ("times: input arguments must be matrices");
33   endif
34   
35   if (!size_equal (M1, M2))
36     error ("times: nonconformant arguments (op1 is %dx%d, op2 is %dx%d)",
37            rows (M1), columns (M1), rows (M2), columns (M2));
38   endif
39
40   ## Take action depending on input types
41   M1_is_KP = isa (M1, "kronprod");
42   M2_is_KP = isa (M2, "kronprod");
43   
44   ## Product of Kronecker Products
45   ## Check if the size match such that the result is a Kronecker Product
46   if (M1_is_KP && M2_is_KP && size_equal (M1.A, M2.A) && size_equal (M1.B, M2.B))
47     retval = kronprod (M1.A .* M2.A, M1.B .* M2.B);
48   elseif (isscalar (M1) || isscalar (M2)) # Product of Kronecker Product and scalar
49     retval = M1 * M2; ## Forward to mtimes.
50   else # All other cases.
51     ## Form the full matrix or sparse matrix of both matrices
52     ## XXX: Can we do something smarter here?
53     if (issparse (M1))
54       M1 = sparse (M1);
55     else
56       M1 = full (M1);
57     endif
58     
59     if (issparse (M2))
60       M2 = sparse (M2);
61     else
62       M2 = full (M2);
63     endif
64     
65     retval = M1 .* M2;
66   endif
67 endfunction