Mercurial > hg > octave-nkf
annotate scripts/general/rat.m @ 20441:83792dd9bcc1
Use in-place operators in m-files where possible.
* scripts/audio/@audioplayer/set.m, scripts/audio/@audiorecorder/set.m,
scripts/audio/mu2lin.m, scripts/elfun/cosd.m, scripts/general/del2.m,
scripts/general/profexplore.m, scripts/general/quadl.m, scripts/general/rat.m,
scripts/general/rotdim.m, scripts/help/get_first_help_sentence.m,
scripts/help/private/__strip_html_tags__.m, scripts/image/cubehelix.m,
scripts/io/textread.m, scripts/linear-algebra/duplication_matrix.m,
scripts/linear-algebra/housh.m, scripts/linear-algebra/krylov.m,
scripts/linear-algebra/logm.m, scripts/linear-algebra/normest.m,
scripts/linear-algebra/onenormest.m, scripts/optimization/fminsearch.m,
scripts/optimization/lsqnonneg.m, scripts/optimization/qp.m,
scripts/plot/appearance/annotation.m, scripts/plot/appearance/axis.m,
scripts/plot/appearance/legend.m, scripts/plot/appearance/specular.m,
scripts/plot/draw/colorbar.m, scripts/plot/draw/hist.m,
scripts/plot/draw/plotmatrix.m, scripts/plot/draw/private/__stem__.m,
scripts/plot/util/__actual_axis_position__.m,
scripts/plot/util/__gnuplot_drawnow__.m, scripts/plot/util/findobj.m,
scripts/plot/util/print.m, scripts/plot/util/private/__go_draw_axes__.m,
scripts/plot/util/private/__print_parse_opts__.m, scripts/plot/util/rotate.m,
scripts/polynomial/pchip.m, scripts/polynomial/polyaffine.m,
scripts/polynomial/polyder.m, scripts/polynomial/private/__splinefit__.m,
scripts/polynomial/residue.m, scripts/signal/arch_fit.m,
scripts/signal/arch_rnd.m, scripts/signal/bartlett.m,
scripts/signal/blackman.m, scripts/signal/freqz.m, scripts/signal/hamming.m,
scripts/signal/hanning.m, scripts/signal/spectral_adf.m,
scripts/signal/spectral_xdf.m, scripts/signal/stft.m,
scripts/sparse/bicgstab.m, scripts/sparse/cgs.m,
scripts/sparse/private/__sprand_impl__.m, scripts/sparse/qmr.m,
scripts/sparse/sprandsym.m, scripts/sparse/svds.m, scripts/specfun/legendre.m,
scripts/special-matrix/gallery.m, scripts/statistics/base/gls.m,
scripts/statistics/models/logistic_regression.m,
scripts/statistics/tests/kruskal_wallis_test.m,
scripts/statistics/tests/manova.m, scripts/statistics/tests/wilcoxon_test.m,
scripts/time/datevec.m:
Use in-place operators in m-files where possible.
author | Rik <rik@octave.org> |
---|---|
date | Tue, 26 May 2015 21:07:42 -0700 |
parents | 7503499a252b |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19475
diff
changeset
|
1 ## Copyright (C) 2001-2015 Paul Kienzle |
6788 | 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. | |
6788 | 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/>. | |
6788 | 18 |
19 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {@var{s} =} rat (@var{x}, @var{tol}) |
6788 | 21 ## @deftypefnx {Function File} {[@var{n}, @var{d}] =} rat (@var{x}, @var{tol}) |
22 ## | |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
23 ## Find a rational approximation to @var{x} within the tolerance defined by |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
24 ## @var{tol} using a continued fraction expansion. |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
25 ## |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
26 ## For example: |
6788 | 27 ## |
28 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
29 ## @group |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
30 ## rat (pi) = 3 + 1/(7 + 1/16) = 355/113 |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
31 ## rat (e) = 3 + 1/(-4 + 1/(2 + 1/(5 + 1/(-2 + 1/(-7))))) |
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
32 ## = 1457/536 |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
9039
diff
changeset
|
33 ## @end group |
6788 | 34 ## @end example |
35 ## | |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
36 ## When called with two output arguments return the numerator and denominator |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
37 ## separately as two matrices. |
12185
862a8e490445
doc fix for rational approximation functions
John W. Eaton <jwe@octave.org>
parents:
11589
diff
changeset
|
38 ## @seealso{rats} |
6788 | 39 ## @end deftypefn |
40 | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
41 function [n,d] = rat (x,tol) |
6788 | 42 |
43 if (nargin != [1,2] || nargout > 2) | |
44 print_usage (); | |
45 endif | |
46 | |
47 y = x(:); | |
48 | |
8506 | 49 ## Replace Inf with 0 while calculating ratios. |
6788 | 50 y(isinf(y)) = 0; |
51 | |
52 ## default norm | |
53 if (nargin < 2) | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
54 tol = 1e-6 * norm (y,1); |
6788 | 55 endif |
56 | |
57 ## First step in the approximation is the integer portion | |
8506 | 58 |
59 ## First element in the continued fraction. | |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
60 n = round (y); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
61 d = ones (size (y)); |
6788 | 62 frac = y-n; |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
63 lastn = ones (size (y)); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
64 lastd = zeros (size (y)); |
6788 | 65 |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
66 nd = ndims (y); |
6967 | 67 nsz = numel (y); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
68 steps = zeros ([nsz, 0]); |
6788 | 69 |
8506 | 70 ## Grab new factors until all continued fractions converge. |
6788 | 71 while (1) |
8506 | 72 ## Determine which fractions have not yet converged. |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
73 idx = find (abs (y-n./d) >= tol); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
74 if (isempty (idx)) |
7881
f74669a09deb
rat.m: handle arrays and all-integer inputs
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
75 if (isempty (steps)) |
10549 | 76 steps = NaN (nsz, 1); |
7881
f74669a09deb
rat.m: handle arrays and all-integer inputs
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
77 endif |
f74669a09deb
rat.m: handle arrays and all-integer inputs
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
78 break; |
f74669a09deb
rat.m: handle arrays and all-integer inputs
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
79 endif |
6788 | 80 |
8506 | 81 ## Grab the next step in the continued fraction. |
6788 | 82 flip = 1./frac(idx); |
8506 | 83 ## Next element in the continued fraction. |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
84 step = round (flip); |
6788 | 85 |
86 if (nargout < 2) | |
10540
952d4df5b686
Eliminate NaN*ones and Inf*ones constructs and just use Nan() and Inf()
Rik <code@nomad.inbox5.com>
parents:
9051
diff
changeset
|
87 tsteps = NaN (nsz, 1); |
6788 | 88 tsteps (idx) = step; |
89 steps = [steps, tsteps]; | |
90 endif | |
91 | |
92 frac(idx) = flip-step; | |
93 | |
8506 | 94 ## Update the numerator/denominator. |
6788 | 95 nextn = n; |
96 nextd = d; | |
97 n(idx) = n(idx).*step + lastn(idx); | |
98 d(idx) = d(idx).*step + lastd(idx); | |
99 lastn = nextn; | |
100 lastd = nextd; | |
101 endwhile | |
102 | |
103 if (nargout == 2) | |
8506 | 104 ## Move the minus sign to the top. |
20441
83792dd9bcc1
Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents:
20368
diff
changeset
|
105 n .*= sign (d); |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
106 d = abs (d); |
6788 | 107 |
8506 | 108 ## Return the same shape as you receive. |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
109 n = reshape (n, size (x)); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
110 d = reshape (d, size (x)); |
6788 | 111 |
8506 | 112 ## Use 1/0 for Inf. |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
113 n(isinf (x)) = sign (x(isinf (x))); |
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
114 d(isinf (x)) = 0; |
6788 | 115 |
8506 | 116 ## Reshape the output. |
6788 | 117 n = reshape (n, size (x)); |
118 d = reshape (d, size (x)); | |
119 else | |
120 n = ""; | |
14872
c2dbdeaa25df
maint: use rows() and columns() to clarify m-files.
Rik <octave@nomad.inbox5.com>
parents:
14868
diff
changeset
|
121 nsteps = columns (steps); |
6788 | 122 for i = 1: nsz |
123 s = [int2str(y(i))," "]; | |
124 j = 1; | |
125 | |
126 while (true) | |
10549 | 127 step = steps(i, j++); |
128 if (isnan (step)) | |
129 break; | |
130 endif | |
131 if (j > nsteps || isnan (steps(i, j))) | |
132 if (step < 0) | |
133 s = [s(1:end-1), " + 1/(", int2str(step), ")"]; | |
134 else | |
135 s = [s(1:end-1), " + 1/", int2str(step)]; | |
136 endif | |
137 break; | |
138 else | |
139 s = [s(1:end-1), " + 1/(", int2str(step), ")"]; | |
6788 | 140 endif |
141 endwhile | |
142 s = [s, repmat(")", 1, j-2)]; | |
7881
f74669a09deb
rat.m: handle arrays and all-integer inputs
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
143 n_nc = columns (n); |
f74669a09deb
rat.m: handle arrays and all-integer inputs
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
144 s_nc = columns (s); |
f74669a09deb
rat.m: handle arrays and all-integer inputs
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
145 if (n_nc > s_nc) |
11589
b0084095098e
missing semicolons in script files
John W. Eaton <jwe@octave.org>
parents:
11587
diff
changeset
|
146 s(:,s_nc+1:n_nc) = " "; |
19475
e1713e281ac5
rat.m: Remove unnecessary blank line at start of output (bug #43374).
Markus Bergholz <markuman+octave@gmail.com>
parents:
17744
diff
changeset
|
147 elseif (s_nc > n_nc && n_nc != 0) |
10549 | 148 n(:,n_nc+1:s_nc) = " "; |
7881
f74669a09deb
rat.m: handle arrays and all-integer inputs
John W. Eaton <jwe@octave.org>
parents:
7031
diff
changeset
|
149 endif |
6788 | 150 n = cat (1, n, s); |
151 endfor | |
152 endif | |
153 | |
154 endfunction | |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12185
diff
changeset
|
155 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12185
diff
changeset
|
156 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12185
diff
changeset
|
157 %!test |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12185
diff
changeset
|
158 %! [n, d] = rat ([0.5, 0.3, 1/3]); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12185
diff
changeset
|
159 %! assert (n, [1, 3, 1]); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
12185
diff
changeset
|
160 %! assert (d, [2, 10, 3]); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
161 |
19475
e1713e281ac5
rat.m: Remove unnecessary blank line at start of output (bug #43374).
Markus Bergholz <markuman+octave@gmail.com>
parents:
17744
diff
changeset
|
162 ## bug #43374 |
e1713e281ac5
rat.m: Remove unnecessary blank line at start of output (bug #43374).
Markus Bergholz <markuman+octave@gmail.com>
parents:
17744
diff
changeset
|
163 %!assert (eval (rat (0.75)), [0.75]) |
e1713e281ac5
rat.m: Remove unnecessary blank line at start of output (bug #43374).
Markus Bergholz <markuman+octave@gmail.com>
parents:
17744
diff
changeset
|
164 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
165 %!error rat (); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
166 %!error rat (1, 2, 3); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
167 |