00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #if !defined (octave_Sparse_h)
00027 #define octave_Sparse_h 1
00028
00029 #include <cassert>
00030 #include <cstddef>
00031
00032 #include <iosfwd>
00033 #include <algorithm>
00034
00035 #include "Array.h"
00036 #include "dim-vector.h"
00037 #include "lo-error.h"
00038 #include "lo-utils.h"
00039
00040 #include "oct-sort.h"
00041 #include "oct-mem.h"
00042
00043 class idx_vector;
00044 class PermMatrix;
00045
00046
00047
00048
00049 template <class T>
00050 class
00051 Sparse
00052 {
00053 public:
00054
00055 typedef T element_type;
00056
00057 protected:
00058
00059
00060
00061
00062 class OCTAVE_API SparseRep
00063 {
00064 public:
00065
00066 T *d;
00067 octave_idx_type *r;
00068 octave_idx_type *c;
00069 octave_idx_type nzmx;
00070 octave_idx_type nrows;
00071 octave_idx_type ncols;
00072 octave_refcount<int> count;
00073
00074 SparseRep (void) : d (0), r (0), c (new octave_idx_type [1]), nzmx (0), nrows (0),
00075 ncols (0), count (1) { c[0] = 0; }
00076
00077 SparseRep (octave_idx_type n) : d (0), r (0), c (new octave_idx_type [n+1]), nzmx (0), nrows (n),
00078 ncols (n), count (1)
00079 {
00080 for (octave_idx_type i = 0; i < n + 1; i++)
00081 c[i] = 0;
00082 }
00083
00084 SparseRep (octave_idx_type nr, octave_idx_type nc) : d (0), r (0), c (new octave_idx_type [nc+1]), nzmx (0),
00085 nrows (nr), ncols (nc), count (1)
00086 {
00087 for (octave_idx_type i = 0; i < nc + 1; i++)
00088 c[i] = 0;
00089 }
00090
00091 SparseRep (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz) : d (new T [nz]),
00092 r (new octave_idx_type [nz]), c (new octave_idx_type [nc+1]), nzmx (nz), nrows (nr),
00093 ncols (nc), count (1)
00094 {
00095 for (octave_idx_type i = 0; i < nc + 1; i++)
00096 c[i] = 0;
00097 }
00098
00099 SparseRep (const SparseRep& a)
00100 : d (new T [a.nzmx]), r (new octave_idx_type [a.nzmx]), c (new octave_idx_type [a.ncols + 1]),
00101 nzmx (a.nzmx), nrows (a.nrows), ncols (a.ncols), count (1)
00102 {
00103 octave_idx_type nz = a.nnz ();
00104 copy_or_memcpy (nz, a.d, d);
00105 copy_or_memcpy (nz, a.r, r);
00106 copy_or_memcpy (ncols + 1, a.c, c);
00107 }
00108
00109 ~SparseRep (void) { delete [] d; delete [] r; delete [] c; }
00110
00111 octave_idx_type length (void) const { return nzmx; }
00112
00113 octave_idx_type nnz (void) const { return c [ncols]; }
00114
00115 T& elem (octave_idx_type _r, octave_idx_type _c);
00116
00117 T celem (octave_idx_type _r, octave_idx_type _c) const;
00118
00119 T& data (octave_idx_type i) { return d[i]; }
00120
00121 T cdata (octave_idx_type i) const { return d[i]; }
00122
00123 octave_idx_type& ridx (octave_idx_type i) { return r[i]; }
00124
00125 octave_idx_type cridx (octave_idx_type i) const { return r[i]; }
00126
00127 octave_idx_type& cidx (octave_idx_type i) { return c[i]; }
00128
00129 octave_idx_type ccidx (octave_idx_type i) const { return c[i]; }
00130
00131 void maybe_compress (bool remove_zeros);
00132
00133 void change_length (octave_idx_type nz);
00134
00135 bool indices_ok (void) const;
00136
00137 private:
00138
00139
00140
00141 SparseRep& operator = (const SparseRep& a);
00142 };
00143
00144
00145
00146 void make_unique (void)
00147 {
00148 if (rep->count > 1)
00149 {
00150 SparseRep *r = new SparseRep (*rep);
00151
00152 if (--rep->count == 0)
00153 delete rep;
00154
00155 rep = r;
00156 }
00157 }
00158
00159 public:
00160
00161
00162
00163
00164 typename Sparse<T>::SparseRep *rep;
00165
00166 dim_vector dimensions;
00167
00168 private:
00169
00170 typename Sparse<T>::SparseRep *nil_rep (void) const
00171 {
00172 static typename Sparse<T>::SparseRep nr;
00173 return &nr;
00174 }
00175
00176 public:
00177
00178 Sparse (void)
00179 : rep (nil_rep ()), dimensions (dim_vector(0,0))
00180 {
00181 rep->count++;
00182 }
00183
00184 explicit Sparse (octave_idx_type n)
00185 : rep (new typename Sparse<T>::SparseRep (n)),
00186 dimensions (dim_vector (n, n)) { }
00187
00188 explicit Sparse (octave_idx_type nr, octave_idx_type nc)
00189 : rep (new typename Sparse<T>::SparseRep (nr, nc)),
00190 dimensions (dim_vector (nr, nc)) { }
00191
00192 explicit Sparse (octave_idx_type nr, octave_idx_type nc, T val);
00193
00194 Sparse (const dim_vector& dv, octave_idx_type nz)
00195 : rep (new typename Sparse<T>::SparseRep (dv(0), dv(1), nz)),
00196 dimensions (dv) { }
00197
00198 Sparse (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz)
00199 : rep (new typename Sparse<T>::SparseRep (nr, nc, nz)),
00200 dimensions (dim_vector (nr, nc)) { }
00201
00202
00203
00204 explicit Sparse (const PermMatrix& a);
00205
00206
00207 template <class U>
00208 Sparse (const Sparse<U>& a)
00209 : rep (new typename Sparse<T>::SparseRep (a.rep->nrows, a.rep->ncols, a.rep->nzmx)),
00210 dimensions (a.dimensions)
00211 {
00212 octave_idx_type nz = a.nnz ();
00213 std::copy (a.rep->d, a.rep->d + nz, rep->d);
00214 copy_or_memcpy (nz, a.rep->r, rep->r);
00215 copy_or_memcpy (rep->ncols + 1, a.rep->c, rep->c);
00216 }
00217
00218
00219 Sparse (const Sparse<T>& a)
00220 : rep (a.rep), dimensions (a.dimensions)
00221 {
00222 rep->count++;
00223 }
00224
00225 public:
00226
00227 Sparse (const dim_vector& dv);
00228
00229 Sparse (const Sparse<T>& a, const dim_vector& dv);
00230
00231 Sparse (const Array<T>& a, const idx_vector& r, const idx_vector& c,
00232 octave_idx_type nr = -1, octave_idx_type nc = -1,
00233 bool sum_terms = true, octave_idx_type nzm = -1);
00234
00235
00236 Sparse (const Array<T>& a);
00237
00238 virtual ~Sparse (void);
00239
00240 Sparse<T>& operator = (const Sparse<T>& a);
00241
00242
00243
00244
00245 octave_idx_type nzmax (void) const { return rep->length (); }
00246 octave_idx_type capacity (void) const { return nzmax (); }
00247 octave_idx_type nnz (void) const { return rep->nnz (); }
00248
00249
00250
00251 octave_idx_type numel (void) const
00252 {
00253 return dimensions.safe_numel ();
00254 }
00255
00256 octave_idx_type nelem (void) const { return capacity (); }
00257 octave_idx_type length (void) const { return numel (); }
00258
00259 octave_idx_type dim1 (void) const { return dimensions(0); }
00260 octave_idx_type dim2 (void) const { return dimensions(1); }
00261
00262 octave_idx_type rows (void) const { return dim1 (); }
00263 octave_idx_type cols (void) const { return dim2 (); }
00264 octave_idx_type columns (void) const { return dim2 (); }
00265
00266 octave_idx_type get_row_index (octave_idx_type k) { return ridx (k); }
00267 octave_idx_type get_col_index (octave_idx_type k)
00268 {
00269 octave_idx_type ret = 0;
00270 while (cidx(ret+1) < k)
00271 ret++;
00272 return ret;
00273 }
00274
00275 size_t byte_size (void) const
00276 {
00277 return (static_cast<size_t>(cols () + 1) * sizeof (octave_idx_type)
00278 + static_cast<size_t> (capacity ()) * (sizeof (T) + sizeof (octave_idx_type)));
00279 }
00280
00281 dim_vector dims (void) const { return dimensions; }
00282
00283 Sparse<T> squeeze (void) const { return *this; }
00284
00285 octave_idx_type compute_index (const Array<octave_idx_type>& ra_idx) const;
00286
00287 T range_error (const char *fcn, octave_idx_type n) const;
00288 T& range_error (const char *fcn, octave_idx_type n);
00289
00290 T range_error (const char *fcn, octave_idx_type i, octave_idx_type j) const;
00291 T& range_error (const char *fcn, octave_idx_type i, octave_idx_type j);
00292
00293 T range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const;
00294 T& range_error (const char *fcn, const Array<octave_idx_type>& ra_idx);
00295
00296
00297
00298 T& xelem (octave_idx_type n)
00299 {
00300 octave_idx_type i = n % rows (), j = n / rows();
00301 return xelem (i, j);
00302 }
00303
00304 T xelem (octave_idx_type n) const
00305 {
00306 octave_idx_type i = n % rows (), j = n / rows();
00307 return xelem (i, j);
00308 }
00309
00310 T& xelem (octave_idx_type i, octave_idx_type j) { return rep->elem (i, j); }
00311 T xelem (octave_idx_type i, octave_idx_type j) const { return rep->celem (i, j); }
00312
00313 T& xelem (const Array<octave_idx_type>& ra_idx)
00314 { return xelem (compute_index (ra_idx)); }
00315
00316 T xelem (const Array<octave_idx_type>& ra_idx) const
00317 { return xelem (compute_index (ra_idx)); }
00318
00319
00320
00321
00322
00323 T& checkelem (octave_idx_type n)
00324 {
00325 if (n < 0 || n >= numel ())
00326 return range_error ("T& Sparse<T>::checkelem", n);
00327 else
00328 {
00329 make_unique ();
00330 return xelem (n);
00331 }
00332 }
00333
00334 T& checkelem (octave_idx_type i, octave_idx_type j)
00335 {
00336 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ())
00337 return range_error ("T& Sparse<T>::checkelem", i, j);
00338 else
00339 {
00340 make_unique ();
00341 return xelem (i, j);
00342 }
00343 }
00344
00345 T& checkelem (const Array<octave_idx_type>& ra_idx)
00346 {
00347 octave_idx_type i = compute_index (ra_idx);
00348
00349 if (i < 0)
00350 return range_error ("T& Sparse<T>::checkelem", ra_idx);
00351 else
00352 return elem (i);
00353 }
00354
00355 T& elem (octave_idx_type n)
00356 {
00357 make_unique ();
00358 return xelem (n);
00359 }
00360
00361 T& elem (octave_idx_type i, octave_idx_type j)
00362 {
00363 make_unique ();
00364 return xelem (i, j);
00365 }
00366
00367 T& elem (const Array<octave_idx_type>& ra_idx)
00368 { return Sparse<T>::elem (compute_index (ra_idx)); }
00369
00370 #if defined (BOUNDS_CHECKING)
00371 T& operator () (octave_idx_type n) { return checkelem (n); }
00372 T& operator () (octave_idx_type i, octave_idx_type j) { return checkelem (i, j); }
00373 T& operator () (const Array<octave_idx_type>& ra_idx) { return checkelem (ra_idx); }
00374 #else
00375 T& operator () (octave_idx_type n) { return elem (n); }
00376 T& operator () (octave_idx_type i, octave_idx_type j) { return elem (i, j); }
00377 T& operator () (const Array<octave_idx_type>& ra_idx) { return elem (ra_idx); }
00378 #endif
00379
00380 T checkelem (octave_idx_type n) const
00381 {
00382 if (n < 0 || n >= numel ())
00383 return range_error ("T Sparse<T>::checkelem", n);
00384 else
00385 return xelem (n);
00386 }
00387
00388 T checkelem (octave_idx_type i, octave_idx_type j) const
00389 {
00390 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ())
00391 return range_error ("T Sparse<T>::checkelem", i, j);
00392 else
00393 return xelem (i, j);
00394 }
00395
00396 T checkelem (const Array<octave_idx_type>& ra_idx) const
00397 {
00398 octave_idx_type i = compute_index (ra_idx);
00399
00400 if (i < 0)
00401 return range_error ("T Sparse<T>::checkelem", ra_idx);
00402 else
00403 return Sparse<T>::elem (i);
00404 }
00405
00406 T elem (octave_idx_type n) const { return xelem (n); }
00407
00408 T elem (octave_idx_type i, octave_idx_type j) const { return xelem (i, j); }
00409
00410 T elem (const Array<octave_idx_type>& ra_idx) const
00411 { return Sparse<T>::elem (compute_index (ra_idx)); }
00412
00413 #if defined (BOUNDS_CHECKING)
00414 T operator () (octave_idx_type n) const { return checkelem (n); }
00415 T operator () (octave_idx_type i, octave_idx_type j) const { return checkelem (i, j); }
00416 T operator () (const Array<octave_idx_type>& ra_idx) const { return checkelem (ra_idx); }
00417 #else
00418 T operator () (octave_idx_type n) const { return elem (n); }
00419 T operator () (octave_idx_type i, octave_idx_type j) const { return elem (i, j); }
00420 T operator () (const Array<octave_idx_type>& ra_idx) const { return elem (ra_idx); }
00421 #endif
00422
00423 Sparse<T> maybe_compress (bool remove_zeros = false)
00424 {
00425 if (remove_zeros)
00426 make_unique ();
00427
00428 rep->maybe_compress (remove_zeros);
00429 return (*this);
00430 }
00431
00432 Sparse<T> reshape (const dim_vector& new_dims) const;
00433
00434 Sparse<T> permute (const Array<octave_idx_type>& vec, bool inv = false) const;
00435
00436 Sparse<T> ipermute (const Array<octave_idx_type>& vec) const
00437 { return permute (vec, true); }
00438
00439 void resize1 (octave_idx_type n);
00440
00441 void resize (octave_idx_type r, octave_idx_type c);
00442
00443 void resize (const dim_vector& dv);
00444
00445 void change_capacity (octave_idx_type nz)
00446 {
00447 if (nz < nnz ())
00448 make_unique ();
00449 rep->change_length (nz);
00450 }
00451
00452 Sparse<T>& insert (const Sparse<T>& a, octave_idx_type r, octave_idx_type c);
00453 Sparse<T>& insert (const Sparse<T>& a, const Array<octave_idx_type>& idx);
00454
00455 bool is_square (void) const { return (dim1 () == dim2 ()); }
00456
00457 bool is_empty (void) const { return (rows () < 1 && cols () < 1); }
00458
00459 Sparse<T> transpose (void) const;
00460
00461 T* data (void) { make_unique (); return rep->d; }
00462 T& data (octave_idx_type i) { make_unique (); return rep->data (i); }
00463 T* xdata (void) { return rep->d; }
00464 T& xdata (octave_idx_type i) { return rep->data (i); }
00465
00466 T data (octave_idx_type i) const { return rep->data (i); }
00467
00468 T* data (void) const { return rep->d; }
00469
00470 octave_idx_type* ridx (void) { make_unique (); return rep->r; }
00471 octave_idx_type& ridx (octave_idx_type i) { make_unique (); return rep->ridx (i); }
00472 octave_idx_type* xridx (void) { return rep->r; }
00473 octave_idx_type& xridx (octave_idx_type i) { return rep->ridx (i); }
00474
00475 octave_idx_type ridx (octave_idx_type i) const { return rep->cridx (i); }
00476
00477 octave_idx_type* ridx (void) const { return rep->r; }
00478
00479 octave_idx_type* cidx (void) { make_unique (); return rep->c; }
00480 octave_idx_type& cidx (octave_idx_type i) { make_unique (); return rep->cidx (i); }
00481 octave_idx_type* xcidx (void) { return rep->c; }
00482 octave_idx_type& xcidx (octave_idx_type i) { return rep->cidx (i); }
00483
00484 octave_idx_type cidx (octave_idx_type i) const { return rep->ccidx (i); }
00485
00486 octave_idx_type* cidx (void) const { return rep->c; }
00487
00488 octave_idx_type ndims (void) const { return dimensions.length (); }
00489
00490 void delete_elements (const idx_vector& i);
00491
00492 void delete_elements (int dim, const idx_vector& i);
00493
00494 void delete_elements (const idx_vector& i, const idx_vector& j);
00495
00496 Sparse<T> index (const idx_vector& i, bool resize_ok = false) const;
00497
00498 Sparse<T> index (const idx_vector& i, const idx_vector& j, bool resize_ok = false) const;
00499
00500 void assign (const idx_vector& i, const Sparse<T>& rhs);
00501
00502 void assign (const idx_vector& i, const idx_vector& j, const Sparse<T>& rhs);
00503
00504 void print_info (std::ostream& os, const std::string& prefix) const;
00505
00506
00507
00508 void *mex_get_data (void) const { return const_cast<T *> (data ()); }
00509
00510 octave_idx_type *mex_get_ir (void) const { return const_cast<octave_idx_type *> (ridx ()); }
00511
00512 octave_idx_type *mex_get_jc (void) const { return const_cast<octave_idx_type *> (cidx ()); }
00513
00514 Sparse<T> sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const;
00515 Sparse<T> sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0,
00516 sortmode mode = ASCENDING) const;
00517
00518 Sparse<T> diag (octave_idx_type k = 0) const;
00519
00520
00521 static Sparse<T>
00522 cat (int dim, octave_idx_type n, const Sparse<T> *sparse_list);
00523
00524 Array<T> array_value (void) const;
00525
00526 template <class U, class F>
00527 Sparse<U>
00528 map (F fcn) const
00529 {
00530 Sparse<U> result;
00531 U f_zero = fcn (0.);
00532
00533 if (f_zero != 0.)
00534 {
00535 octave_idx_type nr = rows ();
00536 octave_idx_type nc = cols ();
00537
00538 result = Sparse<U> (nr, nc, f_zero);
00539
00540 for (octave_idx_type j = 0; j < nc; j++)
00541 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++)
00542 {
00543 octave_quit ();
00544
00545 result.data (ridx (i) + j * nr) = fcn (data(i));
00546 }
00547
00548 result.maybe_compress (true);
00549 }
00550 else
00551 {
00552 octave_idx_type nz = nnz ();
00553 octave_idx_type nr = rows ();
00554 octave_idx_type nc = cols ();
00555
00556 result = Sparse<U> (nr, nc, nz);
00557 octave_idx_type ii = 0;
00558 result.cidx (ii) = 0;
00559
00560 for (octave_idx_type j = 0; j < nc; j++)
00561 {
00562 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++)
00563 {
00564 U val = fcn (data (i));
00565 if (val != 0.0)
00566 {
00567 result.data (ii) = val;
00568 result.ridx (ii++) = ridx (i);
00569 }
00570 octave_quit ();
00571 }
00572 result.cidx (j+1) = ii;
00573 }
00574
00575 result.maybe_compress (false);
00576 }
00577
00578 return result;
00579 }
00580
00581
00582 template <class U>
00583 Sparse<U>
00584 map (U (&fcn) (T)) const
00585 { return map<U, U (&) (T)> (fcn); }
00586
00587 template <class U>
00588 Sparse<U>
00589 map (U (&fcn) (const T&)) const
00590 { return map<U, U (&) (const T&)> (fcn); }
00591
00592 bool indices_ok (void) const { return rep->indices_ok (); }
00593 };
00594
00595 template<typename T>
00596 std::istream&
00597 read_sparse_matrix (std::istream& is, Sparse<T>& a,
00598 T (*read_fcn) (std::istream&))
00599 {
00600 octave_idx_type nr = a.rows ();
00601 octave_idx_type nc = a.cols ();
00602 octave_idx_type nz = a.nzmax ();
00603
00604 if (nr > 0 && nc > 0)
00605 {
00606 octave_idx_type itmp;
00607 octave_idx_type jtmp;
00608 octave_idx_type iold = 0;
00609 octave_idx_type jold = 0;
00610 octave_idx_type ii = 0;
00611 T tmp;
00612
00613 a.cidx (0) = 0;
00614 for (octave_idx_type i = 0; i < nz; i++)
00615 {
00616 itmp = 0; jtmp = 0;
00617 is >> itmp;
00618 itmp--;
00619
00620 is >> jtmp;
00621 jtmp--;
00622
00623 if (itmp < 0 || itmp >= nr)
00624 {
00625 (*current_liboctave_error_handler)
00626 ("invalid sparse matrix: row index = %d out of range",
00627 itmp + 1);
00628 is.setstate (std::ios::failbit);
00629 goto done;
00630 }
00631
00632 if (jtmp < 0 || jtmp >= nc)
00633 {
00634 (*current_liboctave_error_handler)
00635 ("invalid sparse matrix: column index = %d out of range",
00636 jtmp + 1);
00637 is.setstate (std::ios::failbit);
00638 goto done;
00639 }
00640
00641 if (jtmp < jold)
00642 {
00643 (*current_liboctave_error_handler)
00644 ("invalid sparse matrix: column indices must appear in ascending order");
00645 is.setstate (std::ios::failbit);
00646 goto done;
00647 }
00648 else if (jtmp > jold)
00649 {
00650 for (octave_idx_type j = jold; j < jtmp; j++)
00651 a.cidx(j+1) = ii;
00652 }
00653 else if (itmp < iold)
00654 {
00655 (*current_liboctave_error_handler)
00656 ("invalid sparse matrix: row indices must appear in ascending order in each column");
00657 is.setstate (std::ios::failbit);
00658 goto done;
00659 }
00660
00661 iold = itmp;
00662 jold = jtmp;
00663
00664 tmp = read_fcn (is);
00665
00666 if (is)
00667 {
00668 a.data (ii) = tmp;
00669 a.ridx (ii++) = itmp;
00670 }
00671 else
00672 goto done;
00673 }
00674
00675 for (octave_idx_type j = jold; j < nc; j++)
00676 a.cidx(j+1) = ii;
00677 }
00678
00679 done:
00680
00681 return is;
00682 }
00683
00684 #endif