3
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 2000, 2001, 2002, 2004, |
|
4 2005, 2007 John W. Eaton |
3
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
3
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
3
|
21 |
|
22 */ |
|
23 |
238
|
24 #ifdef HAVE_CONFIG_H |
1192
|
25 #include <config.h> |
3
|
26 #endif |
|
27 |
1546
|
28 #include <cfloat> |
1367
|
29 |
3503
|
30 #include <iostream> |
6490
|
31 #include <limits> |
3
|
32 |
|
33 #include "Range.h" |
7458
|
34 #include "lo-error.h" |
2383
|
35 #include "lo-mappers.h" |
7231
|
36 #include "lo-math.h" |
2383
|
37 #include "lo-utils.h" |
|
38 |
|
39 bool |
|
40 Range::all_elements_are_ints (void) const |
|
41 { |
|
42 // If the base and increment are ints, the final value in the range |
6457
|
43 // will also be an integer, even if the limit is not. If there is one |
|
44 // or fewer elements only the base needs to be an integer |
2383
|
45 |
|
46 return (! (xisnan (rng_base) || xisnan (rng_inc)) |
6457
|
47 && (NINTbig (rng_base) == rng_base || rng_nelem < 1) |
|
48 && (NINTbig (rng_inc) == rng_inc || rng_nelem <= 1)); |
2383
|
49 } |
645
|
50 |
|
51 Matrix |
|
52 Range::matrix_value (void) const |
|
53 { |
4811
|
54 if (rng_nelem > 0 && cache.rows () == 0) |
645
|
55 { |
4810
|
56 cache.resize (1, rng_nelem); |
645
|
57 double b = rng_base; |
|
58 double increment = rng_inc; |
5275
|
59 for (octave_idx_type i = 0; i < rng_nelem; i++) |
4810
|
60 cache(i) = b + i * increment; |
4791
|
61 |
|
62 // On some machines (x86 with extended precision floating point |
|
63 // arithmetic, for example) it is possible that we can overshoot |
|
64 // the limit by approximately the machine precision even though |
|
65 // we were very careful in our calculation of the number of |
|
66 // elements. |
|
67 |
4810
|
68 if ((rng_inc > 0 && cache(rng_nelem-1) > rng_limit) |
|
69 || (rng_inc < 0 && cache(rng_nelem-1) < rng_limit)) |
|
70 cache(rng_nelem-1) = rng_limit; |
645
|
71 } |
|
72 |
4810
|
73 return cache; |
645
|
74 } |
3
|
75 |
4810
|
76 |
208
|
77 // NOTE: max and min only return useful values if nelem > 0. |
|
78 |
|
79 double |
|
80 Range::min (void) const |
|
81 { |
|
82 double retval = 0.0; |
|
83 if (rng_nelem > 0) |
|
84 { |
|
85 if (rng_inc > 0) |
|
86 retval = rng_base; |
|
87 else |
4791
|
88 { |
|
89 retval = rng_base + (rng_nelem - 1) * rng_inc; |
|
90 |
|
91 // See the note in the matrix_value method above. |
|
92 |
|
93 if (retval < rng_limit) |
|
94 retval = rng_limit; |
|
95 } |
|
96 |
208
|
97 } |
|
98 return retval; |
|
99 } |
|
100 |
|
101 double |
|
102 Range::max (void) const |
|
103 { |
|
104 double retval = 0.0; |
|
105 if (rng_nelem > 0) |
|
106 { |
|
107 if (rng_inc > 0) |
4791
|
108 { |
|
109 retval = rng_base + (rng_nelem - 1) * rng_inc; |
|
110 |
|
111 // See the note in the matrix_value method above. |
|
112 |
|
113 if (retval > rng_limit) |
|
114 retval = rng_limit; |
|
115 } |
208
|
116 else |
|
117 retval = rng_base; |
|
118 } |
|
119 return retval; |
|
120 } |
|
121 |
|
122 void |
7458
|
123 Range::sort_internal (bool ascending) |
208
|
124 { |
7458
|
125 if (ascending && rng_base > rng_limit && rng_inc < 0.0) |
208
|
126 { |
|
127 double tmp = rng_base; |
|
128 rng_base = min (); |
|
129 rng_limit = tmp; |
|
130 rng_inc = -rng_inc; |
4811
|
131 clear_cache (); |
208
|
132 } |
7458
|
133 else if (rng_base < rng_limit && rng_inc > 0.0) |
|
134 { |
|
135 double tmp = rng_limit; |
|
136 rng_limit = min (); |
|
137 rng_base = tmp; |
|
138 rng_inc = -rng_inc; |
|
139 clear_cache (); |
|
140 } |
|
141 } |
|
142 |
|
143 void |
|
144 Range::sort_internal (Array<octave_idx_type>& sidx, bool ascending) |
|
145 { |
|
146 octave_idx_type nel = nelem (); |
|
147 |
|
148 sidx.resize (dim_vector (1, nel)); |
|
149 |
|
150 octave_idx_type *psidx = sidx.fortran_vec (); |
|
151 |
|
152 bool reverse = false; |
|
153 |
|
154 if (ascending && rng_base > rng_limit && rng_inc < 0.0) |
|
155 { |
|
156 double tmp = rng_base; |
|
157 rng_base = min (); |
|
158 rng_limit = tmp; |
|
159 rng_inc = -rng_inc; |
|
160 clear_cache (); |
|
161 reverse = true; |
|
162 } |
|
163 else if (! ascending && rng_base < rng_limit && rng_inc > 0.0) |
|
164 { |
|
165 double tmp = rng_limit; |
|
166 rng_limit = min (); |
|
167 rng_base = tmp; |
|
168 rng_inc = -rng_inc; |
|
169 clear_cache (); |
|
170 reverse = true; |
|
171 } |
|
172 |
|
173 octave_idx_type tmp = reverse ? nel - 1 : 0; |
|
174 octave_idx_type inc = reverse ? -1 : 1; |
|
175 |
|
176 for (octave_idx_type i = 0; i < nel; i++, tmp += inc) |
|
177 psidx[i] = tmp; |
|
178 |
|
179 } |
|
180 |
|
181 Range |
|
182 Range::sort (octave_idx_type dim, sortmode mode) const |
|
183 { |
|
184 Range retval = *this; |
|
185 |
|
186 if (dim == 1) |
|
187 { |
|
188 switch (mode) |
|
189 { |
|
190 case ASCENDING: |
|
191 retval.sort_internal (true); |
|
192 break; |
|
193 |
|
194 case DESCENDING: |
|
195 retval.sort_internal (false); |
|
196 break; |
|
197 |
|
198 default: |
|
199 (*current_liboctave_error_handler) ("Range::sort: invalid sort mode"); |
|
200 } |
|
201 } |
|
202 else if (dim != 0) |
|
203 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); |
|
204 |
|
205 return retval; |
|
206 } |
|
207 |
|
208 Range |
|
209 Range::sort (Array<octave_idx_type>& sidx, octave_idx_type dim, |
|
210 sortmode mode) const |
|
211 { |
|
212 Range retval = *this; |
|
213 |
|
214 if (dim == 1) |
|
215 { |
|
216 switch (mode) |
|
217 { |
|
218 case ASCENDING: |
|
219 retval.sort_internal (sidx, true); |
|
220 break; |
|
221 |
|
222 case DESCENDING: |
|
223 retval.sort_internal (sidx, false); |
|
224 break; |
|
225 |
|
226 default: |
|
227 (*current_liboctave_error_handler) ("Range::sort: invalid sort mode"); |
|
228 } |
|
229 } |
|
230 else if (dim != 0) |
|
231 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); |
|
232 |
|
233 return retval; |
208
|
234 } |
|
235 |
3504
|
236 std::ostream& |
|
237 operator << (std::ostream& os, const Range& a) |
3
|
238 { |
|
239 double b = a.base (); |
|
240 double increment = a.inc (); |
5275
|
241 octave_idx_type num_elem = a.nelem (); |
3
|
242 |
5275
|
243 for (octave_idx_type i = 0; i < num_elem-1; i++) |
3
|
244 os << b + i * increment << " "; |
|
245 |
4791
|
246 // Prevent overshoot. See comment in the matrix_value method |
|
247 // above. |
|
248 |
|
249 os << (increment > 0 ? a.max () : a.min ()) << "\n"; |
3
|
250 |
|
251 return os; |
|
252 } |
|
253 |
3504
|
254 std::istream& |
|
255 operator >> (std::istream& is, Range& a) |
3
|
256 { |
208
|
257 is >> a.rng_base; |
3
|
258 if (is) |
|
259 { |
208
|
260 is >> a.rng_limit; |
3
|
261 if (is) |
|
262 { |
208
|
263 is >> a.rng_inc; |
|
264 a.rng_nelem = a.nelem_internal (); |
3
|
265 } |
|
266 } |
|
267 |
|
268 return is; |
|
269 } |
|
270 |
2599
|
271 Range |
|
272 operator - (const Range& r) |
|
273 { |
|
274 return Range (-r.base (), -r.limit (), -r.inc ()); |
|
275 } |
|
276 |
1546
|
277 // C See Knuth, Art Of Computer Programming, Vol. 1, Problem 1.2.4-5. |
|
278 // C |
|
279 // C===Tolerant FLOOR function. |
|
280 // C |
|
281 // C X - is given as a Double Precision argument to be operated on. |
|
282 // C It is assumed that X is represented with M mantissa bits. |
|
283 // C CT - is given as a Comparison Tolerance such that |
|
284 // C 0.LT.CT.LE.3-SQRT(5)/2. If the relative difference between |
|
285 // C X and A whole number is less than CT, then TFLOOR is |
|
286 // C returned as this whole number. By treating the |
|
287 // C floating-point numbers as a finite ordered set note that |
|
288 // C the heuristic EPS=2.**(-(M-1)) and CT=3*EPS causes |
|
289 // C arguments of TFLOOR/TCEIL to be treated as whole numbers |
|
290 // C if they are exactly whole numbers or are immediately |
|
291 // C adjacent to whole number representations. Since EPS, the |
|
292 // C "distance" between floating-point numbers on the unit |
|
293 // C interval, and M, the number of bits in X'S mantissa, exist |
|
294 // C on every floating-point computer, TFLOOR/TCEIL are |
|
295 // C consistently definable on every floating-point computer. |
|
296 // C |
|
297 // C For more information see the following references: |
|
298 // C (1) P. E. Hagerty, "More On Fuzzy Floor And Ceiling," APL QUOTE |
|
299 // C QUAD 8(4):20-24, June 1978. Note that TFLOOR=FL5. |
|
300 // C (2) L. M. Breed, "Definitions For Fuzzy Floor And Ceiling", APL |
|
301 // C QUOTE QUAD 8(3):16-23, March 1978. This paper cites FL1 through |
|
302 // C FL5, the history of five years of evolutionary development of |
|
303 // C FL5 - the seven lines of code below - by open collaboration |
|
304 // C and corroboration of the mathematical-computing community. |
|
305 // C |
|
306 // C Penn State University Center for Academic Computing |
|
307 // C H. D. Knoble - August, 1978. |
|
308 |
|
309 static inline double |
|
310 tfloor (double x, double ct) |
|
311 { |
|
312 // C---------FLOOR(X) is the largest integer algebraically less than |
|
313 // C or equal to X; that is, the unfuzzy FLOOR function. |
|
314 |
|
315 // DINT (X) = X - DMOD (X, 1.0); |
|
316 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0); |
|
317 |
|
318 // C---------Hagerty's FL5 function follows... |
|
319 |
|
320 double q = 1.0; |
|
321 |
|
322 if (x < 0.0) |
|
323 q = 1.0 - ct; |
|
324 |
|
325 double rmax = q / (2.0 - ct); |
|
326 |
|
327 double t1 = 1.0 + floor (x); |
|
328 t1 = (ct / q) * (t1 < 0.0 ? -t1 : t1); |
|
329 t1 = rmax < t1 ? rmax : t1; |
|
330 t1 = ct > t1 ? ct : t1; |
|
331 t1 = floor (x + t1); |
|
332 |
1555
|
333 if (x <= 0.0 || (t1 - x) < rmax) |
1546
|
334 return t1; |
|
335 else |
|
336 return t1 - 1.0; |
|
337 } |
|
338 |
|
339 static inline double |
|
340 tceil (double x, double ct) |
|
341 { |
|
342 return -tfloor (-x, ct); |
|
343 } |
|
344 |
3753
|
345 static inline bool |
|
346 teq (double u, double v, double ct = 3.0 * DBL_EPSILON) |
|
347 { |
|
348 double tu = fabs (u); |
|
349 double tv = fabs (v); |
|
350 |
|
351 return fabs (u - v) < ((tu > tv ? tu : tv) * ct); |
|
352 } |
|
353 |
5275
|
354 octave_idx_type |
1360
|
355 Range::nelem_internal (void) const |
|
356 { |
5275
|
357 octave_idx_type retval = -1; |
3
|
358 |
3858
|
359 if (rng_inc == 0 |
|
360 || (rng_limit > rng_base && rng_inc < 0) |
|
361 || (rng_limit < rng_base && rng_inc > 0)) |
|
362 { |
|
363 retval = 0; |
|
364 } |
|
365 else |
|
366 { |
|
367 double ct = 3.0 * DBL_EPSILON; |
3
|
368 |
3858
|
369 double tmp = tfloor ((rng_limit - rng_base + rng_inc) / rng_inc, ct); |
|
370 |
5275
|
371 octave_idx_type n_elt = (tmp > 0.0 ? static_cast<octave_idx_type> (tmp) : 0); |
398
|
372 |
3858
|
373 // If the final element that we would compute for the range is |
|
374 // equal to the limit of the range, or is an adjacent floating |
|
375 // point number, accept it. Otherwise, try a range with one |
|
376 // fewer element. If that fails, try again with one more |
|
377 // element. |
|
378 // |
|
379 // I'm not sure this is very good, but it seems to work better than |
|
380 // just using tfloor as above. For example, without it, the |
|
381 // expression 1.8:0.05:1.9 fails to produce the expected result of |
|
382 // [1.8, 1.85, 1.9]. |
3753
|
383 |
3858
|
384 if (! teq (rng_base + (n_elt - 1) * rng_inc, rng_limit)) |
|
385 { |
|
386 if (teq (rng_base + (n_elt - 2) * rng_inc, rng_limit)) |
|
387 n_elt--; |
|
388 else if (teq (rng_base + n_elt * rng_inc, rng_limit)) |
|
389 n_elt++; |
|
390 } |
|
391 |
6490
|
392 retval = (n_elt >= std::numeric_limits<octave_idx_type>::max () - 1) ? -1 : n_elt; |
3753
|
393 } |
|
394 |
3858
|
395 return retval; |
3
|
396 } |
|
397 |
|
398 /* |
|
399 ;;; Local Variables: *** |
|
400 ;;; mode: C++ *** |
|
401 ;;; End: *** |
|
402 */ |