Mercurial > hg > octave-avbm
changeset 9836:804c21f3659b
improve perms
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 19 Nov 2009 11:15:23 +0100 |
parents | 1bb1ed717d2f |
children | 7c70084b125e |
files | scripts/ChangeLog scripts/specfun/perms.m |
diffstat | 2 files changed, 21 insertions(+), 12 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,8 @@ +2009-11-19 Jaroslav Hajek <highegg@gmail.com> + + * specfun/perms.m: Avoid recursion. Simplify assignments. Allow empty + vectors. + 2009-11-19 Jaroslav Hajek <highegg@gmail.com> * specfun/erfinv.m: Remove.
--- a/scripts/specfun/perms.m +++ b/scripts/specfun/perms.m @@ -1,4 +1,5 @@ ## Copyright (C) 2001, 2006, 2007, 2009 Paul Kienzle +## Copyright (C) 2009 VZLU Prague ## ## This file is part of Octave. ## @@ -42,19 +43,22 @@ endif v = v(:); n = length (v); - if (n == 1) - A = v; + + if (n == 0) + A = []; else - B = perms (v(1:n-1)); - Bidx = 1:size (B, 1); - A = v(n) * ones (prod (2:n), n); - A(Bidx,1:n-1) = B; - k = size (B, 1); - for i = n-1:-1:2 - A(k+Bidx,1:i-1) = B(Bidx,1:i-1); - A(k+Bidx,i+1:n) = B(Bidx,i:n-1); - k = k + size (B, 1); + A = v(1); + for j = 2:n + B = A; + A = zeros (prod (2:j), n, class (v)); + k = size (B, 1); + idx = 1:k; + for i = j:-1:1 + A(idx,1:i-1) = B(:,1:i-1); + A(idx,i) = v(j); + A(idx,i+1:j) = B(:,i:j-1); + idx += k; + endfor endfor - A(k+Bidx,2:n) = B; endif endfunction