5164
|
1 ## Copyright (C) 2004 David Bateman & Andy Adler |
|
2 ## |
7016
|
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. |
|
9 ## |
|
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 -*- |
|
20 ## @deftypefn {Function File} {@var{y} =} spfun (@var{f},@var{x}) |
|
21 ## Compute @code{f(@var{x})} for the non-zero values of @var{x}. |
|
22 ## This results in a sparse matrix with the same structure as |
|
23 ## @var{x}. The function @var{f} can be passed as a string, a |
|
24 ## function handle or an inline function. |
|
25 ## @end deftypefn |
|
26 |
6498
|
27 function t = spfun (f, s) |
|
28 |
5164
|
29 if (nargin != 2) |
6046
|
30 print_usage (); |
5164
|
31 endif |
|
32 |
6498
|
33 if (issparse (s)) |
|
34 [i,j,v,m,n] = spfind (s); |
5164
|
35 else |
6498
|
36 [i, j, v] = find (s); |
|
37 [m, n] = size (s); |
5164
|
38 end |
|
39 |
6220
|
40 if (isa (f, "function_handle") || isa (f, "inline function")) |
6498
|
41 t = sparse (i, j, f(v), m, n); |
5164
|
42 else |
6498
|
43 t = sparse(i, j, feval (f, v), m, n); |
5164
|
44 endif |
|
45 |
|
46 endfunction |
|
47 |
|
48 %!assert(spfun('exp',[1,2;3,0]),sparse([exp(1),exp(2);exp(3),0])) |
|
49 %!assert(spfun('exp',sparse([1,2;3,0])),sparse([exp(1),exp(2);exp(3),0])) |
|
50 %!assert(spfun(@exp,[1,2;3,0]),sparse([exp(1),exp(2);exp(3),0])) |
|
51 %!assert(spfun(@exp,sparse([1,2;3,0])),sparse([exp(1),exp(2);exp(3),0])) |
|
52 |