GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-range.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-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_ov_range_h)
24 #define octave_ov_range_h 1
25 
26 #include "octave-config.h"
27 
28 #include <cstdlib>
29 
30 #include <iosfwd>
31 #include <string>
32 
33 #include "Range.h"
34 
35 #include "lo-mappers.h"
36 #include "lo-utils.h"
37 #include "mx-base.h"
38 #include "str-vec.h"
39 
40 #include "error.h"
41 #include "oct-stream.h"
42 #include "ov-base.h"
43 #include "ov-re-mat.h"
44 #include "ov-typeinfo.h"
45 
46 class octave_value_list;
47 
48 // Range values.
49 
50 class
52 {
53 public:
54 
55  octave_range (void)
56  : octave_base_value (), range (), idx_cache () { }
57 
58  octave_range (double base, double limit, double inc)
59  : octave_base_value (), range (base, limit, inc), idx_cache ()
60  {
61  if (range.numel () < 0)
62  error ("invalid range");
63  }
64 
65  octave_range (const Range& r)
66  : octave_base_value (), range (r), idx_cache ()
67  {
68  if (range.numel () < 0 && range.numel () != -2)
69  error ("invalid range");
70  }
71 
73  : octave_base_value (), range (r.range),
74  idx_cache (r.idx_cache ? new idx_vector (*r.idx_cache) : nullptr)
75  { }
76 
77  octave_range (const Range& r, const idx_vector& cache)
78  : octave_base_value (), range (r), idx_cache ()
79  {
80  set_idx_cache (cache);
81  }
82 
83  ~octave_range (void) { clear_cached_info (); }
84 
85  octave_base_value * clone (void) const { return new octave_range (*this); }
86 
87  // A range is really just a special kind of real matrix object. In
88  // the places where we need to call empty_clone, it makes more sense
89  // to create an empty matrix (0x0) instead of an empty range (1x0).
90  octave_base_value * empty_clone (void) const { return new octave_matrix (); }
91 
92  type_conv_info numeric_conversion_function (void) const;
93 
94  octave_base_value * try_narrowing_conversion (void);
95 
97  const std::list<octave_value_list>& idx);
98 
100  const std::list<octave_value_list>& idx, int)
101  { return subsref (type, idx); }
102 
103  octave_value do_index_op (const octave_value_list& idx,
104  bool resize_ok = false);
105 
106  idx_vector index_vector (bool require_integers = false) const;
107 
108  dim_vector dims (void) const
109  {
110  octave_idx_type n = range.numel ();
111  return dim_vector (n > 0, n);
112  }
113 
114  octave_idx_type nnz (void) const { return range.nnz (); }
115 
116  octave_value resize (const dim_vector& dv, bool fill = false) const;
117 
118  size_t byte_size (void) const { return 3 * sizeof (double); }
119 
120  octave_value reshape (const dim_vector& new_dims) const
121  { return NDArray (array_value ().reshape (new_dims)); }
122 
123  octave_value permute (const Array<int>& vec, bool inv = false) const
124  { return NDArray (array_value ().permute (vec, inv)); }
125 
126  octave_value squeeze (void) const { return range; }
127 
128  octave_value full_value (void) const { return range.matrix_value (); }
129 
130  bool is_defined (void) const { return true; }
131 
132  bool is_constant (void) const { return true; }
133 
134  bool is_range (void) const { return true; }
135 
136  octave_value all (int dim = 0) const;
137 
138  octave_value any (int dim = 0) const;
139 
140  octave_value diag (octave_idx_type k = 0) const;
141 
142  octave_value diag (octave_idx_type m, octave_idx_type n) const;
143 
145  { return range.sort (dim, mode); }
146 
148  sortmode mode = ASCENDING) const
149  { return range.sort (sidx, dim, mode); }
150 
152  { return range.issorted (mode); }
153 
155  { return Array<octave_idx_type> (dim_vector (1, 0)); }
156 
158  { return mode ? mode : ASCENDING; }
159 
160  builtin_type_t builtin_type (void) const { return btyp_double; }
161 
162  bool isreal (void) const { return true; }
163 
164  bool is_double_type (void) const { return true; }
165 
166  bool isfloat (void) const { return true; }
167 
168  bool isnumeric (void) const { return true; }
169 
170  bool is_true (void) const;
171 
172  double double_value (bool = false) const;
173 
174  float float_value (bool = false) const;
175 
176  double scalar_value (bool frc_str_conv = false) const
177  { return double_value (frc_str_conv); }
178 
179  float float_scalar_value (bool frc_str_conv = false) const
180  { return float_value (frc_str_conv); }
181 
182  Matrix matrix_value (bool = false) const
183  { return range.matrix_value (); }
184 
185  FloatMatrix float_matrix_value (bool = false) const
186  { return range.matrix_value (); }
187 
188  NDArray array_value (bool = false) const
189  { return range.matrix_value (); }
190 
191  FloatNDArray float_array_value (bool = false) const
192  { return FloatMatrix (range.matrix_value ()); }
193 
194  charNDArray char_array_value (bool = false) const;
195 
196  // FIXME: it would be better to have Range::intXNDArray_value
197  // functions to avoid the intermediate conversion to a matrix
198  // object.
199 
201  int8_array_value (void) const { return int8NDArray (array_value ()); }
202 
204  int16_array_value (void) const { return int16NDArray (array_value ()); }
205 
207  int32_array_value (void) const { return int32NDArray (array_value ()); }
208 
210  int64_array_value (void) const { return int64NDArray (array_value ()); }
211 
213  uint8_array_value (void) const { return uint8NDArray (array_value ()); }
214 
216  uint16_array_value (void) const { return uint16NDArray (array_value ()); }
217 
219  uint32_array_value (void) const { return uint32NDArray (array_value ()); }
220 
222  uint64_array_value (void) const { return uint64NDArray (array_value ()); }
223 
224  SparseMatrix sparse_matrix_value (bool = false) const
225  { return SparseMatrix (range.matrix_value ()); }
226 
228  { return SparseComplexMatrix (sparse_matrix_value ()); }
229 
230  Complex complex_value (bool = false) const;
231 
232  FloatComplex float_complex_value (bool = false) const;
233 
234  boolNDArray bool_array_value (bool warn = false) const;
235 
236  ComplexMatrix complex_matrix_value (bool = false) const
237  { return ComplexMatrix (range.matrix_value ()); }
238 
240  { return FloatComplexMatrix (range.matrix_value ()); }
241 
242  ComplexNDArray complex_array_value (bool = false) const
243  { return ComplexMatrix (range.matrix_value ()); }
244 
246  { return FloatComplexMatrix (range.matrix_value ()); }
247 
248  Range range_value (void) const { return range; }
249 
250  octave_value convert_to_str_internal (bool pad, bool force, char type) const;
251 
252  octave_value as_double (void) const;
253  octave_value as_single (void) const;
254 
255  octave_value as_int8 (void) const;
256  octave_value as_int16 (void) const;
257  octave_value as_int32 (void) const;
258  octave_value as_int64 (void) const;
259 
260  octave_value as_uint8 (void) const;
261  octave_value as_uint16 (void) const;
262  octave_value as_uint32 (void) const;
263  octave_value as_uint64 (void) const;
264 
265  void print (std::ostream& os, bool pr_as_read_syntax = false);
266 
267  void print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
268 
269  bool print_name_tag (std::ostream& os, const std::string& name) const;
270 
271  void short_disp (std::ostream& os) const;
272 
274 
275  std::string edit_display (const float_display_format& fmt,
277 
278  bool save_ascii (std::ostream& os);
279 
280  bool load_ascii (std::istream& is);
281 
282  bool save_binary (std::ostream& os, bool& save_as_floats);
283 
284  bool load_binary (std::istream& is, bool swap,
286 
287  bool save_hdf5 (octave_hdf5_id loc_id, const char *name, bool save_as_floats);
288 
289  bool load_hdf5 (octave_hdf5_id loc_id, const char *name);
290 
291  int write (octave::stream& os, int block_size,
292  oct_data_conv::data_type output_type, int skip,
294  {
295  // FIXME: could be more memory efficient by having a
296  // special case of the octave::stream::write method for ranges.
297 
298  return os.write (matrix_value (), block_size, output_type, skip, flt_fmt);
299  }
300 
301  mxArray * as_mxArray (void) const;
302 
304  {
305  octave_matrix m (matrix_value ());
306  return m.map (umap);
307  }
308 
309  octave_value fast_elem_extract (octave_idx_type n) const;
310 
311 private:
312 
314 
316  {
317  delete idx_cache;
318  idx_cache = (idx ? new idx_vector (idx) : nullptr);
319  return idx;
320  }
321 
322  void clear_cached_info (void) const
323  {
324  delete idx_cache; idx_cache = nullptr;
325  }
326 
328 
329  // No assignment.
330 
332 
334 };
335 
336 #endif
octave_idx_type write(const octave_value &data, octave_idx_type block_size, oct_data_conv::data_type output_type, octave_idx_type skip, mach_info::float_format flt_fmt)
Definition: oct-stream.cc:6704
bool is_constant(void) const
Definition: ov-range.h:132
octave_range(void)
Definition: ov-range.h:55
int64NDArray int64_array_value(void) const
Definition: ov-range.h:210
intNDArray< octave_int64 > int64NDArray
Definition: int64NDArray.h:33
Matrix matrix_value(bool=false) const
Definition: ov-range.h:182
int8NDArray int8_array_value(void) const
Definition: ov-range.h:201
FloatNDArray float_array_value(bool=false) const
Definition: ov-range.h:191
sortmode
Definition: oct-sort.h:105
FloatComplexMatrix float_complex_matrix_value(bool=false) const
Definition: ov-range.h:239
ComplexNDArray complex_array_value(bool=false) const
Definition: ov-range.h:242
uint32NDArray uint32_array_value(void) const
Definition: ov-range.h:219
int write(octave::stream &os, int block_size, oct_data_conv::data_type output_type, int skip, octave::mach_info::float_format flt_fmt) const
Definition: ov-range.h:291
intNDArray< octave_uint32 > uint32NDArray
Definition: uint32NDArray.h:33
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
idx subsref(val, idx) esult
Definition: ov.cc:3065
intNDArray< octave_uint8 > uint8NDArray
Definition: uint8NDArray.h:33
intNDArray< octave_uint16 > uint16NDArray
Definition: uint16NDArray.h:33
bool isnumeric(void) const
Definition: ov-range.h:168
FloatComplexNDArray float_complex_array_value(bool=false) const
Definition: ov-range.h:245
octave_base_value * empty_clone(void) const
Definition: ov-range.h:90
for large enough k
Definition: lu.cc:617
Range range
Definition: ov-range.h:313
Definition: Range.h:33
SparseMatrix sparse_matrix_value(bool=false) const
Definition: ov-range.h:224
void error(const char *fmt,...)
Definition: error.cc:578
FloatMatrix float_matrix_value(bool=false) const
Definition: ov-range.h:185
octave::mach_info::float_format flt_fmt
Definition: load-save.cc:736
the second is matched to the second specifier and placed in the second column and so forth If there are more words than specifiers then the process is repeated until all words have been processed or the limit imposed by any(non-whitespace) text in the format that is not one of these specifiers is considered a literal. If there is a literal between two format specifiers then that same literal must appear in the input stream between the matching words. The following specifiers are valid
Definition: file-io.cc:1499
octave_range(const Range &r, const idx_vector &cache)
Definition: ov-range.h:77
intNDArray< octave_int16 > int16NDArray
Definition: int16NDArray.h:33
octave_range(const Range &r)
Definition: ov-range.h:65
builtin_type_t
Definition: ov-base.h:71
int32NDArray int32_array_value(void) const
Definition: ov-range.h:207
Range range_value(void) const
Definition: ov-range.h:248
octave_value map(unary_mapper_t umap) const
Definition: ov-range.h:303
uint16NDArray uint16_array_value(void) const
Definition: ov-range.h:216
idx_vector * idx_cache
Definition: ov-range.h:327
octave_range(const octave_range &r)
Definition: ov-range.h:72
static double as_double(time_t sec, long usec)
Definition: oct-time.h:34
double scalar_value(bool frc_str_conv=false) const
Definition: ov-range.h:176
bool swap
Definition: load-save.cc:738
#define DECLARE_OV_TYPEID_FUNCTIONS_AND_DATA
Definition: ov-base.h:158
bool is_defined(void) const
Definition: ov-range.h:130
nd deftypefn *std::string name
Definition: sysdep.cc:647
octave_base_value * clone(void) const
Definition: ov-range.h:85
sortmode issorted(sortmode mode=UNSORTED) const
Definition: ov-range.h:151
octave_value reshape(const dim_vector &new_dims) const
Definition: ov-range.h:120
intNDArray< octave_int8 > int8NDArray
Definition: int8NDArray.h:33
SparseComplexMatrix sparse_complex_matrix_value(bool=false) const
Definition: ov-range.h:227
ComplexMatrix complex_matrix_value(bool=false) const
Definition: ov-range.h:236
Array< octave_value > array_value(void) const
Definition: ovl.h:86
bool save_as_floats
Definition: load-save.cc:1617
void clear_cached_info(void) const
Definition: ov-range.h:322
idx_vector set_idx_cache(const idx_vector &idx) const
Definition: ov-range.h:315
bool is_true(const std::string &s)
octave_value permute(const Array< int > &vec, bool inv=false) const
Definition: ov-range.h:123
int64_t octave_hdf5_id
octave_value squeeze(void) const
Definition: ov-range.h:126
octave_value map(unary_mapper_t umap) const
Definition: ov-re-mat.cc:900
idx type
Definition: ov.cc:3114
Definition: dMatrix.h:36
int16NDArray int16_array_value(void) const
Definition: ov-range.h:204
bool isfloat(void) const
Definition: ov-range.h:166
octave_range(double base, double limit, double inc)
Definition: ov-range.h:58
bool is_range(void) const
Definition: ov-range.h:134
uint8NDArray uint8_array_value(void) const
Definition: ov-range.h:213
builtin_type_t builtin_type(void) const
Definition: ov-range.h:160
intNDArray< octave_int32 > int32NDArray
Definition: int32NDArray.h:33
~octave_range(void)
Definition: ov-range.h:83
octave_idx_type nnz(void) const
Definition: ov-range.h:114
octave_value sort(Array< octave_idx_type > &sidx, octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-range.h:147
octave_value full_value(void) const
Definition: ov-range.h:128
dim_vector dims(void) const
Definition: ov-range.h:108
Array< octave_idx_type > sort_rows_idx(sortmode) const
Definition: ov-range.h:154
uint64NDArray uint64_array_value(void) const
Definition: ov-range.h:222
NDArray array_value(bool=false) const
Definition: ov-range.h:188
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
Definition: ovl.h:100
sortmode is_sorted_rows(sortmode mode=UNSORTED) const
Definition: ov-range.h:157
for i
Definition: data.cc:5264
std::complex< float > FloatComplex
Definition: oct-cmplx.h:32
bool is_double_type(void) const
Definition: ov-range.h:164
std::complex< double > Complex
Definition: oct-cmplx.h:31
OCTAVE_EXPORT octave_value_list or cell arrays Arguments are concatenated vertically The returned values are padded with blanks as needed to make each row of the string array have the same length Empty input strings are significant and will concatenated in the output For numerical each element is converted to the corresponding ASCII character A range error results if an input is outside the ASCII range(0-255). For cell arrays
write the output to stdout if nargout is
Definition: load-save.cc:1612
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int)
Definition: ov-range.h:99
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
bool isreal(void) const
Definition: ov-range.h:162
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
size_t byte_size(void) const
Definition: ov-range.h:118
dim_vector dv
Definition: sub2ind.cc:263
octave::stream os
Definition: file-io.cc:627
float float_scalar_value(bool frc_str_conv=false) const
Definition: ov-range.h:179
octave_value sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: ov-range.h:144
octave_value_list & operator=(const octave_value_list &obj)
Definition: ovl.h:75
static float_display_format get_edit_display_format(const octave_value &val)
intNDArray< octave_uint64 > uint64NDArray
Definition: uint64NDArray.h:33