diff scripts/general/repmat.m @ 6987:deb175b6e4a1

[project @ 2007-10-09 18:39:15 by jwe]
author jwe
date Tue, 09 Oct 2007 18:39:16 +0000
parents 34f96dd5441b
children 93c65f2a5668
line wrap: on
line diff
--- a/scripts/general/repmat.m
+++ b/scripts/general/repmat.m
@@ -20,6 +20,7 @@
 ## -*- texinfo -*-
 ## @deftypefn {Function File} {} repmat (@var{A}, @var{m}, @var{n})
 ## @deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n}])
+## @deftypefnx {Function File} {} repmat (@var{A}, [@var{m} @var{n} @var{p} ...])
 ## Form a block matrix of size @var{m} by @var{n}, with a copy of matrix
 ## @var{A} as each element.  If @var{n} is not specified, form an 
 ## @var{m} by @var{m} block matrix.
@@ -69,8 +70,13 @@
   elseif (ndims (a) == 2 && length (idx) < 3)
     if (ischar (a))
       x = char (kron (ones (idx), toascii (a)));
-    elseif (strcmp (class(a), "double")) 
-      x = kron (ones (idx), a);
+    elseif (strcmp (class(a), "double"))
+      ## FIXME -- DISPATCH.
+      if (issparse (a))
+        x = spkron (ones (idx), a);
+      else
+        x = kron (ones (idx), a);
+      endif
     else
       aidx = size(a);
       x = a (kron (ones (1, idx(1)), 1:aidx(1)),  
@@ -91,3 +97,42 @@
   endif
 
 endfunction
+
+# Test various methods of providing size parameters
+%!shared x
+%! x = [1 2;3 4];
+%!assert(repmat(x, [1 1]), repmat(x, 1));
+%!assert(repmat(x, [3 3]), repmat(x, 3));
+%!assert(repmat(x, [1 1]), repmat(x, 1, 1));
+%!assert(repmat(x, [1 3]), repmat(x, 1, 3));
+%!assert(repmat(x, [3 1]), repmat(x, 3, 1));
+%!assert(repmat(x, [3 3]), repmat(x, 3, 3));
+
+# Tests for numel==1 case:
+%!shared x, r
+%! x = [ 65 ];
+%! r = kron(ones(2,2), x);
+%!assert(r, repmat(x, [2 2]));
+%!assert(char(r), repmat(char(x), [2 2]));
+%!assert(int8(r), repmat(int8(x), [2 2]));
+
+# Tests for ndims==2 case:
+%!shared x, r
+%! x = [ 65 66 67 ];
+%! r = kron(ones(2,2), x);
+%!assert(r, repmat(x, [2 2]));
+%!assert(char(r), repmat(char(x), [2 2]));
+%!assert(int8(r), repmat(int8(x), [2 2]));
+
+# Tests for dim>2 case:
+%!shared x, r
+%! x = [ 65 66 67 ];
+%! r = kron(ones(2,2), x);
+%! r(:,:,2) = r(:,:,1);
+%!assert(r, repmat(x, [2 2 2]));
+%!assert(char(r), repmat(char(x), [2 2 2]));
+%!assert(int8(r), repmat(int8(x), [2 2 2]));
+
+# Test that sparsity is kept
+%!assert(sparse(4,4), repmat(sparse(2,2),[2 2]));
+