1#ifndef ELEMENT_FUNCTIONS_H
2#define ELEMENT_FUNCTIONS_H
11inline bool near(
unsigned val1,
unsigned val2,
double tol) {
13 return (val1 == val2);
17 }
else if (val1 > val2) {
18 return (
double(val1 - val2) <= tol * std::max(val1, val2));
20 return (
double(val2 - val1) <= tol * std::max(val1, val2));
24inline bool near(
int val1,
int val2,
double tol) {
26 return (val1 == val2);
31 if ((0 < val1) != (0 < val2)) {
34 const int aval1 = std::abs(val1);
35 const int aval2 = std::abs(val2);
36 return (
double(aval1 - aval2) <= tol *
double(std::max(aval1, aval2)));
39inline bool near(
float val1,
float val2,
double tol) {
41 return (val1 == val2);
47 return (std::abs(val2) <= (1 + tol) * std::numeric_limits<float>::min());
48 }
else if (val2 == 0) {
49 return (std::abs(val1) <= (1 + tol) * std::numeric_limits<float>::min());
51 if ((0 < val1) != (0 < val2)) {
54 return (std::abs(val1 - val2) <= tol * std::max(std::abs(val1), std::abs(val2)));
57inline bool near(
double val1,
double val2,
double tol) {
59 return (val1 == val2);
65 return (std::abs(val2) <= (1 + tol) * std::numeric_limits<double>::min());
66 }
else if (val2 == 0) {
67 return (std::abs(val1) <= (1 + tol) * std::numeric_limits<double>::min());
69 if ((0 < val1) != (0 < val2)) {
72 return (std::abs(val1 - val2) <= tol * std::max(std::abs(val1), std::abs(val2)));
75inline bool near(
float val1,
double val2,
double tol) {
return near(
double(val1), val2, tol); }
77inline bool near(
double val1,
float val2,
double tol) {
return near(val1,
double(val2), tol); }
79inline bool near(
const std::complex<float> &val1,
const std::complex<float> &val2,
80 double tol = 1.0e-5) {
81 if (tol <= 0)
return val1 == val2;
82 if (val1 == val2)
return true;
83 if (
near(val1.real(), val2.real(), tol) &&
near(val1.imag(), val2.imag(), tol))
return true;
84 float aval1(std::abs(val1)), aval2(std::abs(val2));
86 return aval2 <= (1 + tol) * std::numeric_limits<float>::min();
88 return aval1 <= (1 + tol) * std::numeric_limits<float>::min();
89 std::complex<double> dval(val1);
90 dval -= std::complex<double>(val2);
91 return std::abs(dval) <= tol * (aval1 < aval2 ? aval2 : aval1);
94inline bool near(
const std::complex<double> &val1,
const std::complex<double> &val2,
95 double tol = 1.0e-13) {
96 if (tol <= 0)
return val1 == val2;
97 if (val1 == val2)
return true;
98 if (std::abs(val1) == 0)
99 return std::abs(val2) <= (1 + tol) * std::numeric_limits<double>::min();
100 else if (std::abs(val2) == 0)
101 return std::abs(val1) <= (1 + tol) * std::numeric_limits<double>::min();
102 double aval1(std::abs(val1)), aval2(std::abs(val2));
103 return std::abs(val1 - val2) <= tol * (aval1 < aval2 ? aval2 : aval1);
106inline bool nearAbs(
const std::complex<float> &val1,
const std::complex<float> &val2,
107 double tol = 1.0e-5) {
108 return std::abs(val2 - val1) <= tol;
111inline bool nearAbs(
const std::complex<double> &val1,
const std::complex<double> &val2,
112 double tol = 1.0e-13) {
113 return std::abs(val2 - val1) <= tol;
116inline bool nearAbs(
unsigned val1,
unsigned val2,
double tol) {
119 }
else if (val1 > val2) {
120 return (tol >=
double(val1 - val2));
122 return (tol >=
double(val2 - val1));
126inline bool nearAbs(
int val1,
int val2,
double tol) {
127 return (tol >=
double(std::abs(val2 - val1)));
130inline bool nearAbs(
float val1,
float val2,
double tol) {
131 return (tol >=
double(std::abs(val2 - val1)));
134inline bool nearAbs(
double val1,
double val2,
double tol) {
return (tol >= std::abs(val2 - val1)); }
140template <
typename InputIterator1,
typename InputIterator2,
typename CompareOperator>
141inline bool compareAll(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
142 CompareOperator op) {
143 for (; first1 != last1; ++first1, ++first2) {
144 if (!op(*first1, *first2))
return false;
151template <
typename InputIterator1,
typename T,
typename CompareOperator>
153 CompareOperator op) {
154 for (; first1 != last1; ++first1) {
155 if (!op(left, *first1))
return false;
162template <
typename InputIterator1,
typename T,
typename CompareOperator>
164 CompareOperator op) {
165 for (; first1 != last1; ++first1) {
166 if (!op(*first1, right))
return false;
176template <
typename InputIterator1,
typename InputIterator2,
typename CompareOperator>
177inline bool compareAny(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
178 CompareOperator op) {
179 for (; first1 != last1; ++first1, ++first2) {
180 if (op(*first1, *first2))
return true;
187template <
typename InputIterator1,
typename T,
typename CompareOperator>
189 CompareOperator op) {
190 for (; first1 != last1; ++first1) {
191 if (op(left, *first1))
return true;
198template <
typename InputIterator1,
typename T,
typename CompareOperator>
200 CompareOperator op) {
201 for (; first1 != last1; ++first1) {
202 if (op(*first1, right))
return true;
211template <
typename T,
typename Accum = T>
231 std::complex<T>
operator()(std::complex<T> left, std::complex<T> right)
const {
232 return left + ((right.real() -
itsBase.real()) * (right.real() -
itsBase.real()) +
233 (right.imag() -
itsBase.imag()) * (right.imag() -
itsBase.imag()));
241bool isnan(
const std::complex<T> &val) {
242 return std::isnan(val.real()) || std::isnan(val.imag());
246bool isinf(
const std::complex<T> &val) {
247 return std::isinf(val.real()) || std::isinf(val.imag());
252 return std::isfinite(val.real()) || std::isfinite(val.imag());
257 if (r != 0 && (x < 0) != (y < 0)) r += y;
260inline long long floormod(
long long x,
long long y) {
262 if (r != 0 && (x < 0) != (y < 0)) r += y;
266 float r = std::fmod(x, y);
267 if (r != 0 && (x < 0) != (y < 0)) r += y;
271 double r = std::fmod(x, y);
272 if (r != 0 && (x < 0) != (y < 0)) r += y;
276template <
class T,
class F>
278 out =
static_cast<T
>(in);
281inline void convertScalar(std::complex<float> &out, std::complex<double> in) {
282 out = std::complex<float>(in.real(), in.imag());
bool compareAnyLeft(InputIterator1 first1, InputIterator1 last1, T left, CompareOperator op)
For use with a constant left value.
bool near(unsigned val1, unsigned val2, double tol)
bool compareAll(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, CompareOperator op)
Define a function to compare all elements of two sequences.
bool isfinite(const std::complex< T > &val)
bool compareAnyRight(InputIterator1 first1, InputIterator1 last1, T right, CompareOperator op)
For use with a constant right value.
void convertScalar(T &out, F in)
int floormod(int x, int y)
bool compareAllLeft(InputIterator1 first1, InputIterator1 last1, T left, CompareOperator op)
For use with a constant left value.
bool nearAbs(const std::complex< float > &val1, const std::complex< float > &val2, double tol=1.0e-5)
bool compareAllRight(InputIterator1 first1, InputIterator1 last1, T right, CompareOperator op)
For use with a constant right value.
bool isinf(const std::complex< T > &val)
bool isnan(const std::complex< T > &val)
bool compareAny(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, CompareOperator op)
Define a function to compare all elements of two sequences.
For temporary backward namespace compatibility, use casa as alias for casacore.
Define real & complex conjugation for non-complex types and put comparisons into std namespace.
std::complex< T > itsBase
SumSqrDiff(std::complex< T > base)
std::complex< T > operator()(std::complex< T > left, std::complex< T > right) const
Accum operator()(Accum left, T right) const