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