5164
|
1 ## Copyright (C) 2004 David Bateman & Andy Adler |
|
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 -*- |
|
19 ## @deftypefn {Function File} {@var{y} =} speye (@var{m}) |
|
20 ## @deftypefnx {Function File} {@var{y} =} speye (@var{m}, @var{n}) |
|
21 ## @deftypefnx {Function File} {@var{y} =} speye (@var{sz}) |
|
22 ## Returns a sparse identity matrix. This is significantly more |
|
23 ## efficient than @code{sparse (eye (@var{m}))} as the full matrix |
|
24 ## is not constructed. |
|
25 ## |
|
26 ## Called with a single argument a square matrix of size @var{m} by |
|
27 ## @var{m} is created. Otherwise a matrix of @var{m} by @var{n} is |
|
28 ## created. If called with a single vector argument, this argument |
|
29 ## is taken to be the size of the matrix to create. |
|
30 ## @end deftypefn |
|
31 |
6498
|
32 function s = speye (m, n) |
5164
|
33 if (nargin == 1) |
|
34 if (isvector (m) && length(m) == 2) |
|
35 n = m(2); |
|
36 m = m(1); |
|
37 elseif (isscalar (m)) |
|
38 n = m; |
|
39 else |
|
40 error ("speye: invalid matrix dimension"); |
|
41 endif |
|
42 else |
6498
|
43 if (! isscalar (m) || ! isscalar (n)) |
5164
|
44 error ("speye: invalid matrix dimension"); |
|
45 endif |
|
46 endif |
|
47 |
6498
|
48 lo = min ([m, n]); |
|
49 s = sparse (1:lo, 1:lo, 1, m, n); |
5164
|
50 endfunction |
|
51 |
|
52 %!assert(issparse(speye(4))) |
|
53 %!assert(speye(4),sparse(1:4,1:4,1)) |
|
54 %!assert(speye(2,4),sparse(1:2,1:2,1,2,4)) |
|
55 %!assert(speye(4,2),sparse(1:2,1:2,1,4,2)) |
|
56 %!assert(speye([4,2]),sparse(1:2,1:2,1,4,2)) |