1 ## Copyright (C) 2010 VZLU Prague
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 of the License, or
6 ## (at your option) any later version.
8 ## This program is distributed in the hope that it will be useful,
9 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ## GNU General Public License for more details.
13 ## You should have received a copy of the GNU General Public License
14 ## along with Octave; see the file COPYING. If not, see
15 ## <http://www.gnu.org/licenses/>.
18 ## @deftypefn {Function File} mtimes (@var{x}, @var{y})
19 ## Multiplies a block sparse matrix with a full matrix, or two block sparse
20 ## matrices. Multiplication of block sparse * sparse is not implemented.
21 ## If one of arguments is a scalar, it's a scalar multiply.
24 function c = mtimes (a, b)
25 if (isa (a, "blksparse"))
26 if (isa (b, "blksparse"))
31 elseif (isa (b, "blksparse"))
34 error ("blksparse: invalid arguments to mtimes");
38 function y = mtimes_sm (s, x)
44 error ("blksparse * sparse not implemented.");
50 if (xr != siz(2)*bsiz(2))
51 gripe_nonconformant (siz.*bsiz, [xr, xc]);
54 x = reshape (x, bsiz(2), siz(2), xc);
55 x = permute (x, [1, 3, 2]);
59 ys = blkmm (s.sv, xs);
61 y = accumdim (s.i, ys, 3, siz(1));
62 y = permute (y, [1, 3, 2]);
64 y = reshape (y, bsiz(1)*siz(1), xc);
67 function y = mtimes_ms (x, s)
73 error ("sparse * blksparse not implemented.");
79 if (xc != siz(1)*bsiz(1))
80 gripe_nonconformant ([xr, xc], siz.*bsiz);
83 x = reshape (x, xr, bsiz(2), siz(2));
87 ys = blkmm (xs, s.sv);
89 y = accumdim (s.j, ys, 3, siz(2));
91 y = reshape (y, xr, bsiz(2)*siz(2));
94 function s = mtimes_ss (s1, s2)
101 if (bsiz1(2) != bsiz2(1))
102 gripe_nonconformant (bsiz1, bsiz2, "block sizes");
103 elseif (siz1(2) != siz2(1))
104 gripe_nonconformant (bsiz1.*siz1, bsiz2.*siz2);
107 ## Hardcore hacks, man!
108 ss = sparse (s1.i, s1.j, 1:length (s1.i), "unique");
110 [i, j, k] = find (ss);
111 sv = blkmm (s1.sv(:,:,k), s2.sv(:,:,j));
114 s = blksparse (i, j, sv, siz1(1), siz2(2));
118 function gripe_nonconformant (s1, s2, what = "arguments")
119 error ("Octave:nonconformant-args", ...
120 "nonconformant %s (op1 is %dx%d, op2 is %dx%d)", what, s1, s2);