Mercurial > octave
annotate liboctave/array/Range.cc @ 30835:2989202f92f8 stable
only allow range<FLOAT> to be instantiated
* Range.h, Range.cc: Limit range<T> to floating point values.
* range-fwd.h: Update forward declaration of range<T>.
* ov-base.h, ov-base.cc, ov-range.h, ov-range.cc, ov.h, ov.cc,
pt-eval.cc: Temporarily comment out code that uses range<INT>
objects.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 17 Mar 2022 02:44:19 -0400 |
parents | 796f54d4ddbf |
children | 0f968958d7d5 e88a07dec498 |
rev | line source |
---|---|
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
1 //////////////////////////////////////////////////////////////////////// |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
2 // |
30564
796f54d4ddbf
update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents:
30420
diff
changeset
|
3 // Copyright (C) 1993-2022 The Octave Project Developers |
27923
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
4 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
5 // See the file COPYRIGHT.md in the top-level directory of this |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
6 // distribution or <https://octave.org/copyright/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
7 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
8 // This file is part of Octave. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
9 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
10 // Octave is free software: you can redistribute it and/or modify it |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
11 // under the terms of the GNU General Public License as published by |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
12 // the Free Software Foundation, either version 3 of the License, or |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
13 // (at your option) any later version. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
14 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
15 // Octave is distributed in the hope that it will be useful, but |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
16 // WITHOUT ANY WARRANTY; without even the implied warranty of |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
18 // GNU General Public License for more details. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
19 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
20 // You should have received a copy of the GNU General Public License |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
21 // along with Octave; see the file COPYING. If not, see |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
22 // <https://www.gnu.org/licenses/>. |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
23 // |
bd51beb6205e
update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents:
27919
diff
changeset
|
24 //////////////////////////////////////////////////////////////////////// |
3 | 25 |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21321
diff
changeset
|
26 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21229
diff
changeset
|
27 # include "config.h" |
3 | 28 #endif |
29 | |
23443
3f1bf237908b
maint: Eliminate <cfloat.h> header from liboctave files.
Rik <rik@octave.org>
parents:
23398
diff
changeset
|
30 #include <cmath> |
1367 | 31 |
25438
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
32 #include <istream> |
6490 | 33 #include <limits> |
25438
cb1606f78f6b
prefer <istream>, <ostream>, or <iosfwd> to <iostream> where possible
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
34 #include <ostream> |
3 | 35 |
23443
3f1bf237908b
maint: Eliminate <cfloat.h> header from liboctave files.
Rik <rik@octave.org>
parents:
23398
diff
changeset
|
36 #include "Array-util.h" |
3 | 37 #include "Range.h" |
7458 | 38 #include "lo-error.h" |
2383 | 39 #include "lo-mappers.h" |
40 #include "lo-utils.h" | |
41 | |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
42 namespace octave |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
43 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
44 template <typename T> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
45 T xtfloor (T x, T ct) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
46 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
47 // C---------FLOOR(X) is the largest integer algebraically less than |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
48 // C or equal to X; that is, the unfuzzy FLOOR function. |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
49 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
50 // DINT (X) = X - DMOD (X, 1.0); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
51 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
52 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
53 // C---------Hagerty's FL5 function follows... |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
54 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
55 T q = 1; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
56 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
57 if (x < 0) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
58 q = 1 - ct; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
59 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
60 T rmax = q / (2 - ct); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
61 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
62 T t1 = 1 + std::floor (x); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
63 t1 = (ct / q) * (t1 < 0 ? -t1 : t1); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
64 t1 = (rmax < t1 ? rmax : t1); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
65 t1 = (ct > t1 ? ct : t1); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
66 t1 = std::floor (x + t1); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
67 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
68 if (x <= 0 || (t1 - x) < rmax) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
69 return t1; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
70 else |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
71 return t1 - 1; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
72 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
73 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
74 template <typename T> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
75 bool xteq (T u, T v, T ct = 3 * std::numeric_limits<T>::epsilon ()) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
76 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
77 T tu = std::abs (u); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
78 T tv = std::abs (v); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
79 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
80 return std::abs (u - v) < ((tu > tv ? tu : tv) * ct); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
81 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
82 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
83 template <typename T> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
84 octave_idx_type xnumel_internal (T base, T limit, T inc) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
85 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
86 octave_idx_type retval = -1; |
28857
43ad651cf5a0
eliminate unnecessary uses of octave:: namespace qualifier
John W. Eaton <jwe@octave.org>
parents:
28636
diff
changeset
|
87 if (! math::isfinite (base) || ! math::isfinite (inc) |
43ad651cf5a0
eliminate unnecessary uses of octave:: namespace qualifier
John W. Eaton <jwe@octave.org>
parents:
28636
diff
changeset
|
88 || math::isnan (limit)) |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
89 retval = -2; |
28857
43ad651cf5a0
eliminate unnecessary uses of octave:: namespace qualifier
John W. Eaton <jwe@octave.org>
parents:
28636
diff
changeset
|
90 else if (math::isinf (limit) |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
91 && ((inc > 0 && limit > 0) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
92 || (inc < 0 && limit < 0))) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
93 retval = std::numeric_limits<octave_idx_type>::max () - 1; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
94 else if (inc == 0 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
95 || (limit > base && inc < 0) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
96 || (limit < base && inc > 0)) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
97 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
98 retval = 0; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
99 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
100 else |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
101 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
102 T ct = 3 * std::numeric_limits<T>::epsilon (); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
103 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
104 T tmp = xtfloor ((limit - base + inc) / inc, ct); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
105 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
106 octave_idx_type n_elt |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
107 = (tmp > 0 ? static_cast<octave_idx_type> (tmp) : 0); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
108 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
109 // If the final element that we would compute for the range is |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
110 // equal to the limit of the range, or is an adjacent floating |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
111 // point number, accept it. Otherwise, try a range with one |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
112 // fewer element. If that fails, try again with one more |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
113 // element. |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
114 // |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
115 // I'm not sure this is very good, but it seems to work better |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
116 // than just using tfloor as above. For example, without it, |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
117 // the expression 1.8:0.05:1.9 fails to produce the expected |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
118 // result of [1.8, 1.85, 1.9]. |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
119 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
120 if (! xteq (base + (n_elt - 1) * inc, limit)) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
121 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
122 if (xteq (base + (n_elt - 2) * inc, limit)) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
123 n_elt--; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
124 else if (xteq (base + n_elt * inc, limit)) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
125 n_elt++; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
126 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
127 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
128 retval = (n_elt < std::numeric_limits<octave_idx_type>::max () - 1 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
129 ? n_elt : -1); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
130 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
131 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
132 return retval; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
133 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
134 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
135 template <typename T> |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
136 bool xall_elements_are_ints (T base, T inc, T final_val, octave_idx_type nel) |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
137 { |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
138 // If the range is empty or NaN then there are no elements so there |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
139 // can be no int elements. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
140 if (nel == 0 || math::isnan (final_val)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
141 return false; |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
142 |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
143 // If the base and increment are ints, all elements will be |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
144 // integers. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
145 if (math::nint_big (base) == base && math::nint_big (inc) == inc) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
146 return true; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
147 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
148 // If the range has only one element, then the base needs to be an |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
149 // integer. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
150 if (nel == 1 && math::nint_big (base)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
151 return true; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
152 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
153 return false; |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
154 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
155 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
156 template <typename T> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
157 T |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
158 xfinal_value (T base, T limit, T inc, octave_idx_type nel) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
159 { |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
160 T retval = T (0); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
161 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
162 if (nel <= 1) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
163 return base; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
164 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
165 // If increment is 0, then numel should also be zero. |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
166 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
167 retval = base + (nel - 1) * inc; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
168 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
169 // On some machines (x86 with extended precision floating point |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
170 // arithmetic, for example) it is possible that we can overshoot |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
171 // the limit by approximately the machine precision even though |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
172 // we were very careful in our calculation of the number of |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
173 // elements. Therefore, we clip the result to the limit if it |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
174 // overshoots. |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
175 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
176 // NOTE: The test also includes equality (>= limit) to have |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
177 // expressions such as -5:1:-0 result in a -0 endpoint. |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
178 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
179 if ((inc > T (0) && retval >= limit) || (inc < T (0) && retval <= limit)) |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
180 retval = limit; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
181 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
182 // If all elements are integers, then ensure the final value is. |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
183 // Note that we pass the preliminary computed final value to |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
184 // xall_elements_are_ints, but it only checks whether that value is |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
185 // NaN. |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
186 |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
187 if (xall_elements_are_ints (base, inc, retval, nel)) |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
188 retval = std::round (retval); |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
189 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
190 return retval; |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
191 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
192 |
29352
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
193 template <typename T> |
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
194 void |
30420
366aa563dd2e
style fixes for Range.h and Range.cc
John W. Eaton <jwe@octave.org>
parents:
30416
diff
changeset
|
195 xinit (T base, T limit, T inc, bool reverse, T& final_val, |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
196 octave_idx_type& nel) |
29352
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
197 { |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
198 // Catch obvious NaN ranges. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
199 if (math::isnan (base) || math::isnan (limit) || math::isnan (inc)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
200 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
201 final_val = numeric_limits<T>::NaN (); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
202 nel = 1; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
203 return; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
204 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
205 |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
206 // Floating point numbers are always signed |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
207 if (reverse) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
208 inc = -inc; |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
209 |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
210 // Catch empty ranges. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
211 if (inc == 0 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
212 || (limit < base && inc > 0) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
213 || (limit > base && inc < 0)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
214 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
215 nel = 0; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
216 return; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
217 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
218 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
219 // The following case also catches Inf values for increment when |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
220 // there will be only one element. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
221 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
222 if ((limit <= base && base + inc < limit) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
223 || (limit >= base && base + inc > limit)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
224 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
225 final_val = base; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
226 nel = 1; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
227 return; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
228 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
229 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
230 // Any other calculations with Inf will give us either a NaN range |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
231 // or an infinite nember of elements. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
232 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
233 T dnel = (limit - base) / inc; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
234 if (math::isnan (dnel)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
235 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
236 nel = 1; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
237 final_val = numeric_limits<T>::NaN (); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
238 return; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
239 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
240 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
241 if (dnel > 0 && math::isinf (dnel)) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
242 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
243 // FIXME: Should this be an immediate error? |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
244 nel = std::numeric_limits<octave_idx_type>::max (); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
245 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
246 // FIXME: Will this do the right thing in all cases? |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
247 final_val = xfinal_value (base, limit, inc, nel); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
248 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
249 return; |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
250 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
251 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
252 // Now that we have handled all the special cases, we can compute |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
253 // the number of elements and the final value in a way that attempts |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
254 // to avoid rounding errors as much as possible. |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
255 |
29352
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
256 nel = xnumel_internal (base, limit, inc); |
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
257 final_val = xfinal_value (base, limit, inc, nel); |
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
258 } |
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
259 |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
260 template <typename T> |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
261 void |
30420
366aa563dd2e
style fixes for Range.h and Range.cc
John W. Eaton <jwe@octave.org>
parents:
30416
diff
changeset
|
262 xinit (const octave_int<T>& base, const octave_int<T>& limit, |
366aa563dd2e
style fixes for Range.h and Range.cc
John W. Eaton <jwe@octave.org>
parents:
30416
diff
changeset
|
263 const octave_int<T>& inc, bool reverse, |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
264 octave_int<T>& final_val, octave_idx_type& nel) |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
265 { |
30407
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
266 // We need an integer division that is truncating decimals instead |
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
267 // of rounding. So, use underlying C++ types instead of |
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
268 // octave_int<T>. |
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
269 |
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
270 // FIXME: The numerator might underflow or overflow. Add checks for |
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
271 // that. |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
272 if (reverse) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
273 { |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
274 nel = ((inc == octave_int<T> (0) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
275 || (limit > base && inc > octave_int<T> (0)) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
276 || (limit < base && inc < octave_int<T> (0))) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
277 ? 0 |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
278 : (base.value () - limit.value () + inc.value ()) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
279 / inc.value ()); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
280 |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
281 final_val = base - (nel - 1) * inc; |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
282 } |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
283 else |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
284 { |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
285 nel = ((inc == octave_int<T> (0) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
286 || (limit > base && inc < octave_int<T> (0)) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
287 || (limit < base && inc > octave_int<T> (0))) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
288 ? 0 |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
289 : (limit.value () - base.value () + inc.value ()) |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
290 / inc.value ()); |
30407
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
291 |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
292 final_val = base + (nel - 1) * inc; |
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
293 } |
30407
597275db9c7f
don't return void; pass objects by const ref instead of value
John W. Eaton <jwe@octave.org>
parents:
30405
diff
changeset
|
294 } |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
295 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
296 template <typename T> |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
297 bool |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
298 xis_storable (T base, T limit, octave_idx_type nel) |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
299 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
300 return ! (nel > 1 && (math::isinf (base) || math::isinf (limit))); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
301 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
302 |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
303 template <> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
304 bool |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
305 range<double>::all_elements_are_ints (void) const |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
306 { |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
307 return xall_elements_are_ints (m_base, m_increment, m_final, m_numel); |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
308 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
309 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
310 template <> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
311 bool |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
312 range<float>::all_elements_are_ints (void) const |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
313 { |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
314 return xall_elements_are_ints (m_base, m_increment, m_final, m_numel); |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
315 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
316 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
317 template <> |
29352
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
318 void |
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
319 range<double>::init (void) |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
320 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
321 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
322 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
323 |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
324 template <> |
29352
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
325 void |
03c283f73b9a
specialize range<T>::init instead of defining get_numel and get_final_value
John W. Eaton <jwe@octave.org>
parents:
29351
diff
changeset
|
326 range<float>::init (void) |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
327 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
328 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
329 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
330 |
30835
2989202f92f8
only allow range<FLOAT> to be instantiated
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
331 // For now, only define for float and double. |
2989202f92f8
only allow range<FLOAT> to be instantiated
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
332 |
2989202f92f8
only allow range<FLOAT> to be instantiated
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
333 #if 0 |
2989202f92f8
only allow range<FLOAT> to be instantiated
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
334 |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
335 template <> |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
336 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
337 range<octave_int8>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
338 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
339 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
340 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
341 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
342 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
343 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
344 range<octave_int16>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
345 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
346 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
347 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
348 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
349 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
350 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
351 range<octave_int32>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
352 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
353 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
354 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
355 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
356 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
357 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
358 range<octave_int64>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
359 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
360 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
361 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
362 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
363 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
364 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
365 range<octave_uint8>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
366 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
367 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
368 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
369 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
370 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
371 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
372 range<octave_uint16>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
373 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
374 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
375 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
376 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
377 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
378 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
379 range<octave_uint32>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
380 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
381 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
382 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
383 |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
384 template <> |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
385 void |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
386 range<octave_uint64>::init (void) |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
387 { |
30416
4736bc8e9804
Allow descending ranges of all integer types (bug #61132).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30407
diff
changeset
|
388 xinit (m_base, m_limit, m_increment, m_reverse, m_final, m_numel); |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
389 } |
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
390 |
30835
2989202f92f8
only allow range<FLOAT> to be instantiated
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
391 #endif |
2989202f92f8
only allow range<FLOAT> to be instantiated
John W. Eaton <jwe@octave.org>
parents:
30564
diff
changeset
|
392 |
30405
20cefb3b0da6
range: Use specialization of init function for octave_int<T> types (bug #61300).
Markus Mützel <markus.muetzel@gmx.de>
parents:
30394
diff
changeset
|
393 template <> |
29353
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
394 bool |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
395 range<double>::is_storable (void) const |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
396 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
397 return xis_storable (m_base, m_limit, m_numel); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
398 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
399 |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
400 template <> |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
401 bool |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
402 range<float>::is_storable (void) const |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
403 { |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
404 return xis_storable (m_base, m_limit, m_numel); |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
405 } |
715344f405f0
improve handling of nan, infinite value, and empty ranges (bug #59229)
John W. Eaton <jwe@octave.org>
parents:
29352
diff
changeset
|
406 |
29351
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
407 template <typename T> |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
408 octave_idx_type |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
409 xnnz (T base, T limit, T inc, T final_val, octave_idx_type nel) |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
410 { |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
411 // Note that the order of the following checks matters. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
412 |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
413 // If there are no elements, there can be no non-zero elements. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
414 if (nel == 0) |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
415 return 0; |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
416 |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
417 // All elements have the same sign, hence there are no zeros. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
418 if ((base > 0 && limit > 0) || (base < 0 && limit < 0)) |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
419 return nel; |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
420 |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
421 // All elements are equal (inc = 0) but we know from the previous |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
422 // condition that they are not positive or negative, therefore all |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
423 // elements are zero. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
424 if (inc == 0) |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
425 return 0; |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
426 |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
427 // Exactly one zero at beginning or end of range. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
428 if (base == 0 || final_val == 0) |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
429 return nel - 1; |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
430 |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
431 // Range crosses negative/positive without hitting zero. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
432 // FIXME: Is this test sufficiently tolerant or do we need to be |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
433 // more careful? |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
434 if (math::mod (-base, inc) != 0) |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
435 return nel; |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
436 |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
437 // Range crosses negative/positive and hits zero. |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
438 return nel - 1; |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
439 } |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
440 |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
441 template <> |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
442 octave_idx_type |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
443 range<double>::nnz (void) const |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
444 { |
29351
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
445 return xnnz (m_base, m_limit, m_increment, m_final, m_numel); |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
446 } |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
447 |
29351
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
448 template <> |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
449 octave_idx_type |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
450 range<float>::nnz (void) const |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
451 { |
326a7ca62c43
also define range<float>::nnz specialization
John W. Eaton <jwe@octave.org>
parents:
28857
diff
changeset
|
452 return xnnz (m_base, m_limit, m_increment, m_final, m_numel); |
28635
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
453 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
454 } |
b6371946f106
new template for range objects
John W. Eaton <jwe@octave.org>
parents:
28628
diff
changeset
|
455 |
2383 | 456 bool |
457 Range::all_elements_are_ints (void) const | |
458 { | |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
459 // If the base and increment are ints, the final value in the range will also |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
460 // be an integer, even if the limit is not. If there is one or fewer |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
461 // elements only the base needs to be an integer. |
2383 | 462 |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
463 return (! (octave::math::isnan (m_base) || octave::math::isnan (m_inc)) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
464 && (octave::math::nint_big (m_base) == m_base || m_numel < 1) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
465 && (octave::math::nint_big (m_inc) == m_inc || m_numel <= 1)); |
2383 | 466 } |
645 | 467 |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
468 octave_idx_type |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
469 Range::nnz (void) const |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
470 { |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
471 octave_idx_type retval = 0; |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
472 |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
473 if (! isempty ()) |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
474 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
475 if ((m_base > 0.0 && m_limit > 0.0) || (m_base < 0.0 && m_limit < 0.0)) |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
476 { |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
477 // All elements have the same sign, hence there are no zeros. |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
478 retval = m_numel; |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
479 } |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
480 else if (m_inc != 0.0) |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
481 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
482 if (m_base == 0.0 || m_limit == 0.0) |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
483 // Exactly one zero at beginning or end of range. |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
484 retval = m_numel - 1; |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
485 else if ((m_base / m_inc) != std::floor (m_base / m_inc)) |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
486 // Range crosses negative/positive without hitting zero. |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
487 retval = m_numel; |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
488 else |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
489 // Range crosses negative/positive and hits zero. |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
490 retval = m_numel - 1; |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
491 } |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
492 else |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
493 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
494 // All elements are equal (m_inc = 0) but not positive or negative, |
24859
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
495 // therefore all elements are zero. |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
496 retval = 0; |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
497 } |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
498 } |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
499 |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
500 return retval; |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
501 } |
00ecff875f8a
Add nnz implementation for Range type (bug #53185).
maorshutman <maorus12@gmail.com>
parents:
24799
diff
changeset
|
502 |
645 | 503 Matrix |
504 Range::matrix_value (void) const | |
505 { | |
28524
455fe4a6f22c
deprecate arithmetic operators for ranges; eliminate use in Octave
John W. Eaton <jwe@octave.org>
parents:
28402
diff
changeset
|
506 Matrix retval (1, m_numel); |
20531
dcfbf4c1c3c8
eliminate trailing whitespace and tabs from sources
John W. Eaton <jwe@octave.org>
parents:
20513
diff
changeset
|
507 |
28628
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
508 if (m_numel > 0) |
645 | 509 { |
20513 | 510 // The first element must always be *exactly* the base. |
511 // E.g, -0 would otherwise become +0 in the loop (-0 + 0*increment). | |
28628
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
512 retval(0) = m_base; |
20513 | 513 |
28628
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
514 double b = m_base; |
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
515 double increment = m_inc; |
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
516 for (octave_idx_type i = 1; i < m_numel - 1; i++) |
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
517 retval.xelem (i) = b + i * increment; |
4791 | 518 |
28628
83172e1c77f2
refactor octave_read_* and octave_write_* functions
John W. Eaton <jwe@octave.org>
parents:
28592
diff
changeset
|
519 retval.xelem (m_numel - 1) = m_limit; |
645 | 520 } |
521 | |
28524
455fe4a6f22c
deprecate arithmetic operators for ranges; eliminate use in Octave
John W. Eaton <jwe@octave.org>
parents:
28402
diff
changeset
|
522 return retval; |
645 | 523 } |
3 | 524 |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
525 double |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
526 Range::checkelem (octave_idx_type i) const |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
527 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
528 if (i < 0 || i >= m_numel) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
529 octave::err_index_out_of_range (2, 2, i+1, m_numel, dims ()); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
530 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
531 if (i == 0) |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
532 return m_base; |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
533 else if (i < m_numel - 1) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
534 return m_base + i * m_inc; |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
535 else |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
536 return m_limit; |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
537 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
538 |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
539 double |
24780
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
540 Range::checkelem (octave_idx_type i, octave_idx_type j) const |
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
541 { |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
542 // Ranges are *always* row vectors. |
24780
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
543 if (i != 0) |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
544 octave::err_index_out_of_range (1, 1, i+1, m_numel, dims ()); |
24780
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
545 |
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
546 return checkelem (j); |
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
547 } |
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
548 |
0d21e2a1cdfc
new element access operators for Range objects
John W. Eaton <jwe@octave.org>
parents:
24534
diff
changeset
|
549 double |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
550 Range::elem (octave_idx_type i) const |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
551 { |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
552 if (i == 0) |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
553 return m_base; |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
554 else if (i < m_numel - 1) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
555 return m_base + i * m_inc; |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
556 else |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
557 return m_limit; |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
558 } |
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
559 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
560 Array<double> |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
561 Range::index (const octave::idx_vector& idx) const |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
562 { |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
563 Array<double> retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
564 |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
565 octave_idx_type n = m_numel; |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
566 |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
567 if (idx.is_colon ()) |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
568 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
569 retval = matrix_value ().reshape (dim_vector (m_numel, 1)); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
570 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
571 else |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
572 { |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
573 if (idx.extent (n) != n) |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
574 octave::err_index_out_of_range (1, 1, idx.extent (n), n, dims ()); // throws |
10366
e5ae13b8b2c2
improve Array indexing error messages
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
575 |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
576 dim_vector idx_dims = idx.orig_dimensions (); |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
577 octave_idx_type idx_len = idx.length (n); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
578 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
579 // taken from Array.cc. |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
580 if (n != 1 && idx_dims.isvector ()) |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
581 idx_dims = dim_vector (1, idx_len); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
582 |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
583 retval.clear (idx_dims); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
584 |
30189
41eecdf76372
* Range.h, Range.cc: Fix comments missed in previous change.
John W. Eaton <jwe@octave.org>
parents:
30188
diff
changeset
|
585 // Loop over all values in IDX, executing the lambda expression |
41eecdf76372
* Range.h, Range.cc: Fix comments missed in previous change.
John W. Eaton <jwe@octave.org>
parents:
30188
diff
changeset
|
586 // for each index value. |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
587 |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
588 double *array = retval.fortran_vec (); |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
589 |
30394
f3f3e3793fb5
maint: style check C++ files in liboctave/ ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30189
diff
changeset
|
590 idx.loop (n, [=, &array] (idx_vector i) |
f3f3e3793fb5
maint: style check C++ files in liboctave/ ahead of 7.1 release.
Rik <rik@octave.org>
parents:
30189
diff
changeset
|
591 { |
30188
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
592 if (i == 0) |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
593 *array++ = m_base; |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
594 else if (i < m_numel - 1) |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
595 *array++ = m_base + i * m_inc; |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
596 else |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
597 *array++ = m_limit; |
e424d966dca7
replace rangeidx helper classes with lambda expressions
John W. Eaton <jwe@octave.org>
parents:
30159
diff
changeset
|
598 }); |
9986
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
599 } |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
600 |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
601 return retval; |
672e1b49e01e
optimize indexing of ranges by single subscripts
Jaroslav Hajek <highegg@gmail.com>
parents:
8971
diff
changeset
|
602 } |
4810 | 603 |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
604 // NOTE: max and min only return useful values if numel > 0. |
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
605 // do_minmax_body() in max.cc avoids calling Range::min/max if numel == 0. |
208 | 606 |
607 double | |
608 Range::min (void) const | |
609 { | |
610 double retval = 0.0; | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
611 if (m_numel > 0) |
208 | 612 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
613 if (m_inc > 0) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
614 retval = m_base; |
208 | 615 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
616 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
617 retval = m_base + (m_numel - 1) * m_inc; |
4791 | 618 |
20513 | 619 // Require '<=' test. See note in max (). |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
620 if (retval <= m_limit) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
621 retval = m_limit; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
622 } |
4791 | 623 |
208 | 624 } |
625 return retval; | |
626 } | |
627 | |
628 double | |
629 Range::max (void) const | |
630 { | |
631 double retval = 0.0; | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
632 if (m_numel > 0) |
208 | 633 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
634 if (m_inc > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
635 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
636 retval = m_base + (m_numel - 1) * m_inc; |
4791 | 637 |
20513 | 638 // On some machines (x86 with extended precision floating point |
639 // arithmetic, for example) it is possible that we can overshoot the | |
640 // limit by approximately the machine precision even though we were | |
641 // very careful in our calculation of the number of elements. | |
642 // Therefore, we clip the result to the limit if it overshoots. | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
643 // The test also includes equality (>= m_limit) to have expressions |
20513 | 644 // such as -5:1:-0 result in a -0 endpoint. |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
645 if (retval >= m_limit) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
646 retval = m_limit; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
647 } |
208 | 648 else |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
649 retval = m_base; |
208 | 650 } |
651 return retval; | |
652 } | |
653 | |
654 void | |
7458 | 655 Range::sort_internal (bool ascending) |
208 | 656 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
657 if ((ascending && m_base > m_limit && m_inc < 0.0) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
658 || (! ascending && m_base < m_limit && m_inc > 0.0)) |
208 | 659 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
660 std::swap (m_base, m_limit); |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
661 m_inc = -m_inc; |
7458 | 662 } |
663 } | |
664 | |
665 void | |
666 Range::sort_internal (Array<octave_idx_type>& sidx, bool ascending) | |
667 { | |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
668 octave_idx_type nel = numel (); |
7458 | 669 |
670 sidx.resize (dim_vector (1, nel)); | |
671 | |
672 octave_idx_type *psidx = sidx.fortran_vec (); | |
673 | |
674 bool reverse = false; | |
675 | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
676 if ((ascending && m_base > m_limit && m_inc < 0.0) |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
677 || (! ascending && m_base < m_limit && m_inc > 0.0)) |
7458 | 678 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
679 std::swap (m_base, m_limit); |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
680 m_inc = -m_inc; |
7458 | 681 reverse = true; |
682 } | |
683 | |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23443
diff
changeset
|
684 octave_idx_type tmp = (reverse ? nel - 1 : 0); |
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23443
diff
changeset
|
685 octave_idx_type stp = (reverse ? -1 : 1); |
7458 | 686 |
7467 | 687 for (octave_idx_type i = 0; i < nel; i++, tmp += stp) |
7458 | 688 psidx[i] = tmp; |
689 } | |
690 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
691 Matrix |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
692 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
|
693 { |
8971 | 694 return matrix_value ().diag (k); |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
695 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7467
diff
changeset
|
696 |
7458 | 697 Range |
698 Range::sort (octave_idx_type dim, sortmode mode) const | |
699 { | |
700 Range retval = *this; | |
701 | |
702 if (dim == 1) | |
703 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
704 if (mode == ASCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
705 retval.sort_internal (true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
706 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
707 retval.sort_internal (false); |
7458 | 708 } |
709 else if (dim != 0) | |
710 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
711 | |
712 return retval; | |
713 } | |
714 | |
715 Range | |
716 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
|
717 sortmode mode) const |
7458 | 718 { |
719 Range retval = *this; | |
720 | |
721 if (dim == 1) | |
722 { | |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
723 if (mode == ASCENDING) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
724 retval.sort_internal (sidx, true); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7458
diff
changeset
|
725 else if (mode == DESCENDING) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
726 retval.sort_internal (sidx, false); |
7458 | 727 } |
728 else if (dim != 0) | |
729 (*current_liboctave_error_handler) ("Range::sort: invalid dimension"); | |
730 | |
731 return retval; | |
208 | 732 } |
733 | |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
734 sortmode |
23588
0549061d35b9
maint: Deprecate is_sorted and replace with issorted.
Rik <rik@octave.org>
parents:
23577
diff
changeset
|
735 Range::issorted (sortmode mode) const |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
736 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
737 if (m_numel > 1 && m_inc > 0) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
738 mode = (mode == DESCENDING) ? UNSORTED : ASCENDING; |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
739 else if (m_numel > 1 && m_inc < 0) |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
740 mode = (mode == ASCENDING) ? UNSORTED : DESCENDING; |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
741 else |
26444
5c91daa02f87
Fix static analyzer detected issues with UNSORTED enum (bug #55347).
Rik <rik@octave.org>
parents:
26376
diff
changeset
|
742 mode = (mode == UNSORTED) ? ASCENDING : mode; |
8742
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
743 |
d2b06871afac
add missing return statement
Jaroslav Hajek <highegg@gmail.com>
parents:
8721
diff
changeset
|
744 return mode; |
8721
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
745 } |
e9cb742df9eb
imported patch sort3.diff
Jaroslav Hajek <highegg@gmail.com>
parents:
8589
diff
changeset
|
746 |
20513 | 747 void |
748 Range::set_base (double b) | |
749 { | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
750 if (m_base != b) |
20513 | 751 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
752 m_base = b; |
20513 | 753 |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
754 init (); |
20513 | 755 } |
756 } | |
757 | |
758 void | |
759 Range::set_limit (double l) | |
760 { | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
761 if (m_limit != l) |
20513 | 762 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
763 m_limit = l; |
20513 | 764 |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
765 init (); |
20513 | 766 } |
767 } | |
768 | |
769 void | |
770 Range::set_inc (double i) | |
771 { | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
772 if (m_inc != i) |
20513 | 773 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
774 m_inc = i; |
20513 | 775 |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
776 init (); |
20513 | 777 } |
778 } | |
779 | |
3504 | 780 std::ostream& |
781 operator << (std::ostream& os, const Range& a) | |
3 | 782 { |
783 double b = a.base (); | |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
784 double increment = a.increment (); |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
785 octave_idx_type nel = a.numel (); |
3 | 786 |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
787 if (nel > 1) |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
788 { |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
789 // First element must be the base *exactly* (e.g., -0). |
23807
336f89b6208b
Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents:
23662
diff
changeset
|
790 os << b << ' '; |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
791 for (octave_idx_type i = 1; i < nel-1; i++) |
23807
336f89b6208b
Use character literals 'c' rather than string literals "c" when possible.
Rik <rik@octave.org>
parents:
23662
diff
changeset
|
792 os << b + i * increment << ' '; |
16169
0303fda3e929
Fix range behavior with -0 endpoints (bug #38423)
Rik <rik@octave.org>
parents:
15271
diff
changeset
|
793 } |
3 | 794 |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
795 // Print out the last element exactly, rather than a calculated last element. |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
796 os << a.m_limit << "\n"; |
3 | 797 |
798 return os; | |
799 } | |
800 | |
3504 | 801 std::istream& |
802 operator >> (std::istream& is, Range& a) | |
3 | 803 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
804 is >> a.m_base; |
3 | 805 if (is) |
806 { | |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
807 double tmp_limit; |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
808 is >> tmp_limit; |
20513 | 809 |
3 | 810 if (is) |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
811 is >> a.m_inc; |
20513 | 812 |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
813 // Clip the m_limit to the true limit, rebuild numel, clear cache |
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
814 a.set_limit (tmp_limit); |
3 | 815 } |
816 | |
817 return is; | |
818 } | |
819 | |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
820 // DEPRECATED in Octave 7. |
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
821 Range operator - (const Range& r) |
2599 | 822 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
823 return Range (-r.base (), -r.limit (), -r.increment (), r.numel ()); |
2599 | 824 } |
825 | |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
826 // DEPRECATED in Octave 7. |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
827 Range operator + (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
828 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
829 return Range (x + r.base (), x + r.limit (), r.increment (), r.numel ()); |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
830 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
831 |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
832 // DEPRECATED in Octave 7. |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
833 Range operator + (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
834 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
835 return Range (r.base () + x, r.limit () + x, r.increment (), r.numel ()); |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
836 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
837 |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
838 // DEPRECATED in Octave 7. |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
839 Range operator - (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
840 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
841 return Range (x - r.base (), x - r.limit (), -r.increment (), r.numel ()); |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
842 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
843 |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
844 // DEPRECATED in Octave 7. |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
845 Range operator - (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
846 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
847 return Range (r.base () - x, r.limit () - x, r.increment (), r.numel ()); |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
848 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
849 |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
850 // DEPRECATED in Octave 7. |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
851 Range operator * (double x, const Range& r) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
852 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
853 return Range (x * r.base (), x * r.limit (), x * r.increment (), r.numel ()); |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
854 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
855 |
29954
4c88a452519c
rename OCTAVE_USE_DEPRECATED_FUNCTIONS macro and attempt to make it work
John W. Eaton <jwe@octave.org>
parents:
29569
diff
changeset
|
856 // DEPRECATED in Octave 7. |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
857 Range operator * (const Range& r, double x) |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
858 { |
28636
a3db48e66ef8
use Range::increment instead of Range::inc
John W. Eaton <jwe@octave.org>
parents:
28635
diff
changeset
|
859 return Range (r.base () * x, r.limit () * x, r.increment () * x, r.numel ()); |
8553
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
860 } |
c7ff200e45f5
optimize range-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
861 |
1546 | 862 // C See Knuth, Art Of Computer Programming, Vol. 1, Problem 1.2.4-5. |
863 // C | |
864 // C===Tolerant FLOOR function. | |
865 // C | |
866 // C X - is given as a Double Precision argument to be operated on. | |
867 // C It is assumed that X is represented with M mantissa bits. | |
868 // C CT - is given as a Comparison Tolerance such that | |
869 // C 0.LT.CT.LE.3-SQRT(5)/2. If the relative difference between | |
870 // C X and A whole number is less than CT, then TFLOOR is | |
871 // C returned as this whole number. By treating the | |
872 // C floating-point numbers as a finite ordered set note that | |
873 // C the heuristic EPS=2.**(-(M-1)) and CT=3*EPS causes | |
874 // C arguments of TFLOOR/TCEIL to be treated as whole numbers | |
875 // C if they are exactly whole numbers or are immediately | |
876 // C adjacent to whole number representations. Since EPS, the | |
877 // C "distance" between floating-point numbers on the unit | |
878 // C interval, and M, the number of bits in X'S mantissa, exist | |
879 // C on every floating-point computer, TFLOOR/TCEIL are | |
880 // C consistently definable on every floating-point computer. | |
881 // C | |
882 // C For more information see the following references: | |
883 // C (1) P. E. Hagerty, "More On Fuzzy Floor And Ceiling," APL QUOTE | |
884 // C QUAD 8(4):20-24, June 1978. Note that TFLOOR=FL5. | |
885 // C (2) L. M. Breed, "Definitions For Fuzzy Floor And Ceiling", APL | |
886 // C QUOTE QUAD 8(3):16-23, March 1978. This paper cites FL1 through | |
887 // C FL5, the history of five years of evolutionary development of | |
888 // C FL5 - the seven lines of code below - by open collaboration | |
889 // C and corroboration of the mathematical-computing community. | |
890 // C | |
891 // C Penn State University Center for Academic Computing | |
892 // C H. D. Knoble - August, 1978. | |
893 | |
894 static inline double | |
895 tfloor (double x, double ct) | |
896 { | |
897 // C---------FLOOR(X) is the largest integer algebraically less than | |
898 // C or equal to X; that is, the unfuzzy FLOOR function. | |
899 | |
900 // DINT (X) = X - DMOD (X, 1.0); | |
901 // FLOOR (X) = DINT (X) - DMOD (2.0 + DSIGN (1.0, X), 3.0); | |
902 | |
903 // C---------Hagerty's FL5 function follows... | |
904 | |
905 double q = 1.0; | |
906 | |
907 if (x < 0.0) | |
908 q = 1.0 - ct; | |
909 | |
910 double rmax = q / (2.0 - ct); | |
911 | |
21942
aab79a1885cc
limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
912 double t1 = 1.0 + std::floor (x); |
1546 | 913 t1 = (ct / q) * (t1 < 0.0 ? -t1 : t1); |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23443
diff
changeset
|
914 t1 = (rmax < t1 ? rmax : t1); |
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23443
diff
changeset
|
915 t1 = (ct > t1 ? ct : t1); |
21942
aab79a1885cc
limit gnulib headers to liboctave/wrappers directory
John W. Eaton <jwe@octave.org>
parents:
21782
diff
changeset
|
916 t1 = std::floor (x + t1); |
1546 | 917 |
1555 | 918 if (x <= 0.0 || (t1 - x) < rmax) |
1546 | 919 return t1; |
920 else | |
921 return t1 - 1.0; | |
922 } | |
923 | |
3753 | 924 static inline bool |
17769
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
925 teq (double u, double v, |
49a5a4be04a1
maint: Use GNU style coding conventions for code in liboctave/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
926 double ct = 3.0 * std::numeric_limits<double>::epsilon ()) |
3753 | 927 { |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
928 double tu = std::abs (u); |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
929 double tv = std::abs (v); |
3753 | 930 |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
931 return std::abs (u - v) < ((tu > tv ? tu : tv) * ct); |
3753 | 932 } |
933 | |
5275 | 934 octave_idx_type |
20228
00cf2847355d
Deprecate Array::nelem() and Range::nelem() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
935 Range::numel_internal (void) const |
1360 | 936 { |
5275 | 937 octave_idx_type retval = -1; |
3 | 938 |
28592
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
939 if (! octave::math::isfinite (m_base) || ! octave::math::isfinite (m_inc) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
940 || octave::math::isnan (m_limit)) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
941 retval = -2; |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
942 else if (octave::math::isinf (m_limit) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
943 && ((m_inc > 0 && m_limit > 0) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
944 || (m_inc < 0 && m_limit < 0))) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
945 retval = std::numeric_limits<octave_idx_type>::max () - 1; |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
946 else if (m_inc == 0 |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
947 || (m_limit > m_base && m_inc < 0) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
948 || (m_limit < m_base && m_inc > 0)) |
3858 | 949 { |
950 retval = 0; | |
951 } | |
952 else | |
953 { | |
15220
61822c866ba1
use std::numeric_limits<T>::epsilon in C++ code
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
954 double ct = 3.0 * std::numeric_limits<double>::epsilon (); |
3 | 955 |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
956 double tmp = tfloor ((m_limit - m_base + m_inc) / m_inc, ct); |
3858 | 957 |
28592
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
958 octave_idx_type n_elt = (tmp > 0.0 |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
959 ? static_cast<octave_idx_type> (tmp) : 0); |
398 | 960 |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
961 // If the final element that we would compute for the range is equal to |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
962 // the limit of the range, or is an adjacent floating point number, |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
963 // accept it. Otherwise, try a range with one fewer element. If that |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
964 // fails, try again with one more element. |
3858 | 965 // |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
966 // I'm not sure this is very good, but it seems to work better than just |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
967 // using tfloor as above. For example, without it, the expression |
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
968 // 1.8:0.05:1.9 fails to produce the expected result of [1.8, 1.85, 1.9]. |
3753 | 969 |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
970 if (! teq (m_base + (n_elt - 1) * m_inc, m_limit)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
971 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
972 if (teq (m_base + (n_elt - 2) * m_inc, m_limit)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
973 n_elt--; |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
974 else if (teq (m_base + n_elt * m_inc, m_limit)) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
975 n_elt++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
976 } |
3858 | 977 |
28592
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
978 retval = ((n_elt < std::numeric_limits<octave_idx_type>::max ()) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
979 ? n_elt : -1); |
3753 | 980 } |
981 | |
3858 | 982 return retval; |
3 | 983 } |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
984 |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
985 double |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
986 Range::limit_internal (void) const |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
987 { |
28592
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
988 double new_limit = m_inc > 0 ? max () : min (); |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
989 |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
990 // If result must be an integer then force the new_limit to be one. |
25808
dc47c9e48801
Guarantee an integer for max of Range when base and increment are ints (bug #46859).
Rik <rik@octave.org>
parents:
25438
diff
changeset
|
991 if (all_elements_are_ints ()) |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
992 new_limit = std::round (new_limit); |
25808
dc47c9e48801
Guarantee an integer for max of Range when base and increment are ints (bug #46859).
Rik <rik@octave.org>
parents:
25438
diff
changeset
|
993 |
25812
fc74c8d2a584
Clean up liboctave Range implementation.
Rik <rik@octave.org>
parents:
25808
diff
changeset
|
994 return new_limit; |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
995 } |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
996 |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
997 void |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
998 Range::init (void) |
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
999 { |
28402
09a3c0e91e6e
style update for Range class
John W. Eaton <jwe@octave.org>
parents:
28004
diff
changeset
|
1000 m_numel = numel_internal (); |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
1001 |
28592
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
1002 if (! octave::math::isinf (m_limit)) |
e70c859c4bff
allow infinite limits on for loop ranges (bug #45143)
John W. Eaton <jwe@octave.org>
parents:
28524
diff
changeset
|
1003 m_limit = limit_internal (); |
21321
79a51b7e00b6
eliminate some copy and paste code fragments in the Range class
John W. Eaton <jwe@octave.org>
parents:
21301
diff
changeset
|
1004 } |