casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ArrayLogical.h
Go to the documentation of this file.
1 //# ArrayLogical.h: Element by element logical operations on arrays.
2 //# Copyright (C) 1993,1994,1995,1999,2001
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 #ifndef CASA_ARRAYLOGICAL_2_H
29 #define CASA_ARRAYLOGICAL_2_H
30 
31 //# Includes
32 #include "ArrayFwd.h"
33 #include "IPosition.h"
34 
35 namespace casacore { //# NAMESPACE CASACORE - BEGIN
36 
37 // <summary>
38 // Logical operations for Arrays.
39 // </summary>
40 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayLogical">
41 //
42 // <prerequisite>
43 // <li> <linkto class=Array>Array</linkto>
44 // </prerequisite>
45 //
46 // <etymology>
47 // This file contains global functions which perform element by element logical
48 // operations on arrays.
49 // </etymology>
50 //
51 // <synopsis>
52 // These functions perform element by element logical operations on
53 // arrays. The two arrays must conform, except for allEQ which returns
54 // false if the arrays do not conform.
55 //
56 // There are two classes of functions. One class returns a LogicalArray.
57 // In these functions, the value of an element of the LogicalArray is
58 // the value of the logical operation applied to the corresponding elements
59 // of the input Arrays. The other class of functions returns a single
60 // bool. The return value is true if the logical operation returns true for
61 // all elements of the input arrays for the "all" functions
62 // (e.g. allLE()), and returns true if the logical operation returns true for
63 // any elements of the input arrays for the "any" functions
64 // (e.g. anyLE()).
65 //
66 // For instance allLE (a, b) implies that every element of a is
67 // less than or equal to every element of b. Note that with this definition
68 // allLE (a, b) and allGE (a, b) can both be false (e.g. a = [1,0] b = [0,1]).
69 //
70 // <note role=caution> Comparison between two zero-sized arrays is not defined
71 // (should it throw an exception?).
72 // </note>
73 //
74 // </synopsis>
75 //
76 // <example>
77 // <srcblock>
78 // Vector<int> a(10);
79 // Vector<int> b(10);
80 // LogicalVector l(10);
81 // . . .
82 // l = a < b;
83 // </srcblock>
84 // This example sets the elements of l (a<b).
85 // The result of the comparison is a LogicalArray.
86 // </example>
87 //
88 // <example>
89 // <srcblock>
90 // Vector<int> a(10);
91 // Vector<int> b(10);
92 // bool result;
93 // . . .
94 // result = allLT (a, b);
95 // </srcblock>
96 // This example sets result to true if, for all elements, a<b.
97 // </example>
98 //
99 // <motivation>
100 // One wants to be able to perform logical operations on arrays.
101 // </motivation>
102 //
103 // <todo asof="$DATE:$>
104 // <li> Reconsider where the origin of the returned LogicalArray should
105 // be located.
106 // </todo>
107 //
108 // <linkfrom anchor="Array logical operations" classes="Array Vector Matrix Cube">
109 // <here>Array logical operations</here> -- Logical operations for Arrays.
110 // </linkfrom>
111 //
112 // <group name="Array logical operations">
114 
115 // Determine if the comparisons between corresponding array elements yield true.
116 // <group>
117 template<typename T, typename CompareOperator>
118 bool arrayCompareAll (const Array<T>& left, const Array<T>& right,
119  CompareOperator op);
120 template<typename T, typename CompareOperator>
121 bool arrayCompareAll (const Array<T>& left, T right,
122  CompareOperator op);
123 template<typename T, typename CompareOperator>
124 bool arrayCompareAll (T left, const Array<T>& right,
125  CompareOperator op);
126 // </group>
127 
128 // Determine if the comparisons between corresponding array elements yield true.
129 // <group>
130 template<typename T, typename CompareOperator>
131 bool arrayCompareAny (const Array<T>& left, const Array<T>& right,
132  CompareOperator op);
133 template<typename T, typename CompareOperator>
134 bool arrayCompareAny (const Array<T>& left, T right,
135  CompareOperator op);
136 template<typename T, typename CompareOperator>
137 bool arrayCompareAny (T left, const Array<T>& right,
138  CompareOperator op);
139 // </group>
140 
141 //
142 // Element by element comparisons between the "l" and "r" arrays. The result
143 // is true only if the comparison is true for every element of the arrays.
144 //
145 // The operator forms of array logical operations which return a single bool
146 // have been replaced by these "all" functions.
147 // The operator forms of array logical operations now return a LogicalArray.
148 //
149 // The arrays must conform except for allEQ, which will return false if the
150 // arrays have different shapes.
151 //
152 // <thrown>
153 // <li> ArrayConformanceError
154 // </thrown>
155 //
156 // <group>
157 template<class T> bool allLE (const Array<T> &l, const Array<T> &r);
158 template<class T> bool allLT (const Array<T> &l, const Array<T> &r);
159 template<class T> bool allGE (const Array<T> &l, const Array<T> &r);
160 template<class T> bool allGT (const Array<T> &l, const Array<T> &r);
161 template<class T> bool allEQ (const Array<T> &l, const Array<T> &r);
162 template<class T> bool allNE (const Array<T> &l, const Array<T> &r);
163 template<class T> bool allNear (const Array<T> &l, const Array<T> &r,
164  double tol);
165 template<class T> bool allNearAbs (const Array<T> &l, const Array<T> &r,
166  double tol);
167 //
168 // This only makes sense if the array element type is logical valued.
169 // <group>
170 template<class T> bool allAND (const Array<T> &l, const Array<T> &r);
171 template<class T> bool allOR (const Array<T> &l, const Array<T> &r);
172 // </group>
173 //
174 // </group>
175 
176 
177 //
178 // Element by element comparisons between the "l" and "r" arrays. The result
179 // is a LogicalArray.
180 // The arrays must conform or an exception is thrown.
181 //
182 // The Vector, Matrix and Cube version are present to bypass the problems
183 // due to the existence of automatic comparison inline templates in standard
184 // algorithm library, producing a single bool value.
185 //
186 // <group>
187 template<class T> LogicalArray operator <= (const Array<T> &l,
188  const Array<T> &r);
189 template<class T> LogicalArray operator < (const Array<T> &l,
190  const Array<T> &r);
191 template<class T> LogicalArray operator >= (const Array<T> &l,
192  const Array<T> &r);
193 template<class T> LogicalArray operator > (const Array<T> &l,
194  const Array<T> &r);
195 template<class T> LogicalArray operator == (const Array<T> &l,
196  const Array<T> &r);
197 template<class T> LogicalArray operator != (const Array<T> &l,
198  const Array<T> &r);
199 
200 template<class T> LogicalArray near(const Array<T> &l, const Array<T> &r,
201  double tol);
202 template<class T> LogicalArray nearAbs(const Array<T> &l, const Array<T> &r,
203  double tol);
204 //
205 // This only makes sense if the array element type is logical valued.
206 // <group>
207 template<class T> LogicalArray operator && (const Array<T> &l, const Array<T> &r);
208 template<class T> LogicalArray operator || (const Array<T> &l, const Array<T> &r);
209 // </group>
210 //
211 // </group>
212 
213 
214 //
215 // Logical negation of an array. This only makes sense if the array
216 // element type is logical valued.
217 template<class T> LogicalArray operator ! (const Array<T> &l);
218 
219 
220 //
221 // Element by element comparisons between an array and a scalar, which
222 // behaves as if it were a conformant array filled with the value "val."
223 // The result is true only if the comparison is true for every element
224 // of the array.
225 // <group>
226 template<class T> bool allLE (const Array<T> &array, const T &val);
227 template<class T> bool allLE (const T &val, const Array<T> &array);
228 template<class T> bool allLT (const Array<T> &array, const T &val);
229 template<class T> bool allLT (const T &val, const Array<T> &array);
230 template<class T> bool allGE (const Array<T> &array, const T &val);
231 template<class T> bool allGE (const T &val, const Array<T> &array);
232 template<class T> bool allGT (const Array<T> &array, const T &val);
233 template<class T> bool allGT (const T &val, const Array<T> &array);
234 template<class T> bool allEQ (const Array<T> &array, const T &val);
235 template<class T> bool allEQ (const T &val, const Array<T> &array);
236 template<class T> bool allNE (const Array<T> &array, const T &val);
237 template<class T> bool allNE (const T &val, const Array<T> &array);
238 template<class T> bool allNear (const Array<T> &array, const T &val, double tol);
239 template<class T> bool allNear (const T &val, const Array<T> &array, double tol);
240 template<class T> bool allNearAbs (const Array<T> &array, const T &val,
241  double tol);
242 template<class T> bool allNearAbs (const T &val, const Array<T> &array,
243  double tol);
244 //
245 // This only makes sense if the array element type is logical valued.
246 // <group>
247 template<class T> bool allAND (const Array<T> &array, const T &val);
248 template<class T> bool allAND (const T &val, const Array<T> &array);
249 template<class T> bool allOR (const Array<T> &array, const T &val);
250 template<class T> bool allOR (const T &val, const Array<T> &array);
251 // </group>
252 //
253 // </group>
254 
255 
256 // Test if all elements in an array are the same.
257 template<class T> bool allSame (const Array<T> &a)
258  { return a.size() <= 1 || allEQ(*a.data(), a); }
259 
260 
261 // Element by element test for NaN or (In)finity.
262 // <group>
263 template<class T> LogicalArray isNaN (const Array<T> &array);
264 template<class T> LogicalArray isInf (const Array<T> &array);
265 template<class T> LogicalArray isFinite (const Array<T> &array);
266 // </group>
267 
268 //
269 // Element by element comparisons between an array and a scalar, which
270 // behaves as if it were a conformant array filled with the value "val."
271 // The result is a LogicalArray.
272 //
273 // <thrown>
274 // <li> ArrayConformanceError
275 // </thrown>
276 //
277 // <group>
278 template<class T> LogicalArray operator <= (const Array<T> &array, const T &val);
279 template<class T> LogicalArray operator <= (const T &val, const Array<T> &array);
280 template<class T> LogicalArray operator < (const Array<T> &array, const T &val);
281 template<class T> LogicalArray operator < (const T &val, const Array<T> &array);
282 template<class T> LogicalArray operator >= (const Array<T> &array, const T &val);
283 template<class T> LogicalArray operator >= (const T &val, const Array<T> &array);
284 template<class T> LogicalArray operator > (const Array<T> &array, const T &val);
285 template<class T> LogicalArray operator > (const T &val, const Array<T> &array);
286 template<class T> LogicalArray operator == (const Array<T> &array, const T &val);
287 template<class T> LogicalArray operator == (const T &val, const Array<T> &array);
288 template<class T> LogicalArray operator != (const Array<T> &array, const T &val);
289 template<class T> LogicalArray operator != (const T &val, const Array<T> &array);
290 template<class T> LogicalArray near (const Array<T> &array, const T &val,
291  double tol);
292 template<class T> LogicalArray near (const T &val, const Array<T> &array,
293  double tol);
294 template<class T> LogicalArray nearAbs (const Array<T> &array, const T &val,
295  double tol);
296 template<class T> LogicalArray nearAbs (const T &val, const Array<T> &array,
297  double tol);
298 //
299 // This only makes sense if the array element type is logical valued.
300 // <group>
301 template<class T> LogicalArray operator && (const Array<T> &array, const T &val);
302 template<class T> LogicalArray operator && (const T &val, const Array<T> &array);
303 template<class T> LogicalArray operator || (const Array<T> &array, const T &val);
304 template<class T> LogicalArray operator || (const T &val, const Array<T> &array);
305 // </group>
306 //
307 // </group>
308 
309 
310 //# With two arrays, they must both conform, and the result is done element
311 //# by element. For instance anyLE (a, b) implies that some element of a is
312 //# less than or equal to the corresponding element of b.
313 //# NB comparison between two zero-sized arrays is not defined (should it
314 //# throw an exception?).
315 
316 //
317 // Element by element comparisons between the "l" and "r" arrays. The result
318 // is true if the comparison is true for some element of the arrays.
319 //
320 // <thrown>
321 // <li> ArrayConformanceError
322 // </thrown>
323 //
324 // <group>
325 
326 template<class T> bool anyLE (const Array<T> &l, const Array<T> &r);
327 template<class T> bool anyLT (const Array<T> &l, const Array<T> &r);
328 template<class T> bool anyGE (const Array<T> &l, const Array<T> &r);
329 template<class T> bool anyGT (const Array<T> &l, const Array<T> &r);
330 template<class T> bool anyEQ (const Array<T> &l, const Array<T> &r);
331 template<class T> bool anyNE (const Array<T> &l, const Array<T> &r);
332 template<class T> bool anyNear (const Array<T> &l, const Array<T> &r,
333  double tol);
334 template<class T> bool anyNearAbs (const Array<T> &l, const Array<T> &r,
335  double tol);
336 //
337 // This only makes sense if the array element type is logical valued.
338 // <group>
339 template<class T> bool anyAND (const Array<T> &l, const Array<T> &r);
340 template<class T> bool anyOR (const Array<T> &l, const Array<T> &r);
341 // </group>
342 //
343 // </group>
344 
345 //
346 // Element by element comparisons between an array and a scalar, which
347 // behaves as if it were a conformant array filled with the value "val."
348 // The result is true if the comparison is true for some element of the array.
349 // At some point operators will be available that return masks where the
350 // comparison is true.
351 // <group>
352 
353 template<class T> bool anyLE (const Array<T> &array, const T &val);
354 template<class T> bool anyLE (const T &val, const Array<T> &array);
355 template<class T> bool anyLT (const Array<T> &array, const T &val);
356 template<class T> bool anyLT (const T &val, const Array<T> &array);
357 template<class T> bool anyGE (const Array<T> &array, const T &val);
358 template<class T> bool anyGE (const T &val, const Array<T> &array);
359 template<class T> bool anyGT (const Array<T> &array, const T &val);
360 template<class T> bool anyGT (const T &val, const Array<T> &array);
361 template<class T> bool anyEQ (const Array<T> &array, const T &val);
362 template<class T> bool anyEQ (const T &val, const Array<T> &array);
363 template<class T> bool anyNE (const Array<T> &array, const T &val);
364 template<class T> bool anyNE (const T &val, const Array<T> &array);
365 template<class T> bool anyNear (const Array<T> &array, const T &val, double tol);
366 template<class T> bool anyNear (const T &val, const Array<T> &array, double tol);
367 template<class T> bool anyNearAbs (const Array<T> &array, const T &val,
368  double tol);
369 template<class T> bool anyNearAbs (const T &val, const Array<T> &array,
370  double tol);
371 //
372 // This only makes sense if the array element type is logical valued.
373 // <group>
374 template<class T> bool anyAND (const Array<T> &array, const T &val);
375 template<class T> bool anyAND (const T &val, const Array<T> &array);
376 template<class T> bool anyOR (const Array<T> &array, const T &val);
377 template<class T> bool anyOR (const T &val, const Array<T> &array);
378 // </group>
379 //
380 // </group>
381 
382 
383 // Are all elements true?
384 template<typename Alloc>
385 inline bool allTrue (const Array<bool, Alloc>& array)
386  { return allEQ (array, true); }
387 
388 // Is any element true?
389 template<typename Alloc>
390 inline bool anyTrue (const Array<bool, Alloc>& array)
391  { return anyEQ (array, true); }
392 
393 // The same functions as above, but for selected axes.
394 Array<bool> partialAllTrue (const Array<bool>& array,
395  const IPosition& collapseAxes);
396 Array<bool> partialAnyTrue (const Array<bool>& array,
397  const IPosition& collapseAxes);
398 
399 // Determine the number of true or false elements.
400 // Note: it is meant for bool arrays, but can also be used for
401 // e.g. int arrays.
402 // <group>
403 
404 // Determine it for the full array.
405 // <group>
406 template<class T> size_t nfalse (const Array<T> &array);
407 template<class T> size_t ntrue (const Array<T> &array)
408  { return array.nelements() - nfalse(array); }
409 // </group>
410 
411 // The same functions as above, but determine ntrue and nfalse for the
412 // given axes only. The result is an array with a shape formed by the
413 // remaining axes.
414 // For example, for an array with shape [3,4,5], collapsing axis 0
415 // results in an array with shape [4,5] containing ntrue or nfalse for
416 // each X line.
417 // Summing for axes 0 and 2 results in an array with shape [4] containing
418 // ntrue or nfalse for each XZ plane.
419 // <group>
420 template<class T> Array<size_t> partialNTrue (const Array<T>& array,
421  const IPosition& collapseAxes);
422 template<class T> Array<size_t> partialNFalse (const Array<T>& array,
423  const IPosition& collapseAxes);
424 // </group>
425 
426 // </group>
427 
428 // </group>
429 } //# end of casacore namespace
430 
431 #include "ArrayMathBase.h"
432 
433 namespace casacore {
434 
435 // Logical functor to test if all elements are true
436 template<typename T> class AllFunc final : public ArrayFunctorBase<T,bool> {
437 public:
438  virtual bool operator() (const Array<T>& arr) const override { return allTrue(arr); }
439 };
440 
441 // Logical functor to test if any elements are true
442 template<typename T> class AnyFunc final : public ArrayFunctorBase<T,bool> {
443 public:
444  virtual bool operator() (const Array<T>& arr) const override { return anyTrue(arr); }
445 };
446 
447 // Logical functor to count the number of true elements
448 template<typename T, typename RES=size_t>
449 class NTrueFunc final : public ArrayFunctorBase<T,RES> {
450 public:
451  virtual RES operator() (const Array<T>& arr) const override { return ntrue(arr); }
452 };
453 
454 // Logical functor to count the number of false elements
455 template<typename T, typename RES=size_t>
456 class NFalseFunc final : public ArrayFunctorBase<T,RES> {
457 public:
458  virtual RES operator() (const Array<T>& arr) const override { return nfalse(arr); }
459 };
460 
461 } //# NAMESPACE CASACORE - END
462 
463 #include "ArrayLogical.tcc"
464 
465 #endif
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:118
bool allNearAbs(const C1 &l, const C2 &r, U tolerance)
Test if all elements of the containers are absolutely near each other.
Definition: StdLogical.h:54
bool allNear(const C1 &l, const C2 &r, U tolerance)
Test if all elements of the containers are relatively near each other.
Definition: StdLogical.h:49
size_t nelements() const
How many elements does this array have? Product of all axis lengths.
Definition: ArrayBase.h:103
Bool anyLT(const TableVector< T > &l, const TableVector< T > &r)
Definition: TabVecLogic.h:153
TableExprNode array(const TableExprNode &values, const TableExprNodeSet &shape)
Create an array of the given shape and fill it with the values.
Definition: ExprNode.h:1929
Bool near(const GaussianBeam &left, const GaussianBeam &other, const Double relWidthTol, const Quantity &absPaTol)
bool anyTrue(const Array< bool, Alloc > &array)
Is any element true?
Definition: ArrayLogical.h:390
LatticeExprNode ntrue(const LatticeExprNode &expr)
virtual bool operator()(const Array< T > &arr) const override
Definition: ArrayLogical.h:444
Logical functor to test if all elements are true.
Definition: ArrayLogical.h:436
bool operator==(const casacore_allocator< T, ALIGNMENT > &, const casacore_allocator< T, ALIGNMENT > &)
Definition: Allocator.h:129
Logical functor to test if any elements are true.
Definition: ArrayLogical.h:442
LatticeExprNode operator>=(const LatticeExprNode &left, const LatticeExprNode &right)
Bool anyNE(const TableVector< T > &l, const TableVector< T > &r)
Definition: TabVecLogic.h:173
bool allTrue(const Array< bool, Alloc > &array)
Are all elements true?
Definition: ArrayLogical.h:385
LatticeExprNode nfalse(const LatticeExprNode &expr)
LatticeExprNode operator!(const LatticeExprNode &expr)
bool allSame(const Array< T > &a)
Test if all elements in an array are the same.
Definition: ArrayLogical.h:257
TableExprNode isInf(const TableExprNode &node)
Definition: ExprNode.h:1626
virtual RES operator()(const Array< T > &arr) const override
Definition: ArrayLogical.h:458
Logical functor to count the number of false elements.
Definition: ArrayLogical.h:456
Basic class for math on Array objects.
Definition: ArrayMathBase.h:53
TableExprNode isFinite(const TableExprNode &node)
Function to test if a scalar or array is finite.
Definition: ExprNode.h:1630
Bool anyGE(const TableVector< T > &l, const TableVector< T > &r)
Definition: TabVecLogic.h:158
Bool anyGT(const TableVector< T > &l, const TableVector< T > &r)
Definition: TabVecLogic.h:163
LatticeExprNode operator>(const LatticeExprNode &left, const LatticeExprNode &right)
virtual RES operator()(const Array< T > &arr) const override
Definition: ArrayLogical.h:451
virtual bool operator()(const Array< T > &arr) const override
Definition: ArrayLogical.h:438
Bool anyEQ(const TableVector< T > &l, const TableVector< T > &r)
Definition: TabVecLogic.h:168
TableExprNode nearAbs(const TableExprNode &left, const TableExprNode &right)
Definition: ExprNode.h:1250
LatticeExprNode isNaN(const LatticeExprNode &expr)
Test if a value is a NaN.
size_t size() const
Definition: ArrayBase.h:105
bool operator!=(const casacore_allocator< T, ALIGNMENT > &, const casacore_allocator< T, ALIGNMENT > &)
Definition: Allocator.h:135
Logical functor to count the number of true elements.
Definition: ArrayLogical.h:449
Bool anyLE(const TableVector< T > &l, const TableVector< T > &r)
Element by element comparisons between the &quot;l&quot; and &quot;r&quot; table vectors.
Definition: TabVecLogic.h:148
LatticeExprNode operator||(const LatticeExprNode &left, const LatticeExprNode &right)
T * data()
Get a pointer to the beginning of the array.
Definition: Array.h:604
LatticeExprNode operator&&(const LatticeExprNode &left, const LatticeExprNode &right)
Logical binary operators.