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