# HG changeset patch # User Jaroslav Hajek # Date 1228903468 -3600 # Node ID 49901b624316d51ab9bc6da402763b0c43a6678e # Parent ff5892c8ddd07f72d8f579ca45ff65d2a6e1909d optimize repmat for scalar & matrix case diff -r ff5892c8ddd0 -r 49901b624316 liboctave/Array.cc --- a/liboctave/Array.cc Wed Dec 10 11:04:28 2008 +0100 +++ b/liboctave/Array.cc Wed Dec 10 11:04:28 2008 +0100 @@ -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 (dim_vector (1, nx)); + else + resize_fill (nx, rfv); n = numel (); } diff -r ff5892c8ddd0 -r 49901b624316 scripts/ChangeLog --- a/scripts/ChangeLog Wed Dec 10 11:04:28 2008 +0100 +++ b/scripts/ChangeLog Wed Dec 10 11:04:28 2008 +0100 @@ -1,3 +1,7 @@ +2008-12-09 Jaroslav Hajek + + * general/repmat.m: Optimize & simplify the scalar & 2d matrix case. + 2008-12-07 Thorsten Meyer * strings/lower.m: Remove diff -r ff5892c8ddd0 -r 49901b624316 scripts/general/repmat.m --- a/scripts/general/repmat.m Wed Dec 10 11:04:28 2008 +0100 +++ b/scripts/general/repmat.m Wed Dec 10 11:04:28 2008 +0100 @@ -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);