[BACK]Return to poly.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / kan96xx / Kan

Annotation of OpenXM/src/kan96xx/Kan/poly.c, Revision 1.5

1.5     ! ohara       1: /* $OpenXM: OpenXM/src/kan96xx/Kan/poly.c,v 1.4 2004/09/13 11:24:11 takayama Exp $ */
1.1       maekawa     2: #include <stdio.h>
1.5     ! ohara       3: #include <stdlib.h>
1.1       maekawa     4: #include "datatype.h"
                      5: #include "stackm.h"
                      6: #include "extern.h"
                      7: #include "extern2.h"
                      8:
                      9:
                     10: void KinitKan(void) {
                     11:
                     12:   extern int Msize;
                     13:   extern struct ring SmallRing;
                     14:   extern struct ring *CurrentRingp;
                     15:   extern MP_INT *Mp_one;
                     16:   extern MP_INT *Mp_zero;
                     17:   extern MP_INT Mp_work_iiComb;
                     18:   extern MP_INT Mp_work_iiPoch;
                     19:   extern MP_INT Mp_work_iiPower;
                     20:
                     21:   static MP_INT Mp_One;
                     22:   static MP_INT Mp_Zero;
                     23:
                     24:   extern struct coeff *UniversalZero;
                     25:   extern struct coeff *UniversalOne;
                     26:
                     27:   int i;
                     28:   static char *smallx[] = {"x","t"};
                     29:   static char *smalld[] = {"h","T"};
1.3       takayama   30:   /* t  x  T  h */
1.1       maekawa    31:   static int smallOrder[] = { 1, 0, 0, 0,
                     32:                               0, 1, 0, 0,
                     33:                               0, 0, 1, 0,
                     34:                               0, 0, 0, 1};
                     35:
                     36:   static int outputOrderForSmallRing[] = {1,0,3,2};
                     37:   Msize = sizeof(struct monomial) - sizeof(struct monomialDummy);
                     38:   if (Msize <= 0) errorPoly("Problem of your C-compiler??!!");
                     39:
                     40:   /* Define the CurrentRingp */
                     41:   /* Define the SmallRingp */
                     42:
                     43:   SmallRing.p = 0;
                     44:   SmallRing.n = SmallRing.m = SmallRing.l = SmallRing.c = 2;
                     45:   SmallRing.nn = SmallRing.mm = SmallRing.ll = 2;
                     46:   SmallRing.cc = 1;
                     47:   SmallRing.x = smallx;
                     48:   SmallRing.D = smalld;
                     49:   SmallRing.order = smallOrder;
                     50:   SmallRing.orderMatrixSize = 4;
                     51:   setFromTo(&SmallRing);
                     52:   SmallRing.next = (struct ring *)NULL;
                     53:   SmallRing.multiplication = mpMult_poly;
                     54:   SmallRing.schreyer = 0;
                     55:   SmallRing.gbListTower = NULL;
                     56:   SmallRing.outputOrder = outputOrderForSmallRing;
                     57:   SmallRing.name = "SmallRingp";
1.4       takayama   58:   SmallRing.partialEcart = 0;
1.1       maekawa    59:
                     60:   CurrentRingp = &SmallRing;
                     61:   initSyzRingp();
                     62:
                     63:   switch_init();
                     64:   initT();  /* Initialize poly3.c */
                     65:
                     66:
                     67: #ifndef NOGC
                     68:   /* initialize MPZ */
1.3       takayama   69:   mp_set_memory_functions(sGC_malloc,sGC_realloc2,sGC_free2);
1.1       maekawa    70: #endif
                     71:
                     72:   /* Msize is 8 on SUN gcc */
                     73:
                     74:   /* We have defined a ring. Let's initialize Nodes */
                     75:
                     76:   Mp_one = &Mp_One;
                     77:   mpz_init(Mp_one); mpz_set_si(Mp_one,(long) 1);
                     78:   Mp_zero = &Mp_Zero;
                     79:   mpz_init(Mp_zero); mpz_set_si(Mp_zero,(long) 0);
                     80:   mpz_init(&Mp_work_iiComb); mpz_init(&Mp_work_iiPoch);
                     81:   mpz_init(&Mp_work_iiPower);
                     82:
                     83:   UniversalZero = intToCoeff(0,&SmallRing);
                     84:   UniversalOne = intToCoeff(1,&SmallRing);
                     85:
                     86:   KdefaultPolyRing(KpoInteger(0));
                     87: }
                     88:
                     89: #ifndef NOGC
                     90: void *sGC_realloc2(void *p,size_t old,size_t new)
                     91: {
                     92:   return((void *)sGC_realloc(p,(int) new));
                     93: }
                     94: void sGC_free2(void *p,size_t size)
                     95: {
                     96:   /* Do nothing. */
                     97: }
                     98: #endif
                     99:
                    100:
                    101: MONOMIAL newMonomial(ringp)
