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