Mercurial > hg > octave-nkf
annotate liboctave/array/Range.cc @ 19198:96751a74bbbb
Start doxygenising sources
* Screen.cpp Screen.h Vt102Emulation.cpp files-dock-widget.h
main-window.h octave-qt-link.h parser.h: Reformat existing Doxygen
commands to use @ instead of \ to start a command.
* Array.h: Convert existing comments into Doxygen comments.
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Tue, 12 Aug 2014 15:27:16 -0400 |
parents | 47d4b680d0e0 |
children | 4197fc428c7d |
rev | line source |
---|---|
3 | 1 /* |
2 | |
17744
d63878346099
maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents:
16186
diff
changeset
|
3 Copyright (C) 1993-2013 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 |
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)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
47 && (NINTbig (rng_base) == rng_base || rng_nelem < 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
48 && (NINTbig (rng_inc) == rng_inc || rng_nelem <= 1)); |
2383 | 49 } |
645 | 50 |
51 Matrix | |
52 Range::matrix_value (void) const | |
53 { | |
8971 | 54 if (rng_nelem > 0 && cache.nelem () == 0) |
645 | 55 { |
4810 | 56 cache.resize (1, rng_nelem); |
645 | 57 double b = rng_base; |
58 double increment = rng_inc; | |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
59 if (rng_nelem > 0) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
60 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
61 // The first element must always be *exactly* the base. |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
62 // E.g, -0 would otherwise become +0 in the loop (-0 + 0*increment). |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
63 cache(0) = b; |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
64 for (octave_idx_type i = 1; i < rng_nelem; i++) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
65 cache(i) = b + i * increment; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
66 } |
4791 | 67 |
68 // On some machines (x86 with extended precision floating point | |
69 // arithmetic, for example) it is possible that we can overshoot | |
70 // the limit by approximately the machine precision even though | |
71 // we were very careful in our calculation of the number of | |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
72 // elements. The tests need equality (>= rng_limit or <= rng_limit) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
73 // to have expressions like -5:1:-0 result in a -0 endpoint. |
4791 | 74 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
75 if ((rng_inc > 0 && cache(rng_nelem-1) >= rng_limit) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
76 || (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 cache(rng_nelem-1) = rng_limit; |
645 | 78 } |
79 | |
4810 | 80 return cache; |
645 | 81 } |
3 | 82 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
83 double |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
84 Range::checkelem (octave_idx_type i) const |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
85 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
86 if (i < 0 || i >= rng_nelem) |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
87 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
|
88 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
89 if (i == 0) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
90 return rng_base; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
91 else if (i < rng_nelem - 1) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
92 return rng_base + i * rng_inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
93 else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
94 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
95 double end = rng_base + i * rng_inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
96 if ((rng_inc > 0 && end >= rng_limit) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
97 || (rng_inc < 0 && end <= rng_limit)) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
98 return rng_limit; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
99 else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
100 return end; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
101 } |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
102 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
103 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
104 double |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
105 Range::elem (octave_idx_type i) const |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
106 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
107 #if defined (BOUNDS_CHECKING) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
108 return checkelem (i); |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
109 #else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
110 if (i == 0) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
111 return rng_base; |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
112 else if (i < rng_nelem - 1) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
113 return rng_base + i * rng_inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
114 else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
115 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
116 double end = rng_base + i * rng_inc; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
117 if ((rng_inc > 0 && end >= rng_limit) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
118 || (rng_inc < 0 && end <= rng_limit)) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
119 return rng_limit; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
120 else |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
121 return end; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
122 } |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
123 #endif |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
124 } |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
125 |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
126 // Helper class used solely for idx_vector.loop () function call |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
127 class __rangeidx_helper |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
128 { |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
129 public: |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
130 __rangeidx_helper (double *a, double b, double i, double l, octave_idx_type n) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
131 : array (a), base (b), inc (i), limit (l), nmax (n-1) { } |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
132 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
133 void operator () (octave_idx_type i) |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
134 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
135 if (i == 0) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
136 *array++ = base; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
137 else if (i < nmax) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
138 *array++ = base + i * inc; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
139 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
140 { |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
141 double end = base + i * inc; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
142 if ((inc > 0 && end >= limit) || (inc < 0 && end <= limit)) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
143 *array++ = limit; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
144 else |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
145 *array++ = end; |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
146 } |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
147 } |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
148 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
149 private: |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
150 |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
151 double *array, base, inc, limit; |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
152 octave_idx_type nmax; |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
153 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
154 }; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
155 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
156 Array<double> |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
157 Range::index (const idx_vector& i) const |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
158 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
159 Array<double> retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
160 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
161 octave_idx_type n = rng_nelem; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
162 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
163 if (i.is_colon ()) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
164 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
165 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
|
166 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
167 else |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
168 { |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
169 if (i.extent (n) != n) |
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
170 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
|
171 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
172 dim_vector rd = i.orig_dimensions (); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
173 octave_idx_type il = i.length (n); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
174 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
175 // taken from Array.cc. |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
176 if (n != 1 && rd.is_vector ()) |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
177 rd = dim_vector (1, il); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
178 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
179 retval.clear (rd); |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
180 |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
181 // idx_vector loop across all values in i, |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
182 // executing __rangeidx_helper (i) for each i |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
183 i.loop (n, __rangeidx_helper (retval.fortran_vec (), |
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
184 rng_base, rng_inc, rng_limit, rng_nelem)); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
185 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
186 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
187 return retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
188 } |
4810 | 189 |
208 | 190 // NOTE: max and min only return useful values if nelem > 0. |
16186
b4a6895a9863
Use proper OO-class for __rangeidx_helper().
Rik <rik@octave.org>
parents:
16169
diff
changeset
|
191 // do_minmax_body() in max.cc avoids calling Range::min/max if nelem == 0. |
208 | 192 |
193 double | |
194 Range::min (void) const | |
195 { | |
196 double retval = 0.0; | |
197 if (rng_nelem > 0) | |
198 { | |
199 if (rng_inc > 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
200 retval = rng_base; |
208 | 201 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
202 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
203 retval = rng_base + (rng_nelem - 1) * rng_inc; |
4791 | 204 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
205 // See the note in the matrix_value method above. |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
206 if (retval <= rng_limit) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
207 retval = rng_limit; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
208 } |
4791 | 209 |
208 | 210 } |
211 return retval; | |
212 } | |
213 | |
214 double | |
215 Range::max (void) const | |
216 { | |
217 double retval = 0.0; | |
218 if (rng_nelem > 0) | |
219 { | |
220 if (rng_inc > 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
221 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
222 retval = rng_base + (rng_nelem - 1) * rng_inc; |
4791 | 223 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
224 // See the note in the matrix_value method above. |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
225 if (retval >= rng_limit) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
226 retval = rng_limit; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
227 } |
208 | 228 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
229 retval = rng_base; |
208 | 230 } |
231 return retval; | |
232 } | |
233 | |
234 void | |
7458 | 235 Range::sort_internal (bool ascending) |
208 | 236 { |
7458 | 237 if (ascending && rng_base > rng_limit && rng_inc < 0.0) |
208 | 238 { |
239 double tmp = rng_base; | |
240 rng_base = min (); | |
241 rng_limit = tmp; | |
242 rng_inc = -rng_inc; | |
4811 | 243 clear_cache (); |
208 | 244 } |
8565 | 245 else if (! ascending && rng_base < rng_limit && rng_inc > 0.0) |
7458 | 246 { |
247 double tmp = rng_limit; | |
248 rng_limit = min (); | |
249 rng_base = tmp; | |
250 rng_inc = -rng_inc; | |
251 clear_cache (); | |
252 } | |
253 } | |
254 | |
255 void | |
256 Range::sort_internal (Array<octave_idx_type>& sidx, bool ascending) | |
257 { | |
258 octave_idx_type nel = nelem (); | |
259 | |
260 sidx.resize (dim_vector (1, nel)); | |
261 | |
262 octave_idx_type *psidx = sidx.fortran_vec (); | |
263 | |
264 bool reverse = false; | |
265 | |
266 if (ascending && rng_base > rng_limit && rng_inc < 0.0) | |
267 { | |
268 double tmp = rng_base; | |
269 rng_base = min (); | |
270 rng_limit = tmp; | |
271 rng_inc = -rng_inc; | |
272 clear_cache (); | |
273 reverse = true; | |
274 } | |
275 else if (! ascending && rng_base < rng_limit && rng_inc > 0.0) | |
276 { | |
277 double tmp = rng_limit; | |
278 rng_limit = min (); | |
279 rng_base = tmp; | |
280 rng_inc = -rng_inc; | |
281 clear_cache (); | |
282 reverse = true; | |
283 } | |
284 | |
285 octave_idx_type tmp = reverse ? nel - 1 : 0; | |
7467 | 286 octave_idx_type stp = reverse ? -1 : 1; |
7458 | 287 |
7467 | 288 for (octave_idx_type i = 0; i < nel; i++, tmp += stp) |
7458 | 289 psidx[i] = tmp; |
290 | |
291 } | |
292 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
293 Matrix |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
294 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
|
295 { |
8971 | 296 return matrix_value ().diag (k); |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
297 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
298 |
7458 | 299 Range |
300 Range::sort (octave_idx_type dim, sortmode mode) const | |
301 { | |
302 Range retval = *this; | |
303 | |
304 if (dim == 1) | |
305 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
306 if (mode == ASCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
307 retval.sort_internal (true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
308 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
309 retval.sort_internal (false); |
7458 | 310 } |
311 else if (dim != 0) | |
312 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
313 | |
314 return retval; | |
315 } | |
316 | |
317 Range | |
318 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
|
319 sortmode mode) const |
7458 | 320 { |
321 Range retval = *this; | |
322 | |
323 if (dim == 1) | |
324 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
325 if (mode == ASCENDING) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
326 retval.sort_internal (sidx, true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
327 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
328 retval.sort_internal (sidx, false); |
7458 | 329 } |
330 else if (dim != 0) | |
331 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
332 | |
333 return retval; | |
208 | 334 } |
335 | |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
336 sortmode |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
337 Range::is_sorted (sortmode mode) const |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
338 { |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
339 if (rng_nelem > 1 && rng_inc > 0) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
340 mode = (mode == DESCENDING) ? UNSORTED : ASCENDING; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
341 else if (rng_nelem > 1 && rng_inc < 0) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
342 mode = (mode == ASCENDING) ? UNSORTED : DESCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
343 else |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
344 mode = mode ? mode : ASCENDING; |
8742
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
345 |
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
346 return mode; |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
347 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
348 |
3504 | 349 std::ostream& |
350 operator << (std::ostream& os, const Range& a) | |
3 | 351 { |
352 double b = a.base (); | |
353 double increment = a.inc (); | |
5275 | 354 octave_idx_type num_elem = a.nelem (); |
3 | 355 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
356 if (num_elem > 1) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
357 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
358 // First element must be the base *exactly* (-0). |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
359 os << b << " "; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
360 for (octave_idx_type i = 1; i < num_elem-1; i++) |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
361 os << b + i * increment << " "; |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
362 } |
3 | 363 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
364 // Prevent overshoot. See comment in the matrix_value method above. |
4791 | 365 os << (increment > 0 ? a.max () : a.min ()) << "\n"; |
3 | 366 |
367 return os; | |
368 } | |
369 | |
3504 | 370 std::istream& |
371 operator >> (std::istream& is, Range& a) | |
3 | 372 { |
208 | 373 is >> a.rng_base; |
3 | 374 if (is) |
375 { | |
208 | 376 is >> a.rng_limit; |
3 | 377 if (is) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
378 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
379 is >> a.rng_inc; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
380 a.rng_nelem = a.nelem_internal (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
381 } |
3 | 382 } |
383 | |
384 return is; | |
385 } | |
386 | |
2599 | 387 Range |
388 operator - (const Range& r) | |
389 { | |
19042
47d4b680d0e0
improve accuracy of range/scalar arithmetic (bug #42589)
John W. Eaton <jwe@octave.org>
parents:
17769
diff
changeset
|
390 return Range (-r.base (), -r.limit (), -r.inc (), r.nelem ()); |
2599 | 391 } |
392 | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
393 Range operator + (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
394 { |
19042
47d4b680d0e0
improve accuracy of range/scalar arithmetic (bug #42589)
John W. Eaton <jwe@octave.org>
parents:
17769
diff
changeset
|
395 Range result (x + r.base (), x + r.limit (), r.inc (), r.nelem ()); |
8971 | 396 if (result.rng_nelem < 0) |
397 result.cache = x + r.matrix_value (); | |
398 | |
399 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
400 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
401 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
402 Range operator + (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
403 { |
19042
47d4b680d0e0
improve accuracy of range/scalar arithmetic (bug #42589)
John W. Eaton <jwe@octave.org>
parents:
17769
diff
changeset
|
404 Range result (r.base () + x, r.limit () + x, r.inc (), r.nelem ()); |
8971 | 405 if (result.rng_nelem < 0) |
406 result.cache = r.matrix_value () + x; | |
407 | |
408 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
409 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
410 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
411 Range operator - (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
412 { |
19042
47d4b680d0e0
improve accuracy of range/scalar arithmetic (bug #42589)
John W. Eaton <jwe@octave.org>
parents:
17769
diff
changeset
|
413 Range result (x - r.base (), x - r.limit (), -r.inc (), r.nelem ()); |
8971 | 414 if (result.rng_nelem < 0) |
415 result.cache = x - r.matrix_value (); | |
416 | |
417 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
418 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
419 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
420 Range operator - (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
421 { |
19042
47d4b680d0e0
improve accuracy of range/scalar arithmetic (bug #42589)
John W. Eaton <jwe@octave.org>
parents:
17769
diff
changeset
|
422 Range result (r.base () - x, r.limit () - x, r.inc (), r.nelem ()); |
8971 | 423 if (result.rng_nelem < 0) |
424 result.cache = r.matrix_value () - x; | |
425 | |
426 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
427 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
428 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
429 Range operator * (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
430 { |
19042
47d4b680d0e0
improve accuracy of range/scalar arithmetic (bug #42589)
John W. Eaton <jwe@octave.org>
parents:
17769
diff
changeset
|
431 Range result (x * r.base (), x * r.limit (), x * r.inc (), r.nelem ()); |
8971 | 432 if (result.rng_nelem < 0) |
433 result.cache = x * r.matrix_value (); | |
434 | |
435 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
436 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
437 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
438 Range operator * (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
439 { |
19042
47d4b680d0e0
improve accuracy of range/scalar arithmetic (bug #42589)
John W. Eaton <jwe@octave.org>
parents:
17769
diff
changeset
|
440 Range result (r.base () * x, r.limit () * x, r.inc () * x, r.nelem ()); |
8971 | 441 if (result.rng_nelem < 0) |
442 result.cache = r.matrix_value () * x; | |
443 | |
444 return result; | |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
445 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
446 |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
447 |
1546 | 448 // C See Knuth, Art Of Computer Programming, Vol. 1, Problem 1.2.4-5. |
449 // C | |
450 // C===Tolerant FLOOR function. | |
451 // C | |
452 // C X - is given as a Double Precision argument to be operated on. | |
453 // C It is assumed that X is represented with M mantissa bits. | |
454 // C CT - is given as a Comparison Tolerance such that | |
455 // C 0.LT.CT.LE.3-SQRT(5)/2. If the relative difference between | |
456 // C X and A whole number is less than CT, then TFLOOR is | |
457 // C returned as this whole number. By treating the | |
458 // C floating-point numbers as a finite ordered set note that | |
459 // C the heuristic EPS=2.**(-(M-1)) and CT=3*EPS causes | |
460 // C arguments of TFLOOR/TCEIL to be treated as whole numbers | |
461 // C if they are exactly whole numbers or are immediately | |
462 // C adjacent to whole number representations. Since EPS, the | |
463 // C "distance" between floating-point numbers on the unit | |
464 // C interval, and M, the number of bits in X'S mantissa, exist | |
465 // C on every floating-point computer, TFLOOR/TCEIL are | |
466 // C consistently definable on every floating-point computer. | |
467 // C | |
468 // C For more information see the following references: | |
469 // C (1) P. E. Hagerty, "More On Fuzzy Floor And Ceiling," APL QUOTE | |
470 // C QUAD 8(4):20-24, June 1978. Note that TFLOOR=FL5. | |
471 // C (2) L. M. Breed, "Definitions For Fuzzy Floor And Ceiling", APL | |
472 // C QUOTE QUAD 8(3):16-23, March 1978. This paper cites FL1 through | |
473 // C FL5, the history of five years of evolutionary development of | |
474 // C FL5 - the seven lines of code below - by open collaboration | |
475 // C and corroboration of the mathematical-computing community. | |
476 // C | |
477 // C Penn State University Center for Academic Computing | |
478 // C H. D. Knoble - August, 1978. | |
479 | |
480 static inline double | |
481 tfloor (double x, double ct) | |
482 { | |
483 // C---------FLOOR(X) is the largest integer algebraically less than | |
484 // C or equal to X; that is, the unfuzzy FLOOR function. | |
485 | |
486 // DINT (X) = X - DMOD (X, 1.0); | |
487 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0); | |
488 | |
489 // C---------Hagerty's FL5 function follows... | |
490 | |
491 double q = 1.0; | |
492 | |
493 if (x < 0.0) | |
494 q = 1.0 - ct; | |
495 | |
496 double rmax = q / (2.0 - ct); | |
497 | |
11450
5eb10763069f
substitute and use LAPACK_LIBS in mkoctfile script
John W. Eaton <jwe@octave.org>
parents:
10366
diff
changeset
|
498 double t1 = 1.0 + gnulib::floor (x); |
1546 | 499 t1 = (ct / q) * (t1 < 0.0 ? -t1 : t1); |
500 t1 = rmax < t1 ? rmax : t1; | |
501 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
|
502 t1 = gnulib::floor (x + t1); |
1546 | 503 |
1555 | 504 if (x <= 0.0 || (t1 - x) < rmax) |
1546 | 505 return t1; |
506 else | |
507 return t1 - 1.0; | |
508 } | |
509 | |
510 static inline double | |
511 tceil (double x, double ct) | |
512 { | |
513 return -tfloor (-x, ct); | |
514 } | |
515 | |
3753 | 516 static inline bool |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
517 teq (double u, double v, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
518 double ct = 3.0 * std::numeric_limits<double>::epsilon ()) |
3753 | 519 { |
520 double tu = fabs (u); | |
521 double tv = fabs (v); | |
522 | |
523 return fabs (u - v) < ((tu > tv ? tu : tv) * ct); | |
524 } | |
525 | |
5275 | 526 octave_idx_type |
1360 | 527 Range::nelem_internal (void) const |
528 { | |
5275 | 529 octave_idx_type retval = -1; |
3 | 530 |
3858 | 531 if (rng_inc == 0 |
532 || (rng_limit > rng_base && rng_inc < 0) | |
533 || (rng_limit < rng_base && rng_inc > 0)) | |
534 { | |
535 retval = 0; | |
536 } | |
537 else | |
538 { | |
15220
61822c866ba1
use std::numeric_limits<T>::epsilon in C++ code
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
539 double ct = 3.0 * std::numeric_limits<double>::epsilon (); |
3 | 540 |
3858 | 541 double tmp = tfloor ((rng_limit - rng_base + rng_inc) / rng_inc, ct); |
542 | |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
543 octave_idx_type n_elt = (tmp > 0.0 ? static_cast<octave_idx_type> (tmp) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
544 : 0); |
398 | 545 |
3858 | 546 // If the final element that we would compute for the range is |
547 // equal to the limit of the range, or is an adjacent floating | |
548 // point number, accept it. Otherwise, try a range with one | |
549 // fewer element. If that fails, try again with one more | |
550 // element. | |
551 // | |
552 // I'm not sure this is very good, but it seems to work better than | |
553 // just using tfloor as above. For example, without it, the | |
554 // expression 1.8:0.05:1.9 fails to produce the expected result of | |
555 // [1.8, 1.85, 1.9]. | |
3753 | 556 |
3858 | 557 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
|
558 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
559 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
|
560 n_elt--; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
561 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
|
562 n_elt++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
563 } |
3858 | 564 |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
565 retval = (n_elt < std::numeric_limits<octave_idx_type>::max () - 1) |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
566 ? n_elt : -1; |
3753 | 567 } |
568 | |
3858 | 569 return retval; |
3 | 570 } |