X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?p=CreaPhase.git;a=blobdiff_plain;f=octave_packages%2Flinear-algebra-2.2.0%2F%40kronprod%2Fmtimes.m;fp=octave_packages%2Flinear-algebra-2.2.0%2F%40kronprod%2Fmtimes.m;h=b539cc17dc210fd29c9a377c709f59314279f4b4;hp=0000000000000000000000000000000000000000;hb=c880e8788dfc484bf23ce13fa2787f2c6bca4863;hpb=1705066eceaaea976f010f669ce8e972f3734b05 diff --git a/octave_packages/linear-algebra-2.2.0/@kronprod/mtimes.m b/octave_packages/linear-algebra-2.2.0/@kronprod/mtimes.m new file mode 100644 index 0000000..b539cc1 --- /dev/null +++ b/octave_packages/linear-algebra-2.2.0/@kronprod/mtimes.m @@ -0,0 +1,92 @@ +## Copyright (C) 2010 Soren Hauberg +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3, or (at your option) +## any later version. +## +## This program is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this file. If not, see . + +## -*- texinfo -*- +## @deftypefn {Function File} mtimes (@var{KP}) +## XXX: Write documentation +## @end deftypefn + +function retval = mtimes (M1, M2) + ## Check input + if (nargin == 0) + print_usage (); + elseif (nargin == 1) + ## This seems to be what happens for full and sparse matrices, so we copy this behaviour + retval = M1; + return; + endif + + if (!ismatrix (M1) || !ismatrix (M2)) + error ("mtimes: input arguments must be matrices"); + endif + + if (columns (M1) != rows (M2)) + error ("mtimes: nonconformant arguments (op1 is %dx%d, op2 is %dx%d)", + rows (M1), columns (M1), rows (M2), columns (M2)); + endif + + ## Take action depending on input types + M1_is_KP = isa (M1, "kronprod"); + M2_is_KP = isa (M2, "kronprod"); + + if (M1_is_KP && M2_is_KP) # Product of Kronecker Products + ## Check if the size match such that the result is a Kronecker Product + if (columns (M1.A) == rows (M2.A) && columns (M1.B) == rows (M2.B)) + retval = kronprod (M1.A * M2.A, M1.B * M2.B); + else + ## Form the full matrix of the smallest matrix and use that to compute the + ## final product + ## XXX: Can we do something smarter here? + numel1 = numel (M1); + numel2 = numel (M2); + if (numel1 < numel2) + retval = full (M1) * M2; + else + retval = M1 * full (M2); + endif + endif + + elseif (M1_is_KP && isscalar (M2)) # Product of Kronecker Product and scalar + if (numel (M1.A) < numel (M1.B)) + retval = kronprod (M2 * M1.A, M1.B); + else + retval = kronprod (M1.A, M2 * M1.B); + endif + + elseif (M1_is_KP && ismatrix (M2)) # Product of Kronecker Product and Matrix + retval = zeros (rows (M1), columns (M2)); + for n = 1:columns (M2) + M = reshape (M2 (:, n), [columns(M1.B), columns(M1.A)]); + retval (:, n) = vec (M1.B * M * M1.A'); + endfor + + elseif (isscalar (M1) && M2_is_KP) # Product of scalar and Kronecker Product + if (numel (M2.A) < numel (M2.B)) + retval = kronprod (M1 * M2.A, M2.B); + else + retval = kronprod (M2.A, M1 * M2.B); + endif + + elseif (ismatrix (M1) && M2_is_KP) # Product of Matrix and Kronecker Product + retval = zeros (rows (M1), columns (M2)); + for n = 1:rows (M1) + M = reshape (M1 (n, :), [rows(M2.B), rows(M2.A)]); + retval (n, :) = vec (M2.B' * M * M2.A); + endfor + + else + error ("mtimes: internal error for 'kronprod'"); + endif +endfunction