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