Mercurial > octave-nkf
annotate liboctave/Sparse.cc @ 10432:10207338603a
improve sparse 2D indexing (part 1)
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Mon, 22 Mar 2010 13:54:22 +0100 |
parents | 0677c5d80b77 |
children | 3011d1765a6e |
rev | line source |
---|---|
5164 | 1 // Template sparse array class |
2 /* | |
3 | |
8920 | 4 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 David Bateman |
7016 | 5 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Andy Adler |
6 | |
7 This file is part of Octave. | |
5164 | 8 |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
5164 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
5164 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
26 #include <config.h> | |
27 #endif | |
28 | |
29 #include <cassert> | |
30 #include <climits> | |
31 | |
32 #include <iostream> | |
5765 | 33 #include <sstream> |
5164 | 34 #include <vector> |
35 | |
36 #include "Array.h" | |
37 #include "Array-util.h" | |
38 #include "Range.h" | |
39 #include "idx-vector.h" | |
40 #include "lo-error.h" | |
41 #include "quit.h" | |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
42 #include "oct-locbuf.h" |
5164 | 43 |
44 #include "Sparse.h" | |
45 #include "sparse-sort.h" | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9177
diff
changeset
|
46 #include "sparse-util.h" |
5164 | 47 #include "oct-spparms.h" |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
48 #include "mx-inlines.cc" |
5164 | 49 |
50 template <class T> | |
51 T& | |
5275 | 52 Sparse<T>::SparseRep::elem (octave_idx_type _r, octave_idx_type _c) |
5164 | 53 { |
5275 | 54 octave_idx_type i; |
5164 | 55 |
5604 | 56 if (nzmx > 0) |
5164 | 57 { |
58 for (i = c[_c]; i < c[_c + 1]; i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
59 if (r[i] == _r) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
60 return d[i]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
61 else if (r[i] > _r) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
62 break; |
5164 | 63 |
64 // Ok, If we've gotten here, we're in trouble.. Have to create a | |
65 // new element in the sparse array. This' gonna be slow!!! | |
5869 | 66 if (c[ncols] == nzmx) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
67 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
68 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
69 ("Sparse::SparseRep::elem (octave_idx_type, octave_idx_type): sparse matrix filled"); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
70 return *d; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
71 } |
5164 | 72 |
5275 | 73 octave_idx_type to_move = c[ncols] - i; |
5164 | 74 if (to_move != 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
75 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
76 for (octave_idx_type j = c[ncols]; j > i; j--) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
77 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
78 d[j] = d[j-1]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
79 r[j] = r[j-1]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
80 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
81 } |
5164 | 82 |
5275 | 83 for (octave_idx_type j = _c + 1; j < ncols + 1; j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
84 c[j] = c[j] + 1; |
5164 | 85 |
86 d[i] = 0.; | |
87 r[i] = _r; | |
88 | |
89 return d[i]; | |
90 } | |
91 else | |
92 { | |
93 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
94 ("Sparse::SparseRep::elem (octave_idx_type, octave_idx_type): sparse matrix filled"); |
5164 | 95 return *d; |
96 } | |
97 } | |
98 | |
99 template <class T> | |
100 T | |
5275 | 101 Sparse<T>::SparseRep::celem (octave_idx_type _r, octave_idx_type _c) const |
5164 | 102 { |
5604 | 103 if (nzmx > 0) |
5275 | 104 for (octave_idx_type i = c[_c]; i < c[_c + 1]; i++) |
5164 | 105 if (r[i] == _r) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
106 return d[i]; |
5164 | 107 return T (); |
108 } | |
109 | |
110 template <class T> | |
111 void | |
112 Sparse<T>::SparseRep::maybe_compress (bool remove_zeros) | |
113 { | |
5604 | 114 octave_idx_type ndel = nzmx - c[ncols]; |
5275 | 115 octave_idx_type nzero = 0; |
5164 | 116 |
117 if (remove_zeros) | |
5604 | 118 for (octave_idx_type i = 0; i < nzmx - ndel; i++) |
5164 | 119 if (d[i] == T ()) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
120 nzero++; |
5164 | 121 |
122 if (!ndel && !nzero) | |
123 return; | |
124 | |
125 if (!nzero) | |
126 { | |
5604 | 127 octave_idx_type new_nzmx = nzmx - ndel; |
128 | |
129 T *new_data = new T [new_nzmx]; | |
130 for (octave_idx_type i = 0; i < new_nzmx; i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
131 new_data[i] = d[i]; |
5164 | 132 delete [] d; |
133 d = new_data; | |
134 | |
5604 | 135 octave_idx_type *new_ridx = new octave_idx_type [new_nzmx]; |
136 for (octave_idx_type i = 0; i < new_nzmx; i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
137 new_ridx[i] = r[i]; |
5164 | 138 delete [] r; |
139 r = new_ridx; | |
140 } | |
141 else | |
142 { | |
5604 | 143 octave_idx_type new_nzmx = nzmx - ndel - nzero; |
144 | |
145 T *new_data = new T [new_nzmx]; | |
146 octave_idx_type *new_ridx = new octave_idx_type [new_nzmx]; | |
5275 | 147 |
148 octave_idx_type ii = 0; | |
149 octave_idx_type ic = 0; | |
150 for (octave_idx_type j = 0; j < ncols; j++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
151 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
152 for (octave_idx_type k = ic; k < c[j+1]; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
153 if (d[k] != T ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
154 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
155 new_data [ii] = d[k]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
156 new_ridx [ii++] = r[k]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
157 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
158 ic = c[j+1]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
159 c[j+1] = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
160 } |
5164 | 161 |
162 delete [] d; | |
163 d = new_data; | |
164 | |
165 delete [] r; | |
166 r = new_ridx; | |
167 } | |
168 | |
5604 | 169 nzmx -= ndel + nzero; |
5164 | 170 } |
171 | |
172 template <class T> | |
173 void | |
5275 | 174 Sparse<T>::SparseRep::change_length (octave_idx_type nz) |
5164 | 175 { |
5604 | 176 if (nz != nzmx) |
5164 | 177 { |
5604 | 178 octave_idx_type min_nzmx = (nz < nzmx ? nz : nzmx); |
5275 | 179 |
180 octave_idx_type * new_ridx = new octave_idx_type [nz]; | |
5604 | 181 for (octave_idx_type i = 0; i < min_nzmx; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
182 new_ridx[i] = r[i]; |
5164 | 183 |
184 delete [] r; | |
185 r = new_ridx; | |
186 | |
187 T * new_data = new T [nz]; | |
5604 | 188 for (octave_idx_type i = 0; i < min_nzmx; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
189 new_data[i] = d[i]; |
5164 | 190 |
191 delete [] d; | |
192 d = new_data; | |
193 | |
5604 | 194 if (nz < nzmx) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
195 for (octave_idx_type i = 0; i <= ncols; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
196 if (c[i] > nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
197 c[i] = nz; |
5164 | 198 |
5604 | 199 nzmx = nz; |
5164 | 200 } |
201 } | |
202 | |
203 template <class T> | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9177
diff
changeset
|
204 bool |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9177
diff
changeset
|
205 Sparse<T>::SparseRep::indices_ok (void) const |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9177
diff
changeset
|
206 { |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9177
diff
changeset
|
207 return sparse_indices_ok (r, c, nrows, ncols, nnz ()); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9177
diff
changeset
|
208 } |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9177
diff
changeset
|
209 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
9177
diff
changeset
|
210 template <class T> |
5164 | 211 template <class U> |
212 Sparse<T>::Sparse (const Sparse<U>& a) | |
213 : dimensions (a.dimensions), idx (0), idx_count (0) | |
214 { | |
5681 | 215 if (a.nnz () == 0) |
5164 | 216 rep = new typename Sparse<T>::SparseRep (rows (), cols()); |
217 else | |
218 { | |
5681 | 219 rep = new typename Sparse<T>::SparseRep (rows (), cols (), a.nnz ()); |
5164 | 220 |
5681 | 221 octave_idx_type nz = a.nnz (); |
5275 | 222 octave_idx_type nc = cols (); |
223 for (octave_idx_type i = 0; i < nz; i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
224 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
225 xdata (i) = T (a.data (i)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
226 xridx (i) = a.ridx (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
227 } |
5275 | 228 for (octave_idx_type i = 0; i < nc + 1; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
229 xcidx (i) = a.cidx (i); |
5164 | 230 } |
231 } | |
232 | |
233 template <class T> | |
5275 | 234 Sparse<T>::Sparse (octave_idx_type nr, octave_idx_type nc, T val) |
5630 | 235 : dimensions (dim_vector (nr, nc)), idx (0), idx_count (0) |
5164 | 236 { |
5630 | 237 if (val != T ()) |
5164 | 238 { |
5630 | 239 rep = new typename Sparse<T>::SparseRep (nr, nc, nr*nc); |
240 | |
241 octave_idx_type ii = 0; | |
242 xcidx (0) = 0; | |
243 for (octave_idx_type j = 0; j < nc; j++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
244 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
245 for (octave_idx_type i = 0; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
246 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
247 xdata (ii) = val; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
248 xridx (ii++) = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
249 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
250 xcidx (j+1) = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
251 } |
5630 | 252 } |
253 else | |
254 { | |
255 rep = new typename Sparse<T>::SparseRep (nr, nc, 0); | |
256 for (octave_idx_type j = 0; j < nc+1; j++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
257 xcidx(j) = 0; |
5164 | 258 } |
259 } | |
260 | |
261 template <class T> | |
262 Sparse<T>::Sparse (const dim_vector& dv) | |
263 : dimensions (dv), idx (0), idx_count (0) | |
264 { | |
265 if (dv.length() != 2) | |
266 (*current_liboctave_error_handler) | |
267 ("Sparse::Sparse (const dim_vector&): dimension mismatch"); | |
268 else | |
269 rep = new typename Sparse<T>::SparseRep (dv(0), dv(1)); | |
270 } | |
271 | |
272 template <class T> | |
273 Sparse<T>::Sparse (const Sparse<T>& a, const dim_vector& dv) | |
274 : dimensions (dv), idx (0), idx_count (0) | |
275 { | |
276 | |
277 // Work in unsigned long long to avoid overflow issues with numel | |
278 unsigned long long a_nel = static_cast<unsigned long long>(a.rows ()) * | |
279 static_cast<unsigned long long>(a.cols ()); | |
280 unsigned long long dv_nel = static_cast<unsigned long long>(dv (0)) * | |
281 static_cast<unsigned long long>(dv (1)); | |
282 | |
283 if (a_nel != dv_nel) | |
284 (*current_liboctave_error_handler) | |
285 ("Sparse::Sparse (const Sparse&, const dim_vector&): dimension mismatch"); | |
286 else | |
287 { | |
288 dim_vector old_dims = a.dims(); | |
5681 | 289 octave_idx_type new_nzmx = a.nnz (); |
5275 | 290 octave_idx_type new_nr = dv (0); |
291 octave_idx_type new_nc = dv (1); | |
292 octave_idx_type old_nr = old_dims (0); | |
293 octave_idx_type old_nc = old_dims (1); | |
5164 | 294 |
5604 | 295 rep = new typename Sparse<T>::SparseRep (new_nr, new_nc, new_nzmx); |
5164 | 296 |
5275 | 297 octave_idx_type kk = 0; |
5164 | 298 xcidx(0) = 0; |
5275 | 299 for (octave_idx_type i = 0; i < old_nc; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
300 for (octave_idx_type j = a.cidx(i); j < a.cidx(i+1); j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
301 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
302 octave_idx_type tmp = i * old_nr + a.ridx(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
303 octave_idx_type ii = tmp % new_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
304 octave_idx_type jj = (tmp - ii) / new_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
305 for (octave_idx_type k = kk; k < jj; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
306 xcidx(k+1) = j; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
307 kk = jj; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
308 xdata(j) = a.data(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
309 xridx(j) = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
310 } |
5275 | 311 for (octave_idx_type k = kk; k < new_nc; k++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
312 xcidx(k+1) = new_nzmx; |
5164 | 313 } |
314 } | |
315 | |
316 template <class T> | |
5275 | 317 Sparse<T>::Sparse (const Array<T>& a, const Array<octave_idx_type>& r, |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
318 const Array<octave_idx_type>& c, octave_idx_type nr, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
319 octave_idx_type nc, bool sum_terms) |
5164 | 320 : dimensions (dim_vector (nr, nc)), idx (0), idx_count (0) |
321 { | |
5275 | 322 octave_idx_type a_len = a.length (); |
323 octave_idx_type r_len = r.length (); | |
324 octave_idx_type c_len = c.length (); | |
5164 | 325 bool ri_scalar = (r_len == 1); |
326 bool ci_scalar = (c_len == 1); | |
327 bool cf_scalar = (a_len == 1); | |
328 | |
329 if ((a_len != r_len && !cf_scalar && !ri_scalar) || | |
330 (a_len != c_len && !cf_scalar && !ci_scalar) || | |
331 (r_len != c_len && !ri_scalar && !ci_scalar) || nr < 0 || nc < 0) | |
332 { | |
333 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
334 ("Sparse::Sparse (const Array<T>&, const Array<octave_idx_type>&, ...): dimension mismatch"); |
5164 | 335 rep = nil_rep (); |
336 dimensions = dim_vector (0, 0); | |
337 } | |
338 else | |
339 { | |
5604 | 340 octave_idx_type max_nzmx = (r_len > c_len ? r_len : c_len); |
341 | |
342 OCTAVE_LOCAL_BUFFER (octave_sparse_sort_idxl *, sidx, max_nzmx); | |
343 OCTAVE_LOCAL_BUFFER (octave_sparse_sort_idxl, sidxX, max_nzmx); | |
344 | |
345 for (octave_idx_type i = 0; i < max_nzmx; i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
346 sidx[i] = &sidxX[i]; |
5164 | 347 |
5604 | 348 octave_idx_type actual_nzmx = 0; |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
349 octave_quit (); |
5604 | 350 for (octave_idx_type i = 0; i < max_nzmx; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
351 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
352 octave_idx_type rowidx = (ri_scalar ? r(0) : r(i)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
353 octave_idx_type colidx = (ci_scalar ? c(0) : c(i)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
354 if (rowidx < nr && rowidx >= 0 && |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
355 colidx < nc && colidx >= 0 ) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
356 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
357 if ( a (cf_scalar ? 0 : i ) != T ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
358 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
359 sidx[actual_nzmx]->r = rowidx; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
360 sidx[actual_nzmx]->c = colidx; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
361 sidx[actual_nzmx]->idx = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
362 actual_nzmx++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
363 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
364 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
365 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
366 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
367 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
368 ("Sparse::Sparse : index (%d,%d) out of range", |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
369 rowidx + 1, colidx + 1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
370 rep = nil_rep (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
371 dimensions = dim_vector (0, 0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
372 return; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
373 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
374 } |
5164 | 375 |
5604 | 376 if (actual_nzmx == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
377 rep = new typename Sparse<T>::SparseRep (nr, nc); |
5164 | 378 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
379 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
380 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
381 octave_sort<octave_sparse_sort_idxl *> |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
382 lsort (octave_sparse_sidxl_comp); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
383 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
384 lsort.sort (sidx, actual_nzmx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
385 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
386 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
387 // Now count the unique non-zero values |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
388 octave_idx_type real_nzmx = 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
389 for (octave_idx_type i = 1; i < actual_nzmx; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
390 if (sidx[i-1]->r != sidx[i]->r || sidx[i-1]->c != sidx[i]->c) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
391 real_nzmx++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
392 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
393 rep = new typename Sparse<T>::SparseRep (nr, nc, real_nzmx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
394 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
395 octave_idx_type cx = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
396 octave_idx_type prev_rval = -1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
397 octave_idx_type prev_cval = -1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
398 octave_idx_type ii = -1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
399 xcidx (0) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
400 for (octave_idx_type i = 0; i < actual_nzmx; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
401 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
402 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
403 octave_idx_type iidx = sidx[i]->idx; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
404 octave_idx_type rval = sidx[i]->r; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
405 octave_idx_type cval = sidx[i]->c; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
406 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
407 if (prev_cval < cval || (prev_rval < rval && prev_cval == cval)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
408 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
409 octave_idx_type ci = static_cast<octave_idx_type> (c (ci_scalar ? 0 : iidx)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
410 ii++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
411 while (cx < ci) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
412 xcidx (++cx) = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
413 xdata(ii) = a (cf_scalar ? 0 : iidx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
414 xridx(ii) = static_cast<octave_idx_type> (r (ri_scalar ? 0 : iidx)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
415 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
416 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
417 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
418 if (sum_terms) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
419 xdata(ii) += a (cf_scalar ? 0 : iidx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
420 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
421 xdata(ii) = a (cf_scalar ? 0 : iidx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
422 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
423 prev_rval = rval; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
424 prev_cval = cval; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
425 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
426 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
427 while (cx < nc) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
428 xcidx (++cx) = ii + 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
429 } |
5164 | 430 } |
431 } | |
432 | |
433 template <class T> | |
434 Sparse<T>::Sparse (const Array<T>& a, const Array<double>& r, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
435 const Array<double>& c, octave_idx_type nr, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
436 octave_idx_type nc, bool sum_terms) |
5164 | 437 : dimensions (dim_vector (nr, nc)), idx (0), idx_count (0) |
438 { | |
5275 | 439 octave_idx_type a_len = a.length (); |
440 octave_idx_type r_len = r.length (); | |
441 octave_idx_type c_len = c.length (); | |
5164 | 442 bool ri_scalar = (r_len == 1); |
443 bool ci_scalar = (c_len == 1); | |
444 bool cf_scalar = (a_len == 1); | |
445 | |
446 if ((a_len != r_len && !cf_scalar && !ri_scalar) || | |
447 (a_len != c_len && !cf_scalar && !ci_scalar) || | |
448 (r_len != c_len && !ri_scalar && !ci_scalar) || nr < 0 || nc < 0) | |
449 { | |
450 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
451 ("Sparse::Sparse (const Array<T>&, const Array<double>&, ...): dimension mismatch"); |
5164 | 452 rep = nil_rep (); |
453 dimensions = dim_vector (0, 0); | |
454 } | |
455 else | |
456 { | |
5604 | 457 octave_idx_type max_nzmx = (r_len > c_len ? r_len : c_len); |
5164 | 458 |
5604 | 459 OCTAVE_LOCAL_BUFFER (octave_sparse_sort_idxl *, sidx, max_nzmx); |
460 OCTAVE_LOCAL_BUFFER (octave_sparse_sort_idxl, sidxX, max_nzmx); | |
461 | |
462 for (octave_idx_type i = 0; i < max_nzmx; i++) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
463 sidx[i] = &sidxX[i]; |
5164 | 464 |
5604 | 465 octave_idx_type actual_nzmx = 0; |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
466 octave_quit (); |
5164 | 467 |
5604 | 468 for (octave_idx_type i = 0; i < max_nzmx; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
469 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
470 octave_idx_type rowidx = static_cast<octave_idx_type> (ri_scalar ? r(0) : r(i)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
471 octave_idx_type colidx = static_cast<octave_idx_type> (ci_scalar ? c(0) : c(i)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
472 if (rowidx < nr && rowidx >= 0 && |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
473 colidx < nc && colidx >= 0 ) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
474 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
475 if ( a (cf_scalar ? 0 : i ) != T ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
476 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
477 sidx[actual_nzmx]->r = rowidx; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
478 sidx[actual_nzmx]->c = colidx; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
479 sidx[actual_nzmx]->idx = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
480 actual_nzmx++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
481 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
482 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
483 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
484 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
485 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
486 ("Sparse::Sparse : index (%d,%d) out of range", |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
487 rowidx + 1, colidx + 1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
488 rep = nil_rep (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
489 dimensions = dim_vector (0, 0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
490 return; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
491 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
492 } |
5164 | 493 |
5604 | 494 if (actual_nzmx == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
495 rep = new typename Sparse<T>::SparseRep (nr, nc); |
5164 | 496 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
497 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
498 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
499 octave_sort<octave_sparse_sort_idxl *> |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
500 lsort (octave_sparse_sidxl_comp); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
501 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
502 lsort.sort (sidx, actual_nzmx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
503 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
504 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
505 // Now count the unique non-zero values |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
506 octave_idx_type real_nzmx = 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
507 for (octave_idx_type i = 1; i < actual_nzmx; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
508 if (sidx[i-1]->r != sidx[i]->r || sidx[i-1]->c != sidx[i]->c) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
509 real_nzmx++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
510 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
511 rep = new typename Sparse<T>::SparseRep (nr, nc, real_nzmx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
512 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
513 octave_idx_type cx = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
514 octave_idx_type prev_rval = -1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
515 octave_idx_type prev_cval = -1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
516 octave_idx_type ii = -1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
517 xcidx (0) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
518 for (octave_idx_type i = 0; i < actual_nzmx; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
519 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
520 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
521 octave_idx_type iidx = sidx[i]->idx; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
522 octave_idx_type rval = sidx[i]->r; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
523 octave_idx_type cval = sidx[i]->c; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
524 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
525 if (prev_cval < cval || (prev_rval < rval && prev_cval == cval)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
526 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
527 octave_idx_type ci = static_cast<octave_idx_type> (c (ci_scalar ? 0 : iidx)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
528 ii++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
529 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
530 while (cx < ci) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
531 xcidx (++cx) = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
532 xdata(ii) = a (cf_scalar ? 0 : iidx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
533 xridx(ii) = static_cast<octave_idx_type> (r (ri_scalar ? 0 : iidx)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
534 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
535 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
536 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
537 if (sum_terms) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
538 xdata(ii) += a (cf_scalar ? 0 : iidx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
539 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
540 xdata(ii) = a (cf_scalar ? 0 : iidx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
541 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
542 prev_rval = rval; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
543 prev_cval = cval; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
544 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
545 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
546 while (cx < nc) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
547 xcidx (++cx) = ii + 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
548 } |
5164 | 549 } |
550 } | |
551 | |
552 template <class T> | |
553 Sparse<T>::Sparse (const Array<T>& a) | |
554 : dimensions (a.dims ()), idx (0), idx_count (0) | |
555 { | |
556 if (dimensions.length () > 2) | |
557 (*current_liboctave_error_handler) | |
558 ("Sparse::Sparse (const Array<T>&): dimension mismatch"); | |
559 else | |
560 { | |
5275 | 561 octave_idx_type nr = rows (); |
562 octave_idx_type nc = cols (); | |
563 octave_idx_type len = a.length (); | |
5604 | 564 octave_idx_type new_nzmx = 0; |
5164 | 565 |
566 // First count the number of non-zero terms | |
5275 | 567 for (octave_idx_type i = 0; i < len; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
568 if (a(i) != T ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
569 new_nzmx++; |
5604 | 570 |
571 rep = new typename Sparse<T>::SparseRep (nr, nc, new_nzmx); | |
5164 | 572 |
5275 | 573 octave_idx_type ii = 0; |
5164 | 574 xcidx(0) = 0; |
5275 | 575 for (octave_idx_type j = 0; j < nc; j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
576 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
577 for (octave_idx_type i = 0; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
578 if (a.elem (i,j) != T ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
579 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
580 xdata(ii) = a.elem (i,j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
581 xridx(ii++) = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
582 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
583 xcidx(j+1) = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
584 } |
5164 | 585 } |
586 } | |
587 | |
588 template <class T> | |
589 Sparse<T>::~Sparse (void) | |
590 { | |
591 if (--rep->count <= 0) | |
592 delete rep; | |
593 | |
594 delete [] idx; | |
595 } | |
596 | |
597 template <class T> | |
7717
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
598 Sparse<T>& |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
599 Sparse<T>::operator = (const Sparse<T>& a) |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
600 { |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
601 if (this != &a) |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
602 { |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
603 if (--rep->count <= 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
604 delete rep; |
7717
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
605 |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
606 rep = a.rep; |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
607 rep->count++; |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
608 |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
609 dimensions = a.dimensions; |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
610 |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
611 delete [] idx; |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
612 idx_count = 0; |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
613 idx = 0; |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
614 } |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
615 |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
616 return *this; |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
617 } |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
618 |
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
619 template <class T> |
5275 | 620 octave_idx_type |
621 Sparse<T>::compute_index (const Array<octave_idx_type>& ra_idx) const | |
5164 | 622 { |
5275 | 623 octave_idx_type retval = -1; |
624 | |
625 octave_idx_type n = dimensions.length (); | |
5164 | 626 |
627 if (n > 0 && n == ra_idx.length ()) | |
628 { | |
629 retval = ra_idx(--n); | |
630 | |
631 while (--n >= 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
632 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
633 retval *= dimensions(n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
634 retval += ra_idx(n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
635 } |
5164 | 636 } |
637 else | |
638 (*current_liboctave_error_handler) | |
639 ("Sparse<T>::compute_index: invalid ra_idxing operation"); | |
640 | |
641 return retval; | |
642 } | |
643 | |
644 template <class T> | |
645 T | |
5275 | 646 Sparse<T>::range_error (const char *fcn, octave_idx_type n) const |
5164 | 647 { |
648 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); | |
649 return T (); | |
650 } | |
651 | |
652 template <class T> | |
653 T& | |
5275 | 654 Sparse<T>::range_error (const char *fcn, octave_idx_type n) |
5164 | 655 { |
656 (*current_liboctave_error_handler) ("%s (%d): range error", fcn, n); | |
657 static T foo; | |
658 return foo; | |
659 } | |
660 | |
661 template <class T> | |
662 T | |
5275 | 663 Sparse<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j) const |
5164 | 664 { |
665 (*current_liboctave_error_handler) | |
666 ("%s (%d, %d): range error", fcn, i, j); | |
667 return T (); | |
668 } | |
669 | |
670 template <class T> | |
671 T& | |
5275 | 672 Sparse<T>::range_error (const char *fcn, octave_idx_type i, octave_idx_type j) |
5164 | 673 { |
674 (*current_liboctave_error_handler) | |
675 ("%s (%d, %d): range error", fcn, i, j); | |
676 static T foo; | |
677 return foo; | |
678 } | |
679 | |
680 template <class T> | |
681 T | |
5275 | 682 Sparse<T>::range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const |
5164 | 683 { |
5765 | 684 std::ostringstream buf; |
5164 | 685 |
686 buf << fcn << " ("; | |
687 | |
5275 | 688 octave_idx_type n = ra_idx.length (); |
5164 | 689 |
690 if (n > 0) | |
691 buf << ra_idx(0); | |
692 | |
5275 | 693 for (octave_idx_type i = 1; i < n; i++) |
5164 | 694 buf << ", " << ra_idx(i); |
695 | |
696 buf << "): range error"; | |
5765 | 697 |
698 std::string buf_str = buf.str (); | |
699 | |
700 (*current_liboctave_error_handler) (buf_str.c_str ()); | |
5164 | 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 { |
5765 | 709 std::ostringstream buf; |
5164 | 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 | |
5765 | 723 std::string buf_str = buf.str (); |
724 | |
725 (*current_liboctave_error_handler) (buf_str.c_str ()); | |
5164 | 726 |
727 static T foo; | |
728 return foo; | |
729 } | |
730 | |
731 template <class T> | |
732 Sparse<T> | |
733 Sparse<T>::reshape (const dim_vector& new_dims) const | |
734 { | |
735 Sparse<T> retval; | |
6689 | 736 dim_vector dims2 = new_dims; |
737 | |
738 if (dims2.length () > 2) | |
5164 | 739 { |
6814 | 740 (*current_liboctave_warning_handler) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
741 ("reshape: sparse reshape to N-d array smashes dims"); |
6814 | 742 |
6689 | 743 for (octave_idx_type i = 2; i < dims2.length(); i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
744 dims2(1) *= dims2(i); |
6814 | 745 |
6689 | 746 dims2.resize (2); |
747 } | |
748 | |
749 if (dimensions != dims2) | |
750 { | |
751 if (dimensions.numel () == dims2.numel ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
752 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
753 octave_idx_type new_nnz = nnz (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
754 octave_idx_type new_nr = dims2 (0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
755 octave_idx_type new_nc = dims2 (1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
756 octave_idx_type old_nr = rows (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
757 octave_idx_type old_nc = cols (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
758 retval = Sparse<T> (new_nr, new_nc, new_nnz); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
759 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
760 octave_idx_type kk = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
761 retval.xcidx(0) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
762 for (octave_idx_type i = 0; i < old_nc; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
763 for (octave_idx_type j = cidx(i); j < cidx(i+1); j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
764 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
765 octave_idx_type tmp = i * old_nr + ridx(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
766 octave_idx_type ii = tmp % new_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
767 octave_idx_type jj = (tmp - ii) / new_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
768 for (octave_idx_type k = kk; k < jj; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
769 retval.xcidx(k+1) = j; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
770 kk = jj; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
771 retval.xdata(j) = data(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
772 retval.xridx(j) = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
773 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
774 for (octave_idx_type k = kk; k < new_nc; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
775 retval.xcidx(k+1) = new_nnz; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
776 } |
5164 | 777 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
778 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
779 std::string dimensions_str = dimensions.str (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
780 std::string new_dims_str = new_dims.str (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
781 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
782 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
783 ("reshape: can't reshape %s array to %s array", |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
784 dimensions_str.c_str (), new_dims_str.c_str ()); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
785 } |
5164 | 786 } |
787 else | |
788 retval = *this; | |
789 | |
790 return retval; | |
791 } | |
792 | |
793 template <class T> | |
794 Sparse<T> | |
5275 | 795 Sparse<T>::permute (const Array<octave_idx_type>& perm_vec, bool) const |
5164 | 796 { |
6813 | 797 // The only valid permutations of a sparse array are [1, 2] and [2, 1]. |
798 | |
799 bool fail = false; | |
6817 | 800 bool trans = false; |
6813 | 801 |
802 if (perm_vec.length () == 2) | |
5164 | 803 { |
6813 | 804 if (perm_vec(0) == 0 && perm_vec(1) == 1) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
805 /* do nothing */; |
6813 | 806 else if (perm_vec(0) == 1 && perm_vec(1) == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
807 trans = true; |
5164 | 808 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
809 fail = true; |
5164 | 810 } |
811 else | |
6813 | 812 fail = true; |
813 | |
814 if (fail) | |
815 (*current_liboctave_error_handler) | |
816 ("permutation vector contains an invalid element"); | |
817 | |
6817 | 818 return trans ? this->transpose () : *this; |
5164 | 819 } |
820 | |
821 template <class T> | |
822 void | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
823 Sparse<T>::resize1 (octave_idx_type n) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
824 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
825 octave_idx_type nr = rows (), nc = cols (); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
826 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
827 if (nr == 1 || nr == 0) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
828 resize (1, n); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
829 else if (nc == 1) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
830 resize (n, 1); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
831 else |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
832 gripe_invalid_resize (); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
833 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
834 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
835 template <class T> |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
836 void |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
837 Sparse<T>::resize (const dim_vector& dv) |
5164 | 838 { |
5275 | 839 octave_idx_type n = dv.length (); |
5164 | 840 |
841 if (n != 2) | |
842 { | |
843 (*current_liboctave_error_handler) ("sparse array must be 2-D"); | |
844 return; | |
845 } | |
846 | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
847 resize (dv(0), dv(1)); |
5164 | 848 } |
849 | |
850 template <class T> | |
851 void | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
852 Sparse<T>::resize (octave_idx_type r, octave_idx_type c) |
5164 | 853 { |
854 if (r < 0 || c < 0) | |
855 { | |
856 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
857 ("can't resize to negative dimension"); |
5164 | 858 return; |
859 } | |
860 | |
861 if (ndims () == 0) | |
862 dimensions = dim_vector (0, 0); | |
863 | |
864 if (r == dim1 () && c == dim2 ()) | |
865 return; | |
866 | |
5731 | 867 typename Sparse<T>::SparseRep *old_rep = rep; |
868 | |
5275 | 869 octave_idx_type nc = cols (); |
870 octave_idx_type nr = rows (); | |
5164 | 871 |
5681 | 872 if (nnz () == 0 || r == 0 || c == 0) |
5164 | 873 // Special case of redimensioning to/from a sparse matrix with |
874 // no elements | |
875 rep = new typename Sparse<T>::SparseRep (r, c); | |
876 else | |
877 { | |
5275 | 878 octave_idx_type n = 0; |
5164 | 879 Sparse<T> tmpval; |
880 if (r >= nr) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
881 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
882 if (c > nc) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
883 n = xcidx(nc); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
884 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
885 n = xcidx(c); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
886 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
887 tmpval = Sparse<T> (r, c, n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
888 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
889 if (c > nc) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
890 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
891 for (octave_idx_type i = 0; i < nc + 1; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
892 tmpval.cidx(i) = xcidx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
893 for (octave_idx_type i = nc + 1; i < c + 1; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
894 tmpval.cidx(i) = tmpval.cidx(i-1); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
895 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
896 else if (c <= nc) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
897 for (octave_idx_type i = 0; i < c + 1; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
898 tmpval.cidx(i) = xcidx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
899 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
900 for (octave_idx_type i = 0; i < n; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
901 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
902 tmpval.data(i) = xdata(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
903 tmpval.ridx(i) = xridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
904 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
905 } |
5164 | 906 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
907 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
908 // Count how many non zero terms before we do anything |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
909 octave_idx_type min_nc = (c < nc ? c : nc); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
910 for (octave_idx_type i = 0; i < min_nc; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
911 for (octave_idx_type j = xcidx(i); j < xcidx(i+1); j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
912 if (xridx(j) < r) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
913 n++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
914 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
915 if (n) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
916 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
917 // Now that we know the size we can do something |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
918 tmpval = Sparse<T> (r, c, n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
919 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
920 tmpval.cidx(0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
921 for (octave_idx_type i = 0, ii = 0; i < min_nc; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
922 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
923 for (octave_idx_type j = xcidx(i); j < xcidx(i+1); j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
924 if (xridx(j) < r) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
925 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
926 tmpval.data(ii) = xdata(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
927 tmpval.ridx(ii++) = xridx(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
928 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
929 tmpval.cidx(i+1) = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
930 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
931 if (c > min_nc) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
932 for (octave_idx_type i = nc; i < c; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
933 tmpval.cidx(i+1) = tmpval.cidx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
934 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
935 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
936 tmpval = Sparse<T> (r, c); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
937 } |
5164 | 938 |
939 rep = tmpval.rep; | |
940 rep->count++; | |
941 } | |
942 | |
943 dimensions = dim_vector (r, c); | |
944 | |
945 if (--old_rep->count <= 0) | |
946 delete old_rep; | |
947 } | |
948 | |
949 template <class T> | |
950 Sparse<T>& | |
5275 | 951 Sparse<T>::insert (const Sparse<T>& a, octave_idx_type r, octave_idx_type c) |
5164 | 952 { |
5275 | 953 octave_idx_type a_rows = a.rows (); |
954 octave_idx_type a_cols = a.cols (); | |
955 octave_idx_type nr = rows (); | |
956 octave_idx_type nc = cols (); | |
5164 | 957 |
958 if (r < 0 || r + a_rows > rows () || c < 0 || c + a_cols > cols ()) | |
959 { | |
960 (*current_liboctave_error_handler) ("range error for insert"); | |
961 return *this; | |
962 } | |
963 | |
964 // First count the number of elements in the final array | |
5681 | 965 octave_idx_type nel = cidx(c) + a.nnz (); |
5164 | 966 |
967 if (c + a_cols < nc) | |
968 nel += cidx(nc) - cidx(c + a_cols); | |
969 | |
5275 | 970 for (octave_idx_type i = c; i < c + a_cols; i++) |
971 for (octave_idx_type j = cidx(i); j < cidx(i+1); j++) | |
5164 | 972 if (ridx(j) < r || ridx(j) >= r + a_rows) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
973 nel++; |
5164 | 974 |
975 Sparse<T> tmp (*this); | |
976 --rep->count; | |
977 rep = new typename Sparse<T>::SparseRep (nr, nc, nel); | |
978 | |
5275 | 979 for (octave_idx_type i = 0; i < tmp.cidx(c); i++) |
5164 | 980 { |
981 data(i) = tmp.data(i); | |
982 ridx(i) = tmp.ridx(i); | |
983 } | |
5275 | 984 for (octave_idx_type i = 0; i < c + 1; i++) |
5164 | 985 cidx(i) = tmp.cidx(i); |
986 | |
5275 | 987 octave_idx_type ii = cidx(c); |
988 | |
989 for (octave_idx_type i = c; i < c + a_cols; i++) | |
5164 | 990 { |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
991 octave_quit (); |
5164 | 992 |
5275 | 993 for (octave_idx_type j = tmp.cidx(i); j < tmp.cidx(i+1); j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
994 if (tmp.ridx(j) < r) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
995 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
996 data(ii) = tmp.data(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
997 ridx(ii++) = tmp.ridx(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
998 } |
5164 | 999 |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
1000 octave_quit (); |
5164 | 1001 |
5275 | 1002 for (octave_idx_type j = a.cidx(i-c); j < a.cidx(i-c+1); j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1003 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1004 data(ii) = a.data(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1005 ridx(ii++) = r + a.ridx(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1006 } |
5164 | 1007 |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
1008 octave_quit (); |
5164 | 1009 |
5275 | 1010 for (octave_idx_type j = tmp.cidx(i); j < tmp.cidx(i+1); j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1011 if (tmp.ridx(j) >= r + a_rows) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1012 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1013 data(ii) = tmp.data(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1014 ridx(ii++) = tmp.ridx(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1015 } |
5164 | 1016 |
1017 cidx(i+1) = ii; | |
1018 } | |
1019 | |
5275 | 1020 for (octave_idx_type i = c + a_cols; i < nc; i++) |
5164 | 1021 { |
5275 | 1022 for (octave_idx_type j = tmp.cidx(i); j < tmp.cidx(i+1); j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1023 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1024 data(ii) = tmp.data(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1025 ridx(ii++) = tmp.ridx(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1026 } |
5164 | 1027 cidx(i+1) = ii; |
1028 } | |
1029 | |
1030 return *this; | |
1031 } | |
1032 | |
1033 template <class T> | |
1034 Sparse<T>& | |
5275 | 1035 Sparse<T>::insert (const Sparse<T>& a, const Array<octave_idx_type>& ra_idx) |
5164 | 1036 { |
1037 | |
1038 if (ra_idx.length () != 2) | |
1039 { | |
1040 (*current_liboctave_error_handler) ("range error for insert"); | |
1041 return *this; | |
1042 } | |
1043 | |
1044 return insert (a, ra_idx (0), ra_idx (1)); | |
1045 } | |
1046 | |
1047 template <class T> | |
1048 Sparse<T> | |
1049 Sparse<T>::transpose (void) const | |
1050 { | |
1051 assert (ndims () == 2); | |
1052 | |
5275 | 1053 octave_idx_type nr = rows (); |
1054 octave_idx_type nc = cols (); | |
5648 | 1055 octave_idx_type nz = nnz (); |
5164 | 1056 Sparse<T> retval (nc, nr, nz); |
1057 | |
5648 | 1058 for (octave_idx_type i = 0; i < nz; i++) |
8987
542015fada9e
Eliminate the workspace in sparse transpose.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
1059 retval.xcidx (ridx (i) + 1)++; |
542015fada9e
Eliminate the workspace in sparse transpose.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
1060 // retval.xcidx[1:nr] holds the row degrees for rows 0:(nr-1) |
5648 | 1061 nz = 0; |
8987
542015fada9e
Eliminate the workspace in sparse transpose.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
1062 for (octave_idx_type i = 1; i <= nr; i++) |
5164 | 1063 { |
8987
542015fada9e
Eliminate the workspace in sparse transpose.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
1064 const octave_idx_type tmp = retval.xcidx (i); |
542015fada9e
Eliminate the workspace in sparse transpose.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
1065 retval.xcidx (i) = nz; |
542015fada9e
Eliminate the workspace in sparse transpose.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
1066 nz += tmp; |
5164 | 1067 } |
8987
542015fada9e
Eliminate the workspace in sparse transpose.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
1068 // retval.xcidx[1:nr] holds row entry *start* offsets for rows 0:(nr-1) |
5648 | 1069 |
1070 for (octave_idx_type j = 0; j < nc; j++) | |
1071 for (octave_idx_type k = cidx(j); k < cidx(j+1); k++) | |
1072 { | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1073 octave_idx_type q = retval.xcidx (ridx (k) + 1)++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1074 retval.xridx (q) = j; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1075 retval.xdata (q) = data (k); |
5648 | 1076 } |
8987
542015fada9e
Eliminate the workspace in sparse transpose.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
1077 assert (nnz () == retval.xcidx (nr)); |
542015fada9e
Eliminate the workspace in sparse transpose.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
1078 // retval.xcidx[1:nr] holds row entry *end* offsets for rows 0:(nr-1) |
542015fada9e
Eliminate the workspace in sparse transpose.
Jason Riedy <jason@acm.org>
parents:
8920
diff
changeset
|
1079 // and retval.xcidx[0:(nr-1)] holds their row entry *start* offsets |
5164 | 1080 |
1081 return retval; | |
1082 } | |
1083 | |
1084 template <class T> | |
1085 void | |
1086 Sparse<T>::clear_index (void) | |
1087 { | |
1088 delete [] idx; | |
1089 idx = 0; | |
1090 idx_count = 0; | |
1091 } | |
1092 | |
1093 template <class T> | |
1094 void | |
1095 Sparse<T>::set_index (const idx_vector& idx_arg) | |
1096 { | |
5275 | 1097 octave_idx_type nd = ndims (); |
5164 | 1098 |
1099 if (! idx && nd > 0) | |
1100 idx = new idx_vector [nd]; | |
1101 | |
1102 if (idx_count < nd) | |
1103 { | |
1104 idx[idx_count++] = idx_arg; | |
1105 } | |
1106 else | |
1107 { | |
1108 idx_vector *new_idx = new idx_vector [idx_count+1]; | |
1109 | |
5275 | 1110 for (octave_idx_type i = 0; i < idx_count; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1111 new_idx[i] = idx[i]; |
5164 | 1112 |
1113 new_idx[idx_count++] = idx_arg; | |
1114 | |
1115 delete [] idx; | |
1116 | |
1117 idx = new_idx; | |
1118 } | |
1119 } | |
1120 | |
1121 template <class T> | |
1122 void | |
1123 Sparse<T>::maybe_delete_elements (idx_vector& idx_arg) | |
1124 { | |
5275 | 1125 octave_idx_type nr = dim1 (); |
1126 octave_idx_type nc = dim2 (); | |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1127 octave_idx_type orig_nr = nr; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1128 octave_idx_type orig_nc = nc; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1129 octave_idx_type orig_nnz = nnz (); |
5164 | 1130 |
1131 if (nr == 0 && nc == 0) | |
1132 return; | |
1133 | |
5275 | 1134 octave_idx_type n; |
5164 | 1135 if (nr == 1) |
1136 n = nc; | |
1137 else if (nc == 1) | |
1138 n = nr; | |
1139 else | |
1140 { | |
1141 // Reshape to row vector for Matlab compatibility. | |
1142 | |
1143 n = nr * nc; | |
1144 nr = 1; | |
1145 nc = n; | |
1146 } | |
1147 | |
1148 if (idx_arg.is_colon_equiv (n, 1)) | |
1149 { | |
1150 // Either A(:) = [] or A(idx) = [] with idx enumerating all | |
1151 // elements, so we delete all elements and return [](0x0). To | |
1152 // preserve the orientation of the vector, you have to use | |
1153 // A(idx,:) = [] (delete rows) or A(:,idx) (delete columns). | |
1154 | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1155 resize (0, 0); |
5164 | 1156 return; |
1157 } | |
1158 | |
1159 idx_arg.sort (true); | |
1160 | |
5275 | 1161 octave_idx_type num_to_delete = idx_arg.length (n); |
5164 | 1162 |
1163 if (num_to_delete != 0) | |
1164 { | |
5275 | 1165 octave_idx_type new_n = n; |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1166 octave_idx_type new_nnz = orig_nnz; |
5275 | 1167 |
1168 octave_idx_type iidx = 0; | |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1169 octave_idx_type kk = idx_arg.elem (iidx); |
5164 | 1170 |
1171 const Sparse<T> tmp (*this); | |
1172 | |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1173 if (orig_nr == 1) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1174 { |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1175 for (octave_idx_type i = 0; i < n; i++) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1176 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1177 octave_quit (); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1178 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1179 if (i == kk) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1180 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1181 iidx++; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1182 kk = idx_arg.elem (iidx); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1183 new_n--; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1184 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1185 if (tmp.cidx(i) != tmp.cidx(i + 1)) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1186 new_nnz--; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1187 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1188 if (iidx == num_to_delete) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1189 break; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1190 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1191 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1192 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1193 else if (orig_nc == 1) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1194 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1195 octave_idx_type next_ridx = -1; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1196 octave_idx_type next_ridx_val = -1; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1197 if (orig_nnz > 0) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1198 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1199 next_ridx = 0; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1200 next_ridx_val = tmp.ridx (0); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1201 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1202 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1203 for (octave_idx_type i = 0; i < n; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1204 { |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1205 octave_quit (); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1206 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1207 if (i == kk) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1208 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1209 iidx++; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1210 kk = idx_arg.elem (iidx); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1211 new_n--; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1212 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1213 while (i > next_ridx_val) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1214 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1215 next_ridx++; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1216 if (next_ridx >= orig_nnz) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1217 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1218 next_ridx = -1; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1219 next_ridx_val = n; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1220 break; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1221 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1222 else |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1223 next_ridx_val = tmp.ridx (next_ridx); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1224 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1225 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1226 if (i == next_ridx_val) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1227 new_nnz--; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1228 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1229 if (iidx == num_to_delete) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1230 break; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1231 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1232 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1233 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1234 else |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1235 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1236 for (octave_idx_type i = 0; i < n; i++) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1237 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1238 octave_quit (); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1239 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1240 if (i == kk) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1241 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1242 iidx++; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1243 kk = idx_arg.elem (iidx); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1244 new_n--; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1245 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1246 if (tmp.elem (i) != T ()) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1247 new_nnz--; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1248 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1249 if (iidx == num_to_delete) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1250 break; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1251 } |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1252 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1253 } |
5164 | 1254 |
1255 if (new_n > 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1256 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1257 rep->count--; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1258 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1259 if (nr == 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1260 rep = new typename Sparse<T>::SparseRep (1, new_n, new_nnz); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1261 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1262 rep = new typename Sparse<T>::SparseRep (new_n, 1, new_nnz); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1263 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1264 octave_idx_type ii = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1265 octave_idx_type jj = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1266 iidx = 0; |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1267 kk = idx_arg.elem (iidx); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1268 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1269 if (orig_nr == 1) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1270 { |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1271 for (octave_idx_type i = 0; i < n; i++) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1272 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1273 octave_quit (); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1274 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1275 if (iidx < num_to_delete && i == kk) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1276 kk = idx_arg.elem (++iidx); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1277 else |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1278 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1279 if (tmp.cidx(i) != tmp.cidx(i + 1)) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1280 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1281 data(ii) = tmp.data (tmp.cidx(i)); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1282 ridx(ii++) = jj; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1283 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1284 jj++; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1285 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1286 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1287 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1288 else if (orig_nc == 1) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1289 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1290 octave_idx_type next_ridx = -1; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1291 octave_idx_type next_ridx_val = n; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1292 if (orig_nnz > 0) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1293 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1294 next_ridx = 0; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1295 next_ridx_val = tmp.ridx (0); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1296 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1297 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1298 for (octave_idx_type i = 0; i < n; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1299 { |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1300 octave_quit (); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1301 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1302 if (iidx < num_to_delete && i == kk) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1303 kk = idx_arg.elem (++iidx); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1304 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1305 { |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1306 while (i > next_ridx_val) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1307 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1308 next_ridx++; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1309 if (next_ridx >= orig_nnz) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1310 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1311 next_ridx = -1; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1312 next_ridx_val = n; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1313 break; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1314 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1315 else |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1316 next_ridx_val = tmp.ridx (next_ridx); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1317 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1318 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1319 if (i == next_ridx_val) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1320 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1321 data(ii) = tmp.data(next_ridx); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1322 ridx(ii++) = jj; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1323 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1324 jj++; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1325 } |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1326 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1327 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1328 else |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1329 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1330 for (octave_idx_type i = 0; i < n; i++) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1331 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1332 octave_quit (); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1333 |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1334 if (iidx < num_to_delete && i == kk) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1335 kk = idx_arg.elem (++iidx); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1336 else |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1337 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1338 T el = tmp.elem (i); |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1339 if (el != T ()) |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1340 { |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1341 data(ii) = el; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1342 ridx(ii++) = jj; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1343 } |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1344 jj++; |
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1345 } |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1346 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1347 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1348 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1349 dimensions.resize (2); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1350 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1351 if (nr == 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1352 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1353 ii = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1354 cidx(0) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1355 for (octave_idx_type i = 0; i < new_n; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1356 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1357 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1358 if (ridx(ii) == i) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1359 ridx(ii++) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1360 cidx(i+1) = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1361 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1362 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1363 dimensions(0) = 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1364 dimensions(1) = new_n; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1365 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1366 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1367 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1368 cidx(0) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1369 cidx(1) = new_nnz; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1370 dimensions(0) = new_n; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1371 dimensions(1) = 1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1372 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1373 } |
5164 | 1374 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1375 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1376 ("A(idx) = []: index out of range"); |
5164 | 1377 } |
1378 } | |
1379 | |
1380 template <class T> | |
1381 void | |
1382 Sparse<T>::maybe_delete_elements (idx_vector& idx_i, idx_vector& idx_j) | |
1383 { | |
1384 assert (ndims () == 2); | |
1385 | |
5275 | 1386 octave_idx_type nr = dim1 (); |
1387 octave_idx_type nc = dim2 (); | |
5164 | 1388 |
1389 if (nr == 0 && nc == 0) | |
1390 return; | |
1391 | |
1392 if (idx_i.is_colon ()) | |
1393 { | |
1394 if (idx_j.is_colon ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1395 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1396 // A(:,:) -- We are deleting columns and rows, so the result |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1397 // is [](0x0). |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1398 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1399 resize (0, 0); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1400 return; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1401 } |
5164 | 1402 |
1403 if (idx_j.is_colon_equiv (nc, 1)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1404 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1405 // A(:,j) -- We are deleting columns by enumerating them, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1406 // If we enumerate all of them, we should have zero columns |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1407 // with the same number of rows that we started with. |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1408 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1409 resize (nr, 0); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1410 return; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1411 } |
5164 | 1412 } |
1413 | |
1414 if (idx_j.is_colon () && idx_i.is_colon_equiv (nr, 1)) | |
1415 { | |
1416 // A(i,:) -- We are deleting rows by enumerating them. If we | |
1417 // enumerate all of them, we should have zero rows with the | |
1418 // same number of columns that we started with. | |
1419 | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1420 resize (0, nc); |
5164 | 1421 return; |
1422 } | |
1423 | |
1424 if (idx_i.is_colon_equiv (nr, 1)) | |
1425 { | |
1426 if (idx_j.is_colon_equiv (nc, 1)) | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1427 resize (0, 0); |
5164 | 1428 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1429 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1430 idx_j.sort (true); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1431 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1432 octave_idx_type num_to_delete = idx_j.length (nc); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1433 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1434 if (num_to_delete != 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1435 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1436 if (nr == 1 && num_to_delete == nc) |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1437 resize (0, 0); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1438 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1439 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1440 octave_idx_type new_nc = nc; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1441 octave_idx_type new_nnz = nnz (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1442 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1443 octave_idx_type iidx = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1444 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1445 for (octave_idx_type j = 0; j < nc; j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1446 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1447 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1448 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1449 if (j == idx_j.elem (iidx)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1450 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1451 iidx++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1452 new_nc--; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1453 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1454 new_nnz -= cidx(j+1) - cidx(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1455 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1456 if (iidx == num_to_delete) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1457 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1458 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1459 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1460 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1461 if (new_nc > 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1462 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1463 const Sparse<T> tmp (*this); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1464 --rep->count; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1465 rep = new typename Sparse<T>::SparseRep (nr, new_nc, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1466 new_nnz); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1467 octave_idx_type ii = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1468 octave_idx_type jj = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1469 iidx = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1470 cidx(0) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1471 for (octave_idx_type j = 0; j < nc; j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1472 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1473 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1474 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1475 if (iidx < num_to_delete && j == idx_j.elem (iidx)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1476 iidx++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1477 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1478 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1479 for (octave_idx_type i = tmp.cidx(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1480 i < tmp.cidx(j+1); i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1481 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1482 data(jj) = tmp.data(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1483 ridx(jj++) = tmp.ridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1484 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1485 cidx(++ii) = jj; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1486 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1487 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1488 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1489 dimensions.resize (2); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1490 dimensions(1) = new_nc; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1491 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1492 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1493 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1494 ("A(idx) = []: index out of range"); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1495 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1496 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1497 } |
5164 | 1498 } |
1499 else if (idx_j.is_colon_equiv (nc, 1)) | |
1500 { | |
1501 if (idx_i.is_colon_equiv (nr, 1)) | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1502 resize (0, 0); |
5164 | 1503 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1504 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1505 idx_i.sort (true); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1506 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1507 octave_idx_type num_to_delete = idx_i.length (nr); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1508 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1509 if (num_to_delete != 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1510 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1511 if (nc == 1 && num_to_delete == nr) |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1512 resize (0, 0); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1513 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1514 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1515 octave_idx_type new_nr = nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1516 octave_idx_type new_nnz = nnz (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1517 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1518 octave_idx_type iidx = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1519 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1520 for (octave_idx_type i = 0; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1521 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1522 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1523 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1524 if (i == idx_i.elem (iidx)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1525 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1526 iidx++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1527 new_nr--; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1528 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1529 for (octave_idx_type j = 0; j < nnz (); j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1530 if (ridx(j) == i) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1531 new_nnz--; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1532 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1533 if (iidx == num_to_delete) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1534 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1535 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1536 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1537 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1538 if (new_nr > 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1539 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1540 const Sparse<T> tmp (*this); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1541 --rep->count; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1542 rep = new typename Sparse<T>::SparseRep (new_nr, nc, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1543 new_nnz); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1544 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1545 octave_idx_type jj = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1546 cidx(0) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1547 for (octave_idx_type i = 0; i < nc; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1548 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1549 iidx = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1550 for (octave_idx_type j = tmp.cidx(i); j < tmp.cidx(i+1); j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1551 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1552 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1553 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1554 octave_idx_type ri = tmp.ridx(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1555 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1556 while (iidx < num_to_delete && |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1557 ri > idx_i.elem (iidx)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1558 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1559 iidx++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1560 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1561 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1562 if (iidx == num_to_delete || |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1563 ri != idx_i.elem(iidx)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1564 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1565 data(jj) = tmp.data(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1566 ridx(jj++) = ri - iidx; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1567 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1568 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1569 cidx(i+1) = jj; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1570 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1571 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1572 dimensions.resize (2); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1573 dimensions(0) = new_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1574 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1575 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1576 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1577 ("A(idx) = []: index out of range"); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1578 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1579 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1580 } |
5164 | 1581 } |
1582 } | |
1583 | |
1584 template <class T> | |
1585 void | |
1586 Sparse<T>::maybe_delete_elements (Array<idx_vector>& ra_idx) | |
1587 { | |
1588 if (ra_idx.length () == 1) | |
1589 maybe_delete_elements (ra_idx(0)); | |
1590 else if (ra_idx.length () == 2) | |
1591 maybe_delete_elements (ra_idx(0), ra_idx(1)); | |
1592 else | |
1593 (*current_liboctave_error_handler) | |
1594 ("range error for maybe_delete_elements"); | |
1595 } | |
1596 | |
1597 template <class T> | |
1598 Sparse<T> | |
1599 Sparse<T>::value (void) | |
1600 { | |
1601 Sparse<T> retval; | |
1602 | |
1603 int n_idx = index_count (); | |
1604 | |
1605 if (n_idx == 2) | |
1606 { | |
1607 idx_vector *tmp = get_idx (); | |
1608 | |
1609 idx_vector idx_i = tmp[0]; | |
1610 idx_vector idx_j = tmp[1]; | |
1611 | |
1612 retval = index (idx_i, idx_j); | |
1613 } | |
1614 else if (n_idx == 1) | |
1615 { | |
1616 retval = index (idx[0]); | |
1617 } | |
1618 else | |
1619 (*current_liboctave_error_handler) | |
1620 ("Sparse<T>::value: invalid number of indices specified"); | |
1621 | |
1622 clear_index (); | |
1623 | |
1624 return retval; | |
1625 } | |
1626 | |
1627 template <class T> | |
1628 Sparse<T> | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1629 Sparse<T>::index (const idx_vector& idx, bool resize_ok) const |
5164 | 1630 { |
1631 Sparse<T> retval; | |
1632 | |
1633 assert (ndims () == 2); | |
1634 | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1635 // FIXME: please don't fix the shadowed member warning yet because |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1636 // Sparse<T>::idx will eventually go away. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1637 |
5275 | 1638 octave_idx_type nr = dim1 (); |
1639 octave_idx_type nc = dim2 (); | |
5681 | 1640 octave_idx_type nz = nnz (); |
5275 | 1641 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1642 octave_idx_type nel = numel (); // Can throw. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1643 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1644 const dim_vector idx_dims = idx.orig_dimensions (); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1645 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1646 if (idx_dims.length () > 2) |
5164 | 1647 (*current_liboctave_error_handler) |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
1648 ("cannot index sparse matrix with an N-D Array"); |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1649 else if (idx.is_colon ()) |
5164 | 1650 { |
1651 // Fast magic colon processing. | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1652 retval = Sparse<T> (nel, 1, nz); |
5164 | 1653 |
5275 | 1654 for (octave_idx_type i = 0; i < nc; i++) |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1655 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1656 for (octave_idx_type j = cidx(i); j < cidx(i+1); j++) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1657 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1658 retval.xdata(j) = data(j); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1659 retval.xridx(j) = ridx(j) + i * nr; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1660 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1661 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1662 |
5164 | 1663 retval.xcidx(0) = 0; |
1664 retval.xcidx(1) = nz; | |
1665 } | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1666 else if (idx.extent (nel) > nel) |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
1667 { |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1668 // resize_ok is completely handled here. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1669 if (resize_ok) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1670 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1671 octave_idx_type ext = idx.extent (nel); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1672 Sparse<T> tmp = *this; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1673 tmp.resize1 (ext); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1674 retval = tmp.index (idx); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1675 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1676 else |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1677 gripe_index_out_of_range (1, 1, idx.extent (nel), nel); |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
1678 } |
5164 | 1679 else if (nr == 1 && nc == 1) |
1680 { | |
1681 // You have to be pretty sick to get to this bit of code, | |
1682 // since you have a scalar stored as a sparse matrix, and | |
1683 // then want to make a dense matrix with sparse | |
1684 // representation. Ok, we'll do it, but you deserve what | |
1685 // you get!! | |
10432
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1686 retval = Sparse<T> (idx_dims(0), idx_dims(1), nz ? data(0) : T ()); |
5164 | 1687 } |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1688 else if (nc == 1) |
5164 | 1689 { |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1690 // Sparse column vector. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1691 octave_idx_type lb, ub; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1692 octave_sort<octave_idx_type> isort; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1693 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1694 if (idx.is_scalar ()) |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1695 { |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1696 // Scalar index - just a binary lookup. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1697 octave_idx_type i = isort.lookup (ridx (), nz, idx(0)); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1698 if (i > 0 && ridx(i-1) == idx(0)) |
10432
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1699 retval = Sparse (1, 1, data(i-1)); |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1700 else |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1701 retval = Sparse (1, 1); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1702 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1703 else if (idx.is_cont_range (nel, lb, ub)) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1704 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1705 // Special-case a contiguous range. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1706 // Look-up indices first. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1707 octave_idx_type li = isort.lookup (ridx (), nz, lb); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1708 octave_idx_type ui = isort.lookup (ridx (), nz, ub); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1709 // Adjust to lower bounds. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1710 li -= (li > 0 && ridx(li-1) == lb); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1711 ui -= (ui > 0 && ridx(ui-1) == ub); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1712 // Copy data and adjust indices. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1713 octave_idx_type nz_new = ui - li; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1714 retval = Sparse<T> (ub - lb, 1, nz_new); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1715 copy_or_memcpy (nz_new, data () + li, retval.data ()); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1716 mx_inline_sub (nz_new, retval.xridx (), ridx () + li, lb); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1717 retval.xcidx(1) = nz_new; |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10367
diff
changeset
|
1718 } |
5164 | 1719 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1720 { |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1721 // If indexing a sparse column vector by a vector, the result is a |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1722 // sparse column vector, otherwise it inherits the shape of index. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1723 // Vector transpose is cheap, so do it right here. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1724 const Array<octave_idx_type> idxa = (idx_dims(0) == 1 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1725 ? idx.as_array ().transpose () |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1726 : idx.as_array ()); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1727 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1728 octave_idx_type new_nr = idxa.rows (), new_nc = idxa.cols (); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1729 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1730 // Lookup. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1731 // FIXME: Could specialize for sorted idx? |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1732 NoAlias< Array<octave_idx_type> > lidx (new_nr, new_nc); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1733 isort.lookup (ridx (), nz, idxa.data (), idxa.numel (), lidx.fortran_vec ()); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1734 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1735 // Count matches. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1736 retval = Sparse<T> (idxa.rows (), idxa.cols ()); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1737 for (octave_idx_type j = 0; j < new_nc; j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1738 { |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1739 octave_idx_type nzj = 0; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1740 for (octave_idx_type i = 0; i < new_nr; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1741 { |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1742 octave_idx_type l = lidx(i, j); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1743 if (l > 0 && ridx(l-1) == idxa(i, j)) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1744 nzj++; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1745 else |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1746 lidx(i, j) = 0; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1747 } |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1748 retval.xcidx(j+1) = retval.xcidx(j) + nzj; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1749 } |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1750 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1751 retval.change_capacity (retval.xcidx(new_nc)); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1752 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1753 // Copy data and set row indices. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1754 octave_idx_type k = 0; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1755 for (octave_idx_type j = 0; j < new_nc; j++) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1756 for (octave_idx_type i = 0; i < new_nr; i++) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1757 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1758 octave_idx_type l = lidx(i, j) - 1; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1759 if (l >= 0) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1760 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1761 retval.data(k) = data(l); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1762 retval.xridx(k++) = i; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1763 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1764 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1765 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1766 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1767 else if (nr == 1) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1768 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1769 octave_idx_type lb, ub; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1770 if (idx.is_scalar ()) |
10432
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1771 retval = Sparse<T> (1, 1, elem(0, idx(0))); |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1772 else if (idx.is_cont_range (nel, lb, ub)) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1773 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1774 // Special-case a contiguous range. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1775 octave_idx_type lbi = cidx(lb), ubi = cidx(ub), new_nz = ubi - lbi; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1776 retval = Sparse<T> (1, ub - lb, new_nz); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1777 copy_or_memcpy (new_nz, data () + lbi, retval.data ()); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1778 fill_or_memset (new_nz, static_cast<octave_idx_type> (0), retval.ridx ()); |
10432
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1779 mx_inline_sub (ub - lb + 1, retval.cidx (), cidx () + lb, lbi); |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1780 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1781 else |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1782 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1783 // Sparse row vectors occupy O(nr) storage anyway, so let's just |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1784 // convert the matrix to full, index, and sparsify the result. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1785 retval = Sparse<T> (array_value ().index (idx)); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1786 } |
5164 | 1787 } |
1788 else | |
1789 { | |
7573
755bf7ecc29b
eliminate one_zero stuff from idx_vector
John W. Eaton <jwe@octave.org>
parents:
7546
diff
changeset
|
1790 (*current_liboctave_warning_with_id_handler) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1791 ("Octave:fortran-indexing", "single index used for sparse matrix"); |
5164 | 1792 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1793 if (nr != 0 && idx.is_scalar ()) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1794 retval = Sparse<T> (1, 1, elem (idx(0) % nr, idx(0) / nr)); |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
1795 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1796 { |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1797 // Indexing a non-vector sparse matrix by linear indexing. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1798 // I suppose this is rare (and it may easily overflow), so let's take the easy way, |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1799 // and reshape first to column vector, which is already handled above. |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1800 retval = index (idx_vector::colon).index (idx); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1801 // In this case we're supposed to always inherit the shape, but column(row) doesn't do |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1802 // it, so we'll do it instead. |
10432
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1803 if (idx_dims(0) == 1 && idx_dims(1) != 1) |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1804 retval = retval.transpose (); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1805 } |
5164 | 1806 } |
1807 | |
1808 return retval; | |
1809 } | |
1810 | |
6988 | 1811 struct |
1812 idx_node | |
1813 { | |
1814 octave_idx_type i; | |
1815 struct idx_node *next; | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1816 }; |
6988 | 1817 |
5164 | 1818 template <class T> |
1819 Sparse<T> | |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
1820 Sparse<T>::index (const idx_vector& idx_i, const idx_vector& idx_j, bool resize_ok) const |
5164 | 1821 { |
1822 Sparse<T> retval; | |
1823 | |
1824 assert (ndims () == 2); | |
1825 | |
5275 | 1826 octave_idx_type nr = dim1 (); |
1827 octave_idx_type nc = dim2 (); | |
1828 | |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
1829 octave_idx_type n = idx_i.length (nr); |
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
1830 octave_idx_type m = idx_j.length (nc); |
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
1831 |
10432
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1832 if (idx_i.extent (nr) > nr || idx_j.extent (nc) > nc) |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1833 { |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1834 // resize_ok is completely handled here. |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1835 if (resize_ok) |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1836 { |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1837 octave_idx_type ext_i = idx_i.extent (nr); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1838 octave_idx_type ext_j = idx_j.extent (nc); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1839 Sparse<T> tmp = *this; |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1840 tmp.resize (ext_i, ext_j); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1841 retval = tmp.index (idx_i, idx_j); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1842 } |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1843 else if (idx_i.extent (nr) > nr) |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1844 gripe_index_out_of_range (2, 1, idx_i.extent (nr), nr); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1845 else |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1846 gripe_index_out_of_range (2, 2, idx_j.extent (nc), nc); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1847 } |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1848 else if (idx_i.is_colon ()) |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1849 { |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1850 octave_idx_type lb, ub; |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1851 // Great, we're just manipulating columns. This is going to be quite |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1852 // efficient, because the columns can stay compressed as they are. |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1853 if (idx_j.is_colon ()) |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1854 retval = *this; // Shallow copy. |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1855 else if (idx_j.is_cont_range (nc, lb, ub)) |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1856 { |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1857 // Special-case a contiguous range. |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1858 octave_idx_type lbi = cidx(lb), ubi = cidx(ub), new_nz = ubi - lbi; |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1859 retval = Sparse<T> (nr, ub - lb, new_nz); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1860 copy_or_memcpy (new_nz, data () + lbi, retval.data ()); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1861 copy_or_memcpy (new_nz, ridx () + lbi, retval.ridx ()); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1862 mx_inline_sub (ub - lb + 1, retval.cidx (), cidx () + lb, lbi); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1863 } |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1864 else |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1865 { |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1866 // Count new nonzero elements. |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1867 retval = Sparse<T> (nr, m); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1868 for (octave_idx_type j = 0; j < m; j++) |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1869 { |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1870 octave_idx_type jj = idx_j(j); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1871 retval.xcidx(j+1) = retval.xcidx(j) + (cidx(jj+1) - cidx(jj)); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1872 } |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1873 |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1874 retval.change_capacity (retval.xcidx (m)); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1875 |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1876 // Copy data & indices. |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1877 for (octave_idx_type j = 0; j < m; j++) |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1878 { |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1879 octave_idx_type ljj = cidx(idx_j(j)); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1880 octave_idx_type lj = retval.xcidx(j), nzj = retval.xcidx(j+1) - lj; |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1881 copy_or_memcpy (nzj, data () + ljj, retval.data () + lj); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1882 copy_or_memcpy (nzj, ridx () + ljj, retval.ridx () + lj); |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1883 } |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1884 } |
10207338603a
improve sparse 2D indexing (part 1)
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
1885 } |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
1886 else |
5164 | 1887 { |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
1888 if (n == 0 || m == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1889 { |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
1890 retval.resize (n, m); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1891 } |
5681 | 1892 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1893 { |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
1894 bool idx_i_colon = idx_i.is_colon_equiv (nr); |
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
1895 bool idx_j_colon = idx_j.is_colon_equiv (nc); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1896 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1897 if (idx_i_colon && idx_j_colon) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1898 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1899 retval = *this; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1900 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1901 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1902 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1903 // Identify if the indices have any repeated values |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1904 bool permutation = true; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1905 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1906 OCTAVE_LOCAL_BUFFER (octave_idx_type, itmp, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1907 (nr > nc ? nr : nc)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1908 octave_sort<octave_idx_type> lsort; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1909 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1910 if (n > nr || m > nc) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1911 permutation = false; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1912 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1913 if (permutation && ! idx_i_colon) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1914 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1915 // Can't use something like |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1916 // idx_vector tmp_idx = idx_i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1917 // tmp_idx.sort (true); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1918 // if (tmp_idx.length(nr) != n) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1919 // permutation = false; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1920 // here as there is no make_unique function |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1921 // for idx_vector type. |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1922 for (octave_idx_type i = 0; i < n; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1923 itmp [i] = idx_i.elem (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1924 lsort.sort (itmp, n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1925 for (octave_idx_type i = 1; i < n; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1926 if (itmp[i-1] == itmp[i]) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1927 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1928 permutation = false; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1929 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1930 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1931 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1932 if (permutation && ! idx_j_colon) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1933 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1934 for (octave_idx_type i = 0; i < m; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1935 itmp [i] = idx_j.elem (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1936 lsort.sort (itmp, m); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1937 for (octave_idx_type i = 1; i < m; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1938 if (itmp[i-1] == itmp[i]) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1939 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1940 permutation = false; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1941 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1942 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1943 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1944 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1945 if (permutation) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1946 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1947 // Special case permutation like indexing for speed |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1948 retval = Sparse<T> (n, m, nnz ()); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1949 octave_idx_type *ri = retval.xridx (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1950 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1951 std::vector<T> X (n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1952 for (octave_idx_type i = 0; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1953 itmp [i] = -1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1954 for (octave_idx_type i = 0; i < n; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1955 itmp[idx_i.elem(i)] = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1956 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1957 octave_idx_type kk = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1958 retval.xcidx(0) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1959 for (octave_idx_type j = 0; j < m; j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1960 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1961 octave_idx_type jj = idx_j.elem (j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1962 for (octave_idx_type i = cidx(jj); i < cidx(jj+1); i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1963 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1964 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1965 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1966 octave_idx_type ii = itmp [ridx(i)]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1967 if (ii >= 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1968 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1969 X [ii] = data (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1970 retval.xridx (kk++) = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1971 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1972 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1973 lsort.sort (ri + retval.xcidx (j), kk - retval.xcidx (j)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1974 for (octave_idx_type p = retval.xcidx (j); p < kk; p++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1975 retval.xdata (p) = X [retval.xridx (p)]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1976 retval.xcidx(j+1) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1977 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1978 retval.maybe_compress (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1979 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1980 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1981 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1982 OCTAVE_LOCAL_BUFFER (struct idx_node, nodes, n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1983 OCTAVE_LOCAL_BUFFER (octave_idx_type, start_nodes, nr); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1984 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1985 for (octave_idx_type i = 0; i < nr; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1986 start_nodes[i] = -1; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1987 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1988 for (octave_idx_type i = 0; i < n; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1989 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1990 octave_idx_type ii = idx_i.elem (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1991 nodes[i].i = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1992 nodes[i].next = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1993 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1994 octave_idx_type node = start_nodes[ii]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1995 if (node == -1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1996 start_nodes[ii] = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1997 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1998 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
1999 while (nodes[node].next) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2000 node = nodes[node].next->i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2001 nodes[node].next = nodes + i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2002 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2003 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2004 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2005 // First count the number of non-zero elements |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2006 octave_idx_type new_nzmx = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2007 for (octave_idx_type j = 0; j < m; j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2008 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2009 octave_idx_type jj = idx_j.elem (j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2010 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2011 if (jj < nc) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2012 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2013 for (octave_idx_type i = cidx(jj); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2014 i < cidx(jj+1); i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2015 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2016 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2017 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2018 octave_idx_type ii = start_nodes [ridx(i)]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2019 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2020 if (ii >= 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2021 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2022 struct idx_node inode = nodes[ii]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2023 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2024 while (true) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2025 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2026 if (idx_i.elem (inode.i) < nr) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2027 new_nzmx ++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2028 if (inode.next == 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2029 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2030 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2031 inode = *inode.next; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2032 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2033 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2034 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2035 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2036 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2037 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2038 std::vector<T> X (n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2039 retval = Sparse<T> (n, m, new_nzmx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2040 octave_idx_type *ri = retval.xridx (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2041 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2042 octave_idx_type kk = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2043 retval.xcidx(0) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2044 for (octave_idx_type j = 0; j < m; j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2045 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2046 octave_idx_type jj = idx_j.elem (j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2047 if (jj < nc) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2048 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2049 for (octave_idx_type i = cidx(jj); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2050 i < cidx(jj+1); i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2051 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2052 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2053 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2054 octave_idx_type ii = start_nodes [ridx(i)]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2055 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2056 if (ii >= 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2057 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2058 struct idx_node inode = nodes[ii]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2059 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2060 while (true) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2061 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2062 if (idx_i.elem (inode.i) < nr) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2063 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2064 X [inode.i] = data (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2065 retval.xridx (kk++) = inode.i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2066 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2067 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2068 if (inode.next == 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2069 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2070 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2071 inode = *inode.next; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2072 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2073 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2074 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2075 lsort.sort (ri + retval.xcidx (j), |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2076 kk - retval.xcidx (j)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2077 for (octave_idx_type p = retval.xcidx (j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2078 p < kk; p++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2079 retval.xdata (p) = X [retval.xridx (p)]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2080 retval.xcidx(j+1) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2081 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2082 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2083 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2084 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2085 } |
5164 | 2086 } |
2087 | |
2088 return retval; | |
2089 } | |
2090 | |
7433 | 2091 // Can't use versions of these in Array.cc due to duplication of the |
2092 // instantiations for Array<double and Sparse<double>, etc | |
2093 template <class T> | |
2094 bool | |
8752
06b9903a029b
fix & clean up complex & sparse sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8677
diff
changeset
|
2095 sparse_ascending_compare (typename ref_param<T>::type a, |
06b9903a029b
fix & clean up complex & sparse sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8677
diff
changeset
|
2096 typename ref_param<T>::type b) |
7433 | 2097 { |
2098 return (a < b); | |
2099 } | |
2100 | |
2101 template <class T> | |
2102 bool | |
8752
06b9903a029b
fix & clean up complex & sparse sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8677
diff
changeset
|
2103 sparse_descending_compare (typename ref_param<T>::type a, |
06b9903a029b
fix & clean up complex & sparse sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8677
diff
changeset
|
2104 typename ref_param<T>::type b) |
7433 | 2105 { |
2106 return (a > b); | |
2107 } | |
2108 | |
2109 template <class T> | |
2110 Sparse<T> | |
2111 Sparse<T>::sort (octave_idx_type dim, sortmode mode) const | |
2112 { | |
2113 Sparse<T> m = *this; | |
2114 | |
2115 octave_idx_type nr = m.rows (); | |
2116 octave_idx_type nc = m.columns (); | |
2117 | |
2118 if (m.length () < 1) | |
2119 return m; | |
2120 | |
2121 if (dim > 0) | |
2122 { | |
2123 m = m.transpose (); | |
2124 nr = m.rows (); | |
2125 nc = m.columns (); | |
2126 } | |
2127 | |
2128 octave_sort<T> lsort; | |
2129 | |
2130 if (mode == ASCENDING) | |
8752
06b9903a029b
fix & clean up complex & sparse sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8677
diff
changeset
|
2131 lsort.set_compare (sparse_ascending_compare<T>); |
7433 | 2132 else if (mode == DESCENDING) |
8752
06b9903a029b
fix & clean up complex & sparse sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8677
diff
changeset
|
2133 lsort.set_compare (sparse_descending_compare<T>); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
2134 else |
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
2135 abort (); |
7433 | 2136 |
2137 T *v = m.data (); | |
2138 octave_idx_type *mcidx = m.cidx (); | |
2139 octave_idx_type *mridx = m.ridx (); | |
2140 | |
2141 for (octave_idx_type j = 0; j < nc; j++) | |
2142 { | |
2143 octave_idx_type ns = mcidx [j + 1] - mcidx [j]; | |
2144 lsort.sort (v, ns); | |
2145 | |
2146 octave_idx_type i; | |
2147 if (mode == ASCENDING) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2148 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2149 for (i = 0; i < ns; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2150 if (sparse_ascending_compare<T> (static_cast<T> (0), v [i])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2151 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2152 } |
7433 | 2153 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2154 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2155 for (i = 0; i < ns; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2156 if (sparse_descending_compare<T> (static_cast<T> (0), v [i])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2157 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2158 } |
7433 | 2159 for (octave_idx_type k = 0; k < i; k++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2160 mridx [k] = k; |
7433 | 2161 for (octave_idx_type k = i; k < ns; k++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2162 mridx [k] = k - ns + nr; |
7433 | 2163 |
2164 v += ns; | |
2165 mridx += ns; | |
2166 } | |
2167 | |
2168 if (dim > 0) | |
2169 m = m.transpose (); | |
2170 | |
2171 return m; | |
2172 } | |
2173 | |
2174 template <class T> | |
2175 Sparse<T> | |
2176 Sparse<T>::sort (Array<octave_idx_type> &sidx, octave_idx_type dim, | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2177 sortmode mode) const |
7433 | 2178 { |
2179 Sparse<T> m = *this; | |
2180 | |
2181 octave_idx_type nr = m.rows (); | |
2182 octave_idx_type nc = m.columns (); | |
2183 | |
2184 if (m.length () < 1) | |
2185 { | |
2186 sidx = Array<octave_idx_type> (dim_vector (nr, nc)); | |
2187 return m; | |
2188 } | |
2189 | |
2190 if (dim > 0) | |
2191 { | |
2192 m = m.transpose (); | |
2193 nr = m.rows (); | |
2194 nc = m.columns (); | |
2195 } | |
2196 | |
8752
06b9903a029b
fix & clean up complex & sparse sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8677
diff
changeset
|
2197 octave_sort<T> indexed_sort; |
7433 | 2198 |
2199 if (mode == ASCENDING) | |
8752
06b9903a029b
fix & clean up complex & sparse sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8677
diff
changeset
|
2200 indexed_sort.set_compare (sparse_ascending_compare<T>); |
7433 | 2201 else if (mode == DESCENDING) |
8752
06b9903a029b
fix & clean up complex & sparse sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8677
diff
changeset
|
2202 indexed_sort.set_compare (sparse_descending_compare<T>); |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
2203 else |
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
2204 abort (); |
7433 | 2205 |
2206 T *v = m.data (); | |
2207 octave_idx_type *mcidx = m.cidx (); | |
2208 octave_idx_type *mridx = m.ridx (); | |
2209 | |
2210 sidx = Array<octave_idx_type> (dim_vector (nr, nc)); | |
8752
06b9903a029b
fix & clean up complex & sparse sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8677
diff
changeset
|
2211 OCTAVE_LOCAL_BUFFER (octave_idx_type, vi, nr); |
7433 | 2212 |
2213 for (octave_idx_type j = 0; j < nc; j++) | |
2214 { | |
2215 octave_idx_type ns = mcidx [j + 1] - mcidx [j]; | |
2216 octave_idx_type offset = j * nr; | |
2217 | |
2218 if (ns == 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2219 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2220 for (octave_idx_type k = 0; k < nr; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2221 sidx (offset + k) = k; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2222 } |
7433 | 2223 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2224 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2225 for (octave_idx_type i = 0; i < ns; i++) |
8752
06b9903a029b
fix & clean up complex & sparse sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8677
diff
changeset
|
2226 vi[i] = mridx[i]; |
06b9903a029b
fix & clean up complex & sparse sorting
Jaroslav Hajek <highegg@gmail.com>
parents:
8677
diff
changeset
|
2227 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2228 indexed_sort.sort (v, vi, ns); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2229 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2230 octave_idx_type i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2231 if (mode == ASCENDING) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2232 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2233 for (i = 0; i < ns; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2234 if (sparse_ascending_compare<T> (static_cast<T> (0), v[i])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2235 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2236 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2237 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2238 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2239 for (i = 0; i < ns; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2240 if (sparse_descending_compare<T> (static_cast<T> (0), v[i])) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2241 break; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2242 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2243 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2244 octave_idx_type ii = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2245 octave_idx_type jj = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2246 for (octave_idx_type k = 0; k < nr; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2247 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2248 if (ii < ns && mridx[ii] == k) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2249 ii++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2250 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2251 sidx (offset + jj++) = k; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2252 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2253 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2254 for (octave_idx_type k = 0; k < i; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2255 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2256 sidx (k + offset) = vi [k]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2257 mridx [k] = k; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2258 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2259 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2260 for (octave_idx_type k = i; k < ns; k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2261 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2262 sidx (k - ns + nr + offset) = vi [k]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2263 mridx [k] = k - ns + nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2264 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2265 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2266 v += ns; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2267 mridx += ns; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2268 } |
7433 | 2269 } |
2270 | |
2271 if (dim > 0) | |
2272 { | |
2273 m = m.transpose (); | |
2274 sidx = sidx.transpose (); | |
2275 } | |
2276 | |
2277 return m; | |
2278 } | |
2279 | |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2280 template <class T> |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2281 Sparse<T> |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2282 Sparse<T>::diag (octave_idx_type k) const |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2283 { |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2284 octave_idx_type nnr = rows (); |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2285 octave_idx_type nnc = cols (); |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2286 Sparse<T> d; |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2287 |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2288 if (nnr == 0 || nnc == 0) |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2289 ; // do nothing |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2290 else if (nnr != 1 && nnc != 1) |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2291 { |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2292 if (k > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2293 nnc -= k; |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2294 else if (k < 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2295 nnr += k; |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2296 |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2297 if (nnr > 0 && nnc > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2298 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2299 octave_idx_type ndiag = (nnr < nnc) ? nnr : nnc; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2300 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2301 // Count the number of non-zero elements |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2302 octave_idx_type nel = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2303 if (k > 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2304 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2305 for (octave_idx_type i = 0; i < ndiag; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2306 if (elem (i, i+k) != 0.) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2307 nel++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2308 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2309 else if ( k < 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2310 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2311 for (octave_idx_type i = 0; i < ndiag; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2312 if (elem (i-k, i) != 0.) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2313 nel++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2314 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2315 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2316 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2317 for (octave_idx_type i = 0; i < ndiag; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2318 if (elem (i, i) != 0.) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2319 nel++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2320 } |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2321 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2322 d = Sparse<T> (ndiag, 1, nel); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2323 d.xcidx (0) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2324 d.xcidx (1) = nel; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2325 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2326 octave_idx_type ii = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2327 if (k > 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2328 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2329 for (octave_idx_type i = 0; i < ndiag; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2330 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2331 T tmp = elem (i, i+k); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2332 if (tmp != 0.) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2333 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2334 d.xdata (ii) = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2335 d.xridx (ii++) = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2336 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2337 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2338 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2339 else if ( k < 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2340 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2341 for (octave_idx_type i = 0; i < ndiag; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2342 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2343 T tmp = elem (i-k, i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2344 if (tmp != 0.) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2345 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2346 d.xdata (ii) = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2347 d.xridx (ii++) = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2348 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2349 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2350 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2351 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2352 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2353 for (octave_idx_type i = 0; i < ndiag; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2354 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2355 T tmp = elem (i, i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2356 if (tmp != 0.) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2357 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2358 d.xdata (ii) = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2359 d.xridx (ii++) = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2360 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2361 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2362 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2363 } |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2364 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2365 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2366 ("diag: requested diagonal out of range"); |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2367 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2368 else if (nnr != 0 && nnc != 0) |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2369 { |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2370 octave_idx_type roff = 0; |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2371 octave_idx_type coff = 0; |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2372 if (k > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2373 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2374 roff = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2375 coff = k; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2376 } |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2377 else if (k < 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2378 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2379 roff = -k; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2380 coff = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2381 } |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2382 |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2383 if (nnr == 1) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2384 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2385 octave_idx_type n = nnc + std::abs (k); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2386 octave_idx_type nz = nzmax (); |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2387 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2388 d = Sparse<T> (n, n, nz); |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2389 |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2390 if (nnz () > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2391 { |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2392 for (octave_idx_type i = 0; i < coff+1; i++) |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2393 d.xcidx (i) = 0; |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2394 |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2395 for (octave_idx_type j = 0; j < nnc; j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2396 { |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2397 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2398 { |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2399 d.xdata (i) = data (i); |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2400 d.xridx (i) = j + roff; |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2401 } |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2402 d.xcidx (j + coff + 1) = cidx(j+1); |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2403 } |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2404 |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2405 for (octave_idx_type i = nnc + coff + 1; i < n + 1; i++) |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2406 d.xcidx (i) = nz; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2407 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2408 } |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2409 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2410 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2411 octave_idx_type n = nnr + std::abs (k); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2412 octave_idx_type nz = nzmax (); |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2413 |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2414 d = Sparse<T> (n, n, nz); |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2415 |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2416 if (nnz () > 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2417 { |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2418 octave_idx_type ii = 0; |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2419 octave_idx_type ir = ridx(0); |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2420 |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2421 for (octave_idx_type i = 0; i < coff+1; i++) |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2422 d.xcidx (i) = 0; |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2423 |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2424 for (octave_idx_type i = 0; i < nnr; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2425 { |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2426 if (ir == i) |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2427 { |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2428 d.xdata (ii) = data (ii); |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2429 d.xridx (ii++) = ir + roff; |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2430 |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2431 if (ii != nz) |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2432 ir = ridx (ii); |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2433 } |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2434 d.xcidx (i + coff + 1) = ii; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2435 } |
10367
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2436 |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2437 for (octave_idx_type i = nnr + coff + 1; i < n+1; i++) |
173e10268080
avoid indexing nonexistent elements in sparse diag
John W. Eaton <jwe@octave.org>
parents:
10352
diff
changeset
|
2438 d.xcidx (i) = nz; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2439 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2440 } |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2441 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2442 |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2443 return d; |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2444 } |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7573
diff
changeset
|
2445 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2446 template <class T> |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2447 Array<T> |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2448 Sparse<T>::array_value () const |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2449 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2450 NoAlias< Array<T> > retval (dims (), T()); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2451 if (rows () == 1) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2452 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2453 octave_idx_type i = 0; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2454 for (octave_idx_type j = 0, nc = cols (); j < nc; j++) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2455 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2456 if (cidx(j+1) > i) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2457 retval(j) = data (i++); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2458 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2459 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2460 else |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2461 { |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2462 for (octave_idx_type j = 0, nc = cols (); j < nc; j++) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2463 for (octave_idx_type i = cidx(j), iu = cidx(j+1); i < iu; i++) |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2464 retval (ridx(i), j) = data (i); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2465 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2466 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2467 return retval; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2468 } |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
2469 |
5775 | 2470 // FIXME |
5164 | 2471 // Unfortunately numel can overflow for very large but very sparse matrices. |
2472 // For now just flag an error when this happens. | |
2473 template <class LT, class RT> | |
2474 int | |
2475 assign1 (Sparse<LT>& lhs, const Sparse<RT>& rhs) | |
2476 { | |
2477 int retval = 1; | |
2478 | |
2479 idx_vector *idx_tmp = lhs.get_idx (); | |
2480 | |
2481 idx_vector lhs_idx = idx_tmp[0]; | |
2482 | |
5275 | 2483 octave_idx_type lhs_len = lhs.numel (); |
2484 octave_idx_type rhs_len = rhs.numel (); | |
5164 | 2485 |
5828 | 2486 uint64_t long_lhs_len = |
2487 static_cast<uint64_t> (lhs.rows ()) * | |
2488 static_cast<uint64_t> (lhs.cols ()); | |
2489 | |
2490 uint64_t long_rhs_len = | |
2491 static_cast<uint64_t> (rhs.rows ()) * | |
2492 static_cast<uint64_t> (rhs.cols ()); | |
2493 | |
2494 if (long_rhs_len != static_cast<uint64_t>(rhs_len) || | |
2495 long_lhs_len != static_cast<uint64_t>(lhs_len)) | |
5164 | 2496 { |
2497 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2498 ("A(I) = X: Matrix dimensions too large to ensure correct\n", |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2499 "operation. This is an limitation that should be removed\n", |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2500 "in the future."); |
5164 | 2501 |
2502 lhs.clear_index (); | |
2503 return 0; | |
2504 } | |
2505 | |
5275 | 2506 octave_idx_type nr = lhs.rows (); |
2507 octave_idx_type nc = lhs.cols (); | |
5681 | 2508 octave_idx_type nz = lhs.nnz (); |
5275 | 2509 |
5781 | 2510 octave_idx_type n = lhs_idx.freeze (lhs_len, "vector", true); |
5164 | 2511 |
2512 if (n != 0) | |
2513 { | |
5275 | 2514 octave_idx_type max_idx = lhs_idx.max () + 1; |
5164 | 2515 max_idx = max_idx < lhs_len ? lhs_len : max_idx; |
2516 | |
2517 // Take a constant copy of lhs. This means that elem won't | |
2518 // create missing elements. | |
2519 const Sparse<LT> c_lhs (lhs); | |
2520 | |
2521 if (rhs_len == n) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2522 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2523 octave_idx_type new_nzmx = lhs.nnz (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2524 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2525 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx, n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2526 if (! lhs_idx.is_colon ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2527 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2528 // Ok here we have to be careful with the indexing, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2529 // to treat cases like "a([3,2,1]) = b", and still |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2530 // handle the need for strict sorting of the sparse |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2531 // elements. |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2532 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, sidx, n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2533 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, sidxX, n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2534 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2535 for (octave_idx_type i = 0; i < n; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2536 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2537 sidx[i] = &sidxX[i]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2538 sidx[i]->i = lhs_idx.elem(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2539 sidx[i]->idx = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2540 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2541 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2542 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2543 octave_sort<octave_idx_vector_sort *> |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2544 sort (octave_idx_vector_comp); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2545 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2546 sort.sort (sidx, n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2547 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2548 intNDArray<octave_idx_type> new_idx (dim_vector (n,1)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2549 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2550 for (octave_idx_type i = 0; i < n; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2551 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2552 new_idx.xelem(i) = sidx[i]->i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2553 rhs_idx[i] = sidx[i]->idx; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2554 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2555 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2556 lhs_idx = idx_vector (new_idx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2557 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2558 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2559 for (octave_idx_type i = 0; i < n; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2560 rhs_idx[i] = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2561 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2562 // First count the number of non-zero elements |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2563 for (octave_idx_type i = 0; i < n; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2564 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2565 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2566 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2567 octave_idx_type ii = lhs_idx.elem (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2568 if (i < n - 1 && lhs_idx.elem (i + 1) == ii) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2569 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2570 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2571 new_nzmx--; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2572 if (rhs.elem(rhs_idx[i]) != RT ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2573 new_nzmx++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2574 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2575 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2576 if (nr > 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2577 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2578 Sparse<LT> tmp ((max_idx > nr ? max_idx : nr), 1, new_nzmx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2579 tmp.cidx(0) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2580 tmp.cidx(1) = new_nzmx; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2581 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2582 octave_idx_type i = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2583 octave_idx_type ii = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2584 if (i < nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2585 ii = c_lhs.ridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2586 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2587 octave_idx_type j = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2588 octave_idx_type jj = lhs_idx.elem(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2589 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2590 octave_idx_type kk = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2591 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2592 while (j < n || i < nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2593 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2594 if (j < n - 1 && lhs_idx.elem (j + 1) == jj) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2595 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2596 j++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2597 jj = lhs_idx.elem (j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2598 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2599 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2600 if (j == n || (i < nz && ii < jj)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2601 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2602 tmp.xdata (kk) = c_lhs.data (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2603 tmp.xridx (kk++) = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2604 if (++i < nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2605 ii = c_lhs.ridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2606 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2607 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2608 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2609 RT rtmp = rhs.elem (rhs_idx[j]); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2610 if (rtmp != RT ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2611 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2612 tmp.xdata (kk) = rtmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2613 tmp.xridx (kk++) = jj; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2614 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2615 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2616 if (ii == jj && i < nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2617 if (++i < nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2618 ii = c_lhs.ridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2619 if (++j < n) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2620 jj = lhs_idx.elem(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2621 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2622 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2623 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2624 lhs = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2625 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2626 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2627 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2628 Sparse<LT> tmp (1, (max_idx > nc ? max_idx : nc), new_nzmx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2629 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2630 octave_idx_type i = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2631 octave_idx_type ii = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2632 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2633 ii++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2634 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2635 octave_idx_type j = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2636 octave_idx_type jj = lhs_idx.elem(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2637 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2638 octave_idx_type kk = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2639 octave_idx_type ic = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2640 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2641 while (j < n || i < nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2642 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2643 if (j < n - 1 && lhs_idx.elem (j + 1) == jj) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2644 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2645 j++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2646 jj = lhs_idx.elem (j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2647 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2648 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2649 if (j == n || (i < nz && ii < jj)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2650 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2651 while (ic <= ii) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2652 tmp.xcidx (ic++) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2653 tmp.xdata (kk) = c_lhs.data (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2654 tmp.xridx (kk++) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2655 i++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2656 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2657 ii++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2658 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2659 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2660 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2661 while (ic <= jj) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2662 tmp.xcidx (ic++) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2663 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2664 RT rtmp = rhs.elem (rhs_idx[j]); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2665 if (rtmp != RT ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2666 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2667 tmp.xdata (kk) = rtmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2668 tmp.xridx (kk++) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2669 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2670 if (ii == jj) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2671 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2672 i++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2673 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2674 ii++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2675 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2676 j++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2677 if (j < n) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2678 jj = lhs_idx.elem(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2679 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2680 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2681 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2682 for (octave_idx_type iidx = ic; iidx < max_idx+1; iidx++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2683 tmp.xcidx(iidx) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2684 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2685 lhs = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2686 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2687 } |
5164 | 2688 else if (rhs_len == 1) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2689 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2690 octave_idx_type new_nzmx = lhs.nnz (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2691 RT scalar = rhs.elem (0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2692 bool scalar_non_zero = (scalar != RT ()); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2693 lhs_idx.sort (true); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2694 n = lhs_idx.length (n); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2695 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2696 // First count the number of non-zero elements |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2697 if (scalar != RT ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2698 new_nzmx += n; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2699 for (octave_idx_type i = 0; i < n; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2700 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2701 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2702 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2703 octave_idx_type ii = lhs_idx.elem (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2704 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2705 new_nzmx--; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2706 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2707 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2708 if (nr > 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2709 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2710 Sparse<LT> tmp ((max_idx > nr ? max_idx : nr), 1, new_nzmx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2711 tmp.cidx(0) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2712 tmp.cidx(1) = new_nzmx; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2713 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2714 octave_idx_type i = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2715 octave_idx_type ii = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2716 if (i < nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2717 ii = c_lhs.ridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2718 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2719 octave_idx_type j = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2720 octave_idx_type jj = lhs_idx.elem(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2721 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2722 octave_idx_type kk = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2723 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2724 while (j < n || i < nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2725 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2726 if (j == n || (i < nz && ii < jj)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2727 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2728 tmp.xdata (kk) = c_lhs.data (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2729 tmp.xridx (kk++) = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2730 if (++i < nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2731 ii = c_lhs.ridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2732 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2733 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2734 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2735 if (scalar_non_zero) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2736 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2737 tmp.xdata (kk) = scalar; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2738 tmp.xridx (kk++) = jj; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2739 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2740 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2741 if (ii == jj && i < nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2742 if (++i < nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2743 ii = c_lhs.ridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2744 if (++j < n) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2745 jj = lhs_idx.elem(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2746 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2747 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2748 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2749 lhs = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2750 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2751 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2752 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2753 Sparse<LT> tmp (1, (max_idx > nc ? max_idx : nc), new_nzmx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2754 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2755 octave_idx_type i = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2756 octave_idx_type ii = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2757 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2758 ii++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2759 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2760 octave_idx_type j = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2761 octave_idx_type jj = lhs_idx.elem(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2762 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2763 octave_idx_type kk = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2764 octave_idx_type ic = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2765 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2766 while (j < n || i < nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2767 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2768 if (j == n || (i < nz && ii < jj)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2769 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2770 while (ic <= ii) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2771 tmp.xcidx (ic++) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2772 tmp.xdata (kk) = c_lhs.data (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2773 i++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2774 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2775 ii++; |
9177
39be2c4531c8
fix sparse indexing bug
Jaroslav Hajek <highegg@gmail.com>
parents:
8987
diff
changeset
|
2776 tmp.xridx (kk++) = 0; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2777 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2778 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2779 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2780 while (ic <= jj) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2781 tmp.xcidx (ic++) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2782 if (scalar_non_zero) |
9177
39be2c4531c8
fix sparse indexing bug
Jaroslav Hajek <highegg@gmail.com>
parents:
8987
diff
changeset
|
2783 { |
39be2c4531c8
fix sparse indexing bug
Jaroslav Hajek <highegg@gmail.com>
parents:
8987
diff
changeset
|
2784 tmp.xdata (kk) = scalar; |
39be2c4531c8
fix sparse indexing bug
Jaroslav Hajek <highegg@gmail.com>
parents:
8987
diff
changeset
|
2785 tmp.xridx (kk++) = 0; |
39be2c4531c8
fix sparse indexing bug
Jaroslav Hajek <highegg@gmail.com>
parents:
8987
diff
changeset
|
2786 } |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2787 if (ii == jj) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2788 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2789 i++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2790 while (ii < nc && c_lhs.cidx(ii+1) <= i) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2791 ii++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2792 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2793 j++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2794 if (j < n) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2795 jj = lhs_idx.elem(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2796 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2797 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2798 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2799 for (octave_idx_type iidx = ic; iidx < max_idx+1; iidx++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2800 tmp.xcidx(iidx) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2801 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2802 lhs = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2803 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2804 } |
5164 | 2805 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2806 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2807 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2808 ("A(I) = X: X must be a scalar or a vector with same length as I"); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2809 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2810 retval = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2811 } |
5164 | 2812 } |
2813 else if (lhs_idx.is_colon ()) | |
2814 { | |
2815 if (lhs_len == 0) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2816 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2817 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2818 octave_idx_type new_nzmx = rhs.nnz (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2819 Sparse<LT> tmp (1, rhs_len, new_nzmx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2820 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2821 octave_idx_type ii = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2822 octave_idx_type jj = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2823 for (octave_idx_type i = 0; i < rhs.cols(); i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2824 for (octave_idx_type j = rhs.cidx(i); j < rhs.cidx(i+1); j++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2825 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2826 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2827 for (octave_idx_type k = jj; k <= i * rhs.rows() + rhs.ridx(j); k++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2828 tmp.cidx(jj++) = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2829 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2830 tmp.data(ii) = rhs.data(j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2831 tmp.ridx(ii++) = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2832 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2833 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2834 for (octave_idx_type i = jj; i < rhs_len + 1; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2835 tmp.cidx(i) = ii; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2836 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2837 lhs = tmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2838 } |
5164 | 2839 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2840 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2841 ("A(:) = X: A must be the same size as X"); |
5164 | 2842 } |
2843 else if (! (rhs_len == 1 || rhs_len == 0)) | |
2844 { | |
2845 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2846 ("A([]) = X: X must also be an empty matrix or a scalar"); |
5164 | 2847 |
2848 retval = 0; | |
2849 } | |
2850 | |
2851 lhs.clear_index (); | |
2852 | |
2853 return retval; | |
2854 } | |
2855 | |
2856 template <class LT, class RT> | |
2857 int | |
2858 assign (Sparse<LT>& lhs, const Sparse<RT>& rhs) | |
2859 { | |
2860 int retval = 1; | |
2861 | |
2862 int n_idx = lhs.index_count (); | |
2863 | |
5275 | 2864 octave_idx_type lhs_nr = lhs.rows (); |
2865 octave_idx_type lhs_nc = lhs.cols (); | |
5681 | 2866 octave_idx_type lhs_nz = lhs.nnz (); |
5275 | 2867 |
2868 octave_idx_type rhs_nr = rhs.rows (); | |
2869 octave_idx_type rhs_nc = rhs.cols (); | |
5164 | 2870 |
2871 idx_vector *tmp = lhs.get_idx (); | |
2872 | |
2873 idx_vector idx_i; | |
2874 idx_vector idx_j; | |
2875 | |
2876 if (n_idx > 2) | |
2877 { | |
2878 (*current_liboctave_error_handler) | |
2879 ("A(I, J) = X: can only have 1 or 2 indexes for sparse matrices"); | |
6092 | 2880 |
2881 lhs.clear_index (); | |
5164 | 2882 return 0; |
2883 } | |
2884 | |
2885 if (n_idx > 1) | |
2886 idx_j = tmp[1]; | |
2887 | |
2888 if (n_idx > 0) | |
2889 idx_i = tmp[0]; | |
2890 | |
6988 | 2891 // Take a constant copy of lhs. This means that ridx and family won't |
2892 // call make_unique. | |
2893 const Sparse<LT> c_lhs (lhs); | |
2894 | |
5164 | 2895 if (n_idx == 2) |
2896 { | |
5781 | 2897 octave_idx_type n = idx_i.freeze (lhs_nr, "row", true); |
2898 octave_idx_type m = idx_j.freeze (lhs_nc, "column", true); | |
5164 | 2899 |
2900 int idx_i_is_colon = idx_i.is_colon (); | |
2901 int idx_j_is_colon = idx_j.is_colon (); | |
2902 | |
7238 | 2903 if (lhs_nr == 0 && lhs_nc == 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2904 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2905 if (idx_i_is_colon) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2906 n = rhs_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2907 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2908 if (idx_j_is_colon) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2909 m = rhs_nc; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2910 } |
5164 | 2911 |
2912 if (idx_i && idx_j) | |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2913 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2914 if (rhs_nr == 1 && rhs_nc == 1 && n >= 0 && m >= 0) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2915 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2916 if (n > 0 && m > 0) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2917 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2918 idx_i.sort (true); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2919 n = idx_i.length (n); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2920 idx_j.sort (true); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2921 m = idx_j.length (m); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2922 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2923 octave_idx_type max_row_idx = idx_i_is_colon ? rhs_nr : |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2924 idx_i.max () + 1; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2925 octave_idx_type max_col_idx = idx_j_is_colon ? rhs_nc : |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2926 idx_j.max () + 1; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2927 octave_idx_type new_nr = max_row_idx > lhs_nr ? |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2928 max_row_idx : lhs_nr; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2929 octave_idx_type new_nc = max_col_idx > lhs_nc ? |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2930 max_col_idx : lhs_nc; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2931 RT scalar = rhs.elem (0, 0); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2932 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2933 // Count the number of non-zero terms |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2934 octave_idx_type new_nzmx = lhs.nnz (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2935 for (octave_idx_type j = 0; j < m; j++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2936 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2937 octave_idx_type jj = idx_j.elem (j); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2938 if (jj < lhs_nc) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2939 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2940 for (octave_idx_type i = 0; i < n; i++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2941 { |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
2942 octave_quit (); |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2943 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2944 octave_idx_type ii = idx_i.elem (i); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2945 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2946 if (ii < lhs_nr) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2947 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2948 for (octave_idx_type k = c_lhs.cidx(jj); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2949 k < c_lhs.cidx(jj+1); k++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2950 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2951 if (c_lhs.ridx(k) == ii) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2952 new_nzmx--; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2953 if (c_lhs.ridx(k) >= ii) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2954 break; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2955 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2956 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2957 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2958 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2959 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2960 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2961 if (scalar != RT()) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2962 new_nzmx += m * n; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2963 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2964 Sparse<LT> stmp (new_nr, new_nc, new_nzmx); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2965 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2966 octave_idx_type jji = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2967 octave_idx_type jj = idx_j.elem (jji); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2968 octave_idx_type kk = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2969 stmp.cidx(0) = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2970 for (octave_idx_type j = 0; j < new_nc; j++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2971 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2972 if (jji < m && jj == j) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2973 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2974 octave_idx_type iii = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2975 octave_idx_type ii = idx_i.elem (iii); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2976 octave_idx_type ppp = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2977 octave_idx_type ppi = (j >= lhs_nc ? 0 : |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2978 c_lhs.cidx(j+1) - |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2979 c_lhs.cidx(j)); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2980 octave_idx_type pp = (ppp < ppi ? |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2981 c_lhs.ridx(c_lhs.cidx(j)+ppp) : |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2982 new_nr); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2983 while (ppp < ppi || iii < n) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2984 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2985 if (iii < n && ii <= pp) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2986 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2987 if (scalar != RT ()) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2988 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2989 stmp.data(kk) = scalar; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2990 stmp.ridx(kk++) = ii; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2991 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2992 if (ii == pp) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
2993 pp = (++ppp < ppi ? c_lhs.ridx(c_lhs.cidx(j)+ppp) : new_nr); |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2994 if (++iii < n) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2995 ii = idx_i.elem(iii); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2996 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2997 else |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2998 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
2999 stmp.data(kk) = |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3000 c_lhs.data(c_lhs.cidx(j)+ppp); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3001 stmp.ridx(kk++) = pp; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3002 pp = (++ppp < ppi ? c_lhs.ridx(c_lhs.cidx(j)+ppp) : new_nr); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3003 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3004 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3005 if (++jji < m) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3006 jj = idx_j.elem(jji); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3007 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3008 else if (j < lhs_nc) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3009 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3010 for (octave_idx_type i = c_lhs.cidx(j); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3011 i < c_lhs.cidx(j+1); i++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3012 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3013 stmp.data(kk) = c_lhs.data(i); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3014 stmp.ridx(kk++) = c_lhs.ridx(i); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3015 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3016 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3017 stmp.cidx(j+1) = kk; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3018 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3019 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3020 lhs = stmp; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3021 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3022 else |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3023 { |
7253 | 3024 #if 0 |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3025 // FIXME -- the following code will make this |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3026 // function behave the same as the full matrix |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3027 // case for things like |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3028 // |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3029 // x = sparse (ones (2)); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3030 // x([],3) = 2; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3031 // |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3032 // x = |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3033 // |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3034 // Compressed Column Sparse (rows = 2, cols = 3, nnz = 4) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3035 // |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3036 // (1, 1) -> 1 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3037 // (2, 1) -> 1 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3038 // (1, 2) -> 1 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3039 // (2, 2) -> 1 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3040 // |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3041 // However, Matlab doesn't resize in this case |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3042 // even though it does in the full matrix case. |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3043 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3044 if (n > 0) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3045 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3046 octave_idx_type max_row_idx = idx_i_is_colon ? |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3047 rhs_nr : idx_i.max () + 1; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3048 octave_idx_type new_nr = max_row_idx > lhs_nr ? |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3049 max_row_idx : lhs_nr; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3050 octave_idx_type new_nc = lhs_nc; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3051 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3052 lhs.resize (new_nr, new_nc); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3053 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3054 else if (m > 0) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3055 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3056 octave_idx_type max_col_idx = idx_j_is_colon ? |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3057 rhs_nc : idx_j.max () + 1; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3058 octave_idx_type new_nr = lhs_nr; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3059 octave_idx_type new_nc = max_col_idx > lhs_nc ? |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3060 max_col_idx : lhs_nc; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3061 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3062 lhs.resize (new_nr, new_nc); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3063 } |
7253 | 3064 #endif |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3065 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3066 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3067 else if (n == rhs_nr && m == rhs_nc) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3068 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3069 if (n > 0 && m > 0) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3070 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3071 octave_idx_type max_row_idx = idx_i_is_colon ? rhs_nr : |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3072 idx_i.max () + 1; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3073 octave_idx_type max_col_idx = idx_j_is_colon ? rhs_nc : |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3074 idx_j.max () + 1; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3075 octave_idx_type new_nr = max_row_idx > lhs_nr ? |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3076 max_row_idx : lhs_nr; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3077 octave_idx_type new_nc = max_col_idx > lhs_nc ? |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3078 max_col_idx : lhs_nc; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3079 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3080 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx_i, n); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3081 if (! idx_i.is_colon ()) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3082 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3083 // Ok here we have to be careful with the indexing, |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3084 // to treat cases like "a([3,2,1],:) = b", and still |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3085 // handle the need for strict sorting of the sparse |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3086 // elements. |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3087 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3088 sidx, n); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3089 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3090 sidxX, n); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3091 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3092 for (octave_idx_type i = 0; i < n; i++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3093 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3094 sidx[i] = &sidxX[i]; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3095 sidx[i]->i = idx_i.elem(i); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3096 sidx[i]->idx = i; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3097 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3098 |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
3099 octave_quit (); |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3100 octave_sort<octave_idx_vector_sort *> |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3101 sort (octave_idx_vector_comp); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3102 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3103 sort.sort (sidx, n); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3104 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3105 intNDArray<octave_idx_type> new_idx (dim_vector (n,1)); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3106 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3107 for (octave_idx_type i = 0; i < n; i++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3108 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8150
diff
changeset
|
3109 new_idx.xelem(i) = sidx[i]->i; |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3110 rhs_idx_i[i] = sidx[i]->idx; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3111 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3112 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3113 idx_i = idx_vector (new_idx); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3114 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3115 else |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3116 for (octave_idx_type i = 0; i < n; i++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3117 rhs_idx_i[i] = i; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3118 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3119 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx_j, m); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3120 if (! idx_j.is_colon ()) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3121 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3122 // Ok here we have to be careful with the indexing, |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3123 // to treat cases like "a([3,2,1],:) = b", and still |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3124 // handle the need for strict sorting of the sparse |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3125 // elements. |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3126 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3127 sidx, m); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3128 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3129 sidxX, m); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3130 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3131 for (octave_idx_type i = 0; i < m; i++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3132 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3133 sidx[i] = &sidxX[i]; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3134 sidx[i]->i = idx_j.elem(i); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3135 sidx[i]->idx = i; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3136 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3137 |
10142
829e69ec3110
make OCTAVE_QUIT a function
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
3138 octave_quit (); |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3139 octave_sort<octave_idx_vector_sort *> |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3140 sort (octave_idx_vector_comp); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3141 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3142 sort.sort (sidx, m); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3143 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3144 intNDArray<octave_idx_type> new_idx (dim_vector (m,1)); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3145 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3146 for (octave_idx_type i = 0; i < m; i++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3147 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8150
diff
changeset
|
3148 new_idx.xelem(i) = sidx[i]->i; |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3149 rhs_idx_j[i] = sidx[i]->idx; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3150 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3151 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3152 idx_j = idx_vector (new_idx); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3153 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3154 else |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3155 for (octave_idx_type i = 0; i < m; i++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3156 rhs_idx_j[i] = i; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3157 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3158 // Maximum number of non-zero elements |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3159 octave_idx_type new_nzmx = lhs.nnz() + rhs.nnz(); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3160 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3161 Sparse<LT> stmp (new_nr, new_nc, new_nzmx); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3162 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3163 octave_idx_type jji = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3164 octave_idx_type jj = idx_j.elem (jji); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3165 octave_idx_type kk = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3166 stmp.cidx(0) = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3167 for (octave_idx_type j = 0; j < new_nc; j++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3168 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3169 if (jji < m && jj == j) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3170 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3171 octave_idx_type iii = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3172 octave_idx_type ii = idx_i.elem (iii); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3173 octave_idx_type ppp = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3174 octave_idx_type ppi = (j >= lhs_nc ? 0 : |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3175 c_lhs.cidx(j+1) - |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3176 c_lhs.cidx(j)); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3177 octave_idx_type pp = (ppp < ppi ? |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3178 c_lhs.ridx(c_lhs.cidx(j)+ppp) : |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3179 new_nr); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3180 while (ppp < ppi || iii < n) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3181 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3182 if (iii < n && ii <= pp) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3183 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3184 if (iii < n - 1 && |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3185 idx_i.elem (iii + 1) == ii) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3186 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3187 iii++; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3188 ii = idx_i.elem(iii); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3189 continue; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3190 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3191 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3192 RT rtmp = rhs.elem (rhs_idx_i[iii], |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3193 rhs_idx_j[jji]); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3194 if (rtmp != RT ()) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3195 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3196 stmp.data(kk) = rtmp; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3197 stmp.ridx(kk++) = ii; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3198 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3199 if (ii == pp) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3200 pp = (++ppp < ppi ? c_lhs.ridx(c_lhs.cidx(j)+ppp) : new_nr); |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3201 if (++iii < n) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3202 ii = idx_i.elem(iii); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3203 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3204 else |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3205 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3206 stmp.data(kk) = |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3207 c_lhs.data(c_lhs.cidx(j)+ppp); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3208 stmp.ridx(kk++) = pp; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3209 pp = (++ppp < ppi ? c_lhs.ridx(c_lhs.cidx(j)+ppp) : new_nr); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3210 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3211 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3212 if (++jji < m) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3213 jj = idx_j.elem(jji); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3214 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3215 else if (j < lhs_nc) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3216 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3217 for (octave_idx_type i = c_lhs.cidx(j); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3218 i < c_lhs.cidx(j+1); i++) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3219 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3220 stmp.data(kk) = c_lhs.data(i); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3221 stmp.ridx(kk++) = c_lhs.ridx(i); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3222 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3223 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3224 stmp.cidx(j+1) = kk; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3225 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3226 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3227 stmp.maybe_compress(); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3228 lhs = stmp; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3229 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3230 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3231 else if (n == 0 && m == 0) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3232 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3233 if (! ((rhs_nr == 1 && rhs_nc == 1) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3234 || (rhs_nr == 0 || rhs_nc == 0))) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3235 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3236 (*current_liboctave_error_handler) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3237 ("A([], []) = X: X must be an empty matrix or a scalar"); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3238 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3239 retval = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3240 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3241 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3242 else |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3243 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3244 (*current_liboctave_error_handler) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3245 ("A(I, J) = X: X must be a scalar or the number of elements in I must"); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3246 (*current_liboctave_error_handler) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3247 ("match the number of rows in X and the number of elements in J must"); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3248 (*current_liboctave_error_handler) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3249 ("match the number of columns in X"); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3250 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3251 retval = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3252 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3253 } |
5164 | 3254 // idx_vector::freeze() printed an error message for us. |
3255 } | |
3256 else if (n_idx == 1) | |
3257 { | |
3258 int lhs_is_empty = lhs_nr == 0 || lhs_nc == 0; | |
3259 | |
3260 if (lhs_is_empty || (lhs_nr == 1 && lhs_nc == 1)) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3261 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3262 octave_idx_type lhs_len = lhs.length (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3263 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3264 // Called for side-effects on idx_i. |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3265 idx_i.freeze (lhs_len, 0, true); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3266 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3267 if (idx_i) |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3268 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3269 if (lhs_is_empty |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3270 && idx_i.is_colon () |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3271 && ! (rhs_nr == 1 || rhs_nc == 1)) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3272 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3273 (*current_liboctave_warning_with_id_handler) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3274 ("Octave:fortran-indexing", |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3275 "A(:) = X: X is not a vector or scalar"); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3276 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3277 else |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3278 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3279 octave_idx_type idx_nr = idx_i.orig_rows (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3280 octave_idx_type idx_nc = idx_i.orig_columns (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3281 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3282 if (! (rhs_nr == idx_nr && rhs_nc == idx_nc)) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3283 (*current_liboctave_warning_with_id_handler) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3284 ("Octave:fortran-indexing", |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3285 "A(I) = X: X does not have same shape as I"); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3286 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3287 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3288 if (! assign1 (lhs, rhs)) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3289 retval = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3290 } |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3291 // idx_vector::freeze() printed an error message for us. |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3292 } |
5164 | 3293 else if (lhs_nr == 1) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3294 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3295 idx_i.freeze (lhs_nc, "vector", true); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3296 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3297 if (idx_i) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3298 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3299 if (! assign1 (lhs, rhs)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3300 retval = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3301 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3302 // idx_vector::freeze() printed an error message for us. |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3303 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3304 else if (lhs_nc == 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3305 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3306 idx_i.freeze (lhs_nr, "vector", true); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3307 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3308 if (idx_i) |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3309 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3310 if (! assign1 (lhs, rhs)) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3311 retval = 0; |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7717
diff
changeset
|
3312 } |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3313 // idx_vector::freeze() printed an error message for us. |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3314 } |
5164 | 3315 else |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3316 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3317 if (! idx_i.is_colon ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3318 (*current_liboctave_warning_with_id_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3319 ("Octave:fortran-indexing", "single index used for matrix"); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3320 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3321 octave_idx_type lhs_len = lhs.length (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3322 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3323 octave_idx_type len = idx_i.freeze (lhs_nr * lhs_nc, "matrix"); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3324 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3325 if (idx_i) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3326 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3327 if (len == 0) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3328 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3329 if (! ((rhs_nr == 1 && rhs_nc == 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3330 || (rhs_nr == 0 || rhs_nc == 0))) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3331 (*current_liboctave_error_handler) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3332 ("A([]) = X: X must be an empty matrix or scalar"); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3333 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3334 else if (len == rhs_nr * rhs_nc) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3335 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3336 octave_idx_type new_nzmx = lhs_nz; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3337 OCTAVE_LOCAL_BUFFER (octave_idx_type, rhs_idx, len); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3338 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3339 if (! idx_i.is_colon ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3340 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3341 // Ok here we have to be careful with the indexing, to |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3342 // treat cases like "a([3,2,1]) = b", and still handle |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3343 // the need for strict sorting of the sparse elements. |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3344 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3345 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort *, sidx, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3346 len); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3347 OCTAVE_LOCAL_BUFFER (octave_idx_vector_sort, sidxX, |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3348 len); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3349 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3350 for (octave_idx_type i = 0; i < len; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3351 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3352 sidx[i] = &sidxX[i]; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3353 sidx[i]->i = idx_i.elem(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3354 sidx[i]->idx = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3355 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3356 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3357 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3358 octave_sort<octave_idx_vector_sort *> |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3359 sort (octave_idx_vector_comp); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3360 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3361 sort.sort (sidx, len); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3362 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3363 intNDArray<octave_idx_type> new_idx (dim_vector (len,1)); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3364 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3365 for (octave_idx_type i = 0; i < len; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3366 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3367 new_idx.xelem(i) = sidx[i]->i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3368 rhs_idx[i] = sidx[i]->idx; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3369 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3370 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3371 idx_i = idx_vector (new_idx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3372 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3373 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3374 for (octave_idx_type i = 0; i < len; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3375 rhs_idx[i] = i; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3376 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3377 // First count the number of non-zero elements |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3378 for (octave_idx_type i = 0; i < len; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3379 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3380 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3381 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3382 octave_idx_type ii = idx_i.elem (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3383 if (i < len - 1 && idx_i.elem (i + 1) == ii) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3384 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3385 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3386 new_nzmx--; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3387 if (rhs.elem(rhs_idx[i]) != RT ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3388 new_nzmx++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3389 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3390 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3391 Sparse<LT> stmp (lhs_nr, lhs_nc, new_nzmx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3392 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3393 octave_idx_type i = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3394 octave_idx_type ii = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3395 octave_idx_type ic = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3396 if (i < lhs_nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3397 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3398 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3399 ic++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3400 ii = ic * lhs_nr + c_lhs.ridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3401 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3402 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3403 octave_idx_type j = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3404 octave_idx_type jj = idx_i.elem (j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3405 octave_idx_type jr = jj % lhs_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3406 octave_idx_type jc = (jj - jr) / lhs_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3407 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3408 octave_idx_type kk = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3409 octave_idx_type kc = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3410 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3411 while (j < len || i < lhs_nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3412 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3413 if (j < len - 1 && idx_i.elem (j + 1) == jj) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3414 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3415 j++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3416 jj = idx_i.elem (j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3417 jr = jj % lhs_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3418 jc = (jj - jr) / lhs_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3419 continue; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3420 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3421 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3422 if (j == len || (i < lhs_nz && ii < jj)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3423 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3424 while (kc <= ic) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3425 stmp.xcidx (kc++) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3426 stmp.xdata (kk) = c_lhs.data (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3427 stmp.xridx (kk++) = c_lhs.ridx (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3428 i++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3429 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3430 ic++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3431 if (i < lhs_nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3432 ii = ic * lhs_nr + c_lhs.ridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3433 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3434 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3435 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3436 while (kc <= jc) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3437 stmp.xcidx (kc++) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3438 RT rtmp = rhs.elem (rhs_idx[j]); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3439 if (rtmp != RT ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3440 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3441 stmp.xdata (kk) = rtmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3442 stmp.xridx (kk++) = jr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3443 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3444 if (ii == jj) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3445 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3446 i++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3447 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3448 ic++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3449 if (i < lhs_nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3450 ii = ic * lhs_nr + c_lhs.ridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3451 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3452 j++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3453 if (j < len) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3454 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3455 jj = idx_i.elem (j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3456 jr = jj % lhs_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3457 jc = (jj - jr) / lhs_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3458 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3459 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3460 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3461 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3462 for (octave_idx_type iidx = kc; iidx < lhs_nc+1; iidx++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3463 stmp.xcidx(iidx) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3464 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3465 lhs = stmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3466 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3467 else if (rhs_nr == 1 && rhs_nc == 1) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3468 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3469 RT scalar = rhs.elem (0, 0); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3470 octave_idx_type new_nzmx = lhs_nz; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3471 idx_i.sort (true); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3472 len = idx_i.length (len); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3473 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3474 // First count the number of non-zero elements |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3475 if (scalar != RT ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3476 new_nzmx += len; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3477 for (octave_idx_type i = 0; i < len; i++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3478 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3479 octave_quit (); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3480 octave_idx_type ii = idx_i.elem (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3481 if (ii < lhs_len && c_lhs.elem(ii) != LT ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3482 new_nzmx--; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3483 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3484 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3485 Sparse<LT> stmp (lhs_nr, lhs_nc, new_nzmx); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3486 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3487 octave_idx_type i = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3488 octave_idx_type ii = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3489 octave_idx_type ic = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3490 if (i < lhs_nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3491 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3492 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3493 ic++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3494 ii = ic * lhs_nr + c_lhs.ridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3495 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3496 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3497 octave_idx_type j = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3498 octave_idx_type jj = idx_i.elem (j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3499 octave_idx_type jr = jj % lhs_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3500 octave_idx_type jc = (jj - jr) / lhs_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3501 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3502 octave_idx_type kk = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3503 octave_idx_type kc = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3504 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3505 while (j < len || i < lhs_nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3506 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3507 if (j == len || (i < lhs_nz && ii < jj)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3508 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3509 while (kc <= ic) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3510 stmp.xcidx (kc++) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3511 stmp.xdata (kk) = c_lhs.data (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3512 stmp.xridx (kk++) = c_lhs.ridx (i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3513 i++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3514 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3515 ic++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3516 if (i < lhs_nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3517 ii = ic * lhs_nr + c_lhs.ridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3518 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3519 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3520 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3521 while (kc <= jc) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3522 stmp.xcidx (kc++) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3523 if (scalar != RT ()) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3524 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3525 stmp.xdata (kk) = scalar; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3526 stmp.xridx (kk++) = jr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3527 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3528 if (ii == jj) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3529 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3530 i++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3531 while (ic < lhs_nc && i >= c_lhs.cidx(ic+1)) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3532 ic++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3533 if (i < lhs_nz) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3534 ii = ic * lhs_nr + c_lhs.ridx(i); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3535 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3536 j++; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3537 if (j < len) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3538 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3539 jj = idx_i.elem (j); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3540 jr = jj % lhs_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3541 jc = (jj - jr) / lhs_nr; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3542 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3543 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3544 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3545 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3546 for (octave_idx_type iidx = kc; iidx < lhs_nc+1; iidx++) |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3547 stmp.xcidx(iidx) = kk; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3548 |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3549 lhs = stmp; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3550 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3551 else |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3552 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3553 (*current_liboctave_error_handler) |
5164 | 3554 ("A(I) = X: X must be a scalar or a matrix with the same size as I"); |
3555 | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3556 retval = 0; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3557 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3558 } |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3559 // idx_vector::freeze() printed an error message for us. |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3560 } |
5164 | 3561 } |
3562 else | |
3563 { | |
3564 (*current_liboctave_error_handler) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
3565 ("invalid number of indices for matrix expression"); |
5164 | 3566 |
3567 retval = 0; | |
3568 } | |
3569 | |
3570 lhs.clear_index (); | |
3571 | |
3572 return retval; | |
3573 } | |
3574 | |
7356 | 3575 /* |
3576 * Tests | |
3577 * | |
3578 | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3579 %!function x = set_slice(x, dim, slice, arg) |
7356 | 3580 %! switch dim |
3581 %! case 11 | |
3582 %! x(slice) = 2; | |
3583 %! case 21 | |
3584 %! x(slice, :) = 2; | |
3585 %! case 22 | |
3586 %! x(:, slice) = 2; | |
3587 %! otherwise | |
3588 %! error("invalid dim, '%d'", dim); | |
3589 %! endswitch | |
3590 %! endfunction | |
3591 | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3592 %!function x = set_slice2(x, dim, slice) |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3593 %! switch dim |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3594 %! case 11 |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3595 %! x(slice) = 2 * ones (size(slice)); |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3596 %! case 21 |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3597 %! x(slice, :) = 2 * ones (length(slice), columns (x)); |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3598 %! case 22 |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3599 %! x(:, slice) = 2 * ones (rows (x), length(slice)); |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3600 %! otherwise |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3601 %! error("invalid dim, '%d'", dim); |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3602 %! endswitch |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3603 %! endfunction |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3604 |
7356 | 3605 %!function test_sparse_slice(size, dim, slice) |
3606 %! x = ones(size); | |
3607 %! s = set_slice(sparse(x), dim, slice); | |
3608 %! f = set_slice(x, dim, slice); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3609 %! assert (nnz(s), nnz(f)); |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3610 %! assert(full(s), f); |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3611 %! s = set_slice2(sparse(x), dim, slice); |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3612 %! f = set_slice2(x, dim, slice); |
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3613 %! assert (nnz(s), nnz(f)); |
7356 | 3614 %! assert(full(s), f); |
3615 %! endfunction | |
3616 | |
3617 #### 1d indexing | |
3618 | |
3619 ## size = [2 0] | |
3620 %!test test_sparse_slice([2 0], 11, []); | |
3621 %!assert(set_slice(sparse(ones([2 0])), 11, 1), sparse([2 0]')); # sparse different from full | |
3622 %!assert(set_slice(sparse(ones([2 0])), 11, 2), sparse([0 2]')); # sparse different from full | |
3623 %!assert(set_slice(sparse(ones([2 0])), 11, 3), sparse([0 0 2]')); # sparse different from full | |
3624 %!assert(set_slice(sparse(ones([2 0])), 11, 4), sparse([0 0 0 2]')); # sparse different from full | |
3625 | |
3626 ## size = [0 2] | |
3627 %!test test_sparse_slice([0 2], 11, []); | |
3628 %!assert(set_slice(sparse(ones([0 2])), 11, 1), sparse(1,2)); # sparse different from full | |
3629 %!test test_sparse_slice([0 2], 11, 2); | |
3630 %!test test_sparse_slice([0 2], 11, 3); | |
3631 %!test test_sparse_slice([0 2], 11, 4); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3632 %!test test_sparse_slice([0 2], 11, [4, 4]); |
7356 | 3633 |
3634 ## size = [2 1] | |
3635 %!test test_sparse_slice([2 1], 11, []); | |
3636 %!test test_sparse_slice([2 1], 11, 1); | |
3637 %!test test_sparse_slice([2 1], 11, 2); | |
3638 %!test test_sparse_slice([2 1], 11, 3); | |
3639 %!test test_sparse_slice([2 1], 11, 4); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3640 %!test test_sparse_slice([2 1], 11, [4, 4]); |
7356 | 3641 |
3642 ## size = [1 2] | |
3643 %!test test_sparse_slice([1 2], 11, []); | |
3644 %!test test_sparse_slice([1 2], 11, 1); | |
3645 %!test test_sparse_slice([1 2], 11, 2); | |
3646 %!test test_sparse_slice([1 2], 11, 3); | |
3647 %!test test_sparse_slice([1 2], 11, 4); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3648 %!test test_sparse_slice([1 2], 11, [4, 4]); |
7356 | 3649 |
3650 ## size = [2 2] | |
3651 %!test test_sparse_slice([2 2], 11, []); | |
3652 %!test test_sparse_slice([2 2], 11, 1); | |
3653 %!test test_sparse_slice([2 2], 11, 2); | |
3654 %!test test_sparse_slice([2 2], 11, 3); | |
3655 %!test test_sparse_slice([2 2], 11, 4); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3656 %!test test_sparse_slice([2 2], 11, [4, 4]); |
7356 | 3657 # These 2 errors are the same as in the full case |
3658 %!error <invalid matrix index = 5> set_slice(sparse(ones([2 2])), 11, 5); | |
3659 %!error <invalid matrix index = 6> set_slice(sparse(ones([2 2])), 11, 6); | |
3660 | |
3661 | |
3662 #### 2d indexing | |
3663 | |
3664 ## size = [2 0] | |
3665 %!test test_sparse_slice([2 0], 21, []); | |
3666 %!test test_sparse_slice([2 0], 21, 1); | |
3667 %!test test_sparse_slice([2 0], 21, 2); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3668 %!test test_sparse_slice([2 0], 21, [2,2]); |
7356 | 3669 %!assert(set_slice(sparse(ones([2 0])), 21, 3), sparse(2,0)); # sparse different from full |
3670 %!assert(set_slice(sparse(ones([2 0])), 21, 4), sparse(2,0)); # sparse different from full | |
3671 %!test test_sparse_slice([2 0], 22, []); | |
3672 %!test test_sparse_slice([2 0], 22, 1); | |
3673 %!test test_sparse_slice([2 0], 22, 2); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3674 %!test test_sparse_slice([2 0], 22, [2,2]); |
7356 | 3675 %!assert(set_slice(sparse(ones([2 0])), 22, 3), sparse([0 0 2;0 0 2])); # sparse different from full |
3676 %!assert(set_slice(sparse(ones([2 0])), 22, 4), sparse([0 0 0 2;0 0 0 2])); # sparse different from full | |
3677 | |
3678 ## size = [0 2] | |
3679 %!test test_sparse_slice([0 2], 21, []); | |
3680 %!test test_sparse_slice([0 2], 21, 1); | |
3681 %!test test_sparse_slice([0 2], 21, 2); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3682 %!test test_sparse_slice([0 2], 21, [2,2]); |
7356 | 3683 %!assert(set_slice(sparse(ones([0 2])), 21, 3), sparse([0 0;0 0;2 2])); # sparse different from full |
3684 %!assert(set_slice(sparse(ones([0 2])), 21, 4), sparse([0 0;0 0;0 0;2 2])); # sparse different from full | |
3685 %!test test_sparse_slice([0 2], 22, []); | |
3686 %!test test_sparse_slice([0 2], 22, 1); | |
3687 %!test test_sparse_slice([0 2], 22, 2); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3688 %!test test_sparse_slice([0 2], 22, [2,2]); |
7356 | 3689 %!assert(set_slice(sparse(ones([0 2])), 22, 3), sparse(0,2)); # sparse different from full |
3690 %!assert(set_slice(sparse(ones([0 2])), 22, 4), sparse(0,2)); # sparse different from full | |
3691 | |
3692 ## size = [2 1] | |
3693 %!test test_sparse_slice([2 1], 21, []); | |
3694 %!test test_sparse_slice([2 1], 21, 1); | |
3695 %!test test_sparse_slice([2 1], 21, 2); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3696 %!test test_sparse_slice([2 1], 21, [2,2]); |
7356 | 3697 %!test test_sparse_slice([2 1], 21, 3); |
3698 %!test test_sparse_slice([2 1], 21, 4); | |
3699 %!test test_sparse_slice([2 1], 22, []); | |
3700 %!test test_sparse_slice([2 1], 22, 1); | |
3701 %!test test_sparse_slice([2 1], 22, 2); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3702 %!test test_sparse_slice([2 1], 22, [2,2]); |
7356 | 3703 %!test test_sparse_slice([2 1], 22, 3); |
3704 %!test test_sparse_slice([2 1], 22, 4); | |
3705 | |
3706 ## size = [1 2] | |
3707 %!test test_sparse_slice([1 2], 21, []); | |
3708 %!test test_sparse_slice([1 2], 21, 1); | |
3709 %!test test_sparse_slice([1 2], 21, 2); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3710 %!test test_sparse_slice([1 2], 21, [2,2]); |
7356 | 3711 %!test test_sparse_slice([1 2], 21, 3); |
3712 %!test test_sparse_slice([1 2], 21, 4); | |
3713 %!test test_sparse_slice([1 2], 22, []); | |
3714 %!test test_sparse_slice([1 2], 22, 1); | |
3715 %!test test_sparse_slice([1 2], 22, 2); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3716 %!test test_sparse_slice([1 2], 22, [2,2]); |
7356 | 3717 %!test test_sparse_slice([1 2], 22, 3); |
3718 %!test test_sparse_slice([1 2], 22, 4); | |
3719 | |
3720 ## size = [2 2] | |
3721 %!test test_sparse_slice([2 2], 21, []); | |
3722 %!test test_sparse_slice([2 2], 21, 1); | |
3723 %!test test_sparse_slice([2 2], 21, 2); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3724 %!test test_sparse_slice([2 2], 21, [2,2]); |
7356 | 3725 %!test test_sparse_slice([2 2], 21, 3); |
3726 %!test test_sparse_slice([2 2], 21, 4); | |
3727 %!test test_sparse_slice([2 2], 22, []); | |
3728 %!test test_sparse_slice([2 2], 22, 1); | |
3729 %!test test_sparse_slice([2 2], 22, 2); | |
7546
4249c6fb6e09
Treat repeated indices in the sparse assignments
David Bateman <dbateman@free.fr>
parents:
7463
diff
changeset
|
3730 %!test test_sparse_slice([2 2], 22, [2,2]); |
7356 | 3731 %!test test_sparse_slice([2 2], 22, 3); |
3732 %!test test_sparse_slice([2 2], 22, 4); | |
3733 | |
3734 */ | |
3735 | |
5164 | 3736 template <class T> |
3737 void | |
3738 Sparse<T>::print_info (std::ostream& os, const std::string& prefix) const | |
3739 { | |
3740 os << prefix << "rep address: " << rep << "\n" | |
5604 | 3741 << prefix << "rep->nzmx: " << rep->nzmx << "\n" |
5164 | 3742 << prefix << "rep->nrows: " << rep->nrows << "\n" |
3743 << prefix << "rep->ncols: " << rep->ncols << "\n" | |
3744 << prefix << "rep->data: " << static_cast<void *> (rep->d) << "\n" | |
3745 << prefix << "rep->ridx: " << static_cast<void *> (rep->r) << "\n" | |
3746 << prefix << "rep->cidx: " << static_cast<void *> (rep->c) << "\n" | |
3747 << prefix << "rep->count: " << rep->count << "\n"; | |
3748 } |