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