Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/cellfun.cc @ 16046:1678d0fca146 stable
fix ErrorHandler in cellfun (bug #38256)
* cellfun.cc: The ErrorHandler was not executed on some errors that
throw an expection. Test added.
author | Stefan Mahr <dac922@gmx.de> |
---|---|
date | Mon, 11 Feb 2013 13:26:44 -0500 |
parents | 72c96de7a403 |
children |
rev | line source |
---|---|
5205 | 1 /* |
2 | |
14138
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14119
diff
changeset
|
3 Copyright (C) 2005-2012 Mohamed Kamoun |
72c96de7a403
maint: update copyright notices for 2012
John W. Eaton <jwe@octave.org>
parents:
14119
diff
changeset
|
4 Copyright (C) 2006-2012 Bill Denney |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
5 Copyright (C) 2009 Jaroslav Hajek |
10521
4d1fc073fbb7
add some missing copyright stmts
Jaroslav Hajek <highegg@gmail.com>
parents:
10428
diff
changeset
|
6 Copyright (C) 2010 VZLU Prague |
5205 | 7 |
8 This file is part of Octave. | |
9 | |
10 Octave is free software; you can redistribute it and/or modify it | |
11 under the terms of the GNU General Public License as published by the | |
7016 | 12 Free Software Foundation; either version 3 of the License, or (at your |
13 option) any later version. | |
5205 | 14 |
15 Octave is distributed in the hope that it will be useful, but WITHOUT | |
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 for more details. | |
19 | |
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 |
22 <http://www.gnu.org/licenses/>. | |
5205 | 23 |
24 */ | |
25 | |
26 #ifdef HAVE_CONFIG_H | |
27 #include <config.h> | |
28 #endif | |
29 | |
30 #include <string> | |
5988 | 31 #include <vector> |
32 #include <list> | |
8994 | 33 #include <memory> |
5205 | 34 |
14076
9aff66860e03
accept abbreviated cellfun option names
John W. Eaton <jwe@octave.org>
parents:
13960
diff
changeset
|
35 #include "caseless-str.h" |
5205 | 36 #include "lo-mappers.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8373
diff
changeset
|
37 #include "oct-locbuf.h" |
5205 | 38 |
39 #include "Cell.h" | |
5988 | 40 #include "oct-map.h" |
5205 | 41 #include "defun-dld.h" |
42 #include "parse.h" | |
43 #include "variables.h" | |
5451 | 44 #include "ov-colon.h" |
5988 | 45 #include "unwind-prot.h" |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
46 #include "gripes.h" |
9728
70925b11ba46
again allow the obsolete syntax for cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9684
diff
changeset
|
47 #include "utils.h" |
5205 | 48 |
13329
80f7564a3849
handle class objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13234
diff
changeset
|
49 #include "ov-class.h" |
10044
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
50 #include "ov-scalar.h" |
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
51 #include "ov-float.h" |
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
52 #include "ov-complex.h" |
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
53 #include "ov-flt-complex.h" |
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
54 #include "ov-bool.h" |
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
55 #include "ov-int8.h" |
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
56 #include "ov-int16.h" |
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
57 #include "ov-int32.h" |
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
58 #include "ov-int64.h" |
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
59 #include "ov-uint8.h" |
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
60 #include "ov-uint16.h" |
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
61 #include "ov-uint32.h" |
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
62 #include "ov-uint64.h" |
2d7a8c227713
rewrite uniformoutput optimization in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
10043
diff
changeset
|
63 |
12952
5a5cb2a4b71d
cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12949
diff
changeset
|
64 #include "ov-fcn-handle.h" |
5a5cb2a4b71d
cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12949
diff
changeset
|
65 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
66 static octave_value_list |
10181
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
67 get_output_list (octave_idx_type count, octave_idx_type nargout, |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
68 const octave_value_list& inputlist, |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
69 octave_value& func, |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
70 octave_value& error_handler) |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
71 { |
16046
1678d0fca146
fix ErrorHandler in cellfun (bug #38256)
Stefan Mahr <dac922@gmx.de>
parents:
14138
diff
changeset
|
72 octave_value_list tmp; |
1678d0fca146
fix ErrorHandler in cellfun (bug #38256)
Stefan Mahr <dac922@gmx.de>
parents:
14138
diff
changeset
|
73 try { |
1678d0fca146
fix ErrorHandler in cellfun (bug #38256)
Stefan Mahr <dac922@gmx.de>
parents:
14138
diff
changeset
|
74 tmp = func.do_multi_index_op (nargout, inputlist); |
1678d0fca146
fix ErrorHandler in cellfun (bug #38256)
Stefan Mahr <dac922@gmx.de>
parents:
14138
diff
changeset
|
75 } |
1678d0fca146
fix ErrorHandler in cellfun (bug #38256)
Stefan Mahr <dac922@gmx.de>
parents:
14138
diff
changeset
|
76 catch (octave_execution_exception) { |
1678d0fca146
fix ErrorHandler in cellfun (bug #38256)
Stefan Mahr <dac922@gmx.de>
parents:
14138
diff
changeset
|
77 if (error_handler.is_defined ()) |
1678d0fca146
fix ErrorHandler in cellfun (bug #38256)
Stefan Mahr <dac922@gmx.de>
parents:
14138
diff
changeset
|
78 error_state = 1; |
1678d0fca146
fix ErrorHandler in cellfun (bug #38256)
Stefan Mahr <dac922@gmx.de>
parents:
14138
diff
changeset
|
79 } |
10181
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
80 |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
81 if (error_state) |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
82 { |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
83 if (error_handler.is_defined ()) |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
84 { |
11046
14993c9e857e
amd.cc, cellfun.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
10964
diff
changeset
|
85 octave_scalar_map msg; |
10181
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
86 msg.assign ("identifier", last_error_id ()); |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
87 msg.assign ("message", last_error_message ()); |
11046
14993c9e857e
amd.cc, cellfun.cc: use octave_scalar_map instead of Octave_map
John W. Eaton <jwe@octave.org>
parents:
10964
diff
changeset
|
88 msg.assign ("index", static_cast<double> (count + static_cast<octave_idx_type>(1))); |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
89 |
10181
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
90 octave_value_list errlist = inputlist; |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
91 errlist.prepend (msg); |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
92 |
10181
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
93 buffer_error_messages--; |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
94 |
10181
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
95 error_state = 0; |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
96 |
10181
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
97 tmp = error_handler.do_multi_index_op (nargout, errlist); |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
98 |
10181
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
99 buffer_error_messages++; |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
100 |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
101 if (error_state) |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
102 tmp.clear (); |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
103 } |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
104 else |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
105 tmp.clear (); |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
106 } |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
107 |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
108 return tmp; |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
109 } |
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
110 |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
111 static octave_value_list |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
112 try_cellfun_internal_ops (const octave_value_list& args, int nargin) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
113 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
114 octave_value_list retval; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
115 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
116 std::string name = args(0).string_value (); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
117 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
118 const Cell f_args = args(1).cell_value (); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
119 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
120 octave_idx_type k = f_args.numel (); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
121 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
122 if (name == "isempty") |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
123 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
124 boolNDArray result (f_args.dims ()); |
13231
20ed9548070f
maint: style fixes for cellfun
John W. Eaton <jwe@octave.org>
parents:
13230
diff
changeset
|
125 for (octave_idx_type count = 0; count < k; count++) |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
126 result(count) = f_args.elem(count).is_empty (); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
127 retval(0) = result; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
128 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
129 else if (name == "islogical") |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
130 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
131 boolNDArray result (f_args.dims ()); |
13231
20ed9548070f
maint: style fixes for cellfun
John W. Eaton <jwe@octave.org>
parents:
13230
diff
changeset
|
132 for (octave_idx_type count= 0; count < k; count++) |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
133 result(count) = f_args.elem(count).is_bool_type (); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
134 retval(0) = result; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
135 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
136 else if (name == "isreal") |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
137 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
138 boolNDArray result (f_args.dims ()); |
13231
20ed9548070f
maint: style fixes for cellfun
John W. Eaton <jwe@octave.org>
parents:
13230
diff
changeset
|
139 for (octave_idx_type count= 0; count < k; count++) |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
140 result(count) = f_args.elem(count).is_real_type (); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
141 retval(0) = result; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
142 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
143 else if (name == "length") |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
144 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
145 NDArray result (f_args.dims ()); |
13231
20ed9548070f
maint: style fixes for cellfun
John W. Eaton <jwe@octave.org>
parents:
13230
diff
changeset
|
146 for (octave_idx_type count= 0; count < k; count++) |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
147 result(count) = static_cast<double> (f_args.elem(count).length ()); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
148 retval(0) = result; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
149 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
150 else if (name == "ndims") |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
151 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
152 NDArray result (f_args.dims ()); |
13231
20ed9548070f
maint: style fixes for cellfun
John W. Eaton <jwe@octave.org>
parents:
13230
diff
changeset
|
153 for (octave_idx_type count = 0; count < k; count++) |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
154 result(count) = static_cast<double> (f_args.elem(count).ndims ()); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
155 retval(0) = result; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
156 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
157 else if (name == "prodofsize" || name == "numel") |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
158 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
159 NDArray result (f_args.dims ()); |
13231
20ed9548070f
maint: style fixes for cellfun
John W. Eaton <jwe@octave.org>
parents:
13230
diff
changeset
|
160 for (octave_idx_type count = 0; count < k; count++) |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
161 result(count) = static_cast<double> (f_args.elem(count).numel ()); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
162 retval(0) = result; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
163 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
164 else if (name == "size") |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
165 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
166 if (nargin == 3) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
167 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
168 int d = args(2).nint_value () - 1; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
169 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
170 if (d < 0) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
171 error ("cellfun: K must be a positive integer"); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
172 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
173 if (! error_state) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
174 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
175 NDArray result (f_args.dims ()); |
13231
20ed9548070f
maint: style fixes for cellfun
John W. Eaton <jwe@octave.org>
parents:
13230
diff
changeset
|
176 for (octave_idx_type count = 0; count < k; count++) |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
177 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
178 dim_vector dv = f_args.elem(count).dims (); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
179 if (d < dv.length ()) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
180 result(count) = static_cast<double> (dv(d)); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
181 else |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
182 result(count) = 1.0; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
183 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
184 retval(0) = result; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
185 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
186 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
187 else |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
188 error ("cellfun: not enough arguments for \"size\""); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
189 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
190 else if (name == "isclass") |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
191 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
192 if (nargin == 3) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
193 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
194 std::string class_name = args(2).string_value(); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
195 boolNDArray result (f_args.dims ()); |
13231
20ed9548070f
maint: style fixes for cellfun
John W. Eaton <jwe@octave.org>
parents:
13230
diff
changeset
|
196 for (octave_idx_type count = 0; count < k; count++) |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
197 result(count) = (f_args.elem(count).class_name() == class_name); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
198 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
199 retval(0) = result; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
200 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
201 else |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
202 error ("cellfun: not enough arguments for \"isclass\""); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
203 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
204 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
205 return retval; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
206 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
207 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
208 static void |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
209 get_mapper_fun_options (const octave_value_list& args, int& nargin, |
14076
9aff66860e03
accept abbreviated cellfun option names
John W. Eaton <jwe@octave.org>
parents:
13960
diff
changeset
|
210 bool& uniform_output, octave_value& error_handler) |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
211 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
212 while (nargin > 3 && args(nargin-2).is_string ()) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
213 { |
14076
9aff66860e03
accept abbreviated cellfun option names
John W. Eaton <jwe@octave.org>
parents:
13960
diff
changeset
|
214 caseless_str arg = args(nargin-2).string_value (); |
9aff66860e03
accept abbreviated cellfun option names
John W. Eaton <jwe@octave.org>
parents:
13960
diff
changeset
|
215 |
9aff66860e03
accept abbreviated cellfun option names
John W. Eaton <jwe@octave.org>
parents:
13960
diff
changeset
|
216 size_t compare_len = std::max (arg.length (), static_cast<size_t> (2)); |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
217 |
14076
9aff66860e03
accept abbreviated cellfun option names
John W. Eaton <jwe@octave.org>
parents:
13960
diff
changeset
|
218 if (arg.compare ("uniformoutput", compare_len)) |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
219 uniform_output = args(nargin-1).bool_value(); |
14076
9aff66860e03
accept abbreviated cellfun option names
John W. Eaton <jwe@octave.org>
parents:
13960
diff
changeset
|
220 else if (arg.compare ("errorhandler", compare_len)) |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
221 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
222 if (args(nargin-1).is_function_handle () |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
223 || args(nargin-1).is_inline_function ()) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
224 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
225 error_handler = args(nargin-1); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
226 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
227 else if (args(nargin-1).is_string ()) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
228 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
229 std::string err_name = args(nargin-1).string_value (); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
230 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
231 error_handler = symbol_table::find_function (err_name); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
232 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
233 if (error_handler.is_undefined ()) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
234 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
235 error ("cellfun: invalid function NAME: %s", |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
236 err_name.c_str ()); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
237 break; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
238 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
239 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
240 else |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
241 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
242 error ("cellfun: invalid value for 'ErrorHandler' function"); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
243 break; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
244 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
245 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
246 else |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
247 { |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
248 error ("cellfun: unrecognized parameter %s", |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
249 arg.c_str()); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
250 break; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
251 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
252 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
253 nargin -= 2; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
254 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
255 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
256 nargin -= 1; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
257 } |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
258 |
5988 | 259 DEFUN_DLD (cellfun, args, nargout, |
8715 | 260 "-*- texinfo -*-\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
261 @deftypefn {Loadable Function} {} cellfun (@var{name}, @var{C})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
262 @deftypefnx {Loadable Function} {} cellfun (\"size\", @var{C}, @var{k})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
263 @deftypefnx {Loadable Function} {} cellfun (\"isclass\", @var{C}, @var{class})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
264 @deftypefnx {Loadable Function} {} cellfun (@var{func}, @var{C})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
265 @deftypefnx {Loadable Function} {} cellfun (@var{func}, @var{C}, @var{D})\n\ |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
266 @deftypefnx {Loadable Function} {[@var{a}, @dots{}] =} cellfun (@dots{})\n\ |
6854 | 267 @deftypefnx {Loadable Function} {} cellfun (@dots{}, 'ErrorHandler', @var{errfunc})\n\ |
268 @deftypefnx {Loadable Function} {} cellfun (@dots{}, 'UniformOutput', @var{val})\n\ | |
5205 | 269 \n\ |
270 Evaluate the function named @var{name} on the elements of the cell array\n\ | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
271 @var{C}. Elements in @var{C} are passed on to the named function\n\ |
5205 | 272 individually. The function @var{name} can be one of the functions\n\ |
273 \n\ | |
274 @table @code\n\ | |
275 @item isempty\n\ | |
5543 | 276 Return 1 for empty elements.\n\ |
10840 | 277 \n\ |
5205 | 278 @item islogical\n\ |
279 Return 1 for logical elements.\n\ | |
10840 | 280 \n\ |
5205 | 281 @item isreal\n\ |
282 Return 1 for real elements.\n\ | |
10840 | 283 \n\ |
5205 | 284 @item length\n\ |
285 Return a vector of the lengths of cell elements.\n\ | |
10840 | 286 \n\ |
5543 | 287 @item ndims\n\ |
5205 | 288 Return the number of dimensions of each element.\n\ |
10840 | 289 \n\ |
12932
f951ffdd0133
doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
290 @item numel\n\ |
f951ffdd0133
doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
291 @itemx prodofsize\n\ |
f951ffdd0133
doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
292 Return the number of elements contained within each cell element. The\n\ |
f951ffdd0133
doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
293 number is the product of the dimensions of the object at each cell element.\n\ |
10840 | 294 \n\ |
5205 | 295 @item size\n\ |
296 Return the size along the @var{k}-th dimension.\n\ | |
10840 | 297 \n\ |
5205 | 298 @item isclass\n\ |
299 Return 1 for elements of @var{class}.\n\ | |
300 @end table\n\ | |
301 \n\ | |
302 Additionally, @code{cellfun} accepts an arbitrary function @var{func}\n\ | |
303 in the form of an inline function, function handle, or the name of a\n\ | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
304 function (in a character string). In the case of a character string\n\ |
5988 | 305 argument, the function must accept a single argument named @var{x}, and\n\ |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
306 it must return a string value. The function can take one or more arguments,\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
307 with the inputs arguments given by @var{C}, @var{D}, etc. Equally the\n\ |
10840 | 308 function can return one or more output arguments. For example:\n\ |
5205 | 309 \n\ |
310 @example\n\ | |
311 @group\n\ | |
12932
f951ffdd0133
doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
312 cellfun (\"atan2\", @{1, 0@}, @{0, 1@})\n\ |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
313 @result{}ans = [1.57080 0.00000]\n\ |
5988 | 314 @end group\n\ |
315 @end example\n\ | |
316 \n\ | |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
317 The number of output arguments of @code{cellfun} matches the number of output\n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
318 arguments of the function. The outputs of the function will be collected\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
319 into the output arguments of @code{cellfun} like this:\n\ |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
320 \n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
321 @example\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
322 @group\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
323 function [a, b] = twoouts (x)\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
324 a = x;\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
325 b = x*x;\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
326 endfunction\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
327 [aa, bb] = cellfun(@@twoouts, @{1, 2, 3@})\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
328 @result{}\n\ |
12642
f96b9b9f141b
doc: Periodic grammarcheck and spellcheck of documentation.
Rik <octave@nomad.inbox5.com>
parents:
12483
diff
changeset
|
329 aa =\n\ |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
330 1 2 3\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
331 bb =\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
332 1 4 9\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
333 @end group\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
334 @end example\n\ |
10840 | 335 \n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
336 Note that per default the output argument(s) are arrays of the same size as\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
337 the input arguments. Input arguments that are singleton (1x1) cells will be\n\ |
10840 | 338 automatically expanded to the size of the other arguments.\n\ |
5988 | 339 \n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
340 If the parameter 'UniformOutput' is set to true (the default), then the\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
341 function must return scalars which will be concatenated into the return\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
342 array(s). If 'UniformOutput' is false, the outputs are concatenated into a\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
343 cell array (or cell arrays). For example:\n\ |
5988 | 344 \n\ |
345 @example\n\ | |
346 @group\n\ | |
12932
f951ffdd0133
doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
347 cellfun (\"tolower\", @{\"Foo\", \"Bar\", \"FooBar\"@},\n\ |
6623 | 348 \"UniformOutput\",false)\n\ |
5205 | 349 @result{} ans = @{\"foo\", \"bar\", \"foobar\"@}\n\ |
350 @end group\n\ | |
351 @end example\n\ | |
5988 | 352 \n\ |
353 Given the parameter 'ErrorHandler', then @var{errfunc} defines a function to\n\ | |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
354 call in case @var{func} generates an error. The form of the function is\n\ |
5988 | 355 \n\ |
356 @example\n\ | |
357 function [@dots{}] = errfunc (@var{s}, @dots{})\n\ | |
358 @end example\n\ | |
359 \n\ | |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
360 @noindent\n\ |
5988 | 361 where there is an additional input argument to @var{errfunc} relative to\n\ |
9036
58604c45ca74
Cleanup of data types related documentation
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
362 @var{func}, given by @var{s}. This is a structure with the elements\n\ |
5988 | 363 'identifier', 'message' and 'index', giving respectively the error\n\ |
364 identifier, the error message, and the index into the input arguments\n\ | |
10840 | 365 of the element that caused the error. For example:\n\ |
5988 | 366 \n\ |
367 @example\n\ | |
368 @group\n\ | |
369 function y = foo (s, x), y = NaN; endfunction\n\ | |
12932
f951ffdd0133
doc: Document numel option to cellfun. Use fewer function handles in examples.
Rik <octave@nomad.inbox5.com>
parents:
12642
diff
changeset
|
370 cellfun (\"factorial\", @{-1,2@}, 'ErrorHandler', @@foo)\n\ |
5988 | 371 @result{} ans = [NaN 2]\n\ |
372 @end group\n\ | |
373 @end example\n\ | |
374 \n\ | |
14119
94e2a76f1e5a
doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14116
diff
changeset
|
375 Use @code{cellfun} intelligently. The @code{cellfun} function is a\n\ |
94e2a76f1e5a
doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14116
diff
changeset
|
376 useful tool for avoiding loops. It is often used with anonymous\n\ |
14116
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14093
diff
changeset
|
377 function handles; however, calling an anonymous function involves an\n\ |
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14093
diff
changeset
|
378 overhead quite comparable to the overhead of an m-file function.\n\ |
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14093
diff
changeset
|
379 Passing a handle to a built-in function is faster, because the\n\ |
14119
94e2a76f1e5a
doc: Final grammarcheck and spellcheck before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14116
diff
changeset
|
380 interpreter is not involved in the internal loop. For example:\n\ |
14116
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14093
diff
changeset
|
381 \n\ |
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14093
diff
changeset
|
382 @example\n\ |
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14093
diff
changeset
|
383 @group\n\ |
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14093
diff
changeset
|
384 a = @{@dots{}@}\n\ |
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14093
diff
changeset
|
385 v = cellfun (@@(x) det(x), a); # compute determinants\n\ |
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14093
diff
changeset
|
386 v = cellfun (@@det, a); # faster\n\ |
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14093
diff
changeset
|
387 @end group\n\ |
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14093
diff
changeset
|
388 @end example\n\ |
951eacaf9381
Initial documentation for broadcasting and general vectorization guidelines
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
14093
diff
changeset
|
389 \n\ |
10687
a8ce6bdecce5
Improve documentation strings.
Rik <octave@nomad.inbox5.com>
parents:
10670
diff
changeset
|
390 @seealso{arrayfun, structfun, spfun}\n\ |
5642 | 391 @end deftypefn") |
5205 | 392 { |
5988 | 393 octave_value_list retval; |
5205 | 394 int nargin = args.length (); |
9988
76cf4aec34e9
allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9740
diff
changeset
|
395 int nargout1 = (nargout < 1 ? 1 : nargout); |
5205 | 396 |
397 if (nargin < 2) | |
398 { | |
12483
7a5aacf65f81
Rewrite error strings in src/ to use variables named in documentation.
Rik <octave@nomad.inbox5.com>
parents:
11590
diff
changeset
|
399 error ("cellfun: function requires at least 2 arguments"); |
5823 | 400 print_usage (); |
5205 | 401 return retval; |
402 } | |
403 | |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
404 octave_value func = args(0); |
5205 | 405 |
406 if (! args(1).is_cell ()) | |
407 { | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
408 error ("cellfun: C must be a cell array"); |
5205 | 409 |
410 return retval; | |
411 } | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
412 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
413 if (func.is_string ()) |
5205 | 414 { |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
415 retval = try_cellfun_internal_ops (args, nargin); |
9465
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
416 |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
417 if (error_state || ! retval.empty ()) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
418 return retval; |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
419 |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
420 // See if we can convert the string into a function. |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
421 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
422 std::string name = args(0).string_value (); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
423 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
424 if (! valid_identifier (name)) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
425 { |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
426 std::string fcn_name = unique_symbol_name ("__cellfun_fcn_"); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
427 std::string fname = "function y = " + fcn_name + "(x) y = "; |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
428 |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
429 octave_function *ptr_func |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
430 = extract_function (args(0), "cellfun", fcn_name, |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
431 fname, "; endfunction"); |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
432 |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
433 if (ptr_func && ! error_state) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
434 func = octave_value (ptr_func, true); |
5205 | 435 } |
436 else | |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
437 { |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
438 func = symbol_table::find_function (name); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
439 |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
440 if (func.is_undefined ()) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
441 error ("cellfun: invalid function NAME: %s", name.c_str ()); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
442 } |
12949
d1db86336a49
cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12948
diff
changeset
|
443 |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
444 if (error_state || ! retval.empty ()) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
445 return retval; |
5205 | 446 } |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
447 |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
448 if (func.is_function_handle () || func.is_inline_function () |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
449 || func.is_function ()) |
5205 | 450 { |
13960
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
451 |
12949
d1db86336a49
cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12948
diff
changeset
|
452 // The following is an optimisation because the symbol table can |
d1db86336a49
cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12948
diff
changeset
|
453 // give a more specific function class, so this can result in |
d1db86336a49
cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12948
diff
changeset
|
454 // fewer polymorphic function calls as the function gets called |
d1db86336a49
cellfun.cc (cellfun): Small optimisation for function handles to built-ins
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12948
diff
changeset
|
455 // for each value of the array. |
13960
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
456 { |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
457 if (func.is_function_handle ()) |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
458 { |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
459 octave_fcn_handle* f = func.fcn_handle_value (); |
12952
5a5cb2a4b71d
cellfun: Fix a regression for function handles to overloaded functions
Jordi Gutiérrez Hermoso <jordigh@gmail.com>
parents:
12949
diff
changeset
|
460 |
13960
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
461 // Overloaded function handles need to check the type of the |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
462 // arguments for each element of the array, so they cannot |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
463 // be optimised this way. |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
464 if (f -> is_overloaded ()) |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
465 goto nevermind; |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
466 } |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
467 |
13960
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
468 std::string name = func.function_value () -> name (); |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
469 octave_value f = symbol_table::find_function (name); |
13959
cf8cd43cdeb3
cellfun: further optimisations for the string vs function handle cases
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13928
diff
changeset
|
470 |
13960
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
471 if (f.is_defined ()) |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
472 { |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
473 //Except for these two which are special cases... |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
474 if (name != "size" && name != "class") |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
475 { |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
476 //Try first the optimised code path for built-in functions |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
477 octave_value_list tmp_args = args; |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
478 tmp_args(0) = name; |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
479 retval = try_cellfun_internal_ops (tmp_args, nargin); |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
480 if (error_state || ! retval.empty ()) |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
481 return retval; |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
482 } |
13959
cf8cd43cdeb3
cellfun: further optimisations for the string vs function handle cases
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13928
diff
changeset
|
483 |
13960
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
484 //Okay, we tried, doesn't work, let's do the best we can |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
485 //instead and avoid polymorphic calls for each element of |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
486 //the array. |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
487 func = f; |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
488 } |
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
489 } |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
490 nevermind: |
5988 | 491 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
492 bool uniform_output = true; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
493 octave_value error_handler; |
13960
ef96344a6e58
cellfun: get rid of unnecessary symbol_table_lookup flag
Jordi Gutiérrez Hermoso <jordigh@octave.org>
parents:
13959
diff
changeset
|
494 |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
495 get_mapper_fun_options (args, nargin, uniform_output, error_handler); |
5205 | 496 |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
497 if (error_state) |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
498 return octave_value_list (); |
9465
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
499 |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
500 // Extract cell arguments. |
9465
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
501 |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
502 octave_value_list inputlist (nargin, octave_value ()); |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
503 |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
504 OCTAVE_LOCAL_BUFFER (Cell, inputs, nargin); |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
505 OCTAVE_LOCAL_BUFFER (bool, mask, nargin); |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
506 |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
507 // This is to prevent copy-on-write. |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
508 const Cell *cinputs = inputs; |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
509 |
9684
c5ff5f858cfd
omission from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
9683
diff
changeset
|
510 octave_idx_type k = 1; |
9465
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
511 |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
512 dim_vector fdims (1, 1); |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
513 |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
514 // Collect arguments. Pre-fill scalar elements of inputlist |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
515 // array. |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
516 |
9465
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
517 for (int j = 0; j < nargin; j++) |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
518 { |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
519 if (! args(j+1).is_cell ()) |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
520 { |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
521 error ("cellfun: arguments must be cells"); |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
10044
diff
changeset
|
522 return octave_value_list (); |
9465
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
523 } |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
524 |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
525 inputs[j] = args(j+1).cell_value (); |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
526 mask[j] = inputs[j].numel () != 1; |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
527 if (! mask[j]) |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
528 inputlist(j) = cinputs[j](0); |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
529 } |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
530 |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
531 for (int j = 0; j < nargin; j++) |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
532 { |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
533 if (mask[j]) |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
534 { |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
535 fdims = inputs[j].dims (); |
9683 | 536 k = inputs[j].numel (); |
9465
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
537 for (int i = j+1; i < nargin; i++) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
538 { |
9465
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
539 if (mask[i] && inputs[i].dims () != fdims) |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
540 { |
11590
4ced6b90fffb
style fixes for warning and error messages in source files
John W. Eaton <jwe@octave.org>
parents:
11586
diff
changeset
|
541 error ("cellfun: dimensions mismatch"); |
10066
2cd940306a06
make unwind_protect frames local
Jaroslav Hajek <highegg@gmail.com>
parents:
10044
diff
changeset
|
542 return octave_value_list (); |
9465
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
543 } |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
544 } |
9683 | 545 break; |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
546 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
547 } |
5988 | 548 |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
549 unwind_protect frame; |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
550 frame.protect_var (buffer_error_messages); |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
551 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
552 if (error_handler.is_defined ()) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
553 buffer_error_messages++; |
5988 | 554 |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
555 // Apply functions. |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
556 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
557 if (uniform_output) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
558 { |
10670
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
559 std::list<octave_value_list> idx_list (1); |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
560 idx_list.front ().resize (1); |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
561 std::string idx_type = "("; |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
562 |
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
563 OCTAVE_LOCAL_BUFFER (octave_value, retv, nargout1); |
5988 | 564 |
13231
20ed9548070f
maint: style fixes for cellfun
John W. Eaton <jwe@octave.org>
parents:
13230
diff
changeset
|
565 for (octave_idx_type count = 0; count < k; count++) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
566 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
567 for (int j = 0; j < nargin; j++) |
9465
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
568 { |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
569 if (mask[j]) |
10181
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
570 inputlist.xelem (j) = cinputs[j](count); |
9465
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
571 } |
5988 | 572 |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
573 const octave_value_list tmp |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
574 = get_output_list (count, nargout, inputlist, func, |
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
575 error_handler); |
5988 | 576 |
9459
cb0b21f34abc
avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
577 if (error_state) |
10181
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
578 return retval; |
9459
cb0b21f34abc
avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
579 |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
580 if (nargout > 0 && tmp.length () < nargout) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
581 { |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
582 error ("cellfun: function returned fewer than nargout values"); |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
583 return retval; |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
584 } |
5988 | 585 |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
586 if (nargout > 0 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
587 || (nargout == 0 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
588 && tmp.length () > 0 && tmp(0).is_defined ())) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
589 { |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
590 int num_to_copy = tmp.length (); |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
591 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
592 if (num_to_copy > nargout1) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
593 num_to_copy = nargout1; |
5988 | 594 |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
595 if (count == 0) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
596 { |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
597 for (int j = 0; j < num_to_copy; j++) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
598 { |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
599 if (tmp(j).is_defined ()) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
600 { |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
601 octave_value val = tmp(j); |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
602 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
603 if (val.numel () == 1) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
604 retv[j] = val.resize (fdims); |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
605 else |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
606 { |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
607 error ("cellfun: all values must be scalars when UniformOutput = true"); |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
608 break; |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
609 } |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
610 } |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
611 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
612 } |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
613 else |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
614 { |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
615 for (int j = 0; j < num_to_copy; j++) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
616 { |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
617 if (tmp(j).is_defined ()) |
10670
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
618 { |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
619 octave_value val = tmp(j); |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
620 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
621 if (! retv[j].fast_elem_insert (count, val)) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
622 { |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
623 if (val.numel () == 1) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
624 { |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
625 idx_list.front ()(0) = count + 1.0; |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
626 retv[j].assign (octave_value::op_asn_eq, |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
627 idx_type, idx_list, val); |
10670
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
628 |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
629 if (error_state) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
630 break; |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
631 } |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
632 else |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
633 { |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
634 error ("cellfun: all values must be scalars when UniformOutput = true"); |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
635 break; |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
636 } |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
637 } |
10670
654fbde5dceb
make cellfun's fast scalar collection mechanism public
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
638 } |
8994 | 639 } |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
640 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
641 } |
5988 | 642 |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
643 if (error_state) |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
644 break; |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
645 } |
8994 | 646 |
9988
76cf4aec34e9
allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9740
diff
changeset
|
647 retval.resize (nargout1); |
13229
983720941822
maint: improve readability of cellfun
John W. Eaton <jwe@octave.org>
parents:
12952
diff
changeset
|
648 |
9988
76cf4aec34e9
allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9740
diff
changeset
|
649 for (int j = 0; j < nargout1; j++) |
11457 | 650 { |
651 if (nargout > 0 && retv[j].is_undefined ()) | |
652 retval(j) = NDArray (fdims); | |
653 else | |
654 retval(j) = retv[j]; | |
655 } | |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
656 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
657 else |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
658 { |
9988
76cf4aec34e9
allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9740
diff
changeset
|
659 OCTAVE_LOCAL_BUFFER (Cell, results, nargout1); |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
660 |
9988
76cf4aec34e9
allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9740
diff
changeset
|
661 for (int j = 0; j < nargout1; j++) |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
662 results[j].resize (fdims, Matrix ()); |
5988 | 663 |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
664 bool have_some_output = false; |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
665 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
666 for (octave_idx_type count = 0; count < k; count++) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
667 { |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
668 for (int j = 0; j < nargin; j++) |
9465
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
669 { |
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
670 if (mask[j]) |
10181
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
671 inputlist.xelem (j) = cinputs[j](count); |
9465
40de4692c860
auto-expanding scalar cells in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9459
diff
changeset
|
672 } |
5988 | 673 |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
674 const octave_value_list tmp |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
675 = get_output_list (count, nargout, inputlist, func, |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
676 error_handler); |
5988 | 677 |
9459
cb0b21f34abc
avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
678 if (error_state) |
10181
a668fbd32e34
improve cellfun & avoid undefined values from builtins
Jaroslav Hajek <highegg@gmail.com>
parents:
10155
diff
changeset
|
679 return retval; |
9459
cb0b21f34abc
avoid double error messages in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9450
diff
changeset
|
680 |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
681 if (nargout > 0 && tmp.length () < nargout) |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
682 { |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
683 error ("cellfun: function returned fewer than nargout values"); |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
684 return retval; |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
685 } |
5988 | 686 |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
687 if (nargout > 0 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
688 || (nargout == 0 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
689 && tmp.length () > 0 && tmp(0).is_defined ())) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
690 { |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
691 int num_to_copy = tmp.length (); |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
692 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
693 if (num_to_copy > nargout1) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
694 num_to_copy = nargout1; |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
695 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
696 if (num_to_copy > 0) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
697 have_some_output = true; |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
698 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
699 for (int j = 0; j < num_to_copy; j++) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
700 results[j](count) = tmp(j); |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
701 } |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
702 } |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
703 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
704 if (have_some_output || fdims.any_zero ()) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
705 { |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
706 retval.resize (nargout1); |
5988 | 707 |
9988
76cf4aec34e9
allow functions returning no value in cellfun
Jaroslav Hajek <highegg@gmail.com>
parents:
9740
diff
changeset
|
708 for (int j = 0; j < nargout1; j++) |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
709 retval(j) = results[j]; |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
710 } |
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
711 } |
5205 | 712 } |
9450
cf714e75c656
implement overloaded function handles
Jaroslav Hajek <highegg@gmail.com>
parents:
9377
diff
changeset
|
713 else |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
714 error ("cellfun: argument NAME must be a string or function handle"); |
5205 | 715 |
716 return retval; | |
717 } | |
718 | |
5988 | 719 /* |
720 | |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
721 %!function r = __f11 (x) |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
722 %! global __cellfun_test_num_outputs__ |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
723 %! __cellfun_test_num_outputs__ = nargout; |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
724 %! r = x; |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
725 %!endfunction |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
726 |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
727 %!function __f01 (x) |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
728 %! global __cellfun_test_num_outputs__ |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
729 %! __cellfun_test_num_outputs__ = nargout; |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
730 %!endfunction |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
731 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
732 %!test |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
733 %! global __cellfun_test_num_outputs__ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
734 %! cellfun (@__f11, {1}); |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
735 %! assert (__cellfun_test_num_outputs__, 0) |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
736 %! x = cellfun (@__f11, {1}); |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
737 %! assert (__cellfun_test_num_outputs__, 1) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
738 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
739 %!test |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
740 %! global __cellfun_test_num_outputs__ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
741 %! cellfun (@__f01, {1}); |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
742 %! assert (__cellfun_test_num_outputs__, 0) |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
743 |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
744 %!error x = cellfun (@__f01, {1, 2}); |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
745 |
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
746 %!test |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
747 %! assert (cellfun (@__f11, {1, 2}), [1, 2]) |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
748 %! assert (cellfun (@__f11, {1, 2}, 'uniformoutput', false), {1, 2}) |
13230
c73cc5b67feb
fix nargout handling for functions called by cellfun
John W. Eaton <jwe@octave.org>
parents:
13229
diff
changeset
|
749 |
11457 | 750 %!test |
751 %! [a,b] = cellfun (@(x) x, cell (2, 0)); | |
752 %! assert (a, zeros (2, 0)); | |
753 %! assert (b, zeros (2, 0)); | |
754 | |
755 %!test | |
756 %! [a,b] = cellfun (@(x) x, cell (2, 0), "uniformoutput", false); | |
757 %! assert (a, cell (2, 0)); | |
758 %! assert (b, cell (2, 0)); | |
759 | |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
760 %% Test function to check the "Errorhandler" option |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
761 %!function [z] = __cellfunerror (S, varargin) |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
762 %! z = S; |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
763 %!endfunction |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
764 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
765 %% First input argument can be a string, an inline function, |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
766 %% a function_handle or an anonymous function |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
767 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
768 %! A = cellfun ("islogical", {true, 0.1, false, i*2}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
769 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
770 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
771 %! A = cellfun (inline ("islogical (x)", "x"), {true, 0.1, false, i*2}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
772 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
773 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
774 %! A = cellfun (@islogical, {true, 0.1, false, i*2}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
775 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
776 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
777 %! A = cellfun (@(x) islogical(x), {true, 0.1, false, i*2}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
778 %! assert (A, [true, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
779 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
780 %% First input argument can be the special string "isreal", |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
781 %% "isempty", "islogical", "length", "ndims" or "prodofsize" |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
782 %!test |
10291
fc879f361bda
make isreal matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10181
diff
changeset
|
783 %! A = cellfun ("isreal", {true, 0.1, {}, i*2, [], "abc"}); |
fc879f361bda
make isreal matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10181
diff
changeset
|
784 %! assert (A, [true, true, false, false, true, true]); |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
785 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
786 %! A = cellfun ("isempty", {true, 0.1, false, i*2, [], "abc"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
787 %! assert (A, [false, false, false, false, true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
788 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
789 %! A = cellfun ("islogical", {true, 0.1, false, i*2, [], "abc"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
790 %! assert (A, [true, false, true, false, false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
791 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
792 %! A = cellfun ("length", {true, 0.1, false, i*2, [], "abc"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
793 %! assert (A, [1, 1, 1, 1, 0, 3]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
794 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
795 %! A = cellfun ("ndims", {[1, 2; 3, 4]; (cell (1,2,3,4))}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
796 %! assert (A, [2; 4]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
797 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
798 %! A = cellfun ("prodofsize", {[1, 2; 3, 4], (cell (1,2,3,4))}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
799 %! assert (A, [4, 24]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
800 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
801 %% Number of input and output arguments may not be limited to one |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
802 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
803 %! A = cellfun (@(x,y,z) x + y + z, {1, 1, 1}, {2, 2, 2}, {3, 4, 5}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
804 %! assert (A, [6, 7, 8]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
805 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
806 %! A = cellfun (@(x,y,z) x + y + z, {1, 1, 1}, {2, 2, 2}, {3, 4, 5}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
807 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
808 %! assert (A, {6, 7, 8}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
809 %!test %% Two input arguments of different types |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
810 %! A = cellfun (@(x,y) islogical (x) && ischar (y), {false, true}, {"a", 3}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
811 %! assert (A, [true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
812 %!test %% Pass another variable to the anonymous function |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
813 %! y = true; A = cellfun (@(x) islogical (x) && y, {false, 0.3}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
814 %! assert (A, [true, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
815 %!test %% Three ouptut arguments of different type |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
816 %! [A, B, C] = cellfun (@find, {10, 11; 0, 12}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
817 %! assert (isequal (A, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
818 %! assert (isequal (B, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
819 %! assert (isequal (C, {10, 11; [], 12})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
820 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
821 %% Input arguments can be of type cell array of logical |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
822 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
823 %! A = cellfun (@(x,y) x == y, {false, true}, {true, true}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
824 %! assert (A, [false, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
825 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
826 %! A = cellfun (@(x,y) x == y, {false; true}, {true; true}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
827 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
828 %! assert (A, [false; true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
829 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
830 %! A = cellfun (@(x) x, {false, true; false, true}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
831 %! assert (A, {false, true; false, true}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
832 %!test %% Three ouptut arguments of same type |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
833 %! [A, B, C] = cellfun (@find, {true, false; false, true}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
834 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
835 %! assert (isequal (A, {true, []; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
836 %! assert (isequal (B, {true, []; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
837 %! assert (isequal (C, {true, []; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
838 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
839 %! A = cellfun (@(x,y) cell2str (x,y), {true}, {true}, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
840 %! "ErrorHandler", @__cellfunerror); |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
841 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
842 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
843 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
844 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
845 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
846 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
847 %! A = cellfun (@(x,y) cell2str (x,y), {true}, {true}, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
848 %! "UniformOutput", true, "ErrorHandler", @__cellfunerror); |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
849 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
850 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
851 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
852 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
853 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
854 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
855 %% Input arguments can be of type cell array of numeric |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
856 %!test |
9622 | 857 %! A = cellfun (@(x,y) x>y, {1.1, 4.2}, {3.1, 2+3*i}); |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
858 %! assert (A, [false, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
859 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
860 %! A = cellfun (@(x,y) x>y, {1.1, 4.2; 2, 4}, {3.1, 2; 2, 4+2*i}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
861 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
862 %! assert (A, [false, true; false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
863 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
864 %! A = cellfun (@(x,y) x:y, {1.1, 4}, {3.1, 6}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
865 %! assert (isequal (A{1}, [1.1, 2.1, 3.1])); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
866 %! assert (isequal (A{2}, [4, 5, 6])); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
867 %!test %% Three ouptut arguments of different type |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
868 %! [A, B, C] = cellfun (@find, {10, 11; 0, 12}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
869 %! assert (isequal (A, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
870 %! assert (isequal (B, {true, true; [], true})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
871 %! assert (isequal (C, {10, 11; [], 12})); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
872 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
873 %! A = cellfun (@(x,y) cell2str(x,y), {1.1, 4}, {3.1, 6}, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
874 %! "ErrorHandler", @__cellfunerror); |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
875 %! B = isfield (A(1), "message") && isfield (A(1), "index"); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
876 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
877 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
878 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
879 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
880 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
881 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
882 %! A = cellfun (@(x,y) cell2str(x,y), {1.1, 4}, {3.1, 6}, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
883 %! "UniformOutput", true, "ErrorHandler", @__cellfunerror); |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
884 %! B = isfield (A(1), "message") && isfield (A(1), "index"); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
885 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
886 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
887 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
888 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
889 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
890 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
891 %% Input arguments can be of type cell arrays of character or strings |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
892 %!error %% "UniformOutput" false should be used |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
893 %! A = cellfun (@(x,y) x>y, {"ad", "c", "ghi"}, {"cc", "d", "fgh"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
894 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
895 %! A = cellfun (@(x,y) x>y, {"a"; "f"}, {"c"; "d"}, "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
896 %! assert (A, [false; true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
897 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
898 %! A = cellfun (@(x,y) x:y, {"a", "d"}, {"c", "f"}, "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
899 %! assert (A, {"abc", "def"}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
900 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
901 %! A = cellfun (@(x,y) cell2str(x,y), {"a", "d"}, {"c", "f"}, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
902 %! "ErrorHandler", @__cellfunerror); |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
903 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
904 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
905 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
906 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
907 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
908 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
909 %! A = cellfun (@(x,y) cell2str(x,y), {"a", "d"}, {"c", "f"}, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
910 %! "UniformOutput", true, "ErrorHandler", @__cellfunerror); |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
911 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
912 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
913 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
914 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
915 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
916 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
917 %% Structures cannot be handled by cellfun |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
918 %!error |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
919 %! vst1.a = 1.1; vst1.b = 4.2; vst2.a = 3.1; vst2.b = 2; |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
920 %! A = cellfun (@(x,y) (x.a < y.a) && (x.b > y.b), vst1, vst2); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
921 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
922 %% Input arguments can be of type cell array of cell arrays |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
923 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
924 %! A = cellfun (@(x,y) x{1} < y{1}, {{1.1}, {4.2}}, {{3.1}, {2}}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
925 %! assert (A, [1, 0], 1e-16); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
926 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
927 %! A = cellfun (@(x,y) x{1} < y{1}, {{1.1}; {4.2}}, {{3.1}; {2}}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
928 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
929 %! assert (A, [1; 0], 1e-16); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
930 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
931 %! A = cellfun (@(x,y) x{1} < y{1}, {{1.1}, {4.2}}, {{3.1}, {2}}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
932 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
933 %! assert (A, {true, false}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
934 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
935 %! A = cellfun (@(x,y) mat2str(x,y), {{1.1}, {4.2}}, {{3.1}, {2}}, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
936 %! "ErrorHandler", @__cellfunerror); |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
937 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
938 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
939 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
940 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
941 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
942 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
943 %! A = cellfun (@(x,y) mat2str(x,y), {{1.1}, {4.2}}, {{3.1}, {2}}, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
944 %! "UniformOutput", true, "ErrorHandler", @__cellfunerror); |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
945 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
946 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
947 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
948 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
949 %! assert ([A(1).index, A(2).index], [1, 2]); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
950 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
951 %% Input arguments can be of type cell array of structure arrays |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
952 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
953 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
954 %! A = cellfun (@(x,y) (x.a == y.a) && (x.b < y.b), {a}, {b}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
955 %! assert (A, true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
956 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
957 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
958 %! A = cellfun (@(x,y) (x.a == y.a) && (x.b < y.b) , {a}, {b}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
959 %! "UniformOutput", true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
960 %! assert (A, true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
961 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
962 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
963 %! A = cellfun (@(x,y) (x.a == y.a) && (x.b < y.b) , {a}, {b}, \ |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
964 %! "UniformOutput", false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
965 %! assert (A, {true}); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
966 %!test |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
967 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
968 %! A = cellfun (@(x,y) cell2str (x.a, y.a), {a}, {b}, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
969 %! "ErrorHandler", @__cellfunerror); |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
970 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
971 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
972 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
973 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
974 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
975 %!test %% Overwriting setting of "UniformOutput" true |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
976 %! a = struct ("a", 1, "b", 2); b = struct ("a", 1, "b", 3); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
977 %! A = cellfun (@(x,y) cell2str (x.a, y.a), {a}, {b}, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
978 %! "UniformOutput", true, "ErrorHandler", @__cellfunerror); |
8373
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
979 %! assert (isfield (A, "identifier"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
980 %! assert (isfield (A, "message"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
981 %! assert (isfield (A, "index"), true); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
982 %! assert (isempty (A.message), false); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
983 %! assert (A.index, 1); |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
984 |
63fe023d7898
Added more tests for cellfun.
Thomas Treichl <Thomas.Treichl@gmx.net>
parents:
8347
diff
changeset
|
985 %% A lot of other tests |
5988 | 986 %!error(cellfun(1)) |
987 %!error(cellfun('isclass',1)) | |
988 %!error(cellfun('size',1)) | |
989 %!error(cellfun(@sin,{[]},'BadParam',false)) | |
990 %!error(cellfun(@sin,{[]},'UniformOuput')) | |
991 %!error(cellfun(@sin,{[]},'ErrorHandler')) | |
992 %!assert(cellfun(@sin,{0,1}),sin([0,1])) | |
993 %!assert(cellfun(inline('sin(x)'),{0,1}),sin([0,1])) | |
994 %!assert(cellfun('sin',{0,1}),sin([0,1])) | |
995 %!assert(cellfun('isempty',{1,[]}),[false,true]) | |
996 %!assert(cellfun('islogical',{false,pi}),[true,false]) | |
997 %!assert(cellfun('isreal',{1i,1}),[false,true]) | |
998 %!assert(cellfun('length',{zeros(2,2),1}),[2,1]) | |
999 %!assert(cellfun('prodofsize',{zeros(2,2),1}),[4,1]) | |
1000 %!assert(cellfun('ndims',{zeros([2,2,2]),1}),[3,2]) | |
1001 %!assert(cellfun('isclass',{zeros([2,2,2]),'test'},'double'),[true,false]) | |
1002 %!assert(cellfun('size',{zeros([1,2,3]),1},1),[1,1]) | |
1003 %!assert(cellfun('size',{zeros([1,2,3]),1},2),[2,1]) | |
1004 %!assert(cellfun('size',{zeros([1,2,3]),1},3),[3,1]) | |
1005 %!assert(cellfun(@atan2,{1,1},{1,2}),[atan2(1,1),atan2(1,2)]) | |
1006 %!assert(cellfun(@atan2,{1,1},{1,2},'UniformOutput',false),{atan2(1,1),atan2(1,2)}) | |
6422 | 1007 %!assert(cellfun(@sin,{1,2;3,4}),sin([1,2;3,4])) |
1008 %!assert(cellfun(@atan2,{1,1;1,1},{1,2;1,2}),atan2([1,1;1,1],[1,2;1,2])) | |
5988 | 1009 %!error(cellfun(@factorial,{-1,3})) |
1010 %!assert(cellfun(@factorial,{-1,3},'ErrorHandler',@(x,y) NaN),[NaN,6]) | |
16046
1678d0fca146
fix ErrorHandler in cellfun (bug #38256)
Stefan Mahr <dac922@gmx.de>
parents:
14138
diff
changeset
|
1011 %!assert (cellfun (@(x) x(2),{[1],[1,2]},"ErrorHandler",@(x,y) NaN), [NaN,2]) |
5988 | 1012 %!test |
6704 | 1013 %! [a,b,c]=cellfun(@fileparts,{fullfile("a","b","c.d"),fullfile("e","f","g.h")},'UniformOutput',false); |
1014 %! assert(a,{fullfile("a","b"),fullfile("e","f")}) | |
5988 | 1015 %! assert(b,{'c','g'}) |
1016 %! assert(c,{'.d','.h'}) | |
1017 | |
1018 */ | |
1019 | |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1020 // Arrayfun was originally a .m file written by Bill Denney and Jaroslav |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1021 // Hajek. It was converted to C++ by jwe so that it could properly |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1022 // handle the nargout = 0 case. |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1023 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1024 DEFUN_DLD (arrayfun, args, nargout, |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1025 "-*- texinfo -*-\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1026 @deftypefn {Function File} {} arrayfun (@var{func}, @var{A})\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1027 @deftypefnx {Function File} {@var{x} =} arrayfun (@var{func}, @var{A})\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1028 @deftypefnx {Function File} {@var{x} =} arrayfun (@var{func}, @var{A}, @var{b}, @dots{})\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1029 @deftypefnx {Function File} {[@var{x}, @var{y}, @dots{}] =} arrayfun (@var{func}, @var{A}, @dots{})\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1030 @deftypefnx {Function File} {} arrayfun (@dots{}, \"UniformOutput\", @var{val})\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1031 @deftypefnx {Function File} {} arrayfun (@dots{}, \"ErrorHandler\", @var{errfunc})\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1032 \n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1033 Execute a function on each element of an array. This is useful for\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1034 functions that do not accept array arguments. If the function does\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1035 accept array arguments it is better to call the function directly.\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1036 \n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1037 The first input argument @var{func} can be a string, a function\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1038 handle, an inline function, or an anonymous function. The input\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1039 argument @var{A} can be a logic array, a numeric array, a string\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1040 array, a structure array, or a cell array. By a call of the function\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1041 @command{arrayfun} all elements of @var{A} are passed on to the named\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1042 function @var{func} individually.\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1043 \n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1044 The named function can also take more than two input arguments, with\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1045 the input arguments given as third input argument @var{b}, fourth\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1046 input argument @var{c}, @dots{} If given more than one array input\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1047 argument then all input arguments must have the same sizes, for\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1048 example:\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1049 \n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1050 @example\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1051 @group\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1052 arrayfun (@@atan2, [1, 0], [0, 1])\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1053 @result{} ans = [1.5708 0.0000]\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1054 @end group\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1055 @end example\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1056 \n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1057 If the parameter @var{val} after a further string input argument\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1058 \"UniformOutput\" is set @code{true} (the default), then the named\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1059 function @var{func} must return a single element which then will be\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1060 concatenated into the return value and is of type matrix. Otherwise,\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1061 if that parameter is set to @code{false}, then the outputs are\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1062 concatenated in a cell array. For example:\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1063 \n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1064 @example\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1065 @group\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1066 arrayfun (@@(x,y) x:y, \"abc\", \"def\", \"UniformOutput\", false)\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1067 @result{} ans =\n\ |
14093
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14085
diff
changeset
|
1068 @{\n\ |
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14085
diff
changeset
|
1069 [1,1] = abcd\n\ |
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14085
diff
changeset
|
1070 [1,2] = bcde\n\ |
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14085
diff
changeset
|
1071 [1,3] = cdef\n\ |
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14085
diff
changeset
|
1072 @}\n\ |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1073 @end group\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1074 @end example\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1075 \n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1076 If more than one output arguments are given then the named function\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1077 must return the number of return values that also are expected, for\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1078 example:\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1079 \n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1080 @example\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1081 @group\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1082 [A, B, C] = arrayfun (@@find, [10; 0], \"UniformOutput\", false)\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1083 @result{}\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1084 A =\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1085 @{\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1086 [1,1] = 1\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1087 [2,1] = [](0x0)\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1088 @}\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1089 B =\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1090 @{\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1091 [1,1] = 1\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1092 [2,1] = [](0x0)\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1093 @}\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1094 C =\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1095 @{\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1096 [1,1] = 10\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1097 [2,1] = [](0x0)\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1098 @}\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1099 @end group\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1100 @end example\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1101 \n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1102 If the parameter @var{errfunc} after a further string input argument\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1103 \"ErrorHandler\" is another string, a function handle, an inline\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1104 function, or an anonymous function, then @var{errfunc} defines a\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1105 function to call in the case that @var{func} generates an error.\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1106 The definition of the function must be of the form\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1107 \n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1108 @example\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1109 function [@dots{}] = errfunc (@var{s}, @dots{})\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1110 @end example\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1111 \n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1112 @noindent\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1113 where there is an additional input argument to @var{errfunc}\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1114 relative to @var{func}, given by @var{s}. This is a structure with\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1115 the elements \"identifier\", \"message\", and \"index\" giving,\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1116 respectively, the error identifier, the error message, and the index of\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1117 the array elements that caused the error. The size of the output\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1118 argument of @var{errfunc} must have the same size as the output\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1119 argument of @var{func}, otherwise a real error is thrown. For\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1120 example:\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1121 \n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1122 @example\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1123 @group\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1124 function y = ferr (s, x), y = \"MyString\"; endfunction\n\ |
14093
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14085
diff
changeset
|
1125 arrayfun (@@str2num, [1234],\n\ |
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14085
diff
changeset
|
1126 \"UniformOutput\", false, \"ErrorHandler\", @@ferr)\n\ |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1127 @result{} ans =\n\ |
14093
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14085
diff
changeset
|
1128 @{\n\ |
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14085
diff
changeset
|
1129 [1,1] = MyString\n\ |
050bc580cb60
doc: Various docstring improvements before 3.6.0 release.
Rik <octave@nomad.inbox5.com>
parents:
14085
diff
changeset
|
1130 @}\n\ |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1131 @end group\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1132 @end example\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1133 \n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1134 @seealso{spfun, cellfun, structfun}\n\ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1135 @end deftypefn") |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1136 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1137 octave_value_list retval; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1138 int nargin = args.length (); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1139 int nargout1 = (nargout < 1 ? 1 : nargout); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1140 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1141 if (nargin < 2) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1142 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1143 error ("arrayfun: function requires at least 2 arguments"); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1144 print_usage (); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1145 return retval; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1146 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1147 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1148 octave_value func = args(0); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1149 bool symbol_table_lookup = false; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1150 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1151 if (func.is_string ()) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1152 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1153 // See if we can convert the string into a function. |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1154 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1155 std::string name = args(0).string_value (); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1156 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1157 if (! valid_identifier (name)) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1158 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1159 std::string fcn_name = unique_symbol_name ("__arrayfun_fcn_"); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1160 std::string fname = "function y = " + fcn_name + "(x) y = "; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1161 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1162 octave_function *ptr_func |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1163 = extract_function (args(0), "arrayfun", fcn_name, |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1164 fname, "; endfunction"); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1165 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1166 if (ptr_func && ! error_state) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1167 func = octave_value (ptr_func, true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1168 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1169 else |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1170 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1171 func = symbol_table::find_function (name); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1172 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1173 if (func.is_undefined ()) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1174 error ("arrayfun: invalid function NAME: %s", name.c_str ()); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1175 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1176 symbol_table_lookup = true; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1177 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1178 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1179 if (error_state) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1180 return retval; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1181 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1182 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1183 if (func.is_function_handle () || func.is_inline_function () |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1184 || func.is_function ()) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1185 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1186 // The following is an optimisation because the symbol table can |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1187 // give a more specific function class, so this can result in |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1188 // fewer polymorphic function calls as the function gets called |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1189 // for each value of the array. |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1190 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1191 if (! symbol_table_lookup ) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1192 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1193 if (func.is_function_handle ()) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1194 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1195 octave_fcn_handle* f = func.fcn_handle_value (); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1196 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1197 // Overloaded function handles need to check the type of |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1198 // the arguments for each element of the array, so they |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1199 // cannot be optimised this way. |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1200 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1201 if (f -> is_overloaded ()) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1202 goto nevermind; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1203 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1204 octave_value f = symbol_table::find_function (func.function_value () |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1205 -> name ()); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1206 if (f.is_defined ()) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1207 func = f; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1208 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1209 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1210 nevermind: |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1211 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1212 bool uniform_output = true; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1213 octave_value error_handler; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1214 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1215 get_mapper_fun_options (args, nargin, uniform_output, error_handler); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1216 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1217 if (error_state) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1218 return octave_value_list (); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1219 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1220 octave_value_list inputlist (nargin, octave_value ()); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1221 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1222 OCTAVE_LOCAL_BUFFER (octave_value, inputs, nargin); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1223 OCTAVE_LOCAL_BUFFER (bool, mask, nargin); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1224 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1225 octave_idx_type k = 1; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1226 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1227 dim_vector fdims (1, 1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1228 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1229 // Collect arguments. Pre-fill scalar elements of inputlist |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1230 // array. |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1231 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1232 for (int j = 0; j < nargin; j++) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1233 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1234 inputs[j] = args(j+1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1235 mask[j] = inputs[j].numel () != 1; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1236 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1237 if (! mask[j]) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1238 inputlist(j) = inputs[j]; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1239 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1240 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1241 for (int j = 0; j < nargin; j++) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1242 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1243 if (mask[j]) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1244 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1245 fdims = inputs[j].dims (); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1246 k = inputs[j].numel (); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1247 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1248 for (int i = j+1; i < nargin; i++) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1249 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1250 if (mask[i] && inputs[i].dims () != fdims) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1251 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1252 error ("arrayfun: dimensions mismatch"); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1253 return retval; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1254 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1255 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1256 break; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1257 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1258 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1259 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1260 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1261 unwind_protect frame; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1262 frame.protect_var (buffer_error_messages); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1263 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1264 if (error_handler.is_defined ()) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1265 buffer_error_messages++; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1266 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1267 // Apply functions. |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1268 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1269 if (uniform_output) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1270 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1271 std::list<octave_value_list> idx_list (1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1272 idx_list.front ().resize (1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1273 std::string idx_type = "("; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1274 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1275 OCTAVE_LOCAL_BUFFER (octave_value, retv, nargout1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1276 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1277 for (octave_idx_type count = 0; count < k; count++) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1278 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1279 idx_list.front ()(0) = count + 1.0; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1280 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1281 for (int j = 0; j < nargin; j++) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1282 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1283 if (mask[j]) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1284 inputlist.xelem (j) = inputs[j].do_index_op (idx_list); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1285 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1286 if (error_state) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1287 return retval; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1288 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1289 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1290 const octave_value_list tmp |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1291 = get_output_list (count, nargout, inputlist, func, |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1292 error_handler); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1293 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1294 if (error_state) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1295 return retval; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1296 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1297 if (nargout > 0 && tmp.length () < nargout) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1298 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1299 error ("arrayfun: function returned fewer than nargout values"); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1300 return retval; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1301 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1302 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1303 if (nargout > 0 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1304 || (nargout == 0 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1305 && tmp.length () > 0 && tmp(0).is_defined ())) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1306 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1307 int num_to_copy = tmp.length (); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1308 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1309 if (num_to_copy > nargout1) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1310 num_to_copy = nargout1; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1311 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1312 if (count == 0) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1313 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1314 for (int j = 0; j < num_to_copy; j++) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1315 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1316 if (tmp(j).is_defined ()) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1317 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1318 octave_value val = tmp(j); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1319 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1320 if (val.numel () == 1) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1321 retv[j] = val.resize (fdims); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1322 else |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1323 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1324 error ("arrayfun: all values must be scalars when UniformOutput = true"); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1325 break; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1326 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1327 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1328 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1329 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1330 else |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1331 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1332 for (int j = 0; j < num_to_copy; j++) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1333 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1334 if (tmp(j).is_defined ()) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1335 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1336 octave_value val = tmp(j); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1337 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1338 if (! retv[j].fast_elem_insert (count, val)) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1339 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1340 if (val.numel () == 1) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1341 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1342 idx_list.front ()(0) = count + 1.0; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1343 retv[j].assign (octave_value::op_asn_eq, |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1344 idx_type, idx_list, val); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1345 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1346 if (error_state) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1347 break; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1348 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1349 else |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1350 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1351 error ("arrayfun: all values must be scalars when UniformOutput = true"); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1352 break; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1353 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1354 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1355 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1356 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1357 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1358 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1359 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1360 if (error_state) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1361 break; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1362 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1363 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1364 retval.resize (nargout1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1365 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1366 for (int j = 0; j < nargout1; j++) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1367 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1368 if (nargout > 0 && retv[j].is_undefined ()) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1369 retval(j) = NDArray (fdims); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1370 else |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1371 retval(j) = retv[j]; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1372 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1373 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1374 else |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1375 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1376 std::list<octave_value_list> idx_list (1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1377 idx_list.front ().resize (1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1378 std::string idx_type = "("; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1379 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1380 OCTAVE_LOCAL_BUFFER (Cell, results, nargout1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1381 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1382 for (int j = 0; j < nargout1; j++) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1383 results[j].resize (fdims, Matrix ()); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1384 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1385 bool have_some_output = false; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1386 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1387 for (octave_idx_type count = 0; count < k; count++) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1388 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1389 idx_list.front ()(0) = count + 1.0; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1390 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1391 for (int j = 0; j < nargin; j++) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1392 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1393 if (mask[j]) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1394 inputlist.xelem (j) = inputs[j].do_index_op (idx_list); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1395 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1396 if (error_state) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1397 return retval; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1398 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1399 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1400 const octave_value_list tmp |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1401 = get_output_list (count, nargout, inputlist, func, |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1402 error_handler); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1403 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1404 if (error_state) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1405 return retval; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1406 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1407 if (nargout > 0 && tmp.length () < nargout) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1408 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1409 error ("arrayfun: function returned fewer than nargout values"); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1410 return retval; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1411 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1412 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1413 if (nargout > 0 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1414 || (nargout == 0 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1415 && tmp.length () > 0 && tmp(0).is_defined ())) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1416 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1417 int num_to_copy = tmp.length (); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1418 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1419 if (num_to_copy > nargout1) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1420 num_to_copy = nargout1; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1421 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1422 if (num_to_copy > 0) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1423 have_some_output = true; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1424 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1425 for (int j = 0; j < num_to_copy; j++) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1426 results[j](count) = tmp(j); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1427 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1428 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1429 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1430 if (have_some_output || fdims.any_zero ()) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1431 { |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1432 retval.resize (nargout1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1433 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1434 for (int j = 0; j < nargout1; j++) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1435 retval(j) = results[j]; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1436 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1437 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1438 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1439 else |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1440 error ("arrayfun: argument NAME must be a string or function handle"); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1441 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1442 return retval; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1443 } |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1444 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1445 /* |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1446 %!function r = __f11 (x) |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1447 %! global __arrayfun_test_num_outputs__ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1448 %! __arrayfun_test_num_outputs__ = nargout; |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1449 %! r = x; |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1450 %!endfunction |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1451 |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1452 %!function __f01 (x) |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1453 %! global __arrayfun_test_num_outputs__ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1454 %! __arrayfun_test_num_outputs__ = nargout; |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1455 %!endfunction |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1456 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1457 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1458 %! global __arrayfun_test_num_outputs__ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1459 %! arrayfun (@__f11, {1}); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1460 %! assert (__arrayfun_test_num_outputs__, 0) |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1461 %! x = arrayfun (@__f11, {1}); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1462 %! assert (__arrayfun_test_num_outputs__, 1) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1463 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1464 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1465 %! global __arrayfun_test_num_outputs__ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1466 %! arrayfun (@__f01, {1}); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1467 %! assert (__arrayfun_test_num_outputs__, 0) |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1468 |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1469 %!error x = arrayfun (@__f01, [1, 2]); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1470 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1471 %!test |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1472 %! assert (arrayfun (@__f11, [1, 2]), [1, 2]) |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1473 %! assert (arrayfun (@__f11, [1, 2], 'uniformoutput', false), {1, 2}); |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1474 %! assert (arrayfun (@__f11, {1, 2}), {1, 2}) |
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1475 %! assert (arrayfun (@__f11, {1, 2}, 'uniformoutput', false), {{1}, {2}}); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1476 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1477 %!assert (arrayfun (@ones, 1, [2,3], 'uniformoutput', false), {[1,1], [1,1,1]}); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1478 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1479 %% Test function to check the "Errorhandler" option |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1480 %!function [z] = __arrayfunerror (S, varargin) |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1481 %! z = S; |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1482 %!endfunction |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1483 %% First input argument can be a string, an inline function, a |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1484 %% function_handle or an anonymous function |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1485 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1486 %! arrayfun (@isequal, [false, true], [true, true]); %% No output argument |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1487 %!error |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1488 %! arrayfun (@isequal); %% One or less input arguments |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1489 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1490 %! A = arrayfun ("isequal", [false, true], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1491 %! assert (A, [false, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1492 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1493 %! A = arrayfun (inline ("(x == y)", "x", "y"), [false, true], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1494 %! assert (A, [false, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1495 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1496 %! A = arrayfun (@isequal, [false, true], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1497 %! assert (A, [false, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1498 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1499 %! A = arrayfun (@(x,y) isequal(x,y), [false, true], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1500 %! assert (A, [false, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1501 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1502 %% Number of input and output arguments may be greater than one |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1503 %#!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1504 %! A = arrayfun (@(x) islogical (x), false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1505 %! assert (A, true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1506 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1507 %! A = arrayfun (@(x,y,z) x + y + z, [1, 1, 1], [2, 2, 2], [3, 4, 5]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1508 %! assert (A, [6, 7, 8], 1e-16); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1509 %!test %% Two input arguments of different types |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1510 %! A = arrayfun (@(x,y) islogical (x) && ischar (y), false, "a"); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1511 %! assert (A, true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1512 %!test %% Pass another variable to the anonymous function |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1513 %! y = true; A = arrayfun (@(x) islogical (x && y), false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1514 %! assert (A, true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1515 %!test %% Three ouptut arguments of different type |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1516 %! [A, B, C] = arrayfun (@find, [10, 11; 0, 12], "UniformOutput", false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1517 %! assert (isequal (A, {true, true; [], true})); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1518 %! assert (isequal (B, {true, true; [], true})); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1519 %! assert (isequal (C, {10, 11; [], 12})); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1520 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1521 %% Input arguments can be of type logical |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1522 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1523 %! A = arrayfun (@(x,y) x == y, [false, true], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1524 %! assert (A, [false, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1525 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1526 %! A = arrayfun (@(x,y) x == y, [false; true], [true; true], "UniformOutput", true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1527 %! assert (A, [false; true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1528 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1529 %! A = arrayfun (@(x) x, [false, true, false, true], "UniformOutput", false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1530 %! assert (A, {false, true, false, true}); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1531 %!test %% Three ouptut arguments of same type |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1532 %! [A, B, C] = arrayfun (@find, [true, false; false, true], "UniformOutput", false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1533 %! assert (isequal (A, {true, []; [], true})); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1534 %! assert (isequal (B, {true, []; [], true})); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1535 %! assert (isequal (C, {true, []; [], true})); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1536 %!test |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1537 %! A = arrayfun (@(x,y) array2str (x,y), true, true, "ErrorHandler", @__arrayfunerror); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1538 %! assert (isfield (A, "identifier"), true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1539 %! assert (isfield (A, "message"), true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1540 %! assert (isfield (A, "index"), true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1541 %! assert (isempty (A.message), false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1542 %! assert (A.index, 1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1543 %!test %% Overwriting setting of "UniformOutput" true |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1544 %! A = arrayfun (@(x,y) array2str (x,y), true, true, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1545 %! "UniformOutput", true, "ErrorHandler", @__arrayfunerror); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1546 %! assert (isfield (A, "identifier"), true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1547 %! assert (isfield (A, "message"), true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1548 %! assert (isfield (A, "index"), true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1549 %! assert (isempty (A.message), false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1550 %! assert (A.index, 1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1551 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1552 %% Input arguments can be of type numeric |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1553 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1554 %! A = arrayfun (@(x,y) x>y, [1.1, 4.2], [3.1, 2+3*i]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1555 %! assert (A, [false, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1556 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1557 %! A = arrayfun (@(x,y) x>y, [1.1, 4.2; 2, 4], [3.1, 2; 2, 4+2*i], "UniformOutput", true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1558 %! assert (A, [false, true; false, false]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1559 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1560 %! A = arrayfun (@(x,y) x:y, [1.1, 4], [3.1, 6], "UniformOutput", false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1561 %! assert (isequal (A{1}, [1.1, 2.1, 3.1])); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1562 %! assert (isequal (A{2}, [4, 5, 6])); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1563 %!test %% Three ouptut arguments of different type |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1564 %! [A, B, C] = arrayfun (@find, [10, 11; 0, 12], "UniformOutput", false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1565 %! assert (isequal (A, {true, true; [], true})); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1566 %! assert (isequal (B, {true, true; [], true})); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1567 %! assert (isequal (C, {10, 11; [], 12})); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1568 %!test |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1569 %! A = arrayfun (@(x,y) array2str(x,y), {1.1, 4}, {3.1, 6}, "ErrorHandler", @__arrayfunerror); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1570 %! B = isfield (A(1), "message") && isfield (A(1), "index"); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1571 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1572 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1573 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1574 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1575 %! assert ([A(1).index, A(2).index], [1, 2]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1576 %!test %% Overwriting setting of "UniformOutput" true |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1577 %! A = arrayfun (@(x,y) array2str(x,y), {1.1, 4}, {3.1, 6}, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1578 %! "UniformOutput", true, "ErrorHandler", @__arrayfunerror); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1579 %! B = isfield (A(1), "message") && isfield (A(1), "index"); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1580 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1581 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1582 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1583 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1584 %! assert ([A(1).index, A(2).index], [1, 2]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1585 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1586 %% Input arguments can be of type character or strings |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1587 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1588 %! A = arrayfun (@(x,y) x>y, ["ad", "c", "ghi"], ["cc", "d", "fgh"]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1589 %! assert (A, [false, true, false, true, true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1590 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1591 %! A = arrayfun (@(x,y) x>y, ["a"; "f"], ["c"; "d"], "UniformOutput", true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1592 %! assert (A, [false; true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1593 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1594 %! A = arrayfun (@(x,y) x:y, ["a", "d"], ["c", "f"], "UniformOutput", false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1595 %! assert (A, {"abc", "def"}); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1596 %! %#!test |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1597 %! A = arrayfun (@(x,y) cell2str(x,y), ["a", "d"], ["c", "f"], "ErrorHandler", @__arrayfunerror); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1598 %! B = isfield (A(1), "identifier") && isfield (A(1), "message") && isfield (A(1), "index"); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1599 %! assert (B, true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1600 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1601 %% Input arguments can be of type structure |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1602 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1603 %! a = struct ("a", 1.1, "b", 4.2); b = struct ("a", 3.1, "b", 2); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1604 %! A = arrayfun (@(x,y) (x.a < y.a) && (x.b > y.b), a, b); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1605 %! assert (A, true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1606 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1607 %! a = struct ("a", 1.1, "b", 4.2); b = struct ("a", 3.1, "b", 2); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1608 %! A = arrayfun (@(x,y) (x.a < y.a) && (x.b > y.b), a, b, "UniformOutput", true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1609 %! assert (A, true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1610 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1611 %! a = struct ("a", 1.1, "b", 4.2); b = struct ("a", 3.1, "b", 2); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1612 %! A = arrayfun (@(x,y) x.a:y.a, a, b, "UniformOutput", false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1613 %! assert (isequal (A, {[1.1, 2.1, 3.1]})); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1614 %!test |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1615 %! A = arrayfun (@(x) mat2str(x), "a", "ErrorHandler", @__arrayfunerror); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1616 %! assert (isfield (A, "identifier"), true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1617 %! assert (isfield (A, "message"), true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1618 %! assert (isfield (A, "index"), true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1619 %! assert (isempty (A.message), false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1620 %! assert (A.index, 1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1621 %!test %% Overwriting setting of "UniformOutput" true |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1622 %! A = arrayfun (@(x) mat2str(x), "a", "UniformOutput", true, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1623 %! "ErrorHandler", @__arrayfunerror); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1624 %! assert (isfield (A, "identifier"), true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1625 %! assert (isfield (A, "message"), true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1626 %! assert (isfield (A, "index"), true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1627 %! assert (isempty (A.message), false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1628 %! assert (A.index, 1); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1629 |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1630 %% Input arguments can be of type cell array |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1631 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1632 %! A = arrayfun (@(x,y) x{1} < y{1}, {1.1, 4.2}, {3.1, 2}); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1633 %! assert (A, [true, false]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1634 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1635 %! A = arrayfun (@(x,y) x{1} < y{1}, {1.1; 4.2}, {3.1; 2}, "UniformOutput", true); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1636 %! assert (A, [true; false]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1637 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1638 %! A = arrayfun (@(x,y) x{1} < y{1}, {1.1, 4.2}, {3.1, 2}, "UniformOutput", false); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1639 %! assert (A, {true, false}); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1640 %!test |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1641 %! A = arrayfun (@(x,y) num2str(x,y), {1.1, 4.2}, {3.1, 2}, "ErrorHandler", @__arrayfunerror); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1642 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1643 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1644 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1645 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1646 %! assert ([A(1).index, A(2).index], [1, 2]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1647 %!test |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1648 %! A = arrayfun (@(x,y) num2str(x,y), {1.1, 4.2}, {3.1, 2}, \ |
14085
4e8f23ccadce
test: Use internal prefix "__" for %!functions to avoid polluting workspace
Rik <octave@nomad.inbox5.com>
parents:
14076
diff
changeset
|
1649 %! "UniformOutput", true, "ErrorHandler", @__arrayfunerror); |
13234
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1650 %! assert ([(isfield (A(1), "identifier")), (isfield (A(2), "identifier"))], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1651 %! assert ([(isfield (A(1), "message")), (isfield (A(2), "message"))], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1652 %! assert ([(isfield (A(1), "index")), (isfield (A(2), "index"))], [true, true]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1653 %! assert ([(isempty (A(1).message)), (isempty (A(2).message))], [false, false]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1654 %! assert ([A(1).index, A(2).index], [1, 2]); |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1655 */ |
e066d173aae6
improve compatibility of arrayfun for nargout = 0 case
John W. Eaton <jwe@octave.org>
parents:
13231
diff
changeset
|
1656 |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1657 static void |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1658 do_num2cell_helper (const dim_vector& dv, |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1659 const Array<int>& dimv, |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1660 dim_vector& celldv, dim_vector& arraydv, |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1661 Array<int>& perm) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1662 { |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1663 int dvl = dimv.length (); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1664 int maxd = dv.length (); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1665 celldv = dv; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1666 for (int i = 0; i < dvl; i++) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1667 maxd = std::max (maxd, dimv(i)); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1668 if (maxd > dv.length ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1669 celldv.resize (maxd, 1); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1670 arraydv = celldv; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1671 |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1672 OCTAVE_LOCAL_BUFFER_INIT (bool, sing, maxd, false); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1673 |
10350
12884915a8e4
merge MArray classes & improve Array interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10291
diff
changeset
|
1674 perm.clear (maxd, 1); |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1675 for (int i = 0; i < dvl; i++) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1676 { |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1677 int k = dimv(i) - 1; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1678 if (k < 0) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1679 { |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1680 error ("num2cell: dimension indices must be positive"); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1681 return; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1682 } |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1683 else if (i > 0 && k < dimv(i-1) - 1) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1684 { |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1685 error ("num2cell: dimension indices must be strictly increasing"); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1686 return; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1687 } |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1688 |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1689 sing[k] = true; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1690 perm(i) = k; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1691 } |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1692 |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1693 for (int k = 0, i = dvl; k < maxd; k++) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1694 if (! sing[k]) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1695 perm(i++) = k; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1696 |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1697 for (int i = 0; i < maxd; i++) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1698 if (sing[i]) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1699 celldv(i) = 1; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1700 else |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1701 arraydv(i) = 1; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1702 } |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1703 |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1704 template<class NDA> |
10756
d808eb829d48
optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1705 static inline typename NDA::element_type |
d808eb829d48
optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1706 do_num2cell_elem (const NDA& array, octave_idx_type i) |
d808eb829d48
optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1707 { return array(i); } |
d808eb829d48
optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1708 |
d808eb829d48
optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1709 static inline Cell |
d808eb829d48
optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1710 do_num2cell_elem (const Cell& array, octave_idx_type i) |
d808eb829d48
optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1711 { return Cell (array(i)); } |
d808eb829d48
optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1712 |
d808eb829d48
optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1713 |
d808eb829d48
optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1714 template<class NDA> |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1715 static Cell |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1716 do_num2cell (const NDA& array, const Array<int>& dimv) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1717 { |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1718 if (dimv.is_empty ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1719 { |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1720 Cell retval (array.dims ()); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1721 octave_idx_type nel = array.numel (); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1722 for (octave_idx_type i = 0; i < nel; i++) |
10756
d808eb829d48
optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1723 retval.xelem (i) = do_num2cell_elem (array, i); |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1724 |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1725 return retval; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1726 } |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1727 else |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1728 { |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1729 dim_vector celldv, arraydv; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1730 Array<int> perm; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1731 do_num2cell_helper (array.dims (), dimv, celldv, arraydv, perm); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1732 if (error_state) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1733 return Cell (); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1734 |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1735 NDA parray = array.permute (perm); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1736 |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1737 octave_idx_type nela = arraydv.numel (), nelc = celldv.numel (); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1738 parray = parray.reshape (dim_vector (nela, nelc)); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1739 |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1740 Cell retval (celldv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1741 for (octave_idx_type i = 0; i < nelc; i++) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1742 { |
9731 | 1743 retval.xelem (i) = NDA (parray.column (i).reshape (arraydv)); |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1744 } |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1745 |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1746 return retval; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1747 } |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1748 } |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1749 |
13695
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1750 // FIXME -- this is a mess, but if a size method for the object exists, |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1751 // we have to call it to get the size of the object instead of using the |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1752 // internal dims method. |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1753 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1754 static dim_vector |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1755 get_object_dims (octave_value& obj) |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1756 { |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1757 dim_vector retval; |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1758 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1759 Matrix m = obj.size (); |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1760 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1761 int n = m.numel (); |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1762 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1763 retval.resize (n); |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1764 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1765 for (int i = 0; i < n; i++) |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1766 retval(i) = m(i); |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1767 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1768 return retval; |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1769 } |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1770 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1771 static Cell |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1772 do_object2cell (const octave_value& obj, const Array<int>& dimv) |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1773 { |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1774 Cell retval; |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1775 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1776 // FIXME -- this copy is only needed because the octave_value::size |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1777 // method is not const. |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1778 octave_value array = obj; |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1779 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1780 if (dimv.is_empty ()) |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1781 { |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1782 dim_vector dv = get_object_dims (array); |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1783 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1784 if (! error_state) |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1785 { |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1786 retval.resize (dv); |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1787 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1788 octave_value_list idx (1); |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1789 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1790 for (octave_idx_type i = 0; i < dv.numel (); i++) |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1791 { |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1792 octave_quit (); |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1793 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1794 idx(0) = double (i+1); |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1795 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1796 retval.xelem(i) = array.single_subsref ("(", idx); |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1797 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1798 if (error_state) |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1799 break; |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1800 } |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1801 } |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1802 } |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1803 else |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1804 { |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1805 error ("num2cell (A, dim) not implemented for class objects"); |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1806 } |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1807 |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1808 return retval; |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1809 } |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1810 |
5451 | 1811 DEFUN_DLD (num2cell, args, , |
1812 "-*- texinfo -*-\n\ | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
1813 @deftypefn {Loadable Function} {@var{C} =} num2cell (@var{A})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
1814 @deftypefnx {Loadable Function} {@var{C} =} num2cell (@var{A}, @var{dim})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
1815 Convert the numeric matrix @var{A} to a cell array. If @var{dim} is\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
1816 defined, the value @var{C} is of dimension 1 in this dimension and the\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
1817 elements of @var{A} are placed into @var{C} in slices. For example:\n\ |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1818 \n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1819 @example\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1820 @group\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1821 num2cell([1,2;3,4])\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1822 @result{} ans =\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1823 @{\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1824 [1,1] = 1\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1825 [2,1] = 3\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1826 [1,2] = 2\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1827 [2,2] = 4\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1828 @}\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1829 num2cell([1,2;3,4],1)\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1830 @result{} ans =\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1831 @{\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1832 [1,1] =\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1833 1\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1834 3\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1835 [1,2] =\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1836 2\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1837 4\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1838 @}\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1839 @end group\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1840 @end example\n\ |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
1841 \n\ |
5642 | 1842 @seealso{mat2cell}\n\ |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1843 @end deftypefn") |
5451 | 1844 { |
1845 int nargin = args.length(); | |
1846 octave_value retval; | |
1847 | |
1848 if (nargin < 1 || nargin > 2) | |
5823 | 1849 print_usage (); |
5451 | 1850 else |
1851 { | |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1852 octave_value array = args(0); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1853 Array<int> dimv; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1854 if (nargin > 1) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1855 dimv = args (1).int_vector_value (true); |
5451 | 1856 |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1857 if (error_state) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1858 ; |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1859 else if (array.is_bool_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1860 retval = do_num2cell (array.bool_array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1861 else if (array.is_char_matrix ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1862 retval = do_num2cell (array.char_array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1863 else if (array.is_numeric_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1864 { |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1865 if (array.is_integer_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1866 { |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1867 if (array.is_int8_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1868 retval = do_num2cell (array.int8_array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1869 else if (array.is_int16_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1870 retval = do_num2cell (array.int16_array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1871 else if (array.is_int32_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1872 retval = do_num2cell (array.int32_array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1873 else if (array.is_int64_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1874 retval = do_num2cell (array.int64_array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1875 else if (array.is_uint8_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1876 retval = do_num2cell (array.uint8_array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1877 else if (array.is_uint16_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1878 retval = do_num2cell (array.uint16_array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1879 else if (array.is_uint32_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1880 retval = do_num2cell (array.uint32_array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1881 else if (array.is_uint64_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1882 retval = do_num2cell (array.uint64_array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1883 } |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1884 else if (array.is_complex_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1885 { |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1886 if (array.is_single_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1887 retval = do_num2cell (array.float_complex_array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1888 else |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1889 retval = do_num2cell (array.complex_array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1890 } |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1891 else |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1892 { |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1893 if (array.is_single_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1894 retval = do_num2cell (array.float_array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1895 else |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1896 retval = do_num2cell (array.array_value (), dimv); |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1897 } |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1898 } |
13695
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1899 else if (array.is_object ()) |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1900 retval = do_object2cell (array, dimv); |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1901 else if (array.is_map ()) |
348857854c52
correctly handle multidimensional objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13329
diff
changeset
|
1902 retval = do_num2cell (array.map_value (), dimv); |
10756
d808eb829d48
optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1903 else if (array.is_cell ()) |
d808eb829d48
optimize num2cell on structs
Jaroslav Hajek <highegg@gmail.com>
parents:
10711
diff
changeset
|
1904 retval = do_num2cell (array.cell_value (), dimv); |
13329
80f7564a3849
handle class objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13234
diff
changeset
|
1905 else if (array.is_object ()) |
80f7564a3849
handle class objects in num2cell
John W. Eaton <jwe@octave.org>
parents:
13234
diff
changeset
|
1906 retval = do_num2cell (array.cell_value (), dimv); |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1907 else |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
1908 gripe_wrong_type_arg ("num2cell", array); |
5451 | 1909 } |
1910 | |
1911 return retval; | |
1912 } | |
1913 | |
5988 | 1914 /* |
1915 | |
1916 %!assert(num2cell([1,2;3,4]),{1,2;3,4}) | |
1917 %!assert(num2cell([1,2;3,4],1),{[1;3],[2;4]}) | |
1918 %!assert(num2cell([1,2;3,4],2),{[1,2];[3,4]}) | |
1919 | |
1920 */ | |
1921 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1922 static bool |
10786
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1923 mat2cell_mismatch (const dim_vector& dv, |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1924 const Array<octave_idx_type> *d, int nd) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1925 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1926 for (int i = 0; i < nd; i++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1927 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1928 octave_idx_type s = 0; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1929 for (octave_idx_type j = 0; j < d[i].length (); j++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1930 s += d[i](j); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1931 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1932 octave_idx_type r = i < dv.length () ? dv(i) : 1; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1933 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1934 if (s != r) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1935 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1936 error ("mat2cell: mismatch on %d-th dimension (%d != %d)", |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1937 i+1, r, s); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1938 return true; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1939 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1940 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1941 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1942 return false; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1943 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1944 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1945 template<class container> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
1946 static void |
10786
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1947 prepare_idx (container *idx, int idim, int nd, |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1948 const Array<octave_idx_type>* d) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1949 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1950 octave_idx_type nidx = idim < nd ? d[idim].numel () : 1; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1951 if (nidx == 1) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1952 idx[0] = idx_vector::colon; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1953 else |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1954 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1955 octave_idx_type l = 0; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1956 for (octave_idx_type i = 0; i < nidx; i++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1957 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1958 octave_idx_type u = l + d[idim](i); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1959 idx[i] = idx_vector (l, u); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1960 l = u; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1961 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1962 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1963 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1964 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1965 // 2D specialization, works for Array, Sparse and octave_map. |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1966 // Uses 1D or 2D indexing. |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1967 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1968 template <class Array2D> |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1969 static Cell |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1970 do_mat2cell_2d (const Array2D& a, const Array<octave_idx_type> *d, int nd) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1971 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1972 NoAlias<Cell> retval; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1973 assert (nd == 1 || nd == 2); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1974 assert (a.ndims () == 2); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1975 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1976 if (mat2cell_mismatch (a.dims (), d, nd)) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1977 return retval; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1978 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1979 octave_idx_type nridx = d[0].length (); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1980 octave_idx_type ncidx = nd == 1 ? 1 : d[1].length (); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1981 retval.clear (nridx, ncidx); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1982 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1983 int ivec = -1; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1984 if (a.rows () > 1 && a.cols () == 1 && ncidx == 1) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1985 ivec = 0; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1986 else if (a.rows () == 1 && nridx == 1 && nd == 2) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1987 ivec = 1; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1988 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1989 if (ivec >= 0) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1990 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1991 // Vector split. Use 1D indexing. |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1992 octave_idx_type l = 0, nidx = (ivec == 0 ? nridx : ncidx); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1993 for (octave_idx_type i = 0; i < nidx; i++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1994 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1995 octave_idx_type u = l + d[ivec](i); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1996 retval(i) = a.index (idx_vector (l, u)); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1997 l = u; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1998 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
1999 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2000 else |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2001 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2002 // General 2D case. Use 2D indexing. |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2003 OCTAVE_LOCAL_BUFFER (idx_vector, ridx, nridx); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2004 prepare_idx (ridx, 0, nd, d); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2005 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2006 OCTAVE_LOCAL_BUFFER (idx_vector, cidx, ncidx); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2007 prepare_idx (cidx, 1, nd, d); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2008 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2009 for (octave_idx_type j = 0; j < ncidx; j++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2010 for (octave_idx_type i = 0; i < nridx; i++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2011 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2012 octave_quit (); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2013 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2014 retval(i,j) = a.index (ridx[i], cidx[j]); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2015 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2016 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2017 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2018 return retval; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2019 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2020 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2021 // Nd case. Works for Arrays and octave_map. |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2022 // Uses Nd indexing. |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2023 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2024 template <class ArrayND> |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2025 Cell |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2026 do_mat2cell_nd (const ArrayND& a, const Array<octave_idx_type> *d, int nd) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2027 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2028 NoAlias<Cell> retval; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2029 assert (nd >= 1); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2030 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2031 if (mat2cell_mismatch (a.dims (), d, nd)) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2032 return retval; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2033 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2034 dim_vector rdv = dim_vector::alloc (nd); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2035 OCTAVE_LOCAL_BUFFER (octave_idx_type, nidx, nd); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2036 octave_idx_type idxtot = 0; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2037 for (int i = 0; i < nd; i++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2038 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2039 rdv(i) = nidx[i] = d[i].length (); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2040 idxtot += nidx[i]; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2041 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2042 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2043 retval.clear (rdv); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2044 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2045 OCTAVE_LOCAL_BUFFER (idx_vector, xidx, idxtot); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2046 OCTAVE_LOCAL_BUFFER (idx_vector *, idx, nd); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2047 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2048 idxtot = 0; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2049 for (int i = 0; i < nd; i++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2050 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2051 idx[i] = xidx + idxtot; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2052 prepare_idx (idx[i], i, nd, d); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2053 idxtot += nidx[i]; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2054 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2055 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2056 OCTAVE_LOCAL_BUFFER_INIT (octave_idx_type, ridx, nd, 0); |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
2057 NoAlias< Array<idx_vector> > ra_idx |
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
2058 (dim_vector (1, std::max (nd, a.ndims ())), idx_vector::colon); |
10786
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2059 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2060 for (octave_idx_type j = 0; j < retval.numel (); j++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2061 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2062 octave_quit (); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2063 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2064 for (int i = 0; i < nd; i++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2065 ra_idx(i) = idx[i][ridx[i]]; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2066 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2067 retval(j) = a.index (ra_idx); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2068 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2069 rdv.increment_index (ridx); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2070 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2071 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2072 return retval; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2073 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2074 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2075 // Dispatcher. |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2076 template <class ArrayND> |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2077 Cell |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2078 do_mat2cell (const ArrayND& a, const Array<octave_idx_type> *d, int nd) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2079 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2080 if (a.ndims () == 2 && nd <= 2) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2081 return do_mat2cell_2d (a, d, nd); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2082 else |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2083 return do_mat2cell_nd (a, d, nd); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2084 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2085 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2086 // General case. Works for any class supporting do_index_op. |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2087 // Uses Nd indexing. |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2088 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2089 Cell |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2090 do_mat2cell (octave_value& a, const Array<octave_idx_type> *d, int nd) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2091 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2092 NoAlias<Cell> retval; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2093 assert (nd >= 1); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2094 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2095 if (mat2cell_mismatch (a.dims (), d, nd)) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2096 return retval; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2097 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2098 dim_vector rdv = dim_vector::alloc (nd); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2099 OCTAVE_LOCAL_BUFFER (octave_idx_type, nidx, nd); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2100 octave_idx_type idxtot = 0; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2101 for (int i = 0; i < nd; i++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2102 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2103 rdv(i) = nidx[i] = d[i].length (); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2104 idxtot += nidx[i]; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2105 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2106 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2107 retval.clear (rdv); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2108 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2109 OCTAVE_LOCAL_BUFFER (octave_value, xidx, idxtot); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2110 OCTAVE_LOCAL_BUFFER (octave_value *, idx, nd); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2111 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2112 idxtot = 0; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2113 for (int i = 0; i < nd; i++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2114 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2115 idx[i] = xidx + idxtot; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2116 prepare_idx (idx[i], i, nd, d); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2117 idxtot += nidx[i]; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2118 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2119 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2120 OCTAVE_LOCAL_BUFFER_INIT (octave_idx_type, ridx, nd, 0); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
2121 octave_value_list ra_idx (std::max (nd, a.ndims ()), |
10786
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2122 octave_value::magic_colon_t); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2123 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2124 for (octave_idx_type j = 0; j < retval.numel (); j++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2125 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2126 octave_quit (); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2127 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2128 for (int i = 0; i < nd; i++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2129 ra_idx(i) = idx[i][ridx[i]]; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2130 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2131 retval(j) = a.do_index_op (ra_idx); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2132 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2133 if (error_state) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2134 break; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2135 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2136 rdv.increment_index (ridx); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2137 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2138 |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2139 return retval; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2140 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2141 |
5784 | 2142 DEFUN_DLD (mat2cell, args, , |
2143 "-*- texinfo -*-\n\ | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2144 @deftypefn {Loadable Function} {@var{C} =} mat2cell (@var{A}, @var{m}, @var{n})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2145 @deftypefnx {Loadable Function} {@var{C} =} mat2cell (@var{A}, @var{d1}, @var{d2}, @dots{})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2146 @deftypefnx {Loadable Function} {@var{C} =} mat2cell (@var{A}, @var{r})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2147 Convert the matrix @var{A} to a cell array. If @var{A} is 2-D, then\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2148 it is required that @code{sum (@var{m}) == size (@var{A}, 1)} and\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2149 @code{sum (@var{n}) == size (@var{A}, 2)}. Similarly, if @var{A} is\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2150 multi-dimensional and the number of dimensional arguments is equal\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2151 to the dimensions of @var{A}, then it is required that @code{sum (@var{di})\n\ |
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2152 == size (@var{A}, i)}.\n\ |
5784 | 2153 \n\ |
2154 Given a single dimensional argument @var{r}, the other dimensional\n\ | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2155 arguments are assumed to equal @code{size (@var{A},@var{i})}.\n\ |
5784 | 2156 \n\ |
2157 An example of the use of mat2cell is\n\ | |
2158 \n\ | |
2159 @example\n\ | |
2160 mat2cell (reshape(1:16,4,4),[3,1],[3,1])\n\ | |
2161 @result{} @{\n\ | |
2162 [1,1] =\n\ | |
2163 \n\ | |
2164 1 5 9\n\ | |
2165 2 6 10\n\ | |
2166 3 7 11\n\ | |
2167 \n\ | |
2168 [2,1] =\n\ | |
2169 \n\ | |
2170 4 8 12\n\ | |
2171 \n\ | |
2172 [1,2] =\n\ | |
2173 \n\ | |
2174 13\n\ | |
2175 14\n\ | |
2176 15\n\ | |
2177 \n\ | |
2178 [2,2] = 16\n\ | |
2179 @}\n\ | |
2180 @end example\n\ | |
5829 | 2181 @seealso{num2cell, cell2mat}\n\ |
5784 | 2182 @end deftypefn") |
2183 { | |
2184 int nargin = args.length(); | |
2185 octave_value retval; | |
2186 | |
2187 if (nargin < 2) | |
7038 | 2188 print_usage (); |
5784 | 2189 else |
2190 { | |
10786
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2191 // Prepare indices. |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2192 OCTAVE_LOCAL_BUFFER (Array<octave_idx_type>, d, nargin-1); |
5784 | 2193 |
10786
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2194 for (int i = 1; i < nargin; i++) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2195 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2196 d[i-1] = args(i).octave_idx_type_vector_value (true); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2197 if (error_state) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2198 return retval; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2199 } |
5784 | 2200 |
10786
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2201 octave_value a = args(0); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2202 bool sparse = a.is_sparse_type (); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2203 if (sparse && nargin > 3) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2204 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2205 error ("mat2cell: sparse arguments only support 2D indexing"); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2206 return retval; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2207 } |
5784 | 2208 |
10786
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2209 switch (a.builtin_type ()) |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10125
diff
changeset
|
2210 { |
10786
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2211 case btyp_double: |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2212 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2213 if (sparse) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2214 retval = do_mat2cell_2d (a.sparse_matrix_value (), d, nargin-1); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2215 else |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2216 retval = do_mat2cell (a.array_value (), d, nargin - 1); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2217 break; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2218 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2219 case btyp_complex: |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2220 { |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2221 if (sparse) |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2222 retval = do_mat2cell_2d (a.sparse_complex_matrix_value (), d, nargin-1); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2223 else |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2224 retval = do_mat2cell (a.complex_array_value (), d, nargin - 1); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2225 break; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2226 } |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2227 #define BTYP_BRANCH(X,Y) \ |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2228 case btyp_ ## X: \ |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2229 retval = do_mat2cell (a.Y ## _value (), d, nargin - 1); \ |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2230 break |
5784 | 2231 |
10786
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2232 BTYP_BRANCH (float, float_array); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2233 BTYP_BRANCH (float_complex, float_complex_array); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2234 BTYP_BRANCH (bool, bool_array); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2235 BTYP_BRANCH (char, char_array); |
5784 | 2236 |
10786
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2237 BTYP_BRANCH (int8, int8_array); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2238 BTYP_BRANCH (int16, int16_array); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2239 BTYP_BRANCH (int32, int32_array); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2240 BTYP_BRANCH (int64, int64_array); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2241 BTYP_BRANCH (uint8, uint8_array); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2242 BTYP_BRANCH (uint16, uint16_array); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2243 BTYP_BRANCH (uint32, uint32_array); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2244 BTYP_BRANCH (uint64, uint64_array); |
5784 | 2245 |
10786
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2246 BTYP_BRANCH (cell, cell); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2247 BTYP_BRANCH (struct, map); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2248 #undef BTYP_BRANCH |
5784 | 2249 |
10786
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2250 case btyp_func_handle: |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2251 gripe_wrong_type_arg ("mat2cell", a); |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2252 break; |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2253 default: |
146a97c3bc97
some optimizations for mat2cell
Jaroslav Hajek <highegg@gmail.com>
parents:
10756
diff
changeset
|
2254 retval = do_mat2cell (a, d, nargin-1); |
10154
40dfc0c99116
DLD-FUNCTIONS/*.cc: untabify
John W. Eaton <jwe@octave.org>
parents:
10125
diff
changeset
|
2255 } |
5784 | 2256 } |
2257 | |
2258 return retval; | |
2259 } | |
2260 | |
2261 /* | |
2262 | |
2263 %!test | |
2264 %! x = reshape(1:20,5,4); | |
2265 %! c = mat2cell(x,[3,2],[3,1]); | |
2266 %! assert(c,{[1,6,11;2,7,12;3,8,13],[16;17;18];[4,9,14;5,10,15],[19;20]}) | |
2267 | |
2268 %!test | |
2269 %! x = 'abcdefghij'; | |
2270 %! c = mat2cell(x,1,[0,4,2,0,4,0]); | |
2271 %! empty1by0str = resize('',1,0); | |
2272 %! assert(c,{empty1by0str,'abcd','ef',empty1by0str,'ghij',empty1by0str}) | |
2273 | |
2274 */ | |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2275 |
9740
78ac37d73557
fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2276 // FIXME: it would be nice to allow ranges being handled without a conversion. |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2277 template <class NDA> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
2278 static Cell |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
2279 do_cellslices_nda (const NDA& array, |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
2280 const Array<octave_idx_type>& lb, |
10125 | 2281 const Array<octave_idx_type>& ub, |
2282 int dim = -1) | |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2283 { |
9740
78ac37d73557
fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2284 octave_idx_type n = lb.length (); |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2285 Cell retval (1, n); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
2286 if (array.is_vector () && (dim == -1 |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
2287 || (dim == 0 && array.columns () == 1) |
10125 | 2288 || (dim == 1 && array.rows () == 1))) |
9057
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
2289 { |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
2290 for (octave_idx_type i = 0; i < n && ! error_state; i++) |
9740
78ac37d73557
fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2291 retval(i) = array.index (idx_vector (lb(i) - 1, ub(i))); |
9057
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
2292 } |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
2293 else |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
2294 { |
10125 | 2295 const dim_vector dv = array.dims (); |
2296 int ndims = dv.length (); | |
2297 if (dim < 0) | |
2298 dim = dv.first_non_singleton (); | |
2299 ndims = std::max (ndims, dim + 1); | |
2300 | |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11553
diff
changeset
|
2301 Array<idx_vector> idx (dim_vector (ndims, 1), idx_vector::colon); |
10125 | 2302 |
9057
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
2303 for (octave_idx_type i = 0; i < n && ! error_state; i++) |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
2304 { |
10125 | 2305 idx(dim) = idx_vector (lb(i) - 1, ub(i)); |
2306 retval(i) = array.index (idx); | |
9057
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
2307 } |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
2308 } |
8b263623d0f3
have cellslices index the last dim of n-d arrays
Jaroslav Hajek <highegg@gmail.com>
parents:
9042
diff
changeset
|
2309 |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2310 return retval; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2311 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2312 |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2313 DEFUN_DLD (cellslices, args, , |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2314 "-*- texinfo -*-\n\ |
10125 | 2315 @deftypefn {Loadable Function} {@var{sl} =} cellslices (@var{x}, @var{lb}, @var{ub}, @var{dim})\n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
2316 Given an array @var{x}, this function produces a cell array of slices from\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
2317 the array determined by the index vectors @var{lb}, @var{ub}, for lower and\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
2318 upper bounds, respectively. In other words, it is equivalent to the\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
2319 following code:\n\ |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2320 \n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2321 @example\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9057
diff
changeset
|
2322 @group\n\ |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2323 n = length (lb);\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2324 sl = cell (1, n);\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2325 for i = 1:length (lb)\n\ |
10125 | 2326 sl@{i@} = x(:,@dots{},lb(i):ub(i),@dots{},:);\n\ |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2327 endfor\n\ |
9064
7c02ec148a3c
Check grammar on all .cc files
Rik <rdrider0-list@yahoo.com>
parents:
9057
diff
changeset
|
2328 @end group\n\ |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2329 @end example\n\ |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2330 \n\ |
10846
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
2331 The position of the index is determined by @var{dim}. If not specified,\n\ |
a4f482e66b65
Grammarcheck more of the documentation.
Rik <octave@nomad.inbox5.com>
parents:
10840
diff
changeset
|
2332 slicing is done along the first non-singleton dimension.\n\ |
13928
2892fd834446
doc: Update seealso references for cellslices and cellindexmat
Rik <octave@nomad.inbox5.com>
parents:
13695
diff
changeset
|
2333 @seealso{cell2mat, cellindexmat, cellfun}\n\ |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2334 @end deftypefn") |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2335 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2336 octave_value retval; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2337 int nargin = args.length (); |
10125 | 2338 if (nargin == 3 || nargin == 4) |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2339 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2340 octave_value x = args(0); |
9740
78ac37d73557
fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2341 Array<octave_idx_type> lb = args(1).octave_idx_type_vector_value (); |
78ac37d73557
fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2342 Array<octave_idx_type> ub = args(2).octave_idx_type_vector_value (); |
10125 | 2343 int dim = -1; |
2344 if (nargin == 4) | |
2345 { | |
2346 dim = args(3).int_value () - 1; | |
2347 if (dim < 0) | |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2348 error ("cellslices: DIM must be a valid dimension"); |
10125 | 2349 } |
2350 | |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2351 if (! error_state) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2352 { |
9740
78ac37d73557
fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2353 if (lb.length () != ub.length ()) |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2354 error ("cellslices: the lengths of LB and UB must match"); |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2355 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2356 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2357 Cell retcell; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2358 if (! x.is_sparse_type () && x.is_matrix_type ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2359 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2360 // specialize for some dense arrays. |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2361 if (x.is_bool_type ()) |
10125 | 2362 retcell = do_cellslices_nda (x.bool_array_value (), lb, ub, dim); |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2363 else if (x.is_char_matrix ()) |
10125 | 2364 retcell = do_cellslices_nda (x.char_array_value (), lb, ub, dim); |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
2365 else if (x.is_integer_type ()) |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
2366 { |
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
2367 if (x.is_int8_type ()) |
10125 | 2368 retcell = do_cellslices_nda (x.int8_array_value (), lb, ub, dim); |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
2369 else if (x.is_int16_type ()) |
10125 | 2370 retcell = do_cellslices_nda (x.int16_array_value (), lb, ub, dim); |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
2371 else if (x.is_int32_type ()) |
10125 | 2372 retcell = do_cellslices_nda (x.int32_array_value (), lb, ub, dim); |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
2373 else if (x.is_int64_type ()) |
10125 | 2374 retcell = do_cellslices_nda (x.int64_array_value (), lb, ub, dim); |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
2375 else if (x.is_uint8_type ()) |
10125 | 2376 retcell = do_cellslices_nda (x.uint8_array_value (), lb, ub, dim); |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
2377 else if (x.is_uint16_type ()) |
10125 | 2378 retcell = do_cellslices_nda (x.uint16_array_value (), lb, ub, dim); |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
2379 else if (x.is_uint32_type ()) |
10125 | 2380 retcell = do_cellslices_nda (x.uint32_array_value (), lb, ub, dim); |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
2381 else if (x.is_uint64_type ()) |
10125 | 2382 retcell = do_cellslices_nda (x.uint64_array_value (), lb, ub, dim); |
9678
c929f09457b7
rewrite num2cell for speed-up + a few associated fixes
Jaroslav Hajek <highegg@gmail.com>
parents:
9622
diff
changeset
|
2383 } |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2384 else if (x.is_complex_type ()) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2385 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2386 if (x.is_single_type ()) |
10125 | 2387 retcell = do_cellslices_nda (x.float_complex_array_value (), lb, ub, dim); |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2388 else |
10125 | 2389 retcell = do_cellslices_nda (x.complex_array_value (), lb, ub, dim); |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2390 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2391 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2392 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2393 if (x.is_single_type ()) |
10125 | 2394 retcell = do_cellslices_nda (x.float_array_value (), lb, ub, dim); |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2395 else |
10125 | 2396 retcell = do_cellslices_nda (x.array_value (), lb, ub, dim); |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2397 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2398 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2399 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2400 { |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2401 // generic code. |
9740
78ac37d73557
fix cellslices to handle empty ranges with non-positive bounds
Jaroslav Hajek <highegg@gmail.com>
parents:
9731
diff
changeset
|
2402 octave_idx_type n = lb.length (); |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2403 retcell = Cell (1, n); |
10125 | 2404 const dim_vector dv = x.dims (); |
2405 int ndims = dv.length (); | |
2406 if (dim < 0) | |
2407 dim = dv.first_non_singleton (); | |
2408 ndims = std::max (ndims, dim + 1); | |
2409 octave_value_list idx (ndims, octave_value::magic_colon_t); | |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2410 for (octave_idx_type i = 0; i < n && ! error_state; i++) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2411 { |
10125 | 2412 idx(dim) = Range (lb(i), ub(i)); |
8985
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2413 retcell(i) = x.do_index_op (idx); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2414 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2415 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2416 if (! error_state) |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2417 retval = retcell; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2418 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2419 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2420 } |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2421 else |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2422 print_usage (); |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2423 |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2424 return retval; |
193804a4f82f
add cellslices function
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
2425 } |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
2426 |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
2427 /* |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
2428 %!test |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
2429 %! m = [1, 2, 3, 4; 5, 6, 7, 8; 9, 10, 11, 12]; |
10125 | 2430 %! c = cellslices (m, [1, 2], [2, 3], 2); |
10122
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
2431 %! assert (c, {[1, 2; 5, 6; 9, 10], [2, 3; 6, 7; 10, 11]}); |
9d1a14e12431
Update docs and add tests for container functions
Thorsten Meyer <thorsten.meyier@gmx.de>
parents:
10075
diff
changeset
|
2432 */ |
10963
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2433 |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2434 DEFUN_DLD (cellindexmat, args, , |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2435 "-*- texinfo -*-\n\ |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2436 @deftypefn {Loadable Function} {@var{y} =} cellindexmat (@var{x}, @var{varargin})\n\ |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2437 Given a cell array of matrices @var{x}, this function computes\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2438 \n\ |
10963
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2439 @example\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2440 @group\n\ |
10963
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2441 Y = cell (size (X));\n\ |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2442 for i = 1:numel (X)\n\ |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2443 Y@{i@} = X@{i@}(varargin@{:@});\n\ |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2444 endfor\n\ |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2445 @end group\n\ |
10963
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2446 @end example\n\ |
13928
2892fd834446
doc: Update seealso references for cellslices and cellindexmat
Rik <octave@nomad.inbox5.com>
parents:
13695
diff
changeset
|
2447 @seealso{cellslices, cellfun}\n\ |
10963
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2448 @end deftypefn") |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2449 { |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2450 octave_value retval; |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2451 if (args.length () >= 1) |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2452 { |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2453 if (args(0).is_cell ()) |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2454 { |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2455 const Cell x = args(0).cell_value (); |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2456 NoAlias<Cell> y(x.dims ()); |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2457 octave_idx_type nel = x.numel (); |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2458 octave_value_list idx = args.slice (1, args.length () - 1); |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2459 |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2460 for (octave_idx_type i = 0; i < nel; i++) |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2461 { |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2462 octave_quit (); |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2463 octave_value tmp = x(i); |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2464 y(i) = tmp.do_index_op (idx); |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2465 if (error_state) |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2466 break; |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2467 } |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2468 |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2469 retval = y; |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2470 } |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2471 else |
11553
01f703952eff
Improve docstrings for functions in DLD-FUNCTIONS directory.
Rik <octave@nomad.inbox5.com>
parents:
11523
diff
changeset
|
2472 error ("cellindexmat: X must be a cell"); |
10963
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2473 } |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2474 else |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2475 print_usage (); |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2476 |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2477 return retval; |
0d7624852beb
implement cellindexmat function
Jaroslav Hajek <highegg@gmail.com>
parents:
10846
diff
changeset
|
2478 } |