Annotation of OpenXM/src/ox_pari/convert.c, Revision 1.6
1.1 noro 1: #include "ox_pari.h"
2:
3: GEN cmo_int32_to_GEN(cmo_int32 *c)
4: {
5: GEN z;
6: int i,sgn;
7:
8: i = c->i;
9: if ( !i ) return gen_0;
10: z = cgeti(3);
11: sgn = 1;
12: if ( i < 0 ) {
13: i = -i;
14: sgn = -1;
15: }
16: z[2] = i;
17: setsigne(z,sgn);
18: setlgefint(z,lg(z));
19: return z;
20: }
21:
1.5 noro 22: GEN cmo_string_to_GEN(cmo_string *c)
23: {
24: GEN z;
25: int l;
26:
27: l = strlen(c->s);
28: z = cgetg(l+1,t_STR);
29: strcpy(GSTR(z),c->s);
30: return z;
31: }
32:
1.1 noro 33: GEN cmo_zz_to_GEN(cmo_zz *c)
34: {
35: mpz_ptr mpz;
36: GEN z;
37: long *ptr;
38: int j,sgn,len;
39:
40: mpz = c->mpz;
41: sgn = mpz_sgn(mpz);
42: len = ABSIZ(mpz);
43: ptr = (long *)PTR(mpz);
44: z = cgeti(len+2);
45: for ( j = 0; j < len; j++ )
46: z[len-j+1] = ptr[j];
47: setsigne(z,sgn);
48: setlgefint(z,lg(z));
49: return z;
50: }
51:
52: GEN cmo_qq_to_GEN(cmo_qq *c)
53: {
54: GEN z,nm,den;
55:
56: z = cgetg(3,t_FRAC);
57: nm = cmo_zz_to_GEN(new_cmo_zz_set_mpz(mpq_numref(c->mpq)));
58: den = cmo_zz_to_GEN(new_cmo_zz_set_mpz(mpq_denref(c->mpq)));
59: z[1] = (long)nm;
60: z[2] = (long)den;
61: return z;
62: }
63:
64: GEN cmo_bf_to_GEN(cmo_bf *c)
65: {
66: mpfr_ptr mpfr;
67: GEN z;
68: int sgn,len,j;
69: long exp;
70: long *ptr;
71:
72: mpfr = c->mpfr;
73: sgn = MPFR_SIGN(mpfr);
74: exp = MPFR_EXP(mpfr)-1;
75: len = MPFR_LIMB_SIZE(mpfr);
76: ptr = (long *)MPFR_MANT(mpfr);
77: z = cgetr(len+2);
78: for ( j = 0; j < len; j++ )
79: z[len-j+1] = ptr[j];
80: z[1] = evalsigne(sgn)|evalexpo(exp);
81: setsigne(z,sgn);
82: return z;
83: }
84:
85: /* list->vector */
86:
87: GEN cmo_list_to_GEN(cmo_list *c)
88: {
89: GEN z;
90: int i;
91: cell *cell;
92:
93: z = cgetg(c->length+1,t_VEC);
94: for ( i = 0, cell = c->head->next; cell != c->head; cell = cell->next, i++ ) {
95: z[i+1] = (long)cmo_to_GEN(cell->cmo);
96: }
97: return z;
98: }
99:
100: GEN cmo_complex_to_GEN(cmo_complex *c)
101: {
102: GEN z;
103:
104: z = cgetg(3,t_COMPLEX);
105: z[1] = (long)cmo_to_GEN(c->re);
106: z[2] = (long)cmo_to_GEN(c->im);
107: return z;
108: }
109:
110: GEN cmo_up_to_GEN(cmo_polynomial_in_one_variable *c)
111: {
112: GEN z;
113: int d,i;
114: cell *cell;
115:
116: d = c->head->next->exp;
117: z = cgetg(d+3,t_POL);
118: setsigne(z,1);
119: setvarn(z,c->var);
1.6 ! noro 120: setlg(z,d+3);
1.1 noro 121: for ( i = 2; i <= d+2; i++ )
122: z[i] = (long)gen_0;
123: for ( cell = c->head->next; cell != c->head; cell = cell->next ) {
124: z[2+cell->exp] = (long)cmo_to_GEN(cell->cmo);
125: }
126: return z;
127: }
128:
129: cmo_list *current_ringdef;
130:
131: void register_variables(cmo_list *ringdef)
132: {
133: current_ringdef = ringdef;
134: }
135:
136: GEN cmo_rp_to_GEN(cmo_recursive_polynomial *c)
137: {
138: register_variables(c->ringdef);
139: switch ( c->coef->tag ) {
140: case CMO_ZERO:
141: case CMO_NULL:
142: return gen_0;
143: case CMO_INT32:
144: return cmo_int32_to_GEN((cmo_int32 *)c->coef);
145: case CMO_ZZ:
146: return cmo_zz_to_GEN((cmo_zz *)c->coef);
147: case CMO_QQ:
148: return cmo_qq_to_GEN((cmo_qq *)c->coef);
149: case CMO_POLYNOMIAL_IN_ONE_VARIABLE:
150: return cmo_up_to_GEN((cmo_polynomial_in_one_variable *)c->coef);
151: default:
152: return 0;
153: }
154: }
155:
1.5 noro 156: cmo_zz *GEN_to_cmo_string(GEN z)
157: {
158: cmo_string *c;
159:
160: c = new_cmo_string(GSTR(z));
1.6 ! noro 161: return (cmo_zz *)c;
1.5 noro 162: }
163:
1.1 noro 164: cmo_zz *GEN_to_cmo_zz(GEN z)
165: {
166: cmo_zz *c;
167:
168: c = new_cmo_zz();
1.6 ! noro 169: mpz_import(c->mpz,lg(z)-2,1,sizeof(long),0,0,&z[2]);
1.1 noro 170: if ( signe(z) < 0 )
171: mpz_neg(c->mpz,c->mpz);
172: return c;
173: }
174:
175: cmo_qq *GEN_to_cmo_qq(GEN z)
176: {
177: cmo_qq *c;
178: GEN num,den;
179:
180: num = (GEN)z[1];
181: den = (GEN)z[2];
182: c = new_cmo_qq();
1.6 ! noro 183: mpz_import(mpq_numref(c->mpq),lg(num)-2,1,sizeof(long),0,0,&num[2]);
! 184: mpz_import(mpq_denref(c->mpq),lg(den)-2,1,sizeof(long),0,0,&den[2]);
1.1 noro 185: if ( signe(num)*signe(den) < 0 )
186: mpz_neg(mpq_numref(c->mpq),mpq_numref(c->mpq));
187: return c;
188: }
189:
190:
191: cmo_bf *GEN_to_cmo_bf(GEN z)
192: {
193: cmo_bf *c;
194: int len,prec,j;
195: long *ptr;
196:
197: c = new_cmo_bf();
198: len = lg(z)-2;
199: prec = len*sizeof(long)*8;
200: mpfr_init2(c->mpfr,prec);
201: ptr = (long *)MPFR_MANT(c->mpfr);
202: for ( j = 0; j < len; j++ )
203: ptr[j] = z[len-j+1];
204: MPFR_EXP(c->mpfr) = (long long)(expo(z)+1);
205: MPFR_SIGN(c->mpfr) = gsigne(z);
1.2 noro 206: if ( !mpfr_sgn(c->mpfr) ) c = (cmo_bf *)new_cmo_zero();
1.1 noro 207: return c;
208: }
209:
210:
211: cmo_list *GEN_to_cmo_list(GEN z)
212: {
213: cmo_list *c;
214: cmo *ob;
215: int i,len;
216:
217: c = new_cmo_list();
218: len = lg(z)-1;
219: for ( i = 1; i <= len; i++ ) {
220: ob = GEN_to_cmo((GEN)z[i]);
221: c = list_append(c,ob);
222: }
223: return c;
224: }
225:
226: cmo_complex *GEN_to_cmo_complex(GEN z)
227: {
228: cmo_complex *c;
1.2 noro 229: cmo_bf *re,*im;
1.1 noro 230:
1.2 noro 231: re = (cmo_bf *)GEN_to_cmo((GEN)z[1]);
232: im = (cmo_bf *)GEN_to_cmo((GEN)z[2]);
233: if ( im->tag == CMO_ZERO )
234: return (cmo_complex *)re;
235: else {
236: c = new_cmo_complex();
237: c->re = (cmo *)re; c->im = (cmo *)im;
238: return c;
239: }
1.1 noro 240: }
241:
242: cmo_polynomial_in_one_variable *GEN_to_cmo_up(GEN z)
243: {
244: cmo_polynomial_in_one_variable *c;
245: int i;
246: cmo *coef;
247:
248: c = new_cmo_polynomial_in_one_variable(varn(z));
249: for ( i = lg(z)-1; i >= 2; i-- )
250: if ( (GEN)z[i] != gen_0 ) {
251: coef = GEN_to_cmo((GEN)z[i]);
252: list_append_monomial((cmo_list *)c, coef, i-2);
253: }
254: return c;
255: }
256:
257: cmo_recursive_polynomial *GEN_to_cmo_rp(GEN z)
258: {
259: cmo_recursive_polynomial *c;
260:
261: if ( !signe(z) ) return (cmo_recursive_polynomial *)new_cmo_zero();
262: c = new_cmo_recursive_polynomial(current_ringdef,(cmo *)GEN_to_cmo_up(z));
263: return c;
264: }
265:
266: GEN cmo_to_GEN(cmo *c)
267: {
268: switch ( c->tag ) {
269: case CMO_ZERO:
270: case CMO_NULL:
271: return gen_0;
272: case CMO_ZZ: /* int */
273: return cmo_zz_to_GEN((cmo_zz *)c);
274: case CMO_QQ:
275: return cmo_qq_to_GEN((cmo_qq *)c);
276: case CMO_COMPLEX: /* complex */
277: return cmo_complex_to_GEN((cmo_complex *)c);
278: case CMO_IEEE_DOUBLE_FLOAT:
279: return dbltor(((cmo_double *)c)->d);
1.3 ohara 280: case CMO_BIGFLOAT32: /* bigfloat */
1.1 noro 281: return cmo_bf_to_GEN((cmo_bf *)c);
282: case CMO_LIST:
283: return cmo_list_to_GEN((cmo_list *)c);
284: case CMO_RECURSIVE_POLYNOMIAL:
285: return cmo_rp_to_GEN((cmo_recursive_polynomial *)c);
286: case CMO_POLYNOMIAL_IN_ONE_VARIABLE:
287: return cmo_up_to_GEN((cmo_polynomial_in_one_variable *)c);
1.5 noro 288: case CMO_STRING:
289: return cmo_string_to_GEN((cmo_string *)c);
1.1 noro 290: default:
291: return 0;
292: }
293: }
294:
295: cmo *GEN_to_cmo(GEN z)
296: {
297: char buf[BUFSIZ];
298:
299: if ( gcmp0(z) )
300: return new_cmo_zero();
301: switch ( typ(z) ) {
302: case t_INT: /* int */
303: return (cmo *)GEN_to_cmo_zz(z);
304: case t_REAL: /* bigfloat */
305: return (cmo *)GEN_to_cmo_bf(z);
306: case t_FRAC: /* rational number */
307: return (cmo *)GEN_to_cmo_qq(z);
308: case t_COMPLEX: /* complex */
309: return (cmo *)GEN_to_cmo_complex(z);
310: case t_POL:
311: return (cmo *)GEN_to_cmo_rp(z);
312: case t_VEC: case t_COL: /* vector */
313: return (cmo *)GEN_to_cmo_list(z);
314: case t_MAT: /* matrix */
315: return (cmo *)GEN_to_cmo_list(shallowtrans(z));
1.5 noro 316: case t_STR: /* string */
317: return (cmo *)GEN_to_cmo_string(z);
1.1 noro 318: default:
319: sprintf(buf,"GEN_to_cmo : unsupported type=%d",(int)typ(z));
320: return (cmo *)make_error2(buf);
321: }
322: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>