version 1.19, 2015/08/06 09:12:29 |
version 1.40, 2020/10/04 03:14:07 |
|
|
|
/* $OpenXM: OpenXM_contrib2/asir2000/builtin/parif.c,v 1.39 2018/04/09 04:07:27 noro Exp $ */ |
#include "ca.h" |
#include "ca.h" |
#include "parse.h" |
#include "parse.h" |
#include "ox.h" |
#include "ox.h" |
|
|
Q ox_pari_stream; |
Q ox_pari_stream; |
int ox_pari_stream_initialized = 0; |
int ox_pari_stream_initialized = 0; |
|
int ox_get_pari_result = 0; |
|
P ox_pari_starting_function = 0; |
|
|
|
typedef void (*mpfr_func)(NODE,Obj *); |
|
|
|
void Pmpfr_ai(); |
|
void Pmpfr_eint(), Pmpfr_erf(),Pmpfr_li2(); |
|
void Pmpfr_zeta(); |
|
void Pmpfr_j0(), Pmpfr_j1(); |
|
void Pmpfr_y0(), Pmpfr_y1(); |
|
void Pmpfr_gamma(), Pmpfr_lngamma(), Pmpfr_digamma(); |
|
void Pmpfr_floor(), Pmpfr_round(), Pmpfr_ceil(); |
|
|
|
void Pox_shutdown(NODE arg,Q *rp); |
|
void Pox_launch_nox(NODE arg,Obj *rp); |
|
void Pox_launch(NODE arg,Obj *rp); |
|
void Pox_push_cmo(NODE arg,Obj *rp); |
|
void Pox_pop_cmo(NODE arg,Obj *rp); |
|
void Pox_execute_function(NODE arg,Obj *rp); |
|
|
|
struct mpfr_tab_rec { |
|
char *name; |
|
mpfr_func func; |
|
} mpfr_tab[] = { |
|
{"ai",Pmpfr_ai}, |
|
{"zeta",Pmpfr_zeta}, |
|
{"j0",Pmpfr_j0}, |
|
{"j1",Pmpfr_j1}, |
|
{"y0",Pmpfr_y0}, |
|
{"y1",Pmpfr_y1}, |
|
{"eint",Pmpfr_eint}, |
|
{"erf",Pmpfr_erf}, |
|
{"li2",Pmpfr_li2}, |
|
{"gamma",Pmpfr_gamma}, |
|
{"lngamma",Pmpfr_gamma}, |
|
{"digamma",Pmpfr_gamma}, |
|
{"floor",Pmpfr_floor}, |
|
{"ceil",Pmpfr_ceil}, |
|
{"round",Pmpfr_round}, |
|
}; |
|
|
|
mpfr_func mpfr_search(char *name) |
|
{ |
|
int i,n; |
|
|
|
n = sizeof(mpfr_tab)/sizeof(struct mpfr_tab_rec); |
|
for ( i = 0; i < n; i++ ) |
|
if ( !strcmp(name,mpfr_tab[i].name) ) |
|
return mpfr_tab[i].func; |
|
return 0; |
|
} |
|
|
|
Obj list_to_vect(Obj a) |
|
{ |
|
int len,i; |
|
VECT v; |
|
NODE nd; |
|
|
|
if ( !a || OID(a) != O_LIST ) return a; |
|
len = length(BDY((LIST)a)); |
|
MKVECT(v,len); |
|
for ( i = 0, nd = BDY((LIST)a); nd; nd = NEXT(nd), i++ ) |
|
v->body[i] = (pointer)list_to_vect((Obj)BDY(nd)); |
|
return (Obj)v; |
|
} |
|
|
|
Obj vect_to_mat(VECT v) |
|
{ |
|
MAT m; |
|
int len,col,i,j; |
|
|
|
len = v->len; |
|
if ( v->body[0] && OID((Obj)v->body[0]) == O_VECT ) { |
|
col = ((VECT)v->body[0])->len; |
|
for ( i = 1; i < len; i++ ) |
|
if ( !v->body[i] || OID((Obj)v->body[i]) != O_VECT |
|
|| ((VECT)v->body[i])->len != col ) |
|
break; |
|
if ( i == len ) { |
|
/* convert to a matrix */ |
|
MKMAT(m,len,col); |
|
for ( i = 0; i < len; i++ ) |
|
for ( j = 0; j < col; j++ ) |
|
m->body[i][j] = ((VECT)v->body[i])->body[j]; |
|
return (Obj)m; |
|
} |
|
} |
|
return (Obj)v; |
|
} |
|
|
|
void reset_ox_pari() |
|
{ |
|
NODE nd; |
|
Q r; |
|
|
|
if ( ox_get_pari_result ) { |
|
nd = mknode(1,ox_pari_stream); |
|
Pox_shutdown(nd,&r); |
|
ox_get_pari_result = 0; |
|
ox_pari_stream_initialized = 0; |
|
} |
|
} |
|
|
pointer evalparif(FUNC f,NODE arg) |
pointer evalparif(FUNC f,NODE arg) |
{ |
{ |
int ac,intarg,opt,prec; |
int ac,intarg,opt,prec; |
Q q,r,narg; |
Q q,narg,cmd; |
NODE nd,oxarg; |
Real sec; |
|
NODE nd,oxarg,t,t1,n; |
STRING name; |
STRING name; |
USINT ui; |
USINT ui; |
Obj ret,dmy; |
LIST list; |
|
Obj ret,dmy,r; |
|
mpfr_func mpfr_function; |
|
V v; |
|
|
|
if ( arg && ARG0(arg) && NID((Num)ARG0(arg)) != N_C |
|
&& (mpfr_function = mpfr_search(f->name)) ) { |
|
(*mpfr_function)(arg,&ret); |
|
return (pointer) ret; |
|
} |
|
|
if ( !ox_pari_stream_initialized ) { |
if ( !ox_pari_stream_initialized ) { |
MKSTR(name,"ox_pari"); |
if ( ox_pari_starting_function && OID(ox_pari_starting_function) == O_P ) { |
nd = mknode(2,NULL,name); |
v = VR(ox_pari_starting_function); |
Pox_launch(nd,&r); |
if ( (int)v->attr != V_SR ) { |
ox_pari_stream = r; |
error("pari : no handler."); |
|
} |
|
MKNODE(nd,0,0); |
|
r = (Q)bevalf((FUNC)v->priv,0); |
|
}else { |
|
#if !defined(VISUAL) |
|
MKSTR(name,"ox_pari"); |
|
nd = mknode(2,NULL,name); |
|
Pox_launch_nox(nd,&r); |
|
#else |
|
error("Please load names.rr from latest asir-contrib library before using pari functions."); |
|
#endif |
|
} |
|
ox_pari_stream = r; |
ox_pari_stream_initialized = 1; |
ox_pari_stream_initialized = 1; |
} |
} |
switch ( f->type ) { |
|
case 0: /* in/out : integer */ |
|
ac = argc(arg); |
|
if ( ac > 1 ) { |
|
fprintf(stderr,"argument mismatch in %s()\n",NAME(f)); |
|
error(""); |
|
/* NOTREACHED */ |
|
return 0; |
|
} |
|
intarg = ac == 0 ? 0 : QTOS((Q)ARG0(arg)); |
|
MKUSINT(ui,intarg); |
|
oxarg = mknode(2,ox_pari_stream,ui); |
|
Pox_push_cmo(oxarg,&dmy); |
|
MKSTR(name,f->name); |
|
oxarg = mknode(3,ox_pari_stream,name,ONE); |
|
Pox_execute_function(oxarg,&dmy); |
|
oxarg = mknode(1,ox_pari_stream); |
|
Pox_pop_cmo(oxarg,&r); |
|
return r; |
|
|
|
case 1: |
ac = argc(arg); |
ac = argc(arg); |
/* reverse the arg list */ |
if ( !ac || ( ac > 2 ) ) { |
for ( n = arg, t = 0; n; n = NEXT(n) ) { |
fprintf(stderr,"argument mismatch in %s()\n",NAME(f)); |
MKNODE(t1,BDY(n),t); t = t1; |
error(""); |
} |
/* NOTREACHED */ |
/* push the reversed arg list */ |
return 0; |
for ( ; t; t = NEXT(t) ) { |
} |
oxarg = mknode(2,ox_pari_stream,BDY(t)); |
/* arg1 : prec */ |
Pox_push_cmo(oxarg,&dmy); |
prec = ac == 1 ? 0 : QTOS((Q)ARG1(arg)); |
} |
MKUSINT(ui,prec); |
MKSTR(name,f->name); |
oxarg = mknode(2,ox_pari_stream,ui); |
STOQ(ac,narg); |
Pox_push_cmo(oxarg,&dmy); |
oxarg = mknode(3,ox_pari_stream,name,narg); |
|
Pox_execute_function(oxarg,&dmy); |
/* arg0 : arg */ |
ox_get_pari_result = 1; |
oxarg = mknode(2,ox_pari_stream,ARG0(arg)); |
#if defined(VISUAL) || defined(__MINGW32__) |
Pox_push_cmo(oxarg,&dmy); |
#define SM_popCMO 262 |
|
STOQ(SM_popCMO,cmd); |
MKSTR(name,f->name); |
oxarg = mknode(2,ox_pari_stream,cmd); |
STOQ(2,narg); |
Pox_push_cmd(oxarg,&dmy); |
oxarg = mknode(3,ox_pari_stream,name,narg); |
nd = mknode(1,ox_pari_stream); |
Pox_execute_function(oxarg,&dmy); |
MKLIST(list,nd); |
oxarg = mknode(1,ox_pari_stream); |
MKReal(1.0/8,sec); |
Pox_pop_cmo(oxarg,&r); |
oxarg = mknode(2,list,sec); |
return r; |
ret=0; |
|
do { |
case 2: |
check_intr(); |
ac = argc(arg); |
Pox_select(oxarg,&list); |
if ( !ac || ( ac > 2 ) ) { |
oxarg = mknode(1,list); |
fprintf(stderr,"argument mismatch in %s()\n",NAME(f)); |
Plength(oxarg,&ret); |
error(""); |
}while (!ret); |
/* NOTREACHED */ |
oxarg = mknode(1,ox_pari_stream); |
return 0; |
Pox_get(oxarg,&ret); |
} |
#else |
if ( ac == 1 ) |
oxarg = mknode(1,ox_pari_stream); |
opt = 0; |
Pox_pop_cmo(oxarg,&ret); |
else |
#endif |
opt = QTOS((Q)ARG1(arg)); |
ox_get_pari_result = 0; |
return r; |
if ( ret && OID(ret) == O_ERR ) { |
|
char buf[BUFSIZ]; |
default: |
soutput_init(buf); |
error("evalparif : not implemented yet."); |
sprintexpr(CO,((ERR)ret)->body); |
/* NOTREACHED */ |
error(buf); |
return 0; |
} |
} |
if ( ret && OID(ret) == O_LIST ) { |
|
ret = list_to_vect(ret); |
|
ret = vect_to_mat((VECT)ret); |
|
} |
|
return ret; |
} |
} |
|
|
struct pariftab { |
struct pariftab { |
char *name; |
char *name; |
int dmy; |
int dmy; |
int type; |
int type; |
}; |
}; |
|
|
/* |
/* |
* type = 1 => argc = 1, second arg = precision |
* type = 1 => argc = 1, second arg = precision |
* type = 2 => argc = 1, second arg = optional (long int) |
* type = 2 => argc = 1, second arg = (long int)0 |
* |
* |
*/ |
*/ |
/* |
/* |
Line 225 struct pariftab pariftab[] = { |
|
Line 336 struct pariftab pariftab[] = { |
|
|
|
{"allocatemem",0,0}, |
{"allocatemem",0,0}, |
|
|
|
{"factpol",0,2}, |
{"isprime",0,2}, |
{"isprime",0,2}, |
{"factorint",0,2}, |
{"factorint",0,2}, |
{0,0,0}, |
{0,0,0}, |
}; |
}; |
|
|
void parif_init() { |
void parif_init() { |
int i; |
int i; |
|
|
for ( i = 0, parif = 0; pariftab[i].name; i++ ) |
for ( i = 0, parif = 0; pariftab[i].name; i++ ) |
appendparif(&parif,pariftab[i].name, 0,pariftab[i].type); |
appendparif(&parif,pariftab[i].name, 0,pariftab[i].type); |
} |
} |