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