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 { |
5604
|
205 if (a.nzmax () == 0) |
5164
|
206 rep = new typename Sparse<T>::SparseRep (rows (), cols()); |
|
207 else |
|
208 { |
5604
|
209 rep = new typename Sparse<T>::SparseRep (rows (), cols (), a.nzmax ()); |
5164
|
210 |
5604
|
211 octave_idx_type nz = nzmax (); |
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(); |
5604
|
279 octave_idx_type new_nzmx = a.nzmax (); |
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 { |
5604
|
743 octave_idx_type new_nzmx = nzmax (); |
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 (); |
5604
|
748 retval = Sparse<T> (new_nr, new_nc, new_nzmx); |
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++) |
5604
|
765 retval.xcidx(k+1) = new_nzmx; |
5164
|
766 } |
|
767 else |
|
768 (*current_liboctave_error_handler) ("reshape: size mismatch"); |
|
769 } |
|
770 else |
|
771 retval = *this; |
|
772 |
|
773 return retval; |
|
774 } |
|
775 |
|
776 template <class T> |
|
777 Sparse<T> |
5275
|
778 Sparse<T>::permute (const Array<octave_idx_type>& perm_vec, bool) const |
5164
|
779 { |
|
780 dim_vector dv = dims (); |
|
781 dim_vector dv_new; |
|
782 |
5275
|
783 octave_idx_type nd = dv.length (); |
5164
|
784 |
|
785 dv_new.resize (nd); |
|
786 |
|
787 // Need this array to check for identical elements in permutation array. |
|
788 Array<bool> checked (nd, false); |
|
789 |
|
790 // Find dimension vector of permuted array. |
5275
|
791 for (octave_idx_type i = 0; i < nd; i++) |
5164
|
792 { |
5275
|
793 octave_idx_type perm_el = perm_vec.elem (i); |
5164
|
794 |
|
795 if (perm_el > dv.length () || perm_el < 1) |
|
796 { |
|
797 (*current_liboctave_error_handler) |
|
798 ("permutation vector contains an invalid element"); |
|
799 |
|
800 return Sparse<T> (); |
|
801 } |
|
802 |
|
803 if (checked.elem(perm_el - 1)) |
|
804 { |
|
805 (*current_liboctave_error_handler) |
|
806 ("PERM cannot contain identical elements"); |
|
807 |
|
808 return Sparse<T> (); |
|
809 } |
|
810 else |
|
811 checked.elem(perm_el - 1) = true; |
|
812 |
|
813 dv_new (i) = dv (perm_el - 1); |
|
814 } |
|
815 |
|
816 if (dv_new == dv) |
|
817 return *this; |
|
818 else |
|
819 return transpose (); |
|
820 } |
|
821 |
|
822 template <class T> |
|
823 void |
|
824 Sparse<T>::resize_no_fill (const dim_vector& dv) |
|
825 { |
5275
|
826 octave_idx_type n = dv.length (); |
5164
|
827 |
|
828 if (n != 2) |
|
829 { |
|
830 (*current_liboctave_error_handler) ("sparse array must be 2-D"); |
|
831 return; |
|
832 } |
|
833 |
|
834 resize_no_fill (dv(0), dv(1)); |
|
835 } |
|
836 |
|
837 template <class T> |
|
838 void |
5275
|
839 Sparse<T>::resize_no_fill (octave_idx_type r, octave_idx_type c) |
5164
|
840 { |
|
841 if (r < 0 || c < 0) |
|
842 { |
|
843 (*current_liboctave_error_handler) |
|
844 ("can't resize to negative dimension"); |
|
845 return; |
|
846 } |
|
847 |
|
848 if (ndims () == 0) |
|
849 dimensions = dim_vector (0, 0); |
|
850 |
|
851 if (r == dim1 () && c == dim2 ()) |
|
852 return; |
|
853 |
|
854 typename Sparse<T>::SparseRep *old_rep = Sparse<T>::rep; |
5275
|
855 octave_idx_type nc = cols (); |
|
856 octave_idx_type nr = rows (); |
5164
|
857 |
5604
|
858 if (nzmax () == 0 || r == 0 || c == 0) |
5164
|
859 // Special case of redimensioning to/from a sparse matrix with |
|
860 // no elements |
|
861 rep = new typename Sparse<T>::SparseRep (r, c); |
|
862 else |
|
863 { |
5275
|
864 octave_idx_type n = 0; |
5164
|
865 Sparse<T> tmpval; |
|
866 if (r >= nr) |
|
867 { |
|
868 if (c > nc) |
|
869 n = cidx(nc); |
|
870 else |
|
871 n = cidx(c); |
|
872 |
|
873 tmpval = Sparse<T> (r, c, n); |
|
874 |
|
875 if (c > nc) |
|
876 { |
5275
|
877 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
878 tmpval.cidx(i) = cidx(i); |
5275
|
879 for (octave_idx_type i = nc+2; i < c; i++) |
5164
|
880 tmpval.cidx(i) = tmpval.cidx(i-1); |
|
881 } |
|
882 else if (c <= nc) |
5275
|
883 for (octave_idx_type i = 0; i < c; i++) |
5164
|
884 tmpval.cidx(i) = cidx(i); |
|
885 |
5275
|
886 for (octave_idx_type i = 0; i < n; i++) |
5164
|
887 { |
|
888 tmpval.data(i) = data(i); |
|
889 tmpval.ridx(i) = ridx(i); |
|
890 } |
|
891 } |
|
892 else |
|
893 { |
|
894 // Count how many non zero terms before we do anything |
5275
|
895 for (octave_idx_type i = 0; i < c; i++) |
|
896 for (octave_idx_type j = cidx(i); j < cidx(i+1); j++) |
5164
|
897 if (ridx(j) < r) |
|
898 n++; |
|
899 |
|
900 if (n) |
|
901 { |
|
902 // Now that we know the size we can do something |
|
903 tmpval = Sparse<T> (r, c, n); |
|
904 |
|
905 tmpval.cidx(0); |
5275
|
906 for (octave_idx_type i = 0, ii = 0; i < c; i++) |
5164
|
907 { |
5275
|
908 for (octave_idx_type j = cidx(i); j < cidx(i+1); j++) |
5164
|
909 if (ridx(j) < r) |
|
910 { |
|
911 tmpval.data(ii) = data(j); |
|
912 tmpval.ridx(ii++) = ridx(j); |
|
913 } |
|
914 tmpval.cidx(i+1) = ii; |
|
915 } |
|
916 } |
|
917 else |
|
918 tmpval = Sparse<T> (r, c); |
|
919 } |
|
920 |
|
921 rep = tmpval.rep; |
|
922 rep->count++; |
|
923 } |
|
924 |
|
925 dimensions = dim_vector (r, c); |
|
926 |
|
927 if (--old_rep->count <= 0) |
|
928 delete old_rep; |
|
929 } |
|
930 |
|
931 template <class T> |
|
932 Sparse<T>& |
5275
|
933 Sparse<T>::insert (const Sparse<T>& a, octave_idx_type r, octave_idx_type c) |
5164
|
934 { |
5275
|
935 octave_idx_type a_rows = a.rows (); |
|
936 octave_idx_type a_cols = a.cols (); |
|
937 octave_idx_type nr = rows (); |
|
938 octave_idx_type nc = cols (); |
5164
|
939 |
|
940 if (r < 0 || r + a_rows > rows () || c < 0 || c + a_cols > cols ()) |
|
941 { |
|
942 (*current_liboctave_error_handler) ("range error for insert"); |
|
943 return *this; |
|
944 } |
|
945 |
|
946 // First count the number of elements in the final array |
5604
|
947 octave_idx_type nel = cidx(c) + a.nzmax (); |
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; |
5604
|
1145 octave_idx_type new_nzmx = nzmax (); |
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 ()) |
5604
|
1161 new_nzmx--; |
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) |
5604
|
1173 rep = new typename Sparse<T>::SparseRep (1, new_n, new_nzmx); |
5164
|
1174 else |
5604
|
1175 rep = new typename Sparse<T>::SparseRep (new_n, 1, new_nzmx); |
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; |
5604
|
1218 cidx(1) = new_nzmx; |
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; |
5604
|
1290 octave_idx_type new_nzmx = nzmax (); |
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 |
5604
|
1303 new_nzmx -= 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, |
5604
|
1315 new_nzmx); |
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; |
5604
|
1365 octave_idx_type new_nzmx = nzmax (); |
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 |
5604
|
1378 for (octave_idx_type j = 0; j < nzmax (); j++) |
5164
|
1379 if (ridx(j) == i) |
5604
|
1380 new_nzmx--; |
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, |
5604
|
1392 new_nzmx); |
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 (); |
5604
|
1486 octave_idx_type nz = nzmax (); |
5275
|
1487 |
|
1488 octave_idx_type orig_len = nr * nc; |
5164
|
1489 |
|
1490 dim_vector idx_orig_dims = idx_arg.orig_dimensions (); |
|
1491 |
5275
|
1492 octave_idx_type idx_orig_rows = idx_arg.orig_rows (); |
|
1493 octave_idx_type idx_orig_columns = idx_arg.orig_columns (); |
5164
|
1494 |
|
1495 if (idx_orig_dims.length () > 2) |
|
1496 (*current_liboctave_error_handler) |
|
1497 ("Sparse<T>::index: Can not index Sparse<T> with an N-D Array"); |
|
1498 else if (idx_arg.is_colon ()) |
|
1499 { |
|
1500 // Fast magic colon processing. |
|
1501 retval = Sparse<T> (nr * nc, 1, nz); |
|
1502 |
5275
|
1503 for (octave_idx_type i = 0; i < nc; i++) |
|
1504 for (octave_idx_type j = cidx(i); j < cidx(i+1); j++) |
5164
|
1505 { |
|
1506 OCTAVE_QUIT; |
|
1507 retval.xdata(j) = data(j); |
|
1508 retval.xridx(j) = ridx(j) + i * nr; |
|
1509 } |
|
1510 retval.xcidx(0) = 0; |
|
1511 retval.xcidx(1) = nz; |
|
1512 } |
|
1513 else if (nr == 1 && nc == 1) |
|
1514 { |
|
1515 // You have to be pretty sick to get to this bit of code, |
|
1516 // since you have a scalar stored as a sparse matrix, and |
|
1517 // then want to make a dense matrix with sparse |
|
1518 // representation. Ok, we'll do it, but you deserve what |
|
1519 // you get!! |
5275
|
1520 octave_idx_type n = idx_arg.freeze (length (), "sparse vector", resize_ok); |
5164
|
1521 if (n == 0) |
|
1522 if (idx_arg.one_zero_only ()) |
|
1523 retval = Sparse<T> (dim_vector (0, 0)); |
|
1524 else |
|
1525 retval = Sparse<T> (dim_vector (0, 1)); |
|
1526 else if (nz < 1) |
|
1527 if (n >= idx_orig_dims.numel ()) |
|
1528 retval = Sparse<T> (idx_orig_dims); |
|
1529 else |
|
1530 retval = Sparse<T> (dim_vector (n, 1)); |
|
1531 else if (n >= idx_orig_dims.numel ()) |
|
1532 { |
|
1533 T el = elem (0); |
5275
|
1534 octave_idx_type new_nr = idx_orig_rows; |
|
1535 octave_idx_type new_nc = idx_orig_columns; |
|
1536 for (octave_idx_type i = 2; i < idx_orig_dims.length (); i++) |
5164
|
1537 new_nc *= idx_orig_dims (i); |
|
1538 |
|
1539 retval = Sparse<T> (new_nr, new_nc, idx_arg.ones_count ()); |
|
1540 |
5275
|
1541 octave_idx_type ic = 0; |
|
1542 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1543 { |
|
1544 if (i % new_nr == 0) |
|
1545 retval.xcidx(i % new_nr) = ic; |
|
1546 |
5275
|
1547 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1548 if (ii == 0) |
|
1549 { |
|
1550 OCTAVE_QUIT; |
|
1551 retval.xdata(ic) = el; |
|
1552 retval.xridx(ic++) = i % new_nr; |
|
1553 } |
|
1554 } |
|
1555 retval.xcidx (new_nc) = ic; |
|
1556 } |
|
1557 else |
|
1558 { |
|
1559 T el = elem (0); |
|
1560 retval = Sparse<T> (n, 1, nz); |
|
1561 |
5275
|
1562 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
1563 { |
|
1564 OCTAVE_QUIT; |
|
1565 retval.xdata(i) = el; |
|
1566 retval.xridx(i) = i; |
|
1567 } |
|
1568 retval.xcidx(0) = 0; |
|
1569 retval.xcidx(1) = n; |
|
1570 } |
|
1571 } |
|
1572 else if (nr == 1 || nc == 1) |
|
1573 { |
|
1574 // If indexing a vector with a matrix, return value has same |
|
1575 // shape as the index. Otherwise, it has same orientation as |
|
1576 // indexed object. |
5275
|
1577 octave_idx_type len = length (); |
|
1578 octave_idx_type n = idx_arg.freeze (len, "sparse vector", resize_ok); |
5164
|
1579 |
|
1580 if (n == 0) |
|
1581 if (nr == 1) |
|
1582 retval = Sparse<T> (dim_vector (1, 0)); |
|
1583 else |
|
1584 retval = Sparse<T> (dim_vector (0, 1)); |
|
1585 else if (nz < 1) |
|
1586 if ((n != 0 && idx_arg.one_zero_only ()) |
|
1587 || idx_orig_rows == 1 || idx_orig_columns == 1) |
|
1588 retval = Sparse<T> ((nr == 1 ? 1 : n), (nr == 1 ? n : 1)); |
|
1589 else |
|
1590 retval = Sparse<T> (idx_orig_dims); |
|
1591 else |
|
1592 { |
|
1593 |
5604
|
1594 octave_idx_type new_nzmx = 0; |
5164
|
1595 if (nr == 1) |
5275
|
1596 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1597 { |
|
1598 OCTAVE_QUIT; |
|
1599 |
5275
|
1600 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1601 if (ii < len) |
|
1602 if (cidx(ii) != cidx(ii+1)) |
5604
|
1603 new_nzmx++; |
5164
|
1604 } |
|
1605 else |
5275
|
1606 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1607 { |
5275
|
1608 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1609 if (ii < len) |
5275
|
1610 for (octave_idx_type j = 0; j < nz; j++) |
5164
|
1611 { |
|
1612 OCTAVE_QUIT; |
|
1613 |
|
1614 if (ridx(j) == ii) |
5604
|
1615 new_nzmx++; |
5164
|
1616 if (ridx(j) >= ii) |
|
1617 break; |
|
1618 } |
|
1619 } |
|
1620 |
|
1621 if (idx_arg.one_zero_only () || idx_orig_rows == 1 || |
|
1622 idx_orig_columns == 1) |
|
1623 { |
|
1624 if (nr == 1) |
|
1625 { |
5604
|
1626 retval = Sparse<T> (1, n, new_nzmx); |
5275
|
1627 octave_idx_type jj = 0; |
5164
|
1628 retval.xcidx(0) = 0; |
5275
|
1629 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1630 { |
|
1631 OCTAVE_QUIT; |
|
1632 |
5275
|
1633 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1634 if (ii < len) |
|
1635 if (cidx(ii) != cidx(ii+1)) |
|
1636 { |
|
1637 retval.xdata(jj) = data(cidx(ii)); |
|
1638 retval.xridx(jj++) = 0; |
|
1639 } |
|
1640 retval.xcidx(i+1) = jj; |
|
1641 } |
|
1642 } |
|
1643 else |
|
1644 { |
5604
|
1645 retval = Sparse<T> (n, 1, new_nzmx); |
5164
|
1646 retval.xcidx(0) = 0; |
5604
|
1647 retval.xcidx(1) = new_nzmx; |
5275
|
1648 octave_idx_type jj = 0; |
|
1649 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1650 { |
5275
|
1651 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1652 if (ii < len) |
5275
|
1653 for (octave_idx_type j = 0; j < nz; j++) |
5164
|
1654 { |
|
1655 OCTAVE_QUIT; |
|
1656 |
|
1657 if (ridx(j) == ii) |
|
1658 { |
|
1659 retval.xdata(jj) = data(j); |
|
1660 retval.xridx(jj++) = i; |
|
1661 } |
|
1662 if (ridx(j) >= ii) |
|
1663 break; |
|
1664 } |
|
1665 } |
|
1666 } |
|
1667 } |
|
1668 else |
|
1669 { |
5275
|
1670 octave_idx_type new_nr; |
|
1671 octave_idx_type new_nc; |
5164
|
1672 if (n >= idx_orig_dims.numel ()) |
|
1673 { |
|
1674 new_nr = idx_orig_rows; |
|
1675 new_nc = idx_orig_columns; |
|
1676 } |
|
1677 else |
|
1678 { |
|
1679 new_nr = n; |
|
1680 new_nc = 1; |
|
1681 } |
|
1682 |
5604
|
1683 retval = Sparse<T> (new_nr, new_nc, new_nzmx); |
5164
|
1684 |
|
1685 if (nr == 1) |
|
1686 { |
5275
|
1687 octave_idx_type jj = 0; |
5164
|
1688 retval.xcidx(0) = 0; |
5275
|
1689 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1690 { |
|
1691 OCTAVE_QUIT; |
|
1692 |
5275
|
1693 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1694 if (ii < len) |
|
1695 if (cidx(ii) != cidx(ii+1)) |
|
1696 { |
|
1697 retval.xdata(jj) = data(cidx(ii)); |
|
1698 retval.xridx(jj++) = 0; |
|
1699 } |
|
1700 retval.xcidx(i/new_nr+1) = jj; |
|
1701 } |
|
1702 } |
|
1703 else |
|
1704 { |
5275
|
1705 octave_idx_type jj = 0; |
5164
|
1706 retval.xcidx(0) = 0; |
5275
|
1707 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1708 { |
5275
|
1709 octave_idx_type ii = idx_arg.elem (i); |
5164
|
1710 if (ii < len) |
5275
|
1711 for (octave_idx_type j = 0; j < nz; j++) |
5164
|
1712 { |
|
1713 OCTAVE_QUIT; |
|
1714 |
|
1715 if (ridx(j) == ii) |
|
1716 { |
|
1717 retval.xdata(jj) = data(j); |
|
1718 retval.xridx(jj++) = i; |
|
1719 } |
|
1720 if (ridx(j) >= ii) |
|
1721 break; |
|
1722 } |
|
1723 retval.xcidx(i/new_nr+1) = jj; |
|
1724 } |
|
1725 } |
|
1726 } |
|
1727 } |
|
1728 } |
|
1729 else |
|
1730 { |
|
1731 if (liboctave_wfi_flag |
|
1732 && ! (idx_arg.one_zero_only () |
|
1733 && idx_orig_rows == nr |
|
1734 && idx_orig_columns == nc)) |
|
1735 (*current_liboctave_warning_handler) |
|
1736 ("single index used for sparse matrix"); |
|
1737 |
|
1738 // This code is only for indexing matrices. The vector |
|
1739 // cases are handled above. |
|
1740 |
|
1741 idx_arg.freeze (nr * nc, "matrix", resize_ok); |
|
1742 |
|
1743 if (idx_arg) |
|
1744 { |
5275
|
1745 octave_idx_type result_nr = idx_orig_rows; |
|
1746 octave_idx_type result_nc = idx_orig_columns; |
5164
|
1747 |
|
1748 if (idx_arg.one_zero_only ()) |
|
1749 { |
|
1750 result_nr = idx_arg.ones_count (); |
|
1751 result_nc = (result_nr > 0 ? 1 : 0); |
|
1752 } |
|
1753 |
|
1754 if (nz < 1) |
|
1755 retval = Sparse<T> (result_nr, result_nc); |
|
1756 else |
|
1757 { |
|
1758 // Count number of non-zero elements |
5604
|
1759 octave_idx_type new_nzmx = 0; |
5275
|
1760 octave_idx_type kk = 0; |
|
1761 for (octave_idx_type j = 0; j < result_nc; j++) |
5164
|
1762 { |
5275
|
1763 for (octave_idx_type i = 0; i < result_nr; i++) |
5164
|
1764 { |
|
1765 OCTAVE_QUIT; |
|
1766 |
5275
|
1767 octave_idx_type ii = idx_arg.elem (kk++); |
5164
|
1768 if (ii < orig_len) |
|
1769 { |
5275
|
1770 octave_idx_type fr = ii % nr; |
|
1771 octave_idx_type fc = (ii - fr) / nr; |
|
1772 for (octave_idx_type k = cidx(fc); k < cidx(fc+1); k++) |
5164
|
1773 { |
|
1774 if (ridx(k) == fr) |
5604
|
1775 new_nzmx++; |
5164
|
1776 if (ridx(k) >= fr) |
|
1777 break; |
|
1778 } |
|
1779 } |
|
1780 } |
|
1781 } |
|
1782 |
5604
|
1783 retval = Sparse<T> (result_nr, result_nc, new_nzmx); |
5164
|
1784 |
|
1785 kk = 0; |
5275
|
1786 octave_idx_type jj = 0; |
5164
|
1787 retval.xcidx(0) = 0; |
5275
|
1788 for (octave_idx_type j = 0; j < result_nc; j++) |
5164
|
1789 { |
5275
|
1790 for (octave_idx_type i = 0; i < result_nr; i++) |
5164
|
1791 { |
|
1792 OCTAVE_QUIT; |
|
1793 |
5275
|
1794 octave_idx_type ii = idx_arg.elem (kk++); |
5164
|
1795 if (ii < orig_len) |
|
1796 { |
5275
|
1797 octave_idx_type fr = ii % nr; |
|
1798 octave_idx_type fc = (ii - fr) / nr; |
|
1799 for (octave_idx_type k = cidx(fc); k < cidx(fc+1); k++) |
5164
|
1800 { |
|
1801 if (ridx(k) == fr) |
|
1802 { |
|
1803 retval.xdata(jj) = data(k); |
|
1804 retval.xridx(jj++) = i; |
|
1805 } |
|
1806 if (ridx(k) >= fr) |
|
1807 break; |
|
1808 } |
|
1809 } |
|
1810 } |
|
1811 retval.xcidx(j+1) = jj; |
|
1812 } |
|
1813 } |
|
1814 // idx_vector::freeze() printed an error message for us. |
|
1815 } |
|
1816 } |
|
1817 |
|
1818 return retval; |
|
1819 } |
|
1820 |
|
1821 template <class T> |
|
1822 Sparse<T> |
|
1823 Sparse<T>::index (idx_vector& idx_i, idx_vector& idx_j, int resize_ok) const |
|
1824 { |
|
1825 Sparse<T> retval; |
|
1826 |
|
1827 assert (ndims () == 2); |
|
1828 |
5275
|
1829 octave_idx_type nr = dim1 (); |
|
1830 octave_idx_type nc = dim2 (); |
|
1831 |
|
1832 octave_idx_type n = idx_i.freeze (nr, "row", resize_ok); |
|
1833 octave_idx_type m = idx_j.freeze (nc, "column", resize_ok); |
5164
|
1834 |
|
1835 if (idx_i && idx_j) |
|
1836 { |
|
1837 if (idx_i.orig_empty () || idx_j.orig_empty () || n == 0 || m == 0) |
|
1838 { |
|
1839 retval.resize_no_fill (n, m); |
|
1840 } |
|
1841 else if (idx_i.is_colon_equiv (nr) && idx_j.is_colon_equiv (nc)) |
|
1842 { |
|
1843 retval = *this; |
|
1844 } |
|
1845 else |
|
1846 { |
|
1847 // First count the number of non-zero elements |
5604
|
1848 octave_idx_type new_nzmx = 0; |
5275
|
1849 for (octave_idx_type j = 0; j < m; j++) |
5164
|
1850 { |
5275
|
1851 octave_idx_type jj = idx_j.elem (j); |
|
1852 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1853 { |
|
1854 OCTAVE_QUIT; |
|
1855 |
5275
|
1856 octave_idx_type ii = idx_i.elem (i); |
5164
|
1857 if (ii < nr && jj < nc) |
|
1858 { |
5275
|
1859 for (octave_idx_type k = cidx(jj); k < cidx(jj+1); k++) |
5164
|
1860 { |
|
1861 if (ridx(k) == ii) |
5604
|
1862 new_nzmx++; |
5164
|
1863 if (ridx(k) >= ii) |
|
1864 break; |
|
1865 } |
|
1866 } |
|
1867 } |
|
1868 } |
|
1869 |
5604
|
1870 retval = Sparse<T> (n, m, new_nzmx); |
5164
|
1871 |
5275
|
1872 octave_idx_type kk = 0; |
5164
|
1873 retval.xcidx(0) = 0; |
5275
|
1874 for (octave_idx_type j = 0; j < m; j++) |
5164
|
1875 { |
5275
|
1876 octave_idx_type jj = idx_j.elem (j); |
|
1877 for (octave_idx_type i = 0; i < n; i++) |
5164
|
1878 { |
|
1879 OCTAVE_QUIT; |
|
1880 |
5275
|
1881 octave_idx_type ii = idx_i.elem (i); |
5164
|
1882 if (ii < nr && jj < nc) |
|
1883 { |
5275
|
1884 for (octave_idx_type k = cidx(jj); k < cidx(jj+1); k++) |
5164
|
1885 { |
|
1886 if (ridx(k) == ii) |
|
1887 { |
|
1888 retval.xdata(kk) = data(k); |
|
1889 retval.xridx(kk++) = i; |
|
1890 } |
|
1891 if (ridx(k) >= ii) |
|
1892 break; |
|
1893 } |
|
1894 } |
|
1895 } |
|
1896 retval.xcidx(j+1) = kk; |
|
1897 } |
|
1898 } |
|
1899 } |
|
1900 |
|
1901 // idx_vector::freeze() printed an error message for us. |
|
1902 |
|
1903 return retval; |
|
1904 } |
|
1905 |
|
1906 template <class T> |
|
1907 Sparse<T> |
|
1908 Sparse<T>::index (Array<idx_vector>& ra_idx, int resize_ok) const |
|
1909 { |
|
1910 |
|
1911 if (ra_idx.length () != 2) |
|
1912 { |
|
1913 (*current_liboctave_error_handler) ("range error for index"); |
|
1914 return *this; |
|
1915 } |
|
1916 |
|
1917 return index (ra_idx (0), ra_idx (1), resize_ok); |
|
1918 } |
|
1919 |
|
1920 // XXX FIXME XXX |
|
1921 // Unfortunately numel can overflow for very large but very sparse matrices. |
|
1922 // For now just flag an error when this happens. |
|
1923 template <class LT, class RT> |
|
1924 int |
|
1925 assign1 (Sparse<LT>& lhs, const Sparse<RT>& rhs) |
|
1926 { |
|
1927 int retval = 1; |
|
1928 |
|
1929 idx_vector *idx_tmp = lhs.get_idx (); |
|
1930 |
|
1931 idx_vector lhs_idx = idx_tmp[0]; |
|
1932 |
5275
|
1933 octave_idx_type lhs_len = lhs.numel (); |
|
1934 octave_idx_type rhs_len = rhs.numel (); |
5164
|
1935 |
|
1936 unsigned EIGHT_BYTE_INT long_lhs_len = |
|
1937 static_cast<unsigned EIGHT_BYTE_INT> (lhs.rows ()) * |
|
1938 static_cast<unsigned EIGHT_BYTE_INT> (lhs.cols ()); |
|
1939 |
|
1940 unsigned EIGHT_BYTE_INT long_rhs_len = |
|
1941 static_cast<unsigned EIGHT_BYTE_INT> (rhs.rows ()) * |
|
1942 static_cast<unsigned EIGHT_BYTE_INT> (rhs.cols ()); |
|
1943 |
|
1944 if (long_rhs_len != static_cast<unsigned EIGHT_BYTE_INT>(rhs_len) || |
|
1945 long_lhs_len != static_cast<unsigned EIGHT_BYTE_INT>(lhs_len)) |
|
1946 { |
|
1947 (*current_liboctave_error_handler) |
|
1948 ("A(I) = X: Matrix dimensions too large to ensure correct\n", |
|
1949 "operation. This is an limitation that should be removed\n", |
|
1950 "in the future."); |
|
1951 |
|
1952 lhs.clear_index (); |
|
1953 return 0; |
|
1954 } |
|
1955 |
5275
|
1956 octave_idx_type nr = lhs.rows (); |
|
1957 octave_idx_type nc = lhs.cols (); |
5604
|
1958 octave_idx_type nz = lhs.nzmax (); |
5275
|
1959 |
5603
|
1960 octave_idx_type n = lhs_idx.freeze (lhs_len, "vector", true, |
|
1961 liboctave_wrore_flag); |
5164
|
1962 |
|
1963 if (n != 0) |
|
1964 { |
5275
|
1965 octave_idx_type max_idx = lhs_idx.max () + 1; |
5164
|
1966 max_idx = max_idx < lhs_len ? lhs_len : max_idx; |
|
1967 |
|
1968 // Take a constant copy of lhs. This means that elem won't |
|
1969 // create missing elements. |
|
1970 const Sparse<LT> c_lhs (lhs); |
|
1971 |
|
1972 if (rhs_len == n) |
|
1973 { |
5604
|
1974 octave_idx_type new_nzmx = lhs.nzmax (); |
5164
|
1975 |
5603
|
1976 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx, n); |
|
1977 if (! lhs_idx.is_colon ()) |
|
1978 { |
|
1979 // Ok here we have to be careful with the indexing, |
|
1980 // to treat cases like "a([3,2,1]) = b", and still |
|
1981 // handle the need for strict sorting of the sparse |
|
1982 // elements. |
|
1983 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, sidx, n); |
|
1984 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, sidxX, n); |
|
1985 |
|
1986 for (octave_idx_type i = 0; i < n; i++) |
|
1987 { |
|
1988 sidx[i] = &sidxX[i]; |
|
1989 sidx[i]->i = lhs_idx.elem(i); |
|
1990 sidx[i]->idx = i; |
|
1991 } |
|
1992 |
|
1993 OCTAVE_QUIT; |
|
1994 octave_sort<octave_idx_vector_sort *> |
|
1995 sort (octave_idx_vector_comp); |
|
1996 |
|
1997 sort.sort (sidx, n); |
|
1998 |
|
1999 intNDArray<octave_idx_type> new_idx (dim_vector (n,1)); |
|
2000 |
|
2001 for (octave_idx_type i = 0; i < n; i++) |
|
2002 { |
|
2003 new_idx.xelem(i) = sidx[i]->i + 1; |
|
2004 rhs_idx[i] = sidx[i]->idx; |
|
2005 } |
|
2006 |
|
2007 lhs_idx = idx_vector (new_idx); |
|
2008 } |
|
2009 else |
|
2010 for (octave_idx_type i = 0; i < n; i++) |
|
2011 rhs_idx[i] = i; |
|
2012 |
5164
|
2013 // First count the number of non-zero elements |
5275
|
2014 for (octave_idx_type i = 0; i < n; i++) |
5164
|
2015 { |
|
2016 OCTAVE_QUIT; |
|
2017 |
5275
|
2018 octave_idx_type ii = lhs_idx.elem (i); |
5164
|
2019 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
5604
|
2020 new_nzmx--; |
5603
|
2021 if (rhs.elem(rhs_idx[i]) != RT ()) |
5604
|
2022 new_nzmx++; |
5164
|
2023 } |
|
2024 |
|
2025 if (nr > 1) |
|
2026 { |
5604
|
2027 Sparse<LT> tmp (max_idx, 1, new_nzmx); |
5164
|
2028 tmp.cidx(0) = 0; |
5604
|
2029 tmp.cidx(1) = tmp.nzmax (); |
5164
|
2030 |
5275
|
2031 octave_idx_type i = 0; |
|
2032 octave_idx_type ii = 0; |
5164
|
2033 if (i < nz) |
|
2034 ii = c_lhs.ridx(i); |
|
2035 |
5275
|
2036 octave_idx_type j = 0; |
|
2037 octave_idx_type jj = lhs_idx.elem(j); |
|
2038 |
|
2039 octave_idx_type kk = 0; |
5164
|
2040 |
|
2041 while (j < n || i < nz) |
|
2042 { |
|
2043 if (j == n || (i < nz && ii < jj)) |
|
2044 { |
|
2045 tmp.xdata (kk) = c_lhs.data (i); |
|
2046 tmp.xridx (kk++) = ii; |
|
2047 if (++i < nz) |
|
2048 ii = c_lhs.ridx(i); |
|
2049 } |
|
2050 else |
|
2051 { |
5603
|
2052 RT rtmp = rhs.elem (rhs_idx[j]); |
5164
|
2053 if (rtmp != RT ()) |
|
2054 { |
|
2055 tmp.xdata (kk) = rtmp; |
|
2056 tmp.xridx (kk++) = jj; |
|
2057 } |
|
2058 |
|
2059 if (ii == jj && i < nz) |
|
2060 if (++i < nz) |
|
2061 ii = c_lhs.ridx(i); |
|
2062 if (++j < n) |
|
2063 jj = lhs_idx.elem(j); |
|
2064 } |
|
2065 } |
|
2066 |
|
2067 lhs = tmp; |
|
2068 } |
|
2069 else |
|
2070 { |
5604
|
2071 Sparse<LT> tmp (1, max_idx, new_nzmx); |
5164
|
2072 |
5275
|
2073 octave_idx_type i = 0; |
|
2074 octave_idx_type ii = 0; |
5164
|
2075 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2076 ii++; |
|
2077 |
5275
|
2078 octave_idx_type j = 0; |
|
2079 octave_idx_type jj = lhs_idx.elem(j); |
|
2080 |
|
2081 octave_idx_type kk = 0; |
|
2082 octave_idx_type ic = 0; |
5164
|
2083 |
|
2084 while (j < n || i < nz) |
|
2085 { |
|
2086 if (j == n || (i < nz && ii < jj)) |
|
2087 { |
|
2088 while (ic <= ii) |
|
2089 tmp.xcidx (ic++) = kk; |
|
2090 tmp.xdata (kk) = c_lhs.data (i); |
5603
|
2091 tmp.xridx (kk++) = 0; |
5164
|
2092 i++; |
|
2093 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2094 ii++; |
|
2095 } |
|
2096 else |
|
2097 { |
|
2098 while (ic <= jj) |
|
2099 tmp.xcidx (ic++) = kk; |
|
2100 |
5603
|
2101 RT rtmp = rhs.elem (rhs_idx[j]); |
5164
|
2102 if (rtmp != RT ()) |
5603
|
2103 { |
|
2104 tmp.xdata (kk) = rtmp; |
|
2105 tmp.xridx (kk++) = 0; |
|
2106 } |
5164
|
2107 if (ii == jj) |
|
2108 { |
|
2109 i++; |
|
2110 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2111 ii++; |
|
2112 } |
|
2113 j++; |
|
2114 if (j < n) |
|
2115 jj = lhs_idx.elem(j); |
|
2116 } |
|
2117 } |
|
2118 |
5275
|
2119 for (octave_idx_type iidx = ic; iidx < max_idx+1; iidx++) |
5164
|
2120 tmp.xcidx(iidx) = kk; |
|
2121 |
|
2122 lhs = tmp; |
|
2123 } |
|
2124 } |
|
2125 else if (rhs_len == 1) |
|
2126 { |
5604
|
2127 octave_idx_type new_nzmx = lhs.nzmax (); |
5164
|
2128 RT scalar = rhs.elem (0); |
|
2129 bool scalar_non_zero = (scalar != RT ()); |
5603
|
2130 lhs_idx.sort (true); |
5164
|
2131 |
|
2132 // First count the number of non-zero elements |
|
2133 if (scalar != RT ()) |
5604
|
2134 new_nzmx += n; |
5275
|
2135 for (octave_idx_type i = 0; i < n; i++) |
5164
|
2136 { |
|
2137 OCTAVE_QUIT; |
|
2138 |
5275
|
2139 octave_idx_type ii = lhs_idx.elem (i); |
5164
|
2140 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
5604
|
2141 new_nzmx--; |
5164
|
2142 } |
|
2143 |
|
2144 if (nr > 1) |
|
2145 { |
5604
|
2146 Sparse<LT> tmp (max_idx, 1, new_nzmx); |
5164
|
2147 tmp.cidx(0) = 0; |
5604
|
2148 tmp.cidx(1) = tmp.nzmax (); |
5164
|
2149 |
5275
|
2150 octave_idx_type i = 0; |
|
2151 octave_idx_type ii = 0; |
5164
|
2152 if (i < nz) |
|
2153 ii = c_lhs.ridx(i); |
|
2154 |
5275
|
2155 octave_idx_type j = 0; |
|
2156 octave_idx_type jj = lhs_idx.elem(j); |
|
2157 |
|
2158 octave_idx_type kk = 0; |
5164
|
2159 |
|
2160 while (j < n || i < nz) |
|
2161 { |
|
2162 if (j == n || (i < nz && ii < jj)) |
|
2163 { |
|
2164 tmp.xdata (kk) = c_lhs.data (i); |
|
2165 tmp.xridx (kk++) = ii; |
|
2166 if (++i < nz) |
|
2167 ii = c_lhs.ridx(i); |
|
2168 } |
|
2169 else |
|
2170 { |
|
2171 if (scalar_non_zero) |
|
2172 { |
|
2173 tmp.xdata (kk) = scalar; |
|
2174 tmp.xridx (kk++) = jj; |
|
2175 } |
|
2176 |
|
2177 if (ii == jj && i < nz) |
|
2178 if (++i < nz) |
|
2179 ii = c_lhs.ridx(i); |
|
2180 if (++j < n) |
|
2181 jj = lhs_idx.elem(j); |
|
2182 } |
|
2183 } |
|
2184 |
|
2185 lhs = tmp; |
|
2186 } |
|
2187 else |
|
2188 { |
5604
|
2189 Sparse<LT> tmp (1, max_idx, new_nzmx); |
5164
|
2190 |
5275
|
2191 octave_idx_type i = 0; |
|
2192 octave_idx_type ii = 0; |
5164
|
2193 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2194 ii++; |
|
2195 |
5275
|
2196 octave_idx_type j = 0; |
|
2197 octave_idx_type jj = lhs_idx.elem(j); |
|
2198 |
|
2199 octave_idx_type kk = 0; |
|
2200 octave_idx_type ic = 0; |
5164
|
2201 |
|
2202 while (j < n || i < nz) |
|
2203 { |
|
2204 if (j == n || (i < nz && ii < jj)) |
|
2205 { |
|
2206 while (ic <= ii) |
|
2207 tmp.xcidx (ic++) = kk; |
|
2208 tmp.xdata (kk) = c_lhs.data (i); |
|
2209 i++; |
|
2210 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2211 ii++; |
|
2212 } |
|
2213 else |
|
2214 { |
|
2215 while (ic <= jj) |
|
2216 tmp.xcidx (ic++) = kk; |
|
2217 if (scalar_non_zero) |
|
2218 tmp.xdata (kk) = scalar; |
|
2219 if (ii == jj) |
|
2220 { |
|
2221 i++; |
|
2222 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
|
2223 ii++; |
|
2224 } |
|
2225 j++; |
|
2226 if (j < n) |
|
2227 jj = lhs_idx.elem(j); |
|
2228 } |
|
2229 tmp.xridx (kk++) = 0; |
|
2230 } |
|
2231 |
5275
|
2232 for (octave_idx_type iidx = ic; iidx < max_idx+1; iidx++) |
5164
|
2233 tmp.xcidx(iidx) = kk; |
|
2234 |
|
2235 lhs = tmp; |
|
2236 } |
|
2237 } |
|
2238 else |
|
2239 { |
|
2240 (*current_liboctave_error_handler) |
|
2241 ("A(I) = X: X must be a scalar or a vector with same length as I"); |
|
2242 |
|
2243 retval = 0; |
|
2244 } |
|
2245 } |
|
2246 else if (lhs_idx.is_colon ()) |
|
2247 { |
|
2248 if (lhs_len == 0) |
|
2249 { |
|
2250 |
5604
|
2251 octave_idx_type new_nzmx = rhs.nzmax (); |
|
2252 Sparse<LT> tmp (1, rhs_len, new_nzmx); |
5164
|
2253 |
5275
|
2254 octave_idx_type ii = 0; |
|
2255 octave_idx_type jj = 0; |
|
2256 for (octave_idx_type i = 0; i < rhs.cols(); i++) |
|
2257 for (octave_idx_type j = rhs.cidx(i); j < rhs.cidx(i+1); j++) |
5164
|
2258 { |
|
2259 OCTAVE_QUIT; |
5275
|
2260 for (octave_idx_type k = jj; k <= i * rhs.rows() + rhs.ridx(j); k++) |
5164
|
2261 tmp.cidx(jj++) = ii; |
|
2262 |
|
2263 tmp.data(ii) = rhs.data(j); |
|
2264 tmp.ridx(ii++) = 0; |
|
2265 } |
|
2266 |
5275
|
2267 for (octave_idx_type i = jj; i < rhs_len + 1; i++) |
5164
|
2268 tmp.cidx(i) = ii; |
|
2269 |
|
2270 lhs = tmp; |
|
2271 } |
|
2272 else |
|
2273 (*current_liboctave_error_handler) |
|
2274 ("A(:) = X: A must be the same size as X"); |
|
2275 } |
|
2276 else if (! (rhs_len == 1 || rhs_len == 0)) |
|
2277 { |
|
2278 (*current_liboctave_error_handler) |
|
2279 ("A([]) = X: X must also be an empty matrix or a scalar"); |
|
2280 |
|
2281 retval = 0; |
|
2282 } |
|
2283 |
|
2284 lhs.clear_index (); |
|
2285 |
|
2286 return retval; |
|
2287 } |
|
2288 |
|
2289 template <class LT, class RT> |
|
2290 int |
|
2291 assign (Sparse<LT>& lhs, const Sparse<RT>& rhs) |
|
2292 { |
|
2293 int retval = 1; |
|
2294 |
|
2295 int n_idx = lhs.index_count (); |
|
2296 |
5275
|
2297 octave_idx_type lhs_nr = lhs.rows (); |
|
2298 octave_idx_type lhs_nc = lhs.cols (); |
5604
|
2299 octave_idx_type lhs_nz = lhs.nzmax (); |
5275
|
2300 |
|
2301 octave_idx_type rhs_nr = rhs.rows (); |
|
2302 octave_idx_type rhs_nc = rhs.cols (); |
5164
|
2303 |
|
2304 idx_vector *tmp = lhs.get_idx (); |
|
2305 |
|
2306 idx_vector idx_i; |
|
2307 idx_vector idx_j; |
|
2308 |
|
2309 if (n_idx > 2) |
|
2310 { |
|
2311 (*current_liboctave_error_handler) |
|
2312 ("A(I, J) = X: can only have 1 or 2 indexes for sparse matrices"); |
|
2313 return 0; |
|
2314 } |
|
2315 |
|
2316 if (n_idx > 1) |
|
2317 idx_j = tmp[1]; |
|
2318 |
|
2319 if (n_idx > 0) |
|
2320 idx_i = tmp[0]; |
|
2321 |
|
2322 if (n_idx == 2) |
|
2323 { |
5603
|
2324 octave_idx_type n = idx_i.freeze (lhs_nr, "row", true, |
|
2325 liboctave_wrore_flag); |
|
2326 octave_idx_type m = idx_j.freeze (lhs_nc, "column", true, |
|
2327 liboctave_wrore_flag); |
5164
|
2328 |
|
2329 int idx_i_is_colon = idx_i.is_colon (); |
|
2330 int idx_j_is_colon = idx_j.is_colon (); |
|
2331 |
|
2332 if (idx_i_is_colon) |
|
2333 n = lhs_nr > 0 ? lhs_nr : rhs_nr; |
|
2334 |
|
2335 if (idx_j_is_colon) |
|
2336 m = lhs_nc > 0 ? lhs_nc : rhs_nc; |
|
2337 |
|
2338 if (idx_i && idx_j) |
|
2339 { |
|
2340 if (rhs_nr == 0 && rhs_nc == 0) |
|
2341 { |
|
2342 lhs.maybe_delete_elements (idx_i, idx_j); |
|
2343 } |
|
2344 else |
|
2345 { |
|
2346 if (rhs_nr == 1 && rhs_nc == 1 && n >= 0 && m >= 0) |
|
2347 { |
|
2348 // No need to do anything if either of the indices |
|
2349 // are empty. |
|
2350 |
|
2351 if (n > 0 && m > 0) |
|
2352 { |
5603
|
2353 idx_i.sort (true); |
|
2354 idx_j.sort (true); |
|
2355 |
5275
|
2356 octave_idx_type max_row_idx = idx_i_is_colon ? rhs_nr : |
5164
|
2357 idx_i.max () + 1; |
5275
|
2358 octave_idx_type max_col_idx = idx_j_is_colon ? rhs_nc : |
5164
|
2359 idx_j.max () + 1; |
5603
|
2360 octave_idx_type new_nr = max_row_idx > lhs_nr ? |
|
2361 max_row_idx : lhs_nr; |
|
2362 octave_idx_type new_nc = max_col_idx > lhs_nc ? |
|
2363 max_col_idx : lhs_nc; |
5164
|
2364 RT scalar = rhs.elem (0, 0); |
|
2365 |
|
2366 // Count the number of non-zero terms |
5604
|
2367 octave_idx_type new_nzmx = lhs.nzmax (); |
5275
|
2368 for (octave_idx_type j = 0; j < m; j++) |
5164
|
2369 { |
5275
|
2370 octave_idx_type jj = idx_j.elem (j); |
5164
|
2371 if (jj < lhs_nc) |
|
2372 { |
5275
|
2373 for (octave_idx_type i = 0; i < n; i++) |
5164
|
2374 { |
|
2375 OCTAVE_QUIT; |
|
2376 |
5275
|
2377 octave_idx_type ii = idx_i.elem (i); |
5164
|
2378 |
|
2379 if (ii < lhs_nr) |
|
2380 { |
5275
|
2381 for (octave_idx_type k = lhs.cidx(jj); |
5164
|
2382 k < lhs.cidx(jj+1); k++) |
|
2383 { |
|
2384 if (lhs.ridx(k) == ii) |
5604
|
2385 new_nzmx--; |
5164
|
2386 if (lhs.ridx(k) >= ii) |
|
2387 break; |
|
2388 } |
|
2389 } |
|
2390 } |
|
2391 } |
|
2392 } |
|
2393 |
|
2394 if (scalar != RT()) |
5604
|
2395 new_nzmx += m * n; |
|
2396 |
|
2397 Sparse<LT> stmp (new_nr, new_nc, new_nzmx); |
5164
|
2398 |
5275
|
2399 octave_idx_type jji = 0; |
|
2400 octave_idx_type jj = idx_j.elem (jji); |
|
2401 octave_idx_type kk = 0; |
5164
|
2402 stmp.cidx(0) = 0; |
5275
|
2403 for (octave_idx_type j = 0; j < new_nc; j++) |
5164
|
2404 { |
|
2405 if (jji < m && jj == j) |
|
2406 { |
5275
|
2407 octave_idx_type iii = 0; |
|
2408 octave_idx_type ii = idx_i.elem (iii); |
|
2409 for (octave_idx_type i = 0; i < new_nr; i++) |
5164
|
2410 { |
|
2411 OCTAVE_QUIT; |
|
2412 |
|
2413 if (iii < n && ii == i) |
|
2414 { |
|
2415 if (scalar != RT ()) |
|
2416 { |
|
2417 stmp.data(kk) = scalar; |
|
2418 stmp.ridx(kk++) = i; |
|
2419 } |
|
2420 if (++iii < n) |
|
2421 ii = idx_i.elem(iii); |
|
2422 } |
|
2423 else if (j < lhs.cols()) |
|
2424 { |
5275
|
2425 for (octave_idx_type k = lhs.cidx(j); |
5164
|
2426 k < lhs.cidx(j+1); k++) |
|
2427 { |
|
2428 if (lhs.ridx(k) == i) |
|
2429 { |
|
2430 stmp.data(kk) = lhs.data(k); |
|
2431 stmp.ridx(kk++) = i; |
|
2432 } |
|
2433 if (lhs.ridx(k) >= i) |
|
2434 break; |
|
2435 } |
|
2436 } |
|
2437 } |
|
2438 if (++jji < m) |
|
2439 jj = idx_j.elem(jji); |
|
2440 } |
|
2441 else if (j < lhs.cols()) |
|
2442 { |
5275
|
2443 for (octave_idx_type i = lhs.cidx(j); |
5164
|
2444 i < lhs.cidx(j+1); i++) |
|
2445 { |
|
2446 stmp.data(kk) = lhs.data(i); |
|
2447 stmp.ridx(kk++) = lhs.ridx(i); |
|
2448 } |
|
2449 } |
|
2450 stmp.cidx(j+1) = kk; |
|
2451 } |
|
2452 |
|
2453 lhs = stmp; |
|
2454 } |
|
2455 } |
|
2456 else if (n == rhs_nr && m == rhs_nc) |
|
2457 { |
|
2458 if (n > 0 && m > 0) |
|
2459 { |
5275
|
2460 octave_idx_type max_row_idx = idx_i_is_colon ? rhs_nr : |
5164
|
2461 idx_i.max () + 1; |
5275
|
2462 octave_idx_type max_col_idx = idx_j_is_colon ? rhs_nc : |
5164
|
2463 idx_j.max () + 1; |
5603
|
2464 octave_idx_type new_nr = max_row_idx > lhs_nr ? |
|
2465 max_row_idx : lhs_nr; |
|
2466 octave_idx_type new_nc = max_col_idx > lhs_nc ? |
|
2467 max_col_idx : lhs_nc; |
|
2468 |
|
2469 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx_i, n); |
|
2470 if (! idx_i.is_colon ()) |
|
2471 { |
|
2472 // Ok here we have to be careful with the indexing, |
|
2473 // to treat cases like "a([3,2,1],:) = b", and still |
|
2474 // handle the need for strict sorting of the sparse |
|
2475 // elements. |
|
2476 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, |
|
2477 sidx, n); |
|
2478 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, |
|
2479 sidxX, n); |
|
2480 |
|
2481 for (octave_idx_type i = 0; i < n; i++) |
|
2482 { |
|
2483 sidx[i] = &sidxX[i]; |
|
2484 sidx[i]->i = idx_i.elem(i); |
|
2485 sidx[i]->idx = i; |
|
2486 } |
|
2487 |
|
2488 OCTAVE_QUIT; |
|
2489 octave_sort<octave_idx_vector_sort *> |
|
2490 sort (octave_idx_vector_comp); |
|
2491 |
|
2492 sort.sort (sidx, n); |
|
2493 |
|
2494 intNDArray<octave_idx_type> new_idx (dim_vector (n,1)); |
|
2495 |
|
2496 for (octave_idx_type i = 0; i < n; i++) |
|
2497 { |
|
2498 new_idx.xelem(i) = sidx[i]->i + 1; |
|
2499 rhs_idx_i[i] = sidx[i]->idx; |
|
2500 } |
|
2501 |
|
2502 idx_i = idx_vector (new_idx); |
|
2503 } |
|
2504 else |
|
2505 for (octave_idx_type i = 0; i < n; i++) |
|
2506 rhs_idx_i[i] = i; |
|
2507 |
|
2508 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx_j, m); |
|
2509 if (! idx_j.is_colon ()) |
|
2510 { |
|
2511 // Ok here we have to be careful with the indexing, |
|
2512 // to treat cases like "a([3,2,1],:) = b", and still |
|
2513 // handle the need for strict sorting of the sparse |
|
2514 // elements. |
|
2515 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, |
|
2516 sidx, m); |
|
2517 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, |
|
2518 sidxX, m); |
|
2519 |
|
2520 for (octave_idx_type i = 0; i < m; i++) |
|
2521 { |
|
2522 sidx[i] = &sidxX[i]; |
|
2523 sidx[i]->i = idx_j.elem(i); |
|
2524 sidx[i]->idx = i; |
|
2525 } |
|
2526 |
|
2527 OCTAVE_QUIT; |
|
2528 octave_sort<octave_idx_vector_sort *> |
|
2529 sort (octave_idx_vector_comp); |
|
2530 |
|
2531 sort.sort (sidx, m); |
|
2532 |
|
2533 intNDArray<octave_idx_type> new_idx (dim_vector (m,1)); |
|
2534 |
|
2535 for (octave_idx_type i = 0; i < m; i++) |
|
2536 { |
|
2537 new_idx.xelem(i) = sidx[i]->i + 1; |
|
2538 rhs_idx_j[i] = sidx[i]->idx; |
|
2539 } |
|
2540 |
|
2541 idx_j = idx_vector (new_idx); |
|
2542 } |
|
2543 else |
|
2544 for (octave_idx_type i = 0; i < m; i++) |
|
2545 rhs_idx_j[i] = i; |
5164
|
2546 |
|
2547 // Count the number of non-zero terms |
5604
|
2548 octave_idx_type new_nzmx = lhs.nzmax (); |
5275
|
2549 for (octave_idx_type j = 0; j < m; j++) |
5164
|
2550 { |
5275
|
2551 octave_idx_type jj = idx_j.elem (j); |
|
2552 for (octave_idx_type i = 0; i < n; i++) |
5164
|
2553 { |
|
2554 OCTAVE_QUIT; |
|
2555 |
|
2556 if (jj < lhs_nc) |
|
2557 { |
5275
|
2558 octave_idx_type ii = idx_i.elem (i); |
5164
|
2559 |
|
2560 if (ii < lhs_nr) |
|
2561 { |
5275
|
2562 for (octave_idx_type k = lhs.cidx(jj); |
5164
|
2563 k < lhs.cidx(jj+1); k++) |
|
2564 { |
|
2565 if (lhs.ridx(k) == ii) |
5604
|
2566 new_nzmx--; |
5164
|
2567 if (lhs.ridx(k) >= ii) |
|
2568 break; |
|
2569 } |
|
2570 } |
|
2571 } |
|
2572 |
5603
|
2573 if (rhs.elem(rhs_idx_i[i],rhs_idx_j[j]) != RT ()) |
5604
|
2574 new_nzmx++; |
5164
|
2575 } |
|
2576 } |
|
2577 |
5604
|
2578 Sparse<LT> stmp (new_nr, new_nc, new_nzmx); |
5164
|
2579 |
5275
|
2580 octave_idx_type jji = 0; |
|
2581 octave_idx_type jj = idx_j.elem (jji); |
|
2582 octave_idx_type kk = 0; |
5164
|
2583 stmp.cidx(0) = 0; |
5275
|
2584 for (octave_idx_type j = 0; j < new_nc; j++) |
5164
|
2585 { |
|
2586 if (jji < m && jj == j) |
|
2587 { |
5275
|
2588 octave_idx_type iii = 0; |
|
2589 octave_idx_type ii = idx_i.elem (iii); |
|
2590 for (octave_idx_type i = 0; i < new_nr; i++) |
5164
|
2591 { |
|
2592 OCTAVE_QUIT; |
|
2593 |
|
2594 if (iii < n && ii == i) |
|
2595 { |
5603
|
2596 RT rtmp = rhs.elem (rhs_idx_i[iii], |
|
2597 rhs_idx_j[jji]); |
5164
|
2598 if (rtmp != RT ()) |
|
2599 { |
|
2600 stmp.data(kk) = rtmp; |
|
2601 stmp.ridx(kk++) = i; |
|
2602 } |
|
2603 if (++iii < n) |
|
2604 ii = idx_i.elem(iii); |
|
2605 } |
|
2606 else if (j < lhs.cols()) |
|
2607 { |
5275
|
2608 for (octave_idx_type k = lhs.cidx(j); |
5164
|
2609 k < lhs.cidx(j+1); k++) |
|
2610 { |
|
2611 if (lhs.ridx(k) == i) |
|
2612 { |
|
2613 stmp.data(kk) = lhs.data(k); |
|
2614 stmp.ridx(kk++) = i; |
|
2615 } |
|
2616 if (lhs.ridx(k) >= i) |
|
2617 break; |
|
2618 } |
|
2619 } |
|
2620 } |
|
2621 if (++jji < m) |
|
2622 jj = idx_j.elem(jji); |
|
2623 } |
|
2624 else if (j < lhs.cols()) |
|
2625 { |
5275
|
2626 for (octave_idx_type i = lhs.cidx(j); |
5164
|
2627 i < lhs.cidx(j+1); i++) |
|
2628 { |
|
2629 stmp.data(kk) = lhs.data(i); |
|
2630 stmp.ridx(kk++) = lhs.ridx(i); |
|
2631 } |
|
2632 } |
|
2633 stmp.cidx(j+1) = kk; |
|
2634 } |
|
2635 |
|
2636 lhs = stmp; |
|
2637 } |
|
2638 } |
|
2639 else if (n == 0 && m == 0) |
|
2640 { |
|
2641 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2642 || (rhs_nr == 0 || rhs_nc == 0))) |
|
2643 { |
|
2644 (*current_liboctave_error_handler) |
|
2645 ("A([], []) = X: X must be an empty matrix or a scalar"); |
|
2646 |
|
2647 retval = 0; |
|
2648 } |
|
2649 } |
|
2650 else |
|
2651 { |
|
2652 (*current_liboctave_error_handler) |
|
2653 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
|
2654 (*current_liboctave_error_handler) |
|
2655 ("match the number of rows in X and the number of elements in J must"); |
|
2656 (*current_liboctave_error_handler) |
|
2657 ("match the number of columns in X"); |
|
2658 |
|
2659 retval = 0; |
|
2660 } |
|
2661 } |
|
2662 } |
|
2663 // idx_vector::freeze() printed an error message for us. |
|
2664 } |
|
2665 else if (n_idx == 1) |
|
2666 { |
|
2667 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; |
|
2668 |
|
2669 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) |
|
2670 { |
5275
|
2671 octave_idx_type lhs_len = lhs.length (); |
|
2672 |
5603
|
2673 octave_idx_type n = idx_i.freeze (lhs_len, 0, true, |
|
2674 liboctave_wrore_flag); |
5164
|
2675 |
|
2676 if (idx_i) |
|
2677 { |
|
2678 if (rhs_nr == 0 && rhs_nc == 0) |
|
2679 { |
|
2680 if (n != 0 && (lhs_nr != 0 || lhs_nc != 0)) |
|
2681 lhs.maybe_delete_elements (idx_i); |
|
2682 } |
|
2683 else |
|
2684 { |
|
2685 if (liboctave_wfi_flag) |
|
2686 { |
|
2687 if (lhs_is_empty |
|
2688 && idx_i.is_colon () |
|
2689 && ! (rhs_nr == 1 || rhs_nc == 1)) |
|
2690 { |
|
2691 (*current_liboctave_warning_handler) |
|
2692 ("A(:) = X: X is not a vector or scalar"); |
|
2693 } |
|
2694 else |
|
2695 { |
5275
|
2696 octave_idx_type idx_nr = idx_i.orig_rows (); |
|
2697 octave_idx_type idx_nc = idx_i.orig_columns (); |
5164
|
2698 |
|
2699 if (! (rhs_nr == idx_nr && rhs_nc == idx_nc)) |
|
2700 (*current_liboctave_warning_handler) |
|
2701 ("A(I) = X: X does not have same shape as I"); |
|
2702 } |
|
2703 } |
|
2704 |
|
2705 if (! assign1 ((Sparse<LT>&) lhs, (Sparse<RT>&) rhs)) |
|
2706 retval = 0; |
|
2707 } |
|
2708 } |
|
2709 // idx_vector::freeze() printed an error message for us. |
|
2710 } |
|
2711 else if (lhs_nr == 1) |
|
2712 { |
|
2713 idx_i.freeze (lhs_nc, "vector", true, liboctave_wrore_flag); |
|
2714 |
|
2715 if (idx_i) |
|
2716 { |
|
2717 if (rhs_nr == 0 && rhs_nc == 0) |
|
2718 lhs.maybe_delete_elements (idx_i); |
|
2719 else if (! assign1 ((Sparse<LT>&) lhs, (Sparse<RT>&) rhs)) |
|
2720 retval = 0; |
|
2721 } |
|
2722 // idx_vector::freeze() printed an error message for us. |
|
2723 } |
|
2724 else if (lhs_nc == 1) |
|
2725 { |
|
2726 idx_i.freeze (lhs_nr, "vector", true, liboctave_wrore_flag); |
|
2727 |
|
2728 if (idx_i) |
|
2729 { |
|
2730 if (rhs_nr == 0 && rhs_nc == 0) |
|
2731 lhs.maybe_delete_elements (idx_i); |
|
2732 else if (! assign1 ((Sparse<LT>&) lhs, (Sparse<RT>&) rhs)) |
|
2733 retval = 0; |
|
2734 } |
|
2735 // idx_vector::freeze() printed an error message for us. |
|
2736 } |
|
2737 else |
|
2738 { |
|
2739 if (liboctave_wfi_flag |
|
2740 && ! (idx_i.is_colon () |
|
2741 || (idx_i.one_zero_only () |
|
2742 && idx_i.orig_rows () == lhs_nr |
|
2743 && idx_i.orig_columns () == lhs_nc))) |
|
2744 (*current_liboctave_warning_handler) |
|
2745 ("single index used for matrix"); |
|
2746 |
5275
|
2747 octave_idx_type lhs_len = lhs.length (); |
|
2748 |
|
2749 octave_idx_type len = idx_i.freeze (lhs_nr * lhs_nc, "matrix"); |
5164
|
2750 |
|
2751 if (idx_i) |
|
2752 { |
|
2753 // Take a constant copy of lhs. This means that elem won't |
|
2754 // create missing elements. |
|
2755 const Sparse<LT> c_lhs (lhs); |
|
2756 |
|
2757 if (rhs_nr == 0 && rhs_nc == 0) |
|
2758 lhs.maybe_delete_elements (idx_i); |
|
2759 else if (len == 0) |
|
2760 { |
|
2761 if (! ((rhs_nr == 1 && rhs_nc == 1) |
|
2762 || (rhs_nr == 0 || rhs_nc == 0))) |
|
2763 (*current_liboctave_error_handler) |
|
2764 ("A([]) = X: X must be an empty matrix or scalar"); |
|
2765 } |
|
2766 else if (len == rhs_nr * rhs_nc) |
|
2767 { |
5604
|
2768 octave_idx_type new_nzmx = lhs_nz; |
5603
|
2769 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx, len); |
|
2770 |
|
2771 if (! idx_i.is_colon ()) |
|
2772 { |
|
2773 // Ok here we have to be careful with the indexing, to |
|
2774 // treat cases like "a([3,2,1]) = b", and still handle |
|
2775 // the need for strict sorting of the sparse elements. |
|
2776 |
|
2777 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, sidx, |
|
2778 len); |
|
2779 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, sidxX, |
|
2780 len); |
|
2781 |
|
2782 for (octave_idx_type i = 0; i < len; i++) |
|
2783 { |
|
2784 sidx[i] = &sidxX[i]; |
|
2785 sidx[i]->i = idx_i.elem(i); |
|
2786 sidx[i]->idx = i; |
|
2787 } |
|
2788 |
|
2789 OCTAVE_QUIT; |
|
2790 octave_sort<octave_idx_vector_sort *> |
|
2791 sort (octave_idx_vector_comp); |
|
2792 |
|
2793 sort.sort (sidx, len); |
|
2794 |
|
2795 intNDArray<octave_idx_type> new_idx (dim_vector (len,1)); |
|
2796 |
|
2797 for (octave_idx_type i = 0; i < len; i++) |
|
2798 { |
|
2799 new_idx.xelem(i) = sidx[i]->i + 1; |
|
2800 rhs_idx[i] = sidx[i]->idx; |
|
2801 } |
|
2802 |
|
2803 idx_i = idx_vector (new_idx); |
|
2804 } |
|
2805 else |
|
2806 for (octave_idx_type i = 0; i < len; i++) |
|
2807 rhs_idx[i] = i; |
5164
|
2808 |
|
2809 // First count the number of non-zero elements |
5275
|
2810 for (octave_idx_type i = 0; i < len; i++) |
5164
|
2811 { |
|
2812 OCTAVE_QUIT; |
|
2813 |
5275
|
2814 octave_idx_type ii = idx_i.elem (i); |
5164
|
2815 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
5604
|
2816 new_nzmx--; |
5603
|
2817 if (rhs.elem(rhs_idx[i]) != RT ()) |
5604
|
2818 new_nzmx++; |
5164
|
2819 } |
|
2820 |
5604
|
2821 Sparse<LT> stmp (lhs_nr, lhs_nc, new_nzmx); |
5164
|
2822 |
5275
|
2823 octave_idx_type i = 0; |
|
2824 octave_idx_type ii = 0; |
|
2825 octave_idx_type ic = 0; |
5164
|
2826 if (i < lhs_nz) |
|
2827 { |
|
2828 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2829 ic++; |
|
2830 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2831 } |
|
2832 |
5275
|
2833 octave_idx_type j = 0; |
|
2834 octave_idx_type jj = idx_i.elem (j); |
|
2835 octave_idx_type jr = jj % lhs_nr; |
|
2836 octave_idx_type jc = (jj - jr) / lhs_nr; |
|
2837 |
|
2838 octave_idx_type kk = 0; |
|
2839 octave_idx_type kc = 0; |
5164
|
2840 |
|
2841 while (j < len || i < lhs_nz) |
|
2842 { |
|
2843 if (j == len || (i < lhs_nz && ii < jj)) |
|
2844 { |
|
2845 while (kc <= ic) |
|
2846 stmp.xcidx (kc++) = kk; |
|
2847 stmp.xdata (kk) = c_lhs.data (i); |
|
2848 stmp.xridx (kk++) = c_lhs.ridx (i); |
|
2849 i++; |
|
2850 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2851 ic++; |
|
2852 if (i < lhs_nz) |
|
2853 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2854 } |
|
2855 else |
|
2856 { |
|
2857 while (kc <= jc) |
|
2858 stmp.xcidx (kc++) = kk; |
5603
|
2859 RT rtmp = rhs.elem (rhs_idx[j]); |
5164
|
2860 if (rtmp != RT ()) |
|
2861 { |
|
2862 stmp.xdata (kk) = rtmp; |
|
2863 stmp.xridx (kk++) = jr; |
|
2864 } |
|
2865 if (ii == jj) |
|
2866 { |
|
2867 i++; |
|
2868 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2869 ic++; |
|
2870 if (i < lhs_nz) |
|
2871 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2872 } |
|
2873 j++; |
|
2874 if (j < len) |
|
2875 { |
|
2876 jj = idx_i.elem (j); |
|
2877 jr = jj % lhs_nr; |
|
2878 jc = (jj - jr) / lhs_nr; |
|
2879 } |
|
2880 } |
|
2881 } |
|
2882 |
5275
|
2883 for (octave_idx_type iidx = kc; iidx < lhs_nc+1; iidx++) |
5603
|
2884 stmp.xcidx(iidx) = kk; |
5164
|
2885 |
|
2886 lhs = stmp; |
|
2887 } |
|
2888 else if (rhs_nr == 1 && rhs_nc == 1) |
|
2889 { |
|
2890 RT scalar = rhs.elem (0, 0); |
5604
|
2891 octave_idx_type new_nzmx = lhs_nz; |
5603
|
2892 idx_i.sort (true); |
5164
|
2893 |
|
2894 // First count the number of non-zero elements |
|
2895 if (scalar != RT ()) |
5604
|
2896 new_nzmx += len; |
5275
|
2897 for (octave_idx_type i = 0; i < len; i++) |
5164
|
2898 { |
|
2899 OCTAVE_QUIT; |
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--; |
5164
|
2903 } |
|
2904 |
5604
|
2905 Sparse<LT> stmp (lhs_nr, lhs_nc, new_nzmx); |
5164
|
2906 |
5275
|
2907 octave_idx_type i = 0; |
|
2908 octave_idx_type ii = 0; |
|
2909 octave_idx_type ic = 0; |
5164
|
2910 if (i < lhs_nz) |
|
2911 { |
|
2912 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2913 ic++; |
|
2914 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2915 } |
|
2916 |
5275
|
2917 octave_idx_type j = 0; |
|
2918 octave_idx_type jj = idx_i.elem (j); |
|
2919 octave_idx_type jr = jj % lhs_nr; |
|
2920 octave_idx_type jc = (jj - jr) / lhs_nr; |
|
2921 |
|
2922 octave_idx_type kk = 0; |
|
2923 octave_idx_type kc = 0; |
5164
|
2924 |
|
2925 while (j < len || i < lhs_nz) |
|
2926 { |
|
2927 if (j == len || (i < lhs_nz && ii < jj)) |
|
2928 { |
|
2929 while (kc <= ic) |
|
2930 stmp.xcidx (kc++) = kk; |
|
2931 stmp.xdata (kk) = c_lhs.data (i); |
|
2932 stmp.xridx (kk++) = c_lhs.ridx (i); |
|
2933 i++; |
|
2934 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2935 ic++; |
|
2936 if (i < lhs_nz) |
|
2937 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2938 } |
|
2939 else |
|
2940 { |
|
2941 while (kc <= jc) |
|
2942 stmp.xcidx (kc++) = kk; |
|
2943 if (scalar != RT ()) |
|
2944 { |
|
2945 stmp.xdata (kk) = scalar; |
|
2946 stmp.xridx (kk++) = jr; |
|
2947 } |
|
2948 if (ii == jj) |
|
2949 { |
|
2950 i++; |
|
2951 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
|
2952 ic++; |
|
2953 if (i < lhs_nz) |
|
2954 ii = ic * lhs_nr + c_lhs.ridx(i); |
|
2955 } |
|
2956 j++; |
|
2957 if (j < len) |
|
2958 { |
|
2959 jj = idx_i.elem (j); |
|
2960 jr = jj % lhs_nr; |
|
2961 jc = (jj - jr) / lhs_nr; |
|
2962 } |
|
2963 } |
|
2964 } |
|
2965 |
5275
|
2966 for (octave_idx_type iidx = kc; iidx < lhs_nc+1; iidx++) |
5164
|
2967 stmp.xcidx(iidx) = kk; |
|
2968 |
|
2969 lhs = stmp; |
|
2970 } |
|
2971 else |
|
2972 { |
|
2973 (*current_liboctave_error_handler) |
|
2974 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
|
2975 |
|
2976 retval = 0; |
|
2977 } |
|
2978 } |
|
2979 // idx_vector::freeze() printed an error message for us. |
|
2980 } |
|
2981 } |
|
2982 else |
|
2983 { |
|
2984 (*current_liboctave_error_handler) |
|
2985 ("invalid number of indices for matrix expression"); |
|
2986 |
|
2987 retval = 0; |
|
2988 } |
|
2989 |
|
2990 lhs.clear_index (); |
|
2991 |
|
2992 return retval; |
|
2993 } |
|
2994 |
|
2995 template <class T> |
|
2996 void |
|
2997 Sparse<T>::print_info (std::ostream& os, const std::string& prefix) const |
|
2998 { |
|
2999 os << prefix << "rep address: " << rep << "\n" |
5604
|
3000 << prefix << "rep->nzmx: " << rep->nzmx << "\n" |
5164
|
3001 << prefix << "rep->nrows: " << rep->nrows << "\n" |
|
3002 << prefix << "rep->ncols: " << rep->ncols << "\n" |
|
3003 << prefix << "rep->data: " << static_cast<void *> (rep->d) << "\n" |
|
3004 << prefix << "rep->ridx: " << static_cast<void *> (rep->r) << "\n" |
|
3005 << prefix << "rep->cidx: " << static_cast<void *> (rep->c) << "\n" |
|
3006 << prefix << "rep->count: " << rep->count << "\n"; |
|
3007 } |
|
3008 |
|
3009 /* |
|
3010 ;;; Local Variables: *** |
|
3011 ;;; mode: C++ *** |
|
3012 ;;; End: *** |
|
3013 */ |