Mercurial > hg > octave-lyh
annotate scripts/special-matrix/invhilb.m @ 14475:00d8118d2401
maint: Remove issuperuser.m from private function list for pkg directory.
* pkg/module.mk: Remove issuperuser.m from private function list for pkg directory.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Fri, 16 Mar 2012 14:38:27 -0700 |
parents | f3d52523cde1 |
children |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13892
diff
changeset
|
1 ## Copyright (C) 1993-2012 Dirk Laurie |
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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
2313 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
245 | 18 |
3369 | 19 ## -*- texinfo -*- |
20 ## @deftypefn {Function File} {} invhilb (@var{n}) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
21 ## Return the inverse of the Hilbert matrix of order @var{n}. This can be |
5435 | 22 ## computed exactly using |
3889 | 23 ## @tex |
24 ## $$\eqalign{ | |
25 ## A_{ij} &= -1^{i+j} (i+j-1) | |
26 ## \left( \matrix{n+i-1 \cr n-j } \right) | |
27 ## \left( \matrix{n+j-1 \cr n-i } \right) | |
28 ## \left( \matrix{i+j-2 \cr i-2 } \right)^2 \cr | |
29 ## &= { p(i)p(j) \over (i+j-1) } | |
30 ## }$$ | |
31 ## where | |
32 ## $$ | |
33 ## p(k) = -1^k \left( \matrix{ k+n-1 \cr k-1 } \right) | |
34 ## \left( \matrix{ n \cr k } \right) | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
35 ## $$ |
3889 | 36 ## @end tex |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
37 ## @ifnottex |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
38 ## |
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 |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
50 ## |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
51 ## @noindent |
3889 | 52 ## where |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
53 ## |
3889 | 54 ## @example |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
55 ## @group |
3889 | 56 ## k /k+n-1\ /n\ |
57 ## p(k) = -1 ( ) ( ) | |
58 ## \ k-1 / \k/ | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
59 ## @end group |
3889 | 60 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
61 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
62 ## @end ifnottex |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
63 ## The validity of this formula can easily be checked by expanding |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
64 ## the binomial coefficients in both formulas as factorials. It can |
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
65 ## be derived more directly via the theory of Cauchy matrices. |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10791
diff
changeset
|
66 ## See J. W. Demmel, @cite{Applied Numerical Linear Algebra}, p. 92. |
3889 | 67 ## |
68 ## Compare this with the numerical calculation of @code{inverse (hilb (n))}, | |
3369 | 69 ## which suffers from the ill-conditioning of the Hilbert matrix, and the |
70 ## finite precision of your computer's floating point arithmetic. | |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
71 ## @seealso{hilb} |
3369 | 72 ## @end deftypefn |
4 | 73 |
5132 | 74 ## Author: Dirk Laurie <dlaurie@na-net.ornl.gov> |
2314 | 75 |
2311 | 76 function retval = invhilb (n) |
4 | 77 |
78 if (nargin != 1) | |
6046 | 79 print_usage (); |
13892
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
80 elseif (! isscalar (n)) |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
81 error ("invhilb: N must be a scalar integer"); |
4 | 82 endif |
83 | |
13892
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
84 ## The point about the second formula above is that when vectorized, |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
85 ## p(k) is evaluated for k=1:n which involves O(n) calls to bincoeff |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
86 ## instead of O(n^2). |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
87 ## |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
88 ## We evaluate the expression as (-1)^(i+j)*(p(i)*p(j))/(i+j-1) except |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
89 ## when p(i)*p(j) would overflow. In cases where p(i)*p(j) is an exact |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
90 ## machine number, the result is also exact. Otherwise we calculate |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
91 ## (-1)^(i+j)*p(i)*(p(j)/(i+j-1)). |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
92 ## |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
93 ## The Octave bincoeff routine uses transcendental functions (gammaln |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
94 ## and exp) rather than multiplications, for the sake of speed. |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
95 ## However, it rounds the answer to the nearest integer, which |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
96 ## justifies the claim about exactness made above. |
3889 | 97 |
13892
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
98 retval = zeros (n); |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
99 k = [1:n]; |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
100 p = k .* bincoeff (k+n-1, k-1) .* bincoeff (n, k); |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
101 p(2:2:n) = -p(2:2:n); |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
102 if (n < 203) |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
103 for l = 1:n |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
104 retval(l,:) = (p(l) * p) ./ [l:l+n-1]; |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
105 endfor |
4 | 106 else |
13892
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
107 for l = 1:n |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
108 retval(l,:) = p(l) * (p ./ [l:l+n-1]); |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
109 endfor |
4 | 110 endif |
111 | |
112 endfunction | |
7411 | 113 |
13892
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
114 |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
115 %!assert (invhilb (1), 1) |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
116 %!assert (invhilb (2), [4, -6; -6, 12]) |
7411 | 117 %!test |
13892
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
118 %! result4 = [16 , -120 , 240 , -140; |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
119 %! -120, 1200 , -2700, 1680; |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
120 %! 240 , -2700, 6480 , -4200; |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
121 %! -140, 1680 , -4200, 2800]; |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
122 %! assert (invhilb (4), result4); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
123 %!assert (invhilb (7) * hilb (7), eye (7), sqrt (eps)) |
7411 | 124 |
13892
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
125 %!error invhilb () |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
126 %!error invhilb (1, 2) |
dd01f0bfd78d
invhilb.m: update coding style.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
127 %!error <N must be a scalar integer> invhilb ([1, 2]) |
7411 | 128 |