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

Annotation of OpenXM/src/kan96xx/Kan/output.c, Revision 1.1.1.1

1.1       maekawa     1: #include <stdio.h>
                      2: #include "datatype.h"
                      3: #include "stackm.h"
                      4: #include "extern.h"
                      5: #include "extern2.h"
                      6:
                      7:
                      8: #define OUTPUT_QUEUE_SIZE 10
                      9:
                     10:
                     11: struct outputQueue {
                     12:   int oqP;
                     13:   int oqLimit;
                     14:   char **outputQueue;
                     15: };
                     16: static void putstr(char *s,struct outputQueue *oq);
                     17:
                     18: static void putstr(s,oq)
                     19: char *s;
                     20: struct outputQueue *oq;
                     21: {
                     22:   int i;
                     23:   char **tmp;
                     24:   int oqP,oqLimit;
                     25:   char **outputQueue;
                     26:   oqP = oq->oqP; oqLimit = oq->oqLimit; outputQueue = oq->outputQueue;
                     27:
                     28:   if (oqP < 0) errorOutput("(putstr) Invalid value of OutputQueue Pointer.");
                     29:   if (oqP <oqLimit) {
                     30:     outputQueue[oqP++] = s;
                     31:   }else{
                     32:     oqLimit = 2*oqLimit;
                     33: #ifndef NOGC
                     34:     outputQueue =
                     35:       (char **)sGC_realloc((void *)outputQueue,oqLimit*sizeof(char **));
                     36: #else
                     37:     tmp = (char **)sGC_malloc(oqLimit*sizeof(char **));
                     38:     for (i=0; i<oqP; i++) {
                     39:       tmp[i] = outputQueue[i];
                     40:     }
                     41:     outputQueue = tmp;
                     42: #endif
                     43:     outputQueue[oqP++] = s;
                     44:   }
                     45:   oq->outputQueue = outputQueue;
                     46:   oq->oqP = oqP;
                     47:   oq->oqLimit = oqLimit;
                     48: }
                     49:
                     50: #define multStr(c) (c==' '?" ":(c=='\0'?"":"*"))
                     51:
                     52: char *POLYToString(f,multSym,brace)
                     53: POLY f;
                     54: int multSym;
                     55: int brace;
                     56: {
                     57:   extern int Wrap;
                     58:   int i,j,jj,fi;
                     59:   int printed = 0;
                     60:   int vi; /* index for variables */
                     61:   char *contStr = " \\\n";
                     62:   int length = 0;
                     63:   int lengthLimit = Wrap; /* e.g. = 15 */
                     64:   char *s;
                     65:   int size,sp;
                     66:   struct outputQueue oq;
                     67:   struct ring *ringp;
                     68:   char **xnames;
                     69:   char **dnames;
                     70:   int *xout;  /* output order */
                     71:   int n = 0;
                     72:
                     73:   oq.oqP = 0;
                     74:   oq.oqLimit = OUTPUT_QUEUE_SIZE;
                     75:   oq.outputQueue = (char **)sGC_malloc(OUTPUT_QUEUE_SIZE*sizeof(char **));
                     76:
                     77:
                     78:
                     79:   fi = 0;
                     80:   if (brace) putstr("(",&oq);
                     81:   if (f == POLYNULL) {
                     82:     putstr("0",&oq);
                     83:     printed = 1;
                     84:     xnames = dnames = (char **)NULL;
                     85:   }else{
                     86:     ringp = f->m->ringp; xnames = f->m->ringp->x; dnames = f->m->ringp->D;
                     87:     n = ringp->n;
                     88:     xout = ringp->outputOrder;
                     89:   }
                     90:   while (f != POLYNULL) {
                     91:     printed = 1;
                     92:     /*print coefficient*/
                     93:     if (fi == 0) {
                     94:       if (isConstant(f)) {
                     95:        putstr(coeffToString(f->coeffp),&oq);
                     96:       }else if (isOne(f->coeffp)) {
                     97:        /* do nothing */
                     98:       }else if (isMinusOne(f->coeffp)) {
                     99:        putstr("-",&oq);
                    100:       }else{
                    101:        putstr(coeffToString(f->coeffp),&oq);
                    102:        putstr(multStr(multSym),&oq);
                    103:       }
                    104:     }else{
                    105:       if (isConstant(f)) {
                    106:        if (isNegative(f->coeffp)) {
                    107:          putstr(coeffToString(f->coeffp),&oq);
                    108:        }else{
                    109:          putstr("+",&oq);
                    110:          putstr(coeffToString(f->coeffp),&oq);
                    111:        }
                    112:       } else if (isOne(f->coeffp)) {
                    113:        putstr("+",&oq);
                    114:       }else if (isMinusOne(f->coeffp)) {
                    115:        putstr("-",&oq);
                    116:       }else{
                    117:        if (!isNegative(f->coeffp)) putstr("+",&oq);
                    118:        putstr(coeffToString(f->coeffp),&oq);
                    119:         putstr(multStr(multSym),&oq);
                    120:       }
                    121:     }
                    122:     /* output variables */
                    123:     vi = 0;
                    124:     for (jj=0; jj<n*2; jj++) {
                    125:       j = xout[jj];
                    126:       if (j <n) {
                    127:        if (f->m->e[j].x) {
                    128:          vi++;
                    129:          if (vi != 1) putstr(multStr(multSym),&oq);
                    130:          putstr(xnames[j],&oq);
                    131:          if (f->m->e[j].x >= 2) {
                    132:            putstr("^",&oq);
                    133:            putstr(intToString(f->m->e[j].x),&oq);
                    134:          }else if (f->m->e[j].x < 0) {
                    135:            putstr("^(",&oq);
                    136:            putstr(intToString(f->m->e[j].x),&oq);
                    137:            putstr(")",&oq);
                    138:          }
                    139:        }
                    140:       }else {
                    141:        j = j-n;
                    142:        if (f->m->e[j].D) {
                    143:          vi++;
                    144:          if (vi != 1) putstr(multStr(multSym),&oq);
                    145:          putstr(dnames[j],&oq);
                    146:          if (f->m->e[j].D >= 2) {
                    147:            putstr("^",&oq);
                    148:            putstr(intToString(f->m->e[j].D),&oq);
                    149:          }else if (f->m->e[j].D < 0) {
                    150:            putstr("^(",&oq);
                    151:            putstr(intToString(f->m->e[j].D),&oq);
                    152:            putstr(")",&oq);
                    153:          }
                    154:        }
                    155:       }
                    156:     }
                    157:     fi++;
                    158:     f = f->next;
                    159:     length += n/3+1;
                    160:     if (lengthLimit > 0  && length > lengthLimit) {
                    161:       length = 0;
                    162:       putstr(contStr,&oq);
                    163:     }
                    164:   }
                    165:   if (!printed) putstr("0",&oq);
                    166:
                    167:   if (brace) putstr(")",&oq);
                    168:
                    169:   size = 0;
                    170:   for (i=0; i<oq.oqP;i++) {
                    171:     size += strlen(oq.outputQueue[i]);
                    172:   }
                    173:   s = (char *)sGC_malloc(sizeof(char *)*(size + 2));
                    174:   if (s == (char *)NULL) errorOutput("No more memory.");
                    175:   sp = 0;
                    176:   for (i=0; i<oq.oqP; i++) {
                    177:     strcpy(&(s[sp]),oq.outputQueue[i]);
                    178:     sp += strlen(oq.outputQueue[i]);
                    179:   }
                    180:
                    181:   return(s);
                    182: }
                    183:
                    184: char *KPOLYToString(f)
                    185: POLY f;
                    186: {
                    187:   extern int OutputStyle;
                    188:   return(POLYToString(f,OutputStyle,0));
                    189: }
                    190:
                    191: isOne(c)
                    192: struct coeff *c;
                    193: {
                    194:   switch(c->tag) {
                    195:   case INTEGER:
                    196:     if (c->val.i == 1) return(1); else return(0);
                    197:     break;
                    198:   case POLY_COEFF:
                    199:     return(0);
                    200:     break;
                    201:   case MP_INTEGER:
                    202:     if (mpz_cmp_si(c->val.bigp,(long) 1) == 0) return(1);
                    203:     else return(0);
                    204:     break;
                    205:   default:
                    206:     errorCoeff("not yet");
                    207:   }
                    208: }
                    209: isMinusOne(c)
                    210: struct coeff *c;
                    211: {
                    212:   switch(c->tag) {
                    213:   case INTEGER:
                    214:     if (c->val.i == -1) return(1); else return(0);
                    215:     break;
                    216:   case POLY_COEFF:
                    217:     return(0);
                    218:     break;
                    219:   case MP_INTEGER:
                    220:     if (mpz_cmp_si(c->val.bigp,(long) -1) == 0) return(1);
                    221:     return(0);
                    222:   default:
                    223:     errorCoeff("not yet");
                    224:   }
                    225:
                    226: }
                    227: isNegative(c)
                    228: struct coeff *c;
                    229: {
                    230:   switch(c->tag) {
                    231:   case INTEGER:
                    232:     if (c->val.i < 0) return(1); else return(0);
                    233:     break;
                    234:   case POLY_COEFF:
                    235:     return(0);
                    236:     break;
                    237:   case MP_INTEGER:
                    238:     if (mpz_cmp_si(c->val.bigp,(long) 1) < 0) return(1);
                    239:     else return(0);
                    240:     break;
                    241:   default:
                    242:     errorCoeff("not yet");
                    243:   }
                    244: }
                    245:
                    246: isConstant(f)
                    247: POLY f;
                    248: {
                    249:   int i;
                    250:   int n;
                    251:   if (f == POLYNULL) return(1);
                    252:   n = f->m->ringp->n;
                    253:   for (i=0; i<n; i++) {
                    254:     if (f->m->e[i].x  ||  f->m->e[i].D) return(0);
                    255:   }
                    256:   return(1);
                    257: }
                    258:
                    259: void errorOutput(s)
                    260: char *s;
                    261: {
                    262:   fprintf(stderr,"Error(output.c):%s\n",s);
                    263:   exit(15);
                    264: }

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