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