6869
|
1 /* |
|
2 |
|
3 Copyright (C) 2007 David Bateman |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <string> |
|
29 #include <vector> |
|
30 #include <list> |
|
31 |
|
32 #include "lo-mappers.h" |
|
33 |
|
34 #include "oct-map.h" |
|
35 #include "defun-dld.h" |
|
36 #include "parse.h" |
|
37 #include "variables.h" |
|
38 #include "ov-colon.h" |
|
39 #include "unwind-prot.h" |
|
40 |
|
41 static bool |
|
42 maybe_update_column (octave_value& Ac, const octave_value& A, |
|
43 const dim_vector& dva, const dim_vector& dvc, |
|
44 octave_idx_type i, octave_value_list &idx) |
|
45 { |
|
46 octave_idx_type nd = dva.length (); |
|
47 |
|
48 if (i == 0) |
|
49 { |
|
50 idx(0) = octave_value (':'); |
|
51 for (octave_idx_type j = 1; j < nd; j++) |
|
52 { |
|
53 if (dva (j) == 1) |
|
54 idx (j) = octave_value (1); |
|
55 else |
|
56 idx (j) = octave_value ((i % dvc(j)) + 1); |
|
57 |
|
58 i = i / dvc (j); |
|
59 } |
|
60 |
|
61 Ac = A; |
|
62 Ac = Ac.single_subsref ("(", idx); |
|
63 return true; |
|
64 } |
|
65 else |
|
66 { |
|
67 bool is_changed = false; |
|
68 octave_idx_type k = i; |
|
69 octave_idx_type k1 = i - 1; |
|
70 for (octave_idx_type j = 1; j < nd; j++) |
|
71 { |
|
72 if (dva(j) != 1 && k % dvc (j) != k1 % dvc (j)) |
|
73 { |
|
74 idx (j) = octave_value ((k % dvc(j)) + 1); |
|
75 is_changed = true; |
|
76 } |
|
77 |
|
78 k = k / dvc (j); |
|
79 k1 = k1 / dvc (j); |
|
80 } |
|
81 |
|
82 if (is_changed) |
|
83 { |
|
84 Ac = A; |
|
85 Ac = Ac.single_subsref ("(", idx); |
|
86 return true; |
|
87 } |
|
88 else |
|
89 return false; |
|
90 } |
|
91 } |
|
92 |
6959
|
93 #if 0 |
|
94 // FIXME -- this function is not used; is it OK to delete it? |
6869
|
95 static void |
|
96 update_index (octave_value_list& idx, const dim_vector& dv, octave_idx_type i) |
|
97 { |
|
98 octave_idx_type nd = dv.length (); |
|
99 |
|
100 if (i == 0) |
|
101 { |
|
102 for (octave_idx_type j = nd - 1; j > 0; j--) |
|
103 idx(j) = octave_value (static_cast<double>(1)); |
|
104 idx(0) = octave_value (':'); |
|
105 } |
|
106 else |
|
107 { |
|
108 for (octave_idx_type j = 1; j < nd; j++) |
|
109 { |
|
110 idx (j) = octave_value (i % dv (j) + 1); |
|
111 i = i / dv (j); |
|
112 } |
|
113 } |
|
114 } |
6959
|
115 #endif |
6869
|
116 |
|
117 static void |
|
118 update_index (Array<int>& idx, const dim_vector& dv, octave_idx_type i) |
|
119 { |
|
120 octave_idx_type nd = dv.length (); |
|
121 |
|
122 idx(0) = 0; |
|
123 for (octave_idx_type j = 1; j < nd; j++) |
|
124 { |
|
125 idx (j) = i % dv (j); |
|
126 i = i / dv (j); |
|
127 } |
|
128 } |
|
129 |
6959
|
130 DEFUN_DLD (bsxfun, args, , |
6869
|
131 " -*- texinfo -*-\n\ |
|
132 @deftypefn {Lodable Function} {} bsxfun (@var{f}, @var{a}, @var{b})\n\ |
|
133 Applies a binary function @var{f} element-wise to two matrix arguments\n\ |
|
134 @var{a} and @var{b}. The function @var{f} must be capable of accepting\n\ |
|
135 two column vector arguments of equal length, or one column vector\n\ |
|
136 argument and a scalar.\n\ |
|
137 \n\ |
|
138 The dimensions of @var{a} and @var{b} must be equal or singleton. The\n\ |
6881
|
139 singleton dimensions of the matirces will be expanded to the same\n\ |
|
140 dimensionality as the other matrix.\n\ |
6869
|
141 \n\ |
|
142 @seealso{arrayfun, cellfun}\n\ |
|
143 @end deftypefn") |
|
144 { |
|
145 int nargin = args.length (); |
|
146 octave_value_list retval; |
|
147 |
|
148 if (nargin != 3) |
|
149 print_usage (); |
|
150 else |
|
151 { |
|
152 octave_function *func = 0; |
|
153 std::string name; |
|
154 std::string fcn_name; |
|
155 |
|
156 if (args(0).is_function_handle () || args(0).is_inline_function ()) |
|
157 func = args(0).function_value (); |
|
158 else if (args(0).is_string ()) |
|
159 { |
|
160 name = args(0).string_value (); |
|
161 fcn_name = unique_symbol_name ("__bsxfun_fcn_"); |
|
162 std::string fname = "function y = "; |
|
163 fname.append (fcn_name); |
|
164 fname.append ("(x) y = "); |
|
165 func = extract_function (args(0), "bsxfun", fcn_name, fname, |
|
166 "; endfunction"); |
|
167 } |
|
168 else |
|
169 error ("bsxfun: first argument must be a string or function handle"); |
|
170 |
|
171 if (! error_state) |
|
172 { |
|
173 const octave_value A = args (1); |
|
174 dim_vector dva = A.dims (); |
|
175 octave_idx_type nda = dva.length (); |
|
176 const octave_value B = args (2); |
|
177 dim_vector dvb = B.dims (); |
|
178 octave_idx_type ndb = dvb.length (); |
|
179 octave_idx_type nd = nda; |
|
180 |
|
181 if (nda > ndb) |
|
182 dvb.resize (nda, 1); |
|
183 else if (nda < ndb) |
|
184 { |
|
185 dva.resize (ndb, 1); |
|
186 nd = ndb; |
|
187 } |
|
188 |
|
189 for (octave_idx_type i = 0; i < nd; i++) |
|
190 if (dva (i) != dvb (i) && dva (i) != 1 && dvb (i) != 1) |
|
191 { |
|
192 error ("bsxfun: dimensions don't match"); |
|
193 break; |
|
194 } |
|
195 |
|
196 if (!error_state) |
|
197 { |
|
198 // Find the size of the output |
|
199 dim_vector dvc; |
|
200 dvc.resize (nd); |
|
201 |
|
202 for (octave_idx_type i = 0; i < nd; i++) |
|
203 dvc (i) = (dva (i) < 1 ? dva (i) : (dvb (i) < 1 ? dvb (i) : |
|
204 (dva (i) > dvb (i) ? dva (i) : dvb (i)))); |
|
205 |
|
206 if (dva == dvb || dva.numel () == 1 || dvb.numel () == 1) |
|
207 { |
|
208 octave_value_list inputs; |
|
209 inputs (0) = A; |
|
210 inputs (1) = B; |
|
211 retval = feval (func, inputs, 1); |
|
212 } |
|
213 else if (dvc.numel () < 1) |
|
214 { |
|
215 octave_value_list inputs; |
|
216 inputs (0) = A.resize (dvc); |
|
217 inputs (1) = B.resize (dvc); |
|
218 retval = feval (func, inputs, 1); |
|
219 } |
|
220 else |
|
221 { |
|
222 octave_idx_type ncount = 1; |
|
223 for (octave_idx_type i = 1; i < nd; i++) |
|
224 ncount *= dvc (i); |
|
225 |
|
226 #define BSXDEF(T) \ |
|
227 T result_ ## T; \ |
|
228 bool have_ ## T = false; |
|
229 |
|
230 BSXDEF(NDArray); |
|
231 BSXDEF(ComplexNDArray); |
|
232 BSXDEF(boolNDArray); |
|
233 BSXDEF(int8NDArray); |
|
234 BSXDEF(int16NDArray); |
|
235 BSXDEF(int32NDArray); |
|
236 BSXDEF(int64NDArray); |
|
237 BSXDEF(uint8NDArray); |
|
238 BSXDEF(uint16NDArray); |
|
239 BSXDEF(uint32NDArray); |
|
240 BSXDEF(uint64NDArray); |
|
241 |
|
242 octave_value Ac ; |
|
243 octave_value_list idxA; |
|
244 octave_value Bc; |
|
245 octave_value_list idxB; |
|
246 octave_value C; |
|
247 octave_value_list inputs; |
|
248 Array<int> ra_idx (dvc.length(), 0); |
|
249 |
|
250 |
|
251 for (octave_idx_type i = 0; i < ncount; i++) |
|
252 { |
|
253 if (maybe_update_column (Ac, A, dva, dvc, i, idxA)) |
|
254 inputs (0) = Ac; |
|
255 |
|
256 if (maybe_update_column (Bc, B, dvb, dvc, i, idxB)) |
|
257 inputs (1) = Bc; |
|
258 |
|
259 octave_value_list tmp = feval (func, inputs, 1); |
|
260 |
|
261 if (error_state) |
|
262 break; |
|
263 |
|
264 #define BSXINIT(T, CLS, EXTRACTOR) \ |
|
265 (result_type == CLS) \ |
|
266 { \ |
|
267 have_ ## T = true; \ |
|
268 result_ ## T = \ |
|
269 tmp (0). EXTRACTOR ## _array_value (); \ |
|
270 result_ ## T .resize (dvc); \ |
|
271 } |
|
272 |
|
273 if (i == 0) |
|
274 { |
|
275 if (! tmp(0).is_sparse_type ()) |
|
276 { |
|
277 std::string result_type = tmp(0).class_name (); |
|
278 if (result_type == "double") |
|
279 { |
|
280 if (tmp(0).is_real_type ()) |
|
281 { |
|
282 have_NDArray = true; |
|
283 result_NDArray = tmp(0).array_value (); |
|
284 result_NDArray.resize (dvc); |
|
285 } |
|
286 else |
|
287 { |
|
288 have_ComplexNDArray = true; |
|
289 result_ComplexNDArray = |
|
290 tmp(0).complex_array_value (); |
|
291 result_ComplexNDArray.resize (dvc); |
|
292 } |
|
293 } |
|
294 else if BSXINIT(boolNDArray, "logical", bool) |
|
295 else if BSXINIT(int8NDArray, "int8", int8) |
|
296 else if BSXINIT(int16NDArray, "int16", int16) |
|
297 else if BSXINIT(int32NDArray, "int32", int32) |
|
298 else if BSXINIT(int64NDArray, "int64", int64) |
|
299 else if BSXINIT(uint8NDArray, "uint8", uint8) |
|
300 else if BSXINIT(uint16NDArray, "uint16", uint16) |
|
301 else if BSXINIT(uint32NDArray, "uint32", uint32) |
|
302 else if BSXINIT(uint64NDArray, "uint64", uint64) |
|
303 else |
|
304 { |
|
305 C = tmp (0); |
|
306 C = C.resize (dvc); |
|
307 } |
|
308 } |
|
309 } |
|
310 else |
|
311 { |
|
312 update_index (ra_idx, dvc, i); |
|
313 |
|
314 if (have_NDArray) |
|
315 { |
|
316 if (tmp(0).class_name () != "double") |
|
317 { |
|
318 have_NDArray = false; |
|
319 C = result_NDArray; |
|
320 C = do_cat_op (C, tmp(0), ra_idx); |
|
321 } |
|
322 else if (tmp(0).is_real_type ()) |
|
323 result_NDArray.insert (tmp(0).array_value(), |
|
324 ra_idx); |
|
325 else |
|
326 { |
|
327 result_ComplexNDArray = |
|
328 ComplexNDArray (result_NDArray); |
|
329 result_ComplexNDArray.insert |
|
330 (tmp(0).complex_array_value(), ra_idx); |
|
331 have_NDArray = false; |
|
332 have_ComplexNDArray = true; |
|
333 } |
|
334 } |
|
335 |
|
336 #define BSXLOOP(T, CLS, EXTRACTOR) \ |
|
337 (have_ ## T) \ |
|
338 { \ |
|
339 if (tmp (0).class_name () != CLS) \ |
|
340 { \ |
|
341 have_ ## T = false; \ |
|
342 C = result_ ## T; \ |
|
343 C = do_cat_op (C, tmp (0), ra_idx); \ |
|
344 } \ |
|
345 else \ |
|
346 result_ ## T .insert \ |
|
347 (tmp(0). EXTRACTOR ## _array_value (), \ |
|
348 ra_idx); \ |
|
349 } |
|
350 |
|
351 else if BSXLOOP(ComplexNDArray, "double", complex) |
|
352 else if BSXLOOP(boolNDArray, "logical", bool) |
|
353 else if BSXLOOP(int8NDArray, "int8", int8) |
|
354 else if BSXLOOP(int16NDArray, "int16", int16) |
|
355 else if BSXLOOP(int32NDArray, "int32", int32) |
|
356 else if BSXLOOP(int64NDArray, "int64", int64) |
|
357 else if BSXLOOP(uint8NDArray, "uint8", uint8) |
|
358 else if BSXLOOP(uint16NDArray, "uint16", uint16) |
|
359 else if BSXLOOP(uint32NDArray, "uint32", uint32) |
|
360 else if BSXLOOP(uint64NDArray, "uint64", uint64) |
|
361 else |
|
362 C = do_cat_op (C, tmp(0), ra_idx); |
|
363 } |
|
364 } |
|
365 |
|
366 #define BSXEND(T) \ |
|
367 (have_ ## T) \ |
|
368 retval (0) = result_ ## T; |
|
369 |
|
370 if BSXEND(NDArray) |
|
371 else if BSXEND(ComplexNDArray) |
|
372 else if BSXEND(boolNDArray) |
|
373 else if BSXEND(int8NDArray) |
|
374 else if BSXEND(int16NDArray) |
|
375 else if BSXEND(int32NDArray) |
|
376 else if BSXEND(int64NDArray) |
|
377 else if BSXEND(uint8NDArray) |
|
378 else if BSXEND(uint16NDArray) |
|
379 else if BSXEND(uint32NDArray) |
|
380 else if BSXEND(uint64NDArray) |
|
381 else |
|
382 retval(0) = C; |
|
383 } |
|
384 } |
|
385 } |
|
386 |
|
387 if (! fcn_name.empty ()) |
|
388 clear_function (fcn_name); |
|
389 } |
|
390 |
|
391 return retval; |
|
392 } |
|
393 |
|
394 /* |
|
395 |
|
396 %!shared a, b, c, f |
|
397 %! a = randn (4, 4); |
|
398 %! b = mean (a, 1); |
|
399 %! c = mean (a, 2); |
|
400 %! f = @minus; |
|
401 %!error(bsxfun (f)); |
|
402 %!error(bsxfun (f, a)); |
|
403 %!error(bsxfun (a, b)); |
|
404 %!error(bsxfun (a, b, c)); |
|
405 %!error(bsxfun (f, a, b, c)); |
|
406 %!error(bsxfun (f, ones(4, 0), ones(4, 4))) |
|
407 %!assert(bsxfun (f, ones(4, 0), ones(4, 1)), zeros(4, 0)); |
|
408 %!assert(bsxfun (f, ones(1, 4), ones(4, 1)), zeros(4, 4)); |
|
409 %!assert(bsxfun (f, a, b), a - repmat(b, 4, 1)); |
|
410 %!assert(bsxfun (f, a, c), a - repmat(c, 1, 4)); |
|
411 %!assert(bsxfun ("minus", ones(1, 4), ones(4, 1)), zeros(4, 4)); |
|
412 |
|
413 %!shared a, b, c, f |
|
414 %! a = randn (4, 4); |
|
415 %! a(1) *= 1i; |
|
416 %! b = mean (a, 1); |
|
417 %! c = mean (a, 2); |
|
418 %! f = @minus; |
|
419 %!error(bsxfun (f)); |
|
420 %!error(bsxfun (f, a)); |
|
421 %!error(bsxfun (a, b)); |
|
422 %!error(bsxfun (a, b, c)); |
|
423 %!error(bsxfun (f, a, b, c)); |
|
424 %!error(bsxfun (f, ones(4, 0), ones(4, 4))) |
|
425 %!assert(bsxfun (f, ones(4, 0), ones(4, 1)), zeros(4, 0)); |
|
426 %!assert(bsxfun (f, ones(1, 4), ones(4, 1)), zeros(4, 4)); |
|
427 %!assert(bsxfun (f, a, b), a - repmat(b, 4, 1)); |
|
428 %!assert(bsxfun (f, a, c), a - repmat(c, 1, 4)); |
|
429 %!assert(bsxfun ("minus", ones(1, 4), ones(4, 1)), zeros(4, 4)); |
|
430 |
|
431 %!shared a, b, c, f |
|
432 %! a = randn (4, 4); |
|
433 %! a(end) *= 1i; |
|
434 %! b = mean (a, 1); |
|
435 %! c = mean (a, 2); |
|
436 %! f = @minus; |
|
437 %!error(bsxfun (f)); |
|
438 %!error(bsxfun (f, a)); |
|
439 %!error(bsxfun (a, b)); |
|
440 %!error(bsxfun (a, b, c)); |
|
441 %!error(bsxfun (f, a, b, c)); |
|
442 %!error(bsxfun (f, ones(4, 0), ones(4, 4))) |
|
443 %!assert(bsxfun (f, ones(4, 0), ones(4, 1)), zeros(4, 0)); |
|
444 %!assert(bsxfun (f, ones(1, 4), ones(4, 1)), zeros(4, 4)); |
|
445 %!assert(bsxfun (f, a, b), a - repmat(b, 4, 1)); |
|
446 %!assert(bsxfun (f, a, c), a - repmat(c, 1, 4)); |
|
447 %!assert(bsxfun ("minus", ones(1, 4), ones(4, 1)), zeros(4, 4)); |
|
448 |
|
449 %!shared a, b, c, f |
|
450 %! a = randn (4, 4); |
|
451 %! b = a (1, :); |
|
452 %! c = a (:, 1); |
|
453 %! f = @(x, y) x == y; |
|
454 %!error(bsxfun (f)); |
|
455 %!error(bsxfun (f, a)); |
|
456 %!error(bsxfun (a, b)); |
|
457 %!error(bsxfun (a, b, c)); |
|
458 %!error(bsxfun (f, a, b, c)); |
|
459 %!error(bsxfun (f, ones(4, 0), ones(4, 4))) |
|
460 %!assert(bsxfun (f, ones(4, 0), ones(4, 1)), zeros(4, 0, "logical")); |
|
461 %!assert(bsxfun (f, ones(1, 4), ones(4, 1)), ones(4, 4, "logical")); |
|
462 %!assert(bsxfun (f, a, b), a == repmat(b, 4, 1)); |
|
463 %!assert(bsxfun (f, a, c), a == repmat(c, 1, 4)); |
|
464 |
|
465 %!shared a, b, c, d, f |
|
466 %! a = randn (4, 4, 4); |
|
467 %! b = mean (a, 1); |
|
468 %! c = mean (a, 2); |
|
469 %! d = mean (a, 3); |
|
470 %! f = @minus; |
|
471 %!error(bsxfun (f, ones([4, 0, 4]), ones([4, 4, 4]))); |
|
472 %!assert(bsxfun (f, ones([4, 0, 4]), ones([4, 1, 4])), zeros([4, 0, 4])); |
|
473 %!assert(bsxfun (f, ones([4, 4, 0]), ones([4, 1, 1])), zeros([4, 4, 0])); |
|
474 %!assert(bsxfun (f, ones([1, 4, 4]), ones([4, 1, 4])), zeros([4, 4, 4])); |
|
475 %!assert(bsxfun (f, ones([4, 4, 1]), ones([4, 1, 4])), zeros([4, 4, 4])); |
|
476 %!assert(bsxfun (f, ones([4, 1, 4]), ones([1, 4, 4])), zeros([4, 4, 4])); |
|
477 %!assert(bsxfun (f, ones([4, 1, 4]), ones([1, 4, 1])), zeros([4, 4, 4])); |
|
478 %!assert(bsxfun (f, a, b), a - repmat(b, [4, 1, 1])); |
|
479 %!assert(bsxfun (f, a, c), a - repmat(c, [1, 4, 1])); |
|
480 %!assert(bsxfun (f, a, d), a - repmat(d, [1, 1, 4])); |
|
481 %!assert(bsxfun ("minus", ones([4, 0, 4]), ones([4, 1, 4])), zeros([4, 0, 4])); |
|
482 |
|
483 %% The below is a very hard case to treat |
|
484 %!assert(bsxfun (f, ones([4, 1, 4, 1]), ones([1, 4, 1, 4])), zeros([4, 4, 4, 4])); |
|
485 |
|
486 */ |