Mercurial > octave-nkf
comparison scripts/general/rat.m @ 7881:f74669a09deb
rat.m: handle arrays and all-integer inputs
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 11 Jun 2008 14:21:06 -0400 |
parents | 120f3135952f |
children | bc982528de11 |
comparison
equal
deleted
inserted
replaced
7880:7609a985a052 | 7881:f74669a09deb |
---|---|
62 steps = zeros([nsz, 0]); | 62 steps = zeros([nsz, 0]); |
63 | 63 |
64 ## grab new factors until all continued fractions converge | 64 ## grab new factors until all continued fractions converge |
65 while (1) | 65 while (1) |
66 ## determine which fractions have not yet converged | 66 ## determine which fractions have not yet converged |
67 idx = find (abs(y-n./d) >= tol); | 67 idx = find(abs (y-n./d) >= tol); |
68 if (isempty(idx)) break; endif | 68 if (isempty(idx)) |
69 if (isempty (steps)) | |
70 steps = NaN .* ones (nsz, 1); | |
71 endif | |
72 break; | |
73 endif | |
69 | 74 |
70 ## grab the next step in the continued fraction | 75 ## grab the next step in the continued fraction |
71 flip = 1./frac(idx); | 76 flip = 1./frac(idx); |
72 step = round(flip); # next element in the continued fraction | 77 step = round(flip); # next element in the continued fraction |
73 | 78 |
126 else | 131 else |
127 s = [s(1:end-1), " + 1/(", int2str(step), ")"]; | 132 s = [s(1:end-1), " + 1/(", int2str(step), ")"]; |
128 endif | 133 endif |
129 endwhile | 134 endwhile |
130 s = [s, repmat(")", 1, j-2)]; | 135 s = [s, repmat(")", 1, j-2)]; |
136 n_nc = columns (n); | |
137 s_nc = columns (s); | |
138 if (n_nc > s_nc) | |
139 s(:,s_nc+1:n_nc) = " " | |
140 elseif (s_nc > n_nc) | |
141 n(:,n_nc+1:s_nc) = " "; | |
142 endif | |
131 n = cat (1, n, s); | 143 n = cat (1, n, s); |
132 endfor | 144 endfor |
133 endif | 145 endif |
134 | 146 |
135 endfunction | 147 endfunction |