Mercurial > hg > octave-nkf
annotate liboctave/Range.cc @ 14026:3781981be535 ss-3-5-90
snapshot 3.5.90
* configure.ac (AC_INIT): Version is now 3.5.90.
(OCTAVE_API_VERSION_NUMBER): Now 46.
(OCTAVE_RELEASE_DATE): Now 2011-12-11.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sun, 11 Dec 2011 23:18:31 -0500 |
parents | 731a0b589cab |
children | 72c96de7a403 |
rev | line source |
---|---|
3 | 1 /* |
2 | |
11523 | 3 Copyright (C) 1993-2011 John W. Eaton |
3 | 4 |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
3 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
3 | 20 |
21 */ | |
22 | |
238 | 23 #ifdef HAVE_CONFIG_H |
1192 | 24 #include <config.h> |
3 | 25 #endif |
26 | |
1546 | 27 #include <cfloat> |
1367 | 28 |
3503 | 29 #include <iostream> |
6490 | 30 #include <limits> |
3 | 31 |
32 #include "Range.h" | |
7458 | 33 #include "lo-error.h" |
2383 | 34 #include "lo-mappers.h" |
7231 | 35 #include "lo-math.h" |
2383 | 36 #include "lo-utils.h" |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
37 #include "Array-util.h" |
2383 | 38 |
8971 | 39 Range::Range (double b, double i, octave_idx_type n) |
12326
731a0b589cab
Range.cc (Range::Range (double, double, octave_idx_type)): correctly compute limit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
40 : rng_base (b), rng_limit (b + (n-1) * i), rng_inc (i), |
731a0b589cab
Range.cc (Range::Range (double, double, octave_idx_type)): correctly compute limit
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
41 rng_nelem (n), cache () |
8971 | 42 { |
43 if (! xfinite (b) || ! xfinite (i)) | |
44 rng_nelem = -2; | |
45 } | |
46 | |
2383 | 47 bool |
48 Range::all_elements_are_ints (void) const | |
49 { | |
50 // If the base and increment are ints, the final value in the range | |
6457 | 51 // will also be an integer, even if the limit is not. If there is one |
52 // or fewer elements only the base needs to be an integer | |
2383 | 53 |
54 return (! (xisnan (rng_base) || xisnan (rng_inc)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
55 && (NINTbig (rng_base) == rng_base || rng_nelem < 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
56 && (NINTbig (rng_inc) == rng_inc || rng_nelem <= 1)); |
2383 | 57 } |
645 | 58 |
59 Matrix | |
60 Range::matrix_value (void) const | |
61 { | |
8971 | 62 if (rng_nelem > 0 && cache.nelem () == 0) |
645 | 63 { |
4810 | 64 cache.resize (1, rng_nelem); |
645 | 65 double b = rng_base; |
66 double increment = rng_inc; | |
5275 | 67 for (octave_idx_type i = 0; i < rng_nelem; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
68 cache(i) = b + i * increment; |
4791 | 69 |
70 // On some machines (x86 with extended precision floating point | |
71 // arithmetic, for example) it is possible that we can overshoot | |
72 // the limit by approximately the machine precision even though | |
73 // we were very careful in our calculation of the number of | |
74 // elements. | |
75 | |
4810 | 76 if ((rng_inc > 0 && cache(rng_nelem-1) > rng_limit) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
77 || (rng_inc < 0 && cache(rng_nelem-1) < rng_limit)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
78 cache(rng_nelem-1) = rng_limit; |
645 | 79 } |
80 | |
4810 | 81 return cache; |
645 | 82 } |
3 | 83 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
84 double |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
85 Range::checkelem (octave_idx_type i) const |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
86 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
87 if (i < 0 || i >= rng_nelem) |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
88 gripe_index_out_of_range (1, 1, i+1, rng_nelem); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
89 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
90 return rng_base + rng_inc * i; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
91 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
92 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
93 struct _rangeidx_helper |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
94 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
95 double *array, base, inc; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
96 _rangeidx_helper (double *a, double b, double i) |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
97 : array (a), base (b), inc (i) { } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
98 void operator () (octave_idx_type i) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
99 { *array++ = base + i * inc; } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
100 }; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
101 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
102 Array<double> |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
103 Range::index (const idx_vector& i) const |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
104 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
105 Array<double> retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
106 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
107 octave_idx_type n = rng_nelem; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
108 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
109 if (i.is_colon ()) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
110 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
111 retval = matrix_value ().reshape (dim_vector (rng_nelem, 1)); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
112 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
113 else |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
114 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
115 if (i.extent (n) != n) |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
116 gripe_index_out_of_range (1, 1, i.extent (n), n); // throws |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
117 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
118 dim_vector rd = i.orig_dimensions (); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
119 octave_idx_type il = i.length (n); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
120 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
121 // taken from Array.cc. |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
122 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
123 if (n != 1 && rd.is_vector ()) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
124 rd = dim_vector (1, il); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
125 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
126 retval.clear (rd); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
127 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
128 i.loop (n, _rangeidx_helper (retval.fortran_vec (), rng_base, rng_inc)); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
129 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
130 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
131 return retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
132 } |
4810 | 133 |
208 | 134 // NOTE: max and min only return useful values if nelem > 0. |
135 | |
136 double | |
137 Range::min (void) const | |
138 { | |
139 double retval = 0.0; | |
140 if (rng_nelem > 0) | |
141 { | |
142 if (rng_inc > 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
143 retval = rng_base; |
208 | 144 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
145 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
146 retval = rng_base + (rng_nelem - 1) * rng_inc; |
4791 | 147 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
148 // See the note in the matrix_value method above. |
4791 | 149 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
150 if (retval < rng_limit) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
151 retval = rng_limit; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
152 } |
4791 | 153 |
208 | 154 } |
155 return retval; | |
156 } | |
157 | |
158 double | |
159 Range::max (void) const | |
160 { | |
161 double retval = 0.0; | |
162 if (rng_nelem > 0) | |
163 { | |
164 if (rng_inc > 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
165 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
166 retval = rng_base + (rng_nelem - 1) * rng_inc; |
4791 | 167 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
168 // See the note in the matrix_value method above. |
4791 | 169 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
170 if (retval > rng_limit) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
171 retval = rng_limit; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
172 } |
208 | 173 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
174 retval = rng_base; |
208 | 175 } |
176 return retval; | |
177 } | |
178 | |
179 void | |
7458 | 180 Range::sort_internal (bool ascending) |
208 | 181 { |
7458 | 182 if (ascending && rng_base > rng_limit && rng_inc < 0.0) |
208 | 183 { |
184 double tmp = rng_base; | |
185 rng_base = min (); | |
186 rng_limit = tmp; | |
187 rng_inc = -rng_inc; | |
4811 | 188 clear_cache (); |
208 | 189 } |
8565 | 190 else if (! ascending && rng_base < rng_limit && rng_inc > 0.0) |
7458 | 191 { |
192 double tmp = rng_limit; | |
193 rng_limit = min (); | |
194 rng_base = tmp; | |
195 rng_inc = -rng_inc; | |
196 clear_cache (); | |
197 } | |
198 } | |
199 | |
200 void | |
201 Range::sort_internal (Array<octave_idx_type>& sidx, bool ascending) | |
202 { | |
203 octave_idx_type nel = nelem (); | |
204 | |
205 sidx.resize (dim_vector (1, nel)); | |
206 | |
207 octave_idx_type *psidx = sidx.fortran_vec (); | |
208 | |
209 bool reverse = false; | |
210 | |
211 if (ascending && rng_base > rng_limit && rng_inc < 0.0) | |
212 { | |
213 double tmp = rng_base; | |
214 rng_base = min (); | |
215 rng_limit = tmp; | |
216 rng_inc = -rng_inc; | |
217 clear_cache (); | |
218 reverse = true; | |
219 } | |
220 else if (! ascending && rng_base < rng_limit && rng_inc > 0.0) | |
221 { | |
222 double tmp = rng_limit; | |
223 rng_limit = min (); | |
224 rng_base = tmp; | |
225 rng_inc = -rng_inc; | |
226 clear_cache (); | |
227 reverse = true; | |
228 } | |
229 | |
230 octave_idx_type tmp = reverse ? nel - 1 : 0; | |
7467 | 231 octave_idx_type stp = reverse ? -1 : 1; |
7458 | 232 |
7467 | 233 for (octave_idx_type i = 0; i < nel; i++, tmp += stp) |
7458 | 234 psidx[i] = tmp; |
235 | |
236 } | |
237 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
238 Matrix |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
239 Range::diag (octave_idx_type k) const |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
240 { |
8971 | 241 return matrix_value ().diag (k); |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
242 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
243 |
7458 | 244 Range |
245 Range::sort (octave_idx_type dim, sortmode mode) const | |
246 { | |
247 Range retval = *this; | |
248 | |
249 if (dim == 1) | |
250 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
251 if (mode == ASCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
252 retval.sort_internal (true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
253 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
254 retval.sort_internal (false); |
7458 | 255 } |
256 else if (dim != 0) | |
257 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
258 | |
259 return retval; | |
260 } | |
261 | |
262 Range | |
263 Range::sort (Array<octave_idx_type>& sidx, octave_idx_type dim, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
264 sortmode mode) const |
7458 | 265 { |
266 Range retval = *this; | |
267 | |
268 if (dim == 1) | |
269 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
270 if (mode == ASCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
271 retval.sort_internal (sidx, true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
272 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
273 retval.sort_internal (sidx, false); |
7458 | 274 } |
275 else if (dim != 0) | |
276 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
277 | |
278 return retval; | |
208 | 279 } |
280 | |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
281 sortmode |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
282 Range::is_sorted (sortmode mode) const |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
283 { |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
284 if (rng_nelem > 1 && rng_inc < 0) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
285 mode = (mode == ASCENDING) ? UNSORTED : DESCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
286 else if (rng_nelem > 1 && rng_inc > 0) |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
287 mode = (mode == DESCENDING) ? UNSORTED : ASCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
288 else |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
289 mode = mode ? mode : ASCENDING; |
8742
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
290 |
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
291 return mode; |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
292 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
293 |
3504 | 294 std::ostream& |
295 operator << (std::ostream& os, const Range& a) | |
3 | 296 { |
297 double b = a.base (); | |
298 double increment = a.inc (); | |
5275 | 299 octave_idx_type num_elem = a.nelem (); |
3 | 300 |
5275 | 301 for (octave_idx_type i = 0; i < num_elem-1; i++) |
3 | 302 os << b + i * increment << " "; |
303 | |
4791 | 304 // Prevent overshoot. See comment in the matrix_value method |
305 // above. | |
306 | |
307 os << (increment > 0 ? a.max () : a.min ()) << "\n"; | |
3 | 308 |
309 return os; | |
310 } | |
311 | |
3504 | 312 std::istream& |
313 operator >> (std::istream& is, Range& a) | |
3 | 314 { |
208 | 315 is >> a.rng_base; |
3 | 316 if (is) |
317 { | |
208 | 318 is >> a.rng_limit; |
3 | 319 if (is) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
320 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
321 is >> a.rng_inc; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
322 a.rng_nelem = a.nelem_internal (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
323 } |
3 | 324 } |
325 | |
326 return is; | |
327 } | |
328 | |
2599 | 329 Range |
330 operator - (const Range& r) | |
331 { | |
8589
0131fa223dbc
make length invariant in range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
8565
diff
changeset
|
332 return Range (-r.base (), -r.inc (), r.nelem ()); |
2599 | 333 } |
334 | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
335 Range operator + (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
336 { |
8971 | 337 Range result (x + r.base (), r.inc (), r.nelem ()); |
338 if (result.rng_nelem < 0) | |
339 result.cache = x + r.matrix_value (); | |
340 | |
341 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
342 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
343 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
344 Range operator + (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
345 { |
8971 | 346 Range result (r.base () + x, r.inc (), r.nelem ()); |
347 if (result.rng_nelem < 0) | |
348 result.cache = r.matrix_value () + x; | |
349 | |
350 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
351 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
352 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
353 Range operator - (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
354 { |
8971 | 355 Range result (x - r.base (), -r.inc (), r.nelem ()); |
356 if (result.rng_nelem < 0) | |
357 result.cache = x - r.matrix_value (); | |
358 | |
359 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
360 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
361 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
362 Range operator - (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
363 { |
8971 | 364 Range result (r.base () - x, r.inc (), r.nelem ()); |
365 if (result.rng_nelem < 0) | |
366 result.cache = r.matrix_value () - x; | |
367 | |
368 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
369 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
370 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
371 Range operator * (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
372 { |
8971 | 373 Range result (x * r.base (), x * r.inc (), r.nelem ()); |
374 if (result.rng_nelem < 0) | |
375 result.cache = x * r.matrix_value (); | |
376 | |
377 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
378 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
379 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
380 Range operator * (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
381 { |
8971 | 382 Range result (r.base () * x, r.inc () * x, r.nelem ()); |
383 if (result.rng_nelem < 0) | |
384 result.cache = r.matrix_value () * x; | |
385 | |
386 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
387 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
388 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
389 |
1546 | 390 // C See Knuth, Art Of Computer Programming, Vol. 1, Problem 1.2.4-5. |
391 // C | |
392 // C===Tolerant FLOOR function. | |
393 // C | |
394 // C X - is given as a Double Precision argument to be operated on. | |
395 // C It is assumed that X is represented with M mantissa bits. | |
396 // C CT - is given as a Comparison Tolerance such that | |
397 // C 0.LT.CT.LE.3-SQRT(5)/2. If the relative difference between | |
398 // C X and A whole number is less than CT, then TFLOOR is | |
399 // C returned as this whole number. By treating the | |
400 // C floating-point numbers as a finite ordered set note that | |
401 // C the heuristic EPS=2.**(-(M-1)) and CT=3*EPS causes | |
402 // C arguments of TFLOOR/TCEIL to be treated as whole numbers | |
403 // C if they are exactly whole numbers or are immediately | |
404 // C adjacent to whole number representations. Since EPS, the | |
405 // C "distance" between floating-point numbers on the unit | |
406 // C interval, and M, the number of bits in X'S mantissa, exist | |
407 // C on every floating-point computer, TFLOOR/TCEIL are | |
408 // C consistently definable on every floating-point computer. | |
409 // C | |
410 // C For more information see the following references: | |
411 // C (1) P. E. Hagerty, "More On Fuzzy Floor And Ceiling," APL QUOTE | |
412 // C QUAD 8(4):20-24, June 1978. Note that TFLOOR=FL5. | |
413 // C (2) L. M. Breed, "Definitions For Fuzzy Floor And Ceiling", APL | |
414 // C QUOTE QUAD 8(3):16-23, March 1978. This paper cites FL1 through | |
415 // C FL5, the history of five years of evolutionary development of | |
416 // C FL5 - the seven lines of code below - by open collaboration | |
417 // C and corroboration of the mathematical-computing community. | |
418 // C | |
419 // C Penn State University Center for Academic Computing | |
420 // C H. D. Knoble - August, 1978. | |
421 | |
422 static inline double | |
423 tfloor (double x, double ct) | |
424 { | |
425 // C---------FLOOR(X) is the largest integer algebraically less than | |
426 // C or equal to X; that is, the unfuzzy FLOOR function. | |
427 | |
428 // DINT (X) = X - DMOD (X, 1.0); | |
429 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0); | |
430 | |
431 // C---------Hagerty's FL5 function follows... | |
432 | |
433 double q = 1.0; | |
434 | |
435 if (x < 0.0) | |
436 q = 1.0 - ct; | |
437 | |
438 double rmax = q / (2.0 - ct); | |
439 | |
11450
5eb10763069f
substitute and use LAPACK_LIBS in mkoctfile script
John W. Eaton <jwe@octave.org>
parents:
10366
diff
changeset
|
440 double t1 = 1.0 + gnulib::floor (x); |
1546 | 441 t1 = (ct / q) * (t1 < 0.0 ? -t1 : t1); |
442 t1 = rmax < t1 ? rmax : t1; | |
443 t1 = ct > t1 ? ct : t1; | |
11450
5eb10763069f
substitute and use LAPACK_LIBS in mkoctfile script
John W. Eaton <jwe@octave.org>
parents:
10366
diff
changeset
|
444 t1 = gnulib::floor (x + t1); |
1546 | 445 |
1555 | 446 if (x <= 0.0 || (t1 - x) < rmax) |
1546 | 447 return t1; |
448 else | |
449 return t1 - 1.0; | |
450 } | |
451 | |
452 static inline double | |
453 tceil (double x, double ct) | |
454 { | |
455 return -tfloor (-x, ct); | |
456 } | |
457 | |
3753 | 458 static inline bool |
459 teq (double u, double v, double ct = 3.0 * DBL_EPSILON) | |
460 { | |
461 double tu = fabs (u); | |
462 double tv = fabs (v); | |
463 | |
464 return fabs (u - v) < ((tu > tv ? tu : tv) * ct); | |
465 } | |
466 | |
5275 | 467 octave_idx_type |
1360 | 468 Range::nelem_internal (void) const |
469 { | |
5275 | 470 octave_idx_type retval = -1; |
3 | 471 |
3858 | 472 if (rng_inc == 0 |
473 || (rng_limit > rng_base && rng_inc < 0) | |
474 || (rng_limit < rng_base && rng_inc > 0)) | |
475 { | |
476 retval = 0; | |
477 } | |
478 else | |
479 { | |
480 double ct = 3.0 * DBL_EPSILON; | |
3 | 481 |
3858 | 482 double tmp = tfloor ((rng_limit - rng_base + rng_inc) / rng_inc, ct); |
483 | |
5275 | 484 octave_idx_type n_elt = (tmp > 0.0 ? static_cast<octave_idx_type> (tmp) : 0); |
398 | 485 |
3858 | 486 // If the final element that we would compute for the range is |
487 // equal to the limit of the range, or is an adjacent floating | |
488 // point number, accept it. Otherwise, try a range with one | |
489 // fewer element. If that fails, try again with one more | |
490 // element. | |
491 // | |
492 // I'm not sure this is very good, but it seems to work better than | |
493 // just using tfloor as above. For example, without it, the | |
494 // expression 1.8:0.05:1.9 fails to produce the expected result of | |
495 // [1.8, 1.85, 1.9]. | |
3753 | 496 |
3858 | 497 if (! teq (rng_base + (n_elt - 1) * rng_inc, rng_limit)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
498 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
499 if (teq (rng_base + (n_elt - 2) * rng_inc, rng_limit)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
500 n_elt--; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
501 else if (teq (rng_base + n_elt * rng_inc, rng_limit)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
502 n_elt++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
503 } |
3858 | 504 |
6490 | 505 retval = (n_elt >= std::numeric_limits<octave_idx_type>::max () - 1) ? -1 : n_elt; |
3753 | 506 } |
507 | |
3858 | 508 return retval; |
3 | 509 } |