5164
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 2004, 2005, 2006, 2007 David Bateman |
7016
|
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 Andy Adler |
|
5 |
|
6 This file is part of Octave. |
5164
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
5164
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
5164
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cstdlib> |
|
29 #include <string> |
|
30 |
|
31 #include "variables.h" |
|
32 #include "utils.h" |
|
33 #include "pager.h" |
|
34 #include "defun-dld.h" |
|
35 #include "gripes.h" |
|
36 #include "quit.h" |
|
37 |
|
38 #include "ov-re-sparse.h" |
|
39 #include "ov-cx-sparse.h" |
|
40 #include "ov-bool-sparse.h" |
|
41 |
|
42 static bool |
|
43 is_sparse (const octave_value& arg) |
|
44 { |
5631
|
45 return (arg.is_sparse_type ()); |
5164
|
46 } |
|
47 |
|
48 DEFUN_DLD (issparse, args, , |
|
49 "-*- texinfo -*-\n\ |
|
50 @deftypefn {Loadable Function} {} issparse (@var{expr})\n\ |
|
51 Return 1 if the value of the expression @var{expr} is a sparse matrix.\n\ |
|
52 @end deftypefn") |
|
53 { |
|
54 if (args.length() != 1) |
|
55 { |
5823
|
56 print_usage (); |
5164
|
57 return octave_value (); |
|
58 } |
|
59 else |
|
60 return octave_value (is_sparse (args(0))); |
|
61 } |
|
62 |
|
63 DEFUN_DLD (sparse, args, , |
|
64 "-*- texinfo -*-\n\ |
6556
|
65 @deftypefn {Loadable Function} {@var{s} =} sparse (@var{a})\n\ |
|
66 Create a sparse matrix from the full matrix @var{a}.\n\ |
|
67 is forced back to a full matrix is resulting matrix is sparse\n\ |
5164
|
68 \n\ |
6556
|
69 @deftypefnx {Loadable Function} {@var{s} =} sparse (@var{a}, 1)\n\ |
|
70 Create a sparse matrix and convert it back to a full matrix.\n\ |
5164
|
71 is forced back to a full matrix is resulting matrix is sparse\n\ |
|
72 \n\ |
6556
|
73 @deftypefnx {Loadable Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{sv}, @var{m}, @var{n}, @var{nzmax})\n\ |
|
74 Create a sparse matrix given integer index vectors @var{i} and @var{j},\n\ |
|
75 a 1-by-@code{nnz} vector of real of complex values @var{sv}, overall\n\ |
|
76 dimensions @var{m} and @var{n} of the sparse matrix. The argument\n\ |
7001
|
77 @code{nzmax} is ignored but accepted for compatibility with @sc{Matlab}.\n\ |
5164
|
78 \n\ |
6556
|
79 @strong{Note}: if multiple values are specified with the same\n\ |
|
80 @var{i}, @var{j} indices, the corresponding values in @var{s} will\n\ |
|
81 be added.\n\ |
|
82 \n\ |
6557
|
83 The following are all equivalent:\n\ |
5164
|
84 \n\ |
6556
|
85 @example\n\ |
|
86 @group\n\ |
|
87 s = sparse (i, j, s, m, n)\n\ |
|
88 s = sparse (i, j, s, m, n, \"summation\")\n\ |
|
89 s = sparse (i, j, s, m, n, \"sum\")\n\ |
|
90 @end group\n\ |
|
91 @end example\n\ |
5164
|
92 \n\ |
6556
|
93 @deftypefnx {Loadable Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{s}, @var{m}, @var{n}, \"unique\")\n\ |
|
94 Same as above, except that if more than two values are specified for the\n\ |
|
95 same @var{i}, @var{j} indices, the last specified value will be used.\n\ |
5164
|
96 \n\ |
6556
|
97 @deftypefnx {Loadable Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{sv})\n\ |
|
98 Uses @code{@var{m} = max (@var{i})}, @code{@var{n} = max (@var{j})}\n\ |
5164
|
99 \n\ |
6556
|
100 @deftypefnx {Loadable Function} {@var{s} =} sparse (@var{m}, @var{n})\n\ |
|
101 Equivalent to @code{sparse ([], [], [], @var{m}, @var{n}, 0)}\n\ |
|
102 \n\ |
|
103 If any of @var{sv}, @var{i} or @var{j} are scalars, they are expanded\n\ |
|
104 to have a common size.\n\ |
5164
|
105 @seealso{full}\n\ |
|
106 @end deftypefn") |
|
107 { |
|
108 octave_value retval; |
|
109 |
|
110 // WARNING: This function should always use constructions like |
|
111 // retval = new octave_sparse_matrix (sm); |
|
112 // To avoid calling the maybe_mutate function. This is the only |
7287
|
113 // function that should not call maybe_mutate |
5164
|
114 |
|
115 int nargin= args.length(); |
|
116 if (nargin < 1 || (nargin == 4 && !args(3).is_string ()) || nargin > 6) |
|
117 { |
5823
|
118 print_usage (); |
5164
|
119 return retval; |
|
120 } |
|
121 |
|
122 bool use_complex = false; |
|
123 bool use_bool = false; |
|
124 if (nargin > 2) |
|
125 { |
|
126 use_complex= args(2).is_complex_type(); |
|
127 use_bool = args(2).is_bool_type (); |
|
128 } |
|
129 else |
|
130 { |
|
131 use_complex= args(0).is_complex_type(); |
|
132 use_bool = args(0).is_bool_type (); |
|
133 } |
|
134 |
7287
|
135 if (nargin == 1) |
5164
|
136 { |
|
137 octave_value arg = args (0); |
|
138 |
|
139 if (is_sparse (arg)) |
|
140 { |
|
141 if (use_complex) |
|
142 { |
5760
|
143 SparseComplexMatrix sm = arg.sparse_complex_matrix_value (); |
5164
|
144 retval = new octave_sparse_complex_matrix (sm); |
|
145 } |
|
146 else if (use_bool) |
|
147 { |
5760
|
148 SparseBoolMatrix sm = arg.sparse_bool_matrix_value (); |
5164
|
149 retval = new octave_sparse_bool_matrix (sm); |
|
150 } |
|
151 else |
|
152 { |
5760
|
153 SparseMatrix sm = arg.sparse_matrix_value (); |
5164
|
154 retval = new octave_sparse_matrix (sm); |
|
155 } |
|
156 } |
|
157 else |
|
158 { |
|
159 if (use_complex) |
|
160 { |
|
161 SparseComplexMatrix sm (args (0).complex_matrix_value ()); |
|
162 if (error_state) |
|
163 return retval; |
|
164 retval = new octave_sparse_complex_matrix (sm); |
|
165 } |
|
166 else if (use_bool) |
|
167 { |
|
168 SparseBoolMatrix sm (args (0).bool_matrix_value ()); |
|
169 if (error_state) |
|
170 return retval; |
|
171 retval = new octave_sparse_bool_matrix (sm); |
|
172 } |
|
173 else |
|
174 { |
|
175 SparseMatrix sm (args (0).matrix_value ()); |
|
176 if (error_state) |
|
177 return retval; |
|
178 retval = new octave_sparse_matrix (sm); |
|
179 } |
|
180 } |
|
181 } |
|
182 else |
|
183 { |
5275
|
184 octave_idx_type m = 1, n = 1; |
5164
|
185 if (nargin == 2) |
|
186 { |
|
187 m = args(0).int_value(); |
|
188 n = args(1).int_value(); |
|
189 if (error_state) return retval; |
|
190 |
|
191 if (use_complex) |
|
192 retval = new octave_sparse_complex_matrix |
|
193 (SparseComplexMatrix (m, n)); |
|
194 else if (use_bool) |
|
195 retval = new octave_sparse_bool_matrix |
|
196 (SparseBoolMatrix (m, n)); |
|
197 else |
|
198 retval = new octave_sparse_matrix |
|
199 (SparseMatrix (m, n)); |
|
200 } |
|
201 else |
|
202 { |
|
203 if (args(0).is_empty () || args (1).is_empty () |
|
204 || args(2).is_empty ()) |
|
205 { |
|
206 if (nargin > 4) |
|
207 { |
|
208 m = args(3).int_value(); |
|
209 n = args(4).int_value(); |
|
210 } |
|
211 |
|
212 if (use_bool) |
|
213 retval = new octave_sparse_bool_matrix |
|
214 (SparseBoolMatrix (m, n)); |
|
215 else |
|
216 retval = new octave_sparse_matrix (SparseMatrix (m, n)); |
|
217 } |
|
218 else |
|
219 { |
|
220 // |
|
221 // I use this clumsy construction so that we can use |
|
222 // any orientation of args |
|
223 ColumnVector ridxA = ColumnVector (args(0).vector_value |
|
224 (false, true)); |
|
225 ColumnVector cidxA = ColumnVector (args(1).vector_value |
|
226 (false, true)); |
|
227 ColumnVector coefA; |
|
228 boolNDArray coefAB; |
|
229 ComplexColumnVector coefAC; |
|
230 bool assemble_do_sum = true; // this is the default in matlab6 |
|
231 |
|
232 if (use_complex) |
|
233 { |
|
234 if (args(2).is_empty ()) |
|
235 coefAC = ComplexColumnVector (0); |
|
236 else |
|
237 coefAC = ComplexColumnVector |
|
238 (args(2).complex_vector_value (false, true)); |
|
239 } |
|
240 else if (use_bool) |
|
241 { |
|
242 if (args(2).is_empty ()) |
|
243 coefAB = boolNDArray (dim_vector (1, 0)); |
|
244 else |
|
245 coefAB = args(2).bool_array_value (); |
|
246 dim_vector AB_dims = coefAB.dims (); |
|
247 if (AB_dims.length() > 2 || (AB_dims(0) != 1 && |
|
248 AB_dims(1) != 1)) |
|
249 error ("sparse: vector arguments required"); |
|
250 } |
|
251 else |
|
252 if (args(2).is_empty ()) |
|
253 coefA = ColumnVector (0); |
|
254 else |
|
255 coefA = ColumnVector (args(2).vector_value (false, true)); |
|
256 |
|
257 if (error_state) |
|
258 return retval; |
|
259 |
|
260 // Confirm that i,j,s all have the same number of elements |
5275
|
261 octave_idx_type ns; |
5164
|
262 if (use_complex) |
|
263 ns = coefAC.length(); |
|
264 else if (use_bool) |
|
265 ns = coefAB.length(); |
|
266 else |
|
267 ns = coefA.length(); |
|
268 |
5275
|
269 octave_idx_type ni = ridxA.length(); |
|
270 octave_idx_type nj = cidxA.length(); |
|
271 octave_idx_type nnz = (ni > nj ? ni : nj); |
5164
|
272 if ((ns != 1 && ns != nnz) || |
|
273 (ni != 1 && ni != nnz) || |
|
274 (nj != 1 && nj != nnz)) |
|
275 { |
|
276 error ("sparse i, j and s must have the same length"); |
|
277 return retval; |
|
278 } |
|
279 |
|
280 if (nargin == 3 || nargin == 4) |
|
281 { |
5275
|
282 m = static_cast<octave_idx_type> (ridxA.max()); |
|
283 n = static_cast<octave_idx_type> (cidxA.max()); |
5164
|
284 |
|
285 // if args(3) is not string, then ignore the value |
|
286 // otherwise check for summation or unique |
|
287 if (nargin == 4 && args(3).is_string()) |
|
288 { |
|
289 std::string vv= args(3).string_value(); |
|
290 if (error_state) return retval; |
|
291 |
|
292 if ( vv == "summation" || |
|
293 vv == "sum" ) |
|
294 assemble_do_sum = true; |
|
295 else |
|
296 if ( vv == "unique" ) |
|
297 assemble_do_sum = false; |
|
298 else { |
|
299 error("sparse repeat flag must be 'sum' or 'unique'"); |
|
300 return retval; |
|
301 } |
|
302 } |
|
303 } |
|
304 else |
|
305 { |
|
306 m = args(3).int_value(); |
|
307 n = args(4).int_value(); |
|
308 if (error_state) |
|
309 return retval; |
|
310 |
|
311 // if args(5) is not string, then ignore the value |
|
312 // otherwise check for summation or unique |
|
313 if (nargin >= 6 && args(5).is_string()) |
|
314 { |
|
315 std::string vv= args(5).string_value(); |
|
316 if (error_state) return retval; |
|
317 |
|
318 if ( vv == "summation" || |
|
319 vv == "sum" ) |
|
320 assemble_do_sum = true; |
|
321 else |
|
322 if ( vv == "unique" ) |
|
323 assemble_do_sum = false; |
|
324 else { |
|
325 error("sparse repeat flag must be 'sum' or 'unique'"); |
|
326 return retval; |
|
327 } |
|
328 } |
|
329 |
|
330 } |
|
331 |
|
332 // Convert indexing to zero-indexing used internally |
|
333 ridxA -= 1.; |
|
334 cidxA -= 1.; |
|
335 |
|
336 if (use_complex) |
|
337 retval = new octave_sparse_complex_matrix |
|
338 (SparseComplexMatrix (coefAC, ridxA, cidxA, m, n, |
|
339 assemble_do_sum)); |
|
340 else if (use_bool) |
|
341 retval = new octave_sparse_bool_matrix |
|
342 (SparseBoolMatrix (coefAB, ridxA, cidxA, m, n, |
|
343 assemble_do_sum)); |
|
344 else |
|
345 retval = new octave_sparse_matrix |
|
346 (SparseMatrix (coefA, ridxA, cidxA, m, n, |
|
347 assemble_do_sum)); |
|
348 } |
|
349 } |
|
350 } |
|
351 |
|
352 return retval; |
|
353 } |
|
354 |
|
355 DEFUN_DLD (full, args, , |
|
356 "-*- texinfo -*-\n\ |
|
357 @deftypefn {Loadable Function} {@var{FM} =} full (@var{SM})\n\ |
|
358 returns a full storage matrix from a sparse one\n\ |
|
359 @seealso{sparse}\n\ |
|
360 @end deftypefn") |
|
361 { |
|
362 octave_value retval; |
|
363 |
5760
|
364 if (args.length() < 1) |
|
365 { |
5823
|
366 print_usage (); |
5760
|
367 return retval; |
|
368 } |
5164
|
369 |
5631
|
370 if (args(0).is_sparse_type ()) |
5164
|
371 { |
|
372 if (args(0).type_name () == "sparse matrix") |
|
373 retval = args(0).matrix_value (); |
|
374 else if (args(0).type_name () == "sparse complex matrix") |
|
375 retval = args(0).complex_matrix_value (); |
|
376 else if (args(0).type_name () == "sparse bool matrix") |
|
377 retval = args(0).bool_matrix_value (); |
|
378 } |
|
379 else if (args(0).is_real_type()) |
|
380 retval = args(0).matrix_value(); |
|
381 else if (args(0).is_complex_type()) |
|
382 retval = args(0).complex_matrix_value(); |
|
383 else |
|
384 gripe_wrong_type_arg ("full", args(0)); |
|
385 |
|
386 return retval; |
|
387 } |
|
388 |
|
389 #define SPARSE_DIM_ARG_BODY(NAME, FUNC) \ |
|
390 int nargin = args.length(); \ |
|
391 octave_value retval; \ |
|
392 if ((nargin != 1 ) && (nargin != 2)) \ |
5823
|
393 print_usage (); \ |
5164
|
394 else { \ |
|
395 int dim = (nargin == 1 ? -1 : args(1).int_value(true) - 1); \ |
|
396 if (error_state) return retval; \ |
|
397 if (dim < -1 || dim > 1) { \ |
|
398 error (#NAME ": invalid dimension argument = %d", dim + 1); \ |
|
399 return retval; \ |
|
400 } \ |
|
401 if (args(0).type_id () == \ |
|
402 octave_sparse_matrix::static_type_id () || args(0).type_id () == \ |
|
403 octave_sparse_bool_matrix::static_type_id ()) { \ |
|
404 retval = args(0).sparse_matrix_value () .FUNC (dim); \ |
|
405 } else if (args(0).type_id () == \ |
|
406 octave_sparse_complex_matrix::static_type_id ()) { \ |
|
407 retval = args(0).sparse_complex_matrix_value () .FUNC (dim); \ |
|
408 } else \ |
5823
|
409 print_usage (); \ |
5164
|
410 } \ |
|
411 return retval |
|
412 |
|
413 // PKG_ADD: dispatch ("prod", "spprod", "sparse matrix"); |
|
414 // PKG_ADD: dispatch ("prod", "spprod", "sparse complex matrix"); |
|
415 // PKG_ADD: dispatch ("prod", "spprod", "sparse bool matrix"); |
|
416 DEFUN_DLD (spprod, args, , |
|
417 "-*- texinfo -*-\n\ |
|
418 @deftypefn {Loadable Function} {@var{y} =} spprod (@var{x},@var{dim})\n\ |
|
419 Product of elements along dimension @var{dim}. If @var{dim} is omitted,\n\ |
|
420 it defaults to 1 (column-wise products).\n\ |
5642
|
421 @seealso{spsum, spsumsq}\n\ |
|
422 @end deftypefn") |
5164
|
423 { |
|
424 SPARSE_DIM_ARG_BODY (spprod, prod); |
|
425 } |
|
426 |
|
427 // PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse matrix"); |
|
428 // PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse complex matrix"); |
|
429 // PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse bool matrix"); |
|
430 DEFUN_DLD (spcumprod, args, , |
|
431 "-*- texinfo -*-\n\ |
|
432 @deftypefn {Loadable Function} {@var{y} =} spcumprod (@var{x},@var{dim})\n\ |
|
433 Cumulative product of elements along dimension @var{dim}. If @var{dim}\n\ |
|
434 is omitted, it defaults to 1 (column-wise cumulative products).\n\ |
5642
|
435 @seealso{spcumsum}\n\ |
|
436 @end deftypefn") |
5164
|
437 { |
|
438 SPARSE_DIM_ARG_BODY (spcumprod, cumprod); |
|
439 } |
|
440 |
|
441 // PKG_ADD: dispatch ("sum", "spsum", "sparse matrix"); |
|
442 // PKG_ADD: dispatch ("sum", "spsum", "sparse complex matrix"); |
|
443 // PKG_ADD: dispatch ("sum", "spsum", "sparse bool matrix"); |
|
444 DEFUN_DLD (spsum, args, , |
|
445 "-*- texinfo -*-\n\ |
|
446 @deftypefn {Loadable Function} {@var{y} =} spsum (@var{x},@var{dim})\n\ |
|
447 Sum of elements along dimension @var{dim}. If @var{dim} is omitted, it\n\ |
|
448 defaults to 1 (column-wise sum).\n\ |
5642
|
449 @seealso{spprod, spsumsq}\n\ |
|
450 @end deftypefn") |
5164
|
451 { |
|
452 SPARSE_DIM_ARG_BODY (spsum, sum); |
|
453 } |
|
454 |
|
455 // PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse matrix"); |
|
456 // PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse complex matrix"); |
|
457 // PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse bool matrix"); |
|
458 DEFUN_DLD (spcumsum, args, , |
|
459 "-*- texinfo -*-\n\ |
|
460 @deftypefn {Loadable Function} {@var{y} =} spcumsum (@var{x},@var{dim})\n\ |
|
461 Cumulative sum of elements along dimension @var{dim}. If @var{dim}\n\ |
|
462 is omitted, it defaults to 1 (column-wise cumulative sums).\n\ |
5642
|
463 @seealso{spcumprod}\n\ |
|
464 @end deftypefn") |
5164
|
465 { |
|
466 SPARSE_DIM_ARG_BODY (spcumsum, cumsum); |
|
467 } |
|
468 |
|
469 // PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse matrix"); |
|
470 // PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse complex matrix"); |
|
471 // PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse bool matrix"); |
|
472 DEFUN_DLD (spsumsq, args, , |
|
473 "-*- texinfo -*-\n\ |
|
474 @deftypefn {Loadable Function} {@var{y} =} spsumsq (@var{x},@var{dim})\n\ |
|
475 Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ |
|
476 is omitted, it defaults to 1 (column-wise sum of squares).\n\ |
|
477 This function is equivalent to computing\n\ |
|
478 @example\n\ |
|
479 spsum (x .* spconj (x), dim)\n\ |
|
480 @end example\n\ |
|
481 but it uses less memory and avoids calling @code{spconj} if @var{x} is\n\ |
|
482 real.\n\ |
5642
|
483 @seealso{spprod, spsum}\n\ |
|
484 @end deftypefn") |
5164
|
485 { |
|
486 SPARSE_DIM_ARG_BODY (spsumsq, sumsq); |
|
487 } |
|
488 |
|
489 #define MINMAX_BODY(FCN) \ |
|
490 \ |
|
491 octave_value_list retval; \ |
|
492 \ |
|
493 int nargin = args.length (); \ |
|
494 \ |
|
495 if (nargin < 1 || nargin > 3 || nargout > 2) \ |
|
496 { \ |
5823
|
497 print_usage (); \ |
5164
|
498 return retval; \ |
|
499 } \ |
|
500 \ |
|
501 octave_value arg1; \ |
|
502 octave_value arg2; \ |
|
503 octave_value arg3; \ |
|
504 \ |
|
505 switch (nargin) \ |
|
506 { \ |
|
507 case 3: \ |
|
508 arg3 = args(2); \ |
|
509 \ |
|
510 case 2: \ |
|
511 arg2 = args(1); \ |
|
512 \ |
|
513 case 1: \ |
|
514 arg1 = args(0); \ |
|
515 break; \ |
|
516 \ |
|
517 default: \ |
|
518 panic_impossible (); \ |
|
519 break; \ |
|
520 } \ |
|
521 \ |
|
522 int dim; \ |
5759
|
523 dim_vector dv = arg1.dims (); \ |
5164
|
524 if (error_state) \ |
|
525 { \ |
|
526 gripe_wrong_type_arg (#FCN, arg1); \ |
|
527 return retval; \ |
|
528 } \ |
|
529 \ |
|
530 if (nargin == 3) \ |
|
531 { \ |
|
532 dim = arg3.nint_value () - 1; \ |
|
533 if (dim < 0 || dim >= dv.length ()) \ |
|
534 { \ |
|
535 error ("%s: invalid dimension", #FCN); \ |
|
536 return retval; \ |
|
537 } \ |
|
538 } \ |
|
539 else \ |
|
540 { \ |
|
541 dim = 0; \ |
|
542 while ((dim < dv.length ()) && (dv (dim) <= 1)) \ |
|
543 dim++; \ |
|
544 if (dim == dv.length ()) \ |
|
545 dim = 0; \ |
|
546 } \ |
|
547 \ |
|
548 bool single_arg = (nargin == 1) || arg2.is_empty(); \ |
|
549 \ |
|
550 if (single_arg && (nargout == 1 || nargout == 0)) \ |
|
551 { \ |
|
552 if (arg1.type_id () == octave_sparse_matrix::static_type_id ()) \ |
|
553 retval(0) = arg1.sparse_matrix_value () .FCN (dim); \ |
|
554 else if (arg1.type_id () == \ |
|
555 octave_sparse_complex_matrix::static_type_id ()) \ |
|
556 retval(0) = arg1.sparse_complex_matrix_value () .FCN (dim); \ |
|
557 else \ |
|
558 gripe_wrong_type_arg (#FCN, arg1); \ |
|
559 } \ |
|
560 else if (single_arg && nargout == 2) \ |
|
561 { \ |
5275
|
562 Array2<octave_idx_type> index; \ |
5164
|
563 \ |
|
564 if (arg1.type_id () == octave_sparse_matrix::static_type_id ()) \ |
|
565 retval(0) = arg1.sparse_matrix_value () .FCN (index, dim); \ |
|
566 else if (arg1.type_id () == \ |
|
567 octave_sparse_complex_matrix::static_type_id ()) \ |
|
568 retval(0) = arg1.sparse_complex_matrix_value () .FCN (index, dim); \ |
|
569 else \ |
|
570 gripe_wrong_type_arg (#FCN, arg1); \ |
|
571 \ |
5275
|
572 octave_idx_type len = index.numel (); \ |
5164
|
573 \ |
|
574 if (len > 0) \ |
|
575 { \ |
|
576 double nan_val = lo_ieee_nan_value (); \ |
|
577 \ |
|
578 NDArray idx (index.dims ()); \ |
|
579 \ |
5275
|
580 for (octave_idx_type i = 0; i < len; i++) \ |
5164
|
581 { \ |
|
582 OCTAVE_QUIT; \ |
5275
|
583 octave_idx_type tmp = index.elem (i) + 1; \ |
5164
|
584 idx.elem (i) = (tmp <= 0) \ |
|
585 ? nan_val : static_cast<double> (tmp); \ |
|
586 } \ |
|
587 \ |
|
588 retval(1) = idx; \ |
|
589 } \ |
|
590 else \ |
|
591 retval(1) = NDArray (); \ |
|
592 } \ |
|
593 else \ |
|
594 { \ |
|
595 int arg1_is_scalar = arg1.is_scalar_type (); \ |
|
596 int arg2_is_scalar = arg2.is_scalar_type (); \ |
|
597 \ |
|
598 int arg1_is_complex = arg1.is_complex_type (); \ |
|
599 int arg2_is_complex = arg2.is_complex_type (); \ |
|
600 \ |
|
601 if (arg1_is_scalar) \ |
|
602 { \ |
|
603 if (arg1_is_complex || arg2_is_complex) \ |
|
604 { \ |
|
605 Complex c1 = arg1.complex_value (); \ |
|
606 \ |
|
607 SparseComplexMatrix m2 = arg2.sparse_complex_matrix_value (); \ |
|
608 \ |
|
609 if (! error_state) \ |
|
610 { \ |
|
611 SparseComplexMatrix result = FCN (c1, m2); \ |
|
612 if (! error_state) \ |
|
613 retval(0) = result; \ |
|
614 } \ |
|
615 } \ |
|
616 else \ |
|
617 { \ |
|
618 double d1 = arg1.double_value (); \ |
|
619 SparseMatrix m2 = arg2.sparse_matrix_value (); \ |
|
620 \ |
|
621 if (! error_state) \ |
|
622 { \ |
|
623 SparseMatrix result = FCN (d1, m2); \ |
|
624 if (! error_state) \ |
|
625 retval(0) = result; \ |
|
626 } \ |
|
627 } \ |
|
628 } \ |
|
629 else if (arg2_is_scalar) \ |
|
630 { \ |
|
631 if (arg1_is_complex || arg2_is_complex) \ |
|
632 { \ |
|
633 SparseComplexMatrix m1 = arg1.sparse_complex_matrix_value (); \ |
|
634 \ |
|
635 if (! error_state) \ |
|
636 { \ |
|
637 Complex c2 = arg2.complex_value (); \ |
|
638 SparseComplexMatrix result = FCN (m1, c2); \ |
|
639 if (! error_state) \ |
|
640 retval(0) = result; \ |
|
641 } \ |
|
642 } \ |
|
643 else \ |
|
644 { \ |
|
645 SparseMatrix m1 = arg1.sparse_matrix_value (); \ |
|
646 \ |
|
647 if (! error_state) \ |
|
648 { \ |
|
649 double d2 = arg2.double_value (); \ |
|
650 SparseMatrix result = FCN (m1, d2); \ |
|
651 if (! error_state) \ |
|
652 retval(0) = result; \ |
|
653 } \ |
|
654 } \ |
|
655 } \ |
|
656 else \ |
|
657 { \ |
|
658 if (arg1_is_complex || arg2_is_complex) \ |
|
659 { \ |
|
660 SparseComplexMatrix m1 = arg1.sparse_complex_matrix_value (); \ |
|
661 \ |
|
662 if (! error_state) \ |
|
663 { \ |
|
664 SparseComplexMatrix m2 = arg2.sparse_complex_matrix_value (); \ |
|
665 \ |
|
666 if (! error_state) \ |
|
667 { \ |
|
668 SparseComplexMatrix result = FCN (m1, m2); \ |
|
669 if (! error_state) \ |
|
670 retval(0) = result; \ |
|
671 } \ |
|
672 } \ |
|
673 } \ |
|
674 else \ |
|
675 { \ |
|
676 SparseMatrix m1 = arg1.sparse_matrix_value (); \ |
|
677 \ |
|
678 if (! error_state) \ |
|
679 { \ |
|
680 SparseMatrix m2 = arg2.sparse_matrix_value (); \ |
|
681 \ |
|
682 if (! error_state) \ |
|
683 { \ |
|
684 SparseMatrix result = FCN (m1, m2); \ |
|
685 if (! error_state) \ |
|
686 retval(0) = result; \ |
|
687 } \ |
|
688 } \ |
|
689 } \ |
|
690 } \ |
|
691 } \ |
|
692 \ |
|
693 return retval |
|
694 |
|
695 // PKG_ADD: dispatch ("min", "spmin", "sparse matrix"); |
|
696 // PKG_ADD: dispatch ("min", "spmin", "sparse complex matrix"); |
|
697 // PKG_ADD: dispatch ("min", "spmin", "sparse bool matrix"); |
|
698 DEFUN_DLD (spmin, args, nargout, |
|
699 "-*- texinfo -*-\n\ |
|
700 @deftypefn {Mapping Function} {} spmin (@var{x}, @var{y}, @var{dim})\n\ |
|
701 @deftypefnx {Mapping Function} {[@var{w}, @var{iw}] =} spmin (@var{x})\n\ |
|
702 @cindex Utility Functions\n\ |
|
703 For a vector argument, return the minimum value. For a matrix\n\ |
|
704 argument, return the minimum value from each column, as a row\n\ |
|
705 vector, or over the dimension @var{dim} if defined. For two matrices\n\ |
|
706 (or a matrix and scalar), return the pair-wise minimum.\n\ |
|
707 Thus,\n\ |
|
708 \n\ |
|
709 @example\n\ |
|
710 min (min (@var{x}))\n\ |
|
711 @end example\n\ |
|
712 \n\ |
|
713 @noindent\n\ |
|
714 returns the smallest element of @var{x}, and\n\ |
|
715 \n\ |
|
716 @example\n\ |
|
717 @group\n\ |
|
718 min (2:5, pi)\n\ |
|
719 @result{} 2.0000 3.0000 3.1416 3.1416\n\ |
|
720 @end group\n\ |
|
721 @end example\n\ |
|
722 @noindent\n\ |
|
723 compares each element of the range @code{2:5} with @code{pi}, and\n\ |
|
724 returns a row vector of the minimum values.\n\ |
|
725 \n\ |
|
726 For complex arguments, the magnitude of the elements are used for\n\ |
|
727 comparison.\n\ |
|
728 \n\ |
|
729 If called with one input and two output arguments,\n\ |
|
730 @code{min} also returns the first index of the\n\ |
|
731 minimum value(s). Thus,\n\ |
|
732 \n\ |
|
733 @example\n\ |
|
734 @group\n\ |
|
735 [x, ix] = min ([1, 3, 0, 2, 5])\n\ |
|
736 @result{} x = 0\n\ |
|
737 ix = 3\n\ |
|
738 @end group\n\ |
|
739 @end example\n\ |
|
740 @end deftypefn") |
|
741 { |
|
742 MINMAX_BODY (min); |
|
743 } |
|
744 |
|
745 // PKG_ADD: dispatch ("max", "spmax", "sparse matrix"); |
|
746 // PKG_ADD: dispatch ("max", "spmax", "sparse complex matrix"); |
|
747 // PKG_ADD: dispatch ("max", "spmax", "sparse bool matrix"); |
|
748 DEFUN_DLD (spmax, args, nargout, |
|
749 "-*- texinfo -*-\n\ |
|
750 @deftypefn {Mapping Function} {} spmax (@var{x}, @var{y}, @var{dim})\n\ |
|
751 @deftypefnx {Mapping Function} {[@var{w}, @var{iw}] =} spmax (@var{x})\n\ |
|
752 @cindex Utility Functions\n\ |
|
753 For a vector argument, return the maximum value. For a matrix\n\ |
|
754 argument, return the maximum value from each column, as a row\n\ |
|
755 vector, or over the dimension @var{dim} if defined. For two matrices\n\ |
|
756 (or a matrix and scalar), return the pair-wise maximum.\n\ |
|
757 Thus,\n\ |
|
758 \n\ |
|
759 @example\n\ |
|
760 max (max (@var{x}))\n\ |
|
761 @end example\n\ |
|
762 \n\ |
|
763 @noindent\n\ |
|
764 returns the largest element of @var{x}, and\n\ |
|
765 \n\ |
|
766 @example\n\ |
|
767 @group\n\ |
|
768 max (2:5, pi)\n\ |
|
769 @result{} 3.1416 3.1416 4.0000 5.0000\n\ |
|
770 @end group\n\ |
|
771 @end example\n\ |
|
772 @noindent\n\ |
|
773 compares each element of the range @code{2:5} with @code{pi}, and\n\ |
|
774 returns a row vector of the maximum values.\n\ |
|
775 \n\ |
|
776 For complex arguments, the magnitude of the elements are used for\n\ |
|
777 comparison.\n\ |
|
778 \n\ |
|
779 If called with one input and two output arguments,\n\ |
|
780 @code{max} also returns the first index of the\n\ |
|
781 maximum value(s). Thus,\n\ |
|
782 \n\ |
|
783 @example\n\ |
|
784 @group\n\ |
|
785 [x, ix] = max ([1, 3, 5, 2, 5])\n\ |
|
786 @result{} x = 5\n\ |
|
787 ix = 3\n\ |
|
788 @end group\n\ |
|
789 @end example\n\ |
|
790 @end deftypefn") |
|
791 { |
|
792 MINMAX_BODY (max); |
|
793 } |
|
794 |
|
795 // PKG_ADD: dispatch ("atan2", "spatan2", "sparse matrix"); |
|
796 // PKG_ADD: dispatch ("atan2", "spatan2", "sparse complex matrix"); |
|
797 // PKG_ADD: dispatch ("atan2", "spatan2", "sparse bool matrix"); |
|
798 DEFUN_DLD (spatan2, args, , |
|
799 "-*- texinfo -*-\n\ |
|
800 @deftypefn {Loadable Function} {} spatan2 (@var{y}, @var{x})\n\ |
|
801 Compute atan (Y / X) for corresponding sparse matrix elements of Y and X.\n\ |
|
802 The result is in range -pi to pi.\n\ |
5646
|
803 @end deftypefn") |
5164
|
804 { |
|
805 octave_value retval; |
|
806 int nargin = args.length (); |
|
807 if (nargin == 2) { |
|
808 SparseMatrix a, b; |
|
809 double da, db; |
|
810 bool is_double_a = false; |
|
811 bool is_double_b = false; |
|
812 |
|
813 if (args(0).is_scalar_type ()) |
|
814 { |
|
815 is_double_a = true; |
|
816 da = args(0).double_value(); |
|
817 } |
|
818 else |
|
819 a = args(0).sparse_matrix_value (); |
|
820 |
|
821 if (args(1).is_scalar_type ()) |
|
822 { |
|
823 is_double_b = true; |
|
824 db = args(1).double_value(); |
|
825 } |
|
826 else |
|
827 b = args(1).sparse_matrix_value (); |
|
828 |
|
829 if (is_double_a && is_double_b) |
|
830 retval = Matrix (1, 1, atan2(da, db)); |
|
831 else if (is_double_a) |
|
832 retval = atan2 (da, b); |
|
833 else if (is_double_b) |
|
834 retval = atan2 (a, db); |
|
835 else |
|
836 retval = atan2 (a, b); |
|
837 |
|
838 } else |
5823
|
839 print_usage (); |
5164
|
840 |
|
841 return retval; |
|
842 } |
|
843 |
|
844 static octave_value |
|
845 make_spdiag (const octave_value& a, const octave_value& b) |
|
846 { |
|
847 octave_value retval; |
|
848 |
|
849 if (a.is_complex_type ()) |
|
850 { |
|
851 SparseComplexMatrix m = a.sparse_complex_matrix_value (); |
5275
|
852 octave_idx_type k = b.nint_value(true); |
5164
|
853 |
|
854 if (error_state) |
|
855 return retval; |
|
856 |
5275
|
857 octave_idx_type nr = m.rows (); |
|
858 octave_idx_type nc = m.columns (); |
5164
|
859 |
|
860 if (nr == 0 || nc == 0) |
|
861 retval = m; |
|
862 else if (nr == 1 || nc == 1) |
|
863 { |
5275
|
864 octave_idx_type roff = 0; |
|
865 octave_idx_type coff = 0; |
5164
|
866 if (k > 0) |
|
867 { |
|
868 roff = 0; |
|
869 coff = k; |
|
870 } |
|
871 else if (k < 0) |
|
872 { |
|
873 k = -k; |
|
874 roff = k; |
|
875 coff = 0; |
|
876 } |
|
877 |
|
878 if (nr == 1) |
|
879 { |
5275
|
880 octave_idx_type n = nc + k; |
5604
|
881 octave_idx_type nz = m.nzmax (); |
5164
|
882 SparseComplexMatrix r (n, n, nz); |
5275
|
883 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
884 r.xcidx (i) = 0; |
5275
|
885 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
886 { |
5275
|
887 for (octave_idx_type i = m.cidx(j); i < m.cidx(j+1); i++) |
5164
|
888 { |
|
889 r.xdata (i) = m.data (i); |
|
890 r.xridx (i) = j + roff; |
|
891 } |
|
892 r.xcidx (j+coff+1) = m.cidx(j+1); |
|
893 } |
5275
|
894 for (octave_idx_type i = nc+coff+1; i < n+1; i++) |
5164
|
895 r.xcidx (i) = nz; |
|
896 retval = r; |
|
897 } |
|
898 else |
|
899 { |
5275
|
900 octave_idx_type n = nr + k; |
5604
|
901 octave_idx_type nz = m.nzmax (); |
5275
|
902 octave_idx_type ii = 0; |
|
903 octave_idx_type ir = m.ridx(0); |
5164
|
904 SparseComplexMatrix r (n, n, nz); |
5275
|
905 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
906 r.xcidx (i) = 0; |
5275
|
907 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
908 { |
|
909 if (ir == i) |
|
910 { |
|
911 r.xdata (ii) = m.data (ii); |
|
912 r.xridx (ii++) = ir + roff; |
|
913 if (ii != nz) |
|
914 ir = m.ridx (ii); |
|
915 } |
|
916 r.xcidx (i+coff+1) = ii; |
|
917 } |
5275
|
918 for (octave_idx_type i = nr+coff+1; i < n+1; i++) |
5164
|
919 r.xcidx (i) = nz; |
|
920 retval = r; |
|
921 } |
|
922 } |
|
923 else |
|
924 { |
|
925 SparseComplexMatrix r = m.diag (k); |
|
926 // Don't use numel, since it can overflow for very large matrices |
|
927 if (r.rows () > 0 && r.cols () > 0) |
|
928 retval = r; |
|
929 } |
|
930 } |
|
931 else if (a.is_real_type ()) |
|
932 { |
|
933 SparseMatrix m = a.sparse_matrix_value (); |
|
934 |
5275
|
935 octave_idx_type k = b.nint_value(true); |
5164
|
936 |
|
937 if (error_state) |
|
938 return retval; |
|
939 |
5275
|
940 octave_idx_type nr = m.rows (); |
|
941 octave_idx_type nc = m.columns (); |
5164
|
942 |
|
943 if (nr == 0 || nc == 0) |
|
944 retval = m; |
|
945 else if (nr == 1 || nc == 1) |
|
946 { |
5275
|
947 octave_idx_type roff = 0; |
|
948 octave_idx_type coff = 0; |
5164
|
949 if (k > 0) |
|
950 { |
|
951 roff = 0; |
|
952 coff = k; |
|
953 } |
|
954 else if (k < 0) |
|
955 { |
|
956 k = -k; |
|
957 roff = k; |
|
958 coff = 0; |
|
959 } |
|
960 |
|
961 if (nr == 1) |
|
962 { |
5275
|
963 octave_idx_type n = nc + k; |
5604
|
964 octave_idx_type nz = m.nzmax (); |
5164
|
965 SparseMatrix r (n, n, nz); |
|
966 |
5275
|
967 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
968 r.xcidx (i) = 0; |
5275
|
969 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
970 { |
5275
|
971 for (octave_idx_type i = m.cidx(j); i < m.cidx(j+1); i++) |
5164
|
972 { |
|
973 r.xdata (i) = m.data (i); |
|
974 r.xridx (i) = j + roff; |
|
975 } |
|
976 r.xcidx (j+coff+1) = m.cidx(j+1); |
|
977 } |
5275
|
978 for (octave_idx_type i = nc+coff+1; i < n+1; i++) |
5164
|
979 r.xcidx (i) = nz; |
|
980 retval = r; |
|
981 } |
|
982 else |
|
983 { |
5275
|
984 octave_idx_type n = nr + k; |
5604
|
985 octave_idx_type nz = m.nzmax (); |
5275
|
986 octave_idx_type ii = 0; |
|
987 octave_idx_type ir = m.ridx(0); |
5164
|
988 SparseMatrix r (n, n, nz); |
5275
|
989 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
990 r.xcidx (i) = 0; |
5275
|
991 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
992 { |
|
993 if (ir == i) |
|
994 { |
|
995 r.xdata (ii) = m.data (ii); |
|
996 r.xridx (ii++) = ir + roff; |
|
997 if (ii != nz) |
|
998 ir = m.ridx (ii); |
|
999 } |
|
1000 r.xcidx (i+coff+1) = ii; |
|
1001 } |
5275
|
1002 for (octave_idx_type i = nr+coff+1; i < n+1; i++) |
5164
|
1003 r.xcidx (i) = nz; |
|
1004 retval = r; |
|
1005 } |
|
1006 } |
|
1007 else |
|
1008 { |
|
1009 SparseMatrix r = m.diag (k); |
|
1010 if (r.rows () > 0 && r.cols () > 0) |
|
1011 retval = r; |
|
1012 } |
|
1013 } |
|
1014 else |
|
1015 gripe_wrong_type_arg ("spdiag", a); |
|
1016 |
|
1017 return retval; |
|
1018 } |
|
1019 |
6771
|
1020 static octave_value |
|
1021 make_spdiag (const octave_value& a) |
|
1022 { |
|
1023 octave_value retval; |
|
1024 octave_idx_type nr = a.rows (); |
|
1025 octave_idx_type nc = a.columns (); |
|
1026 |
|
1027 if (nr == 0 || nc == 0) |
|
1028 retval = SparseMatrix (); |
|
1029 else |
|
1030 retval = make_spdiag (a, octave_value (0.)); |
|
1031 |
|
1032 return retval; |
|
1033 } |
|
1034 |
5164
|
1035 // PKG_ADD: dispatch ("diag", "spdiag", "sparse matrix"); |
|
1036 // PKG_ADD: dispatch ("diag", "spdiag", "sparse complex matrix"); |
|
1037 // PKG_ADD: dispatch ("diag", "spdiag", "sparse bool matrix"); |
|
1038 DEFUN_DLD (spdiag, args, , |
|
1039 "-*- texinfo -*-\n\ |
|
1040 @deftypefn {Loadable Function} {} spdiag (@var{v}, @var{k})\n\ |
|
1041 Return a diagonal matrix with the sparse vector @var{v} on diagonal\n\ |
|
1042 @var{k}. The second argument is optional. If it is positive, the vector is\n\ |
|
1043 placed on the @var{k}-th super-diagonal. If it is negative, it is placed\n\ |
|
1044 on the @var{-k}-th sub-diagonal. The default value of @var{k} is 0, and\n\ |
|
1045 the vector is placed on the main diagonal. For example,\n\ |
|
1046 \n\ |
|
1047 @example\n\ |
6772
|
1048 @group\n\ |
5164
|
1049 spdiag ([1, 2, 3], 1)\n\ |
|
1050 ans =\n\ |
|
1051 \n\ |
|
1052 Compressed Column Sparse (rows=4, cols=4, nnz=3)\n\ |
|
1053 (1 , 2) -> 1\n\ |
|
1054 (2 , 3) -> 2\n\ |
|
1055 (3 , 4) -> 3\n\ |
6772
|
1056 @end group\n\ |
5164
|
1057 @end example\n\ |
6772
|
1058 \n\ |
|
1059 @noindent\n\ |
|
1060 Given a matrix argument, instead of a vector, @code{spdiag} extracts the\n\ |
6774
|
1061 @var{k}-th diagonal of the sparse matrix.\n\ |
5642
|
1062 @seealso{diag}\n\ |
|
1063 @end deftypefn") |
5164
|
1064 { |
|
1065 octave_value retval; |
|
1066 |
|
1067 int nargin = args.length (); |
|
1068 |
|
1069 if (nargin == 1 && args(0).is_defined ()) |
6771
|
1070 retval = make_spdiag (args(0)); |
5164
|
1071 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
|
1072 retval = make_spdiag (args(0), args(1)); |
|
1073 else |
5823
|
1074 print_usage (); |
5164
|
1075 |
|
1076 return retval; |
|
1077 } |
|
1078 |
|
1079 /* |
|
1080 ;;; Local Variables: *** |
|
1081 ;;; mode: C++ *** |
|
1082 ;;; End: *** |
|
1083 */ |