GNU Octave  6.2.0
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-2021 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_Range_h)
27 #define octave_Range_h 1
28 
29 #include "octave-config.h"
30 
31 #include <iosfwd>
32 
33 #include "dMatrix.h"
34 #include "dim-vector.h"
35 #include "oct-sort.h"
36 
37 class
38 OCTAVE_API
39 Range
40 {
41 public:
42 
43  Range (void)
44  : rng_base (0), rng_limit (0), rng_inc (0), rng_numel (0), cache (1, 0) { }
45 
46  Range (const Range& r) = default;
47 
48  Range& operator = (const Range& r) = default;
49 
50  ~Range (void) = default;
51 
52  Range (double b, double l)
53  : rng_base (b), rng_limit (l), rng_inc (1),
54  rng_numel (numel_internal ()), cache ()
55  {
56  rng_limit = limit_internal ();
57  }
58 
59  Range (double b, double l, double i)
60  : rng_base (b), rng_limit (l), rng_inc (i),
61  rng_numel (numel_internal ()), cache ()
62  {
63  rng_limit = limit_internal ();
64  }
65 
66  // For operators' usage (to preserve element count).
67  Range (double b, double i, octave_idx_type n)
68  : rng_base (b), rng_limit (b + (n-1) * i), rng_inc (i),
69  rng_numel (n), cache ()
70  {
72  || ! octave::math::isfinite (rng_limit))
73  rng_numel = -2;
74  else
75  {
76  // Code below is only needed if the resulting range must be 100%
77  // correctly constructed. If the Range object created is only
78  // a temporary one used by operators this may be unnecessary.
79  rng_limit = limit_internal ();
80  }
81  }
82 
83  double base (void) const { return rng_base; }
84  double limit (void) const { return rng_limit; }
85  double inc (void) const { return rng_inc; }
86 
87  octave_idx_type numel (void) const { return rng_numel; }
88 
89  dim_vector dims (void) const { return dim_vector (1, rng_numel); }
90 
91  octave_idx_type rows (void) const { return 1; }
92 
93  octave_idx_type cols (void) const { return numel (); }
94  octave_idx_type columns (void) const { return numel (); }
95 
96  bool isempty (void) const { return numel () == 0; }
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  Range sort (Array<octave_idx_type>& sidx, octave_idx_type dim = 0,
112  sortmode mode = ASCENDING) const;
113 
114  sortmode issorted (sortmode mode = ASCENDING) const;
115 
116  octave_idx_type nnz (void) const;
117 
118  // Support for single-index subscripting, without generating matrix cache.
119 
120  double checkelem (octave_idx_type i) const;
121  double checkelem (octave_idx_type i, octave_idx_type j) const;
122 
123  double elem (octave_idx_type i) const;
124  double elem (octave_idx_type /* i */, octave_idx_type j) const
125  { return elem (j); }
126 
127  double operator () (octave_idx_type i) const { return elem (i); }
128  double operator () (octave_idx_type i, octave_idx_type j) const
129  { return elem (i, j); }
130 
131  Array<double> index (const idx_vector& i) const;
132 
133  void set_base (double b);
134 
135  void set_limit (double l);
136 
137  void set_inc (double i);
138 
139  friend OCTAVE_API std::ostream& operator << (std::ostream& os,
140  const Range& r);
141  friend OCTAVE_API std::istream& operator >> (std::istream& is, Range& r);
142 
143  friend OCTAVE_API Range operator - (const Range& r);
144  friend OCTAVE_API Range operator + (double x, const Range& r);
145  friend OCTAVE_API Range operator + (const Range& r, double x);
146  friend OCTAVE_API Range operator - (double x, const Range& r);
147  friend OCTAVE_API Range operator - (const Range& r, double x);
148  friend OCTAVE_API Range operator * (double x, const Range& r);
149  friend OCTAVE_API Range operator * (const Range& r, double x);
150 
151 private:
152 
153  double rng_base;
154  double rng_limit;
155  double rng_inc;
156 
158 
159  mutable Matrix cache;
160 
161  octave_idx_type numel_internal (void) const;
162 
163  double limit_internal (void) const;
164 
165  void init (void);
166 
167  void clear_cache (void) const { cache.resize (0, 0); }
168 
169 protected:
170 
171  // For operators' usage (to allow all values to be set directly).
172  Range (double b, double l, double i, octave_idx_type n)
173  : rng_base (b), rng_limit (l), rng_inc (i),
174  rng_numel (n), cache ()
175  {
177  || ! octave::math::isfinite (l))
178  rng_numel = -2;
179  }
180 };
181 
182 extern OCTAVE_API Range operator - (const Range& r);
183 extern OCTAVE_API Range operator + (double x, const Range& r);
184 extern OCTAVE_API Range operator + (const Range& r, double x);
185 extern OCTAVE_API Range operator - (double x, const Range& r);
186 extern OCTAVE_API Range operator - (const Range& r, double x);
187 extern OCTAVE_API Range operator * (double x, const Range& r);
188 extern OCTAVE_API Range operator * (const Range& r, double x);
189 
190 #endif
template OCTAVE_API std::ostream & operator<<(std::ostream &, const Array< bool > &)
OCTAVE_API Range operator+(double x, const Range &r)
Definition: Range.cc:429
OCTAVE_API Range operator*(double x, const Range &r)
Definition: Range.cc:468
OCTAVE_API Range operator-(const Range &r)
Definition: Range.cc:424
static int elem
Definition: __contourc__.cc:52
std::istream & operator>>(std::istream &is, SparseBoolMatrix &a)
Definition: boolSparse.cc:281
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:230
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:207
Definition: dMatrix.h:42
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Definition: dMatrix.h:151
Definition: Range.h:40
bool isempty(void) const
Definition: Range.h:96
void clear_cache(void) const
Definition: Range.h:167
octave_idx_type rows(void) const
Definition: Range.h:91
double rng_base
Definition: Range.h:153
dim_vector dims(void) const
Definition: Range.h:89
Range(const Range &r)=default
octave_idx_type cols(void) const
Definition: Range.h:93
double inc(void) const
Definition: Range.h:85
double base(void) const
Definition: Range.h:83
Range(double b, double l)
Definition: Range.h:52
~Range(void)=default
double rng_limit
Definition: Range.h:154
Range(void)
Definition: Range.h:43
Range(double b, double l, double i, octave_idx_type n)
Definition: Range.h:172
octave_idx_type rng_numel
Definition: Range.h:157
Matrix cache
Definition: Range.h:159
double limit(void) const
Definition: Range.h:84
Range(double b, double i, octave_idx_type n)
Definition: Range.h:67
octave_idx_type columns(void) const
Definition: Range.h:94
octave_idx_type numel(void) const
Definition: Range.h:87
Range(double b, double l, double i)
Definition: Range.h:59
double rng_inc
Definition: Range.h:155
double elem(octave_idx_type, octave_idx_type j) const
Definition: Range.h:124
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:95
F77_RET_T const F77_DBLE * x
octave_idx_type n
Definition: mx-inlines.cc:753
T * r
Definition: mx-inlines.cc:773
bool isfinite(double x)
Definition: lo-mappers.h:192
sortmode
Definition: oct-sort.h:95
@ ASCENDING
Definition: oct-sort.h:95
T::size_type numel(const T &str)
Definition: oct-string.cc:71