7017
|
1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2004, |
|
2 ## 2005, 2006, 2007 John W. Eaton |
2313
|
3 ## |
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
2313
|
10 ## |
|
11 ## Octave is distributed in the hope that it will be useful, but |
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 ## General Public License for more details. |
|
15 ## |
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
245
|
19 |
3369
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} hilb (@var{n}) |
|
22 ## Return the Hilbert matrix of order @var{n}. The |
|
23 ## @iftex |
|
24 ## @tex |
|
25 ## $i,\,j$ |
|
26 ## @end tex |
|
27 ## @end iftex |
|
28 ## @ifinfo |
|
29 ## i, j |
|
30 ## @end ifinfo |
|
31 ## element of a Hilbert matrix is defined as |
|
32 ## @iftex |
|
33 ## @tex |
|
34 ## $$ |
|
35 ## H (i, j) = {1 \over (i + j - 1)} |
|
36 ## $$ |
|
37 ## @end tex |
|
38 ## @end iftex |
|
39 ## @ifinfo |
3426
|
40 ## |
3369
|
41 ## @example |
|
42 ## H (i, j) = 1 / (i + j - 1) |
|
43 ## @end example |
|
44 ## @end ifinfo |
5642
|
45 ## @seealso{hankel, vander, sylvester_matrix, invhilb, toeplitz} |
3369
|
46 ## @end deftypefn |
4
|
47 |
2314
|
48 ## Author: jwe |
|
49 |
2311
|
50 function retval = hilb (n) |
4
|
51 |
|
52 |
|
53 if (nargin != 1) |
6046
|
54 print_usage (); |
4
|
55 endif |
|
56 |
|
57 nmax = length (n); |
|
58 if (nmax == 1) |
|
59 retval = zeros (n); |
556
|
60 tmp = 1:n; |
|
61 for i = 1:n |
|
62 retval (i, :) = 1.0 ./ (tmp + (i - 1)); |
4
|
63 endfor |
|
64 else |
|
65 error ("hilb: expecting scalar argument, found something else"); |
|
66 endif |
|
67 |
|
68 endfunction |
7411
|
69 |
|
70 %!assert((hilb (2) == [1, 1/2; 1/2, 1/3] |
|
71 %! && hilb (3) == [1, 1/2, 1/3; 1/2, 1/3, 1/4; 1/3, 1/4, 1/5])); |
|
72 |
|
73 %!error hilb (); |
|
74 |
|
75 %!error hilb (1, 2); |
|
76 |