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