source: git/factory/canonicalform.cc @ c22249

spielwiese
Last change on this file since c22249 was c22249, checked in by Martin Lee <martinlee84@…>, 10 years ago
chg: use NTL/FLINT in *=
  • Property mode set to 100644
File size: 55.1 KB
Line 
1/* emacs edit mode for this file is -*- C++ -*- */
2
3#ifdef HAVE_CONFIG_H
4#include "config.h"
5#endif /* HAVE_CONFIG_H */
6
7#include "cf_assert.h"
8#include "cf_factory.h"
9
10#include "cf_defs.h"
11#include "cf_globals.h"
12#include "canonicalform.h"
13#include "cf_iter.h"
14#include "int_cf.h"
15#include "cf_algorithm.h"
16#include "imm.h"
17#include "gfops.h"
18#include "facMul.h"
19
20#include <factory/cf_gmp.h>
21
22
23#ifndef NOSTREAMIO
24CanonicalForm readCF( ISTREAM& );
25#endif /* NOSTREAMIO */
26
27//{{{ constructors, destructors, selectors
28CanonicalForm::CanonicalForm( const char * str, const int base ) : value( CFFactory::basic( str, base ) )
29{
30}
31
32InternalCF*
33CanonicalForm::getval() const
34{
35    if ( is_imm( value ) )
36        return value;
37    else
38        return value->copyObject();
39}
40
41CanonicalForm
42CanonicalForm::deepCopy() const
43{
44    if ( is_imm( value ) )
45        return *this;
46    else
47        return CanonicalForm( value->deepCopyObject() );
48}
49
50void
51CanonicalForm::mpzval(mpz_t val) const
52{
53    ASSERT (!is_imm (value) && value->levelcoeff() == IntegerDomain, "non-immediate integer expected");
54    getmpi (value, val);
55}
56//}}}
57
58//{{{ predicates
59#if 0
60bool
61CanonicalForm::isImm() const
62{
63    return is_imm( value );
64}
65#endif
66
67bool
68CanonicalForm::inZ() const
69{
70    if ( is_imm( value ) == INTMARK )
71        return true;
72    else if ( is_imm( value ) )
73        return false;
74    else
75        return value->levelcoeff() == IntegerDomain;
76}
77
78bool
79CanonicalForm::inQ() const
80{
81    if ( is_imm( value ) == INTMARK )
82        return true;
83    else if ( is_imm( value ) )
84        return false;
85    else
86        return value->levelcoeff() == IntegerDomain ||
87            value->levelcoeff() == RationalDomain;
88}
89
90bool
91CanonicalForm::inFF() const
92{
93    return is_imm( value ) == FFMARK;
94}
95
96bool
97CanonicalForm::inGF() const
98{
99    return is_imm( value ) == GFMARK;
100}
101
102bool
103CanonicalForm::inBaseDomain() const
104{
105    if ( is_imm( value ) )
106        return true;
107    else
108        return value->inBaseDomain();
109}
110
111bool
112CanonicalForm::inExtension() const
113{
114    if ( is_imm( value ) )
115        return false;
116    else
117        return value->inExtension();
118}
119
120bool
121CanonicalForm::inCoeffDomain() const
122{
123    if ( is_imm( value ) )
124        return true;
125    else
126        return value->inCoeffDomain();
127}
128
129bool
130CanonicalForm::inPolyDomain() const
131{
132    if ( is_imm( value ) )
133        return false;
134    else
135        return value->inPolyDomain();
136}
137
138bool
139CanonicalForm::inQuotDomain() const
140{
141    if ( is_imm( value ) )
142        return false;
143    else
144        return value->inQuotDomain();
145}
146
147bool
148CanonicalForm::isFFinGF() const
149{
150    return is_imm( value ) == GFMARK && gf_isff( imm2int( value ) );
151}
152
153bool
154CanonicalForm::isUnivariate() const
155{
156    if ( is_imm( value ) )
157        return false;
158    else
159        return value->isUnivariate();
160}
161
162// is_homogeneous returns 1 iff f is homogeneous, 0 otherwise//
163bool
164CanonicalForm::isHomogeneous() const
165{
166  if (this->isZero()) return true;
167  else if (this->inCoeffDomain()) return true;
168  else
169  {
170#if 0
171    CFIterator i;
172    int cdeg = -2, dummy;
173    for ( i = *this; i.hasTerms(); i++ )
174    {
175      if (!(i.coeff().isHomogeneous())) return false;
176      if ( (dummy = totaldegree( i.coeff() ) + i.exp()) != cdeg )
177      {
178         if (cdeg == -2) cdeg = dummy;
179         else return false;
180      }
181    }
182    return true;
183#else
184    CFList termlist= get_Terms(*this);
185    CFListIterator i;
186    int deg= totaldegree(termlist.getFirst());
187
188    for ( i=termlist; i.hasItem(); i++ )
189      if ( totaldegree(i.getItem()) != deg ) return false;
190    return true;
191#endif
192  }
193}
194
195//}}}
196
197//{{{ conversion functions
198long
199CanonicalForm::intval() const
200{
201    if ( is_imm( value ) )
202        return imm_intval( value );
203    else
204        return value->intval();
205}
206
207CanonicalForm
208CanonicalForm::mapinto () const
209{
210    //ASSERT( is_imm( value ) ||  ! value->inExtension(), "cannot map into different Extension" );
211    if ( is_imm( value ) )
212        if ( getCharacteristic() == 0 )
213            if ( is_imm( value ) == FFMARK )
214                return CanonicalForm( int2imm( ff_symmetric( imm2int( value ) ) ) );
215            else  if ( is_imm( value ) == GFMARK )
216                return CanonicalForm( int2imm( ff_symmetric( gf_gf2ff( imm2int( value ) ) ) ) );
217            else
218                return *this;
219        else  if ( getGFDegree() == 1 )
220            return CanonicalForm( int2imm_p( ff_norm( imm2int( value ) ) ) );
221        else
222            return CanonicalForm( int2imm_gf( gf_int2gf( imm2int( value ) ) ) );
223    else  if ( value->inBaseDomain() )
224        if ( getCharacteristic() == 0 )
225             return *this;
226        else
227        {
228            int val;
229            if ( value->levelcoeff() == IntegerDomain )
230                val = value->intmod( ff_prime );
231            else  if ( value->levelcoeff() == RationalDomain )
232                return num().mapinto() / den().mapinto();
233            else {
234                ASSERT( 0, "illegal domain" );
235                return 0;
236            }
237            if ( getGFDegree() > 1 )
238                return CanonicalForm( int2imm_gf( gf_int2gf( val ) ) );
239            else
240                return CanonicalForm( int2imm_p( val ) );
241        }
242    else
243    {
244        Variable x = value->variable();
245        CanonicalForm result;
246        for ( CFIterator i = *this; i.hasTerms(); i++ )
247            result += (power( x, i.exp() ) * i.coeff().mapinto());
248        return result;
249    }
250}
251//}}}
252
253//{{{ CanonicalForm CanonicalForm::lc (), Lc (), LC (), LC ( v ) const
254//{{{ docu
255//
256// lc(), Lc(), LC() - leading coefficient functions.
257//
258// All methods return CO if CO is in a base domain.
259//
260// lc() returns the leading coefficient of CO with respect to
261// lexicographic ordering.  Elements in an algebraic extension
262// are considered polynomials so lc() always returns a leading
263// coefficient in a base domain.  This method is useful to get
264// the base domain over which CO is defined.
265//
266// Lc() returns the leading coefficient of CO with respect to
267// lexicographic ordering.  In contrast to lc() elements in an
268// algebraic extension are considered coefficients so Lc() always
269// returns a leading coefficient in a coefficient domain.
270//
271// LC() returns the leading coefficient of CO where CO is
272// considered a univariate polynomial in its main variable.  An
273// element of an algebraic extension is considered an univariate
274// polynomial, too.
275//
276// LC( v ) returns the leading coefficient of CO where CO is
277// considered an univariate polynomial in the polynomial variable
278// v.
279// Note: If v is less than the main variable of CO we have to
280// swap variables which may be quite expensive.
281//
282// Examples:
283// Let x < y be polynomial variables, a an algebraic variable.
284//
285// (3*a*x*y^2+y+x).lc() = 3
286// (3*a*x*y^2+y+x).Lc() = 3*a
287// (3*a*x*y^2+y+x).LC() = 3*a*x
288// (3*a*x*y^2+y+x).LC( x ) = 3*a*y^2+1
289//
290// (3*a^2+4*a).lc() = 3
291// (3*a^2+4*a).Lc() = 3*a^2+4*a
292// (3*a^2+4*a).LC() = 3
293// (3*a^2+4*a).LC( x ) = 3*a^2+4*a
294//
295// See also: InternalCF::lc(), InternalCF::Lc(), InternalCF::LC(),
296// InternalPoly::lc(), InternalPoly::Lc(), InternalPoly::LC(),
297// ::lc(), ::Lc(), ::LC(), ::LC( v )
298//
299//}}}
300CanonicalForm
301CanonicalForm::lc () const
302{
303    if ( is_imm( value ) )
304        return *this;
305    else
306        return value->lc();
307}
308
309CanonicalForm
310CanonicalForm::Lc () const
311{
312    if ( is_imm( value ) || value->inCoeffDomain() )
313        return *this;
314    else
315        return value->Lc();
316}
317
318CanonicalForm
319CanonicalForm::LC () const
320{
321    if ( is_imm( value ) )
322        return *this;
323    else
324        return value->LC();
325}
326
327CanonicalForm
328CanonicalForm::LC ( const Variable & v ) const
329{
330    if ( is_imm( value ) || value->inCoeffDomain() )
331        return *this;
332
333    Variable x = value->variable();
334    if ( v > x )
335        return *this;
336    else if ( v == x )
337        return value->LC();
338    else {
339        CanonicalForm f = swapvar( *this, v, x );
340         if ( f.mvar() == x )
341             return swapvar( f.value->LC(), v, x );
342         else
343            // v did not occur in f
344            return *this;
345    }
346}
347//}}}
348
349//{{{ int CanonicalForm::degree (), degree ( v ) const
350//{{{ docu
351//
352// degree() - degree methods.
353//
354// Both methods returns -1 for the zero polynomial and 0 if
355// CO is in a base domain.
356//
357// degree() returns the degree of CO in its main variable.
358// Elements in an algebraic extension are considered polynomials.
359//
360// degree( v ) returns the degree of CO with respect to v.
361// Elements in an algebraic extension are considered polynomials,
362// and v may be algebraic.
363//
364// See also: InternalCf::degree(), InternalPoly::degree(),
365// ::degree(), ::degree( v )
366//
367//}}}
368int
369CanonicalForm::degree() const
370{
371    int what = is_imm( value );
372    if ( what )
373        if ( what == FFMARK )
374            return imm_iszero_p( value ) ? -1 : 0;
375        else if ( what == INTMARK )
376            return imm_iszero( value ) ? -1 : 0;
377        else
378            return imm_iszero_gf( value ) ? -1 : 0;
379    else
380        return value->degree();
381}
382
383int
384CanonicalForm::degree( const Variable & v ) const
385{
386    int what = is_imm( value );
387#if 0
388    if ( what )
389        if ( what == FFMARK )
390            return imm_iszero_p( value ) ? -1 : 0;
391        else if ( what == INTMARK )
392            return imm_iszero( value ) ? -1 : 0;
393        else
394            return imm_iszero_gf( value ) ? -1 : 0;
395    else if ( value->inBaseDomain() )
396        return value->degree();
397#else
398    switch(what)
399    {
400      case FFMARK: return imm_iszero_p( value ) ? -1 : 0;
401      case INTMARK: return imm_iszero( value ) ? -1 : 0;
402      case GFMARK:  return imm_iszero_gf( value ) ? -1 : 0;
403      case 0: if ( value->inBaseDomain() )
404              return value->degree();
405              break;
406    }
407#endif
408
409    Variable x = value->variable();
410    if ( v == x )
411        return value->degree();
412    else  if ( v > x )
413        // relatively to v, f is in a coefficient ring
414        return 0;
415    else {
416        int coeffdeg, result = 0;
417        // search for maximum of coefficient degree
418        for ( CFIterator i = *this; i.hasTerms(); i++ ) {
419            coeffdeg = i.coeff().degree( v );
420            if ( coeffdeg > result )
421                result = coeffdeg;
422        }
423        return result;
424    }
425}
426//}}}
427
428//{{{ CanonicalForm CanonicalForm::tailcoeff (), int CanonicalForm::taildegree () const
429//{{{ docu
430//
431// tailcoeff(), taildegree() - return least coefficient and
432//   degree, resp.
433//
434// tailcoeff() returns the coefficient of the term with the least
435// degree in CO where CO is considered an univariate polynomial
436// in its main variable.  Elements in an algebraic extension are
437// considered coefficients.
438//
439// taildegree() returns -1 for the zero polynomial, 0 if CO is in
440// a base domain, otherwise the least degree of CO where CO is
441// considered a univariate polynomial in its main variable.  In
442// contrast to tailcoeff(), elements in an algebraic extension
443// are considered polynomials, not coefficients, and such may
444// have a taildegree larger than zero.
445//
446// tailcoeff( v ) returns the tail coefficient of CO where CO is
447// considered an univariate polynomial in the polynomial variable
448// v.
449// Note: If v is less than the main variable of CO we have to
450// swap variables which may be quite expensive.
451//
452// See also: InternalCF::tailcoeff(), InternalCF::tailcoeff(),
453// InternalPoly::tailcoeff(), InternalPoly::taildegree,
454// ::tailcoeff(), ::taildegree()
455//
456//}}}
457CanonicalForm
458CanonicalForm::tailcoeff () const
459{
460    if ( is_imm( value ) || value->inCoeffDomain() )
461        return *this;
462    else
463        return value->tailcoeff();
464}
465
466CanonicalForm
467CanonicalForm::tailcoeff (const Variable& v) const
468{
469    if ( is_imm( value ) || value->inCoeffDomain() )
470        return *this;
471
472    Variable x = value->variable();
473    if ( v > x )
474        return *this;
475    else if ( v == x )
476        return value->tailcoeff();
477    else {
478        CanonicalForm f = swapvar( *this, v, x );
479         if ( f.mvar() == x )
480             return swapvar( f.value->tailcoeff(), v, x );
481         else
482            // v did not occur in f
483            return *this;
484    }
485}
486
487int
488CanonicalForm::taildegree () const
489{
490    int what = is_imm( value );
491    if ( what )
492        if ( what == FFMARK )
493            return imm_iszero_p( value ) ? -1 : 0;
494        else if ( what == INTMARK )
495            return imm_iszero( value ) ? -1 : 0;
496        else
497            return imm_iszero_gf( value ) ? -1 : 0;
498    else
499        return value->taildegree();
500}
501//}}}
502
503//{{{ int CanonicalForm::level (), Variable CanonicalForm::mvar () const
504//{{{ docu
505//
506// level(), mvar() - return level and main variable of CO.
507//
508// level() returns the level of CO.  For a list of the levels and
509// their meanings, see cf_defs.h.
510//
511// mvar() returns the main variable of CO or Variable() if CO is
512// in a base domain.
513//
514// See also: InternalCF::level(), InternalCF::variable(),
515// InternalPoly::level(), InternalPoly::variable(), ::level(),
516// ::mvar()
517//
518//}}}
519int
520CanonicalForm::level () const
521{
522    if ( is_imm( value ) )
523        return LEVELBASE;
524    else
525        return value->level();
526}
527
528Variable
529CanonicalForm::mvar () const
530{
531    if ( is_imm( value ) )
532        return Variable();
533    else
534        return value->variable();
535}
536//}}}
537
538//{{{ CanonicalForm CanonicalForm::num (), den () const
539//{{{ docu
540//
541// num(), den() - return numinator and denominator of CO.
542//
543// num() returns the numinator of CO if CO is a rational number,
544// CO itself otherwise.
545//
546// den() returns the denominator of CO if CO is a rational
547// number, 1 (from the current domain!) otherwise.
548//
549// See also: InternalCF::num(), InternalCF::den(),
550// InternalRational::num(), InternalRational::den(), ::num(),
551// ::den()
552//
553//}}}
554CanonicalForm
555CanonicalForm::num () const
556{
557    if ( is_imm( value ) )
558        return *this;
559    else
560        return CanonicalForm( value->num() );
561}
562
563CanonicalForm
564CanonicalForm::den () const
565{
566    if ( is_imm( value ) )
567        return CanonicalForm( 1 );
568    else
569        return CanonicalForm( value->den() );
570}
571//}}}
572
573//{{{ assignment operators
574CanonicalForm &
575CanonicalForm::operator += ( const CanonicalForm & cf )
576{
577    int what = is_imm( value );
578    if ( what ) {
579        ASSERT ( ! is_imm( cf.value ) || (what==is_imm( cf.value )), "illegal base coefficients" );
580        if ( (what = is_imm( cf.value )) == FFMARK )
581            value = imm_add_p( value, cf.value );
582        else  if ( what == GFMARK )
583            value = imm_add_gf( value, cf.value );
584        else  if ( what )
585            value = imm_add( value, cf.value );
586        else {
587            InternalCF * dummy = cf.value->copyObject();
588            value = dummy->addcoeff( value );
589        }
590    }
591    else  if ( is_imm( cf.value ) )
592        value = value->addcoeff( cf.value );
593    else  if ( value->level() == cf.value->level() ) {
594        if ( value->levelcoeff() == cf.value->levelcoeff() )
595            value = value->addsame( cf.value );
596        else  if ( value->levelcoeff() > cf.value->levelcoeff() )
597            value = value->addcoeff( cf.value );
598        else {
599            InternalCF * dummy = cf.value->copyObject();
600            dummy = dummy->addcoeff( value );
601            if ( value->deleteObject() ) delete value;
602            value = dummy;
603        }
604    }
605    else  if ( level() > cf.level() )
606        value = value->addcoeff( cf.value );
607    else {
608        InternalCF * dummy = cf.value->copyObject();
609        dummy = dummy->addcoeff( value );
610        if ( value->deleteObject() ) delete value;
611        value = dummy;
612    }
613    return *this;
614}
615
616CanonicalForm &
617CanonicalForm::operator -= ( const CanonicalForm & cf )
618{
619    int what = is_imm( value );
620    if ( what ) {
621        ASSERT ( ! is_imm( cf.value ) || (what==is_imm( cf.value )), "illegal base coefficients" );
622        if ( (what = is_imm( cf.value )) == FFMARK )
623            value = imm_sub_p( value, cf.value );
624        else  if ( what == GFMARK )
625            value = imm_sub_gf( value, cf.value );
626        else  if ( what )
627            value = imm_sub( value, cf.value );
628        else {
629            InternalCF * dummy = cf.value->copyObject();
630            value = dummy->subcoeff( value, true );
631        }
632    }
633    else  if ( is_imm( cf.value ) )
634        value = value->subcoeff( cf.value, false );
635    else  if ( value->level() == cf.value->level() ) {
636        if ( value->levelcoeff() == cf.value->levelcoeff() )
637            value = value->subsame( cf.value );
638        else  if ( value->levelcoeff() > cf.value->levelcoeff() )
639            value = value->subcoeff( cf.value, false );
640        else {
641            InternalCF * dummy = cf.value->copyObject();
642            dummy = dummy->subcoeff( value, true );
643            if ( value->deleteObject() ) delete value;
644            value = dummy;
645        }
646    }
647    else  if ( level() > cf.level() )
648        value = value->subcoeff( cf.value, false );
649    else {
650        InternalCF * dummy = cf.value->copyObject();
651        dummy = dummy->subcoeff( value, true );
652        if ( value->deleteObject() ) delete value;
653        value = dummy;
654    }
655    return *this;
656}
657
658CanonicalForm &
659CanonicalForm::operator *= ( const CanonicalForm & cf )
660{
661    int what = is_imm( value );
662    if ( what ) {
663        ASSERT ( ! is_imm( cf.value ) || (what==is_imm( cf.value )), "illegal base coefficients" );
664        if ( (what = is_imm( cf.value )) == FFMARK )
665            value = imm_mul_p( value, cf.value );
666        else  if ( what == GFMARK )
667            value = imm_mul_gf( value, cf.value );
668        else  if ( what )
669            value = imm_mul( value, cf.value );
670        else {
671            InternalCF * dummy = cf.value->copyObject();
672            value = dummy->mulcoeff( value );
673        }
674    }
675    else  if ( is_imm( cf.value ) )
676        value = value->mulcoeff( cf.value );
677    else  if ( value->level() == cf.value->level() ) {
678        if (value->levelcoeff() == cf.value->levelcoeff() && cf.isUnivariate() && (*this).isUnivariate())
679        {
680          if (value->level() < 0 || CFFactory::gettype() == GaloisFieldDomain || (size (cf) <= 10 || size (*this) <= 10) )
681            value = value->mulsame( cf.value );
682          else
683            *this= mulNTL (*this, cf);
684        }
685        else if (value->levelcoeff() == cf.value->levelcoeff() && (!cf.isUnivariate() || !(*this).isUnivariate()))
686            value = value->mulsame( cf.value );
687        else  if ( value->levelcoeff() > cf.value->levelcoeff() )
688            value = value->mulcoeff( cf.value );
689        else {
690            InternalCF * dummy = cf.value->copyObject();
691            dummy = dummy->mulcoeff( value );
692            if ( value->deleteObject() ) delete value;
693            value = dummy;
694        }
695    }
696    else  if ( level() > cf.level() )
697        value = value->mulcoeff( cf.value );
698    else {
699        InternalCF * dummy = cf.value->copyObject();
700        dummy = dummy->mulcoeff( value );
701        if ( value->deleteObject() ) delete value;
702        value = dummy;
703    }
704    return *this;
705}
706
707CanonicalForm &
708CanonicalForm::operator /= ( const CanonicalForm & cf )
709{
710    int what = is_imm( value );
711    if ( what ) {
712        ASSERT ( ! is_imm( cf.value ) || (what==is_imm( cf.value )), "illegal base coefficients" );
713        if ( (what = is_imm( cf.value )) == FFMARK )
714            value = imm_div_p( value, cf.value );
715        else  if ( what == GFMARK )
716            value = imm_div_gf( value, cf.value );
717        else  if ( what )
718            value = imm_divrat( value, cf.value );
719        else {
720            InternalCF * dummy = cf.value->copyObject();
721            value = dummy->dividecoeff( value, true );
722        }
723    }
724    else  if ( is_imm( cf.value ) )
725        value = value->dividecoeff( cf.value, false );
726    else  if ( value->level() == cf.value->level() ) {
727        if ( value->levelcoeff() == cf.value->levelcoeff() )
728            value = value->dividesame( cf.value );
729        else  if ( value->levelcoeff() > cf.value->levelcoeff() )
730            value = value->dividecoeff( cf.value, false );
731        else {
732            InternalCF * dummy = cf.value->copyObject();
733            dummy = dummy->dividecoeff( value, true );
734            if ( value->deleteObject() ) delete value;
735            value = dummy;
736        }
737    }
738    else  if ( level() > cf.level() )
739        value = value->dividecoeff( cf.value, false );
740    else {
741        InternalCF * dummy = cf.value->copyObject();
742        dummy = dummy->dividecoeff( value, true );
743        if ( value->deleteObject() ) delete value;
744        value = dummy;
745    }
746    return *this;
747}
748
749CanonicalForm &
750CanonicalForm::div ( const CanonicalForm & cf )
751{
752    int what = is_imm( value );
753    if ( what ) {
754        ASSERT ( ! is_imm( cf.value ) || (what==is_imm( cf.value )), "illegal base coefficients" );
755        if ( (what = is_imm( cf.value )) == FFMARK )
756            value = imm_div_p( value, cf.value );
757        else  if ( what == GFMARK )
758            value = imm_div_gf( value, cf.value );
759        else  if ( what )
760            value = imm_div( value, cf.value );
761        else {
762            InternalCF * dummy = cf.value->copyObject();
763            value = dummy->divcoeff( value, true );
764        }
765    }
766    else  if ( is_imm( cf.value ) )
767        value = value->divcoeff( cf.value, false );
768    else  if ( value->level() == cf.value->level() ) {
769        if ( value->levelcoeff() == cf.value->levelcoeff() )
770            value = value->divsame( cf.value );
771        else  if ( value->levelcoeff() > cf.value->levelcoeff() )
772            value = value->divcoeff( cf.value, false );
773        else {
774            InternalCF * dummy = cf.value->copyObject();
775            dummy = dummy->divcoeff( value, true );
776            if ( value->deleteObject() ) delete value;
777            value = dummy;
778        }
779    }
780    else  if ( level() > cf.level() )
781        value = value->divcoeff( cf.value, false );
782    else {
783        InternalCF * dummy = cf.value->copyObject();
784        dummy = dummy->divcoeff( value, true );
785        if ( value->deleteObject() ) delete value;
786        value = dummy;
787    }
788    return *this;
789}
790
791//same as divremt but handles zero divisors in case we are in Z_p[x]/(f) where f is not irreducible
792CanonicalForm &
793CanonicalForm::tryDiv ( const CanonicalForm & cf, const CanonicalForm& M, bool& fail )
794{
795    ASSERT (getCharacteristic() > 0, "expected positive characteristic");
796    ASSERT (!getReduce (M.mvar()), "do not reduce modulo M");
797    fail= false;
798    int what = is_imm( value );
799    if ( what ) {
800        ASSERT ( ! is_imm( cf.value ) || (what==is_imm( cf.value )), "illegal base coefficients" );
801        if ( (what = is_imm( cf.value )) == FFMARK )
802            value = imm_div_p( value, cf.value );
803        else  if ( what == GFMARK )
804            value = imm_div_gf( value, cf.value );
805        else {
806            InternalCF * dummy = cf.value->copyObject();
807            value = dummy->divcoeff( value, true );
808        }
809    }
810    else  if ( is_imm( cf.value ) )
811        value = value->tryDivcoeff (cf.value, false, M, fail);
812    else  if ( value->level() == cf.value->level() ) {
813        if ( value->levelcoeff() == cf.value->levelcoeff() )
814            value = value->tryDivsame( cf.value, M, fail );
815        else  if ( value->levelcoeff() > cf.value->levelcoeff() )
816            value = value->tryDivcoeff( cf.value, false, M, fail );
817        else {
818            InternalCF * dummy = cf.value->copyObject();
819            dummy = dummy->tryDivcoeff( value, true, M, fail );
820            if ( value->deleteObject() ) delete value;
821            value = dummy;
822        }
823    }
824    else  if ( level() > cf.level() )
825        value = value->tryDivcoeff( cf.value, false, M, fail );
826    else {
827        InternalCF * dummy = cf.value->copyObject();
828        dummy = dummy->tryDivcoeff( value, true, M, fail );
829        if ( value->deleteObject() ) delete value;
830        value = dummy;
831    }
832    return *this;
833}
834
835CanonicalForm &
836CanonicalForm::operator %= ( const CanonicalForm & cf )
837{
838    int what = is_imm( value );
839    if ( what ) {
840        ASSERT ( ! is_imm( cf.value ) || (what==is_imm( cf.value )), "illegal base coefficients" );
841        if ( (what = is_imm( cf.value )) == FFMARK )
842            value = imm_mod_p( value, cf.value );
843        else  if ( what == GFMARK )
844            value = imm_mod_gf( value, cf.value );
845        else  if ( what )
846            value = imm_mod( value, cf.value );
847        else {
848            InternalCF * dummy = cf.value->copyObject();
849            value = dummy->modulocoeff( value, true );
850        }
851    }
852    else  if ( is_imm( cf.value ) )
853        value = value->modulocoeff( cf.value, false );
854    else  if ( value->level() == cf.value->level() ) {
855        if ( value->levelcoeff() == cf.value->levelcoeff() )
856            value = value->modulosame( cf.value );
857        else  if ( value->levelcoeff() > cf.value->levelcoeff() )
858            value = value->modulocoeff( cf.value, false );
859        else {
860            InternalCF * dummy = cf.value->copyObject();
861            dummy = dummy->modulocoeff( value, true );
862            if ( value->deleteObject() ) delete value;
863            value = dummy;
864        }
865    }
866    else  if ( level() > cf.level() )
867        value = value->modulocoeff( cf.value, false );
868    else {
869        InternalCF * dummy = cf.value->copyObject();
870        dummy = dummy->modulocoeff( value, true );
871        if ( value->deleteObject() ) delete value;
872        value = dummy;
873    }
874    return *this;
875}
876
877CanonicalForm &
878CanonicalForm::mod ( const CanonicalForm & cf )
879{
880    int what = is_imm( value );
881    if ( what ) {
882        ASSERT ( ! is_imm( cf.value ) || (what==is_imm( cf.value )), "illegal base coefficients" );
883        if ( (what = is_imm( cf.value )) == FFMARK )
884            value = imm_mod_p( value, cf.value );
885        else  if ( what == GFMARK )
886            value = imm_mod_gf( value, cf.value );
887        else  if ( what )
888            value = imm_mod( value, cf.value );
889        else {
890            InternalCF * dummy = cf.value->copyObject();
891            value = dummy->modcoeff( value, true );
892        }
893    }
894    else  if ( is_imm( cf.value ) )
895        value = value->modcoeff( cf.value, false );
896    else  if ( value->level() == cf.value->level() ) {
897        if ( value->levelcoeff() == cf.value->levelcoeff() )
898            value = value->modsame( cf.value );
899        else  if ( value->levelcoeff() > cf.value->levelcoeff() )
900            value = value->modcoeff( cf.value, false );
901        else {
902            InternalCF * dummy = cf.value->copyObject();
903            dummy = dummy->modcoeff( value, true );
904            if ( value->deleteObject() ) delete value;
905            value = dummy;
906        }
907    }
908    else  if ( level() > cf.level() )
909        value = value->modcoeff( cf.value, false );
910    else {
911        InternalCF * dummy = cf.value->copyObject();
912        dummy = dummy->modcoeff( value, true );
913        if ( value->deleteObject() ) delete value;
914        value = dummy;
915    }
916    return *this;
917}
918
919void
920divrem ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & q, CanonicalForm & r )
921{
922    InternalCF * qq = 0, * rr = 0;
923    int what = is_imm( f.value );
924    if ( what )
925        if ( is_imm( g.value ) ) {
926            if ( what == FFMARK )
927                imm_divrem_p( f.value, g.value, qq, rr );
928            else  if ( what == GFMARK )
929                imm_divrem_gf( f.value, g.value, qq, rr );
930            else
931                imm_divrem( f.value, g.value, qq, rr );
932        }
933        else
934            g.value->divremcoeff( f.value, qq, rr, true );
935    else  if ( (what=is_imm( g.value )) )
936        f.value->divremcoeff( g.value, qq, rr, false );
937    else  if ( f.value->level() == g.value->level() )
938        if ( f.value->levelcoeff() == g.value->levelcoeff() )
939            f.value->divremsame( g.value, qq, rr );
940        else  if ( f.value->levelcoeff() > g.value->levelcoeff() )
941            f.value->divremcoeff( g.value, qq, rr, false );
942        else
943            g.value->divremcoeff( f.value, qq, rr, true );
944    else  if ( f.value->level() > g.value->level() )
945        f.value->divremcoeff( g.value, qq, rr, false );
946    else
947        g.value->divremcoeff( f.value, qq, rr, true );
948    ASSERT( qq != 0 && rr != 0, "error in divrem" );
949    q = CanonicalForm( qq );
950    r = CanonicalForm( rr );
951}
952
953bool
954divremt ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & q, CanonicalForm & r )
955{
956    InternalCF * qq = 0, * rr = 0;
957    int what = is_imm( f.value );
958    bool result = true;
959    if ( what )
960        if ( is_imm( g.value ) ) {
961            if ( what == FFMARK )
962                imm_divrem_p( f.value, g.value, qq, rr );
963            else  if ( what == GFMARK )
964                imm_divrem_gf( f.value, g.value, qq, rr );
965            else
966                imm_divrem( f.value, g.value, qq, rr );
967        }
968        else
969            result = g.value->divremcoefft( f.value, qq, rr, true );
970    else  if ( (what=is_imm( g.value )) )
971        result = f.value->divremcoefft( g.value, qq, rr, false );
972    else  if ( f.value->level() == g.value->level() )
973        if ( f.value->levelcoeff() == g.value->levelcoeff() )
974            result = f.value->divremsamet( g.value, qq, rr );
975        else  if ( f.value->levelcoeff() > g.value->levelcoeff() )
976            result = f.value->divremcoefft( g.value, qq, rr, false );
977        else
978            result = g.value->divremcoefft( f.value, qq, rr, true );
979    else  if ( f.value->level() > g.value->level() )
980        result = f.value->divremcoefft( g.value, qq, rr, false );
981    else
982        result = g.value->divremcoefft( f.value, qq, rr, true );
983    if ( result ) {
984        ASSERT( qq != 0 && rr != 0, "error in divrem" );
985        q = CanonicalForm( qq );
986        r = CanonicalForm( rr );
987    }
988    else {
989        q = 0; r = 0;
990    }
991    return result;
992}
993
994//same as divremt but handles zero divisors in case we are in Z_p[x]/(f) where f is not irreducible
995bool
996tryDivremt ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & q, CanonicalForm & r, const CanonicalForm& M, bool& fail )
997{
998    ASSERT (getCharacteristic() > 0, "expected positive characteristic");
999    ASSERT (!getReduce (M.mvar()), "do not reduce modulo M");
1000    fail= false;
1001    InternalCF * qq = 0, * rr = 0;
1002    int what = is_imm( f.value );
1003    bool result = true;
1004    if ( what )
1005        if ( is_imm( g.value ) ) {
1006            if ( what == FFMARK )
1007                imm_divrem_p( f.value, g.value, qq, rr );
1008            else  if ( what == GFMARK )
1009                imm_divrem_gf( f.value, g.value, qq, rr );
1010        }
1011        else
1012            result = g.value->tryDivremcoefft( f.value, qq, rr, true, M, fail );
1013    else  if ( (what=is_imm( g.value )) )
1014        result = f.value->tryDivremcoefft( g.value, qq, rr, false, M, fail );
1015    else  if ( f.value->level() == g.value->level() )
1016        if ( f.value->levelcoeff() == g.value->levelcoeff() )
1017            result = f.value->tryDivremsamet( g.value, qq, rr, M, fail );
1018        else  if ( f.value->levelcoeff() > g.value->levelcoeff() )
1019            result = f.value->tryDivremcoefft( g.value, qq, rr, false, M, fail );
1020        else
1021            result = g.value->tryDivremcoefft( f.value, qq, rr, true, M, fail );
1022    else  if ( f.value->level() > g.value->level() )
1023        result = f.value->tryDivremcoefft( g.value, qq, rr, false, M, fail );
1024    else
1025        result = g.value->tryDivremcoefft( f.value, qq, rr, true, M, fail );
1026    if (fail)
1027    {
1028      q= 0;
1029      r= 0;
1030      return false;
1031    }
1032    if ( result ) {
1033        ASSERT( qq != 0 && rr != 0, "error in divrem" );
1034        q = CanonicalForm( qq );
1035        r = CanonicalForm( rr );
1036        q= reduce (q, M);
1037        r= reduce (r, M);
1038    }
1039    else {
1040        q = 0; r = 0;
1041    }
1042    return result;
1043}
1044
1045//}}}
1046
1047//{{{ CanonicalForm CanonicalForm::operator () ( f ), operator () ( f, v ) const
1048//{{{ docu
1049//
1050// operator ()() - evaluation operator.
1051//
1052// Both operators return CO if CO is in a base domain.
1053//
1054// operator () ( f ) returns CO with f inserted for the main
1055// variable.  Elements in an algebraic extension are considered
1056// polynomials.
1057//
1058// operator () ( f, v ) returns CO with f inserted for v.
1059// Elements in an algebraic extension are considered polynomials
1060// and v may be an algebraic variable.
1061//
1062//}}}
1063CanonicalForm
1064CanonicalForm::operator () ( const CanonicalForm & f ) const
1065{
1066    if ( is_imm( value ) || value->inBaseDomain() )
1067        return *this;
1068    else {
1069#if 0
1070        CFIterator i = *this;
1071        int lastExp = i.exp();
1072        CanonicalForm result = i.coeff();
1073        i++;
1074        while ( i.hasTerms() ) {
1075            if ( (lastExp - i.exp()) == 1 )
1076                result *= f;
1077            else
1078                result *= power( f, lastExp - i.exp() );
1079            result += i.coeff();
1080            lastExp = i.exp();
1081            i++;
1082        }
1083        if ( lastExp != 0 )
1084            result *= power( f, lastExp );
1085#else
1086        CFIterator i = *this;
1087        int lastExp = i.exp();
1088        CanonicalForm result = i.coeff();
1089        i++;
1090        while ( i.hasTerms() )
1091        {
1092            int i_exp=i.exp();
1093            if ( (lastExp - i_exp /* i.exp()*/) == 1 )
1094                result *= f;
1095            else
1096                result *= power( f, lastExp - i_exp /*i.exp()*/ );
1097            result += i.coeff();
1098            lastExp = i_exp /*i.exp()*/;
1099            i++;
1100        }
1101        if ( lastExp != 0 )
1102            result *= power( f, lastExp );
1103#endif
1104        return result;
1105    }
1106}
1107
1108CanonicalForm
1109CanonicalForm::operator () ( const CanonicalForm & f, const Variable & v ) const
1110{
1111    if ( is_imm( value ) || value->inBaseDomain() )
1112        return *this;
1113
1114    Variable x = value->variable();
1115    if ( v > x )
1116        return *this;
1117    else  if ( v == x )
1118        return (*this)( f );
1119    else {
1120        // v is less than main variable of f
1121        CanonicalForm result = 0;
1122        for ( CFIterator i = *this; i.hasTerms(); i++ )
1123            result += i.coeff()( f, v ) * power( x, i.exp() );
1124        return result;
1125    }
1126}
1127//}}}
1128
1129//{{{ CanonicalForm CanonicalForm::operator [] ( int i ) const
1130//{{{ docu
1131//
1132// operator []() - return i'th coefficient from CO.
1133//
1134// Returns CO if CO is in a base domain and i equals zero.
1135// Returns zero (from the current domain) if CO is in a base
1136// domain and i is larger than zero.  Otherwise, returns the
1137// coefficient to x^i in CO (if x denotes the main variable of
1138// CO) or zero if CO does not contain x^i.  Elements in an
1139// algebraic extension are considered polynomials.  i should be
1140// larger or equal zero.
1141//
1142// Note: Never use a loop like
1143//
1144// for ( int i = degree( f ); i >= 0; i-- )
1145//     foo( i, f[ i ] );
1146//
1147// which is much slower than
1148//
1149// for ( int i = degree( f ), CFIterator I = f; I.hasTerms(); I++ ) {
1150//     // fill gap with zeroes
1151//     for ( ; i > I.exp(); i-- )
1152//         foo( i, 0 );
1153//     // at this point, i == I.exp()
1154//     foo( i, i.coeff() );
1155//     i--;
1156// }
1157// // work through trailing zeroes
1158// for ( ; i >= 0; i-- )
1159//     foo( i, 0 );
1160//
1161//}}}
1162CanonicalForm
1163CanonicalForm::operator [] ( int i ) const
1164{
1165    ASSERT( i >= 0, "index to operator [] less than zero" );
1166    if ( is_imm( value ) )
1167        if ( i == 0 )
1168            return *this;
1169        else
1170            return CanonicalForm( 0 );
1171    else
1172        return value->coeff( i );
1173}
1174//}}}
1175
1176//{{{ CanonicalForm CanonicalForm::deriv (), deriv ( x )
1177//{{{ docu
1178//
1179// deriv() - return the formal derivation of CO.
1180//
1181// deriv() derives CO with respect to its main variable.  Returns
1182// zero from the current domain if f is in a coefficient domain.
1183//
1184// deriv( x ) derives CO with respect to x.  x should be a
1185// polynomial variable.  Returns zero from the current domain if
1186// f is in a coefficient domain.
1187//
1188// See also: ::deriv()
1189//
1190//}}}
1191CanonicalForm
1192CanonicalForm::deriv () const
1193{
1194    if ( is_imm( value ) || value->inCoeffDomain() )
1195        return CanonicalForm( 0 );
1196    else {
1197        CanonicalForm result = 0;
1198        Variable x = value->variable();
1199        for ( CFIterator i = *this; i.hasTerms(); i++ )
1200            if ( i.exp() > 0 )
1201                result += power( x, i.exp()-1 ) * i.coeff() * i.exp();
1202        return result;
1203    }
1204}
1205
1206CanonicalForm
1207CanonicalForm::deriv ( const Variable & x ) const
1208{
1209    ASSERT( x.level() > 0, "cannot derive with respect to algebraic variables" );
1210    if ( is_imm( value ) || value->inCoeffDomain() )
1211        return CanonicalForm( 0 );
1212
1213    Variable y = value->variable();
1214    if ( x > y )
1215        return CanonicalForm( 0 );
1216    else if ( x == y )
1217        return deriv();
1218    else {
1219        CanonicalForm result = 0;
1220        for ( CFIterator i = *this; i.hasTerms(); i++ )
1221            result += i.coeff().deriv( x ) * power( y, i.exp() );
1222        return result;
1223    }
1224}
1225//}}}
1226
1227//{{{ int CanonicalForm::sign () const
1228//{{{ docu
1229//
1230// sign() - return sign of CO.
1231//
1232// If CO is an integer or a rational number, the sign is defined
1233// as usual.  If CO is an element of a prime power domain or of
1234// FF(p) and SW_SYMMETRIC_FF is on, the sign of CO is the sign of
1235// the symmetric representation of CO.  If CO is in GF(q) or in
1236// FF(p) and SW_SYMMETRIC_FF is off, the sign of CO is zero iff
1237// CO is zero, otherwise the sign is one.
1238//
1239// If CO is a polynomial or in an extension of one of the base
1240// domains, the sign of CO is the sign of its leading
1241// coefficient.
1242//
1243// See also: InternalCF::sign(), InternalInteger::sign(),
1244// InternalPrimePower::sign(), InternalRational::sign(),
1245// InternalPoly::sign(), imm_sign(), gf_sign()
1246//
1247//}}}
1248int
1249CanonicalForm::sign () const
1250{
1251    if ( is_imm( value ) )
1252        return imm_sign( value );
1253    else
1254        return value->sign();
1255}
1256//}}}
1257
1258//{{{ CanonicalForm CanonicalForm::sqrt () const
1259//{{{ docu
1260//
1261// sqrt() - calculate integer square root.
1262//
1263// CO has to be an integer greater or equal zero.  Returns the
1264// largest integer less or equal sqrt(CO).
1265//
1266// In the immediate case, we use the newton method to find the
1267// root.  The algorithm is from H. Cohen - 'A Course in
1268// Computational Algebraic Number Theory', ch. 1.7.1.
1269//
1270// See also: InternalCF::sqrt(), InternalInteger::sqrt(), ::sqrt()
1271//
1272//}}}
1273CanonicalForm
1274CanonicalForm::sqrt () const
1275{
1276    if ( is_imm( value ) ) {
1277        ASSERT( is_imm( value ) == INTMARK, "sqrt() not implemented" );
1278        long n = imm2int( value );
1279        ASSERT( n >= 0, "arg to sqrt() less than zero" );
1280        if ( n == 0 || n == 1 )
1281            return CanonicalForm( n );
1282        else {
1283            long x, y = n;
1284            do {
1285                x = y;
1286                // the intermediate result may not fit into an
1287                // integer, but the result does
1288                y = (unsigned long)(x + n/x)/2;
1289            } while ( y < x );
1290            return CanonicalForm( x );
1291        }
1292    }
1293    else
1294        return CanonicalForm( value->sqrt() );
1295}
1296//}}}
1297
1298//{{{ int CanonicalForm::ilog2 () const
1299//{{{ docu
1300//
1301// ilog2() - integer logarithm to base 2.
1302//
1303// Returns the largest integer less or equal logarithm of CO to
1304// base 2.  CO should be a positive integer.
1305//
1306// See also: InternalCF::ilog2(), InternalInteger::ilog2(), ::ilog2()
1307//
1308//}}}
1309int
1310CanonicalForm::ilog2 () const
1311{
1312    if ( is_imm( value ) )
1313    {
1314        ASSERT( is_imm( value ) == INTMARK, "ilog2() not implemented" );
1315        long a = imm2int( value );
1316        ASSERT( a > 0, "arg to ilog2() less or equal zero" );
1317        int n = -1;
1318        while ( a > 0 )
1319        {
1320          n++;
1321          a /=2;
1322        }
1323        return n;
1324    }
1325    else
1326        return value->ilog2();
1327}
1328//}}}
1329
1330//{{{ bool operator ==, operator != ( const CanonicalForm & lhs, const CanonicalForm & rhs )
1331//{{{ docu
1332//
1333// operator ==(), operator !=() - compare canonical forms on
1334//   (in)equality.
1335//
1336// operator ==() returns true iff lhs equals rhs.
1337// operator !=() returns true iff lhs does not equal rhs.
1338//
1339// This is the point in factory where we essentially use that
1340// CanonicalForms in fact are canonical.  There must not be two
1341// different representations of the same mathematical object,
1342// otherwise, such (in)equality will not be recognized by these
1343// operators.  In other word, we rely on the fact that structural
1344// different factory objects in any case represent different
1345// mathematical objects.
1346//
1347// So we use the following procedure to test on equality (and
1348// analogously on inequality).  First, we check whether lhs.value
1349// equals rhs.value.  If so we are ready and return true.
1350// Second, if one of the operands is immediate, but the other one
1351// not, we return false.  Third, if the operand's levels differ
1352// we return false.  Fourth, if the operand's levelcoeffs differ
1353// we return false.  At last, we call the corresponding internal
1354// method to compare both operands.
1355//
1356// Both operands should have coefficients from the same base domain.
1357//
1358// Note: To compare with the zero or the unit of the current domain,
1359// you better use the methods `CanonicalForm::isZero()' or
1360// `CanonicalForm::isOne()', resp., than something like `f == 0',
1361// since the latter is quite a lot slower.
1362//
1363// See also: InternalCF::comparesame(),
1364// InternalInteger::comparesame(), InternalRational::comparesame(),
1365// InternalPrimePower::comparesame(), InternalPoly::comparesame()
1366//
1367//}}}
1368bool
1369operator == ( const CanonicalForm & lhs, const CanonicalForm & rhs )
1370{
1371    if ( lhs.value == rhs.value )
1372        return true;
1373    else if ( is_imm( rhs.value ) || is_imm( lhs.value ) ) {
1374        ASSERT( ! is_imm( rhs.value ) ||
1375                ! is_imm( lhs.value ) ||
1376                is_imm( rhs.value ) == is_imm( lhs.value ),
1377                "incompatible operands" );
1378        return false;
1379    }
1380    else  if ( lhs.value->level() != rhs.value->level() )
1381        return false;
1382    else  if ( lhs.value->levelcoeff() != rhs.value->levelcoeff() )
1383        return false;
1384    else
1385        return rhs.value->comparesame( lhs.value ) == 0;
1386}
1387
1388bool
1389operator != ( const CanonicalForm & lhs, const CanonicalForm & rhs )
1390{
1391    if ( lhs.value == rhs.value )
1392        return false;
1393    else if ( is_imm( rhs.value ) || is_imm( lhs.value ) ) {
1394        ASSERT( ! is_imm( rhs.value ) ||
1395                ! is_imm( lhs.value ) ||
1396                is_imm( rhs.value ) == is_imm( lhs.value ),
1397                "incompatible operands" );
1398        return true;
1399    }
1400    else  if ( lhs.value->level() != rhs.value->level() )
1401        return true;
1402    else  if ( lhs.value->levelcoeff() != rhs.value->levelcoeff() )
1403        return true;
1404    else        return rhs.value->comparesame( lhs.value ) != 0;
1405}
1406//}}}
1407
1408//{{{ bool operator >, operator < ( const CanonicalForm & lhs, const CanonicalForm & rhs )
1409//{{{ docu
1410//
1411// operator >(), operator <() - compare canonical forms. on size or
1412//   level.
1413//
1414// The most common and most useful application of these operators
1415// is to compare two integers or rationals, of course.  However,
1416// these operators are defined on all other base domains and on
1417// polynomials, too.  From a mathematical point of view this may
1418// seem meaningless, since there is no ordering on finite fields
1419// or on polynomials respecting the algebraic structure.
1420// Nevertheless, from a programmer's point of view it may be
1421// sensible to order these objects, e.g. to sort them.
1422//
1423// Therefore, the ordering defined by these operators in any case
1424// is a total ordering which fulfills the law of trichotomy.
1425//
1426// It is clear how this is done in the case of the integers and
1427// the rationals.  For finite fields, all you can say is that
1428// zero is the minimal element w.r.t. the ordering, the other
1429// elements are ordered in an arbitrary (but total!)  way.  For
1430// polynomials, you have an ordering derived from the
1431// lexicographical ordering of monomials.  E.g. if lm(f) < lm(g)
1432// w.r.t. lexicographic ordering, then f < g.  For more details,
1433// refer to the documentation of `InternalPoly::operator <()'.
1434//
1435// Both operands should have coefficients from the same base domain.
1436//
1437// The scheme how both operators are implemented is allmost the
1438// same as for the assignment operators (check for immediates,
1439// then check levels, then check levelcoeffs, then call the
1440// appropriate internal comparesame()/comparecoeff() method).
1441// For more information, confer to the overview for the
1442// arithmetic operators.
1443//
1444// See also: InternalCF::comparesame(),
1445// InternalInteger::comparesame(), InternalRational::comparesame(),
1446// InternalPrimePower::comparesame(), InternalPoly::comparesame(),
1447// InternalCF::comparecoeff(), InternalInteger::comparecoeff(),
1448// InternalRational::comparecoeff(),
1449// InternalPrimePower::comparecoeff(), InternalPoly::comparecoeff(),
1450// imm_cmp(), imm_cmp_p(), imm_cmp_gf()
1451//
1452//}}}
1453bool
1454operator > ( const CanonicalForm & lhs, const CanonicalForm & rhs )
1455{
1456    int what = is_imm( rhs.value );
1457    if ( is_imm( lhs.value ) ) {
1458        ASSERT( ! what || (what == is_imm( lhs.value )), "incompatible operands" );
1459        if ( what == 0 )
1460            return rhs.value->comparecoeff( lhs.value ) < 0;
1461        else if ( what == INTMARK )
1462            return imm_cmp( lhs.value, rhs.value ) > 0;
1463        else if ( what == FFMARK )
1464            return imm_cmp_p( lhs.value, rhs.value ) > 0;
1465        else
1466            return imm_cmp_gf( lhs.value, rhs.value ) > 0;
1467    }
1468    else  if ( what )
1469        return lhs.value->comparecoeff( rhs.value ) > 0;
1470    else  if ( lhs.value->level() == rhs.value->level() )
1471        if ( lhs.value->levelcoeff() == rhs.value->levelcoeff() )
1472            return lhs.value->comparesame( rhs.value ) > 0;
1473        else  if ( lhs.value->levelcoeff() > rhs.value->levelcoeff() )
1474            return lhs.value->comparecoeff( rhs.value ) > 0;
1475        else
1476            return rhs.value->comparecoeff( lhs.value ) < 0;
1477    else
1478        return lhs.value->level() > rhs.value->level();
1479}
1480
1481bool
1482operator < ( const CanonicalForm & lhs, const CanonicalForm & rhs )
1483{
1484    int what = is_imm( rhs.value );
1485    if ( is_imm( lhs.value ) ) {
1486        ASSERT( ! what || (what == is_imm( lhs.value )), "incompatible operands" );
1487        if ( what == 0 )
1488            return rhs.value->comparecoeff( lhs.value ) > 0;
1489        else if ( what == INTMARK )
1490            return imm_cmp( lhs.value, rhs.value ) < 0;
1491        else if ( what == FFMARK )
1492            return imm_cmp_p( lhs.value, rhs.value ) < 0;
1493        else
1494            return imm_cmp_gf( lhs.value, rhs.value ) < 0;
1495    }
1496    else  if ( what )
1497        return lhs.value->comparecoeff( rhs.value ) < 0;
1498    else  if ( lhs.value->level() == rhs.value->level() )
1499        if ( lhs.value->levelcoeff() == rhs.value->levelcoeff() )
1500            return lhs.value->comparesame( rhs.value ) < 0;
1501        else  if ( lhs.value->levelcoeff() > rhs.value->levelcoeff() )
1502            return lhs.value->comparecoeff( rhs.value ) < 0;
1503        else
1504            return rhs.value->comparecoeff( lhs.value ) > 0;
1505    else
1506        return lhs.value->level() < rhs.value->level();
1507}
1508//}}}
1509
1510//{{{ CanonicalForm bgcd ( const CanonicalForm & f, const CanonicalForm & g )
1511//{{{ docu
1512//
1513// bgcd() - return base coefficient gcd.
1514//
1515// If both f and g are integers and `SW_RATIONAL' is off the
1516// positive greatest common divisor of f and g is returned.
1517// Otherwise, if `SW_RATIONAL' is on or one of f and g is not an
1518// integer, the greatest common divisor is trivial: either zero
1519// if f and g equal zero or one (both from the current domain).
1520//
1521// f and g should come from one base domain which should be not
1522// the prime power domain.
1523//
1524// Implementation:
1525//
1526// CanonicalForm::bgcd() handles the immediate case with a
1527//   standard euclidean algorithm.  For the non-immediate cases
1528//   `InternalCF::bgcdsame()' or `InternalCF::bgcdcoeff()', resp. are
1529//   called following the usual level/levelcoeff approach.
1530//
1531// InternalCF::bgcdsame() and
1532// InternalCF::bgcdcoeff() throw an assertion ("not implemented")
1533//
1534// InternalInteger::bgcdsame() is a wrapper around `mpz_gcd()'
1535//   which takes some care about immediate results and the sign
1536//   of the result
1537// InternalInteger::bgcdcoeff() is a wrapper around
1538//   `mpz_gcd_ui()' which takes some care about the sign
1539//   of the result
1540//
1541// InternalRational::bgcdsame() and
1542// InternalRational::bgcdcoeff() always return one
1543//
1544//}}}
1545CanonicalForm
1546bgcd ( const CanonicalForm & f, const CanonicalForm & g )
1547{
1548    // check immediate cases
1549    int what = is_imm( g.value );
1550    if ( is_imm( f.value ) )
1551    {
1552        ASSERT( ! what || (what == is_imm( f.value )), "incompatible operands" );
1553        if ( what == 0 )
1554            return g.value->bgcdcoeff( f.value );
1555        else if ( what == INTMARK && ! cf_glob_switches.isOn( SW_RATIONAL ) )
1556        {
1557            // calculate gcd using standard integer
1558            // arithmetic
1559            long fInt = imm2int( f.value );
1560            long gInt = imm2int( g.value );
1561
1562            if ( fInt < 0 ) fInt = -fInt;
1563            if ( gInt < 0 ) gInt = -gInt;
1564            // swap fInt and gInt
1565            if ( gInt > fInt )
1566            {
1567                long swap = gInt;
1568                gInt = fInt;
1569                fInt = swap;
1570            }
1571
1572            // now, 0 <= gInt <= fInt.  Start the loop.
1573            while ( gInt )
1574            {
1575                // calculate (fInt, gInt) = (gInt, fInt%gInt)
1576                long r = fInt % gInt;
1577                fInt = gInt;
1578                gInt = r;
1579            }
1580
1581            return CanonicalForm( fInt );
1582        }
1583        else
1584            // we do not go for maximal speed for these stupid
1585            // special cases
1586            return CanonicalForm( f.isZero() && g.isZero() ? 0 : 1 );
1587    }
1588    else if ( what )
1589        return f.value->bgcdcoeff( g.value );
1590
1591    int fLevel = f.value->level();
1592    int gLevel = g.value->level();
1593
1594    // check levels
1595    if ( fLevel == gLevel )
1596    {
1597        fLevel = f.value->levelcoeff();
1598        gLevel = g.value->levelcoeff();
1599
1600        // check levelcoeffs
1601        if ( fLevel == gLevel )
1602            return f.value->bgcdsame( g.value );
1603        else if ( fLevel < gLevel )
1604            return g.value->bgcdcoeff( f.value );
1605        else
1606            return f.value->bgcdcoeff( g.value );
1607    }
1608    else if ( fLevel < gLevel )
1609        return g.value->bgcdcoeff( f.value );
1610    else
1611        return f.value->bgcdcoeff( g.value );
1612}
1613//}}}
1614
1615//{{{ CanonicalForm bextgcd ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & a, CanonicalForm & b )
1616//{{{ docu
1617//
1618// bextgcd() - return base coefficient extended gcd.
1619//
1620//}}}
1621CanonicalForm
1622bextgcd ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & a, CanonicalForm & b )
1623{
1624    // check immediate cases
1625    int what = is_imm( g.value );
1626    if ( is_imm( f.value ) ) {
1627        ASSERT( ! what || (what == is_imm( f.value )), "incompatible operands" );
1628        if ( what == 0 )
1629            return g.value->bextgcdcoeff( f.value, b, a );
1630        else if ( what == INTMARK && ! cf_glob_switches.isOn( SW_RATIONAL ) ) {
1631            // calculate extended gcd using standard integer
1632            // arithmetic
1633            long fInt = imm2int( f.value );
1634            long gInt = imm2int( g.value );
1635
1636            // to avoid any system dpendencies with `%', we work
1637            // with positive numbers only.  To a pity, we have to
1638            // redo all the checks when assigning to a and b.
1639            if ( fInt < 0 ) fInt = -fInt;
1640            if ( gInt < 0 ) gInt = -gInt;
1641            // swap fInt and gInt
1642            if ( gInt > fInt ) {
1643                long swap = gInt;
1644                gInt = fInt;
1645                fInt = swap;
1646            }
1647
1648            long u = 1; long v = 0;
1649            long uNext = 0; long vNext = 1;
1650
1651            // at any step, we have:
1652            // fInt_0 * u + gInt_0 * v = fInt
1653            // fInt_0 * uNext + gInt_0 * vNext = gInt
1654            // where fInt_0 and gInt_0 denote the values of fint
1655            // and gInt, resp., at the beginning
1656            while ( gInt ) {
1657                long r = fInt % gInt;
1658                long q = fInt / gInt;
1659                long uSwap = u - q * uNext;
1660                long vSwap = v - q * vNext;
1661
1662                // update variables
1663                fInt = gInt;
1664                gInt = r;
1665                u = uNext; v = vNext;
1666                uNext = uSwap; vNext = vSwap;
1667            }
1668
1669            // now, assign to a and b
1670            long fTest = imm2int( f.value );
1671            long gTest = imm2int( g.value );
1672            if ( gTest > fTest ) {
1673                a = v; b = u;
1674            } else {
1675                a = u; b = v;
1676            }
1677            if ( fTest < 0 ) a = -a;
1678            if ( gTest < 0 ) b = -b;
1679            return CanonicalForm( fInt );
1680        } else
1681            // stupid special cases
1682            if ( ! f.isZero() ) {
1683                a = 1/f; b = 0; return CanonicalForm( 1 );
1684            } else if ( ! g.isZero() ) {
1685                a = 0; b = 1/g; return CanonicalForm( 1 );
1686            } else {
1687                a = 0; b = 0; return CanonicalForm( 0 );
1688            }
1689    }
1690    else if ( what )
1691        return f.value->bextgcdcoeff( g.value, a, b );
1692
1693    int fLevel = f.value->level();
1694    int gLevel = g.value->level();
1695
1696    // check levels
1697    if ( fLevel == gLevel ) {
1698        fLevel = f.value->levelcoeff();
1699        gLevel = g.value->levelcoeff();
1700
1701        // check levelcoeffs
1702        if ( fLevel == gLevel )
1703            return f.value->bextgcdsame( g.value, a, b );
1704        else if ( fLevel < gLevel )
1705            return g.value->bextgcdcoeff( f.value, b, a );
1706        else
1707            return f.value->bextgcdcoeff( g.value, a, b );
1708    }
1709    else if ( fLevel < gLevel )
1710        return g.value->bextgcdcoeff( f.value, b, a );
1711    else
1712        return f.value->bextgcdcoeff( g.value, a, b );
1713}
1714//}}}
1715
1716CanonicalForm
1717blcm ( const CanonicalForm & f, const CanonicalForm & g )
1718{
1719    if ( f.isZero() || g.isZero() )
1720        return CanonicalForm( 0 );
1721/*
1722    else if (f.isOne())
1723        return g;
1724    else if (g.isOne())
1725        return f;
1726*/
1727    else
1728        return (f / bgcd( f, g )) * g;
1729}
1730
1731//{{{ input/output
1732#ifndef NOSTREAMIO
1733void
1734CanonicalForm::print( OSTREAM & os, char * str ) const
1735{
1736    if ( is_imm( value ) )
1737        imm_print( os, value, str );
1738    else
1739        value->print( os, str );
1740}
1741
1742void
1743CanonicalForm::print( OSTREAM & os ) const
1744{
1745    if ( is_imm( value ) )
1746        imm_print( os, value, "" );
1747    else
1748        value->print( os, "" );
1749}
1750
1751OSTREAM&
1752operator << ( OSTREAM & os, const CanonicalForm & cf )
1753{
1754    cf.print( os, "" );
1755    return os;
1756}
1757
1758ISTREAM&
1759operator >> ( ISTREAM & is, CanonicalForm & cf )
1760{
1761    cf = readCF( is );
1762    return is;
1763}
1764#endif /* NOSTREAMIO */
1765//}}}
1766
1767//{{{ genOne(), genZero()
1768CanonicalForm
1769CanonicalForm::genZero() const
1770{
1771    int what = is_imm( value );
1772    if ( what == FFMARK )
1773        return CanonicalForm( CFFactory::basic( FiniteFieldDomain, 0L ) );
1774    else  if ( what == GFMARK )
1775        return CanonicalForm( CFFactory::basic( GaloisFieldDomain, 0L ) );
1776    else  if ( what )
1777        return CanonicalForm( CFFactory::basic( IntegerDomain, 0L ) );
1778    else
1779        return CanonicalForm( value->genZero() );
1780}
1781
1782CanonicalForm
1783CanonicalForm::genOne() const
1784{
1785    int what = is_imm( value );
1786    if ( what == FFMARK )
1787        return CanonicalForm( CFFactory::basic( FiniteFieldDomain, 1L ) );
1788    else  if ( what == GFMARK )
1789        return CanonicalForm( CFFactory::basic( GaloisFieldDomain, 1L ) );
1790    else  if ( what )
1791        return CanonicalForm( CFFactory::basic( IntegerDomain, 1L ) );
1792    else
1793        return CanonicalForm( value->genOne() );
1794}
1795//}}}
1796
1797//{{{ exponentiation
1798CanonicalForm
1799power ( const CanonicalForm & f, int n )
1800{
1801  ASSERT( n >= 0, "illegal exponent" );
1802  if ( f.isZero() )
1803    return 0;
1804  else  if ( f.isOne() )
1805    return f;
1806  else  if ( f == -1 )
1807  {
1808    if ( n % 2 == 0 )
1809      return 1;
1810    else
1811      return -1;
1812  }
1813  else  if ( n == 0 )
1814    return 1;
1815
1816  //else if (f.inGF())
1817  //{
1818  //}
1819  else
1820  {
1821    CanonicalForm g,h;
1822    h=f;
1823    while(n%2==0)
1824    {
1825      h*=h;
1826      n/=2;
1827    }
1828    g=h;
1829    while(1)
1830    {
1831      n/=2;
1832      if(n==0)
1833        return g;
1834      h*=h;
1835      if(n%2!=0) g*=h;
1836    }
1837  }
1838}
1839
1840CanonicalForm
1841power ( const Variable & v, int n )
1842{
1843    //ASSERT( n >= 0, "illegal exponent" );
1844    if ( n == 0 )
1845        return 1;
1846    else  if ( n == 1 )
1847        return v;
1848    else  if (( v.level() < 0 ) && (hasMipo(v)))
1849    {
1850        CanonicalForm result( v, n-1 );
1851        return result * v;
1852    }
1853    else
1854        return CanonicalForm( v, n );
1855}
1856//}}}
1857
1858//{{{ switches
1859void
1860On( int sw )
1861{
1862    cf_glob_switches.On( sw );
1863}
1864
1865void
1866Off( int sw )
1867{
1868    cf_glob_switches.Off( sw );
1869}
1870
1871bool
1872isOn( int sw )
1873{
1874    return cf_glob_switches.isOn( sw );
1875}
1876//}}}
Note: See TracBrowser for help on using the repository browser.