annotate scripts/general/common_size.m @ 33617:ec2635a02328 bytecode-interpreter tip

maint: Merge default to bytecode-interpreter.
author Markus Mützel <markus.muetzel@gmx.de>
date Tue, 21 May 2024 18:29:03 +0200
parents 2e484f9f1f18
children
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 ##
32632
2e484f9f1f18 maint: update Octave Project Developers copyright for the new year
John W. Eaton <jwe@octave.org>
parents: 31706
diff changeset
3 ## Copyright (C) 1995-2024 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/>.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
7 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3426
diff changeset
8 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3426
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
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3426
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: 22428
diff changeset
13 ## (at your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
14 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3426
diff changeset
15 ## Octave is distributed in the hope that it will be useful, but
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
16 ## WITHOUT ANY WARRANTY; without even the implied warranty of
22755
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22428
diff changeset
17 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a2b891d0b33 maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents: 22428
diff changeset
18 ## GNU General Public License for more details.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
19 ##
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 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: 6547
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 ########################################################################
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
25
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2539
diff changeset
26 ## -*- texinfo -*-
22428
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
27 ## @deftypefn {} {[@var{err}, @var{yi}, @dots{}] =} common_size (@var{xi}, @dots{})
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
28 ## Determine if all input arguments are either scalar or of common size.
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
29 ##
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
30 ## If true, @var{err} is zero, and @var{yi} is a matrix of the common size
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
31 ## with all entries equal to @var{xi} if this is a scalar or @var{xi}
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
32 ## otherwise. If the inputs cannot be brought to a common size, @var{err} is
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
33 ## 1, and @var{yi} is @var{xi}. For example:
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
34 ##
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2539
diff changeset
35 ## @example
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2539
diff changeset
36 ## @group
22428
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
37 ## [err, a, b] = common_size ([1 2; 3 4], 5)
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
38 ## @result{} err = 0
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2539
diff changeset
39 ## @result{} a = [ 1, 2; 3, 4 ]
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2539
diff changeset
40 ## @result{} b = [ 5, 5; 5, 5 ]
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2539
diff changeset
41 ## @end group
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2539
diff changeset
42 ## @end example
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
43 ##
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2539
diff changeset
44 ## @noindent
20158
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
45 ## This is useful for implementing functions where arguments can either be
7503499a252b doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
46 ## scalars or of common size.
22428
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
47 ## @seealso{size, size_equal, numel, ndims}
3369
f37ca3017116 [project @ 1999-11-21 16:26:02 by jwe]
jwe
parents: 2539
diff changeset
48 ## @end deftypefn
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
49
22428
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
50 function [err, varargout] = common_size (varargin)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
51
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
52 if (nargin < 2)
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
53 error ("common_size: only makes sense if nargin >= 2");
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
54 endif
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
55
22428
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
56 ## Find array args
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
57 array = cellfun ("numel", varargin) != 1;
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
58 aridx = find (array, 1);
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3369
diff changeset
59
22428
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
60 if (isempty (aridx))
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
61 ## All inputs are scalars
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
62 err = 0;
3979
e0b7a493e5a8 [project @ 2002-07-10 17:45:34 by jwe]
jwe
parents: 3922
diff changeset
63 varargout = varargin;
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
64 else
22428
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
65 sz_eq = cellfun ("size_equal", varargin, varargin(aridx));
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
66 if (any (! sz_eq & array))
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
67 err = 1;
9477
4d3d90253e8a optimize common_size
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
68 varargout = varargin;
4d3d90253e8a optimize common_size
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
69 else
22428
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
70 err = 0;
9477
4d3d90253e8a optimize common_size
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
71 if (nargout > 1)
4d3d90253e8a optimize common_size
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
72 varargout = varargin;
22428
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
73 if (any (array))
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
74 scalar = ! array;
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
75 dims = size (varargin{aridx});
11191
01ddaedd6ad5 Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents: 11190
diff changeset
76 subs = arrayfun (@ones, 1, dims, "uniformoutput", false);
22428
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
77 varargout(scalar) = cellindexmat (varargin(scalar), subs{:});
10034
439c7a9d0fca optimize common_size
Jaroslav Hajek <highegg@gmail.com>
parents: 9477
diff changeset
78 endif
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
79 endif
9477
4d3d90253e8a optimize common_size
Jaroslav Hajek <highegg@gmail.com>
parents: 7017
diff changeset
80 endif
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
81 endif
21758
ffad2baa90f7 maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents: 20852
diff changeset
82
2539
1dca28c213f0 [project @ 1996-11-19 23:54:48 by jwe]
jwe
parents:
diff changeset
83 endfunction
12795
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
84
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
85
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
86 %!test
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
87 %! m = [1,2;3,4];
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
88 %! [err, a, b, c] = common_size (m, 3, 5);
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
89 %! assert (err, 0);
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
90 %! assert (a, m);
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
91 %! assert (b, [3,3;3,3]);
9e7ebbaf69ff codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
92 %! assert (c, [5,5;5,5]);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
93
22428
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
94 %!test
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
95 %! m = [1,2;3,4];
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
96 %! [err, a, b, c] = common_size (m, [], 5);
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
97 %! assert (err, 1);
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
98 %! assert (a, m);
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
99 %! assert (b, []);
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
100 %! assert (c, 5);
994dc0f7a6ce common_size.m: Overhaul function.
Rik <rik@octave.org>
parents: 22323
diff changeset
101
28896
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 27984
diff changeset
102 ## Test input validation
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 27984
diff changeset
103 %!error <only makes sense if nargin .= 2> common_size ()
90fea9cc9caa test: Add expected error message <Invalid call> to BIST tests for nargin.
Rik <rik@octave.org>
parents: 27984
diff changeset
104 %!error <only makes sense if nargin .= 2> common_size (1)