From b01afc120ab66a7b7d7a02472d380d5f1268f9da Mon Sep 17 00:00:00 2001 From: Michael Hemmer Date: Tue, 1 Apr 2008 14:29:07 +0000 Subject: [PATCH] mv wang.h and Wang_traits.h to CGAL/Polynomial/*.h and all into namespace CGALi --- .../include/CGAL/Polynomial/Wang_traits.h | 164 ++++++++++++++++++ .../Polynomial/modular_gcd_utcf_pure_wang.h | 7 +- .../Polynomial/modular_gcd_utcf_with_wang.h | 9 +- Polynomial/include/CGAL/Polynomial/wang.h | 111 ++++++++++++ 4 files changed, 279 insertions(+), 12 deletions(-) create mode 100644 Polynomial/include/CGAL/Polynomial/Wang_traits.h create mode 100644 Polynomial/include/CGAL/Polynomial/wang.h diff --git a/Polynomial/include/CGAL/Polynomial/Wang_traits.h b/Polynomial/include/CGAL/Polynomial/Wang_traits.h new file mode 100644 index 00000000000..88098fb301e --- /dev/null +++ b/Polynomial/include/CGAL/Polynomial/Wang_traits.h @@ -0,0 +1,164 @@ +// ============================================================================ +// +// Copyright (c) 2001-2006 Max-Planck-Institut Saarbruecken (Germany). +// All rights reserved. +// +// This file is part of EXACUS (http://www.mpi-inf.mpg.de/projects/EXACUS/); +// you may redistribute it under the terms of the Q Public License version 1.0. +// See the file LICENSE.QPL distributed with EXACUS. +// +// 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. +// +// ---------------------------------------------------------------------------- +// +// Library : CGAL +// File : include/CGAL/Polynomial/Wang_traits.h +// CGAL_release : $Name: $ +// Revision : $Revision$ +// Revision_date : $Date$ +// +// Author(s) : Michael Hemmer +// +// ============================================================================ + +#ifndef CGAL_WANG_TRAITS_H +#define CGAL_WANG_TRAITS_H 1 + +#include +#include + +/*! \file CGAL/Polynomial/Wang_traits.h + * \brief Definition of traits class CGAL::Wang_traits. + */ + +namespace CGAL{ + +// fwd +template class Sqrt_extension; +template class Polynomial; + +namespace CGALi{ + +/*! \nosubgrouping + * \brief traits class for rational reconstrcution based on wangs + * algorithm + * + * This is experimental, and should serve as a design study, i.e., + * It may be joint with Scalar_factor_traits. + * + * This is the default implementation of CGAL::Wang_traits. + * It is valid for scalar types beeing a EuclideanRing, e.g., Integer + */ +template +class Wang_traits { +public: + // the supported number type + typedef NT_ NT; + // NT is also + typedef NT Scalar; + + struct Wang { + bool + operator() + (const NT& u, const Scalar& m, NT& n, Scalar& d) const { + n = d = NT(0); + return CGAL::CGALi::wang(u,m,n,d); + } + }; +}; + +template +class Wang_traits< CGAL::Sqrt_extension >{ + typedef Wang_traits WT; +public: + // the supported number type + typedef CGAL::Sqrt_extension NT; + // the scalar type (same as Scalar factor traits ?) + typedef typename WT::Scalar Scalar; + + struct Wang { + bool + operator() + (const NT& ext, const Scalar& m, NT& n, Scalar& d) const { + typename Algebraic_structure_traits::Integral_division idiv; + typename WT::Wang wang; + + AS a0,a1; + Scalar d0,d1; + ROOT root; + n = NT(0); + d = Scalar(0); + + if(!wang(ext.a0(),m,a0,d0)) return false; + + if(ext.is_extended()){ + if(!wang(ext.a1(),m,a1,d1)) return false; + d = d0 * idiv(d1,CGAL::gcd(d0,d1)); + a0 = a0 * idiv(d,d0); + a1 = a1 * idiv(d,d1); + n = NT(a0,a1,ext.root()); + }else{ + d = d0; + n = NT(a0); + } + return true; + } + }; +}; + +template +class Wang_traits< Polynomial >{ + + typedef Wang_traits WT; +public: + // the supported number type + typedef Polynomial NT; + // the scalar type (same as Scalar factor traits ?) + typedef typename WT::Scalar Scalar; + + struct Wang { + bool operator() + (const NT& p, const Scalar& m, NT& result_n, Scalar& result_d) const { + typename Algebraic_structure_traits::Integral_division idiv; + typename Algebraic_structure_traits::Gcd gcd; + typename WT::Wang wang; + + result_n = NT(0); + result_d = Scalar(0); +// std::cout<<"Poly "< nums(d+1); + std::vector denoms(d+1); + for (int i = 0; i <= d; i++) { +// bool w = wang(p[i], m, nums[i], denoms[i]); +// wang(p[i], m, nums[i], denoms[i]); +// std::cout< #include #include - - -#include -#include +#include #include @@ -126,7 +123,7 @@ Polynomial modular_gcd_utcf_pure_wang( typedef Polynomial Poly; typedef Polynomial_traits_d PT; - typedef CGAL::Wang_traits WT_POLY; + typedef CGAL::CGALi::Wang_traits WT_POLY; typename WT_POLY::Wang wang; diff --git a/Polynomial/include/CGAL/Polynomial/modular_gcd_utcf_with_wang.h b/Polynomial/include/CGAL/Polynomial/modular_gcd_utcf_with_wang.h index fe3d63712b9..9bd2ccdfdcf 100644 --- a/Polynomial/include/CGAL/Polynomial/modular_gcd_utcf_with_wang.h +++ b/Polynomial/include/CGAL/Polynomial/modular_gcd_utcf_with_wang.h @@ -45,14 +45,9 @@ #include #include #include - - -#include -#include +#include #include - - namespace CGAL { namespace CGALi_with_wang{ @@ -128,7 +123,7 @@ Polynomial modular_gcd_utcf_with_wang( typedef Polynomial Poly; typedef Polynomial_traits_d PT; - typedef CGAL::Wang_traits WT_POLY; + typedef CGAL::CGALi::Wang_traits WT_POLY; typename WT_POLY::Wang wang; diff --git a/Polynomial/include/CGAL/Polynomial/wang.h b/Polynomial/include/CGAL/Polynomial/wang.h new file mode 100644 index 00000000000..39f2b77dc10 --- /dev/null +++ b/Polynomial/include/CGAL/Polynomial/wang.h @@ -0,0 +1,111 @@ +// ============================================================================ +// +// Copyright (c) 2001-2006 Max-Planck-Institut Saarbruecken (Germany). +// All rights reserved. +// +// This file is part of EXACUS (http://www.mpi-inf.mpg.de/projects/EXACUS/); +// you may redistribute it under the terms of the Q Public License version 1.0. +// See the file LICENSE.QPL distributed with EXACUS. +// +// 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. +// +// ---------------------------------------------------------------------------- +// +// Library : CGAL +// File : include/CGAL/Polynomial/wang.h +// CGAL_release : $Name: $ +// Revision : $Revision$ +// Revision_date : $Date$ +// +// Author(s) : Dominik Huelse +// Michael Hemmer +// +// +// ============================================================================ + +/*! \file CGAL/Polynomial/wang.h + * \brief Wang's algorithm for Rational Reconstruction. + */ + +#ifndef CGAL_WANG_H +#define CGAL_WANG_H 1 + + +#include +#include +//#include +#include + +namespace CGAL { + +namespace CGALi{ + +template +inline +bool wang_general(const Integer& u, const Integer& m, + Integer& n, Integer& d, + const Integer& N, const Integer& D) { + Integer r0,r1,t0,t1,q,hilf; + +// std::cout<<" wang general "<=0); + CGAL_precondition((m>u) && (2*N*DN){ + q = CGAL::div(r0,r1); + hilf=r0; + r0=r1; + r1=hilf-q*r1; + hilf=t0; + t0=t1; + t1=hilf-q*t1; + } + n=r1; + d=t1; + if(d<0){ + n=-n; + d=-d; + } + + if(d<=D && (CGAL::gcd(n,d))==1) + return true; + else{ + return false; + } + +} // wang_general + + +/*! + * \brief Wang's algorithm for Rational Reconstruction + */ +template +bool wang( const Integer& u, const Integer& m, + Integer& n, Integer& d ){ + + typename CGAL::Algebraic_structure_traits::Sqrt sqrt; + // set N and D to wang's default values + Integer N = sqrt(CGAL::div(m,Integer(2))); + Integer D = N-Integer(1); + + return CGALi::wang_general(u, m, n, d, N, D); + +}// wang + +} // namespace CGALi +} // namespace CGAL + +#endif // CGAL_WANG_H + +// EOF