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} times (@var{KP})
18 ## XXX: Write documentation
21 function retval = times (M1, M2)
26 ## This seems to be what happens for full and sparse matrices, so we copy this behaviour
31 if (!ismatrix (M1) || !ismatrix (M2))
32 error ("times: input arguments must be matrices");
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));
40 ## Take action depending on input types
41 M1_is_KP = isa (M1, "kronprod");
42 M2_is_KP = isa (M2, "kronprod");
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?