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