5164
|
1 // Template sparse array class |
|
2 /* |
|
3 |
|
4 Copyright (C) 2004 David Bateman |
|
5 Copyright (C) 1998-2004 Andy Adler |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
5307
|
18 along with this program; see the file COPYING. If not, write to the |
|
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
20 Boston, MA 02110-1301, USA. |
5164
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cassert> |
|
29 #include <climits> |
|
30 |
|
31 #include <iostream> |
5765
|
32 #include <sstream> |
5164
|
33 #include <vector> |
|
34 |
|
35 #include "Array.h" |
|
36 #include "Array-util.h" |
|
37 #include "Range.h" |
|
38 #include "idx-vector.h" |
|
39 #include "lo-error.h" |
|
40 #include "quit.h" |
|
41 |
|
42 #include "Sparse.h" |
|
43 #include "sparse-sort.h" |
|
44 #include "oct-spparms.h" |
|
45 |
|
46 template <class T> |
|
47 T& |
5275
|
48 Sparse<T>::SparseRep::elem (octave_idx_type _r, octave_idx_type _c) |
5164
|
49 { |
5275
|
50 octave_idx_type i; |
5164
|
51 |
5604
|
52 if (nzmx > 0) |
5164
|
53 { |
|
54 for (i = c[_c]; i < c[_c + 1]; i++) |
|
55 if (r[i] == _r) |
|
56 return d[i]; |
|
57 else if (r[i] > _r) |
|
58 break; |
|
59 |
|
60 // Ok, If we've gotten here, we're in trouble.. Have to create a |
|
61 // new element in the sparse array. This' gonna be slow!!! |
5869
|
62 if (c[ncols] == nzmx) |
5164
|
63 { |
|
64 (*current_liboctave_error_handler) |
5275
|
65 ("Sparse::SparseRep::elem (octave_idx_type, octave_idx_type): sparse matrix filled"); |
5164
|
66 return *d; |
|
67 } |
|
68 |
5275
|
69 octave_idx_type to_move = c[ncols] - i; |
5164
|
70 if (to_move != 0) |
|
71 { |
5275
|
72 for (octave_idx_type j = c[ncols]; j > i; j--) |
5164
|
73 { |
|
74 d[j] = d[j-1]; |
|
75 r[j] = r[j-1]; |
|
76 } |
|
77 } |
|
78 |
5275
|
79 for (octave_idx_type j = _c + 1; j < ncols + 1; j++) |
5164
|
80 c[j] = c[j] + 1; |
|
81 |
|
82 d[i] = 0.; |
|
83 r[i] = _r; |
|
84 |
|
85 return d[i]; |
|
86 } |
|
87 else |
|
88 { |
|
89 (*current_liboctave_error_handler) |
5275
|
90 ("Sparse::SparseRep::elem (octave_idx_type, octave_idx_type): sparse matrix filled"); |
5164
|
91 return *d; |
|
92 } |
|
93 } |
|
94 |
|
95 template <class T> |
|
96 T |
5275
|
97 Sparse<T>::SparseRep::celem (octave_idx_type _r, octave_idx_type _c) const |
5164
|
98 { |
5604
|
99 if (nzmx > 0) |
5275
|
100 for (octave_idx_type i = c[_c]; i < c[_c + 1]; i++) |
5164
|
101 if (r[i] == _r) |
|
102 return d[i]; |
|
103 return T (); |
|
104 } |
|
105 |
|
106 template <class T> |
|
107 void |
|
108 Sparse<T>::SparseRep::maybe_compress (bool remove_zeros) |
|
109 { |
5604
|
110 octave_idx_type ndel = nzmx - c[ncols]; |
5275
|
111 octave_idx_type nzero = 0; |
5164
|
112 |
|
113 if (remove_zeros) |
5604
|
114 for (octave_idx_type i = 0; i < nzmx - ndel; i++) |
5164
|
115 if (d[i] == T ()) |
|
116 nzero++; |
|
117 |
|
118 if (!ndel && !nzero) |
|
119 return; |
|
120 |
|
121 if (!nzero) |
|
122 { |
5604
|
123 octave_idx_type new_nzmx = nzmx - ndel; |
|
124 |
|
125 T *new_data = new T [new_nzmx]; |
|
126 for (octave_idx_type i = 0; i < new_nzmx; i++) |
5164
|
127 new_data[i] = d[i]; |
|
128 delete [] d; |
|
129 d = new_data; |
|
130 |
5604
|
131 octave_idx_type *new_ridx = new octave_idx_type [new_nzmx]; |
|
132 for (octave_idx_type i = 0; i < new_nzmx; i++) |
5164
|
133 new_ridx[i] = r[i]; |
|
134 delete [] r; |
|
135 r = new_ridx; |
|
136 } |
|
137 else |
|
138 { |
5604
|
139 octave_idx_type new_nzmx = nzmx - ndel - nzero; |
|
140 |
|
141 T *new_data = new T [new_nzmx]; |
|
142 octave_idx_type *new_ridx = new octave_idx_type [new_nzmx]; |
5275
|
143 |
|
144 octave_idx_type ii = 0; |
|
145 octave_idx_type ic = 0; |
|
146 for (octave_idx_type j = 0; j < ncols; j++) |
5164
|
147 { |
5275
|
148 for (octave_idx_type k = ic; k < c[j+1]; k++) |
5164
|
149 if (d[k] != T ()) |
|
150 { |
|
151 new_data [ii] = d[k]; |
|
152 new_ridx [ii++] = r[k]; |
|
153 } |
|
154 ic = c[j+1]; |
|
155 c[j+1] = ii; |
|
156 } |
|
157 |
|
158 delete [] d; |
|
159 d = new_data; |
|
160 |
|
161 delete [] r; |
|
162 r = new_ridx; |
|
163 } |
|
164 |
5604
|
165 nzmx -= ndel + nzero; |
5164
|
166 } |
|
167 |
|
168 template <class T> |
|
169 void |
5275
|
170 Sparse<T>::SparseRep::change_length (octave_idx_type nz) |
5164
|
171 { |
5604
|
172 if (nz != nzmx) |
5164
|
173 { |
5604
|
174 octave_idx_type min_nzmx = (nz < nzmx ? nz : nzmx); |
5275
|
175 |
|
176 octave_idx_type * new_ridx = new octave_idx_type [nz]; |
5604
|
177 for (octave_idx_type i = 0; i < min_nzmx; i++) |
5164
|
178 new_ridx[i] = r[i]; |
|
179 |
|
180 delete [] r; |
|
181 r = new_ridx; |
|
182 |
|
183 T * new_data = new T [nz]; |
5604
|
184 for (octave_idx_type i = 0; i < min_nzmx; i++) |
5164
|
185 new_data[i] = d[i]; |
|
186 |
|
187 delete [] d; |
|
188 d = new_data; |
|
189 |
5604
|
190 if (nz < nzmx) |
5275
|
191 for (octave_idx_type i = 0; i <= ncols; i++) |
5164
|
192 if (c[i] > nz) |
|
193 c[i] = nz; |
|
194 |
5604
|
195 nzmx = nz; |
5164
|
196 } |
|
197 } |
|
198 |
|
199 template <class T> |
|
200 template <class U> |
|
201 Sparse<T>::Sparse (const Sparse<U>& a) |
|
202 : dimensions (a.dimensions), idx (0), idx_count (0) |
|
203 { |
5681
|
204 if (a.nnz () == 0) |
5164
|
205 rep = new typename Sparse<T>::SparseRep (rows (), cols()); |
|
206 else |
|
207 { |
5681
|
208 rep = new typename Sparse<T>::SparseRep (rows (), cols (), a.nnz ()); |
5164
|
209 |
5681
|
210 octave_idx_type nz = a.nnz (); |
5275
|
211 octave_idx_type nc = cols (); |
|
212 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
213 { |
|
214 xdata (i) = T (a.data (i)); |
|
215 xridx (i) = a.ridx (i); |
|
216 } |
5275
|
217 for (octave_idx_type i = 0; i < nc + 1; i++) |
5164
|
218 xcidx (i) = a.cidx (i); |
|
219 } |
|
220 } |
|
221 |
|
222 template <class T> |
5275
|
223 Sparse<T>::Sparse (octave_idx_type nr, octave_idx_type nc, T val) |
5630
|
224 : dimensions (dim_vector (nr, nc)), idx (0), idx_count (0) |
5164
|
225 { |
5630
|
226 if (val != T ()) |
5164
|
227 { |
5630
|
228 rep = new typename Sparse<T>::SparseRep (nr, nc, nr*nc); |
|
229 |
|
230 octave_idx_type ii = 0; |
|
231 xcidx (0) = 0; |
|
232 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
233 { |
5630
|
234 for (octave_idx_type i = 0; i < nr; i++) |
|
235 { |
|
236 xdata (ii) = val; |
|
237 xridx (ii++) = i; |
|
238 } |
|
239 xcidx (j+1) = ii; |
|
240 } |
|
241 } |
|
242 else |
|
243 { |
|
244 rep = new typename Sparse<T>::SparseRep (nr, nc, 0); |
|
245 for (octave_idx_type j = 0; j < nc+1; j++) |
|
246 xcidx(j) = 0; |
5164
|
247 } |
|
248 } |
|
249 |
|
250 template <class T> |
|
251 Sparse<T>::Sparse (const dim_vector& dv) |
|
252 : dimensions (dv), idx (0), idx_count (0) |
|
253 { |
|
254 if (dv.length() != 2) |
|
255 (*current_liboctave_error_handler) |
|
256 ("Sparse::Sparse (const dim_vector&): dimension mismatch"); |
|
257 else |
|
258 rep = new typename Sparse<T>::SparseRep (dv(0), dv(1)); |
|
259 } |
|
260 |
|
261 template <class T> |
|
262 Sparse<T>::Sparse (const Sparse<T>& a, const dim_vector& dv) |
|
263 : dimensions (dv), idx (0), idx_count (0) |
|
264 { |
|
265 |
|
266 // Work in unsigned long long to avoid overflow issues with numel |
|
267 unsigned long long a_nel = static_cast<unsigned long long>(a.rows ()) * |
|
268 static_cast<unsigned long long>(a.cols ()); |
|
269 unsigned long long dv_nel = static_cast<unsigned long long>(dv (0)) * |
|
270 static_cast<unsigned long long>(dv (1)); |
|
271 |
|
272 if (a_nel != dv_nel) |
|
273 (*current_liboctave_error_handler) |
|
274 ("Sparse::Sparse (const Sparse&, const dim_vector&): dimension mismatch"); |
|
275 else |
|
276 { |
|
277 dim_vector old_dims = a.dims(); |
5681
|
278 octave_idx_type new_nzmx = a.nnz (); |
5275
|
279 octave_idx_type new_nr = dv (0); |
|
280 octave_idx_type new_nc = dv (1); |
|
281 octave_idx_type old_nr = old_dims (0); |
|
282 octave_idx_type old_nc = old_dims (1); |
5164
|
283 |
5604
|
284 rep = new typename Sparse<T>::SparseRep (new_nr, new_nc, new_nzmx); |
5164
|
285 |
5275
|
286 octave_idx_type kk = 0; |
5164
|
287 xcidx(0) = 0; |
5275
|
288 for (octave_idx_type i = 0; i < old_nc; i++) |
|
289 for (octave_idx_type j = a.cidx(i); j < a.cidx(i+1); j++) |
5164
|
290 { |
5275
|
291 octave_idx_type tmp = i * old_nr + a.ridx(j); |
|
292 octave_idx_type ii = tmp % new_nr; |
|
293 octave_idx_type jj = (tmp - ii) / new_nr; |
|
294 for (octave_idx_type k = kk; k < jj; k++) |
5164
|
295 xcidx(k+1) = j; |
|
296 kk = jj; |
|
297 xdata(j) = a.data(j); |
|
298 xridx(j) = ii; |
|
299 } |
5275
|
300 for (octave_idx_type k = kk; k < new_nc; k++) |
5604
|
301 xcidx(k+1) = new_nzmx; |
5164
|
302 } |
|
303 } |
|
304 |
|
305 template <class T> |
5275
|
306 Sparse<T>::Sparse (const Array<T>& a, const Array<octave_idx_type>& r, |
|
307 const Array<octave_idx_type>& c, octave_idx_type nr, |
|
308 octave_idx_type nc, bool sum_terms) |
5164
|
309 : dimensions (dim_vector (nr, nc)), idx (0), idx_count (0) |
|
310 { |
5275
|
311 octave_idx_type a_len = a.length (); |
|
312 octave_idx_type r_len = r.length (); |
|
313 octave_idx_type c_len = c.length (); |
5164
|
314 bool ri_scalar = (r_len == 1); |
|
315 bool ci_scalar = (c_len == 1); |
|
316 bool cf_scalar = (a_len == 1); |
|
317 |
|
318 if ((a_len != r_len && !cf_scalar && !ri_scalar) || |
|
319 (a_len != c_len && !cf_scalar && !ci_scalar) || |
|
320 (r_len != c_len && !ri_scalar && !ci_scalar) || nr < 0 || nc < 0) |
|
321 { |
|
322 (*current_liboctave_error_handler) |
5275
|
323 ("Sparse::Sparse (const Array<T>&, const Array<octave_idx_type>&, ...): dimension mismatch"); |
5164
|
324 rep = nil_rep (); |
|
325 dimensions = dim_vector (0, 0); |
|
326 } |
|
327 else |
|
328 { |
5604
|
329 octave_idx_type max_nzmx = (r_len > c_len ? r_len : c_len); |
|
330 |
|
331 OCTAVE_LOCAL_BUFFER (octave_sparse_sort_idxl *, sidx, max_nzmx); |
|
332 OCTAVE_LOCAL_BUFFER (octave_sparse_sort_idxl, sidxX, max_nzmx); |
|
333 |
|
334 for (octave_idx_type i = 0; i < max_nzmx; i++) |
5164
|
335 sidx[i] = &sidxX[i]; |
|
336 |
5604
|
337 octave_idx_type actual_nzmx = 0; |
5164
|
338 OCTAVE_QUIT; |
5604
|
339 for (octave_idx_type i = 0; i < max_nzmx; i++) |
5164
|
340 { |
5275
|
341 octave_idx_type rowidx = (ri_scalar ? r(0) : r(i)); |
|
342 octave_idx_type colidx = (ci_scalar ? c(0) : c(i)); |
5164
|
343 if (rowidx < nr && rowidx >= 0 && |
|
344 colidx < nc && colidx >= 0 ) |
|
345 { |
|
346 if ( a (cf_scalar ? 0 : i ) != T ()) |
|
347 { |
5604
|
348 sidx[actual_nzmx]->r = rowidx; |
|
349 sidx[actual_nzmx]->c = colidx; |
|
350 sidx[actual_nzmx]->idx = i; |
|
351 actual_nzmx++; |
5164
|
352 } |
|
353 } |
|
354 else |
|
355 { |
|
356 (*current_liboctave_error_handler) |
|
357 ("Sparse::Sparse : index (%d,%d) out of range", |
|
358 rowidx + 1, colidx + 1); |
|
359 rep = nil_rep (); |
|
360 dimensions = dim_vector (0, 0); |
|
361 return; |
|
362 } |
|
363 } |
|
364 |
5604
|
365 if (actual_nzmx == 0) |
5164
|
366 rep = new typename Sparse<T>::SparseRep (nr, nc); |
|
367 else |
|
368 { |
|
369 OCTAVE_QUIT; |
|
370 octave_sort<octave_sparse_sort_idxl *> |
|
371 sort (octave_sparse_sidxl_comp); |
|
372 |
5604
|
373 sort.sort (sidx, actual_nzmx); |
5164
|
374 OCTAVE_QUIT; |
|
375 |
|
376 // Now count the unique non-zero values |
5604
|
377 octave_idx_type real_nzmx = 1; |
|
378 for (octave_idx_type i = 1; i < actual_nzmx; i++) |
5164
|
379 if (sidx[i-1]->r != sidx[i]->r || sidx[i-1]->c != sidx[i]->c) |
5604
|
380 real_nzmx++; |
|
381 |
|
382 rep = new typename Sparse<T>::SparseRep (nr, nc, real_nzmx); |
5164
|
383 |
5275
|
384 octave_idx_type cx = 0; |
|
385 octave_idx_type prev_rval = -1; |
|
386 octave_idx_type prev_cval = -1; |
|
387 octave_idx_type ii = -1; |
5164
|
388 xcidx (0) = 0; |
5604
|
389 for (octave_idx_type i = 0; i < actual_nzmx; i++) |
5164
|
390 { |
|
391 OCTAVE_QUIT; |
5275
|
392 octave_idx_type iidx = sidx[i]->idx; |
|
393 octave_idx_type rval = sidx[i]->r; |
|
394 octave_idx_type cval = sidx[i]->c; |
5164
|
395 |
|
396 if (prev_cval < cval || (prev_rval < rval && prev_cval == cval)) |
|
397 { |
5275
|
398 octave_idx_type ci = static_cast<octave_idx_type> (c (ci_scalar ? 0 : iidx)); |
5164
|
399 ii++; |
|
400 while (cx < ci) |
|
401 xcidx (++cx) = ii; |
|
402 xdata(ii) = a (cf_scalar ? 0 : iidx); |
5275
|
403 xridx(ii) = static_cast<octave_idx_type> (r (ri_scalar ? 0 : iidx)); |
5164
|
404 } |
|
405 else |
|
406 { |
|
407 if (sum_terms) |
|
408 xdata(ii) += a (cf_scalar ? 0 : iidx); |
|
409 else |
|
410 xdata(ii) = a (cf_scalar ? 0 : iidx); |
|
411 } |
|
412 prev_rval = rval; |
|
413 prev_cval = cval; |
|
414 } |
|
415 |
|
416 while (cx < nc) |
|
417 xcidx (++cx) = ii + 1; |
|
418 } |
|
419 } |
|
420 } |
|
421 |
|
422 template <class T> |
|
423 Sparse<T>::Sparse (const Array<T>& a, const Array<double>& r, |
5275
|
424 const Array<double>& c, octave_idx_type nr, |
|
425 octave_idx_type nc, bool sum_terms) |
5164
|
426 : dimensions (dim_vector (nr, nc)), idx (0), idx_count (0) |
|
427 { |
5275
|
428 octave_idx_type a_len = a.length (); |
|
429 octave_idx_type r_len = r.length (); |
|
430 octave_idx_type c_len = c.length (); |
5164
|
431 bool ri_scalar = (r_len == 1); |
|
432 bool ci_scalar = (c_len == 1); |
|
433 bool cf_scalar = (a_len == 1); |
|
434 |
|
435 if ((a_len != r_len && !cf_scalar && !ri_scalar) || |
|
436 (a_len != c_len && !cf_scalar && !ci_scalar) || |
|
437 (r_len != c_len && !ri_scalar && !ci_scalar) || nr < 0 || nc < 0) |
|
438 { |
|
439 (*current_liboctave_error_handler) |
|
440 ("Sparse::Sparse (const Array<T>&, const Array<double>&, ...): dimension mismatch"); |
|
441 rep = nil_rep (); |
|
442 dimensions = dim_vector (0, 0); |
|
443 } |
|
444 else |
|
445 { |
5604
|
446 octave_idx_type max_nzmx = (r_len > c_len ? r_len : c_len); |
5164
|
447 |
5604
|
448 OCTAVE_LOCAL_BUFFER (octave_sparse_sort_idxl *, sidx, max_nzmx); |
|
449 OCTAVE_LOCAL_BUFFER (octave_sparse_sort_idxl, sidxX, max_nzmx); |
|
450 |
|
451 for (octave_idx_type i = 0; i < max_nzmx; i++) |
5164
|
452 sidx[i] = &sidxX[i]; |
|
453 |
5604
|
454 octave_idx_type actual_nzmx = 0; |
5164
|
455 OCTAVE_QUIT; |
|
456 |
5604
|
457 for (octave_idx_type i = 0; i < max_nzmx; i++) |
5164
|
458 { |
5275
|
459 octave_idx_type rowidx = static_cast<octave_idx_type> (ri_scalar ? r(0) : r(i)); |
|
460 octave_idx_type colidx = static_cast<octave_idx_type> (ci_scalar ? c(0) : c(i)); |
5164
|
461 if (rowidx < nr && rowidx >= 0 && |
|
462 colidx < nc && colidx >= 0 ) |
|
463 { |
|
464 if ( a (cf_scalar ? 0 : i ) != T ()) |
|
465 { |
5604
|
466 sidx[actual_nzmx]->r = rowidx; |
|
467 sidx[actual_nzmx]->c = colidx; |
|
468 sidx[actual_nzmx]->idx = i; |
|
469 actual_nzmx++; |
5164
|
470 } |
|
471 } |
|
472 else |
|
473 { |
|
474 (*current_liboctave_error_handler) |
|
475 ("Sparse::Sparse : index (%d,%d) out of range", |
|
476 rowidx + 1, colidx + 1); |
|
477 rep = nil_rep (); |
|
478 dimensions = dim_vector (0, 0); |
|
479 return; |
|
480 } |
|
481 } |
|
482 |
5604
|
483 if (actual_nzmx == 0) |
5164
|
484 rep = new typename Sparse<T>::SparseRep (nr, nc); |
|
485 else |
|
486 { |
|
487 OCTAVE_QUIT; |
|
488 octave_sort<octave_sparse_sort_idxl *> |
|
489 sort (octave_sparse_sidxl_comp); |
|
490 |
5604
|
491 sort.sort (sidx, actual_nzmx); |
5164
|
492 OCTAVE_QUIT; |
|
493 |
|
494 // Now count the unique non-zero values |
5604
|
495 octave_idx_type real_nzmx = 1; |
|
496 for (octave_idx_type i = 1; i < actual_nzmx; i++) |
5164
|
497 if (sidx[i-1]->r != sidx[i]->r || sidx[i-1]->c != sidx[i]->c) |
5604
|
498 real_nzmx++; |
|
499 |
|
500 rep = new typename Sparse<T>::SparseRep (nr, nc, real_nzmx); |
5164
|
501 |
5275
|
502 octave_idx_type cx = 0; |
|
503 octave_idx_type prev_rval = -1; |
|
504 octave_idx_type prev_cval = -1; |
|
505 octave_idx_type ii = -1; |
5164
|
506 xcidx (0) = 0; |
5604
|
507 for (octave_idx_type i = 0; i < actual_nzmx; i++) |
5164
|
508 { |
|
509 OCTAVE_QUIT; |
5275
|
510 octave_idx_type iidx = sidx[i]->idx; |
|
511 octave_idx_type rval = sidx[i]->r; |
|
512 octave_idx_type cval = sidx[i]->c; |
5164
|
513 |
|
514 if (prev_cval < cval || (prev_rval < rval && prev_cval == cval)) |
|
515 { |
5275
|
516 octave_idx_type ci = static_cast<octave_idx_type> (c (ci_scalar ? 0 : iidx)); |
5164
|
517 ii++; |
|
518 |
|
519 while (cx < ci) |
|
520 xcidx (++cx) = ii; |
|
521 xdata(ii) = a (cf_scalar ? 0 : iidx); |
5275
|
522 xridx(ii) = static_cast<octave_idx_type> (r (ri_scalar ? 0 : iidx)); |
5164
|
523 } |
|
524 else |
|
525 { |
|
526 if (sum_terms) |
|
527 xdata(ii) += a (cf_scalar ? 0 : iidx); |
|
528 else |
|
529 xdata(ii) = a (cf_scalar ? 0 : iidx); |
|
530 } |
|
531 prev_rval = rval; |
|
532 prev_cval = cval; |
|
533 } |
|
534 |
|
535 while (cx < nc) |
|
536 xcidx (++cx) = ii + 1; |
|
537 } |
|
538 } |
|
539 } |
|
540 |
|
541 template <class T> |
|
542 Sparse<T>::Sparse (const Array2<T>& a) |
|
543 : dimensions (a.dims ()), idx (0), idx_count (0) |
|
544 { |
5275
|
545 octave_idx_type nr = rows (); |
|
546 octave_idx_type nc = cols (); |
|
547 octave_idx_type len = a.length (); |
5604
|
548 octave_idx_type new_nzmx = 0; |
5164
|
549 |
|
550 // First count the number of non-zero terms |
5275
|
551 for (octave_idx_type i = 0; i < len; i++) |
5164
|
552 if (a(i) != T ()) |
5604
|
553 new_nzmx++; |
|
554 |
|
555 rep = new typename Sparse<T>::SparseRep (nr, nc, new_nzmx); |
5164
|
556 |
5275
|
557 octave_idx_type ii = 0; |
5164
|
558 xcidx(0) = 0; |
5275
|
559 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
560 { |
5275
|
561 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
562 if (a.elem (i,j) != T ()) |
|
563 { |
|
564 xdata(ii) = a.elem (i,j); |
|
565 xridx(ii++) = i; |
|
566 } |
|
567 xcidx(j+1) = ii; |
|
568 } |
|
569 } |
|
570 |
|
571 template <class T> |
|
572 Sparse<T>::Sparse (const Array<T>& a) |
|
573 : dimensions (a.dims ()), idx (0), idx_count (0) |
|
574 { |
|
575 if (dimensions.length () > 2) |
|
576 (*current_liboctave_error_handler) |
|
577 ("Sparse::Sparse (const Array<T>&): dimension mismatch"); |
|
578 else |
|
579 { |
5275
|
580 octave_idx_type nr = rows (); |
|
581 octave_idx_type nc = cols (); |
|
582 octave_idx_type len = a.length (); |
5604
|
583 octave_idx_type new_nzmx = 0; |
5164
|
584 |
|
585 // First count the number of non-zero terms |
5275
|
586 for (octave_idx_type i = 0; i < len; i++) |
5164
|
587 if (a(i) != T ()) |
5604
|
588 new_nzmx++; |
|
589 |
|
590 rep = new typename Sparse<T>::SparseRep (nr, nc, new_nzmx); |
5164
|
591 |
5275
|
592 octave_idx_type ii = 0; |
5164
|
593 xcidx(0) = 0; |
5275
|
594 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
595 { |
5275
|
596 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
597 if (a.elem (i,j) != T ()) |
|
598 { |
|
599 xdata(ii) = a.elem (i,j); |
|
600 xridx(ii++) = i; |
|
601 } |
|
602 xcidx(j+1) = ii; |
|
603 } |
|
604 } |
|
605 } |
|
606 |
|
607 template <class T> |
|
608 Sparse<T>::~Sparse (void) |
|
609 { |
|
610 if (--rep->count <= 0) |
|
611 delete rep; |
|
612 |
|
613 delete [] idx; |
|
614 } |
|
615 |
|
616 template <class T> |
5275
|
617 octave_idx_type |
|
618 Sparse<T>::compute_index (const Array<octave_idx_type>& ra_idx) const |
5164
|
619 { |
5275
|
620 octave_idx_type retval = -1; |
|
621 |
|
622 octave_idx_type n = dimensions.length (); |
5164
|
623 |
|
624 if (n > 0 && n == ra_idx.length ()) |
|
625 { |
|
626 retval = ra_idx(--n); |
|
627 |
|
628 while (--n >= 0) |
|
629 { |
|
630 retval *= dimensions(n); |
|
631 retval += ra_idx(n); |
|
632 } |
|
633 } |
|
634 else |
|
635 (*current_liboctave_error_handler) |
|
636 ("Sparse<T>::compute_index: invalid ra_idxing operation"); |
|
637 |
|
638 return retval; |
|
639 } |
|
640 |
|
641 template <class T> |
|
642 T |
5275
|
643 Sparse<T>::range_error (const char *fcn, octave_idx_type n) const |
5164
|
644 { |
|
645 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
|
646 return T (); |
|
647 } |
|
648 |
|
649 template <class T> |
|
650 T& |
5275
|
651 Sparse<T>::range_error (const char *fcn, octave_idx_type n) |
5164
|
652 { |
|
653 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
|
654 static T foo; |
|
655 return foo; |
|
656 } |
|
657 |
|
658 template <class T> |
|
659 T |
5275
|
660 Sparse<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j) const |
5164
|
661 { |
|
662 (*current_liboctave_error_handler) |
|
663 ("%s (%d, %d): range error", fcn, i, j); |
|
664 return T (); |
|
665 } |
|
666 |
|
667 template <class T> |
|
668 T& |
5275
|
669 Sparse<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j) |
5164
|
670 { |
|
671 (*current_liboctave_error_handler) |
|
672 ("%s (%d, %d): range error", fcn, i, j); |
|
673 static T foo; |
|
674 return foo; |
|
675 } |
|
676 |
|
677 template <class T> |
|
678 T |
5275
|
679 Sparse<T>::range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const |
5164
|
680 { |
5765
|
681 std::ostringstream buf; |
5164
|
682 |
|
683 buf << fcn << " ("; |
|
684 |
5275
|
685 octave_idx_type n = ra_idx.length (); |
5164
|
686 |
|
687 if (n > 0) |
|
688 buf << ra_idx(0); |
|
689 |
5275
|
690 for (octave_idx_type i = 1; i < n; i++) |
5164
|
691 buf << ", " << ra_idx(i); |
|
692 |
|
693 buf << "): range error"; |
5765
|
694 |
|
695 std::string buf_str = buf.str (); |
|
696 |
|
697 (*current_liboctave_error_handler) (buf_str.c_str ()); |
5164
|
698 |
|
699 return T (); |
|
700 } |
|
701 |
|
702 template <class T> |
|
703 T& |
5275
|
704 Sparse<T>::range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) |
5164
|
705 { |
5765
|
706 std::ostringstream buf; |
5164
|
707 |
|
708 buf << fcn << " ("; |
|
709 |
5275
|
710 octave_idx_type n = ra_idx.length (); |
5164
|
711 |
|
712 if (n > 0) |
|
713 buf << ra_idx(0); |
|
714 |
5275
|
715 for (octave_idx_type i = 1; i < n; i++) |
5164
|
716 buf << ", " << ra_idx(i); |
|
717 |
|
718 buf << "): range error"; |
|
719 |
5765
|
720 std::string buf_str = buf.str (); |
|
721 |
|
722 (*current_liboctave_error_handler) (buf_str.c_str ()); |
5164
|
723 |
|
724 static T foo; |
|
725 return foo; |
|
726 } |
|
727 |
|
728 template <class T> |
|
729 Sparse<T> |
|
730 Sparse<T>::reshape (const dim_vector& new_dims) const |
|
731 { |
|
732 Sparse<T> retval; |
6689
|
733 dim_vector dims2 = new_dims; |
|
734 |
|
735 if (dims2.length () > 2) |
5164
|
736 { |
6814
|
737 (*current_liboctave_warning_handler) |
|
738 ("reshape: sparse reshape to N-d array smashes dims"); |
|
739 |
6689
|
740 for (octave_idx_type i = 2; i < dims2.length(); i++) |
6814
|
741 dims2(1) *= dims2(i); |
|
742 |
6689
|
743 dims2.resize (2); |
|
744 } |
|
745 |
|
746 if (dimensions != dims2) |
|
747 { |
|
748 if (dimensions.numel () == dims2.numel ()) |
5164
|
749 { |
5681
|
750 octave_idx_type new_nnz = nnz (); |
6689
|
751 octave_idx_type new_nr = dims2 (0); |
|
752 octave_idx_type new_nc = dims2 (1); |
5275
|
753 octave_idx_type old_nr = rows (); |
|
754 octave_idx_type old_nc = cols (); |
5681
|
755 retval = Sparse<T> (new_nr, new_nc, new_nnz); |
5164
|
756 |
5275
|
757 octave_idx_type kk = 0; |
5164
|
758 retval.xcidx(0) = 0; |
5275
|
759 for (octave_idx_type i = 0; i < old_nc; i++) |
|
760 for (octave_idx_type j = cidx(i); j < cidx(i+1); j++) |
5164
|
761 { |
5275
|
762 octave_idx_type tmp = i * old_nr + ridx(j); |
|
763 octave_idx_type ii = tmp % new_nr; |
|
764 octave_idx_type jj = (tmp - ii) / new_nr; |
|
765 for (octave_idx_type k = kk; k < jj; k++) |
5164
|
766 retval.xcidx(k+1) = j; |
|
767 kk = jj; |
|
768 retval.xdata(j) = data(j); |
|
769 retval.xridx(j) = ii; |
|
770 } |
5275
|
771 for (octave_idx_type k = kk; k < new_nc; k++) |
5681
|
772 retval.xcidx(k+1) = new_nnz; |
5164
|
773 } |
|
774 else |
|
775 (*current_liboctave_error_handler) ("reshape: size mismatch"); |
|
776 } |
|
777 else |
|
778 retval = *this; |
|
779 |
|
780 return retval; |
|
781 } |
|
782 |
|
783 template <class T> |
|
784 Sparse<T> |
5275
|
785 Sparse<T>::permute (const Array<octave_idx_type>& perm_vec, bool) const |
5164
|
786 { |
6813
|
787 // The only valid permutations of a sparse array are [1, 2] and [2, 1]. |
|
788 |
|
789 bool fail = false; |
6817
|
790 bool trans = false; |
6813
|
791 |
|
792 if (perm_vec.length () == 2) |
5164
|
793 { |
6813
|
794 if (perm_vec(0) == 0 && perm_vec(1) == 1) |
|
795 /* do nothing */; |
|
796 else if (perm_vec(0) == 1 && perm_vec(1) == 0) |
6817
|
797 trans = true; |
5164
|
798 else |
6813
|
799 fail = true; |
5164
|
800 } |
|
801 else |
6813
|
802 fail = true; |
|
803 |
|
804 if (fail) |
|
805 (*current_liboctave_error_handler) |
|
806 ("permutation vector contains an invalid element"); |
|
807 |
6817
|
808 return trans ? this->transpose () : *this; |
5164
|
809 } |
|
810 |
|
811 template <class T> |
|
812 void |
|
813 Sparse<T>::resize_no_fill (const dim_vector& dv) |
|
814 { |
5275
|
815 octave_idx_type n = dv.length (); |
5164
|
816 |
|
817 if (n != 2) |
|
818 { |
|
819 (*current_liboctave_error_handler) ("sparse array must be 2-D"); |
|
820 return; |
|
821 } |
|
822 |
|
823 resize_no_fill (dv(0), dv(1)); |
|
824 } |
|
825 |
|
826 template <class T> |
|
827 void |
5275
|
828 Sparse<T>::resize_no_fill (octave_idx_type r, octave_idx_type c) |
5164
|
829 { |
|
830 if (r < 0 || c < 0) |
|
831 { |
|
832 (*current_liboctave_error_handler) |
|
833 ("can't resize to negative dimension"); |
|
834 return; |
|
835 } |
|
836 |
|
837 if (ndims () == 0) |
|
838 dimensions = dim_vector (0, 0); |
|
839 |
|
840 if (r == dim1 () && c == dim2 ()) |
|
841 return; |
|
842 |
5731
|
843 typename Sparse<T>::SparseRep *old_rep = rep; |
|
844 |
5275
|
845 octave_idx_type nc = cols (); |
|
846 octave_idx_type nr = rows (); |
5164
|
847 |
5681
|
848 if (nnz () == 0 || r == 0 || c == 0) |
5164
|
849 // Special case of redimensioning to/from a sparse matrix with |
|
850 // no elements |
|
851 rep = new typename Sparse<T>::SparseRep (r, c); |
|
852 else |
|
853 { |
5275
|
854 octave_idx_type n = 0; |
5164
|
855 Sparse<T> tmpval; |
|
856 if (r >= nr) |
|
857 { |
|
858 if (c > nc) |
5731
|
859 n = xcidx(nc); |
5164
|
860 else |
5731
|
861 n = xcidx(c); |
5164
|
862 |
|
863 tmpval = Sparse<T> (r, c, n); |
|
864 |
|
865 if (c > nc) |
|
866 { |
6101
|
867 for (octave_idx_type i = 0; i < nc + 1; i++) |
5731
|
868 tmpval.cidx(i) = xcidx(i); |
6101
|
869 for (octave_idx_type i = nc + 1; i < c + 1; i++) |
5164
|
870 tmpval.cidx(i) = tmpval.cidx(i-1); |
|
871 } |
|
872 else if (c <= nc) |
6101
|
873 for (octave_idx_type i = 0; i < c + 1; i++) |
5731
|
874 tmpval.cidx(i) = xcidx(i); |
5164
|
875 |
5275
|
876 for (octave_idx_type i = 0; i < n; i++) |
5164
|
877 { |
5731
|
878 tmpval.data(i) = xdata(i); |
|
879 tmpval.ridx(i) = xridx(i); |
5164
|
880 } |
|
881 } |
|
882 else |
|
883 { |
|
884 // Count how many non zero terms before we do anything |
6101
|
885 octave_idx_type min_nc = (c < nc ? c : nc); |
|
886 for (octave_idx_type i = 0; i < min_nc; i++) |
5731
|
887 for (octave_idx_type j = xcidx(i); j < xcidx(i+1); j++) |
|
888 if (xridx(j) < r) |
5164
|
889 n++; |
|
890 |
|
891 if (n) |
|
892 { |
|
893 // Now that we know the size we can do something |
|
894 tmpval = Sparse<T> (r, c, n); |
|
895 |
|
896 tmpval.cidx(0); |
6101
|
897 for (octave_idx_type i = 0, ii = 0; i < min_nc; i++) |
5164
|
898 { |
5731
|
899 for (octave_idx_type j = xcidx(i); j < xcidx(i+1); j++) |
|
900 if (xridx(j) < r) |
5164
|
901 { |
5731
|
902 tmpval.data(ii) = xdata(j); |
|
903 tmpval.ridx(ii++) = xridx(j); |
5164
|
904 } |
|
905 tmpval.cidx(i+1) = ii; |
|
906 } |
6101
|
907 if (c > min_nc) |
|
908 for (octave_idx_type i = nc; i < c; i++) |
|
909 tmpval.cidx(i+1) = tmpval.cidx(i); |
5164
|
910 } |
|
911 else |
|
912 tmpval = Sparse<T> (r, c); |
|
913 } |
|
914 |
|
915 rep = tmpval.rep; |
|
916 rep->count++; |
|
917 } |
|
918 |
|
919 dimensions = dim_vector (r, c); |
|
920 |
|
921 if (--old_rep->count <= 0) |
|
922 delete old_rep; |
|
923 } |
|
924 |
|
925 template <class T> |
|
926 Sparse<T>& |
5275
|
927 Sparse<T>::insert (const Sparse<T>& a, octave_idx_type r, octave_idx_type c) |
5164
|
928 { |
5275
|
929 octave_idx_type a_rows = a.rows (); |
|
930 octave_idx_type a_cols = a.cols (); |
|
931 octave_idx_type nr = rows (); |
|
932 octave_idx_type nc = cols (); |
5164
|
933 |
|
934 if (r < 0 || r + a_rows > rows () || c < 0 || c + a_cols > cols ()) |
|
935 { |
|
936 (*current_liboctave_error_handler) ("range error for insert"); |
|
937 return *this; |
|
938 } |
|
939 |
|
940 // First count the number of elements in the final array |
5681
|
941 octave_idx_type nel = cidx(c) + a.nnz (); |
5164
|
942 |
|
943 if (c + a_cols < nc) |
|
944 nel += cidx(nc) - cidx(c + a_cols); |
|
945 |
5275
|
946 for (octave_idx_type i = c; i < c + a_cols; i++) |
|
947 for (octave_idx_type j = cidx(i); j < cidx(i+1); j++) |
5164
|
948 if (ridx(j) < r || ridx(j) >= r + a_rows) |
|
949 nel++; |
|
950 |
|
951 Sparse<T> tmp (*this); |
|
952 --rep->count; |
|
953 rep = new typename Sparse<T>::SparseRep (nr, nc, nel); |
|
954 |
5275
|
955 for (octave_idx_type i = 0; i < tmp.cidx(c); i++) |
5164
|
956 { |
|
957 data(i) = tmp.data(i); |
|
958 ridx(i) = tmp.ridx(i); |
|
959 } |
5275
|
960 for (octave_idx_type i = 0; i < c + 1; i++) |
5164
|
961 cidx(i) = tmp.cidx(i); |
|
962 |
5275
|
963 octave_idx_type ii = cidx(c); |
|
964 |
|
965 for (octave_idx_type i = c; i < c + a_cols; i++) |
5164
|
966 { |
|
967 OCTAVE_QUIT; |
|
968 |
5275
|
969 for (octave_idx_type j = tmp.cidx(i); j < tmp.cidx(i+1); j++) |
5164
|
970 if (tmp.ridx(j) < r) |
|
971 { |
|
972 data(ii) = tmp.data(j); |
|
973 ridx(ii++) = tmp.ridx(j); |
|
974 } |
|
975 |
|
976 OCTAVE_QUIT; |
|
977 |
5275
|
978 for (octave_idx_type j = a.cidx(i-c); j < a.cidx(i-c+1); j++) |
5164
|
979 { |
|
980 data(ii) = a.data(j); |
|
981 ridx(ii++) = r + a.ridx(j); |
|
982 } |
|
983 |
|
984 OCTAVE_QUIT; |
|
985 |
5275
|
986 for (octave_idx_type j = tmp.cidx(i); j < tmp.cidx(i+1); j++) |
5164
|
987 if (tmp.ridx(j) >= r + a_rows) |
|
988 { |
|
989 data(ii) = tmp.data(j); |
|
990 ridx(ii++) = tmp.ridx(j); |
|
991 } |
|
992 |
|
993 cidx(i+1) = ii; |
|
994 } |
|
995 |
5275
|
996 for (octave_idx_type i = c + a_cols; i < nc; i++) |
5164
|
997 { |
5275
|
998 for (octave_idx_type j = tmp.cidx(i); j < tmp.cidx(i+1); j++) |
5164
|
999 { |
|
1000 data(ii) = tmp.data(j); |
|
1001 ridx(ii++) = tmp.ridx(j); |
|
1002 } |
|
1003 cidx(i+1) = ii; |
|
1004 } |
|
1005 |
|
1006 return *this; |
|
1007 } |
|
1008 |
|
1009 template <class T> |
|
1010 Sparse<T>& |
5275
|
1011 Sparse<T>::insert (const Sparse<T>& a, const Array<octave_idx_type>& ra_idx) |
5164
|
1012 { |
|
1013 |
|
1014 if (ra_idx.length () != 2) |
|
1015 { |
|
1016 (*current_liboctave_error_handler) ("range error for insert"); |
|
1017 return *this; |
|
1018 } |
|
1019 |
|
1020 return insert (a, ra_idx (0), ra_idx (1)); |
|
1021 } |
|
1022 |
|
1023 template <class T> |
|
1024 Sparse<T> |
|
1025 Sparse<T>::transpose (void) const |
|
1026 { |
|
1027 assert (ndims () == 2); |
|
1028 |
5275
|
1029 octave_idx_type nr = rows (); |
|
1030 octave_idx_type nc = cols (); |
5648
|
1031 octave_idx_type nz = nnz (); |
5164
|
1032 Sparse<T> retval (nc, nr, nz); |
|
1033 |
5648
|
1034 OCTAVE_LOCAL_BUFFER (octave_idx_type, w, nr + 1); |
|
1035 for (octave_idx_type i = 0; i < nr; i++) |
|
1036 w[i] = 0; |
|
1037 for (octave_idx_type i = 0; i < nz; i++) |
|
1038 w[ridx(i)]++; |
|
1039 nz = 0; |
|
1040 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
1041 { |
5648
|
1042 retval.xcidx(i) = nz; |
|
1043 nz += w[i]; |
|
1044 w[i] = retval.xcidx(i); |
5164
|
1045 } |
5648
|
1046 retval.xcidx(nr) = nz; |
|
1047 w[nr] = nz; |
|
1048 |
|
1049 for (octave_idx_type j = 0; j < nc; j++) |
|
1050 for (octave_idx_type k = cidx(j); k < cidx(j+1); k++) |
|
1051 { |
|
1052 octave_idx_type q = w [ridx(k)]++; |
|
1053 retval.xridx (q) = j; |
|
1054 retval.xdata (q) = data (k); |
|
1055 } |
5164
|
1056 |
|
1057 return retval; |
|
1058 } |
|
1059 |
|
1060 template <class T> |
|
1061 void |
|
1062 Sparse<T>::clear_index (void) |
|
1063 { |
|
1064 delete [] idx; |
|
1065 idx = 0; |
|
1066 idx_count = 0; |
|
1067 } |
|
1068 |
|
1069 template <class T> |
|
1070 void |
|
1071 Sparse<T>::set_index (const idx_vector& idx_arg) |
|
1072 { |
5275
|
1073 octave_idx_type nd = ndims (); |
5164
|
1074 |
|
1075 if (! idx && nd > 0) |
|
1076 idx = new idx_vector [nd]; |
|
1077 |
|
1078 if (idx_count < nd) |
|
1079 { |
|
1080 idx[idx_count++] = idx_arg; |
|
1081 } |
|
1082 else |
|
1083 { |
|
1084 idx_vector *new_idx = new idx_vector [idx_count+1]; |
|
1085 |
5275
|
1086 for (octave_idx_type i = 0; i < idx_count; i++) |
5164
|
1087 new_idx[i] = idx[i]; |
|
1088 |
|
1089 new_idx[idx_count++] = idx_arg; |
|
1090 |
|
1091 delete [] idx; |
|
1092 |
|
1093 idx = new_idx; |
|
1094 } |
|
1095 } |
|
1096 |
|
1097 template <class T> |
|
1098 void |
|
1099 Sparse<T>::maybe_delete_elements (idx_vector& idx_arg) |
|
1100 { |
5275
|
1101 octave_idx_type nr = dim1 (); |
|
1102 octave_idx_type nc = dim2 (); |
5164
|
1103 |
|
1104 if (nr == 0 && nc == 0) |
|
1105 return; |
|
1106 |
5275
|
1107 octave_idx_type n; |
5164
|
1108 if (nr == 1) |
|
1109 n = nc; |
|
1110 else if (nc == 1) |
|
1111 n = nr; |
|
1112 else |
|
1113 { |
|
1114 // Reshape to row vector for Matlab compatibility. |
|
1115 |
|
1116 n = nr * nc; |
|
1117 nr = 1; |
|
1118 nc = n; |
|
1119 } |
|
1120 |
|
1121 if (idx_arg.is_colon_equiv (n, 1)) |
|
1122 { |
|
1123 // Either A(:) = [] or A(idx) = [] with idx enumerating all |
|
1124 // elements, so we delete all elements and return [](0x0). To |
|
1125 // preserve the orientation of the vector, you have to use |
|
1126 // A(idx,:) = [] (delete rows) or A(:,idx) (delete columns). |
|
1127 |
|
1128 resize_no_fill (0, 0); |
|
1129 return; |
|
1130 } |
|
1131 |
|
1132 idx_arg.sort (true); |
|
1133 |
5275
|
1134 octave_idx_type num_to_delete = idx_arg.length (n); |
5164
|
1135 |
|
1136 if (num_to_delete != 0) |
|
1137 { |
5275
|
1138 octave_idx_type new_n = n; |
5681
|
1139 octave_idx_type new_nnz = nnz (); |
5275
|
1140 |
|
1141 octave_idx_type iidx = 0; |
5164
|
1142 |
|
1143 const Sparse<T> tmp (*this); |
|
1144 |
5275
|
1145 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1146 { |
|
1147 OCTAVE_QUIT; |
|
1148 |
|
1149 if (i == idx_arg.elem (iidx)) |
|
1150 { |
|
1151 iidx++; |
|
1152 new_n--; |
|
1153 |
|
1154 if (tmp.elem (i) != T ()) |
5681
|
1155 new_nnz--; |
5164
|
1156 |
|
1157 if (iidx == num_to_delete) |
|
1158 break; |
|
1159 } |
|
1160 } |
|
1161 |
|
1162 if (new_n > 0) |
|
1163 { |
|
1164 rep->count--; |
|
1165 |
|
1166 if (nr == 1) |
5681
|
1167 rep = new typename Sparse<T>::SparseRep (1, new_n, new_nnz); |
5164
|
1168 else |
5681
|
1169 rep = new typename Sparse<T>::SparseRep (new_n, 1, new_nnz); |
5164
|
1170 |
5275
|
1171 octave_idx_type ii = 0; |
|
1172 octave_idx_type jj = 0; |
5164
|
1173 iidx = 0; |
5275
|
1174 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1175 { |
|
1176 OCTAVE_QUIT; |
|
1177 |
|
1178 if (iidx < num_to_delete && i == idx_arg.elem (iidx)) |
|
1179 iidx++; |
|
1180 else |
|
1181 { |
|
1182 T el = tmp.elem (i); |
|
1183 if (el != T ()) |
|
1184 { |
|
1185 data(ii) = el; |
|
1186 ridx(ii++) = jj; |
|
1187 } |
|
1188 jj++; |
|
1189 } |
|
1190 } |
|
1191 |
|
1192 dimensions.resize (2); |
|
1193 |
|
1194 if (nr == 1) |
|
1195 { |
|
1196 ii = 0; |
|
1197 cidx(0) = 0; |
5275
|
1198 for (octave_idx_type i = 0; i < new_n; i++) |
5164
|
1199 { |
|
1200 OCTAVE_QUIT; |
|
1201 if (ridx(ii) == i) |
|
1202 ridx(ii++) = 0; |
|
1203 cidx(i+1) = ii; |
|
1204 } |
|
1205 |
|
1206 dimensions(0) = 1; |
|
1207 dimensions(1) = new_n; |
|
1208 } |
|
1209 else |
|
1210 { |
|
1211 cidx(0) = 0; |
5681
|
1212 cidx(1) = new_nnz; |
5164
|
1213 dimensions(0) = new_n; |
|
1214 dimensions(1) = 1; |
|
1215 } |
|
1216 } |
|
1217 else |
|
1218 (*current_liboctave_error_handler) |
|
1219 ("A(idx) = []: index out of range"); |
|
1220 } |
|
1221 } |
|
1222 |
|
1223 template <class T> |
|
1224 void |
|
1225 Sparse<T>::maybe_delete_elements (idx_vector& idx_i, idx_vector& idx_j) |
|
1226 { |
|
1227 assert (ndims () == 2); |
|
1228 |
5275
|
1229 octave_idx_type nr = dim1 (); |
|
1230 octave_idx_type nc = dim2 (); |
5164
|
1231 |
|
1232 if (nr == 0 && nc == 0) |
|
1233 return; |
|
1234 |
|
1235 if (idx_i.is_colon ()) |
|
1236 { |
|
1237 if (idx_j.is_colon ()) |
|
1238 { |
|
1239 // A(:,:) -- We are deleting columns and rows, so the result |
|
1240 // is [](0x0). |
|
1241 |
|
1242 resize_no_fill (0, 0); |
|
1243 return; |
|
1244 } |
|
1245 |
|
1246 if (idx_j.is_colon_equiv (nc, 1)) |
|
1247 { |
|
1248 // A(:,j) -- We are deleting columns by enumerating them, |
|
1249 // If we enumerate all of them, we should have zero columns |
|
1250 // with the same number of rows that we started with. |
|
1251 |
|
1252 resize_no_fill (nr, 0); |
|
1253 return; |
|
1254 } |
|
1255 } |
|
1256 |
|
1257 if (idx_j.is_colon () && idx_i.is_colon_equiv (nr, 1)) |
|
1258 { |
|
1259 // A(i,:) -- We are deleting rows by enumerating them. If we |
|
1260 // enumerate all of them, we should have zero rows with the |
|
1261 // same number of columns that we started with. |
|
1262 |
|
1263 resize_no_fill (0, nc); |
|
1264 return; |
|
1265 } |
|
1266 |
|
1267 if (idx_i.is_colon_equiv (nr, 1)) |
|
1268 { |
|
1269 if (idx_j.is_colon_equiv (nc, 1)) |
|
1270 resize_no_fill (0, 0); |
|
1271 else |
|
1272 { |
|
1273 idx_j.sort (true); |
|
1274 |
5275
|
1275 octave_idx_type num_to_delete = idx_j.length (nc); |
5164
|
1276 |
|
1277 if (num_to_delete != 0) |
|
1278 { |
|
1279 if (nr == 1 && num_to_delete == nc) |
|
1280 resize_no_fill (0, 0); |
|
1281 else |
|
1282 { |
5275
|
1283 octave_idx_type new_nc = nc; |
5681
|
1284 octave_idx_type new_nnz = nnz (); |
5275
|
1285 |
|
1286 octave_idx_type iidx = 0; |
|
1287 |
|
1288 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
1289 { |
|
1290 OCTAVE_QUIT; |
|
1291 |
|
1292 if (j == idx_j.elem (iidx)) |
|
1293 { |
|
1294 iidx++; |
|
1295 new_nc--; |
|
1296 |
5681
|
1297 new_nnz -= cidx(j+1) - cidx(j); |
5164
|
1298 |
|
1299 if (iidx == num_to_delete) |
|
1300 break; |
|
1301 } |
|
1302 } |
|
1303 |
|
1304 if (new_nc > 0) |
|
1305 { |
|
1306 const Sparse<T> tmp (*this); |
|
1307 --rep->count; |
|
1308 rep = new typename Sparse<T>::SparseRep (nr, new_nc, |
5681
|
1309 new_nnz); |
5275
|
1310 octave_idx_type ii = 0; |
|
1311 octave_idx_type jj = 0; |
5164
|
1312 iidx = 0; |
|
1313 cidx(0) = 0; |
5275
|
1314 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
1315 { |
|
1316 OCTAVE_QUIT; |
|
1317 |
|
1318 if (iidx < num_to_delete && j == idx_j.elem (iidx)) |
|
1319 iidx++; |
|
1320 else |
|
1321 { |
5275
|
1322 for (octave_idx_type i = tmp.cidx(j); |
5164
|
1323 i < tmp.cidx(j+1); i++) |
|
1324 { |
|
1325 data(jj) = tmp.data(i); |
|
1326 ridx(jj++) = tmp.ridx(i); |
|
1327 } |
|
1328 cidx(++ii) = jj; |
|
1329 } |
|
1330 } |
|
1331 |
|
1332 dimensions.resize (2); |
|
1333 dimensions(1) = new_nc; |
|
1334 } |
|
1335 else |
|
1336 (*current_liboctave_error_handler) |
|
1337 ("A(idx) = []: index out of range"); |
|
1338 } |
|
1339 } |
|
1340 } |
|
1341 } |
|
1342 else if (idx_j.is_colon_equiv (nc, 1)) |
|
1343 { |
|
1344 if (idx_i.is_colon_equiv (nr, 1)) |
|
1345 resize_no_fill (0, 0); |
|
1346 else |
|
1347 { |
|
1348 idx_i.sort (true); |
|
1349 |
5275
|
1350 octave_idx_type num_to_delete = idx_i.length (nr); |
5164
|
1351 |
|
1352 if (num_to_delete != 0) |
|
1353 { |
|
1354 if (nc == 1 && num_to_delete == nr) |
|
1355 resize_no_fill (0, 0); |
|
1356 else |
|
1357 { |
5275
|
1358 octave_idx_type new_nr = nr; |
5681
|
1359 octave_idx_type new_nnz = nnz (); |
5275
|
1360 |
|
1361 octave_idx_type iidx = 0; |
|
1362 |
|
1363 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
1364 { |
|
1365 OCTAVE_QUIT; |
|
1366 |
|
1367 if (i == idx_i.elem (iidx)) |
|
1368 { |
|
1369 iidx++; |
|
1370 new_nr--; |
|
1371 |
5681
|
1372 for (octave_idx_type j = 0; j < nnz (); j++) |
5164
|
1373 if (ridx(j) == i) |
5681
|
1374 new_nnz--; |
5164
|
1375 |
|
1376 if (iidx == num_to_delete) |
|
1377 break; |
|
1378 } |
|
1379 } |
|
1380 |
|
1381 if (new_nr > 0) |
|
1382 { |
|
1383 const Sparse<T> tmp (*this); |
|
1384 --rep->count; |
|
1385 rep = new typename Sparse<T>::SparseRep (new_nr, nc, |
5681
|
1386 new_nnz); |
5164
|
1387 |
5275
|
1388 octave_idx_type jj = 0; |
5164
|
1389 cidx(0) = 0; |
5275
|
1390 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
1391 { |
|
1392 iidx = 0; |
5275
|
1393 for (octave_idx_type j = tmp.cidx(i); j < tmp.cidx(i+1); j++) |
5164
|
1394 { |
|
1395 OCTAVE_QUIT; |
|
1396 |
5275
|
1397 octave_idx_type ri = tmp.ridx(j); |
5164
|
1398 |
|
1399 while (iidx < num_to_delete && |
|
1400 ri > idx_i.elem (iidx)) |
|
1401 { |
|
1402 iidx++; |
|
1403 } |
|
1404 |
|
1405 if (iidx == num_to_delete || |
|
1406 ri != idx_i.elem(iidx)) |
|
1407 { |
|
1408 data(jj) = tmp.data(j); |
|
1409 ridx(jj++) = ri - iidx; |
|
1410 } |
|
1411 } |
|
1412 cidx(i+1) = jj; |
|
1413 } |
|
1414 |
|
1415 dimensions.resize (2); |
|
1416 dimensions(0) = new_nr; |
|
1417 } |
|
1418 else |
|
1419 (*current_liboctave_error_handler) |
|
1420 ("A(idx) = []: index out of range"); |
|
1421 } |
|
1422 } |
|
1423 } |
|
1424 } |
|
1425 } |
|
1426 |
|
1427 template <class T> |
|
1428 void |
|
1429 Sparse<T>::maybe_delete_elements (Array<idx_vector>& ra_idx) |
|
1430 { |
|
1431 if (ra_idx.length () == 1) |
|
1432 maybe_delete_elements (ra_idx(0)); |
|
1433 else if (ra_idx.length () == 2) |
|
1434 maybe_delete_elements (ra_idx(0), ra_idx(1)); |
|
1435 else |
|
1436 (*current_liboctave_error_handler) |
|
1437 ("range error for maybe_delete_elements"); |
|
1438 } |
|
1439 |
|
1440 template <class T> |
|
1441 Sparse<T> |
|
1442 Sparse<T>::value (void) |
|
1443 { |
|
1444 Sparse<T> retval; |
|
1445 |
|
1446 int n_idx = index_count (); |
|
1447 |
|
1448 if (n_idx == 2) |
|
1449 { |
|
1450 idx_vector *tmp = get_idx (); |
|
1451 |
|
1452 idx_vector idx_i = tmp[0]; |
|
1453 idx_vector idx_j = tmp[1]; |
|
1454 |
|
1455 retval = index (idx_i, idx_j); |
|
1456 } |
|
1457 else if (n_idx == 1) |
|
1458 { |
|
1459 retval = index (idx[0]); |
|
1460 } |
|
1461 else |
|
1462 (*current_liboctave_error_handler) |
|
1463 ("Sparse<T>::value: invalid number of indices specified"); |
|
1464 |
|
1465 clear_index (); |
|
1466 |
|
1467 return retval; |
|
1468 } |
|
1469 |
|
1470 template <class T> |
|
1471 Sparse<T> |
|
1472 Sparse<T>::index (idx_vector& idx_arg, int resize_ok) const |
|
1473 { |
|
1474 Sparse<T> retval; |
|
1475 |
|
1476 assert (ndims () == 2); |
|
1477 |
5275
|
1478 octave_idx_type nr = dim1 (); |
|
1479 octave_idx_type nc = dim2 (); |
5681
|
1480 octave_idx_type nz = nnz (); |
5275
|
1481 |
|
1482 octave_idx_type orig_len = nr * nc; |
5164
|
1483 |
|
1484 dim_vector idx_orig_dims = idx_arg.orig_dimensions (); |
|
1485 |
5275
|
1486 octave_idx_type idx_orig_rows = idx_arg.orig_rows (); |
|
1487 octave_idx_type idx_orig_columns = idx_arg.orig_columns (); |
5164
|
1488 |
|
1489 if (idx_orig_dims.length () > 2) |
|
1490 (*current_liboctave_error_handler) |
|
1491 ("Sparse<T>::index: Can not index Sparse<T> with an N-D Array"); |
|
1492 else if (idx_arg.is_colon ()) |
|
1493 { |
|
1494 // Fast magic colon processing. |
|
1495 retval = Sparse<T> (nr * nc, 1, nz); |
|
1496 |
5275
|
1497 for (octave_idx_type i = 0; i < nc; i++) |
|
1498 for (octave_idx_type j = cidx(i); j < cidx(i+1); j++) |
5164
|
1499 { |
|
1500 OCTAVE_QUIT; |
|
1501 retval.xdata(j) = data(j); |
|
1502 retval.xridx(j) = ridx(j) + i * nr; |
|
1503 } |
|
1504 retval.xcidx(0) = 0; |
|
1505 retval.xcidx(1) = nz; |
|
1506 } |
|
1507 else if (nr == 1 && nc == 1) |
|
1508 { |
|
1509 // You have to be pretty sick to get to this bit of code, |
|
1510 // since you have a scalar stored as a sparse matrix, and |
|
1511 // then want to make a dense matrix with sparse |
|
1512 // representation. Ok, we'll do it, but you deserve what |
|
1513 // you get!! |
5275
|
1514 octave_idx_type n = idx_arg.freeze (length (), "sparse vector", resize_ok); |
5164
|
1515 if (n == 0) |
|
1516 if (idx_arg.one_zero_only ()) |
|
1517 retval = Sparse<T> (dim_vector (0, 0)); |
|
1518 else |
|
1519 retval = Sparse<T> (dim_vector (0, 1)); |
|
1520 else if (nz < 1) |
|
1521 if (n >= idx_orig_dims.numel ()) |
|
1522 retval = Sparse<T> (idx_orig_dims); |
|
1523 else |
|
1524 retval = Sparse<T> (dim_vector (n, 1)); |
|
1525 else if (n >= idx_orig_dims.numel ()) |
|
1526 { |
|
1527 T el = elem (0); |
5275
|
1528 octave_idx_type new_nr = idx_orig_rows; |
|
1529 octave_idx_type new_nc = idx_orig_columns; |
|
1530 for (octave_idx_type i = 2; i < idx_orig_dims.length (); i++) |
5164
|
1531 new_nc *= idx_orig_dims (i); |
|
1532 |
|
1533 retval = Sparse<T> (new_nr, new_nc, idx_arg.ones_count ()); |
|
1534 |
5275
|
1535 octave_idx_type ic = 0; |
|
1536 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1537 { |
|
1538 if (i % new_nr == 0) |
|
1539 retval.xcidx(i % new_nr) = ic; |
|
1540 |
5275
|
1541 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1542 if (ii == 0) |
|
1543 { |
|
1544 OCTAVE_QUIT; |
|
1545 retval.xdata(ic) = el; |
|
1546 retval.xridx(ic++) = i % new_nr; |
|
1547 } |
|
1548 } |
|
1549 retval.xcidx (new_nc) = ic; |
|
1550 } |
|
1551 else |
|
1552 { |
|
1553 T el = elem (0); |
|
1554 retval = Sparse<T> (n, 1, nz); |
|
1555 |
5275
|
1556 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
1557 { |
|
1558 OCTAVE_QUIT; |
|
1559 retval.xdata(i) = el; |
|
1560 retval.xridx(i) = i; |
|
1561 } |
|
1562 retval.xcidx(0) = 0; |
|
1563 retval.xcidx(1) = n; |
|
1564 } |
|
1565 } |
|
1566 else if (nr == 1 || nc == 1) |
|
1567 { |
|
1568 // If indexing a vector with a matrix, return value has same |
|
1569 // shape as the index. Otherwise, it has same orientation as |
|
1570 // indexed object. |
5275
|
1571 octave_idx_type len = length (); |
|
1572 octave_idx_type n = idx_arg.freeze (len, "sparse vector", resize_ok); |
5164
|
1573 |
|
1574 if (n == 0) |
|
1575 if (nr == 1) |
|
1576 retval = Sparse<T> (dim_vector (1, 0)); |
|
1577 else |
|
1578 retval = Sparse<T> (dim_vector (0, 1)); |
|
1579 else if (nz < 1) |
|
1580 if ((n != 0 && idx_arg.one_zero_only ()) |
|
1581 || idx_orig_rows == 1 || idx_orig_columns == 1) |
|
1582 retval = Sparse<T> ((nr == 1 ? 1 : n), (nr == 1 ? n : 1)); |
|
1583 else |
|
1584 retval = Sparse<T> (idx_orig_dims); |
|
1585 else |
|
1586 { |
|
1587 |
5604
|
1588 octave_idx_type new_nzmx = 0; |
5164
|
1589 if (nr == 1) |
5275
|
1590 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1591 { |
|
1592 OCTAVE_QUIT; |
|
1593 |
5275
|
1594 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1595 if (ii < len) |
|
1596 if (cidx(ii) != cidx(ii+1)) |
5604
|
1597 new_nzmx++; |
5164
|
1598 } |
|
1599 else |
5275
|
1600 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1601 { |
5275
|
1602 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1603 if (ii < len) |
5275
|
1604 for (octave_idx_type j = 0; j < nz; j++) |
5164
|
1605 { |
|
1606 OCTAVE_QUIT; |
|
1607 |
|
1608 if (ridx(j) == ii) |
5604
|
1609 new_nzmx++; |
5164
|
1610 if (ridx(j) >= ii) |
|
1611 break; |
|
1612 } |
|
1613 } |
|
1614 |
|
1615 if (idx_arg.one_zero_only () || idx_orig_rows == 1 || |
|
1616 idx_orig_columns == 1) |
|
1617 { |
|
1618 if (nr == 1) |
|
1619 { |
5604
|
1620 retval = Sparse<T> (1, n, new_nzmx); |
5275
|
1621 octave_idx_type jj = 0; |
5164
|
1622 retval.xcidx(0) = 0; |
5275
|
1623 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1624 { |
|
1625 OCTAVE_QUIT; |
|
1626 |
5275
|
1627 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1628 if (ii < len) |
|
1629 if (cidx(ii) != cidx(ii+1)) |
|
1630 { |
|
1631 retval.xdata(jj) = data(cidx(ii)); |
|
1632 retval.xridx(jj++) = 0; |
|
1633 } |
|
1634 retval.xcidx(i+1) = jj; |
|
1635 } |
|
1636 } |
|
1637 else |
|
1638 { |
5604
|
1639 retval = Sparse<T> (n, 1, new_nzmx); |
5164
|
1640 retval.xcidx(0) = 0; |
5604
|
1641 retval.xcidx(1) = new_nzmx; |
5275
|
1642 octave_idx_type jj = 0; |
|
1643 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1644 { |
5275
|
1645 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1646 if (ii < len) |
5275
|
1647 for (octave_idx_type j = 0; j < nz; j++) |
5164
|
1648 { |
|
1649 OCTAVE_QUIT; |
|
1650 |
|
1651 if (ridx(j) == ii) |
|
1652 { |
|
1653 retval.xdata(jj) = data(j); |
|
1654 retval.xridx(jj++) = i; |
|
1655 } |
|
1656 if (ridx(j) >= ii) |
|
1657 break; |
|
1658 } |
|
1659 } |
|
1660 } |
|
1661 } |
|
1662 else |
|
1663 { |
5275
|
1664 octave_idx_type new_nr; |
|
1665 octave_idx_type new_nc; |
5164
|
1666 if (n >= idx_orig_dims.numel ()) |
|
1667 { |
|
1668 new_nr = idx_orig_rows; |
|
1669 new_nc = idx_orig_columns; |
|
1670 } |
|
1671 else |
|
1672 { |
|
1673 new_nr = n; |
|
1674 new_nc = 1; |
|
1675 } |
|
1676 |
5604
|
1677 retval = Sparse<T> (new_nr, new_nc, new_nzmx); |
5164
|
1678 |
|
1679 if (nr == 1) |
|
1680 { |
5275
|
1681 octave_idx_type jj = 0; |
5164
|
1682 retval.xcidx(0) = 0; |
5275
|
1683 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1684 { |
|
1685 OCTAVE_QUIT; |
|
1686 |
5275
|
1687 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1688 if (ii < len) |
|
1689 if (cidx(ii) != cidx(ii+1)) |
|
1690 { |
|
1691 retval.xdata(jj) = data(cidx(ii)); |
|
1692 retval.xridx(jj++) = 0; |
|
1693 } |
|
1694 retval.xcidx(i/new_nr+1) = jj; |
|
1695 } |
|
1696 } |
|
1697 else |
|
1698 { |
5275
|
1699 octave_idx_type jj = 0; |
5164
|
1700 retval.xcidx(0) = 0; |
5275
|
1701 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1702 { |
5275
|
1703 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1704 if (ii < len) |
5275
|
1705 for (octave_idx_type j = 0; j < nz; j++) |
5164
|
1706 { |
|
1707 OCTAVE_QUIT; |
|
1708 |
|
1709 if (ridx(j) == ii) |
|
1710 { |
|
1711 retval.xdata(jj) = data(j); |
|
1712 retval.xridx(jj++) = i; |
|
1713 } |
|
1714 if (ridx(j) >= ii) |
|
1715 break; |
|
1716 } |
|
1717 retval.xcidx(i/new_nr+1) = jj; |
|
1718 } |
|
1719 } |
|
1720 } |
|
1721 } |
|
1722 } |
|
1723 else |
|
1724 { |
5781
|
1725 if (! (idx_arg.one_zero_only () |
|
1726 && idx_orig_rows == nr |
|
1727 && idx_orig_columns == nc)) |
|
1728 (*current_liboctave_warning_with_id_handler) |
|
1729 ("Octave:fortran-indexing", "single index used for sparse matrix"); |
5164
|
1730 |
|
1731 // This code is only for indexing matrices. The vector |
|
1732 // cases are handled above. |
|
1733 |
|
1734 idx_arg.freeze (nr * nc, "matrix", resize_ok); |
|
1735 |
|
1736 if (idx_arg) |
|
1737 { |
5275
|
1738 octave_idx_type result_nr = idx_orig_rows; |
|
1739 octave_idx_type result_nc = idx_orig_columns; |
5164
|
1740 |
|
1741 if (idx_arg.one_zero_only ()) |
|
1742 { |
|
1743 result_nr = idx_arg.ones_count (); |
|
1744 result_nc = (result_nr > 0 ? 1 : 0); |
|
1745 } |
|
1746 |
|
1747 if (nz < 1) |
|
1748 retval = Sparse<T> (result_nr, result_nc); |
|
1749 else |
|
1750 { |
|
1751 // Count number of non-zero elements |
5604
|
1752 octave_idx_type new_nzmx = 0; |
5275
|
1753 octave_idx_type kk = 0; |
|
1754 for (octave_idx_type j = 0; j < result_nc; j++) |
5164
|
1755 { |
5275
|
1756 for (octave_idx_type i = 0; i < result_nr; i++) |
5164
|
1757 { |
|
1758 OCTAVE_QUIT; |
|
1759 |
5275
|
1760 octave_idx_type ii = idx_arg.elem (kk++); |
5164
|
1761 if (ii < orig_len) |
|
1762 { |
5275
|
1763 octave_idx_type fr = ii % nr; |
|
1764 octave_idx_type fc = (ii - fr) / nr; |
|
1765 for (octave_idx_type k = cidx(fc); k < cidx(fc+1); k++) |
5164
|
1766 { |
|
1767 if (ridx(k) == fr) |
5604
|
1768 new_nzmx++; |
5164
|
1769 if (ridx(k) >= fr) |
|
1770 break; |
|
1771 } |
|
1772 } |
|
1773 } |
|
1774 } |
|
1775 |
5604
|
1776 retval = Sparse<T> (result_nr, result_nc, new_nzmx); |
5164
|
1777 |
|
1778 kk = 0; |
5275
|
1779 octave_idx_type jj = 0; |
5164
|
1780 retval.xcidx(0) = 0; |
5275
|
1781 for (octave_idx_type j = 0; j < result_nc; j++) |
5164
|
1782 { |
5275
|
1783 for (octave_idx_type i = 0; i < result_nr; i++) |
5164
|
1784 { |
|
1785 OCTAVE_QUIT; |
|
1786 |
5275
|
1787 octave_idx_type ii = idx_arg.elem (kk++); |
5164
|
1788 if (ii < orig_len) |
|
1789 { |
5275
|
1790 octave_idx_type fr = ii % nr; |
|
1791 octave_idx_type fc = (ii - fr) / nr; |
|
1792 for (octave_idx_type k = cidx(fc); k < cidx(fc+1); k++) |
5164
|
1793 { |
|
1794 if (ridx(k) == fr) |
|
1795 { |
|
1796 retval.xdata(jj) = data(k); |
|
1797 retval.xridx(jj++) = i; |
|
1798 } |
|
1799 if (ridx(k) >= fr) |
|
1800 break; |
|
1801 } |
|
1802 } |
|
1803 } |
|
1804 retval.xcidx(j+1) = jj; |
|
1805 } |
|
1806 } |
|
1807 // idx_vector::freeze() printed an error message for us. |
|
1808 } |
|
1809 } |
|
1810 |
|
1811 return retval; |
|
1812 } |
|
1813 |
6988
|
1814 struct |
|
1815 idx_node |
|
1816 { |
|
1817 octave_idx_type i; |
|
1818 struct idx_node *next; |
|
1819 }; |
|
1820 |
5164
|
1821 template <class T> |
|
1822 Sparse<T> |
|
1823 Sparse<T>::index (idx_vector& idx_i, idx_vector& idx_j, int resize_ok) const |
|
1824 { |
|
1825 Sparse<T> retval; |
|
1826 |
|
1827 assert (ndims () == 2); |
|
1828 |
5275
|
1829 octave_idx_type nr = dim1 (); |
|
1830 octave_idx_type nc = dim2 (); |
|
1831 |
|
1832 octave_idx_type n = idx_i.freeze (nr, "row", resize_ok); |
|
1833 octave_idx_type m = idx_j.freeze (nc, "column", resize_ok); |
5164
|
1834 |
|
1835 if (idx_i && idx_j) |
|
1836 { |
|
1837 if (idx_i.orig_empty () || idx_j.orig_empty () || n == 0 || m == 0) |
|
1838 { |
|
1839 retval.resize_no_fill (n, m); |
|
1840 } |
5681
|
1841 else |
5164
|
1842 { |
5681
|
1843 int idx_i_colon = idx_i.is_colon_equiv (nr); |
|
1844 int idx_j_colon = idx_j.is_colon_equiv (nc); |
|
1845 |
|
1846 if (idx_i_colon && idx_j_colon) |
|
1847 { |
|
1848 retval = *this; |
|
1849 } |
|
1850 else |
5164
|
1851 { |
5681
|
1852 // Identify if the indices have any repeated values |
|
1853 bool permutation = true; |
|
1854 |
|
1855 OCTAVE_LOCAL_BUFFER (octave_idx_type, itmp, |
|
1856 (nr > nc ? nr : nc)); |
|
1857 octave_sort<octave_idx_type> sort; |
|
1858 |
|
1859 if (n > nr || m > nc) |
|
1860 permutation = false; |
|
1861 |
|
1862 if (permutation && ! idx_i_colon) |
|
1863 { |
|
1864 // Can't use something like |
|
1865 // idx_vector tmp_idx = idx_i; |
|
1866 // tmp_idx.sort (true); |
|
1867 // if (tmp_idx.length(nr) != n) |
|
1868 // permutation = false; |
|
1869 // here as there is no make_unique function |
|
1870 // for idx_vector type. |
|
1871 for (octave_idx_type i = 0; i < n; i++) |
|
1872 itmp [i] = idx_i.elem (i); |
|
1873 sort.sort (itmp, n); |
|
1874 for (octave_idx_type i = 1; i < n; i++) |
|
1875 if (itmp[i-1] == itmp[i]) |
|
1876 { |
|
1877 permutation = false; |
|
1878 break; |
|
1879 } |
|
1880 } |
|
1881 if (permutation && ! idx_j_colon) |
|
1882 { |
|
1883 for (octave_idx_type i = 0; i < m; i++) |
|
1884 itmp [i] = idx_j.elem (i); |
|
1885 sort.sort (itmp, m); |
|
1886 for (octave_idx_type i = 1; i < m; i++) |
|
1887 if (itmp[i-1] == itmp[i]) |
|
1888 { |
|
1889 permutation = false; |
|
1890 break; |
|
1891 } |
|
1892 } |
|
1893 |
|
1894 if (permutation) |
5164
|
1895 { |
5681
|
1896 // Special case permutation like indexing for speed |
|
1897 retval = Sparse<T> (n, m, nnz ()); |
|
1898 octave_idx_type *ri = retval.xridx (); |
|
1899 |
5766
|
1900 std::vector<T> X (n); |
5681
|
1901 for (octave_idx_type i = 0; i < nr; i++) |
|
1902 itmp [i] = -1; |
|
1903 for (octave_idx_type i = 0; i < n; i++) |
|
1904 itmp[idx_i.elem(i)] = i; |
|
1905 |
|
1906 octave_idx_type kk = 0; |
|
1907 retval.xcidx(0) = 0; |
|
1908 for (octave_idx_type j = 0; j < m; j++) |
|
1909 { |
|
1910 octave_idx_type jj = idx_j.elem (j); |
|
1911 for (octave_idx_type i = cidx(jj); i < cidx(jj+1); i++) |
|
1912 { |
6988
|
1913 OCTAVE_QUIT; |
|
1914 |
5681
|
1915 octave_idx_type ii = itmp [ridx(i)]; |
|
1916 if (ii >= 0) |
|
1917 { |
|
1918 X [ii] = data (i); |
|
1919 retval.xridx (kk++) = ii; |
|
1920 } |
|
1921 } |
|
1922 sort.sort (ri + retval.xcidx (j), kk - retval.xcidx (j)); |
|
1923 for (octave_idx_type p = retval.xcidx (j); p < kk; p++) |
|
1924 retval.xdata (p) = X [retval.xridx (p)]; |
|
1925 retval.xcidx(j+1) = kk; |
|
1926 } |
|
1927 retval.maybe_compress (); |
|
1928 } |
|
1929 else |
|
1930 { |
6988
|
1931 OCTAVE_LOCAL_BUFFER (struct idx_node, nodes, n); |
|
1932 OCTAVE_LOCAL_BUFFER (octave_idx_type, start_nodes, nr); |
|
1933 |
|
1934 for (octave_idx_type i = 0; i < nr; i++) |
|
1935 start_nodes[i] = -1; |
|
1936 |
|
1937 for (octave_idx_type i = 0; i < n; i++) |
|
1938 { |
|
1939 octave_idx_type ii = idx_i.elem (i); |
|
1940 nodes[i].i = i; |
|
1941 nodes[i].next = 0; |
|
1942 |
|
1943 octave_idx_type node = start_nodes[ii]; |
|
1944 if (node == -1) |
|
1945 start_nodes[ii] = i; |
|
1946 else |
|
1947 { |
|
1948 struct idx_node inode = nodes[node]; |
|
1949 while (inode.next) |
|
1950 inode = *inode.next; |
|
1951 inode.next = nodes + i; |
|
1952 } |
|
1953 } |
|
1954 |
5681
|
1955 // First count the number of non-zero elements |
|
1956 octave_idx_type new_nzmx = 0; |
|
1957 for (octave_idx_type j = 0; j < m; j++) |
5164
|
1958 { |
5681
|
1959 octave_idx_type jj = idx_j.elem (j); |
6988
|
1960 |
|
1961 if (jj < nc) |
5164
|
1962 { |
6988
|
1963 for (octave_idx_type i = cidx(jj); |
|
1964 i < cidx(jj+1); i++) |
5681
|
1965 { |
6988
|
1966 OCTAVE_QUIT; |
|
1967 |
|
1968 octave_idx_type ii = start_nodes [ridx(i)]; |
|
1969 |
|
1970 if (ii >= 0) |
5681
|
1971 { |
6988
|
1972 struct idx_node inode = nodes[ii]; |
|
1973 |
|
1974 while (true) |
|
1975 { |
|
1976 if (inode.i >= 0 && |
|
1977 idx_i.elem (inode.i) < nc) |
|
1978 new_nzmx ++; |
|
1979 if (inode.next == 0) |
|
1980 break; |
|
1981 else |
|
1982 inode = *inode.next; |
|
1983 } |
5681
|
1984 } |
|
1985 } |
5164
|
1986 } |
|
1987 } |
5681
|
1988 |
6988
|
1989 std::vector<T> X (n); |
5681
|
1990 retval = Sparse<T> (n, m, new_nzmx); |
6988
|
1991 octave_idx_type *ri = retval.xridx (); |
5681
|
1992 |
|
1993 octave_idx_type kk = 0; |
|
1994 retval.xcidx(0) = 0; |
|
1995 for (octave_idx_type j = 0; j < m; j++) |
|
1996 { |
|
1997 octave_idx_type jj = idx_j.elem (j); |
6988
|
1998 if (jj < nc) |
5681
|
1999 { |
6988
|
2000 for (octave_idx_type i = cidx(jj); |
|
2001 i < cidx(jj+1); i++) |
5681
|
2002 { |
6988
|
2003 OCTAVE_QUIT; |
|
2004 |
|
2005 octave_idx_type ii = start_nodes [ridx(i)]; |
|
2006 |
|
2007 if (ii >= 0) |
5681
|
2008 { |
6988
|
2009 struct idx_node inode = nodes[ii]; |
|
2010 |
|
2011 while (true) |
5681
|
2012 { |
6988
|
2013 if (inode.i >= 0 && |
|
2014 idx_i.elem (inode.i) < nc) |
|
2015 { |
|
2016 X [inode.i] = data (i); |
|
2017 retval.xridx (kk++) = inode.i; |
|
2018 } |
|
2019 |
|
2020 if (inode.next == 0) |
|
2021 break; |
|
2022 else |
|
2023 inode = *inode.next; |
5681
|
2024 } |
|
2025 } |
|
2026 } |
6988
|
2027 sort.sort (ri + retval.xcidx (j), |
|
2028 kk - retval.xcidx (j)); |
|
2029 for (octave_idx_type p = retval.xcidx (j); |
|
2030 p < kk; p++) |
|
2031 retval.xdata (p) = X [retval.xridx (p)]; |
|
2032 retval.xcidx(j+1) = kk; |
5681
|
2033 } |
|
2034 } |
5164
|
2035 } |
|
2036 } |
|
2037 } |
|
2038 } |
|
2039 // idx_vector::freeze() printed an error message for us. |
|
2040 |
|
2041 return retval; |
|
2042 } |
|
2043 |
|
2044 template <class T> |
|
2045 Sparse<T> |
|
2046 Sparse<T>::index (Array<idx_vector>& ra_idx, int resize_ok) const |
|
2047 { |
|
2048 |
|
2049 if (ra_idx.length () != 2) |
|
2050 { |
|
2051 (*current_liboctave_error_handler) ("range error for index"); |
|
2052 return *this; |
|
2053 } |
|
2054 |
|
2055 return index (ra_idx (0), ra_idx (1), resize_ok); |
|
2056 } |
|
2057 |
5775
|
2058 // FIXME |
5164
|
2059 // Unfortunately numel can overflow for very large but very sparse matrices. |
|
2060 // For now just flag an error when this happens. |
|
2061 template <class LT, class RT> |
|
2062 int |
|
2063 assign1 (Sparse<LT>& lhs, const Sparse<RT>& rhs) |
|
2064 { |
|
2065 int retval = 1; |
|
2066 |
|
2067 idx_vector *idx_tmp = lhs.get_idx (); |
|
2068 |
|
2069 idx_vector lhs_idx = idx_tmp[0]; |
|
2070 |
5275
|
2071 octave_idx_type lhs_len = lhs.numel (); |
|
2072 octave_idx_type rhs_len = rhs.numel (); |
5164
|
2073 |
5828
|
2074 uint64_t long_lhs_len = |
|
2075 static_cast<uint64_t> (lhs.rows ()) * |
|
2076 static_cast<uint64_t> (lhs.cols ()); |
|
2077 |
|
2078 uint64_t long_rhs_len = |
|
2079 static_cast<uint64_t> (rhs.rows ()) * |
|
2080 static_cast<uint64_t> (rhs.cols ()); |
|
2081 |
|
2082 if (long_rhs_len != static_cast<uint64_t>(rhs_len) || |
|
2083 long_lhs_len != static_cast<uint64_t>(lhs_len)) |
5164
|
2084 { |
|
2085 (*current_liboctave_error_handler) |
|
2086 ("A(I) = X: Matrix dimensions too large to ensure correct\n", |
|
2087 "operation. This is an limitation that should be removed\n", |
|
2088 "in the future."); |
|
2089 |
|
2090 lhs.clear_index (); |
|
2091 return 0; |
|
2092 } |
|
2093 |
5275
|
2094 octave_idx_type nr = lhs.rows (); |
|
2095 octave_idx_type nc = lhs.cols (); |
5681
|
2096 octave_idx_type nz = lhs.nnz (); |
5275
|
2097 |
5781
|
2098 octave_idx_type n = lhs_idx.freeze (lhs_len, "vector", true); |
5164
|
2099 |
|
2100 if (n != 0) |
|
2101 { |
5275
|
2102 octave_idx_type max_idx = lhs_idx.max () + 1; |
5164
|
2103 max_idx = max_idx < lhs_len ? lhs_len : max_idx; |
|
2104 |
|
2105 // Take a constant copy of lhs. This means that elem won't |
|
2106 // create missing elements. |
|
2107 const Sparse<LT> c_lhs (lhs); |
|
2108 |
|
2109 if (rhs_len == n) |
|
2110 { |
5681
|
2111 octave_idx_type new_nzmx = lhs.nnz (); |
5164
|
2112 |
5603
|
2113 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx, n); |
|
2114 if (! lhs_idx.is_colon ()) |
|
2115 { |
|
2116 // Ok here we have to be careful with the indexing, |
|
2117 // to treat cases like "a([3,2,1]) = b", and still |
|
2118 // handle the need for strict sorting of the sparse |
|
2119 // elements. |
|
2120 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, sidx, n); |
|
2121 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, sidxX, n); |
|
2122 |
|
2123 for (octave_idx_type i = 0; i < n; i++) |
|
2124 { |
|
2125 sidx[i] = &sidxX[i]; |
|
2126 sidx[i]->i = lhs_idx.elem(i); |
|
2127 sidx[i]->idx = i; |
|
2128 } |
|
2129 |
|
2130 OCTAVE_QUIT; |
|
2131 octave_sort<octave_idx_vector_sort *> |
|
2132 sort (octave_idx_vector_comp); |
|
2133 |
|
2134 sort.sort (sidx, n); |
|
2135 |
|
2136 intNDArray<octave_idx_type> new_idx (dim_vector (n,1)); |
|
2137 |
|
2138 for (octave_idx_type i = 0; i < n; i++) |
|
2139 { |
|
2140 new_idx.xelem(i) = sidx[i]->i + 1; |
|
2141 rhs_idx[i] = sidx[i]->idx; |
|
2142 } |
|
2143 |
|
2144 lhs_idx = idx_vector (new_idx); |
|
2145 } |
|
2146 else |
|
2147 for (octave_idx_type i = 0; i < n; i++) |
|
2148 rhs_idx[i] = i; |
|
2149 |
5164
|
2150 // First count the number of non-zero elements |
5275
|
2151 for (octave_idx_type i = 0; i < n; i++) |
5164
|
2152 { |
|
2153 OCTAVE_QUIT; |
|
2154 |
5275
|
2155 octave_idx_type ii = lhs_idx.elem (i); |
5164
|
2156 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
5604
|
2157 new_nzmx--; |
5603
|
2158 if (rhs.elem(rhs_idx[i]) != RT ()) |
5604
|
2159 new_nzmx++; |
5164
|
2160 } |
|
2161 |
|
2162 if (nr > 1) |
|
2163 { |
5604
|
2164 Sparse<LT> tmp (max_idx, 1, new_nzmx); |
5164
|
2165 tmp.cidx(0) = 0; |
5681
|
2166 tmp.cidx(1) = new_nzmx; |
5164
|
2167 |
5275
|
2168 octave_idx_type i = 0; |
|
2169 octave_idx_type ii = 0; |
5164
|
2170 if (i < nz) |
|
2171 ii = c_lhs.ridx(i); |
|
2172 |
5275
|
2173 octave_idx_type j = 0; |
|
2174 octave_idx_type jj = lhs_idx.elem(j); |
|
2175 |
|
2176 octave_idx_type kk = 0; |
5164
|
2177 |
|
2178 while (j < n || i < nz) |
|
2179 { |
|
2180 if (j == n || (i < nz && ii < jj)) |
|
2181 { |
|
2182 tmp.xdata (kk) = c_lhs.data (i); |
|
2183 tmp.xridx (kk++) = ii; |
|
2184 if (++i < nz) |
|
2185 ii = c_lhs.ridx(i); |
|
2186 } |
|
2187 else |
|
2188 { |
5603
|
2189 RT rtmp = rhs.elem (rhs_idx[j]); |
5164
|
2190 if (rtmp != RT ()) |
|
2191 { |
|
2192 tmp.xdata (kk) = rtmp; |
|
2193 tmp.xridx (kk++) = jj; |
|
2194 } |
|
2195 |
|
2196 if (ii == jj && i < nz) |
|
2197 if (++i < nz) |
|
2198 ii = c_lhs.ridx(i); |
|
2199 if (++j < n) |
|
2200 jj = lhs_idx.elem(j); |
|
2201 } |
|
2202 } |
|
2203 |
|
2204 lhs = tmp; |
|
2205 } |
|
2206 else |
|
2207 { |
5604
|
2208 Sparse<LT> tmp (1, max_idx, new_nzmx); |
5164
|
2209 |
5275
|
2210 octave_idx_type i = 0; |
|
2211 octave_idx_type ii = 0; |
5164
|
2212 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2213 ii++; |
|
2214 |
5275
|
2215 octave_idx_type j = 0; |
|
2216 octave_idx_type jj = lhs_idx.elem(j); |
|
2217 |
|
2218 octave_idx_type kk = 0; |
|
2219 octave_idx_type ic = 0; |
5164
|
2220 |
|
2221 while (j < n || i < nz) |
|
2222 { |
|
2223 if (j == n || (i < nz && ii < jj)) |
|
2224 { |
|
2225 while (ic <= ii) |
|
2226 tmp.xcidx (ic++) = kk; |
|
2227 tmp.xdata (kk) = c_lhs.data (i); |
5603
|
2228 tmp.xridx (kk++) = 0; |
5164
|
2229 i++; |
|
2230 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2231 ii++; |
|
2232 } |
|
2233 else |
|
2234 { |
|
2235 while (ic <= jj) |
|
2236 tmp.xcidx (ic++) = kk; |
|
2237 |
5603
|
2238 RT rtmp = rhs.elem (rhs_idx[j]); |
5164
|
2239 if (rtmp != RT ()) |
5603
|
2240 { |
|
2241 tmp.xdata (kk) = rtmp; |
|
2242 tmp.xridx (kk++) = 0; |
|
2243 } |
5164
|
2244 if (ii == jj) |
|
2245 { |
|
2246 i++; |
|
2247 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2248 ii++; |
|
2249 } |
|
2250 j++; |
|
2251 if (j < n) |
|
2252 jj = lhs_idx.elem(j); |
|
2253 } |
|
2254 } |
|
2255 |
5275
|
2256 for (octave_idx_type iidx = ic; iidx < max_idx+1; iidx++) |
5164
|
2257 tmp.xcidx(iidx) = kk; |
|
2258 |
|
2259 lhs = tmp; |
|
2260 } |
|
2261 } |
|
2262 else if (rhs_len == 1) |
|
2263 { |
5681
|
2264 octave_idx_type new_nzmx = lhs.nnz (); |
5164
|
2265 RT scalar = rhs.elem (0); |
|
2266 bool scalar_non_zero = (scalar != RT ()); |
5603
|
2267 lhs_idx.sort (true); |
5164
|
2268 |
|
2269 // First count the number of non-zero elements |
|
2270 if (scalar != RT ()) |
5604
|
2271 new_nzmx += n; |
5275
|
2272 for (octave_idx_type i = 0; i < n; i++) |
5164
|
2273 { |
|
2274 OCTAVE_QUIT; |
|
2275 |
5275
|
2276 octave_idx_type ii = lhs_idx.elem (i); |
5164
|
2277 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
5604
|
2278 new_nzmx--; |
5164
|
2279 } |
|
2280 |
|
2281 if (nr > 1) |
|
2282 { |
5604
|
2283 Sparse<LT> tmp (max_idx, 1, new_nzmx); |
5164
|
2284 tmp.cidx(0) = 0; |
5681
|
2285 tmp.cidx(1) = new_nzmx; |
5164
|
2286 |
5275
|
2287 octave_idx_type i = 0; |
|
2288 octave_idx_type ii = 0; |
5164
|
2289 if (i < nz) |
|
2290 ii = c_lhs.ridx(i); |
|
2291 |
5275
|
2292 octave_idx_type j = 0; |
|
2293 octave_idx_type jj = lhs_idx.elem(j); |
|
2294 |
|
2295 octave_idx_type kk = 0; |
5164
|
2296 |
|
2297 while (j < n || i < nz) |
|
2298 { |
|
2299 if (j == n || (i < nz && ii < jj)) |
|
2300 { |
|
2301 tmp.xdata (kk) = c_lhs.data (i); |
|
2302 tmp.xridx (kk++) = ii; |
|
2303 if (++i < nz) |
|
2304 ii = c_lhs.ridx(i); |
|
2305 } |
|
2306 else |
|
2307 { |
|
2308 if (scalar_non_zero) |
|
2309 { |
|
2310 tmp.xdata (kk) = scalar; |
|
2311 tmp.xridx (kk++) = jj; |
|
2312 } |
|
2313 |
|
2314 if (ii == jj && i < nz) |
|
2315 if (++i < nz) |
|
2316 ii = c_lhs.ridx(i); |
|
2317 if (++j < n) |
|
2318 jj = lhs_idx.elem(j); |
|
2319 } |
|
2320 } |
|
2321 |
|
2322 lhs = tmp; |
|
2323 } |
|
2324 else |
|
2325 { |
5604
|
2326 Sparse<LT> tmp (1, max_idx, new_nzmx); |
5164
|
2327 |
5275
|
2328 octave_idx_type i = 0; |
|
2329 octave_idx_type ii = 0; |
5164
|
2330 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2331 ii++; |
|
2332 |
5275
|
2333 octave_idx_type j = 0; |
|
2334 octave_idx_type jj = lhs_idx.elem(j); |
|
2335 |
|
2336 octave_idx_type kk = 0; |
|
2337 octave_idx_type ic = 0; |
5164
|
2338 |
|
2339 while (j < n || i < nz) |
|
2340 { |
|
2341 if (j == n || (i < nz && ii < jj)) |
|
2342 { |
|
2343 while (ic <= ii) |
|
2344 tmp.xcidx (ic++) = kk; |
|
2345 tmp.xdata (kk) = c_lhs.data (i); |
|
2346 i++; |
|
2347 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2348 ii++; |
|
2349 } |
|
2350 else |
|
2351 { |
|
2352 while (ic <= jj) |
|
2353 tmp.xcidx (ic++) = kk; |
|
2354 if (scalar_non_zero) |
|
2355 tmp.xdata (kk) = scalar; |
|
2356 if (ii == jj) |
|
2357 { |
|
2358 i++; |
|
2359 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2360 ii++; |
|
2361 } |
|
2362 j++; |
|
2363 if (j < n) |
|
2364 jj = lhs_idx.elem(j); |
|
2365 } |
|
2366 tmp.xridx (kk++) = 0; |
|
2367 } |
|
2368 |
5275
|
2369 for (octave_idx_type iidx = ic; iidx < max_idx+1; iidx++) |
5164
|
2370 tmp.xcidx(iidx) = kk; |
|
2371 |
|
2372 lhs = tmp; |
|
2373 } |
|
2374 } |
|
2375 else |
|
2376 { |
|
2377 (*current_liboctave_error_handler) |
|
2378 ("A(I) = X: X must be a scalar or a vector with same length as I"); |
|
2379 |
|
2380 retval = 0; |
|
2381 } |
|
2382 } |
|
2383 else if (lhs_idx.is_colon ()) |
|
2384 { |
|
2385 if (lhs_len == 0) |
|
2386 { |
|
2387 |
5681
|
2388 octave_idx_type new_nzmx = rhs.nnz (); |
5604
|
2389 Sparse<LT> tmp (1, rhs_len, new_nzmx); |
5164
|
2390 |
5275
|
2391 octave_idx_type ii = 0; |
|
2392 octave_idx_type jj = 0; |
|
2393 for (octave_idx_type i = 0; i < rhs.cols(); i++) |
|
2394 for (octave_idx_type j = rhs.cidx(i); j < rhs.cidx(i+1); j++) |
5164
|
2395 { |
|
2396 OCTAVE_QUIT; |
5275
|
2397 for (octave_idx_type k = jj; k <= i * rhs.rows() + rhs.ridx(j); k++) |
5164
|
2398 tmp.cidx(jj++) = ii; |
|
2399 |
|
2400 tmp.data(ii) = rhs.data(j); |
|
2401 tmp.ridx(ii++) = 0; |
|
2402 } |
|
2403 |
5275
|
2404 for (octave_idx_type i = jj; i < rhs_len + 1; i++) |
5164
|
2405 tmp.cidx(i) = ii; |
|
2406 |
|
2407 lhs = tmp; |
|
2408 } |
|
2409 else |
|
2410 (*current_liboctave_error_handler) |
|
2411 ("A(:) = X: A must be the same size as X"); |
|
2412 } |
|
2413 else if (! (rhs_len == 1 || rhs_len == 0)) |
|
2414 { |
|
2415 (*current_liboctave_error_handler) |
|
2416 ("A([]) = X: X must also be an empty matrix or a scalar"); |
|
2417 |
|
2418 retval = 0; |
|
2419 } |
|
2420 |
|
2421 lhs.clear_index (); |
|
2422 |
|
2423 return retval; |
|
2424 } |
|
2425 |
|
2426 template <class LT, class RT> |
|
2427 int |
|
2428 assign (Sparse<LT>& lhs, const Sparse<RT>& rhs) |
|
2429 { |
|
2430 int retval = 1; |
|
2431 |
|
2432 int n_idx = lhs.index_count (); |
|
2433 |
5275
|
2434 octave_idx_type lhs_nr = lhs.rows (); |
|
2435 octave_idx_type lhs_nc = lhs.cols (); |
5681
|
2436 octave_idx_type lhs_nz = lhs.nnz (); |
5275
|
2437 |
|
2438 octave_idx_type rhs_nr = rhs.rows (); |
|
2439 octave_idx_type rhs_nc = rhs.cols (); |
5164
|
2440 |
|
2441 idx_vector *tmp = lhs.get_idx (); |
|
2442 |
|
2443 idx_vector idx_i; |
|
2444 idx_vector idx_j; |
|
2445 |
|
2446 if (n_idx > 2) |
|
2447 { |
|
2448 (*current_liboctave_error_handler) |
|
2449 ("A(I, J) = X: can only have 1 or 2 indexes for sparse matrices"); |
6092
|
2450 |
|
2451 lhs.clear_index (); |
5164
|
2452 return 0; |
|
2453 } |
|
2454 |
|
2455 if (n_idx > 1) |
|
2456 idx_j = tmp[1]; |
|
2457 |
|
2458 if (n_idx > 0) |
|
2459 idx_i = tmp[0]; |
|
2460 |
6988
|
2461 // Take a constant copy of lhs. This means that ridx and family won't |
|
2462 // call make_unique. |
|
2463 const Sparse<LT> c_lhs (lhs); |
|
2464 |
5164
|
2465 if (n_idx == 2) |
|
2466 { |
5781
|
2467 octave_idx_type n = idx_i.freeze (lhs_nr, "row", true); |
|
2468 octave_idx_type m = idx_j.freeze (lhs_nc, "column", true); |
5164
|
2469 |
|
2470 int idx_i_is_colon = idx_i.is_colon (); |
|
2471 int idx_j_is_colon = idx_j.is_colon (); |
|
2472 |
|
2473 if (idx_i_is_colon) |
|
2474 n = lhs_nr > 0 ? lhs_nr : rhs_nr; |
|
2475 |
|
2476 if (idx_j_is_colon) |
|
2477 m = lhs_nc > 0 ? lhs_nc : rhs_nc; |
|
2478 |
|
2479 if (idx_i && idx_j) |
|
2480 { |
|
2481 if (rhs_nr == 0 && rhs_nc == 0) |
|
2482 { |
|
2483 lhs.maybe_delete_elements (idx_i, idx_j); |
|
2484 } |
|
2485 else |
|
2486 { |
|
2487 if (rhs_nr == 1 && rhs_nc == 1 && n >= 0 && m >= 0) |
|
2488 { |
|
2489 // No need to do anything if either of the indices |
|
2490 // are empty. |
|
2491 |
|
2492 if (n > 0 && m > 0) |
|
2493 { |
5603
|
2494 idx_i.sort (true); |
|
2495 idx_j.sort (true); |
|
2496 |
5275
|
2497 octave_idx_type max_row_idx = idx_i_is_colon ? rhs_nr : |
5164
|
2498 idx_i.max () + 1; |
5275
|
2499 octave_idx_type max_col_idx = idx_j_is_colon ? rhs_nc : |
5164
|
2500 idx_j.max () + 1; |
5603
|
2501 octave_idx_type new_nr = max_row_idx > lhs_nr ? |
|
2502 max_row_idx : lhs_nr; |
|
2503 octave_idx_type new_nc = max_col_idx > lhs_nc ? |
|
2504 max_col_idx : lhs_nc; |
5164
|
2505 RT scalar = rhs.elem (0, 0); |
|
2506 |
|
2507 // Count the number of non-zero terms |
5681
|
2508 octave_idx_type new_nzmx = lhs.nnz (); |
5275
|
2509 for (octave_idx_type j = 0; j < m; j++) |
5164
|
2510 { |
5275
|
2511 octave_idx_type jj = idx_j.elem (j); |
5164
|
2512 if (jj < lhs_nc) |
|
2513 { |
5275
|
2514 for (octave_idx_type i = 0; i < n; i++) |
5164
|
2515 { |
|
2516 OCTAVE_QUIT; |
|
2517 |
5275
|
2518 octave_idx_type ii = idx_i.elem (i); |
5164
|
2519 |
|
2520 if (ii < lhs_nr) |
|
2521 { |
6988
|
2522 for (octave_idx_type k = c_lhs.cidx(jj); |
|
2523 k < c_lhs.cidx(jj+1); k++) |
5164
|
2524 { |
6988
|
2525 if (c_lhs.ridx(k) == ii) |
5604
|
2526 new_nzmx--; |
6988
|
2527 if (c_lhs.ridx(k) >= ii) |
5164
|
2528 break; |
|
2529 } |
|
2530 } |
|
2531 } |
|
2532 } |
|
2533 } |
|
2534 |
|
2535 if (scalar != RT()) |
5604
|
2536 new_nzmx += m * n; |
|
2537 |
|
2538 Sparse<LT> stmp (new_nr, new_nc, new_nzmx); |
5164
|
2539 |
5275
|
2540 octave_idx_type jji = 0; |
|
2541 octave_idx_type jj = idx_j.elem (jji); |
|
2542 octave_idx_type kk = 0; |
5164
|
2543 stmp.cidx(0) = 0; |
5275
|
2544 for (octave_idx_type j = 0; j < new_nc; j++) |
5164
|
2545 { |
|
2546 if (jji < m && jj == j) |
|
2547 { |
5275
|
2548 octave_idx_type iii = 0; |
|
2549 octave_idx_type ii = idx_i.elem (iii); |
5760
|
2550 octave_idx_type ppp = 0; |
6092
|
2551 octave_idx_type ppi = (j >= lhs_nc ? 0 : |
6988
|
2552 c_lhs.cidx(j+1) - |
|
2553 c_lhs.cidx(j)); |
5760
|
2554 octave_idx_type pp = (ppp < ppi ? |
6988
|
2555 c_lhs.ridx(c_lhs.cidx(j)+ppp) : |
|
2556 new_nr); |
5760
|
2557 while (ppp < ppi || iii < n) |
5164
|
2558 { |
5760
|
2559 if (iii < n && ii <= pp) |
5164
|
2560 { |
|
2561 if (scalar != RT ()) |
|
2562 { |
|
2563 stmp.data(kk) = scalar; |
5760
|
2564 stmp.ridx(kk++) = ii; |
5164
|
2565 } |
5760
|
2566 if (ii == pp) |
6988
|
2567 pp = (++ppp < ppi ? c_lhs.ridx(c_lhs.cidx(j)+ppp) : new_nr); |
5164
|
2568 if (++iii < n) |
|
2569 ii = idx_i.elem(iii); |
|
2570 } |
5760
|
2571 else |
5164
|
2572 { |
5760
|
2573 stmp.data(kk) = |
6988
|
2574 c_lhs.data(c_lhs.cidx(j)+ppp); |
5760
|
2575 stmp.ridx(kk++) = pp; |
6988
|
2576 pp = (++ppp < ppi ? c_lhs.ridx(c_lhs.cidx(j)+ppp) : new_nr); |
5164
|
2577 } |
|
2578 } |
|
2579 if (++jji < m) |
|
2580 jj = idx_j.elem(jji); |
|
2581 } |
6988
|
2582 else if (j < lhs_nc) |
5164
|
2583 { |
6988
|
2584 for (octave_idx_type i = c_lhs.cidx(j); |
|
2585 i < c_lhs.cidx(j+1); i++) |
5164
|
2586 { |
6988
|
2587 stmp.data(kk) = c_lhs.data(i); |
|
2588 stmp.ridx(kk++) = c_lhs.ridx(i); |
5164
|
2589 } |
|
2590 } |
|
2591 stmp.cidx(j+1) = kk; |
|
2592 } |
|
2593 |
|
2594 lhs = stmp; |
|
2595 } |
|
2596 } |
|
2597 else if (n == rhs_nr && m == rhs_nc) |
|
2598 { |
|
2599 if (n > 0 && m > 0) |
|
2600 { |
5275
|
2601 octave_idx_type max_row_idx = idx_i_is_colon ? rhs_nr : |
5164
|
2602 idx_i.max () + 1; |
5275
|
2603 octave_idx_type max_col_idx = idx_j_is_colon ? rhs_nc : |
5164
|
2604 idx_j.max () + 1; |
5603
|
2605 octave_idx_type new_nr = max_row_idx > lhs_nr ? |
|
2606 max_row_idx : lhs_nr; |
|
2607 octave_idx_type new_nc = max_col_idx > lhs_nc ? |
|
2608 max_col_idx : lhs_nc; |
|
2609 |
|
2610 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx_i, n); |
|
2611 if (! idx_i.is_colon ()) |
|
2612 { |
|
2613 // Ok here we have to be careful with the indexing, |
|
2614 // to treat cases like "a([3,2,1],:) = b", and still |
|
2615 // handle the need for strict sorting of the sparse |
|
2616 // elements. |
|
2617 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, |
|
2618 sidx, n); |
|
2619 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, |
|
2620 sidxX, n); |
|
2621 |
|
2622 for (octave_idx_type i = 0; i < n; i++) |
|
2623 { |
|
2624 sidx[i] = &sidxX[i]; |
|
2625 sidx[i]->i = idx_i.elem(i); |
|
2626 sidx[i]->idx = i; |
|
2627 } |
|
2628 |
|
2629 OCTAVE_QUIT; |
|
2630 octave_sort<octave_idx_vector_sort *> |
|
2631 sort (octave_idx_vector_comp); |
|
2632 |
|
2633 sort.sort (sidx, n); |
|
2634 |
|
2635 intNDArray<octave_idx_type> new_idx (dim_vector (n,1)); |
|
2636 |
|
2637 for (octave_idx_type i = 0; i < n; i++) |
|
2638 { |
|
2639 new_idx.xelem(i) = sidx[i]->i + 1; |
|
2640 rhs_idx_i[i] = sidx[i]->idx; |
|
2641 } |
|
2642 |
|
2643 idx_i = idx_vector (new_idx); |
|
2644 } |
|
2645 else |
|
2646 for (octave_idx_type i = 0; i < n; i++) |
|
2647 rhs_idx_i[i] = i; |
|
2648 |
|
2649 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx_j, m); |
|
2650 if (! idx_j.is_colon ()) |
|
2651 { |
|
2652 // Ok here we have to be careful with the indexing, |
|
2653 // to treat cases like "a([3,2,1],:) = b", and still |
|
2654 // handle the need for strict sorting of the sparse |
|
2655 // elements. |
|
2656 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, |
|
2657 sidx, m); |
|
2658 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, |
|
2659 sidxX, m); |
|
2660 |
|
2661 for (octave_idx_type i = 0; i < m; i++) |
|
2662 { |
|
2663 sidx[i] = &sidxX[i]; |
|
2664 sidx[i]->i = idx_j.elem(i); |
|
2665 sidx[i]->idx = i; |
|
2666 } |
|
2667 |
|
2668 OCTAVE_QUIT; |
|
2669 octave_sort<octave_idx_vector_sort *> |
|
2670 sort (octave_idx_vector_comp); |
|
2671 |
|
2672 sort.sort (sidx, m); |
|
2673 |
|
2674 intNDArray<octave_idx_type> new_idx (dim_vector (m,1)); |
|
2675 |
|
2676 for (octave_idx_type i = 0; i < m; i++) |
|
2677 { |
|
2678 new_idx.xelem(i) = sidx[i]->i + 1; |
|
2679 rhs_idx_j[i] = sidx[i]->idx; |
|
2680 } |
|
2681 |
|
2682 idx_j = idx_vector (new_idx); |
|
2683 } |
|
2684 else |
|
2685 for (octave_idx_type i = 0; i < m; i++) |
|
2686 rhs_idx_j[i] = i; |
5164
|
2687 |
5760
|
2688 // Maximum number of non-zero elements |
|
2689 octave_idx_type new_nzmx = lhs.nnz() + rhs.nnz(); |
5164
|
2690 |
5604
|
2691 Sparse<LT> stmp (new_nr, new_nc, new_nzmx); |
5164
|
2692 |
5275
|
2693 octave_idx_type jji = 0; |
|
2694 octave_idx_type jj = idx_j.elem (jji); |
|
2695 octave_idx_type kk = 0; |
5164
|
2696 stmp.cidx(0) = 0; |
5275
|
2697 for (octave_idx_type j = 0; j < new_nc; j++) |
5164
|
2698 { |
|
2699 if (jji < m && jj == j) |
|
2700 { |
5275
|
2701 octave_idx_type iii = 0; |
|
2702 octave_idx_type ii = idx_i.elem (iii); |
5760
|
2703 octave_idx_type ppp = 0; |
6092
|
2704 octave_idx_type ppi = (j >= lhs_nc ? 0 : |
6988
|
2705 c_lhs.cidx(j+1) - |
|
2706 c_lhs.cidx(j)); |
5760
|
2707 octave_idx_type pp = (ppp < ppi ? |
6988
|
2708 c_lhs.ridx(c_lhs.cidx(j)+ppp) : |
|
2709 new_nr); |
5760
|
2710 while (ppp < ppi || iii < n) |
5164
|
2711 { |
5760
|
2712 if (iii < n && ii <= pp) |
5164
|
2713 { |
5603
|
2714 RT rtmp = rhs.elem (rhs_idx_i[iii], |
|
2715 rhs_idx_j[jji]); |
5164
|
2716 if (rtmp != RT ()) |
|
2717 { |
|
2718 stmp.data(kk) = rtmp; |
5760
|
2719 stmp.ridx(kk++) = ii; |
5164
|
2720 } |
5760
|
2721 if (ii == pp) |
6988
|
2722 pp = (++ppp < ppi ? c_lhs.ridx(c_lhs.cidx(j)+ppp) : new_nr); |
5164
|
2723 if (++iii < n) |
|
2724 ii = idx_i.elem(iii); |
|
2725 } |
5760
|
2726 else |
5164
|
2727 { |
5760
|
2728 stmp.data(kk) = |
6988
|
2729 c_lhs.data(c_lhs.cidx(j)+ppp); |
5760
|
2730 stmp.ridx(kk++) = pp; |
6988
|
2731 pp = (++ppp < ppi ? c_lhs.ridx(c_lhs.cidx(j)+ppp) : new_nr); |
5164
|
2732 } |
|
2733 } |
|
2734 if (++jji < m) |
|
2735 jj = idx_j.elem(jji); |
|
2736 } |
6988
|
2737 else if (j < lhs_nc) |
5164
|
2738 { |
6988
|
2739 for (octave_idx_type i = c_lhs.cidx(j); |
|
2740 i < c_lhs.cidx(j+1); i++) |
5164
|
2741 { |
6988
|
2742 stmp.data(kk) = c_lhs.data(i); |
|
2743 stmp.ridx(kk++) = c_lhs.ridx(i); |
5164
|
2744 } |
|
2745 } |
|
2746 stmp.cidx(j+1) = kk; |
|
2747 } |
|
2748 |
5760
|
2749 stmp.maybe_compress(); |
5164
|
2750 lhs = stmp; |
|
2751 } |
|
2752 } |
|
2753 else if (n == 0 && m == 0) |
|
2754 { |
|
2755 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2756 || (rhs_nr == 0 || rhs_nc == 0))) |
|
2757 { |
|
2758 (*current_liboctave_error_handler) |
|
2759 ("A([], []) = X: X must be an empty matrix or a scalar"); |
|
2760 |
|
2761 retval = 0; |
|
2762 } |
|
2763 } |
|
2764 else |
|
2765 { |
|
2766 (*current_liboctave_error_handler) |
|
2767 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
|
2768 (*current_liboctave_error_handler) |
|
2769 ("match the number of rows in X and the number of elements in J must"); |
|
2770 (*current_liboctave_error_handler) |
|
2771 ("match the number of columns in X"); |
|
2772 |
|
2773 retval = 0; |
|
2774 } |
|
2775 } |
|
2776 } |
|
2777 // idx_vector::freeze() printed an error message for us. |
|
2778 } |
|
2779 else if (n_idx == 1) |
|
2780 { |
|
2781 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; |
|
2782 |
|
2783 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) |
|
2784 { |
5275
|
2785 octave_idx_type lhs_len = lhs.length (); |
|
2786 |
5781
|
2787 octave_idx_type n = idx_i.freeze (lhs_len, 0, true); |
5164
|
2788 |
|
2789 if (idx_i) |
|
2790 { |
|
2791 if (rhs_nr == 0 && rhs_nc == 0) |
|
2792 { |
|
2793 if (n != 0 && (lhs_nr != 0 || lhs_nc != 0)) |
|
2794 lhs.maybe_delete_elements (idx_i); |
|
2795 } |
|
2796 else |
|
2797 { |
5781
|
2798 if (lhs_is_empty |
|
2799 && idx_i.is_colon () |
|
2800 && ! (rhs_nr == 1 || rhs_nc == 1)) |
5164
|
2801 { |
5781
|
2802 (*current_liboctave_warning_with_id_handler) |
|
2803 ("Octave:fortran-indexing", |
|
2804 "A(:) = X: X is not a vector or scalar"); |
|
2805 } |
|
2806 else |
|
2807 { |
|
2808 octave_idx_type idx_nr = idx_i.orig_rows (); |
|
2809 octave_idx_type idx_nc = idx_i.orig_columns (); |
|
2810 |
|
2811 if (! (rhs_nr == idx_nr && rhs_nc == idx_nc)) |
|
2812 (*current_liboctave_warning_with_id_handler) |
|
2813 ("Octave:fortran-indexing", |
|
2814 "A(I) = X: X does not have same shape as I"); |
5164
|
2815 } |
|
2816 |
5760
|
2817 if (! assign1 (lhs, rhs)) |
5164
|
2818 retval = 0; |
|
2819 } |
|
2820 } |
|
2821 // idx_vector::freeze() printed an error message for us. |
|
2822 } |
|
2823 else if (lhs_nr == 1) |
|
2824 { |
5781
|
2825 idx_i.freeze (lhs_nc, "vector", true); |
5164
|
2826 |
|
2827 if (idx_i) |
|
2828 { |
|
2829 if (rhs_nr == 0 && rhs_nc == 0) |
|
2830 lhs.maybe_delete_elements (idx_i); |
5760
|
2831 else if (! assign1 (lhs, rhs)) |
5164
|
2832 retval = 0; |
|
2833 } |
|
2834 // idx_vector::freeze() printed an error message for us. |
|
2835 } |
|
2836 else if (lhs_nc == 1) |
|
2837 { |
5781
|
2838 idx_i.freeze (lhs_nr, "vector", true); |
5164
|
2839 |
|
2840 if (idx_i) |
|
2841 { |
|
2842 if (rhs_nr == 0 && rhs_nc == 0) |
|
2843 lhs.maybe_delete_elements (idx_i); |
5760
|
2844 else if (! assign1 (lhs, rhs)) |
5164
|
2845 retval = 0; |
|
2846 } |
|
2847 // idx_vector::freeze() printed an error message for us. |
|
2848 } |
|
2849 else |
|
2850 { |
5781
|
2851 if (! (idx_i.is_colon () |
|
2852 || (idx_i.one_zero_only () |
|
2853 && idx_i.orig_rows () == lhs_nr |
|
2854 && idx_i.orig_columns () == lhs_nc))) |
|
2855 (*current_liboctave_warning_with_id_handler) |
|
2856 ("Octave:fortran-indexing", "single index used for matrix"); |
5164
|
2857 |
5275
|
2858 octave_idx_type lhs_len = lhs.length (); |
|
2859 |
|
2860 octave_idx_type len = idx_i.freeze (lhs_nr * lhs_nc, "matrix"); |
5164
|
2861 |
|
2862 if (idx_i) |
|
2863 { |
|
2864 if (rhs_nr == 0 && rhs_nc == 0) |
|
2865 lhs.maybe_delete_elements (idx_i); |
|
2866 else if (len == 0) |
|
2867 { |
|
2868 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2869 || (rhs_nr == 0 || rhs_nc == 0))) |
|
2870 (*current_liboctave_error_handler) |
|
2871 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2872 } |
|
2873 else if (len == rhs_nr * rhs_nc) |
|
2874 { |
5604
|
2875 octave_idx_type new_nzmx = lhs_nz; |
5603
|
2876 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx, len); |
|
2877 |
|
2878 if (! idx_i.is_colon ()) |
|
2879 { |
|
2880 // Ok here we have to be careful with the indexing, to |
|
2881 // treat cases like "a([3,2,1]) = b", and still handle |
|
2882 // the need for strict sorting of the sparse elements. |
|
2883 |
|
2884 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, sidx, |
|
2885 len); |
|
2886 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, sidxX, |
|
2887 len); |
|
2888 |
|
2889 for (octave_idx_type i = 0; i < len; i++) |
|
2890 { |
|
2891 sidx[i] = &sidxX[i]; |
|
2892 sidx[i]->i = idx_i.elem(i); |
|
2893 sidx[i]->idx = i; |
|
2894 } |
|
2895 |
|
2896 OCTAVE_QUIT; |
|
2897 octave_sort<octave_idx_vector_sort *> |
|
2898 sort (octave_idx_vector_comp); |
|
2899 |
|
2900 sort.sort (sidx, len); |
|
2901 |
|
2902 intNDArray<octave_idx_type> new_idx (dim_vector (len,1)); |
|
2903 |
|
2904 for (octave_idx_type i = 0; i < len; i++) |
|
2905 { |
|
2906 new_idx.xelem(i) = sidx[i]->i + 1; |
|
2907 rhs_idx[i] = sidx[i]->idx; |
|
2908 } |
|
2909 |
|
2910 idx_i = idx_vector (new_idx); |
|
2911 } |
|
2912 else |
|
2913 for (octave_idx_type i = 0; i < len; i++) |
|
2914 rhs_idx[i] = i; |
5164
|
2915 |
|
2916 // First count the number of non-zero elements |
5275
|
2917 for (octave_idx_type i = 0; i < len; i++) |
5164
|
2918 { |
|
2919 OCTAVE_QUIT; |
|
2920 |
5275
|
2921 octave_idx_type ii = idx_i.elem (i); |
5164
|
2922 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
5604
|
2923 new_nzmx--; |
5603
|
2924 if (rhs.elem(rhs_idx[i]) != RT ()) |
5604
|
2925 new_nzmx++; |
5164
|
2926 } |
|
2927 |
5604
|
2928 Sparse<LT> stmp (lhs_nr, lhs_nc, new_nzmx); |
5164
|
2929 |
5275
|
2930 octave_idx_type i = 0; |
|
2931 octave_idx_type ii = 0; |
|
2932 octave_idx_type ic = 0; |
5164
|
2933 if (i < lhs_nz) |
|
2934 { |
|
2935 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2936 ic++; |
|
2937 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2938 } |
|
2939 |
5275
|
2940 octave_idx_type j = 0; |
|
2941 octave_idx_type jj = idx_i.elem (j); |
|
2942 octave_idx_type jr = jj % lhs_nr; |
|
2943 octave_idx_type jc = (jj - jr) / lhs_nr; |
|
2944 |
|
2945 octave_idx_type kk = 0; |
|
2946 octave_idx_type kc = 0; |
5164
|
2947 |
|
2948 while (j < len || i < lhs_nz) |
|
2949 { |
|
2950 if (j == len || (i < lhs_nz && ii < jj)) |
|
2951 { |
|
2952 while (kc <= ic) |
|
2953 stmp.xcidx (kc++) = kk; |
|
2954 stmp.xdata (kk) = c_lhs.data (i); |
|
2955 stmp.xridx (kk++) = c_lhs.ridx (i); |
|
2956 i++; |
|
2957 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2958 ic++; |
|
2959 if (i < lhs_nz) |
|
2960 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2961 } |
|
2962 else |
|
2963 { |
|
2964 while (kc <= jc) |
|
2965 stmp.xcidx (kc++) = kk; |
5603
|
2966 RT rtmp = rhs.elem (rhs_idx[j]); |
5164
|
2967 if (rtmp != RT ()) |
|
2968 { |
|
2969 stmp.xdata (kk) = rtmp; |
|
2970 stmp.xridx (kk++) = jr; |
|
2971 } |
|
2972 if (ii == jj) |
|
2973 { |
|
2974 i++; |
|
2975 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2976 ic++; |
|
2977 if (i < lhs_nz) |
|
2978 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2979 } |
|
2980 j++; |
|
2981 if (j < len) |
|
2982 { |
|
2983 jj = idx_i.elem (j); |
|
2984 jr = jj % lhs_nr; |
|
2985 jc = (jj - jr) / lhs_nr; |
|
2986 } |
|
2987 } |
|
2988 } |
|
2989 |
5275
|
2990 for (octave_idx_type iidx = kc; iidx < lhs_nc+1; iidx++) |
5603
|
2991 stmp.xcidx(iidx) = kk; |
5164
|
2992 |
|
2993 lhs = stmp; |
|
2994 } |
|
2995 else if (rhs_nr == 1 && rhs_nc == 1) |
|
2996 { |
|
2997 RT scalar = rhs.elem (0, 0); |
5604
|
2998 octave_idx_type new_nzmx = lhs_nz; |
5603
|
2999 idx_i.sort (true); |
5164
|
3000 |
|
3001 // First count the number of non-zero elements |
|
3002 if (scalar != RT ()) |
5604
|
3003 new_nzmx += len; |
5275
|
3004 for (octave_idx_type i = 0; i < len; i++) |
5164
|
3005 { |
|
3006 OCTAVE_QUIT; |
5275
|
3007 octave_idx_type ii = idx_i.elem (i); |
5164
|
3008 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
5604
|
3009 new_nzmx--; |
5164
|
3010 } |
|
3011 |
5604
|
3012 Sparse<LT> stmp (lhs_nr, lhs_nc, new_nzmx); |
5164
|
3013 |
5275
|
3014 octave_idx_type i = 0; |
|
3015 octave_idx_type ii = 0; |
|
3016 octave_idx_type ic = 0; |
5164
|
3017 if (i < lhs_nz) |
|
3018 { |
|
3019 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
3020 ic++; |
|
3021 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
3022 } |
|
3023 |
5275
|
3024 octave_idx_type j = 0; |
|
3025 octave_idx_type jj = idx_i.elem (j); |
|
3026 octave_idx_type jr = jj % lhs_nr; |
|
3027 octave_idx_type jc = (jj - jr) / lhs_nr; |
|
3028 |
|
3029 octave_idx_type kk = 0; |
|
3030 octave_idx_type kc = 0; |
5164
|
3031 |
|
3032 while (j < len || i < lhs_nz) |
|
3033 { |
|
3034 if (j == len || (i < lhs_nz && ii < jj)) |
|
3035 { |
|
3036 while (kc <= ic) |
|
3037 stmp.xcidx (kc++) = kk; |
|
3038 stmp.xdata (kk) = c_lhs.data (i); |
|
3039 stmp.xridx (kk++) = c_lhs.ridx (i); |
|
3040 i++; |
|
3041 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
3042 ic++; |
|
3043 if (i < lhs_nz) |
|
3044 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
3045 } |
|
3046 else |
|
3047 { |
|
3048 while (kc <= jc) |
|
3049 stmp.xcidx (kc++) = kk; |
|
3050 if (scalar != RT ()) |
|
3051 { |
|
3052 stmp.xdata (kk) = scalar; |
|
3053 stmp.xridx (kk++) = jr; |
|
3054 } |
|
3055 if (ii == jj) |
|
3056 { |
|
3057 i++; |
|
3058 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
3059 ic++; |
|
3060 if (i < lhs_nz) |
|
3061 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
3062 } |
|
3063 j++; |
|
3064 if (j < len) |
|
3065 { |
|
3066 jj = idx_i.elem (j); |
|
3067 jr = jj % lhs_nr; |
|
3068 jc = (jj - jr) / lhs_nr; |
|
3069 } |
|
3070 } |
|
3071 } |
|
3072 |
5275
|
3073 for (octave_idx_type iidx = kc; iidx < lhs_nc+1; iidx++) |
5164
|
3074 stmp.xcidx(iidx) = kk; |
|
3075 |
|
3076 lhs = stmp; |
|
3077 } |
|
3078 else |
|
3079 { |
|
3080 (*current_liboctave_error_handler) |
|
3081 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
3082 |
|
3083 retval = 0; |
|
3084 } |
|
3085 } |
|
3086 // idx_vector::freeze() printed an error message for us. |
|
3087 } |
|
3088 } |
|
3089 else |
|
3090 { |
|
3091 (*current_liboctave_error_handler) |
|
3092 ("invalid number of indices for matrix expression"); |
|
3093 |
|
3094 retval = 0; |
|
3095 } |
|
3096 |
|
3097 lhs.clear_index (); |
|
3098 |
|
3099 return retval; |
|
3100 } |
|
3101 |
|
3102 template <class T> |
|
3103 void |
|
3104 Sparse<T>::print_info (std::ostream& os, const std::string& prefix) const |
|
3105 { |
|
3106 os << prefix << "rep address: " << rep << "\n" |
5604
|
3107 << prefix << "rep->nzmx: " << rep->nzmx << "\n" |
5164
|
3108 << prefix << "rep->nrows: " << rep->nrows << "\n" |
|
3109 << prefix << "rep->ncols: " << rep->ncols << "\n" |
|
3110 << prefix << "rep->data: " << static_cast<void *> (rep->d) << "\n" |
|
3111 << prefix << "rep->ridx: " << static_cast<void *> (rep->r) << "\n" |
|
3112 << prefix << "rep->cidx: " << static_cast<void *> (rep->c) << "\n" |
|
3113 << prefix << "rep->count: " << rep->count << "\n"; |
|
3114 } |
|
3115 |
|
3116 /* |
|
3117 ;;; Local Variables: *** |
|
3118 ;;; mode: C++ *** |
|
3119 ;;; End: *** |
|
3120 */ |