1.3       takayama  102:      struct ring *ringp;
1.1       maekawa   103: {
                    104:   MONOMIAL f;
                    105:   extern int Msize;
                    106:   int i;
                    107:   int n;
                    108:   n = ringp->n;
                    109:   f = (MONOMIAL) sGC_malloc(sizeof(struct smallMonomial)+n*Msize);
                    110:   if (f == (MONOMIAL) NULL) errorPoly("No more memory.");
                    111:   f->ringp = ringp;
                    112:   for (i=0; i<n; i++) {
                    113:     (f->e)[i].x = 0;
                    114:     (f->e)[i].D = 0;  /* necessary?->Yes. */
                    115:   }
                    116:   return(f);
                    117: }
                    118:
                    119:
                    120: MONOMIAL monomialCopy(m)
1.3       takayama  121:      MONOMIAL m;
1.1       maekawa   122: {
                    123:   extern int Msize;
                    124:   MONOMIAL f;
                    125:   int i;
                    126:   int n;
                    127:   n = m->ringp->n;
                    128:   f = (MONOMIAL) sGC_malloc(sizeof(struct smallMonomial)+n*Msize);
                    129:
                    130:   if (f == (MONOMIAL) NULL) errorPoly("No more memory.");
                    131:   f->ringp = m->ringp;
                    132:   for (i=0; i<n; i++) {
                    133:     (f->e)[i].x = (m->e)[i].x;
                    134:     (f->e)[i].D = (m->e)[i].D;
                    135:   }
                    136:   return(f);
                    137: }
                    138:
                    139:
                    140: struct coeff *newCoeff() {
                    141:   struct coeff *cp;
                    142:   cp = (struct coeff *)sGC_malloc(sizeof (struct coeff));
                    143:   if (cp == (struct coeff *)NULL) errorPoly("No more memory.");
                    144:   cp->tag = UNKNOWN;
                    145:   cp->p = -123; /* stupid value */
                    146:   return(cp);
                    147: }
                    148:
                    149: MP_INT *newMP_INT() {
                    150:   MP_INT *ip;
                    151:   ip = (MP_INT *)sGC_malloc(sizeof(MP_INT));
                    152:   if (ip == (MP_INT *)NULL) errorPoly("No more memory.");
                    153:   mpz_init(ip);
                    154:   return(ip);
                    155: }
                    156:
                    157: POLY newCell(c,mon)
1.3       takayama  158:      struct coeff *c;
                    159:      MONOMIAL mon;
1.1       maekawa   160: {
                    161:   POLY ff;
                    162:   ff = (POLY) sGC_malloc(sizeof(struct listPoly));
                    163:   if (ff == POLYNULL) errorPoly("No more memory.");
                    164:   ff->next = POLYNULL;
                    165:   ff->coeffp = c;
                    166:   ff->m = mon;
                    167:   return(ff);
                    168: }
                    169:
                    170: /* constructors */
                    171: POLY cxx(c,i,k,ringp)
