1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
/* emacs edit mode for this file is -*- C++ -*- */
/**
* @file cf_gcd.cc
*
* gcd/content/lcm of polynomials
*
* To compute the GCD different variants are chosen automatically
**/
#include "config.h"
#include "timing.h"
#include "cf_assert.h"
#include "debug.h"
#include "cf_defs.h"
#include "canonicalform.h"
#include "cf_iter.h"
#include "cf_reval.h"
#include "cf_primes.h"
#include "cf_algorithm.h"
#include "cfEzgcd.h"
#include "cfGcdAlgExt.h"
#include "cfSubResGcd.h"
#include "cfModGcd.h"
#ifdef HAVE_NTL
#include <NTL/ZZX.h>
#include "NTLconvert.h"
bool isPurePoly(const CanonicalForm & );
#endif
void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
/** static CanonicalForm icontent ( const CanonicalForm & f, const CanonicalForm & c )
*
* icontent() - return gcd of c and all coefficients of f which
* are in a coefficient domain.
*
* @sa icontent().
*
**/
static CanonicalForm
icontent ( const CanonicalForm & f, const CanonicalForm & c )
{
if ( f.inBaseDomain() )
{
if (c.isZero()) return abs(f);
return bgcd( f, c );
}
//else if ( f.inCoeffDomain() )
// return gcd(f,c);
else
{
CanonicalForm g = c;
for ( CFIterator i = f; i.hasTerms() && ! g.isOne(); i++ )
g = icontent( i.coeff(), g );
return g;
}
}
/** CanonicalForm icontent ( const CanonicalForm & f )
*
* icontent() - return gcd over all coefficients of f which are
* in a coefficient domain.
*
**/
CanonicalForm
icontent ( const CanonicalForm & f )
{
return icontent( f, 0 );
}
/** CanonicalForm gcd_poly ( const CanonicalForm & f, const CanonicalForm & g )
*
* gcd_poly() - calculate polynomial gcd.
*
* This is the dispatcher for polynomial gcd calculation.
* Different gcd variants get called depending the input, characteristic, and
* on switches (cf_defs.h)
*
* With the current settings from Singular (i.e. SW_USE_EZGCD= on,
* SW_USE_EZGCD_P= on, SW_USE_CHINREM_GCD= on, the EZ GCD variants are the
* default algorithms for multivariate polynomial GCD computations)
*
* @sa gcd(), cf_defs.h
*
**/
#if 0
int si_factor_reminder=1;
#endif
CanonicalForm gcd_poly ( const CanonicalForm & f, const CanonicalForm & g )
{
CanonicalForm fc, gc, d1;
bool fc_isUnivariate=f.isUnivariate();
bool gc_isUnivariate=g.isUnivariate();
bool fc_and_gc_Univariate=fc_isUnivariate && gc_isUnivariate;
fc = f;
gc = g;
if ( getCharacteristic() != 0 )
{
#ifdef HAVE_NTL
if ((!fc_and_gc_Univariate) && (isOn( SW_USE_EZGCD_P )))
{
fc= EZGCD_P (fc, gc);
}
else if (isOn(SW_USE_FF_MOD_GCD) && !fc_and_gc_Univariate)
{
Variable a;
if (hasFirstAlgVar (fc, a) || hasFirstAlgVar (gc, a))
fc=modGCDFq (fc, gc, a);
else if (CFFactory::gettype() == GaloisFieldDomain)
fc=modGCDGF (fc, gc);
else
fc=modGCDFp (fc, gc);
}
else
#endif
fc = subResGCD_p( fc, gc );
}
else if (!fc_and_gc_Univariate)
{
if ( isOn( SW_USE_EZGCD ) )
fc= ezgcd (fc, gc);
#ifdef HAVE_NTL
else if (isOn(SW_USE_CHINREM_GCD))
fc = modGCDZ( fc, gc);
#endif
else
{
fc = subResGCD_0( fc, gc );
}
}
else
{
fc = subResGCD_0( fc, gc );
}
if ( d1.degree() > 0 )
fc *= d1;
return fc;
}
/** static CanonicalForm cf_content ( const CanonicalForm & f, const CanonicalForm & g )
*
* cf_content() - return gcd(g, content(f)).
*
* content(f) is calculated with respect to f's main variable.
*
* @sa gcd(), content(), content( CF, Variable ).
*
**/
static CanonicalForm
cf_content ( const CanonicalForm & f, const CanonicalForm & g )
{
if ( f.inPolyDomain() || ( f.inExtension() && ! getReduce( f.mvar() ) ) )
{
CFIterator i = f;
CanonicalForm result = g;
while ( i.hasTerms() && ! result.isOne() )
{
result = gcd( i.coeff(), result );
i++;
}
return result;
}
else
return abs( f );
}
/** CanonicalForm content ( const CanonicalForm & f )
*
* content() - return content(f) with respect to main variable.
*
* Normalizes result.
*
**/
CanonicalForm
content ( const CanonicalForm & f )
{
if ( f.inPolyDomain() || ( f.inExtension() && ! getReduce( f.mvar() ) ) )
{
CFIterator i = f;
CanonicalForm result = abs( i.coeff() );
i++;
while ( i.hasTerms() && ! result.isOne() )
{
result = gcd( i.coeff(), result );
i++;
}
return result;
}
else
return abs( f );
}
/** CanonicalForm content ( const CanonicalForm & f, const Variable & x )
*
* content() - return content(f) with respect to x.
*
* x should be a polynomial variable.
*
**/
CanonicalForm
content ( const CanonicalForm & f, const Variable & x )
{
if (f.inBaseDomain()) return f;
ASSERT( x.level() > 0, "cannot calculate content with respect to algebraic variable" );
Variable y = f.mvar();
if ( y == x )
return cf_content( f, 0 );
else if ( y < x )
return f;
else
return swapvar( content( swapvar( f, y, x ), y ), y, x );
}
/** CanonicalForm vcontent ( const CanonicalForm & f, const Variable & x )
*
* vcontent() - return content of f with repect to variables >= x.
*
* The content is recursively calculated over all coefficients in
* f having level less than x. x should be a polynomial
* variable.
*
**/
CanonicalForm
vcontent ( const CanonicalForm & f, const Variable & x )
{
ASSERT( x.level() > 0, "cannot calculate vcontent with respect to algebraic variable" );
if ( f.mvar() <= x )
return content( f, x );
else {
CFIterator i;
CanonicalForm d = 0;
for ( i = f; i.hasTerms() && ! d.isOne(); i++ )
d = gcd( d, vcontent( i.coeff(), x ) );
return d;
}
}
/** CanonicalForm pp ( const CanonicalForm & f )
*
* pp() - return primitive part of f.
*
* Returns zero if f equals zero, otherwise f / content(f).
*
**/
CanonicalForm
pp ( const CanonicalForm & f )
{
if ( f.isZero() )
return f;
else
return f / content( f );
}
CanonicalForm
gcd ( const CanonicalForm & f, const CanonicalForm & g )
{
bool b = f.isZero();
if ( b || g.isZero() )
{
if ( b )
return abs( g );
else
return abs( f );
}
if ( f.inPolyDomain() || g.inPolyDomain() )
{
if ( f.mvar() != g.mvar() )
{
if ( f.mvar() > g.mvar() )
return cf_content( f, g );
else
return cf_content( g, f );
}
if (isOn(SW_USE_QGCD))
{
Variable m;
if (
(getCharacteristic() == 0) &&
(hasFirstAlgVar(f,m) || hasFirstAlgVar(g,m))
)
{
bool on_rational = isOn(SW_RATIONAL);
CanonicalForm r=QGCD(f,g);
On(SW_RATIONAL);
CanonicalForm cdF = bCommonDen( r );
if (!on_rational) Off(SW_RATIONAL);
return cdF*r;
}
}
if ( f.inExtension() && getReduce( f.mvar() ) )
return CanonicalForm(1);
else
{
if ( fdivides( f, g ) )
return abs( f );
else if ( fdivides( g, f ) )
return abs( g );
if ( !( getCharacteristic() == 0 && isOn( SW_RATIONAL ) ) )
{
CanonicalForm d;
d = gcd_poly( f, g );
return abs( d );
}
else
{
CanonicalForm cdF = bCommonDen( f );
CanonicalForm cdG = bCommonDen( g );
Off( SW_RATIONAL );
CanonicalForm l = lcm( cdF, cdG );
On( SW_RATIONAL );
CanonicalForm F = f * l, G = g * l;
Off( SW_RATIONAL );
l = gcd_poly( F, G );
On( SW_RATIONAL );
return abs( l );
}
}
}
if ( f.inBaseDomain() && g.inBaseDomain() )
return bgcd( f, g );
else
return 1;
}
/** CanonicalForm lcm ( const CanonicalForm & f, const CanonicalForm & g )
*
* lcm() - return least common multiple of f and g.
*
* The lcm is calculated using the formula lcm(f, g) = f * g / gcd(f, g).
*
* Returns zero if one of f or g equals zero.
*
**/
CanonicalForm
lcm ( const CanonicalForm & f, const CanonicalForm & g )
{
if ( f.isZero() || g.isZero() )
return 0;
else
return ( f / gcd( f, g ) ) * g;
}
|