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 } |
5166
|
754 for (int k = kk; k < new_nc; k++) |
|
755 retval.xcidx(k+1) = new_nnz; |
5164
|
756 } |
|
757 else |
|
758 (*current_liboctave_error_handler) ("reshape: size mismatch"); |
|
759 } |
|
760 else |
|
761 retval = *this; |
|
762 |
|
763 return retval; |
|
764 } |
|
765 |
|
766 template <class T> |
|
767 Sparse<T> |
|
768 Sparse<T>::permute (const Array<int>& perm_vec, bool) const |
|
769 { |
|
770 dim_vector dv = dims (); |
|
771 dim_vector dv_new; |
|
772 |
|
773 int nd = dv.length (); |
|
774 |
|
775 dv_new.resize (nd); |
|
776 |
|
777 // Need this array to check for identical elements in permutation array. |
|
778 Array<bool> checked (nd, false); |
|
779 |
|
780 // Find dimension vector of permuted array. |
|
781 for (int i = 0; i < nd; i++) |
|
782 { |
|
783 int perm_el = perm_vec.elem (i); |
|
784 |
|
785 if (perm_el > dv.length () || perm_el < 1) |
|
786 { |
|
787 (*current_liboctave_error_handler) |
|
788 ("permutation vector contains an invalid element"); |
|
789 |
|
790 return Sparse<T> (); |
|
791 } |
|
792 |
|
793 if (checked.elem(perm_el - 1)) |
|
794 { |
|
795 (*current_liboctave_error_handler) |
|
796 ("PERM cannot contain identical elements"); |
|
797 |
|
798 return Sparse<T> (); |
|
799 } |
|
800 else |
|
801 checked.elem(perm_el - 1) = true; |
|
802 |
|
803 dv_new (i) = dv (perm_el - 1); |
|
804 } |
|
805 |
|
806 if (dv_new == dv) |
|
807 return *this; |
|
808 else |
|
809 return transpose (); |
|
810 } |
|
811 |
|
812 template <class T> |
|
813 void |
|
814 Sparse<T>::resize_no_fill (const dim_vector& dv) |
|
815 { |
|
816 int n = dv.length (); |
|
817 |
|
818 if (n != 2) |
|
819 { |
|
820 (*current_liboctave_error_handler) ("sparse array must be 2-D"); |
|
821 return; |
|
822 } |
|
823 |
|
824 resize_no_fill (dv(0), dv(1)); |
|
825 } |
|
826 |
|
827 template <class T> |
|
828 void |
|
829 Sparse<T>::resize_no_fill (int r, int c) |
|
830 { |
|
831 if (r < 0 || c < 0) |
|
832 { |
|
833 (*current_liboctave_error_handler) |
|
834 ("can't resize to negative dimension"); |
|
835 return; |
|
836 } |
|
837 |
|
838 if (ndims () == 0) |
|
839 dimensions = dim_vector (0, 0); |
|
840 |
|
841 if (r == dim1 () && c == dim2 ()) |
|
842 return; |
|
843 |
|
844 typename Sparse<T>::SparseRep *old_rep = Sparse<T>::rep; |
|
845 int nc = cols (); |
|
846 int nr = rows (); |
|
847 |
|
848 if (nnz () == 0 || r == 0 || c == 0) |
|
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 { |
|
854 int n = 0; |
|
855 Sparse<T> tmpval; |
|
856 if (r >= nr) |
|
857 { |
|
858 if (c > nc) |
|
859 n = cidx(nc); |
|
860 else |
|
861 n = cidx(c); |
|
862 |
|
863 tmpval = Sparse<T> (r, c, n); |
|
864 |
|
865 if (c > nc) |
|
866 { |
|
867 for (int i = 0; i < nc; i++) |
|
868 tmpval.cidx(i) = cidx(i); |
|
869 for (int i = nc+2; i < c; i++) |
|
870 tmpval.cidx(i) = tmpval.cidx(i-1); |
|
871 } |
|
872 else if (c <= nc) |
|
873 for (int i = 0; i < c; i++) |
|
874 tmpval.cidx(i) = cidx(i); |
|
875 |
|
876 for (int i = 0; i < n; i++) |
|
877 { |
|
878 tmpval.data(i) = data(i); |
|
879 tmpval.ridx(i) = ridx(i); |
|
880 } |
|
881 } |
|
882 else |
|
883 { |
|
884 // Count how many non zero terms before we do anything |
|
885 for (int i = 0; i < c; i++) |
|
886 for (int j = cidx(i); j < cidx(i+1); j++) |
|
887 if (ridx(j) < r) |
|
888 n++; |
|
889 |
|
890 if (n) |
|
891 { |
|
892 // Now that we know the size we can do something |
|
893 tmpval = Sparse<T> (r, c, n); |
|
894 |
|
895 tmpval.cidx(0); |
|
896 for (int i = 0, ii = 0; i < c; i++) |
|
897 { |
|
898 for (int j = cidx(i); j < cidx(i+1); j++) |
|
899 if (ridx(j) < r) |
|
900 { |
|
901 tmpval.data(ii) = data(j); |
|
902 tmpval.ridx(ii++) = ridx(j); |
|
903 } |
|
904 tmpval.cidx(i+1) = ii; |
|
905 } |
|
906 } |
|
907 else |
|
908 tmpval = Sparse<T> (r, c); |
|
909 } |
|
910 |
|
911 rep = tmpval.rep; |
|
912 rep->count++; |
|
913 } |
|
914 |
|
915 dimensions = dim_vector (r, c); |
|
916 |
|
917 if (--old_rep->count <= 0) |
|
918 delete old_rep; |
|
919 } |
|
920 |
|
921 template <class T> |
|
922 Sparse<T>& |
|
923 Sparse<T>::insert (const Sparse<T>& a, int r, int c) |
|
924 { |
|
925 int a_rows = a.rows (); |
|
926 int a_cols = a.cols (); |
|
927 int nr = rows (); |
|
928 int nc = cols (); |
|
929 |
|
930 if (r < 0 || r + a_rows > rows () || c < 0 || c + a_cols > cols ()) |
|
931 { |
|
932 (*current_liboctave_error_handler) ("range error for insert"); |
|
933 return *this; |
|
934 } |
|
935 |
|
936 // First count the number of elements in the final array |
|
937 int nel = cidx(c) + a.nnz (); |
|
938 |
|
939 if (c + a_cols < nc) |
|
940 nel += cidx(nc) - cidx(c + a_cols); |
|
941 |
|
942 for (int i = c; i < c + a_cols; i++) |
|
943 for (int j = cidx(i); j < cidx(i+1); j++) |
|
944 if (ridx(j) < r || ridx(j) >= r + a_rows) |
|
945 nel++; |
|
946 |
|
947 Sparse<T> tmp (*this); |
|
948 --rep->count; |
|
949 rep = new typename Sparse<T>::SparseRep (nr, nc, nel); |
|
950 |
|
951 for (int i = 0; i < tmp.cidx(c); i++) |
|
952 { |
|
953 data(i) = tmp.data(i); |
|
954 ridx(i) = tmp.ridx(i); |
|
955 } |
|
956 for (int i = 0; i < c + 1; i++) |
|
957 cidx(i) = tmp.cidx(i); |
|
958 |
|
959 int ii = cidx(c); |
|
960 |
|
961 for (int i = c; i < c + a_cols; i++) |
|
962 { |
|
963 OCTAVE_QUIT; |
|
964 |
|
965 for (int j = tmp.cidx(i); j < tmp.cidx(i+1); j++) |
|
966 if (tmp.ridx(j) < r) |
|
967 { |
|
968 data(ii) = tmp.data(j); |
|
969 ridx(ii++) = tmp.ridx(j); |
|
970 } |
|
971 |
|
972 OCTAVE_QUIT; |
|
973 |
|
974 for (int j = a.cidx(i-c); j < a.cidx(i-c+1); j++) |
|
975 { |
|
976 data(ii) = a.data(j); |
|
977 ridx(ii++) = r + a.ridx(j); |
|
978 } |
|
979 |
|
980 OCTAVE_QUIT; |
|
981 |
|
982 for (int j = tmp.cidx(i); j < tmp.cidx(i+1); j++) |
|
983 if (tmp.ridx(j) >= r + a_rows) |
|
984 { |
|
985 data(ii) = tmp.data(j); |
|
986 ridx(ii++) = tmp.ridx(j); |
|
987 } |
|
988 |
|
989 cidx(i+1) = ii; |
|
990 } |
|
991 |
|
992 for (int i = c + a_cols; i < nc; i++) |
|
993 { |
|
994 for (int j = tmp.cidx(i); j < tmp.cidx(i+1); j++) |
|
995 { |
|
996 data(ii) = tmp.data(j); |
|
997 ridx(ii++) = tmp.ridx(j); |
|
998 } |
|
999 cidx(i+1) = ii; |
|
1000 } |
|
1001 |
|
1002 return *this; |
|
1003 } |
|
1004 |
|
1005 template <class T> |
|
1006 Sparse<T>& |
|
1007 Sparse<T>::insert (const Sparse<T>& a, const Array<int>& ra_idx) |
|
1008 { |
|
1009 |
|
1010 if (ra_idx.length () != 2) |
|
1011 { |
|
1012 (*current_liboctave_error_handler) ("range error for insert"); |
|
1013 return *this; |
|
1014 } |
|
1015 |
|
1016 return insert (a, ra_idx (0), ra_idx (1)); |
|
1017 } |
|
1018 |
|
1019 template <class T> |
|
1020 Sparse<T> |
|
1021 Sparse<T>::transpose (void) const |
|
1022 { |
|
1023 assert (ndims () == 2); |
|
1024 |
|
1025 int nr = rows (); |
|
1026 int nc = cols (); |
|
1027 int nz = nnz (); |
|
1028 Sparse<T> retval (nc, nr, nz); |
|
1029 |
|
1030 retval.cidx(0) = 0; |
|
1031 for (int i = 0, iidx = 0; i < nr; i++) |
|
1032 { |
|
1033 for (int j = 0; j < nc; j++) |
|
1034 for (int k = cidx(j); k < cidx(j+1); k++) |
|
1035 if (ridx(k) == i) |
|
1036 { |
|
1037 retval.data(iidx) = data(k); |
|
1038 retval.ridx(iidx++) = j; |
|
1039 } |
|
1040 retval.cidx(i+1) = iidx; |
|
1041 } |
|
1042 |
|
1043 return retval; |
|
1044 } |
|
1045 |
|
1046 template <class T> |
|
1047 void |
|
1048 Sparse<T>::clear_index (void) |
|
1049 { |
|
1050 delete [] idx; |
|
1051 idx = 0; |
|
1052 idx_count = 0; |
|
1053 } |
|
1054 |
|
1055 template <class T> |
|
1056 void |
|
1057 Sparse<T>::set_index (const idx_vector& idx_arg) |
|
1058 { |
|
1059 int nd = ndims (); |
|
1060 |
|
1061 if (! idx && nd > 0) |
|
1062 idx = new idx_vector [nd]; |
|
1063 |
|
1064 if (idx_count < nd) |
|
1065 { |
|
1066 idx[idx_count++] = idx_arg; |
|
1067 } |
|
1068 else |
|
1069 { |
|
1070 idx_vector *new_idx = new idx_vector [idx_count+1]; |
|
1071 |
|
1072 for (int i = 0; i < idx_count; i++) |
|
1073 new_idx[i] = idx[i]; |
|
1074 |
|
1075 new_idx[idx_count++] = idx_arg; |
|
1076 |
|
1077 delete [] idx; |
|
1078 |
|
1079 idx = new_idx; |
|
1080 } |
|
1081 } |
|
1082 |
|
1083 template <class T> |
|
1084 void |
|
1085 Sparse<T>::maybe_delete_elements (idx_vector& idx_arg) |
|
1086 { |
|
1087 int nr = dim1 (); |
|
1088 int nc = dim2 (); |
|
1089 |
|
1090 if (nr == 0 && nc == 0) |
|
1091 return; |
|
1092 |
|
1093 int n; |
|
1094 if (nr == 1) |
|
1095 n = nc; |
|
1096 else if (nc == 1) |
|
1097 n = nr; |
|
1098 else |
|
1099 { |
|
1100 // Reshape to row vector for Matlab compatibility. |
|
1101 |
|
1102 n = nr * nc; |
|
1103 nr = 1; |
|
1104 nc = n; |
|
1105 } |
|
1106 |
|
1107 if (idx_arg.is_colon_equiv (n, 1)) |
|
1108 { |
|
1109 // Either A(:) = [] or A(idx) = [] with idx enumerating all |
|
1110 // elements, so we delete all elements and return [](0x0). To |
|
1111 // preserve the orientation of the vector, you have to use |
|
1112 // A(idx,:) = [] (delete rows) or A(:,idx) (delete columns). |
|
1113 |
|
1114 resize_no_fill (0, 0); |
|
1115 return; |
|
1116 } |
|
1117 |
|
1118 idx_arg.sort (true); |
|
1119 |
|
1120 int num_to_delete = idx_arg.length (n); |
|
1121 |
|
1122 if (num_to_delete != 0) |
|
1123 { |
|
1124 int new_n = n; |
|
1125 int new_nnz = nnz (); |
|
1126 |
|
1127 int iidx = 0; |
|
1128 |
|
1129 const Sparse<T> tmp (*this); |
|
1130 |
|
1131 for (int i = 0; i < n; i++) |
|
1132 { |
|
1133 OCTAVE_QUIT; |
|
1134 |
|
1135 if (i == idx_arg.elem (iidx)) |
|
1136 { |
|
1137 iidx++; |
|
1138 new_n--; |
|
1139 |
|
1140 if (tmp.elem (i) != T ()) |
|
1141 new_nnz--; |
|
1142 |
|
1143 if (iidx == num_to_delete) |
|
1144 break; |
|
1145 } |
|
1146 } |
|
1147 |
|
1148 if (new_n > 0) |
|
1149 { |
|
1150 rep->count--; |
|
1151 |
|
1152 if (nr == 1) |
|
1153 rep = new typename Sparse<T>::SparseRep (1, new_n, new_nnz); |
|
1154 else |
|
1155 rep = new typename Sparse<T>::SparseRep (new_n, 1, new_nnz); |
|
1156 |
|
1157 int ii = 0; |
|
1158 int jj = 0; |
|
1159 iidx = 0; |
|
1160 for (int i = 0; i < n; i++) |
|
1161 { |
|
1162 OCTAVE_QUIT; |
|
1163 |
|
1164 if (iidx < num_to_delete && i == idx_arg.elem (iidx)) |
|
1165 iidx++; |
|
1166 else |
|
1167 { |
|
1168 T el = tmp.elem (i); |
|
1169 if (el != T ()) |
|
1170 { |
|
1171 data(ii) = el; |
|
1172 ridx(ii++) = jj; |
|
1173 } |
|
1174 jj++; |
|
1175 } |
|
1176 } |
|
1177 |
|
1178 dimensions.resize (2); |
|
1179 |
|
1180 if (nr == 1) |
|
1181 { |
|
1182 ii = 0; |
|
1183 cidx(0) = 0; |
|
1184 for (int i = 0; i < new_n; i++) |
|
1185 { |
|
1186 OCTAVE_QUIT; |
|
1187 if (ridx(ii) == i) |
|
1188 ridx(ii++) = 0; |
|
1189 cidx(i+1) = ii; |
|
1190 } |
|
1191 |
|
1192 dimensions(0) = 1; |
|
1193 dimensions(1) = new_n; |
|
1194 } |
|
1195 else |
|
1196 { |
|
1197 cidx(0) = 0; |
|
1198 cidx(1) = new_nnz; |
|
1199 dimensions(0) = new_n; |
|
1200 dimensions(1) = 1; |
|
1201 } |
|
1202 } |
|
1203 else |
|
1204 (*current_liboctave_error_handler) |
|
1205 ("A(idx) = []: index out of range"); |
|
1206 } |
|
1207 } |
|
1208 |
|
1209 template <class T> |
|
1210 void |
|
1211 Sparse<T>::maybe_delete_elements (idx_vector& idx_i, idx_vector& idx_j) |
|
1212 { |
|
1213 assert (ndims () == 2); |
|
1214 |
|
1215 int nr = dim1 (); |
|
1216 int nc = dim2 (); |
|
1217 |
|
1218 if (nr == 0 && nc == 0) |
|
1219 return; |
|
1220 |
|
1221 if (idx_i.is_colon ()) |
|
1222 { |
|
1223 if (idx_j.is_colon ()) |
|
1224 { |
|
1225 // A(:,:) -- We are deleting columns and rows, so the result |
|
1226 // is [](0x0). |
|
1227 |
|
1228 resize_no_fill (0, 0); |
|
1229 return; |
|
1230 } |
|
1231 |
|
1232 if (idx_j.is_colon_equiv (nc, 1)) |
|
1233 { |
|
1234 // A(:,j) -- We are deleting columns by enumerating them, |
|
1235 // If we enumerate all of them, we should have zero columns |
|
1236 // with the same number of rows that we started with. |
|
1237 |
|
1238 resize_no_fill (nr, 0); |
|
1239 return; |
|
1240 } |
|
1241 } |
|
1242 |
|
1243 if (idx_j.is_colon () && idx_i.is_colon_equiv (nr, 1)) |
|
1244 { |
|
1245 // A(i,:) -- We are deleting rows by enumerating them. If we |
|
1246 // enumerate all of them, we should have zero rows with the |
|
1247 // same number of columns that we started with. |
|
1248 |
|
1249 resize_no_fill (0, nc); |
|
1250 return; |
|
1251 } |
|
1252 |
|
1253 if (idx_i.is_colon_equiv (nr, 1)) |
|
1254 { |
|
1255 if (idx_j.is_colon_equiv (nc, 1)) |
|
1256 resize_no_fill (0, 0); |
|
1257 else |
|
1258 { |
|
1259 idx_j.sort (true); |
|
1260 |
|
1261 int num_to_delete = idx_j.length (nc); |
|
1262 |
|
1263 if (num_to_delete != 0) |
|
1264 { |
|
1265 if (nr == 1 && num_to_delete == nc) |
|
1266 resize_no_fill (0, 0); |
|
1267 else |
|
1268 { |
|
1269 int new_nc = nc; |
|
1270 int new_nnz = nnz (); |
|
1271 |
|
1272 int iidx = 0; |
|
1273 |
|
1274 for (int j = 0; j < nc; j++) |
|
1275 { |
|
1276 OCTAVE_QUIT; |
|
1277 |
|
1278 if (j == idx_j.elem (iidx)) |
|
1279 { |
|
1280 iidx++; |
|
1281 new_nc--; |
|
1282 |
|
1283 new_nnz -= cidx(j+1) - cidx(j); |
|
1284 |
|
1285 if (iidx == num_to_delete) |
|
1286 break; |
|
1287 } |
|
1288 } |
|
1289 |
|
1290 if (new_nc > 0) |
|
1291 { |
|
1292 const Sparse<T> tmp (*this); |
|
1293 --rep->count; |
|
1294 rep = new typename Sparse<T>::SparseRep (nr, new_nc, |
|
1295 new_nnz); |
|
1296 int ii = 0; |
|
1297 int jj = 0; |
|
1298 iidx = 0; |
|
1299 cidx(0) = 0; |
|
1300 for (int j = 0; j < nc; j++) |
|
1301 { |
|
1302 OCTAVE_QUIT; |
|
1303 |
|
1304 if (iidx < num_to_delete && j == idx_j.elem (iidx)) |
|
1305 iidx++; |
|
1306 else |
|
1307 { |
|
1308 for (int i = tmp.cidx(j); |
|
1309 i < tmp.cidx(j+1); i++) |
|
1310 { |
|
1311 data(jj) = tmp.data(i); |
|
1312 ridx(jj++) = tmp.ridx(i); |
|
1313 } |
|
1314 cidx(++ii) = jj; |
|
1315 } |
|
1316 } |
|
1317 |
|
1318 dimensions.resize (2); |
|
1319 dimensions(1) = new_nc; |
|
1320 } |
|
1321 else |
|
1322 (*current_liboctave_error_handler) |
|
1323 ("A(idx) = []: index out of range"); |
|
1324 } |
|
1325 } |
|
1326 } |
|
1327 } |
|
1328 else if (idx_j.is_colon_equiv (nc, 1)) |
|
1329 { |
|
1330 if (idx_i.is_colon_equiv (nr, 1)) |
|
1331 resize_no_fill (0, 0); |
|
1332 else |
|
1333 { |
|
1334 idx_i.sort (true); |
|
1335 |
|
1336 int num_to_delete = idx_i.length (nr); |
|
1337 |
|
1338 if (num_to_delete != 0) |
|
1339 { |
|
1340 if (nc == 1 && num_to_delete == nr) |
|
1341 resize_no_fill (0, 0); |
|
1342 else |
|
1343 { |
|
1344 int new_nr = nr; |
|
1345 int new_nnz = nnz (); |
|
1346 |
|
1347 int iidx = 0; |
|
1348 |
|
1349 for (int i = 0; i < nr; i++) |
|
1350 { |
|
1351 OCTAVE_QUIT; |
|
1352 |
|
1353 if (i == idx_i.elem (iidx)) |
|
1354 { |
|
1355 iidx++; |
|
1356 new_nr--; |
|
1357 |
|
1358 for (int j = 0; j < nnz (); j++) |
|
1359 if (ridx(j) == i) |
|
1360 new_nnz--; |
|
1361 |
|
1362 if (iidx == num_to_delete) |
|
1363 break; |
|
1364 } |
|
1365 } |
|
1366 |
|
1367 if (new_nr > 0) |
|
1368 { |
|
1369 const Sparse<T> tmp (*this); |
|
1370 --rep->count; |
|
1371 rep = new typename Sparse<T>::SparseRep (new_nr, nc, |
|
1372 new_nnz); |
|
1373 |
|
1374 int jj = 0; |
|
1375 cidx(0) = 0; |
|
1376 for (int i = 0; i < nc; i++) |
|
1377 { |
|
1378 iidx = 0; |
|
1379 for (int j = tmp.cidx(i); j < tmp.cidx(i+1); j++) |
|
1380 { |
|
1381 OCTAVE_QUIT; |
|
1382 |
|
1383 int ri = tmp.ridx(j); |
|
1384 |
|
1385 while (iidx < num_to_delete && |
|
1386 ri > idx_i.elem (iidx)) |
|
1387 { |
|
1388 iidx++; |
|
1389 } |
|
1390 |
|
1391 if (iidx == num_to_delete || |
|
1392 ri != idx_i.elem(iidx)) |
|
1393 { |
|
1394 data(jj) = tmp.data(j); |
|
1395 ridx(jj++) = ri - iidx; |
|
1396 } |
|
1397 } |
|
1398 cidx(i+1) = jj; |
|
1399 } |
|
1400 |
|
1401 dimensions.resize (2); |
|
1402 dimensions(0) = new_nr; |
|
1403 } |
|
1404 else |
|
1405 (*current_liboctave_error_handler) |
|
1406 ("A(idx) = []: index out of range"); |
|
1407 } |
|
1408 } |
|
1409 } |
|
1410 } |
|
1411 } |
|
1412 |
|
1413 template <class T> |
|
1414 void |
|
1415 Sparse<T>::maybe_delete_elements (Array<idx_vector>& ra_idx) |
|
1416 { |
|
1417 if (ra_idx.length () == 1) |
|
1418 maybe_delete_elements (ra_idx(0)); |
|
1419 else if (ra_idx.length () == 2) |
|
1420 maybe_delete_elements (ra_idx(0), ra_idx(1)); |
|
1421 else |
|
1422 (*current_liboctave_error_handler) |
|
1423 ("range error for maybe_delete_elements"); |
|
1424 } |
|
1425 |
|
1426 template <class T> |
|
1427 Sparse<T> |
|
1428 Sparse<T>::value (void) |
|
1429 { |
|
1430 Sparse<T> retval; |
|
1431 |
|
1432 int n_idx = index_count (); |
|
1433 |
|
1434 if (n_idx == 2) |
|
1435 { |
|
1436 idx_vector *tmp = get_idx (); |
|
1437 |
|
1438 idx_vector idx_i = tmp[0]; |
|
1439 idx_vector idx_j = tmp[1]; |
|
1440 |
|
1441 retval = index (idx_i, idx_j); |
|
1442 } |
|
1443 else if (n_idx == 1) |
|
1444 { |
|
1445 retval = index (idx[0]); |
|
1446 } |
|
1447 else |
|
1448 (*current_liboctave_error_handler) |
|
1449 ("Sparse<T>::value: invalid number of indices specified"); |
|
1450 |
|
1451 clear_index (); |
|
1452 |
|
1453 return retval; |
|
1454 } |
|
1455 |
|
1456 template <class T> |
|
1457 Sparse<T> |
|
1458 Sparse<T>::index (idx_vector& idx_arg, int resize_ok) const |
|
1459 { |
|
1460 Sparse<T> retval; |
|
1461 |
|
1462 assert (ndims () == 2); |
|
1463 |
|
1464 int nr = dim1 (); |
|
1465 int nc = dim2 (); |
|
1466 int nz = nnz (); |
|
1467 |
|
1468 int orig_len = nr * nc; |
|
1469 |
|
1470 dim_vector idx_orig_dims = idx_arg.orig_dimensions (); |
|
1471 |
|
1472 int idx_orig_rows = idx_arg.orig_rows (); |
|
1473 int idx_orig_columns = idx_arg.orig_columns (); |
|
1474 |
|
1475 if (idx_orig_dims.length () > 2) |
|
1476 (*current_liboctave_error_handler) |
|
1477 ("Sparse<T>::index: Can not index Sparse<T> with an N-D Array"); |
|
1478 else if (idx_arg.is_colon ()) |
|
1479 { |
|
1480 // Fast magic colon processing. |
|
1481 retval = Sparse<T> (nr * nc, 1, nz); |
|
1482 |
|
1483 for (int i = 0; i < nc; i++) |
|
1484 for (int j = cidx(i); j < cidx(i+1); j++) |
|
1485 { |
|
1486 OCTAVE_QUIT; |
|
1487 retval.xdata(j) = data(j); |
|
1488 retval.xridx(j) = ridx(j) + i * nr; |
|
1489 } |
|
1490 retval.xcidx(0) = 0; |
|
1491 retval.xcidx(1) = nz; |
|
1492 } |
|
1493 else if (nr == 1 && nc == 1) |
|
1494 { |
|
1495 // You have to be pretty sick to get to this bit of code, |
|
1496 // since you have a scalar stored as a sparse matrix, and |
|
1497 // then want to make a dense matrix with sparse |
|
1498 // representation. Ok, we'll do it, but you deserve what |
|
1499 // you get!! |
|
1500 int n = idx_arg.freeze (length (), "sparse vector", resize_ok); |
|
1501 if (n == 0) |
|
1502 if (idx_arg.one_zero_only ()) |
|
1503 retval = Sparse<T> (dim_vector (0, 0)); |
|
1504 else |
|
1505 retval = Sparse<T> (dim_vector (0, 1)); |
|
1506 else if (nz < 1) |
|
1507 if (n >= idx_orig_dims.numel ()) |
|
1508 retval = Sparse<T> (idx_orig_dims); |
|
1509 else |
|
1510 retval = Sparse<T> (dim_vector (n, 1)); |
|
1511 else if (n >= idx_orig_dims.numel ()) |
|
1512 { |
|
1513 T el = elem (0); |
|
1514 int new_nr = idx_orig_rows; |
|
1515 int new_nc = idx_orig_columns; |
|
1516 for (int i = 2; i < idx_orig_dims.length (); i++) |
|
1517 new_nc *= idx_orig_dims (i); |
|
1518 |
|
1519 retval = Sparse<T> (new_nr, new_nc, idx_arg.ones_count ()); |
|
1520 |
|
1521 int ic = 0; |
|
1522 for (int i = 0; i < n; i++) |
|
1523 { |
|
1524 if (i % new_nr == 0) |
|
1525 retval.xcidx(i % new_nr) = ic; |
|
1526 |
|
1527 int ii = idx_arg.elem (i); |
|
1528 if (ii == 0) |
|
1529 { |
|
1530 OCTAVE_QUIT; |
|
1531 retval.xdata(ic) = el; |
|
1532 retval.xridx(ic++) = i % new_nr; |
|
1533 } |
|
1534 } |
|
1535 retval.xcidx (new_nc) = ic; |
|
1536 } |
|
1537 else |
|
1538 { |
|
1539 T el = elem (0); |
|
1540 retval = Sparse<T> (n, 1, nz); |
|
1541 |
|
1542 for (int i = 0; i < nz; i++) |
|
1543 { |
|
1544 OCTAVE_QUIT; |
|
1545 retval.xdata(i) = el; |
|
1546 retval.xridx(i) = i; |
|
1547 } |
|
1548 retval.xcidx(0) = 0; |
|
1549 retval.xcidx(1) = n; |
|
1550 } |
|
1551 } |
|
1552 else if (nr == 1 || nc == 1) |
|
1553 { |
|
1554 // If indexing a vector with a matrix, return value has same |
|
1555 // shape as the index. Otherwise, it has same orientation as |
|
1556 // indexed object. |
|
1557 int len = length (); |
|
1558 int n = idx_arg.freeze (len, "sparse vector", resize_ok); |
|
1559 |
|
1560 if (n == 0) |
|
1561 if (nr == 1) |
|
1562 retval = Sparse<T> (dim_vector (1, 0)); |
|
1563 else |
|
1564 retval = Sparse<T> (dim_vector (0, 1)); |
|
1565 else if (nz < 1) |
|
1566 if ((n != 0 && idx_arg.one_zero_only ()) |
|
1567 || idx_orig_rows == 1 || idx_orig_columns == 1) |
|
1568 retval = Sparse<T> ((nr == 1 ? 1 : n), (nr == 1 ? n : 1)); |
|
1569 else |
|
1570 retval = Sparse<T> (idx_orig_dims); |
|
1571 else |
|
1572 { |
|
1573 |
|
1574 int new_nnz = 0; |
|
1575 if (nr == 1) |
|
1576 for (int i = 0; i < n; i++) |
|
1577 { |
|
1578 OCTAVE_QUIT; |
|
1579 |
|
1580 int ii = idx_arg.elem (i); |
|
1581 if (ii < len) |
|
1582 if (cidx(ii) != cidx(ii+1)) |
|
1583 new_nnz++; |
|
1584 } |
|
1585 else |
|
1586 for (int i = 0; i < n; i++) |
|
1587 { |
|
1588 int ii = idx_arg.elem (i); |
|
1589 if (ii < len) |
|
1590 for (int j = 0; j < nz; j++) |
|
1591 { |
|
1592 OCTAVE_QUIT; |
|
1593 |
|
1594 if (ridx(j) == ii) |
|
1595 new_nnz++; |
|
1596 if (ridx(j) >= ii) |
|
1597 break; |
|
1598 } |
|
1599 } |
|
1600 |
|
1601 if (idx_arg.one_zero_only () || idx_orig_rows == 1 || |
|
1602 idx_orig_columns == 1) |
|
1603 { |
|
1604 if (nr == 1) |
|
1605 { |
|
1606 retval = Sparse<T> (1, n, new_nnz); |
|
1607 int jj = 0; |
|
1608 retval.xcidx(0) = 0; |
|
1609 for (int i = 0; i < n; i++) |
|
1610 { |
|
1611 OCTAVE_QUIT; |
|
1612 |
|
1613 int ii = idx_arg.elem (i); |
|
1614 if (ii < len) |
|
1615 if (cidx(ii) != cidx(ii+1)) |
|
1616 { |
|
1617 retval.xdata(jj) = data(cidx(ii)); |
|
1618 retval.xridx(jj++) = 0; |
|
1619 } |
|
1620 retval.xcidx(i+1) = jj; |
|
1621 } |
|
1622 } |
|
1623 else |
|
1624 { |
|
1625 retval = Sparse<T> (n, 1, new_nnz); |
|
1626 retval.xcidx(0) = 0; |
|
1627 retval.xcidx(1) = new_nnz; |
|
1628 int jj = 0; |
|
1629 for (int i = 0; i < n; i++) |
|
1630 { |
|
1631 int ii = idx_arg.elem (i); |
|
1632 if (ii < len) |
|
1633 for (int j = 0; j < nz; j++) |
|
1634 { |
|
1635 OCTAVE_QUIT; |
|
1636 |
|
1637 if (ridx(j) == ii) |
|
1638 { |
|
1639 retval.xdata(jj) = data(j); |
|
1640 retval.xridx(jj++) = i; |
|
1641 } |
|
1642 if (ridx(j) >= ii) |
|
1643 break; |
|
1644 } |
|
1645 } |
|
1646 } |
|
1647 } |
|
1648 else |
|
1649 { |
|
1650 int new_nr; |
|
1651 int new_nc; |
|
1652 if (n >= idx_orig_dims.numel ()) |
|
1653 { |
|
1654 new_nr = idx_orig_rows; |
|
1655 new_nc = idx_orig_columns; |
|
1656 } |
|
1657 else |
|
1658 { |
|
1659 new_nr = n; |
|
1660 new_nc = 1; |
|
1661 } |
|
1662 |
|
1663 retval = Sparse<T> (new_nr, new_nc, new_nnz); |
|
1664 |
|
1665 if (nr == 1) |
|
1666 { |
|
1667 int jj = 0; |
|
1668 retval.xcidx(0) = 0; |
|
1669 for (int i = 0; i < n; i++) |
|
1670 { |
|
1671 OCTAVE_QUIT; |
|
1672 |
|
1673 int ii = idx_arg.elem (i); |
|
1674 if (ii < len) |
|
1675 if (cidx(ii) != cidx(ii+1)) |
|
1676 { |
|
1677 retval.xdata(jj) = data(cidx(ii)); |
|
1678 retval.xridx(jj++) = 0; |
|
1679 } |
|
1680 retval.xcidx(i/new_nr+1) = jj; |
|
1681 } |
|
1682 } |
|
1683 else |
|
1684 { |
|
1685 int jj = 0; |
|
1686 retval.xcidx(0) = 0; |
|
1687 for (int i = 0; i < n; i++) |
|
1688 { |
|
1689 int ii = idx_arg.elem (i); |
|
1690 if (ii < len) |
|
1691 for (int j = 0; j < nz; j++) |
|
1692 { |
|
1693 OCTAVE_QUIT; |
|
1694 |
|
1695 if (ridx(j) == ii) |
|
1696 { |
|
1697 retval.xdata(jj) = data(j); |
|
1698 retval.xridx(jj++) = i; |
|
1699 } |
|
1700 if (ridx(j) >= ii) |
|
1701 break; |
|
1702 } |
|
1703 retval.xcidx(i/new_nr+1) = jj; |
|
1704 } |
|
1705 } |
|
1706 } |
|
1707 } |
|
1708 } |
|
1709 else |
|
1710 { |
|
1711 if (liboctave_wfi_flag |
|
1712 && ! (idx_arg.one_zero_only () |
|
1713 && idx_orig_rows == nr |
|
1714 && idx_orig_columns == nc)) |
|
1715 (*current_liboctave_warning_handler) |
|
1716 ("single index used for sparse matrix"); |
|
1717 |
|
1718 // This code is only for indexing matrices. The vector |
|
1719 // cases are handled above. |
|
1720 |
|
1721 idx_arg.freeze (nr * nc, "matrix", resize_ok); |
|
1722 |
|
1723 if (idx_arg) |
|
1724 { |
|
1725 int result_nr = idx_orig_rows; |
|
1726 int result_nc = idx_orig_columns; |
|
1727 |
|
1728 if (idx_arg.one_zero_only ()) |
|
1729 { |
|
1730 result_nr = idx_arg.ones_count (); |
|
1731 result_nc = (result_nr > 0 ? 1 : 0); |
|
1732 } |
|
1733 |
|
1734 if (nz < 1) |
|
1735 retval = Sparse<T> (result_nr, result_nc); |
|
1736 else |
|
1737 { |
|
1738 // Count number of non-zero elements |
|
1739 int new_nnz = 0; |
|
1740 int kk = 0; |
|
1741 for (int j = 0; j < result_nc; j++) |
|
1742 { |
|
1743 for (int i = 0; i < result_nr; i++) |
|
1744 { |
|
1745 OCTAVE_QUIT; |
|
1746 |
|
1747 int ii = idx_arg.elem (kk++); |
|
1748 if (ii < orig_len) |
|
1749 { |
|
1750 int fr = ii % nr; |
|
1751 int fc = (ii - fr) / nr; |
|
1752 for (int k = cidx(fc); k < cidx(fc+1); k++) |
|
1753 { |
|
1754 if (ridx(k) == fr) |
|
1755 new_nnz++; |
|
1756 if (ridx(k) >= fr) |
|
1757 break; |
|
1758 } |
|
1759 } |
|
1760 } |
|
1761 } |
|
1762 |
|
1763 retval = Sparse<T> (result_nr, result_nc, new_nnz); |
|
1764 |
|
1765 kk = 0; |
|
1766 int jj = 0; |
|
1767 retval.xcidx(0) = 0; |
|
1768 for (int j = 0; j < result_nc; j++) |
|
1769 { |
|
1770 for (int i = 0; i < result_nr; i++) |
|
1771 { |
|
1772 OCTAVE_QUIT; |
|
1773 |
|
1774 int ii = idx_arg.elem (kk++); |
|
1775 if (ii < orig_len) |
|
1776 { |
|
1777 int fr = ii % nr; |
|
1778 int fc = (ii - fr) / nr; |
|
1779 for (int k = cidx(fc); k < cidx(fc+1); k++) |
|
1780 { |
|
1781 if (ridx(k) == fr) |
|
1782 { |
|
1783 retval.xdata(jj) = data(k); |
|
1784 retval.xridx(jj++) = i; |
|
1785 } |
|
1786 if (ridx(k) >= fr) |
|
1787 break; |
|
1788 } |
|
1789 } |
|
1790 } |
|
1791 retval.xcidx(j+1) = jj; |
|
1792 } |
|
1793 } |
|
1794 // idx_vector::freeze() printed an error message for us. |
|
1795 } |
|
1796 } |
|
1797 |
|
1798 return retval; |
|
1799 } |
|
1800 |
|
1801 template <class T> |
|
1802 Sparse<T> |
|
1803 Sparse<T>::index (idx_vector& idx_i, idx_vector& idx_j, int resize_ok) const |
|
1804 { |
|
1805 Sparse<T> retval; |
|
1806 |
|
1807 assert (ndims () == 2); |
|
1808 |
|
1809 int nr = dim1 (); |
|
1810 int nc = dim2 (); |
|
1811 |
|
1812 int n = idx_i.freeze (nr, "row", resize_ok); |
|
1813 int m = idx_j.freeze (nc, "column", resize_ok); |
|
1814 |
|
1815 if (idx_i && idx_j) |
|
1816 { |
|
1817 if (idx_i.orig_empty () || idx_j.orig_empty () || n == 0 || m == 0) |
|
1818 { |
|
1819 retval.resize_no_fill (n, m); |
|
1820 } |
|
1821 else if (idx_i.is_colon_equiv (nr) && idx_j.is_colon_equiv (nc)) |
|
1822 { |
|
1823 retval = *this; |
|
1824 } |
|
1825 else |
|
1826 { |
|
1827 // First count the number of non-zero elements |
|
1828 int new_nnz = 0; |
|
1829 for (int j = 0; j < m; j++) |
|
1830 { |
|
1831 int jj = idx_j.elem (j); |
|
1832 for (int i = 0; i < n; i++) |
|
1833 { |
|
1834 OCTAVE_QUIT; |
|
1835 |
|
1836 int ii = idx_i.elem (i); |
|
1837 if (ii < nr && jj < nc) |
|
1838 { |
|
1839 for (int k = cidx(jj); k < cidx(jj+1); k++) |
|
1840 { |
|
1841 if (ridx(k) == ii) |
|
1842 new_nnz++; |
|
1843 if (ridx(k) >= ii) |
|
1844 break; |
|
1845 } |
|
1846 } |
|
1847 } |
|
1848 } |
|
1849 |
|
1850 retval = Sparse<T> (n, m, new_nnz); |
|
1851 |
|
1852 int kk = 0; |
|
1853 retval.xcidx(0) = 0; |
|
1854 for (int j = 0; j < m; j++) |
|
1855 { |
|
1856 int jj = idx_j.elem (j); |
|
1857 for (int i = 0; i < n; i++) |
|
1858 { |
|
1859 OCTAVE_QUIT; |
|
1860 |
|
1861 int ii = idx_i.elem (i); |
|
1862 if (ii < nr && jj < nc) |
|
1863 { |
|
1864 for (int k = cidx(jj); k < cidx(jj+1); k++) |
|
1865 { |
|
1866 if (ridx(k) == ii) |
|
1867 { |
|
1868 retval.xdata(kk) = data(k); |
|
1869 retval.xridx(kk++) = i; |
|
1870 } |
|
1871 if (ridx(k) >= ii) |
|
1872 break; |
|
1873 } |
|
1874 } |
|
1875 } |
|
1876 retval.xcidx(j+1) = kk; |
|
1877 } |
|
1878 } |
|
1879 } |
|
1880 |
|
1881 // idx_vector::freeze() printed an error message for us. |
|
1882 |
|
1883 return retval; |
|
1884 } |
|
1885 |
|
1886 template <class T> |
|
1887 Sparse<T> |
|
1888 Sparse<T>::index (Array<idx_vector>& ra_idx, int resize_ok) const |
|
1889 { |
|
1890 |
|
1891 if (ra_idx.length () != 2) |
|
1892 { |
|
1893 (*current_liboctave_error_handler) ("range error for index"); |
|
1894 return *this; |
|
1895 } |
|
1896 |
|
1897 return index (ra_idx (0), ra_idx (1), resize_ok); |
|
1898 } |
|
1899 |
|
1900 // XXX FIXME XXX |
|
1901 // Unfortunately numel can overflow for very large but very sparse matrices. |
|
1902 // For now just flag an error when this happens. |
|
1903 template <class LT, class RT> |
|
1904 int |
|
1905 assign1 (Sparse<LT>& lhs, const Sparse<RT>& rhs) |
|
1906 { |
|
1907 int retval = 1; |
|
1908 |
|
1909 idx_vector *idx_tmp = lhs.get_idx (); |
|
1910 |
|
1911 idx_vector lhs_idx = idx_tmp[0]; |
|
1912 |
|
1913 int lhs_len = lhs.numel (); |
|
1914 int rhs_len = rhs.numel (); |
|
1915 |
|
1916 unsigned EIGHT_BYTE_INT long_lhs_len = |
|
1917 static_cast<unsigned EIGHT_BYTE_INT> (lhs.rows ()) * |
|
1918 static_cast<unsigned EIGHT_BYTE_INT> (lhs.cols ()); |
|
1919 |
|
1920 unsigned EIGHT_BYTE_INT long_rhs_len = |
|
1921 static_cast<unsigned EIGHT_BYTE_INT> (rhs.rows ()) * |
|
1922 static_cast<unsigned EIGHT_BYTE_INT> (rhs.cols ()); |
|
1923 |
|
1924 if (long_rhs_len != static_cast<unsigned EIGHT_BYTE_INT>(rhs_len) || |
|
1925 long_lhs_len != static_cast<unsigned EIGHT_BYTE_INT>(lhs_len)) |
|
1926 { |
|
1927 (*current_liboctave_error_handler) |
|
1928 ("A(I) = X: Matrix dimensions too large to ensure correct\n", |
|
1929 "operation. This is an limitation that should be removed\n", |
|
1930 "in the future."); |
|
1931 |
|
1932 lhs.clear_index (); |
|
1933 return 0; |
|
1934 } |
|
1935 |
|
1936 int nr = lhs.rows (); |
|
1937 int nc = lhs.cols (); |
|
1938 int nz = lhs.nnz (); |
|
1939 |
|
1940 int n = lhs_idx.freeze (lhs_len, "vector", true, liboctave_wrore_flag); |
|
1941 |
|
1942 if (n != 0) |
|
1943 { |
|
1944 int max_idx = lhs_idx.max () + 1; |
|
1945 max_idx = max_idx < lhs_len ? lhs_len : max_idx; |
|
1946 |
|
1947 // Take a constant copy of lhs. This means that elem won't |
|
1948 // create missing elements. |
|
1949 const Sparse<LT> c_lhs (lhs); |
|
1950 |
|
1951 if (rhs_len == n) |
|
1952 { |
|
1953 int new_nnz = lhs.nnz (); |
|
1954 |
|
1955 // First count the number of non-zero elements |
|
1956 for (int i = 0; i < n; i++) |
|
1957 { |
|
1958 OCTAVE_QUIT; |
|
1959 |
|
1960 int ii = lhs_idx.elem (i); |
|
1961 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
|
1962 new_nnz--; |
|
1963 if (rhs.elem(i) != RT ()) |
|
1964 new_nnz++; |
|
1965 } |
|
1966 |
|
1967 if (nr > 1) |
|
1968 { |
|
1969 Sparse<LT> tmp (max_idx, 1, new_nnz); |
|
1970 tmp.cidx(0) = 0; |
|
1971 tmp.cidx(1) = tmp.nnz (); |
|
1972 |
|
1973 int i = 0; |
|
1974 int ii = 0; |
|
1975 if (i < nz) |
|
1976 ii = c_lhs.ridx(i); |
|
1977 |
|
1978 int j = 0; |
|
1979 int jj = lhs_idx.elem(j); |
|
1980 |
|
1981 int kk = 0; |
|
1982 |
|
1983 while (j < n || i < nz) |
|
1984 { |
|
1985 if (j == n || (i < nz && ii < jj)) |
|
1986 { |
|
1987 tmp.xdata (kk) = c_lhs.data (i); |
|
1988 tmp.xridx (kk++) = ii; |
|
1989 if (++i < nz) |
|
1990 ii = c_lhs.ridx(i); |
|
1991 } |
|
1992 else |
|
1993 { |
|
1994 RT rtmp = rhs.elem (j); |
|
1995 if (rtmp != RT ()) |
|
1996 { |
|
1997 tmp.xdata (kk) = rtmp; |
|
1998 tmp.xridx (kk++) = jj; |
|
1999 } |
|
2000 |
|
2001 if (ii == jj && i < nz) |
|
2002 if (++i < nz) |
|
2003 ii = c_lhs.ridx(i); |
|
2004 if (++j < n) |
|
2005 jj = lhs_idx.elem(j); |
|
2006 } |
|
2007 } |
|
2008 |
|
2009 lhs = tmp; |
|
2010 } |
|
2011 else |
|
2012 { |
|
2013 Sparse<LT> tmp (1, max_idx, new_nnz); |
|
2014 |
|
2015 int i = 0; |
|
2016 int ii = 0; |
|
2017 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2018 ii++; |
|
2019 |
|
2020 int j = 0; |
|
2021 int jj = lhs_idx.elem(j); |
|
2022 |
|
2023 int kk = 0; |
|
2024 int ic = 0; |
|
2025 |
|
2026 while (j < n || i < nz) |
|
2027 { |
|
2028 if (j == n || (i < nz && ii < jj)) |
|
2029 { |
|
2030 while (ic <= ii) |
|
2031 tmp.xcidx (ic++) = kk; |
|
2032 tmp.xdata (kk) = c_lhs.data (i); |
|
2033 i++; |
|
2034 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2035 ii++; |
|
2036 } |
|
2037 else |
|
2038 { |
|
2039 while (ic <= jj) |
|
2040 tmp.xcidx (ic++) = kk; |
|
2041 |
|
2042 RT rtmp = rhs.elem (j); |
|
2043 if (rtmp != RT ()) |
|
2044 tmp.xdata (kk) = rtmp; |
|
2045 if (ii == jj) |
|
2046 { |
|
2047 i++; |
|
2048 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2049 ii++; |
|
2050 } |
|
2051 j++; |
|
2052 if (j < n) |
|
2053 jj = lhs_idx.elem(j); |
|
2054 } |
|
2055 tmp.xridx (kk++) = 0; |
|
2056 } |
|
2057 |
|
2058 for (int iidx = ic; iidx < max_idx+1; iidx++) |
|
2059 tmp.xcidx(iidx) = kk; |
|
2060 |
|
2061 lhs = tmp; |
|
2062 } |
|
2063 } |
|
2064 else if (rhs_len == 1) |
|
2065 { |
|
2066 int new_nnz = lhs.nnz (); |
|
2067 RT scalar = rhs.elem (0); |
|
2068 bool scalar_non_zero = (scalar != RT ()); |
|
2069 |
|
2070 // First count the number of non-zero elements |
|
2071 if (scalar != RT ()) |
|
2072 new_nnz += n; |
|
2073 for (int i = 0; i < n; i++) |
|
2074 { |
|
2075 OCTAVE_QUIT; |
|
2076 |
|
2077 int ii = lhs_idx.elem (i); |
|
2078 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
|
2079 new_nnz--; |
|
2080 } |
|
2081 |
|
2082 if (nr > 1) |
|
2083 { |
|
2084 Sparse<LT> tmp (max_idx, 1, new_nnz); |
|
2085 tmp.cidx(0) = 0; |
|
2086 tmp.cidx(1) = tmp.nnz (); |
|
2087 |
|
2088 int i = 0; |
|
2089 int ii = 0; |
|
2090 if (i < nz) |
|
2091 ii = c_lhs.ridx(i); |
|
2092 |
|
2093 int j = 0; |
|
2094 int jj = lhs_idx.elem(j); |
|
2095 |
|
2096 int kk = 0; |
|
2097 |
|
2098 while (j < n || i < nz) |
|
2099 { |
|
2100 if (j == n || (i < nz && ii < jj)) |
|
2101 { |
|
2102 tmp.xdata (kk) = c_lhs.data (i); |
|
2103 tmp.xridx (kk++) = ii; |
|
2104 if (++i < nz) |
|
2105 ii = c_lhs.ridx(i); |
|
2106 } |
|
2107 else |
|
2108 { |
|
2109 if (scalar_non_zero) |
|
2110 { |
|
2111 tmp.xdata (kk) = scalar; |
|
2112 tmp.xridx (kk++) = jj; |
|
2113 } |
|
2114 |
|
2115 if (ii == jj && i < nz) |
|
2116 if (++i < nz) |
|
2117 ii = c_lhs.ridx(i); |
|
2118 if (++j < n) |
|
2119 jj = lhs_idx.elem(j); |
|
2120 } |
|
2121 } |
|
2122 |
|
2123 lhs = tmp; |
|
2124 } |
|
2125 else |
|
2126 { |
|
2127 Sparse<LT> tmp (1, max_idx, new_nnz); |
|
2128 |
|
2129 int i = 0; |
|
2130 int ii = 0; |
|
2131 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2132 ii++; |
|
2133 |
|
2134 int j = 0; |
|
2135 int jj = lhs_idx.elem(j); |
|
2136 |
|
2137 int kk = 0; |
|
2138 int ic = 0; |
|
2139 |
|
2140 while (j < n || i < nz) |
|
2141 { |
|
2142 if (j == n || (i < nz && ii < jj)) |
|
2143 { |
|
2144 while (ic <= ii) |
|
2145 tmp.xcidx (ic++) = kk; |
|
2146 tmp.xdata (kk) = c_lhs.data (i); |
|
2147 i++; |
|
2148 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2149 ii++; |
|
2150 } |
|
2151 else |
|
2152 { |
|
2153 while (ic <= jj) |
|
2154 tmp.xcidx (ic++) = kk; |
|
2155 if (scalar_non_zero) |
|
2156 tmp.xdata (kk) = scalar; |
|
2157 if (ii == jj) |
|
2158 { |
|
2159 i++; |
|
2160 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2161 ii++; |
|
2162 } |
|
2163 j++; |
|
2164 if (j < n) |
|
2165 jj = lhs_idx.elem(j); |
|
2166 } |
|
2167 tmp.xridx (kk++) = 0; |
|
2168 } |
|
2169 |
|
2170 for (int iidx = ic; iidx < max_idx+1; iidx++) |
|
2171 tmp.xcidx(iidx) = kk; |
|
2172 |
|
2173 lhs = tmp; |
|
2174 } |
|
2175 } |
|
2176 else |
|
2177 { |
|
2178 (*current_liboctave_error_handler) |
|
2179 ("A(I) = X: X must be a scalar or a vector with same length as I"); |
|
2180 |
|
2181 retval = 0; |
|
2182 } |
|
2183 } |
|
2184 else if (lhs_idx.is_colon ()) |
|
2185 { |
|
2186 if (lhs_len == 0) |
|
2187 { |
|
2188 |
|
2189 int new_nnz = rhs.nnz (); |
|
2190 Sparse<LT> tmp (1, rhs_len, new_nnz); |
|
2191 |
|
2192 int ii = 0; |
|
2193 int jj = 0; |
|
2194 for (int i = 0; i < rhs.cols(); i++) |
|
2195 for (int j = rhs.cidx(i); j < rhs.cidx(i+1); j++) |
|
2196 { |
|
2197 OCTAVE_QUIT; |
|
2198 for (int k = jj; k <= i * rhs.rows() + rhs.ridx(j); k++) |
|
2199 tmp.cidx(jj++) = ii; |
|
2200 |
|
2201 tmp.data(ii) = rhs.data(j); |
|
2202 tmp.ridx(ii++) = 0; |
|
2203 } |
|
2204 |
|
2205 for (int i = jj; i < rhs_len + 1; i++) |
|
2206 tmp.cidx(i) = ii; |
|
2207 |
|
2208 lhs = tmp; |
|
2209 } |
|
2210 else |
|
2211 (*current_liboctave_error_handler) |
|
2212 ("A(:) = X: A must be the same size as X"); |
|
2213 } |
|
2214 else if (! (rhs_len == 1 || rhs_len == 0)) |
|
2215 { |
|
2216 (*current_liboctave_error_handler) |
|
2217 ("A([]) = X: X must also be an empty matrix or a scalar"); |
|
2218 |
|
2219 retval = 0; |
|
2220 } |
|
2221 |
|
2222 lhs.clear_index (); |
|
2223 |
|
2224 return retval; |
|
2225 } |
|
2226 |
|
2227 template <class LT, class RT> |
|
2228 int |
|
2229 assign (Sparse<LT>& lhs, const Sparse<RT>& rhs) |
|
2230 { |
|
2231 int retval = 1; |
|
2232 |
|
2233 int n_idx = lhs.index_count (); |
|
2234 |
|
2235 int lhs_nr = lhs.rows (); |
|
2236 int lhs_nc = lhs.cols (); |
|
2237 int lhs_nz = lhs.nnz (); |
|
2238 |
|
2239 int rhs_nr = rhs.rows (); |
|
2240 int rhs_nc = rhs.cols (); |
|
2241 |
|
2242 idx_vector *tmp = lhs.get_idx (); |
|
2243 |
|
2244 idx_vector idx_i; |
|
2245 idx_vector idx_j; |
|
2246 |
|
2247 if (n_idx > 2) |
|
2248 { |
|
2249 (*current_liboctave_error_handler) |
|
2250 ("A(I, J) = X: can only have 1 or 2 indexes for sparse matrices"); |
|
2251 return 0; |
|
2252 } |
|
2253 |
|
2254 if (n_idx > 1) |
|
2255 idx_j = tmp[1]; |
|
2256 |
|
2257 if (n_idx > 0) |
|
2258 idx_i = tmp[0]; |
|
2259 |
|
2260 if (n_idx == 2) |
|
2261 { |
|
2262 int n = idx_i.freeze (lhs_nr, "row", true, liboctave_wrore_flag); |
|
2263 idx_i.sort (true); |
|
2264 |
|
2265 int m = idx_j.freeze (lhs_nc, "column", true, liboctave_wrore_flag); |
|
2266 idx_j.sort (true); |
|
2267 |
|
2268 |
|
2269 int idx_i_is_colon = idx_i.is_colon (); |
|
2270 int idx_j_is_colon = idx_j.is_colon (); |
|
2271 |
|
2272 if (idx_i_is_colon) |
|
2273 n = lhs_nr > 0 ? lhs_nr : rhs_nr; |
|
2274 |
|
2275 if (idx_j_is_colon) |
|
2276 m = lhs_nc > 0 ? lhs_nc : rhs_nc; |
|
2277 |
|
2278 if (idx_i && idx_j) |
|
2279 { |
|
2280 if (rhs_nr == 0 && rhs_nc == 0) |
|
2281 { |
|
2282 lhs.maybe_delete_elements (idx_i, idx_j); |
|
2283 } |
|
2284 else |
|
2285 { |
|
2286 if (rhs_nr == 1 && rhs_nc == 1 && n >= 0 && m >= 0) |
|
2287 { |
|
2288 // No need to do anything if either of the indices |
|
2289 // are empty. |
|
2290 |
|
2291 if (n > 0 && m > 0) |
|
2292 { |
|
2293 int max_row_idx = idx_i_is_colon ? rhs_nr : |
|
2294 idx_i.max () + 1; |
|
2295 int max_col_idx = idx_j_is_colon ? rhs_nc : |
|
2296 idx_j.max () + 1; |
|
2297 int new_nr = max_row_idx > lhs_nr ? max_row_idx : |
|
2298 lhs_nr; |
|
2299 int new_nc = max_col_idx > lhs_nc ? max_col_idx : |
|
2300 lhs_nc; |
|
2301 RT scalar = rhs.elem (0, 0); |
|
2302 |
|
2303 // Count the number of non-zero terms |
|
2304 int new_nnz = lhs.nnz (); |
|
2305 for (int j = 0; j < m; j++) |
|
2306 { |
|
2307 int jj = idx_j.elem (j); |
|
2308 if (jj < lhs_nc) |
|
2309 { |
|
2310 for (int i = 0; i < n; i++) |
|
2311 { |
|
2312 OCTAVE_QUIT; |
|
2313 |
|
2314 int ii = idx_i.elem (i); |
|
2315 |
|
2316 if (ii < lhs_nr) |
|
2317 { |
|
2318 for (int k = lhs.cidx(jj); |
|
2319 k < lhs.cidx(jj+1); k++) |
|
2320 { |
|
2321 if (lhs.ridx(k) == ii) |
|
2322 new_nnz--; |
|
2323 if (lhs.ridx(k) >= ii) |
|
2324 break; |
|
2325 } |
|
2326 } |
|
2327 } |
|
2328 } |
|
2329 } |
|
2330 |
|
2331 if (scalar != RT()) |
|
2332 new_nnz += m * n; |
|
2333 |
|
2334 Sparse<LT> stmp (new_nr, new_nc, new_nnz); |
|
2335 |
|
2336 int jji = 0; |
|
2337 int jj = idx_j.elem (jji); |
|
2338 int kk = 0; |
|
2339 stmp.cidx(0) = 0; |
|
2340 for (int j = 0; j < new_nc; j++) |
|
2341 { |
|
2342 if (jji < m && jj == j) |
|
2343 { |
|
2344 int iii = 0; |
|
2345 int ii = idx_i.elem (iii); |
|
2346 for (int i = 0; i < new_nr; i++) |
|
2347 { |
|
2348 OCTAVE_QUIT; |
|
2349 |
|
2350 if (iii < n && ii == i) |
|
2351 { |
|
2352 if (scalar != RT ()) |
|
2353 { |
|
2354 stmp.data(kk) = scalar; |
|
2355 stmp.ridx(kk++) = i; |
|
2356 } |
|
2357 if (++iii < n) |
|
2358 ii = idx_i.elem(iii); |
|
2359 } |
|
2360 else if (j < lhs.cols()) |
|
2361 { |
|
2362 for (int k = lhs.cidx(j); |
|
2363 k < lhs.cidx(j+1); k++) |
|
2364 { |
|
2365 if (lhs.ridx(k) == i) |
|
2366 { |
|
2367 stmp.data(kk) = lhs.data(k); |
|
2368 stmp.ridx(kk++) = i; |
|
2369 } |
|
2370 if (lhs.ridx(k) >= i) |
|
2371 break; |
|
2372 } |
|
2373 } |
|
2374 } |
|
2375 if (++jji < m) |
|
2376 jj = idx_j.elem(jji); |
|
2377 } |
|
2378 else if (j < lhs.cols()) |
|
2379 { |
|
2380 for (int i = lhs.cidx(j); |
|
2381 i < lhs.cidx(j+1); i++) |
|
2382 { |
|
2383 stmp.data(kk) = lhs.data(i); |
|
2384 stmp.ridx(kk++) = lhs.ridx(i); |
|
2385 } |
|
2386 } |
|
2387 stmp.cidx(j+1) = kk; |
|
2388 } |
|
2389 |
|
2390 lhs = stmp; |
|
2391 } |
|
2392 } |
|
2393 else if (n == rhs_nr && m == rhs_nc) |
|
2394 { |
|
2395 if (n > 0 && m > 0) |
|
2396 { |
|
2397 int max_row_idx = idx_i_is_colon ? rhs_nr : |
|
2398 idx_i.max () + 1; |
|
2399 int max_col_idx = idx_j_is_colon ? rhs_nc : |
|
2400 idx_j.max () + 1; |
|
2401 int new_nr = max_row_idx > lhs_nr ? max_row_idx : |
|
2402 lhs_nr; |
|
2403 int new_nc = max_col_idx > lhs_nc ? max_col_idx : |
|
2404 lhs_nc; |
|
2405 |
|
2406 // Count the number of non-zero terms |
|
2407 int new_nnz = lhs.nnz (); |
|
2408 for (int j = 0; j < m; j++) |
|
2409 { |
|
2410 int jj = idx_j.elem (j); |
|
2411 for (int i = 0; i < n; i++) |
|
2412 { |
|
2413 OCTAVE_QUIT; |
|
2414 |
|
2415 if (jj < lhs_nc) |
|
2416 { |
|
2417 int ii = idx_i.elem (i); |
|
2418 |
|
2419 if (ii < lhs_nr) |
|
2420 { |
|
2421 for (int k = lhs.cidx(jj); |
|
2422 k < lhs.cidx(jj+1); k++) |
|
2423 { |
|
2424 if (lhs.ridx(k) == ii) |
|
2425 new_nnz--; |
|
2426 if (lhs.ridx(k) >= ii) |
|
2427 break; |
|
2428 } |
|
2429 } |
|
2430 } |
|
2431 |
|
2432 if (rhs.elem(i,j) != RT ()) |
|
2433 new_nnz++; |
|
2434 } |
|
2435 } |
|
2436 |
|
2437 Sparse<LT> stmp (new_nr, new_nc, new_nnz); |
|
2438 |
|
2439 int jji = 0; |
|
2440 int jj = idx_j.elem (jji); |
|
2441 int kk = 0; |
|
2442 stmp.cidx(0) = 0; |
|
2443 for (int j = 0; j < new_nc; j++) |
|
2444 { |
|
2445 if (jji < m && jj == j) |
|
2446 { |
|
2447 int iii = 0; |
|
2448 int ii = idx_i.elem (iii); |
|
2449 for (int i = 0; i < new_nr; i++) |
|
2450 { |
|
2451 OCTAVE_QUIT; |
|
2452 |
|
2453 if (iii < n && ii == i) |
|
2454 { |
|
2455 RT rtmp = rhs.elem (iii, jji); |
|
2456 if (rtmp != RT ()) |
|
2457 { |
|
2458 stmp.data(kk) = rtmp; |
|
2459 stmp.ridx(kk++) = i; |
|
2460 } |
|
2461 if (++iii < n) |
|
2462 ii = idx_i.elem(iii); |
|
2463 } |
|
2464 else if (j < lhs.cols()) |
|
2465 { |
|
2466 for (int k = lhs.cidx(j); |
|
2467 k < lhs.cidx(j+1); k++) |
|
2468 { |
|
2469 if (lhs.ridx(k) == i) |
|
2470 { |
|
2471 stmp.data(kk) = lhs.data(k); |
|
2472 stmp.ridx(kk++) = i; |
|
2473 } |
|
2474 if (lhs.ridx(k) >= i) |
|
2475 break; |
|
2476 } |
|
2477 } |
|
2478 } |
|
2479 if (++jji < m) |
|
2480 jj = idx_j.elem(jji); |
|
2481 } |
|
2482 else if (j < lhs.cols()) |
|
2483 { |
|
2484 for (int i = lhs.cidx(j); |
|
2485 i < lhs.cidx(j+1); i++) |
|
2486 { |
|
2487 stmp.data(kk) = lhs.data(i); |
|
2488 stmp.ridx(kk++) = lhs.ridx(i); |
|
2489 } |
|
2490 } |
|
2491 stmp.cidx(j+1) = kk; |
|
2492 } |
|
2493 |
|
2494 lhs = stmp; |
|
2495 } |
|
2496 } |
|
2497 else if (n == 0 && m == 0) |
|
2498 { |
|
2499 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2500 || (rhs_nr == 0 || rhs_nc == 0))) |
|
2501 { |
|
2502 (*current_liboctave_error_handler) |
|
2503 ("A([], []) = X: X must be an empty matrix or a scalar"); |
|
2504 |
|
2505 retval = 0; |
|
2506 } |
|
2507 } |
|
2508 else |
|
2509 { |
|
2510 (*current_liboctave_error_handler) |
|
2511 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
|
2512 (*current_liboctave_error_handler) |
|
2513 ("match the number of rows in X and the number of elements in J must"); |
|
2514 (*current_liboctave_error_handler) |
|
2515 ("match the number of columns in X"); |
|
2516 |
|
2517 retval = 0; |
|
2518 } |
|
2519 } |
|
2520 } |
|
2521 // idx_vector::freeze() printed an error message for us. |
|
2522 } |
|
2523 else if (n_idx == 1) |
|
2524 { |
|
2525 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; |
|
2526 |
|
2527 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) |
|
2528 { |
|
2529 int lhs_len = lhs.length (); |
|
2530 |
|
2531 int n = idx_i.freeze (lhs_len, 0, true, liboctave_wrore_flag); |
|
2532 idx_i.sort (true); |
|
2533 |
|
2534 if (idx_i) |
|
2535 { |
|
2536 if (rhs_nr == 0 && rhs_nc == 0) |
|
2537 { |
|
2538 if (n != 0 && (lhs_nr != 0 || lhs_nc != 0)) |
|
2539 lhs.maybe_delete_elements (idx_i); |
|
2540 } |
|
2541 else |
|
2542 { |
|
2543 if (liboctave_wfi_flag) |
|
2544 { |
|
2545 if (lhs_is_empty |
|
2546 && idx_i.is_colon () |
|
2547 && ! (rhs_nr == 1 || rhs_nc == 1)) |
|
2548 { |
|
2549 (*current_liboctave_warning_handler) |
|
2550 ("A(:) = X: X is not a vector or scalar"); |
|
2551 } |
|
2552 else |
|
2553 { |
|
2554 int idx_nr = idx_i.orig_rows (); |
|
2555 int idx_nc = idx_i.orig_columns (); |
|
2556 |
|
2557 if (! (rhs_nr == idx_nr && rhs_nc == idx_nc)) |
|
2558 (*current_liboctave_warning_handler) |
|
2559 ("A(I) = X: X does not have same shape as I"); |
|
2560 } |
|
2561 } |
|
2562 |
|
2563 if (! assign1 ((Sparse<LT>&) lhs, (Sparse<RT>&) rhs)) |
|
2564 retval = 0; |
|
2565 } |
|
2566 } |
|
2567 // idx_vector::freeze() printed an error message for us. |
|
2568 } |
|
2569 else if (lhs_nr == 1) |
|
2570 { |
|
2571 idx_i.freeze (lhs_nc, "vector", true, liboctave_wrore_flag); |
|
2572 idx_i.sort (true); |
|
2573 |
|
2574 if (idx_i) |
|
2575 { |
|
2576 if (rhs_nr == 0 && rhs_nc == 0) |
|
2577 lhs.maybe_delete_elements (idx_i); |
|
2578 else if (! assign1 ((Sparse<LT>&) lhs, (Sparse<RT>&) rhs)) |
|
2579 retval = 0; |
|
2580 } |
|
2581 // idx_vector::freeze() printed an error message for us. |
|
2582 } |
|
2583 else if (lhs_nc == 1) |
|
2584 { |
|
2585 idx_i.freeze (lhs_nr, "vector", true, liboctave_wrore_flag); |
|
2586 idx_i.sort (true); |
|
2587 |
|
2588 if (idx_i) |
|
2589 { |
|
2590 if (rhs_nr == 0 && rhs_nc == 0) |
|
2591 lhs.maybe_delete_elements (idx_i); |
|
2592 else if (! assign1 ((Sparse<LT>&) lhs, (Sparse<RT>&) rhs)) |
|
2593 retval = 0; |
|
2594 } |
|
2595 // idx_vector::freeze() printed an error message for us. |
|
2596 } |
|
2597 else |
|
2598 { |
|
2599 if (liboctave_wfi_flag |
|
2600 && ! (idx_i.is_colon () |
|
2601 || (idx_i.one_zero_only () |
|
2602 && idx_i.orig_rows () == lhs_nr |
|
2603 && idx_i.orig_columns () == lhs_nc))) |
|
2604 (*current_liboctave_warning_handler) |
|
2605 ("single index used for matrix"); |
|
2606 |
|
2607 int lhs_len = lhs.length (); |
|
2608 |
|
2609 int len = idx_i.freeze (lhs_nr * lhs_nc, "matrix"); |
|
2610 idx_i.sort (true); |
|
2611 |
|
2612 if (idx_i) |
|
2613 { |
|
2614 // Take a constant copy of lhs. This means that elem won't |
|
2615 // create missing elements. |
|
2616 const Sparse<LT> c_lhs (lhs); |
|
2617 |
|
2618 if (rhs_nr == 0 && rhs_nc == 0) |
|
2619 lhs.maybe_delete_elements (idx_i); |
|
2620 else if (len == 0) |
|
2621 { |
|
2622 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2623 || (rhs_nr == 0 || rhs_nc == 0))) |
|
2624 (*current_liboctave_error_handler) |
|
2625 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2626 } |
|
2627 else if (len == rhs_nr * rhs_nc) |
|
2628 { |
|
2629 int new_nnz = lhs_nz; |
|
2630 |
|
2631 // First count the number of non-zero elements |
|
2632 for (int i = 0; i < len; i++) |
|
2633 { |
|
2634 OCTAVE_QUIT; |
|
2635 |
|
2636 int ii = idx_i.elem (i); |
|
2637 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
|
2638 new_nnz--; |
|
2639 if (rhs.elem(i) != RT ()) |
|
2640 new_nnz++; |
|
2641 } |
|
2642 |
|
2643 Sparse<LT> stmp (lhs_nr, lhs_nc, new_nnz); |
|
2644 |
|
2645 int i = 0; |
|
2646 int ii = 0; |
|
2647 int ic = 0; |
|
2648 if (i < lhs_nz) |
|
2649 { |
|
2650 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2651 ic++; |
|
2652 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2653 } |
|
2654 |
|
2655 int j = 0; |
|
2656 int jj = idx_i.elem (j); |
|
2657 int jr = jj % lhs_nr; |
|
2658 int jc = (jj - jr) / lhs_nr; |
|
2659 |
|
2660 int kk = 0; |
|
2661 int kc = 0; |
|
2662 |
|
2663 while (j < len || i < lhs_nz) |
|
2664 { |
|
2665 if (j == len || (i < lhs_nz && ii < jj)) |
|
2666 { |
|
2667 while (kc <= ic) |
|
2668 stmp.xcidx (kc++) = kk; |
|
2669 stmp.xdata (kk) = c_lhs.data (i); |
|
2670 stmp.xridx (kk++) = c_lhs.ridx (i); |
|
2671 i++; |
|
2672 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2673 ic++; |
|
2674 if (i < lhs_nz) |
|
2675 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2676 } |
|
2677 else |
|
2678 { |
|
2679 while (kc <= jc) |
|
2680 stmp.xcidx (kc++) = kk; |
|
2681 RT rtmp = rhs.elem (j); |
|
2682 if (rtmp != RT ()) |
|
2683 { |
|
2684 stmp.xdata (kk) = rtmp; |
|
2685 stmp.xridx (kk++) = jr; |
|
2686 } |
|
2687 if (ii == jj) |
|
2688 { |
|
2689 i++; |
|
2690 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2691 ic++; |
|
2692 if (i < lhs_nz) |
|
2693 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2694 } |
|
2695 j++; |
|
2696 if (j < len) |
|
2697 { |
|
2698 jj = idx_i.elem (j); |
|
2699 jr = jj % lhs_nr; |
|
2700 jc = (jj - jr) / lhs_nr; |
|
2701 } |
|
2702 } |
|
2703 } |
|
2704 |
|
2705 for (int iidx = kc; iidx < lhs_nc+1; iidx++) |
|
2706 stmp.xcidx(iidx) = kk; |
|
2707 |
|
2708 |
|
2709 lhs = stmp; |
|
2710 } |
|
2711 else if (rhs_nr == 1 && rhs_nc == 1) |
|
2712 { |
|
2713 RT scalar = rhs.elem (0, 0); |
|
2714 int new_nnz = lhs_nz; |
|
2715 |
|
2716 // First count the number of non-zero elements |
|
2717 if (scalar != RT ()) |
|
2718 new_nnz += len; |
|
2719 for (int i = 0; i < len; i++) |
|
2720 { |
|
2721 OCTAVE_QUIT; |
|
2722 int ii = idx_i.elem (i); |
|
2723 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
|
2724 new_nnz--; |
|
2725 } |
|
2726 |
|
2727 Sparse<LT> stmp (lhs_nr, lhs_nc, new_nnz); |
|
2728 |
|
2729 int i = 0; |
|
2730 int ii = 0; |
|
2731 int ic = 0; |
|
2732 if (i < lhs_nz) |
|
2733 { |
|
2734 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2735 ic++; |
|
2736 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2737 } |
|
2738 |
|
2739 int j = 0; |
|
2740 int jj = idx_i.elem (j); |
|
2741 int jr = jj % lhs_nr; |
|
2742 int jc = (jj - jr) / lhs_nr; |
|
2743 |
|
2744 int kk = 0; |
|
2745 int kc = 0; |
|
2746 |
|
2747 while (j < len || i < lhs_nz) |
|
2748 { |
|
2749 if (j == len || (i < lhs_nz && ii < jj)) |
|
2750 { |
|
2751 while (kc <= ic) |
|
2752 stmp.xcidx (kc++) = kk; |
|
2753 stmp.xdata (kk) = c_lhs.data (i); |
|
2754 stmp.xridx (kk++) = c_lhs.ridx (i); |
|
2755 i++; |
|
2756 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2757 ic++; |
|
2758 if (i < lhs_nz) |
|
2759 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2760 } |
|
2761 else |
|
2762 { |
|
2763 while (kc <= jc) |
|
2764 stmp.xcidx (kc++) = kk; |
|
2765 if (scalar != RT ()) |
|
2766 { |
|
2767 stmp.xdata (kk) = scalar; |
|
2768 stmp.xridx (kk++) = jr; |
|
2769 } |
|
2770 if (ii == jj) |
|
2771 { |
|
2772 i++; |
|
2773 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2774 ic++; |
|
2775 if (i < lhs_nz) |
|
2776 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2777 } |
|
2778 j++; |
|
2779 if (j < len) |
|
2780 { |
|
2781 jj = idx_i.elem (j); |
|
2782 jr = jj % lhs_nr; |
|
2783 jc = (jj - jr) / lhs_nr; |
|
2784 } |
|
2785 } |
|
2786 } |
|
2787 |
|
2788 for (int iidx = kc; iidx < lhs_nc+1; iidx++) |
|
2789 stmp.xcidx(iidx) = kk; |
|
2790 |
|
2791 lhs = stmp; |
|
2792 } |
|
2793 else |
|
2794 { |
|
2795 (*current_liboctave_error_handler) |
|
2796 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
2797 |
|
2798 retval = 0; |
|
2799 } |
|
2800 } |
|
2801 // idx_vector::freeze() printed an error message for us. |
|
2802 } |
|
2803 } |
|
2804 else |
|
2805 { |
|
2806 (*current_liboctave_error_handler) |
|
2807 ("invalid number of indices for matrix expression"); |
|
2808 |
|
2809 retval = 0; |
|
2810 } |
|
2811 |
|
2812 lhs.clear_index (); |
|
2813 |
|
2814 return retval; |
|
2815 } |
|
2816 |
|
2817 template <class T> |
|
2818 void |
|
2819 Sparse<T>::print_info (std::ostream& os, const std::string& prefix) const |
|
2820 { |
|
2821 os << prefix << "rep address: " << rep << "\n" |
|
2822 << prefix << "rep->nnz: " << rep->nnz << "\n" |
|
2823 << prefix << "rep->nrows: " << rep->nrows << "\n" |
|
2824 << prefix << "rep->ncols: " << rep->ncols << "\n" |
|
2825 << prefix << "rep->data: " << static_cast<void *> (rep->d) << "\n" |
|
2826 << prefix << "rep->ridx: " << static_cast<void *> (rep->r) << "\n" |
|
2827 << prefix << "rep->cidx: " << static_cast<void *> (rep->c) << "\n" |
|
2828 << prefix << "rep->count: " << rep->count << "\n"; |
|
2829 } |
|
2830 |
|
2831 /* |
|
2832 ;;; Local Variables: *** |
|
2833 ;;; mode: C++ *** |
|
2834 ;;; End: *** |
|
2835 */ |