]> Creatis software - CreaPhase.git/blob - octave_packages/m/general/blkdiag.m
update packages
[CreaPhase.git] / octave_packages / m / general / blkdiag.m
1 ## Copyright (C) 2000-2012 Daniel Calvelo
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} blkdiag (@var{A}, @var{B}, @var{C}, @dots{})
21 ## Build a block diagonal matrix from @var{A}, @var{B}, @var{C}, @dots{}
22 ## All the arguments must be numeric and are two-dimensional matrices or
23 ## scalars.  If any argument is of type sparse, the output will also be
24 ## sparse.
25 ## @seealso{diag, horzcat, vertcat, sparse}
26 ## @end deftypefn
27
28 ## Author: Daniel Calvelo
29 ## Modified by: William Poetra Yoga Hadisoeseno
30
31 function retval = blkdiag (varargin)
32
33   if (nargin < 1)
34     print_usage ();
35   endif
36
37   if (! all (cellfun ("isnumeric", varargin)))
38     error ("blkdiag: all arguments must be numeric");
39   endif
40
41   ## Note: trailing singletons are automatically (correctly) ignored.
42   if (! all (cellfun ("ndims", varargin) == 2))
43     error ("blkdiag: all arguments must be two-dimensional matrices");
44   endif
45
46   ## size is an option for cellfun, but it's a bit different from
47   ## calling size directly.
48   tmp = cell2mat (cellfun (@size, varargin', "uniformoutput", false));
49   csz = cumsum ([0 0; tmp], 1);
50
51   if (any (cellfun ("issparse", varargin)))
52     retval = sparse (csz(end,1), csz(end,2));
53   else
54     retval = zeros (csz(end,:));
55   endif
56
57   for p = 1:nargin
58     vp = varargin{p};
59     if (! isempty (vp))
60       retval((csz(p,1)+1):csz(p+1,1),(csz(p,2)+1):csz(p+1,2)) = vp;
61     endif
62   endfor
63
64 endfunction
65
66 ## regular tests
67 %!assert(blkdiag(1,ones(2),1),[1,0,0,0;0,1,1,0;0,1,1,0;0,0,0,1])
68 %!assert(blkdiag([1,2],[3,4],[5,6]),[1,2,0,0,0,0;0,0,3,4,0,0;0,0,0,0,5,6])
69 %!assert(blkdiag([1,2],[3;4],[5,6]),[1,2,0,0,0;0,0,3,0,0;0,0,4,0,0;0,0,0,5,6])
70 %!assert(blkdiag([1,2;3,4],[5,6,7]),[1,2,0,0,0;3,4,0,0,0;0,0,5,6,7])
71 ## tests involving empty matrices
72 %!assert(blkdiag([],[],[]),[])
73 %!assert(blkdiag([],[1,2;3,4],[],5,[]),[1,2,0;3,4,0;0,0,5])
74 %!assert(blkdiag(zeros(1,0,1),[1,2,3],1,0,5,zeros(0,1,1)),[0,0,0,0,0,0,0;1,2,3,0,0,0,0;0,0,0,1,0,0,0;0,0,0,0,0,0,0;0,0,0,0,0,5,0]);
75 ## tests involving sparse matrices
76 %!assert (blkdiag (sparse([1,2;3,4]),[5,6;7,8]), sparse([1,2,0,0;3,4,0,0;0,0,5,6;0,0,7,8]))
77 %!assert (blkdiag (sparse([1,2;3,4]),[5,6]), sparse([1,2,0,0;3,4,0,0;0,0,5,6]))
78 # sanity checks
79 %!test
80 %! A = rand (round (rand (1, 2) * 10));
81 %! assert (blkdiag (A), A);