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