//# ArrayLogical.h: Element by element logical operations on arrays. //# Copyright (C) 1993,1994,1995,1999,2001 //# Associated Universities, Inc. Washington DC, USA. //# //# This library is free software; you can redistribute it and/or modify it //# under the terms of the GNU Library General Public License as published by //# the Free Software Foundation; either version 2 of the License, or (at your //# option) any later version. //# //# This library is distributed in the hope that it will be useful, but WITHOUT //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public //# License for more details. //# //# You should have received a copy of the GNU Library General Public License //# along with this library; if not, write to the Free Software Foundation, //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. //# //# Correspondence concerning AIPS++ should be addressed as follows: //# Internet email: aips2-request@nrao.edu. //# Postal address: AIPS++ Project Office //# National Radio Astronomy Observatory //# 520 Edgemont Road //# Charlottesville, VA 22903-2475 USA //# //# $Id$ #ifndef CASA_ARRAYLOGICAL_2_H #define CASA_ARRAYLOGICAL_2_H //# Includes #include "ArrayFwd.h" #include "IPosition.h" namespace casacore { //# NAMESPACE CASACORE - BEGIN // // Logical operations for Arrays. // // // // //
  • Array // // // // This file contains global functions which perform element by element logical // operations on arrays. // // // // These functions perform element by element logical operations on // arrays. The two arrays must conform, except for allEQ which returns // false if the arrays do not conform. // // There are two classes of functions. One class returns a LogicalArray. // In these functions, the value of an element of the LogicalArray is // the value of the logical operation applied to the corresponding elements // of the input Arrays. The other class of functions returns a single // bool. The return value is true if the logical operation returns true for // all elements of the input arrays for the "all" functions // (e.g. allLE()), and returns true if the logical operation returns true for // any elements of the input arrays for the "any" functions // (e.g. anyLE()). // // For instance allLE (a, b) implies that every element of a is // less than or equal to every element of b. Note that with this definition // allLE (a, b) and allGE (a, b) can both be false (e.g. a = [1,0] b = [0,1]). // // Comparison between two zero-sized arrays is not defined // (should it throw an exception?). // // // // // // // Vector a(10); // Vector b(10); // LogicalVector l(10); // . . . // l = a < b; // // This example sets the elements of l (a // // // // Vector a(10); // Vector b(10); // bool result; // . . . // result = allLT (a, b); // // This example sets result to true if, for all elements, a // // // One wants to be able to perform logical operations on arrays. // // // // Array logical operations -- Logical operations for Arrays. // // // // Determine if the comparisons between corresponding array elements yield true. // template bool arrayCompareAll (const Array& left, const Array& right, CompareOperator op); template bool arrayCompareAll (const Array& left, T right, CompareOperator op); template bool arrayCompareAll (T left, const Array& right, CompareOperator op); // // Determine if the comparisons between corresponding array elements yield true. // template bool arrayCompareAny (const Array& left, const Array& right, CompareOperator op); template bool arrayCompareAny (const Array& left, T right, CompareOperator op); template bool arrayCompareAny (T left, const Array& right, CompareOperator op); // // // Element by element comparisons between the "l" and "r" arrays. The result // is true only if the comparison is true for every element of the arrays. // // The operator forms of array logical operations which return a single bool // have been replaced by these "all" functions. // The operator forms of array logical operations now return a LogicalArray. // // The arrays must conform except for allEQ, which will return false if the // arrays have different shapes. // // //
  • ArrayConformanceError // // // template bool allLE (const Array &l, const Array &r); template bool allLT (const Array &l, const Array &r); template bool allGE (const Array &l, const Array &r); template bool allGT (const Array &l, const Array &r); template bool allEQ (const Array &l, const Array &r); template bool allNE (const Array &l, const Array &r); template bool allNear (const Array &l, const Array &r, double tol); template bool allNearAbs (const Array &l, const Array &r, double tol); // // This only makes sense if the array element type is logical valued. // template bool allAND (const Array &l, const Array &r); template bool allOR (const Array &l, const Array &r); // // // // // Element by element comparisons between the "l" and "r" arrays. The result // is a LogicalArray. // The arrays must conform or an exception is thrown. // // The Vector, Matrix and Cube version are present to bypass the problems // due to the existence of automatic comparison inline templates in standard // algorithm library, producing a single bool value. // // template LogicalArray operator <= (const Array &l, const Array &r); template LogicalArray operator < (const Array &l, const Array &r); template LogicalArray operator >= (const Array &l, const Array &r); template LogicalArray operator > (const Array &l, const Array &r); template LogicalArray operator == (const Array &l, const Array &r); template LogicalArray operator != (const Array &l, const Array &r); template LogicalArray near(const Array &l, const Array &r, double tol); template LogicalArray nearAbs(const Array &l, const Array &r, double tol); // // This only makes sense if the array element type is logical valued. // template LogicalArray operator && (const Array &l, const Array &r); template LogicalArray operator || (const Array &l, const Array &r); // // // // // Logical negation of an array. This only makes sense if the array // element type is logical valued. template LogicalArray operator ! (const Array &l); // // Element by element comparisons between an array and a scalar, which // behaves as if it were a conformant array filled with the value "val." // The result is true only if the comparison is true for every element // of the array. // template bool allLE (const Array &array, const T &val); template bool allLE (const T &val, const Array &array); template bool allLT (const Array &array, const T &val); template bool allLT (const T &val, const Array &array); template bool allGE (const Array &array, const T &val); template bool allGE (const T &val, const Array &array); template bool allGT (const Array &array, const T &val); template bool allGT (const T &val, const Array &array); template bool allEQ (const Array &array, const T &val); template bool allEQ (const T &val, const Array &array); template bool allNE (const Array &array, const T &val); template bool allNE (const T &val, const Array &array); template bool allNear (const Array &array, const T &val, double tol); template bool allNear (const T &val, const Array &array, double tol); template bool allNearAbs (const Array &array, const T &val, double tol); template bool allNearAbs (const T &val, const Array &array, double tol); // // This only makes sense if the array element type is logical valued. // template bool allAND (const Array &array, const T &val); template bool allAND (const T &val, const Array &array); template bool allOR (const Array &array, const T &val); template bool allOR (const T &val, const Array &array); // // // // Test if all elements in an array are the same. template bool allSame (const Array &a) { return a.size() <= 1 || allEQ(*a.data(), a); } // Element by element test for NaN or (In)finity. // template LogicalArray isNaN (const Array &array); template LogicalArray isInf (const Array &array); template LogicalArray isFinite (const Array &array); // // // Element by element comparisons between an array and a scalar, which // behaves as if it were a conformant array filled with the value "val." // The result is a LogicalArray. // // //
  • ArrayConformanceError // // // template LogicalArray operator <= (const Array &array, const T &val); template LogicalArray operator <= (const T &val, const Array &array); template LogicalArray operator < (const Array &array, const T &val); template LogicalArray operator < (const T &val, const Array &array); template LogicalArray operator >= (const Array &array, const T &val); template LogicalArray operator >= (const T &val, const Array &array); template LogicalArray operator > (const Array &array, const T &val); template LogicalArray operator > (const T &val, const Array &array); template LogicalArray operator == (const Array &array, const T &val); template LogicalArray operator == (const T &val, const Array &array); template LogicalArray operator != (const Array &array, const T &val); template LogicalArray operator != (const T &val, const Array &array); template LogicalArray near (const Array &array, const T &val, double tol); template LogicalArray near (const T &val, const Array &array, double tol); template LogicalArray nearAbs (const Array &array, const T &val, double tol); template LogicalArray nearAbs (const T &val, const Array &array, double tol); // // This only makes sense if the array element type is logical valued. // template LogicalArray operator && (const Array &array, const T &val); template LogicalArray operator && (const T &val, const Array &array); template LogicalArray operator || (const Array &array, const T &val); template LogicalArray operator || (const T &val, const Array &array); // // // //# With two arrays, they must both conform, and the result is done element //# by element. For instance anyLE (a, b) implies that some element of a is //# less than or equal to the corresponding element of b. //# NB comparison between two zero-sized arrays is not defined (should it //# throw an exception?). // // Element by element comparisons between the "l" and "r" arrays. The result // is true if the comparison is true for some element of the arrays. // // //
  • ArrayConformanceError // // // template bool anyLE (const Array &l, const Array &r); template bool anyLT (const Array &l, const Array &r); template bool anyGE (const Array &l, const Array &r); template bool anyGT (const Array &l, const Array &r); template bool anyEQ (const Array &l, const Array &r); template bool anyNE (const Array &l, const Array &r); template bool anyNear (const Array &l, const Array &r, double tol); template bool anyNearAbs (const Array &l, const Array &r, double tol); // // This only makes sense if the array element type is logical valued. // template bool anyAND (const Array &l, const Array &r); template bool anyOR (const Array &l, const Array &r); // // // // // Element by element comparisons between an array and a scalar, which // behaves as if it were a conformant array filled with the value "val." // The result is true if the comparison is true for some element of the array. // At some point operators will be available that return masks where the // comparison is true. // template bool anyLE (const Array &array, const T &val); template bool anyLE (const T &val, const Array &array); template bool anyLT (const Array &array, const T &val); template bool anyLT (const T &val, const Array &array); template bool anyGE (const Array &array, const T &val); template bool anyGE (const T &val, const Array &array); template bool anyGT (const Array &array, const T &val); template bool anyGT (const T &val, const Array &array); template bool anyEQ (const Array &array, const T &val); template bool anyEQ (const T &val, const Array &array); template bool anyNE (const Array &array, const T &val); template bool anyNE (const T &val, const Array &array); template bool anyNear (const Array &array, const T &val, double tol); template bool anyNear (const T &val, const Array &array, double tol); template bool anyNearAbs (const Array &array, const T &val, double tol); template bool anyNearAbs (const T &val, const Array &array, double tol); // // This only makes sense if the array element type is logical valued. // template bool anyAND (const Array &array, const T &val); template bool anyAND (const T &val, const Array &array); template bool anyOR (const Array &array, const T &val); template bool anyOR (const T &val, const Array &array); // // // // Are all elements true? template inline bool allTrue (const Array& array) { return allEQ (array, true); } // Is any element true? template inline bool anyTrue (const Array& array) { return anyEQ (array, true); } // The same functions as above, but for selected axes. Array partialAllTrue (const Array& array, const IPosition& collapseAxes); Array partialAnyTrue (const Array& array, const IPosition& collapseAxes); // Determine the number of true or false elements. // Note: it is meant for bool arrays, but can also be used for // e.g. int arrays. // // Determine it for the full array. // template size_t nfalse (const Array &array); template size_t ntrue (const Array &array) { return array.nelements() - nfalse(array); } // // The same functions as above, but determine ntrue and nfalse for the // given axes only. The result is an array with a shape formed by the // remaining axes. // For example, for an array with shape [3,4,5], collapsing axis 0 // results in an array with shape [4,5] containing ntrue or nfalse for // each X line. // Summing for axes 0 and 2 results in an array with shape [4] containing // ntrue or nfalse for each XZ plane. // template Array partialNTrue (const Array& array, const IPosition& collapseAxes); template Array partialNFalse (const Array& array, const IPosition& collapseAxes); // // // } //# end of casacore namespace #include "ArrayMathBase.h" namespace casacore { // Logical functor to test if all elements are true template class AllFunc final : public ArrayFunctorBase { public: virtual bool operator() (const Array& arr) const override { return allTrue(arr); } }; // Logical functor to test if any elements are true template class AnyFunc final : public ArrayFunctorBase { public: virtual bool operator() (const Array& arr) const override { return anyTrue(arr); } }; // Logical functor to count the number of true elements template class NTrueFunc final : public ArrayFunctorBase { public: virtual RES operator() (const Array& arr) const override { return ntrue(arr); } }; // Logical functor to count the number of false elements template class NFalseFunc final : public ArrayFunctorBase { public: virtual RES operator() (const Array& arr) const override { return nfalse(arr); } }; } //# NAMESPACE CASACORE - END #include "ArrayLogical.tcc" #endif