21 #ifdef gsCoDiPack_ENABLED
36 typedef std::numeric_limits<real_t> limits;
65 #ifdef gsCoDiPack_ENABLED
96 #ifdef gsUniversal_ENABLED
98 using sw::universal::acos;
99 using sw::universal::asin;
100 using sw::universal::atan2;
101 using sw::universal::atan;
102 using sw::universal::ceil;
103 using sw::universal::cos;
104 using sw::universal::cosh;
105 using sw::universal::exp;
106 using sw::universal::floor;
109 using sw::universal::log10;
110 using sw::universal::log;
111 using sw::universal::max;
112 using sw::universal::min;
113 using sw::universal::pow;
114 using sw::universal::sin;
115 using sw::universal::sinh;
116 using sw::universal::sqrt;
117 using sw::universal::tan;
118 using sw::universal::tanh;
121 template<
size_t nbits,
size_t es>
122 inline sw::universal::posit<nbits,es> frexp(
const sw::universal::posit<nbits,es> & a,
int* b) {
return a;}
124 template<
size_t nbits,
size_t es>
125 inline sw::universal::posit<nbits,es> ldexp(
const sw::universal::posit<nbits,es> & a,
int b ) {
return a;}
127 using sw::universal::isnan;
129 using sw::universal::isinf;
131 using sw::universal::real;
132 using sw::universal::imag;
133 using sw::universal::conj;
140 template <
typename T>
inline T exp2(
const T a) {
return 1U << a;}
142 template <
typename T>
143 T round(T a) {
return math::floor(a+0.5); }
149 template <
typename T>
152 # if defined(_MSC_VER) && _MSC_VER < 1800
153 return _nextafter(x,y);
159 #ifdef gsMpfr_ENABLED
161 inline mpfr::mpreal
nextafter(mpfr::mpreal x, mpfr::mpreal y)
163 return x + ( y < x ? -1e-16 : 1e-16 );
169 inline mpq_class
nextafter(mpq_class x, mpq_class y)
171 return x + ( y < x ? -1e-16 : 1e-16 );
175 #ifdef gsUniversal_ENABLED
176 template<
size_t nbits,
size_t es>
177 inline sw::universal::posit<nbits,es>
nextafter(sw::universal::posit<nbits,es> x,
178 sw::universal::posit<nbits,es> y)
201 template <
typename T>
204 inline static int digits()
205 {
return std::numeric_limits<T>::digits; }
207 inline static int digits10()
208 {
return std::numeric_limits<T>::digits10; }
211 #ifdef gsMpfr_ENABLED
215 inline static int digits()
216 {
return std::numeric_limits<mpfr::mpreal>::digits(); }
218 inline static int digits10()
219 {
return std::numeric_limits<mpfr::mpreal>::digits10(); }
228 #define REAL_DIG math::numeric_limits<real_t>::digits10()
234 template <
typename T>
238 template <
typename T>
242 template <
typename T>
243 bool isinf(T a) {
return (_FPCLASS_PINF|_FPCLASS_NINF) & _fpclass(a);}
248 #define NAN (std::numeric_limits<real_t>::quiet_NaN())
252 #ifdef _INTEL_COMPILER
261 #ifdef gsMpfr_ENABLED
282 inline mpfr::mpreal frexp(
const mpfr::mpreal & a,
int* b) {
return a;}
283 inline mpfr::mpreal ldexp(
const mpfr::mpreal & a,
int b ) {
return a;}
303 inline mpq_class log10(
const mpq_class & a) {
return log(a)/log(10); }
313 inline mpq_class (max)(
const mpq_class & a,
const mpq_class & b)
314 {
return mpq_class(a < b ? b : a);}
315 inline mpq_class (min)(
const mpq_class & a,
const mpq_class & b)
316 {
return mpq_class(a < b ? a : b);}
317 inline bool isfinite(mpq_class a) {
return true; }
318 inline bool isinf(mpq_class a) {
return false;}
319 inline bool isnan(mpq_class a) {
return false;}
320 inline mpq_class frexp(
const mpq_class & a,
int* b) {
return a;}
321 inline mpq_class ldexp(
const mpq_class & a,
int b ) {
return a;}
327 return (T(0) < val) - (val < (T)(0));
332 template <
typename T>
333 inline typename util::make_unsigned<T>::type
abs_diff(T a, T b)
335 typedef typename util::make_unsigned<T>::type unsignedT;
336 return a > b ? unsignedT(a) - unsignedT(b) : unsignedT(b) - unsignedT(a);
340 inline int ipow(
int x,
unsigned exp)
354 inline unsigned isqrt(
unsigned value)
356 const unsigned sr =
static_cast<unsigned>(std::sqrt(static_cast<double>(value)));
364 inline T
mix(T
const & a, T
const & b, T
const & t)
366 return (1 - t) * a + t * b;
371 inline bool lessthan (T
const a, T
const b)
372 {
return ( b-a > math::limits::epsilon()); }
375 template<
int prec,
class T>
376 bool almostEqual (T
const a, T
const b)
377 {
return ( math::log10(
math::abs(b-a)) < -prec); }
381 bool almostEqualUlp(
const T a,
const T b,
const unsigned ulps)
383 typedef std::numeric_limits<T> Tlimits;
386 if (isnan(a) || isnan(b))
390 if (
math::abs(a-b) <= ulps * Tlimits::denorm_min())
395 if (a == 0 || b == 0)
400 int min_exp(0), max_exp(0);
401 T min_frac = frexp(a, &min_exp);
402 T max_frac = frexp(b, &max_exp);
403 if (min_exp > max_exp)
405 std::swap(min_frac, max_frac);
406 std::swap(min_exp, max_exp);
411 const T scaled_min_frac = math::ldexp(min_frac, min_exp-max_exp);
415 return math::abs(max_frac-scaled_min_frac) <= ulps * Tlimits::epsilon() / 2.0;
420 bool almostEqual(
const T a,
const T b)
421 {
return almostEqualUlp<T>(a,b,4); }
438 template <
typename T>
439 inline bool gsClose (
const T &a,
const T &b,
const T &tol)
451 template <
typename matrix_t1,
typename matrix_t2>
454 GISMO_ASSERT( a.cols()==b.cols() && a.rows()==b.rows(),
"Only matrices of the same size can be compared" );
455 return (a-b).array().abs().maxCoeff() <= tol*math::max(a.array().abs().maxCoeff(), b.array().abs().maxCoeff());
464 template <
typename matrix_t1,
typename matrix_t2>
465 inline bool gsAllCloseAbsolute (
const matrix_t1 &a,
const matrix_t2 &b,
const typename matrix_t1::Scalar &tol )
467 GISMO_ASSERT( a.cols()==b.cols() && a.rows()==b.rows(),
"Only matrices of the same size can be compared" );
468 return (a-b).array().abs().maxCoeff() <= tol;
478 template <
typename matrix_t1,
typename matrix_t2>
479 inline bool gsAllCloseRelAndAbsWithRef (
const matrix_t1 &a,
const matrix_t2 &b,
const typename matrix_t1::Scalar &tol,
const typename matrix_t1::Scalar &ref )
481 GISMO_ASSERT( a.cols()==b.cols() && a.rows()==b.rows(),
"Only matrices of the same size can be compared" );
482 return (a-b).array().abs().maxCoeff() <= tol*math::max(ref,a.array().abs().maxCoeff(), b.array().abs().maxCoeff());
bool gsAllCloseAbsolute(const matrix_t1 &a, const matrix_t2 &b, const typename matrix_t1::Scalar &tol)
tests if the difference between two matrices is bounded by tol in norm
Definition: gsMath.h:465
bool gsAllCloseRelAndAbsWithRef(const matrix_t1 &a, const matrix_t2 &b, const typename matrix_t1::Scalar &tol, const typename matrix_t1::Scalar &ref)
Tests whether the difference between two matrices is bounded by tol in norm.
Definition: gsMath.h:479
int getSign(T val)
Returns the sign of val.
Definition: gsMath.h:325
#define GISMO_ASSERT(cond, message)
Definition: gsDebug.h:89
bool isfinite(const gsEigen::MatrixBase< Derived > &x)
Check if all the entires if the matrix x are not INF (infinite)
Definition: gsLinearAlgebra.h:109
Provides forward declarations of types and structs.
unsigned isqrt(unsigned value)
integer square root
Definition: gsMath.h:354
T mix(T const &a, T const &b, T const &t)
Returns convex combination of a and b with weight t.
Definition: gsMath.h:364
Header for CoDiPack package.
bool gsClose(const T &a, const T &b, const T &tol)
tests if the difference between two numbers is below tolerance
Definition: gsMath.h:439
int ipow(int x, unsigned exp)
integer power
Definition: gsMath.h:340
EIGEN_STRONG_INLINE abs_expr< E > abs(const E &u)
Absolute value.
Definition: gsExpressions.h:4488
util::make_unsigned< T >::type abs_diff(T a, T b)
Definition: gsMath.h:333
T nextafter(T x, T y)
Definition: gsMath.h:150
bool gsAllCloseRelativeToMax(const matrix_t1 &a, const matrix_t2 &b, const typename matrix_t1::Scalar &tol)
tests if the difference between two matrices is bounded by tol in norm
Definition: gsMath.h:452