Mercurial > octave
annotate liboctave/Sparse.h @ 10515:189274f6c7c4
omissions from last patch
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 13 Apr 2010 14:56:29 +0200 |
parents | 40c58502a78b |
children | 4d1fc073fbb7 |
rev | line source |
---|---|
5164 | 1 // Template sparse classes |
2 /* | |
3 | |
8920 | 4 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 David Bateman |
7016 | 5 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Andy Adler |
6 | |
7 This file is part of Octave. | |
5164 | 8 |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
5164 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
5164 | 22 |
23 */ | |
24 | |
25 #if !defined (octave_Sparse_h) | |
26 #define octave_Sparse_h 1 | |
27 | |
28 #include <cassert> | |
29 #include <cstddef> | |
30 | |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
31 #include <iosfwd> |
10514
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
32 #include <algorithm> |
5164 | 33 |
34 #include "Array.h" | |
35 #include "dim-vector.h" | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
36 #include "lo-error.h" |
5164 | 37 #include "lo-utils.h" |
38 | |
7433 | 39 #include "oct-sort.h" |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10358
diff
changeset
|
40 #include "oct-mem.h" |
7433 | 41 |
5164 | 42 class idx_vector; |
43 | |
44 // Two dimensional sparse class. Handles the reference counting for | |
45 // all the derived classes. | |
46 | |
47 template <class T> | |
48 class | |
49 Sparse | |
50 { | |
8181
1ebcb9872ced
fix sparse-matrix bool/cmp op instantiation problem
John W. Eaton <jwe@octave.org>
parents:
7717
diff
changeset
|
51 public: |
1ebcb9872ced
fix sparse-matrix bool/cmp op instantiation problem
John W. Eaton <jwe@octave.org>
parents:
7717
diff
changeset
|
52 |
8918
f5408862892f
Consistently use element_type in the array classes.
Jason Riedy <jason@acm.org>
parents:
8181
diff
changeset
|
53 typedef T element_type; |
8181
1ebcb9872ced
fix sparse-matrix bool/cmp op instantiation problem
John W. Eaton <jwe@octave.org>
parents:
7717
diff
changeset
|
54 |
5164 | 55 protected: |
56 //-------------------------------------------------------------------- | |
57 // The real representation of all Sparse arrays. | |
58 //-------------------------------------------------------------------- | |
59 | |
6108 | 60 class OCTAVE_API SparseRep |
5164 | 61 { |
62 public: | |
63 | |
64 T *d; | |
5275 | 65 octave_idx_type *r; |
66 octave_idx_type *c; | |
5604 | 67 octave_idx_type nzmx; |
5275 | 68 octave_idx_type nrows; |
69 octave_idx_type ncols; | |
5164 | 70 int count; |
71 | |
5604 | 72 SparseRep (void) : d (0), r (0), c (new octave_idx_type [1]), nzmx (0), nrows (0), |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
73 ncols (0), count (1) { c[0] = 0; } |
5164 | 74 |
5604 | 75 SparseRep (octave_idx_type n) : d (0), r (0), c (new octave_idx_type [n+1]), nzmx (0), nrows (n), |
5164 | 76 ncols (n), count (1) |
77 { | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
78 for (octave_idx_type i = 0; i < n + 1; i++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
79 c[i] = 0; |
5164 | 80 } |
81 | |
5604 | 82 SparseRep (octave_idx_type nr, octave_idx_type nc) : d (0), r (0), c (new octave_idx_type [nc+1]), nzmx (0), |
5164 | 83 nrows (nr), ncols (nc), count (1) |
84 { | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
85 for (octave_idx_type i = 0; i < nc + 1; i++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
86 c[i] = 0; |
5164 | 87 } |
88 | |
5275 | 89 SparseRep (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz) : d (new T [nz]), |
5604 | 90 r (new octave_idx_type [nz]), c (new octave_idx_type [nc+1]), nzmx (nz), nrows (nr), |
5164 | 91 ncols (nc), count (1) |
92 { | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
93 for (octave_idx_type i = 0; i < nc + 1; i++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
94 c[i] = 0; |
5164 | 95 } |
96 | |
97 SparseRep (const SparseRep& a) | |
5604 | 98 : d (new T [a.nzmx]), r (new octave_idx_type [a.nzmx]), c (new octave_idx_type [a.ncols + 1]), |
99 nzmx (a.nzmx), nrows (a.nrows), ncols (a.ncols), count (1) | |
5164 | 100 { |
10514
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
101 octave_idx_type nz = a.nnz (); |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
102 copy_or_memcpy (nz, a.d, d); |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
103 copy_or_memcpy (nz, a.r, r); |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
104 copy_or_memcpy (ncols + 1, a.c, c); |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
105 } |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
106 |
5164 | 107 ~SparseRep (void) { delete [] d; delete [] r; delete [] c; } |
108 | |
5604 | 109 octave_idx_type length (void) const { return nzmx; } |
5164 | 110 |
5604 | 111 octave_idx_type nnz (void) const { return c [ncols]; } |
5164 | 112 |
5275 | 113 T& elem (octave_idx_type _r, octave_idx_type _c); |
5164 | 114 |
5275 | 115 T celem (octave_idx_type _r, octave_idx_type _c) const; |
5164 | 116 |
5275 | 117 T& data (octave_idx_type i) { return d[i]; } |
5164 | 118 |
5275 | 119 T cdata (octave_idx_type i) const { return d[i]; } |
5164 | 120 |
5275 | 121 octave_idx_type& ridx (octave_idx_type i) { return r[i]; } |
5164 | 122 |
5275 | 123 octave_idx_type cridx (octave_idx_type i) const { return r[i]; } |
5164 | 124 |
5275 | 125 octave_idx_type& cidx (octave_idx_type i) { return c[i]; } |
5164 | 126 |
5275 | 127 octave_idx_type ccidx (octave_idx_type i) const { return c[i]; } |
5164 | 128 |
129 void maybe_compress (bool remove_zeros); | |
130 | |
5275 | 131 void change_length (octave_idx_type nz); |
5164 | 132 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
133 bool indices_ok (void) const; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
134 |
5164 | 135 private: |
136 | |
137 // No assignment! | |
138 | |
139 SparseRep& operator = (const SparseRep& a); | |
140 }; | |
141 | |
142 //-------------------------------------------------------------------- | |
143 | |
144 void make_unique (void) | |
145 { | |
146 if (rep->count > 1) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
147 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
148 --rep->count; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
149 rep = new SparseRep (*rep); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
150 } |
5164 | 151 } |
152 | |
153 public: | |
154 | |
155 // !!! WARNING !!! -- these should be protected, not public. You | |
156 // should not access these data members directly! | |
157 | |
158 typename Sparse<T>::SparseRep *rep; | |
159 | |
160 dim_vector dimensions; | |
161 | |
162 private: | |
163 | |
164 typename Sparse<T>::SparseRep *nil_rep (void) const | |
165 { | |
166 static typename Sparse<T>::SparseRep *nr | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
167 = new typename Sparse<T>::SparseRep (); |
5164 | 168 |
169 nr->count++; | |
170 | |
171 return nr; | |
172 } | |
173 | |
174 public: | |
175 | |
176 Sparse (void) | |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
177 : rep (nil_rep ()), dimensions (dim_vector(0,0)) { } |
5164 | 178 |
5275 | 179 explicit Sparse (octave_idx_type n) |
5164 | 180 : rep (new typename Sparse<T>::SparseRep (n)), |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
181 dimensions (dim_vector (n, n)) { } |
5164 | 182 |
5275 | 183 explicit Sparse (octave_idx_type nr, octave_idx_type nc) |
5164 | 184 : rep (new typename Sparse<T>::SparseRep (nr, nc)), |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
185 dimensions (dim_vector (nr, nc)) { } |
5164 | 186 |
5275 | 187 explicit Sparse (octave_idx_type nr, octave_idx_type nc, T val); |
5164 | 188 |
5275 | 189 Sparse (const dim_vector& dv, octave_idx_type nz) |
5164 | 190 : rep (new typename Sparse<T>::SparseRep (dv(0), dv(1), nz)), |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
191 dimensions (dv) { } |
5164 | 192 |
5275 | 193 Sparse (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz) |
5164 | 194 : rep (new typename Sparse<T>::SparseRep (nr, nc, nz)), |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
195 dimensions (dim_vector (nr, nc)) { } |
5164 | 196 |
10514
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
197 // Type conversion case. Preserves capacity (). |
10515
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
198 template <class U> |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
199 Sparse (const Sparse<U>& a) |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
200 : rep (new typename Sparse<T>::SparseRep (a.rep->nrows, a.rep->ncols, a.rep->nzmx)), |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
201 dimensions (a.dimensions) |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
202 { |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
203 octave_idx_type nz = a.nnz (); |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
204 std::copy (a.rep->d, a.rep->d + nz, rep->d); |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
205 copy_or_memcpy (nz, a.rep->r, rep->r); |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
206 copy_or_memcpy (rep->ncols + 1, a.rep->c, rep->c); |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
207 } |
5164 | 208 |
209 // No type conversion case. | |
210 Sparse (const Sparse<T>& a) | |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
211 : rep (a.rep), dimensions (a.dimensions) |
5164 | 212 { |
213 rep->count++; | |
214 } | |
215 | |
216 public: | |
217 | |
218 Sparse (const dim_vector& dv); | |
219 | |
220 Sparse (const Sparse<T>& a, const dim_vector& dv); | |
221 | |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
222 Sparse (const Array<T>& a, const idx_vector& r, const idx_vector& c, |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
223 octave_idx_type nr = -1, octave_idx_type nc = -1, bool sum_terms = true); |
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
224 |
5164 | 225 // Sparsify a normal matrix |
226 Sparse (const Array<T>& a); | |
227 | |
228 virtual ~Sparse (void); | |
229 | |
7717
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
230 Sparse<T>& operator = (const Sparse<T>& a); |
5164 | 231 |
5604 | 232 // Note that nzmax and capacity are the amount of storage for |
233 // non-zero elements, while nnz is the actual number of non-zero | |
234 // terms. | |
235 octave_idx_type nzmax (void) const { return rep->length (); } | |
236 octave_idx_type capacity (void) const { return nzmax (); } | |
237 octave_idx_type nnz (void) const { return rep->nnz (); } | |
5164 | 238 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
239 // Querying the number of elements (incl. zeros) may overflow the index type, |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
240 // so don't do it unless you really need it. |
5275 | 241 octave_idx_type numel (void) const |
5164 | 242 { |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
243 return dimensions.safe_numel (); |
5164 | 244 } |
245 | |
5275 | 246 octave_idx_type nelem (void) const { return capacity (); } |
247 octave_idx_type length (void) const { return numel (); } | |
5164 | 248 |
5275 | 249 octave_idx_type dim1 (void) const { return dimensions(0); } |
250 octave_idx_type dim2 (void) const { return dimensions(1); } | |
5164 | 251 |
5275 | 252 octave_idx_type rows (void) const { return dim1 (); } |
253 octave_idx_type cols (void) const { return dim2 (); } | |
254 octave_idx_type columns (void) const { return dim2 (); } | |
5164 | 255 |
5275 | 256 octave_idx_type get_row_index (octave_idx_type k) { return ridx (k); } |
257 octave_idx_type get_col_index (octave_idx_type k) | |
5164 | 258 { |
5275 | 259 octave_idx_type ret = 0; |
5164 | 260 while (cidx(ret+1) < k) |
261 ret++; | |
262 return ret; | |
263 } | |
10358
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
264 |
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
265 size_t byte_size (void) const |
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
266 { |
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
267 return (static_cast<size_t>(cols () + 1) * sizeof (octave_idx_type) |
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
268 + static_cast<size_t> (capacity ()) * (sizeof (T) + sizeof (octave_idx_type))); |
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
269 } |
5164 | 270 |
271 dim_vector dims (void) const { return dimensions; } | |
272 | |
273 Sparse<T> squeeze (void) const { return *this; } | |
274 | |
5275 | 275 octave_idx_type compute_index (const Array<octave_idx_type>& ra_idx) const; |
5164 | 276 |
5275 | 277 T range_error (const char *fcn, octave_idx_type n) const; |
278 T& range_error (const char *fcn, octave_idx_type n); | |
5164 | 279 |
5275 | 280 T range_error (const char *fcn, octave_idx_type i, octave_idx_type j) const; |
281 T& range_error (const char *fcn, octave_idx_type i, octave_idx_type j); | |
5164 | 282 |
5275 | 283 T range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const; |
284 T& range_error (const char *fcn, const Array<octave_idx_type>& ra_idx); | |
5164 | 285 |
286 // No checking, even for multiple references, ever. | |
287 | |
5275 | 288 T& xelem (octave_idx_type n) |
5164 | 289 { |
5275 | 290 octave_idx_type i = n % rows (), j = n / rows(); |
5164 | 291 return xelem (i, j); |
292 } | |
293 | |
5275 | 294 T xelem (octave_idx_type n) const |
5164 | 295 { |
5275 | 296 octave_idx_type i = n % rows (), j = n / rows(); |
5164 | 297 return xelem (i, j); |
298 } | |
299 | |
5275 | 300 T& xelem (octave_idx_type i, octave_idx_type j) { return rep->elem (i, j); } |
301 T xelem (octave_idx_type i, octave_idx_type j) const { return rep->celem (i, j); } | |
5164 | 302 |
5275 | 303 T& xelem (const Array<octave_idx_type>& ra_idx) |
5164 | 304 { return xelem (compute_index (ra_idx)); } |
305 | |
5275 | 306 T xelem (const Array<octave_idx_type>& ra_idx) const |
5164 | 307 { return xelem (compute_index (ra_idx)); } |
308 | |
5775 | 309 // FIXME -- would be nice to fix this so that we don't |
5164 | 310 // unnecessarily force a copy, but that is not so easy, and I see no |
311 // clean way to do it. | |
312 | |
5275 | 313 T& checkelem (octave_idx_type n) |
5164 | 314 { |
315 if (n < 0 || n >= numel ()) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
316 return range_error ("T& Sparse<T>::checkelem", n); |
5164 | 317 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
318 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
319 make_unique (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
320 return xelem (n); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
321 } |
5164 | 322 } |
323 | |
5275 | 324 T& checkelem (octave_idx_type i, octave_idx_type j) |
5164 | 325 { |
326 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ()) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
327 return range_error ("T& Sparse<T>::checkelem", i, j); |
5164 | 328 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
329 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
330 make_unique (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
331 return xelem (i, j); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
332 } |
5164 | 333 } |
334 | |
5275 | 335 T& checkelem (const Array<octave_idx_type>& ra_idx) |
5164 | 336 { |
5275 | 337 octave_idx_type i = compute_index (ra_idx); |
5164 | 338 |
339 if (i < 0) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
340 return range_error ("T& Sparse<T>::checkelem", ra_idx); |
5164 | 341 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
342 return elem (i); |
5164 | 343 } |
344 | |
5275 | 345 T& elem (octave_idx_type n) |
5164 | 346 { |
347 make_unique (); | |
348 return xelem (n); | |
349 } | |
350 | |
5275 | 351 T& elem (octave_idx_type i, octave_idx_type j) |
5164 | 352 { |
353 make_unique (); | |
354 return xelem (i, j); | |
355 } | |
356 | |
5275 | 357 T& elem (const Array<octave_idx_type>& ra_idx) |
5164 | 358 { return Sparse<T>::elem (compute_index (ra_idx)); } |
359 | |
360 #if defined (BOUNDS_CHECKING) | |
5275 | 361 T& operator () (octave_idx_type n) { return checkelem (n); } |
362 T& operator () (octave_idx_type i, octave_idx_type j) { return checkelem (i, j); } | |
363 T& operator () (const Array<octave_idx_type>& ra_idx) { return checkelem (ra_idx); } | |
5164 | 364 #else |
5275 | 365 T& operator () (octave_idx_type n) { return elem (n); } |
366 T& operator () (octave_idx_type i, octave_idx_type j) { return elem (i, j); } | |
367 T& operator () (const Array<octave_idx_type>& ra_idx) { return elem (ra_idx); } | |
5164 | 368 #endif |
369 | |
5275 | 370 T checkelem (octave_idx_type n) const |
5164 | 371 { |
372 if (n < 0 || n >= numel ()) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
373 return range_error ("T Sparse<T>::checkelem", n); |
5164 | 374 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
375 return xelem (n); |
5164 | 376 } |
377 | |
5275 | 378 T checkelem (octave_idx_type i, octave_idx_type j) const |
5164 | 379 { |
380 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ()) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
381 return range_error ("T Sparse<T>::checkelem", i, j); |
5164 | 382 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
383 return xelem (i, j); |
5164 | 384 } |
385 | |
5275 | 386 T checkelem (const Array<octave_idx_type>& ra_idx) const |
5164 | 387 { |
5275 | 388 octave_idx_type i = compute_index (ra_idx); |
5164 | 389 |
390 if (i < 0) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
391 return range_error ("T Sparse<T>::checkelem", ra_idx); |
5164 | 392 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
393 return Sparse<T>::elem (i); |
5164 | 394 } |
395 | |
5275 | 396 T elem (octave_idx_type n) const { return xelem (n); } |
5164 | 397 |
5275 | 398 T elem (octave_idx_type i, octave_idx_type j) const { return xelem (i, j); } |
5164 | 399 |
5275 | 400 T elem (const Array<octave_idx_type>& ra_idx) const |
5164 | 401 { return Sparse<T>::elem (compute_index (ra_idx)); } |
402 | |
403 #if defined (BOUNDS_CHECKING) | |
5275 | 404 T operator () (octave_idx_type n) const { return checkelem (n); } |
405 T operator () (octave_idx_type i, octave_idx_type j) const { return checkelem (i, j); } | |
406 T operator () (const Array<octave_idx_type>& ra_idx) const { return checkelem (ra_idx); } | |
5164 | 407 #else |
5275 | 408 T operator () (octave_idx_type n) const { return elem (n); } |
409 T operator () (octave_idx_type i, octave_idx_type j) const { return elem (i, j); } | |
410 T operator () (const Array<octave_idx_type>& ra_idx) const { return elem (ra_idx); } | |
5164 | 411 #endif |
412 | |
413 Sparse<T> maybe_compress (bool remove_zeros = false) | |
10497
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
414 { |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
415 if (remove_zeros) |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
416 make_unique (); // Needs to unshare because elements are removed. |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
417 |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
418 rep->maybe_compress (remove_zeros); |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
419 return (*this); |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
420 } |
5164 | 421 |
422 Sparse<T> reshape (const dim_vector& new_dims) const; | |
423 | |
5275 | 424 Sparse<T> permute (const Array<octave_idx_type>& vec, bool inv = false) const; |
5164 | 425 |
5275 | 426 Sparse<T> ipermute (const Array<octave_idx_type>& vec) const |
5164 | 427 { return permute (vec, true); } |
428 | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
429 void resize1 (octave_idx_type n); |
5164 | 430 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
431 void resize (octave_idx_type r, octave_idx_type c); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
432 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
433 void resize (const dim_vector& dv); |
5164 | 434 |
10497
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
435 void change_capacity (octave_idx_type nz) |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
436 { |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
437 if (nz < nnz ()) |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
438 make_unique (); // Unshare now because elements will be truncated. |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
439 rep->change_length (nz); |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
440 } |
5164 | 441 |
5275 | 442 Sparse<T>& insert (const Sparse<T>& a, octave_idx_type r, octave_idx_type c); |
443 Sparse<T>& insert (const Sparse<T>& a, const Array<octave_idx_type>& idx); | |
5164 | 444 |
445 bool is_square (void) const { return (dim1 () == dim2 ()); } | |
446 | |
447 bool is_empty (void) const { return (rows () < 1 && cols () < 1); } | |
448 | |
449 Sparse<T> transpose (void) const; | |
450 | |
451 T* data (void) { make_unique (); return rep->d; } | |
5275 | 452 T& data (octave_idx_type i) { make_unique (); return rep->data (i); } |
5164 | 453 T* xdata (void) { return rep->d; } |
5275 | 454 T& xdata (octave_idx_type i) { return rep->data (i); } |
5164 | 455 |
5275 | 456 T data (octave_idx_type i) const { return rep->data (i); } |
5900 | 457 // FIXME -- shouldn't this be returning const T*? |
5164 | 458 T* data (void) const { return rep->d; } |
459 | |
5275 | 460 octave_idx_type* ridx (void) { make_unique (); return rep->r; } |
461 octave_idx_type& ridx (octave_idx_type i) { make_unique (); return rep->ridx (i); } | |
462 octave_idx_type* xridx (void) { return rep->r; } | |
463 octave_idx_type& xridx (octave_idx_type i) { return rep->ridx (i); } | |
5164 | 464 |
5275 | 465 octave_idx_type ridx (octave_idx_type i) const { return rep->cridx (i); } |
5900 | 466 // FIXME -- shouldn't this be returning const octave_idx_type*? |
5275 | 467 octave_idx_type* ridx (void) const { return rep->r; } |
5164 | 468 |
5275 | 469 octave_idx_type* cidx (void) { make_unique (); return rep->c; } |
470 octave_idx_type& cidx (octave_idx_type i) { make_unique (); return rep->cidx (i); } | |
471 octave_idx_type* xcidx (void) { return rep->c; } | |
472 octave_idx_type& xcidx (octave_idx_type i) { return rep->cidx (i); } | |
5164 | 473 |
5275 | 474 octave_idx_type cidx (octave_idx_type i) const { return rep->ccidx (i); } |
5900 | 475 // FIXME -- shouldn't this be returning const octave_idx_type*? |
5275 | 476 octave_idx_type* cidx (void) const { return rep->c; } |
5164 | 477 |
5275 | 478 octave_idx_type ndims (void) const { return dimensions.length (); } |
5164 | 479 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10480
diff
changeset
|
480 void delete_elements (const idx_vector& i); |
5164 | 481 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10480
diff
changeset
|
482 void delete_elements (int dim, const idx_vector& i); |
5164 | 483 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10480
diff
changeset
|
484 void delete_elements (const idx_vector& i, const idx_vector& j); |
5164 | 485 |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
486 Sparse<T> index (const idx_vector& i, bool resize_ok = false) const; |
5164 | 487 |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
488 Sparse<T> index (const idx_vector& i, const idx_vector& j, bool resize_ok = false) const; |
5164 | 489 |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
490 void assign (const idx_vector& i, const Sparse<T>& rhs); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
491 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
492 void assign (const idx_vector& i, const idx_vector& j, const Sparse<T>& rhs); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
493 |
5164 | 494 void print_info (std::ostream& os, const std::string& prefix) const; |
495 | |
5900 | 496 // Unsafe. These functions exist to support the MEX interface. |
497 // You should not use them anywhere else. | |
498 void *mex_get_data (void) const { return const_cast<T *> (data ()); } | |
499 | |
500 octave_idx_type *mex_get_ir (void) const { return const_cast<octave_idx_type *> (ridx ()); } | |
501 | |
502 octave_idx_type *mex_get_jc (void) const { return const_cast<octave_idx_type *> (cidx ()); } | |
7433 | 503 |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
504 Sparse<T> sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const; |
7433 | 505 Sparse<T> sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0, |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
506 sortmode mode = ASCENDING) const; |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
507 |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7602
diff
changeset
|
508 Sparse<T> diag (octave_idx_type k = 0) const; |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7602
diff
changeset
|
509 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
510 Array<T> array_value (void) const; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
511 |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
512 template <class U, class F> |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
513 Sparse<U> |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
514 map (F fcn) const |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
515 { |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
516 Sparse<U> result; |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
517 U f_zero = fcn (0.); |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
518 |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
519 if (f_zero != 0.) |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
520 { |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
521 octave_idx_type nr = rows (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
522 octave_idx_type nc = cols (); |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
523 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
524 result = Sparse<U> (nr, nc, f_zero); |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
525 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
526 for (octave_idx_type j = 0; j < nc; j++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
527 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
528 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
529 octave_quit (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
530 /* Use data instead of elem for better performance. */ |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
531 result.data (ridx (i) + j * nr) = fcn (data(i)); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
532 } |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
533 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
534 result.maybe_compress (true); |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
535 } |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
536 else |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
537 { |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
538 octave_idx_type nz = nnz (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
539 octave_idx_type nr = rows (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
540 octave_idx_type nc = cols (); |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
541 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
542 result = Sparse<U> (nr, nc, nz); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
543 octave_idx_type ii = 0; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
544 result.cidx (ii) = 0; |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
545 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
546 for (octave_idx_type j = 0; j < nc; j++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
547 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
548 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
549 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
550 U val = fcn (data (i)); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
551 if (val != 0.0) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
552 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
553 result.data (ii) = val; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
554 result.ridx (ii++) = ridx (i); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
555 } |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
556 octave_quit (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
557 } |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
558 result.cidx (j+1) = ii; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
559 } |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
560 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
561 result.maybe_compress (false); |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
562 } |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
563 |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
564 return result; |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
565 } |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
566 |
9812
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
567 // Overloads for function references. |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
568 template <class U> |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
569 Sparse<U> |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
570 map (U (&fcn) (T)) const |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
571 { return map<U, U (&) (T)> (fcn); } |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
572 |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
573 template <class U> |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
574 Sparse<U> |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
575 map (U (&fcn) (const T&)) const |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
576 { return map<U, U (&) (const T&)> (fcn); } |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
577 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
578 bool indices_ok (void) const { return rep->indices_ok (); } |
5164 | 579 }; |
580 | |
581 // NOTE: these functions should be friends of the Sparse<T> class and | |
582 // Sparse<T>::dimensions should be protected, not public, but we can't | |
583 // do that because of bugs in gcc prior to 3.3. | |
584 | |
585 template <class LT, class RT> | |
586 /* friend */ int | |
587 assign (Sparse<LT>& lhs, const Sparse<RT>& rhs); | |
588 | |
589 template <class LT, class RT> | |
590 /* friend */ int | |
591 assign1 (Sparse<LT>& lhs, const Sparse<RT>& rhs); | |
592 | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
593 template<typename T> |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
594 std::istream& |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
595 read_sparse_matrix (std::istream& is, Sparse<T>& a, |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
596 T (*read_fcn) (std::istream&)) |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
597 { |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
598 octave_idx_type nr = a.rows (); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
599 octave_idx_type nc = a.cols (); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
600 octave_idx_type nz = a.nzmax (); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
601 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
602 if (nr > 0 && nc > 0) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
603 { |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
604 octave_idx_type itmp; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
605 octave_idx_type jtmp; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
606 octave_idx_type iold = 0; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
607 octave_idx_type jold = 0; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
608 octave_idx_type ii = 0; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
609 T tmp; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
610 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
611 a.cidx (0) = 0; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
612 for (octave_idx_type i = 0; i < nz; i++) |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
613 { |
9829
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
614 itmp = 0; jtmp = 0; |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
615 is >> itmp; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
616 itmp--; |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
617 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
618 is >> jtmp; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
619 jtmp--; |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
620 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
621 if (itmp < 0 || itmp >= nr) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
622 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
623 (*current_liboctave_error_handler) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
624 ("invalid sparse matrix: row index = %d out of range", |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
625 itmp + 1); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
626 is.setstate (std::ios::failbit); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
627 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
628 } |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
629 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
630 if (jtmp < 0 || jtmp >= nc) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
631 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
632 (*current_liboctave_error_handler) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
633 ("invalid sparse matrix: column index = %d out of range", |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
634 jtmp + 1); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
635 is.setstate (std::ios::failbit); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
636 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
637 } |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
638 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
639 if (jtmp < jold) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
640 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
641 (*current_liboctave_error_handler) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
642 ("invalid sparse matrix: column indices must appear in ascending order"); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
643 is.setstate (std::ios::failbit); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
644 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
645 } |
9829
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
646 else if (jtmp > jold) |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
647 { |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
648 for (octave_idx_type j = jold; j < jtmp; j++) |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
649 a.cidx(j+1) = ii; |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
650 } |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
651 else if (itmp < iold) |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
652 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
653 (*current_liboctave_error_handler) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
654 ("invalid sparse matrix: row indices must appear in ascending order in each column"); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
655 is.setstate (std::ios::failbit); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
656 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
657 } |
9829
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
658 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
659 iold = itmp; |
9829
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
660 jold = jtmp; |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
661 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
662 tmp = read_fcn (is); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
663 |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
664 if (is) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
665 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
666 a.data (ii) = tmp; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
667 a.ridx (ii++) = itmp; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
668 } |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
669 else |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
670 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
671 } |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
672 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
673 for (octave_idx_type j = jold; j < nc; j++) |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
674 a.cidx(j+1) = ii; |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
675 } |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
676 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
677 done: |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
678 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
679 return is; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
680 } |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
681 |
5164 | 682 #endif |