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