Changeset 1e6de6 in git


Ignore:
Timestamp:
Jan 7, 2008, 2:33:11 PM (15 years ago)
Author:
Hans Schönemann <hannes@…>
Branches:
(u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', '0604212ebb110535022efecad887940825b97c3f')
Children:
aa7480c120ef293aaf5a185ec38a2440c23fc879
Parents:
6840b2040b65b549b02b0e852339ab1a0a791484
Message:
*hannes: some optimizations(gcd)


git-svn-id: file:///usr/local/Singular/svn/trunk@10489 2c84dea3-7e68-4137-9b89-c4e89433aadc
Location:
factory
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • factory/NTLconvert.cc

    r6840b2 r1e6de6  
    1 /* $Id: NTLconvert.cc,v 1.22 2007-04-26 08:22:48 Singular Exp $ */
     1/* $Id: NTLconvert.cc,v 1.23 2008-01-07 13:33:09 Singular Exp $ */
    22#include <config.h>
    33
     
    7171{
    7272  printf("%s",s1);
    73   if (f==0) printf("+0");
     73  if (f.isZero()) printf("+0");
    7474  else if (! f.inCoeffDomain() )
    7575  {
  • factory/cf_factor.cc

    r6840b2 r1e6de6  
    11/* emacs edit mode for this file is -*- C++ -*- */
    2 /* $Id: cf_factor.cc,v 1.35 2007-10-25 14:45:41 Singular Exp $ */
     2/* $Id: cf_factor.cc,v 1.36 2008-01-07 13:33:10 Singular Exp $ */
    33
    44//{{{ docu
     
    8585{
    8686  printf("%s",s1);
    87   if (f==0) printf("+0");
     87  if (f.isZero()) printf("+0");
    8888  //else if (! f.inCoeffDomain() )
    8989  else if (! f.inBaseDomain() )
  • factory/cf_gcd.cc

    r6840b2 r1e6de6  
    11/* emacs edit mode for this file is -*- C++ -*- */
    2 /* $Id: cf_gcd.cc,v 1.58 2007-12-19 14:27:14 Singular Exp $ */
     2/* $Id: cf_gcd.cc,v 1.59 2008-01-07 13:33:10 Singular Exp $ */
    33
    44#include <config.h>
     
    475475  int mp, cc, p1, pe;
    476476  mp = f.level()+1;
    477 #if 0
     477#if 1
    478478  if (( getCharacteristic() == 0 )
    479479  && (f.level() >4)
     
    489489  }
    490490#endif
    491   cf_prepgcd( f, g, cc, p1, pe);
    492   if ( cc != 0 )
    493   {
    494     if ( cc > 0 )
    495     {
    496       fc = replacevar( f, Variable(cc), Variable(mp) );
    497       gc = g;
    498     }
    499     else
    500     {
    501       fc = replacevar( g, Variable(-cc), Variable(mp) );
    502       gc = f;
    503     }
    504     return cf_content( fc, gc );
    505   }
    506 // now each appearing variable is in f and g
    507   fc = f;
    508   gc = g;
    509   if( gcd_avoid_mtaildegree ( fc, gc, d1 ) )
    510       return d1;
    511   bool fc_isUnivariate=fc.isUnivariate();
    512   bool fc_and_gc_Univariate=fc_isUnivariate && gc.isUnivariate();
     491  bool fc_isUnivariate=f.isUnivariate();
     492  bool fc_and_gc_Univariate=fc_isUnivariate && g.isUnivariate();
    513493  if ( getCharacteristic() != 0 )
    514494  {
    515495    if (isOn( SW_USE_EZGCD_P ) && (!fc_and_gc_Univariate))
    516496    {
    517       if ( pe == 1 )
    518         fc = fin_ezgcd( fc, gc );
    519       else if ( pe > 0 )// no variable at position 1
     497        return fin_ezgcd( f, g );
     498    }
     499    else if (isOn(SW_USE_GCD_P))
     500    {
     501      return newGCD(f,g);
     502    }
     503    else
     504    {
     505      cf_prepgcd( f, g, cc, p1, pe);
     506      if ( cc != 0 )
    520507      {
    521         fc = replacevar( fc, Variable(pe), Variable(1) );
    522         gc = replacevar( gc, Variable(pe), Variable(1) );
    523         fc = replacevar( fin_ezgcd( fc, gc ), Variable(1), Variable(pe) );
     508        if ( cc > 0 )
     509        {
     510          fc = replacevar( f, Variable(cc), Variable(mp) );
     511          gc = g;
     512        }
     513        else
     514        {
     515          fc = replacevar( g, Variable(-cc), Variable(mp) );
     516          gc = f;
     517        }
     518        return cf_content( fc, gc );
    524519      }
     520      // now each appearing variable is in f and g
     521      fc = f;
     522      gc = g;
     523      if( gcd_avoid_mtaildegree ( fc, gc, d1 ) )
     524        return d1;
     525      if ( p1 == fc.level() )
     526        fc = gcd_poly_p( fc, gc );
    525527      else
    526528      {
    527         pe = -pe;
    528         fc = swapvar( fc, Variable(pe), Variable(1) );
    529         gc = swapvar( gc, Variable(pe), Variable(1) );
    530         fc = swapvar( fin_ezgcd( fc, gc ), Variable(1), Variable(pe) );
     529        fc = replacevar( fc, Variable(p1), Variable(mp) );
     530        gc = replacevar( gc, Variable(p1), Variable(mp) );
     531        fc = replacevar( gcd_poly_p( fc, gc ), Variable(mp), Variable(p1) );
    531532      }
    532     }
    533     else if (isOn(SW_USE_GCD_P))
    534     {
    535       fc=newGCD(fc,gc);
    536     }
    537     else if ( p1 == fc.level() )
    538       fc = gcd_poly_p( fc, gc );
    539     else
    540     {
    541       fc = replacevar( fc, Variable(p1), Variable(mp) );
    542       gc = replacevar( gc, Variable(p1), Variable(mp) );
    543       fc = replacevar( gcd_poly_p( fc, gc ), Variable(mp), Variable(p1) );
    544533    }
    545534  }
     
    548537    if (
    549538    isOn(SW_USE_CHINREM_GCD)
    550     && (gc.level() >5)
    551     && (fc.level() >5)
    552     && (isPurePoly_m(fc)) && (isPurePoly_m(gc))
     539    && (g.level() >5)
     540    && (f.level() >5)
     541    && (isPurePoly_m(f)) && (isPurePoly_m(g))
    553542    )
    554543    {
    555     #if 0
    556       if ( p1 == fc.level() )
    557         fc = chinrem_gcd( fc, gc );
    558       else
     544      return chinrem_gcd( f, g);
     545    }
     546    if ( isOn( SW_USE_EZGCD ) )
     547    {
     548      return ezgcd( f, g );
     549    }
     550    else if (
     551    isOn(SW_USE_CHINREM_GCD)
     552    && (isPurePoly_m(f)) && (isPurePoly_m(g))
     553    )
     554    {
     555      return chinrem_gcd( f, g);
     556    }
     557    else
     558    {
     559      cf_prepgcd( f, g, cc, p1, pe);
     560      if ( cc != 0 )
    559561      {
    560         fc = replacevar( fc, Variable(p1), Variable(mp) );
    561         gc = replacevar( gc, Variable(p1), Variable(mp) );
    562         fc = replacevar( chinrem_gcd( fc, gc ), Variable(mp), Variable(p1) );
     562        if ( cc > 0 )
     563        {
     564          fc = replacevar( f, Variable(cc), Variable(mp) );
     565          gc = g;
     566        }
     567        else
     568        {
     569          fc = replacevar( g, Variable(-cc), Variable(mp) );
     570          gc = f;
     571        }
     572        return cf_content( fc, gc );
    563573      }
    564     #else
    565       fc = chinrem_gcd( fc, gc);
    566     #endif
    567     }
    568     if ( isOn( SW_USE_EZGCD ) )
    569     {
    570       if ( pe == 1 )
    571         fc = ezgcd( fc, gc );
    572       else if ( pe > 0 )// no variable at position 1
     574      // now each appearing variable is in f and g
     575      fc = f;
     576      gc = g;
     577      if( gcd_avoid_mtaildegree ( fc, gc, d1 ) )
     578        return d1;
     579      fc = gcd_poly_0( fc, gc );
     580    }
     581  }
     582  else
     583  {
     584    cf_prepgcd( f, g, cc, p1, pe);
     585    if ( cc != 0 )
     586    {
     587      if ( cc > 0 )
    573588      {
    574         fc = replacevar( fc, Variable(pe), Variable(1) );
    575         gc = replacevar( gc, Variable(pe), Variable(1) );
    576         fc = replacevar( ezgcd( fc, gc ), Variable(1), Variable(pe) );
     589        fc = replacevar( f, Variable(cc), Variable(mp) );
     590        gc = g;
    577591      }
    578592      else
    579593      {
    580         pe = -pe;
    581         fc = swapvar( fc, Variable(pe), Variable(1) );
    582         gc = swapvar( gc, Variable(pe), Variable(1) );
    583         fc = swapvar( ezgcd( fc, gc ), Variable(1), Variable(pe) );
     594        fc = replacevar( g, Variable(-cc), Variable(mp) );
     595        gc = f;
    584596      }
    585     }
    586     else if (
    587     isOn(SW_USE_CHINREM_GCD)
    588     && (isPurePoly_m(fc)) && (isPurePoly_m(gc))
    589     )
    590     {
    591     #if 0
    592       if ( p1 == fc.level() )
    593         fc = chinrem_gcd( fc, gc );
    594       else
    595       {
    596         fc = replacevar( fc, Variable(p1), Variable(mp) );
    597         gc = replacevar( gc, Variable(p1), Variable(mp) );
    598         fc = replacevar( chinrem_gcd( fc, gc ), Variable(mp), Variable(p1) );
    599       }
    600     #else
    601       fc = chinrem_gcd( fc, gc);
    602     #endif
    603     }
    604     else
    605     {
    606        fc = gcd_poly_0( fc, gc );
    607     }
    608   }
    609   else
    610   {
     597      return cf_content( fc, gc );
     598    }
     599    // now each appearing variable is in f and g
     600    fc = f;
     601    gc = g;
     602    if( gcd_avoid_mtaildegree ( fc, gc, d1 ) )
     603      return d1;
    611604    fc = gcd_poly_0( fc, gc );
    612605  }
  • factory/ffreval.cc

    r6840b2 r1e6de6  
    2020
    2121  for( int i=values.min(); i<=n; i++ )
    22     values[i] = offset[i] = gen->generate(); // generate random point
     22  {
     23    CanonicalForm tmp=gen->generate();
     24    values[i] = tmp; // generate random point
     25    offset[i] = tmp; // generate random point
     26  }
    2327}
    2428
     
    5054  if( this != &e )
    5155  {
    52     if( gen != 0 )
     56    if( gen != NULL )
    5357      delete gen;
    5458    values = e.values;
    5559    offset = e.offset;
    56     if( e.gen == 0 )
    57       gen = 0;
     60    if( e.gen == NULL )
     61      gen = NULL;
    5862    else
    5963      gen = e.gen->clone();
     
    112116        b.init(); // choose random point
    113117    }
    114     while ( ! gcdfound ) {
     118    while ( ! gcdfound )
     119    {
    115120        /// ---> A2
    116121        DEBOUTLN( cerr, "search for evaluation, delta = " << delta );
     
    138143        /// ---> A4
    139144        //deltaold = delta;
    140         while ( 1 ) {
     145        while ( 1 )
     146        {
    141147            bt = b;
    142148            if( ! fin_findeval( F, G, Fbt, Gbt, Dbt, bt, delta + 1, degF, degG ) )
     
    156162            if ( dd /*degree( Dbt )*/ == delta )
    157163                break;
    158             else  if ( dd /*degree( Dbt )*/ < delta ) {
     164            else  if ( dd /*degree( Dbt )*/ < delta )
     165            {
    159166                delta = dd /*degree( Dbt )*/;
    160167                b = bt;
     
    174181            return d*G;
    175182        }
    176         if ( delta != degF && delta != degG ) {
     183        if ( delta != degF && delta != degG )
     184        {
    177185            bool B_is_F;
    178186            /// ---> A6
     
    184192            DEBOUTLN( cerr, "Fb/Db = " << DD[1] );
    185193            DEBOUTLN( cerr, "Db = " << Db );
    186             if (xxx.inCoeffDomain()) {
     194            if (xxx.inCoeffDomain())
     195            {
    187196                B = F;
    188197                DD[2] = Db;
     
    261270{
    262271    CanonicalForm d;
    263 #if 1
    264     Off(SW_USE_EZGCD);
    265     //bool ntl0=isOn(SW_USE_NTL_GCD_0);
    266     //Off(SW_USE_NTL_GCD_0);
    267     //bool ntlp=isOn(SW_USE_NTL_GCD_P);
    268     //Off(SW_USE_NTL_GCD_P);
     272    Off(SW_USE_EZGCD_P);
    269273    d=gcd_poly( F, G );
    270     //if (ntl0) On(SW_USE_NTL_GCD_0);
    271     //if (ntlp) On(SW_USE_NTL_GCD_P);
    272     On(SW_USE_EZGCD);
     274    On(SW_USE_EZGCD_P);
    273275    return d;
    274 #else
    275     DEBOUTLN( cerr, "fin_ezgcd: special case" );
    276     CanonicalForm Ft, Gt, L, LL, Fb, Gb, Db, Lb, D, Ds, Dt;
    277     CFArray DD( 1, 2 ), lcDD( 1, 2 );
    278     Variable x = Variable( 1 );
    279     bool gcdfound;
    280     Dt = 1;
    281     /// ---> S1
    282     DEBOUTLN( cerr, "fin_ezgcdspec: (S1)" );
    283     Ft = fin_ezgcd( F, F.deriv( x ) );
    284     if ( Ft.isOne() ) {
    285         // In this case F is squarefree and we came here by bad chance
    286         // (means: bad evaluation point).  Try again with another
    287         // evaluation point.  Bug fix (?) by JS.  The bad example was
    288         // gcd.debug -ocr /+USE_EZGCD/@12/CB \
    289         //     '(16*B^8-208*B^6*C+927*B^4*C^2-1512*B^2*C^3+432*C^4)' \
    290         //     '(4*B^7*C^2-50*B^5*C^3+208*B^3*C^4-288*B*C^5)'
    291         if( ! b.step() ) // Error: run out of points
    292         {
    293           Off(SW_USE_EZGCD_P);
    294           d = gcd_poly(F,G);
    295           On(SW_USE_EZGCD_P);
    296           return d;
    297         }
    298         return fin_ezgcd( F, G, b, true );
    299     }
    300     DEBOUTLN( cerr, "fin_ezgcdspec: (S1) done, Ft = " << Ft );
    301     L = F / Ft;
    302     /// ---> S2
    303     DEBOUTLN( cerr, "fin_ezgcdspec: (S2)" );
    304     L = fin_ezgcd( L, G, b, true );
    305     DEBOUTLN( cerr, "fin_ezgcdspec: (S2) done, Ds = " << Ds );
    306     Gt = G / L;
    307     Ds = L; Dt = L;
    308     Lb = b( L ); Gb = b( Gt ); Fb = b( Ft );
    309     d = gcd( LC( L, x ), gcd( LC( Ft, x ), LC( Gt, x ) ) );
    310     while ( true ) {
    311         /// ---> S3
    312         DEBOUTLN( cerr, "fin_ezgcdspec: (S3)" );
    313         DEBOUTLN( cerr, "fin_ezgcdspec: Fb = " << Fb );
    314         DEBOUTLN( cerr, "fin_ezgcdspec: Gb = " << Gb );
    315         DD[1] = gcd( Lb, gcd( Fb, Gb ) );
    316         /// ---> S4
    317         DEBOUTLN( cerr, "fin_ezgcdspec: (S4)" );
    318         if ( degree( DD[1] ) == 0 )
    319             return Ds;
    320         /// ---> S5
    321         DEBOUTLN( cerr, "fin_ezgcdspec: (S5)" );
    322         DEBOUTLN( cerr, "fin_ezgcdspec: Lb = " << Lb );
    323         DEBOUTLN( cerr, "fin_ezgcdspec: Db = " << DD[1] );
    324         Db = DD[1];
    325         if ( ! (DD[2] = Lb/DD[1]).isOne() ) {
    326             DEBOUTLN( cerr, "fin_ezgcdspec: (S55)" );
    327             lcDD[2] = LC( L, x );
    328             lcDD[1] = d;
    329             DD[1] = ( DD[1] * b( d ) ) / lc( DD[1] );
    330             DD[2] = ( DD[2] * b( lcDD[2] ) ) / lc( DD[2] );
    331             LL = L * d;
    332             DEBOUTLN( cerr, "fin_ezgcdspec: begin with lift." );
    333             DEBOUTLN( cerr, "fin_ezgcdspec: B     = " << LL );
    334             DEBOUTLN( cerr, "fin_ezgcdspec: DD    = " << DD );
    335             DEBOUTLN( cerr, "fin_ezgcdspec: lcDD  = " << lcDD );
    336             DEBOUTLN( cerr, "fin_ezgcdspec: b     = " << b );
    337             DEBOUTLN( cerr, "fin_ezgcdspec: lc(B) = " << LC( LL, x ) );
    338             DEBOUTLN( cerr, "fin_ezgcdspec: test  = " << b( LL ) - DD[1] * DD[2] );
    339             gcdfound = Hensel( LL, DD, lcDD, b, p, x );
    340             ASSERT( gcdfound, "fatal error in algorithm" );
    341             Dt = pp( DD[1] );
    342         }
    343         DEBOUTLN( cerr, "fin_ezgcdspec: (S7)" );
    344         Ds = Ds * Dt;
    345         Fb = Fb / Db;
    346         Gb = Gb / Db;
    347     }
    348     // this point is never reached, but to avoid compiler warnings let's return a value
    349     return 0;
    350 #endif
    351276}
    352277
     
    357282    DEBOUTLN( cerr, "fin_ezgcd: (fin_findeval) F = " << F  <<", G="<< G);
    358283    DEBOUTLN( cerr, "fin_ezgcd: (fin_findeval) degF = " << degF << ", degG="<<degG );
    359     while(1) {
     284    while(1)
     285    {
    360286        DEBOUTLN( cerr, "fin_ezgcd: (fin_findeval) b = " << b );
    361287        Fb = b( F );
    362288        ok = degree( Fb ) == degF;
    363         if ( ok ) {
     289        if ( ok )
     290        {
    364291            Gb = b( G );
    365292            ok = degree( Gb ) == degG;
  • factory/int_int.cc

    r6840b2 r1e6de6  
    11/* emacs edit mode for this file is -*- C++ -*- */
    2 /* $Id: int_int.cc,v 1.19 2007-12-04 15:24:53 Singular Exp $ */
     2/* $Id: int_int.cc,v 1.20 2008-01-07 13:33:10 Singular Exp $ */
    33
    44#include <config.h>
     
    8686InternalInteger::neg ()
    8787{
    88     if ( getRefCount() > 1 ) {
     88    if ( getRefCount() > 1 )
     89    {
    8990        decRefCount();
    9091        MP_INT dummy;
     
    9293        mpz_neg( &dummy, &dummy );
    9394        return new InternalInteger( dummy );
    94     } else {
     95    }
     96    else
     97    {
    9598        mpz_neg( &thempi, &thempi );
    9699        return this;
     
    102105InternalCF* InternalInteger::addsame( InternalCF * c )
    103106{
    104     if ( getRefCount() > 1 ) {
     107    if ( getRefCount() > 1 )
     108    {
    105109        decRefCount();
    106110        MP_INT dummy;
    107111        mpz_init( &dummy );
    108112        mpz_add( &dummy, &thempi, &MPI( c ) );
    109         if ( mpz_is_imm( &dummy ) ) {
     113        if ( mpz_is_imm( &dummy ) )
     114        {
    110115            InternalCF * res = int2imm( mpz_get_si( &dummy ) );
    111116            mpz_clear( &dummy );
     
    115120            return new InternalInteger( dummy );
    116121    }
    117     else {
     122    else
     123    {
    118124        mpz_add( &thempi, &thempi, &MPI( c ) );
    119         if ( mpz_is_imm( &thempi ) ) {
     125        if ( mpz_is_imm( &thempi ) )
     126        {
    120127            InternalCF * res = int2imm( mpz_get_si( &thempi ) );
    121128            delete this;
     
    129136InternalCF* InternalInteger::subsame( InternalCF * c )
    130137{
    131     if ( getRefCount() > 1 ) {
     138    if ( getRefCount() > 1 )
     139    {
    132140        decRefCount();
    133141        MP_INT dummy;
    134142        mpz_init( &dummy );
    135143        mpz_sub( &dummy, &thempi, &MPI( c ) );
    136         if ( mpz_is_imm( &dummy ) ) {
     144        if ( mpz_is_imm( &dummy ) )
     145        {
    137146            InternalCF * res = int2imm( mpz_get_si( &dummy ) );
    138147            mpz_clear( &dummy );
     
    142151            return new InternalInteger( dummy );
    143152    }
    144     else {
     153    else
     154    {
    145155        mpz_sub( &thempi, &thempi, &MPI( c ) );
    146         if ( mpz_is_imm( &thempi ) ) {
     156        if ( mpz_is_imm( &thempi ) )
     157        {
    147158            InternalCF * res = int2imm( mpz_get_si( &thempi ) );
    148159            delete this;
     
    156167InternalCF* InternalInteger::mulsame( InternalCF * c )
    157168{
    158     if ( getRefCount() > 1 ) {
     169    if ( getRefCount() > 1 )
     170    {
    159171        decRefCount();
    160172        MP_INT dummy;
    161173        mpz_init( &dummy );
    162174        mpz_mul( &dummy, &thempi, &MPI( c ) );
    163         if ( mpz_is_imm( &dummy ) ) {
     175        if ( mpz_is_imm( &dummy ) )
     176        {
     177        // can this happen ???
    164178            InternalCF * res = int2imm( mpz_get_si( &dummy ) );
    165179            mpz_clear( &dummy );
     
    169183            return new InternalInteger( dummy );
    170184    }
    171     else {
     185    else
     186    {
    172187        mpz_mul( &thempi, &thempi, &MPI( c ) );
    173         if ( mpz_is_imm( &thempi ) ) {
     188        if ( mpz_is_imm( &thempi ) )
     189        {
     190        // can this happen ???
    174191            InternalCF * res = int2imm( mpz_get_si( &thempi ) );
    175192            delete this;
     
    202219    ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" );
    203220    int cc = imm2int( c );
    204     if ( getRefCount() > 1 ) {
     221    if ( getRefCount() > 1 )
     222    {
    205223        decRefCount();
    206224        MP_INT dummy;
     
    210228        else
    211229            mpz_add_ui( &dummy, &thempi, cc );
    212         if ( mpz_is_imm( &dummy ) ) {
     230        if ( mpz_is_imm( &dummy ) )
     231        {
    213232            InternalCF * res = int2imm( mpz_get_si( &dummy ) );
    214233            mpz_clear( &dummy );
  • factory/int_poly.cc

    r6840b2 r1e6de6  
    11/* emacs edit mode for this file is -*- C++ -*- */
    2 /* $Id: int_poly.cc,v 1.19 2006-09-15 10:15:48 Singular Exp $ */
     2/* $Id: int_poly.cc,v 1.20 2008-01-07 13:33:10 Singular Exp $ */
    33
    44#include <config.h>
     
    11401140    if ( aTermList == 0 )
    11411141        return 0;
    1142     else  if ( negate ) {
     1142    else  if ( negate )
     1143    {
    11431144        termList sourceCursor = aTermList;
    11441145        termList dummy = new term;
    11451146        termList targetCursor = dummy;
    11461147
    1147         while ( sourceCursor ) {
     1148        while ( sourceCursor )
     1149        {
    11481150            targetCursor->next = new term( 0, -sourceCursor->coeff, sourceCursor->exp );
    11491151            targetCursor = targetCursor->next;
     
    11561158        return targetCursor;
    11571159    }
    1158     else {
     1160    else
     1161    {
    11591162        termList sourceCursor = aTermList;
    11601163        termList dummy = new term;
    11611164        termList targetCursor = dummy;
    11621165
    1163         while ( sourceCursor ) {
     1166        while ( sourceCursor )
     1167        {
    11641168            targetCursor->next = new term( 0, sourceCursor->coeff, sourceCursor->exp );
    11651169            targetCursor = targetCursor->next;
     
    11791183    if ( aTermList == 0 )
    11801184        return 0;
    1181     else {
     1185    else
     1186    {
    11821187        termList sourceCursor = aTermList;
    11831188        termList dummy = new term;
    11841189        termList targetCursor = dummy;
    11851190
    1186         while ( sourceCursor ) {
     1191        while ( sourceCursor )
     1192        {
    11871193            targetCursor->next = new term( 0, sourceCursor->coeff.deepCopy(), sourceCursor->exp );
    11881194            targetCursor = targetCursor->next;
     
    12021208    termList cursor = aTermList;
    12031209
    1204     while ( cursor ) {
     1210    while ( cursor )
     1211    {
    12051212        cursor = cursor->next;
    12061213        delete aTermList;
     
    12131220{
    12141221    termList cursor = terms;
    1215     while ( cursor ) {
     1222    while ( cursor )
     1223    {
    12161224        cursor->coeff = -cursor->coeff;
    12171225        cursor = cursor->next;
     
    12261234    termList predCursor = 0;
    12271235
    1228     while ( theCursor && aCursor ) {
    1229         if ( theCursor->exp == aCursor->exp ) {
     1236    while ( theCursor && aCursor )
     1237    {
     1238        if ( theCursor->exp == aCursor->exp )
     1239        {
    12301240            if ( negate )
    12311241                theCursor->coeff -= aCursor->coeff;
    12321242            else
    12331243                theCursor->coeff += aCursor->coeff;
    1234             if ( theCursor->coeff.isZero() ) {
    1235                 if ( predCursor ) {
     1244            if ( theCursor->coeff.isZero() )
     1245            {
     1246                if ( predCursor )
     1247                {
    12361248                    predCursor->next = theCursor->next;
    12371249                    delete theCursor;
    12381250                    theCursor = predCursor->next;
    12391251                }
    1240                 else {
     1252                else
     1253                {
    12411254                    theList = theList->next;
    12421255                    delete theCursor;
     
    12441257                }
    12451258            }
    1246             else {
     1259            else
     1260            {
    12471261                predCursor = theCursor;
    12481262                theCursor = theCursor->next;
     
    12501264            aCursor = aCursor->next;
    12511265        }
    1252         else if ( theCursor->exp < aCursor->exp ) {
     1266        else if ( theCursor->exp < aCursor->exp )
     1267        {
    12531268            if ( negate )
    1254                 if ( predCursor ) {
     1269                if ( predCursor )
     1270                {
    12551271                    predCursor->next = new term( theCursor, -aCursor->coeff, aCursor->exp );
    12561272                    predCursor = predCursor->next;
    12571273                }
    1258                 else {
     1274                else
     1275                {
    12591276                    theList = new term( theCursor, -aCursor->coeff, aCursor->exp );
    12601277                    predCursor = theList;
    12611278                }
    12621279            else
    1263                 if ( predCursor ) {
     1280                if ( predCursor )
     1281                {
    12641282                    predCursor->next = new term( theCursor, aCursor->coeff, aCursor->exp );
    12651283                    predCursor = predCursor->next;
    12661284                }
    1267                 else {
     1285                else
     1286                {
    12681287                    theList = new term( theCursor, aCursor->coeff, aCursor->exp );
    12691288                    predCursor = theList;
     
    12711290            aCursor = aCursor->next;
    12721291        }
    1273         else {
     1292        else
     1293        {
    12741294            predCursor = theCursor;
    12751295            theCursor = theCursor->next;
    12761296        }
    12771297    }
    1278     if ( aCursor ) {
     1298    if ( aCursor )
     1299    {
    12791300        if ( predCursor )
    12801301            predCursor->next = copyTermList( aCursor, lastTerm, negate );
     
    12911312InternalPoly::mulTermList ( termList theCursor, const CanonicalForm& coeff, const int exp )
    12921313{
    1293     while ( theCursor ) {
     1314    while ( theCursor )
     1315    {
    12941316        theCursor->coeff *= coeff;
    12951317        theCursor->exp += exp;
     
    13051327    termList dummy;
    13061328
    1307     while ( theCursor ) {
     1329    while ( theCursor )
     1330    {
    13081331        theCursor->coeff /= coeff;
    1309         if ( theCursor->coeff.isZero() ) {
     1332        if ( theCursor->coeff.isZero() )
     1333        {
    13101334            if ( theCursor == firstTerm )
    13111335                firstTerm = theCursor->next;
     
    13161340            delete dummy;
    13171341        }
    1318         else {
     1342        else
     1343        {
    13191344            lastTerm = theCursor;
    13201345            theCursor = theCursor->next;
     
    13311356    termList dummy;
    13321357
    1333     while ( theCursor ) {
     1358    while ( theCursor )
     1359    {
    13341360        theCursor->coeff.div( coeff );
    1335         if ( theCursor->coeff.isZero() ) {
     1361        if ( theCursor->coeff.isZero() )
     1362        {
    13361363            if ( theCursor == firstTerm )
    13371364                firstTerm = theCursor->next;
     
    13421369            delete dummy;
    13431370        }
    1344         else {
     1371        else
     1372        {
    13451373            lastTerm = theCursor;
    13461374            theCursor = theCursor->next;
     
    13571385    termList dummy;
    13581386
    1359     while ( theCursor ) {
     1387    while ( theCursor )
     1388    {
    13601389        theCursor->coeff.mod( coeff );
    1361         if ( theCursor->coeff.isZero() ) {
     1390        if ( theCursor->coeff.isZero() )
     1391        {
    13621392            if ( theCursor == firstTerm )
    13631393                firstTerm = theCursor->next;
     
    13681398            delete dummy;
    13691399        }
    1370         else {
     1400        else
     1401        {
    13711402            lastTerm = theCursor;
    13721403            theCursor = theCursor->next;
     
    13791410InternalPoly::appendTermList ( termList& first, termList& last, const CanonicalForm& coeff, const int exp )
    13801411{
    1381     if ( last ) {
     1412    if ( last )
     1413    {
    13821414        last->next = new term( 0, coeff, exp );
    13831415        last = last->next;
    13841416    }
    1385     else {
     1417    else
     1418    {
    13861419        first = new term( 0, coeff, exp );
    13871420        last = first;
     
    14021435        coeff = c;
    14031436
    1404     while ( theCursor && aCursor ) {
    1405         if ( theCursor->exp == aCursor->exp + exp ) {
     1437    while ( theCursor && aCursor )
     1438    {
     1439        if ( theCursor->exp == aCursor->exp + exp )
     1440        {
    14061441            theCursor->coeff += aCursor->coeff * coeff;
    1407             if ( theCursor->coeff.isZero() ) {
    1408                 if ( predCursor ) {
     1442            if ( theCursor->coeff.isZero() )
     1443            {
     1444                if ( predCursor )
     1445                {
    14091446                    predCursor->next = theCursor->next;
    14101447                    delete theCursor;
    14111448                    theCursor = predCursor->next;
    14121449                }
    1413                 else {
     1450                else
     1451                {
    14141452                    theList = theList->next;
    14151453                    delete theCursor;
     
    14171455                }
    14181456            }
    1419             else {
     1457            else
     1458            {
    14201459                predCursor = theCursor;
    14211460                theCursor = theCursor->next;
     
    14231462            aCursor = aCursor->next;
    14241463        }
    1425         else if ( theCursor->exp < aCursor->exp + exp ) {
    1426             if ( predCursor ) {
     1464        else if ( theCursor->exp < aCursor->exp + exp )
     1465        {
     1466            if ( predCursor )
     1467            {
    14271468                predCursor->next = new term( theCursor, aCursor->coeff * coeff, aCursor->exp + exp );
    14281469                predCursor = predCursor->next;
    14291470            }
    1430             else {
     1471            else
     1472            {
    14311473                theList = new term( theCursor, aCursor->coeff * coeff, aCursor->exp + exp );
    14321474                predCursor = theList;
     
    14341476            aCursor = aCursor->next;
    14351477        }
    1436         else {
     1478        else
     1479        {
    14371480            predCursor = theCursor;
    14381481            theCursor = theCursor->next;
    14391482        }
    14401483    }
    1441     if ( aCursor ) {
    1442         if ( predCursor ) {
     1484    if ( aCursor )
     1485    {
     1486        if ( predCursor )
     1487        {
    14431488            predCursor->next = copyTermList( aCursor, lastTerm );
    14441489            predCursor = predCursor->next;
    14451490        }
    1446         else {
     1491        else
     1492        {
    14471493            theList = copyTermList( aCursor, lastTerm );
    14481494            predCursor = theList;
    14491495        }
    1450         while ( predCursor ) {
     1496        while ( predCursor )
     1497        {
    14511498            predCursor->exp += exp;
    14521499            predCursor->coeff *= coeff;
     
    14671514    int exp = redterms->exp;
    14681515    termList dummy;
    1469     while ( first && ( first->exp >= exp ) ) {
     1516    while ( first && ( first->exp >= exp ) )
     1517    {
    14701518        newcoeff = first->coeff / coeff;
    14711519        newexp = first->exp - exp;
  • factory/int_rat.cc

    r6840b2 r1e6de6  
    11/* emacs edit mode for this file is -*- C++ -*- */
    2 /* $Id: int_rat.cc,v 1.14 2007-12-04 15:24:53 Singular Exp $ */
     2/* $Id: int_rat.cc,v 1.15 2008-01-07 13:33:11 Singular Exp $ */
    33
    44#include <config.h>
     
    2121    int c;
    2222
    23     while ( b != 0 ) {
     23    while ( b != 0 )
     24    {
    2425        c = a % b;
    2526        a = b;
     
    4546{
    4647    ASSERT( d != 0, "divide by zero" );
    47     if ( n == 0 ) {
     48    if ( n == 0 )
     49    {
    4850        mpz_init_set_si( &_num, 0 );
    4951        mpz_init_set_si( &_den, 1 );
    5052    }
    51     else {
     53    else
     54    {
    5255        int g = intgcd( n, d );
    53         if ( d < 0 ) {
     56        if ( d < 0 )
     57        {
    5458            mpz_init_set_si( &_num, -n / g );
    5559            mpz_init_set_si( &_den, -d / g );
    5660        }
    57         else {
     61        else
     62        {
    5863            mpz_init_set_si( &_num, n / g );
    5964            mpz_init_set_si( &_den, d / g );
     
    133138InternalCF * InternalRational::num ()
    134139{
    135     if ( mpz_is_imm( &_num ) ) {
     140    if ( mpz_is_imm( &_num ) )
     141    {
    136142        InternalCF * res = int2imm( mpz_get_si( &_num ) );
    137143        return res;
    138144    }
    139     else {
     145    else
     146    {
    140147        MP_INT dummy;
    141148        mpz_init_set( &dummy, &_num );
     
    146153InternalCF * InternalRational::den ()
    147154{
    148     if ( mpz_is_imm( &_den ) ) {
     155    if ( mpz_is_imm( &_den ) )
     156    {
    149157        InternalCF * res = int2imm( mpz_get_si( &_den ) );
    150158        return res;
    151159    }
    152     else {
     160    else
     161    {
    153162        MP_INT dummy;
    154163        mpz_init_set( &dummy, &_den );
     
    163172InternalRational::neg ()
    164173{
    165     if ( getRefCount() > 1 ) {
     174    if ( getRefCount() > 1 )
     175    {
    166176        decRefCount();
    167177        MP_INT dummy_num;
     
    171181        mpz_neg( &dummy_num, &dummy_num );
    172182        return new InternalRational( dummy_num, dummy_den );
    173     } else {
     183    }
     184    else
     185    {
    174186        mpz_neg( &_num, &_num );
    175187        return this;
     
    186198    mpz_gcd( &g, &_den, &MPQDEN( c ) );
    187199
    188     if ( mpz_cmp_si( &g, 1 ) == 0 ) {
     200    if ( mpz_cmp_si( &g, 1 ) == 0 )
     201    {
    189202        mpz_mul( &n, &_den, &MPQNUM( c ) );
    190203        mpz_mul( &g, &_num, &MPQDEN( c ) );
     
    192205        mpz_mul( &d, &_den, &MPQDEN( c ) );
    193206    }
    194     else {
     207    else
     208    {
    195209        MP_INT tmp1;
    196210        MP_INT tmp2;
     
    204218        mpz_add( &n, &tmp1, &tmp2 );
    205219        mpz_gcd( &g, &n, &d );
    206         if ( mpz_cmp_si( &g, 1 ) != 0 ) {
     220        if ( mpz_cmp_si( &g, 1 ) != 0 )
     221        {
    207222            mpz_div( &n, &n, &g );
    208223            mpz_div( &d, &d, &g );
     
    213228    mpz_clear( &g );
    214229    if ( deleteObject() ) delete this;
    215     if ( mpz_cmp_si( &d, 1 ) == 0 ) {
    216         if ( mpz_is_imm( &n ) ) {
     230    if ( mpz_cmp_si( &d, 1 ) == 0 )
     231    {
     232        if ( mpz_is_imm( &n ) )
     233        {
    217234            InternalCF * res = int2imm( mpz_get_si( &n ) );
    218235            mpz_clear( &n ); mpz_clear( &d );
    219236            return res;
    220237        }
    221         else {
     238        else
     239        {
    222240            mpz_clear( &d );
    223241            return new InternalInteger( n );
     
    236254    mpz_gcd( &g, &_den, &MPQDEN( c ) );
    237255
    238     if ( mpz_cmp_si( &g, 1 ) == 0 ) {
     256    if ( mpz_cmp_si( &g, 1 ) == 0 )
     257    {
    239258        mpz_mul( &n, &_den, &MPQNUM( c ) );
    240259        mpz_mul( &g, &_num, &MPQDEN( c ) );
     
    242261        mpz_mul( &d, &_den, &MPQDEN( c ) );
    243262    }
    244     else {
     263    else
     264    {
    245265        MP_INT tmp1;
    246266        MP_INT tmp2;
     
    254274        mpz_sub( &n, &tmp2, &tmp1 );
    255275        mpz_gcd( &g, &n, &d );
    256         if ( mpz_cmp_si( &g, 1 ) != 0 ) {
     276        if ( mpz_cmp_si( &g, 1 ) != 0 )
     277        {
    257278            mpz_div( &n, &n, &g );
    258279            mpz_div( &d, &d, &g );
     
    263284    mpz_clear( &g );
    264285    if ( deleteObject() ) delete this;
    265     if ( mpz_cmp_si( &d, 1 ) == 0 ) {
    266         if ( mpz_is_imm( &n ) ) {
     286    if ( mpz_cmp_si( &d, 1 ) == 0 )
     287    {
     288        if ( mpz_is_imm( &n ) )
     289        {
    267290            InternalCF * res = int2imm( mpz_get_si( &n ) );
    268291            mpz_clear( &n ); mpz_clear( &d );
    269292            return res;
    270293        }
    271         else {
     294        else
     295        {
    272296            mpz_clear( &d );
    273297            return new InternalInteger( n );
     
    283307    MP_INT n, d;
    284308
    285     if ( this == c ) {
     309    if ( this == c )
     310    {
    286311        mpz_init( &n ); mpz_init( &d );
    287312        mpz_mul( &n, &_num, &_num );
    288313        mpz_mul( &d, &_den, &_den );
    289314    }
    290     else {
     315    else
     316    {
    291317        MP_INT g1, g2, tmp1, tmp2;
    292318        mpz_init( &g1 ); mpz_init( &g2 );
     
    319345    }
    320346    if ( deleteObject() ) delete this;
    321     if ( mpz_cmp_si( &d, 1 ) == 0 ) {
    322         if ( mpz_is_imm( &n ) ) {
     347    if ( mpz_cmp_si( &d, 1 ) == 0 )
     348    {
     349        if ( mpz_is_imm( &n ) )
     350        {
    323351            InternalCF * res = int2imm( mpz_get_si( &n ) );
    324352            mpz_clear( &n ); mpz_clear( &d );
    325353            return res;
    326354        }
    327         else {
     355        else
     356        {
    328357            mpz_clear( &d );
    329358            return new InternalInteger( n );
     
    338367    ASSERT( ! ::is_imm( c ) && c->levelcoeff() == RationalDomain, "illegal domain" );
    339368
    340     if ( this == c ) {
     369    if ( this == c )
     370    {
    341371        if ( deleteObject() ) delete this;
    342372        return CFFactory::basic( 1 );
    343373    }
    344     else {
     374    else
     375    {
    345376        MP_INT n, d;
    346377        MP_INT g1, g2, tmp1, tmp2;
     
    377408            mpz_neg( &n, &n );
    378409        }
    379         if ( mpz_cmp_si( &d, 1 ) == 0 ) {
    380             if ( mpz_is_imm( &n ) ) {
     410        if ( mpz_cmp_si( &d, 1 ) == 0 )
     411        {
     412            if ( mpz_is_imm( &n ) )
     413            {
    381414                InternalCF * res = int2imm( mpz_get_si( &n ) );
    382415                mpz_clear( &n ); mpz_clear( &d );
    383416                return res;
    384417            }
    385             else {
     418            else
     419            {
    386420                mpz_clear( &d );
    387421                return new InternalInteger( n );
     
    456490InternalRational::comparecoeff ( InternalCF* c )
    457491{
    458     if ( ::is_imm( c ) ) {
     492    if ( ::is_imm( c ) )
     493    {
    459494        ASSERT( ::is_imm( c ) == INTMARK, "incompatible base coefficients" );
    460495        MP_INT dummy;
     
    465500        return result;
    466501    }
    467     else {
     502    else
     503    {
    468504        ASSERT( c->levelcoeff() == IntegerDomain, "incompatible base coefficients" );
    469505        MP_INT dummy;
     
    481517    ASSERT( ::is_imm( c ) == INTMARK || ! ::is_imm( c ), "expected integer" );
    482518    MP_INT n, d;
    483     if ( ::is_imm( c ) ) {
     519    if ( ::is_imm( c ) )
     520    {
    484521        int cc = imm2int( c );
    485522        if ( cc == 0 )
    486523            return this;
    487         else  if ( cc < 0 ) {
     524        else  if ( cc < 0 )
     525        {
    488526            mpz_init( &n );
    489527            mpz_mul_ui( &n, &_den, -cc );
    490528            mpz_sub( &n, &_num, &n );
    491529        }
    492         else {
     530        else
     531        {
    493532            mpz_init( &n );
    494533            mpz_mul_ui( &n, &_den, cc );
     
    496535        }
    497536    }
    498     else {
     537    else
     538    {
    499539        ASSERT( c->levelcoeff() == IntegerDomain, "expected integer" );
    500540        mpz_init( &n );
     
    512552    ASSERT( ::is_imm( c ) == INTMARK || ! ::is_imm( c ), "expected integer" );
    513553    MP_INT n, d;
    514     if ( ::is_imm( c ) ) {
     554    if ( ::is_imm( c ) )
     555    {
    515556        int cc = imm2int( c );
    516557        if ( cc == 0 )
    517             if ( negate ) {
    518                 if ( getRefCount() == 1 ) {
     558            if ( negate )
     559            {
     560                if ( getRefCount() == 1 )
     561                {
    519562                    mpz_neg( &_num, &_num );
    520563                    return this;
    521564                }
    522                 else {
     565                else
     566                {
    523567                    decRefCount();
    524568                    mpz_init_set( &d, &_den );
     
    531575                return this;
    532576        mpz_init( &n );
    533         if ( cc < 0 ) {
     577        if ( cc < 0 )
     578        {
    534579            mpz_mul_ui( &n, &_den, -cc );
    535580            mpz_neg( &n, &n );
     
    542587            mpz_sub( &n, &_num, &n );
    543588    }
    544     else {
     589    else
     590    {
    545591        ASSERT( c->levelcoeff() == IntegerDomain, "expected integer" );
    546592        mpz_init( &n );
     
    561607    ASSERT( ::is_imm( c ) == INTMARK || ! ::is_imm( c ), "expected integer" );
    562608    MP_INT n, d, g;
    563     if ( ::is_imm( c ) ) {
     609    if ( ::is_imm( c ) )
     610    {
    564611        int cc = imm2int( c );
    565         if ( cc == 0 ) {
     612        if ( cc == 0 )
     613        {
    566614            if ( deleteObject() ) delete this;
    567615            return CFFactory::basic( 0 );
     
    569617        mpz_init_set_si( &n, cc );
    570618    }
    571     else {
     619    else
     620    {
    572621        ASSERT( c->levelcoeff() == IntegerDomain, "expected integer" );
    573622        mpz_init_set( &n, &InternalInteger::MPI( c ) );
     
    575624    mpz_init( &g );
    576625    mpz_gcd( &g, &n, &_den );
    577     if ( mpz_cmp_si( &g, 1 ) == 0 ) {
     626    if ( mpz_cmp_si( &g, 1 ) == 0 )
     627    {
    578628        mpz_mul( &n, &n, &_num );
    579629        mpz_init_set( &d, &_den );
    580630    }
    581     else {
     631    else
     632    {
    582633        mpz_div( &n, &n, &g );
    583634        mpz_mul( &n, &n, &_num );
     
    587638    mpz_clear( &g );
    588639    if ( deleteObject() ) delete this;
    589     if ( mpz_cmp_si( &d, 1 ) == 0 ) {
    590         if ( mpz_is_imm( &n ) ) {
     640    if ( mpz_cmp_si( &d, 1 ) == 0 )
     641    {
     642        if ( mpz_is_imm( &n ) )
     643        {
    591644            InternalCF * res = int2imm( mpz_get_si( &n ) );
    592645            mpz_clear( &n ); mpz_clear( &d );
    593646            return res;
    594647        }
    595         else {
     648        else
     649        {
    596650            mpz_clear( &d );
    597651            return new InternalInteger( n );
     
    606660    ASSERT( ::is_imm( c ) == INTMARK || ! ::is_imm( c ), "expected integer" );
    607661    MP_INT n, d, g;
    608     if ( ::is_imm( c ) ) {
     662    if ( ::is_imm( c ) )
     663    {
    609664        int cc = imm2int( c );
    610665        ASSERT( c != 0 || invert, "divide by zero" );
    611         if ( cc == 0 ) {
     666        if ( cc == 0 )
     667        {
    612668            // => invert
    613669            if ( deleteObject() ) delete this;
    614670            return CFFactory::basic( 0 );
    615671        }
    616         if ( invert ) {
     672        if ( invert )
     673        {
    617674            mpz_init_set_si( &n, cc );
    618675            mpz_mul( &n, &n, &_den );
    619676            mpz_init_set( &d, &_num );
    620677        }
    621         else {
     678        else
     679        {
    622680            mpz_init_set_si( &d, cc );
    623681            mpz_mul( &d, &d, &_den );
     
    625683        }
    626684    }
    627     else {
     685    else
     686    {
    628687        ASSERT( c->levelcoeff() == IntegerDomain, "expected integer" );
    629         if ( invert ) {
     688        if ( invert )
     689        {
    630690            mpz_init_set( &n, &InternalInteger::MPI( c ) );
    631691            mpz_mul( &n, &n, &_den );
    632692            mpz_init_set( &d, &_num );
    633693        }
    634         else {
     694        else
     695        {
    635696            mpz_init_set( &d, &InternalInteger::MPI( c ) );
    636697            mpz_mul( &d, &d, &_den );
     
    638699        }
    639700    }
    640     if ( mpz_cmp_si( &d, 0 ) < 0 ) {
     701    if ( mpz_cmp_si( &d, 0 ) < 0 )
     702    {
    641703        mpz_neg( &d, &d );
    642704        mpz_neg( &n, &n );
     
    644706    mpz_init( &g );
    645707    mpz_gcd( &g, &n, &d );
    646     if ( mpz_cmp_si( &g, 1 ) != 0 ) {
     708    if ( mpz_cmp_si( &g, 1 ) != 0 )
     709    {
    647710        mpz_div( &d, &d, &g );
    648711        mpz_div( &n, &n, &g );
     
    650713    mpz_clear( &g );
    651714    if ( deleteObject() ) delete this;
    652     if ( ! invert ) {
     715    if ( ! invert )
     716    {
    653717        // then there was no way for the result to become an integer
    654718        return new InternalRational( n, d );
    655719    }
    656     if ( mpz_cmp_si( &d, 1 ) == 0 ) {
    657         if ( mpz_is_imm( &n ) ) {
     720    if ( mpz_cmp_si( &d, 1 ) == 0 )
     721    {
     722        if ( mpz_is_imm( &n ) )
     723        {
    658724            InternalCF * res = int2imm( mpz_get_si( &n ) );
    659725            mpz_clear( &n ); mpz_clear( &d );
    660726            return res;
    661727        }
    662         else {
     728        else
     729        {
    663730            mpz_clear( &d );
    664731            return new InternalInteger( n );
     
    751818    }
    752819    if ( mpz_cmp_si( &_den, 1 ) == 0 )
     820    {
    753821        if ( mpz_is_imm( &_num ) )
    754822        {
     
    764832            return new InternalInteger( res );
    765833        }
     834    }
    766835    else
    767836        return this;
Note: See TracChangeset for help on using the changeset viewer.