File: cf_algorithm.cc

package info (click to toggle)
singular 1%3A4.1.1-p2%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 35,860 kB
  • sloc: cpp: 288,280; ansic: 17,387; lisp: 4,242; yacc: 1,654; python: 1,608; makefile: 1,424; lex: 1,387; perl: 632; sh: 567; xml: 182
file content (574 lines) | stat: -rw-r--r-- 16,232 bytes parent folder | download | duplicates (4)
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/* emacs edit mode for this file is -*- C++ -*- */

/**
 *
 *
 * cf_algorithm.cc - simple mathematical algorithms.
 *
 * Hierarchy: mathematical algorithms on canonical forms
 *
 * Developers note:
 * ----------------
 * A "mathematical" algorithm is an algorithm which calculates
 * some mathematical function in contrast to a "structural"
 * algorithm which gives structural information on polynomials.
 *
 * Compare these functions to the functions in `cf_ops.cc', which
 * are structural algorithms.
 *
**/


#include "config.h"


#include "cf_assert.h"

#include "cf_factory.h"
#include "cf_defs.h"
#include "canonicalform.h"
#include "cf_algorithm.h"
#include "variable.h"
#include "cf_iter.h"
#include "templates/ftmpl_functions.h"
#include "cfGcdAlgExt.h"

void out_cf(const char *s1,const CanonicalForm &f,const char *s2);

/** CanonicalForm psr ( const CanonicalForm & f, const CanonicalForm & g, const Variable & x )
 *
 *
 * psr() - return pseudo remainder of `f' and `g' with respect
 *   to `x'.
 *
 * `g' must not equal zero.
 *
 * For f and g in R[x], R an arbitrary ring, g != 0, there is a
 * representation
 *
 *   LC(g)^s*f = g*q + r
 *
 * with r = 0 or deg(r) < deg(g) and s = 0 if f = 0 or
 * s = max( 0, deg(f)-deg(g)+1 ) otherwise.
 * r = psr(f, g) and q = psq(f, g) are called "pseudo remainder"
 * and "pseudo quotient", resp.  They are uniquely determined if
 * LC(g) is not a zero divisor in R.
 *
 * See H.-J. Reiffen/G. Scheja/U. Vetter - "Algebra", 2nd ed.,
 * par. 15, for a reference.
 *
 * Type info:
 * ----------
 * f, g: Current
 * x: Polynomial
 *
 * Polynomials over prime power domains are admissible if
 * lc(LC(`g',`x')) is not a zero divisor.  This is a slightly
 * stronger precondition than mathematically necessary since
 * pseudo remainder and quotient are well-defined if LC(`g',`x')
 * is not a zero divisor.
 *
 * For example, psr(y^2, (13*x+1)*y) is well-defined in
 * (Z/13^2[x])[y] since (13*x+1) is not a zero divisor.  But
 * calculating it with Factory would fail since 13 is a zero
 * divisor in Z/13^2.
 *
 * Due to this inconsistency with mathematical notion, we decided
 * not to declare type `CurrentPP' for `f' and `g'.
 *
 * Developers note:
 * ----------------
 * This is not an optimal implementation.  Better would have been
 * an implementation in `InternalPoly' avoiding the
 * exponentiation of the leading coefficient of `g'.  In contrast
 * to `psq()' and `psqr()' it definitely seems worth to implement
 * the pseudo remainder on the internal level.
 *
 * @sa psq(), psqr()
**/
CanonicalForm
#if 0
psr ( const CanonicalForm & f, const CanonicalForm & g, const Variable & x )
{

    ASSERT( x.level() > 0, "type error: polynomial variable expected" );
    ASSERT( ! g.isZero(), "math error: division by zero" );

    // swap variables such that x's level is larger or equal
    // than both f's and g's levels.
    Variable X = tmax( tmax( f.mvar(), g.mvar() ), x );
    CanonicalForm F = swapvar( f, x, X );
    CanonicalForm G = swapvar( g, x, X );

    // now, we have to calculate the pseudo remainder of F and G
    // w.r.t. X
    int fDegree = degree( F, X );
    int gDegree = degree( G, X );
    if ( (fDegree < 0) || (fDegree < gDegree) )
        return f;
    else
    {
        CanonicalForm xresult = (power( LC( G, X ), fDegree-gDegree+1 ) * F) ;
        CanonicalForm result = xresult -(xresult/G)*G;
        return swapvar( result, x, X );
    }
}
#else
psr ( const CanonicalForm &rr, const CanonicalForm &vv, const Variable & x )
{
  CanonicalForm r=rr, v=vv, l, test, lu, lv, t, retvalue;
  int dr, dv, d,n=0;


  dr = degree( r, x );
  if (dr>0)
  {
    dv = degree( v, x );
    if (dv <= dr) {l=LC(v,x); v = v -l*power(x,dv);}
    else { l = 1; }
    d= dr-dv+1;
    //out_cf("psr(",rr," ");
    //out_cf("",vv," ");
    //printf(" var=%d\n",x.level());
    while ( ( dv <= dr  ) && ( !r.isZero()) )
    {
      test = power(x,dr-dv)*v*LC(r,x);
      if ( dr == 0 ) { r= CanonicalForm(0); }
      else { r= r - LC(r,x)*power(x,dr); }
      r= l*r -test;
      dr= degree(r,x);
      n+=1;
    }
    r= power(l, d-n)*r;
  }
  return r;
}
#endif

