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} {@var{s} =} blksparse (@var{i}, @var{j}, @var{sv})
19 ## @deftypefnx{Function File} {@var{s} =} blksparse (@var{i}, @var{j}, @var{sv}, @var{m}, @var{n})
20 ## @deftypefnx{Function File} {@var{s} =} blksparse (@dots{}, @var{mode})
22 ## Construct a block sparse matrix. The meaning of arguments is analogous to the
23 ## built-in @code{sparse} function, except that @var{i}, @var{j} are indices of
24 ## blocks rather than elements, and @var{sv} is a 3-dimensional array, the first two
25 ## dimensions determining the block size. Optionally, @var{m} and @var{n} can be
26 ## specified as the true block dimensions; if not, the maximum values of @var{i}, @var{j}
27 ## are taken instead. The resulting sparse matrix has the size
30 ## [@var{m}*@var{p}, @var{n}*@var{q}]
36 ## @var{p} = size (@var{sv}, 1)
37 ## @var{q} = size (@var{sv}, 2)
40 ## The blocks are located so that
43 ## @var{s}(@var{i}(k):@var{i}(k)+@var{p}-1, @var{j}(k):@var{j}(K)+@var{q}-1) = @var{sv}(:,:,k)
46 ## Multiple blocks corresponding to the same pair of indices are summed, unless
47 ## @var{mode} is "unique", in which case the last of them is used.
50 function s = blksparse (i, j, sv, m = 0, n = 0, mode)
51 persistent chkver = check_version ();
55 s = class (struct ("i", i, "j", j, "sv", sv, "siz", [0, 0], "bsiz", [1, 1]), "blksparse");
59 if (nargin < 3 || nargin > 6)
63 if (! isvector (i) || ! isvector (j))
64 error ("blksparse: i, j must be vectors");
65 elseif (ndims (sv) != 3)
66 error ("blksparse: sv must be a 3D array");
69 if (nargin == 4 && ischar (m))
76 if (strcmp (mode, "unique"))
78 elseif (strcmp (mode, "sum") || strcmp (mode, "summation"))
81 error ("blksparse: invalid mode: %s", mode);
94 [ji, fidx, ridx] = unique (ji, "rows");
99 sv = accumdim (ridx, sv, 3, rows (ji));
104 s = struct ("i", i, "j", j, "sv", sv, "siz", siz, "bsiz", size (sv)(1:2));
105 s = class (s, "blksparse");
109 function ok = check_version ()
110 ok = compare_versions (version, "3.3.51", ">=");
112 error ("blksparse: can only be used with Octave 3.3.51+");