1 #ifndef ELEMENT_FUNCTIONS_H
2 #define ELEMENT_FUNCTIONS_H
9 namespace arrays_internal {
11 inline 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));
24 inline bool near(
int val1,
int val2,
double tol) {
26 return (val1 == val2);
31 if ((0<val1) != (0<val2)) {
36 return (
double(aval1-aval2) <= tol*
double(
std::max(aval1,aval2)));
39 inline bool near(
float val1,
float val2,
double tol) {
41 return (val1 == val2);
52 if ((0<val1) != (0<val2)) {
58 inline bool near(
double val1,
double val2,
double tol) {
60 return (val1 == val2);
71 if ((0<val1) != (0<val2)) {
77 inline bool near(
float val1,
double val2,
double tol) {
78 return near(
double(val1), val2, tol);
81 inline bool near(
double val1,
float val2,
double tol) {
82 return near(val1,
double(val2), tol);
85 inline bool near(
const std::complex<float> &val1,
const std::complex<float> &val2,
double tol=1.0
e-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;
94 std::complex<double> dval(val1);
95 dval -= std::complex<double>(val2);
96 return std::abs(dval) <= tol * (aval1 < aval2 ? aval2 : aval1);
99 inline bool near(
const std::complex<double> &val1,
const std::complex<double> &val2,
double tol=1.0
e-13)
101 if (tol <= 0)
return val1 == val2;
102 if (val1 == val2)
return true;
106 return std::abs(val1-val2) <= tol * (aval1 < aval2 ? aval2 : aval1);
109 inline bool nearAbs(
const std::complex<float> &val1,
const std::complex<float> &val2,
double tol=1.0
e-5)
111 return std::abs(val2 - val1) <= tol;
114 inline bool nearAbs(
const std::complex<double> &val1,
const std::complex<double> &val2,
double tol=1.0
e-13)
116 return std::abs(val2 - val1) <= tol;
119 inline 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));
129 inline bool nearAbs(
int val1,
int val2,
double tol) {
130 return (tol >=
double(
std::abs(val2 - val1)));
133 inline bool nearAbs(
float val1,
float val2,
double tol) {
134 return (tol >=
double(
std::abs(val2 - val1)));
137 inline bool nearAbs(
double val1,
double val2,
double tol) {
138 return (tol >=
std::abs(val2 - val1));
145 template<
typename InputIterator1,
typename InputIterator2,
typename CompareOperator>
146 inline bool compareAll (InputIterator1 first1, InputIterator1 last1,
147 InputIterator2 first2, CompareOperator op)
149 for (; first1!=last1; ++first1, ++first2) {
150 if (!op(*first1, *first2))
return false;
157 template<
typename InputIterator1,
typename T,
typename CompareOperator>
159 T left, CompareOperator op)
161 for (; first1!=last1; ++first1) {
162 if (!op(left, *first1))
return false;
169 template<
typename InputIterator1,
typename T,
typename CompareOperator>
171 T right, CompareOperator op)
173 for (; first1!=last1; ++first1) {
174 if (!op(*first1, right))
return false;
184 template<
typename InputIterator1,
typename InputIterator2,
typename CompareOperator>
185 inline bool compareAny (InputIterator1 first1, InputIterator1 last1,
186 InputIterator2 first2, CompareOperator op)
188 for (; first1!=last1; ++first1, ++first2) {
189 if (op(*first1, *first2))
return true;
196 template<
typename InputIterator1,
typename T,
typename CompareOperator>
198 T left, CompareOperator op)
200 for (; first1!=last1; ++first1) {
201 if (op(left, *first1))
return true;
208 template<
typename InputIterator1,
typename T,
typename CompareOperator>
210 T right, CompareOperator op)
212 for (; first1!=last1; ++first1) {
213 if (op(*first1, right))
return true;
222 template<
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())); }
250 bool isnan(
const std::complex<T> &val)
256 bool isinf(
const std::complex<T> &val)
270 if (r != 0 && (x<0) != (y<0)) r+=y;
273 inline long long floormod (
long long x,
long long y)
276 if (r != 0 && (x<0) != (y<0)) r+=y;
282 if (r != 0 && (x<0) != (y<0)) r+=y;
288 if (r != 0 && (x<0) != (y<0)) r+=y;
293 { out =
static_cast<T
>(in); }
295 inline void convertScalar (std::complex<float>& out, std::complex<double> in)
296 { out = std::complex<float>(in.real(), in.imag()); }
bool compareAllLeft(InputIterator1 first1, InputIterator1 last1, T left, CompareOperator op)
For use with a constant left value.
bool compareAny(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, CompareOperator op)
Define a function to compare all elements of two sequences.
void convertScalar(T &out, F in)
LatticeExprNode max(const LatticeExprNode &left, const LatticeExprNode &right)
Accum operator()(Accum left, T right) const
bool compareAllRight(InputIterator1 first1, InputIterator1 last1, T right, CompareOperator op)
For use with a constant right value.
bool nearAbs(const std::complex< float > &val1, const std::complex< float > &val2, double tol=1.0e-5)
SumSqrDiff(std::complex< T > base)
bool compareAll(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, CompareOperator op)
Define a function to compare all elements of two sequences.
bool isnan(const std::complex< T > &val)
bool isfinite(const std::complex< T > &val)
bool compareAnyLeft(InputIterator1 first1, InputIterator1 last1, T left, CompareOperator op)
For use with a constant left value.
Functor to add squared diff of right and base value to left.
bool near(unsigned val1, unsigned val2, double tol)
LatticeExprNode min(const LatticeExprNode &left, const LatticeExprNode &right)
std::complex< T > itsBase
bool compareAnyRight(InputIterator1 first1, InputIterator1 last1, T right, CompareOperator op)
For use with a constant right value.
LatticeExprNode abs(const LatticeExprNode &expr)
Numerical 1-argument functions which result in a real number regardless of input expression type...
bool isinf(const std::complex< T > &val)
LatticeExprNode fmod(const LatticeExprNode &left, const LatticeExprNode &right)
const Double e
e and functions thereof:
int floormod(int x, int y)