1#ifndef ELEMENT_FUNCTIONS_H
2#define ELEMENT_FUNCTIONS_H
9namespace arrays_internal {
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());
50 return (std::abs(val1) <= (1+tol)*std::numeric_limits<float>::min());
52 if ((0<val1) != (0<val2)) {
55 return (std::abs(val1-val2) <= tol*std::max(std::abs(val1), std::abs(val2)));
58inline bool near(
double val1,
double val2,
double tol) {
60 return (val1 == val2);
66 return (std::abs(val2) <= (1+tol)*std::numeric_limits<double>::min());
69 return (std::abs(val1) <= (1+tol)*std::numeric_limits<double>::min());
71 if ((0<val1) != (0<val2)) {
74 return (std::abs(val1-val2) <= tol*std::max(std::abs(val1),std::abs(val2)));
77inline bool near(
float val1,
double val2,
double tol) {
78 return near(
double(val1), val2, tol);
81inline bool near(
double val1,
float val2,
double tol) {
82 return near(val1,
double(val2), tol);
85inline bool near(
const std::complex<float> &val1,
const std::complex<float> &val2,
double tol=1.0e-5)
87 if (tol <= 0)
return val1 == val2;
88 if (val1 == val2)
return true;
89 if (
near(val1.real(), val2.real(), tol) &&
90 near(val1.imag(), val2.imag(), tol))
return true;
91 float aval1(std::abs(val1)), aval2(std::abs(val2));
92 if (aval1 == 0)
return aval2 <= (1+tol)*std::numeric_limits<float>::min();
93 else if (aval2 == 0)
return aval1 <= (1+tol)*std::numeric_limits<float>::min();
94 std::complex<double> dval(val1);
95 dval -= std::complex<double>(val2);
96 return std::abs(dval) <= tol * (aval1 < aval2 ? aval2 : aval1);
99inline bool near(
const std::complex<double> &val1,
const std::complex<double> &val2,
double tol=1.0e-13)
101 if (tol <= 0)
return val1 == val2;
102 if (val1 == val2)
return true;
103 if (std::abs(val1) == 0)
return std::abs(val2) <= (1+tol)*std::numeric_limits<double>::min();
104 else if (std::abs(val2) == 0)
return std::abs(val1) <= (1+tol)*std::numeric_limits<double>::min();
105 double aval1(std::abs(val1)), aval2(std::abs(val2));
106 return std::abs(val1-val2) <= tol * (aval1 < aval2 ? aval2 : aval1);
109inline bool nearAbs(
const std::complex<float> &val1,
const std::complex<float> &val2,
double tol=1.0e-5)
111 return std::abs(val2 - val1) <= tol;
114inline bool nearAbs(
const std::complex<double> &val1,
const std::complex<double> &val2,
double tol=1.0e-13)
116 return std::abs(val2 - val1) <= tol;
119inline bool nearAbs(
unsigned val1,
unsigned val2,
double tol) {
122 }
else if (val1 > val2) {
123 return (tol >=
double(val1 - val2));
125 return (tol >=
double(val2 - val1));
129inline bool nearAbs(
int val1,
int val2,
double tol) {
130 return (tol >=
double(std::abs(val2 - val1)));
133inline bool nearAbs(
float val1,
float val2,
double tol) {
134 return (tol >=
double(std::abs(val2 - val1)));
137inline bool nearAbs(
double val1,
double val2,
double tol) {
138 return (tol >= std::abs(val2 - val1));
145template<
typename InputIterator1,
typename InputIterator2,
typename CompareOperator>
146inline bool compareAll (InputIterator1 first1, InputIterator1 last1,
147 InputIterator2 first2, CompareOperator op)
149 for (; first1!=last1; ++first1, ++first2) {
150 if (!op(*first1, *first2))
return false;
157template<
typename InputIterator1,
typename T,
typename CompareOperator>
159 T left, CompareOperator op)
161 for (; first1!=last1; ++first1) {
162 if (!op(left, *first1))
return false;
169template<
typename InputIterator1,
typename T,
typename CompareOperator>
171 T right, CompareOperator op)
173 for (; first1!=last1; ++first1) {
174 if (!op(*first1, right))
return false;
184template<
typename InputIterator1,
typename InputIterator2,
typename CompareOperator>
185inline bool compareAny (InputIterator1 first1, InputIterator1 last1,
186 InputIterator2 first2, CompareOperator op)
188 for (; first1!=last1; ++first1, ++first2) {
189 if (op(*first1, *first2))
return true;
196template<
typename InputIterator1,
typename T,
typename CompareOperator>
198 T left, CompareOperator op)
200 for (; first1!=last1; ++first1) {
201 if (op(left, *first1))
return true;
208template<
typename InputIterator1,
typename T,
typename CompareOperator>
210 T right, CompareOperator op)
212 for (; first1!=last1; ++first1) {
213 if (op(*first1, right))
return true;
222template<
typename T,
typename Accum=T>
242 std::complex<T>
operator() (std::complex<T> left, std::complex<T> right)
const
243 {
return left + ((right.real() -
itsBase.real()) * (right.real() -
itsBase.real()) +
244 (right.imag() -
itsBase.imag()) * (right.imag() -
itsBase.imag())); }
250bool isnan(
const std::complex<T> &val)
252 return std::isnan(val.real()) || std::isnan(val.imag());
256bool isinf(
const std::complex<T> &val)
258 return std::isinf(val.real()) || std::isinf(val.imag());
264 return std::isfinite(val.real()) || std::isfinite(val.imag());
270 if (r != 0 && (x<0) != (y<0)) r+=y;
273inline long long floormod (
long long x,
long long y)
276 if (r != 0 && (x<0) != (y<0)) r+=y;
281 float r = std::fmod(x,y);
282 if (r != 0 && (x<0) != (y<0)) r+=y;
287 double r = std::fmod(x,y);
288 if (r != 0 && (x<0) != (y<0)) r+=y;
293{ out =
static_cast<T
>(in); }
295inline void convertScalar (std::complex<float>& out, std::complex<double> in)
296{ 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.
this file contains all the compiler specific defines
Define real & complex conjugation for non-complex types and put comparisons into std namespace.
std::complex< T > itsBase
SumSqrDiff(std::complex< T > base)
Functor to add squared diff of right and base value to left.
Accum operator()(Accum left, T right) const