Mercurial > hg > octave-nkf
annotate scripts/general/common_size.m @ 20727:4479d73eac72
Fix error when building annotation objects in gnuplot (bug #46035)
* __gnuplot_draw_figure__.m: ignore uicontextmenu objects.
author | Pantxo Diribarne <pantxo.diribarne@gmail.com> |
---|---|
date | Sat, 26 Sep 2015 11:26:18 +0200 |
parents | 7503499a252b |
children |
rev | line source |
---|---|
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
17744
diff
changeset
|
1 ## Copyright (C) 1995-2015 Kurt Hornik |
9477 | 2 ## Copyright (C) 2009 VZLU Prague |
10034 | 3 ## Copyright (C) 2009 Jaroslav Hajek |
3426 | 4 ## |
3922 | 5 ## This file is part of Octave. |
6 ## | |
7 ## Octave is free software; you can redistribute it and/or modify it | |
8 ## under the terms of the GNU General Public License as published by | |
7016 | 9 ## the Free Software Foundation; either version 3 of the License, or (at |
10 ## your option) any later version. | |
3426 | 11 ## |
3922 | 12 ## Octave is distributed in the hope that it will be useful, but |
2539 | 13 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
14 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 15 ## General Public License for more details. |
16 ## | |
2539 | 17 ## You should have received a copy of the GNU General Public License |
7016 | 18 ## along with Octave; see the file COPYING. If not, see |
19 ## <http://www.gnu.org/licenses/>. | |
2539 | 20 |
3369 | 21 ## -*- texinfo -*- |
6547 | 22 ## @deftypefn {Function File} {[@var{err}, @var{y1}, @dots{}] =} common_size (@var{x1}, @dots{}) |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
23 ## 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:
20038
diff
changeset
|
24 ## |
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
25 ## 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:
20038
diff
changeset
|
26 ## 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:
20038
diff
changeset
|
27 ## 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:
20038
diff
changeset
|
28 ## 1, and @var{yi} is @var{xi}. For example: |
3426 | 29 ## |
3369 | 30 ## @example |
31 ## @group | |
32 ## [errorcode, a, b] = common_size ([1 2; 3 4], 5) | |
33 ## @result{} errorcode = 0 | |
34 ## @result{} a = [ 1, 2; 3, 4 ] | |
35 ## @result{} b = [ 5, 5; 5, 5 ] | |
36 ## @end group | |
37 ## @end example | |
3426 | 38 ## |
3369 | 39 ## @noindent |
20368
7503499a252b
doc: Update docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents:
20038
diff
changeset
|
40 ## 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:
20038
diff
changeset
|
41 ## scalars or of common size. |
3369 | 42 ## @end deftypefn |
2539 | 43 |
5428 | 44 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2539 | 45 ## Created: 15 October 1994 |
46 ## Adapted-By: jwe | |
9477 | 47 ## Optimized-By: Jaroslav Hajek |
2539 | 48 |
3979 | 49 function [errorcode, varargout] = common_size (varargin) |
3426 | 50 |
2539 | 51 if (nargin < 2) |
52 error ("common_size: only makes sense if nargin >= 2"); | |
53 endif | |
54 | |
9477 | 55 ## Find scalar args. |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12795
diff
changeset
|
56 nscal = cellfun ("numel", varargin) != 1; |
3426 | 57 |
9477 | 58 i = find (nscal, 1); |
59 | |
60 if (isempty (i)) | |
61 errorcode = 0; | |
3979 | 62 varargout = varargin; |
2539 | 63 else |
12931
cefd568ea073
Replace function handles with function names in cellfun calls for 15% speedup.
Rik <octave@nomad.inbox5.com>
parents:
12795
diff
changeset
|
64 match = cellfun ("size_equal", varargin, varargin(i)); |
9477 | 65 if (any (nscal &! match)) |
66 errorcode = 1; | |
67 varargout = varargin; | |
68 else | |
69 errorcode = 0; | |
70 if (nargout > 1) | |
20038
9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents:
19898
diff
changeset
|
71 scal = ! nscal; |
9477 | 72 varargout = varargin; |
10034 | 73 if (any (nscal)) |
74 dims = size (varargin{find (nscal, 1)}); | |
11191
01ddaedd6ad5
Reverse changeset b1f4bdc276b6. Use all lower case for "uniformoutput" option.
Rik <octave@nomad.inbox5.com>
parents:
11190
diff
changeset
|
75 subs = arrayfun (@ones, 1, dims, "uniformoutput", false); |
10965
28ef5a31763d
small rewrite in common_size
Jaroslav Hajek <highegg@gmail.com>
parents:
10821
diff
changeset
|
76 varargout(scal) = cellindexmat (varargin(scal), subs{:}); |
10034 | 77 endif |
2539 | 78 endif |
9477 | 79 endif |
2539 | 80 endif |
81 endfunction | |
12795
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
82 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
83 |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
84 %!test |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
85 %! 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
|
86 %! [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
|
87 %! assert (err, 0); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
88 %! assert (a, m); |
9e7ebbaf69ff
codesprint: new tests for files in scripts/general directory
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
89 %! 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
|
90 %! 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
|
91 |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
92 %!error common_size () |
f3d52523cde1
Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents:
14138
diff
changeset
|
93 |