changeset 8390:49901b624316

optimize repmat for scalar & matrix case
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 10 Dec 2008 11:04:28 +0100
parents ff5892c8ddd0
children 343f0fbca6eb
files liboctave/Array.cc scripts/ChangeLog scripts/general/repmat.m
diffstat 3 files changed, 25 insertions(+), 29 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/Array.cc
+++ b/liboctave/Array.cc
@@ -1113,7 +1113,13 @@
       // Try to resize first if necessary. 
       if (nx != n)
         {
-          resize_fill (nx, rfv);      
+          // A simple optimization. Things like A(1:N) = x will skip fill on
+          // resizing, if A is 0x0.
+          if (rows () == 0 && columns () == 0 && ndims () == 2
+              && rhl == 1 && i.is_colon_equiv (nx))
+            *this = Array<T> (dim_vector (1, nx));
+          else
+            resize_fill (nx, rfv);      
           n = numel ();
         }
 
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -1,3 +1,7 @@
+2008-12-09  Jaroslav Hajek  <highegg@gmail.com>
+
+	* general/repmat.m: Optimize & simplify the scalar & 2d matrix case.
+
 2008-12-07  Thorsten Meyer  <thorsten.meyier@gmx.de>
 
         * strings/lower.m: Remove
--- a/scripts/general/repmat.m
+++ b/scripts/general/repmat.m
@@ -1,4 +1,5 @@
 ## Copyright (C) 2000, 2002, 2004, 2005, 2006, 2007 Paul Kienzle
+## Copyright (C) 2008 Jaroslav Hajek
 ##
 ## This file is part of Octave.
 ##
@@ -44,7 +45,7 @@
       idx = [m, m];
       n = m;
     elseif (isvector (m) && length (m) > 1)
-      # Ensure that we have a row vector
+      ## Ensure that we have a row vector
       idx = m(:).';
     else
       error ("repmat: invalid dimensional argument");
@@ -52,34 +53,19 @@
   endif
 
   if (numel (a) == 1)
-    if (ischar (a))
-      x = char (toascii (a) * ones (idx));
+    ## optimize the scalar fill case.
+    x(1:prod (idx)) = a;
+    x = reshape (x, idx);
+  elseif (ndims (a) == 2 && length (idx) < 3)
+    if (issparse (a))
+      x = spkron (ones (idx), a);
     else
-      if (strcmp (class (a), "double"))
-	## This is faster with octave for double/Complex
-	x = a * ones(idx, class(a));
-      else
-	cidx = cell (1, length (idx));
-	for i=1:length(idx)
-	  cidx{i} = ones (1,idx(i));
-	endfor
-	x = a (cidx{:});
-      endif
-    endif
-  elseif (ndims (a) == 2 && length (idx) < 3)
-    if (ischar (a))
-      x = char (kron (ones (idx), toascii (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)),  
-	     kron (ones (1, idx(2)), 1:aidx(2)));
+      ## indexing is now faster, so we use it rather than kron.
+      m = rows (a); n = columns (a);
+      p = idx(1); q = idx(2);
+      x = reshape (a, m, 1, n, 1);
+      x = x(:, ones (1, p), :, ones (1, q));
+      x = reshape (x, m*p, n*q);
     endif
   else
     aidx = size(a);