21#ifdef gsCoDiPack_ENABLED
36typedef std::numeric_limits<real_t> limits;
65#ifdef gsCoDiPack_ENABLED
96#ifdef gsUniversal_ENABLED
97using sw::universal::abs;
98using sw::universal::acos;
99using sw::universal::asin;
100using sw::universal::atan2;
101using sw::universal::atan;
102using sw::universal::ceil;
103using sw::universal::cos;
104using sw::universal::cosh;
105using sw::universal::exp;
106using sw::universal::floor;
109using sw::universal::log10;
110using sw::universal::log;
111using sw::universal::max;
112using sw::universal::min;
113using sw::universal::pow;
114using sw::universal::sin;
115using sw::universal::sinh;
116using sw::universal::sqrt;
117using sw::universal::tan;
118using sw::universal::tanh;
121template<
size_t nbits,
size_t es>
122inline sw::universal::posit<nbits,es> frexp(
const sw::universal::posit<nbits,es> & a,
int* b) {
return a;}
124template<
size_t nbits,
size_t es>
125inline sw::universal::posit<nbits,es> ldexp(
const sw::universal::posit<nbits,es> & a,
int b ) {
return a;}
127using sw::universal::isnan;
128using sw::universal::isfinite;
129using sw::universal::isinf;
131using sw::universal::real;
132using sw::universal::imag;
133using sw::universal::conj;
140template <
typename T>
inline T exp2(
const T a) {
return 1U << a;}
143T round(T a) {
return math::floor(a+0.5); }
149template <
typename T>
152# if defined(_MSC_VER) && _MSC_VER < 1800
153 return _nextafter(x,y);
155 return ::nextafter(x,y);
161inline mpfr::mpreal
nextafter(mpfr::mpreal x, mpfr::mpreal y)
163 return x + ( y < x ? -1e-16 : 1e-16 );
169inline mpq_class
nextafter(mpq_class x, mpq_class y)
171 return x + ( y < x ? -1e-16 : 1e-16 );
175#ifdef gsUniversal_ENABLED
176template<
size_t nbits,
size_t es>
177inline sw::universal::posit<nbits,es>
nextafter(sw::universal::posit<nbits,es> x,
178 sw::universal::posit<nbits,es> y)
180 return sw::universal::nextafter(x,y);
204 inline static int digits()
205 {
return std::numeric_limits<T>::digits; }
207 inline static int digits10()
208 {
return std::numeric_limits<T>::digits10; }
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()
243bool isinf(T a) {
return (_FPCLASS_PINF|_FPCLASS_NINF) & _fpclass(a);}
248#define NAN (std::numeric_limits<real_t>::quiet_NaN())
252#ifdef _INTEL_COMPILER
282inline mpfr::mpreal frexp(
const mpfr::mpreal & a,
int* b) {
return a;}
283inline mpfr::mpreal ldexp(
const mpfr::mpreal & a,
int b ) {
return a;}
303inline mpq_class log10(
const mpq_class & a) {
return log(a)/log(10); }
313inline mpq_class (max)(
const mpq_class & a,
const mpq_class & b)
314{
return mpq_class(a < b ? b : a);}
315inline mpq_class (min)(
const mpq_class & a,
const mpq_class & b)
316{
return mpq_class(a < b ? a : b);}
317inline bool isfinite(mpq_class a) {
return true; }
318inline bool isinf(mpq_class a) {
return false;}
319inline bool isnan(mpq_class a) {
return false;}
320inline mpq_class frexp(
const mpq_class & a,
int* b) {
return a;}
321inline mpq_class ldexp(
const mpq_class & a,
int b ) {
return a;}
327 return (T(0) < val) - (val < (T)(0));
333inline 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);
340inline int ipow(
int x,
unsigned exp)
354inline unsigned isqrt(
unsigned value)
356 const unsigned sr =
static_cast<unsigned>(std::sqrt(
static_cast<double>(value)));
364inline T
mix(T
const & a, T
const & b, T
const & t)
366 return (1 - t) * a + t * b;
371inline bool lessthan (T
const a, T
const b)
372{
return ( b-a > math::limits::epsilon()); }
375template<
int prec,
class T>
376bool almostEqual (T
const a, T
const b)
377{
return ( math::log10(math::abs(b-a)) < -prec); }
381bool 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;
420bool almostEqual(
const T a,
const T b)
421{
return almostEqualUlp<T>(a,b,4); }
439inline bool gsClose (
const T &a,
const T &b,
const T &tol)
441 return math::abs(a-b) <= tol;
451template <
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());
464template <
typename matrix_t1,
typename matrix_t2>
465inline 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;
478template <
typename matrix_t1,
typename matrix_t2>
479inline 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());
Header for CoDiPack package.
#define GISMO_ASSERT(cond, message)
Definition gsDebug.h:89
Provides forward declarations of types and structs.
int getSign(T val)
Returns the sign of val.
Definition gsMath.h:325
int ipow(int x, unsigned exp)
integer power
Definition gsMath.h:340
util::make_unsigned< T >::type abs_diff(T a, T b)
Definition gsMath.h:333
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
unsigned isqrt(unsigned value)
integer square root
Definition gsMath.h:354
T nextafter(T x, T y)
Definition gsMath.h:150
The G+Smo namespace, containing all definitions for the library.
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
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
bool isfinite(const gsEigen::MatrixBase< Derived > &x)
Check if all the entires if the matrix x are not INF (infinite)
Definition gsLinearAlgebra.h:109
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 gsClose(const T &a, const T &b, const T &tol)
tests if the difference between two numbers is below tolerance
Definition gsMath.h:439