annotate scripts/general/rat.m @ 27919:1891570abac8

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2020.
author John W. Eaton <jwe@octave.org>
date Mon, 06 Jan 2020 22:29:51 -0500
parents b442ec6dda5c
children bd51beb6205e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27919
1891570abac8 update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 27918
diff changeset
1 ## Copyright (C) 2001-2020 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26600
diff changeset
2 ##
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26600
diff changeset
3 ## See the file COPYRIGHT.md in the top-level directory of this distribution
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26600
diff changeset
4 ## or <https://octave.org/COPYRIGHT.html/>.
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26600
diff changeset
5 ##
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
6 ##
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
7 ## This file is part of Octave.
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
8 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23573
diff changeset
9 ## Octave is free software: you can redistribute it and/or modify it
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
10 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23573
diff changeset
11 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
12 ## (at your option) any later version.
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
13 ##
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
14 ## Octave is distributed in the hope that it will be useful, but
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
15 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
16 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22489
diff changeset
17 ## GNU General Public License for more details.
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
18 ##
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
19 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
20 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23573
diff changeset
21 ## <https://www.gnu.org/licenses/>.
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
22
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
23 ## -*- texinfo -*-
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
24 ## @deftypefn {} {@var{s} =} rat (@var{x})
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
25 ## @deftypefnx {} {@var{s} =} rat (@var{x}, @var{tol})
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
26 ## @deftypefnx {} {[@var{n}, @var{d}] =} rat (@dots{})
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
27 ##
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
28 ## Find a rational approximation of @var{x} to within the tolerance defined by
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
29 ## @var{tol}.
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
30 ##
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
31 ## If unspecified, the default tolerance is @code{1e-6 * norm (@var{x}(:), 1)}.
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
32 ##
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
33 ## When called with one output argument, return a string containing a
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
34 ## continued fraction expansion (multiple terms).
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
35 ##
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
36 ## When called with two output arguments, return numeric matrices for the
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
37 ## numerator and denominator of a fractional representation of @var{x} such
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
38 ## that @code{@var{x} = @var{n} ./ @var{d}}.
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
39 ##
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
40 ## For example:
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
41 ##
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
42 ## @example
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
43 ## @group
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
44 ## s = rat (pi)
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
45 ## @result{} s = 3 + 1/(7 + 1/16)
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
46 ##
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
47 ## [n, d] = rat (pi)
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
48 ## @result{} n = 355
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
49 ## @result{} d = 113
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
50 ##
26600
f6730533820e doc: clean up doc example blocks for accuracy and consistent formatting
Mike Miller <mtmiller@octave.org>
parents: 26376
diff changeset
51 ## n / d - pi
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
52 ## @result{} 0.00000026676
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 9039
diff changeset
53 ## @end group
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
54 ## @end example
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
55 ##
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
56 ## Programming Note: With one output @code{rat} produces a string which is a
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
57 ## continued fraction expansion. To produce a string which is a simple
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
58 ## fraction (one numerator, one denominator) use @code{rats}.
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
59 ##
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
60 ## @seealso{rats, format}
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
61 ## @end deftypefn
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
62
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
63 function [n, d] = rat (x, tol)
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
64
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
65 if (nargin < 1 || nargin > 2)
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
66 print_usage ();
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
67 endif
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
68
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
69 if (! isfloat (x))
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
70 error ("rat: X must be a single or double array");
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
71 endif
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
72
26197
95633ec174cf rat.m: Add input validation to detect and fail on complex inputs (bug #55198)
Rik <rik@octave.org>
parents: 26176
diff changeset
73 ## FIXME: This test should be removed when complex support is added.
95633ec174cf rat.m: Add input validation to detect and fail on complex inputs (bug #55198)
Rik <rik@octave.org>
parents: 26176
diff changeset
74 ## See bug #55198.
95633ec174cf rat.m: Add input validation to detect and fail on complex inputs (bug #55198)
Rik <rik@octave.org>
parents: 26176
diff changeset
75 if (iscomplex (x))
95633ec174cf rat.m: Add input validation to detect and fail on complex inputs (bug #55198)
Rik <rik@octave.org>
parents: 26176
diff changeset
76 error ("rat: X must be a real, not complex, array");
95633ec174cf rat.m: Add input validation to detect and fail on complex inputs (bug #55198)
Rik <rik@octave.org>
parents: 26176
diff changeset
77 endif
95633ec174cf rat.m: Add input validation to detect and fail on complex inputs (bug #55198)
Rik <rik@octave.org>
parents: 26176
diff changeset
78
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
79 y = x(:);
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
80
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 7881
diff changeset
81 ## Replace Inf with 0 while calculating ratios.
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
82 inf_idx = isinf (x);
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
83 y(inf_idx(:)) = 0;
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
84
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
85 if (nargin == 1)
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
86 ## default norm
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
87 tol = 1e-6 * norm (y, 1);
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
88 else
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
89 if (! (isscalar (tol) && isnumeric (tol) && tol > 0))
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
90 error ("rat: TOL must be a numeric scalar > 0");
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
91 endif
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
92 endif
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
93
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
94 ## First step in the approximation is the integer portion
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 7881
diff changeset
95
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 7881
diff changeset
96 ## 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
97 n = round (y);
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
98 d = ones (size (y));
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
99 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
100 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
101 lastd = zeros (size (y));
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
102
6967
65a28e9de0a5 [project @ 2007-10-06 12:47:11 by jwe]
jwe
parents: 6788
diff changeset
103 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
104 steps = zeros ([nsz, 0]);
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
105
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 7881
diff changeset
106 ## Grab new factors until all continued fractions converge.
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
107 while (1)
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 7881
diff changeset
108 ## Determine which fractions have not yet converged.
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
109 idx = find (y != 0 & abs (y - n./d) >= tol);
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
110 if (isempty (idx))
7881
f74669a09deb rat.m: handle arrays and all-integer inputs
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
111 if (isempty (steps))
10549
95c3e38098bf Untabify .m scripts
Rik <code@nomad.inbox5.com>
parents: 10540
diff changeset
112 steps = NaN (nsz, 1);
7881
f74669a09deb rat.m: handle arrays and all-integer inputs
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
113 endif
f74669a09deb rat.m: handle arrays and all-integer inputs
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
114 break;
f74669a09deb rat.m: handle arrays and all-integer inputs
John W. Eaton <jwe@octave.org>
parents: 7031
diff changeset
115 endif
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
116
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 7881
diff changeset
117 ## Grab the next step in the continued fraction.
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
118 flip = 1 ./ frac(idx);
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 7881
diff changeset
119 ## 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
120 step = round (flip);
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
121
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
122 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
123 tsteps = NaN (nsz, 1);
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
124 tsteps(idx) = step;
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
125 steps = [steps, tsteps];
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
126 endif
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
127
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
128 frac(idx) = flip - step;
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
129
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 7881
diff changeset
130 ## Update the numerator/denominator.
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
131 savedn = n;
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
132 savedd = d;
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
133 n(idx) = n(idx).*step + lastn(idx);
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
134 d(idx) = d(idx).*step + lastd(idx);
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
135 lastn = savedn;
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
136 lastd = savedd;
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
137 endwhile
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
138
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
139 if (nargout <= 1)
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
140 ## string output
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
141 n = "";
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
142 nsteps = columns (steps);
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
143 ## Loop over all values in array
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
144 for i = 1:nsz
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
145
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
146 if (inf_idx(i))
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
147 s = ifelse (x(i) > 0, "Inf", "-Inf");
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
148 elseif (y(i) == 0)
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
149 s = "0";
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
150 else
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
151 ## Create partial fraction expansion of one value
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
152 s = [int2str(y(i)), " "];
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
153 j = 1;
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
154
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
155 while (true)
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
156 step = steps(i, j++);
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
157 if (isnan (step))
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
158 break;
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
159 endif
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
160 if (j > nsteps || isnan (steps(i, j)))
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
161 if (step < 0)
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
162 s = [s(1:end-1), " + 1/(", int2str(step), ")"];
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
163 else
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
164 s = [s(1:end-1), " + 1/", int2str(step)];
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
165 endif
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
166 break;
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
167 else
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
168 s = [s(1:end-1), " + 1/(", int2str(step), ")"];
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
169 endif
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
170 endwhile
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
171 s = [s, repmat(")", 1, j-2)];
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
172 endif
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
173
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
174 ## Append result to output
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
175 n_nc = columns (n);
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
176 s_nc = columns (s);
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
177 if (n_nc > s_nc)
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
178 s(:, s_nc+1:n_nc) = " ";
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
179 elseif (s_nc > n_nc && n_nc != 0)
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
180 n(:, n_nc+1:s_nc) = " ";
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
181 endif
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
182 n = cat (1, n, s);
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
183 endfor
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
184 else
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
185 ## numerator, denominator output
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
186
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
187 ## Move the minus sign to the numerator.
20231
83792dd9bcc1 Use in-place operators in m-files where possible.
Rik <rik@octave.org>
parents: 20158
diff changeset
188 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
189 d = abs (d);
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
190
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
191 ## Return the same shape as the input.
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
192 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
193 d = reshape (d, size (x));
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
194
8506
bc982528de11 comment style fixes
John W. Eaton <jwe@octave.org>
parents: 7881
diff changeset
195 ## Use 1/0 for Inf.
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
196 n(inf_idx) = sign (x(inf_idx));
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
197 d(inf_idx) = 0;
6788
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
198 endif
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
199
c81a0f3f5a82 [project @ 2007-07-23 22:05:29 by dbateman]
dbateman
parents:
diff changeset
200 endfunction
12795
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12185
diff changeset
201
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12185
diff changeset
202
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
203 %!assert (rat (pi), "3 + 1/(7 + 1/16)")
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
204 %!assert (rat (pi, 1e-2), "3 + 1/7")
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
205 ## Test exceptional values
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
206 %!assert (rat (0), "0")
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
207 %!assert (rat (Inf), "Inf")
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
208 %!assert (rat (-Inf), "-Inf")
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
209
12795
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12185
diff changeset
210 %!test
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 12185
diff changeset
211 %! [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
212 %! 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
213 %! assert (d, [2, 10, 3]);
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
214 ## Test exceptional values
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
215 %!test
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
216 %! [n, d] = rat ([Inf, 0, -Inf]);
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
217 %! assert (n, [1, 0, -1]);
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
218 %! assert (d, [0, 1, 0]);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
219
23573
1b4f4ec53b4a use new script to tag fixed bugs in tests
John W. Eaton <jwe@octave.org>
parents: 23572
diff changeset
220 %!assert <*43374> (eval (rat (0.75)), [0.75])
19283
e1713e281ac5 rat.m: Remove unnecessary blank line at start of output (bug #43374).
Markus Bergholz <markuman+octave@gmail.com>
parents: 17744
diff changeset
221
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
222 ## Test input validation
21581
6fab85c1538f maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents: 20852
diff changeset
223 %!error rat ()
6fab85c1538f maint: Follow Octave conventions for use of semicolon in BIST tests.
Rik <rik@octave.org>
parents: 20852
diff changeset
224 %!error rat (1, 2, 3)
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
225 %!error <X must be a single or double array> rat (int8 (3))
26197
95633ec174cf rat.m: Add input validation to detect and fail on complex inputs (bug #55198)
Rik <rik@octave.org>
parents: 26176
diff changeset
226 %!error <X must be a real, not complex, array> rat (1+1i)
26176
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
227 %!error <TOL must be a numeric scalar> rat (1, "a")
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
228 %!error <TOL must be a numeric scalar> rat (1, [1 2])
b5418132423f rat.m: Overhaul function.
Rik <rik@octave.org>
parents: 25054
diff changeset
229 %!error <TOL must be a numeric scalar . 0> rat (1, -1)