GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
bsxfun.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2012-2026 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
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
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26#if ! defined (octave_bsxfun_h)
27#define octave_bsxfun_h 1
28
29#include "octave-config.h"
30
31#include <algorithm>
32#include <string>
33
34#include "dim-vector.h"
35#include "oct-error.h"
36
37inline
38bool
39is_valid_bsxfun (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 return true;
51}
52
53// For inplace operations the size of the resulting matrix cannot be changed.
54// Therefore we can only apply singleton expansion on the second matrix which
55// alters the conditions to check.
56inline
57bool
59{
60 octave_idx_type r_nd = rdv.ndims ();
61 octave_idx_type x_nd = xdv.ndims ();
62 if (r_nd < x_nd)
63 return false;
64
65 for (int i = 0; i < x_nd; i++)
66 {
67 octave_idx_type rk = rdv(i);
68 octave_idx_type xk = xdv(i);
69
70 // Only two valid conditions to check; can't stretch rk
71 if ((rk != xk) && xk != 1)
72 return false;
73 }
74
75 return true;
76}
77
78#include "bsxfun-defs.cc"
79
80#endif
bool is_valid_inplace_bsxfun(const dim_vector &rdv, const dim_vector &xdv)
Definition bsxfun.h:58
bool is_valid_bsxfun(const dim_vector &xdv, const dim_vector &ydv)
Definition bsxfun.h:39
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:92
octave_idx_type ndims() const
Number of dimensions.
Definition dim-vector.h:263