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
bsxfun.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2012-2013 Jordi GutiĆ©rrez Hermoso
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 // Author: Jordi GutiĆ©rrez Hermoso <[email protected]>
24 
25 #if !defined (octave_bsxfun_h)
26 #define octave_bsxfun_h 1
27 
28 #include <algorithm>
29 
30 #include "Array.h"
31 #include "dim-vector.h"
32 #include "lo-error.h"
33 
34 inline
35 bool
36 is_valid_bsxfun (const std::string& name, const dim_vector& dx,
37  const dim_vector& dy)
38 {
39  for (int i = 0; i < std::min (dx.length (), dy.length ()); i++)
40  {
41  octave_idx_type xk = dx(i), yk = dy(i);
42  // Check the three conditions for valid bsxfun dims
43  if (! ( (xk == yk) || (xk == 1 && yk > 1) || (xk > 1 && yk == 1)))
44  return false;
45  }
46 
47  (*current_liboctave_warning_with_id_handler)
48  ("Octave:broadcast", "%s: automatic broadcasting operation applied",
49  name.c_str ());
50 
51  return true;
52 }
53 
54 // since we can't change the size of the assigned-to matrix, we cannot
55 // apply singleton expansion to it, so the conditions to check are
56 // different here.
57 inline
58 bool
59 is_valid_inplace_bsxfun (const std::string& name, const dim_vector& dr,
60  const dim_vector& dx)
61 {
62  octave_idx_type drl = dr.length (), dxl = dx.length ();
63  if (drl < dxl)
64  return false;
65 
66  for (int i = 0; i < drl; i++)
67  {
68  octave_idx_type rk = dr(i), xk = dx(i);
69 
70  // Only two valid canditions to check; can't stretch rk
71  if (! ( (rk == xk) || (rk > 1 && xk == 1)))
72  return false;
73  }
74 
75  (*current_liboctave_warning_with_id_handler)
76  ("Octave:broadcast", "%s: automatic broadcasting operation applied",
77  name.c_str ());
78 
79  return true;
80 }
81 
82 #include "bsxfun-defs.cc"
83 
84 #endif