Mercurial > hg > octave-nkf
annotate scripts/special-matrix/hilb.m @ 14327:4d917a6a858b stable
doc: Use Octave coding conventions in @example blocks of docstrings.
* accumarray.m, accumdim.m, bar.m, base2dec.m, bincoeff.m, bitcmp.m, bitset.m,
celldisp.m, chop.m, clabel.m, cloglog.m, colon.m, compass.m, computer.m,
contour3.m, contourc.m, corr.m, cstrcat.m, ctime.m, cylinder.m, date.m,
dec2base.m, demo.m, dir.m, dlmwrite.m, expm.m, ezcontourf.m, ezcontour.m,
ezmeshc.m, ezmesh.m, ezplot.m, ezsurfc.m, ezsurf.m, feather.m, findobj.m,
flipdim.m, fplot.m, genvarname.m, getfield.m, hankel.m, hilb.m, hist.m,
idivide.m, index.m, int2str.m, interp1.m, is_leap_year.m, ismember.m,
isocolors.m, isonormals.m, isosurface.m, kurtosis.m, legendre.m, linkprop.m,
logit.m, logm.m, __makeinfo__.m, __marching_cube__.m, median.m, mkoctfile.m,
moment.m, mpoles.m, orderfields.m, pcg.m, pcr.m, plot3.m, plotmatrix.m,
polyaffine.m, polygcd.m, poly.m, polyout.m, print.m, qp.m, quadgk.m, qzhess.m,
randi.m, rat.m, refreshdata.m, residue.m, rose.m, rot90.m, saveas.m, saveobj.m,
shiftdim.m, skewness.m, spaugment.m, spdiags.m, sqp.m, stem.m, str2num.m,
strcat.m, strjust.m, strread.m, strsplit.m, structfun.m, subplot.m,
subsindex.m, substruct.m, surfl.m, surfnorm.m, svds.m, uimenu.m, union.m,
voronoi.m, warning_ids.m, wblpdf.m: Use Octave coding conventions in
@example blocks of docstrings.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 04 Feb 2012 22:12:50 -0800 |
parents | 72c96de7a403 |
children | f3d52523cde1 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
13872
diff
changeset
|
1 ## Copyright (C) 1993-2012 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 | |
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} {} hilb (@var{n}) | |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
21 ## Return the Hilbert matrix of order @var{n}. The @math{i,j} element |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
22 ## of a Hilbert matrix is defined as |
3369 | 23 ## @tex |
24 ## $$ | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
25 ## H(i, j) = {1 \over (i + j - 1)} |
3369 | 26 ## $$ |
27 ## @end tex | |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
28 ## @ifnottex |
3426 | 29 ## |
3369 | 30 ## @example |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
31 ## H(i, j) = 1 / (i + j - 1) |
3369 | 32 ## @end example |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
9211
diff
changeset
|
33 ## |
8517
81d6ab3ac93c
Allow documentation tobe built for other formats than tex and info
sh@sh-laptop
parents:
7411
diff
changeset
|
34 ## @end ifnottex |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
35 ## |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
36 ## Hilbert matrices are close to being singular which make them difficult to |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
37 ## invert with numerical routines. |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
38 ## Comparing the condition number of a random matrix 5x5 matrix with that of |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
39 ## a Hilbert matrix of order 5 reveals just how difficult the problem is. |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
40 ## |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
41 ## @example |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
42 ## @group |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
43 ## cond (rand (5)) |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
44 ## @result{} 14.392 |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
45 ## cond (hilb (5)) |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
46 ## @result{} 4.7661e+05 |
12639
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
47 ## @end group |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
48 ## @end example |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
49 ## |
4d777e05d47c
doc: Review and update documentation for "Matrix Manipulation" chapter.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
50 ## @seealso{invhilb} |
3369 | 51 ## @end deftypefn |
4 | 52 |
2314 | 53 ## Author: jwe |
54 | |
2311 | 55 function retval = hilb (n) |
4 | 56 |
57 if (nargin != 1) | |
6046 | 58 print_usage (); |
13872
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
59 elseif (! isscalar (n)) |
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
60 error ("hilb: N must be a scalar integer"); |
4 | 61 endif |
62 | |
13872
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
63 retval = zeros (n); |
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
64 tmp = 1:n; |
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
65 for i = 1:n |
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
66 retval(i, :) = 1.0 ./ tmp; |
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
67 tmp++; |
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
68 endfor |
4 | 69 |
70 endfunction | |
7411 | 71 |
13872
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
72 |
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
73 %!assert (hilb (2), [1, 1/2; 1/2, 1/3]) |
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
74 %!assert (hilb (3), [1, 1/2, 1/3; 1/2, 1/3, 1/4; 1/3, 1/4, 1/5]) |
7411 | 75 |
13872
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
76 %!error hilb () |
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
77 %!error hilb (1, 2) |
779e15b69738
hilb.m: 10% speedup by using in-place accumulation.
Rik <octave@nomad.inbox5.com>
parents:
12639
diff
changeset
|
78 %!error <N must be a scalar integer> hilb (ones(2)) |
7411 | 79 |