Mercurial > hg > octave-nkf
annotate scripts/sparse/spdiags.m @ 14953:711e64a11d36
Fix some segfaults
author | Max Brister <max@2bass.com> |
---|---|
date | Fri, 15 Jun 2012 13:15:48 -0500 |
parents | 1e77f6078692 |
children | 1c89599167a6 |
rev | line source |
---|---|
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
12558
diff
changeset
|
1 ## Copyright (C) 2000-2012 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 -*- | |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
20 ## @deftypefn {Function File} {[@var{b}, @var{c}] =} spdiags (@var{A}) |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
21 ## @deftypefnx {Function File} {@var{b} =} spdiags (@var{A}, @var{c}) |
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
22 ## @deftypefnx {Function File} {@var{b} =} spdiags (@var{v}, @var{c}, @var{A}) |
7144 | 23 ## @deftypefnx {Function File} {@var{b} =} spdiags (@var{v}, @var{c}, @var{m}, @var{n}) |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
24 ## A generalization of the function @code{diag}. Called with a single |
5164 | 25 ## input argument, the non-zero diagonals @var{c} of @var{A} are extracted. |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
26 ## With two arguments the diagonals to extract are given by the vector |
5164 | 27 ## @var{c}. |
28 ## | |
29 ## The other two forms of @code{spdiags} modify the input matrix by | |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
30 ## replacing the diagonals. They use the columns of @var{v} to replace |
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
31 ## the columns represented by the vector @var{c}. If the sparse matrix |
11471
994e2a93a8e2
Use uppercase 'A' to refer to matrix inputs in m-files.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
32 ## @var{A} is defined then the diagonals of this matrix are replaced. |
5164 | 33 ## Otherwise a matrix of @var{m} by @var{n} is created with the |
34 ## diagonals given by @var{v}. | |
35 ## | |
8325
b93ac0586e4b
spelling corrections
Brian Gough<bjg@network-theory.co.uk>
parents:
7515
diff
changeset
|
36 ## Negative values of @var{c} represent diagonals below the main |
5164 | 37 ## diagonal, and positive values of @var{c} diagonals above the main |
38 ## diagonal. | |
39 ## | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
40 ## For example: |
5164 | 41 ## |
42 ## @example | |
43 ## @group | |
44 ## spdiags (reshape (1:12, 4, 3), [-1 0 1], 5, 4) | |
14327
4d917a6a858b
doc: Use Octave coding conventions in @example blocks of docstrings.
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
45 ## @result{} 5 10 0 0 |
5164 | 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 | |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7505
diff
changeset
|
59 [nr, nc] = size (v); |
7505
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7144
diff
changeset
|
60 [i, j, v] = find (v); |
f5005d9510f4
Remove dispatched sparse functions and treat in the generic versions of the functions
David Bateman <dbateman@free.fr>
parents:
7144
diff
changeset
|
61 |
6498 | 62 if (nargin == 1) |
63 ## c contains the active diagonals | |
64 c = unique (j-i); | |
5164 | 65 endif |
6498 | 66 ## FIXME: we can do this without a loop if we are clever |
67 offset = max (min (c, nc-nr), 0); | |
68 A = zeros (min (nr, nc), length (c)); | |
69 for k = 1:length (c) | |
70 idx = find (j-i == c(k)); | |
71 A(j(idx)-offset(k),k) = v(idx); | |
72 endfor | |
73 elseif (nargin == 3) | |
74 ## Replace specific diagonals c of m with v,c | |
75 [nr, nc] = size (m); | |
76 B = spdiags (m, c); | |
77 A = m - spdiags (B, c, nr, nc) + spdiags (v, c, nr, nc); | |
78 else | |
79 ## Create new matrix of size mxn using v,c | |
80 [j, i, v] = find (v); | |
81 offset = max (min (c(:), n-m), 0); | |
14666
1e77f6078692
spdiags.m: correctly treat row vector arguments
David Bateman <dbateman@free.fr>
parents:
14363
diff
changeset
|
82 j = j(:) + offset(i(:)); |
1e77f6078692
spdiags.m: correctly treat row vector arguments
David Bateman <dbateman@free.fr>
parents:
14363
diff
changeset
|
83 i = j - c(:)(i(:)); |
6498 | 84 idx = i > 0 & i <= m & j > 0 & j <= n; |
85 A = sparse (i(idx), j(idx), v(idx), m, n); | |
86 endif | |
5164 | 87 |
88 endfunction | |
12558
1f0243949b77
spdiags.m: Treat empty vector (1x0 or 0x1) the same as diag().
Marco Caliari <marco.caliari@univr.it>
parents:
11587
diff
changeset
|
89 |
1f0243949b77
spdiags.m: Treat empty vector (1x0 or 0x1) the same as diag().
Marco Caliari <marco.caliari@univr.it>
parents:
11587
diff
changeset
|
90 |
14363
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
91 %!assert (spdiags (zeros (1,0),1,1,1), sparse (0)) |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14327
diff
changeset
|
92 %!assert (spdiags (zeros (0,1),1,1,1), sparse (0)) |
14666
1e77f6078692
spdiags.m: correctly treat row vector arguments
David Bateman <dbateman@free.fr>
parents:
14363
diff
changeset
|
93 %!assert (spdiags ([0.5 -1 0.5], 0:2, 1, 1), sparse(0.5)) |