casa  5.7.0-16
 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_H
29 #define CASA_ARRAYLOGICAL_H
30 
31 //# Includes
32 #include <casacore/casa/aips.h>
36 
37 namespace casacore { //# NAMESPACE CASACORE - BEGIN
38 
39 //# Forward Declarations
40 template <class T> class Vector;
41 template <class T> class Matrix;
42 template <class T> class Cube;
43 
44 // <summary>
45 // Logical operations for Arrays.
46 // </summary>
47 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="tArrayLogical">
48 //
49 // <prerequisite>
50 // <li> <linkto class=Array>Array</linkto>
51 // </prerequisite>
52 //
53 // <etymology>
54 // This file contains global functions which perform element by element logical
55 // operations on arrays.
56 // </etymology>
57 //
58 // <synopsis>
59 // These functions perform element by element logical operations on
60 // arrays. The two arrays must conform, except for allEQ which returns
61 // False if the arrays do not conform.
62 //
63 // There are two classes of functions. One class returns a LogicalArray.
64 // In these functions, the value of an element of the LogicalArray is
65 // the value of the logical operation applied to the corresponding elements
66 // of the input Arrays. The other class of functions returns a single
67 // Bool. The return value is True if the logical operation returns True for
68 // all elements of the input arrays for the "all" functions
69 // (e.g. allLE()), and returns True if the logical operation returns True for
70 // any elements of the input arrays for the "any" functions
71 // (e.g. anyLE()).
72 //
73 // For instance allLE (a, b) implies that every element of a is
74 // less than or equal to every element of b. Note that with this definition
75 // allLE (a, b) and allGE (a, b) can both be false (e.g. a = [1,0] b = [0,1]).
76 //
77 // <note role=caution> Comparison between two zero-sized arrays is not defined
78 // (should it throw an exception?).
79 // </note>
80 //
81 // </synopsis>
82 //
83 // <example>
84 // <srcblock>
85 // Vector<Int> a(10);
86 // Vector<Int> b(10);
87 // LogicalVector l(10);
88 // . . .
89 // l = a < b;
90 // </srcblock>
91 // This example sets the elements of l (a<b).
92 // The result of the comparison is a LogicalArray.
93 // </example>
94 //
95 // <example>
96 // <srcblock>
97 // Vector<Int> a(10);
98 // Vector<Int> b(10);
99 // Bool result;
100 // . . .
101 // result = allLT (a, b);
102 // </srcblock>
103 // This example sets result to True if, for all elements, a<b.
104 // </example>
105 //
106 // <motivation>
107 // One wants to be able to perform logical operations on arrays.
108 // </motivation>
109 //
110 // <todo asof="$DATE:$>
111 // <li> Reconsider where the origin of the returned LogicalArray should
112 // be located.
113 // </todo>
114 //
115 // <linkfrom anchor="Array logical operations" classes="Array Vector Matrix Cube">
116 // <here>Array logical operations</here> -- Logical operations for Arrays.
117 // </linkfrom>
118 //
119 // <group name="Array logical operations">
121 
122 // Determine if the comparisons between corresponding array elements yield True.
123 // <group>
124 template<typename T, typename CompareOperator>
125 bool arrayCompareAll (const Array<T>& left, const Array<T>& right,
126  CompareOperator op);
127 template<typename T, typename CompareOperator>
128 bool arrayCompareAll (const Array<T>& left, T right,
129  CompareOperator op);
130 template<typename T, typename CompareOperator>
131 bool arrayCompareAll (T left, const Array<T>& right,
132  CompareOperator op);
133 // </group>
134 
135 // Determine if the comparisons between corresponding array elements yield True.
136 // <group>
137 template<typename T, typename CompareOperator>
138 bool arrayCompareAny (const Array<T>& left, const Array<T>& right,
139  CompareOperator op);
140 template<typename T, typename CompareOperator>
141 bool arrayCompareAny (const Array<T>& left, T right,
142  CompareOperator op);
143 template<typename T, typename CompareOperator>
144 bool arrayCompareAny (T left, const Array<T>& right,
145  CompareOperator op);
146 // </group>
147 
148 //
149 // Element by element comparisons between the "l" and "r" arrays. The result
150 // is true only if the comparison is true for every element of the arrays.
151 //
152 // The operator forms of array logical operations which return a single Bool
153 // have been replaced by these "all" functions.
154 // The operator forms of array logical operations now return a LogicalArray.
155 //
156 // The arrays must conform except for allEQ, which will return False if the
157 // arrays have different shapes.
158 //
159 // <thrown>
160 // <li> ArrayConformanceError
161 // </thrown>
162 //
163 // <group>
164 template<class T> Bool allLE (const Array<T> &l, const Array<T> &r);
165 template<class T> Bool allLT (const Array<T> &l, const Array<T> &r);
166 template<class T> Bool allGE (const Array<T> &l, const Array<T> &r);
167 template<class T> Bool allGT (const Array<T> &l, const Array<T> &r);
168 template<class T> Bool allEQ (const Array<T> &l, const Array<T> &r);
169 template<class T> Bool allNE (const Array<T> &l, const Array<T> &r);
170 template<class T> Bool allNear (const Array<T> &l, const Array<T> &r,
171  Double tol);
172 template<class T> Bool allNearAbs (const Array<T> &l, const Array<T> &r,
173  Double tol);
174 //
175 // This only makes sense if the array element type is logical valued.
176 // <group>
177 template<class T> Bool allAND (const Array<T> &l, const Array<T> &r);
178 template<class T> Bool allOR (const Array<T> &l, const Array<T> &r);
179 // </group>
180 //
181 // </group>
182 
183 
184 //
185 // Element by element comparisons between the "l" and "r" arrays. The result
186 // is a LogicalArray.
187 // The arrays must conform or an exception is thrown.
188 //
189 // The Vector, Matrix and Cube version are present to bypass the problems
190 // due to the existence of automatic comparison inline templates in standard
191 // algorithm library, producing a single Bool value.
192 //
193 // <group>
194 template<class T> LogicalArray operator <= (const Array<T> &l,
195  const Array<T> &r);
196 template<class T> LogicalArray operator < (const Array<T> &l,
197  const Array<T> &r);
198 template<class T> LogicalArray operator >= (const Array<T> &l,
199  const Array<T> &r);
200 template<class T> LogicalArray operator > (const Array<T> &l,
201  const Array<T> &r);
202 template<class T> LogicalArray operator == (const Array<T> &l,
203  const Array<T> &r);
204 template<class T> LogicalArray operator != (const Array<T> &l,
205  const Array<T> &r);
206 
207 template<class T> LogicalArray near(const Array<T> &l, const Array<T> &r,
208  Double tol);
209 template<class T> LogicalArray nearAbs(const Array<T> &l, const Array<T> &r,
210  Double tol);
211 //
212 // This only makes sense if the array element type is logical valued.
213 // <group>
214 template<class T> LogicalArray operator && (const Array<T> &l, const Array<T> &r);
215 template<class T> LogicalArray operator || (const Array<T> &l, const Array<T> &r);
216 // </group>
217 //
218 // </group>
219 
220 
221 //
222 // Logical negation of an array. This only makes sense if the array
223 // element type is logical valued.
224 template<class T> LogicalArray operator ! (const Array<T> &l);
225 
226 
227 //
228 // Element by element comparisons between an array and a scalar, which
229 // behaves as if it were a conformant array filled with the value "val."
230 // The result is true only if the comparison is true for every element
231 // of the array.
232 // <group>
233 template<class T> Bool allLE (const Array<T> &array, const T &val);
234 template<class T> Bool allLE (const T &val, const Array<T> &array);
235 template<class T> Bool allLT (const Array<T> &array, const T &val);
236 template<class T> Bool allLT (const T &val, const Array<T> &array);
237 template<class T> Bool allGE (const Array<T> &array, const T &val);
238 template<class T> Bool allGE (const T &val, const Array<T> &array);
239 template<class T> Bool allGT (const Array<T> &array, const T &val);
240 template<class T> Bool allGT (const T &val, const Array<T> &array);
241 template<class T> Bool allEQ (const Array<T> &array, const T &val);
242 template<class T> Bool allEQ (const T &val, const Array<T> &array);
243 template<class T> Bool allNE (const Array<T> &array, const T &val);
244 template<class T> Bool allNE (const T &val, const Array<T> &array);
245 template<class T> Bool allNear (const Array<T> &array, const T &val, Double tol);
246 template<class T> Bool allNear (const T &val, const Array<T> &array, Double tol);
247 template<class T> Bool allNearAbs (const Array<T> &array, const T &val,
248  Double tol);
249 template<class T> Bool allNearAbs (const T &val, const Array<T> &array,
250  Double tol);
251 //
252 // This only makes sense if the array element type is logical valued.
253 // <group>
254 template<class T> Bool allAND (const Array<T> &array, const T &val);
255 template<class T> Bool allAND (const T &val, const Array<T> &array);
256 template<class T> Bool allOR (const Array<T> &array, const T &val);
257 template<class T> Bool allOR (const T &val, const Array<T> &array);
258 // </group>
259 //
260 // </group>
261 
262 
263 // Test if all elements in an array are the same.
264 template<class T> Bool allSame (const Array<T> &a)
265  { return a.size() <= 1 || allEQ(*a.data(), a); }
266 
267 
268 // Element by element test for NaN or (In)finity.
269 // <group>
270 template<class T> LogicalArray isNaN (const Array<T> &array);
271 template<class T> LogicalArray isInf (const Array<T> &array);
272 template<class T> LogicalArray isFinite (const Array<T> &array);
273 // </group>
274 
275 //
276 // Element by element comparisons between an array and a scalar, which
277 // behaves as if it were a conformant array filled with the value "val."
278 // The result is a LogicalArray.
279 //
280 // <thrown>
281 // <li> ArrayConformanceError
282 // </thrown>
283 //
284 // <group>
285 template<class T> LogicalArray operator <= (const Array<T> &array, const T &val);
286 template<class T> LogicalArray operator <= (const T &val, const Array<T> &array);
287 template<class T> LogicalArray operator < (const Array<T> &array, const T &val);
288 template<class T> LogicalArray operator < (const T &val, const Array<T> &array);
289 template<class T> LogicalArray operator >= (const Array<T> &array, const T &val);
290 template<class T> LogicalArray operator >= (const T &val, const Array<T> &array);
291 template<class T> LogicalArray operator > (const Array<T> &array, const T &val);
292 template<class T> LogicalArray operator > (const T &val, const Array<T> &array);
293 template<class T> LogicalArray operator == (const Array<T> &array, const T &val);
294 template<class T> LogicalArray operator == (const T &val, const Array<T> &array);
295 template<class T> LogicalArray operator != (const Array<T> &array, const T &val);
296 template<class T> LogicalArray operator != (const T &val, const Array<T> &array);
297 template<class T> LogicalArray near (const Array<T> &array, const T &val,
298  Double tol);
299 template<class T> LogicalArray near (const T &val, const Array<T> &array,
300  Double tol);
301 template<class T> LogicalArray nearAbs (const Array<T> &array, const T &val,
302  Double tol);
303 template<class T> LogicalArray nearAbs (const T &val, const Array<T> &array,
304  Double tol);
305 //
306 // This only makes sense if the array element type is logical valued.
307 // <group>
308 template<class T> LogicalArray operator && (const Array<T> &array, const T &val);
309 template<class T> LogicalArray operator && (const T &val, const Array<T> &array);
310 template<class T> LogicalArray operator || (const Array<T> &array, const T &val);
311 template<class T> LogicalArray operator || (const T &val, const Array<T> &array);
312 // </group>
313 //
314 // </group>
315 
316 
317 //# With two arrays, they must both conform, and the result is done element
318 //# by element. For instance anyLE (a, b) implies that some element of a is
319 //# less than or equal to the corresponding element of b.
320 //# NB comparison between two zero-sized arrays is not defined (should it
321 //# throw an exception?).
322 
323 //
324 // Element by element comparisons between the "l" and "r" arrays. The result
325 // is true if the comparison is true for some element of the arrays.
326 //
327 // <thrown>
328 // <li> ArrayConformanceError
329 // </thrown>
330 //
331 // <group>
332 
333 template<class T> Bool anyLE (const Array<T> &l, const Array<T> &r);
334 template<class T> Bool anyLT (const Array<T> &l, const Array<T> &r);
335 template<class T> Bool anyGE (const Array<T> &l, const Array<T> &r);
336 template<class T> Bool anyGT (const Array<T> &l, const Array<T> &r);
337 template<class T> Bool anyEQ (const Array<T> &l, const Array<T> &r);
338 template<class T> Bool anyNE (const Array<T> &l, const Array<T> &r);
339 template<class T> Bool anyNear (const Array<T> &l, const Array<T> &r,
340  Double tol);
341 template<class T> Bool anyNearAbs (const Array<T> &l, const Array<T> &r,
342  Double tol);
343 //
344 // This only makes sense if the array element type is logical valued.
345 // <group>
346 template<class T> Bool anyAND (const Array<T> &l, const Array<T> &r);
347 template<class T> Bool anyOR (const Array<T> &l, const Array<T> &r);
348 // </group>
349 //
350 // </group>
351 
352 //
353 // Element by element comparisons between an array and a scalar, which
354 // behaves as if it were a conformant array filled with the value "val."
355 // The result is true if the comparison is true for some element of the array.
356 // At some point operators will be available that return masks where the
357 // comparison is true.
358 // <group>
359 
360 template<class T> Bool anyLE (const Array<T> &array, const T &val);
361 template<class T> Bool anyLE (const T &val, const Array<T> &array);
362 template<class T> Bool anyLT (const Array<T> &array, const T &val);
363 template<class T> Bool anyLT (const T &val, const Array<T> &array);
364 template<class T> Bool anyGE (const Array<T> &array, const T &val);
365 template<class T> Bool anyGE (const T &val, const Array<T> &array);
366 template<class T> Bool anyGT (const Array<T> &array, const T &val);
367 template<class T> Bool anyGT (const T &val, const Array<T> &array);
368 template<class T> Bool anyEQ (const Array<T> &array, const T &val);
369 template<class T> Bool anyEQ (const T &val, const Array<T> &array);
370 template<class T> Bool anyNE (const Array<T> &array, const T &val);
371 template<class T> Bool anyNE (const T &val, const Array<T> &array);
372 template<class T> Bool anyNear (const Array<T> &array, const T &val, Double tol);
373 template<class T> Bool anyNear (const T &val, const Array<T> &array, Double tol);
374 template<class T> Bool anyNearAbs (const Array<T> &array, const T &val,
375  Double tol);
376 template<class T> Bool anyNearAbs (const T &val, const Array<T> &array,
377  Double tol);
378 //
379 // This only makes sense if the array element type is logical valued.
380 // <group>
381 template<class T> Bool anyAND (const Array<T> &array, const T &val);
382 template<class T> Bool anyAND (const T &val, const Array<T> &array);
383 template<class T> Bool anyOR (const Array<T> &array, const T &val);
384 template<class T> Bool anyOR (const T &val, const Array<T> &array);
385 // </group>
386 //
387 // </group>
388 
389 
390 // Are all elements true?
391 inline Bool allTrue (const Array<Bool>& array)
392  { return allEQ (array, True); }
393 
394 // Is any element true?
395 inline Bool anyTrue (const Array<Bool>& array)
396  { return anyEQ (array, True); }
397 
398 // The same functions as above, but for selected axes.
399 Array<Bool> partialAllTrue (const Array<Bool>& array,
400  const IPosition& collapseAxes);
401 Array<Bool> partialAnyTrue (const Array<Bool>& array,
402  const IPosition& collapseAxes);
403 
404 // Determine the number of true or false elements.
405 // Note: it is meant for Bool arrays, but can also be used for
406 // e.g. Int arrays.
407 // <group>
408 
409 // Determine it for the full array.
410 // <group>
411 template<class T> size_t nfalse (const Array<T> &array);
412 template<class T> size_t ntrue (const Array<T> &array)
413  { return array.nelements() - nfalse(array); }
414 // </group>
415 
416 // The same functions as above, but determine ntrue and nfalse for the
417 // given axes only. The result is an array with a shape formed by the
418 // remaining axes.
419 // For example, for an array with shape [3,4,5], collapsing axis 0
420 // results in an array with shape [4,5] containing ntrue or nfalse for
421 // each X line.
422 // Summing for axes 0 and 2 results in an array with shape [4] containing
423 // ntrue or nfalse for each XZ plane.
424 // <group>
425 template<class T> Array<uInt> partialNTrue (const Array<T>& array,
426  const IPosition& collapseAxes);
427 template<class T> Array<uInt> partialNFalse (const Array<T>& array,
428  const IPosition& collapseAxes);
429 // </group>
430 
431 // </group>
432 
433 // </group>
434 
435 // Define logical Functors.
436 // <group>
437 template<typename T> class AllFunc : public ArrayFunctorBase<T,Bool> {
438 public:
439  virtual ~AllFunc() {}
440  virtual Bool operator() (const Array<T>& arr) const { return allTrue(arr); }
441 };
442 template<typename T> class AnyFunc : public ArrayFunctorBase<T,Bool> {
443 public:
444  virtual ~AnyFunc() {}
445  virtual Bool operator() (const Array<T>& arr) const { return anyTrue(arr); }
446 };
447 
448 template<typename T, typename RES=size_t>
449 class NTrueFunc : public ArrayFunctorBase<T,RES> {
450 public:
451  virtual ~NTrueFunc() {}
452  virtual RES operator() (const Array<T>& arr) const { return ntrue(arr); }
453 };
454 template<typename T, typename RES=size_t>
455 class NFalseFunc : public ArrayFunctorBase<T,RES> {
456 public:
457  virtual ~NFalseFunc() {}
458  virtual RES operator() (const Array<T>& arr) const { return nfalse(arr); }
459 };
460 // </group>
461 
462 
463 } //# NAMESPACE CASACORE - END
464 
465 #ifndef CASACORE_NO_AUTO_TEMPLATES
466 #include <casacore/casa/Arrays/ArrayLogical.tcc>
467 #endif //# CASACORE_NO_AUTO_TEMPLATES
468 #endif
A Vector of integers, for indexing into Array&lt;T&gt; objects.
Definition: IPosition.h:119
std::vector< double > Vector
Definition: ds9context.h:24
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:99
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:1886
Bool near(const GaussianBeam &left, const GaussianBeam &other, const Double relWidthTol, const Quantity &absPaTol)
LatticeExprNode ntrue(const LatticeExprNode &expr)
LatticeExprNode operator!=(const LatticeExprNode &left, const LatticeExprNode &right)
Bool allTrue(const Array< Bool > &array)
Are all elements true?
Definition: ArrayLogical.h:391
Define logical Functors.
Definition: ArrayLogical.h:437
virtual RES operator()(const Array< T > &arr) const
Definition: ArrayLogical.h:458
virtual RES operator()(const Array< T > &arr) const
Definition: ArrayLogical.h:452
LatticeExprNode operator>=(const LatticeExprNode &left, const LatticeExprNode &right)
Bool anyNE(const TableVector< T > &l, const TableVector< T > &r)
Definition: TabVecLogic.h:173
T * data()
Get a pointer to the beginning of the array.
Definition: Array.h:597
LatticeExprNode nfalse(const LatticeExprNode &expr)
LatticeExprNode operator!(const LatticeExprNode &expr)
TableExprNode isInf(const TableExprNode &node)
Definition: ExprNode.h:1583
double Double
Definition: aipstype.h:55
virtual Bool operator()(const Array< T > &arr) const
Definition: ArrayLogical.h:445
Basic class for math on Array objects.
Definition: ArrayMathBase.h:57
TableExprNode isFinite(const TableExprNode &node)
Function to test if a scalar or array is finite.
Definition: ExprNode.h:1587
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
Bool anyGE(const TableVector< T > &l, const TableVector< T > &r)
Definition: TabVecLogic.h:158
virtual ~AnyFunc()
Definition: ArrayLogical.h:444
Bool anyGT(const TableVector< T > &l, const TableVector< T > &r)
Definition: TabVecLogic.h:163
template &lt;class T, class U&gt; class vector;
Definition: MSFlagger.h:37
LatticeExprNode operator>(const LatticeExprNode &left, const LatticeExprNode &right)
virtual ~AllFunc()
Definition: ArrayLogical.h:439
Bool allSame(const Array< T > &a)
Test if all elements in an array are the same.
Definition: ArrayLogical.h:264
Bool anyEQ(const TableVector< T > &l, const TableVector< T > &r)
Definition: TabVecLogic.h:168
Bool anyTrue(const Array< Bool > &array)
Is any element true?
Definition: ArrayLogical.h:395
TableExprNode nearAbs(const TableExprNode &left, const TableExprNode &right)
Definition: ExprNode.h:1207
Bool operator==(const MVTime &lh, const MVTime &rh)
is equal operator, uses operator Double which returns days
Definition: MVTime.h:465
LatticeExprNode isNaN(const LatticeExprNode &expr)
Test if a value is a NaN.
size_t size() const
Definition: ArrayBase.h:101
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
const Bool True
Definition: aipstype.h:43
LatticeExprNode operator||(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode operator&&(const LatticeExprNode &left, const LatticeExprNode &right)
Logical binary operators.
virtual Bool operator()(const Array< T > &arr) const
Definition: ArrayLogical.h:440
#define casacore
&lt;X11/Intrinsic.h&gt; #defines true, false, casacore::Bool, and String.
Definition: X11Intrinsic.h:42