Annotation of OpenXM/src/ox_ntl/ntlconv.cpp, Revision 1.2
1.2 ! iwane 1: /* $OpenXM: OpenXM/src/ox_ntl/ntlconv.cpp,v 1.1 2003/11/08 12:34:00 iwane Exp $ */
1.1 iwane 2:
3: #include <NTL/ZZX.h>
1.2 ! iwane 4: #include <NTL/mat_ZZ.h>
1.1 iwane 5:
6: #include <strstream>
7:
8: #include "ntl.h"
9:
10: #if __NTL_DEBUG
11: #define __NTL_PRINT (1)
12: #endif
13:
1.2 ! iwane 14: #define BLOCK_NEW_CMO() BLOCK_INPUT()
! 15: #define UNBLOCK_NEW_CMO() UNBLOCK_INPUT()
! 16:
! 17:
! 18:
1.1 iwane 19: /*==========================================================================*
20: * Check string format
21: *==========================================================================*/
22:
23: #define NtlIsSpace(c) ((c) == ' ' || (c) == '\t')
24: #define NtlIsDigit(c) ((c) >= '0' && (c) <= '9')
25:
26: /****************************************************************************
27: *
28: * test for string format of integer
29: *
30: * PARAM : I : str : string
31: * : O : endptr :
32: * RETURN: !0 : the string tests true
33: * : 0 : the string tests false
34: *
35: ****************************************************************************/
36: static int
37: ntl_isZZstr_(const char *str, char const **endptr)
38: {
39: while (NtlIsSpace(*str))
40: str++;
41:
42: /* NTL reject "+999" */
43: if (*str == '-')
44: str++;
45:
46: if (!NtlIsDigit(*str))
47: return (0);
48:
49: str++;
50:
51: while (NtlIsDigit(*str))
52: str++;
53:
54: *endptr = str;
55:
56: return (!0);
57:
58: }
59:
60: static int
61: ntl_isZZstr(const char *str)
62: {
63: const char *ptr;
64: int ret = ntl_isZZstr_(str, &ptr);
65: if (!ret)
66: return (ret);
67:
68: while (NtlIsSpace(*ptr))
69: ptr++;
70:
71: return (*ptr == '\0');
72: }
73:
74:
75: /****************************************************************************
76: *
77: * test for string format of univariate polynomials with integer coefficients
78: * in NTL style.
79: *
80: * PARAM : I : str : string
81: * : O : endptr :
82: * RETURN: !0 : the string tests true
83: * : 0 : the string tests false
84: *
85: ****************************************************************************/
86: static int
87: ntl_isZZXstr_(const char *str, char const **endptr)
88: {
89: const char *s;
90:
91: while (NtlIsSpace(*str))
92: str++;
93:
94: if (*str != '[')
95: return (0);
96:
97: str++;
98:
99: while (*str != ']' && *str != '\0') {
100:
101: if (!ntl_isZZstr_(str, &s))
102: return (0);
103: str = s;
104:
105: while (NtlIsSpace(*str))
106: str++;
107: }
108:
109: while (NtlIsSpace(*str))
110: str++;
111:
112: if (*str != ']')
113: return (0);
114:
115: str++;
116: *endptr = str;
117: return (!0);
118: }
119:
120: static int
121: ntl_isZZXstr(const char *str)
122: {
123: const char *ptr;
124: int ret = ntl_isZZXstr_(str, &ptr);
125: if (!ret)
126: return (ret);
127:
128: while (NtlIsSpace(*ptr))
129: ptr++;
130:
131: return (*ptr == '\0');
132: }
133:
134:
135: /*==========================================================================*
136: * Convert
137: *==========================================================================*/
138: /****************************************************************************
139: * convert ZZ to cmo_zz
140: *
141: * PARAM : I : z : integer
142: * RETURN: cmo_zz
143: ****************************************************************************/
144: cmo_zz *
145: ZZ_to_cmo_zz(const ZZ &z)
146: {
147: cmo_zz *c;
148:
149: ostrstream sout;
150: sout << z << '\0';
151:
1.2 ! iwane 152: BLOCK_NEW_CMO();
1.1 iwane 153: c = new_cmo_zz_set_string(sout.str());
1.2 ! iwane 154: UNBLOCK_NEW_CMO();
1.1 iwane 155:
156: return (c);
157: }
158:
159:
160: /****************************************************************************
161: * convert cmo to ZZ which is integer
162: *
163: * PARAM : O : z : integer.
164: * : I : c : cmo.
165: * RETURN: success : NTL_SUCCESS
166: * : failure : NTL_FAILURE
167: ****************************************************************************/
168: int
169: cmo_to_ZZ(ZZ &z, cmo *c)
170: {
171: int ret = NTL_SUCCESS;
172: char *str;
173:
174: switch (c->tag) {
175: case CMO_ZERO:
1.2 ! iwane 176: case CMO_NULL:
1.1 iwane 177: z = to_ZZ(0);
178: break;
179: case CMO_ZZ:
180: {
181: str = new_string_set_cmo(c);
182: z = to_ZZ(str);
183: break;
184: }
185: case CMO_INT32:
186: z = to_ZZ(((cmo_int32 *)c)->i);
187: break;
188: case CMO_STRING:
189: {
190: str = ((cmo_string *)c)->s;
191: if (!ntl_isZZstr(str))
192: return (NTL_FAILURE);
193: z = to_ZZ(str);
194: break;
195: }
196: default:
197: ret = NTL_FAILURE;
198: break;
199: }
200:
201: return (ret);
202: }
203:
204:
205: /****************************************************************************
206: * convert cmo to ZZX which is polynomial in Z[x]
207: *
208: * PARAM : O : f : polynomial in Z[x]
209: * : I : m : cmo.
210: * : O : x : indeterminate
211: * RETURN: success : NTL_SUCCESS
212: * : failure : NTL_FAILURE
213: ****************************************************************************/
1.2 ! iwane 214: cmo_list *
! 215: mat_zz_to_cmo(mat_ZZ &mat)
! 216: {
! 217: cmo_list *list;
! 218: int ret;
! 219:
! 220: cmo_zz *zz;
! 221: int row, col;
! 222: int i, j;
! 223:
! 224: row = (int)mat.NumRows();
! 225: col = (int)mat.NumCols();
! 226:
! 227: BLOCK_NEW_CMO();
! 228: list = list_appendl(NULL, new_cmo_int32(row), new_cmo_int32(col), NULL);
! 229:
! 230: for (i = 0; i < row; i++) {
! 231: for (j = 0; j < col; j++) {
! 232: zz = ZZ_to_cmo_zz(mat[i][j]);
! 233: list_append(list, (cmo *)zz);
! 234: }
! 235: }
! 236: UNBLOCK_NEW_CMO();
! 237:
! 238: return (list);
! 239: }
! 240:
! 241:
! 242:
! 243:
! 244: /****************************************************************************
! 245: * convert cmo to ZZX which is polynomial in Z[x]
! 246: *
! 247: * PARAM : O : f : polynomial in Z[x]
! 248: * : I : m : cmo.
! 249: * : O : x : indeterminate
! 250: * RETURN: success : NTL_SUCCESS
! 251: * : failure : NTL_FAILURE
! 252: ****************************************************************************/
! 253: int
! 254: cmo_to_mat_zz(mat_ZZ &mat, cmo *m)
! 255: {
! 256: cmo_list *list;
! 257: int ret;
! 258: ZZ row, col, size;
! 259: int len;
! 260: cell *el;
! 261: int i, j;
! 262: int c, r;
! 263:
! 264: if (m->tag != CMO_LIST) {
! 265: return (NTL_FAILURE);
! 266: }
! 267:
! 268: list = (cmo_list *)m;
! 269: len = list_length(list);
! 270:
! 271: if (len < 2) {
! 272: return (NTL_FAILURE);
! 273: }
! 274:
! 275: el = list_first(list);
! 276: ret = cmo_to_ZZ(row, el->cmo);
! 277: if (ret != NTL_SUCCESS) {
! 278: return (ret);
! 279: }
! 280:
! 281: el = list_next(el);
! 282: ret = cmo_to_ZZ(col, el->cmo);
! 283: if (ret != NTL_SUCCESS) {
! 284: return (ret);
! 285: }
! 286:
! 287: mul(size, row, col);
! 288:
! 289: if (len - 2 != size) {
! 290: return (NTL_FAILURE);
! 291: }
! 292:
! 293: /* row and col is less than INT_MAX */
! 294: r = to_int(row);
! 295: c = to_int(col);
! 296: mat.SetDims(r, c);
! 297: for (i = 0; i < r; i++) {
! 298: for (j = 0; j < c; j++) {
! 299: el = list_next(el);
! 300: ret = cmo_to_ZZ(mat[i][j], el->cmo);
! 301: if (ret) {
! 302: return (ret);
! 303: }
! 304: }
! 305: }
! 306: return (NTL_SUCCESS);
! 307: }
! 308:
! 309:
! 310:
! 311:
! 312:
! 313:
! 314:
! 315:
! 316:
! 317: /****************************************************************************
! 318: * convert cmo to ZZX which is polynomial in Z[x]
! 319: *
! 320: * PARAM : O : f : polynomial in Z[x]
! 321: * : I : m : cmo.
! 322: * : O : x : indeterminate
! 323: * RETURN: success : NTL_SUCCESS
! 324: * : failure : NTL_FAILURE
! 325: ****************************************************************************/
1.1 iwane 326: int
327: cmo_to_ZZX(ZZX &f, cmo *m, cmo_indeterminate *&x)
328: {
329: char *str;
330: int ret;
331:
332: switch (m->tag) {
333: case CMO_STRING: /* [ 3 4 7 ] ==> 3+4*x+7*x^2 */
334: str = ((cmo_string *)m)->s;
335: ret = ntl_isZZXstr(str);
336:
337: if (!ret) {
338: /* format error */
339: return (NTL_FAILURE);
340: }
341: {
342: istrstream sin(str, strlen(str));
343: sin >> f;
344: }
345: break;
346: case CMO_RECURSIVE_POLYNOMIAL:
347: {
348: cmo_recursive_polynomial *rec = (cmo_recursive_polynomial *)m;
349: cmo_polynomial_in_one_variable *poly = (cmo_polynomial_in_one_variable *)rec->coef;
350: cell *el;
351: int len;
352:
353: if (poly->tag != CMO_POLYNOMIAL_IN_ONE_VARIABLE) {
354: return (NTL_FAILURE);
355: }
356:
357: el = list_first((cmo_list *)poly);
358: len = list_length((cmo_list *)poly);
359:
360: f = 0;
361:
362: while (!list_endof((cmo_list *)poly, el)) {
363: ZZ c;
364: cmo *coef = el->cmo;
365: int exp = el->exp;
366:
367: ret = cmo_to_ZZ(c, coef);
368: if (ret != NTL_SUCCESS) {
369: return (NTL_FAILURE);
370: }
371:
372: SetCoeff(f, exp, c);
373:
374: el = list_next(el);
375: }
376:
377:
378: el = list_first(rec->ringdef);
379: x = (cmo_indeterminate *)el->cmo;
380:
381: break;
382: }
383: default:
384: break;
385: }
386: return (NTL_SUCCESS);
387: }
388:
389: /****************************************************************************
390: * convert polynomial in Z[x] to cmo_recursive_polynomial
391: *
392: * PARAM : I : factor : polynomial in Z[x]
393: * : I : x : indeterminate
394: * RETURN:
395: ****************************************************************************/
396: cmo_recursive_polynomial *
397: ZZX_to_cmo(ZZX &factor, cmo_indeterminate *x)
398: {
399: cmo_recursive_polynomial *rec;
400: cmo_polynomial_in_one_variable *poly;
401:
402: cmo_list *ringdef;
403: int i;
404: cmo *coef;
405:
1.2 ! iwane 406: BLOCK_NEW_CMO();
! 407:
1.1 iwane 408: ringdef = new_cmo_list();
409: list_append(ringdef, (cmo *)x);
410:
411: poly = new_cmo_polynomial_in_one_variable(0);
412: for (i = deg(factor); i >= 0; i--) {
413: if (coeff(factor, i) == 0)
414: continue;
415:
416: coef = (cmo *)ZZ_to_cmo_zz(coeff(factor, i));
417: list_append_monomial((cmo_list *)poly, coef, i);
418: }
419:
420: rec = new_cmo_recursive_polynomial(ringdef, (cmo *)poly);
1.2 ! iwane 421:
! 422: UNBLOCK_NEW_CMO();
! 423:
1.1 iwane 424: return (rec);
425: }
426:
427:
428: /****************************************************************************
429: * convert pair of factor and multiplicity to cmo_list
430: *
431: * PARAM : I : factor : polynomial in Z[x]
432: * : I : d : multiplicity
433: * : I : x : indeterminate
434: * RETURN:
435: ****************************************************************************/
436: cmo_list *
437: ZZX_int_to_cmo(ZZX &factor, int d, cmo_indeterminate *x)
438: {
439: cmo_recursive_polynomial *poly;
440: cmo_int32 *deg;
441: cmo_list *list;
442:
443: poly = ZZX_to_cmo(factor, x);
1.2 ! iwane 444:
! 445: BLOCK_NEW_CMO();
! 446:
1.1 iwane 447: deg = new_cmo_int32(d);
1.2 ! iwane 448: list = list_appendl(NULL, poly, deg, NULL);
1.1 iwane 449:
1.2 ! iwane 450: UNBLOCK_NEW_CMO();
1.1 iwane 451:
452: return (list);
453: }
454:
455:
456: /****************************************************************************
457: * convert vec_pair_ZZX_long(list which pair of factor and multiplicity)
458: * to cmo_list
459: *
460: * PARAM : I : factors : list which pair of factor and multiplicity
461: * : : : [[factor1,multiplicity1][factor2,multiplicity2]...]
462: * : I : x : indeterminate
463: * RETURN:
464: ****************************************************************************/
465: cmo_list *
466: vec_pair_ZZX_long_to_cmo(vec_pair_ZZX_long &factors, cmo_indeterminate *x)
467: {
468: int i;
1.2 ! iwane 469: cmo_list *list;
1.1 iwane 470: cmo_list *factor;
471:
1.2 ! iwane 472: BLOCK_NEW_CMO();
! 473:
! 474: list = new_cmo_list();
1.1 iwane 475: for (i = 0; i < factors.length(); i++) {
476: factor = ZZX_int_to_cmo(factors[i].a, (int)factors[i].b, x);
477: list_append(list, (cmo *)factor);
478: }
479:
1.2 ! iwane 480: UNBLOCK_NEW_CMO();
! 481:
1.1 iwane 482: return (list);
483: }
1.2 ! iwane 484:
! 485:
! 486: /****************************************************************************
! 487: * convert local object to cmo.
! 488: * for SM_popCMO
! 489: *
! 490: * PARAM : I : p : cmo
! 491: * RETURN: cmo
! 492: ****************************************************************************/
! 493: cmo *
! 494: convert_cmon(cmo *p)
! 495: {
! 496:
! 497: switch (p->tag) {
! 498: case CMON_ZZ:
! 499: {
! 500: cmon_zz_t *z = (cmon_zz_t *)p;
! 501: return ((cmo *)ZZ_to_cmo_zz(*z->z));
! 502: }
! 503: case CMON_ZZX:
! 504: {
! 505: cmon_zzx_t *f = (cmon_zzx_t *)p;
! 506: return ((cmo *)ZZX_to_cmo(*f->f, f->x));
! 507: }
! 508: case CMON_FACTORS:
! 509: {
! 510: cmon_factors_t *f = (cmon_factors_t *)p;
! 511: cmo_zz *z = ZZ_to_cmo_zz(*f->cont);
! 512: cmo_list *list = vec_pair_ZZX_long_to_cmo(*f->f, f->x);
! 513: return ((cmo *)list_appendl(NULL, (cmo *)z, list, NULL));
! 514: }
! 515: case CMON_MAT_ZZ:
! 516: {
! 517: cmon_mat_zz_t *m = (cmon_mat_zz_t *)p;
! 518: cmo_list *list = mat_zz_to_cmo(*m->mat);
! 519: return ((cmo *)list);
! 520: }
! 521: default:
! 522: return (p);
! 523: }
! 524: }
! 525:
! 526:
! 527:
! 528: /****************************************************************************
! 529: * convert tag of local object to tag of cmo.
! 530: * for SM_pushCMOtag
! 531: *
! 532: * PARAM : I : p : cmo
! 533: * RETURN: tag
! 534: ****************************************************************************/
! 535: int
! 536: get_cmon_tag(cmo *p)
! 537: {
! 538: switch (p->tag) {
! 539: case CMON_ZZ:
! 540: return (CMO_ZZ);
! 541: case CMON_ZZX:
! 542: return (CMO_RECURSIVE_POLYNOMIAL);
! 543: case CMON_FACTORS:
! 544: return (CMO_LIST);
! 545: case CMON_MAT_ZZ:
! 546: return (CMON_MAT_ZZ);
! 547: default:
! 548: return (p->tag);
! 549: }
! 550: }
! 551:
! 552:
1.1 iwane 553:
554:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>