Mercurial > hg > octave-lyh
annotate liboctave/Sparse.h @ 10514:40c58502a78b
improve conversion & copy ctors of sparse matrix
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 13 Apr 2010 12:45:24 +0200 |
parents | aac9f4265048 |
children | 189274f6c7c4 |
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 |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
107 template <class U> |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
108 SparseRep (const Sparse<U>::SparseRep& a) |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
109 : d (new T [a.nzmx]), r (new octave_idx_type [a.nzmx]), c (new octave_idx_type [a.ncols + 1]), |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
110 nzmx (a.nzmx), nrows (a.nrows), ncols (a.ncols), count (1) |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
111 { |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
112 octave_idx_type nz = a.nnz (); |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
113 std::copy (a.d, a.d + nz, d); |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
114 copy_or_memcpy (nz, a.r, r); |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
115 copy_or_memcpy (ncols + 1, a.c, c); |
5164 | 116 } |
117 | |
118 ~SparseRep (void) { delete [] d; delete [] r; delete [] c; } | |
119 | |
5604 | 120 octave_idx_type length (void) const { return nzmx; } |
5164 | 121 |
5604 | 122 octave_idx_type nnz (void) const { return c [ncols]; } |
5164 | 123 |
5275 | 124 T& elem (octave_idx_type _r, octave_idx_type _c); |
5164 | 125 |
5275 | 126 T celem (octave_idx_type _r, octave_idx_type _c) const; |
5164 | 127 |
5275 | 128 T& data (octave_idx_type i) { return d[i]; } |
5164 | 129 |
5275 | 130 T cdata (octave_idx_type i) const { return d[i]; } |
5164 | 131 |
5275 | 132 octave_idx_type& ridx (octave_idx_type i) { return r[i]; } |
5164 | 133 |
5275 | 134 octave_idx_type cridx (octave_idx_type i) const { return r[i]; } |
5164 | 135 |
5275 | 136 octave_idx_type& cidx (octave_idx_type i) { return c[i]; } |
5164 | 137 |
5275 | 138 octave_idx_type ccidx (octave_idx_type i) const { return c[i]; } |
5164 | 139 |
140 void maybe_compress (bool remove_zeros); | |
141 | |
5275 | 142 void change_length (octave_idx_type nz); |
5164 | 143 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
144 bool indices_ok (void) const; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
145 |
5164 | 146 private: |
147 | |
148 // No assignment! | |
149 | |
150 SparseRep& operator = (const SparseRep& a); | |
151 }; | |
152 | |
153 //-------------------------------------------------------------------- | |
154 | |
155 void make_unique (void) | |
156 { | |
157 if (rep->count > 1) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
158 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
159 --rep->count; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
160 rep = new SparseRep (*rep); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
161 } |
5164 | 162 } |
163 | |
164 public: | |
165 | |
166 // !!! WARNING !!! -- these should be protected, not public. You | |
167 // should not access these data members directly! | |
168 | |
169 typename Sparse<T>::SparseRep *rep; | |
170 | |
171 dim_vector dimensions; | |
172 | |
173 private: | |
174 | |
175 typename Sparse<T>::SparseRep *nil_rep (void) const | |
176 { | |
177 static typename Sparse<T>::SparseRep *nr | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
178 = new typename Sparse<T>::SparseRep (); |
5164 | 179 |
180 nr->count++; | |
181 | |
182 return nr; | |
183 } | |
184 | |
185 public: | |
186 | |
187 Sparse (void) | |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
188 : rep (nil_rep ()), dimensions (dim_vector(0,0)) { } |
5164 | 189 |
5275 | 190 explicit Sparse (octave_idx_type n) |
5164 | 191 : rep (new typename Sparse<T>::SparseRep (n)), |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
192 dimensions (dim_vector (n, n)) { } |
5164 | 193 |
5275 | 194 explicit Sparse (octave_idx_type nr, octave_idx_type nc) |
5164 | 195 : rep (new typename Sparse<T>::SparseRep (nr, nc)), |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
196 dimensions (dim_vector (nr, nc)) { } |
5164 | 197 |
5275 | 198 explicit Sparse (octave_idx_type nr, octave_idx_type nc, T val); |
5164 | 199 |
5275 | 200 Sparse (const dim_vector& dv, octave_idx_type nz) |
5164 | 201 : 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
|
202 dimensions (dv) { } |
5164 | 203 |
5275 | 204 Sparse (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz) |
5164 | 205 : rep (new typename Sparse<T>::SparseRep (nr, nc, nz)), |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
206 dimensions (dim_vector (nr, nc)) { } |
5164 | 207 |
10514
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
208 // Type conversion case. Preserves capacity (). |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
209 template <class U> Sparse (const Sparse<U>& a) |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
210 : rep (new typename Sparse<T>::SparseRep (a.rep)), |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
211 dimensions (a.dimensions) { } |
5164 | 212 |
213 // No type conversion case. | |
214 Sparse (const Sparse<T>& a) | |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
215 : rep (a.rep), dimensions (a.dimensions) |
5164 | 216 { |
217 rep->count++; | |
218 } | |
219 | |
220 public: | |
221 | |
222 Sparse (const dim_vector& dv); | |
223 | |
224 Sparse (const Sparse<T>& a, const dim_vector& dv); | |
225 | |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
226 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
|
227 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
|
228 |
5164 | 229 // Sparsify a normal matrix |
230 Sparse (const Array<T>& a); | |
231 | |
232 virtual ~Sparse (void); | |
233 | |
7717
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
234 Sparse<T>& operator = (const Sparse<T>& a); |
5164 | 235 |
5604 | 236 // Note that nzmax and capacity are the amount of storage for |
237 // non-zero elements, while nnz is the actual number of non-zero | |
238 // terms. | |
239 octave_idx_type nzmax (void) const { return rep->length (); } | |
240 octave_idx_type capacity (void) const { return nzmax (); } | |
241 octave_idx_type nnz (void) const { return rep->nnz (); } | |
5164 | 242 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
243 // 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
|
244 // so don't do it unless you really need it. |
5275 | 245 octave_idx_type numel (void) const |
5164 | 246 { |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
247 return dimensions.safe_numel (); |
5164 | 248 } |
249 | |
5275 | 250 octave_idx_type nelem (void) const { return capacity (); } |
251 octave_idx_type length (void) const { return numel (); } | |
5164 | 252 |
5275 | 253 octave_idx_type dim1 (void) const { return dimensions(0); } |
254 octave_idx_type dim2 (void) const { return dimensions(1); } | |
5164 | 255 |
5275 | 256 octave_idx_type rows (void) const { return dim1 (); } |
257 octave_idx_type cols (void) const { return dim2 (); } | |
258 octave_idx_type columns (void) const { return dim2 (); } | |
5164 | 259 |
5275 | 260 octave_idx_type get_row_index (octave_idx_type k) { return ridx (k); } |
261 octave_idx_type get_col_index (octave_idx_type k) | |
5164 | 262 { |
5275 | 263 octave_idx_type ret = 0; |
5164 | 264 while (cidx(ret+1) < k) |
265 ret++; | |
266 return ret; | |
267 } | |
10358
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
268 |
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
269 size_t byte_size (void) const |
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
270 { |
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
271 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
|
272 + 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
|
273 } |
5164 | 274 |
275 dim_vector dims (void) const { return dimensions; } | |
276 | |
277 Sparse<T> squeeze (void) const { return *this; } | |
278 | |
5275 | 279 octave_idx_type compute_index (const Array<octave_idx_type>& ra_idx) const; |
5164 | 280 |
5275 | 281 T range_error (const char *fcn, octave_idx_type n) const; |
282 T& range_error (const char *fcn, octave_idx_type n); | |
5164 | 283 |
5275 | 284 T range_error (const char *fcn, octave_idx_type i, octave_idx_type j) const; |
285 T& range_error (const char *fcn, octave_idx_type i, octave_idx_type j); | |
5164 | 286 |
5275 | 287 T range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const; |
288 T& range_error (const char *fcn, const Array<octave_idx_type>& ra_idx); | |
5164 | 289 |
290 // No checking, even for multiple references, ever. | |
291 | |
5275 | 292 T& xelem (octave_idx_type n) |
5164 | 293 { |
5275 | 294 octave_idx_type i = n % rows (), j = n / rows(); |
5164 | 295 return xelem (i, j); |
296 } | |
297 | |
5275 | 298 T xelem (octave_idx_type n) const |
5164 | 299 { |
5275 | 300 octave_idx_type i = n % rows (), j = n / rows(); |
5164 | 301 return xelem (i, j); |
302 } | |
303 | |
5275 | 304 T& xelem (octave_idx_type i, octave_idx_type j) { return rep->elem (i, j); } |
305 T xelem (octave_idx_type i, octave_idx_type j) const { return rep->celem (i, j); } | |
5164 | 306 |
5275 | 307 T& xelem (const Array<octave_idx_type>& ra_idx) |
5164 | 308 { return xelem (compute_index (ra_idx)); } |
309 | |
5275 | 310 T xelem (const Array<octave_idx_type>& ra_idx) const |
5164 | 311 { return xelem (compute_index (ra_idx)); } |
312 | |
5775 | 313 // FIXME -- would be nice to fix this so that we don't |
5164 | 314 // unnecessarily force a copy, but that is not so easy, and I see no |
315 // clean way to do it. | |
316 | |
5275 | 317 T& checkelem (octave_idx_type n) |
5164 | 318 { |
319 if (n < 0 || n >= numel ()) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
320 return range_error ("T& Sparse<T>::checkelem", n); |
5164 | 321 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
322 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
323 make_unique (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
324 return xelem (n); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
325 } |
5164 | 326 } |
327 | |
5275 | 328 T& checkelem (octave_idx_type i, octave_idx_type j) |
5164 | 329 { |
330 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
|
331 return range_error ("T& Sparse<T>::checkelem", i, j); |
5164 | 332 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
333 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
334 make_unique (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
335 return xelem (i, j); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
336 } |
5164 | 337 } |
338 | |
5275 | 339 T& checkelem (const Array<octave_idx_type>& ra_idx) |
5164 | 340 { |
5275 | 341 octave_idx_type i = compute_index (ra_idx); |
5164 | 342 |
343 if (i < 0) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
344 return range_error ("T& Sparse<T>::checkelem", ra_idx); |
5164 | 345 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
346 return elem (i); |
5164 | 347 } |
348 | |
5275 | 349 T& elem (octave_idx_type n) |
5164 | 350 { |
351 make_unique (); | |
352 return xelem (n); | |
353 } | |
354 | |
5275 | 355 T& elem (octave_idx_type i, octave_idx_type j) |
5164 | 356 { |
357 make_unique (); | |
358 return xelem (i, j); | |
359 } | |
360 | |
5275 | 361 T& elem (const Array<octave_idx_type>& ra_idx) |
5164 | 362 { return Sparse<T>::elem (compute_index (ra_idx)); } |
363 | |
364 #if defined (BOUNDS_CHECKING) | |
5275 | 365 T& operator () (octave_idx_type n) { return checkelem (n); } |
366 T& operator () (octave_idx_type i, octave_idx_type j) { return checkelem (i, j); } | |
367 T& operator () (const Array<octave_idx_type>& ra_idx) { return checkelem (ra_idx); } | |
5164 | 368 #else |
5275 | 369 T& operator () (octave_idx_type n) { return elem (n); } |
370 T& operator () (octave_idx_type i, octave_idx_type j) { return elem (i, j); } | |
371 T& operator () (const Array<octave_idx_type>& ra_idx) { return elem (ra_idx); } | |
5164 | 372 #endif |
373 | |
5275 | 374 T checkelem (octave_idx_type n) const |
5164 | 375 { |
376 if (n < 0 || n >= numel ()) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
377 return range_error ("T Sparse<T>::checkelem", n); |
5164 | 378 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
379 return xelem (n); |
5164 | 380 } |
381 | |
5275 | 382 T checkelem (octave_idx_type i, octave_idx_type j) const |
5164 | 383 { |
384 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
|
385 return range_error ("T Sparse<T>::checkelem", i, j); |
5164 | 386 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
387 return xelem (i, j); |
5164 | 388 } |
389 | |
5275 | 390 T checkelem (const Array<octave_idx_type>& ra_idx) const |
5164 | 391 { |
5275 | 392 octave_idx_type i = compute_index (ra_idx); |
5164 | 393 |
394 if (i < 0) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
395 return range_error ("T Sparse<T>::checkelem", ra_idx); |
5164 | 396 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
397 return Sparse<T>::elem (i); |
5164 | 398 } |
399 | |
5275 | 400 T elem (octave_idx_type n) const { return xelem (n); } |
5164 | 401 |
5275 | 402 T elem (octave_idx_type i, octave_idx_type j) const { return xelem (i, j); } |
5164 | 403 |
5275 | 404 T elem (const Array<octave_idx_type>& ra_idx) const |
5164 | 405 { return Sparse<T>::elem (compute_index (ra_idx)); } |
406 | |
407 #if defined (BOUNDS_CHECKING) | |
5275 | 408 T operator () (octave_idx_type n) const { return checkelem (n); } |
409 T operator () (octave_idx_type i, octave_idx_type j) const { return checkelem (i, j); } | |
410 T operator () (const Array<octave_idx_type>& ra_idx) const { return checkelem (ra_idx); } | |
5164 | 411 #else |
5275 | 412 T operator () (octave_idx_type n) const { return elem (n); } |
413 T operator () (octave_idx_type i, octave_idx_type j) const { return elem (i, j); } | |
414 T operator () (const Array<octave_idx_type>& ra_idx) const { return elem (ra_idx); } | |
5164 | 415 #endif |
416 | |
417 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
|
418 { |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
419 if (remove_zeros) |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
420 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
|
421 |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
422 rep->maybe_compress (remove_zeros); |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
423 return (*this); |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
424 } |
5164 | 425 |
426 Sparse<T> reshape (const dim_vector& new_dims) const; | |
427 | |
5275 | 428 Sparse<T> permute (const Array<octave_idx_type>& vec, bool inv = false) const; |
5164 | 429 |
5275 | 430 Sparse<T> ipermute (const Array<octave_idx_type>& vec) const |
5164 | 431 { return permute (vec, true); } |
432 | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
433 void resize1 (octave_idx_type n); |
5164 | 434 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
435 void resize (octave_idx_type r, octave_idx_type c); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
436 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
437 void resize (const dim_vector& dv); |
5164 | 438 |
10497
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
439 void change_capacity (octave_idx_type nz) |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
440 { |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
441 if (nz < nnz ()) |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
442 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
|
443 rep->change_length (nz); |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
444 } |
5164 | 445 |
5275 | 446 Sparse<T>& insert (const Sparse<T>& a, octave_idx_type r, octave_idx_type c); |
447 Sparse<T>& insert (const Sparse<T>& a, const Array<octave_idx_type>& idx); | |
5164 | 448 |
449 bool is_square (void) const { return (dim1 () == dim2 ()); } | |
450 | |
451 bool is_empty (void) const { return (rows () < 1 && cols () < 1); } | |
452 | |
453 Sparse<T> transpose (void) const; | |
454 | |
455 T* data (void) { make_unique (); return rep->d; } | |
5275 | 456 T& data (octave_idx_type i) { make_unique (); return rep->data (i); } |
5164 | 457 T* xdata (void) { return rep->d; } |
5275 | 458 T& xdata (octave_idx_type i) { return rep->data (i); } |
5164 | 459 |
5275 | 460 T data (octave_idx_type i) const { return rep->data (i); } |
5900 | 461 // FIXME -- shouldn't this be returning const T*? |
5164 | 462 T* data (void) const { return rep->d; } |
463 | |
5275 | 464 octave_idx_type* ridx (void) { make_unique (); return rep->r; } |
465 octave_idx_type& ridx (octave_idx_type i) { make_unique (); return rep->ridx (i); } | |
466 octave_idx_type* xridx (void) { return rep->r; } | |
467 octave_idx_type& xridx (octave_idx_type i) { return rep->ridx (i); } | |
5164 | 468 |
5275 | 469 octave_idx_type ridx (octave_idx_type i) const { return rep->cridx (i); } |
5900 | 470 // FIXME -- shouldn't this be returning const octave_idx_type*? |
5275 | 471 octave_idx_type* ridx (void) const { return rep->r; } |
5164 | 472 |
5275 | 473 octave_idx_type* cidx (void) { make_unique (); return rep->c; } |
474 octave_idx_type& cidx (octave_idx_type i) { make_unique (); return rep->cidx (i); } | |
475 octave_idx_type* xcidx (void) { return rep->c; } | |
476 octave_idx_type& xcidx (octave_idx_type i) { return rep->cidx (i); } | |
5164 | 477 |
5275 | 478 octave_idx_type cidx (octave_idx_type i) const { return rep->ccidx (i); } |
5900 | 479 // FIXME -- shouldn't this be returning const octave_idx_type*? |
5275 | 480 octave_idx_type* cidx (void) const { return rep->c; } |
5164 | 481 |
5275 | 482 octave_idx_type ndims (void) const { return dimensions.length (); } |
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); |
5164 | 485 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10480
diff
changeset
|
486 void delete_elements (int dim, const idx_vector& i); |
5164 | 487 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10480
diff
changeset
|
488 void delete_elements (const idx_vector& i, const idx_vector& j); |
5164 | 489 |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
490 Sparse<T> index (const idx_vector& i, bool resize_ok = false) const; |
5164 | 491 |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
492 Sparse<T> index (const idx_vector& i, const idx_vector& j, bool resize_ok = false) const; |
5164 | 493 |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
494 void assign (const idx_vector& i, const Sparse<T>& rhs); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
495 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
496 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
|
497 |
5164 | 498 void print_info (std::ostream& os, const std::string& prefix) const; |
499 | |
5900 | 500 // Unsafe. These functions exist to support the MEX interface. |
501 // You should not use them anywhere else. | |
502 void *mex_get_data (void) const { return const_cast<T *> (data ()); } | |
503 | |
504 octave_idx_type *mex_get_ir (void) const { return const_cast<octave_idx_type *> (ridx ()); } | |
505 | |
506 octave_idx_type *mex_get_jc (void) const { return const_cast<octave_idx_type *> (cidx ()); } | |
7433 | 507 |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
508 Sparse<T> sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const; |
7433 | 509 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
|
510 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
|
511 |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7602
diff
changeset
|
512 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
|
513 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
514 Array<T> array_value (void) const; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
515 |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
516 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
|
517 Sparse<U> |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
518 map (F fcn) const |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
519 { |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
520 Sparse<U> result; |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
521 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
|
522 |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
523 if (f_zero != 0.) |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
524 { |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
525 octave_idx_type nr = rows (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
526 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
|
527 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
528 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
|
529 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
530 for (octave_idx_type j = 0; j < nc; j++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
531 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
|
532 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
533 octave_quit (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
534 /* Use data instead of elem for better performance. */ |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
535 result.data (ridx (i) + j * nr) = fcn (data(i)); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
536 } |
7602
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 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
|
539 } |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
540 else |
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 octave_idx_type nz = nnz (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
543 octave_idx_type nr = rows (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
544 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
|
545 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
546 result = Sparse<U> (nr, nc, nz); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
547 octave_idx_type ii = 0; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
548 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
|
549 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
550 for (octave_idx_type j = 0; j < nc; j++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
551 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
552 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
|
553 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
554 U val = fcn (data (i)); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
555 if (val != 0.0) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
556 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
557 result.data (ii) = val; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
558 result.ridx (ii++) = ridx (i); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
559 } |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
560 octave_quit (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
561 } |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
562 result.cidx (j+1) = ii; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
563 } |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
564 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
565 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
|
566 } |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
567 |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
568 return result; |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
569 } |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
570 |
9812
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
571 // Overloads for function references. |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
572 template <class U> |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
573 Sparse<U> |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
574 map (U (&fcn) (T)) const |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
575 { return map<U, U (&) (T)> (fcn); } |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
576 |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
577 template <class U> |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
578 Sparse<U> |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
579 map (U (&fcn) (const T&)) const |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
580 { return map<U, U (&) (const T&)> (fcn); } |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
581 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
582 bool indices_ok (void) const { return rep->indices_ok (); } |
5164 | 583 }; |
584 | |
585 // NOTE: these functions should be friends of the Sparse<T> class and | |
586 // Sparse<T>::dimensions should be protected, not public, but we can't | |
587 // do that because of bugs in gcc prior to 3.3. | |
588 | |
589 template <class LT, class RT> | |
590 /* friend */ int | |
591 assign (Sparse<LT>& lhs, const Sparse<RT>& rhs); | |
592 | |
593 template <class LT, class RT> | |
594 /* friend */ int | |
595 assign1 (Sparse<LT>& lhs, const Sparse<RT>& rhs); | |
596 | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
597 template<typename T> |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
598 std::istream& |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
599 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
|
600 T (*read_fcn) (std::istream&)) |
9469
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 octave_idx_type nr = a.rows (); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
603 octave_idx_type nc = a.cols (); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
604 octave_idx_type nz = a.nzmax (); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
605 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
606 if (nr > 0 && nc > 0) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
607 { |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
608 octave_idx_type itmp; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
609 octave_idx_type jtmp; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
610 octave_idx_type iold = 0; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
611 octave_idx_type jold = 0; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
612 octave_idx_type ii = 0; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
613 T tmp; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
614 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
615 a.cidx (0) = 0; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
616 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
|
617 { |
9829
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
618 itmp = 0; jtmp = 0; |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
619 is >> itmp; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
620 itmp--; |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
621 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
622 is >> jtmp; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
623 jtmp--; |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
624 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
625 if (itmp < 0 || itmp >= nr) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
626 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
627 (*current_liboctave_error_handler) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
628 ("invalid sparse matrix: row index = %d out of range", |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
629 itmp + 1); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
630 is.setstate (std::ios::failbit); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
631 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
632 } |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
633 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
634 if (jtmp < 0 || jtmp >= nc) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
635 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
636 (*current_liboctave_error_handler) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
637 ("invalid sparse matrix: column index = %d out of range", |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
638 jtmp + 1); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
639 is.setstate (std::ios::failbit); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
640 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
641 } |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
642 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
643 if (jtmp < jold) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
644 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
645 (*current_liboctave_error_handler) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
646 ("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
|
647 is.setstate (std::ios::failbit); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
648 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
649 } |
9829
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
650 else if (jtmp > jold) |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
651 { |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
652 for (octave_idx_type j = jold; j < jtmp; j++) |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
653 a.cidx(j+1) = ii; |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
654 } |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
655 else if (itmp < iold) |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
656 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
657 (*current_liboctave_error_handler) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
658 ("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
|
659 is.setstate (std::ios::failbit); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
660 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
661 } |
9829
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
662 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
663 iold = itmp; |
9829
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
664 jold = jtmp; |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
665 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
666 tmp = read_fcn (is); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
667 |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
668 if (is) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
669 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
670 a.data (ii) = tmp; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
671 a.ridx (ii++) = itmp; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
672 } |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
673 else |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
674 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
675 } |
9469
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 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
|
678 a.cidx(j+1) = ii; |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
679 } |
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 done: |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
682 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
683 return is; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
684 } |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
685 |
5164 | 686 #endif |