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