/** CanonicalForm psq ( const CanonicalForm & f, const CanonicalForm & g, const Variable & x )
 *
 *
 * psq() - return pseudo quotient of `f' and `g' with respect
 *   to `x'.
 *
 * `g' must not equal zero.
 *
 * Type info:
 * ----------
 * f, g: Current
 * x: Polynomial
 *
 * Developers note:
 * ----------------
 * This is not an optimal implementation.  Better would have been
 * an implementation in `InternalPoly' avoiding the
 * exponentiation of the leading coefficient of `g'.  It seemed
 * not worth to do so.
 *
 * @sa psr(), psqr()
 *
**/
CanonicalForm
psq ( const CanonicalForm & f, const CanonicalForm & g, const Variable & x )
{
    ASSERT( x.level() > 0, "type error: polynomial variable expected" );
    ASSERT( ! g.isZero(), "math error: division by zero" );

    // swap variables such that x's level is larger or equal
    // than both f's and g's levels.
    Variable X = tmax( tmax( f.mvar(), g.mvar() ), x );
    CanonicalForm F = swapvar( f, x, X );
    CanonicalForm G = swapvar( g, x, X );

    // now, we have to calculate the pseudo remainder of F and G
    // w.r.t. X
    int fDegree = degree( F, X );
    int gDegree = degree( G, X );
    if ( fDegree < 0 || fDegree < gDegree )
        return 0;
    else {
        CanonicalForm result = (power( LC( G, X ), fDegree-gDegree+1 ) * F) / G;
        return swapvar( result, x, X );
    }
}

/** void psqr ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & q, CanonicalForm & r, const Variable & x )
 *
 *
 * psqr() - calculate pseudo quotient and remainder of `f' and
 *   `g' with respect to `x'.
 *
 * Returns the pseudo quotient of `f' and `g' in `q', the pseudo
 * remainder in `r'.  `g' must not equal zero.
 *
 * See `psr()' for more detailed information.
 *
 * Type info:
 * ----------
 * f, g: Current
 * q, r: Anything
 * x: Polynomial
 *
 * Developers note:
 * ----------------
 * This is not an optimal implementation.  Better would have been
 * an implementation in `InternalPoly' avoiding the
 * exponentiation of the leading coefficient of `g'.  It seemed
 * not worth to do so.
 *
 * @sa psr(), psq()
 *
**/
void
psqr ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & q, CanonicalForm & r, const Variable& x )
{
    ASSERT( x.level() > 0, "type error: polynomial variable expected" );
    ASSERT( ! g.isZero(), "math error: division by zero" );

    // swap variables such that x's level is larger or equal
    // than both f's and g's levels.
    Variable X = tmax( tmax( f.mvar(), g.mvar() ), x );
    CanonicalForm F = swapvar( f, x, X );
    CanonicalForm G = swapvar( g, x, X );

    // now, we have to calculate the pseudo remainder of F and G
    // w.r.t. X
    int fDegree = degree( F, X );
    int gDegree = degree( G, X );
    if ( fDegree < 0 || fDegree < gDegree ) {
        q = 0; r = f;
    } else {
        divrem( power( LC( G, X ), fDegree-gDegree+1 ) * F, G, q, r );
        q = swapvar( q, x, X );
        r = swapvar( r, x, X );
    }
}

/** static CanonicalForm internalBCommonDen ( const CanonicalForm & f )
 *
 *
 * internalBCommonDen() - recursively calculate multivariate
 *   common denominator of coefficients of `f'.
 *
 * Used by: bCommonDen()
 *
 * Type info:
 * ----------
 * f: Poly( Q )
 * Switches: isOff( SW_RATIONAL )
 *
**/
static CanonicalForm
internalBCommonDen ( const CanonicalForm & f )
{
    if ( f.inBaseDomain() )
        return f.den();
    else {
        CanonicalForm result = 1;
        for ( CFIterator i = f; i.hasTerms(); i++ )
            result = blcm( result, internalBCommonDen( i.coeff() ) );
        return result;
    }
}

