5164
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 David Bateman |
|
4 Copyright (C) 1998-2004 Andy Adler |
|
5 |
|
6 Octave is free software; you can redistribute it and/or modify it |
|
7 under the terms of the GNU General Public License as published by the |
|
8 Free Software Foundation; either version 2, or (at your option) any |
|
9 later version. |
|
10 |
|
11 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 for more details. |
|
15 |
|
16 You should have received a copy of the GNU General Public License |
5307
|
17 along with this program; see the file COPYING. If not, write to the |
|
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
19 Boston, MA 02110-1301, USA. |
5164
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cstdlib> |
|
28 #include <string> |
|
29 |
|
30 #include "variables.h" |
|
31 #include "utils.h" |
|
32 #include "pager.h" |
|
33 #include "defun-dld.h" |
|
34 #include "gripes.h" |
|
35 #include "quit.h" |
|
36 |
|
37 #include "ov-re-sparse.h" |
|
38 #include "ov-cx-sparse.h" |
|
39 #include "ov-bool-sparse.h" |
|
40 |
|
41 static bool |
|
42 is_sparse (const octave_value& arg) |
|
43 { |
|
44 return (arg.class_name () == "sparse"); |
|
45 } |
|
46 |
|
47 DEFUN_DLD (issparse, args, , |
|
48 "-*- texinfo -*-\n\ |
|
49 @deftypefn {Loadable Function} {} issparse (@var{expr})\n\ |
|
50 Return 1 if the value of the expression @var{expr} is a sparse matrix.\n\ |
|
51 @end deftypefn") |
|
52 { |
|
53 if (args.length() != 1) |
|
54 { |
|
55 print_usage("issparse"); |
|
56 return octave_value (); |
|
57 } |
|
58 else |
|
59 return octave_value (is_sparse (args(0))); |
|
60 } |
|
61 |
|
62 DEFUN_DLD (sparse, args, , |
|
63 "-*- texinfo -*-\n\ |
|
64 @deftypefn {Loadable Function} {@var{sparse_val} =} sparse (...)\n\ |
|
65 SPARSE: create a sparse matrix\n\ |
|
66 \n\ |
|
67 sparse can be called in the following ways:\n\ |
|
68 \n\ |
|
69 @enumerate\n\ |
|
70 @item @var{S} = sparse(@var{A}) where @var{A} is a full matrix\n\ |
|
71 \n\ |
|
72 @item @var{S} = sparse(@var{A},1) where @var{A} is a full matrix, result\n\ |
|
73 is forced back to a full matrix is resulting matrix is sparse\n\ |
|
74 \n\ |
|
75 @item @var{S} = sparse(@var{i},@var{j},@var{s},@var{m},@var{n},@var{nzmax}) where\n\ |
|
76 @itemize @w \n\ |
|
77 @var{i},@var{j} are integer index vectors (1 x nnz) @* \n\ |
|
78 @var{s} is the vector of real or complex entries (1 x nnz) @* \n\ |
|
79 @var{m},@var{n} are the scalar dimentions of S @* \n\ |
|
80 @var{nzmax} is ignored (here for compatability with Matlab) @* \n\ |
|
81 \n\ |
|
82 if multiple values are specified with the same @var{i},@var{j}\n\ |
|
83 position, the corresponding values in @var{s} will be added\n\ |
|
84 @end itemize\n\ |
|
85 \n\ |
|
86 @item The following usages are equivalent to (2) above:\n\ |
|
87 @itemize @w \n\ |
|
88 @var{S} = sparse(@var{i},@var{j},@var{s},@var{m},@var{n})@*\n\ |
|
89 @var{S} = sparse(@var{i},@var{j},@var{s},@var{m},@var{n},'summation')@*\n\ |
|
90 @var{S} = sparse(@var{i},@var{j},@var{s},@var{m},@var{n},'sum')@*\n\ |
|
91 @end itemize\n\ |
|
92 \n\ |
|
93 @item @var{S} = sparse(@var{i},@var{j},@var{s},@var{m},@var{n},'unique')@*\n\ |
|
94 \n\ |
|
95 @itemize @w \n\ |
|
96 same as (2) above, except that rather than adding,\n\ |
|
97 if more than two values are specified for the same @var{i},@var{j}\n\ |
|
98 position, then the last specified value will be kept\n\ |
|
99 @end itemize\n\ |
|
100 \n\ |
|
101 @item @var{S}= sparse(@var{i},@var{j},@var{sv}) uses @var{m}=max(@var{i}), @var{n}=max(@var{j})\n\ |
|
102 \n\ |
|
103 @item @var{S}= sparse(@var{m},@var{n}) does sparse([],[],[],@var{m},@var{n},0)\n\ |
|
104 \n\ |
|
105 @var{sv}, and @var{i} or @var{j} may be scalars, in\n\ |
|
106 which case they are expanded to all have the same length\n\ |
|
107 @end enumerate\n\ |
|
108 @seealso{full}\n\ |
|
109 @end deftypefn") |
|
110 { |
|
111 octave_value retval; |
|
112 bool mutate = false; |
|
113 |
|
114 // WARNING: This function should always use constructions like |
|
115 // retval = new octave_sparse_matrix (sm); |
|
116 // To avoid calling the maybe_mutate function. This is the only |
|
117 // function that should not call maybe_mutate, or at least only |
|
118 // in very particular cases. |
|
119 |
|
120 int nargin= args.length(); |
|
121 if (nargin < 1 || (nargin == 4 && !args(3).is_string ()) || nargin > 6) |
|
122 { |
|
123 print_usage ("sparse"); |
|
124 return retval; |
|
125 } |
|
126 |
|
127 bool use_complex = false; |
|
128 bool use_bool = false; |
|
129 if (nargin > 2) |
|
130 { |
|
131 use_complex= args(2).is_complex_type(); |
|
132 use_bool = args(2).is_bool_type (); |
|
133 } |
|
134 else |
|
135 { |
|
136 use_complex= args(0).is_complex_type(); |
|
137 use_bool = args(0).is_bool_type (); |
|
138 } |
|
139 |
|
140 if (nargin == 2 && ! args(0).is_scalar_type() && args(1).is_scalar_type()) |
|
141 mutate = (args(1).double_value() != 0.); |
|
142 |
5282
|
143 if (nargin == 1 || (nargin == 2 && ! args(0).is_scalar_type() && |
|
144 args(1).is_scalar_type())) |
5164
|
145 { |
|
146 octave_value arg = args (0); |
|
147 |
|
148 if (is_sparse (arg)) |
|
149 { |
|
150 if (use_complex) |
|
151 { |
|
152 SparseComplexMatrix sm (((const octave_sparse_complex_matrix&) arg |
|
153 .get_rep ()) |
|
154 .sparse_complex_matrix_value ()); |
|
155 retval = new octave_sparse_complex_matrix (sm); |
|
156 } |
|
157 else if (use_bool) |
|
158 { |
|
159 SparseBoolMatrix sm (((const octave_sparse_bool_matrix&) arg |
|
160 .get_rep ()) |
|
161 .sparse_bool_matrix_value ()); |
|
162 retval = new octave_sparse_bool_matrix (sm); |
|
163 } |
|
164 else |
|
165 { |
|
166 SparseMatrix sm (((const octave_sparse_matrix&) arg |
|
167 .get_rep ()) |
|
168 .sparse_matrix_value ()); |
|
169 retval = new octave_sparse_matrix (sm); |
|
170 } |
|
171 } |
|
172 else |
|
173 { |
|
174 if (use_complex) |
|
175 { |
|
176 SparseComplexMatrix sm (args (0).complex_matrix_value ()); |
|
177 if (error_state) |
|
178 return retval; |
|
179 retval = new octave_sparse_complex_matrix (sm); |
|
180 } |
|
181 else if (use_bool) |
|
182 { |
|
183 SparseBoolMatrix sm (args (0).bool_matrix_value ()); |
|
184 if (error_state) |
|
185 return retval; |
|
186 retval = new octave_sparse_bool_matrix (sm); |
|
187 } |
|
188 else |
|
189 { |
|
190 SparseMatrix sm (args (0).matrix_value ()); |
|
191 if (error_state) |
|
192 return retval; |
|
193 retval = new octave_sparse_matrix (sm); |
|
194 } |
|
195 } |
|
196 } |
|
197 else |
|
198 { |
5275
|
199 octave_idx_type m = 1, n = 1; |
5164
|
200 if (nargin == 2) |
|
201 { |
|
202 m = args(0).int_value(); |
|
203 n = args(1).int_value(); |
|
204 if (error_state) return retval; |
|
205 |
|
206 if (use_complex) |
|
207 retval = new octave_sparse_complex_matrix |
|
208 (SparseComplexMatrix (m, n)); |
|
209 else if (use_bool) |
|
210 retval = new octave_sparse_bool_matrix |
|
211 (SparseBoolMatrix (m, n)); |
|
212 else |
|
213 retval = new octave_sparse_matrix |
|
214 (SparseMatrix (m, n)); |
|
215 } |
|
216 else |
|
217 { |
|
218 if (args(0).is_empty () || args (1).is_empty () |
|
219 || args(2).is_empty ()) |
|
220 { |
|
221 if (nargin > 4) |
|
222 { |
|
223 m = args(3).int_value(); |
|
224 n = args(4).int_value(); |
|
225 } |
|
226 |
|
227 if (use_bool) |
|
228 retval = new octave_sparse_bool_matrix |
|
229 (SparseBoolMatrix (m, n)); |
|
230 else |
|
231 retval = new octave_sparse_matrix (SparseMatrix (m, n)); |
|
232 } |
|
233 else |
|
234 { |
|
235 // |
|
236 // I use this clumsy construction so that we can use |
|
237 // any orientation of args |
|
238 ColumnVector ridxA = ColumnVector (args(0).vector_value |
|
239 (false, true)); |
|
240 ColumnVector cidxA = ColumnVector (args(1).vector_value |
|
241 (false, true)); |
|
242 ColumnVector coefA; |
|
243 boolNDArray coefAB; |
|
244 ComplexColumnVector coefAC; |
|
245 bool assemble_do_sum = true; // this is the default in matlab6 |
|
246 |
|
247 if (use_complex) |
|
248 { |
|
249 if (args(2).is_empty ()) |
|
250 coefAC = ComplexColumnVector (0); |
|
251 else |
|
252 coefAC = ComplexColumnVector |
|
253 (args(2).complex_vector_value (false, true)); |
|
254 } |
|
255 else if (use_bool) |
|
256 { |
|
257 if (args(2).is_empty ()) |
|
258 coefAB = boolNDArray (dim_vector (1, 0)); |
|
259 else |
|
260 coefAB = args(2).bool_array_value (); |
|
261 dim_vector AB_dims = coefAB.dims (); |
|
262 if (AB_dims.length() > 2 || (AB_dims(0) != 1 && |
|
263 AB_dims(1) != 1)) |
|
264 error ("sparse: vector arguments required"); |
|
265 } |
|
266 else |
|
267 if (args(2).is_empty ()) |
|
268 coefA = ColumnVector (0); |
|
269 else |
|
270 coefA = ColumnVector (args(2).vector_value (false, true)); |
|
271 |
|
272 if (error_state) |
|
273 return retval; |
|
274 |
|
275 // Confirm that i,j,s all have the same number of elements |
5275
|
276 octave_idx_type ns; |
5164
|
277 if (use_complex) |
|
278 ns = coefAC.length(); |
|
279 else if (use_bool) |
|
280 ns = coefAB.length(); |
|
281 else |
|
282 ns = coefA.length(); |
|
283 |
5275
|
284 octave_idx_type ni = ridxA.length(); |
|
285 octave_idx_type nj = cidxA.length(); |
|
286 octave_idx_type nnz = (ni > nj ? ni : nj); |
5164
|
287 if ((ns != 1 && ns != nnz) || |
|
288 (ni != 1 && ni != nnz) || |
|
289 (nj != 1 && nj != nnz)) |
|
290 { |
|
291 error ("sparse i, j and s must have the same length"); |
|
292 return retval; |
|
293 } |
|
294 |
|
295 if (nargin == 3 || nargin == 4) |
|
296 { |
5275
|
297 m = static_cast<octave_idx_type> (ridxA.max()); |
|
298 n = static_cast<octave_idx_type> (cidxA.max()); |
5164
|
299 |
|
300 // if args(3) is not string, then ignore the value |
|
301 // otherwise check for summation or unique |
|
302 if (nargin == 4 && args(3).is_string()) |
|
303 { |
|
304 std::string vv= args(3).string_value(); |
|
305 if (error_state) return retval; |
|
306 |
|
307 if ( vv == "summation" || |
|
308 vv == "sum" ) |
|
309 assemble_do_sum = true; |
|
310 else |
|
311 if ( vv == "unique" ) |
|
312 assemble_do_sum = false; |
|
313 else { |
|
314 error("sparse repeat flag must be 'sum' or 'unique'"); |
|
315 return retval; |
|
316 } |
|
317 } |
|
318 } |
|
319 else |
|
320 { |
|
321 m = args(3).int_value(); |
|
322 n = args(4).int_value(); |
|
323 if (error_state) |
|
324 return retval; |
|
325 |
|
326 // if args(5) is not string, then ignore the value |
|
327 // otherwise check for summation or unique |
|
328 if (nargin >= 6 && args(5).is_string()) |
|
329 { |
|
330 std::string vv= args(5).string_value(); |
|
331 if (error_state) return retval; |
|
332 |
|
333 if ( vv == "summation" || |
|
334 vv == "sum" ) |
|
335 assemble_do_sum = true; |
|
336 else |
|
337 if ( vv == "unique" ) |
|
338 assemble_do_sum = false; |
|
339 else { |
|
340 error("sparse repeat flag must be 'sum' or 'unique'"); |
|
341 return retval; |
|
342 } |
|
343 } |
|
344 |
|
345 } |
|
346 |
|
347 // Convert indexing to zero-indexing used internally |
|
348 ridxA -= 1.; |
|
349 cidxA -= 1.; |
|
350 |
|
351 if (use_complex) |
|
352 retval = new octave_sparse_complex_matrix |
|
353 (SparseComplexMatrix (coefAC, ridxA, cidxA, m, n, |
|
354 assemble_do_sum)); |
|
355 else if (use_bool) |
|
356 retval = new octave_sparse_bool_matrix |
|
357 (SparseBoolMatrix (coefAB, ridxA, cidxA, m, n, |
|
358 assemble_do_sum)); |
|
359 else |
|
360 retval = new octave_sparse_matrix |
|
361 (SparseMatrix (coefA, ridxA, cidxA, m, n, |
|
362 assemble_do_sum)); |
|
363 } |
|
364 } |
|
365 } |
|
366 |
|
367 // Only called in very particular cases, not the default case |
|
368 if (mutate) |
|
369 retval.maybe_mutate (); |
|
370 |
|
371 return retval; |
|
372 } |
|
373 |
|
374 DEFUN_DLD (full, args, , |
|
375 "-*- texinfo -*-\n\ |
|
376 @deftypefn {Loadable Function} {@var{FM} =} full (@var{SM})\n\ |
|
377 returns a full storage matrix from a sparse one\n\ |
|
378 @seealso{sparse}\n\ |
|
379 @end deftypefn") |
|
380 { |
|
381 octave_value retval; |
|
382 |
|
383 if (args.length() < 1) { |
|
384 print_usage ("full"); |
|
385 return retval; |
|
386 } |
|
387 |
|
388 if (args(0).class_name () == "sparse") |
|
389 { |
|
390 if (args(0).type_name () == "sparse matrix") |
|
391 retval = args(0).matrix_value (); |
|
392 else if (args(0).type_name () == "sparse complex matrix") |
|
393 retval = args(0).complex_matrix_value (); |
|
394 else if (args(0).type_name () == "sparse bool matrix") |
|
395 retval = args(0).bool_matrix_value (); |
|
396 } |
|
397 else if (args(0).is_real_type()) |
|
398 retval = args(0).matrix_value(); |
|
399 else if (args(0).is_complex_type()) |
|
400 retval = args(0).complex_matrix_value(); |
|
401 else |
|
402 gripe_wrong_type_arg ("full", args(0)); |
|
403 |
|
404 return retval; |
|
405 } |
|
406 |
|
407 DEFUN_DLD (nzmax, args, , |
|
408 "-*- texinfo -*-\n\ |
|
409 @deftypefn {Loadable Function} {@var{scalar} =} nzmax (@var{SM})\n\ |
5324
|
410 Return the amount of storage allocated to the sparse matrix @var{SM}.\n\ |
|
411 Note that Octave tends to crop unused memory at the first oppurtunity\n\ |
5164
|
412 for sparse objects. There are some cases of user created sparse objects\n\ |
|
413 where the value returned by @dfn{nzmaz} will not be the same as @dfn{nnz},\n\ |
|
414 but in general they will give the same result.\n\ |
|
415 @seealso{sparse, spalloc}\n\ |
|
416 @end deftypefn") |
|
417 { |
|
418 octave_value retval; |
|
419 |
|
420 if (args.length() < 1) |
|
421 { |
|
422 print_usage ("nzmax"); |
|
423 return retval; |
|
424 } |
|
425 |
|
426 if (args(0).class_name () == "sparse") |
|
427 { |
|
428 // XXX FIXME XXX should nnz be a method of octave_base_value so that the |
|
429 // below can be replaced with "retval = (double) (args(0).nz ());" |
|
430 const octave_value& rep = args(0).get_rep (); |
|
431 |
|
432 if (args(0).type_name () == "sparse matrix") |
|
433 retval = (double) ((const octave_sparse_matrix&) rep) .nnz (); |
|
434 else if (args(0).type_name () == "sparse complex matrix") |
|
435 retval = (double) ((const octave_sparse_complex_matrix&) rep) .nnz (); |
|
436 else if (args(0).type_name () == "sparse bool matrix") |
|
437 retval = (double) ((const octave_sparse_bool_matrix&) rep) .nnz (); |
|
438 } |
|
439 else |
|
440 error ("nzmax: argument must be a sparse matrix"); |
|
441 |
|
442 return retval; |
|
443 } |
|
444 |
|
445 static octave_value_list |
|
446 sparse_find (const SparseMatrix& v) |
|
447 { |
|
448 octave_value_list retval; |
5591
|
449 octave_idx_type nnz = v.nonzero (); |
5164
|
450 dim_vector dv = v.dims (); |
5275
|
451 octave_idx_type nr = dv(0); |
|
452 octave_idx_type nc = dv (1); |
5164
|
453 |
|
454 ColumnVector I (nnz), J (nnz); |
|
455 ColumnVector S (nnz); |
|
456 |
5275
|
457 for (octave_idx_type i = 0, cx = 0; i < nc; i++) |
5164
|
458 { |
|
459 OCTAVE_QUIT; |
5275
|
460 for (octave_idx_type j = v.cidx(i); j < v.cidx(i+1); j++ ) |
5164
|
461 { |
|
462 I (cx) = static_cast<double> (v.ridx(j) + 1); |
|
463 J (cx) = static_cast<double> (i + 1); |
|
464 S (cx) = v.data(j); |
|
465 cx++; |
|
466 } |
|
467 } |
|
468 |
|
469 if (dv(0) == 1) |
|
470 { |
|
471 retval(0)= I.transpose (); |
|
472 retval(1)= J.transpose (); |
|
473 retval(2)= S.transpose (); |
|
474 } |
|
475 else |
|
476 { |
|
477 retval(0)= I; |
|
478 retval(1)= J; |
|
479 retval(2)= S; |
|
480 } |
|
481 retval(3)= (double) nr; |
|
482 retval(4)= (double) nc; |
|
483 return retval; |
|
484 } |
|
485 |
|
486 static octave_value_list |
|
487 sparse_find (const SparseComplexMatrix& v) |
|
488 { |
|
489 octave_value_list retval; |
5591
|
490 octave_idx_type nnz = v.nonzero (); |
5164
|
491 dim_vector dv = v.dims (); |
5275
|
492 octave_idx_type nr = dv(0); |
|
493 octave_idx_type nc = dv (1); |
5164
|
494 |
|
495 ColumnVector I (nnz), J (nnz); |
|
496 ComplexColumnVector S (nnz); |
|
497 |
5275
|
498 for (octave_idx_type i = 0, cx = 0; i < nc; i++) |
5164
|
499 { |
|
500 OCTAVE_QUIT; |
5275
|
501 for (octave_idx_type j = v.cidx(i); j < v.cidx(i+1); j++ ) |
5164
|
502 { |
|
503 I (cx) = static_cast<double> (v.ridx(j) + 1); |
|
504 J (cx) = static_cast<double> (i + 1); |
|
505 S (cx) = v.data(j); |
|
506 cx++; |
|
507 } |
|
508 } |
|
509 |
|
510 if (dv(0) == 1) |
|
511 { |
|
512 retval(0)= I.transpose (); |
|
513 retval(1)= J.transpose (); |
|
514 retval(2)= S.transpose (); |
|
515 } |
|
516 else |
|
517 { |
|
518 retval(0)= I; |
|
519 retval(1)= J; |
|
520 retval(2)= S; |
|
521 } |
|
522 retval(3)= (double) nr; |
|
523 retval(4)= (double) nc; |
|
524 return retval; |
|
525 } |
|
526 |
|
527 static octave_value_list |
|
528 sparse_find (const SparseBoolMatrix& v) |
|
529 { |
|
530 octave_value_list retval; |
5591
|
531 octave_idx_type nnz = v.nonzero (); |
5164
|
532 dim_vector dv = v.dims (); |
5275
|
533 octave_idx_type nr = dv(0); |
|
534 octave_idx_type nc = dv (1); |
5164
|
535 |
|
536 ColumnVector I (nnz), J (nnz); |
|
537 ColumnVector S (nnz); |
|
538 |
5275
|
539 for (octave_idx_type i = 0, cx = 0; i < nc; i++) |
5164
|
540 { |
|
541 OCTAVE_QUIT; |
5275
|
542 for (octave_idx_type j = v.cidx(i); j < v.cidx(i+1); j++ ) |
5164
|
543 { |
|
544 I (cx) = static_cast<double> (v.ridx(j) + 1); |
|
545 J (cx) = static_cast<double> (i + 1); |
|
546 S (cx) = static_cast<double> (v.data(j)); |
|
547 cx++; |
|
548 } |
|
549 } |
|
550 |
|
551 if (dv(0) == 1) |
|
552 { |
|
553 retval(0)= I.transpose (); |
|
554 retval(1)= J.transpose (); |
|
555 retval(2)= S.transpose (); |
|
556 } |
|
557 else |
|
558 { |
|
559 retval(0)= I; |
|
560 retval(1)= J; |
|
561 retval(2)= S; |
|
562 } |
|
563 retval(3)= (double) nr; |
|
564 retval(4)= (double) nc; |
|
565 return retval; |
|
566 } |
|
567 |
5534
|
568 // PKG_ADD: dispatch ("find", "spfind", "sparse matrix"); |
|
569 // PKG_ADD: dispatch ("find", "spfind", "sparse complex matrix"); |
|
570 // PKG_ADD: dispatch ("find", "spfind", "sparse bool matrix"); |
5164
|
571 DEFUN_DLD (spfind, args, nargout , |
|
572 "-*- texinfo -*-\n\ |
|
573 @deftypefn {Loadable Function} {[...] =} spfind (...)\n\ |
|
574 SPFIND: a sparse version of the find operator\n\ |
|
575 @enumerate\n\ |
|
576 @item\n\ |
|
577 @var{x }= spfind( @var{a })\n\ |
|
578 @itemize @w\n\ |
|
579 is analagous to @var{x}= find(@var{A}(:))@*\n\ |
|
580 where @var{A}= full(@var{a})\n\ |
|
581 @end itemize\n\ |
|
582 @item\n\ |
|
583 [@var{i},@var{j},@var{v},@var{nr},@var{nc}] = spfind( @var{a} )\n\ |
|
584 @itemize @w\n\ |
|
585 returns column vectors @var{i},@var{j},@var{v} such that@*\n\ |
|
586 @var{a}= sparse(@var{i},@var{j},@var{v},@var{nr},@var{nc})\n\ |
|
587 @end itemize\n\ |
|
588 @end enumerate\n\ |
|
589 @seealso{sparse}\n\ |
|
590 @end deftypefn") |
|
591 { |
|
592 octave_value_list retval; |
|
593 int nargin = args.length (); |
|
594 |
|
595 if (nargin != 1) |
|
596 { |
|
597 print_usage ("spfind"); |
|
598 return retval; |
|
599 } |
|
600 |
|
601 |
|
602 octave_value arg = args(0); |
|
603 |
|
604 if (arg.class_name () == "sparse") |
|
605 { |
|
606 if (arg.type_name () == "sparse matrix") |
|
607 retval = sparse_find (args(0).sparse_matrix_value ()); |
|
608 else if (arg.type_name () == "sparse complex matrix" ) |
|
609 retval = sparse_find (args(0).sparse_complex_matrix_value ()); |
|
610 else if (arg.type_name () == "sparse bool matrix" ) |
|
611 retval = sparse_find (args(0).sparse_bool_matrix_value ()); |
|
612 else |
|
613 gripe_wrong_type_arg ("spfind", arg); |
|
614 } |
|
615 else |
|
616 gripe_wrong_type_arg ("spfind", arg); |
|
617 |
|
618 if (nargout == 1 || nargout ==0 ) |
|
619 { |
|
620 // only find location as fortran index |
|
621 octave_value_list tmp; |
|
622 tmp(0) = retval(0) + (retval(1)-1)*retval(3); |
|
623 retval = tmp; |
|
624 } |
|
625 |
|
626 return retval; |
|
627 } |
|
628 |
|
629 #define SPARSE_DIM_ARG_BODY(NAME, FUNC) \ |
|
630 int nargin = args.length(); \ |
|
631 octave_value retval; \ |
|
632 if ((nargin != 1 ) && (nargin != 2)) \ |
|
633 print_usage (#NAME); \ |
|
634 else { \ |
|
635 int dim = (nargin == 1 ? -1 : args(1).int_value(true) - 1); \ |
|
636 if (error_state) return retval; \ |
|
637 if (dim < -1 || dim > 1) { \ |
|
638 error (#NAME ": invalid dimension argument = %d", dim + 1); \ |
|
639 return retval; \ |
|
640 } \ |
|
641 if (args(0).type_id () == \ |
|
642 octave_sparse_matrix::static_type_id () || args(0).type_id () == \ |
|
643 octave_sparse_bool_matrix::static_type_id ()) { \ |
|
644 retval = args(0).sparse_matrix_value () .FUNC (dim); \ |
|
645 } else if (args(0).type_id () == \ |
|
646 octave_sparse_complex_matrix::static_type_id ()) { \ |
|
647 retval = args(0).sparse_complex_matrix_value () .FUNC (dim); \ |
|
648 } else \ |
|
649 print_usage (#NAME); \ |
|
650 } \ |
|
651 return retval |
|
652 |
|
653 // PKG_ADD: dispatch ("prod", "spprod", "sparse matrix"); |
|
654 // PKG_ADD: dispatch ("prod", "spprod", "sparse complex matrix"); |
|
655 // PKG_ADD: dispatch ("prod", "spprod", "sparse bool matrix"); |
|
656 DEFUN_DLD (spprod, args, , |
|
657 "-*- texinfo -*-\n\ |
|
658 @deftypefn {Loadable Function} {@var{y} =} spprod (@var{x},@var{dim})\n\ |
|
659 Product of elements along dimension @var{dim}. If @var{dim} is omitted,\n\ |
|
660 it defaults to 1 (column-wise products).\n\ |
|
661 @end deftypefn\n\ |
|
662 @seealso{spsum, spsumsq}") |
|
663 { |
|
664 SPARSE_DIM_ARG_BODY (spprod, prod); |
|
665 } |
|
666 |
|
667 // PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse matrix"); |
|
668 // PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse complex matrix"); |
|
669 // PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse bool matrix"); |
|
670 DEFUN_DLD (spcumprod, args, , |
|
671 "-*- texinfo -*-\n\ |
|
672 @deftypefn {Loadable Function} {@var{y} =} spcumprod (@var{x},@var{dim})\n\ |
|
673 Cumulative product of elements along dimension @var{dim}. If @var{dim}\n\ |
|
674 is omitted, it defaults to 1 (column-wise cumulative products).\n\ |
|
675 @end deftypefn\n\ |
|
676 @seealso{spcumsum}") |
|
677 { |
|
678 SPARSE_DIM_ARG_BODY (spcumprod, cumprod); |
|
679 } |
|
680 |
|
681 // PKG_ADD: dispatch ("sum", "spsum", "sparse matrix"); |
|
682 // PKG_ADD: dispatch ("sum", "spsum", "sparse complex matrix"); |
|
683 // PKG_ADD: dispatch ("sum", "spsum", "sparse bool matrix"); |
|
684 DEFUN_DLD (spsum, args, , |
|
685 "-*- texinfo -*-\n\ |
|
686 @deftypefn {Loadable Function} {@var{y} =} spsum (@var{x},@var{dim})\n\ |
|
687 Sum of elements along dimension @var{dim}. If @var{dim} is omitted, it\n\ |
|
688 defaults to 1 (column-wise sum).\n\ |
|
689 @end deftypefn\n\ |
|
690 @seealso{spprod, spsumsq}") |
|
691 { |
|
692 SPARSE_DIM_ARG_BODY (spsum, sum); |
|
693 } |
|
694 |
|
695 // PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse matrix"); |
|
696 // PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse complex matrix"); |
|
697 // PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse bool matrix"); |
|
698 DEFUN_DLD (spcumsum, args, , |
|
699 "-*- texinfo -*-\n\ |
|
700 @deftypefn {Loadable Function} {@var{y} =} spcumsum (@var{x},@var{dim})\n\ |
|
701 Cumulative sum of elements along dimension @var{dim}. If @var{dim}\n\ |
|
702 is omitted, it defaults to 1 (column-wise cumulative sums).\n\ |
|
703 @end deftypefn\n\ |
|
704 @seealso{spcumprod}") |
|
705 { |
|
706 SPARSE_DIM_ARG_BODY (spcumsum, cumsum); |
|
707 } |
|
708 |
|
709 // PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse matrix"); |
|
710 // PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse complex matrix"); |
|
711 // PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse bool matrix"); |
|
712 DEFUN_DLD (spsumsq, args, , |
|
713 "-*- texinfo -*-\n\ |
|
714 @deftypefn {Loadable Function} {@var{y} =} spsumsq (@var{x},@var{dim})\n\ |
|
715 Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ |
|
716 is omitted, it defaults to 1 (column-wise sum of squares).\n\ |
|
717 This function is equivalent to computing\n\ |
|
718 @example\n\ |
|
719 spsum (x .* spconj (x), dim)\n\ |
|
720 @end example\n\ |
|
721 but it uses less memory and avoids calling @code{spconj} if @var{x} is\n\ |
|
722 real.\n\ |
|
723 @end deftypefn\n\ |
|
724 @seealso{spprod, spsum}") |
|
725 { |
|
726 SPARSE_DIM_ARG_BODY (spsumsq, sumsq); |
|
727 } |
|
728 |
|
729 #define MINMAX_BODY(FCN) \ |
|
730 \ |
|
731 octave_value_list retval; \ |
|
732 \ |
|
733 int nargin = args.length (); \ |
|
734 \ |
|
735 if (nargin < 1 || nargin > 3 || nargout > 2) \ |
|
736 { \ |
|
737 print_usage (#FCN); \ |
|
738 return retval; \ |
|
739 } \ |
|
740 \ |
|
741 octave_value arg1; \ |
|
742 octave_value arg2; \ |
|
743 octave_value arg3; \ |
|
744 \ |
|
745 switch (nargin) \ |
|
746 { \ |
|
747 case 3: \ |
|
748 arg3 = args(2); \ |
|
749 \ |
|
750 case 2: \ |
|
751 arg2 = args(1); \ |
|
752 \ |
|
753 case 1: \ |
|
754 arg1 = args(0); \ |
|
755 break; \ |
|
756 \ |
|
757 default: \ |
|
758 panic_impossible (); \ |
|
759 break; \ |
|
760 } \ |
|
761 \ |
|
762 int dim; \ |
|
763 dim_vector dv = ((const octave_sparse_matrix&) arg1) .dims (); \ |
|
764 if (error_state) \ |
|
765 { \ |
|
766 gripe_wrong_type_arg (#FCN, arg1); \ |
|
767 return retval; \ |
|
768 } \ |
|
769 \ |
|
770 if (nargin == 3) \ |
|
771 { \ |
|
772 dim = arg3.nint_value () - 1; \ |
|
773 if (dim < 0 || dim >= dv.length ()) \ |
|
774 { \ |
|
775 error ("%s: invalid dimension", #FCN); \ |
|
776 return retval; \ |
|
777 } \ |
|
778 } \ |
|
779 else \ |
|
780 { \ |
|
781 dim = 0; \ |
|
782 while ((dim < dv.length ()) && (dv (dim) <= 1)) \ |
|
783 dim++; \ |
|
784 if (dim == dv.length ()) \ |
|
785 dim = 0; \ |
|
786 } \ |
|
787 \ |
|
788 bool single_arg = (nargin == 1) || arg2.is_empty(); \ |
|
789 \ |
|
790 if (single_arg && (nargout == 1 || nargout == 0)) \ |
|
791 { \ |
|
792 if (arg1.type_id () == octave_sparse_matrix::static_type_id ()) \ |
|
793 retval(0) = arg1.sparse_matrix_value () .FCN (dim); \ |
|
794 else if (arg1.type_id () == \ |
|
795 octave_sparse_complex_matrix::static_type_id ()) \ |
|
796 retval(0) = arg1.sparse_complex_matrix_value () .FCN (dim); \ |
|
797 else \ |
|
798 gripe_wrong_type_arg (#FCN, arg1); \ |
|
799 } \ |
|
800 else if (single_arg && nargout == 2) \ |
|
801 { \ |
5275
|
802 Array2<octave_idx_type> index; \ |
5164
|
803 \ |
|
804 if (arg1.type_id () == octave_sparse_matrix::static_type_id ()) \ |
|
805 retval(0) = arg1.sparse_matrix_value () .FCN (index, dim); \ |
|
806 else if (arg1.type_id () == \ |
|
807 octave_sparse_complex_matrix::static_type_id ()) \ |
|
808 retval(0) = arg1.sparse_complex_matrix_value () .FCN (index, dim); \ |
|
809 else \ |
|
810 gripe_wrong_type_arg (#FCN, arg1); \ |
|
811 \ |
5275
|
812 octave_idx_type len = index.numel (); \ |
5164
|
813 \ |
|
814 if (len > 0) \ |
|
815 { \ |
|
816 double nan_val = lo_ieee_nan_value (); \ |
|
817 \ |
|
818 NDArray idx (index.dims ()); \ |
|
819 \ |
5275
|
820 for (octave_idx_type i = 0; i < len; i++) \ |
5164
|
821 { \ |
|
822 OCTAVE_QUIT; \ |
5275
|
823 octave_idx_type tmp = index.elem (i) + 1; \ |
5164
|
824 idx.elem (i) = (tmp <= 0) \ |
|
825 ? nan_val : static_cast<double> (tmp); \ |
|
826 } \ |
|
827 \ |
|
828 retval(1) = idx; \ |
|
829 } \ |
|
830 else \ |
|
831 retval(1) = NDArray (); \ |
|
832 } \ |
|
833 else \ |
|
834 { \ |
|
835 int arg1_is_scalar = arg1.is_scalar_type (); \ |
|
836 int arg2_is_scalar = arg2.is_scalar_type (); \ |
|
837 \ |
|
838 int arg1_is_complex = arg1.is_complex_type (); \ |
|
839 int arg2_is_complex = arg2.is_complex_type (); \ |
|
840 \ |
|
841 if (arg1_is_scalar) \ |
|
842 { \ |
|
843 if (arg1_is_complex || arg2_is_complex) \ |
|
844 { \ |
|
845 Complex c1 = arg1.complex_value (); \ |
|
846 \ |
|
847 SparseComplexMatrix m2 = arg2.sparse_complex_matrix_value (); \ |
|
848 \ |
|
849 if (! error_state) \ |
|
850 { \ |
|
851 SparseComplexMatrix result = FCN (c1, m2); \ |
|
852 if (! error_state) \ |
|
853 retval(0) = result; \ |
|
854 } \ |
|
855 } \ |
|
856 else \ |
|
857 { \ |
|
858 double d1 = arg1.double_value (); \ |
|
859 SparseMatrix m2 = arg2.sparse_matrix_value (); \ |
|
860 \ |
|
861 if (! error_state) \ |
|
862 { \ |
|
863 SparseMatrix result = FCN (d1, m2); \ |
|
864 if (! error_state) \ |
|
865 retval(0) = result; \ |
|
866 } \ |
|
867 } \ |
|
868 } \ |
|
869 else if (arg2_is_scalar) \ |
|
870 { \ |
|
871 if (arg1_is_complex || arg2_is_complex) \ |
|
872 { \ |
|
873 SparseComplexMatrix m1 = arg1.sparse_complex_matrix_value (); \ |
|
874 \ |
|
875 if (! error_state) \ |
|
876 { \ |
|
877 Complex c2 = arg2.complex_value (); \ |
|
878 SparseComplexMatrix result = FCN (m1, c2); \ |
|
879 if (! error_state) \ |
|
880 retval(0) = result; \ |
|
881 } \ |
|
882 } \ |
|
883 else \ |
|
884 { \ |
|
885 SparseMatrix m1 = arg1.sparse_matrix_value (); \ |
|
886 \ |
|
887 if (! error_state) \ |
|
888 { \ |
|
889 double d2 = arg2.double_value (); \ |
|
890 SparseMatrix result = FCN (m1, d2); \ |
|
891 if (! error_state) \ |
|
892 retval(0) = result; \ |
|
893 } \ |
|
894 } \ |
|
895 } \ |
|
896 else \ |
|
897 { \ |
|
898 if (arg1_is_complex || arg2_is_complex) \ |
|
899 { \ |
|
900 SparseComplexMatrix m1 = arg1.sparse_complex_matrix_value (); \ |
|
901 \ |
|
902 if (! error_state) \ |
|
903 { \ |
|
904 SparseComplexMatrix m2 = arg2.sparse_complex_matrix_value (); \ |
|
905 \ |
|
906 if (! error_state) \ |
|
907 { \ |
|
908 SparseComplexMatrix result = FCN (m1, m2); \ |
|
909 if (! error_state) \ |
|
910 retval(0) = result; \ |
|
911 } \ |
|
912 } \ |
|
913 } \ |
|
914 else \ |
|
915 { \ |
|
916 SparseMatrix m1 = arg1.sparse_matrix_value (); \ |
|
917 \ |
|
918 if (! error_state) \ |
|
919 { \ |
|
920 SparseMatrix m2 = arg2.sparse_matrix_value (); \ |
|
921 \ |
|
922 if (! error_state) \ |
|
923 { \ |
|
924 SparseMatrix result = FCN (m1, m2); \ |
|
925 if (! error_state) \ |
|
926 retval(0) = result; \ |
|
927 } \ |
|
928 } \ |
|
929 } \ |
|
930 } \ |
|
931 } \ |
|
932 \ |
|
933 return retval |
|
934 |
|
935 // PKG_ADD: dispatch ("min", "spmin", "sparse matrix"); |
|
936 // PKG_ADD: dispatch ("min", "spmin", "sparse complex matrix"); |
|
937 // PKG_ADD: dispatch ("min", "spmin", "sparse bool matrix"); |
|
938 DEFUN_DLD (spmin, args, nargout, |
|
939 "-*- texinfo -*-\n\ |
|
940 @deftypefn {Mapping Function} {} spmin (@var{x}, @var{y}, @var{dim})\n\ |
|
941 @deftypefnx {Mapping Function} {[@var{w}, @var{iw}] =} spmin (@var{x})\n\ |
|
942 @cindex Utility Functions\n\ |
|
943 For a vector argument, return the minimum value. For a matrix\n\ |
|
944 argument, return the minimum value from each column, as a row\n\ |
|
945 vector, or over the dimension @var{dim} if defined. For two matrices\n\ |
|
946 (or a matrix and scalar), return the pair-wise minimum.\n\ |
|
947 Thus,\n\ |
|
948 \n\ |
|
949 @example\n\ |
|
950 min (min (@var{x}))\n\ |
|
951 @end example\n\ |
|
952 \n\ |
|
953 @noindent\n\ |
|
954 returns the smallest element of @var{x}, and\n\ |
|
955 \n\ |
|
956 @example\n\ |
|
957 @group\n\ |
|
958 min (2:5, pi)\n\ |
|
959 @result{} 2.0000 3.0000 3.1416 3.1416\n\ |
|
960 @end group\n\ |
|
961 @end example\n\ |
|
962 @noindent\n\ |
|
963 compares each element of the range @code{2:5} with @code{pi}, and\n\ |
|
964 returns a row vector of the minimum values.\n\ |
|
965 \n\ |
|
966 For complex arguments, the magnitude of the elements are used for\n\ |
|
967 comparison.\n\ |
|
968 \n\ |
|
969 If called with one input and two output arguments,\n\ |
|
970 @code{min} also returns the first index of the\n\ |
|
971 minimum value(s). Thus,\n\ |
|
972 \n\ |
|
973 @example\n\ |
|
974 @group\n\ |
|
975 [x, ix] = min ([1, 3, 0, 2, 5])\n\ |
|
976 @result{} x = 0\n\ |
|
977 ix = 3\n\ |
|
978 @end group\n\ |
|
979 @end example\n\ |
|
980 @end deftypefn") |
|
981 { |
|
982 MINMAX_BODY (min); |
|
983 } |
|
984 |
|
985 // PKG_ADD: dispatch ("max", "spmax", "sparse matrix"); |
|
986 // PKG_ADD: dispatch ("max", "spmax", "sparse complex matrix"); |
|
987 // PKG_ADD: dispatch ("max", "spmax", "sparse bool matrix"); |
|
988 DEFUN_DLD (spmax, args, nargout, |
|
989 "-*- texinfo -*-\n\ |
|
990 @deftypefn {Mapping Function} {} spmax (@var{x}, @var{y}, @var{dim})\n\ |
|
991 @deftypefnx {Mapping Function} {[@var{w}, @var{iw}] =} spmax (@var{x})\n\ |
|
992 @cindex Utility Functions\n\ |
|
993 For a vector argument, return the maximum value. For a matrix\n\ |
|
994 argument, return the maximum value from each column, as a row\n\ |
|
995 vector, or over the dimension @var{dim} if defined. For two matrices\n\ |
|
996 (or a matrix and scalar), return the pair-wise maximum.\n\ |
|
997 Thus,\n\ |
|
998 \n\ |
|
999 @example\n\ |
|
1000 max (max (@var{x}))\n\ |
|
1001 @end example\n\ |
|
1002 \n\ |
|
1003 @noindent\n\ |
|
1004 returns the largest element of @var{x}, and\n\ |
|
1005 \n\ |
|
1006 @example\n\ |
|
1007 @group\n\ |
|
1008 max (2:5, pi)\n\ |
|
1009 @result{} 3.1416 3.1416 4.0000 5.0000\n\ |
|
1010 @end group\n\ |
|
1011 @end example\n\ |
|
1012 @noindent\n\ |
|
1013 compares each element of the range @code{2:5} with @code{pi}, and\n\ |
|
1014 returns a row vector of the maximum values.\n\ |
|
1015 \n\ |
|
1016 For complex arguments, the magnitude of the elements are used for\n\ |
|
1017 comparison.\n\ |
|
1018 \n\ |
|
1019 If called with one input and two output arguments,\n\ |
|
1020 @code{max} also returns the first index of the\n\ |
|
1021 maximum value(s). Thus,\n\ |
|
1022 \n\ |
|
1023 @example\n\ |
|
1024 @group\n\ |
|
1025 [x, ix] = max ([1, 3, 5, 2, 5])\n\ |
|
1026 @result{} x = 5\n\ |
|
1027 ix = 3\n\ |
|
1028 @end group\n\ |
|
1029 @end example\n\ |
|
1030 @end deftypefn") |
|
1031 { |
|
1032 MINMAX_BODY (max); |
|
1033 } |
|
1034 |
|
1035 // PKG_ADD: dispatch ("atan2", "spatan2", "sparse matrix"); |
|
1036 // PKG_ADD: dispatch ("atan2", "spatan2", "sparse complex matrix"); |
|
1037 // PKG_ADD: dispatch ("atan2", "spatan2", "sparse bool matrix"); |
|
1038 DEFUN_DLD (spatan2, args, , |
|
1039 "-*- texinfo -*-\n\ |
|
1040 @deftypefn {Loadable Function} {} spatan2 (@var{y}, @var{x})\n\ |
|
1041 Compute atan (Y / X) for corresponding sparse matrix elements of Y and X.\n\ |
|
1042 The result is in range -pi to pi.\n\ |
|
1043 @end deftypefn\n") |
|
1044 { |
|
1045 octave_value retval; |
|
1046 int nargin = args.length (); |
|
1047 if (nargin == 2) { |
|
1048 SparseMatrix a, b; |
|
1049 double da, db; |
|
1050 bool is_double_a = false; |
|
1051 bool is_double_b = false; |
|
1052 |
|
1053 if (args(0).is_scalar_type ()) |
|
1054 { |
|
1055 is_double_a = true; |
|
1056 da = args(0).double_value(); |
|
1057 } |
|
1058 else |
|
1059 a = args(0).sparse_matrix_value (); |
|
1060 |
|
1061 if (args(1).is_scalar_type ()) |
|
1062 { |
|
1063 is_double_b = true; |
|
1064 db = args(1).double_value(); |
|
1065 } |
|
1066 else |
|
1067 b = args(1).sparse_matrix_value (); |
|
1068 |
|
1069 if (is_double_a && is_double_b) |
|
1070 retval = Matrix (1, 1, atan2(da, db)); |
|
1071 else if (is_double_a) |
|
1072 retval = atan2 (da, b); |
|
1073 else if (is_double_b) |
|
1074 retval = atan2 (a, db); |
|
1075 else |
|
1076 retval = atan2 (a, b); |
|
1077 |
|
1078 } else |
|
1079 print_usage("spatan2"); |
|
1080 |
|
1081 return retval; |
|
1082 } |
|
1083 |
|
1084 static octave_value |
|
1085 make_spdiag (const octave_value& a, const octave_value& b) |
|
1086 { |
|
1087 octave_value retval; |
|
1088 |
|
1089 if (a.is_complex_type ()) |
|
1090 { |
|
1091 SparseComplexMatrix m = a.sparse_complex_matrix_value (); |
5275
|
1092 octave_idx_type k = b.nint_value(true); |
5164
|
1093 |
|
1094 if (error_state) |
|
1095 return retval; |
|
1096 |
5275
|
1097 octave_idx_type nr = m.rows (); |
|
1098 octave_idx_type nc = m.columns (); |
5164
|
1099 |
|
1100 if (nr == 0 || nc == 0) |
|
1101 retval = m; |
|
1102 else if (nr == 1 || nc == 1) |
|
1103 { |
5275
|
1104 octave_idx_type roff = 0; |
|
1105 octave_idx_type coff = 0; |
5164
|
1106 if (k > 0) |
|
1107 { |
|
1108 roff = 0; |
|
1109 coff = k; |
|
1110 } |
|
1111 else if (k < 0) |
|
1112 { |
|
1113 k = -k; |
|
1114 roff = k; |
|
1115 coff = 0; |
|
1116 } |
|
1117 |
|
1118 if (nr == 1) |
|
1119 { |
5275
|
1120 octave_idx_type n = nc + k; |
|
1121 octave_idx_type nz = m.nnz (); |
5164
|
1122 SparseComplexMatrix r (n, n, nz); |
5275
|
1123 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
1124 r.xcidx (i) = 0; |
5275
|
1125 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
1126 { |
5275
|
1127 for (octave_idx_type i = m.cidx(j); i < m.cidx(j+1); i++) |
5164
|
1128 { |
|
1129 r.xdata (i) = m.data (i); |
|
1130 r.xridx (i) = j + roff; |
|
1131 } |
|
1132 r.xcidx (j+coff+1) = m.cidx(j+1); |
|
1133 } |
5275
|
1134 for (octave_idx_type i = nc+coff+1; i < n+1; i++) |
5164
|
1135 r.xcidx (i) = nz; |
|
1136 retval = r; |
|
1137 } |
|
1138 else |
|
1139 { |
5275
|
1140 octave_idx_type n = nr + k; |
|
1141 octave_idx_type nz = m.nnz (); |
|
1142 octave_idx_type ii = 0; |
|
1143 octave_idx_type ir = m.ridx(0); |
5164
|
1144 SparseComplexMatrix r (n, n, nz); |
5275
|
1145 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
1146 r.xcidx (i) = 0; |
5275
|
1147 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
1148 { |
|
1149 if (ir == i) |
|
1150 { |
|
1151 r.xdata (ii) = m.data (ii); |
|
1152 r.xridx (ii++) = ir + roff; |
|
1153 if (ii != nz) |
|
1154 ir = m.ridx (ii); |
|
1155 } |
|
1156 r.xcidx (i+coff+1) = ii; |
|
1157 } |
5275
|
1158 for (octave_idx_type i = nr+coff+1; i < n+1; i++) |
5164
|
1159 r.xcidx (i) = nz; |
|
1160 retval = r; |
|
1161 } |
|
1162 } |
|
1163 else |
|
1164 { |
|
1165 SparseComplexMatrix r = m.diag (k); |
|
1166 // Don't use numel, since it can overflow for very large matrices |
|
1167 if (r.rows () > 0 && r.cols () > 0) |
|
1168 retval = r; |
|
1169 } |
|
1170 } |
|
1171 else if (a.is_real_type ()) |
|
1172 { |
|
1173 SparseMatrix m = a.sparse_matrix_value (); |
|
1174 |
5275
|
1175 octave_idx_type k = b.nint_value(true); |
5164
|
1176 |
|
1177 if (error_state) |
|
1178 return retval; |
|
1179 |
5275
|
1180 octave_idx_type nr = m.rows (); |
|
1181 octave_idx_type nc = m.columns (); |
5164
|
1182 |
|
1183 if (nr == 0 || nc == 0) |
|
1184 retval = m; |
|
1185 else if (nr == 1 || nc == 1) |
|
1186 { |
5275
|
1187 octave_idx_type roff = 0; |
|
1188 octave_idx_type coff = 0; |
5164
|
1189 if (k > 0) |
|
1190 { |
|
1191 roff = 0; |
|
1192 coff = k; |
|
1193 } |
|
1194 else if (k < 0) |
|
1195 { |
|
1196 k = -k; |
|
1197 roff = k; |
|
1198 coff = 0; |
|
1199 } |
|
1200 |
|
1201 if (nr == 1) |
|
1202 { |
5275
|
1203 octave_idx_type n = nc + k; |
|
1204 octave_idx_type nz = m.nnz (); |
5164
|
1205 SparseMatrix r (n, n, nz); |
|
1206 |
5275
|
1207 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
1208 r.xcidx (i) = 0; |
5275
|
1209 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
1210 { |
5275
|
1211 for (octave_idx_type i = m.cidx(j); i < m.cidx(j+1); i++) |
5164
|
1212 { |
|
1213 r.xdata (i) = m.data (i); |
|
1214 r.xridx (i) = j + roff; |
|
1215 } |
|
1216 r.xcidx (j+coff+1) = m.cidx(j+1); |
|
1217 } |
5275
|
1218 for (octave_idx_type i = nc+coff+1; i < n+1; i++) |
5164
|
1219 r.xcidx (i) = nz; |
|
1220 retval = r; |
|
1221 } |
|
1222 else |
|
1223 { |
5275
|
1224 octave_idx_type n = nr + k; |
|
1225 octave_idx_type nz = m.nnz (); |
|
1226 octave_idx_type ii = 0; |
|
1227 octave_idx_type ir = m.ridx(0); |
5164
|
1228 SparseMatrix r (n, n, nz); |
5275
|
1229 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
1230 r.xcidx (i) = 0; |
5275
|
1231 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
1232 { |
|
1233 if (ir == i) |
|
1234 { |
|
1235 r.xdata (ii) = m.data (ii); |
|
1236 r.xridx (ii++) = ir + roff; |
|
1237 if (ii != nz) |
|
1238 ir = m.ridx (ii); |
|
1239 } |
|
1240 r.xcidx (i+coff+1) = ii; |
|
1241 } |
5275
|
1242 for (octave_idx_type i = nr+coff+1; i < n+1; i++) |
5164
|
1243 r.xcidx (i) = nz; |
|
1244 retval = r; |
|
1245 } |
|
1246 } |
|
1247 else |
|
1248 { |
|
1249 SparseMatrix r = m.diag (k); |
|
1250 if (r.rows () > 0 && r.cols () > 0) |
|
1251 retval = r; |
|
1252 } |
|
1253 } |
|
1254 else |
|
1255 gripe_wrong_type_arg ("spdiag", a); |
|
1256 |
|
1257 return retval; |
|
1258 } |
|
1259 |
|
1260 // PKG_ADD: dispatch ("diag", "spdiag", "sparse matrix"); |
|
1261 // PKG_ADD: dispatch ("diag", "spdiag", "sparse complex matrix"); |
|
1262 // PKG_ADD: dispatch ("diag", "spdiag", "sparse bool matrix"); |
|
1263 DEFUN_DLD (spdiag, args, , |
|
1264 "-*- texinfo -*-\n\ |
|
1265 @deftypefn {Loadable Function} {} spdiag (@var{v}, @var{k})\n\ |
|
1266 Return a diagonal matrix with the sparse vector @var{v} on diagonal\n\ |
|
1267 @var{k}. The second argument is optional. If it is positive, the vector is\n\ |
|
1268 placed on the @var{k}-th super-diagonal. If it is negative, it is placed\n\ |
|
1269 on the @var{-k}-th sub-diagonal. The default value of @var{k} is 0, and\n\ |
|
1270 the vector is placed on the main diagonal. For example,\n\ |
|
1271 \n\ |
|
1272 @example\n\ |
|
1273 spdiag ([1, 2, 3], 1)\n\ |
|
1274 ans =\n\ |
|
1275 \n\ |
|
1276 Compressed Column Sparse (rows=4, cols=4, nnz=3)\n\ |
|
1277 (1 , 2) -> 1\n\ |
|
1278 (2 , 3) -> 2\n\ |
|
1279 (3 , 4) -> 3\n\ |
|
1280 @end example\n\ |
|
1281 \n\ |
|
1282 @end deftypefn\n\ |
|
1283 @seealso{diag}") |
|
1284 { |
|
1285 octave_value retval; |
|
1286 |
|
1287 int nargin = args.length (); |
|
1288 |
|
1289 if (nargin == 1 && args(0).is_defined ()) |
|
1290 retval = make_spdiag (args(0), octave_value(0.)); |
|
1291 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
|
1292 retval = make_spdiag (args(0), args(1)); |
|
1293 else |
|
1294 print_usage ("spdiag"); |
|
1295 |
|
1296 return retval; |
|
1297 } |
|
1298 |
|
1299 /* |
|
1300 ;;; Local Variables: *** |
|
1301 ;;; mode: C++ *** |
|
1302 ;;; End: *** |
|
1303 */ |