GNU Octave  4.0.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
boolNDArray.cc
Go to the documentation of this file.
1 // N-D Array manipulations.
2 /*
3 
4 Copyright (C) 1996-2015 John W. Eaton
5 Copyright (C) 2009 VZLU Prague, a.s.
6 
7 This file is part of Octave.
8 
9 Octave is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13 
14 Octave is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Octave; see the file COPYING. If not, see
21 <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include "Array-util.h"
30 #include "boolNDArray.h"
31 #include "CNDArray.h"
32 #include "mx-base.h"
33 #include "lo-ieee.h"
34 #include "mx-op-defs.h"
35 #include "MArray-defs.h"
36 
37 #include "bsxfun-defs.cc"
38 
39 // unary operations
40 
43 {
44  return do_mx_unary_op<bool, bool> (*this, mx_inline_not);
45 }
46 
49 {
50  if (is_shared ())
51  *this = ! *this;
52  else
53  do_mx_inplace_op<bool> (*this, mx_inline_not2);
54 
55  return *this;
56 }
57 
58 // FIXME: this is not quite the right thing.
59 
61 boolNDArray::all (int dim) const
62 {
63  return do_mx_red_op<bool, bool> (*this, dim, mx_inline_all);
64 }
65 
67 boolNDArray::any (int dim) const
68 {
69  return do_mx_red_op<bool, bool> (*this, dim, mx_inline_any);
70 }
71 
72 NDArray
73 boolNDArray::sum (int dim) const
74 {
75  // NOTE: going via octave_idx_type is typically faster even though it
76  // requires a conversion.
77  return do_mx_red_op<octave_idx_type, bool> (*this, dim, mx_inline_count);
78 }
79 
80 NDArray
81 boolNDArray::cumsum (int dim) const
82 {
83  // In this case, it's better to sum directly to doubles.
84  return do_mx_cum_op<double , bool> (*this, dim, mx_inline_cumcount);
85 }
86 
90 {
91  if (rb.numel () > 0)
92  insert (rb, ra_idx);
93  return *this;
94 }
95 
98 {
99  Array<bool>::insert (a, r, c);
100  return *this;
101 }
102 
105 {
106  Array<bool>::insert (a, ra_idx);
107  return *this;
108 }
109 
110 void
112  const dim_vector& dimensions,
113  int start_dimension)
114 {
115  ::increment_index (ra_idx, dimensions, start_dimension);
116 }
117 
120  const dim_vector& dimensions)
121 {
122  return ::compute_index (ra_idx, dimensions);
123 }
124 
127 {
128  return Array<bool>::diag (k);
129 }
130 
133 {
134  return Array<bool>::diag (m, n);
135 }
136 
138 NDND_CMP_OPS (boolNDArray, boolNDArray)
139 
140 NDS_BOOL_OPS (boolNDArray, bool)
141 NDS_CMP_OPS (boolNDArray, bool)
142 
143 SND_BOOL_OPS (bool, boolNDArray)
144 SND_CMP_OPS (bool, boolNDArray)
145 
146 boolNDArray&
147 mx_el_and_assign (boolNDArray& a, const boolNDArray& b)
148 {
149  if (a.is_shared ())
150  a = mx_el_and (a, b);
151  else
152  do_mm_inplace_op<bool, bool> (a, b, mx_inline_and2, mx_inline_and2,
153  "operator &=");
154 
155  return a;
156 }
157 
160 {
161  if (a.is_shared ())
162  a = mx_el_or (a, b);
163  else
164  do_mm_inplace_op<bool, bool> (a, b, mx_inline_or2, mx_inline_or2,
165  "operator |=");
166 
167  return a;
168 }
169 
octave_idx_type compute_index(octave_idx_type n, const dim_vector &dims)
Definition: Array-util.cc:178
boolNDArray all(int dim=-1) const
Definition: boolNDArray.cc:61
#define NDS_BOOL_OPS(ND, S)
Definition: mx-op-defs.h:253
#define NDS_CMP_OPS(ND, S)
Definition: mx-op-defs.h:236
void mx_inline_or2(size_t n, bool *r, const X *x)
Definition: mx-inlines.cc:178
NDArray cumsum(int dim=-1) const
Definition: boolNDArray.cc:81
boolNDArray & invert(void)
Definition: boolNDArray.cc:48
const octave_base_value const Array< octave_idx_type > & ra_idx
#define SND_CMP_OPS(S, ND)
Definition: mx-op-defs.h:283
#define BSXFUN_OP_DEF_MXLOOP(OP, ARRAY, LOOP)
Definition: bsxfun-defs.cc:221
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:275
static void increment_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions, int start_dimension=0)
Definition: boolNDArray.cc:111
boolNDArray mx_el_or(const boolNDArray &m1, const boolNDArray &m2)
Definition: boolNDArray.cc:137
boolNDArray concat(const boolNDArray &rb, const Array< octave_idx_type > &ra_idx)
Definition: boolNDArray.cc:88
void mx_inline_cumcount(const bool *v, T *r, octave_idx_type n)
Definition: mx-inlines.cc:629
Array< T > diag(octave_idx_type k=0) const
Get the kth super or subdiagonal.
Definition: Array.cc:2526
boolNDArray any(int dim=-1) const
Definition: boolNDArray.cc:67
bool mx_inline_any(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:524
Array< T > & insert(const Array< T > &a, const Array< octave_idx_type > &idx)
Insert an array into another at a specified position.
Definition: Array.cc:1591
#define NDND_CMP_OPS(ND1, ND2)
Definition: mx-op-defs.h:330
#define NDND_BOOL_OPS(ND1, ND2)
Definition: mx-op-defs.h:347
boolNDArray mx_el_and(const boolNDArray &m1, const boolNDArray &m2)
Definition: boolNDArray.cc:137
void mx_inline_and2(size_t n, bool *r, const X *x)
Definition: mx-inlines.cc:177
#define SND_BOOL_OPS(S, ND)
Definition: mx-op-defs.h:300
boolNDArray diag(octave_idx_type k=0) const
Definition: boolNDArray.cc:126
boolNDArray & mx_el_and_assign(boolNDArray &a, const boolNDArray &b)
Definition: boolNDArray.cc:147
void mx_inline_and(size_t n, bool *r, const X *x, const Y *y)
Definition: mx-inlines.cc:159
bool is_shared(void)
Definition: Array.h:485
void mx_inline_or(size_t n, bool *r, const X *x, const Y *y)
Definition: mx-inlines.cc:160
static octave_idx_type compute_index(Array< octave_idx_type > &ra_idx, const dim_vector &dimensions)
Definition: boolNDArray.cc:119
bool mx_inline_all(const T *v, octave_idx_type n)
Definition: mx-inlines.cc:524
void mx_inline_not(size_t n, bool *r, const X *x)
Definition: mx-inlines.cc:126
void mx_inline_not2(size_t n, bool *r)
Definition: mx-inlines.cc:132
NDArray sum(int dim=-1) const
Definition: boolNDArray.cc:73
boolNDArray & mx_el_or_assign(boolNDArray &a, const boolNDArray &b)
Definition: boolNDArray.cc:159
boolNDArray operator!(void) const
Definition: boolNDArray.cc:42
T mx_inline_count(const bool *v, octave_idx_type n)
Definition: mx-inlines.cc:523
boolNDArray & insert(const boolNDArray &a, octave_idx_type r, octave_idx_type c)
Definition: boolNDArray.cc:97