GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
Sparse.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1998-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 (octave_Sparse_h)
27 #define octave_Sparse_h 1
28 
29 #include "octave-config.h"
30 
31 #include <cassert>
32 #include <cstddef>
33 
34 #include <algorithm>
35 #include <iosfwd>
36 #include <string>
37 
38 #include "Array-fwd.h"
39 #include "Sparse-fwd.h"
40 #include "mx-fwd.h"
41 
42 // Two dimensional sparse class. Handles the reference counting for
43 // all the derived classes.
44 
45 template <typename T, typename Alloc>
46 class
48 Sparse
49 {
50 public:
51 
52  typedef T element_type;
53 
54 protected:
55  //--------------------------------------------------------------------
56  // The real representation of all Sparse arrays.
57  //--------------------------------------------------------------------
58 
59  class SparseRep : public Alloc
60  {
61  public:
62 
63  typedef std::allocator_traits<Alloc> Alloc_traits;
64 
65  typedef typename Alloc_traits::template rebind_traits<T> T_Alloc_traits;
66  typedef typename T_Alloc_traits::pointer T_pointer;
67 
68  typedef typename Alloc_traits::template rebind_traits<octave_idx_type> idx_type_Alloc_traits;
69  typedef typename idx_type_Alloc_traits::pointer idx_type_pointer;
70 
77  octave::refcount<octave_idx_type> m_count;
78 
80  : Alloc (), m_data (T_allocate (1)), m_ridx (idx_type_allocate (1)),
81  m_cidx (idx_type_allocate (1)),
82  m_nzmax (1), m_nrows (0), m_ncols (0), m_count (1)
83  { }
84 
86  : Alloc (), m_data (T_allocate (1)), m_ridx (idx_type_allocate (1)),
87  m_cidx (idx_type_allocate (n+1)),
88  m_nzmax (1), m_nrows (n), m_ncols (n), m_count (1)
89  { }
90 
92  : Alloc (), m_data (T_allocate (nz > 0 ? nz : 1)),
93  m_ridx (idx_type_allocate (nz > 0 ? nz : 1)),
94  m_cidx (idx_type_allocate (nc+1)),
95  m_nzmax (nz > 0 ? nz : 1), m_nrows (nr), m_ncols (nc), m_count (1)
96  { }
97 
99  const T *d, const octave_idx_type *r, const octave_idx_type *c)
100  : Alloc (), m_data (T_allocate (nz)),
101  m_ridx (idx_type_allocate (nz)),
102  m_cidx (idx_type_allocate (nc+1)),
103  m_nzmax (nz), m_nrows (nr), m_ncols (nc), m_count (1)
104  {
105  std::copy_n (d, nz, m_data);
106  std::copy_n (r, nz, m_ridx);
107  std::copy_n (c, m_ncols + 1, m_cidx);
108  }
109 
110  template <typename U>
112  const U *d, const octave_idx_type *r, const octave_idx_type *c)
113  : Alloc (), m_data (T_allocate (nz)),
114  m_ridx (idx_type_allocate (nz)),
115  m_cidx (idx_type_allocate (nc+1)),
116  m_nzmax (nz), m_nrows (nr), m_ncols (nc), m_count (1)
117  {
118  std::copy_n (d, nz, m_data);
119  std::copy_n (r, nz, m_ridx);
120  std::copy_n (c, nc + 1, m_cidx);
121  }
122 
123  template <typename U>
126  const Alloc& xallocator = Alloc ())
127  : Alloc (xallocator), m_data (d), m_ridx (r), m_cidx (c),
128  m_nzmax (nz), m_nrows (dv(0)), m_ncols (dv(1)), m_count (1)
129  { }
130 
131  SparseRep (const SparseRep& a)
132  : Alloc (), m_data (T_allocate (a.m_nzmax)),
133  m_ridx (idx_type_allocate (a.m_nzmax)),
134  m_cidx (idx_type_allocate (a.m_ncols + 1)),
135  m_nzmax (a.m_nzmax), m_nrows (a.m_nrows), m_ncols (a.m_ncols),
136  m_count (1)
137  {
138  octave_idx_type nz = a.nnz ();
139  std::copy_n (a.m_data, nz, m_data);
140  std::copy_n (a.m_ridx, nz, m_ridx);
141  std::copy_n (a.m_cidx, m_ncols + 1, m_cidx);
142  }
143 
145  {
146  T_deallocate (m_data, m_nzmax);
147  idx_type_deallocate (m_ridx, m_nzmax);
148  idx_type_deallocate (m_cidx, m_ncols + 1);
149  }
150 
151  octave_idx_type nzmax () const { return m_nzmax; }
152  octave_idx_type nnz () const { return m_cidx[m_ncols]; }
153 
154  octave_idx_type rows () const { return m_nrows; }
155  octave_idx_type cols () const { return m_ncols; }
156  octave_idx_type columns () const { return m_ncols; }
157 
159 
160  OCTAVE_API T celem (octave_idx_type r, octave_idx_type c) const;
161 
162  T& data (octave_idx_type i) { return m_data[i]; }
163 
164  T cdata (octave_idx_type i) const { return m_data[i]; }
165 
166  octave_idx_type& ridx (octave_idx_type i) { return m_ridx[i]; }
167 
168  octave_idx_type cridx (octave_idx_type i) const { return m_ridx[i]; }
169 
170  octave_idx_type& cidx (octave_idx_type i) { return m_cidx[i]; }
171 
172  octave_idx_type ccidx (octave_idx_type i) const { return m_cidx[i]; }
173 
174  OCTAVE_API void maybe_compress (bool remove_zeros);
175 
176  OCTAVE_API void change_length (octave_idx_type nz);
177 
178  OCTAVE_API bool indices_ok () const;
179 
180  OCTAVE_API bool any_element_is_nan () const;
181 
182  // Prefer nzmax.
183  octave_idx_type length () const { return m_nzmax; }
184 
185  template <typename U, typename A> friend class Sparse;
186 
187  // No assignment!
188 
189  SparseRep& operator = (const SparseRep&) = delete;
190 
192  {
193  typename T_Alloc_traits::allocator_type& alloc = *this;
194 
195  T_pointer data = T_Alloc_traits::allocate (alloc, len);
196  for (size_t i = 0; i < len; i++)
197  T_Alloc_traits::construct (alloc, data+i);
198 
199  return data;
200  }
201 
203  {
204  typename T_Alloc_traits::allocator_type& alloc = *this;
205 
206  for (size_t i = 0; i < len; i++)
207  T_Alloc_traits::destroy (alloc, data+i);
208  T_Alloc_traits::deallocate (alloc, data, len);
209  }
210 
212  {
213  typename idx_type_Alloc_traits::allocator_type alloc = *this;
214 
215  idx_type_pointer idx = idx_type_Alloc_traits::allocate (alloc, len);
216  for (size_t i = 0; i < len; i++)
217  idx_type_Alloc_traits::construct (alloc, idx+i);
218 
219  return idx;
220  }
221 
223  {
224  typename idx_type_Alloc_traits::allocator_type alloc = *this;
225 
226  for (size_t i = 0; i < len; i++)
227  idx_type_Alloc_traits::destroy (alloc, idx+i);
228  idx_type_Alloc_traits::deallocate (alloc, idx, len);
229  }
230  };
231 
232  //--------------------------------------------------------------------
233 
234  void make_unique ()
235  {
236  if (m_rep->m_count > 1)
237  {
238  SparseRep *r = new SparseRep (*m_rep);
239 
240  if (--m_rep->m_count == 0)
241  delete m_rep;
242 
243  m_rep = r;
244  }
245  }
246 
247 protected:
248 
250 
252 
253 private:
254 
255  static OCTAVE_API typename Sparse<T, Alloc>::SparseRep * nil_rep ();
256 
257 public:
258 
260  : m_rep (nil_rep ()), m_dimensions (dim_vector (0, 0))
261  {
262  m_rep->m_count++;
263  }
264 
266  : m_rep (new typename Sparse<T, Alloc>::SparseRep (n)),
267  m_dimensions (dim_vector (n, n)) { }
268 
270  : m_rep (new typename Sparse<T, Alloc>::SparseRep (nr, nc)),
271  m_dimensions (dim_vector (nr, nc)) { }
272 
273  explicit OCTAVE_API Sparse (octave_idx_type nr, octave_idx_type nc, T val);
274 
276  : m_rep (new typename Sparse<T, Alloc>::SparseRep (dv(0), dv(1), nz)),
277  m_dimensions (dv) { }
278 
280  : m_rep (new typename Sparse<T, Alloc>::SparseRep (nr, nc, nz)),
281  m_dimensions (dim_vector (nr, nc)) { }
282 
283  // Construct a Sparse array from pointers to externally allocated
284  // arrays of values and indices. PTR, RIDX, and CIDX must be
285  // allocated with operator new. The Sparse object takes ownership of
286  // these arrays and will delete them when the Sparse object is
287  // deleted. The dimension vector DV must be consistent with the sizes
288  // of the allocated PTR, CIDX, and RIDX arrays.
289 
292  const Alloc& xallocator = Alloc ())
293  : m_rep (new typename Sparse<T, Alloc>::SparseRep (dv, nz, ptr, ridx, cidx, xallocator)),
294  m_dimensions (dv)
295  { }
296 
297  // Both SparseMatrix and SparseBoolMatrix need this ctor, and this
298  // is their only common ancestor.
299  explicit OCTAVE_API Sparse (const PermMatrix& a);
300 
301  // Type conversion case. Preserves nzmax.
302  template <typename U>
303  Sparse (const Sparse<U>& a)
304  : m_rep (new typename Sparse<T, Alloc>::SparseRep (a.rows (), a.cols (),
305  a.nzmax (), a.data (),
306  a.ridx (), a.cidx ())),
307  m_dimensions (a.dims ()) { }
308 
309  // No type conversion case.
312  {
313  m_rep->m_count++;
314  }
315 
316 public:
317 
318  OCTAVE_API Sparse (const dim_vector& dv);
319 
320  OCTAVE_API Sparse (const Sparse<T, Alloc>& a, const dim_vector& dv);
321 
322  OCTAVE_API
323  Sparse (const Array<T>& a, const octave::idx_vector& r, const octave::idx_vector& c,
324  octave_idx_type nr = -1, octave_idx_type nc = -1,
325  bool sum_terms = true, octave_idx_type nzm = -1);
326 
327  // Sparsify a normal matrix
328  OCTAVE_API Sparse (const Array<T>& a);
329 
330  virtual ~Sparse ();
331 
333 
334  //! Amount of storage for nonzero elements.
335  //! This may differ from the actual number of elements, see nnz().
336  octave_idx_type nzmax () const { return m_rep->nzmax (); }
337 
338  //! Actual number of nonzero terms.
339  octave_idx_type nnz () const { return m_rep->nnz (); }
340 
341  // Querying the number of elements (incl. zeros) may overflow the index type,
342  // so don't do it unless you really need it.
344  {
345  return m_dimensions.safe_numel ();
346  }
347 
348  octave_idx_type dim1 () const { return m_dimensions(0); }
349  octave_idx_type dim2 () const { return m_dimensions(1); }
350 
351  octave_idx_type rows () const { return dim1 (); }
352  octave_idx_type cols () const { return dim2 (); }
353  octave_idx_type columns () const { return dim2 (); }
354 
357  {
358  octave_idx_type ret = 0;
359  while (cidx (ret+1) < k)
360  ret++;
361  return ret;
362  }
363 
364  std::size_t byte_size () const
365  {
366  return (static_cast<std::size_t> (cols () + 1) * sizeof (octave_idx_type)
367  + static_cast<std::size_t> (nzmax ())
368  * (sizeof (T) + sizeof (octave_idx_type)));
369  }
370 
371  dim_vector dims () const { return m_dimensions; }
372 
373  Sparse<T, Alloc> squeeze () const { return *this; }
374 
377 
378  OCTAVE_NORETURN OCTAVE_API T
379  range_error (const char *fcn, octave_idx_type n) const;
380  OCTAVE_NORETURN OCTAVE_API T&
381  range_error (const char *fcn, octave_idx_type n);
382 
383  OCTAVE_NORETURN OCTAVE_API T
384  range_error (const char *fcn, octave_idx_type i, octave_idx_type j) const;
385  OCTAVE_NORETURN OCTAVE_API T&
386  range_error (const char *fcn, octave_idx_type i, octave_idx_type j);
387 
388  OCTAVE_NORETURN OCTAVE_API T
389  range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const;
390  OCTAVE_NORETURN OCTAVE_API T&
391  range_error (const char *fcn, const Array<octave_idx_type>& ra_idx);
392 
393  // No checking, even for multiple references, ever.
394 
396  {
397  octave_idx_type i = n % rows ();
398  octave_idx_type j = n / rows ();
399  return xelem (i, j);
400  }
401 
403  {
404  octave_idx_type i = n % rows ();
405  octave_idx_type j = n / rows ();
406  return xelem (i, j);
407  }
408 
409  T& xelem (octave_idx_type i, octave_idx_type j) { return m_rep->elem (i, j); }
411  {
412  return m_rep->celem (i, j);
413  }
414 
416  { return xelem (compute_index (ra_idx)); }
417 
419  { return xelem (compute_index (ra_idx)); }
420 
421  // FIXME: would be nice to fix this so that we don't unnecessarily force a
422  // copy, but that is not so easy, and I see no clean way to do it.
423 
425  {
426  if (n < 0 || n >= numel ())
427  range_error ("T& Sparse<T>::checkelem", n);
428  else
429  {
430  make_unique ();
431  return xelem (n);
432  }
433  }
434 
436  {
437  if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ())
438  range_error ("T& Sparse<T>::checkelem", i, j);
439  else
440  {
441  make_unique ();
442  return xelem (i, j);
443  }
444  }
445 
447  {
449 
450  if (i < 0)
451  range_error ("T& Sparse<T>::checkelem", ra_idx);
452  else
453  return elem (i);
454  }
455 
457  {
458  make_unique ();
459  return xelem (n);
460  }
461 
463  {
464  make_unique ();
465  return xelem (i, j);
466  }
467 
470 
472  {
473  return elem (n);
474  }
475 
477  {
478  return elem (i, j);
479  }
480 
482  {
483  return elem (ra_idx);
484  }
485 
487  {
488  if (n < 0 || n >= numel ())
489  range_error ("T Sparse<T>::checkelem", n);
490  else
491  return xelem (n);
492  }
493 
495  {
496  if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ())
497  range_error ("T Sparse<T>::checkelem", i, j);
498  else
499  return xelem (i, j);
500  }
501 
503  {
505 
506  if (i < 0)
507  range_error ("T Sparse<T>::checkelem", ra_idx);
508  else
509  return Sparse<T, Alloc>::elem (i);
510  }
511 
512  T elem (octave_idx_type n) const { return xelem (n); }
513 
514  T elem (octave_idx_type i, octave_idx_type j) const { return xelem (i, j); }
515 
518 
519  T operator () (octave_idx_type n) const { return elem (n); }
520 
522  {
523  return elem (i, j);
524  }
525 
527  {
528  return elem (ra_idx);
529  }
530 
531  Sparse<T, Alloc> maybe_compress (bool remove_zeros = false)
532  {
533  if (remove_zeros)
534  make_unique (); // Need to unshare because elements are removed.
535 
536  m_rep->maybe_compress (remove_zeros);
537  return (*this);
538  }
539 
540  OCTAVE_API Sparse<T, Alloc> reshape (const dim_vector& new_dims) const;
541 
543  permute (const Array<octave_idx_type>& vec, bool inv = false) const;
544 
546  {
547  return permute (vec, true);
548  }
549 
551 
553 
554  OCTAVE_API void resize (const dim_vector& dv);
555 
557  {
558  if (nz < nnz ())
559  make_unique (); // Unshare now because elements will be truncated.
560  m_rep->change_length (nz);
561  }
562 
566  insert (const Sparse<T, Alloc>& a, const Array<octave_idx_type>& idx);
567 
568  bool issquare () const { return (dim1 () == dim2 ()); }
569 
570  bool isempty () const { return (rows () < 1 || cols () < 1); }
571 
573 
574  T * data () { make_unique (); return m_rep->m_data; }
575  T& data (octave_idx_type i) { make_unique (); return m_rep->data (i); }
576  T * xdata () { return m_rep->m_data; }
577  T& xdata (octave_idx_type i) { return m_rep->data (i); }
578 
579  T data (octave_idx_type i) const { return m_rep->data (i); }
580  // FIXME: shouldn't this be returning const T*?
581  T * data () const { return m_rep->m_data; }
582 
585  {
586  make_unique (); return m_rep->ridx (i);
587  }
588 
589  octave_idx_type * xridx () { return m_rep->m_ridx; }
591 
592  octave_idx_type ridx (octave_idx_type i) const { return m_rep->cridx (i); }
593  // FIXME: shouldn't this be returning const octave_idx_type*?
594  octave_idx_type * ridx () const { return m_rep->m_ridx; }
595 
598  {
599  make_unique (); return m_rep->cidx (i);
600  }
601 
602  octave_idx_type * xcidx () { return m_rep->m_cidx; }
604 
605  octave_idx_type cidx (octave_idx_type i) const { return m_rep->ccidx (i); }
606  // FIXME: shouldn't this be returning const octave_idx_type*?
607  octave_idx_type * cidx () const { return m_rep->m_cidx; }
608 
609  octave_idx_type ndims () const { return m_dimensions.ndims (); }
610 
612 
613  OCTAVE_API void delete_elements (int dim, const octave::idx_vector& i);
614 
616 
618  index (const octave::idx_vector& i, bool resize_ok = false) const;
619 
621  index (const octave::idx_vector& i, const octave::idx_vector& j,
622  bool resize_ok = false) const;
623 
624  OCTAVE_API void assign (const octave::idx_vector& i,
625  const Sparse<T, Alloc>& rhs);
626 
627  OCTAVE_API void assign (const octave::idx_vector& i, const T& rhs);
628 
629  OCTAVE_API void
630  assign (const octave::idx_vector& i, const octave::idx_vector& j,
631  const Sparse<T, Alloc>& rhs);
632 
633  OCTAVE_API void
634  assign (const octave::idx_vector& i, const octave::idx_vector& j,
635  const T& rhs);
636 
637  OCTAVE_API void
638  print_info (std::ostream& os, const std::string& prefix) const;
639 
641  sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
644  sortmode mode = ASCENDING) const;
645 
647 
648  // dim = -1 and dim = -2 are special; see Array<T>::cat description.
650  cat (int dim, octave_idx_type n, const Sparse<T, Alloc> *sparse_list);
651 
653 
654  // Generic any/all test functionality with arbitrary predicate.
655  template <typename F, bool zero>
656  bool test (F fcn) const
657  {
658  return octave::any_all_test<F, T, zero> (fcn, data (), nnz ());
659  }
660 
661  // Simpler calls.
662  template <typename F>
663  bool test_any (F fcn) const
664  { return test<F, false> (fcn); }
665 
666  template <typename F>
667  bool test_all (F fcn) const
668  { return test<F, true> (fcn); }
669 
670  // Overloads for function references.
671  bool test_any (bool (&fcn) (T)) const
672  { return test<bool (&) (T), false> (fcn); }
673 
674  bool test_any (bool (&fcn) (const T&)) const
675  { return test<bool (&) (const T&), false> (fcn); }
676 
677  bool test_all (bool (&fcn) (T)) const
678  { return test<bool (&) (T), true> (fcn); }
679 
680  bool test_all (bool (&fcn) (const T&)) const
681  { return test<bool (&) (const T&), true> (fcn); }
682 
683  template <typename U, typename F>
684  Sparse<U>
685  map (F fcn) const
686  {
687  Sparse<U> result;
688  U f_zero = fcn (0.0);
689 
690  if (f_zero != 0.0)
691  {
692  octave_idx_type nr = rows ();
693  octave_idx_type nc = cols ();
694 
695  result = Sparse<U> (nr, nc, f_zero);
696 
697  for (octave_idx_type j = 0; j < nc; j++)
698  for (octave_idx_type i = cidx (j); i < cidx (j+1); i++)
699  {
700  octave_quit ();
701  /* Use data instead of elem for better performance. */
702  result.data (ridx (i) + j * nr) = fcn (data (i));
703  }
704 
705  result.maybe_compress (true);
706  }
707  else
708  {
709  octave_idx_type nz = nnz ();
710  octave_idx_type nr = rows ();
711  octave_idx_type nc = cols ();
712 
713  result = Sparse<U> (nr, nc, nz);
714  octave_idx_type ii = 0;
715  result.cidx (ii) = 0;
716 
717  for (octave_idx_type j = 0; j < nc; j++)
718  {
719  for (octave_idx_type i = cidx (j); i < cidx (j+1); i++)
720  {
721  U val = fcn (data (i));
722  if (val != 0.0)
723  {
724  result.data (ii) = val;
725  result.ridx (ii++) = ridx (i);
726  }
727  octave_quit ();
728  }
729  result.cidx (j+1) = ii;
730  }
731 
732  result.maybe_compress (false);
733  }
734 
735  return result;
736  }
737 
738  // Overloads for function references.
739  template <typename U>
740  Sparse<U>
741  map (U (&fcn) (T)) const
742  { return map<U, U (&) (T)> (fcn); }
743 
744  template <typename U>
745  Sparse<U>
746  map (U (&fcn) (const T&)) const
747  { return map<U, U (&) (const T&)> (fcn); }
748 
749  bool indices_ok () const { return m_rep->indices_ok (); }
750 
751  bool any_element_is_nan () const
752  { return m_rep->any_element_is_nan (); }
753 };
754 
755 template <typename T>
757 std::istream&
758 read_sparse_matrix (std::istream& is, Sparse<T>& a,
759  T (*read_fcn) (std::istream&));
760 
761 #endif
octave_idx_type compute_index(octave_idx_type n, const dim_vector &dims)
Definition: Array-util.cc:177
std::istream & read_sparse_matrix(std::istream &is, Sparse< T > &a, T(*read_fcn)(std::istream &))
Definition: Sparse.cc:2792
SparseRep(const dim_vector &dv, octave_idx_type nz, U *d, octave_idx_type *r, octave_idx_type *c, const Alloc &xallocator=Alloc())
Definition: Sparse.h:124
bool indices_ok() const
Definition: Sparse.cc:179
octave_idx_type cols() const
Definition: Sparse.h:155
bool any_element_is_nan() const
Definition: Sparse.cc:187
void change_length(octave_idx_type nz)
Definition: Sparse.cc:145
octave_idx_type nzmax() const
Definition: Sparse.h:151
octave_idx_type nnz() const
Definition: Sparse.h:152
octave_idx_type m_ncols
Definition: Sparse.h:76
void T_deallocate(T_pointer data, size_t len)
Definition: Sparse.h:202
octave::refcount< octave_idx_type > m_count
Definition: Sparse.h:77
idx_type_pointer m_cidx
Definition: Sparse.h:73
Alloc_traits::template rebind_traits< T > T_Alloc_traits
Definition: Sparse.h:65
T_pointer m_data
Definition: Sparse.h:71
SparseRep(octave_idx_type n)
Definition: Sparse.h:85
SparseRep(octave_idx_type nr, octave_idx_type nc, octave_idx_type nz, const T *d, const octave_idx_type *r, const octave_idx_type *c)
Definition: Sparse.h:98
octave_idx_type columns() const
Definition: Sparse.h:156
octave_idx_type rows() const
Definition: Sparse.h:154
T cdata(octave_idx_type i) const
Definition: Sparse.h:164
T_pointer T_allocate(size_t len)
Definition: Sparse.h:191
octave_idx_type m_nrows
Definition: Sparse.h:75
SparseRep(const SparseRep &a)
Definition: Sparse.h:131
octave_idx_type & cidx(octave_idx_type i)
Definition: Sparse.h:170
void maybe_compress(bool remove_zeros)
Definition: Sparse.cc:120
SparseRep(octave_idx_type nr, octave_idx_type nc, octave_idx_type nz=1)
Definition: Sparse.h:91
T & data(octave_idx_type i)
Definition: Sparse.h:162
octave_idx_type length() const
Definition: Sparse.h:183
SparseRep(octave_idx_type nr, octave_idx_type nc, octave_idx_type nz, const U *d, const octave_idx_type *r, const octave_idx_type *c)
Definition: Sparse.h:111
T & elem(octave_idx_type r, octave_idx_type c)
Definition: Sparse.cc:66
idx_type_pointer m_ridx
Definition: Sparse.h:72
T_Alloc_traits::pointer T_pointer
Definition: Sparse.h:66
octave_idx_type m_nzmax
Definition: Sparse.h:74
idx_type_pointer idx_type_allocate(size_t len)
Definition: Sparse.h:211
std::allocator_traits< Alloc > Alloc_traits
Definition: Sparse.h:63
octave_idx_type cridx(octave_idx_type i) const
Definition: Sparse.h:168
idx_type_Alloc_traits::pointer idx_type_pointer
Definition: Sparse.h:69
octave_idx_type & ridx(octave_idx_type i)
Definition: Sparse.h:166
octave_idx_type ccidx(octave_idx_type i) const
Definition: Sparse.h:172
SparseRep & operator=(const SparseRep &)=delete
Alloc_traits::template rebind_traits< octave_idx_type > idx_type_Alloc_traits
Definition: Sparse.h:68
T celem(octave_idx_type r, octave_idx_type c) const
Definition: Sparse.cc:108
void idx_type_deallocate(idx_type_pointer idx, size_t len)
Definition: Sparse.h:222
Definition: Sparse.h:49
octave_idx_type cols() const
Definition: Sparse.h:352
Sparse< T, Alloc > diag(octave_idx_type k=0) const
Definition: Sparse.cc:2493
T xelem(const Array< octave_idx_type > &ra_idx) const
Definition: Sparse.h:418
Sparse< T, Alloc > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Definition: Sparse.cc:931
Sparse(octave_idx_type nr, octave_idx_type nc, octave_idx_type nz)
Definition: Sparse.h:279
bool test_all(F fcn) const
Definition: Sparse.h:667
octave_idx_type * xcidx()
Definition: Sparse.h:602
octave_idx_type nzmax() const
Amount of storage for nonzero elements.
Definition: Sparse.h:336
T checkelem(octave_idx_type n) const
Definition: Sparse.h:486
Sparse< T, Alloc > squeeze() const
Definition: Sparse.h:373
Sparse(const dim_vector &dv, octave_idx_type nz)
Definition: Sparse.h:275
T elem(const Array< octave_idx_type > &ra_idx) const
Definition: Sparse.h:516
octave_idx_type get_col_index(octave_idx_type k)
Definition: Sparse.h:356
T * xdata()
Definition: Sparse.h:576
Array< T > array_value() const
Definition: Sparse.cc:2767
void delete_elements(const octave::idx_vector &i)
Definition: Sparse.cc:1198
T & xelem(octave_idx_type i, octave_idx_type j)
Definition: Sparse.h:409
void make_unique()
Definition: Sparse.h:234
Sparse< T, Alloc > index(const octave::idx_vector &i, bool resize_ok=false) const
Definition: Sparse.cc:1434
void print_info(std::ostream &os, const std::string &prefix) const
Definition: Sparse.cc:3095
T & elem(const Array< octave_idx_type > &ra_idx)
Definition: Sparse.h:468
octave_idx_type columns() const
Definition: Sparse.h:353
bool test_any(bool(&fcn)(const T &)) const
Definition: Sparse.h:674
octave_idx_type * cidx()
Definition: Sparse.h:596
void assign(const octave::idx_vector &i, const Sparse< T, Alloc > &rhs)
Definition: Sparse.cc:1881
virtual ~Sparse()
Definition: Sparse.cc:707
T xelem(octave_idx_type n) const
Definition: Sparse.h:402
octave_idx_type & cidx(octave_idx_type i)
Definition: Sparse.h:597
void resize(octave_idx_type r, octave_idx_type c)
Definition: Sparse.cc:993
Sparse< U > map(U(&fcn)(T)) const
Definition: Sparse.h:741
T * data()
Definition: Sparse.h:574
bool test_any(bool(&fcn)(T)) const
Definition: Sparse.h:671
T & data(octave_idx_type i)
Definition: Sparse.h:575
bool indices_ok() const
Definition: Sparse.h:749
T & checkelem(octave_idx_type n)
Definition: Sparse.h:424
Sparse(octave_idx_type n)
Definition: Sparse.h:265
Sparse< T, Alloc >::SparseRep * m_rep
Definition: Sparse.h:249
T & operator()(octave_idx_type n)
Definition: Sparse.h:471
Sparse< U > map(F fcn) const
Definition: Sparse.h:685
Sparse< T, Alloc > sort(octave_idx_type dim=0, sortmode mode=ASCENDING) const
Definition: Sparse.cc:2318
octave_idx_type get_row_index(octave_idx_type k)
Definition: Sparse.h:355
bool test_any(F fcn) const
Definition: Sparse.h:663
octave_idx_type & xridx(octave_idx_type i)
Definition: Sparse.h:590
octave_idx_type & ridx(octave_idx_type i)
Definition: Sparse.h:584
octave_idx_type * ridx()
Definition: Sparse.h:583
Sparse< T, Alloc > & insert(const Sparse< T, Alloc > &a, octave_idx_type r, octave_idx_type c)
Definition: Sparse.cc:1044
T element_type
Definition: Sparse.h:52
T checkelem(const Array< octave_idx_type > &ra_idx) const
Definition: Sparse.h:502
bool test(F fcn) const
Definition: Sparse.h:656
octave_idx_type ridx(octave_idx_type i) const
Definition: Sparse.h:592
octave_idx_type ndims() const
Definition: Sparse.h:609
T & checkelem(octave_idx_type i, octave_idx_type j)
Definition: Sparse.h:435
void resize1(octave_idx_type n)
Definition: Sparse.cc:960
T elem(octave_idx_type n) const
Definition: Sparse.h:512
Sparse< T, Alloc > maybe_compress(bool remove_zeros=false)
Definition: Sparse.h:531
bool test_all(bool(&fcn)(T)) const
Definition: Sparse.h:677
Sparse(const Sparse< U > &a)
Definition: Sparse.h:303
OCTAVE_NORETURN T range_error(const char *fcn, octave_idx_type n) const
Definition: Sparse.cc:758
T & xdata(octave_idx_type i)
Definition: Sparse.h:577
T & elem(octave_idx_type n)
Definition: Sparse.h:456
octave_idx_type numel() const
Definition: Sparse.h:343
T * data() const
Definition: Sparse.h:581
Sparse()
Definition: Sparse.h:259
T & xelem(octave_idx_type n)
Definition: Sparse.h:395
static Sparse< T, Alloc > cat(int dim, octave_idx_type n, const Sparse< T, Alloc > *sparse_list)
Definition: Sparse.cc:2667
T data(octave_idx_type i) const
Definition: Sparse.h:579
T & elem(octave_idx_type i, octave_idx_type j)
Definition: Sparse.h:462
bool test_all(bool(&fcn)(const T &)) const
Definition: Sparse.h:680
Sparse(const dim_vector &dv, octave_idx_type nz, T *ptr, octave_idx_type *ridx, octave_idx_type *cidx, const Alloc &xallocator=Alloc())
Definition: Sparse.h:290
T xelem(octave_idx_type i, octave_idx_type j) const
Definition: Sparse.h:410
std::size_t byte_size() const
Definition: Sparse.h:364
octave_idx_type dim2() const
Definition: Sparse.h:349
octave_idx_type dim1() const
Definition: Sparse.h:348
T elem(octave_idx_type i, octave_idx_type j) const
Definition: Sparse.h:514
Sparse< T, Alloc > & operator=(const Sparse< T, Alloc > &a)
Definition: Sparse.cc:715
octave_idx_type nnz() const
Actual number of nonzero terms.
Definition: Sparse.h:339
Sparse(const Sparse< T, Alloc > &a)
Definition: Sparse.h:310
bool issquare() const
Definition: Sparse.h:568
T checkelem(octave_idx_type i, octave_idx_type j) const
Definition: Sparse.h:494
octave_idx_type rows() const
Definition: Sparse.h:351
void change_capacity(octave_idx_type nz)
Definition: Sparse.h:556
Sparse(octave_idx_type nr, octave_idx_type nc)
Definition: Sparse.h:269
octave_idx_type * cidx() const
Definition: Sparse.h:607
Sparse< T, Alloc > transpose() const
Definition: Sparse.cc:1140
dim_vector m_dimensions
Definition: Sparse.h:251
T & checkelem(const Array< octave_idx_type > &ra_idx)
Definition: Sparse.h:446
Sparse< T, Alloc > ipermute(const Array< octave_idx_type > &vec) const
Definition: Sparse.h:545
octave_idx_type * ridx() const
Definition: Sparse.h:594
T & xelem(const Array< octave_idx_type > &ra_idx)
Definition: Sparse.h:415
octave_idx_type cidx(octave_idx_type i) const
Definition: Sparse.h:605
bool any_element_is_nan() const
Definition: Sparse.h:751
octave_idx_type & xcidx(octave_idx_type i)
Definition: Sparse.h:603
Sparse< U > map(U(&fcn)(const T &)) const
Definition: Sparse.h:746
Sparse< T, Alloc > reshape(const dim_vector &new_dims) const
Definition: Sparse.cc:848
dim_vector dims() const
Definition: Sparse.h:371
octave_idx_type * xridx()
Definition: Sparse.h:589
bool isempty() const
Definition: Sparse.h:570
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_idx_type safe_numel() const
The following function will throw a std::bad_alloc () exception if the requested size is larger than ...
Definition: dim-vector.cc:98
octave_idx_type ndims() const
Number of dimensions.
Definition: dim-vector.h:257
octave::idx_vector idx_vector
Definition: idx-vector.h:1022
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
#define OCTAVE_API
Definition: main.cc:55
octave_idx_type n
Definition: mx-inlines.cc:761
T * r
Definition: mx-inlines.cc:781
sortmode
Definition: oct-sort.h:97
@ ASCENDING
Definition: oct-sort.h:97
T::size_type numel(const T &str)
Definition: oct-string.cc:74
const octave_base_value const Array< octave_idx_type > & ra_idx
F77_RET_T len
Definition: xerbla.cc:61