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