// Copyright (c) 2007-2009 Inria Lorraine (France). All rights reserved. // // This file is part of CGAL (www.cgal.org); you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation; either version 3 of the License, // or (at your option) any later version. // // Licensees holding a valid commercial license may use this file in // accordance with the commercial license agreement provided with the software. // // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. // // $URL$ // $Id$ // SPDX-License-Identifier: LGPL-3.0+ // // Author: Luis PeƱaranda // Michael Hemmer #ifndef CGAL_GMPFI_H #define CGAL_GMPFI_H #include #ifdef CGAL_USE_MPFI #include #include #include #include #include namespace CGAL{ template <> class Algebraic_structure_traits: public Algebraic_structure_traits_base{ public: typedef Tag_false Is_exact; typedef Tag_true Is_numerical_sensitive; typedef Uncertain Boolean; struct Is_zero: public CGAL::cpp98::unary_function{ Boolean operator()(const Type &x)const{ return x.is_zero(); } }; struct Is_one: public CGAL::cpp98::unary_function{ Boolean operator()(const Type &x)const{ return x.is_one(); } }; struct Square: public CGAL::cpp98::unary_function{ Type operator()(const Type &x)const{ return x.square(); }; }; struct Is_square: public CGAL::cpp98::binary_function{ Boolean operator()(const Type &x)const{ return x.is_square(); }; Boolean operator()(const Type &x,Type &y)const{ return x.is_square(y); }; }; struct Sqrt: public CGAL::cpp98::unary_function{ Type operator()(const Type &x)const{ return x.sqrt(); }; }; struct Kth_Root: public CGAL::cpp98::binary_function{ Type operator()(int k,const Type &x)const{ return (k==3?x.cbrt():x.kthroot(k)); }; }; struct Divides: public CGAL::cpp98::binary_function{ Boolean operator()(const Type &d,const Type &n)const{ // Avoid compiler warning (void)n; return !(d.is_zero()); }; Boolean operator()(const Type &d,const Type &n,Type &c)const{ return d.divides(n,c); }; }; }; template <> class Real_embeddable_traits: public INTERN_RET::Real_embeddable_traits_base{ typedef Algebraic_structure_traits AST; public: typedef Tag_true Is_real_embeddable; typedef Uncertain Boolean; typedef Uncertain Comparison_result; typedef Uncertain Sign; typedef AST::Is_zero Is_zero; struct Is_finite: public CGAL::cpp98::unary_function{ inline Boolean operator()(const Type &x)const{ return(x.is_number()); }; }; struct Abs: public CGAL::cpp98::unary_function{ inline Type operator()(const Type &x)const{ return x.abs(); }; }; struct Sgn: public CGAL::cpp98::unary_function{ inline Sign operator()(const Type &x)const{ return x.sign(); }; }; struct Is_positive: public CGAL::cpp98::unary_function{ inline Boolean operator()(const Type &x)const{ return x.is_positive(); }; }; struct Is_negative: public CGAL::cpp98::unary_function{ inline Boolean operator()(const Type &x)const{ return x.is_negative(); }; }; struct Compare: public CGAL::cpp98::binary_function{ inline Comparison_result operator() (const Type &x,const Type &y)const{ return x.compare(y); }; CGAL_IMPLICIT_INTEROPERABLE_BINARY_OPERATOR_WITH_RT(Type,Comparison_result) }; struct To_double: public CGAL::cpp98::unary_function{ inline double operator()(const Type &x)const{ return x.to_double(); }; }; struct To_interval: public CGAL::cpp98::unary_function >{ inline std::pair operator()(const Type &x)const{ return x.to_interval(); }; }; }; template<> class Interval_traits : public internal::Interval_traits_base{ public: typedef Interval_traits Self; typedef Gmpfi Interval; typedef CGAL::Gmpfr Bound; typedef CGAL::Tag_false With_empty_interval; typedef CGAL::Tag_true Is_interval; struct Construct :public CGAL::cpp98::binary_function{ Interval operator()( const Bound& l,const Bound& r) const { CGAL_precondition( l < r ); return Interval(std::make_pair(l,r)); } }; struct Lower :public CGAL::cpp98::unary_function{ Bound operator()( const Interval& a ) const { return a.inf(); } }; struct Upper :public CGAL::cpp98::unary_function{ Bound operator()( const Interval& a ) const { return a.sup(); } }; struct Width :public CGAL::cpp98::unary_function{ Bound operator()( const Interval& a ) const { return Gmpfr::sub(a.sup(),a.inf(),std::round_toward_infinity); } }; struct Median :public CGAL::cpp98::unary_function{ Bound operator()( const Interval& a ) const { return (a.inf()+a.sup())/2; } }; struct Norm :public CGAL::cpp98::unary_function{ Bound operator()( const Interval& a ) const { return a.abs().sup(); } }; struct Singleton :public CGAL::cpp98::unary_function{ bool operator()( const Interval& a ) const { return a.inf() == a.sup(); } }; struct Zero_in :public CGAL::cpp98::unary_function{ bool operator()( const Interval& a ) const { return a.inf() <= 0 && 0 <= a.sup(); } }; struct In :public CGAL::cpp98::binary_function{ bool operator()( Bound x, const Interval& a ) const { return a.inf() <= x && x <= a.sup(); } }; struct Equal :public CGAL::cpp98::binary_function{ bool operator()( const Interval& a, const Interval& b ) const { return a.is_same(b); } }; struct Overlap :public CGAL::cpp98::binary_function{ bool operator()( const Interval& a, const Interval& b ) const { return a.do_overlap(b); } }; struct Subset :public CGAL::cpp98::binary_function{ bool operator()( const Interval& a, const Interval& b ) const { return b.inf() <= a.inf() && a.sup() <= b.sup() ; } }; struct Proper_subset :public CGAL::cpp98::binary_function{ bool operator()( const Interval& a, const Interval& b ) const { return Subset()(a,b) && ! Equal()(a,b); } }; struct Hull :public CGAL::cpp98::binary_function{ Interval operator()( const Interval& a, const Interval& b ) const { BOOST_USING_STD_MAX(); BOOST_USING_STD_MIN(); return Interval( std::make_pair( min BOOST_PREVENT_MACRO_SUBSTITUTION (a.inf(),b.inf()), max BOOST_PREVENT_MACRO_SUBSTITUTION (a.sup(),b.sup()))); } }; // struct Empty is Null_functor struct Intersection :public CGAL::cpp98::binary_function{ Interval operator()( const Interval& a, const Interval& b ) const { BOOST_USING_STD_MAX(); BOOST_USING_STD_MIN(); Bound l(max BOOST_PREVENT_MACRO_SUBSTITUTION (Lower()(a),Lower()(b))); Bound u(min BOOST_PREVENT_MACRO_SUBSTITUTION (Upper()(a),Upper()(b))); if(u < l ) throw Exception_intersection_is_empty(); return Construct()(l,u); } }; }; template<> class Bigfloat_interval_traits :public Interval_traits { typedef Gmpfi NT; typedef CGAL::Gmpfr BF; public: typedef Bigfloat_interval_traits Self; typedef CGAL::Tag_true Is_bigfloat_interval; struct Relative_precision: public CGAL::cpp98::unary_function{ long operator()(const NT& x) const { CGAL_precondition(!Singleton()(x)); CGAL_precondition(!CGAL::zero_in(x)); // w = |x| * 2^-p (return p) BF w(CGAL::width(x)); mpfr_div(w.fr(), w.fr(), CGAL::lower(CGAL::abs(x)).fr(), GMP_RNDU); mpfr_log2(w.fr(), w.fr(), GMP_RNDU); return -mpfr_get_si(w.fr(), GMP_RNDU); } }; struct Set_precision { // type for the \c AdaptableUnaryFunction concept. typedef long argument_type; // type for the \c AdaptableUnaryFunction concept. typedef long result_type; long operator()( long prec ) const { return Gmpfi::set_default_precision(prec); } }; struct Get_precision { // type for the \c AdaptableGenerator concept. typedef long result_type; long operator()() const { return Gmpfi::get_default_precision(); } }; }; } // namespace CGAL namespace Eigen { template struct NumTraits; template<> struct NumTraits { typedef CGAL::Gmpfi Real; typedef CGAL::Gmpfi NonInteger; typedef CGAL::Gmpfi Nested; typedef CGAL::Gmpfi Literal; static inline Real epsilon() { return 0; } static inline Real dummy_precision() { return 0; } enum { IsInteger = 0, IsSigned = 1, IsComplex = 0, RequireInitialization = 1, ReadCost = 12, AddCost = 100, MulCost = 100 }; }; } #include #include #endif #endif // CGAL_GMPFI_H // vim: tabstop=8: softtabstop=8: smarttab: shiftwidth=8: expandtab