1 ## Copyright (C) 1993-2012 John W. Eaton
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {} hankel (@var{c})
21 ## @deftypefnx {Function File} {} hankel (@var{c}, @var{r})
22 ## Return the Hankel matrix constructed from the first column @var{c}, and
23 ## (optionally) the last row @var{r}. If the last element of @var{c} is
24 ## not the same as the first element of @var{r}, the last element of
25 ## @var{c} is used. If the second argument is omitted, it is assumed to
26 ## be a vector of zeros with the same size as @var{c}.
28 ## A Hankel matrix formed from an m-vector @var{c}, and an n-vector
29 ## @var{r}, has the elements
32 ## H(i, j) = \cases{c_{i+j-1},&$i+j-1\le m$;\cr r_{i+j-m},&otherwise.\cr}
39 ## H(i,j) = c(i+j-1), i+j-1 <= m;
40 ## H(i,j) = r(i+j-m), otherwise
45 ## @seealso{hadamard, toeplitz}
50 function retval = hankel (c, r)
52 if (nargin < 1 || nargin > 2)
59 error ("hankel: C must be a vector");
64 data = [c(:) ; zeros(nr, 1)];
68 if (! (isvector (c) && isvector (r)))
69 error ("hankel: C and R must be vectors");
70 elseif (r(1) != c(end))
71 warning ("hankel: column wins anti-diagonal conflict");
76 data = [c(:) ; r(2:end)(:)];
80 slices = cellslices (data, 1:nc, nr:1:nc+nr-1);
81 retval = horzcat (slices{:});
86 %!assert (hankel (1), [1])
87 %!assert (hankel ([1, 2]), [1, 2; 2, 0])
88 %!assert (hankel ([1, 2], [2; -1; -3]), [1, 2, -1; 2, -1, -3])
89 %!assert (hankel (1:3), [1,2,3;2,3,0;3,0,0])
90 %!assert (hankel (1:3,3:6), [1,2,3,4;2,3,4,5;3,4,5,6])
91 %!assert (hankel (1:3,3:4), [1,2;2,3;3,4])
92 %!assert (hankel (1:3,4:6), [1,2,3;2,3,5;3,5,6])
95 %!error hankel (1, 2, 3);
96 %!error <C must be a vector> hankel ([1, 2; 3, 4])
97 %!error <C and R must be vectors> hankel (1:4, [1, 2; 3, 4])