GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Range.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1993-2017 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 the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://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::finite (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 ("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  bool is_empty (void) const { return numel () == 0; }
88 
89  bool all_elements_are_ints (void) const;
90 
91  Matrix matrix_value (void) const;
92 
93  double min (void) const;
94  double max (void) const;
95 
96  void sort_internal (bool ascending = true);
97  void sort_internal (Array<octave_idx_type>& sidx, bool ascending = true);
98 
99  Matrix diag (octave_idx_type k = 0) const;
100 
101  Range sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
102 
103  Range sort (Array<octave_idx_type>& sidx, octave_idx_type dim = 0,
104  sortmode mode = ASCENDING) const;
105 
106  sortmode is_sorted (sortmode mode = ASCENDING) const;
107 
108  // Support for single-index subscripting, without generating matrix cache.
109 
110  double checkelem (octave_idx_type i) const;
111 
112  double elem (octave_idx_type i) const;
113 
114  Array<double> index (const idx_vector& i) const;
115 
116  void set_base (double b);
117 
118  void set_limit (double l);
119 
120  void set_inc (double i);
121 
122  friend OCTAVE_API std::ostream& operator << (std::ostream& os,
123  const Range& r);
124  friend OCTAVE_API std::istream& operator >> (std::istream& is, Range& r);
125 
126  friend OCTAVE_API Range operator - (const Range& r);
127  friend OCTAVE_API Range operator + (double x, const Range& r);
128  friend OCTAVE_API Range operator + (const Range& r, double x);
129  friend OCTAVE_API Range operator - (double x, const Range& r);
130  friend OCTAVE_API Range operator - (const Range& r, double x);
131  friend OCTAVE_API Range operator * (double x, const Range& r);
132  friend OCTAVE_API Range operator * (const Range& r, double x);
133 
134 private:
135 
136  double rng_base;
137  double rng_limit;
138  double rng_inc;
139 
141 
142  mutable Matrix cache;
143 
144  octave_idx_type numel_internal (void) const;
145 
146  double limit_internal (void) const;
147 
148  void init (void);
149 
150  void clear_cache (void) const { cache.resize (0, 0); }
151 
152 protected:
153 
154  // For operators' usage (to allow all values to be set directly).
155  Range (double b, double l, double i, octave_idx_type n)
156  : rng_base (b), rng_limit (l), rng_inc (i),
157  rng_numel (n), cache ()
158  {
159  if (! octave::math::finite (b) || ! octave::math::finite (i)
160  || ! octave::math::finite (l))
161  rng_numel = -2;
162  }
163 };
164 
165 extern OCTAVE_API Range operator - (const Range& r);
166 
167 extern OCTAVE_API Range operator + (double x, const Range& r);
168 
169 extern OCTAVE_API Range operator + (const Range& r, double x);
170 
171 extern OCTAVE_API Range operator - (double x, const Range& r);
172 
173 extern OCTAVE_API Range operator - (const Range& r, double x);
174 
175 extern OCTAVE_API Range operator * (double x, const Range& r);
176 
177 extern OCTAVE_API Range operator * (const Range& r, double x);
178 
179 #endif
OCTAVE_API Range operator-(const Range &r)
Definition: Range.cc:380
Matrix cache
Definition: Range.h:142
Range(void)
Definition: Range.h:39
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition: dMatrix.h:145
std::istream & operator>>(std::istream &is, SparseBoolMatrix &a)
Definition: boolSparse.cc:279
double rng_limit
Definition: Range.h:137
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:6386
for large enough k
Definition: lu.cc:606
Definition: Range.h:33
double rng_base
Definition: Range.h:136
double limit(void) const
Definition: Range.h:79
double inc(void) const
Definition: Range.h:80
octave_idx_type numel(void) const
Definition: Range.h:85
Range(const Range &r)
Definition: Range.h:42
bool is_empty(void) const
Definition: Range.h:87
static int elem
Definition: __contourc__.cc:50
bool finite(double x)
Definition: lo-mappers.cc:367
Definition: dMatrix.h:37
octave_idx_type rng_numel
Definition: Range.h:140
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:228
T::size_type numel(const T &str)
Definition: oct-string.cc:61
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
Range(double b, double l)
Definition: Range.h:46
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
b
Definition: cellfun.cc:398
double base(void) const
Definition: Range.h:78
OCTAVE_API Range operator*(double x, const Range &r)
Definition: Range.cc:421
OCTAVE_API Range operator+(double x, const Range &r)
Definition: Range.cc:385
double rng_inc
Definition: Range.h:138
Range(double b, double l, double i, octave_idx_type n)
Definition: Range.h:155
write the output to stdout if nargout is
Definition: load-save.cc:1576
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &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 F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE * x
void clear_cache(void) const
Definition: Range.h:150
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:205