Mercurial > octave
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 |
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 | 7 ## |
3922 | 8 ## This file is part of Octave. |
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 | 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 | 14 ## |
3922 | 15 ## Octave is distributed in the hope that it will be useful, but |
2539 | 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 | 19 ## |
2539 | 20 ## You should have received a copy of the GNU General Public License |
7016 | 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 | 25 |
3369 | 26 ## -*- texinfo -*- |
22428 | 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 | 34 ## |
3369 | 35 ## @example |
36 ## @group | |
22428 | 37 ## [err, a, b] = common_size ([1 2; 3 4], 5) |
38 ## @result{} err = 0 | |
3369 | 39 ## @result{} a = [ 1, 2; 3, 4 ] |
40 ## @result{} b = [ 5, 5; 5, 5 ] | |
41 ## @end group | |
42 ## @end example | |
3426 | 43 ## |
3369 | 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 | 47 ## @seealso{size, size_equal, numel, ndims} |
3369 | 48 ## @end deftypefn |
2539 | 49 |
22428 | 50 function [err, varargout] = common_size (varargin) |
3426 | 51 |
2539 | 52 if (nargin < 2) |
53 error ("common_size: only makes sense if nargin >= 2"); | |
54 endif | |
55 | |
22428 | 56 ## Find array args |
57 array = cellfun ("numel", varargin) != 1; | |
58 aridx = find (array, 1); | |
3426 | 59 |
22428 | 60 if (isempty (aridx)) |
61 ## All inputs are scalars | |
62 err = 0; | |
3979 | 63 varargout = varargin; |
2539 | 64 else |
22428 | 65 sz_eq = cellfun ("size_equal", varargin, varargin(aridx)); |
66 if (any (! sz_eq & array)) | |
67 err = 1; | |
9477 | 68 varargout = varargin; |
69 else | |
22428 | 70 err = 0; |
9477 | 71 if (nargout > 1) |
72 varargout = varargin; | |
22428 | 73 if (any (array)) |
74 scalar = ! array; | |
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 | 77 varargout(scalar) = cellindexmat (varargin(scalar), subs{:}); |
10034 | 78 endif |
2539 | 79 endif |
9477 | 80 endif |
2539 | 81 endif |
21758
ffad2baa90f7
maint: Use newlines to make code more readable.
Rik <rik@octave.org>
parents:
20852
diff
changeset
|
82 |
2539 | 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 | 94 %!test |
95 %! m = [1,2;3,4]; | |
96 %! [err, a, b, c] = common_size (m, [], 5); | |
97 %! assert (err, 1); | |
98 %! assert (a, m); | |
99 %! assert (b, []); | |
100 %! assert (c, 5); | |
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) |