BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/CORE_Expr.h
Go to the documentation of this file.
00001 // Copyright (c) 2002-2004,2007  Utrecht University (The Netherlands),
00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
00005 // and Tel-Aviv University (Israel).  All rights reserved.
00006 //
00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License as
00009 // published by the Free Software Foundation; version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 //
00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Number_types/include/CGAL/CORE_Expr.h $
00019 // $Id: CORE_Expr.h 45636 2008-09-18 15:35:55Z hemmer $
00020 //
00021 //
00022 // Author(s)     : Sylvain Pion, Michael Hemmer
00023 
00024 #ifndef CGAL_CORE_EXPR_H
00025 #define CGAL_CORE_EXPR_H
00026 
00027 #include <CGAL/number_type_basic.h>
00028 #include <CGAL/CORE_coercion_traits.h>
00029 #include <CGAL/CORE/Expr.h>
00030 #include <utility>
00031 
00032 
00033 CGAL_BEGIN_NAMESPACE
00034 
00035 template <> class Algebraic_structure_traits< CORE::Expr >
00036   : public Algebraic_structure_traits_base< CORE::Expr,
00037                                             Field_with_root_of_tag >  {
00038   public:
00039     typedef Tag_true            Is_exact;
00040     typedef Tag_true            Is_numerical_sensitive;
00041 
00042     class Sqrt
00043       : public std::unary_function< Type, Type > {
00044       public:
00045         Type operator()( const Type& x ) const {
00046           return CORE::sqrt( x );
00047         }
00048     };
00049 
00050     class Kth_root
00051       : public std::binary_function<int, Type, Type> {
00052       public:
00053         Type operator()( int k,
00054                                         const Type& x) const {
00055           CGAL_precondition_msg( k > 0, "'k' must be positive for k-th roots");
00056           // CORE::radical isn't implemented for negative values of x, so we
00057           //  have to handle this case separately
00058           if( x < 0 && k%2 != 0)
00059             return -CORE::radical( -x, k );
00060 
00061           return CORE::radical( x, k );
00062         }
00063     };
00064 
00065     class Root_of {
00066       public:
00067 //        typedef CORE::BigRat Boundary;
00068         typedef Type   result_type;
00069 
00070 
00071       public:
00072         // constructs the kth roots of the polynomial
00073         // given by the iterator range, starting from 0.
00074         template< class ForwardIterator >
00075         Type operator()( int k,
00076                                         ForwardIterator begin,
00077                                         ForwardIterator end) const {
00078             std::vector<Type> coeffs;
00079             for(ForwardIterator it = begin; it != end; it++){
00080                 coeffs.push_back(*it);
00081             }
00082             CORE::Polynomial<Type> polynomial(coeffs);
00083             return Type(polynomial,k);
00084         }
00085 
00086 // TODO: Need to be fixed: polynomial<CORE::Expr>.eval() cannot return
00087 //       CORE::BigFloat, so this does not compile.
00088 
00089 /*        template <class ForwardIterator>
00090         Type operator()( CORE::BigRat lower,
00091                                         CORE::BigRat upper,
00092                                         ForwardIterator begin,
00093                                         ForwardIterator end) const {
00094             std::vector<Type> coeffs;
00095             for(ForwardIterator it = begin; it != end; it++){
00096                  coeffs.push_back(*it);
00097             }
00098             CORE::Polynomial<Type> polynomial(coeffs);
00099             CORE::BigFloat lower_bf, upper_bf;
00100             CORE::BigFloat eval_at_lower(0), eval_at_upper(0);
00101 
00102             CORE::extLong r(16),a(16);
00103             while((eval_at_lower.isZeroIn() ||
00104                    eval_at_upper.isZeroIn())){
00105                 //std::cout << "while"<<std::endl;
00106                 r*=2;
00107                 a*=2;
00108                 lower_bf.approx(lower,r,a);
00109                 upper_bf.approx(upper,r,a);
00110                 // The most expensive precond I've ever seen :)),
00111                 // since the coefficients of the polynomial are CORE::Expr
00112                 // TODO: be sure that lower_bf, upper_bf contain exactly one root
00113                 //NiX_expensive_precond(
00114                 //     CORE::Sturm(polynomial).numberOfRoots(lower_bf,upper_bf)==1);
00115                 eval_at_lower = polynomial.eval(lower_bf);
00116                 eval_at_upper = polynomial.eval(upper_bf);
00117             }
00118             CORE::BFInterval interval(lower_bf,upper_bf);
00119 
00120             return Type(polynomial,interval);
00121         };  */
00122     };
00123 
00124 };
00125 
00126 template <> class Real_embeddable_traits< CORE::Expr >
00127   : public INTERN_RET::Real_embeddable_traits_base< CORE::Expr , CGAL::Tag_true > {
00128   public:
00129     class Abs
00130       : public std::unary_function< Type, Type > {
00131       public:
00132         Type operator()( const Type& x ) const {
00133             return CORE::abs( x );
00134         }
00135     };
00136 
00137     class Sgn
00138       : public std::unary_function< Type, ::CGAL::Sign > {
00139       public:
00140         ::CGAL::Sign operator()( const Type& x ) const {
00141           return (::CGAL::Sign) CORE::sign( x );
00142         }
00143     };
00144 
00145     class Compare
00146       : public std::binary_function< Type, Type,
00147                                 Comparison_result > {
00148       public:
00149         Comparison_result operator()( const Type& x,
00150                                             const Type& y ) const {
00151           return (Comparison_result) CORE::cmp( x, y );
00152         }
00153 
00154         CGAL_IMPLICIT_INTEROPERABLE_BINARY_OPERATOR_WITH_RT( Type,
00155                                                       Comparison_result )
00156 
00157     };
00158 
00159     class To_double
00160       : public std::unary_function< Type, double > {
00161       public:
00162         double operator()( const Type& x ) const {
00163           x.approx(53,1024);
00164           return x.doubleValue();
00165         }
00166     };
00167 
00168     class To_interval
00169       : public std::unary_function< Type, std::pair< double, double > > {
00170       public:
00171         std::pair<double, double> operator()( const Type& x ) const {
00172             std::pair<double,double> result;
00173             x.approx(53,1024);
00174             x.doubleInterval(result.first, result.second);
00175             CGAL_expensive_assertion(result.first  <= x);
00176             CGAL_expensive_assertion(result.second >= x);
00177             return result;
00178         }
00179     };
00180 };
00181 
00182 CGAL_END_NAMESPACE
00183 
00184 //since types are included by CORE_coercion_traits.h:
00185 #include <CGAL/CORE_Expr.h>
00186 #include <CGAL/CORE_BigInt.h> 
00187 #include <CGAL/CORE_BigRat.h>
00188 #include <CGAL/CORE_BigFloat.h>
00189 
00190 #endif // CGAL_CORE_EXPR_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines