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

1.8     ! takayama    1: /* $OpenXM: OpenXM/src/kan96xx/Kan/output.c,v 1.7 2011/10/05 05:46:14 takayama 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.7       takayama   63:   extern int COutput;
1.1       maekawa    64:   int i,j,jj,fi;
                     65:   int printed = 0;
                     66:   int vi; /* index for variables */
                     67:   char *contStr = " \\\n";
                     68:   int length = 0;
                     69:   int lengthLimit = Wrap; /* e.g. = 15 */
                     70:   char *s;
                     71:   int size,sp;
                     72:   struct outputQueue oq;
                     73:   struct ring *ringp;
                     74:   char **xnames;
                     75:   char **dnames;
                     76:   int *xout;  /* output order */
                     77:   int n = 0;
                     78:
                     79:   oq.oqP = 0;
                     80:   oq.oqLimit = OUTPUT_QUEUE_SIZE;
                     81:   oq.outputQueue = (char **)sGC_malloc(OUTPUT_QUEUE_SIZE*sizeof(char **));
                     82:
                     83:
                     84:
                     85:   fi = 0;
                     86:   if (brace) putstr("(",&oq);
                     87:   if (f == POLYNULL) {
                     88:     putstr("0",&oq);
                     89:     printed = 1;
                     90:     xnames = dnames = (char **)NULL;
                     91:   }else{
1.6       takayama   92:     ringp = f->m->ringp; xnames = f->m->ringp->x;
                     93:     if (UseDsmall) dnames = f->m->ringp->Dsmall;
                     94:     else dnames = f->m->ringp->D;
1.1       maekawa    95:     n = ringp->n;
                     96:     xout = ringp->outputOrder;
                     97:   }
                     98:   while (f != POLYNULL) {
                     99:     printed = 1;
                    100:     /*print coefficient*/
                    101:     if (fi == 0) {
                    102:       if (isConstant(f)) {
1.3       takayama  103:         putstr(coeffToString(f->coeffp),&oq);
1.1       maekawa   104:       }else if (isOne(f->coeffp)) {
1.3       takayama  105:         /* do nothing */
1.1       maekawa   106:       }else if (isMinusOne(f->coeffp)) {
1.3       takayama  107:         putstr("-",&oq);
1.1       maekawa   108:       }else{
1.3       takayama  109:         putstr(coeffToString(f->coeffp),&oq);
                    110:         putstr(multStr(multSym),&oq);
1.1       maekawa   111:       }
                    112:     }else{
                    113:       if (isConstant(f)) {
1.3       takayama  114:         if (isNegative(f->coeffp)) {
                    115:           putstr(coeffToString(f->coeffp),&oq);
                    116:         }else{
                    117:           putstr("+",&oq);
                    118:           putstr(coeffToString(f->coeffp),&oq);
                    119:         }
1.1       maekawa   120:       } else if (isOne(f->coeffp)) {
1.3       takayama  121:         putstr("+",&oq);
1.1       maekawa   122:       }else if (isMinusOne(f->coeffp)) {
1.3       takayama  123:         putstr("-",&oq);
1.1       maekawa   124:       }else{
1.3       takayama  125:         if (!isNegative(f->coeffp)) putstr("+",&oq);
                    126:         putstr(coeffToString(f->coeffp),&oq);
1.1       maekawa   127:         putstr(multStr(multSym),&oq);
                    128:       }
                    129:     }
                    130:     /* output variables */
                    131:     vi = 0;
                    132:     for (jj=0; jj<n*2; jj++) {
                    133:       j = xout[jj];
                    134:       if (j <n) {
1.7       takayama  135:                if (COutput) {
                    136:                  if (f->m->e[j].x) {
                    137:                        vi++;
                    138:                        if (vi != 1) putstr(multStr(multSym),&oq);
                    139:                        if (f->m->e[j].x == 1) {
                    140:                          putstr(xnames[j],&oq);
                    141:                        } else {
                    142:                          putstr("sm1power(",&oq);
                    143:                          putstr(xnames[j],&oq);
                    144:                          putstr(",",&oq);
                    145:                          putstr(intToString(f->m->e[j].x),&oq);
                    146:                          putstr(")",&oq);
                    147:                        }
                    148:                  }
                    149:                }else{
                    150:                  if (f->m->e[j].x) {
                    151:                        vi++;
                    152:                        if (vi != 1) putstr(multStr(multSym),&oq);
                    153:                        putstr(xnames[j],&oq);
                    154:                        if (f->m->e[j].x >= 2) {
                    155:                          putstr("^",&oq);
                    156:                          putstr(intToString(f->m->e[j].x),&oq);
                    157:                        }else if (f->m->e[j].x < 0) {
                    158:                          putstr("^(",&oq);
                    159:                          putstr(intToString(f->m->e[j].x),&oq);
                    160:                          putstr(")",&oq);
                    161:                        }
                    162:                  }
                    163:                }
1.1       maekawa   164:       }else {
1.7       takayama  165:                if (COutput) {
                    166:                  j = j-n;
                    167:                  if (f->m->e[j].D) {
                    168:                        vi++;
                    169:                        if (vi != 1) putstr(multStr(multSym),&oq);
                    170:                        if (f->m->e[j].D == 1) {
                    171:                          putstr(dnames[j],&oq);
                    172:                        }else {
                    173:                          if (f->m->e[j].D) putstr("sm1power(",&oq);
                    174:                          putstr(dnames[j],&oq);
                    175:                          putstr(",",&oq);
                    176:                          putstr(intToString(f->m->e[j].D),&oq);
                    177:                          putstr(")",&oq);
                    178:                        }
                    179:                  }
                    180:                }else{
                    181:                  j = j-n;
                    182:                  if (f->m->e[j].D) {
                    183:                        vi++;
                    184:                        if (vi != 1) putstr(multStr(multSym),&oq);
                    185:                        putstr(dnames[j],&oq);
                    186:                        if (f->m->e[j].D >= 2) {
                    187:                          putstr("^",&oq);
                    188:                          putstr(intToString(f->m->e[j].D),&oq);
                    189:                        }else if (f->m->e[j].D < 0) {
                    190:                          putstr("^(",&oq);
                    191:                          putstr(intToString(f->m->e[j].D),&oq);
                    192:                          putstr(")",&oq);
                    193:                        }
                    194:                  }
                    195:                }
1.1       maekawa   196:       }
                    197:     }
                    198:     fi++;
                    199:     f = f->next;
                    200:     length += n/3+1;
                    201:     if (lengthLimit > 0  && length > lengthLimit) {
                    202:       length = 0;
                    203:       putstr(contStr,&oq);
                    204:     }
                    205:   }
                    206:   if (!printed) putstr("0",&oq);
                    207:
                    208:   if (brace) putstr(")",&oq);
                    209:
                    210:   size = 0;
                    211:   for (i=0; i<oq.oqP;i++) {
                    212:     size += strlen(oq.outputQueue[i]);
                    213:   }
                    214:   s = (char *)sGC_malloc(sizeof(char *)*(size + 2));
                    215:   if (s == (char *)NULL) errorOutput("No more memory.");
                    216:   sp = 0;
                    217:   for (i=0; i<oq.oqP; i++) {
                    218:     strcpy(&(s[sp]),oq.outputQueue[i]);
                    219:     sp += strlen(oq.outputQueue[i]);
                    220:   }
                    221:
                    222:   return(s);
                    223: }
                    224:
                    225: char *KPOLYToString(f)