/** CanonicalForm bCommonDen ( const CanonicalForm & f )
 *
 *
 * bCommonDen() - calculate multivariate common denominator of
 *   coefficients of `f'.
 *
 * The common denominator is calculated with respect to all
 * coefficients of `f' which are in a base domain.  In other
 * words, common_den( `f' ) * `f' is guaranteed to have integer
 * coefficients only.  The common denominator of zero is one.
 *
 * Returns something non-trivial iff the current domain is Q.
 *
 * Type info:
 * ----------
 * f: CurrentPP
 *
**/
CanonicalForm
bCommonDen ( const CanonicalForm & f )
{
    if ( getCharacteristic() == 0 && isOn( SW_RATIONAL ) ) {
        // otherwise `bgcd()' returns one
        Off( SW_RATIONAL );
        CanonicalForm result = internalBCommonDen( f );
        On( SW_RATIONAL );
        return result;
    } else
        return CanonicalForm( 1 );
}

/** bool fdivides ( const CanonicalForm & f, const CanonicalForm & g )
 *
 *
 * fdivides() - check whether `f' divides `g'.
 *
 * Returns true iff `f' divides `g'.  Uses some extra heuristic
 * to avoid polynomial division.  Without the heuristic, the test
 * essentialy looks like `divremt(g, f, q, r) && r.isZero()'.
 *
 * Type info:
 * ----------
 * f, g: Current
 *
 * Elements from prime power domains (or polynomials over such
 * domains) are admissible if `f' (or lc(`f'), resp.) is not a
 * zero divisor.  This is a slightly stronger precondition than
 * mathematically necessary since divisibility is a well-defined
 * notion in arbitrary rings.  Hence, we decided not to declare
 * the weaker type `CurrentPP'.
 *
 * Developers note:
 * ----------------
 * One may consider the the test `fdivides( f.LC(), g.LC() )' in
 * the main `if'-test superfluous since `divremt()' in the
 * `if'-body repeats the test.  However, `divremt()' does not use
 * any heuristic to do so.
 *
 * It seems not reasonable to call `fdivides()' from `divremt()'
 * to check divisibility of leading coefficients.  `fdivides()' is
 * on a relatively high level compared to `divremt()'.
 *
**/
bool
fdivides ( const CanonicalForm & f, const CanonicalForm & g )
{
    // trivial cases
    if ( g.isZero() )
        return true;
    else if ( f.isZero() )
        return false;

    if ( (f.inCoeffDomain() || g.inCoeffDomain())
         && ((getCharacteristic() == 0 && isOn( SW_RATIONAL ))
             || (getCharacteristic() > 0) ))
    {
        // if we are in a field all elements not equal to zero are units
        if ( f.inCoeffDomain() )
            return true;
        else
            // g.inCoeffDomain()
            return false;
    }

    // we may assume now that both levels either equal LEVELBASE
    // or are greater zero
    int fLevel = f.level();
    int gLevel = g.level();
    if ( (gLevel > 0) && (fLevel == gLevel) )
        // f and g are polynomials in the same main variable
        if ( degree( f ) <= degree( g )
             && fdivides( f.tailcoeff(), g.tailcoeff() )
             && fdivides( f.LC(), g.LC() ) )
        {
            CanonicalForm q, r;
            return divremt( g, f, q, r ) && r.isZero();
        }
        else
            return false;
    else if ( gLevel < fLevel )
        // g is a coefficient w.r.t. f
        return false;
    else
    {
        // either f is a coefficient w.r.t. polynomial g or both
        // f and g are from a base domain (should be Z or Z/p^n,
        // then)
        CanonicalForm q, r;
        return divremt( g, f, q, r ) && r.isZero();
    }
}

