7017
|
1 ## Copyright (C) 2000, 2001, 2004, 2005, 2007 Paul Kienzle |
7016
|
2 ## |
|
3 ## This file is part of Octave. |
5164
|
4 ## |
7016
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5164
|
9 ## |
7016
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
5164
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5164
|
18 |
|
19 ## -*- texinfo -*- |
7144
|
20 ## @deftypefn {Function File} {[@var{b}, @var{c}] =} spdiags (@var{a}) |
|
21 ## @deftypefnx {Function File} {@var{b} =} spdiags (@var{a}, @var{c}) |
|
22 ## @deftypefnx {Function File} {@var{b} =} spdiags (@var{v}, @var{c}, @var{a}) |
|
23 ## @deftypefnx {Function File} {@var{b} =} spdiags (@var{v}, @var{c}, @var{m}, @var{n}) |
5164
|
24 ## A generalization of the function @code{spdiag}. Called with a single |
|
25 ## input argument, the non-zero diagonals @var{c} of @var{A} are extracted. |
|
26 ## With two arguments the diagonals to extract are given by the vector |
|
27 ## @var{c}. |
|
28 ## |
|
29 ## The other two forms of @code{spdiags} modify the input matrix by |
|
30 ## replacing the diagonals. They use the columns of @var{v} to replace |
|
31 ## the columns represented by the vector @var{c}. If the sparse matrix |
|
32 ## @var{a} is defined then the diagonals of this matrix are replaced. |
|
33 ## Otherwise a matrix of @var{m} by @var{n} is created with the |
|
34 ## diagonals given by @var{v}. |
|
35 ## |
|
36 ## Negative values of @var{c} representive diagonals below the main |
|
37 ## diagonal, and positive values of @var{c} diagonals above the main |
|
38 ## diagonal. |
|
39 ## |
|
40 ## For example |
|
41 ## |
|
42 ## @example |
|
43 ## @group |
|
44 ## spdiags (reshape (1:12, 4, 3), [-1 0 1], 5, 4) |
|
45 ## @result{} 5 10 0 0 |
|
46 ## 1 6 11 0 |
|
47 ## 0 2 7 12 |
|
48 ## 0 0 3 8 |
|
49 ## 0 0 0 4 |
|
50 ## @end group |
|
51 ## @end example |
|
52 ## |
|
53 ## @end deftypefn |
|
54 |
5568
|
55 function [A, c] = spdiags (v, c, m, n) |
5164
|
56 |
6498
|
57 if (nargin == 1 || nargin == 2) |
|
58 ## extract nonzero diagonals of v into A,c |
|
59 [i, j, v, nr, nc] = spfind (v); |
|
60 if (nargin == 1) |
|
61 ## c contains the active diagonals |
|
62 c = unique (j-i); |
5164
|
63 endif |
6498
|
64 ## FIXME: we can do this without a loop if we are clever |
|
65 offset = max (min (c, nc-nr), 0); |
|
66 A = zeros (min (nr, nc), length (c)); |
|
67 for k = 1:length (c) |
|
68 idx = find (j-i == c(k)); |
|
69 A(j(idx)-offset(k),k) = v(idx); |
|
70 endfor |
|
71 elseif (nargin == 3) |
|
72 ## Replace specific diagonals c of m with v,c |
|
73 [nr, nc] = size (m); |
|
74 B = spdiags (m, c); |
|
75 A = m - spdiags (B, c, nr, nc) + spdiags (v, c, nr, nc); |
|
76 else |
|
77 ## Create new matrix of size mxn using v,c |
|
78 [j, i, v] = find (v); |
|
79 offset = max (min (c(:), n-m), 0); |
6524
|
80 j = j(:) + offset(i); |
6498
|
81 i = j-c(:)(i); |
|
82 idx = i > 0 & i <= m & j > 0 & j <= n; |
|
83 A = sparse (i(idx), j(idx), v(idx), m, n); |
|
84 endif |
5164
|
85 |
|
86 endfunction |