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