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