Mercurial > hg > octave-nkf
annotate scripts/general/blkdiag.m @ 20830:b65888ec820e draft default tip gccjit
dmalcom gcc jit import
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Fri, 27 Feb 2015 16:59:36 +0100 |
parents | 7503499a252b |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 2000-2015 Daniel Calvelo |
5551 | 2 ## |
7016 | 3 ## This file is part of Octave. |
5551 | 4 ## |
7016 | 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. | |
5551 | 14 ## |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5551 | 18 |
19 ## -*- texinfo -*- | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
11191
diff
changeset
|
20 ## @deftypefn {Function File} {} blkdiag (@var{A}, @var{B}, @var{C}, @dots{}) |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
21 ## Build a block diagonal matrix from @var{A}, @var{B}, @var{C}, @dots{} |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
22 ## |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
23 ## All arguments must be numeric and either two-dimensional matrices or |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
24 ## scalars. If any argument is of type sparse, the output will also be sparse. |
13118
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
25 ## @seealso{diag, horzcat, vertcat, sparse} |
5551 | 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) | |
6046 | 34 print_usage (); |
5551 | 35 endif |
36 | |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
37 if (! all (cellfun ("isnumeric", varargin))) |
5552 | 38 error ("blkdiag: all arguments must be numeric"); |
5551 | 39 endif |
40 | |
5552 | 41 ## Note: trailing singletons are automatically (correctly) ignored. |
5569 | 42 if (! all (cellfun ("ndims", varargin) == 2)) |
5657 | 43 error ("blkdiag: all arguments must be two-dimensional matrices"); |
5551 | 44 endif |
45 | |
5552 | 46 ## size is an option for cellfun, but it's a bit different from |
47 ## calling size directly. | |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
8920
diff
changeset
|
48 tmp = cell2mat (cellfun (@size, varargin', "uniformoutput", false)); |
5989 | 49 csz = cumsum ([0 0; tmp], 1); |
13118
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
50 |
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
51 if (any (cellfun ("issparse", varargin))) |
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
52 retval = sparse (csz(end,1), csz(end,2)); |
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
53 else |
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
54 retval = zeros (csz(end,:)); |
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
55 endif |
5592 | 56 |
57 for p = 1:nargin | |
8120
8f0150a0d19e
fix blkdiag to not rely on Matlab-incompatible behaviour
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
58 vp = varargin{p}; |
8f0150a0d19e
fix blkdiag to not rely on Matlab-incompatible behaviour
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
59 if (! isempty (vp)) |
8f0150a0d19e
fix blkdiag to not rely on Matlab-incompatible behaviour
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
60 retval((csz(p,1)+1):csz(p+1,1),(csz(p,2)+1):csz(p+1,2)) = vp; |
8f0150a0d19e
fix blkdiag to not rely on Matlab-incompatible behaviour
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
61 endif |
5551 | 62 endfor |
63 | |
64 endfunction | |
65 | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
66 |
13118
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
67 ## regular tests |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
68 %!assert (blkdiag (1,ones (2),1), [1,0,0,0;0,1,1,0;0,1,1,0;0,0,0,1]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
69 %!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]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
70 %!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]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
71 %!assert (blkdiag ([1,2;3,4],[5,6,7]), [1,2,0,0,0;3,4,0,0,0;0,0,5,6,7]) |
13118
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
72 ## tests involving empty matrices |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
73 %!assert (blkdiag ([],[],[]), []) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
74 %!assert (blkdiag ([],[1,2;3,4],[],5,[]), [1,2,0;3,4,0;0,0,5]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
75 %!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]); |
13118
c6601cb63e4e
Improve blkdiag for sparse matrices
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
12931
diff
changeset
|
76 ## tests involving sparse matrices |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
77 %!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])) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
78 %!assert (blkdiag (sparse ([1,2;3,4]),[5,6]), sparse ([1,2,0,0;3,4,0,0;0,0,5,6])) |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
79 ## sanity checks |
5551 | 80 %!test |
81 %! A = rand (round (rand (1, 2) * 10)); | |
82 %! assert (blkdiag (A), A); | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
83 |