annotate scripts/general/chop.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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14138
72c96de7a403 maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
1 ## Copyright (C) 2010-2012 John W. Eaton
10203
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
2 ##
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
3 ## This file is part of Octave.
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
4 ##
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
6 ## under the terms of the GNU General Public License as published by
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
8 ## your option) any later version.
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
9 ##
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
13 ## General Public License for more details.
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
14 ##
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
17 ## <http://www.gnu.org/licenses/>.
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
18
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
19 ## -*- texinfo -*-
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10203
diff changeset
20 ## @deftypefn {Function File} {} chop (@var{x}, @var{ndigits}, @var{base})
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10203
diff changeset
21 ## Truncate elements of @var{x} to a length of @var{ndigits} such that the
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10203
diff changeset
22 ## resulting numbers are exactly divisible by @var{base}. If @var{base} is not
10203
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
23 ## specified it defaults to 10.
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
24 ##
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
25 ## @example
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
26 ## @group
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
27 ## chop (-pi, 5, 10)
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
28 ## @result{} -3.14200000000000
10203
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
29 ## chop (-pi, 5, 5)
14327
4d917a6a858b doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
30 ## @result{} -3.14150000000000
10203
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
31 ## @end group
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
32 ## @end example
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
33 ## @end deftypefn
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
34
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10203
diff changeset
35 function retval = chop (x, ndigits, base = 10)
10203
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
36
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
37 if (nargin == 2 || nargin == 3)
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
38 tmp = abs (x);
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
39
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
40 ## Avoid computing log (0).
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
41 tmp(x == 0) = 1;
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
42
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
43 ## Digits to the left of the decimal.
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
44 tmp = floor (log10 (tmp) + 1);
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
45
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
46 ## The expression
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
47 ##
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
48 ## round (x .* inflate)
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
49 ##
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
50 ## produces an integer that contains the digits we want to keep.
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
51 ## Multiplying by deflate puts the decimal back where it belngs.
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
52 ##
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10203
diff changeset
53 ## Further scaling and rounding with the base factor produces a
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10203
diff changeset
54 ## value with ndigits exactly divisible by base. We skip that step
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10203
diff changeset
55 ## unless base was explicitly provided.
10203
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
56
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10203
diff changeset
57 inflate = 10 .^ (ndigits - tmp);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10203
diff changeset
58 deflate = 10 .^ (tmp - ndigits);
10203
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
59 if (nargin == 2)
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
60 retval = deflate .* round (x .* inflate);
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
61 else
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10203
diff changeset
62 retval = base .* deflate .* round (round (x .* inflate) ./ base);
10203
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
63 endif
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
64 else
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
65 print_usage ();
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
66 endif
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
67
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
68 endfunction
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
69
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10203
diff changeset
70
10203
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
71 %!assert (chop (e, 3), 2.72)
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
72 %!assert (chop (e, 4), 2.718)
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
73 %!assert (chop (e, 4, 5), 2.72)
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
74 %!assert (chop (e, 4, 7), 2.716)
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
75 %!assert (chop (-e, 3), -2.72)
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
76 %!assert (chop (-e, 4), -2.718)
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
77 %!assert (chop (-e, 4, 5), -2.72)
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
78 %!assert (chop (-e, 4, 7), -2.716)
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
79 %!assert (chop (hilb (3), 3), [1,.5,.333;.5,.333,.25;.333,.25,.2])
27777bd27e71 general/chop.m: new function
John W. Eaton <jwe@octave.org>
parents:
diff changeset
80 %!assert (chop (hilb (3), 2, 7), [.7,.49,.35;.49,.35,.28;.35,.28,.21], 2*eps)