Annotation of OpenXM/src/kan96xx/Kan/poly.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: void KinitKan(void) {
9:
10: extern int Msize;
11: extern struct ring SmallRing;
12: extern struct ring *CurrentRingp;
13: extern MP_INT *Mp_one;
14: extern MP_INT *Mp_zero;
15: extern MP_INT Mp_work_iiComb;
16: extern MP_INT Mp_work_iiPoch;
17: extern MP_INT Mp_work_iiPower;
18:
19: static MP_INT Mp_One;
20: static MP_INT Mp_Zero;
21:
22: extern struct coeff *UniversalZero;
23: extern struct coeff *UniversalOne;
24:
25: int i;
26: static char *smallx[] = {"x","t"};
27: static char *smalld[] = {"h","T"};
28: /* t x T h */
29: static int smallOrder[] = { 1, 0, 0, 0,
30: 0, 1, 0, 0,
31: 0, 0, 1, 0,
32: 0, 0, 0, 1};
33:
34: static int outputOrderForSmallRing[] = {1,0,3,2};
35: Msize = sizeof(struct monomial) - sizeof(struct monomialDummy);
36: if (Msize <= 0) errorPoly("Problem of your C-compiler??!!");
37:
38: /* Define the CurrentRingp */
39: /* Define the SmallRingp */
40:
41: SmallRing.p = 0;
42: SmallRing.n = SmallRing.m = SmallRing.l = SmallRing.c = 2;
43: SmallRing.nn = SmallRing.mm = SmallRing.ll = 2;
44: SmallRing.cc = 1;
45: SmallRing.x = smallx;
46: SmallRing.D = smalld;
47: SmallRing.order = smallOrder;
48: SmallRing.orderMatrixSize = 4;
49: setFromTo(&SmallRing);
50: SmallRing.next = (struct ring *)NULL;
51: SmallRing.multiplication = mpMult_poly;
52: SmallRing.schreyer = 0;
53: SmallRing.gbListTower = NULL;
54: SmallRing.outputOrder = outputOrderForSmallRing;
55: SmallRing.name = "SmallRingp";
56:
57: CurrentRingp = &SmallRing;
58: initSyzRingp();
59:
60: switch_init();
61: initT(); /* Initialize poly3.c */
62:
63:
64: #ifndef NOGC
65: /* initialize MPZ */
66: mp_set_memory_functions(sGC_malloc,sGC_realloc2,sGC_free2);
67: #endif
68:
69: /* Msize is 8 on SUN gcc */
70:
71: /* We have defined a ring. Let's initialize Nodes */
72:
73: Mp_one = &Mp_One;
74: mpz_init(Mp_one); mpz_set_si(Mp_one,(long) 1);
75: Mp_zero = &Mp_Zero;
76: mpz_init(Mp_zero); mpz_set_si(Mp_zero,(long) 0);
77: mpz_init(&Mp_work_iiComb); mpz_init(&Mp_work_iiPoch);
78: mpz_init(&Mp_work_iiPower);
79:
80: UniversalZero = intToCoeff(0,&SmallRing);
81: UniversalOne = intToCoeff(1,&SmallRing);
82:
83: KdefaultPolyRing(KpoInteger(0));
84: }
85:
86: #ifndef NOGC
87: void *sGC_realloc2(void *p,size_t old,size_t new)
88: {
89: return((void *)sGC_realloc(p,(int) new));
90: }
91: void sGC_free2(void *p,size_t size)
92: {
93: /* Do nothing. */
94: }
95: #endif
96:
97:
98: MONOMIAL newMonomial(ringp)
99: struct ring *ringp;
100: {
101: MONOMIAL f;
102: extern int Msize;
103: int i;
104: int n;
105: n = ringp->n;
106: f = (MONOMIAL) sGC_malloc(sizeof(struct smallMonomial)+n*Msize);
107: if (f == (MONOMIAL) NULL) errorPoly("No more memory.");
108: f->ringp = ringp;
109: for (i=0; i<n; i++) {
110: (f->e)[i].x = 0;
111: (f->e)[i].D = 0; /* necessary?->Yes. */
112: }
113: return(f);
114: }
115:
116:
117: MONOMIAL monomialCopy(m)
118: MONOMIAL m;
119: {
120: extern int Msize;
121: MONOMIAL f;
122: int i;
123: int n;
124: n = m->ringp->n;
125: f = (MONOMIAL) sGC_malloc(sizeof(struct smallMonomial)+n*Msize);
126:
127: if (f == (MONOMIAL) NULL) errorPoly("No more memory.");
128: f->ringp = m->ringp;
129: for (i=0; i<n; i++) {
130: (f->e)[i].x = (m->e)[i].x;
131: (f->e)[i].D = (m->e)[i].D;
132: }
133: return(f);
134: }
135:
136:
137: struct coeff *newCoeff() {
138: struct coeff *cp;
139: cp = (struct coeff *)sGC_malloc(sizeof (struct coeff));
140: if (cp == (struct coeff *)NULL) errorPoly("No more memory.");
141: cp->tag = UNKNOWN;
142: cp->p = -123; /* stupid value */
143: return(cp);
144: }
145:
146: MP_INT *newMP_INT() {
147: MP_INT *ip;
148: ip = (MP_INT *)sGC_malloc(sizeof(MP_INT));
149: if (ip == (MP_INT *)NULL) errorPoly("No more memory.");
150: mpz_init(ip);
151: return(ip);
152: }
153:
154: POLY newCell(c,mon)
155: struct coeff *c;
156: MONOMIAL mon;
157: {
158: POLY ff;
159: ff = (POLY) sGC_malloc(sizeof(struct listPoly));
160: if (ff == POLYNULL) errorPoly("No more memory.");
161: ff->next = POLYNULL;
162: ff->coeffp = c;
163: ff->m = mon;
164: return(ff);
165: }
166:
167: /* constructors */
168: POLY cxx(c,i,k,ringp)
169: int c,i,k;
170: struct ring *ringp;
171: /* c x_i^k where p is the characteristic. */
172: /* New cell, monomial and coeff. */
173: {
174: POLY f;
175: int p;
176: p = ringp->p;
177: if (c == 0) return(POLYNULL);
178: f = pMalloc(ringp);
179: if (ringp->next == (struct ring *)NULL) {
180: if (p) {
181: f->coeffp->tag = INTEGER; f->coeffp->p = p;
182: f->coeffp->val.i = c % p;
183: if (f->coeffp->val.i == 0) return(POLYNULL);
184: }else{
185: f->coeffp->tag = MP_INTEGER; f->coeffp->p = 0;
186: f->coeffp->val.bigp = newMP_INT();
187: mpz_set_si(f->coeffp->val.bigp,(long) c);
188: }
189: f->m->e[i].x = k;
190: return(f);
191: }else{
192: f->coeffp->tag = POLY_COEFF; f->coeffp->p = p;
193: f->coeffp->val.f = cxx(c,0,0,ringp->next);
194: if (f->coeffp->val.f == POLYNULL) return(POLYNULL);
195: f->m->e[i].x = k;
196: return(f);
197: }
198: }
199:
200: POLY bxx(c,i,k,ringp)
201: MP_INT *c;
202: int i,k;
203: struct ring *ringp;
204: /* c x_i^k. c is not copied. */
205: {
206: /* new cell, monomial, coeff. MP_INT c is not copied. */
207: POLY f;
208: int p;
209: p = ringp->p;
210: if (mpz_cmp_si(c,(long)0) == 0) return(POLYNULL);
211: f = pMalloc(ringp);
212: if (ringp->next == (struct ring *)NULL) {
213: if (p) {
214: f->coeffp->tag = INTEGER; f->coeffp->p = p;
215: f->coeffp->val.i = (int) mpz_get_si(c);
216: f->coeffp->val.i %= p;
217: if (f->coeffp->val.i == 0) return(POLYNULL);
218: }else{
219: f->coeffp->tag = MP_INTEGER; f->coeffp->p = 0;
220: f->coeffp->val.bigp = c;
221: }
222: f->m->e[i].x = k;
223: return(f);
224: }else{
225: warningPoly("cbb(): ringp->next is not NULL. Returns 0.");
226: return(POLYNULL);
227: }
228: }
229:
230: POLY cdd(c,i,k,ringp)
231: int c,i,k;
232: struct ring *ringp;
233: /* c D_i^k where p is the characteristic. */
234: /* New cell, monomial and coeff. */
235: {
236: POLY f;
237: int p;
238: p = ringp->p;
239: if (c == 0) return(POLYNULL);
240: f = pMalloc(ringp);
241: if (ringp->next == (struct ring *)NULL) {
242: if (p) {
243: f->coeffp->tag = INTEGER; f->coeffp->p = p;
244: f->coeffp->val.i = c % p;
245: if (f->coeffp->val.i == 0) return(POLYNULL);
246: }else{
247: f->coeffp->tag = MP_INTEGER; f->coeffp->p = 0;
248: f->coeffp->val.bigp = newMP_INT();
249: mpz_set_si(f->coeffp->val.bigp,(long) c);
250: }
251: f->m->e[i].D = k;
252: return(f);
253: }else{
254: f->coeffp->tag = POLY_COEFF; f->coeffp->p = p;
255: f->coeffp->val.f = cdd(c,0,0,ringp->next);
256: if (f->coeffp->val.f == POLYNULL) return(POLYNULL);
257: f->m->e[i].D = k;
258: return(f);
259: }
260:
261: }
262:
263:
264: POLY pCopy(f)
265: POLY f;
266: {
267: POLY node;
268: struct listPoly nod;
269: POLY h;
270: node = &nod;
271: if (f == POLYNULL) return(POLYNULL);
272: node->next = POLYNULL;
273: h = node;
274: while (f != POLYNULL) {
275: h->next = newCell(f->coeffp,f->m); /* shallow */
276: h = h->next;
277: f = f->next;
278: }
279: return(node->next);
280: }
281:
282: POLY pcCopy(f)
283: POLY f;
284: {
285:
286: POLY node;
287: struct listPoly nod;
288: POLY h;
289: struct coeff *c;
290: node = &nod;
291: if (f == POLYNULL) return(POLYNULL);
292: node->next = POLYNULL;
293: h = node;
294: while (f != POLYNULL) {
295: c = coeffCopy(f->coeffp);
296: h->next = newCell(c,f->m); /* poly and coeff. */
297: h = h->next;
298: f = f->next;
299: }
300: return(node->next);
301: }
302:
303: POLY pmCopy(f)
304: POLY f;
305: {
306: POLY node;
307: struct listPoly nod;
308: POLY h;
309: struct coeff *c;
310: node = &nod;
311: if (f == POLYNULL) return(POLYNULL);
312: node->next = POLYNULL;
313: h = node;
314: while (f != POLYNULL) {
315: h->next = newCell(f->coeffp,monomialCopy(f->m));
316: h = h->next;
317: f = f->next;
318: }
319: return(node->next);
320: }
321:
322: POLY pcmCopy(f)
323: POLY f;
324: {
325: POLY node;
326: struct listPoly nod;
327: POLY h;
328: struct coeff *c;
329: node = &nod;
330: if (f == POLYNULL) return(POLYNULL);
331: node->next = POLYNULL;
332: h = node;
333: while (f != POLYNULL) {
334: h->next = newCell(coeffCopy(f->coeffp),monomialCopy(f->m));
335: h = h->next;
336: f = f->next;
337: }
338: return(node->next);
339: }
340:
341: POLY head(f)
342: POLY f;
343: {
344: if (f == ZERO) return(f);
345: else {
346: return(newCell(f->coeffp,f->m));
347: }
348: }
349:
350: void errorPoly(str)
351: char *str;
352: {
353: fprintf(stderr,"Error(poly.c): %s\n",str);
354: exit(20);
355: }
356:
357: void warningPoly(str)
358: char *str;
359: {
360: fprintf(stderr,"Warning(poly.c): %s\n",str);
361: }
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>