Mercurial > hg > octave-nkf
annotate scripts/general/repmat.m @ 9665:1dba57e9d08d
use blas_trans_type for xgemm
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Sat, 26 Sep 2009 10:41:07 +0200 |
parents | 5556563c6551 |
children | be55736a0783 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2002, 2004, 2005, 2006, 2007, 2009 Paul Kienzle |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
2 ## Copyright (C) 2008 Jaroslav Hajek |
3914 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3914 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
3914 | 19 |
20 ## -*- texinfo -*- | |
21 ## @deftypefn {Function File} {} repmat (@var{A}, @var{m}, @var{n}) | |
22 ## @deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n}]) | |
9041
853f96e8008f
Cleanup documentation file matrix.texi
Rik <rdrider0-list@yahoo.com>
parents:
8970
diff
changeset
|
23 ## @deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n} @var{p} @dots{}]) |
3914 | 24 ## Form a block matrix of size @var{m} by @var{n}, with a copy of matrix |
25 ## @var{A} as each element. If @var{n} is not specified, form an | |
26 ## @var{m} by @var{m} block matrix. | |
27 ## @end deftypefn | |
28 | |
29 ## Author: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> | |
30 ## Created: July 2000 | |
31 | |
3915 | 32 function x = repmat (a, m, n) |
33 | |
3914 | 34 if (nargin < 2 || nargin > 3) |
6046 | 35 print_usage (); |
3914 | 36 endif |
37 | |
4844 | 38 if (nargin == 3) |
5195 | 39 if (! (isscalar (m) && isscalar (n))) |
4844 | 40 error ("repmat: with 3 arguments m and n must be scalar"); |
41 endif | |
42 idx = [m, n]; | |
43 else | |
4030 | 44 if (isscalar (m)) |
4844 | 45 idx = [m, m]; |
3914 | 46 n = m; |
4844 | 47 elseif (isvector (m) && length (m) > 1) |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
48 ## Ensure that we have a row vector |
4844 | 49 idx = m(:).'; |
3914 | 50 else |
4844 | 51 error ("repmat: invalid dimensional argument"); |
3914 | 52 endif |
53 endif | |
8508
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
54 |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
55 if (all (idx < 0)) |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
56 error ("repmat: invalid dimensions"); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
57 else |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
58 idx = max (idx, 0); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
59 endif |
3914 | 60 |
4844 | 61 if (numel (a) == 1) |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
62 ## optimize the scalar fill case. |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
63 x(1:prod (idx)) = a; |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
64 x = reshape (x, idx); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
65 elseif (ndims (a) == 2 && length (idx) < 3) |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
66 if (issparse (a)) |
9388
5556563c6551
repmat.m: call cron, not spkron
Marco Caliari <marco.caliari@univr.it>
parents:
9041
diff
changeset
|
67 x = kron (ones (idx), a); |
4844 | 68 else |
8390
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
69 ## indexing is now faster, so we use it rather than kron. |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
70 m = rows (a); n = columns (a); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
71 p = idx(1); q = idx(2); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
72 x = reshape (a, m, 1, n, 1); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
73 x = x(:, ones (1, p), :, ones (1, q)); |
49901b624316
optimize repmat for scalar & matrix case
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
74 x = reshape (x, m*p, n*q); |
4844 | 75 endif |
3914 | 76 else |
8970 | 77 aidx = size (a); |
78 ## ensure matching size | |
79 idx(end+1:length (aidx)) = 1; | |
80 aidx(end+1:length (idx)) = 1; | |
81 ## create subscript array | |
82 cidx = cell (2, length (aidx)); | |
8507 | 83 for i = 1:length (aidx) |
8970 | 84 cidx{1,i} = ':'; |
85 cidx{2,i} = ones (1, idx (i)); | |
4844 | 86 endfor |
8970 | 87 aaidx = aidx; |
88 # add singleton dims | |
89 aaidx(2,:) = 1; | |
90 a = reshape (a, aaidx(:)); | |
91 x = reshape (a (cidx{:}), idx .* aidx); | |
3914 | 92 endif |
93 | |
94 endfunction | |
6987 | 95 |
96 # Test various methods of providing size parameters | |
97 %!shared x | |
98 %! x = [1 2;3 4]; | |
99 %!assert(repmat(x, [1 1]), repmat(x, 1)); | |
100 %!assert(repmat(x, [3 3]), repmat(x, 3)); | |
101 %!assert(repmat(x, [1 1]), repmat(x, 1, 1)); | |
102 %!assert(repmat(x, [1 3]), repmat(x, 1, 3)); | |
103 %!assert(repmat(x, [3 1]), repmat(x, 3, 1)); | |
104 %!assert(repmat(x, [3 3]), repmat(x, 3, 3)); | |
105 | |
106 # Tests for numel==1 case: | |
107 %!shared x, r | |
108 %! x = [ 65 ]; | |
109 %! r = kron(ones(2,2), x); | |
110 %!assert(r, repmat(x, [2 2])); | |
111 %!assert(char(r), repmat(char(x), [2 2])); | |
112 %!assert(int8(r), repmat(int8(x), [2 2])); | |
113 | |
114 # Tests for ndims==2 case: | |
115 %!shared x, r | |
116 %! x = [ 65 66 67 ]; | |
117 %! r = kron(ones(2,2), x); | |
118 %!assert(r, repmat(x, [2 2])); | |
119 %!assert(char(r), repmat(char(x), [2 2])); | |
120 %!assert(int8(r), repmat(int8(x), [2 2])); | |
121 | |
122 # Tests for dim>2 case: | |
123 %!shared x, r | |
124 %! x = [ 65 66 67 ]; | |
125 %! r = kron(ones(2,2), x); | |
126 %! r(:,:,2) = r(:,:,1); | |
127 %!assert(r, repmat(x, [2 2 2])); | |
128 %!assert(char(r), repmat(char(x), [2 2 2])); | |
129 %!assert(int8(r), repmat(int8(x), [2 2 2])); | |
130 | |
131 # Test that sparsity is kept | |
132 %!assert(sparse(4,4), repmat(sparse(2,2),[2 2])); | |
133 | |
8508
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
134 |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
135 %!assert (size (repmat (".", -1, 1)), [0, 1]); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
136 %!assert (size (repmat (".", 1, -1)), [1, 0]); |
dee629f14bfa
repmat.m: handle negative dimensions properly
John W. Eaton <jwe@octave.org>
parents:
8507
diff
changeset
|
137 %!error (size (repmat (".", -1, -1))); |