annotate scripts/sparse/spdiags.m @ 18816:5556ddfb841b

spdiags.m: Treat sub/super-diagonals the same as Matlab. Distinguish between M >= N and M < N cases when placing diagonals. * spdiags.m: Add test for M >= N or M < N. When M >= N, sub-diagonals are taken from the first entries in a column while super-diagonals are taken from the last entries in a column. This is reversed for M < N. Add %!tests for new behavior. Add seealso link to diag() in docstring.
author Rik <rik@octave.org>
date Sun, 25 May 2014 13:13:17 -0700
parents 190ef1764d30
children 9ac2357f19bc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17744
d63878346099 maint: Update copyright notices for release.
John W. Eaton <jwe@octave.org>
parents: 17338
diff changeset
1 ## Copyright (C) 2000-2013 Paul Kienzle
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
2 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
3 ## This file is part of Octave.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
4 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
6 ## under the terms of the GNU General Public License as published by
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
8 ## your option) any later version.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
9 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
13 ## General Public License for more details.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
14 ##
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6547
diff changeset
17 ## <http://www.gnu.org/licenses/>.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
18
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
19 ## -*- texinfo -*-
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
20 ## @deftypefn {Function File} {@var{B} =} spdiags (@var{A})
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
21 ## @deftypefnx {Function File} {[@var{B}, @var{d}] =} spdiags (@var{A})
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
22 ## @deftypefnx {Function File} {@var{B} =} spdiags (@var{A}, @var{d})
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
23 ## @deftypefnx {Function File} {@var{A} =} spdiags (@var{v}, @var{d}, @var{A})
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
24 ## @deftypefnx {Function File} {@var{A} =} spdiags (@var{v}, @var{d}, @var{m}, @var{n})
9051
1bf0ce0930be Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents: 8920
diff changeset
25 ## A generalization of the function @code{diag}. Called with a single
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
26 ## input argument, the non-zero diagonals @var{d} 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
27 ## With two arguments the diagonals to extract are given by the vector
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
28 ## @var{d}.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
29 ##
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
30 ## 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
31 ## replacing the diagonals. They use the columns of @var{v} to replace
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
32 ## the diagonals represented by the vector @var{d}. 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
33 ## @var{A} is defined then the diagonals of this matrix are replaced.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
34 ## Otherwise a matrix of @var{m} by @var{n} is created with the
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
35 ## diagonals given by the columns of @var{v}.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
36 ##
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
37 ## Negative values of @var{d} represent diagonals below the main
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
38 ## diagonal, and positive values of @var{d} diagonals above the main
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
39 ## diagonal.
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
40 ##
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10793
diff changeset
41 ## For example:
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
42 ##
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
43 ## @example
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
44 ## @group
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
45 ## 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
46 ## @result{} 5 10 0 0
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
47 ## 1 6 11 0
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
48 ## 0 2 7 12
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
49 ## 0 0 3 8
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
50 ## 0 0 0 4
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
51 ## @end group
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
52 ## @end example
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
53 ##
18816
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
54 ## @seealso{diag}
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
55 ## @end deftypefn
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
56
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
57 function [B, d] = spdiags (v, d, m, n)
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
58
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
59 if (nargin < 1 || nargin > 4)
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
60 print_usage ();
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
61 endif
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
62
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 5568
diff changeset
63 if (nargin == 1 || nargin == 2)
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
64 ## extract nonzero diagonals of A into B,d
7515
f3c00dc0912b Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents: 7505
diff changeset
65 [nr, nc] = size (v);
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
66 [i, j] = find (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
67
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 5568
diff changeset
68 if (nargin == 1)
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
69 ## d contains the active diagonals
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
70 d = unique (j-i);
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
71 endif
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
72
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
73 ## FIXME: Maybe this could be done faster using [i,j,v] = find (v)
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
74 ## and then massaging the indices i, j. However, some
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
75 ## benchmarking has shown that diag() written in C++ makes
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
76 ## the following code faster even with the for loop.
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
77 Brows = min (nr, nc);
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
78 B = zeros (Brows, length (d));
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
79 for k = 1:length (d)
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
80 dn = d(k);
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
81 if (dn <= -nr || dn > nc)
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
82 continue;
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
83 endif
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
84 dv = diag (v, dn);
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
85 len = rows (dv);
18816
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
86 ## Put sub/super-diagonals in the right place based on matrix size (MxN)
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
87 if (nr >= nc)
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
88 if (dn > 0)
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
89 offset = Brows - len + 1;
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
90 B(offset:Brows, k) = dv;
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
91 else
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
92 B(1:len, k) = dv;
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
93 endif
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
94 else
18816
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
95 if (dn < 0)
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
96 offset = Brows - len + 1;
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
97 B(offset:Brows, k) = dv;
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
98 else
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
99 B(1:len, k) = dv;
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
100 endif
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
101 endif
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 5568
diff changeset
102 endfor
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
103
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 5568
diff changeset
104 elseif (nargin == 3)
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
105 ## Replace specific diagonals d of m with v,d
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 5568
diff changeset
106 [nr, nc] = size (m);
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
107 A = spdiags (m, d);
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
108 B = m - spdiags (A, d, nr, nc) + spdiags (v, d, nr, nc);
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
109
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 5568
diff changeset
110 else
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
111 ## Create new matrix of size mxn using v,d
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 5568
diff changeset
112 [j, i, v] = find (v);
18816
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
113 if (m >= n)
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
114 offset = max (min (d(:), n-m), 0);
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
115 else
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
116 offset = d(:);
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
117 endif
14666
1e77f6078692 spdiags.m: correctly treat row vector arguments
David Bateman <dbateman@free.fr>
parents: 14363
diff changeset
118 j = j(:) + offset(i(:));
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
119 i = j - d(:)(i(:));
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 5568
diff changeset
120 idx = i > 0 & i <= m & j > 0 & j <= n;
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
121 B = sparse (i(idx), j(idx), v(idx), m, n);
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
122
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 5568
diff changeset
123 endif
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
124
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
125 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
126
1f0243949b77 spdiags.m: Treat empty vector (1x0 or 0x1) the same as diag().
Marco Caliari <marco.caliari@univr.it>
parents: 11587
diff changeset
127
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
128 %!test
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
129 %! [B,d] = spdiags (magic (3));
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
130 %! assert (d, [-2 -1 0 1 2]');
18816
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
131 %! assert (B, [4 3 8 0 0
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
132 %! 0 9 5 1 0
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
133 %! 0 0 2 7 6]);
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
134 %! B = spdiags (magic (3), [-2 1]);
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
135 %! assert (B, [4 0; 0 1; 0 7]);
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
136
18816
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
137 ## Test zero filling for supra- and super-diagonals
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
138 %!test
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
139 %! ## Case 1: M = N
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
140 %! A = sparse (zeros (3,3));
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
141 %! A(1,3) = 13;
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
142 %! A(3,1) = 31;
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
143 %! [B, d] = spdiags (A);
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
144 %! assert (d, [-2 2]');
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
145 %! assert (B, [31 0; 0 0; 0 13]);
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
146 %! assert (spdiags (B, d, 3,3), A)
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
147
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
148 %!test
18816
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
149 %! ## Case 1: M > N
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
150 %! A = sparse (zeros (4,3));
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
151 %! A(1,3) = 13;
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
152 %! A(3,1) = 31;
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
153 %! [B, d] = spdiags (A);
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
154 %! assert (d, [-2 2]');
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
155 %! assert (B, [31 0; 0 0; 0 13]);
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
156 %! assert (spdiags (B, d, 4,3), A)
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
157
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
158 %!test
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
159 %! ## Case 1: M < N
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
160 %! A = sparse (zeros (3,4));
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
161 %! A(1,3) = 13;
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
162 %! A(3,1) = 31;
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
163 %! [B, d] = spdiags (A);
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
164 %! assert (d, [-2 2]');
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
165 %! assert (B, [0 13; 0 0; 31 0]);
18816
5556ddfb841b spdiags.m: Treat sub/super-diagonals the same as Matlab.
Rik <rik@octave.org>
parents: 18149
diff changeset
166 %! assert (spdiags (B, d, 3,4), A)
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
167
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14327
diff changeset
168 %!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
169 %!assert (spdiags (zeros (0,1),1,1,1), sparse (0))
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
170 %!assert (spdiags ([0.5 -1 0.5], 0:2, 1, 1), sparse (0.5))
17338
1c89599167a6 maint: End m-files with 1 blank line.
Rik <rik@octave.org>
parents: 14666
diff changeset
171
18149
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
172 %% Test input validation
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
173 %!error spdiags ()
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
174 %!error spdiags (1,2,3,4,5)
190ef1764d30 spdiags.m: Overhaul function for performance (bug #32966).
Rik <rik@octave.org>
parents: 17744
diff changeset
175