GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Array-util.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2003-2024 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 (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "Array-util.h"
31 #include "lo-error.h"
32 #include "oct-locbuf.h"
33 
34 bool
36  const dim_vector& dimensions)
37 {
38  bool retval = true;
39 
40  int n = ra_idx.numel ();
41 
42  if (n == dimensions.ndims ())
43  {
44  for (int i = 0; i < n; i++)
45  {
46  if (ra_idx(i) < 0 || ra_idx(i) >= dimensions(i))
47  {
48  retval = false;
49  break;
50  }
51  }
52  }
53  else
54  retval = false;
55 
56  return retval;
57 }
58 
59 void
61  int start_dimension)
62 {
63  ra_idx(start_dimension)++;
64 
65  int n = ra_idx.numel () - 1;
66  int nda = dimensions.ndims ();
67 
68  for (int i = start_dimension; i < n; i++)
69  {
70  if (ra_idx(i) < (i < nda ? dimensions(i) : 1))
71  break;
72  else
73  {
74  ra_idx(i) = 0;
75  ra_idx(i+1)++;
76  }
77  }
78 }
79 
82 {
83  octave_idx_type retval (-1);
84 
85  int n = idx.numel ();
86 
87  if (n > 0)
88  {
89  retval = idx(--n);
90 
91  while (--n >= 0)
92  {
93  retval *= dims(n);
94 
95  retval += idx(n);
96  }
97  }
98  return retval;
99 }
100 
103 {
104  octave_idx_type retval = 0;
105 
106  for (octave_idx_type i = 0; i < ra_idx.numel (); i++)
107  {
108  if (ra_idx(i) == 1)
109  retval++;
110  }
111 
112  return retval;
113 }
114 
115 bool
116 is_scalar (const dim_vector& dim)
117 {
118  bool retval = true;
119 
120  int n = dim.ndims ();
121 
122  if (n == 0)
123  retval = false;
124  else
125  {
126  for (int i = 0; i < n; i++)
127  {
128  if (dim(i) != 1)
129  {
130  retval = false;
131 
132  break;
133  }
134  }
135  }
136  return retval;
137 }
138 
139 bool
140 isvector (const dim_vector& dim)
141 {
142  int m = 0;
143  int n = dim.ndims ();
144 
145  if (n == 0)
146  m = 2;
147  else
148  {
149  for (int i = 0; i < n; i++)
150  if (dim(i) > 1)
151  m++;
152  else if (dim(i) < 1)
153  m += 2;
154  }
155 
156  return (m < 2);
157 }
158 
159 bool
161 {
162  bool retval = false;
163 
164  for (octave_idx_type i = 0; i < arr.numel (); i++)
165  {
166  if (arr (i) == 1)
167  {
168  retval = true;
169 
170  break;
171  }
172  }
173  return retval;
174 }
175 
178 {
179  if (n < 0)
181  if (n >= dims.numel ())
182  octave::err_index_out_of_range (1, 1, n+1, dims.numel (), dims);
183 
184  return n;
185 }
186 
189 {
190  if (i < 0)
191  octave::err_invalid_index (i, 2, 1);
192  if (j < 0)
193  octave::err_invalid_index (j, 2, 2);
194  if (i >= dims(0))
195  octave::err_index_out_of_range (2, 1, i+1, dims(0), dims);
196  if (j >= dims.numel (1))
197  octave::err_index_out_of_range (2, 2, j+1, dims.numel (1), dims);
198 
199  return j*dims(0) + i;
200 }
201 
204  const dim_vector& dims)
205 {
206  if (i < 0)
207  octave::err_invalid_index (i, 3, 1);
208  if (j < 0)
209  octave::err_invalid_index (j, 3, 2);
210  if (k < 0)
211  octave::err_invalid_index (k, 3, 3);
212  if (i >= dims(0))
213  octave::err_index_out_of_range (3, 1, i+1, dims(0), dims);
214  if (j >= dims(1))
215  octave::err_index_out_of_range (3, 2, j+1, dims(1), dims);
216  if (k >= dims.numel (2))
217  octave::err_index_out_of_range (3, 3, k+1, dims.numel (2), dims);
218 
219  return (k*dims(1) + j)*dims(0) + i;
220 }
221 
224 {
225  int nd = ra_idx.numel ();
226  const dim_vector dv = dims.redim (nd);
227  for (int d = 0; d < nd; d++)
228  {
229  if (ra_idx(d) < 0)
231  if (ra_idx(d) >= dv(d))
232  octave::err_index_out_of_range (nd, d+1, ra_idx(d)+1, dv(d), dims);
233  }
234 
235  return dv.compute_index (ra_idx.data ());
236 }
237 
240 {
241  Array<octave_idx_type> retval (a.dims ());
242 
243  for (octave_idx_type i = 0; i < a.numel (); i++)
244  retval(i) = a(i).elem (0);
245 
246  return retval;
247 }
248 
251 {
253 
254  for (octave_idx_type i = 0; i < len; i++)
255  retval(i) = tmp[i];
256 
257  return retval;
258 }
259 
261 freeze (Array<octave::idx_vector>& ra_idx, const dim_vector& dimensions, int resize_ok)
262 {
263  dim_vector retval;
264 
265  int n = ra_idx.numel ();
266 
267  assert (n == dimensions.ndims ());
268 
269  retval.resize (n);
270 
271  static const char *tag[3] = { "row", "column", nullptr };
272 
273  for (int i = 0; i < n; i++)
274  retval(i) = ra_idx(i).freeze (dimensions(i), tag[i < 2 ? i : 2],
275  resize_ok);
276 
277  return retval;
278 }
279 
280 bool
282 {
283  int n = dv.ndims ();
284 
285  bool found_first = false;
286 
287  for (int i = 0; i < n; i++)
288  {
289  if (dv(i) != 1)
290  {
291  if (! found_first)
292  found_first = true;
293  else
294  return false;
295  }
296  }
297 
298  return true;
299 }
300 
301 // FIXME: Deprecated in Octave 9.
302 bool
304 {
305  octave_unused_parameter (ra_idx);
306 
307  // idx_vector objects are always valid once constructed
308  return true;
309 }
310 
311 bool
313 {
314  bool retval = false;
315 
317 
318  for (octave_idx_type i = 0; i < n; i++)
319  {
320  if (ra_idx(i).orig_empty ())
321  {
322  retval = true;
323  break;
324  }
325  }
326 
327  return retval;
328 }
329 
330 bool
332  const dim_vector& frozen_lengths)
333 {
334  bool retval = true;
335 
336  octave_idx_type idx_n = ra_idx.numel ();
337 
338  int n = frozen_lengths.ndims ();
339 
340  assert (idx_n == n);
341 
342  for (octave_idx_type i = 0; i < n; i++)
343  {
344  if (! ra_idx(i).is_colon_equiv (frozen_lengths(i)))
345  {
346  retval = false;
347  break;
348  }
349  }
350 
351  return retval;
352 }
353 
354 bool
356 {
357  bool retval = true;
358 
359  for (octave_idx_type i = 0; i < arr.numel (); i++)
360  {
361  if (arr(i) != 1)
362  {
363  retval = false;
364  break;
365  }
366  }
367 
368  return retval;
369 }
370 
373  const Array<octave_idx_type>& result_idx)
374 {
376 
377  Array<octave_idx_type> retval (dim_vector (n, 1));
378 
379  for (octave_idx_type i = 0; i < n; i++)
380  retval(i) = ra_idx(i).elem (result_idx(i));
381 
382  return retval;
383 }
384 
387 {
388  Array<octave_idx_type> retval;
389 
390  int n_dims = dims.ndims ();
391 
392  retval.resize (dim_vector (n_dims, 1));
393 
394  for (int i = 0; i < n_dims; i++)
395  retval(i) = 0;
396 
397  assert (idx > 0 || idx < dims.numel ());
398 
399  for (octave_idx_type i = 0; i < idx; i++)
400  increment_index (retval, dims);
401 
402  // FIXME: the solution using increment_index is not efficient.
403 
404  return retval;
405 }
406 
409 {
410  int ial = ia.numel ();
411  int rhdvl = rhdv.ndims ();
412  dim_vector rdv = dim_vector::alloc (ial);
413  bool *scalar = new bool [ial];
414  bool *colon = new bool [ial];
415  // Mark scalars and colons, count non-scalar indices.
416  int nonsc = 0;
417  bool all_colons = true;
418  for (int i = 0; i < ial; i++)
419  {
420  // FIXME: should we check for length() instead?
421  scalar[i] = ia(i).is_scalar ();
422  colon[i] = ia(i).is_colon ();
423  if (! scalar[i]) nonsc++;
424  if (! colon[i]) rdv(i) = ia(i).extent (0);
425  all_colons = all_colons && colon[i];
426  }
427 
428  // If the number of nonscalar indices matches the dimensionality of
429  // RHS, we try an exact match, inquiring even singleton dimensions.
430  if (all_colons)
431  {
432  rdv = rhdv;
433  rdv.resize (ial, 1);
434  }
435  else if (nonsc == rhdvl)
436  {
437  for (int i = 0, j = 0; i < ial; i++)
438  {
439  if (scalar[i]) continue;
440  if (colon[i])
441  rdv(i) = rhdv(j);
442  j++;
443  }
444  }
445  else
446  {
447  dim_vector rhdv0 = rhdv;
448  rhdv0.chop_all_singletons ();
449  int rhdv0l = rhdv0.ndims ();
450  for (int i = 0, j = 0; i < ial; i++)
451  {
452  if (scalar[i]) continue;
453  if (colon[i])
454  rdv(i) = (j < rhdv0l) ? rhdv0(j++) : 1;
455  }
456  }
457 
458  delete [] scalar;
459  delete [] colon;
460 
461  return rdv;
462 }
463 
466  const dim_vector& rhdv)
467 {
468  bool icol = i.is_colon ();
469  bool jcol = j.is_colon ();
470  dim_vector rdv;
471  if (icol && jcol && rhdv.ndims () == 2)
472  {
473  rdv(0) = rhdv(0);
474  rdv(1) = rhdv(1);
475  }
476  else if (rhdv.ndims () == 2
477  && ! i.is_scalar () && ! j.is_scalar ())
478  {
479  rdv(0) = (icol ? rhdv(0) : i.extent (0));
480  rdv(1) = (jcol ? rhdv(1) : j.extent (0));
481  }
482  else
483  {
484  dim_vector rhdv0 = rhdv;
485  rhdv0.chop_all_singletons ();
486  int k = 0;
487  rdv(0) = i.extent (0);
488  if (icol)
489  rdv(0) = rhdv0(k++);
490  else if (! i.is_scalar ())
491  k++;
492  rdv(1) = j.extent (0);
493  if (jcol)
494  rdv(1) = rhdv0(k++);
495  else if (! j.is_scalar ())
496  k++;
497  }
498 
499  return rdv;
500 }
501 
504 {
505  octave::idx_vector retval;
506  octave_idx_type len = idxa.numel ();
507 
508  if (len == 0)
509  (*current_liboctave_error_handler) ("sub2ind: needs at least 2 indices");
510 
511  const dim_vector dvx = dv.redim (len);
512  bool all_ranges = true;
513  octave_idx_type clen = -1;
514 
515  for (octave_idx_type i = 0; i < len; i++)
516  {
517  try
518  {
519  octave::idx_vector idx = idxa(i);
520  octave_idx_type n = dvx(i);
521 
522  all_ranges = all_ranges && idx.is_range ();
523  if (clen < 0)
524  clen = idx.length (n);
525  else if (clen != idx.length (n))
526  (*current_liboctave_error_handler)
527  ("sub2ind: lengths of indices must match");
528 
529  if (idx.extent (n) > n)
530  octave::err_index_out_of_range (len, i+1, idx.extent (n), n, dv);
531  }
532  catch (octave::index_exception& ie)
533  {
534  ie.set_pos_if_unset (len, i+1);
535  ie.set_var ();
536  std::string msg = ie.message ();
537  (*current_liboctave_error_with_id_handler)
538  (ie.err_id (), "%s", msg.c_str ());
539  }
540  }
541  // idxa known to be valid.
542  // Shouldn't need to catch index_exception below here.
543 
544  if (len == 1)
545  retval = idxa(0);
546  else if (clen == 1)
547  {
548  // All scalars case - the result is a scalar.
549  octave_idx_type idx = idxa(len-1)(0);
550  for (octave_idx_type i = len - 2; i >= 0; i--)
551  idx = dvx(i) * idx + idxa(i)(0);
552  retval = octave::idx_vector (idx);
553  }
554  else if (all_ranges && clen != 0)
555  {
556  // All ranges case - the result is a range.
557  octave_idx_type start = 0;
558  octave_idx_type step = 0;
559  for (octave_idx_type i = len - 1; i >= 0; i--)
560  {
561  octave_idx_type xstart = idxa(i)(0);
562  octave_idx_type xstep = idxa(i)(1) - xstart;
563  start = dvx(i) * start + xstart;
564  step = dvx(i) * step + xstep;
565  }
566  retval = octave::idx_vector::make_range (start, step, clen);
567  }
568  else
569  {
570  Array<octave_idx_type> idx (idxa(0).orig_dimensions ());
571 
572  for (octave_idx_type i = len - 1; i >= 0; i--)
573  {
574  // Initialized inside the loop so that each call to
575  // idx_vector::loop operates from the beginning of IDX_VEC.
576 
577  octave_idx_type *idx_vec = idx.fortran_vec ();
578 
579  if (i < len - 1)
580  {
581  octave_idx_type n = dvx(i);
582 
583  idxa(i).loop (clen, [=, &idx_vec] (octave_idx_type k) {
584  (*idx_vec++ *= n) += k;
585  });
586  }
587  else
588  idxa(i).copy_data (idx_vec);
589  }
590 
591  retval = octave::idx_vector (idx);
592  }
593 
594  return retval;
595 }
596 
598 ind2sub (const dim_vector& dv, const octave::idx_vector& idx)
599 {
600  octave_idx_type len = idx.length (0);
601  octave_idx_type n = dv.ndims ();
602  Array<octave::idx_vector> retval (dim_vector (n, 1));
603  octave_idx_type numel = dv.numel ();
604 
605  if (idx.is_scalar ())
606  {
607  octave_idx_type k = idx(0);
608  for (octave_idx_type j = 0; j < n-1; j++)
609  {
610  retval(j) = k % dv(j);
611  k /= dv(j);
612  }
613 
614  retval(n-1) = idx(0) < numel ? k % dv(n-1) : k;
615  }
616  else
617  {
619 
620  dim_vector odv = idx.orig_dimensions ();
621  for (octave_idx_type j = 0; j < n; j++)
622  rdata[j] = Array<octave_idx_type> (odv);
623 
624  for (octave_idx_type i = 0; i < len; i++)
625  {
626  octave_idx_type k = idx(i);
627  for (octave_idx_type j = 0; j < n-1; j++)
628  {
629  rdata[j](i) = k % dv(j);
630  k /= dv(j);
631  }
632 
633  rdata[n-1](i) = idx(i) < numel ? k % dv(n-1) : k;
634  }
635 
636  for (octave_idx_type j = 0; j < n; j++)
637  retval(j) = rdata[j];
638  }
639 
640  return retval;
641 }
642 
643 int
644 permute_vector_compare (const void *a, const void *b)
645 {
646  const permute_vector *pva = static_cast<const permute_vector *> (a);
647  const permute_vector *pvb = static_cast<const permute_vector *> (b);
648 
649  return pva->pidx > pvb->pidx;
650 }
octave_idx_type compute_index(octave_idx_type n, const dim_vector &dims)
Definition: Array-util.cc:177
Array< octave::idx_vector > conv_to_array(const octave::idx_vector *tmp, const octave_idx_type len)
Definition: Array-util.cc:250
bool is_scalar(const dim_vector &dim)
Definition: Array-util.cc:116
int permute_vector_compare(const void *a, const void *b)
Definition: Array-util.cc:644
dim_vector zero_dims_inquire(const Array< octave::idx_vector > &ia, const dim_vector &rhdv)
Definition: Array-util.cc:408
Array< octave_idx_type > conv_to_int_array(const Array< octave::idx_vector > &a)
Definition: Array-util.cc:239
octave::idx_vector sub2ind(const dim_vector &dv, const Array< octave::idx_vector > &idxa)
Definition: Array-util.cc:503
bool all_ones(const Array< octave_idx_type > &arr)
Definition: Array-util.cc:355
octave_idx_type num_ones(const Array< octave_idx_type > &ra_idx)
Definition: Array-util.cc:102
bool all_colon_equiv(const Array< octave::idx_vector > &ra_idx, const dim_vector &frozen_lengths)
Definition: Array-util.cc:331
Array< octave_idx_type > get_ra_idx(octave_idx_type idx, const dim_vector &dims)
Definition: Array-util.cc:386
Array< octave_idx_type > get_elt_idx(const Array< octave::idx_vector > &ra_idx, const Array< octave_idx_type > &result_idx)
Definition: Array-util.cc:372
bool vector_equivalent(const dim_vector &dv)
Definition: Array-util.cc:281
bool any_ones(const Array< octave_idx_type > &arr)
Definition: Array-util.cc:160
dim_vector freeze(Array< octave::idx_vector > &ra_idx, const dim_vector &dimensions, int resize_ok)
Definition: Array-util.cc:261
bool any_orig_empty(const Array< octave::idx_vector > &ra_idx)
Definition: Array-util.cc:312
Array< octave::idx_vector > ind2sub(const dim_vector &dv, const octave::idx_vector &idx)
Definition: Array-util.cc:598
bool isvector(const dim_vector &dim)
Definition: Array-util.cc:140
bool index_in_bounds(const Array< octave_idx_type > &ra_idx, const dim_vector &dimensions)
Definition: Array-util.cc:35
bool all_ok(const Array< octave::idx_vector > &ra_idx)
Definition: Array-util.cc:303
void increment_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions, int start_dimension)
Definition: Array-util.cc:60
octave_idx_type get_scalar_idx(Array< octave_idx_type > &idx, dim_vector &dims)
Definition: Array-util.cc:81
T & elem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:562
T * fortran_vec()
Size of the specified dimension.
Definition: Array-base.cc:1764
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
Definition: Array-base.cc:1023
const dim_vector & dims() const
Return a const-reference so that dims ()(i) works efficiently.
Definition: Array.h:503
octave_idx_type numel() const
Number of elements in the array.
Definition: Array.h:414
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_idx_type compute_index(const octave_idx_type *idx) const
Linear index from an index tuple.
Definition: dim-vector.h:456
octave_idx_type numel(int n=0) const
Number of elements that a matrix with this dimensions would have.
Definition: dim-vector.h:335
void resize(int n, int fill_value=0)
Definition: dim-vector.h:272
static dim_vector alloc(int n)
Definition: dim-vector.h:202
octave_idx_type ndims() const
Number of dimensions.
Definition: dim-vector.h:257
void chop_all_singletons()
Definition: dim-vector.cc:50
dim_vector redim(int n) const
Force certain dimensionality, preserving numel ().
Definition: dim-vector.cc:226
virtual octave_idx_type numel() const
Definition: ov-base.h:391
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
void err_invalid_index(const std::string &idx, octave_idx_type nd, octave_idx_type dim, const std::string &)
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
T octave_idx_type m
Definition: mx-inlines.cc:781
octave_idx_type n
Definition: mx-inlines.cc:761
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
Definition: oct-locbuf.h:44
T::size_type numel(const T &str)
Definition: oct-string.cc:74
const octave_base_value const Array< octave_idx_type > & ra_idx
octave_idx_type pidx
Definition: Array-util.h:115
F77_RET_T len
Definition: xerbla.cc:61