[BACK]Return to convert.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_pari

Annotation of OpenXM/src/ox_pari/convert.c, Revision 1.4

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]);
1.4     ! noro      165:   mpz_import(mpq_denref(c->mpq),lgef(den)-2,1,sizeof(long),0,0,&den[2]);
1.1       noro      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);
1.2       noro      187:   if ( !mpfr_sgn(c->mpfr) ) c = (cmo_bf *)new_cmo_zero();
1.1       noro      188:   return c;
                    189: }
                    190:
                    191:
                    192: cmo_list *GEN_to_cmo_list(GEN z)
                    193: {
                    194:   cmo_list *c;
                    195:   cmo *ob;
                    196:   int i,len;
                    197:
                    198:   c = new_cmo_list();
                    199:   len = lg(z)-1;
                    200:   for ( i = 1; i <= len; i++ ) {
                    201:     ob = GEN_to_cmo((GEN)z[i]);
                    202:     c = list_append(c,ob);
                    203:   }
                    204:   return c;
                    205: }
                    206:
                    207: cmo_complex *GEN_to_cmo_complex(GEN z)
                    208: {
                    209:   cmo_complex *c;
1.2       noro      210:   cmo_bf *re,*im;
1.1       noro      211:
1.2       noro      212:   re = (cmo_bf *)GEN_to_cmo((GEN)z[1]);
                    213:   im = (cmo_bf *)GEN_to_cmo((GEN)z[2]);
                    214:   if ( im->tag == CMO_ZERO )
                    215:     return (cmo_complex *)re;
                    216:   else {
                    217:     c = new_cmo_complex();
                    218:     c->re = (cmo *)re; c->im = (cmo *)im;
                    219:     return c;
                    220:   }
1.1       noro      221: }
                    222:
                    223: cmo_polynomial_in_one_variable *GEN_to_cmo_up(GEN z)
                    224: {
                    225:   cmo_polynomial_in_one_variable *c;
                    226:   int i;
                    227:   cmo *coef;
                    228:
                    229:   c = new_cmo_polynomial_in_one_variable(varn(z));
                    230:   for ( i = lg(z)-1; i >= 2; i-- )
                    231:     if ( (GEN)z[i] != gen_0 ) {
                    232:       coef = GEN_to_cmo((GEN)z[i]);
                    233:       list_append_monomial((cmo_list *)c, coef, i-2);
                    234:     }
                    235:   return c;
                    236: }
                    237:
                    238: cmo_recursive_polynomial *GEN_to_cmo_rp(GEN z)
                    239: {
                    240:   cmo_recursive_polynomial *c;
                    241:
                    242:   if ( !signe(z) ) return (cmo_recursive_polynomial *)new_cmo_zero();
                    243:   c = new_cmo_recursive_polynomial(current_ringdef,(cmo *)GEN_to_cmo_up(z));
                    244:   return c;
                    245: }
                    246:
                    247: GEN cmo_to_GEN(cmo *c)
                    248: {
                    249:   switch ( c->tag ) {
                    250:   case CMO_ZERO:
                    251:   case CMO_NULL:
                    252:     return gen_0;
                    253:   case CMO_ZZ: /* int */
                    254:     return cmo_zz_to_GEN((cmo_zz *)c);
                    255:   case CMO_QQ:
                    256:     return cmo_qq_to_GEN((cmo_qq *)c);
                    257:   case CMO_COMPLEX: /* complex */
                    258:     return cmo_complex_to_GEN((cmo_complex *)c);
                    259:   case CMO_IEEE_DOUBLE_FLOAT:
                    260:      return dbltor(((cmo_double *)c)->d);
1.3       ohara     261:   case CMO_BIGFLOAT32: /* bigfloat */
1.1       noro      262:     return cmo_bf_to_GEN((cmo_bf *)c);
                    263:   case CMO_LIST:
                    264:     return cmo_list_to_GEN((cmo_list *)c);
                    265:   case CMO_RECURSIVE_POLYNOMIAL:
                    266:     return cmo_rp_to_GEN((cmo_recursive_polynomial *)c);
                    267:   case CMO_POLYNOMIAL_IN_ONE_VARIABLE:
                    268:     return cmo_up_to_GEN((cmo_polynomial_in_one_variable *)c);
                    269:   default:
                    270:     return 0;
                    271:   }
                    272: }
                    273:
                    274: cmo *GEN_to_cmo(GEN z)
                    275: {
                    276:   char buf[BUFSIZ];
                    277:
                    278:   if ( gcmp0(z) )
                    279:     return new_cmo_zero();
                    280:   switch ( typ(z) ) {
                    281:   case t_INT: /* int */
                    282:     return (cmo *)GEN_to_cmo_zz(z);
                    283:   case t_REAL: /* bigfloat */
                    284:     return (cmo *)GEN_to_cmo_bf(z);
                    285:   case t_FRAC: /* rational number */
                    286:     return (cmo *)GEN_to_cmo_qq(z);
                    287:   case t_COMPLEX: /* complex */
                    288:     return (cmo *)GEN_to_cmo_complex(z);
                    289:   case t_POL:
                    290:     return (cmo *)GEN_to_cmo_rp(z);
                    291:   case t_VEC: case t_COL: /* vector */
                    292:     return (cmo *)GEN_to_cmo_list(z);
                    293:   case t_MAT: /* matrix */
                    294:     return (cmo *)GEN_to_cmo_list(shallowtrans(z));
                    295:   default:
                    296:     sprintf(buf,"GEN_to_cmo : unsupported type=%d",(int)typ(z));
                    297:     return (cmo *)make_error2(buf);
                    298:   }
                    299: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>