Annotation of OpenXM/src/ox_toolkit/cmo.c, Revision 1.22
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.22 ! ohara 2: /* $OpenXM: OpenXM/src/ox_toolkit/cmo.c,v 1.21 2005/10/12 04:03:37 takayama Exp $ */
1.1 ohara 3:
4: /*
5: This module includes functions for sending/receiveng CMO's.
6: Some commnets is written in Japanese by the EUC-JP coded
7: character set.
8: */
9:
10: #include <stdio.h>
11: #include <stdlib.h>
12: #include <stdarg.h>
13: #include <string.h>
14: #include "ox_toolkit.h"
15: #include "parse.h"
16:
1.12 ohara 17: static cell* new_cell(cmo *ob, int e);
1.1 ohara 18: static char* new_string_set_cmo_null();
19: static char* new_string_set_cmo_int32(int integer);
20: static char* new_string_set_cmo_list(cmo_list *c);
21: static char* new_string_set_cmo_zz(cmo_zz *c);
1.7 ohara 22: static char* new_string_set_cmo_double(cmo_double *m);
1.1 ohara 23:
24: /* functions for a cmo_list */
1.12 ohara 25: static cell* new_cell(cmo *ob, int e)
1.1 ohara 26: {
1.11 ohara 27: cell* h = MALLOC(sizeof(cell));
1.1 ohara 28: h->next = NULL;
29: h->prev = NULL;
30: h->cmo = ob;
1.12 ohara 31: h->exp = e;
1.1 ohara 32: return h;
33: }
34:
35: cell* list_next(cell *el)
36: {
37: return el->next;
38: }
39:
40: cell* list_first(cmo_list *this)
41: {
42: return this->head->next;
43: }
44:
45: cmo* list_first_cmo(cmo_list *this)
46: {
47: return list_first(this)->cmo;
48: }
49:
50: int list_endof(cmo_list *this, cell *el)
51: {
52: return (this->head == el);
53: }
54:
55: /* (prev, head) => (prev, new, head) */
56: static void list_cons(cell *head, cell *new)
57: {
58: cell* prev = head->prev;
59: new->prev = prev;
60: head->prev = new;
61: prev->next = new;
62: new->next = head;
63: }
64:
65: cmo_list *list_append(cmo_list* this, cmo* ob)
66: {
1.12 ohara 67: list_cons(this->head, new_cell(ob, 0));
68: this->length++;
69: return this;
70: }
71:
72: cmo_list *list_append_monomial(cmo_list* this, cmo* coef, int exp)
73: {
74: list_cons(this->head, new_cell(coef, exp));
1.1 ohara 75: this->length++;
76: return this;
77: }
78:
79: /* call as list_appendl(List, ob1, ob2, ob3, NULL) */
80: cmo_list *list_appendl(cmo_list* this, ...)
81: {
82: cmo *ob;
83: va_list ap;
84: va_start(ap, this);
1.3 ohara 85: if (this == NULL) {
86: this = new_cmo_list();
87: }
1.1 ohara 88: while((ob = va_arg(ap, cmo *)) != NULL) {
89: list_append(this, ob);
90: }
91: va_end(ap);
92: return this;
93: }
94:
95: int list_length(cmo_list* this)
96: {
97: return this->length;
98: }
99:
100: cmo *list_nth(cmo_list* this, int n)
101: {
102: cell* el;
103: if(list_length(this) > n) {
104: el = list_first(this);
105: while(n-- > 0) {
106: el = list_next(el);
107: }
108: return el->cmo;
109: }
110: return NULL;
111: }
112:
113: /* for GNU mpz */
114: void resize_mpz(mpz_ptr mpz, int size)
115: {
116: _mpz_realloc(mpz, abs(size));
117: mpz->_mp_size = size;
118: }
119:
120: /* functions named new_cmo_*. */
121: cmo_null* new_cmo_null()
122: {
1.13 ohara 123: cmo_null* m = MALLOC_ATOMIC(sizeof(cmo_null));
1.1 ohara 124: m->tag = CMO_NULL;
125: return m;
126: }
127:
128: cmo_int32* new_cmo_int32(int i)
129: {
130: cmo_int32* c;
1.13 ohara 131: c = MALLOC_ATOMIC(sizeof(cmo_int32));
132: c->tag = CMO_INT32;
1.1 ohara 133: c->i = i;
134: return c;
135: }
136:
137: cmo_string* new_cmo_string(char* s)
138: {
1.11 ohara 139: cmo_string* c = MALLOC(sizeof(cmo_string));
1.1 ohara 140: c->tag = CMO_STRING;
141: if (s != NULL) {
1.13 ohara 142: c->s = MALLOC_ATOMIC(strlen(s)+1);
1.1 ohara 143: strcpy(c->s, s);
144: }else {
145: c->s = NULL;
146: }
147: return c;
148: }
149:
150: cmo_mathcap* new_cmo_mathcap(cmo* ob)
151: {
1.11 ohara 152: cmo_mathcap* c = MALLOC(sizeof(cmo_mathcap));
1.1 ohara 153: c->tag = CMO_MATHCAP;
154: c->ob = ob;
155: return c;
156: }
157:
158: cmo_list* new_cmo_list()
159: {
1.11 ohara 160: cmo_list* c = MALLOC(sizeof(cmo_list));
1.1 ohara 161: c->tag = CMO_LIST;
162: c->length = 0;
163: c->head->next = c->head;
164: c->head->prev = c->head;
1.19 ohara 165: return c;
166: }
167:
1.21 takayama 168: cmo_list* new_cmo_list_array(void *array[], int n)
1.19 ohara 169: {
170: int i;
171: cmo_list* c = new_cmo_list();
172: for(i=0; i<n; i++) {
173: list_append(c, array[i]);
174: }
175: return c;
176: }
177:
1.21 takayama 178: cmo_list* new_cmo_list_array_map(void *array[], int n, void *(* mapf)(void *))
1.19 ohara 179: {
180: int i;
181: cmo_list* c = new_cmo_list();
182: for(i=0; i<n; i++) {
183: list_append(c, (cmo *)mapf(array[i]));
184: }
1.1 ohara 185: return c;
186: }
187:
188: cmo_monomial32* new_cmo_monomial32()
189: {
1.11 ohara 190: cmo_monomial32* c = MALLOC(sizeof(cmo_monomial32));
1.1 ohara 191: c->tag = CMO_MONOMIAL32;
192: return c;
193: }
194:
195: cmo_monomial32* new_cmo_monomial32_size(int size)
196: {
197: cmo_monomial32* c = new_cmo_monomial32();
198: if (size>0) {
199: c->length = size;
1.11 ohara 200: c->exps = MALLOC(sizeof(int)*size);
1.1 ohara 201: }
202: return c;
203: }
204:
205: cmo_zz* new_cmo_zz()
206: {
1.11 ohara 207: cmo_zz* c = MALLOC(sizeof(cmo_zz));
1.1 ohara 208: c->tag = CMO_ZZ;
209: mpz_init(c->mpz);
210: return c;
211: }
212:
213: cmo_zz* new_cmo_zz_noinit()
214: {
1.11 ohara 215: cmo_zz* c = MALLOC(sizeof(cmo_zz));
1.1 ohara 216: c->tag = CMO_ZZ;
217: return c;
218: }
219:
220: cmo_zz* new_cmo_zz_set_si(int i)
221: {
222: cmo_zz* c = new_cmo_zz();
223: mpz_set_si(c->mpz, i);
224: return c;
225: }
226:
227: cmo_zz* new_cmo_zz_set_mpz(mpz_ptr z)
228: {
229: cmo_zz* c = new_cmo_zz();
230: mpz_set(c->mpz, z);
231: return c;
232: }
233:
234: cmo_zz *new_cmo_zz_set_string(char *s)
235: {
236: cmo_zz* c = new_cmo_zz_noinit();
237: mpz_init_set_str(c->mpz, s, 10);
238: return c;
239: }
240:
241: cmo_zz* new_cmo_zz_size(int size)
242: {
243: cmo_zz* c = new_cmo_zz();
244: resize_mpz(c->mpz, size);
245: return c;
246: }
247:
1.22 ! ohara 248: cmo_qq* new_cmo_qq()
1.18 ohara 249: {
250: cmo_qq* c = MALLOC(sizeof(cmo_qq));
251: c->tag = CMO_QQ;
1.22 ! ohara 252: mpq_init(c->mpq);
1.18 ohara 253: return c;
254: }
255:
256: cmo_qq* new_cmo_qq_set_mpq(mpq_ptr q)
257: {
1.22 ! ohara 258: cmo_qq* c = new_cmo_qq();
! 259: mpq_set(c->mpq, q);
1.18 ohara 260: return c;
261: }
262:
263: cmo_qq* new_cmo_qq_set_mpz(mpz_ptr num, mpz_ptr den)
264: {
1.22 ! ohara 265: cmo_qq* c = new_cmo_qq();
! 266: mpq_set_num(c->mpq, num);
! 267: mpq_set_den(c->mpq, den);
1.18 ohara 268: return c;
269: }
270:
1.1 ohara 271: cmo_zero* new_cmo_zero()
272: {
1.13 ohara 273: cmo_zero* m = MALLOC_ATOMIC(sizeof(cmo_zero));
1.1 ohara 274: m->tag = CMO_ZERO;
1.6 ohara 275: return m;
276: }
277:
278: cmo_double *new_cmo_double(double d)
279: {
1.13 ohara 280: cmo_double* m = MALLOC_ATOMIC(sizeof(cmo_double));
1.18 ohara 281: m->tag = CMO_IEEE_DOUBLE_FLOAT;
1.6 ohara 282: m->d = d;
1.1 ohara 283: return m;
284: }
285:
286: cmo_dms_generic* new_cmo_dms_generic()
287: {
1.13 ohara 288: cmo_dms_generic* m = MALLOC_ATOMIC(sizeof(cmo_dms_generic));
1.1 ohara 289: m->tag = CMO_DMS_GENERIC;
290: return m;
291: }
292:
293: cmo_ring_by_name* new_cmo_ring_by_name(cmo* ob)
294: {
1.11 ohara 295: cmo_ring_by_name* c = MALLOC(sizeof(cmo_ring_by_name));
1.1 ohara 296: c->tag = CMO_RING_BY_NAME;
297: c->ob = ob;
298: return c;
299: }
300:
301: cmo_indeterminate* new_cmo_indeterminate(cmo* ob)
302: {
1.11 ohara 303: cmo_indeterminate* c = MALLOC(sizeof(cmo_indeterminate));
1.1 ohara 304: c->tag = CMO_INDETERMINATE;
305: c->ob = ob;
306: return c;
307: }
308:
309: cmo_distributed_polynomial* new_cmo_distributed_polynomial()
310: {
1.11 ohara 311: cmo_distributed_polynomial* c = MALLOC(sizeof(cmo_distributed_polynomial));
1.1 ohara 312: c->tag = CMO_DISTRIBUTED_POLYNOMIAL;
313: c->length = 0;
314: c->head->next = c->head;
315: c->head->prev = c->head;
316: c->ringdef = NULL;
1.14 ohara 317: return c;
318: }
319:
320: cmo_polynomial_in_one_variable* new_cmo_polynomial_in_one_variable(int var)
321: {
322: cmo_polynomial_in_one_variable* c = MALLOC(sizeof(cmo_polynomial_in_one_variable));
323: c->tag = CMO_POLYNOMIAL_IN_ONE_VARIABLE;
324: c->length = 0;
325: c->head->next = c->head;
326: c->head->prev = c->head;
327: c->var = var;
328: return c;
329: }
330:
1.15 ohara 331: cmo_recursive_polynomial* new_cmo_recursive_polynomial(cmo_list* ringdef, cmo* coef)
1.14 ohara 332: {
333: cmo_recursive_polynomial* c = MALLOC(sizeof(cmo_recursive_polynomial));
334: c->tag = CMO_RECURSIVE_POLYNOMIAL;
335: c->ringdef = ringdef;
336: c->coef = coef;
1.16 ohara 337: return c;
338: }
339:
340: cmo_tree* new_cmo_tree(cmo_string* name, cmo_list* attributes, cmo_list* leaves)
341: {
342: cmo_tree* c = MALLOC(sizeof(cmo_tree));
343: c->tag = CMO_TREE;
344: c->name= name;
345: c->attributes = attributes;
346: c->leaves = leaves;
347: return c;
348: }
349:
350: cmo_lambda* new_cmo_lambda(cmo_list* args, cmo_tree* body)
351: {
352: cmo_lambda* c = MALLOC(sizeof(cmo_lambda));
353: c->tag = CMO_LAMBDA;
354: c->args = args;
355: c->body = body;
1.1 ohara 356: return c;
357: }
358:
359: cmo_error2* new_cmo_error2(cmo* ob)
360: {
1.11 ohara 361: cmo_error2* c = MALLOC(sizeof(cmo_error2));
1.1 ohara 362: c->tag = CMO_ERROR2;
363: c->ob = ob;
364: return c;
365: }
366:
1.2 ohara 367:
368: /* Following functions translate cmo's to (asciiz) strings. */
1.1 ohara 369: static char *new_string_set_cmo_zz(cmo_zz *c)
370: {
371: return mpz_get_str(NULL, 10, c->mpz);
372: }
373:
374: static char *new_string_set_cmo_null()
375: {
376: static char* null_string = "";
377: return null_string;
378: }
379:
380: static char *new_string_set_cmo_int32(int integer)
381: {
382: char buff[1024];
383: char *s;
384:
385: sprintf(buff, "%d", integer);
1.11 ohara 386: s = MALLOC(strlen(buff)+1);
1.1 ohara 387: strcpy(s, buff);
388:
389: return s;
390: }
391:
392: static char *new_string_set_cmo_list(cmo_list *m)
393: {
394: char *s;
395: int i;
396: int size = 0;
397: int len = list_length(m);
1.11 ohara 398: char **sp = ALLOCA(len*sizeof(cmo *));
1.1 ohara 399:
400: cell* cp = list_first(m);
401: for(i = 0; i < len; i++) {
402: sp[i] = new_string_set_cmo(cp->cmo);
403: size += strlen(sp[i]) + 3;
404: cp = list_next(cp);
405: }
1.11 ohara 406: s = MALLOC(size+2);
1.1 ohara 407: strcpy(s, "[ ");
408: for(i = 0; i < len - 1; i++) {
409: strcat(s, sp[i]);
410: strcat(s, " , ");
411: }
1.17 iwane 412: if (len > 0)
413: strcat(s, sp[len-1]);
1.1 ohara 414: strcat(s, " ]");
415: return s;
416: }
417:
1.7 ohara 418: static char *new_string_set_cmo_double(cmo_double *m)
419: {
420: char buff[1024];
421: char *s;
422:
1.13 ohara 423: sprintf(buff, "%.20f", m->d);
424: s = MALLOC_ATOMIC(strlen(buff)+1);
1.7 ohara 425: strcpy(s, buff);
426:
427: return s;
428: }
429:
1.1 ohara 430: char *new_string_set_cmo(cmo *m)
431: {
432: switch(m->tag) {
433: case CMO_ZZ:
434: return new_string_set_cmo_zz((cmo_zz *)m);
435: case CMO_INT32:
436: return new_string_set_cmo_int32(((cmo_int32 *)m)->i);
437: case CMO_STRING:
438: return ((cmo_string *)m)->s;
439: case CMO_NULL:
440: return new_string_set_cmo_null();
441: case CMO_LIST:
442: return new_string_set_cmo_list((cmo_list *)m);
1.18 ohara 443: case CMO_64BIT_MACHINE_DOUBLE:
444: case CMO_IEEE_DOUBLE_FLOAT:
1.15 ohara 445: return new_string_set_cmo_double((cmo_double *)m);
1.1 ohara 446: default:
1.8 ohara 447: ox_printf("unconvertible <%s>\n", get_symbol_by_tag(m->tag));
1.1 ohara 448: /* yet not implemented. */
449: return NULL;
450: }
451: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>