GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-binmap.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2010-2013 VZLU Prague
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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_oct_binmap_h)
24 #define octave_oct_binmap_h 1
25 
26 #include "Array.h"
27 #include "Sparse.h"
28 #include "Array-util.h"
29 
30 #include "bsxfun.h"
31 
32 // This source file implements a general binary maping function for
33 // arrays. The syntax is binmap<type> (a, b, f,[name]). type denotes
34 // the expected return type of the operation. a, b, should be one of
35 // the 6 combinations:
36 //
37 // Array-Array
38 // Array-scalar
39 // scalar-Array
40 // Sparse-Sparse
41 // Sparse-scalar
42 // scalar-Sparse
43 //
44 // If both operands are nonscalar, name must be supplied. It is used
45 // as the base for error message when operands are nonconforming.
46 //
47 // The operation needs not be homogeneous, i.e. a, b and the result
48 // may be of distinct types. f can have any of the four signatures:
49 //
50 // U f (T, R)
51 // U f (const T&, R)
52 // U f (T, const R&)
53 // U f (const T&, const R&)
54 //
55 // Additionally, f can be an arbitrary functor object.
56 //
57 // octave_quit() is called at appropriate places, hence the operation
58 // is breakable.
59 
60 // The following template wrappers are provided for automatic bsxfun
61 // calls (see the function signature for do_bsxfun_op).
62 
63 template<typename R, typename X, typename Y, typename F>
65 {
66 private:
67  static F f;
68 
69 public:
70  static void
71  set_f (const F& f_in)
72  {
73  f = f_in;
74  }
75 
76  static void
77  op_mm (size_t n, R* r, const X* x , const Y* y)
78  {
79  for (size_t i = 0; i < n; i++)
80  r[i] = f (x[i], y[i]);
81  }
82 
83  static void
84  op_sm (size_t n, R* r, X x, const Y* y)
85  {
86  for (size_t i = 0; i < n; i++)
87  r[i] = f (x, y[i]);
88  }
89 
90  static void
91  op_ms (size_t n , R* r, const X* x, Y y)
92  {
93  for (size_t i = 0; i < n; i++)
94  r[i] = f (x[i], y);
95  }
96 };
97 
98 // Static init
99 template<typename R, typename X, typename Y, typename F>
101 
102 
103 // scalar-Array
104 template <class U, class T, class R, class F>
105 Array<U>
106 binmap (const T& x, const Array<R>& ya, F fcn)
107 {
108  octave_idx_type len = ya.numel ();
109 
110  const R *y = ya.data ();
111 
112  Array<U> result (ya.dims ());
113  U *p = result.fortran_vec ();
114 
115  octave_idx_type i;
116  for (i = 0; i < len - 3; i += 4)
117  {
118  octave_quit ();
119 
120  p[i] = fcn (x, y[i]);
121  p[i+1] = fcn (x, y[i+1]);
122  p[i+2] = fcn (x, y[i+2]);
123  p[i+3] = fcn (x, y[i+3]);
124  }
125 
126  octave_quit ();
127 
128  for (; i < len; i++)
129  p[i] = fcn (x, y[i]);
130 
131  return result;
132 }
133 
134 // Array-scalar
135 template <class U, class T, class R, class F>
136 Array<U>
137 binmap (const Array<T>& xa, const R& y, F fcn)
138 {
139  octave_idx_type len = xa.numel ();
140 
141  const R *x = xa.data ();
142 
143  Array<U> result (xa.dims ());
144  U *p = result.fortran_vec ();
145 
146  octave_idx_type i;
147  for (i = 0; i < len - 3; i += 4)
148  {
149  octave_quit ();
150 
151  p[i] = fcn (x[i], y);
152  p[i+1] = fcn (x[i+1], y);
153  p[i+2] = fcn (x[i+2], y);
154  p[i+3] = fcn (x[i+3], y);
155  }
156 
157  octave_quit ();
158 
159  for (; i < len; i++)
160  p[i] = fcn (x[i], y);
161 
162  return result;
163 }
164 
165 // Array-Array (treats singletons as scalars)
166 template <class U, class T, class R, class F>
167 Array<U>
168 binmap (const Array<T>& xa, const Array<R>& ya, F fcn, const char *name)
169 {
170  dim_vector xad = xa.dims (), yad = ya.dims ();
171  if (xa.numel () == 1)
172  return binmap<U, T, R, F> (xa(0), ya, fcn);
173  else if (ya.numel () == 1)
174  return binmap<U, T, R, F> (xa, ya(0), fcn);
175  else if (xad != yad)
176  {
177  if (is_valid_bsxfun (name, xad, yad))
178  {
180  return do_bsxfun_op (xa, ya,
184  }
185  else
186  gripe_nonconformant (name, xad, yad);
187  }
188 
189  octave_idx_type len = xa.numel ();
190 
191  const T *x = xa.data ();
192  const T *y = ya.data ();
193 
194  Array<U> result (xa.dims ());
195  U *p = result.fortran_vec ();
196 
197  octave_idx_type i;
198  for (i = 0; i < len - 3; i += 4)
199  {
200  octave_quit ();
201 
202  p[i] = fcn (x[i], y[i]);
203  p[i+1] = fcn (x[i+1], y[i+1]);
204  p[i+2] = fcn (x[i+2], y[i+2]);
205  p[i+3] = fcn (x[i+3], y[i+3]);
206  }
207 
208  octave_quit ();
209 
210  for (; i < len; i++)
211  p[i] = fcn (x[i], y[i]);
212 
213  return result;
214 }
215 
216 // scalar-Sparse
217 template <class U, class T, class R, class F>
218 Sparse<U>
219 binmap (const T& x, const Sparse<R>& ys, F fcn)
220 {
221  octave_idx_type nz = ys.nnz ();
222  Sparse<U> retval (ys.rows (), ys.cols (), nz);
223  for (octave_idx_type i = 0; i < nz; i++)
224  {
225  octave_quit ();
226  retval.xdata (i) = fcn (x, ys.data (i));
227  }
228 
229  octave_quit ();
230  retval.maybe_compress ();
231  return retval;
232 }
233 
234 // Sparse-scalar
235 template <class U, class T, class R, class F>
236 Sparse<U>
237 binmap (const Sparse<T>& xs, const R& y, F fcn)
238 {
239  octave_idx_type nz = xs.nnz ();
240  Sparse<U> retval (xs.rows (), xs.cols (), nz);
241  for (octave_idx_type i = 0; i < nz; i++)
242  {
243  octave_quit ();
244  retval.xdata (i) = fcn (xs.data (i), y);
245  }
246 
247  octave_quit ();
248  retval.maybe_compress ();
249  return retval;
250 }
251 
252 // Sparse-Sparse (treats singletons as scalars)
253 template <class U, class T, class R, class F>
254 Sparse<U>
255 binmap (const Sparse<T>& xs, const Sparse<R>& ys, F fcn, const char *name)
256 {
257  if (xs.rows () == 1 && xs.cols () == 1)
258  return binmap<U, T, R, F> (xs(0,0), ys, fcn);
259  else if (ys.rows () == 1 && ys.cols () == 1)
260  return binmap<U, T, R, F> (xs, ys(0,0), fcn);
261  else if (xs.dims () != ys.dims ())
262  gripe_nonconformant (name, xs.dims (), ys.dims ());
263 
264  T xzero = T ();
265  R yzero = R ();
266 
267  U fz = fcn (xzero, yzero);
268  if (fz == U ())
269  {
270  // Sparsity-preserving function. Do it efficiently.
271  octave_idx_type nr = xs.rows (), nc = xs.cols ();
272  Sparse<T> retval (nr, nc);
273 
274  octave_idx_type nz = 0;
275  // Count nonzeros.
276  for (octave_idx_type j = 0; j < nc; j++)
277  {
278  octave_quit ();
279  octave_idx_type ix = xs.cidx (j), iy = ys.cidx (j);
280  octave_idx_type ux = xs.cidx (j+1), uy = ys.cidx (j+1);
281  while (ix != ux || iy != uy)
282  {
283  octave_idx_type rx = xs.ridx (ix), ry = ys.ridx (ix);
284  ix += rx <= ry;
285  iy += ry <= rx;
286  nz++;
287  }
288 
289  retval.xcidx (j+1) = nz;
290  }
291 
292  // Allocate space.
293  retval.change_capacity (retval.xcidx (nc));
294 
295  // Fill.
296  nz = 0;
297  for (octave_idx_type j = 0; j < nc; j++)
298  {
299  octave_quit ();
300  octave_idx_type ix = xs.cidx (j), iy = ys.cidx (j);
301  octave_idx_type ux = xs.cidx (j+1), uy = ys.cidx (j+1);
302  while (ix != ux || iy != uy)
303  {
304  octave_idx_type rx = xs.ridx (ix), ry = ys.ridx (ix);
305  if (rx == ry)
306  {
307  retval.xridx (nz) = rx;
308  retval.xdata (nz) = fcn (xs.data (ix), ys.data (iy));
309  ix++;
310  iy++;
311  }
312  else if (rx < ry)
313  {
314  retval.xridx (nz) = rx;
315  retval.xdata (nz) = fcn (xs.data (ix), yzero);
316  ix++;
317  }
318  else if (ry < rx)
319  {
320  retval.xridx (nz) = ry;
321  retval.xdata (nz) = fcn (xzero, ys.data (iy));
322  iy++;
323  }
324 
325  nz++;
326  }
327  }
328 
329  retval.maybe_compress ();
330  return retval;
331  }
332  else
333  return Sparse<U> (binmap<U, T, R, F> (xs.array_value (), ys.array_value (),
334  fcn, name));
335 }
336 
337 // Overloads for function pointers.
338 
339 // Signature (T, R)
340 
341 template <class U, class T, class R>
342 inline Array<U>
343 binmap (const Array<T>& xa, const Array<R>& ya, U (*fcn) (T, R),
344  const char *name)
345 { return binmap<U, T, R, U (*) (T, R)> (xa, ya, fcn, name); }
346 
347 template <class U, class T, class R>
348 inline Array<U>
349 binmap (const T& x, const Array<R>& ya, U (*fcn) (T, R))
350 { return binmap<U, T, R, U (*) (T, R)> (x, ya, fcn); }
351 
352 template <class U, class T, class R>
353 inline Array<U>
354 binmap (const Array<T>& xa, const R& y, U (*fcn) (T, R))
355 { return binmap<U, T, R, U (*) (T, R)> (xa, y, fcn); }
356 
357 template <class U, class T, class R>
358 inline Sparse<U>
359 binmap (const Sparse<T>& xa, const Sparse<R>& ya, U (*fcn) (T, R),
360  const char *name)
361 { return binmap<U, T, R, U (*) (T, R)> (xa, ya, fcn, name); }
362 
363 template <class U, class T, class R>
364 inline Sparse<U>
365 binmap (const T& x, const Sparse<R>& ya, U (*fcn) (T, R))
366 { return binmap<U, T, R, U (*) (T, R)> (x, ya, fcn); }
367 
368 template <class U, class T, class R>
369 inline Sparse<U>
370 binmap (const Sparse<T>& xa, const R& y, U (*fcn) (T, R))
371 { return binmap<U, T, R, U (*) (T, R)> (xa, y, fcn); }
372 
373 // Signature (const T&, const R&)
374 
375 template <class U, class T, class R>
376 inline Array<U>
377 binmap (const Array<T>& xa, const Array<R>& ya, U (*fcn) (const T&, const R&),
378  const char *name)
379 { return binmap<U, T, R, U (*) (const T&, const R&)> (xa, ya, fcn, name); }
380 
381 template <class U, class T, class R>
382 inline Array<U>
383 binmap (const T& x, const Array<R>& ya, U (*fcn) (const T&, const R&))
384 { return binmap<U, T, R, U (*) (const T&, const R&)> (x, ya, fcn); }
385 
386 template <class U, class T, class R>
387 inline Array<U>
388 binmap (const Array<T>& xa, const R& y, U (*fcn) (const T&, const R&))
389 { return binmap<U, T, R, U (*) (const T&, const R&)> (xa, y, fcn); }
390 
391 template <class U, class T, class R>
392 inline Sparse<U>
393 binmap (const Sparse<T>& xa, const Sparse<R>& ya, U (*fcn) (const T&, const R&),
394  const char *name)
395 { return binmap<U, T, R, U (*) (const T&, const R&)> (xa, ya, fcn, name); }
396 
397 template <class U, class T, class R>
398 inline Sparse<U>
399 binmap (const T& x, const Sparse<R>& ya, U (*fcn) (const T&, const R&))
400 { return binmap<U, T, R, U (*) (const T&, const R&)> (x, ya, fcn); }
401 
402 template <class U, class T, class R>
403 inline Sparse<U>
404 binmap (const Sparse<T>& xa, const R& y, U (*fcn) (const T&, const R&))
405 { return binmap<U, T, R, U (*) (const T&, const R&)> (xa, y, fcn); }
406 
407 // Signature (const T&, R)
408 
409 template <class U, class T, class R>
410 inline Array<U>
411 binmap (const Array<T>& xa, const Array<R>& ya, U (*fcn) (const T&, R),
412  const char *name)
413 { return binmap<U, T, R, U (*) (const T&, R)> (xa, ya, fcn, name); }
414 
415 template <class U, class T, class R>
416 inline Array<U>
417 binmap (const T& x, const Array<R>& ya, U (*fcn) (const T&, R))
418 { return binmap<U, T, R, U (*) (const T&, R)> (x, ya, fcn); }
419 
420 template <class U, class T, class R>
421 inline Array<U>
422 binmap (const Array<T>& xa, const R& y, U (*fcn) (const T&, R))
423 { return binmap<U, T, R, U (*) (const T&, R)> (xa, y, fcn); }
424 
425 template <class U, class T, class R>
426 inline Sparse<U>
427 binmap (const Sparse<T>& xa, const Sparse<R>& ya, U (*fcn) (const T&, R),
428  const char *name)
429 { return binmap<U, T, R, U (*) (const T&, R)> (xa, ya, fcn, name); }
430 
431 template <class U, class T, class R>
432 inline Sparse<U>
433 binmap (const T& x, const Sparse<R>& ya, U (*fcn) (const T&, R))
434 { return binmap<U, T, R, U (*) (const T&, R)> (x, ya, fcn); }
435 
436 template <class U, class T, class R>
437 inline Sparse<U>
438 binmap (const Sparse<T>& xa, const R& y, U (*fcn) (const T&, R))
439 { return binmap<U, T, R, U (*) (const T&, R)> (xa, y, fcn); }
440 
441 // Signature (T, const R&)
442 
443 template <class U, class T, class R>
444 inline Array<U>
445 binmap (const Array<T>& xa, const Array<R>& ya, U (*fcn) (T, const R&),
446  const char *name)
447 { return binmap<U, T, R, U (*) (T, const R&)> (xa, ya, fcn, name); }
448 
449 template <class U, class T, class R>
450 inline Array<U>
451 binmap (const T& x, const Array<R>& ya, U (*fcn) (T, const R&))
452 { return binmap<U, T, R, U (*) (T, const R&)> (x, ya, fcn); }
453 
454 template <class U, class T, class R>
455 inline Array<U>
456 binmap (const Array<T>& xa, const R& y, U (*fcn) (T, const R&))
457 { return binmap<U, T, R, U (*) (T, const R&)> (xa, y, fcn); }
458 
459 template <class U, class T, class R>
460 inline Sparse<U>
461 binmap (const Sparse<T>& xa, const Sparse<R>& ya, U (*fcn) (T, const R&),
462  const char *name)
463 { return binmap<U, T, R, U (*) (T, const R&)> (xa, ya, fcn, name); }
464 
465 template <class U, class T, class R>
466 inline Sparse<U>
467 binmap (const T& x, const Sparse<R>& ya, U (*fcn) (T, const R&))
468 { return binmap<U, T, R, U (*) (T, const R&)> (x, ya, fcn); }
469 
470 template <class U, class T, class R>
471 inline Sparse<U>
472 binmap (const Sparse<T>& xa, const R& y, U (*fcn) (T, const R&))
473 { return binmap<U, T, R, U (*) (T, const R&)> (xa, y, fcn); }
474 
475 #endif