]> Creatis software - CreaPhase.git/blob - octave_packages/m/special-matrix/hankel.m
update packages
[CreaPhase.git] / octave_packages / m / special-matrix / hankel.m
1 ## Copyright (C) 1993-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
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.
9 ##
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.
14 ##
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/>.
18
19 ## -*- texinfo -*-
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}.
27 ##
28 ## A Hankel matrix formed from an m-vector @var{c}, and an n-vector
29 ## @var{r}, has the elements
30 ## @tex
31 ## $$
32 ## H(i, j) = \cases{c_{i+j-1},&$i+j-1\le m$;\cr r_{i+j-m},&otherwise.\cr}
33 ## $$
34 ## @end tex
35 ## @ifnottex
36 ##
37 ## @example
38 ## @group
39 ## H(i,j) = c(i+j-1),  i+j-1 <= m;
40 ## H(i,j) = r(i+j-m),  otherwise
41 ## @end group
42 ## @end example
43 ##
44 ## @end ifnottex
45 ## @seealso{hadamard, toeplitz}
46 ## @end deftypefn
47
48 ## Author: jwe
49
50 function retval = hankel (c, r)
51
52   if (nargin < 1 || nargin > 2)
53     print_usage ();
54   endif
55
56   if (nargin == 1)
57
58     if (! isvector (c))
59       error ("hankel: C must be a vector");
60     endif
61
62     nr = length (c);
63     nc = nr;
64     data = [c(:) ; zeros(nr, 1)];
65
66   else
67
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");
72     endif
73
74     nr = length (c);
75     nc = length (r);
76     data = [c(:) ; r(2:end)(:)];
77
78   endif
79    
80   slices = cellslices (data, 1:nc, nr:1:nc+nr-1);
81   retval = horzcat (slices{:});
82
83 endfunction
84
85
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])
93
94 %!error hankel ();
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])
98