/// same as fdivides if true returns quotient quot of g by f otherwise quot == 0
bool
fdivides ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm& quot )
{
    quot= 0;
    // trivial cases
    if ( g.isZero() )
        return true;
    else if ( f.isZero() )
        return false;

    if ( (f.inCoeffDomain() || g.inCoeffDomain())
         && ((getCharacteristic() == 0 && isOn( SW_RATIONAL ))
             || (getCharacteristic() > 0) ))
    {
        // if we are in a field all elements not equal to zero are units
        if ( f.inCoeffDomain() )
        {
            quot= g/f;
            return true;
        }
        else
            // g.inCoeffDomain()
            return false;
    }

    // we may assume now that both levels either equal LEVELBASE
    // or are greater zero
    int fLevel = f.level();
    int gLevel = g.level();
    if ( (gLevel > 0) && (fLevel == gLevel) )
        // f and g are polynomials in the same main variable
        if ( degree( f ) <= degree( g )
             && fdivides( f.tailcoeff(), g.tailcoeff() )
             && fdivides( f.LC(), g.LC() ) )
        {
            CanonicalForm q, r;
            if (divremt( g, f, q, r ) && r.isZero())
            {
              quot= q;
              return true;
            }
            else
              return false;
        }
        else
            return false;
    else if ( gLevel < fLevel )
        // g is a coefficient w.r.t. f
        return false;
    else
    {
        // either f is a coefficient w.r.t. polynomial g or both
        // f and g are from a base domain (should be Z or Z/p^n,
        // then)
        CanonicalForm q, r;
        if (divremt( g, f, q, r ) && r.isZero())
        {
          quot= q;
          return true;
        }
        else
          return false;
    }
}

/// same as fdivides but handles zero divisors in Z_p[t]/(f)[x1,...,xn] for reducible f
bool
tryFdivides ( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm& M, bool& fail )
{
    fail= false;
    // trivial cases
    if ( g.isZero() )
        return true;
    else if ( f.isZero() )
        return false;

    if (f.inCoeffDomain() || g.inCoeffDomain())
    {
        // if we are in a field all elements not equal to zero are units
        if ( f.inCoeffDomain() )
        {
            CanonicalForm inv;
            tryInvert (f, M, inv, fail);
            return !fail;
        }
        else
        {
            return false;
        }
    }

    // we may assume now that both levels either equal LEVELBASE
    // or are greater zero
    int fLevel = f.level();
    int gLevel = g.level();
    if ( (gLevel > 0) && (fLevel == gLevel) )
    {
        if (degree( f ) > degree( g ))
          return false;
        bool dividestail= tryFdivides (f.tailcoeff(), g.tailcoeff(), M, fail);

        if (fail || !dividestail)
          return false;
        bool dividesLC= tryFdivides (f.LC(),g.LC(), M, fail);
        if (fail || !dividesLC)
          return false;
        CanonicalForm q,r;
        bool divides= tryDivremt (g, f, q, r, M, fail);
        if (fail || !divides)
          return false;
        return r.isZero();
    }
    else if ( gLevel < fLevel )
    {
        // g is a coefficient w.r.t. f
        return false;
    }
    else
    {
        // either f is a coefficient w.r.t. polynomial g or both
        // f and g are from a base domain (should be Z or Z/p^n,
        // then)
        CanonicalForm q, r;
        bool divides= tryDivremt (g, f, q, r, M, fail);
        if (fail || !divides)
          return false;
        return r.isZero();
    }
}

/** CanonicalForm maxNorm ( const CanonicalForm & f )
 *
 *
 * maxNorm() - return maximum norm of `f'.
 *
 * That is, the base coefficient of `f' with the largest absolute
 * value.
 *
 * Valid for arbitrary polynomials over arbitrary domains, but
 * most useful for multivariate polynomials over Z.
 *
 * Type info:
 * ----------
 * f: CurrentPP
 *
**/
CanonicalForm
maxNorm ( const CanonicalForm & f )
{
    if ( f.inBaseDomain() )
        return abs( f );
    else {
        CanonicalForm result = 0;
        for ( CFIterator i = f; i.hasTerms(); i++ ) {
            CanonicalForm coeffMaxNorm = maxNorm( i.coeff() );
            if ( coeffMaxNorm > result )
                result = coeffMaxNorm;
        }
        return result;
    }
}

/** CanonicalForm euclideanNorm ( const CanonicalForm & f )
 *
 *
 * euclideanNorm() - return Euclidean norm of `f'.
 *
 * Returns the largest integer smaller or equal norm(`f') =
 * sqrt(sum( `f'[i]^2 )).
 *
 * Type info:
 * ----------
 * f: UVPoly( Z )
 *
**/
CanonicalForm
euclideanNorm ( const CanonicalForm & f )
{
    ASSERT( (f.inBaseDomain() || f.isUnivariate()) && f.LC().inZ(),
            "type error: univariate poly over Z expected" );

    CanonicalForm result = 0;
    for ( CFIterator i = f; i.hasTerms(); i++ ) {
        CanonicalForm coeff = i.coeff();
        result += coeff*coeff;
    }
    return sqrt( result );
}