Mercurial > hg > octave-terminal
changeset 9092:38c3a0f8c6d0
optimize toeplitz
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 06 Apr 2009 08:33:13 +0200 |
parents | 45c832e9976d |
children | 8be05554bbd0 |
files | scripts/ChangeLog scripts/special-matrix/toeplitz.m |
diffstat | 2 files changed, 20 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/scripts/ChangeLog +++ b/scripts/ChangeLog @@ -1,3 +1,7 @@ +2009-04-06 Jaroslav Hajek <highegg@gmail.com> + + * special-matrix/toeplitz.m: Optimize. + 2008-04-03 David Bateman <dbateman@free.fr> * plot/__scatter__.m: correct indexing of cdata.x
--- a/scripts/special-matrix/toeplitz.m +++ b/scripts/special-matrix/toeplitz.m @@ -1,5 +1,6 @@ ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2004, ## 2005, 2006, 2007, 2008, 2009 John W. Eaton +## Copyright (C) 2009 VZLU Prague ## ## This file is part of Octave. ## @@ -54,7 +55,7 @@ ## @seealso{hankel, vander, sylvester_matrix, hilb, invhilb} ## @end deftypefn -## Author: jwe +## Author: jwe && jh function retval = toeplitz (c, r) @@ -64,19 +65,17 @@ print_usage (); endif - [c_nr, c_nc] = size (c); - [r_nr, r_nc] = size (r); - - if ((c_nr != 1 && c_nc != 1) || (r_nr != 1 && r_nc != 1)) + if (! (isvector (c) && isvector (r))) error ("toeplitz: expecting vector arguments"); endif - if (c_nc != 1) - c = c.'; - endif + nc = length (r); + nr = length (c); - if (r_nr != 1) - r = r.'; + if (nr == 0 || nc == 0) + ## Empty matrix. + retval = zeros (nr, nc, class (c)); + return; endif if (r (1) != c (1)) @@ -87,25 +86,19 @@ ## Hermitian-symmetric matrix (actually, this will really only be ## Hermitian-symmetric if the first element of the vector is real). - if (nargin == 1) + if (nargin == 1 && iscomplex (c)) c = conj (c); c(1) = conj (c(1)); endif - ## This should probably be done with the colon operator... - - nc = length (r); - nr = length (c); - - retval = resize (resize (c, 0), nr, nc); + ## Concatenate data into a single column vector. + data = [r(end:-1:2)(:); c(:)]; - for i = 1:min (nc, nr) - retval (i:nr, i) = c (1:nr-i+1); - endfor + ## Get slices. + slices = cellslices (data, nc:-1:1, nc+nr-1:-1:nr); - for i = 1:min (nr, nc-1) - retval (i, i+1:nc) = r (2:nc-i+1); - endfor + ## Form matrix. + retval = horzcat (slices{:}); endfunction