[BACK]Return to parif.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2018 / builtin

Annotation of OpenXM_contrib2/asir2018/builtin/parif.c, Revision 1.4

1.4     ! noro        1: /* $OpenXM: OpenXM_contrib2/asir2018/builtin/parif.c,v 1.3 2019/05/07 02:09:50 noro Exp $ */
1.1       noro        2: #include "ca.h"
                      3: #include "parse.h"
                      4: #include "ox.h"
                      5:
                      6: Q ox_pari_stream;
                      7: int ox_pari_stream_initialized = 0;
                      8: int ox_get_pari_result = 0;
                      9: P ox_pari_starting_function = 0;
                     10:
                     11: typedef void (*mpfr_func)(NODE,Obj *);
                     12:
                     13: void Pmpfr_ai();
                     14: void Pmpfr_eint(), Pmpfr_erf(),Pmpfr_li2();
                     15: void Pmpfr_zeta();
                     16: void Pmpfr_j0(), Pmpfr_j1();
                     17: void Pmpfr_y0(), Pmpfr_y1();
                     18: void Pmpfr_gamma(), Pmpfr_lngamma(), Pmpfr_digamma();
                     19: void Pmpfr_floor(), Pmpfr_round(), Pmpfr_ceil();
                     20:
                     21: void Pox_shutdown(NODE arg,Q *rp);
                     22: void Pox_launch_nox(NODE arg,Obj *rp);
1.4     ! noro       23: void Pox_launch(NODE arg,Obj *rp);
1.1       noro       24: void Pox_push_cmo(NODE arg,Obj *rp);
                     25: void Pox_pop_cmo(NODE arg,Obj *rp);
                     26: void Pox_execute_function(NODE arg,Obj *rp);
                     27:
1.4     ! noro       28: int debug_pari;
        !            29:
