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