Changeset 6e338f in git


Ignore:
Timestamp:
Jan 31, 2014, 8:16:13 PM (10 years ago)
Author:
Adi Popescu <adi_popescum@…>
Branches:
(u'fieker-DuVal', '117eb8c30fc9e991c4decca4832b1d19036c4c65')(u'spielwiese', 'fc741b6502fd8a97288eaa3eba6e5220f3c3df87')
Children:
b933aa2fe9e404c447f02411a78e2abe2d885e9c
Parents:
c274a029c85d80fc1e67a20a94756f2afdcefd08
Message:
Still in work - have to optimze it and to make it as a new function
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/hilb.cc

    rc274a0 r6e338f  
    3030static int  hLength;
    3131//ADICHANGES
    32 static int NNN;
    33 static mpz_t ec;
    34 static mpz_ptr hilbertcoef = (mpz_ptr)omAlloc(NNN*sizeof(mpz_t));
    35 static std::vector<int> hilbertpowers;
    36 static std::vector<int> hilbertpowerssorted;
    3732static std::vector<int> degsort;
    38 static std::vector<int> eulerprop;
    39 static ideal eulersimp;
    40 static int steps, prune = 0, moreprune = 0, eulerstep, eulerskips=0;
    4133   
    4234
     
    294286    idPrint(res);*/
    295287    return(res);
    296 }
    297 
    298 //puts in hilbertpowerssorted the two new entries and shifts if needed the old ones
    299 static void PutInVector(int degvalue, int oldposition, int where)
    300 {
    301     hilbertpowerssorted.resize(hilbertpowerssorted.size()+2);
    302     int i;
    303     for(i=hilbertpowerssorted.size()-1;i>=where+2;i--)
    304     {
    305         hilbertpowerssorted[i] = hilbertpowerssorted[i-2];
    306     }
    307     hilbertpowerssorted[where] = degvalue;
    308     hilbertpowerssorted[where+1] = oldposition;
    309 }
    310 
    311 //sorts hilbertpowers (new finds the positions)
    312 static void SortPowerVec()
    313 {
    314     int i,j;
    315     int test;
    316     bool flag;
    317     //printf("Size of hilbertpowers: %i", hilbertpowers.size());
    318     hilbertpowerssorted.resize(2);
    319     hilbertpowerssorted[0] = hilbertpowers[0];
    320     hilbertpowerssorted[1] = 0;
    321     for(i=1;i<hilbertpowers.size();i++)
    322     {
    323         flag=TRUE;
    324         if(hilbertpowers[i]<=hilbertpowerssorted[0])
    325         {
    326             flag=FALSE;
    327             PutInVector(hilbertpowers[i], i, 0);
    328         }
    329         if((hilbertpowers[i]>=hilbertpowerssorted[hilbertpowerssorted.size()-2]) && (flag == TRUE))
    330         {
    331             flag=FALSE;
    332             PutInVector(hilbertpowers[i], i, hilbertpowerssorted.size()-2);
    333         }
    334         if(flag==TRUE)
    335         {
    336             for(j=2;(j<=hilbertpowerssorted.size()-4)&&(flag);j=j+2)
    337             {
    338                 if(hilbertpowers[i]<=hilbertpowerssorted[j])
    339                 {
    340                     flag=FALSE;
    341                     PutInVector(hilbertpowers[i], i, j);
    342                 }
    343             }
    344         }
    345     }
    346288}
    347289
     
    483425    return(I);
    484426}
    485 
    486 #if 0
    487 //Constructs a list of the simplices (for which we have to compute the Euler Characteristic)
    488 static void eulerSimpAdd(ideal newsimp)
    489 {
    490     if(idIs0(newsimp))
    491     {
    492         return;
    493     }
    494     ideal P;
    495     int i;
    496     bool flag;
    497     P = idInit(1,1);
    498     for(i=0;i<IDELEMS(newsimp);i++)
    499     {
    500         P->m[0] = pAdd(P->m[0], pCopy(newsimp->m[i]));
    501     }
    502     poly p = pCopy(P->m[0]);
    503     //idPrint(eulersimp);
    504     //idPrint(newsimp);
    505     //idPrint(P);
    506     int terms = 0, deg = 0, howmanyvars = 0;
    507     terms = IDELEMS(newsimp);
    508     deg = DegMon(newsimp->m[0]);
    509     flag=TRUE;
    510     for(i=1;(i<IDELEMS(newsimp))&&(flag==TRUE);i++)
    511     {
    512        
    513         if(deg != DegMon(newsimp->m[i]))
    514         {
    515             //The ideal is not homogenous
    516             flag=FALSE;
    517         }
    518     }
    519     if(!flag)
    520     {
    521         deg = 0;
    522     }
    523     int* e;
    524     howmanyvars = pGetVariables(p, e);
    525     flag = FALSE;
    526     int thisone;
    527     //idPrint(eulersimp);
    528     if(idIs0(eulersimp))
    529     {
    530         eulersimp = idAddMon(eulersimp, P);
    531         eulerprop.resize(eulerprop.size()+3);
    532         eulerprop[eulerprop.size()-3] = terms ;
    533         eulerprop[eulerprop.size()-2] = deg ;
    534         eulerprop[eulerprop.size()-1] = howmanyvars ;
    535         return;
    536     }
    537     //for(i=0;i<IDELEMS(eulersimp);i++)
    538     //{
    539     //    printf("\n%i %i %i\n",eulerprop[3*i],eulerprop[3*i+1],eulerprop[3*i+2]);
    540     //}
    541     i=0;
    542    
    543     for(i = 0; (i<IDELEMS(eulersimp)) && (!flag);i++)
    544     {
    545         if((eulerprop[3*i] == terms) && (eulerprop[3*i+1] == deg) && (eulerprop[3*i+2] == howmanyvars))
    546         {
    547             //  !!!!!!!!!!!!!!!!!!!have to do it!!!!!!!!!!!!!!!!!!!!!!!!!!
    548             //if(TestIfIsSame(p, eulersimp->m[i]))
    549             {
    550                 flag = TRUE;
    551                 thisone = i;
    552                 //It is the the same simplex, and hence we already know it's euler characteristic
    553             }
    554         }
    555     }
    556     if(!flag)
    557     {
    558         eulersimp = idAddMon(eulersimp, P);
    559         eulerprop.resize(eulerprop.size()+3);
    560         eulerprop[eulerprop.size()-3] = terms ;
    561         eulerprop[eulerprop.size()-2] = deg ;
    562         eulerprop[eulerprop.size()-1] = howmanyvars ;
    563     }
    564     if(flag)
    565     {
    566         eulerskips++;
    567         hilbertcoef = (mpz_ptr)omRealloc(hilbertcoef, (NNN+1)*sizeof(mpz_t));
    568         mpz_init(&hilbertcoef[NNN]);
    569         mpz_set(&hilbertcoef[NNN], &hilbertcoef[thisone]);
    570         hilbertpowers.resize(NNN+1);
    571         NNN++;
    572     }
    573     return;
    574 }
    575 #endif
    576427
    577428//searches for a variable that is not yet used (assumes that the ideal is sqrfree)
     
    921772
    922773//computes the Euler Characteristic of the ideal
    923 static void eulerchar (ideal I, int variables)
     774static void eulerchar (ideal I, int variables, mpz_ptr ec)
    924775{
    925776  loop
    926777  {
    927     //idPrint(I);printf("%i", variables);
    928     eulerstep++;
    929778    mpz_t dummy;
    930779    if(JustVar(I) == TRUE)
     
    944793    ideal p = idInit(1,1);
    945794    p->m[0] = SearchP(I);
    946     //pWrite(p->m[0]);
    947795    ideal Ip = idQuotMon(I,p);
    948796    Ip = SortByDeg(Ip);
     
    955803        }
    956804    }   
    957     eulerchar(Ip, variables-howmanyvarinp);
     805    eulerchar(Ip, variables-howmanyvarinp, ec);
    958806    id_Delete(&Ip, currRing);
    959     //ideal Iplusp = idAddMon(I,p);
    960     //eulerchar(Iplusp, variables);
    961     //id_Delete(&Iplusp, currRing);
    962     //return;
    963807    I = idAddMon(I,p);
    964808  }
     
    1013857        return(FALSE);
    1014858    }
    1015     //if(DegMon(p)<DegMon(I->m[IDELEMS(I)-1]))
    1016     //{
    1017     //    return(FALSE);
    1018     //}
    1019     #if 0
    1020     if(p == NULL)
    1021     {
    1022         if(IDELEMS(I) == 0)
    1023         {
    1024             return(TRUE);
    1025         }
    1026         if(IDELEMS(I) == 1)
    1027         {
    1028             if(I->m[0] == poly(0))
    1029             {
    1030                 return(TRUE);
    1031             }
    1032         }
    1033         return(FALSE);
    1034     }
    1035     if(IDELEMS(I)==0)
    1036     {
    1037         return(FALSE);
    1038     }
    1039     if(IDELEMS(I) == 1)
    1040     {
    1041         if(I->m[0] == poly(0))
    1042             {
    1043                 return(FALSE);
    1044             }   
    1045     }
    1046     #endif
    1047859    int i,j;
    1048860    bool flag;
     
    1091903}
    1092904
    1093 #if 0
    1094 //return TRUE if a | b, a, b monomials
    1095 static bool Divides(poly a, poly b)
    1096 {
    1097     if(a == NULL)
    1098     {
    1099         return(FALSE);
    1100     }
    1101     if(b == NULL)
    1102     {
    1103         return(TRUE);
    1104     }
    1105     int i;
    1106     for(i=1;i<=currRing->N;i++)
    1107     {
    1108         if(p_GetExp(a,i,currRing) > p_GetExp(b,i,currRing))
    1109         {
    1110             return(FALSE);
    1111         }
    1112     }
    1113     return(TRUE);
    1114 }
    1115 #endif
    1116 
    1117905//the Roune Slice Algorithm
    1118 void rouneslice(ideal I, ideal S, poly q, poly x)
     906void rouneslice(ideal I, ideal S, poly q, poly x, int &prune, int &moreprune, int &steps, int &NNN, mpz_ptr &hilbertcoef, int* &hilbpower)
    1119907{
    1120908  loop
    1121909  {
    1122     steps++;
     910    (steps)++;
    1123911    int i,j;
    1124912    int dummy;
    1125     mpz_t dummympz;
    1126913    poly m;
    1127914    clock_t t;
     
    1134921        if(IsIn(S->m[i],I))
    1135922        {
    1136             //idPrint(I);idPrint(S);getchar();
    1137923            S->m[i]=NULL;
    1138924            prune++;
     
    1159945        }
    1160946        p_Setm(m, currRing);
    1161         //printf("\n Check if m is in S: %i\n", IsIn(m,S));pWrite(m);idPrint(S);
    1162947        if(IsIn(m,S))
    1163948        {
    1164             //printf("\nIt was deleted: \n");pWrite(I->m[i]);
    1165949            I->m[i]=NULL;
    1166950            //printf("\n Deleted, since pi(m) is in S\n");pWrite(m);
     
    1210994    if(idIs0(I))
    1211995    {
    1212         /*if(!pIsConstantPoly(q))
    1213         {
    1214             mpz_init(dummympz);
    1215             mpz_set_si(dummympz, -1);
    1216             hilbertcoef = (mpz_ptr)omRealloc(hilbertcoef, (NNN+1)*sizeof(mpz_t));
    1217             mpz_init(&hilbertcoef[NNN]);
    1218             mpz_set(&hilbertcoef[NNN], dummympz);
    1219             hilbertpowers.resize(NNN+1);
    1220             hilbertpowers[NNN] = DegMon(q);
    1221             NNN++;
    1222             pWrite(q);
    1223             //printf("\nOld ERROR\n");getchar();
    1224         }*/
    1225996        id_Delete(&I, currRing);
    1226997        id_Delete(&S, currRing);
    1227998        p_Delete(&m, currRing);
    1228999        break;
    1229         //return;
    12301000    }
    12311001    S = SortByDeg(S);
     
    12341004    {
    12351005        //printf("\nx does not divide lcm(I)");
    1236         //pWrite(x);pWrite(m);idPrint(I);
    12371006        //printf("\nEmpty set");pWrite(q);
    1238         //idPrint(S);pWrite(q);
    1239         //getchar();
    12401007        id_Delete(&I, currRing);
    12411008        id_Delete(&S, currRing);
     
    12541021        {
    12551022            koszsimp->m[i] = I->m[i];
    1256             //pWrite(koszsimp->m[i]);
    1257             /*for(j = 1;j<=currRing->N;j++)
    1258             {
    1259                 if(p_GetExp(koszsimp->m[i],j,currRing)!=0)
    1260                 {
    1261                     p_SetExp(koszsimp->m[i],j,0,currRing);
    1262                 }
    1263                 else
    1264                 {
    1265                     p_SetExp(koszsimp->m[i],j,1,currRing);
    1266                 }
    1267             }*/
    1268             //pWrite(koszsimp->m[i]);
    12691023        }
    12701024        //idPrint(koszsimp);
    12711025        //getchar();
    12721026        //printf("\n Euler Characteristic = ");
    1273         mpz_init(dummympz);
    12741027        t = clock();
    1275         eulerstep=0;
    12761028        //idPrint(koszsimp);
    12771029        //getchar();
    1278         eulerchar(koszsimp, currRing->N);
    1279         if(((float)(clock()-t)/CLOCKS_PER_SEC)!=0)
    1280         {
    1281             //printf("\nEulerChar took %f \n",(float)(clock()-t)/CLOCKS_PER_SEC);
    1282             //idPrint(koszsimp);getchar();
    1283         }
    1284         //printf("\nEulerChar tooked %i steps\n", eulerstep);
    1285         //gmp_printf("dummy %Zd \n", dummympz);
    1286         //gmp_printf("ec %Zd \n", ec);
     1030        mpz_t ec;
     1031        mpz_init(ec);
     1032        mpz_ptr ec_ptr = ec;
     1033        eulerchar(koszsimp, currRing->N, ec_ptr);
     1034        bool flag = FALSE;
     1035        if(NNN==0)
     1036            {
     1037                hilbertcoef = (mpz_ptr)omAlloc((NNN+1)*sizeof(mpz_t));
     1038                hilbpower = (int*)omAlloc((NNN+1)*sizeof(int));
     1039                mpz_init( &hilbertcoef[NNN]);
     1040                mpz_set(  &hilbertcoef[NNN], ec);
     1041                mpz_clear(ec);
     1042                hilbpower[NNN] = DegMon(q);
     1043                NNN++;
     1044            }
     1045        else
     1046        {
     1047            //I look if the power appears already
     1048            for(i = 0;(i<NNN)&&(flag == FALSE)&&(DegMon(q)>=hilbpower[i]);i++)
     1049            {
     1050                if((hilbpower[i]) == (DegMon(q)))
     1051                {
     1052                    flag = TRUE;
     1053                    mpz_add(&hilbertcoef[i],&hilbertcoef[i],ec_ptr);
     1054                }
     1055            }
     1056            if(flag == FALSE)
     1057            {
     1058                hilbertcoef = (mpz_ptr)omRealloc(hilbertcoef, (NNN+1)*sizeof(mpz_t));
     1059                hilbpower = (int*)omRealloc(hilbpower, (NNN+1)*sizeof(int));
     1060                mpz_init(&hilbertcoef[NNN]);
     1061                for(j = NNN; j>i; j--)
     1062                {
     1063                    mpz_set(&hilbertcoef[j],&hilbertcoef[j-1]);
     1064                    hilbpower[j] = hilbpower[j-1];
     1065                }
     1066                mpz_set(  &hilbertcoef[i], ec);
     1067                mpz_clear(ec);
     1068                hilbpower[i] = DegMon(q);
     1069                NNN++;
     1070            }
     1071        }
     1072        #if 0
     1073        for(i = 0; i<NNN; i++)
     1074        {
     1075            gmp_printf("\n//  %8Zd t^%d",&hilbertcoef[i],hilbpower[i]);
     1076        }
     1077        printf("\n");
    12871078        //getchar();
    1288         //if(DegMon(q)==7) {getchar();}
    1289         mpz_set(dummympz, ec);
    1290         mpz_sub(ec, ec, ec);
    1291         hilbertcoef = (mpz_ptr)omRealloc(hilbertcoef, (NNN+1)*sizeof(mpz_t));
    1292         mpz_init(&hilbertcoef[NNN]);
    1293         mpz_set(&hilbertcoef[NNN], dummympz);
    1294         mpz_clear(dummympz);
    1295         hilbertpowers.resize(NNN+1);
    1296         hilbertpowers[NNN] = DegMon(q);
    1297         NNN++;
     1079        #endif
    12981080        break;
    1299         //return;
    13001081    }
    13011082    m = ChooseP(I);
    13021083    p = idInit(1,1);
    13031084    p->m[0] = m;
    1304     //idPrint(I);idPrint(p);
    1305     //idTest(I);idTest(p);
    13061085    ideal Ip = idQuotMon(I,p);
    13071086    Ip = SortByDeg(Ip);
    13081087    ideal Sp = idQuotMon(I,p);
    13091088    Sp = SortByDeg(Sp);
    1310     //printf("\np, q = \n");pWrite(m);pWrite(q);
    13111089    poly pq = pp_Mult_mm(q,m,currRing);
    1312     rouneslice(Ip, Sp, pq, x);
    1313     //idPrint(Ip);
    1314     //id_Delete(&Ip, currRing);
    1315     //id_Delete(&Sp, currRing);
     1090    rouneslice(Ip, Sp, pq, x, prune, moreprune, steps, NNN, hilbertcoef,hilbpower);
     1091    id_Delete(&Ip, currRing);
     1092    id_Delete(&Sp, currRing);
    13161093    S = idAddMon(S,p);
    1317     //p->m[0]=NULL; id_Delete(&p, currRing); // p->m[0] was also in S
     1094    p->m[0]=NULL;
     1095    id_Delete(&p, currRing); // p->m[0] was also in S
    13181096    p_Delete(&pq,currRing);
    1319     //Splusp = idSimplify(Splusp);
    1320   //  rouneslice(I, S, q, x);  tail-recursion solved via loop
    1321 
    1322     //id_Delete(&Splusp, currRing);
    1323     //return;
    1324   }
    1325 }
    1326 
     1097  }
     1098}
    13271099
    13281100//it computes the first hilbert series by means of Roune Slice Algorithm
     
    13311103    I = SortByDeg(I);
    13321104    printf("Adi changes are here: \n");
    1333     int i;
    1334     eulersimp = idInit(1,1);
     1105    int i, NNN = 0;
     1106    int steps = 0, prune = 0, moreprune = 0;
     1107    mpz_ptr hilbertcoef;
     1108    int *hilbpower;
    13351109    ideal S = idInit(1,1);
    13361110    poly q = p_ISet(1,currRing);
     
    13441118    I = id_Mult(I,X,currRing);
    13451119    printf("\n-------------RouneSlice--------------\n");
    1346     steps=0;
    1347     rouneslice(I,S,q,X->m[0]);
     1120    rouneslice(I,S,q,X->m[0],prune, moreprune, steps, NNN, hilbertcoef, hilbpower);
    13481121    printf("\nIn total Prune got rid of %i elements\n",prune);
    13491122    printf("\nIn total More Prune got rid of %i elements\n",moreprune);
    1350     //printf("\nWe skipped computing the EulerChar for %i simplices\n",eulerskips);
    13511123    printf("\nSteps of rouneslice: %i\n\n", steps);
    1352     /*for(i=0;i<NNN;i++)
    1353     {
    1354         gmp_printf(" %Zd ", &hilbertcoef[i]);
    1355     }
    1356     printf("\n");
    1357     for(i=0;i<NNN;i++)
    1358     {
    1359         printf(" %d ", hilbertpowers[i]);
    1360     }*/
    1361     SortPowerVec();
    1362     /*printf("\n");
    1363     for(i=0;i<hilbertpowerssorted.size();i++)
    1364     {
    1365         printf(" %d ", hilbertpowerssorted[i]);
    1366     }*/
    13671124    mpz_t coefhilb;
    13681125    mpz_t dummy;
     
    13701127    mpz_init(dummy);
    13711128    printf("\n//  %8d t^0",1);
    1372     for(i=0;i<hilbertpowerssorted.size();i=i+2)
    1373     {
    1374         mpz_set(dummy, &hilbertcoef[hilbertpowerssorted[i+1]]);
    1375         mpz_add(coefhilb, coefhilb, dummy);
    1376         while((hilbertpowerssorted[i]==hilbertpowerssorted[i+2]) && (i<=hilbertpowerssorted.size()-2))
    1377         {
    1378             i=i+2;
    1379             mpz_add(coefhilb, coefhilb, &hilbertcoef[hilbertpowerssorted[i+1]]);
    1380         }
    1381         if(mpz_sgn(coefhilb)!=0)
    1382         {
    1383             gmp_printf("\n//  %8Zd t^%d",coefhilb,hilbertpowerssorted[i]);
    1384         }
    1385         mpz_sub(coefhilb, coefhilb, coefhilb);
    1386     }
    1387     omFreeSize(hilbertcoef, NNN*sizeof(mpz_t));
     1129    for(i = 0; i<NNN; i++)
     1130    {
     1131        if(mpz_sgn(&hilbertcoef[i])!=0)
     1132        {
     1133            gmp_printf("\n//  %8Zd t^%d",&hilbertcoef[i],hilbpower[i]);
     1134        }
     1135    }
     1136    omFreeSize(hilbertcoef, (NNN)*sizeof(mpz_t));
     1137    omFreeSize(hilbpower, (NNN)*sizeof(int));
    13881138    printf("\n-------------------------------------\n");
    13891139}
Note: See TracChangeset for help on using the changeset viewer.