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 { |
|
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 |
5631
|
388 if (args(0).is_sparse_type ()) |
5164
|
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 static octave_value_list |
|
408 sparse_find (const SparseMatrix& v) |
|
409 { |
|
410 octave_value_list retval; |
5604
|
411 octave_idx_type nnz = v.nnz (); |
5164
|
412 dim_vector dv = v.dims (); |
5275
|
413 octave_idx_type nr = dv(0); |
|
414 octave_idx_type nc = dv (1); |
5164
|
415 |
|
416 ColumnVector I (nnz), J (nnz); |
|
417 ColumnVector S (nnz); |
|
418 |
5275
|
419 for (octave_idx_type i = 0, cx = 0; i < nc; i++) |
5164
|
420 { |
|
421 OCTAVE_QUIT; |
5275
|
422 for (octave_idx_type j = v.cidx(i); j < v.cidx(i+1); j++ ) |
5164
|
423 { |
|
424 I (cx) = static_cast<double> (v.ridx(j) + 1); |
|
425 J (cx) = static_cast<double> (i + 1); |
|
426 S (cx) = v.data(j); |
|
427 cx++; |
|
428 } |
|
429 } |
|
430 |
|
431 if (dv(0) == 1) |
|
432 { |
|
433 retval(0)= I.transpose (); |
|
434 retval(1)= J.transpose (); |
|
435 retval(2)= S.transpose (); |
|
436 } |
|
437 else |
|
438 { |
|
439 retval(0)= I; |
|
440 retval(1)= J; |
|
441 retval(2)= S; |
|
442 } |
|
443 retval(3)= (double) nr; |
|
444 retval(4)= (double) nc; |
|
445 return retval; |
|
446 } |
|
447 |
|
448 static octave_value_list |
|
449 sparse_find (const SparseComplexMatrix& v) |
|
450 { |
|
451 octave_value_list retval; |
5604
|
452 octave_idx_type nnz = v.nnz (); |
5164
|
453 dim_vector dv = v.dims (); |
5275
|
454 octave_idx_type nr = dv(0); |
|
455 octave_idx_type nc = dv (1); |
5164
|
456 |
|
457 ColumnVector I (nnz), J (nnz); |
|
458 ComplexColumnVector S (nnz); |
|
459 |
5275
|
460 for (octave_idx_type i = 0, cx = 0; i < nc; i++) |
5164
|
461 { |
|
462 OCTAVE_QUIT; |
5275
|
463 for (octave_idx_type j = v.cidx(i); j < v.cidx(i+1); j++ ) |
5164
|
464 { |
|
465 I (cx) = static_cast<double> (v.ridx(j) + 1); |
|
466 J (cx) = static_cast<double> (i + 1); |
|
467 S (cx) = v.data(j); |
|
468 cx++; |
|
469 } |
|
470 } |
|
471 |
|
472 if (dv(0) == 1) |
|
473 { |
|
474 retval(0)= I.transpose (); |
|
475 retval(1)= J.transpose (); |
|
476 retval(2)= S.transpose (); |
|
477 } |
|
478 else |
|
479 { |
|
480 retval(0)= I; |
|
481 retval(1)= J; |
|
482 retval(2)= S; |
|
483 } |
|
484 retval(3)= (double) nr; |
|
485 retval(4)= (double) nc; |
|
486 return retval; |
|
487 } |
|
488 |
|
489 static octave_value_list |
|
490 sparse_find (const SparseBoolMatrix& v) |
|
491 { |
|
492 octave_value_list retval; |
5604
|
493 octave_idx_type nnz = v.nnz (); |
5164
|
494 dim_vector dv = v.dims (); |
5275
|
495 octave_idx_type nr = dv(0); |
|
496 octave_idx_type nc = dv (1); |
5164
|
497 |
|
498 ColumnVector I (nnz), J (nnz); |
|
499 ColumnVector S (nnz); |
|
500 |
5275
|
501 for (octave_idx_type i = 0, cx = 0; i < nc; i++) |
5164
|
502 { |
|
503 OCTAVE_QUIT; |
5275
|
504 for (octave_idx_type j = v.cidx(i); j < v.cidx(i+1); j++ ) |
5164
|
505 { |
|
506 I (cx) = static_cast<double> (v.ridx(j) + 1); |
|
507 J (cx) = static_cast<double> (i + 1); |
|
508 S (cx) = static_cast<double> (v.data(j)); |
|
509 cx++; |
|
510 } |
|
511 } |
|
512 |
|
513 if (dv(0) == 1) |
|
514 { |
|
515 retval(0)= I.transpose (); |
|
516 retval(1)= J.transpose (); |
|
517 retval(2)= S.transpose (); |
|
518 } |
|
519 else |
|
520 { |
|
521 retval(0)= I; |
|
522 retval(1)= J; |
|
523 retval(2)= S; |
|
524 } |
|
525 retval(3)= (double) nr; |
|
526 retval(4)= (double) nc; |
|
527 return retval; |
|
528 } |
|
529 |
5534
|
530 // PKG_ADD: dispatch ("find", "spfind", "sparse matrix"); |
|
531 // PKG_ADD: dispatch ("find", "spfind", "sparse complex matrix"); |
|
532 // PKG_ADD: dispatch ("find", "spfind", "sparse bool matrix"); |
5164
|
533 DEFUN_DLD (spfind, args, nargout , |
|
534 "-*- texinfo -*-\n\ |
|
535 @deftypefn {Loadable Function} {[...] =} spfind (...)\n\ |
|
536 SPFIND: a sparse version of the find operator\n\ |
|
537 @enumerate\n\ |
|
538 @item\n\ |
|
539 @var{x }= spfind( @var{a })\n\ |
|
540 @itemize @w\n\ |
|
541 is analagous to @var{x}= find(@var{A}(:))@*\n\ |
|
542 where @var{A}= full(@var{a})\n\ |
|
543 @end itemize\n\ |
|
544 @item\n\ |
|
545 [@var{i},@var{j},@var{v},@var{nr},@var{nc}] = spfind( @var{a} )\n\ |
|
546 @itemize @w\n\ |
|
547 returns column vectors @var{i},@var{j},@var{v} such that@*\n\ |
|
548 @var{a}= sparse(@var{i},@var{j},@var{v},@var{nr},@var{nc})\n\ |
|
549 @end itemize\n\ |
|
550 @end enumerate\n\ |
|
551 @seealso{sparse}\n\ |
|
552 @end deftypefn") |
|
553 { |
|
554 octave_value_list retval; |
|
555 int nargin = args.length (); |
|
556 |
|
557 if (nargin != 1) |
|
558 { |
|
559 print_usage ("spfind"); |
|
560 return retval; |
|
561 } |
|
562 |
|
563 |
|
564 octave_value arg = args(0); |
|
565 |
5631
|
566 if (arg.is_sparse_type ()) |
5164
|
567 { |
|
568 if (arg.type_name () == "sparse matrix") |
|
569 retval = sparse_find (args(0).sparse_matrix_value ()); |
|
570 else if (arg.type_name () == "sparse complex matrix" ) |
|
571 retval = sparse_find (args(0).sparse_complex_matrix_value ()); |
|
572 else if (arg.type_name () == "sparse bool matrix" ) |
|
573 retval = sparse_find (args(0).sparse_bool_matrix_value ()); |
|
574 else |
|
575 gripe_wrong_type_arg ("spfind", arg); |
|
576 } |
|
577 else |
|
578 gripe_wrong_type_arg ("spfind", arg); |
|
579 |
|
580 if (nargout == 1 || nargout ==0 ) |
|
581 { |
|
582 // only find location as fortran index |
|
583 octave_value_list tmp; |
|
584 tmp(0) = retval(0) + (retval(1)-1)*retval(3); |
|
585 retval = tmp; |
|
586 } |
|
587 |
|
588 return retval; |
|
589 } |
|
590 |
|
591 #define SPARSE_DIM_ARG_BODY(NAME, FUNC) \ |
|
592 int nargin = args.length(); \ |
|
593 octave_value retval; \ |
|
594 if ((nargin != 1 ) && (nargin != 2)) \ |
|
595 print_usage (#NAME); \ |
|
596 else { \ |
|
597 int dim = (nargin == 1 ? -1 : args(1).int_value(true) - 1); \ |
|
598 if (error_state) return retval; \ |
|
599 if (dim < -1 || dim > 1) { \ |
|
600 error (#NAME ": invalid dimension argument = %d", dim + 1); \ |
|
601 return retval; \ |
|
602 } \ |
|
603 if (args(0).type_id () == \ |
|
604 octave_sparse_matrix::static_type_id () || args(0).type_id () == \ |
|
605 octave_sparse_bool_matrix::static_type_id ()) { \ |
|
606 retval = args(0).sparse_matrix_value () .FUNC (dim); \ |
|
607 } else if (args(0).type_id () == \ |
|
608 octave_sparse_complex_matrix::static_type_id ()) { \ |
|
609 retval = args(0).sparse_complex_matrix_value () .FUNC (dim); \ |
|
610 } else \ |
|
611 print_usage (#NAME); \ |
|
612 } \ |
|
613 return retval |
|
614 |
|
615 // PKG_ADD: dispatch ("prod", "spprod", "sparse matrix"); |
|
616 // PKG_ADD: dispatch ("prod", "spprod", "sparse complex matrix"); |
|
617 // PKG_ADD: dispatch ("prod", "spprod", "sparse bool matrix"); |
|
618 DEFUN_DLD (spprod, args, , |
|
619 "-*- texinfo -*-\n\ |
|
620 @deftypefn {Loadable Function} {@var{y} =} spprod (@var{x},@var{dim})\n\ |
|
621 Product of elements along dimension @var{dim}. If @var{dim} is omitted,\n\ |
|
622 it defaults to 1 (column-wise products).\n\ |
5642
|
623 @seealso{spsum, spsumsq}\n\ |
|
624 @end deftypefn") |
5164
|
625 { |
|
626 SPARSE_DIM_ARG_BODY (spprod, prod); |
|
627 } |
|
628 |
|
629 // PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse matrix"); |
|
630 // PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse complex matrix"); |
|
631 // PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse bool matrix"); |
|
632 DEFUN_DLD (spcumprod, args, , |
|
633 "-*- texinfo -*-\n\ |
|
634 @deftypefn {Loadable Function} {@var{y} =} spcumprod (@var{x},@var{dim})\n\ |
|
635 Cumulative product of elements along dimension @var{dim}. If @var{dim}\n\ |
|
636 is omitted, it defaults to 1 (column-wise cumulative products).\n\ |
5642
|
637 @seealso{spcumsum}\n\ |
|
638 @end deftypefn") |
5164
|
639 { |
|
640 SPARSE_DIM_ARG_BODY (spcumprod, cumprod); |
|
641 } |
|
642 |
|
643 // PKG_ADD: dispatch ("sum", "spsum", "sparse matrix"); |
|
644 // PKG_ADD: dispatch ("sum", "spsum", "sparse complex matrix"); |
|
645 // PKG_ADD: dispatch ("sum", "spsum", "sparse bool matrix"); |
|
646 DEFUN_DLD (spsum, args, , |
|
647 "-*- texinfo -*-\n\ |
|
648 @deftypefn {Loadable Function} {@var{y} =} spsum (@var{x},@var{dim})\n\ |
|
649 Sum of elements along dimension @var{dim}. If @var{dim} is omitted, it\n\ |
|
650 defaults to 1 (column-wise sum).\n\ |
5642
|
651 @seealso{spprod, spsumsq}\n\ |
|
652 @end deftypefn") |
5164
|
653 { |
|
654 SPARSE_DIM_ARG_BODY (spsum, sum); |
|
655 } |
|
656 |
|
657 // PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse matrix"); |
|
658 // PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse complex matrix"); |
|
659 // PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse bool matrix"); |
|
660 DEFUN_DLD (spcumsum, args, , |
|
661 "-*- texinfo -*-\n\ |
|
662 @deftypefn {Loadable Function} {@var{y} =} spcumsum (@var{x},@var{dim})\n\ |
|
663 Cumulative sum of elements along dimension @var{dim}. If @var{dim}\n\ |
|
664 is omitted, it defaults to 1 (column-wise cumulative sums).\n\ |
5642
|
665 @seealso{spcumprod}\n\ |
|
666 @end deftypefn") |
5164
|
667 { |
|
668 SPARSE_DIM_ARG_BODY (spcumsum, cumsum); |
|
669 } |
|
670 |
|
671 // PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse matrix"); |
|
672 // PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse complex matrix"); |
|
673 // PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse bool matrix"); |
|
674 DEFUN_DLD (spsumsq, args, , |
|
675 "-*- texinfo -*-\n\ |
|
676 @deftypefn {Loadable Function} {@var{y} =} spsumsq (@var{x},@var{dim})\n\ |
|
677 Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ |
|
678 is omitted, it defaults to 1 (column-wise sum of squares).\n\ |
|
679 This function is equivalent to computing\n\ |
|
680 @example\n\ |
|
681 spsum (x .* spconj (x), dim)\n\ |
|
682 @end example\n\ |
|
683 but it uses less memory and avoids calling @code{spconj} if @var{x} is\n\ |
|
684 real.\n\ |
5642
|
685 @seealso{spprod, spsum}\n\ |
|
686 @end deftypefn") |
5164
|
687 { |
|
688 SPARSE_DIM_ARG_BODY (spsumsq, sumsq); |
|
689 } |
|
690 |
|
691 #define MINMAX_BODY(FCN) \ |
|
692 \ |
|
693 octave_value_list retval; \ |
|
694 \ |
|
695 int nargin = args.length (); \ |
|
696 \ |
|
697 if (nargin < 1 || nargin > 3 || nargout > 2) \ |
|
698 { \ |
|
699 print_usage (#FCN); \ |
|
700 return retval; \ |
|
701 } \ |
|
702 \ |
|
703 octave_value arg1; \ |
|
704 octave_value arg2; \ |
|
705 octave_value arg3; \ |
|
706 \ |
|
707 switch (nargin) \ |
|
708 { \ |
|
709 case 3: \ |
|
710 arg3 = args(2); \ |
|
711 \ |
|
712 case 2: \ |
|
713 arg2 = args(1); \ |
|
714 \ |
|
715 case 1: \ |
|
716 arg1 = args(0); \ |
|
717 break; \ |
|
718 \ |
|
719 default: \ |
|
720 panic_impossible (); \ |
|
721 break; \ |
|
722 } \ |
|
723 \ |
|
724 int dim; \ |
|
725 dim_vector dv = ((const octave_sparse_matrix&) arg1) .dims (); \ |
|
726 if (error_state) \ |
|
727 { \ |
|
728 gripe_wrong_type_arg (#FCN, arg1); \ |
|
729 return retval; \ |
|
730 } \ |
|
731 \ |
|
732 if (nargin == 3) \ |
|
733 { \ |
|
734 dim = arg3.nint_value () - 1; \ |
|
735 if (dim < 0 || dim >= dv.length ()) \ |
|
736 { \ |
|
737 error ("%s: invalid dimension", #FCN); \ |
|
738 return retval; \ |
|
739 } \ |
|
740 } \ |
|
741 else \ |
|
742 { \ |
|
743 dim = 0; \ |
|
744 while ((dim < dv.length ()) && (dv (dim) <= 1)) \ |
|
745 dim++; \ |
|
746 if (dim == dv.length ()) \ |
|
747 dim = 0; \ |
|
748 } \ |
|
749 \ |
|
750 bool single_arg = (nargin == 1) || arg2.is_empty(); \ |
|
751 \ |
|
752 if (single_arg && (nargout == 1 || nargout == 0)) \ |
|
753 { \ |
|
754 if (arg1.type_id () == octave_sparse_matrix::static_type_id ()) \ |
|
755 retval(0) = arg1.sparse_matrix_value () .FCN (dim); \ |
|
756 else if (arg1.type_id () == \ |
|
757 octave_sparse_complex_matrix::static_type_id ()) \ |
|
758 retval(0) = arg1.sparse_complex_matrix_value () .FCN (dim); \ |
|
759 else \ |
|
760 gripe_wrong_type_arg (#FCN, arg1); \ |
|
761 } \ |
|
762 else if (single_arg && nargout == 2) \ |
|
763 { \ |
5275
|
764 Array2<octave_idx_type> index; \ |
5164
|
765 \ |
|
766 if (arg1.type_id () == octave_sparse_matrix::static_type_id ()) \ |
|
767 retval(0) = arg1.sparse_matrix_value () .FCN (index, dim); \ |
|
768 else if (arg1.type_id () == \ |
|
769 octave_sparse_complex_matrix::static_type_id ()) \ |
|
770 retval(0) = arg1.sparse_complex_matrix_value () .FCN (index, dim); \ |
|
771 else \ |
|
772 gripe_wrong_type_arg (#FCN, arg1); \ |
|
773 \ |
5275
|
774 octave_idx_type len = index.numel (); \ |
5164
|
775 \ |
|
776 if (len > 0) \ |
|
777 { \ |
|
778 double nan_val = lo_ieee_nan_value (); \ |
|
779 \ |
|
780 NDArray idx (index.dims ()); \ |
|
781 \ |
5275
|
782 for (octave_idx_type i = 0; i < len; i++) \ |
5164
|
783 { \ |
|
784 OCTAVE_QUIT; \ |
5275
|
785 octave_idx_type tmp = index.elem (i) + 1; \ |
5164
|
786 idx.elem (i) = (tmp <= 0) \ |
|
787 ? nan_val : static_cast<double> (tmp); \ |
|
788 } \ |
|
789 \ |
|
790 retval(1) = idx; \ |
|
791 } \ |
|
792 else \ |
|
793 retval(1) = NDArray (); \ |
|
794 } \ |
|
795 else \ |
|
796 { \ |
|
797 int arg1_is_scalar = arg1.is_scalar_type (); \ |
|
798 int arg2_is_scalar = arg2.is_scalar_type (); \ |
|
799 \ |
|
800 int arg1_is_complex = arg1.is_complex_type (); \ |
|
801 int arg2_is_complex = arg2.is_complex_type (); \ |
|
802 \ |
|
803 if (arg1_is_scalar) \ |
|
804 { \ |
|
805 if (arg1_is_complex || arg2_is_complex) \ |
|
806 { \ |
|
807 Complex c1 = arg1.complex_value (); \ |
|
808 \ |
|
809 SparseComplexMatrix m2 = arg2.sparse_complex_matrix_value (); \ |
|
810 \ |
|
811 if (! error_state) \ |
|
812 { \ |
|
813 SparseComplexMatrix result = FCN (c1, m2); \ |
|
814 if (! error_state) \ |
|
815 retval(0) = result; \ |
|
816 } \ |
|
817 } \ |
|
818 else \ |
|
819 { \ |
|
820 double d1 = arg1.double_value (); \ |
|
821 SparseMatrix m2 = arg2.sparse_matrix_value (); \ |
|
822 \ |
|
823 if (! error_state) \ |
|
824 { \ |
|
825 SparseMatrix result = FCN (d1, m2); \ |
|
826 if (! error_state) \ |
|
827 retval(0) = result; \ |
|
828 } \ |
|
829 } \ |
|
830 } \ |
|
831 else if (arg2_is_scalar) \ |
|
832 { \ |
|
833 if (arg1_is_complex || arg2_is_complex) \ |
|
834 { \ |
|
835 SparseComplexMatrix m1 = arg1.sparse_complex_matrix_value (); \ |
|
836 \ |
|
837 if (! error_state) \ |
|
838 { \ |
|
839 Complex c2 = arg2.complex_value (); \ |
|
840 SparseComplexMatrix result = FCN (m1, c2); \ |
|
841 if (! error_state) \ |
|
842 retval(0) = result; \ |
|
843 } \ |
|
844 } \ |
|
845 else \ |
|
846 { \ |
|
847 SparseMatrix m1 = arg1.sparse_matrix_value (); \ |
|
848 \ |
|
849 if (! error_state) \ |
|
850 { \ |
|
851 double d2 = arg2.double_value (); \ |
|
852 SparseMatrix result = FCN (m1, d2); \ |
|
853 if (! error_state) \ |
|
854 retval(0) = result; \ |
|
855 } \ |
|
856 } \ |
|
857 } \ |
|
858 else \ |
|
859 { \ |
|
860 if (arg1_is_complex || arg2_is_complex) \ |
|
861 { \ |
|
862 SparseComplexMatrix m1 = arg1.sparse_complex_matrix_value (); \ |
|
863 \ |
|
864 if (! error_state) \ |
|
865 { \ |
|
866 SparseComplexMatrix m2 = arg2.sparse_complex_matrix_value (); \ |
|
867 \ |
|
868 if (! error_state) \ |
|
869 { \ |
|
870 SparseComplexMatrix result = FCN (m1, m2); \ |
|
871 if (! error_state) \ |
|
872 retval(0) = result; \ |
|
873 } \ |
|
874 } \ |
|
875 } \ |
|
876 else \ |
|
877 { \ |
|
878 SparseMatrix m1 = arg1.sparse_matrix_value (); \ |
|
879 \ |
|
880 if (! error_state) \ |
|
881 { \ |
|
882 SparseMatrix m2 = arg2.sparse_matrix_value (); \ |
|
883 \ |
|
884 if (! error_state) \ |
|
885 { \ |
|
886 SparseMatrix result = FCN (m1, m2); \ |
|
887 if (! error_state) \ |
|
888 retval(0) = result; \ |
|
889 } \ |
|
890 } \ |
|
891 } \ |
|
892 } \ |
|
893 } \ |
|
894 \ |
|
895 return retval |
|
896 |
|
897 // PKG_ADD: dispatch ("min", "spmin", "sparse matrix"); |
|
898 // PKG_ADD: dispatch ("min", "spmin", "sparse complex matrix"); |
|
899 // PKG_ADD: dispatch ("min", "spmin", "sparse bool matrix"); |
|
900 DEFUN_DLD (spmin, args, nargout, |
|
901 "-*- texinfo -*-\n\ |
|
902 @deftypefn {Mapping Function} {} spmin (@var{x}, @var{y}, @var{dim})\n\ |
|
903 @deftypefnx {Mapping Function} {[@var{w}, @var{iw}] =} spmin (@var{x})\n\ |
|
904 @cindex Utility Functions\n\ |
|
905 For a vector argument, return the minimum value. For a matrix\n\ |
|
906 argument, return the minimum value from each column, as a row\n\ |
|
907 vector, or over the dimension @var{dim} if defined. For two matrices\n\ |
|
908 (or a matrix and scalar), return the pair-wise minimum.\n\ |
|
909 Thus,\n\ |
|
910 \n\ |
|
911 @example\n\ |
|
912 min (min (@var{x}))\n\ |
|
913 @end example\n\ |
|
914 \n\ |
|
915 @noindent\n\ |
|
916 returns the smallest element of @var{x}, and\n\ |
|
917 \n\ |
|
918 @example\n\ |
|
919 @group\n\ |
|
920 min (2:5, pi)\n\ |
|
921 @result{} 2.0000 3.0000 3.1416 3.1416\n\ |
|
922 @end group\n\ |
|
923 @end example\n\ |
|
924 @noindent\n\ |
|
925 compares each element of the range @code{2:5} with @code{pi}, and\n\ |
|
926 returns a row vector of the minimum values.\n\ |
|
927 \n\ |
|
928 For complex arguments, the magnitude of the elements are used for\n\ |
|
929 comparison.\n\ |
|
930 \n\ |
|
931 If called with one input and two output arguments,\n\ |
|
932 @code{min} also returns the first index of the\n\ |
|
933 minimum value(s). Thus,\n\ |
|
934 \n\ |
|
935 @example\n\ |
|
936 @group\n\ |
|
937 [x, ix] = min ([1, 3, 0, 2, 5])\n\ |
|
938 @result{} x = 0\n\ |
|
939 ix = 3\n\ |
|
940 @end group\n\ |
|
941 @end example\n\ |
|
942 @end deftypefn") |
|
943 { |
|
944 MINMAX_BODY (min); |
|
945 } |
|
946 |
|
947 // PKG_ADD: dispatch ("max", "spmax", "sparse matrix"); |
|
948 // PKG_ADD: dispatch ("max", "spmax", "sparse complex matrix"); |
|
949 // PKG_ADD: dispatch ("max", "spmax", "sparse bool matrix"); |
|
950 DEFUN_DLD (spmax, args, nargout, |
|
951 "-*- texinfo -*-\n\ |
|
952 @deftypefn {Mapping Function} {} spmax (@var{x}, @var{y}, @var{dim})\n\ |
|
953 @deftypefnx {Mapping Function} {[@var{w}, @var{iw}] =} spmax (@var{x})\n\ |
|
954 @cindex Utility Functions\n\ |
|
955 For a vector argument, return the maximum value. For a matrix\n\ |
|
956 argument, return the maximum value from each column, as a row\n\ |
|
957 vector, or over the dimension @var{dim} if defined. For two matrices\n\ |
|
958 (or a matrix and scalar), return the pair-wise maximum.\n\ |
|
959 Thus,\n\ |
|
960 \n\ |
|
961 @example\n\ |
|
962 max (max (@var{x}))\n\ |
|
963 @end example\n\ |
|
964 \n\ |
|
965 @noindent\n\ |
|
966 returns the largest element of @var{x}, and\n\ |
|
967 \n\ |
|
968 @example\n\ |
|
969 @group\n\ |
|
970 max (2:5, pi)\n\ |
|
971 @result{} 3.1416 3.1416 4.0000 5.0000\n\ |
|
972 @end group\n\ |
|
973 @end example\n\ |
|
974 @noindent\n\ |
|
975 compares each element of the range @code{2:5} with @code{pi}, and\n\ |
|
976 returns a row vector of the maximum values.\n\ |
|
977 \n\ |
|
978 For complex arguments, the magnitude of the elements are used for\n\ |
|
979 comparison.\n\ |
|
980 \n\ |
|
981 If called with one input and two output arguments,\n\ |
|
982 @code{max} also returns the first index of the\n\ |
|
983 maximum value(s). Thus,\n\ |
|
984 \n\ |
|
985 @example\n\ |
|
986 @group\n\ |
|
987 [x, ix] = max ([1, 3, 5, 2, 5])\n\ |
|
988 @result{} x = 5\n\ |
|
989 ix = 3\n\ |
|
990 @end group\n\ |
|
991 @end example\n\ |
|
992 @end deftypefn") |
|
993 { |
|
994 MINMAX_BODY (max); |
|
995 } |
|
996 |
|
997 // PKG_ADD: dispatch ("atan2", "spatan2", "sparse matrix"); |
|
998 // PKG_ADD: dispatch ("atan2", "spatan2", "sparse complex matrix"); |
|
999 // PKG_ADD: dispatch ("atan2", "spatan2", "sparse bool matrix"); |
|
1000 DEFUN_DLD (spatan2, args, , |
|
1001 "-*- texinfo -*-\n\ |
|
1002 @deftypefn {Loadable Function} {} spatan2 (@var{y}, @var{x})\n\ |
|
1003 Compute atan (Y / X) for corresponding sparse matrix elements of Y and X.\n\ |
|
1004 The result is in range -pi to pi.\n\ |
|
1005 @end deftypefn\n") |
|
1006 { |
|
1007 octave_value retval; |
|
1008 int nargin = args.length (); |
|
1009 if (nargin == 2) { |
|
1010 SparseMatrix a, b; |
|
1011 double da, db; |
|
1012 bool is_double_a = false; |
|
1013 bool is_double_b = false; |
|
1014 |
|
1015 if (args(0).is_scalar_type ()) |
|
1016 { |
|
1017 is_double_a = true; |
|
1018 da = args(0).double_value(); |
|
1019 } |
|
1020 else |
|
1021 a = args(0).sparse_matrix_value (); |
|
1022 |
|
1023 if (args(1).is_scalar_type ()) |
|
1024 { |
|
1025 is_double_b = true; |
|
1026 db = args(1).double_value(); |
|
1027 } |
|
1028 else |
|
1029 b = args(1).sparse_matrix_value (); |
|
1030 |
|
1031 if (is_double_a && is_double_b) |
|
1032 retval = Matrix (1, 1, atan2(da, db)); |
|
1033 else if (is_double_a) |
|
1034 retval = atan2 (da, b); |
|
1035 else if (is_double_b) |
|
1036 retval = atan2 (a, db); |
|
1037 else |
|
1038 retval = atan2 (a, b); |
|
1039 |
|
1040 } else |
|
1041 print_usage("spatan2"); |
|
1042 |
|
1043 return retval; |
|
1044 } |
|
1045 |
|
1046 static octave_value |
|
1047 make_spdiag (const octave_value& a, const octave_value& b) |
|
1048 { |
|
1049 octave_value retval; |
|
1050 |
|
1051 if (a.is_complex_type ()) |
|
1052 { |
|
1053 SparseComplexMatrix m = a.sparse_complex_matrix_value (); |
5275
|
1054 octave_idx_type k = b.nint_value(true); |
5164
|
1055 |
|
1056 if (error_state) |
|
1057 return retval; |
|
1058 |
5275
|
1059 octave_idx_type nr = m.rows (); |
|
1060 octave_idx_type nc = m.columns (); |
5164
|
1061 |
|
1062 if (nr == 0 || nc == 0) |
|
1063 retval = m; |
|
1064 else if (nr == 1 || nc == 1) |
|
1065 { |
5275
|
1066 octave_idx_type roff = 0; |
|
1067 octave_idx_type coff = 0; |
5164
|
1068 if (k > 0) |
|
1069 { |
|
1070 roff = 0; |
|
1071 coff = k; |
|
1072 } |
|
1073 else if (k < 0) |
|
1074 { |
|
1075 k = -k; |
|
1076 roff = k; |
|
1077 coff = 0; |
|
1078 } |
|
1079 |
|
1080 if (nr == 1) |
|
1081 { |
5275
|
1082 octave_idx_type n = nc + k; |
5604
|
1083 octave_idx_type nz = m.nzmax (); |
5164
|
1084 SparseComplexMatrix r (n, n, nz); |
5275
|
1085 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
1086 r.xcidx (i) = 0; |
5275
|
1087 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
1088 { |
5275
|
1089 for (octave_idx_type i = m.cidx(j); i < m.cidx(j+1); i++) |
5164
|
1090 { |
|
1091 r.xdata (i) = m.data (i); |
|
1092 r.xridx (i) = j + roff; |
|
1093 } |
|
1094 r.xcidx (j+coff+1) = m.cidx(j+1); |
|
1095 } |
5275
|
1096 for (octave_idx_type i = nc+coff+1; i < n+1; i++) |
5164
|
1097 r.xcidx (i) = nz; |
|
1098 retval = r; |
|
1099 } |
|
1100 else |
|
1101 { |
5275
|
1102 octave_idx_type n = nr + k; |
5604
|
1103 octave_idx_type nz = m.nzmax (); |
5275
|
1104 octave_idx_type ii = 0; |
|
1105 octave_idx_type ir = m.ridx(0); |
5164
|
1106 SparseComplexMatrix r (n, n, nz); |
5275
|
1107 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
1108 r.xcidx (i) = 0; |
5275
|
1109 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
1110 { |
|
1111 if (ir == i) |
|
1112 { |
|
1113 r.xdata (ii) = m.data (ii); |
|
1114 r.xridx (ii++) = ir + roff; |
|
1115 if (ii != nz) |
|
1116 ir = m.ridx (ii); |
|
1117 } |
|
1118 r.xcidx (i+coff+1) = ii; |
|
1119 } |
5275
|
1120 for (octave_idx_type i = nr+coff+1; i < n+1; i++) |
5164
|
1121 r.xcidx (i) = nz; |
|
1122 retval = r; |
|
1123 } |
|
1124 } |
|
1125 else |
|
1126 { |
|
1127 SparseComplexMatrix r = m.diag (k); |
|
1128 // Don't use numel, since it can overflow for very large matrices |
|
1129 if (r.rows () > 0 && r.cols () > 0) |
|
1130 retval = r; |
|
1131 } |
|
1132 } |
|
1133 else if (a.is_real_type ()) |
|
1134 { |
|
1135 SparseMatrix m = a.sparse_matrix_value (); |
|
1136 |
5275
|
1137 octave_idx_type k = b.nint_value(true); |
5164
|
1138 |
|
1139 if (error_state) |
|
1140 return retval; |
|
1141 |
5275
|
1142 octave_idx_type nr = m.rows (); |
|
1143 octave_idx_type nc = m.columns (); |
5164
|
1144 |
|
1145 if (nr == 0 || nc == 0) |
|
1146 retval = m; |
|
1147 else if (nr == 1 || nc == 1) |
|
1148 { |
5275
|
1149 octave_idx_type roff = 0; |
|
1150 octave_idx_type coff = 0; |
5164
|
1151 if (k > 0) |
|
1152 { |
|
1153 roff = 0; |
|
1154 coff = k; |
|
1155 } |
|
1156 else if (k < 0) |
|
1157 { |
|
1158 k = -k; |
|
1159 roff = k; |
|
1160 coff = 0; |
|
1161 } |
|
1162 |
|
1163 if (nr == 1) |
|
1164 { |
5275
|
1165 octave_idx_type n = nc + k; |
5604
|
1166 octave_idx_type nz = m.nzmax (); |
5164
|
1167 SparseMatrix r (n, n, nz); |
|
1168 |
5275
|
1169 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
1170 r.xcidx (i) = 0; |
5275
|
1171 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
1172 { |
5275
|
1173 for (octave_idx_type i = m.cidx(j); i < m.cidx(j+1); i++) |
5164
|
1174 { |
|
1175 r.xdata (i) = m.data (i); |
|
1176 r.xridx (i) = j + roff; |
|
1177 } |
|
1178 r.xcidx (j+coff+1) = m.cidx(j+1); |
|
1179 } |
5275
|
1180 for (octave_idx_type i = nc+coff+1; i < n+1; i++) |
5164
|
1181 r.xcidx (i) = nz; |
|
1182 retval = r; |
|
1183 } |
|
1184 else |
|
1185 { |
5275
|
1186 octave_idx_type n = nr + k; |
5604
|
1187 octave_idx_type nz = m.nzmax (); |
5275
|
1188 octave_idx_type ii = 0; |
|
1189 octave_idx_type ir = m.ridx(0); |
5164
|
1190 SparseMatrix r (n, n, nz); |
5275
|
1191 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
1192 r.xcidx (i) = 0; |
5275
|
1193 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
1194 { |
|
1195 if (ir == i) |
|
1196 { |
|
1197 r.xdata (ii) = m.data (ii); |
|
1198 r.xridx (ii++) = ir + roff; |
|
1199 if (ii != nz) |
|
1200 ir = m.ridx (ii); |
|
1201 } |
|
1202 r.xcidx (i+coff+1) = ii; |
|
1203 } |
5275
|
1204 for (octave_idx_type i = nr+coff+1; i < n+1; i++) |
5164
|
1205 r.xcidx (i) = nz; |
|
1206 retval = r; |
|
1207 } |
|
1208 } |
|
1209 else |
|
1210 { |
|
1211 SparseMatrix r = m.diag (k); |
|
1212 if (r.rows () > 0 && r.cols () > 0) |
|
1213 retval = r; |
|
1214 } |
|
1215 } |
|
1216 else |
|
1217 gripe_wrong_type_arg ("spdiag", a); |
|
1218 |
|
1219 return retval; |
|
1220 } |
|
1221 |
|
1222 // PKG_ADD: dispatch ("diag", "spdiag", "sparse matrix"); |
|
1223 // PKG_ADD: dispatch ("diag", "spdiag", "sparse complex matrix"); |
|
1224 // PKG_ADD: dispatch ("diag", "spdiag", "sparse bool matrix"); |
|
1225 DEFUN_DLD (spdiag, args, , |
|
1226 "-*- texinfo -*-\n\ |
|
1227 @deftypefn {Loadable Function} {} spdiag (@var{v}, @var{k})\n\ |
|
1228 Return a diagonal matrix with the sparse vector @var{v} on diagonal\n\ |
|
1229 @var{k}. The second argument is optional. If it is positive, the vector is\n\ |
|
1230 placed on the @var{k}-th super-diagonal. If it is negative, it is placed\n\ |
|
1231 on the @var{-k}-th sub-diagonal. The default value of @var{k} is 0, and\n\ |
|
1232 the vector is placed on the main diagonal. For example,\n\ |
|
1233 \n\ |
|
1234 @example\n\ |
|
1235 spdiag ([1, 2, 3], 1)\n\ |
|
1236 ans =\n\ |
|
1237 \n\ |
|
1238 Compressed Column Sparse (rows=4, cols=4, nnz=3)\n\ |
|
1239 (1 , 2) -> 1\n\ |
|
1240 (2 , 3) -> 2\n\ |
|
1241 (3 , 4) -> 3\n\ |
|
1242 @end example\n\ |
5642
|
1243 @seealso{diag}\n\ |
|
1244 @end deftypefn") |
5164
|
1245 { |
|
1246 octave_value retval; |
|
1247 |
|
1248 int nargin = args.length (); |
|
1249 |
|
1250 if (nargin == 1 && args(0).is_defined ()) |
|
1251 retval = make_spdiag (args(0), octave_value(0.)); |
|
1252 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
|
1253 retval = make_spdiag (args(0), args(1)); |
|
1254 else |
|
1255 print_usage ("spdiag"); |
|
1256 |
|
1257 return retval; |
|
1258 } |
|
1259 |
|
1260 /* |
|
1261 ;;; Local Variables: *** |
|
1262 ;;; mode: C++ *** |
|
1263 ;;; End: *** |
|
1264 */ |