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