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