5164
|
1 // Template sparse array class |
|
2 /* |
|
3 |
|
4 Copyright (C) 2004 David Bateman |
|
5 Copyright (C) 1998-2004 Andy Adler |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
5307
|
18 along with this program; see the file COPYING. If not, write to the |
|
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
20 Boston, MA 02110-1301, USA. |
5164
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cassert> |
|
29 #include <climits> |
|
30 |
|
31 #include <iostream> |
5765
|
32 #include <sstream> |
5164
|
33 #include <vector> |
|
34 |
|
35 #include "Array.h" |
|
36 #include "Array-util.h" |
|
37 #include "Range.h" |
|
38 #include "idx-vector.h" |
|
39 #include "lo-error.h" |
|
40 #include "quit.h" |
|
41 |
|
42 #include "Sparse.h" |
|
43 #include "sparse-sort.h" |
|
44 #include "oct-spparms.h" |
|
45 |
|
46 template <class T> |
|
47 T& |
5275
|
48 Sparse<T>::SparseRep::elem (octave_idx_type _r, octave_idx_type _c) |
5164
|
49 { |
5275
|
50 octave_idx_type i; |
5164
|
51 |
5604
|
52 if (nzmx > 0) |
5164
|
53 { |
|
54 for (i = c[_c]; i < c[_c + 1]; i++) |
|
55 if (r[i] == _r) |
|
56 return d[i]; |
|
57 else if (r[i] > _r) |
|
58 break; |
|
59 |
|
60 // Ok, If we've gotten here, we're in trouble.. Have to create a |
|
61 // new element in the sparse array. This' gonna be slow!!! |
5869
|
62 if (c[ncols] == nzmx) |
5164
|
63 { |
|
64 (*current_liboctave_error_handler) |
5275
|
65 ("Sparse::SparseRep::elem (octave_idx_type, octave_idx_type): sparse matrix filled"); |
5164
|
66 return *d; |
|
67 } |
|
68 |
5275
|
69 octave_idx_type to_move = c[ncols] - i; |
5164
|
70 if (to_move != 0) |
|
71 { |
5275
|
72 for (octave_idx_type j = c[ncols]; j > i; j--) |
5164
|
73 { |
|
74 d[j] = d[j-1]; |
|
75 r[j] = r[j-1]; |
|
76 } |
|
77 } |
|
78 |
5275
|
79 for (octave_idx_type j = _c + 1; j < ncols + 1; j++) |
5164
|
80 c[j] = c[j] + 1; |
|
81 |
|
82 d[i] = 0.; |
|
83 r[i] = _r; |
|
84 |
|
85 return d[i]; |
|
86 } |
|
87 else |
|
88 { |
|
89 (*current_liboctave_error_handler) |
5275
|
90 ("Sparse::SparseRep::elem (octave_idx_type, octave_idx_type): sparse matrix filled"); |
5164
|
91 return *d; |
|
92 } |
|
93 } |
|
94 |
|
95 template <class T> |
|
96 T |
5275
|
97 Sparse<T>::SparseRep::celem (octave_idx_type _r, octave_idx_type _c) const |
5164
|
98 { |
5604
|
99 if (nzmx > 0) |
5275
|
100 for (octave_idx_type i = c[_c]; i < c[_c + 1]; i++) |
5164
|
101 if (r[i] == _r) |
|
102 return d[i]; |
|
103 return T (); |
|
104 } |
|
105 |
|
106 template <class T> |
|
107 void |
|
108 Sparse<T>::SparseRep::maybe_compress (bool remove_zeros) |
|
109 { |
5604
|
110 octave_idx_type ndel = nzmx - c[ncols]; |
5275
|
111 octave_idx_type nzero = 0; |
5164
|
112 |
|
113 if (remove_zeros) |
5604
|
114 for (octave_idx_type i = 0; i < nzmx - ndel; i++) |
5164
|
115 if (d[i] == T ()) |
|
116 nzero++; |
|
117 |
|
118 if (!ndel && !nzero) |
|
119 return; |
|
120 |
|
121 if (!nzero) |
|
122 { |
5604
|
123 octave_idx_type new_nzmx = nzmx - ndel; |
|
124 |
|
125 T *new_data = new T [new_nzmx]; |
|
126 for (octave_idx_type i = 0; i < new_nzmx; i++) |
5164
|
127 new_data[i] = d[i]; |
|
128 delete [] d; |
|
129 d = new_data; |
|
130 |
5604
|
131 octave_idx_type *new_ridx = new octave_idx_type [new_nzmx]; |
|
132 for (octave_idx_type i = 0; i < new_nzmx; i++) |
5164
|
133 new_ridx[i] = r[i]; |
|
134 delete [] r; |
|
135 r = new_ridx; |
|
136 } |
|
137 else |
|
138 { |
5604
|
139 octave_idx_type new_nzmx = nzmx - ndel - nzero; |
|
140 |
|
141 T *new_data = new T [new_nzmx]; |
|
142 octave_idx_type *new_ridx = new octave_idx_type [new_nzmx]; |
5275
|
143 |
|
144 octave_idx_type ii = 0; |
|
145 octave_idx_type ic = 0; |
|
146 for (octave_idx_type j = 0; j < ncols; j++) |
5164
|
147 { |
5275
|
148 for (octave_idx_type k = ic; k < c[j+1]; k++) |
5164
|
149 if (d[k] != T ()) |
|
150 { |
|
151 new_data [ii] = d[k]; |
|
152 new_ridx [ii++] = r[k]; |
|
153 } |
|
154 ic = c[j+1]; |
|
155 c[j+1] = ii; |
|
156 } |
|
157 |
|
158 delete [] d; |
|
159 d = new_data; |
|
160 |
|
161 delete [] r; |
|
162 r = new_ridx; |
|
163 } |
|
164 |
5604
|
165 nzmx -= ndel + nzero; |
5164
|
166 } |
|
167 |
|
168 template <class T> |
|
169 void |
5275
|
170 Sparse<T>::SparseRep::change_length (octave_idx_type nz) |
5164
|
171 { |
5604
|
172 if (nz != nzmx) |
5164
|
173 { |
5604
|
174 octave_idx_type min_nzmx = (nz < nzmx ? nz : nzmx); |
5275
|
175 |
|
176 octave_idx_type * new_ridx = new octave_idx_type [nz]; |
5604
|
177 for (octave_idx_type i = 0; i < min_nzmx; i++) |
5164
|
178 new_ridx[i] = r[i]; |
|
179 |
|
180 delete [] r; |
|
181 r = new_ridx; |
|
182 |
|
183 T * new_data = new T [nz]; |
5604
|
184 for (octave_idx_type i = 0; i < min_nzmx; i++) |
5164
|
185 new_data[i] = d[i]; |
|
186 |
|
187 delete [] d; |
|
188 d = new_data; |
|
189 |
5604
|
190 if (nz < nzmx) |
5275
|
191 for (octave_idx_type i = 0; i <= ncols; i++) |
5164
|
192 if (c[i] > nz) |
|
193 c[i] = nz; |
|
194 |
5604
|
195 nzmx = nz; |
5164
|
196 } |
|
197 } |
|
198 |
|
199 template <class T> |
|
200 template <class U> |
|
201 Sparse<T>::Sparse (const Sparse<U>& a) |
|
202 : dimensions (a.dimensions), idx (0), idx_count (0) |
|
203 { |
5681
|
204 if (a.nnz () == 0) |
5164
|
205 rep = new typename Sparse<T>::SparseRep (rows (), cols()); |
|
206 else |
|
207 { |
5681
|
208 rep = new typename Sparse<T>::SparseRep (rows (), cols (), a.nnz ()); |
5164
|
209 |
5681
|
210 octave_idx_type nz = a.nnz (); |
5275
|
211 octave_idx_type nc = cols (); |
|
212 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
213 { |
|
214 xdata (i) = T (a.data (i)); |
|
215 xridx (i) = a.ridx (i); |
|
216 } |
5275
|
217 for (octave_idx_type i = 0; i < nc + 1; i++) |
5164
|
218 xcidx (i) = a.cidx (i); |
|
219 } |
|
220 } |
|
221 |
|
222 template <class T> |
5275
|
223 Sparse<T>::Sparse (octave_idx_type nr, octave_idx_type nc, T val) |
5630
|
224 : dimensions (dim_vector (nr, nc)), idx (0), idx_count (0) |
5164
|
225 { |
5630
|
226 if (val != T ()) |
5164
|
227 { |
5630
|
228 rep = new typename Sparse<T>::SparseRep (nr, nc, nr*nc); |
|
229 |
|
230 octave_idx_type ii = 0; |
|
231 xcidx (0) = 0; |
|
232 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
233 { |
5630
|
234 for (octave_idx_type i = 0; i < nr; i++) |
|
235 { |
|
236 xdata (ii) = val; |
|
237 xridx (ii++) = i; |
|
238 } |
|
239 xcidx (j+1) = ii; |
|
240 } |
|
241 } |
|
242 else |
|
243 { |
|
244 rep = new typename Sparse<T>::SparseRep (nr, nc, 0); |
|
245 for (octave_idx_type j = 0; j < nc+1; j++) |
|
246 xcidx(j) = 0; |
5164
|
247 } |
|
248 } |
|
249 |
|
250 template <class T> |
|
251 Sparse<T>::Sparse (const dim_vector& dv) |
|
252 : dimensions (dv), idx (0), idx_count (0) |
|
253 { |
|
254 if (dv.length() != 2) |
|
255 (*current_liboctave_error_handler) |
|
256 ("Sparse::Sparse (const dim_vector&): dimension mismatch"); |
|
257 else |
|
258 rep = new typename Sparse<T>::SparseRep (dv(0), dv(1)); |
|
259 } |
|
260 |
|
261 template <class T> |
|
262 Sparse<T>::Sparse (const Sparse<T>& a, const dim_vector& dv) |
|
263 : dimensions (dv), idx (0), idx_count (0) |
|
264 { |
|
265 |
|
266 // Work in unsigned long long to avoid overflow issues with numel |
|
267 unsigned long long a_nel = static_cast<unsigned long long>(a.rows ()) * |
|
268 static_cast<unsigned long long>(a.cols ()); |
|
269 unsigned long long dv_nel = static_cast<unsigned long long>(dv (0)) * |
|
270 static_cast<unsigned long long>(dv (1)); |
|
271 |
|
272 if (a_nel != dv_nel) |
|
273 (*current_liboctave_error_handler) |
|
274 ("Sparse::Sparse (const Sparse&, const dim_vector&): dimension mismatch"); |
|
275 else |
|
276 { |
|
277 dim_vector old_dims = a.dims(); |
5681
|
278 octave_idx_type new_nzmx = a.nnz (); |
5275
|
279 octave_idx_type new_nr = dv (0); |
|
280 octave_idx_type new_nc = dv (1); |
|
281 octave_idx_type old_nr = old_dims (0); |
|
282 octave_idx_type old_nc = old_dims (1); |
5164
|
283 |
5604
|
284 rep = new typename Sparse<T>::SparseRep (new_nr, new_nc, new_nzmx); |
5164
|
285 |
5275
|
286 octave_idx_type kk = 0; |
5164
|
287 xcidx(0) = 0; |
5275
|
288 for (octave_idx_type i = 0; i < old_nc; i++) |
|
289 for (octave_idx_type j = a.cidx(i); j < a.cidx(i+1); j++) |
5164
|
290 { |
5275
|
291 octave_idx_type tmp = i * old_nr + a.ridx(j); |
|
292 octave_idx_type ii = tmp % new_nr; |
|
293 octave_idx_type jj = (tmp - ii) / new_nr; |
|
294 for (octave_idx_type k = kk; k < jj; k++) |
5164
|
295 xcidx(k+1) = j; |
|
296 kk = jj; |
|
297 xdata(j) = a.data(j); |
|
298 xridx(j) = ii; |
|
299 } |
5275
|
300 for (octave_idx_type k = kk; k < new_nc; k++) |
5604
|
301 xcidx(k+1) = new_nzmx; |
5164
|
302 } |
|
303 } |
|
304 |
|
305 template <class T> |
5275
|
306 Sparse<T>::Sparse (const Array<T>& a, const Array<octave_idx_type>& r, |
|
307 const Array<octave_idx_type>& c, octave_idx_type nr, |
|
308 octave_idx_type nc, bool sum_terms) |
5164
|
309 : dimensions (dim_vector (nr, nc)), idx (0), idx_count (0) |
|
310 { |
5275
|
311 octave_idx_type a_len = a.length (); |
|
312 octave_idx_type r_len = r.length (); |
|
313 octave_idx_type c_len = c.length (); |
5164
|
314 bool ri_scalar = (r_len == 1); |
|
315 bool ci_scalar = (c_len == 1); |
|
316 bool cf_scalar = (a_len == 1); |
|
317 |
|
318 if ((a_len != r_len && !cf_scalar && !ri_scalar) || |
|
319 (a_len != c_len && !cf_scalar && !ci_scalar) || |
|
320 (r_len != c_len && !ri_scalar && !ci_scalar) || nr < 0 || nc < 0) |
|
321 { |
|
322 (*current_liboctave_error_handler) |
5275
|
323 ("Sparse::Sparse (const Array<T>&, const Array<octave_idx_type>&, ...): dimension mismatch"); |
5164
|
324 rep = nil_rep (); |
|
325 dimensions = dim_vector (0, 0); |
|
326 } |
|
327 else |
|
328 { |
5604
|
329 octave_idx_type max_nzmx = (r_len > c_len ? r_len : c_len); |
|
330 |
|
331 OCTAVE_LOCAL_BUFFER (octave_sparse_sort_idxl *, sidx, max_nzmx); |
|
332 OCTAVE_LOCAL_BUFFER (octave_sparse_sort_idxl, sidxX, max_nzmx); |
|
333 |
|
334 for (octave_idx_type i = 0; i < max_nzmx; i++) |
5164
|
335 sidx[i] = &sidxX[i]; |
|
336 |
5604
|
337 octave_idx_type actual_nzmx = 0; |
5164
|
338 OCTAVE_QUIT; |
5604
|
339 for (octave_idx_type i = 0; i < max_nzmx; i++) |
5164
|
340 { |
5275
|
341 octave_idx_type rowidx = (ri_scalar ? r(0) : r(i)); |
|
342 octave_idx_type colidx = (ci_scalar ? c(0) : c(i)); |
5164
|
343 if (rowidx < nr && rowidx >= 0 && |
|
344 colidx < nc && colidx >= 0 ) |
|
345 { |
|
346 if ( a (cf_scalar ? 0 : i ) != T ()) |
|
347 { |
5604
|
348 sidx[actual_nzmx]->r = rowidx; |
|
349 sidx[actual_nzmx]->c = colidx; |
|
350 sidx[actual_nzmx]->idx = i; |
|
351 actual_nzmx++; |
5164
|
352 } |
|
353 } |
|
354 else |
|
355 { |
|
356 (*current_liboctave_error_handler) |
|
357 ("Sparse::Sparse : index (%d,%d) out of range", |
|
358 rowidx + 1, colidx + 1); |
|
359 rep = nil_rep (); |
|
360 dimensions = dim_vector (0, 0); |
|
361 return; |
|
362 } |
|
363 } |
|
364 |
5604
|
365 if (actual_nzmx == 0) |
5164
|
366 rep = new typename Sparse<T>::SparseRep (nr, nc); |
|
367 else |
|
368 { |
|
369 OCTAVE_QUIT; |
|
370 octave_sort<octave_sparse_sort_idxl *> |
|
371 sort (octave_sparse_sidxl_comp); |
|
372 |
5604
|
373 sort.sort (sidx, actual_nzmx); |
5164
|
374 OCTAVE_QUIT; |
|
375 |
|
376 // Now count the unique non-zero values |
5604
|
377 octave_idx_type real_nzmx = 1; |
|
378 for (octave_idx_type i = 1; i < actual_nzmx; i++) |
5164
|
379 if (sidx[i-1]->r != sidx[i]->r || sidx[i-1]->c != sidx[i]->c) |
5604
|
380 real_nzmx++; |
|
381 |
|
382 rep = new typename Sparse<T>::SparseRep (nr, nc, real_nzmx); |
5164
|
383 |
5275
|
384 octave_idx_type cx = 0; |
|
385 octave_idx_type prev_rval = -1; |
|
386 octave_idx_type prev_cval = -1; |
|
387 octave_idx_type ii = -1; |
5164
|
388 xcidx (0) = 0; |
5604
|
389 for (octave_idx_type i = 0; i < actual_nzmx; i++) |
5164
|
390 { |
|
391 OCTAVE_QUIT; |
5275
|
392 octave_idx_type iidx = sidx[i]->idx; |
|
393 octave_idx_type rval = sidx[i]->r; |
|
394 octave_idx_type cval = sidx[i]->c; |
5164
|
395 |
|
396 if (prev_cval < cval || (prev_rval < rval && prev_cval == cval)) |
|
397 { |
5275
|
398 octave_idx_type ci = static_cast<octave_idx_type> (c (ci_scalar ? 0 : iidx)); |
5164
|
399 ii++; |
|
400 while (cx < ci) |
|
401 xcidx (++cx) = ii; |
|
402 xdata(ii) = a (cf_scalar ? 0 : iidx); |
5275
|
403 xridx(ii) = static_cast<octave_idx_type> (r (ri_scalar ? 0 : iidx)); |
5164
|
404 } |
|
405 else |
|
406 { |
|
407 if (sum_terms) |
|
408 xdata(ii) += a (cf_scalar ? 0 : iidx); |
|
409 else |
|
410 xdata(ii) = a (cf_scalar ? 0 : iidx); |
|
411 } |
|
412 prev_rval = rval; |
|
413 prev_cval = cval; |
|
414 } |
|
415 |
|
416 while (cx < nc) |
|
417 xcidx (++cx) = ii + 1; |
|
418 } |
|
419 } |
|
420 } |
|
421 |
|
422 template <class T> |
|
423 Sparse<T>::Sparse (const Array<T>& a, const Array<double>& r, |
5275
|
424 const Array<double>& c, octave_idx_type nr, |
|
425 octave_idx_type nc, bool sum_terms) |
5164
|
426 : dimensions (dim_vector (nr, nc)), idx (0), idx_count (0) |
|
427 { |
5275
|
428 octave_idx_type a_len = a.length (); |
|
429 octave_idx_type r_len = r.length (); |
|
430 octave_idx_type c_len = c.length (); |
5164
|
431 bool ri_scalar = (r_len == 1); |
|
432 bool ci_scalar = (c_len == 1); |
|
433 bool cf_scalar = (a_len == 1); |
|
434 |
|
435 if ((a_len != r_len && !cf_scalar && !ri_scalar) || |
|
436 (a_len != c_len && !cf_scalar && !ci_scalar) || |
|
437 (r_len != c_len && !ri_scalar && !ci_scalar) || nr < 0 || nc < 0) |
|
438 { |
|
439 (*current_liboctave_error_handler) |
|
440 ("Sparse::Sparse (const Array<T>&, const Array<double>&, ...): dimension mismatch"); |
|
441 rep = nil_rep (); |
|
442 dimensions = dim_vector (0, 0); |
|
443 } |
|
444 else |
|
445 { |
5604
|
446 octave_idx_type max_nzmx = (r_len > c_len ? r_len : c_len); |
5164
|
447 |
5604
|
448 OCTAVE_LOCAL_BUFFER (octave_sparse_sort_idxl *, sidx, max_nzmx); |
|
449 OCTAVE_LOCAL_BUFFER (octave_sparse_sort_idxl, sidxX, max_nzmx); |
|
450 |
|
451 for (octave_idx_type i = 0; i < max_nzmx; i++) |
5164
|
452 sidx[i] = &sidxX[i]; |
|
453 |
5604
|
454 octave_idx_type actual_nzmx = 0; |
5164
|
455 OCTAVE_QUIT; |
|
456 |
5604
|
457 for (octave_idx_type i = 0; i < max_nzmx; i++) |
5164
|
458 { |
5275
|
459 octave_idx_type rowidx = static_cast<octave_idx_type> (ri_scalar ? r(0) : r(i)); |
|
460 octave_idx_type colidx = static_cast<octave_idx_type> (ci_scalar ? c(0) : c(i)); |
5164
|
461 if (rowidx < nr && rowidx >= 0 && |
|
462 colidx < nc && colidx >= 0 ) |
|
463 { |
|
464 if ( a (cf_scalar ? 0 : i ) != T ()) |
|
465 { |
5604
|
466 sidx[actual_nzmx]->r = rowidx; |
|
467 sidx[actual_nzmx]->c = colidx; |
|
468 sidx[actual_nzmx]->idx = i; |
|
469 actual_nzmx++; |
5164
|
470 } |
|
471 } |
|
472 else |
|
473 { |
|
474 (*current_liboctave_error_handler) |
|
475 ("Sparse::Sparse : index (%d,%d) out of range", |
|
476 rowidx + 1, colidx + 1); |
|
477 rep = nil_rep (); |
|
478 dimensions = dim_vector (0, 0); |
|
479 return; |
|
480 } |
|
481 } |
|
482 |
5604
|
483 if (actual_nzmx == 0) |
5164
|
484 rep = new typename Sparse<T>::SparseRep (nr, nc); |
|
485 else |
|
486 { |
|
487 OCTAVE_QUIT; |
|
488 octave_sort<octave_sparse_sort_idxl *> |
|
489 sort (octave_sparse_sidxl_comp); |
|
490 |
5604
|
491 sort.sort (sidx, actual_nzmx); |
5164
|
492 OCTAVE_QUIT; |
|
493 |
|
494 // Now count the unique non-zero values |
5604
|
495 octave_idx_type real_nzmx = 1; |
|
496 for (octave_idx_type i = 1; i < actual_nzmx; i++) |
5164
|
497 if (sidx[i-1]->r != sidx[i]->r || sidx[i-1]->c != sidx[i]->c) |
5604
|
498 real_nzmx++; |
|
499 |
|
500 rep = new typename Sparse<T>::SparseRep (nr, nc, real_nzmx); |
5164
|
501 |
5275
|
502 octave_idx_type cx = 0; |
|
503 octave_idx_type prev_rval = -1; |
|
504 octave_idx_type prev_cval = -1; |
|
505 octave_idx_type ii = -1; |
5164
|
506 xcidx (0) = 0; |
5604
|
507 for (octave_idx_type i = 0; i < actual_nzmx; i++) |
5164
|
508 { |
|
509 OCTAVE_QUIT; |
5275
|
510 octave_idx_type iidx = sidx[i]->idx; |
|
511 octave_idx_type rval = sidx[i]->r; |
|
512 octave_idx_type cval = sidx[i]->c; |
5164
|
513 |
|
514 if (prev_cval < cval || (prev_rval < rval && prev_cval == cval)) |
|
515 { |
5275
|
516 octave_idx_type ci = static_cast<octave_idx_type> (c (ci_scalar ? 0 : iidx)); |
5164
|
517 ii++; |
|
518 |
|
519 while (cx < ci) |
|
520 xcidx (++cx) = ii; |
|
521 xdata(ii) = a (cf_scalar ? 0 : iidx); |
5275
|
522 xridx(ii) = static_cast<octave_idx_type> (r (ri_scalar ? 0 : iidx)); |
5164
|
523 } |
|
524 else |
|
525 { |
|
526 if (sum_terms) |
|
527 xdata(ii) += a (cf_scalar ? 0 : iidx); |
|
528 else |
|
529 xdata(ii) = a (cf_scalar ? 0 : iidx); |
|
530 } |
|
531 prev_rval = rval; |
|
532 prev_cval = cval; |
|
533 } |
|
534 |
|
535 while (cx < nc) |
|
536 xcidx (++cx) = ii + 1; |
|
537 } |
|
538 } |
|
539 } |
|
540 |
|
541 template <class T> |
|
542 Sparse<T>::Sparse (const Array2<T>& a) |
|
543 : dimensions (a.dims ()), idx (0), idx_count (0) |
|
544 { |
5275
|
545 octave_idx_type nr = rows (); |
|
546 octave_idx_type nc = cols (); |
|
547 octave_idx_type len = a.length (); |
5604
|
548 octave_idx_type new_nzmx = 0; |
5164
|
549 |
|
550 // First count the number of non-zero terms |
5275
|
551 for (octave_idx_type i = 0; i < len; i++) |
5164
|
552 if (a(i) != T ()) |
5604
|
553 new_nzmx++; |
|
554 |
|
555 rep = new typename Sparse<T>::SparseRep (nr, nc, new_nzmx); |
5164
|
556 |
5275
|
557 octave_idx_type ii = 0; |
5164
|
558 xcidx(0) = 0; |
5275
|
559 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
560 { |
5275
|
561 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
562 if (a.elem (i,j) != T ()) |
|
563 { |
|
564 xdata(ii) = a.elem (i,j); |
|
565 xridx(ii++) = i; |
|
566 } |
|
567 xcidx(j+1) = ii; |
|
568 } |
|
569 } |
|
570 |
|
571 template <class T> |
|
572 Sparse<T>::Sparse (const Array<T>& a) |
|
573 : dimensions (a.dims ()), idx (0), idx_count (0) |
|
574 { |
|
575 if (dimensions.length () > 2) |
|
576 (*current_liboctave_error_handler) |
|
577 ("Sparse::Sparse (const Array<T>&): dimension mismatch"); |
|
578 else |
|
579 { |
5275
|
580 octave_idx_type nr = rows (); |
|
581 octave_idx_type nc = cols (); |
|
582 octave_idx_type len = a.length (); |
5604
|
583 octave_idx_type new_nzmx = 0; |
5164
|
584 |
|
585 // First count the number of non-zero terms |
5275
|
586 for (octave_idx_type i = 0; i < len; i++) |
5164
|
587 if (a(i) != T ()) |
5604
|
588 new_nzmx++; |
|
589 |
|
590 rep = new typename Sparse<T>::SparseRep (nr, nc, new_nzmx); |
5164
|
591 |
5275
|
592 octave_idx_type ii = 0; |
5164
|
593 xcidx(0) = 0; |
5275
|
594 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
595 { |
5275
|
596 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
597 if (a.elem (i,j) != T ()) |
|
598 { |
|
599 xdata(ii) = a.elem (i,j); |
|
600 xridx(ii++) = i; |
|
601 } |
|
602 xcidx(j+1) = ii; |
|
603 } |
|
604 } |
|
605 } |
|
606 |
|
607 template <class T> |
|
608 Sparse<T>::~Sparse (void) |
|
609 { |
|
610 if (--rep->count <= 0) |
|
611 delete rep; |
|
612 |
|
613 delete [] idx; |
|
614 } |
|
615 |
|
616 template <class T> |
5275
|
617 octave_idx_type |
|
618 Sparse<T>::compute_index (const Array<octave_idx_type>& ra_idx) const |
5164
|
619 { |
5275
|
620 octave_idx_type retval = -1; |
|
621 |
|
622 octave_idx_type n = dimensions.length (); |
5164
|
623 |
|
624 if (n > 0 && n == ra_idx.length ()) |
|
625 { |
|
626 retval = ra_idx(--n); |
|
627 |
|
628 while (--n >= 0) |
|
629 { |
|
630 retval *= dimensions(n); |
|
631 retval += ra_idx(n); |
|
632 } |
|
633 } |
|
634 else |
|
635 (*current_liboctave_error_handler) |
|
636 ("Sparse<T>::compute_index: invalid ra_idxing operation"); |
|
637 |
|
638 return retval; |
|
639 } |
|
640 |
|
641 template <class T> |
|
642 T |
5275
|
643 Sparse<T>::range_error (const char *fcn, octave_idx_type n) const |
5164
|
644 { |
|
645 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
|
646 return T (); |
|
647 } |
|
648 |
|
649 template <class T> |
|
650 T& |
5275
|
651 Sparse<T>::range_error (const char *fcn, octave_idx_type n) |
5164
|
652 { |
|
653 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); |
|
654 static T foo; |
|
655 return foo; |
|
656 } |
|
657 |
|
658 template <class T> |
|
659 T |
5275
|
660 Sparse<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j) const |
5164
|
661 { |
|
662 (*current_liboctave_error_handler) |
|
663 ("%s (%d, %d): range error", fcn, i, j); |
|
664 return T (); |
|
665 } |
|
666 |
|
667 template <class T> |
|
668 T& |
5275
|
669 Sparse<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j) |
5164
|
670 { |
|
671 (*current_liboctave_error_handler) |
|
672 ("%s (%d, %d): range error", fcn, i, j); |
|
673 static T foo; |
|
674 return foo; |
|
675 } |
|
676 |
|
677 template <class T> |
|
678 T |
5275
|
679 Sparse<T>::range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const |
5164
|
680 { |
5765
|
681 std::ostringstream buf; |
5164
|
682 |
|
683 buf << fcn << " ("; |
|
684 |
5275
|
685 octave_idx_type n = ra_idx.length (); |
5164
|
686 |
|
687 if (n > 0) |
|
688 buf << ra_idx(0); |
|
689 |
5275
|
690 for (octave_idx_type i = 1; i < n; i++) |
5164
|
691 buf << ", " << ra_idx(i); |
|
692 |
|
693 buf << "): range error"; |
5765
|
694 |
|
695 std::string buf_str = buf.str (); |
|
696 |
|
697 (*current_liboctave_error_handler) (buf_str.c_str ()); |
5164
|
698 |
|
699 return T (); |
|
700 } |
|
701 |
|
702 template <class T> |
|
703 T& |
5275
|
704 Sparse<T>::range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) |
5164
|
705 { |
5765
|
706 std::ostringstream buf; |
5164
|
707 |
|
708 buf << fcn << " ("; |
|
709 |
5275
|
710 octave_idx_type n = ra_idx.length (); |
5164
|
711 |
|
712 if (n > 0) |
|
713 buf << ra_idx(0); |
|
714 |
5275
|
715 for (octave_idx_type i = 1; i < n; i++) |
5164
|
716 buf << ", " << ra_idx(i); |
|
717 |
|
718 buf << "): range error"; |
|
719 |
5765
|
720 std::string buf_str = buf.str (); |
|
721 |
|
722 (*current_liboctave_error_handler) (buf_str.c_str ()); |
5164
|
723 |
|
724 static T foo; |
|
725 return foo; |
|
726 } |
|
727 |
|
728 template <class T> |
|
729 Sparse<T> |
|
730 Sparse<T>::reshape (const dim_vector& new_dims) const |
|
731 { |
|
732 Sparse<T> retval; |
|
733 |
|
734 if (dimensions != new_dims) |
|
735 { |
|
736 if (dimensions.numel () == new_dims.numel ()) |
|
737 { |
5681
|
738 octave_idx_type new_nnz = nnz (); |
5275
|
739 octave_idx_type new_nr = new_dims (0); |
|
740 octave_idx_type new_nc = new_dims (1); |
|
741 octave_idx_type old_nr = rows (); |
|
742 octave_idx_type old_nc = cols (); |
5681
|
743 retval = Sparse<T> (new_nr, new_nc, new_nnz); |
5164
|
744 |
5275
|
745 octave_idx_type kk = 0; |
5164
|
746 retval.xcidx(0) = 0; |
5275
|
747 for (octave_idx_type i = 0; i < old_nc; i++) |
|
748 for (octave_idx_type j = cidx(i); j < cidx(i+1); j++) |
5164
|
749 { |
5275
|
750 octave_idx_type tmp = i * old_nr + ridx(j); |
|
751 octave_idx_type ii = tmp % new_nr; |
|
752 octave_idx_type jj = (tmp - ii) / new_nr; |
|
753 for (octave_idx_type k = kk; k < jj; k++) |
5164
|
754 retval.xcidx(k+1) = j; |
|
755 kk = jj; |
|
756 retval.xdata(j) = data(j); |
|
757 retval.xridx(j) = ii; |
|
758 } |
5275
|
759 for (octave_idx_type k = kk; k < new_nc; k++) |
5681
|
760 retval.xcidx(k+1) = new_nnz; |
5164
|
761 } |
|
762 else |
|
763 (*current_liboctave_error_handler) ("reshape: size mismatch"); |
|
764 } |
|
765 else |
|
766 retval = *this; |
|
767 |
|
768 return retval; |
|
769 } |
|
770 |
|
771 template <class T> |
|
772 Sparse<T> |
5275
|
773 Sparse<T>::permute (const Array<octave_idx_type>& perm_vec, bool) const |
5164
|
774 { |
|
775 dim_vector dv = dims (); |
|
776 dim_vector dv_new; |
|
777 |
5275
|
778 octave_idx_type nd = dv.length (); |
5164
|
779 |
|
780 dv_new.resize (nd); |
|
781 |
|
782 // Need this array to check for identical elements in permutation array. |
|
783 Array<bool> checked (nd, false); |
|
784 |
|
785 // Find dimension vector of permuted array. |
5275
|
786 for (octave_idx_type i = 0; i < nd; i++) |
5164
|
787 { |
5275
|
788 octave_idx_type perm_el = perm_vec.elem (i); |
5164
|
789 |
|
790 if (perm_el > dv.length () || perm_el < 1) |
|
791 { |
|
792 (*current_liboctave_error_handler) |
|
793 ("permutation vector contains an invalid element"); |
|
794 |
|
795 return Sparse<T> (); |
|
796 } |
|
797 |
|
798 if (checked.elem(perm_el - 1)) |
|
799 { |
|
800 (*current_liboctave_error_handler) |
|
801 ("PERM cannot contain identical elements"); |
|
802 |
|
803 return Sparse<T> (); |
|
804 } |
|
805 else |
|
806 checked.elem(perm_el - 1) = true; |
|
807 |
|
808 dv_new (i) = dv (perm_el - 1); |
|
809 } |
|
810 |
|
811 if (dv_new == dv) |
|
812 return *this; |
|
813 else |
|
814 return transpose (); |
|
815 } |
|
816 |
|
817 template <class T> |
|
818 void |
|
819 Sparse<T>::resize_no_fill (const dim_vector& dv) |
|
820 { |
5275
|
821 octave_idx_type n = dv.length (); |
5164
|
822 |
|
823 if (n != 2) |
|
824 { |
|
825 (*current_liboctave_error_handler) ("sparse array must be 2-D"); |
|
826 return; |
|
827 } |
|
828 |
|
829 resize_no_fill (dv(0), dv(1)); |
|
830 } |
|
831 |
|
832 template <class T> |
|
833 void |
5275
|
834 Sparse<T>::resize_no_fill (octave_idx_type r, octave_idx_type c) |
5164
|
835 { |
|
836 if (r < 0 || c < 0) |
|
837 { |
|
838 (*current_liboctave_error_handler) |
|
839 ("can't resize to negative dimension"); |
|
840 return; |
|
841 } |
|
842 |
|
843 if (ndims () == 0) |
|
844 dimensions = dim_vector (0, 0); |
|
845 |
|
846 if (r == dim1 () && c == dim2 ()) |
|
847 return; |
|
848 |
5731
|
849 typename Sparse<T>::SparseRep *old_rep = rep; |
|
850 |
5275
|
851 octave_idx_type nc = cols (); |
|
852 octave_idx_type nr = rows (); |
5164
|
853 |
5681
|
854 if (nnz () == 0 || r == 0 || c == 0) |
5164
|
855 // Special case of redimensioning to/from a sparse matrix with |
|
856 // no elements |
|
857 rep = new typename Sparse<T>::SparseRep (r, c); |
|
858 else |
|
859 { |
5275
|
860 octave_idx_type n = 0; |
5164
|
861 Sparse<T> tmpval; |
|
862 if (r >= nr) |
|
863 { |
|
864 if (c > nc) |
5731
|
865 n = xcidx(nc); |
5164
|
866 else |
5731
|
867 n = xcidx(c); |
5164
|
868 |
|
869 tmpval = Sparse<T> (r, c, n); |
|
870 |
|
871 if (c > nc) |
|
872 { |
6101
|
873 for (octave_idx_type i = 0; i < nc + 1; i++) |
5731
|
874 tmpval.cidx(i) = xcidx(i); |
6101
|
875 for (octave_idx_type i = nc + 1; i < c + 1; i++) |
5164
|
876 tmpval.cidx(i) = tmpval.cidx(i-1); |
|
877 } |
|
878 else if (c <= nc) |
6101
|
879 for (octave_idx_type i = 0; i < c + 1; i++) |
5731
|
880 tmpval.cidx(i) = xcidx(i); |
5164
|
881 |
5275
|
882 for (octave_idx_type i = 0; i < n; i++) |
5164
|
883 { |
5731
|
884 tmpval.data(i) = xdata(i); |
|
885 tmpval.ridx(i) = xridx(i); |
5164
|
886 } |
|
887 } |
|
888 else |
|
889 { |
|
890 // Count how many non zero terms before we do anything |
6101
|
891 octave_idx_type min_nc = (c < nc ? c : nc); |
|
892 for (octave_idx_type i = 0; i < min_nc; i++) |
5731
|
893 for (octave_idx_type j = xcidx(i); j < xcidx(i+1); j++) |
|
894 if (xridx(j) < r) |
5164
|
895 n++; |
|
896 |
|
897 if (n) |
|
898 { |
|
899 // Now that we know the size we can do something |
|
900 tmpval = Sparse<T> (r, c, n); |
|
901 |
|
902 tmpval.cidx(0); |
6101
|
903 for (octave_idx_type i = 0, ii = 0; i < min_nc; i++) |
5164
|
904 { |
5731
|
905 for (octave_idx_type j = xcidx(i); j < xcidx(i+1); j++) |
|
906 if (xridx(j) < r) |
5164
|
907 { |
5731
|
908 tmpval.data(ii) = xdata(j); |
|
909 tmpval.ridx(ii++) = xridx(j); |
5164
|
910 } |
|
911 tmpval.cidx(i+1) = ii; |
|
912 } |
6101
|
913 if (c > min_nc) |
|
914 for (octave_idx_type i = nc; i < c; i++) |
|
915 tmpval.cidx(i+1) = tmpval.cidx(i); |
5164
|
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 { |
5781
|
1731 if (! (idx_arg.one_zero_only () |
|
1732 && idx_orig_rows == nr |
|
1733 && idx_orig_columns == nc)) |
|
1734 (*current_liboctave_warning_with_id_handler) |
|
1735 ("Octave:fortran-indexing", "single index used for sparse matrix"); |
5164
|
1736 |
|
1737 // This code is only for indexing matrices. The vector |
|
1738 // cases are handled above. |
|
1739 |
|
1740 idx_arg.freeze (nr * nc, "matrix", resize_ok); |
|
1741 |
|
1742 if (idx_arg) |
|
1743 { |
5275
|
1744 octave_idx_type result_nr = idx_orig_rows; |
|
1745 octave_idx_type result_nc = idx_orig_columns; |
5164
|
1746 |
|
1747 if (idx_arg.one_zero_only ()) |
|
1748 { |
|
1749 result_nr = idx_arg.ones_count (); |
|
1750 result_nc = (result_nr > 0 ? 1 : 0); |
|
1751 } |
|
1752 |
|
1753 if (nz < 1) |
|
1754 retval = Sparse<T> (result_nr, result_nc); |
|
1755 else |
|
1756 { |
|
1757 // Count number of non-zero elements |
5604
|
1758 octave_idx_type new_nzmx = 0; |
5275
|
1759 octave_idx_type kk = 0; |
|
1760 for (octave_idx_type j = 0; j < result_nc; j++) |
5164
|
1761 { |
5275
|
1762 for (octave_idx_type i = 0; i < result_nr; i++) |
5164
|
1763 { |
|
1764 OCTAVE_QUIT; |
|
1765 |
5275
|
1766 octave_idx_type ii = idx_arg.elem (kk++); |
5164
|
1767 if (ii < orig_len) |
|
1768 { |
5275
|
1769 octave_idx_type fr = ii % nr; |
|
1770 octave_idx_type fc = (ii - fr) / nr; |
|
1771 for (octave_idx_type k = cidx(fc); k < cidx(fc+1); k++) |
5164
|
1772 { |
|
1773 if (ridx(k) == fr) |
5604
|
1774 new_nzmx++; |
5164
|
1775 if (ridx(k) >= fr) |
|
1776 break; |
|
1777 } |
|
1778 } |
|
1779 } |
|
1780 } |
|
1781 |
5604
|
1782 retval = Sparse<T> (result_nr, result_nc, new_nzmx); |
5164
|
1783 |
|
1784 kk = 0; |
5275
|
1785 octave_idx_type jj = 0; |
5164
|
1786 retval.xcidx(0) = 0; |
5275
|
1787 for (octave_idx_type j = 0; j < result_nc; j++) |
5164
|
1788 { |
5275
|
1789 for (octave_idx_type i = 0; i < result_nr; i++) |
5164
|
1790 { |
|
1791 OCTAVE_QUIT; |
|
1792 |
5275
|
1793 octave_idx_type ii = idx_arg.elem (kk++); |
5164
|
1794 if (ii < orig_len) |
|
1795 { |
5275
|
1796 octave_idx_type fr = ii % nr; |
|
1797 octave_idx_type fc = (ii - fr) / nr; |
|
1798 for (octave_idx_type k = cidx(fc); k < cidx(fc+1); k++) |
5164
|
1799 { |
|
1800 if (ridx(k) == fr) |
|
1801 { |
|
1802 retval.xdata(jj) = data(k); |
|
1803 retval.xridx(jj++) = i; |
|
1804 } |
|
1805 if (ridx(k) >= fr) |
|
1806 break; |
|
1807 } |
|
1808 } |
|
1809 } |
|
1810 retval.xcidx(j+1) = jj; |
|
1811 } |
|
1812 } |
|
1813 // idx_vector::freeze() printed an error message for us. |
|
1814 } |
|
1815 } |
|
1816 |
|
1817 return retval; |
|
1818 } |
|
1819 |
|
1820 template <class T> |
|
1821 Sparse<T> |
|
1822 Sparse<T>::index (idx_vector& idx_i, idx_vector& idx_j, int resize_ok) const |
|
1823 { |
|
1824 Sparse<T> retval; |
|
1825 |
|
1826 assert (ndims () == 2); |
|
1827 |
5275
|
1828 octave_idx_type nr = dim1 (); |
|
1829 octave_idx_type nc = dim2 (); |
|
1830 |
|
1831 octave_idx_type n = idx_i.freeze (nr, "row", resize_ok); |
|
1832 octave_idx_type m = idx_j.freeze (nc, "column", resize_ok); |
5164
|
1833 |
|
1834 if (idx_i && idx_j) |
|
1835 { |
|
1836 if (idx_i.orig_empty () || idx_j.orig_empty () || n == 0 || m == 0) |
|
1837 { |
|
1838 retval.resize_no_fill (n, m); |
|
1839 } |
5681
|
1840 else |
5164
|
1841 { |
5681
|
1842 int idx_i_colon = idx_i.is_colon_equiv (nr); |
|
1843 int idx_j_colon = idx_j.is_colon_equiv (nc); |
|
1844 |
|
1845 if (idx_i_colon && idx_j_colon) |
|
1846 { |
|
1847 retval = *this; |
|
1848 } |
|
1849 else |
5164
|
1850 { |
5681
|
1851 // Identify if the indices have any repeated values |
|
1852 bool permutation = true; |
|
1853 |
|
1854 OCTAVE_LOCAL_BUFFER (octave_idx_type, itmp, |
|
1855 (nr > nc ? nr : nc)); |
|
1856 octave_sort<octave_idx_type> sort; |
|
1857 |
|
1858 if (n > nr || m > nc) |
|
1859 permutation = false; |
|
1860 |
|
1861 if (permutation && ! idx_i_colon) |
|
1862 { |
|
1863 // Can't use something like |
|
1864 // idx_vector tmp_idx = idx_i; |
|
1865 // tmp_idx.sort (true); |
|
1866 // if (tmp_idx.length(nr) != n) |
|
1867 // permutation = false; |
|
1868 // here as there is no make_unique function |
|
1869 // for idx_vector type. |
|
1870 for (octave_idx_type i = 0; i < n; i++) |
|
1871 itmp [i] = idx_i.elem (i); |
|
1872 sort.sort (itmp, n); |
|
1873 for (octave_idx_type i = 1; i < n; i++) |
|
1874 if (itmp[i-1] == itmp[i]) |
|
1875 { |
|
1876 permutation = false; |
|
1877 break; |
|
1878 } |
|
1879 } |
|
1880 if (permutation && ! idx_j_colon) |
|
1881 { |
|
1882 for (octave_idx_type i = 0; i < m; i++) |
|
1883 itmp [i] = idx_j.elem (i); |
|
1884 sort.sort (itmp, m); |
|
1885 for (octave_idx_type i = 1; i < m; i++) |
|
1886 if (itmp[i-1] == itmp[i]) |
|
1887 { |
|
1888 permutation = false; |
|
1889 break; |
|
1890 } |
|
1891 } |
|
1892 |
|
1893 if (permutation) |
5164
|
1894 { |
5681
|
1895 // Special case permutation like indexing for speed |
|
1896 retval = Sparse<T> (n, m, nnz ()); |
|
1897 octave_idx_type *ri = retval.xridx (); |
|
1898 |
5766
|
1899 std::vector<T> X (n); |
5681
|
1900 for (octave_idx_type i = 0; i < nr; i++) |
|
1901 itmp [i] = -1; |
|
1902 for (octave_idx_type i = 0; i < n; i++) |
|
1903 itmp[idx_i.elem(i)] = i; |
|
1904 |
|
1905 octave_idx_type kk = 0; |
|
1906 retval.xcidx(0) = 0; |
|
1907 for (octave_idx_type j = 0; j < m; j++) |
|
1908 { |
|
1909 octave_idx_type jj = idx_j.elem (j); |
|
1910 for (octave_idx_type i = cidx(jj); i < cidx(jj+1); i++) |
|
1911 { |
|
1912 octave_idx_type ii = itmp [ridx(i)]; |
|
1913 if (ii >= 0) |
|
1914 { |
|
1915 X [ii] = data (i); |
|
1916 retval.xridx (kk++) = ii; |
|
1917 } |
|
1918 } |
|
1919 sort.sort (ri + retval.xcidx (j), kk - retval.xcidx (j)); |
|
1920 for (octave_idx_type p = retval.xcidx (j); p < kk; p++) |
|
1921 retval.xdata (p) = X [retval.xridx (p)]; |
|
1922 retval.xcidx(j+1) = kk; |
|
1923 } |
|
1924 retval.maybe_compress (); |
|
1925 } |
|
1926 else |
|
1927 { |
|
1928 // First count the number of non-zero elements |
|
1929 octave_idx_type new_nzmx = 0; |
|
1930 for (octave_idx_type j = 0; j < m; j++) |
5164
|
1931 { |
5681
|
1932 octave_idx_type jj = idx_j.elem (j); |
|
1933 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1934 { |
5681
|
1935 OCTAVE_QUIT; |
|
1936 |
|
1937 octave_idx_type ii = idx_i.elem (i); |
|
1938 if (ii < nr && jj < nc) |
|
1939 { |
|
1940 for (octave_idx_type k = cidx(jj); k < cidx(jj+1); k++) |
|
1941 { |
|
1942 if (ridx(k) == ii) |
|
1943 new_nzmx++; |
|
1944 if (ridx(k) >= ii) |
|
1945 break; |
|
1946 } |
|
1947 } |
5164
|
1948 } |
|
1949 } |
5681
|
1950 |
|
1951 retval = Sparse<T> (n, m, new_nzmx); |
|
1952 |
|
1953 octave_idx_type kk = 0; |
|
1954 retval.xcidx(0) = 0; |
|
1955 for (octave_idx_type j = 0; j < m; j++) |
|
1956 { |
|
1957 octave_idx_type jj = idx_j.elem (j); |
|
1958 for (octave_idx_type i = 0; i < n; i++) |
|
1959 { |
|
1960 OCTAVE_QUIT; |
|
1961 |
|
1962 octave_idx_type ii = idx_i.elem (i); |
|
1963 if (ii < nr && jj < nc) |
|
1964 { |
|
1965 for (octave_idx_type k = cidx(jj); k < cidx(jj+1); k++) |
|
1966 { |
|
1967 if (ridx(k) == ii) |
|
1968 { |
|
1969 retval.xdata(kk) = data(k); |
|
1970 retval.xridx(kk++) = i; |
|
1971 } |
|
1972 if (ridx(k) >= ii) |
|
1973 break; |
|
1974 } |
|
1975 } |
|
1976 } |
|
1977 retval.xcidx(j+1) = kk; |
|
1978 } |
5164
|
1979 } |
|
1980 } |
|
1981 } |
|
1982 } |
|
1983 // idx_vector::freeze() printed an error message for us. |
|
1984 |
|
1985 return retval; |
|
1986 } |
|
1987 |
|
1988 template <class T> |
|
1989 Sparse<T> |
|
1990 Sparse<T>::index (Array<idx_vector>& ra_idx, int resize_ok) const |
|
1991 { |
|
1992 |
|
1993 if (ra_idx.length () != 2) |
|
1994 { |
|
1995 (*current_liboctave_error_handler) ("range error for index"); |
|
1996 return *this; |
|
1997 } |
|
1998 |
|
1999 return index (ra_idx (0), ra_idx (1), resize_ok); |
|
2000 } |
|
2001 |
5775
|
2002 // FIXME |
5164
|
2003 // Unfortunately numel can overflow for very large but very sparse matrices. |
|
2004 // For now just flag an error when this happens. |
|
2005 template <class LT, class RT> |
|
2006 int |
|
2007 assign1 (Sparse<LT>& lhs, const Sparse<RT>& rhs) |
|
2008 { |
|
2009 int retval = 1; |
|
2010 |
|
2011 idx_vector *idx_tmp = lhs.get_idx (); |
|
2012 |
|
2013 idx_vector lhs_idx = idx_tmp[0]; |
|
2014 |
5275
|
2015 octave_idx_type lhs_len = lhs.numel (); |
|
2016 octave_idx_type rhs_len = rhs.numel (); |
5164
|
2017 |
5828
|
2018 uint64_t long_lhs_len = |
|
2019 static_cast<uint64_t> (lhs.rows ()) * |
|
2020 static_cast<uint64_t> (lhs.cols ()); |
|
2021 |
|
2022 uint64_t long_rhs_len = |
|
2023 static_cast<uint64_t> (rhs.rows ()) * |
|
2024 static_cast<uint64_t> (rhs.cols ()); |
|
2025 |
|
2026 if (long_rhs_len != static_cast<uint64_t>(rhs_len) || |
|
2027 long_lhs_len != static_cast<uint64_t>(lhs_len)) |
5164
|
2028 { |
|
2029 (*current_liboctave_error_handler) |
|
2030 ("A(I) = X: Matrix dimensions too large to ensure correct\n", |
|
2031 "operation. This is an limitation that should be removed\n", |
|
2032 "in the future."); |
|
2033 |
|
2034 lhs.clear_index (); |
|
2035 return 0; |
|
2036 } |
|
2037 |
5275
|
2038 octave_idx_type nr = lhs.rows (); |
|
2039 octave_idx_type nc = lhs.cols (); |
5681
|
2040 octave_idx_type nz = lhs.nnz (); |
5275
|
2041 |
5781
|
2042 octave_idx_type n = lhs_idx.freeze (lhs_len, "vector", true); |
5164
|
2043 |
|
2044 if (n != 0) |
|
2045 { |
5275
|
2046 octave_idx_type max_idx = lhs_idx.max () + 1; |
5164
|
2047 max_idx = max_idx < lhs_len ? lhs_len : max_idx; |
|
2048 |
|
2049 // Take a constant copy of lhs. This means that elem won't |
|
2050 // create missing elements. |
|
2051 const Sparse<LT> c_lhs (lhs); |
|
2052 |
|
2053 if (rhs_len == n) |
|
2054 { |
5681
|
2055 octave_idx_type new_nzmx = lhs.nnz (); |
5164
|
2056 |
5603
|
2057 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx, n); |
|
2058 if (! lhs_idx.is_colon ()) |
|
2059 { |
|
2060 // Ok here we have to be careful with the indexing, |
|
2061 // to treat cases like "a([3,2,1]) = b", and still |
|
2062 // handle the need for strict sorting of the sparse |
|
2063 // elements. |
|
2064 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, sidx, n); |
|
2065 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, sidxX, n); |
|
2066 |
|
2067 for (octave_idx_type i = 0; i < n; i++) |
|
2068 { |
|
2069 sidx[i] = &sidxX[i]; |
|
2070 sidx[i]->i = lhs_idx.elem(i); |
|
2071 sidx[i]->idx = i; |
|
2072 } |
|
2073 |
|
2074 OCTAVE_QUIT; |
|
2075 octave_sort<octave_idx_vector_sort *> |
|
2076 sort (octave_idx_vector_comp); |
|
2077 |
|
2078 sort.sort (sidx, n); |
|
2079 |
|
2080 intNDArray<octave_idx_type> new_idx (dim_vector (n,1)); |
|
2081 |
|
2082 for (octave_idx_type i = 0; i < n; i++) |
|
2083 { |
|
2084 new_idx.xelem(i) = sidx[i]->i + 1; |
|
2085 rhs_idx[i] = sidx[i]->idx; |
|
2086 } |
|
2087 |
|
2088 lhs_idx = idx_vector (new_idx); |
|
2089 } |
|
2090 else |
|
2091 for (octave_idx_type i = 0; i < n; i++) |
|
2092 rhs_idx[i] = i; |
|
2093 |
5164
|
2094 // First count the number of non-zero elements |
5275
|
2095 for (octave_idx_type i = 0; i < n; i++) |
5164
|
2096 { |
|
2097 OCTAVE_QUIT; |
|
2098 |
5275
|
2099 octave_idx_type ii = lhs_idx.elem (i); |
5164
|
2100 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
5604
|
2101 new_nzmx--; |
5603
|
2102 if (rhs.elem(rhs_idx[i]) != RT ()) |
5604
|
2103 new_nzmx++; |
5164
|
2104 } |
|
2105 |
|
2106 if (nr > 1) |
|
2107 { |
5604
|
2108 Sparse<LT> tmp (max_idx, 1, new_nzmx); |
5164
|
2109 tmp.cidx(0) = 0; |
5681
|
2110 tmp.cidx(1) = new_nzmx; |
5164
|
2111 |
5275
|
2112 octave_idx_type i = 0; |
|
2113 octave_idx_type ii = 0; |
5164
|
2114 if (i < nz) |
|
2115 ii = c_lhs.ridx(i); |
|
2116 |
5275
|
2117 octave_idx_type j = 0; |
|
2118 octave_idx_type jj = lhs_idx.elem(j); |
|
2119 |
|
2120 octave_idx_type kk = 0; |
5164
|
2121 |
|
2122 while (j < n || i < nz) |
|
2123 { |
|
2124 if (j == n || (i < nz && ii < jj)) |
|
2125 { |
|
2126 tmp.xdata (kk) = c_lhs.data (i); |
|
2127 tmp.xridx (kk++) = ii; |
|
2128 if (++i < nz) |
|
2129 ii = c_lhs.ridx(i); |
|
2130 } |
|
2131 else |
|
2132 { |
5603
|
2133 RT rtmp = rhs.elem (rhs_idx[j]); |
5164
|
2134 if (rtmp != RT ()) |
|
2135 { |
|
2136 tmp.xdata (kk) = rtmp; |
|
2137 tmp.xridx (kk++) = jj; |
|
2138 } |
|
2139 |
|
2140 if (ii == jj && i < nz) |
|
2141 if (++i < nz) |
|
2142 ii = c_lhs.ridx(i); |
|
2143 if (++j < n) |
|
2144 jj = lhs_idx.elem(j); |
|
2145 } |
|
2146 } |
|
2147 |
|
2148 lhs = tmp; |
|
2149 } |
|
2150 else |
|
2151 { |
5604
|
2152 Sparse<LT> tmp (1, max_idx, new_nzmx); |
5164
|
2153 |
5275
|
2154 octave_idx_type i = 0; |
|
2155 octave_idx_type ii = 0; |
5164
|
2156 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2157 ii++; |
|
2158 |
5275
|
2159 octave_idx_type j = 0; |
|
2160 octave_idx_type jj = lhs_idx.elem(j); |
|
2161 |
|
2162 octave_idx_type kk = 0; |
|
2163 octave_idx_type ic = 0; |
5164
|
2164 |
|
2165 while (j < n || i < nz) |
|
2166 { |
|
2167 if (j == n || (i < nz && ii < jj)) |
|
2168 { |
|
2169 while (ic <= ii) |
|
2170 tmp.xcidx (ic++) = kk; |
|
2171 tmp.xdata (kk) = c_lhs.data (i); |
5603
|
2172 tmp.xridx (kk++) = 0; |
5164
|
2173 i++; |
|
2174 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2175 ii++; |
|
2176 } |
|
2177 else |
|
2178 { |
|
2179 while (ic <= jj) |
|
2180 tmp.xcidx (ic++) = kk; |
|
2181 |
5603
|
2182 RT rtmp = rhs.elem (rhs_idx[j]); |
5164
|
2183 if (rtmp != RT ()) |
5603
|
2184 { |
|
2185 tmp.xdata (kk) = rtmp; |
|
2186 tmp.xridx (kk++) = 0; |
|
2187 } |
5164
|
2188 if (ii == jj) |
|
2189 { |
|
2190 i++; |
|
2191 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2192 ii++; |
|
2193 } |
|
2194 j++; |
|
2195 if (j < n) |
|
2196 jj = lhs_idx.elem(j); |
|
2197 } |
|
2198 } |
|
2199 |
5275
|
2200 for (octave_idx_type iidx = ic; iidx < max_idx+1; iidx++) |
5164
|
2201 tmp.xcidx(iidx) = kk; |
|
2202 |
|
2203 lhs = tmp; |
|
2204 } |
|
2205 } |
|
2206 else if (rhs_len == 1) |
|
2207 { |
5681
|
2208 octave_idx_type new_nzmx = lhs.nnz (); |
5164
|
2209 RT scalar = rhs.elem (0); |
|
2210 bool scalar_non_zero = (scalar != RT ()); |
5603
|
2211 lhs_idx.sort (true); |
5164
|
2212 |
|
2213 // First count the number of non-zero elements |
|
2214 if (scalar != RT ()) |
5604
|
2215 new_nzmx += n; |
5275
|
2216 for (octave_idx_type i = 0; i < n; i++) |
5164
|
2217 { |
|
2218 OCTAVE_QUIT; |
|
2219 |
5275
|
2220 octave_idx_type ii = lhs_idx.elem (i); |
5164
|
2221 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
5604
|
2222 new_nzmx--; |
5164
|
2223 } |
|
2224 |
|
2225 if (nr > 1) |
|
2226 { |
5604
|
2227 Sparse<LT> tmp (max_idx, 1, new_nzmx); |
5164
|
2228 tmp.cidx(0) = 0; |
5681
|
2229 tmp.cidx(1) = new_nzmx; |
5164
|
2230 |
5275
|
2231 octave_idx_type i = 0; |
|
2232 octave_idx_type ii = 0; |
5164
|
2233 if (i < nz) |
|
2234 ii = c_lhs.ridx(i); |
|
2235 |
5275
|
2236 octave_idx_type j = 0; |
|
2237 octave_idx_type jj = lhs_idx.elem(j); |
|
2238 |
|
2239 octave_idx_type kk = 0; |
5164
|
2240 |
|
2241 while (j < n || i < nz) |
|
2242 { |
|
2243 if (j == n || (i < nz && ii < jj)) |
|
2244 { |
|
2245 tmp.xdata (kk) = c_lhs.data (i); |
|
2246 tmp.xridx (kk++) = ii; |
|
2247 if (++i < nz) |
|
2248 ii = c_lhs.ridx(i); |
|
2249 } |
|
2250 else |
|
2251 { |
|
2252 if (scalar_non_zero) |
|
2253 { |
|
2254 tmp.xdata (kk) = scalar; |
|
2255 tmp.xridx (kk++) = jj; |
|
2256 } |
|
2257 |
|
2258 if (ii == jj && i < nz) |
|
2259 if (++i < nz) |
|
2260 ii = c_lhs.ridx(i); |
|
2261 if (++j < n) |
|
2262 jj = lhs_idx.elem(j); |
|
2263 } |
|
2264 } |
|
2265 |
|
2266 lhs = tmp; |
|
2267 } |
|
2268 else |
|
2269 { |
5604
|
2270 Sparse<LT> tmp (1, max_idx, new_nzmx); |
5164
|
2271 |
5275
|
2272 octave_idx_type i = 0; |
|
2273 octave_idx_type ii = 0; |
5164
|
2274 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2275 ii++; |
|
2276 |
5275
|
2277 octave_idx_type j = 0; |
|
2278 octave_idx_type jj = lhs_idx.elem(j); |
|
2279 |
|
2280 octave_idx_type kk = 0; |
|
2281 octave_idx_type ic = 0; |
5164
|
2282 |
|
2283 while (j < n || i < nz) |
|
2284 { |
|
2285 if (j == n || (i < nz && ii < jj)) |
|
2286 { |
|
2287 while (ic <= ii) |
|
2288 tmp.xcidx (ic++) = kk; |
|
2289 tmp.xdata (kk) = c_lhs.data (i); |
|
2290 i++; |
|
2291 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2292 ii++; |
|
2293 } |
|
2294 else |
|
2295 { |
|
2296 while (ic <= jj) |
|
2297 tmp.xcidx (ic++) = kk; |
|
2298 if (scalar_non_zero) |
|
2299 tmp.xdata (kk) = scalar; |
|
2300 if (ii == jj) |
|
2301 { |
|
2302 i++; |
|
2303 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2304 ii++; |
|
2305 } |
|
2306 j++; |
|
2307 if (j < n) |
|
2308 jj = lhs_idx.elem(j); |
|
2309 } |
|
2310 tmp.xridx (kk++) = 0; |
|
2311 } |
|
2312 |
5275
|
2313 for (octave_idx_type iidx = ic; iidx < max_idx+1; iidx++) |
5164
|
2314 tmp.xcidx(iidx) = kk; |
|
2315 |
|
2316 lhs = tmp; |
|
2317 } |
|
2318 } |
|
2319 else |
|
2320 { |
|
2321 (*current_liboctave_error_handler) |
|
2322 ("A(I) = X: X must be a scalar or a vector with same length as I"); |
|
2323 |
|
2324 retval = 0; |
|
2325 } |
|
2326 } |
|
2327 else if (lhs_idx.is_colon ()) |
|
2328 { |
|
2329 if (lhs_len == 0) |
|
2330 { |
|
2331 |
5681
|
2332 octave_idx_type new_nzmx = rhs.nnz (); |
5604
|
2333 Sparse<LT> tmp (1, rhs_len, new_nzmx); |
5164
|
2334 |
5275
|
2335 octave_idx_type ii = 0; |
|
2336 octave_idx_type jj = 0; |
|
2337 for (octave_idx_type i = 0; i < rhs.cols(); i++) |
|
2338 for (octave_idx_type j = rhs.cidx(i); j < rhs.cidx(i+1); j++) |
5164
|
2339 { |
|
2340 OCTAVE_QUIT; |
5275
|
2341 for (octave_idx_type k = jj; k <= i * rhs.rows() + rhs.ridx(j); k++) |
5164
|
2342 tmp.cidx(jj++) = ii; |
|
2343 |
|
2344 tmp.data(ii) = rhs.data(j); |
|
2345 tmp.ridx(ii++) = 0; |
|
2346 } |
|
2347 |
5275
|
2348 for (octave_idx_type i = jj; i < rhs_len + 1; i++) |
5164
|
2349 tmp.cidx(i) = ii; |
|
2350 |
|
2351 lhs = tmp; |
|
2352 } |
|
2353 else |
|
2354 (*current_liboctave_error_handler) |
|
2355 ("A(:) = X: A must be the same size as X"); |
|
2356 } |
|
2357 else if (! (rhs_len == 1 || rhs_len == 0)) |
|
2358 { |
|
2359 (*current_liboctave_error_handler) |
|
2360 ("A([]) = X: X must also be an empty matrix or a scalar"); |
|
2361 |
|
2362 retval = 0; |
|
2363 } |
|
2364 |
|
2365 lhs.clear_index (); |
|
2366 |
|
2367 return retval; |
|
2368 } |
|
2369 |
|
2370 template <class LT, class RT> |
|
2371 int |
|
2372 assign (Sparse<LT>& lhs, const Sparse<RT>& rhs) |
|
2373 { |
|
2374 int retval = 1; |
|
2375 |
|
2376 int n_idx = lhs.index_count (); |
|
2377 |
5275
|
2378 octave_idx_type lhs_nr = lhs.rows (); |
|
2379 octave_idx_type lhs_nc = lhs.cols (); |
5681
|
2380 octave_idx_type lhs_nz = lhs.nnz (); |
5275
|
2381 |
|
2382 octave_idx_type rhs_nr = rhs.rows (); |
|
2383 octave_idx_type rhs_nc = rhs.cols (); |
5164
|
2384 |
|
2385 idx_vector *tmp = lhs.get_idx (); |
|
2386 |
|
2387 idx_vector idx_i; |
|
2388 idx_vector idx_j; |
|
2389 |
|
2390 if (n_idx > 2) |
|
2391 { |
|
2392 (*current_liboctave_error_handler) |
|
2393 ("A(I, J) = X: can only have 1 or 2 indexes for sparse matrices"); |
6092
|
2394 |
|
2395 lhs.clear_index (); |
5164
|
2396 return 0; |
|
2397 } |
|
2398 |
|
2399 if (n_idx > 1) |
|
2400 idx_j = tmp[1]; |
|
2401 |
|
2402 if (n_idx > 0) |
|
2403 idx_i = tmp[0]; |
|
2404 |
|
2405 if (n_idx == 2) |
|
2406 { |
5781
|
2407 octave_idx_type n = idx_i.freeze (lhs_nr, "row", true); |
|
2408 octave_idx_type m = idx_j.freeze (lhs_nc, "column", true); |
5164
|
2409 |
|
2410 int idx_i_is_colon = idx_i.is_colon (); |
|
2411 int idx_j_is_colon = idx_j.is_colon (); |
|
2412 |
|
2413 if (idx_i_is_colon) |
|
2414 n = lhs_nr > 0 ? lhs_nr : rhs_nr; |
|
2415 |
|
2416 if (idx_j_is_colon) |
|
2417 m = lhs_nc > 0 ? lhs_nc : rhs_nc; |
|
2418 |
|
2419 if (idx_i && idx_j) |
|
2420 { |
|
2421 if (rhs_nr == 0 && rhs_nc == 0) |
|
2422 { |
|
2423 lhs.maybe_delete_elements (idx_i, idx_j); |
|
2424 } |
|
2425 else |
|
2426 { |
|
2427 if (rhs_nr == 1 && rhs_nc == 1 && n >= 0 && m >= 0) |
|
2428 { |
|
2429 // No need to do anything if either of the indices |
|
2430 // are empty. |
|
2431 |
|
2432 if (n > 0 && m > 0) |
|
2433 { |
5603
|
2434 idx_i.sort (true); |
|
2435 idx_j.sort (true); |
|
2436 |
5275
|
2437 octave_idx_type max_row_idx = idx_i_is_colon ? rhs_nr : |
5164
|
2438 idx_i.max () + 1; |
5275
|
2439 octave_idx_type max_col_idx = idx_j_is_colon ? rhs_nc : |
5164
|
2440 idx_j.max () + 1; |
5603
|
2441 octave_idx_type new_nr = max_row_idx > lhs_nr ? |
|
2442 max_row_idx : lhs_nr; |
|
2443 octave_idx_type new_nc = max_col_idx > lhs_nc ? |
|
2444 max_col_idx : lhs_nc; |
5164
|
2445 RT scalar = rhs.elem (0, 0); |
|
2446 |
|
2447 // Count the number of non-zero terms |
5681
|
2448 octave_idx_type new_nzmx = lhs.nnz (); |
5275
|
2449 for (octave_idx_type j = 0; j < m; j++) |
5164
|
2450 { |
5275
|
2451 octave_idx_type jj = idx_j.elem (j); |
5164
|
2452 if (jj < lhs_nc) |
|
2453 { |
5275
|
2454 for (octave_idx_type i = 0; i < n; i++) |
5164
|
2455 { |
|
2456 OCTAVE_QUIT; |
|
2457 |
5275
|
2458 octave_idx_type ii = idx_i.elem (i); |
5164
|
2459 |
|
2460 if (ii < lhs_nr) |
|
2461 { |
5275
|
2462 for (octave_idx_type k = lhs.cidx(jj); |
5164
|
2463 k < lhs.cidx(jj+1); k++) |
|
2464 { |
|
2465 if (lhs.ridx(k) == ii) |
5604
|
2466 new_nzmx--; |
5164
|
2467 if (lhs.ridx(k) >= ii) |
|
2468 break; |
|
2469 } |
|
2470 } |
|
2471 } |
|
2472 } |
|
2473 } |
|
2474 |
|
2475 if (scalar != RT()) |
5604
|
2476 new_nzmx += m * n; |
|
2477 |
|
2478 Sparse<LT> stmp (new_nr, new_nc, new_nzmx); |
5164
|
2479 |
5275
|
2480 octave_idx_type jji = 0; |
|
2481 octave_idx_type jj = idx_j.elem (jji); |
|
2482 octave_idx_type kk = 0; |
5164
|
2483 stmp.cidx(0) = 0; |
5275
|
2484 for (octave_idx_type j = 0; j < new_nc; j++) |
5164
|
2485 { |
|
2486 if (jji < m && jj == j) |
|
2487 { |
5275
|
2488 octave_idx_type iii = 0; |
|
2489 octave_idx_type ii = idx_i.elem (iii); |
5760
|
2490 octave_idx_type ppp = 0; |
6092
|
2491 octave_idx_type ppi = (j >= lhs_nc ? 0 : |
|
2492 lhs.cidx(j+1) - |
|
2493 lhs.cidx(j)); |
5760
|
2494 octave_idx_type pp = (ppp < ppi ? |
|
2495 lhs.ridx(lhs.cidx(j)+ppp) : |
|
2496 new_nr); |
|
2497 while (ppp < ppi || iii < n) |
5164
|
2498 { |
5760
|
2499 if (iii < n && ii <= pp) |
5164
|
2500 { |
|
2501 if (scalar != RT ()) |
|
2502 { |
|
2503 stmp.data(kk) = scalar; |
5760
|
2504 stmp.ridx(kk++) = ii; |
5164
|
2505 } |
5760
|
2506 if (ii == pp) |
|
2507 pp = (++ppp < ppi ? lhs.ridx(lhs.cidx(j)+ppp) : new_nr); |
5164
|
2508 if (++iii < n) |
|
2509 ii = idx_i.elem(iii); |
|
2510 } |
5760
|
2511 else |
5164
|
2512 { |
5760
|
2513 stmp.data(kk) = |
|
2514 lhs.data(lhs.cidx(j)+ppp); |
|
2515 stmp.ridx(kk++) = pp; |
|
2516 pp = (++ppp < ppi ? lhs.ridx(lhs.cidx(j)+ppp) : new_nr); |
5164
|
2517 } |
|
2518 } |
|
2519 if (++jji < m) |
|
2520 jj = idx_j.elem(jji); |
|
2521 } |
|
2522 else if (j < lhs.cols()) |
|
2523 { |
5275
|
2524 for (octave_idx_type i = lhs.cidx(j); |
5164
|
2525 i < lhs.cidx(j+1); i++) |
|
2526 { |
|
2527 stmp.data(kk) = lhs.data(i); |
|
2528 stmp.ridx(kk++) = lhs.ridx(i); |
|
2529 } |
|
2530 } |
|
2531 stmp.cidx(j+1) = kk; |
|
2532 } |
|
2533 |
|
2534 lhs = stmp; |
|
2535 } |
|
2536 } |
|
2537 else if (n == rhs_nr && m == rhs_nc) |
|
2538 { |
|
2539 if (n > 0 && m > 0) |
|
2540 { |
5275
|
2541 octave_idx_type max_row_idx = idx_i_is_colon ? rhs_nr : |
5164
|
2542 idx_i.max () + 1; |
5275
|
2543 octave_idx_type max_col_idx = idx_j_is_colon ? rhs_nc : |
5164
|
2544 idx_j.max () + 1; |
5603
|
2545 octave_idx_type new_nr = max_row_idx > lhs_nr ? |
|
2546 max_row_idx : lhs_nr; |
|
2547 octave_idx_type new_nc = max_col_idx > lhs_nc ? |
|
2548 max_col_idx : lhs_nc; |
|
2549 |
|
2550 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx_i, n); |
|
2551 if (! idx_i.is_colon ()) |
|
2552 { |
|
2553 // Ok here we have to be careful with the indexing, |
|
2554 // to treat cases like "a([3,2,1],:) = b", and still |
|
2555 // handle the need for strict sorting of the sparse |
|
2556 // elements. |
|
2557 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, |
|
2558 sidx, n); |
|
2559 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, |
|
2560 sidxX, n); |
|
2561 |
|
2562 for (octave_idx_type i = 0; i < n; i++) |
|
2563 { |
|
2564 sidx[i] = &sidxX[i]; |
|
2565 sidx[i]->i = idx_i.elem(i); |
|
2566 sidx[i]->idx = i; |
|
2567 } |
|
2568 |
|
2569 OCTAVE_QUIT; |
|
2570 octave_sort<octave_idx_vector_sort *> |
|
2571 sort (octave_idx_vector_comp); |
|
2572 |
|
2573 sort.sort (sidx, n); |
|
2574 |
|
2575 intNDArray<octave_idx_type> new_idx (dim_vector (n,1)); |
|
2576 |
|
2577 for (octave_idx_type i = 0; i < n; i++) |
|
2578 { |
|
2579 new_idx.xelem(i) = sidx[i]->i + 1; |
|
2580 rhs_idx_i[i] = sidx[i]->idx; |
|
2581 } |
|
2582 |
|
2583 idx_i = idx_vector (new_idx); |
|
2584 } |
|
2585 else |
|
2586 for (octave_idx_type i = 0; i < n; i++) |
|
2587 rhs_idx_i[i] = i; |
|
2588 |
|
2589 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx_j, m); |
|
2590 if (! idx_j.is_colon ()) |
|
2591 { |
|
2592 // Ok here we have to be careful with the indexing, |
|
2593 // to treat cases like "a([3,2,1],:) = b", and still |
|
2594 // handle the need for strict sorting of the sparse |
|
2595 // elements. |
|
2596 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, |
|
2597 sidx, m); |
|
2598 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, |
|
2599 sidxX, m); |
|
2600 |
|
2601 for (octave_idx_type i = 0; i < m; i++) |
|
2602 { |
|
2603 sidx[i] = &sidxX[i]; |
|
2604 sidx[i]->i = idx_j.elem(i); |
|
2605 sidx[i]->idx = i; |
|
2606 } |
|
2607 |
|
2608 OCTAVE_QUIT; |
|
2609 octave_sort<octave_idx_vector_sort *> |
|
2610 sort (octave_idx_vector_comp); |
|
2611 |
|
2612 sort.sort (sidx, m); |
|
2613 |
|
2614 intNDArray<octave_idx_type> new_idx (dim_vector (m,1)); |
|
2615 |
|
2616 for (octave_idx_type i = 0; i < m; i++) |
|
2617 { |
|
2618 new_idx.xelem(i) = sidx[i]->i + 1; |
|
2619 rhs_idx_j[i] = sidx[i]->idx; |
|
2620 } |
|
2621 |
|
2622 idx_j = idx_vector (new_idx); |
|
2623 } |
|
2624 else |
|
2625 for (octave_idx_type i = 0; i < m; i++) |
|
2626 rhs_idx_j[i] = i; |
5164
|
2627 |
5760
|
2628 // Maximum number of non-zero elements |
|
2629 octave_idx_type new_nzmx = lhs.nnz() + rhs.nnz(); |
5164
|
2630 |
5604
|
2631 Sparse<LT> stmp (new_nr, new_nc, new_nzmx); |
5164
|
2632 |
5275
|
2633 octave_idx_type jji = 0; |
|
2634 octave_idx_type jj = idx_j.elem (jji); |
|
2635 octave_idx_type kk = 0; |
5164
|
2636 stmp.cidx(0) = 0; |
5275
|
2637 for (octave_idx_type j = 0; j < new_nc; j++) |
5164
|
2638 { |
|
2639 if (jji < m && jj == j) |
|
2640 { |
5275
|
2641 octave_idx_type iii = 0; |
|
2642 octave_idx_type ii = idx_i.elem (iii); |
5760
|
2643 octave_idx_type ppp = 0; |
6092
|
2644 octave_idx_type ppi = (j >= lhs_nc ? 0 : |
|
2645 lhs.cidx(j+1) - |
|
2646 lhs.cidx(j)); |
5760
|
2647 octave_idx_type pp = (ppp < ppi ? |
|
2648 lhs.ridx(lhs.cidx(j)+ppp) : |
|
2649 new_nr); |
|
2650 while (ppp < ppi || iii < n) |
5164
|
2651 { |
5760
|
2652 if (iii < n && ii <= pp) |
5164
|
2653 { |
5603
|
2654 RT rtmp = rhs.elem (rhs_idx_i[iii], |
|
2655 rhs_idx_j[jji]); |
5164
|
2656 if (rtmp != RT ()) |
|
2657 { |
|
2658 stmp.data(kk) = rtmp; |
5760
|
2659 stmp.ridx(kk++) = ii; |
5164
|
2660 } |
5760
|
2661 if (ii == pp) |
|
2662 pp = (++ppp < ppi ? lhs.ridx(lhs.cidx(j)+ppp) : new_nr); |
5164
|
2663 if (++iii < n) |
|
2664 ii = idx_i.elem(iii); |
|
2665 } |
5760
|
2666 else |
5164
|
2667 { |
5760
|
2668 stmp.data(kk) = |
|
2669 lhs.data(lhs.cidx(j)+ppp); |
|
2670 stmp.ridx(kk++) = pp; |
|
2671 pp = (++ppp < ppi ? lhs.ridx(lhs.cidx(j)+ppp) : new_nr); |
5164
|
2672 } |
|
2673 } |
|
2674 if (++jji < m) |
|
2675 jj = idx_j.elem(jji); |
|
2676 } |
|
2677 else if (j < lhs.cols()) |
|
2678 { |
5275
|
2679 for (octave_idx_type i = lhs.cidx(j); |
5164
|
2680 i < lhs.cidx(j+1); i++) |
|
2681 { |
|
2682 stmp.data(kk) = lhs.data(i); |
|
2683 stmp.ridx(kk++) = lhs.ridx(i); |
|
2684 } |
|
2685 } |
|
2686 stmp.cidx(j+1) = kk; |
|
2687 } |
|
2688 |
5760
|
2689 stmp.maybe_compress(); |
5164
|
2690 lhs = stmp; |
|
2691 } |
|
2692 } |
|
2693 else if (n == 0 && m == 0) |
|
2694 { |
|
2695 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2696 || (rhs_nr == 0 || rhs_nc == 0))) |
|
2697 { |
|
2698 (*current_liboctave_error_handler) |
|
2699 ("A([], []) = X: X must be an empty matrix or a scalar"); |
|
2700 |
|
2701 retval = 0; |
|
2702 } |
|
2703 } |
|
2704 else |
|
2705 { |
|
2706 (*current_liboctave_error_handler) |
|
2707 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
|
2708 (*current_liboctave_error_handler) |
|
2709 ("match the number of rows in X and the number of elements in J must"); |
|
2710 (*current_liboctave_error_handler) |
|
2711 ("match the number of columns in X"); |
|
2712 |
|
2713 retval = 0; |
|
2714 } |
|
2715 } |
|
2716 } |
|
2717 // idx_vector::freeze() printed an error message for us. |
|
2718 } |
|
2719 else if (n_idx == 1) |
|
2720 { |
|
2721 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; |
|
2722 |
|
2723 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) |
|
2724 { |
5275
|
2725 octave_idx_type lhs_len = lhs.length (); |
|
2726 |
5781
|
2727 octave_idx_type n = idx_i.freeze (lhs_len, 0, true); |
5164
|
2728 |
|
2729 if (idx_i) |
|
2730 { |
|
2731 if (rhs_nr == 0 && rhs_nc == 0) |
|
2732 { |
|
2733 if (n != 0 && (lhs_nr != 0 || lhs_nc != 0)) |
|
2734 lhs.maybe_delete_elements (idx_i); |
|
2735 } |
|
2736 else |
|
2737 { |
5781
|
2738 if (lhs_is_empty |
|
2739 && idx_i.is_colon () |
|
2740 && ! (rhs_nr == 1 || rhs_nc == 1)) |
5164
|
2741 { |
5781
|
2742 (*current_liboctave_warning_with_id_handler) |
|
2743 ("Octave:fortran-indexing", |
|
2744 "A(:) = X: X is not a vector or scalar"); |
|
2745 } |
|
2746 else |
|
2747 { |
|
2748 octave_idx_type idx_nr = idx_i.orig_rows (); |
|
2749 octave_idx_type idx_nc = idx_i.orig_columns (); |
|
2750 |
|
2751 if (! (rhs_nr == idx_nr && rhs_nc == idx_nc)) |
|
2752 (*current_liboctave_warning_with_id_handler) |
|
2753 ("Octave:fortran-indexing", |
|
2754 "A(I) = X: X does not have same shape as I"); |
5164
|
2755 } |
|
2756 |
5760
|
2757 if (! assign1 (lhs, rhs)) |
5164
|
2758 retval = 0; |
|
2759 } |
|
2760 } |
|
2761 // idx_vector::freeze() printed an error message for us. |
|
2762 } |
|
2763 else if (lhs_nr == 1) |
|
2764 { |
5781
|
2765 idx_i.freeze (lhs_nc, "vector", true); |
5164
|
2766 |
|
2767 if (idx_i) |
|
2768 { |
|
2769 if (rhs_nr == 0 && rhs_nc == 0) |
|
2770 lhs.maybe_delete_elements (idx_i); |
5760
|
2771 else if (! assign1 (lhs, rhs)) |
5164
|
2772 retval = 0; |
|
2773 } |
|
2774 // idx_vector::freeze() printed an error message for us. |
|
2775 } |
|
2776 else if (lhs_nc == 1) |
|
2777 { |
5781
|
2778 idx_i.freeze (lhs_nr, "vector", true); |
5164
|
2779 |
|
2780 if (idx_i) |
|
2781 { |
|
2782 if (rhs_nr == 0 && rhs_nc == 0) |
|
2783 lhs.maybe_delete_elements (idx_i); |
5760
|
2784 else if (! assign1 (lhs, rhs)) |
5164
|
2785 retval = 0; |
|
2786 } |
|
2787 // idx_vector::freeze() printed an error message for us. |
|
2788 } |
|
2789 else |
|
2790 { |
5781
|
2791 if (! (idx_i.is_colon () |
|
2792 || (idx_i.one_zero_only () |
|
2793 && idx_i.orig_rows () == lhs_nr |
|
2794 && idx_i.orig_columns () == lhs_nc))) |
|
2795 (*current_liboctave_warning_with_id_handler) |
|
2796 ("Octave:fortran-indexing", "single index used for matrix"); |
5164
|
2797 |
5275
|
2798 octave_idx_type lhs_len = lhs.length (); |
|
2799 |
|
2800 octave_idx_type len = idx_i.freeze (lhs_nr * lhs_nc, "matrix"); |
5164
|
2801 |
|
2802 if (idx_i) |
|
2803 { |
|
2804 // Take a constant copy of lhs. This means that elem won't |
|
2805 // create missing elements. |
|
2806 const Sparse<LT> c_lhs (lhs); |
|
2807 |
|
2808 if (rhs_nr == 0 && rhs_nc == 0) |
|
2809 lhs.maybe_delete_elements (idx_i); |
|
2810 else if (len == 0) |
|
2811 { |
|
2812 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2813 || (rhs_nr == 0 || rhs_nc == 0))) |
|
2814 (*current_liboctave_error_handler) |
|
2815 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2816 } |
|
2817 else if (len == rhs_nr * rhs_nc) |
|
2818 { |
5604
|
2819 octave_idx_type new_nzmx = lhs_nz; |
5603
|
2820 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx, len); |
|
2821 |
|
2822 if (! idx_i.is_colon ()) |
|
2823 { |
|
2824 // Ok here we have to be careful with the indexing, to |
|
2825 // treat cases like "a([3,2,1]) = b", and still handle |
|
2826 // the need for strict sorting of the sparse elements. |
|
2827 |
|
2828 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, sidx, |
|
2829 len); |
|
2830 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, sidxX, |
|
2831 len); |
|
2832 |
|
2833 for (octave_idx_type i = 0; i < len; i++) |
|
2834 { |
|
2835 sidx[i] = &sidxX[i]; |
|
2836 sidx[i]->i = idx_i.elem(i); |
|
2837 sidx[i]->idx = i; |
|
2838 } |
|
2839 |
|
2840 OCTAVE_QUIT; |
|
2841 octave_sort<octave_idx_vector_sort *> |
|
2842 sort (octave_idx_vector_comp); |
|
2843 |
|
2844 sort.sort (sidx, len); |
|
2845 |
|
2846 intNDArray<octave_idx_type> new_idx (dim_vector (len,1)); |
|
2847 |
|
2848 for (octave_idx_type i = 0; i < len; i++) |
|
2849 { |
|
2850 new_idx.xelem(i) = sidx[i]->i + 1; |
|
2851 rhs_idx[i] = sidx[i]->idx; |
|
2852 } |
|
2853 |
|
2854 idx_i = idx_vector (new_idx); |
|
2855 } |
|
2856 else |
|
2857 for (octave_idx_type i = 0; i < len; i++) |
|
2858 rhs_idx[i] = i; |
5164
|
2859 |
|
2860 // First count the number of non-zero elements |
5275
|
2861 for (octave_idx_type i = 0; i < len; i++) |
5164
|
2862 { |
|
2863 OCTAVE_QUIT; |
|
2864 |
5275
|
2865 octave_idx_type ii = idx_i.elem (i); |
5164
|
2866 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
5604
|
2867 new_nzmx--; |
5603
|
2868 if (rhs.elem(rhs_idx[i]) != RT ()) |
5604
|
2869 new_nzmx++; |
5164
|
2870 } |
|
2871 |
5604
|
2872 Sparse<LT> stmp (lhs_nr, lhs_nc, new_nzmx); |
5164
|
2873 |
5275
|
2874 octave_idx_type i = 0; |
|
2875 octave_idx_type ii = 0; |
|
2876 octave_idx_type ic = 0; |
5164
|
2877 if (i < lhs_nz) |
|
2878 { |
|
2879 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2880 ic++; |
|
2881 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2882 } |
|
2883 |
5275
|
2884 octave_idx_type j = 0; |
|
2885 octave_idx_type jj = idx_i.elem (j); |
|
2886 octave_idx_type jr = jj % lhs_nr; |
|
2887 octave_idx_type jc = (jj - jr) / lhs_nr; |
|
2888 |
|
2889 octave_idx_type kk = 0; |
|
2890 octave_idx_type kc = 0; |
5164
|
2891 |
|
2892 while (j < len || i < lhs_nz) |
|
2893 { |
|
2894 if (j == len || (i < lhs_nz && ii < jj)) |
|
2895 { |
|
2896 while (kc <= ic) |
|
2897 stmp.xcidx (kc++) = kk; |
|
2898 stmp.xdata (kk) = c_lhs.data (i); |
|
2899 stmp.xridx (kk++) = c_lhs.ridx (i); |
|
2900 i++; |
|
2901 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2902 ic++; |
|
2903 if (i < lhs_nz) |
|
2904 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2905 } |
|
2906 else |
|
2907 { |
|
2908 while (kc <= jc) |
|
2909 stmp.xcidx (kc++) = kk; |
5603
|
2910 RT rtmp = rhs.elem (rhs_idx[j]); |
5164
|
2911 if (rtmp != RT ()) |
|
2912 { |
|
2913 stmp.xdata (kk) = rtmp; |
|
2914 stmp.xridx (kk++) = jr; |
|
2915 } |
|
2916 if (ii == jj) |
|
2917 { |
|
2918 i++; |
|
2919 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2920 ic++; |
|
2921 if (i < lhs_nz) |
|
2922 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2923 } |
|
2924 j++; |
|
2925 if (j < len) |
|
2926 { |
|
2927 jj = idx_i.elem (j); |
|
2928 jr = jj % lhs_nr; |
|
2929 jc = (jj - jr) / lhs_nr; |
|
2930 } |
|
2931 } |
|
2932 } |
|
2933 |
5275
|
2934 for (octave_idx_type iidx = kc; iidx < lhs_nc+1; iidx++) |
5603
|
2935 stmp.xcidx(iidx) = kk; |
5164
|
2936 |
|
2937 lhs = stmp; |
|
2938 } |
|
2939 else if (rhs_nr == 1 && rhs_nc == 1) |
|
2940 { |
|
2941 RT scalar = rhs.elem (0, 0); |
5604
|
2942 octave_idx_type new_nzmx = lhs_nz; |
5603
|
2943 idx_i.sort (true); |
5164
|
2944 |
|
2945 // First count the number of non-zero elements |
|
2946 if (scalar != RT ()) |
5604
|
2947 new_nzmx += len; |
5275
|
2948 for (octave_idx_type i = 0; i < len; i++) |
5164
|
2949 { |
|
2950 OCTAVE_QUIT; |
5275
|
2951 octave_idx_type ii = idx_i.elem (i); |
5164
|
2952 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
5604
|
2953 new_nzmx--; |
5164
|
2954 } |
|
2955 |
5604
|
2956 Sparse<LT> stmp (lhs_nr, lhs_nc, new_nzmx); |
5164
|
2957 |
5275
|
2958 octave_idx_type i = 0; |
|
2959 octave_idx_type ii = 0; |
|
2960 octave_idx_type ic = 0; |
5164
|
2961 if (i < lhs_nz) |
|
2962 { |
|
2963 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2964 ic++; |
|
2965 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2966 } |
|
2967 |
5275
|
2968 octave_idx_type j = 0; |
|
2969 octave_idx_type jj = idx_i.elem (j); |
|
2970 octave_idx_type jr = jj % lhs_nr; |
|
2971 octave_idx_type jc = (jj - jr) / lhs_nr; |
|
2972 |
|
2973 octave_idx_type kk = 0; |
|
2974 octave_idx_type kc = 0; |
5164
|
2975 |
|
2976 while (j < len || i < lhs_nz) |
|
2977 { |
|
2978 if (j == len || (i < lhs_nz && ii < jj)) |
|
2979 { |
|
2980 while (kc <= ic) |
|
2981 stmp.xcidx (kc++) = kk; |
|
2982 stmp.xdata (kk) = c_lhs.data (i); |
|
2983 stmp.xridx (kk++) = c_lhs.ridx (i); |
|
2984 i++; |
|
2985 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2986 ic++; |
|
2987 if (i < lhs_nz) |
|
2988 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2989 } |
|
2990 else |
|
2991 { |
|
2992 while (kc <= jc) |
|
2993 stmp.xcidx (kc++) = kk; |
|
2994 if (scalar != RT ()) |
|
2995 { |
|
2996 stmp.xdata (kk) = scalar; |
|
2997 stmp.xridx (kk++) = jr; |
|
2998 } |
|
2999 if (ii == jj) |
|
3000 { |
|
3001 i++; |
|
3002 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
3003 ic++; |
|
3004 if (i < lhs_nz) |
|
3005 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
3006 } |
|
3007 j++; |
|
3008 if (j < len) |
|
3009 { |
|
3010 jj = idx_i.elem (j); |
|
3011 jr = jj % lhs_nr; |
|
3012 jc = (jj - jr) / lhs_nr; |
|
3013 } |
|
3014 } |
|
3015 } |
|
3016 |
5275
|
3017 for (octave_idx_type iidx = kc; iidx < lhs_nc+1; iidx++) |
5164
|
3018 stmp.xcidx(iidx) = kk; |
|
3019 |
|
3020 lhs = stmp; |
|
3021 } |
|
3022 else |
|
3023 { |
|
3024 (*current_liboctave_error_handler) |
|
3025 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
3026 |
|
3027 retval = 0; |
|
3028 } |
|
3029 } |
|
3030 // idx_vector::freeze() printed an error message for us. |
|
3031 } |
|
3032 } |
|
3033 else |
|
3034 { |
|
3035 (*current_liboctave_error_handler) |
|
3036 ("invalid number of indices for matrix expression"); |
|
3037 |
|
3038 retval = 0; |
|
3039 } |
|
3040 |
|
3041 lhs.clear_index (); |
|
3042 |
|
3043 return retval; |
|
3044 } |
|
3045 |
|
3046 template <class T> |
|
3047 void |
|
3048 Sparse<T>::print_info (std::ostream& os, const std::string& prefix) const |
|
3049 { |
|
3050 os << prefix << "rep address: " << rep << "\n" |
5604
|
3051 << prefix << "rep->nzmx: " << rep->nzmx << "\n" |
5164
|
3052 << prefix << "rep->nrows: " << rep->nrows << "\n" |
|
3053 << prefix << "rep->ncols: " << rep->ncols << "\n" |
|
3054 << prefix << "rep->data: " << static_cast<void *> (rep->d) << "\n" |
|
3055 << prefix << "rep->ridx: " << static_cast<void *> (rep->r) << "\n" |
|
3056 << prefix << "rep->cidx: " << static_cast<void *> (rep->c) << "\n" |
|
3057 << prefix << "rep->count: " << rep->count << "\n"; |
|
3058 } |
|
3059 |
|
3060 /* |
|
3061 ;;; Local Variables: *** |
|
3062 ;;; mode: C++ *** |
|
3063 ;;; End: *** |
|
3064 */ |