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