2847
|
1 ## Copyright (C) 1996, 1997 John W. Eaton |
2313
|
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 2, or (at your option) |
|
8 ## 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, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
245
|
19 |
3369
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} hankel (@var{c}, @var{r}) |
|
22 ## Return the Hankel matrix constructed given 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 |
4922
|
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}. |
3426
|
27 ## |
3369
|
28 ## A Hankel matrix formed from an m-vector @var{c}, and an n-vector |
|
29 ## @var{r}, has the elements |
|
30 ## @iftex |
|
31 ## @tex |
|
32 ## $$ |
|
33 ## H (i, j) = \cases{c_{i+j-1},&$i+j-1\le m$;\cr r_{i+j-m},&otherwise.\cr} |
|
34 ## $$ |
|
35 ## @end tex |
|
36 ## @end iftex |
|
37 ## @ifinfo |
3426
|
38 ## |
3369
|
39 ## @example |
|
40 ## @group |
4922
|
41 ## H(i,j) = c(i+j-1), i+j-1 <= m; |
|
42 ## H(i,j) = r(i+j-m), otherwise |
3369
|
43 ## @end group |
|
44 ## @end example |
|
45 ## @end ifinfo |
5642
|
46 ## @seealso{vander, sylvester_matrix, hilb, invhilb, toeplitz} |
3369
|
47 ## @end deftypefn |
4
|
48 |
2314
|
49 ## Author: jwe |
|
50 |
2311
|
51 function retval = hankel (c, r) |
4
|
52 |
|
53 if (nargin == 1) |
5731
|
54 r = resize (resize (c, 0), size(c)); |
4
|
55 elseif (nargin != 2) |
6046
|
56 print_usage (); |
4
|
57 endif |
|
58 |
|
59 [c_nr, c_nc] = size (c); |
|
60 [r_nr, r_nc] = size (r); |
|
61 |
|
62 if ((c_nr != 1 && c_nc != 1) || (r_nr != 1 && r_nc != 1)) |
1518
|
63 error ("hankel: expecting vector arguments"); |
4
|
64 endif |
|
65 |
2325
|
66 if (nargin == 1) |
1396
|
67 r (1) = c (length (c)); |
|
68 endif |
|
69 |
4
|
70 if (c_nc != 1) |
1035
|
71 c = c.'; |
4
|
72 endif |
|
73 |
|
74 if (r_nr != 1) |
1035
|
75 r = r.'; |
4
|
76 endif |
|
77 |
1396
|
78 nc = length (r); |
|
79 nr = length (c); |
|
80 |
|
81 if (r (1) != c (nr)) |
904
|
82 warning ("hankel: column wins anti-diagonal conflict"); |
4
|
83 endif |
|
84 |
2303
|
85 ## This should probably be done with the colon operator... |
4
|
86 |
5731
|
87 retval = resize (resize (c, 0), nr, nc); |
4
|
88 |
|
89 for i = 1:min (nr, nc) |
|
90 retval (1:nr-i+1, i) = c (i:nr); |
|
91 endfor |
|
92 |
|
93 tmp = 1; |
|
94 if (nc <= nr) |
|
95 tmp = nr - nc + 2; |
|
96 endif |
|
97 |
|
98 for i = nr:-1:tmp |
|
99 retval (i, 2+nr-i:nc) = r (2:nc-nr+i); |
|
100 endfor |
|
101 |
|
102 endfunction |
5731
|
103 |
|
104 %!assert(hankel(1:3),[1,2,3;2,3,0;3,0,0]) |
|
105 %!assert(hankel(1),[1]); |
|
106 %!assert(hankel(1:3,3:6),[1,2,3,4;2,3,4,5;3,4,5,6]); |
|
107 %!assert(hankel(1:3,3:4),[1,2;2,3;3,4]); |
|
108 %!assert(hankel(1:3,4:6),[1,2,3;2,3,5;3,5,6]); |