BWAPI
|
00001 // Copyright (c) 2008 Max-Planck-Institute Saarbruecken (Germany) 00002 // 00003 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or 00004 // modify it under the terms of the GNU Lesser General Public License as 00005 // published by the Free Software Foundation; version 2.1 of the License. 00006 // See the file LICENSE.LGPL distributed with CGAL. 00007 // 00008 // Licensees holding a valid commercial license may use this file in 00009 // accordance with the commercial license agreement provided with the software. 00010 // 00011 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00012 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00013 // 00014 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Polynomial/include/CGAL/Polynomial/Fraction_traits.h $ 00015 // $Id: Fraction_traits.h 47254 2008-12-06 21:18:27Z afabri $ 00016 // 00017 // 00018 // Author(s) : Arno Eigenwillig <arno@mpi-inf.mpg.de> 00019 // Michael Hemmer <hemmer@informatik.uni-mainz.de> 00020 // 00021 // ============================================================================ 00022 00023 // TODO: The comments are all original EXACUS comments and aren't adapted. So 00024 // they may be wrong now. 00025 00026 00027 #ifndef CGAL_POLYNOMIAL_FRACTION_TRAITS_H 00028 #define CGAL_POLYNOMIAL_FRACTION_TRAITS_H 00029 00030 #include <CGAL/basic.h> 00031 00032 CGAL_BEGIN_NAMESPACE 00033 00034 // We need to play a similar game to provide Fraction_traits 00035 00036 template <class POLY, class TAG> 00037 class Poly_Ftr_base; 00038 00039 // Use this if the coefficients cannot be decomposed 00040 // into numerator and denominator 00041 template <class NT_> 00042 class Poly_Ftr_base< Polynomial<NT_>, CGAL::Tag_false > { 00043 public: 00044 typedef Polynomial<NT_> Type; 00045 typedef CGAL::Tag_false Is_fraction; 00046 typedef CGAL::Null_tag Numerator; 00047 typedef CGAL::Null_tag Denominator_type; 00048 typedef CGAL::Null_functor Common_factor; 00049 typedef CGAL::Null_functor Decompose; 00050 typedef CGAL::Null_functor Compose; 00051 }; 00052 00053 // If they can, use this 00054 template <class NT_> 00055 class Poly_Ftr_base< Polynomial<NT_>, CGAL::Tag_true > { 00056 typedef Polynomial<NT_> Poly; 00057 typedef NT_ Coefficient_type; 00058 public: 00059 typedef Polynomial<NT_> Type; 00060 typedef CGAL::Tag_true Is_fraction; 00061 typedef Polynomial<typename Fraction_traits<NT_>::Numerator_type> 00062 Numerator_type; 00063 typedef typename Fraction_traits<NT_>::Denominator_type Denominator_type; 00064 typedef typename Fraction_traits<NT_>::Common_factor Common_factor; 00065 class Decompose { 00066 public: 00067 typedef Type first_argument_type; 00068 typedef Numerator_type& second_argument_type; 00069 typedef Denominator_type& third_argument_type; 00070 inline void operator () ( 00071 const Type& p, 00072 Numerator_type& num, 00073 Denominator_type& den){ 00074 00075 typedef Numerator_type INTPOLY; 00076 typedef Denominator_type DENOM; 00077 00078 typedef Fraction_traits<Coefficient_type> CFTRAITS; 00079 typedef typename CFTRAITS::Numerator_type INTCOEFF; 00080 00081 const int d = p.degree(); 00082 std::vector<INTCOEFF> integ(d+1); 00083 std::vector<DENOM> denom(d+1); 00084 00085 int i; 00086 00087 // decompose each coefficient into integral part and denominator 00088 typename CFTRAITS::Decompose decomp_coeff; 00089 for (i = 0; i <= d; i++) { 00090 decomp_coeff(p[i], integ[i], denom[i]); 00091 } 00092 00093 // c = lcm(denom[0], ..., denom[d]) 00094 typename Algebraic_structure_traits<DENOM>::Integral_division idiv; 00095 typename CFTRAITS::Common_factor gcd; // not really `greatest' 00096 00097 den = denom[0]; 00098 for (i = 1; i <= d; i++) { 00099 den *= idiv(denom[i], gcd(den, denom[i])); 00100 } 00101 00102 // expand each (integ, denom) pair to common denominator 00103 for (i = 0; i <= d; i++) { 00104 integ[i] *= INTCOEFF(idiv(den, denom[i])); 00105 } 00106 num = INTPOLY(integ.begin(), integ.end()); 00107 } 00108 }; 00109 00110 class Compose { 00111 public: 00112 typedef Numerator_type first_argument_type; 00113 typedef Denominator_type second_argument_type; 00114 typedef Type result_type; 00115 inline Type operator () (const Numerator_type& n, 00116 const Denominator_type& d){ 00117 typename Fraction_traits<NT_>::Compose comp_coeff; 00118 (void)comp_coeff; 00119 00120 std::vector< NT_> coeffs(n.degree()+1); 00121 00122 for (int i = 0; i <= n.degree(); i++) { 00123 coeffs[i] = comp_coeff(n[i], d); 00124 } 00125 00126 return Type(coeffs.begin(), coeffs.end()); 00127 }; 00128 }; 00129 }; 00130 00131 00132 // Select the right alternative as Fraction_traits 00145 template <class NT_> 00146 class Fraction_traits< Polynomial<NT_> > 00147 : public Poly_Ftr_base< Polynomial<NT_>, 00148 typename Fraction_traits<NT_>::Is_fraction > 00149 { 00150 // nothing new 00151 }; 00152 00153 CGAL_END_NAMESPACE 00154 #endif // CGAL_POLYNOMIAL_FRACTION_TRAITS_H