1.3       takayama  172:      int c,i,k;
                    173:      struct ring *ringp;
                    174:      /*  c x_i^k where p is the characteristic. */
                    175:      /* New cell, monomial and coeff. */
1.1       maekawa   176: {
                    177:   POLY f;
                    178:   int p;
                    179:   p = ringp->p;
                    180:   if (c == 0) return(POLYNULL);
                    181:   f = pMalloc(ringp);
                    182:   if (ringp->next == (struct ring *)NULL) {
                    183:     if (p) {
                    184:       f->coeffp->tag = INTEGER; f->coeffp->p = p;
                    185:       f->coeffp->val.i = c % p;
                    186:       if (f->coeffp->val.i == 0) return(POLYNULL);
                    187:     }else{
                    188:       f->coeffp->tag = MP_INTEGER; f->coeffp->p = 0;
                    189:       f->coeffp->val.bigp = newMP_INT();
                    190:       mpz_set_si(f->coeffp->val.bigp,(long) c);
                    191:     }
                    192:     f->m->e[i].x = k;
                    193:     return(f);
                    194:   }else{
                    195:     f->coeffp->tag = POLY_COEFF; f->coeffp->p = p;
                    196:     f->coeffp->val.f = cxx(c,0,0,ringp->next);
                    197:     if (f->coeffp->val.f == POLYNULL) return(POLYNULL);
                    198:     f->m->e[i].x = k;
                    199:     return(f);
                    200:   }
                    201: }
                    202:
                    203: POLY bxx(c,i,k,ringp)
1.3       takayama  204:      MP_INT *c;
                    205:      int i,k;
                    206:      struct ring *ringp;
                    207:      /*  c x_i^k.  c is not copied. */
1.1       maekawa   208: {
1.3       takayama  209:   /* new cell, monomial, coeff. MP_INT c is not copied. */
1.1       maekawa   210:   POLY f;
                    211:   int p;
                    212:   p = ringp->p;
                    213:   if (mpz_cmp_si(c,(long)0) == 0) return(POLYNULL);
                    214:   f = pMalloc(ringp);
                    215:   if (ringp->next == (struct ring *)NULL) {
                    216:     if (p) {
                    217:       f->coeffp->tag = INTEGER; f->coeffp->p = p;
                    218:       f->coeffp->val.i = (int) mpz_get_si(c);
                    219:       f->coeffp->val.i %= p;
                    220:       if (f->coeffp->val.i == 0) return(POLYNULL);
                    221:     }else{
                    222:       f->coeffp->tag = MP_INTEGER; f->coeffp->p = 0;
                    223:       f->coeffp->val.bigp = c;
                    224:     }
                    225:     f->m->e[i].x = k;
                    226:     return(f);
                    227:   }else{
                    228:     warningPoly("cbb(): ringp->next is not NULL. Returns 0.");
                    229:     return(POLYNULL);
                    230:   }
                    231: }
                    232:
                    233: POLY cdd(c,i,k,ringp)
1.3       takayama  234:      int c,i,k;
                    235:      struct ring *ringp;
                    236:      /*  c D_i^k where p is the characteristic. */
                    237:      /* New cell, monomial and coeff. */
1.1       maekawa   238: {
                    239:   POLY f;
                    240:   int p;
                    241:   p = ringp->p;
                    242:   if (c == 0) return(POLYNULL);
                    243:   f = pMalloc(ringp);
                    244:   if (ringp->next == (struct ring *)NULL) {
                    245:     if (p) {
                    246:       f->coeffp->tag = INTEGER; f->coeffp->p = p;
                    247:       f->coeffp->val.i = c % p;
                    248:       if (f->coeffp->val.i == 0) return(POLYNULL);
                    249:     }else{
                    250:       f->coeffp->tag = MP_INTEGER; f->coeffp->p = 0;
                    251:       f->coeffp->val.bigp = newMP_INT();
                    252:       mpz_set_si(f->coeffp->val.bigp,(long) c);
                    253:     }
                    254:     f->m->e[i].D = k;
                    255:     return(f);
                    256:   }else{
                    257:     f->coeffp->tag = POLY_COEFF; f->coeffp->p = p;
                    258:     f->coeffp->val.f = cdd(c,0,0,ringp->next);
                    259:     if (f->coeffp->val.f == POLYNULL) return(POLYNULL);
                    260:     f->m->e[i].D = k;
                    261:     return(f);
                    262:   }
                    263:
                    264: }
                    265:
                    266:
                    267: POLY pCopy(f)
