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{i}, @var{j}, @var{sv}, @var{m}, @var{n}, @var{nzmax})\n\ |
|
70 Create a sparse matrix given integer index vectors @var{i} and @var{j},\n\ |
|
71 a 1-by-@code{nnz} vector of real of complex values @var{sv}, overall\n\ |
|
72 dimensions @var{m} and @var{n} of the sparse matrix. The argument\n\ |
7001
|
73 @code{nzmax} is ignored but accepted for compatibility with @sc{Matlab}.\n\ |
5164
|
74 \n\ |
6556
|
75 @strong{Note}: if multiple values are specified with the same\n\ |
|
76 @var{i}, @var{j} indices, the corresponding values in @var{s} will\n\ |
|
77 be added.\n\ |
|
78 \n\ |
6557
|
79 The following are all equivalent:\n\ |
5164
|
80 \n\ |
6556
|
81 @example\n\ |
|
82 @group\n\ |
|
83 s = sparse (i, j, s, m, n)\n\ |
|
84 s = sparse (i, j, s, m, n, \"summation\")\n\ |
|
85 s = sparse (i, j, s, m, n, \"sum\")\n\ |
|
86 @end group\n\ |
|
87 @end example\n\ |
5164
|
88 \n\ |
6556
|
89 @deftypefnx {Loadable Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{s}, @var{m}, @var{n}, \"unique\")\n\ |
|
90 Same as above, except that if more than two values are specified for the\n\ |
|
91 same @var{i}, @var{j} indices, the last specified value will be used.\n\ |
5164
|
92 \n\ |
6556
|
93 @deftypefnx {Loadable Function} {@var{s} =} sparse (@var{i}, @var{j}, @var{sv})\n\ |
|
94 Uses @code{@var{m} = max (@var{i})}, @code{@var{n} = max (@var{j})}\n\ |
5164
|
95 \n\ |
6556
|
96 @deftypefnx {Loadable Function} {@var{s} =} sparse (@var{m}, @var{n})\n\ |
|
97 Equivalent to @code{sparse ([], [], [], @var{m}, @var{n}, 0)}\n\ |
|
98 \n\ |
|
99 If any of @var{sv}, @var{i} or @var{j} are scalars, they are expanded\n\ |
|
100 to have a common size.\n\ |
5164
|
101 @seealso{full}\n\ |
|
102 @end deftypefn") |
|
103 { |
|
104 octave_value retval; |
|
105 |
|
106 // WARNING: This function should always use constructions like |
|
107 // retval = new octave_sparse_matrix (sm); |
|
108 // To avoid calling the maybe_mutate function. This is the only |
7287
|
109 // function that should not call maybe_mutate |
5164
|
110 |
|
111 int nargin= args.length(); |
|
112 if (nargin < 1 || (nargin == 4 && !args(3).is_string ()) || nargin > 6) |
|
113 { |
5823
|
114 print_usage (); |
5164
|
115 return retval; |
|
116 } |
|
117 |
|
118 bool use_complex = false; |
|
119 bool use_bool = false; |
|
120 if (nargin > 2) |
|
121 { |
|
122 use_complex= args(2).is_complex_type(); |
|
123 use_bool = args(2).is_bool_type (); |
|
124 } |
|
125 else |
|
126 { |
|
127 use_complex= args(0).is_complex_type(); |
|
128 use_bool = args(0).is_bool_type (); |
|
129 } |
|
130 |
7287
|
131 if (nargin == 1) |
5164
|
132 { |
|
133 octave_value arg = args (0); |
|
134 |
|
135 if (is_sparse (arg)) |
|
136 { |
|
137 if (use_complex) |
|
138 { |
5760
|
139 SparseComplexMatrix sm = arg.sparse_complex_matrix_value (); |
5164
|
140 retval = new octave_sparse_complex_matrix (sm); |
|
141 } |
|
142 else if (use_bool) |
|
143 { |
5760
|
144 SparseBoolMatrix sm = arg.sparse_bool_matrix_value (); |
5164
|
145 retval = new octave_sparse_bool_matrix (sm); |
|
146 } |
|
147 else |
|
148 { |
5760
|
149 SparseMatrix sm = arg.sparse_matrix_value (); |
5164
|
150 retval = new octave_sparse_matrix (sm); |
|
151 } |
|
152 } |
|
153 else |
|
154 { |
|
155 if (use_complex) |
|
156 { |
|
157 SparseComplexMatrix sm (args (0).complex_matrix_value ()); |
|
158 if (error_state) |
|
159 return retval; |
|
160 retval = new octave_sparse_complex_matrix (sm); |
|
161 } |
|
162 else if (use_bool) |
|
163 { |
|
164 SparseBoolMatrix sm (args (0).bool_matrix_value ()); |
|
165 if (error_state) |
|
166 return retval; |
|
167 retval = new octave_sparse_bool_matrix (sm); |
|
168 } |
|
169 else |
|
170 { |
|
171 SparseMatrix sm (args (0).matrix_value ()); |
|
172 if (error_state) |
|
173 return retval; |
|
174 retval = new octave_sparse_matrix (sm); |
|
175 } |
|
176 } |
|
177 } |
|
178 else |
|
179 { |
5275
|
180 octave_idx_type m = 1, n = 1; |
5164
|
181 if (nargin == 2) |
|
182 { |
7308
|
183 if (args(0).numel () == 1 && args(1).numel () == 1) |
|
184 { |
|
185 m = args(0).int_value(); |
|
186 n = args(1).int_value(); |
|
187 if (error_state) return retval; |
5164
|
188 |
7308
|
189 if (use_complex) |
|
190 retval = new octave_sparse_complex_matrix |
|
191 (SparseComplexMatrix (m, n)); |
|
192 else if (use_bool) |
|
193 retval = new octave_sparse_bool_matrix |
|
194 (SparseBoolMatrix (m, n)); |
|
195 else |
|
196 retval = new octave_sparse_matrix |
|
197 (SparseMatrix (m, n)); |
|
198 } |
5164
|
199 else |
7308
|
200 error ("sparse: expecting scalar values"); |
5164
|
201 } |
|
202 else |
|
203 { |
|
204 if (args(0).is_empty () || args (1).is_empty () |
|
205 || args(2).is_empty ()) |
|
206 { |
|
207 if (nargin > 4) |
|
208 { |
|
209 m = args(3).int_value(); |
|
210 n = args(4).int_value(); |
|
211 } |
|
212 |
|
213 if (use_bool) |
|
214 retval = new octave_sparse_bool_matrix |
|
215 (SparseBoolMatrix (m, n)); |
|
216 else |
|
217 retval = new octave_sparse_matrix (SparseMatrix (m, n)); |
|
218 } |
|
219 else |
|
220 { |
|
221 // |
|
222 // I use this clumsy construction so that we can use |
|
223 // any orientation of args |
|
224 ColumnVector ridxA = ColumnVector (args(0).vector_value |
|
225 (false, true)); |
|
226 ColumnVector cidxA = ColumnVector (args(1).vector_value |
|
227 (false, true)); |
|
228 ColumnVector coefA; |
|
229 boolNDArray coefAB; |
|
230 ComplexColumnVector coefAC; |
|
231 bool assemble_do_sum = true; // this is the default in matlab6 |
|
232 |
|
233 if (use_complex) |
|
234 { |
|
235 if (args(2).is_empty ()) |
|
236 coefAC = ComplexColumnVector (0); |
|
237 else |
|
238 coefAC = ComplexColumnVector |
|
239 (args(2).complex_vector_value (false, true)); |
|
240 } |
|
241 else if (use_bool) |
|
242 { |
|
243 if (args(2).is_empty ()) |
|
244 coefAB = boolNDArray (dim_vector (1, 0)); |
|
245 else |
|
246 coefAB = args(2).bool_array_value (); |
|
247 dim_vector AB_dims = coefAB.dims (); |
|
248 if (AB_dims.length() > 2 || (AB_dims(0) != 1 && |
|
249 AB_dims(1) != 1)) |
|
250 error ("sparse: vector arguments required"); |
|
251 } |
|
252 else |
|
253 if (args(2).is_empty ()) |
|
254 coefA = ColumnVector (0); |
|
255 else |
|
256 coefA = ColumnVector (args(2).vector_value (false, true)); |
|
257 |
|
258 if (error_state) |
|
259 return retval; |
|
260 |
|
261 // Confirm that i,j,s all have the same number of elements |
5275
|
262 octave_idx_type ns; |
5164
|
263 if (use_complex) |
|
264 ns = coefAC.length(); |
|
265 else if (use_bool) |
|
266 ns = coefAB.length(); |
|
267 else |
|
268 ns = coefA.length(); |
|
269 |
5275
|
270 octave_idx_type ni = ridxA.length(); |
|
271 octave_idx_type nj = cidxA.length(); |
|
272 octave_idx_type nnz = (ni > nj ? ni : nj); |
5164
|
273 if ((ns != 1 && ns != nnz) || |
|
274 (ni != 1 && ni != nnz) || |
|
275 (nj != 1 && nj != nnz)) |
|
276 { |
|
277 error ("sparse i, j and s must have the same length"); |
|
278 return retval; |
|
279 } |
|
280 |
|
281 if (nargin == 3 || nargin == 4) |
|
282 { |
5275
|
283 m = static_cast<octave_idx_type> (ridxA.max()); |
|
284 n = static_cast<octave_idx_type> (cidxA.max()); |
5164
|
285 |
|
286 // if args(3) is not string, then ignore the value |
|
287 // otherwise check for summation or unique |
|
288 if (nargin == 4 && args(3).is_string()) |
|
289 { |
|
290 std::string vv= args(3).string_value(); |
|
291 if (error_state) return retval; |
|
292 |
|
293 if ( vv == "summation" || |
|
294 vv == "sum" ) |
|
295 assemble_do_sum = true; |
|
296 else |
|
297 if ( vv == "unique" ) |
|
298 assemble_do_sum = false; |
|
299 else { |
|
300 error("sparse repeat flag must be 'sum' or 'unique'"); |
|
301 return retval; |
|
302 } |
|
303 } |
|
304 } |
|
305 else |
|
306 { |
|
307 m = args(3).int_value(); |
|
308 n = args(4).int_value(); |
|
309 if (error_state) |
|
310 return retval; |
|
311 |
|
312 // if args(5) is not string, then ignore the value |
|
313 // otherwise check for summation or unique |
|
314 if (nargin >= 6 && args(5).is_string()) |
|
315 { |
|
316 std::string vv= args(5).string_value(); |
|
317 if (error_state) return retval; |
|
318 |
|
319 if ( vv == "summation" || |
|
320 vv == "sum" ) |
|
321 assemble_do_sum = true; |
|
322 else |
|
323 if ( vv == "unique" ) |
|
324 assemble_do_sum = false; |
|
325 else { |
|
326 error("sparse repeat flag must be 'sum' or 'unique'"); |
|
327 return retval; |
|
328 } |
|
329 } |
|
330 |
|
331 } |
|
332 |
|
333 // Convert indexing to zero-indexing used internally |
|
334 ridxA -= 1.; |
|
335 cidxA -= 1.; |
|
336 |
|
337 if (use_complex) |
|
338 retval = new octave_sparse_complex_matrix |
|
339 (SparseComplexMatrix (coefAC, ridxA, cidxA, m, n, |
|
340 assemble_do_sum)); |
|
341 else if (use_bool) |
|
342 retval = new octave_sparse_bool_matrix |
|
343 (SparseBoolMatrix (coefAB, ridxA, cidxA, m, n, |
|
344 assemble_do_sum)); |
|
345 else |
|
346 retval = new octave_sparse_matrix |
|
347 (SparseMatrix (coefA, ridxA, cidxA, m, n, |
|
348 assemble_do_sum)); |
|
349 } |
|
350 } |
|
351 } |
|
352 |
|
353 return retval; |
|
354 } |
|
355 |
|
356 DEFUN_DLD (full, args, , |
|
357 "-*- texinfo -*-\n\ |
|
358 @deftypefn {Loadable Function} {@var{FM} =} full (@var{SM})\n\ |
|
359 returns a full storage matrix from a sparse one\n\ |
|
360 @seealso{sparse}\n\ |
|
361 @end deftypefn") |
|
362 { |
|
363 octave_value retval; |
|
364 |
5760
|
365 if (args.length() < 1) |
|
366 { |
5823
|
367 print_usage (); |
5760
|
368 return retval; |
|
369 } |
5164
|
370 |
5631
|
371 if (args(0).is_sparse_type ()) |
5164
|
372 { |
|
373 if (args(0).type_name () == "sparse matrix") |
|
374 retval = args(0).matrix_value (); |
|
375 else if (args(0).type_name () == "sparse complex matrix") |
|
376 retval = args(0).complex_matrix_value (); |
|
377 else if (args(0).type_name () == "sparse bool matrix") |
|
378 retval = args(0).bool_matrix_value (); |
|
379 } |
|
380 else if (args(0).is_real_type()) |
|
381 retval = args(0).matrix_value(); |
|
382 else if (args(0).is_complex_type()) |
|
383 retval = args(0).complex_matrix_value(); |
|
384 else |
|
385 gripe_wrong_type_arg ("full", args(0)); |
|
386 |
|
387 return retval; |
|
388 } |
|
389 |
|
390 #define SPARSE_DIM_ARG_BODY(NAME, FUNC) \ |
|
391 int nargin = args.length(); \ |
|
392 octave_value retval; \ |
|
393 if ((nargin != 1 ) && (nargin != 2)) \ |
5823
|
394 print_usage (); \ |
5164
|
395 else { \ |
|
396 int dim = (nargin == 1 ? -1 : args(1).int_value(true) - 1); \ |
|
397 if (error_state) return retval; \ |
|
398 if (dim < -1 || dim > 1) { \ |
|
399 error (#NAME ": invalid dimension argument = %d", dim + 1); \ |
|
400 return retval; \ |
|
401 } \ |
|
402 if (args(0).type_id () == \ |
|
403 octave_sparse_matrix::static_type_id () || args(0).type_id () == \ |
|
404 octave_sparse_bool_matrix::static_type_id ()) { \ |
|
405 retval = args(0).sparse_matrix_value () .FUNC (dim); \ |
|
406 } else if (args(0).type_id () == \ |
|
407 octave_sparse_complex_matrix::static_type_id ()) { \ |
|
408 retval = args(0).sparse_complex_matrix_value () .FUNC (dim); \ |
|
409 } else \ |
5823
|
410 print_usage (); \ |
5164
|
411 } \ |
|
412 return retval |
|
413 |
|
414 // PKG_ADD: dispatch ("prod", "spprod", "sparse matrix"); |
|
415 // PKG_ADD: dispatch ("prod", "spprod", "sparse complex matrix"); |
|
416 // PKG_ADD: dispatch ("prod", "spprod", "sparse bool matrix"); |
|
417 DEFUN_DLD (spprod, args, , |
|
418 "-*- texinfo -*-\n\ |
|
419 @deftypefn {Loadable Function} {@var{y} =} spprod (@var{x},@var{dim})\n\ |
|
420 Product of elements along dimension @var{dim}. If @var{dim} is omitted,\n\ |
|
421 it defaults to 1 (column-wise products).\n\ |
5642
|
422 @seealso{spsum, spsumsq}\n\ |
|
423 @end deftypefn") |
5164
|
424 { |
|
425 SPARSE_DIM_ARG_BODY (spprod, prod); |
|
426 } |
|
427 |
|
428 // PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse matrix"); |
|
429 // PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse complex matrix"); |
|
430 // PKG_ADD: dispatch ("cumprod", "spcumprod", "sparse bool matrix"); |
|
431 DEFUN_DLD (spcumprod, args, , |
|
432 "-*- texinfo -*-\n\ |
|
433 @deftypefn {Loadable Function} {@var{y} =} spcumprod (@var{x},@var{dim})\n\ |
|
434 Cumulative product of elements along dimension @var{dim}. If @var{dim}\n\ |
|
435 is omitted, it defaults to 1 (column-wise cumulative products).\n\ |
5642
|
436 @seealso{spcumsum}\n\ |
|
437 @end deftypefn") |
5164
|
438 { |
|
439 SPARSE_DIM_ARG_BODY (spcumprod, cumprod); |
|
440 } |
|
441 |
|
442 // PKG_ADD: dispatch ("sum", "spsum", "sparse matrix"); |
|
443 // PKG_ADD: dispatch ("sum", "spsum", "sparse complex matrix"); |
|
444 // PKG_ADD: dispatch ("sum", "spsum", "sparse bool matrix"); |
|
445 DEFUN_DLD (spsum, args, , |
|
446 "-*- texinfo -*-\n\ |
|
447 @deftypefn {Loadable Function} {@var{y} =} spsum (@var{x},@var{dim})\n\ |
|
448 Sum of elements along dimension @var{dim}. If @var{dim} is omitted, it\n\ |
|
449 defaults to 1 (column-wise sum).\n\ |
5642
|
450 @seealso{spprod, spsumsq}\n\ |
|
451 @end deftypefn") |
5164
|
452 { |
|
453 SPARSE_DIM_ARG_BODY (spsum, sum); |
|
454 } |
|
455 |
|
456 // PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse matrix"); |
|
457 // PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse complex matrix"); |
|
458 // PKG_ADD: dispatch ("cumsum", "spcumsum", "sparse bool matrix"); |
|
459 DEFUN_DLD (spcumsum, args, , |
|
460 "-*- texinfo -*-\n\ |
|
461 @deftypefn {Loadable Function} {@var{y} =} spcumsum (@var{x},@var{dim})\n\ |
|
462 Cumulative sum of elements along dimension @var{dim}. If @var{dim}\n\ |
|
463 is omitted, it defaults to 1 (column-wise cumulative sums).\n\ |
5642
|
464 @seealso{spcumprod}\n\ |
|
465 @end deftypefn") |
5164
|
466 { |
|
467 SPARSE_DIM_ARG_BODY (spcumsum, cumsum); |
|
468 } |
|
469 |
|
470 // PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse matrix"); |
|
471 // PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse complex matrix"); |
|
472 // PKG_ADD: dispatch ("sumsq", "spsumsq", "sparse bool matrix"); |
|
473 DEFUN_DLD (spsumsq, args, , |
|
474 "-*- texinfo -*-\n\ |
|
475 @deftypefn {Loadable Function} {@var{y} =} spsumsq (@var{x},@var{dim})\n\ |
|
476 Sum of squares of elements along dimension @var{dim}. If @var{dim}\n\ |
|
477 is omitted, it defaults to 1 (column-wise sum of squares).\n\ |
|
478 This function is equivalent to computing\n\ |
|
479 @example\n\ |
|
480 spsum (x .* spconj (x), dim)\n\ |
|
481 @end example\n\ |
|
482 but it uses less memory and avoids calling @code{spconj} if @var{x} is\n\ |
|
483 real.\n\ |
5642
|
484 @seealso{spprod, spsum}\n\ |
|
485 @end deftypefn") |
5164
|
486 { |
|
487 SPARSE_DIM_ARG_BODY (spsumsq, sumsq); |
|
488 } |
|
489 |
|
490 |
|
491 static octave_value |
|
492 make_spdiag (const octave_value& a, const octave_value& b) |
|
493 { |
|
494 octave_value retval; |
|
495 |
|
496 if (a.is_complex_type ()) |
|
497 { |
|
498 SparseComplexMatrix m = a.sparse_complex_matrix_value (); |
5275
|
499 octave_idx_type k = b.nint_value(true); |
5164
|
500 |
|
501 if (error_state) |
|
502 return retval; |
|
503 |
5275
|
504 octave_idx_type nr = m.rows (); |
|
505 octave_idx_type nc = m.columns (); |
5164
|
506 |
|
507 if (nr == 0 || nc == 0) |
|
508 retval = m; |
|
509 else if (nr == 1 || nc == 1) |
|
510 { |
5275
|
511 octave_idx_type roff = 0; |
|
512 octave_idx_type coff = 0; |
5164
|
513 if (k > 0) |
|
514 { |
|
515 roff = 0; |
|
516 coff = k; |
|
517 } |
|
518 else if (k < 0) |
|
519 { |
|
520 k = -k; |
|
521 roff = k; |
|
522 coff = 0; |
|
523 } |
|
524 |
|
525 if (nr == 1) |
|
526 { |
5275
|
527 octave_idx_type n = nc + k; |
5604
|
528 octave_idx_type nz = m.nzmax (); |
5164
|
529 SparseComplexMatrix r (n, n, nz); |
5275
|
530 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
531 r.xcidx (i) = 0; |
5275
|
532 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
533 { |
5275
|
534 for (octave_idx_type i = m.cidx(j); i < m.cidx(j+1); i++) |
5164
|
535 { |
|
536 r.xdata (i) = m.data (i); |
|
537 r.xridx (i) = j + roff; |
|
538 } |
|
539 r.xcidx (j+coff+1) = m.cidx(j+1); |
|
540 } |
5275
|
541 for (octave_idx_type i = nc+coff+1; i < n+1; i++) |
5164
|
542 r.xcidx (i) = nz; |
|
543 retval = r; |
|
544 } |
|
545 else |
|
546 { |
5275
|
547 octave_idx_type n = nr + k; |
5604
|
548 octave_idx_type nz = m.nzmax (); |
5275
|
549 octave_idx_type ii = 0; |
|
550 octave_idx_type ir = m.ridx(0); |
5164
|
551 SparseComplexMatrix r (n, n, nz); |
5275
|
552 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
553 r.xcidx (i) = 0; |
5275
|
554 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
555 { |
|
556 if (ir == i) |
|
557 { |
|
558 r.xdata (ii) = m.data (ii); |
|
559 r.xridx (ii++) = ir + roff; |
|
560 if (ii != nz) |
|
561 ir = m.ridx (ii); |
|
562 } |
|
563 r.xcidx (i+coff+1) = ii; |
|
564 } |
5275
|
565 for (octave_idx_type i = nr+coff+1; i < n+1; i++) |
5164
|
566 r.xcidx (i) = nz; |
|
567 retval = r; |
|
568 } |
|
569 } |
|
570 else |
|
571 { |
|
572 SparseComplexMatrix r = m.diag (k); |
|
573 // Don't use numel, since it can overflow for very large matrices |
|
574 if (r.rows () > 0 && r.cols () > 0) |
|
575 retval = r; |
|
576 } |
|
577 } |
|
578 else if (a.is_real_type ()) |
|
579 { |
|
580 SparseMatrix m = a.sparse_matrix_value (); |
|
581 |
5275
|
582 octave_idx_type k = b.nint_value(true); |
5164
|
583 |
|
584 if (error_state) |
|
585 return retval; |
|
586 |
5275
|
587 octave_idx_type nr = m.rows (); |
|
588 octave_idx_type nc = m.columns (); |
5164
|
589 |
|
590 if (nr == 0 || nc == 0) |
|
591 retval = m; |
|
592 else if (nr == 1 || nc == 1) |
|
593 { |
5275
|
594 octave_idx_type roff = 0; |
|
595 octave_idx_type coff = 0; |
5164
|
596 if (k > 0) |
|
597 { |
|
598 roff = 0; |
|
599 coff = k; |
|
600 } |
|
601 else if (k < 0) |
|
602 { |
|
603 k = -k; |
|
604 roff = k; |
|
605 coff = 0; |
|
606 } |
|
607 |
|
608 if (nr == 1) |
|
609 { |
5275
|
610 octave_idx_type n = nc + k; |
5604
|
611 octave_idx_type nz = m.nzmax (); |
5164
|
612 SparseMatrix r (n, n, nz); |
|
613 |
5275
|
614 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
615 r.xcidx (i) = 0; |
5275
|
616 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
617 { |
5275
|
618 for (octave_idx_type i = m.cidx(j); i < m.cidx(j+1); i++) |
5164
|
619 { |
|
620 r.xdata (i) = m.data (i); |
|
621 r.xridx (i) = j + roff; |
|
622 } |
|
623 r.xcidx (j+coff+1) = m.cidx(j+1); |
|
624 } |
5275
|
625 for (octave_idx_type i = nc+coff+1; i < n+1; i++) |
5164
|
626 r.xcidx (i) = nz; |
|
627 retval = r; |
|
628 } |
|
629 else |
|
630 { |
5275
|
631 octave_idx_type n = nr + k; |
5604
|
632 octave_idx_type nz = m.nzmax (); |
5275
|
633 octave_idx_type ii = 0; |
|
634 octave_idx_type ir = m.ridx(0); |
5164
|
635 SparseMatrix r (n, n, nz); |
5275
|
636 for (octave_idx_type i = 0; i < coff+1; i++) |
5164
|
637 r.xcidx (i) = 0; |
5275
|
638 for (octave_idx_type i = 0; i < nr; i++) |
5164
|
639 { |
|
640 if (ir == i) |
|
641 { |
|
642 r.xdata (ii) = m.data (ii); |
|
643 r.xridx (ii++) = ir + roff; |
|
644 if (ii != nz) |
|
645 ir = m.ridx (ii); |
|
646 } |
|
647 r.xcidx (i+coff+1) = ii; |
|
648 } |
5275
|
649 for (octave_idx_type i = nr+coff+1; i < n+1; i++) |
5164
|
650 r.xcidx (i) = nz; |
|
651 retval = r; |
|
652 } |
|
653 } |
|
654 else |
|
655 { |
|
656 SparseMatrix r = m.diag (k); |
|
657 if (r.rows () > 0 && r.cols () > 0) |
|
658 retval = r; |
|
659 } |
|
660 } |
|
661 else |
|
662 gripe_wrong_type_arg ("spdiag", a); |
|
663 |
|
664 return retval; |
|
665 } |
|
666 |
6771
|
667 static octave_value |
|
668 make_spdiag (const octave_value& a) |
|
669 { |
|
670 octave_value retval; |
|
671 octave_idx_type nr = a.rows (); |
|
672 octave_idx_type nc = a.columns (); |
|
673 |
|
674 if (nr == 0 || nc == 0) |
|
675 retval = SparseMatrix (); |
|
676 else |
|
677 retval = make_spdiag (a, octave_value (0.)); |
|
678 |
|
679 return retval; |
|
680 } |
|
681 |
5164
|
682 // PKG_ADD: dispatch ("diag", "spdiag", "sparse matrix"); |
|
683 // PKG_ADD: dispatch ("diag", "spdiag", "sparse complex matrix"); |
|
684 // PKG_ADD: dispatch ("diag", "spdiag", "sparse bool matrix"); |
|
685 DEFUN_DLD (spdiag, args, , |
|
686 "-*- texinfo -*-\n\ |
|
687 @deftypefn {Loadable Function} {} spdiag (@var{v}, @var{k})\n\ |
|
688 Return a diagonal matrix with the sparse vector @var{v} on diagonal\n\ |
|
689 @var{k}. The second argument is optional. If it is positive, the vector is\n\ |
|
690 placed on the @var{k}-th super-diagonal. If it is negative, it is placed\n\ |
|
691 on the @var{-k}-th sub-diagonal. The default value of @var{k} is 0, and\n\ |
|
692 the vector is placed on the main diagonal. For example,\n\ |
|
693 \n\ |
|
694 @example\n\ |
6772
|
695 @group\n\ |
5164
|
696 spdiag ([1, 2, 3], 1)\n\ |
|
697 ans =\n\ |
|
698 \n\ |
|
699 Compressed Column Sparse (rows=4, cols=4, nnz=3)\n\ |
|
700 (1 , 2) -> 1\n\ |
|
701 (2 , 3) -> 2\n\ |
|
702 (3 , 4) -> 3\n\ |
6772
|
703 @end group\n\ |
5164
|
704 @end example\n\ |
6772
|
705 \n\ |
|
706 @noindent\n\ |
|
707 Given a matrix argument, instead of a vector, @code{spdiag} extracts the\n\ |
6774
|
708 @var{k}-th diagonal of the sparse matrix.\n\ |
5642
|
709 @seealso{diag}\n\ |
|
710 @end deftypefn") |
5164
|
711 { |
|
712 octave_value retval; |
|
713 |
|
714 int nargin = args.length (); |
|
715 |
|
716 if (nargin == 1 && args(0).is_defined ()) |
6771
|
717 retval = make_spdiag (args(0)); |
5164
|
718 else if (nargin == 2 && args(0).is_defined () && args(1).is_defined ()) |
|
719 retval = make_spdiag (args(0), args(1)); |
|
720 else |
5823
|
721 print_usage (); |
5164
|
722 |
|
723 return retval; |
|
724 } |
|
725 |
|
726 /* |
|
727 ;;; Local Variables: *** |
|
728 ;;; mode: C++ *** |
|
729 ;;; End: *** |
|
730 */ |