GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Range.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1993-2018 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software: you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_Range_h)
24 #define octave_Range_h 1
25 
26 #include "octave-config.h"
27 
28 #include <iosfwd>
29 
30 #include "dMatrix.h"
31 #include "oct-sort.h"
32 
33 class
34 OCTAVE_API
35 Range
36 {
37 public:
38 
39  Range (void)
40  : rng_base (0), rng_limit (0), rng_inc (0), rng_numel (0), cache (1, 0) { }
41 
42  Range (const Range& r)
43  : rng_base (r.rng_base), rng_limit (r.rng_limit), rng_inc (r.rng_inc),
44  rng_numel (r.rng_numel), cache (r.cache) { }
45 
46  Range (double b, double l)
47  : rng_base (b), rng_limit (l), rng_inc (1),
48  rng_numel (numel_internal ()), cache ()
49  {
50  rng_limit = limit_internal ();
51  }
52 
53  Range (double b, double l, double i)
54  : rng_base (b), rng_limit (l), rng_inc (i),
55  rng_numel (numel_internal ()), cache ()
56  {
57  rng_limit = limit_internal ();
58  }
59 
60  // For operators' usage (to preserve element count).
61  Range (double b, double i, octave_idx_type n)
62  : rng_base (b), rng_limit (b + (n-1) * i), rng_inc (i),
63  rng_numel (n), cache ()
64  {
66  || ! octave::math::isfinite (rng_limit))
67  rng_numel = -2;
68  else
69  {
70  // Code below is only needed if the resulting range must be 100%
71  // correctly constructed. If the Range object created is only
72  // a temporary one used by operators this may be unnecessary.
73 
74  rng_limit = limit_internal ();
75  }
76  }
77 
78  double base (void) const { return rng_base; }
79  double limit (void) const { return rng_limit; }
80  double inc (void) const { return rng_inc; }
81 
82  OCTAVE_DEPRECATED (4.4, "use 'numel' instead")
83  octave_idx_type nelem (void) const { return numel (); }
84 
85  octave_idx_type numel (void) const { return rng_numel; }
86 
87  octave_idx_type rows (void) const { return 1; }
88 
89  octave_idx_type cols (void) const { return numel (); }
90  octave_idx_type columns (void) const { return numel (); }
91 
92  bool isempty (void) const { return numel () == 0; }
93 
94  OCTAVE_DEPRECATED (4.4, "use 'isempty' instead")
95  bool is_empty (void) const
96  { return isempty (); }
97 
98  bool all_elements_are_ints (void) const;
99 
100  Matrix matrix_value (void) const;
101 
102  double min (void) const;
103  double max (void) const;
104 
105  void sort_internal (bool ascending = true);
106  void sort_internal (Array<octave_idx_type>& sidx, bool ascending = true);
107 
108  Matrix diag (octave_idx_type k = 0) const;
109 
110  Range sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
111 
112  Range sort (Array<octave_idx_type>& sidx, octave_idx_type dim = 0,
113  sortmode mode = ASCENDING) const;
114 
115  sortmode issorted (sortmode mode = ASCENDING) const;
116 
117  OCTAVE_DEPRECATED (4.4, "use 'issorted' instead")
118  sortmode is_sorted (sortmode mode = ASCENDING) const
119  { return issorted (mode); }
120 
121  octave_idx_type nnz (void) const;
122 
123  // Support for single-index subscripting, without generating matrix cache.
124 
125  double checkelem (octave_idx_type i) const;
126  double checkelem (octave_idx_type i, octave_idx_type j) const;
127 
128  double elem (octave_idx_type i) const;
129  double elem (octave_idx_type /* i */, octave_idx_type j) const
130  { return elem (j); }
131 
132  double operator () (octave_idx_type i) const { return elem (i); }
133  double operator () (octave_idx_type i, octave_idx_type j) const
134  { return elem (i, j); }
135 
136  Array<double> index (const idx_vector& i) const;
137 
138  void set_base (double b);
139 
140  void set_limit (double l);
141 
142  void set_inc (double i);
143 
144  friend OCTAVE_API std::ostream& operator << (std::ostream& os,
145  const Range& r);
146  friend OCTAVE_API std::istream& operator >> (std::istream& is, Range& r);
147 
148  friend OCTAVE_API Range operator - (const Range& r);
149  friend OCTAVE_API Range operator + (double x, const Range& r);
150  friend OCTAVE_API Range operator + (const Range& r, double x);
151  friend OCTAVE_API Range operator - (double x, const Range& r);
152  friend OCTAVE_API Range operator - (const Range& r, double x);
153  friend OCTAVE_API Range operator * (double x, const Range& r);
154  friend OCTAVE_API Range operator * (const Range& r, double x);
155 
156 private:
157 
158  double rng_base;
159  double rng_limit;
160  double rng_inc;
161 
163 
164  mutable Matrix cache;
165 
166  octave_idx_type numel_internal (void) const;
167 
168  double limit_internal (void) const;
169 
170  void init (void);
171 
172  void clear_cache (void) const { cache.resize (0, 0); }
173 
174 protected:
175 
176  // For operators' usage (to allow all values to be set directly).
177  Range (double b, double l, double i, octave_idx_type n)
178  : rng_base (b), rng_limit (l), rng_inc (i),
179  rng_numel (n), cache ()
180  {
182  || ! octave::math::isfinite (l))
183  rng_numel = -2;
184  }
185 };
186 
187 extern OCTAVE_API Range operator - (const Range& r);
188 
189 extern OCTAVE_API Range operator + (double x, const Range& r);
190 
191 extern OCTAVE_API Range operator + (const Range& r, double x);
192 
193 extern OCTAVE_API Range operator - (double x, const Range& r);
194 
195 extern OCTAVE_API Range operator - (const Range& r, double x);
196 
197 extern OCTAVE_API Range operator * (double x, const Range& r);
198 
199 extern OCTAVE_API Range operator * (const Range& r, double x);
200 
201 #endif
OCTAVE_API Range operator-(const Range &r)
Definition: Range.cc:419
octave_idx_type columns(void) const
Definition: Range.h:90
Matrix cache
Definition: Range.h:164
Range(void)
Definition: Range.h:39
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition: dMatrix.h:148
std::istream & operator>>(std::istream &is, SparseBoolMatrix &a)
Definition: boolSparse.cc:279
double rng_limit
Definition: Range.h:159
sortmode
Definition: oct-sort.h:105
Return the CPU time used by your Octave session The first output is the total time spent executing your process and is equal to the sum of second and third which are the number of CPU seconds spent executing in user mode and the number of CPU seconds spent executing in system mode
Definition: data.cc:6348
octave_idx_type cols(void) const
Definition: Range.h:89
for large enough k
Definition: lu.cc:617
Definition: Range.h:33
void clear_cache(void) const
Definition: Range.h:172
double rng_base
Definition: Range.h:158
double elem(octave_idx_type, octave_idx_type j) const
Definition: Range.h:129
octave_idx_type rows(void) const
Definition: Range.h:87
double limit(void) const
Definition: Range.h:79
double base(void) const
Definition: Range.h:78
Range(const Range &r)
Definition: Range.h:42
static int elem
Definition: __contourc__.cc:47
octave_idx_type numel(void) const
Definition: Range.h:85
Definition: dMatrix.h:36
octave_idx_type rng_numel
Definition: Range.h:162
Range(double b, double i, octave_idx_type n)
Definition: Range.h:61
Range(double b, double l, double i)
Definition: Range.h:53
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:227
T::size_type numel(const T &str)
Definition: oct-string.cc:61
Range(double b, double l)
Definition: Range.h:46
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
bool isfinite(double x)
Definition: lo-mappers.h:201
b
Definition: cellfun.cc:400
OCTAVE_API Range operator*(double x, const Range &r)
Definition: Range.cc:460
for i
Definition: data.cc:5264
OCTAVE_API Range operator+(double x, const Range &r)
Definition: Range.cc:424
double rng_inc
Definition: Range.h:160
Range(double b, double l, double i, octave_idx_type n)
Definition: Range.h:177
write the output to stdout if nargout is
Definition: load-save.cc:1612
bool isempty(void) const
Definition: Range.h:92
octave::stream os
Definition: file-io.cc:627
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE * x
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:204
double inc(void) const
Definition: Range.h:80