1.3       takayama  226:      POLY f;
1.1       maekawa   227: {
                    228:   extern int OutputStyle;
                    229:   return(POLYToString(f,OutputStyle,0));
                    230: }
                    231:
1.8     ! takayama  232: int isOne(c)
1.3       takayama  233:      struct coeff *c;
1.1       maekawa   234: {
                    235:   switch(c->tag) {
                    236:   case INTEGER:
                    237:     if (c->val.i == 1) return(1); else return(0);
                    238:     break;
                    239:   case POLY_COEFF:
                    240:     return(0);
                    241:     break;
                    242:   case MP_INTEGER:
                    243:     if (mpz_cmp_si(c->val.bigp,(long) 1) == 0) return(1);
                    244:     else return(0);
                    245:     break;
                    246:   default:
                    247:     errorCoeff("not yet");
                    248:   }
                    249: }
1.8     ! takayama  250: int isMinusOne(c)
1.3       takayama  251:      struct coeff *c;
1.1       maekawa   252: {
                    253:   switch(c->tag) {
                    254:   case INTEGER:
                    255:     if (c->val.i == -1) return(1); else return(0);
                    256:     break;
                    257:   case POLY_COEFF:
                    258:     return(0);
                    259:     break;
                    260:   case MP_INTEGER:
                    261:     if (mpz_cmp_si(c->val.bigp,(long) -1) == 0) return(1);
                    262:     return(0);
                    263:   default:
                    264:     errorCoeff("not yet");
                    265:   }
                    266:
                    267: }
1.8     ! takayama  268: int isNegative(c)
1.3       takayama  269:      struct coeff *c;
1.1       maekawa   270: {
                    271:   switch(c->tag) {
                    272:   case INTEGER:
                    273:     if (c->val.i < 0) return(1); else return(0);
                    274:     break;
                    275:   case POLY_COEFF:
                    276:     return(0);
                    277:     break;
                    278:   case MP_INTEGER:
                    279:     if (mpz_cmp_si(c->val.bigp,(long) 1) < 0) return(1);
                    280:     else return(0);
                    281:     break;
                    282:   default:
                    283:     errorCoeff("not yet");
                    284:   }
                    285: }
                    286:
1.8     ! takayama  287: int isConstant(f)
1.3       takayama  288:      POLY f;
1.1       maekawa   289: {
                    290:   int i;
                    291:   int n;
                    292:   if (f == POLYNULL) return(1);
                    293:   n = f->m->ringp->n;
                    294:   for (i=0; i<n; i++) {
                    295:     if (f->m->e[i].x  ||  f->m->e[i].D) return(0);
                    296:   }
                    297:   return(1);
1.4       takayama  298: }
                    299:
                    300: int isConstantAll(POLY f)
                    301: {
                    302:   int i;
                    303:   int n;
                    304:   if (f == POLYNULL) return(1);
                    305:   while (f != POLYNULL) {
                    306:        if (!isConstant(f)) return 0;
                    307:        f = f->next;
                    308:   }
                    309:   return 1;
1.1       maekawa   310: }
                    311:
                    312: void errorOutput(s)
1.3       takayama  313:      char *s;
1.1       maekawa   314: {
                    315:   fprintf(stderr,"Error(output.c):%s\n",s);
                    316:   exit(15);
                    317: }
1.6       takayama  318:
                    319: char **makeDsmall(char **dvars,int n) {
                    320:   char **ans;
                    321:   int i;
                    322:   ans = (char **) sGC_malloc(sizeof(char *)*(n+1));
                    323:   for (i=0; i<n; i++) {
                    324:        ans[i] =  (char *) sGC_malloc(sizeof(char)*(strlen(dvars[i])+1));
                    325:        strcpy(ans[i],dvars[i]);
                    326:        ans[i][0] = tolower(ans[i][0]);
                    327:   }
                    328:   ans[n] = NULL;
                    329:   return ans;
                    330: }

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