1.1       noro       30: struct mpfr_tab_rec {
                     31:   char *name;
                     32:   mpfr_func func;
                     33: } mpfr_tab[] = {
                     34:   {"ai",Pmpfr_ai},
                     35:   {"zeta",Pmpfr_zeta},
                     36:   {"j0",Pmpfr_j0},
                     37:   {"j1",Pmpfr_j1},
                     38:   {"y0",Pmpfr_y0},
                     39:   {"y1",Pmpfr_y1},
                     40:   {"eint",Pmpfr_eint},
                     41:   {"erf",Pmpfr_erf},
                     42:   {"li2",Pmpfr_li2},
                     43:   {"gamma",Pmpfr_gamma},
                     44:   {"lngamma",Pmpfr_gamma},
                     45:   {"digamma",Pmpfr_gamma},
                     46:   {"floor",Pmpfr_floor},
                     47:   {"ceil",Pmpfr_ceil},
                     48:   {"round",Pmpfr_round},
                     49: };
                     50:
                     51: mpfr_func mpfr_search(char *name)
                     52: {
                     53:   int i,n;
                     54:
                     55:   n = sizeof(mpfr_tab)/sizeof(struct mpfr_tab_rec);
                     56:   for ( i = 0; i < n; i++ )
                     57:     if ( !strcmp(name,mpfr_tab[i].name) )
                     58:       return mpfr_tab[i].func;
                     59:   return 0;
                     60: }
                     61:
                     62: Obj list_to_vect(Obj a)
                     63: {
                     64:   int len,i;
                     65:   VECT v;
                     66:   NODE nd;
                     67:
                     68:   if ( !a || OID(a) != O_LIST ) return a;
                     69:   len = length(BDY((LIST)a));
                     70:   MKVECT(v,len);
                     71:   for ( i = 0, nd = BDY((LIST)a); nd; nd = NEXT(nd), i++ )
                     72:      v->body[i] = (pointer)list_to_vect((Obj)BDY(nd));
                     73:   return (Obj)v;
                     74: }
                     75:
                     76: Obj vect_to_mat(VECT v)
                     77: {
                     78:   MAT m;
                     79:   int len,col,i,j;
                     80:
                     81:   len = v->len;
                     82:   if ( v->body[0] && OID((Obj)v->body[0]) == O_VECT ) {
                     83:     col = ((VECT)v->body[0])->len;
                     84:   for ( i = 1; i < len; i++ )
                     85:     if ( !v->body[i] || OID((Obj)v->body[i]) != O_VECT
                     86:      || ((VECT)v->body[i])->len != col )
                     87:     break;
                     88:     if ( i == len ) {
                     89:     /* convert to a matrix */
                     90:     MKMAT(m,len,col);
                     91:     for ( i = 0; i < len; i++ )
                     92:       for ( j = 0; j < col; j++ )
                     93:       m->body[i][j] = ((VECT)v->body[i])->body[j];
                     94:     return (Obj)m;
                     95:   }
                     96:   }
                     97:   return (Obj)v;
                     98: }
                     99:
                    100: void reset_ox_pari()
                    101: {
                    102:   NODE nd;
                    103:   Q r;
                    104:
                    105:   if ( ox_get_pari_result ) {
                    106:   nd = mknode(1,ox_pari_stream);
                    107:   Pox_shutdown(nd,&r);
                    108:     ox_get_pari_result = 0;
                    109:   ox_pari_stream_initialized = 0;
                    110:   }
                    111: }
                    112:
                    113: pointer evalparif(FUNC f,NODE arg)
                    114: {
                    115:   int ac,intarg,opt,prec;
                    116:   Q q,r,cmd;
                    117:   Z narg;
                    118:   Real sec;
                    119:   NODE nd,oxarg,t,t1,n;
                    120:   STRING name;
                    121:   USINT ui;
                    122:   LIST list;
                    123:   Obj ret,dmy;
                    124:   mpfr_func mpfr_function;
                    125:   V v;
                    126:
1.3       noro      127:   if ( arg && (!ARG0(arg) || (NUM(ARG0(arg)) && NID((Num)ARG0(arg)) != N_C))
1.1       noro      128:     && (mpfr_function = mpfr_search(f->name)) ) {
                    129:     (*mpfr_function)(arg,&ret);
                    130:     return (pointer) ret;
                    131:   }
                    132:
                    133:   if ( !ox_pari_stream_initialized ) {
                    134:   if ( ox_pari_starting_function && OID(ox_pari_starting_function) == O_P ) {
                    135:     v = VR(ox_pari_starting_function);
                    136:     if ( (long)v->attr != V_SR ) {
                    137:       error("pari : no handler.");
                    138:     }
                    139:     MKNODE(nd,0,0);
                    140:     r = (Q)bevalf((FUNC)v->priv,0);
                    141:   }else {
                    142: #if !defined(VISUAL)
                    143:   MKSTR(name,"ox_pari");
                    144:   nd = mknode(2,NULL,name);
1.4     ! noro      145:   if ( debug_pari )
        !           146:     Pox_launch(nd,(Obj *)&r);
        !           147:   else
        !           148:     Pox_launch_nox(nd,(Obj *)&r);
1.1       noro      149: #else
                    150:   error("Please load names.rr from latest asir-contrib library before using pari functions.");
                    151: #endif
                    152:   }
                    153:   ox_pari_stream = r;
                    154:     ox_pari_stream_initialized = 1;
                    155:   }
                    156:
                    157:   ac = argc(arg);
                    158:   /* reverse the arg list */
                    159:   for ( n = arg, t = 0; n; n = NEXT(n) ) {
                    160:     MKNODE(t1,BDY(n),t); t = t1;
                    161:   }
                    162:   /* push the reversed arg list */
                    163:   for ( ; t; t = NEXT(t) ) {
                    164:     oxarg = mknode(2,ox_pari_stream,BDY(t));
                    165:     Pox_push_cmo(oxarg,&dmy);
                    166:   }
                    167:   MKSTR(name,f->name);
1.2       noro      168:   STOZ(ac,narg);
1.1       noro      169:   oxarg = mknode(3,ox_pari_stream,name,narg);
                    170:   Pox_execute_function(oxarg,&dmy);
                    171:   ox_get_pari_result = 1;
                    172: #if defined(VISUAL) || defined(__MINGW32__)
                    173: #define SM_popCMO 262
1.2       noro      174:   STOZ(SM_popCMO,cmd);
1.1       noro      175:   oxarg = mknode(2,ox_pari_stream,cmd);
                    176:   Pox_push_cmd(oxarg,&dmy);
                    177:   nd = mknode(1,ox_pari_stream);
                    178:   MKLIST(list,nd);
                    179:   MKReal(1.0/8,sec);
                    180:   oxarg = mknode(2,list,sec);
                    181:   ret=0;
                    182:   do {
                    183:     check_intr();
                    184:     Pox_select(oxarg,&list);
                    185:     oxarg = mknode(1,list);
                    186:     Plength(oxarg,&ret);
                    187:   }while (!ret);
                    188:   oxarg = mknode(1,ox_pari_stream);
                    189:   Pox_get(oxarg,&ret);
                    190: #else
                    191:   oxarg = mknode(1,ox_pari_stream);
                    192:   Pox_pop_cmo(oxarg,&ret);
                    193: #endif
                    194:   ox_get_pari_result = 0;
                    195:   if ( ret && OID(ret) == O_ERR ) {
                    196:     char buf[BUFSIZ];
                    197:     soutput_init(buf);
                    198:     sprintexpr(CO,((ERR)ret)->body);
                    199:     error(buf);
                    200:   }
                    201:   if ( ret && OID(ret) == O_LIST ) {
                    202:     ret = list_to_vect(ret);
                    203:   ret = vect_to_mat((VECT)ret);
                    204:   }
                    205:   return ret;
                    206: }
                    207:
                    208: struct pariftab {
                    209:   char *name;
                    210:   int dmy;
                    211:   int type;
                    212: };
                    213:
                    214: /*
                    215:  * type = 1 => argc = 1, second arg = precision
                    216:  * type = 2 => argc = 1, second arg = (long int)0
                    217:  *
                    218:  */
                    219: /*
                    220: {"abs",0,1},
                    221: {"adj",0,1},
                    222: */
                    223:
                    224: struct pariftab pariftab[] = {
                    225: {"arg",0,1},
                    226: {"bigomega",0,1},
                    227: {"binary",0,1},
                    228: {"ceil",0,1},
                    229: {"centerlift",0,1},
                    230: {"cf",0,1},
                    231: {"classno",0,1},
                    232: {"classno2",0,1},
                    233: {"conj",0,1},
                    234: {"content",0,1},
                    235: {"denom",0,1},
                    236: {"det",0,1},
                    237: {"det2",0,1},
                    238: {"dilog",0,1},
                    239: {"disc",0,1},
                    240: {"discf",0,1},
                    241: {"divisors",0,1},
                    242: {"eigen",0,1},
                    243: {"eintg1",0,1},
                    244: {"erfc",0,1},
                    245: {"eta",0,1},
                    246: {"floor",0,1},
                    247: {"frac",0,1},
                    248: {"galois",0,1},
                    249: {"galoisconj",0,1},
                    250: {"gamh",0,1},
                    251: {"gamma",0,1},
                    252: {"hclassno",0,1},
                    253: {"hermite",0,1},
                    254: {"hess",0,1},
                    255: {"imag",0,1},
                    256: {"image",0,1},
                    257: {"image2",0,1},
                    258: {"indexrank",0,1},
                    259: {"indsort",0,1},
                    260: {"initalg",0,1},
                    261: {"isfund",0,1},
                    262: {"ispsp",0,1},
                    263: {"isqrt",0,1},
                    264: {"issqfree",0,1},
                    265: {"issquare",0,1},
                    266: {"jacobi",0,1},
                    267: {"jell",0,1},
                    268: {"ker",0,1},
                    269: {"keri",0,1},
                    270: {"kerint",0,1},
                    271: {"kerintg1",0,1},
                    272: {"length",0,1},
                    273: {"lexsort",0,1},
                    274: {"lift",0,1},
                    275: {"lindep",0,1},
                    276: {"lll",0,1},
                    277: {"lllgen",0,1},
                    278: {"lllgram",0,1},
                    279: {"lllgramgen",0,1},
                    280: {"lllgramint",0,1},
                    281: {"lllgramkerim",0,1},
                    282: {"lllgramkerimgen",0,1},
                    283: {"lllint",0,1},
                    284: {"lllkerim",0,1},
                    285: {"lllkerimgen",0,1},
                    286: {"lngamma",0,1},
                    287: {"logagm",0,1},
                    288: {"mat",0,1},
                    289: {"matrixqz2",0,1},
                    290: {"matrixqz3",0,1},
                    291: {"matsize",0,1},
                    292: {"modreverse",0,1},
                    293: {"mu",0,1},
                    294: {"nextprime",0,1},
                    295: {"norm",0,1},
                    296: {"norml2",0,1},
                    297: {"numdiv",0,1},
                    298: {"numer",0,1},
                    299: {"omega",0,1},
                    300: {"order",0,1},
                    301: {"ordred",0,1},
                    302: {"phi",0,1},
                    303: {"pnqn",0,1},
                    304: {"polred",0,1},
                    305: {"polred2",0,1},
                    306: {"primroot",0,1},
                    307: {"psi",0,1},
                    308: {"quadgen",0,1},
                    309: {"quadpoly",0,1},
                    310: {"real",0,1},
                    311: {"recip",0,1},
                    312: {"redreal",0,1},
                    313: {"regula",0,1},
                    314: {"reorder",0,1},
                    315: {"reverse",0,1},
                    316: {"rhoreal",0,1},
                    317: {"roots",0,1},
                    318: {"round",0,1},
                    319: {"sigma",0,1},
                    320: {"signat",0,1},
                    321: {"simplify",0,1},
                    322: {"smalldiscf",0,1},
                    323: {"smallfact",0,1},
                    324: {"smallpolred",0,1},
                    325: {"smallpolred2",0,1},
                    326: {"smith",0,1},
                    327: {"smith2",0,1},
                    328: {"sort",0,1},
                    329: {"sqr",0,1},
                    330: {"sqred",0,1},
                    331: {"sqrt",0,1},
                    332: {"supplement",0,1},
                    333: {"trace",0,1},
                    334: {"trans",0,1},
                    335: {"trunc",0,1},
                    336: {"unit",0,1},
                    337: {"vec",0,1},
                    338: {"wf",0,1},
                    339: {"wf2",0,1},
                    340: {"zeta",0,1},
                    341: {"factor",0,1},
                    342:
                    343: {"allocatemem",0,0},
                    344:
                    345: {"factpol",0,2},
                    346: {"isprime",0,2},
                    347: {"factorint",0,2},
                    348: {0,0,0},
                    349: };
                    350:
                    351: void parif_init() {
                    352:   int i;
                    353:
                    354:   for ( i = 0, parif = 0; pariftab[i].name; i++ )
                    355:      appendparif(&parif,pariftab[i].name, 0,pariftab[i].type);
                    356: }

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