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