Mercurial > octave-libgccjit
annotate scripts/special-matrix/invhilb.m @ 8920:eb63fbe60fab
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 07 Mar 2009 10:41:27 -0500 |
parents | 81d6ab3ac93c |
children | 1bf0ce0930be |
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 |
40 ## | |
41 ## (i+j) /n+i-1\ /n+j-1\ /i+j-2\ 2 | |
42 ## A(i,j) = -1 (i+j-1)( )( ) ( ) | |
43 ## \ n-j / \ n-i / \ i-2 / | |
44 ## | |
45 ## = p(i) p(j) / (i+j-1) | |
46 ## | |
47 ## @end example | |
48 ## where | |
49 ## @example | |
50 ## k /k+n-1\ /n\ | |
51 ## p(k) = -1 ( ) ( ) | |
52 ## \ k-1 / \k/ | |
53 ## @end example | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
54 ## @end ifnottex |
3889 | 55 ## |
56 ## The validity of this formula can easily be checked by expanding | |
57 ## the binomial coefficients in both formulas as factorials. It can | |
58 ## be derived more directly via the theory of Cauchy matrices: | |
59 ## see J. W. Demmel, Applied Numerical Linear Algebra, page 92. | |
60 ## | |
61 ## Compare this with the numerical calculation of @code{inverse (hilb (n))}, | |
3369 | 62 ## which suffers from the ill-conditioning of the Hilbert matrix, and the |
63 ## finite precision of your computer's floating point arithmetic. | |
5642 | 64 ## @seealso{hankel, vander, sylvester_matrix, hilb, toeplitz} |
3369 | 65 ## @end deftypefn |
4 | 66 |
5132 | 67 ## Author: Dirk Laurie <dlaurie@na-net.ornl.gov> |
2314 | 68 |
2311 | 69 function retval = invhilb (n) |
4 | 70 |
71 if (nargin != 1) | |
6046 | 72 print_usage (); |
4 | 73 endif |
74 | |
75 nmax = length (n); | |
76 if (nmax == 1) | |
3889 | 77 |
78 ## The point about the second formula above is that when vectorized, | |
79 ## p(k) is evaluated for k=1:n which involves O(n) calls to bincoeff | |
80 ## instead of O(n^2). | |
81 ## | |
82 ## We evaluate the expression as (-1)^(i+j)*(p(i)*p(j))/(i+j-1) except | |
83 ## when p(i)*p(j) would overflow. In cases where p(i)*p(j) is an exact | |
84 ## machine number, the result is also exact. Otherwise we calculate | |
85 ## (-1)^(i+j)*p(i)*(p(j)/(i+j-1)). | |
86 ## | |
4031 | 87 ## The Octave bincoeff routine uses transcendental functions (gammaln |
3889 | 88 ## and exp) rather than multiplications, for the sake of speed. |
89 ## However, it rounds the answer to the nearest integer, which | |
90 ## justifies the claim about exactness made above. | |
91 | |
92 retval = zeros (n); | |
93 k = [1:n]; | |
94 p = k .* bincoeff (k+n-1, k-1) .* bincoeff (n, k); | |
95 p(2:2:n) = -p(2:2:n); | |
96 if (n < 203) | |
97 for l = 1:n | |
98 retval(l,:) = (p(l) * p) ./ [l:l+n-1]; | |
4 | 99 endfor |
3889 | 100 else |
101 for l = 1:n | |
102 retval(l,:) = p(l) * (p ./ [l:l+n-1]); | |
103 endfor | |
104 endif | |
4 | 105 else |
3889 | 106 error ("invhilb: expecting scalar argument, found something else"); |
4 | 107 endif |
108 | |
109 endfunction | |
7411 | 110 |
111 %!test | |
112 %! result4 = [16, -120, 240, -140; | |
113 %! -120, 1200, -2700, 1680; | |
114 %! 240, -2700, 6480, -4200; | |
115 %! -140, 1680, -4200, 2800]; | |
116 %! | |
117 %! assert((invhilb (1) == 1 && invhilb (2) == [4, -6; -6, 12] | |
118 %! && invhilb (4) == result4 | |
119 %! && abs (invhilb (7) * hilb (7) - eye (7)) < sqrt (eps))); | |
120 | |
121 %!error invhilb ([1, 2]); | |
122 | |
123 %!error invhilb (); | |
124 | |
125 %!error invhilb (1, 2); | |
126 |