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

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

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