GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
bsxfun.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2012-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 // Author: Jordi GutiĆ©rrez Hermoso <jordigh@octave.org>
24 
25 #if ! defined (octave_bsxfun_h)
26 #define octave_bsxfun_h 1
27 
28 #include "octave-config.h"
29 
30 #include <algorithm>
31 #include <string>
32 
33 #include "dim-vector.h"
34 #include "lo-error.h"
35 
36 inline
37 bool
39  const dim_vector& xdv, const dim_vector& ydv)
40 {
41  for (int i = 0; i < std::min (xdv.ndims (), ydv.ndims ()); i++)
42  {
43  octave_idx_type xk = xdv(i);
44  octave_idx_type yk = ydv(i);
45  // Check the three conditions for valid bsxfun dims
46  if (! ((xk == yk) || (xk == 1 && yk != 1) || (xk != 1 && yk == 1)))
47  return false;
48  }
49 
50  (*current_liboctave_warning_with_id_handler)
51  ("Octave:language-extension", "performing `%s' automatic broadcasting",
52  name.c_str ());
53 
54  return true;
55 }
56 
57 // For inplace operations the size of the resulting matrix cannot be changed.
58 // Therefore we can only apply singleton expansion on the second matrix which
59 // alters the conditions to check.
60 inline
61 bool
63  const dim_vector& rdv, const dim_vector& xdv)
64 {
65  octave_idx_type r_nd = rdv.ndims ();
66  octave_idx_type x_nd = xdv.ndims ();
67  if (r_nd < x_nd)
68  return false;
69 
70  for (int i = 0; i < r_nd; i++)
71  {
72  octave_idx_type rk = rdv(i);
73  octave_idx_type xk = xdv(i);
74 
75  // Only two valid conditions to check; can't stretch rk
76  if (! ((rk == xk) || (rk != 1 && xk == 1)))
77  return false;
78  }
79 
80  (*current_liboctave_warning_with_id_handler)
81  ("Octave:language-extension", "performing `%s' automatic broadcasting",
82  name.c_str ());
83 
84  return true;
85 }
86 
87 #include "bsxfun-defs.cc"
88 
89 #endif
bool is_valid_bsxfun(const std::string &name, const dim_vector &xdv, const dim_vector &ydv)
Definition: bsxfun.h:38
bool is_valid_inplace_bsxfun(const std::string &name, const dim_vector &rdv, const dim_vector &xdv)
Definition: bsxfun.h:62
nd deftypefn *std::string name
Definition: sysdep.cc:647
for i
Definition: data.cc:5264
octave_idx_type ndims(void) const
Number of dimensions.
Definition: dim-vector.h:295
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:204