Mercurial > hg > octave-lyh
annotate scripts/special-matrix/invhilb.m @ 9051:1bf0ce0930be
Grammar check TexInfo in all .m files
Cleanup documentation sources to follow a few consistent rules.
Spellcheck was NOT done. (but will be in another changeset)
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Fri, 27 Mar 2009 22:31:03 -0700 |
parents | eb63fbe60fab |
children | 3140cb7a05a1 |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, |
8920 | 2 ## 2004, 2005, 2006, 2007, 2008, 2009 Dirk Laurie |
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} {} invhilb (@var{n}) | |
3889 | 22 ## Return the inverse of a Hilbert matrix of order @var{n}. This can be |
5435 | 23 ## computed exactly using |
3889 | 24 ## @tex |
25 ## $$\eqalign{ | |
26 ## A_{ij} &= -1^{i+j} (i+j-1) | |
27 ## \left( \matrix{n+i-1 \cr n-j } \right) | |
28 ## \left( \matrix{n+j-1 \cr n-i } \right) | |
29 ## \left( \matrix{i+j-2 \cr i-2 } \right)^2 \cr | |
30 ## &= { p(i)p(j) \over (i+j-1) } | |
31 ## }$$ | |
32 ## where | |
33 ## $$ | |
34 ## p(k) = -1^k \left( \matrix{ k+n-1 \cr k-1 } \right) | |
35 ## \left( \matrix{ n \cr k } \right) | |
36 ##$$ | |
37 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
38 ## @ifnottex |
3889 | 39 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
40 ## @group |
3889 | 41 ## |
42 ## (i+j) /n+i-1\ /n+j-1\ /i+j-2\ 2 | |
43 ## A(i,j) = -1 (i+j-1)( )( ) ( ) | |
44 ## \ n-j / \ n-i / \ i-2 / | |
45 ## | |
46 ## = p(i) p(j) / (i+j-1) | |
47 ## | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
48 ## @end group |
3889 | 49 ## @end example |
50 ## where | |
51 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
52 ## @group |
3889 | 53 ## k /k+n-1\ /n\ |
54 ## p(k) = -1 ( ) ( ) | |
55 ## \ k-1 / \k/ | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
56 ## @end group |
3889 | 57 ## @end example |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
58 ## @end ifnottex |
3889 | 59 ## |
60 ## The validity of this formula can easily be checked by expanding | |
61 ## the binomial coefficients in both formulas as factorials. It can | |
62 ## be derived more directly via the theory of Cauchy matrices: | |
63 ## see J. W. Demmel, Applied Numerical Linear Algebra, page 92. | |
64 ## | |
65 ## Compare this with the numerical calculation of @code{inverse (hilb (n))}, | |
3369 | 66 ## which suffers from the ill-conditioning of the Hilbert matrix, and the |
67 ## finite precision of your computer's floating point arithmetic. | |
5642 | 68 ## @seealso{hankel, vander, sylvester_matrix, hilb, toeplitz} |
3369 | 69 ## @end deftypefn |
4 | 70 |
5132 | 71 ## Author: Dirk Laurie <dlaurie@na-net.ornl.gov> |
2314 | 72 |
2311 | 73 function retval = invhilb (n) |
4 | 74 |
75 if (nargin != 1) | |
6046 | 76 print_usage (); |
4 | 77 endif |
78 | |
79 nmax = length (n); | |
80 if (nmax == 1) | |
3889 | 81 |
82 ## The point about the second formula above is that when vectorized, | |
83 ## p(k) is evaluated for k=1:n which involves O(n) calls to bincoeff | |
84 ## instead of O(n^2). | |
85 ## | |
86 ## We evaluate the expression as (-1)^(i+j)*(p(i)*p(j))/(i+j-1) except | |
87 ## when p(i)*p(j) would overflow. In cases where p(i)*p(j) is an exact | |
88 ## machine number, the result is also exact. Otherwise we calculate | |
89 ## (-1)^(i+j)*p(i)*(p(j)/(i+j-1)). | |
90 ## | |
4031 | 91 ## The Octave bincoeff routine uses transcendental functions (gammaln |
3889 | 92 ## and exp) rather than multiplications, for the sake of speed. |
93 ## However, it rounds the answer to the nearest integer, which | |
94 ## justifies the claim about exactness made above. | |
95 | |
96 retval = zeros (n); | |
97 k = [1:n]; | |
98 p = k .* bincoeff (k+n-1, k-1) .* bincoeff (n, k); | |
99 p(2:2:n) = -p(2:2:n); | |
100 if (n < 203) | |
101 for l = 1:n | |
102 retval(l,:) = (p(l) * p) ./ [l:l+n-1]; | |
4 | 103 endfor |
3889 | 104 else |
105 for l = 1:n | |
106 retval(l,:) = p(l) * (p ./ [l:l+n-1]); | |
107 endfor | |
108 endif | |
4 | 109 else |
3889 | 110 error ("invhilb: expecting scalar argument, found something else"); |
4 | 111 endif |
112 | |
113 endfunction | |
7411 | 114 |
115 %!test | |
116 %! result4 = [16, -120, 240, -140; | |
117 %! -120, 1200, -2700, 1680; | |
118 %! 240, -2700, 6480, -4200; | |
119 %! -140, 1680, -4200, 2800]; | |
120 %! | |
121 %! assert((invhilb (1) == 1 && invhilb (2) == [4, -6; -6, 12] | |
122 %! && invhilb (4) == result4 | |
123 %! && abs (invhilb (7) * hilb (7) - eye (7)) < sqrt (eps))); | |
124 | |
125 %!error invhilb ([1, 2]); | |
126 | |
127 %!error invhilb (); | |
128 | |
129 %!error invhilb (1, 2); | |
130 |