]> Creatis software - CreaPhase.git/blob - octave_packages/m/sparse/spfun.m
update packages
[CreaPhase.git] / octave_packages / m / sparse / spfun.m
1 ## Copyright (C) 2004-2012 David Bateman and Andy Adler
2 ##
3 ## This file is part of Octave.
4 ##
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.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {@var{y} =} spfun (@var{f}, @var{S})
21 ## Compute @code{f(@var{S})} for the non-zero values of @var{S}.
22 ## This results in a sparse matrix with the same structure as
23 ## @var{S}.  The function @var{f} can be passed as a string, a
24 ## function handle, or an inline function.
25 ## @seealso{arrayfun, cellfun, structfun}
26 ## @end deftypefn
27
28 function y = spfun (f, S)
29
30   if (nargin != 2)
31     print_usage ();
32   endif
33
34   [i, j, v] = find (S);
35   [m, n] = size (S);
36
37   if (isa (f, "function_handle") || isa (f, "inline function"))
38     y = sparse (i, j, f(v), m, n);
39   else
40     y = sparse(i, j, feval (f, v), m, n);
41   endif
42
43 endfunction
44
45 %!assert(spfun('exp',[1,2;3,0]),sparse([exp(1),exp(2);exp(3),0]))
46 %!assert(spfun('exp',sparse([1,2;3,0])),sparse([exp(1),exp(2);exp(3),0]))
47 %!assert(spfun(@exp,[1,2;3,0]),sparse([exp(1),exp(2);exp(3),0]))
48 %!assert(spfun(@exp,sparse([1,2;3,0])),sparse([exp(1),exp(2);exp(3),0]))
49