dust3d/thirdparty/cgal/CGAL-5.1/include/CGAL/Polynomial/modular_gcd_utils.h

97 lines
2.6 KiB
C
Raw Normal View History

// Copyright (c) 2002-2008 Max-Planck-Institute Saarbruecken (Germany)
//
2020-10-13 12:44:25 +00:00
// This file is part of CGAL (www.cgal.org)
//
2020-10-13 12:44:25 +00:00
// $URL: https://github.com/CGAL/cgal/blob/v5.1/Polynomial/include/CGAL/Polynomial/modular_gcd_utils.h $
// $Id: modular_gcd_utils.h 0779373 2020-03-26T13:31:46+01:00 Sébastien Loriot
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
//
// Author(s) : Michael Hemmer <hemmer@mpi-inf.mpg.de>
// Dominik Huelse <dominik.huelse@gmx.de>
2020-10-13 12:44:25 +00:00
//
// ============================================================================
/*! \file CGAL/Polynomial/modular_gcd_utils.h
2020-10-13 12:44:25 +00:00
* \brief Provides additional utils for the modular GCD calculation
*/
#ifndef CGAL_POLYNOMIAL_MODULAR_GCD_UTILS_H
#define CGAL_POLYNOMIAL_MODULAR_GCD_UTILS_H
#include <CGAL/basic.h>
#include <vector>
#include <CGAL/Polynomial.h>
#include <CGAL/Timer.h>
namespace CGAL{
2020-10-13 12:44:25 +00:00
namespace internal {
template <class NT>
2020-10-13 12:44:25 +00:00
void euclidean_division_obstinate(const NT& F1, const NT& F2,
NT& Q, NT& R){
CGAL_precondition(F2 != 0);
2020-10-13 12:44:25 +00:00
CGAL::div_mod(F1, F2, Q, R);
CGAL_postcondition(F1 == F2*Q + R);
}
template <class NT>
2020-10-13 12:44:25 +00:00
void euclidean_division_obstinate(const Polynomial<NT>& F1,
const Polynomial<NT>& F2,
Polynomial<NT>& Q, Polynomial<NT>& R){
// std::cout<<" my_modular_gcd_utils "<<std::endl;
CGAL_precondition(!F2.is_zero());
int d1 = F1.degree();
int d2 = F2.degree();
if ( d1 < d2 ) {
Q = Polynomial<NT>(NT(0)); R = F1;
2020-10-13 12:44:25 +00:00
CGAL_postcondition( !(boost::is_same< typename Algebraic_structure_traits<NT>::Is_exact,
CGAL::Tag_true >::value) || F1 == Q*F2 + R); return;
}
2020-10-13 12:44:25 +00:00
typedef std::vector<NT> Vector;
2020-10-13 12:44:25 +00:00
Vector V_R, V_Q;
V_Q.reserve(d1);
if(d2==0){
2020-10-13 12:44:25 +00:00
for(int i=d1;i>=0;--i){
V_Q.push_back(F1[i]/F2[0]);
}
V_R.push_back(NT(0));
}
2020-10-13 12:44:25 +00:00
else{
V_R.reserve(d1);
V_R=Vector(F1.begin(),F1.end());
Vector tmp1;
tmp1.reserve(d2);
for(int k=0; k<=d1-d2; ++k){
2020-10-13 12:44:25 +00:00
V_Q.push_back(V_R[d1-k]/F2[d2]);
for(int j=0;j<d2;++j){
tmp1.push_back(F2[j]*V_Q[k]);
2020-10-13 12:44:25 +00:00
}
V_R[d1-k]=0;
for(int i=d1-d2-k;i<=d1-k-1;++i){
V_R[i]=V_R[i]-tmp1[i-(d1-d2-k)];
2020-10-13 12:44:25 +00:00
}
tmp1.clear();
}
2020-10-13 12:44:25 +00:00
}
Q = Polynomial<NT>(V_Q.rbegin(),V_Q.rend());
R = Polynomial<NT>(V_R.begin(),V_R.end());
CGAL_postcondition(F1 == F2*Q + R);
}
} // namespace internal
} // namespace CGAL
#endif //#ifnedef CGAL_POLYNOMIAL_MODULAR_GCD_UTILS_H 1
// EOF