annotate scripts/sparse/spfun.m @ 30564:796f54d4ddbf stable

update Octave Project Developers copyright for the new year In files that have the "Octave Project Developers" copyright notice, update for 2021. In all .txi and .texi files except gpl.txi and gpl.texi in the doc/liboctave and doc/interpreter directories, change the copyright to "Octave Project Developers", the same as used for other source files. Update copyright notices for 2022 (not done since 2019). For gpl.txi and gpl.texi, change the copyright notice to be "Free Software Foundation, Inc." and leave the date at 2007 only because this file only contains the text of the GPL, not anything created by the Octave Project Developers. Add Paul Thomas to contributors.in.
author John W. Eaton <jwe@octave.org>
date Tue, 28 Dec 2021 18:22:40 -0500
parents 7854d5752dd2
children 597f3ee61a48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
1 ########################################################################
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
2 ##
30564
796f54d4ddbf update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 29359
diff changeset
3 ## Copyright (C) 2004-2022 The Octave Project Developers
27918
b442ec6dda5c use centralized file for copyright info for individual contributors
John W. Eaton <jwe@octave.org>
parents: 26376
diff changeset
4 ##
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
5 ## See the file COPYRIGHT.md in the top-level directory of this
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
6 ## distribution or <https://octave.org/copyright/>.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
7 ##
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
8 ## This file is part of Octave.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
9 ##
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
10 ## Octave is free software: you can redistribute it and/or modify it
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
11 ## under the terms of the GNU General Public License as published by
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
12 ## the Free Software Foundation, either version 3 of the License, or
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
13 ## (at your option) any later version.
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
14 ##
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6498
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22323
diff changeset
18 ## GNU General Public License for more details.
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
19 ##
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
20 ## 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: 6498
diff changeset
21 ## along with Octave; see the file COPYING. If not, see
24534
194eb4bd202b maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents: 23220
diff changeset
22 ## <https://www.gnu.org/licenses/>.
27923
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
23 ##
bd51beb6205e update formatting of copyright notices
John W. Eaton <jwe@octave.org>
parents: 27919
diff changeset
24 ########################################################################
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
25
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
26 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20164
diff changeset
27 ## @deftypefn {} {@var{y} =} spfun (@var{f}, @var{S})
29125
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
28 ## Compute @code{f (@var{S})} for the nonzero elements of @var{S}.
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
29 ##
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
30 ## The input function @var{f} is applied only to the nonzero elements of
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
31 ## the input matrix @var{S} which is typically sparse. The function @var{f}
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
32 ## can be passed as a string, function handle, or inline function.
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
33 ##
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
34 ## The output @var{y} is a sparse matrix with the same sparsity structure as
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
35 ## the input @var{S}. @code{spfun} preserves sparsity structure which is
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
36 ## different than simply applying the function @var{f} to the sparse matrix
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
37 ## @var{S} when @code{@var{f} (0) != 0}.
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
38 ##
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
39 ## Example
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
40 ##
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
41 ## Sparsity preserving @code{spfun} versus normal function application
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
42 ##
29125
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
43 ## @example
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
44 ## @group
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
45 ## S = pi * speye (2,2)
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
46 ## S =
29127
a948253f9976 maint: strip trailing spaces from code base.
Rik <rik@octave.org>
parents: 29125
diff changeset
47 ##
29125
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
48 ## Compressed Column Sparse (rows = 2, cols = 2, nnz = 2 [50%])
20164
df437a52bcaf doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19697
diff changeset
49 ##
29125
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
50 ## (1, 1) -> 3.1416
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
51 ## (2, 2) -> 3.1416
29127
a948253f9976 maint: strip trailing spaces from code base.
Rik <rik@octave.org>
parents: 29125
diff changeset
52 ##
29125
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
53 ## y = spfun (@@cos, S)
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
54 ## y =
29127
a948253f9976 maint: strip trailing spaces from code base.
Rik <rik@octave.org>
parents: 29125
diff changeset
55 ##
29125
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
56 ## Compressed Column Sparse (rows = 2, cols = 2, nnz = 2 [50%])
29127
a948253f9976 maint: strip trailing spaces from code base.
Rik <rik@octave.org>
parents: 29125
diff changeset
57 ##
29125
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
58 ## (1, 1) -> -1
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
59 ## (2, 2) -> -1
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
60 ## @end group
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
61 ##
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
62 ## @group
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
63 ## y = cos (S)
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
64 ## y =
29127
a948253f9976 maint: strip trailing spaces from code base.
Rik <rik@octave.org>
parents: 29125
diff changeset
65 ##
29125
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
66 ## Compressed Column Sparse (rows = 2, cols = 2, nnz = 4 [100%])
29127
a948253f9976 maint: strip trailing spaces from code base.
Rik <rik@octave.org>
parents: 29125
diff changeset
67 ##
29125
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
68 ## (1, 1) -> -1
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
69 ## (2, 1) -> 1
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
70 ## (1, 2) -> 1
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
71 ## (2, 2) -> -1
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
72 ##
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
73 ## @end group
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
74 ## @end example
10687
a8ce6bdecce5 Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
75 ## @seealso{arrayfun, cellfun, structfun}
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
76 ## @end deftypefn
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
77
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
78 function y = spfun (f, S)
6498
2c85044aa63f [project @ 2007-04-05 17:59:47 by jwe]
jwe
parents: 6220
diff changeset
79
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
80 if (nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5307
diff changeset
81 print_usage ();
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
82 endif
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
83
29125
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
84 if (! isnumeric (S))
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
85 error ("spfun: S must be numeric");
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
86 endif
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
87
11469
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
88 [i, j, v] = find (S);
c776f063fefe Overhaul m-script files to use common variable name between code and documentation.
Rik <octave@nomad.inbox5.com>
parents: 10687
diff changeset
89 [m, n] = size (S);
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
90
29125
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
91 y = sparse (i, j, feval (f, v), m, n);
5164
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
92
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
93 endfunction
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
94
57077d0ddc8e [project @ 2005-02-25 19:55:24 by jwe]
jwe
parents:
diff changeset
95
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
96 %!assert (spfun ("exp", [1,2;3,0]), sparse ([exp(1),exp(2);exp(3),0]))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
97 %!assert (spfun ("exp", sparse ([1,2;3,0])), sparse ([exp(1),exp(2);exp(3),0]))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
98 %!assert (spfun (@exp, [1,2;3,0]), sparse ([exp(1),exp(2);exp(3),0]))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
99 %!assert (spfun (@exp, sparse ([1,2;3,0])), sparse ([exp(1),exp(2);exp(3),0]))
29125
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
100
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
101 ## Test input validation
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
102 %!error <Invalid call> spfun ()
c45cf1fd51b8 spfun.m: Overhaul function.
Rik <rik@octave.org>
parents: 27923
diff changeset
103 %!error <Invalid call> spfun (@cos)
29127
a948253f9976 maint: strip trailing spaces from code base.
Rik <rik@octave.org>
parents: 29125
diff changeset
104 %!error <S must be numeric> spfun (@cos, {1})
a948253f9976 maint: strip trailing spaces from code base.
Rik <rik@octave.org>
parents: 29125
diff changeset
105 %!error <S must be numeric> spfun (@cos, "FooBar")