1.3       takayama  268:      POLY f;
1.1       maekawa   269: {
                    270:   POLY node;
                    271:   struct listPoly nod;
                    272:   POLY h;
                    273:   node = &nod;
                    274:   if (f == POLYNULL) return(POLYNULL);
                    275:   node->next = POLYNULL;
                    276:   h = node;
                    277:   while (f != POLYNULL) {
                    278:     h->next = newCell(f->coeffp,f->m); /* shallow */
                    279:     h = h->next;
                    280:     f = f->next;
                    281:   }
                    282:   return(node->next);
                    283: }
                    284:
                    285: POLY pcCopy(f)
1.3       takayama  286:      POLY f;
1.1       maekawa   287: {
                    288:
                    289:   POLY node;
                    290:   struct listPoly nod;
                    291:   POLY h;
                    292:   struct coeff *c;
                    293:   node = &nod;
                    294:   if (f == POLYNULL) return(POLYNULL);
                    295:   node->next = POLYNULL;
                    296:   h = node;
                    297:   while (f != POLYNULL) {
                    298:     c = coeffCopy(f->coeffp);
                    299:     h->next = newCell(c,f->m); /* poly and coeff. */
                    300:     h = h->next;
                    301:     f = f->next;
                    302:   }
                    303:   return(node->next);
                    304: }
                    305:
                    306: POLY pmCopy(f)
1.3       takayama  307:      POLY f;
1.1       maekawa   308: {
                    309:   POLY node;
                    310:   struct listPoly nod;
                    311:   POLY h;
                    312:   struct coeff *c;
                    313:   node = &nod;
                    314:   if (f == POLYNULL) return(POLYNULL);
                    315:   node->next = POLYNULL;
                    316:   h = node;
                    317:   while (f != POLYNULL) {
                    318:     h->next = newCell(f->coeffp,monomialCopy(f->m));
                    319:     h = h->next;
                    320:     f = f->next;
                    321:   }
                    322:   return(node->next);
                    323: }
                    324:
                    325: POLY pcmCopy(f)
1.3       takayama  326:      POLY f;
1.1       maekawa   327: {
                    328:   POLY node;
                    329:   struct listPoly nod;
                    330:   POLY h;
                    331:   struct coeff *c;
                    332:   node = &nod;
                    333:   if (f == POLYNULL) return(POLYNULL);
                    334:   node->next = POLYNULL;
                    335:   h = node;
                    336:   while (f != POLYNULL) {
                    337:     h->next = newCell(coeffCopy(f->coeffp),monomialCopy(f->m));
                    338:     h = h->next;
                    339:     f = f->next;
                    340:   }
                    341:   return(node->next);
                    342: }
                    343:
                    344: POLY head(f)
1.3       takayama  345:      POLY f;
1.1       maekawa   346: {
                    347:   if (f == ZERO) return(f);
                    348:   else {
                    349:     return(newCell(f->coeffp,f->m));
                    350:   }
                    351: }
                    352:
                    353: void errorPoly(str)
1.3       takayama  354:      char *str;
1.1       maekawa   355: {
                    356:   fprintf(stderr,"Error(poly.c): %s\n",str);
                    357:   exit(20);
                    358: }
                    359:
                    360: void warningPoly(str)
1.3       takayama  361:      char *str;
1.1       maekawa   362: {
                    363:   fprintf(stderr,"Warning(poly.c): %s\n",str);
                    364: }
                    365:
                    366:
                    367:
                    368:
                    369:
                    370:
                    371:
                    372:
                    373:
                    374:
                    375:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>