[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.3

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

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