Mercurial > hg > octave-nkf
annotate scripts/strings/dec2base.m @ 20815:a260a6acb70f
fix test failures introduced by a22d8a2eb0e5
* scripts/ode/private/integrate_adaptive.m: fix stepping backwards, fix
invocation of OutputFcn, fix text of some error messages
* scripts/ode/private/integrate_const.m: remove use of option OutputSave
* scripts/ode/private/integrate_n_steps.m: remove use of option OutputSave
author | Carlo de Falco <carlo.defalco@polimi.it> |
---|---|
date | Sun, 11 Oct 2015 23:09:01 +0200 |
parents | df437a52bcaf |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
19790
diff
changeset
|
1 ## Copyright (C) 2000-2015 Daniel Calvelo |
3789 | 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. | |
3789 | 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/>. | |
3789 | 18 |
19 ## -*- texinfo -*- | |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11172
diff
changeset
|
20 ## @deftypefn {Function File} {} dec2base (@var{d}, @var{base}) |
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11172
diff
changeset
|
21 ## @deftypefnx {Function File} {} dec2base (@var{d}, @var{base}, @var{len}) |
20374
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
22 ## Return a string of symbols in base @var{base} corresponding to the |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
23 ## non-negative integer @var{d}. |
3789 | 24 ## |
25 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
26 ## @group |
3789 | 27 ## dec2base (123, 3) |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
28 ## @result{} "11120" |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
29 ## @end group |
3789 | 30 ## @end example |
31 ## | |
20374
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
32 ## If @var{d} is a matrix or cell array, return a string matrix with one row |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
33 ## per element in @var{d}, padded with leading zeros to the width of the |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
34 ## largest value. |
3789 | 35 ## |
11469
c776f063fefe
Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents:
11172
diff
changeset
|
36 ## If @var{base} is a string then the characters of @var{base} are used as |
20374
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
37 ## the symbols for the digits of @var{d}. Space (' ') may not be used as a |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
38 ## symbol. |
3789 | 39 ## |
40 ## @example | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
41 ## @group |
3789 | 42 ## dec2base (123, "aei") |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
43 ## @result{} "eeeia" |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
44 ## @end group |
3789 | 45 ## @end example |
4492 | 46 ## |
20374
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
47 ## The optional third argument, @var{len}, specifies the minimum number of |
df437a52bcaf
doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
48 ## digits in the result. |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
11169
diff
changeset
|
49 ## @seealso{base2dec, dec2bin, dec2hex} |
3789 | 50 ## @end deftypefn |
51 | |
3791 | 52 ## Author: Daniel Calvelo <dcalvelo@yahoo.com> |
3789 | 53 ## Adapted-by: Paul Kienzle <pkienzle@kienzle.powernet.co.uk> |
54 | |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
55 function retval = dec2base (d, base, len) |
3789 | 56 |
4492 | 57 if (nargin < 2 || nargin > 3) |
6046 | 58 print_usage (); |
3789 | 59 endif |
60 | |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
61 if (iscell (d)) |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
62 d = cell2mat (d); |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
63 endif |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
64 |
17336
b81b9d079515
Use '##' for comments which stand alone on a line.
Rik <rik@octave.org>
parents:
16773
diff
changeset
|
65 ## Create column vector for algorithm |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
66 if (! iscolumn (d)) |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
67 d = d(:); |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
68 endif |
19790
446c46af4b42
strip trailing whitespace from most source files
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
69 |
16773
3542e106c496
dec2base.m: treat logical as double for compatibility with ML (bug #38815).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
70 ## Treat logical as numeric for compatibility with ML |
3542e106c496
dec2base.m: treat logical as double for compatibility with ML (bug #38815).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
71 if (islogical (d)) |
3542e106c496
dec2base.m: treat logical as double for compatibility with ML (bug #38815).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
72 d = double (d); |
3542e106c496
dec2base.m: treat logical as double for compatibility with ML (bug #38815).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
73 elseif (! isnumeric (d) || iscomplex (d) || any (d < 0 | d != fix (d))) |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
74 error ("dec2base: input must be real non-negative integers"); |
3789 | 75 endif |
76 | |
77 symbols = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
5443 | 78 if (ischar (base)) |
3789 | 79 symbols = base; |
80 base = length (symbols); | |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
81 if (length (unique (symbols)) != base) |
5407 | 82 error ("dec2base: symbols representing digits must be unique"); |
3789 | 83 endif |
11172
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
11169
diff
changeset
|
84 if (any (isspace (symbols))) |
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
11169
diff
changeset
|
85 error ("dec2base: whitespace characters are not valid symbols"); |
7e8ce65f73cf
Overhaul functions used to convert between number bases.
Rik <octave@nomad.inbox5.com>
parents:
11169
diff
changeset
|
86 endif |
4030 | 87 elseif (! isscalar (base)) |
5407 | 88 error ("dec2base: cannot convert from several bases at once"); |
3789 | 89 elseif (base < 2 || base > length (symbols)) |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
90 error ("dec2base: BASE must be between 2 and 36, or a string of symbols"); |
3789 | 91 endif |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
92 |
5125 | 93 ## determine number of digits required to handle all numbers, can overflow |
94 ## by 1 digit | |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
95 max_len = round (log (max (max (d(:)), 1)) / log (base)) + 1; |
4492 | 96 |
97 if (nargin == 3) | |
98 max_len = max (max_len, len); | |
99 endif | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
100 |
3789 | 101 ## determine digits for each number |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
102 digits = zeros (length (d), max_len); |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
103 for k = max_len:-1:1 |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
104 digits(:,k) = mod (d, base); |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
105 d = round ((d - digits(:,k)) / base); |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
106 endfor |
3789 | 107 |
108 ## convert digits to symbols | |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
109 retval = reshape (symbols(digits+1), size (digits)); |
3789 | 110 |
5133 | 111 ## Check if the first element is the zero symbol. It seems possible |
112 ## that LEN is provided, and is less than the computed MAX_LEN and | |
113 ## MAX_LEN is computed to be one larger than necessary, so we would | |
114 ## have a leading zero to remove. But if LEN >= MAX_LEN, we should | |
115 ## not remove any leading zeros. | |
116 if ((nargin == 2 || (nargin == 3 && max_len > len)) | |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
117 && length (retval) != 1 && ! any (retval(:,1) != symbols(1))) |
5125 | 118 retval = retval(:,2:end); |
119 endif | |
120 | |
3789 | 121 endfunction |
5125 | 122 |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
123 |
5125 | 124 %!test |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
125 %! s0 = ""; |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
126 %! for n = 1:13 |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
127 %! for b = 2:16 |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
128 %! pp = dec2base (b^n+1, b); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
129 %! assert (dec2base (b^n, b), ['1',s0,'0']); |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
130 %! assert (dec2base (b^n+1, b), ['1',s0,'1']); |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
131 %! endfor |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
132 %! s0 = [s0,'0']; |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
133 %! endfor |
5125 | 134 |
135 %!test | |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
136 %! digits = "0123456789ABCDEF"; |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
137 %! for n = 1:13 |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
138 %! for b = 2:16 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
139 %! pm = dec2base (b^n-1, b); |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
140 %! assert (length (pm), n); |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
141 %! assert (all (pm == digits(b))); |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
142 %! endfor |
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
143 %! endfor |
5125 | 144 |
145 %!test | |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
146 %! for b = 2:16 |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
147 %! assert (dec2base (0, b), '0'); |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
148 %! endfor |
5125 | 149 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
150 %!assert (dec2base (0, 2, 4), "0000") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
151 %!assert (dec2base (2^51-1, 2), ... |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
152 %! "111111111111111111111111111111111111111111111111111") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
153 %!assert (dec2base (uint64 (2)^63-1, 16), "7FFFFFFFFFFFFFFF") |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
154 %!assert (dec2base ([1, 2; 3, 4], 2, 3), ["001"; "011"; "010"; "100"]) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
155 %!assert (dec2base ({1, 2; 3, 4}, 2, 3), ["001"; "011"; "010"; "100"]) |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
156 |
16773
3542e106c496
dec2base.m: treat logical as double for compatibility with ML (bug #38815).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
157 %!test |
3542e106c496
dec2base.m: treat logical as double for compatibility with ML (bug #38815).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
158 %! a = 0:3; |
3542e106c496
dec2base.m: treat logical as double for compatibility with ML (bug #38815).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
159 %! assert (dec2base (!a, 2, 1), ["1"; "0"; "0"; "0"]) |
3542e106c496
dec2base.m: treat logical as double for compatibility with ML (bug #38815).
Pantxo Diribarne <pantxo.diribarne@gmail.com>
parents:
14868
diff
changeset
|
160 |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
161 ## Test input validation |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
162 %!error dec2base () |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
163 %!error dec2base (1) |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
164 %!error dec2base (1, 2, 3, 4) |
13167
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
165 %!error dec2base ("A") |
f7cb824dc8c0
Allow cellstr inputs for dec2* conversion functions.
Rik <octave@nomad.inbox5.com>
parents:
11587
diff
changeset
|
166 %!error dec2base (2i) |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
167 %!error dec2base (-1) |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
168 %!error dec2base (1.1) |
13309
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
169 %!error dec2base (1, "ABA") |
1f075333d9e8
dec2base.m: Code tweaks for slightly better performance.
Rik <octave@nomad.inbox5.com>
parents:
13167
diff
changeset
|
170 %!error dec2base (1, "A B") |
14868
5d3a684236b0
maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents:
14363
diff
changeset
|
171 %!error dec2base (1, ones (2)) |
11169
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
172 %!error dec2base (1, 1) |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
173 %!error dec2base (1, 37) |
51beeeff9027
Update dec2base to handle numbers up to 2^64-1.
Rik <octave@nomad.inbox5.com>
parents:
9844
diff
changeset
|
174 |