Annotation of OpenXM_contrib2/asir2000/builtin/parif.c, Revision 1.32
1.32 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/builtin/parif.c,v 1.31 2015/08/20 08:42:07 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.32 ! noro 113: if ( arg && ARG0(arg) && NID((Num)ARG0(arg)) != N_C
! 114: && (mpfr_function = mpfr_search(f->name)) ) {
! 115: (*mpfr_function)(arg,&ret);
! 116: return (pointer) ret;
1.20 takayama 117: }
1.24 noro 118:
1.19 noro 119: if ( !ox_pari_stream_initialized ) {
1.29 noro 120: MKSTR(name,"ox_pari");
121: nd = mknode(2,NULL,name);
122: Pox_launch_nox(nd,&r);
123: ox_pari_stream = r;
1.19 noro 124: ox_pari_stream_initialized = 1;
125: }
1.25 noro 126:
127: ac = argc(arg);
128: /* reverse the arg list */
129: for ( n = arg, t = 0; n; n = NEXT(n) ) {
130: MKNODE(t1,BDY(n),t); t = t1;
131: }
132: /* push the reversed arg list */
133: for ( ; t; t = NEXT(t) ) {
134: oxarg = mknode(2,ox_pari_stream,BDY(t));
135: Pox_push_cmo(oxarg,&dmy);
136: }
137: MKSTR(name,f->name);
138: STOQ(ac,narg);
139: oxarg = mknode(3,ox_pari_stream,name,narg);
140: Pox_execute_function(oxarg,&dmy);
141: oxarg = mknode(1,ox_pari_stream);
1.29 noro 142: ox_get_pari_result = 1;
1.28 noro 143: Pox_pop_cmo(oxarg,&ret);
1.29 noro 144: ox_get_pari_result = 0;
1.31 noro 145: if ( ret && OID(ret) == O_ERR ) {
146: char buf[BUFSIZ];
147: soutput_init(buf);
148: sprintexpr(CO,((ERR)ret)->body);
149: error(buf);
150: }
1.30 noro 151: if ( ret && OID(ret) == O_LIST ) {
1.28 noro 152: ret = list_to_vect(ret);
1.30 noro 153: ret = vect_to_mat((VECT)ret);
154: }
1.28 noro 155: return ret;
1.1 noro 156: }
157:
158: struct pariftab {
159: char *name;
1.19 noro 160: int dmy;
1.1 noro 161: int type;
162: };
163:
1.8 noro 164: /*
165: * type = 1 => argc = 1, second arg = precision
166: * type = 2 => argc = 1, second arg = optional (long int)
167: *
168: */
1.19 noro 169: /*
170: {"abs",0,1},
171: {"adj",0,1},
172: */
1.8 noro 173:
1.1 noro 174: struct pariftab pariftab[] = {
1.19 noro 175: {"arg",0,1},
176: {"bigomega",0,1},
177: {"binary",0,1},
178: {"ceil",0,1},
179: {"centerlift",0,1},
180: {"cf",0,1},
181: {"classno",0,1},
182: {"classno2",0,1},
183: {"conj",0,1},
184: {"content",0,1},
185: {"denom",0,1},
186: {"det",0,1},
187: {"det2",0,1},
188: {"dilog",0,1},
189: {"disc",0,1},
190: {"discf",0,1},
191: {"divisors",0,1},
192: {"eigen",0,1},
193: {"eintg1",0,1},
194: {"erfc",0,1},
195: {"eta",0,1},
196: {"floor",0,1},
197: {"frac",0,1},
198: {"galois",0,1},
199: {"galoisconj",0,1},
200: {"gamh",0,1},
201: {"gamma",0,1},
202: {"hclassno",0,1},
203: {"hermite",0,1},
204: {"hess",0,1},
205: {"imag",0,1},
206: {"image",0,1},
207: {"image2",0,1},
208: {"indexrank",0,1},
209: {"indsort",0,1},
210: {"initalg",0,1},
211: {"isfund",0,1},
212: {"ispsp",0,1},
213: {"isqrt",0,1},
214: {"issqfree",0,1},
215: {"issquare",0,1},
216: {"jacobi",0,1},
217: {"jell",0,1},
218: {"ker",0,1},
219: {"keri",0,1},
220: {"kerint",0,1},
221: {"kerintg1",0,1},
222: {"length",0,1},
223: {"lexsort",0,1},
224: {"lift",0,1},
225: {"lindep",0,1},
226: {"lll",0,1},
227: {"lllgen",0,1},
228: {"lllgram",0,1},
229: {"lllgramgen",0,1},
230: {"lllgramint",0,1},
231: {"lllgramkerim",0,1},
232: {"lllgramkerimgen",0,1},
233: {"lllint",0,1},
234: {"lllkerim",0,1},
235: {"lllkerimgen",0,1},
236: {"lngamma",0,1},
237: {"logagm",0,1},
238: {"mat",0,1},
239: {"matrixqz2",0,1},
240: {"matrixqz3",0,1},
241: {"matsize",0,1},
242: {"modreverse",0,1},
243: {"mu",0,1},
244: {"nextprime",0,1},
245: {"norm",0,1},
246: {"norml2",0,1},
247: {"numdiv",0,1},
248: {"numer",0,1},
249: {"omega",0,1},
250: {"order",0,1},
251: {"ordred",0,1},
252: {"phi",0,1},
253: {"pnqn",0,1},
254: {"polred",0,1},
255: {"polred2",0,1},
256: {"primroot",0,1},
257: {"psi",0,1},
258: {"quadgen",0,1},
259: {"quadpoly",0,1},
260: {"real",0,1},
261: {"recip",0,1},
262: {"redreal",0,1},
263: {"regula",0,1},
264: {"reorder",0,1},
265: {"reverse",0,1},
266: {"rhoreal",0,1},
267: {"roots",0,1},
268: {"round",0,1},
269: {"sigma",0,1},
270: {"signat",0,1},
271: {"simplify",0,1},
272: {"smalldiscf",0,1},
273: {"smallfact",0,1},
274: {"smallpolred",0,1},
275: {"smallpolred2",0,1},
276: {"smith",0,1},
277: {"smith2",0,1},
278: {"sort",0,1},
279: {"sqr",0,1},
280: {"sqred",0,1},
281: {"sqrt",0,1},
282: {"supplement",0,1},
283: {"trace",0,1},
284: {"trans",0,1},
285: {"trunc",0,1},
286: {"unit",0,1},
287: {"vec",0,1},
288: {"wf",0,1},
289: {"wf2",0,1},
290: {"zeta",0,1},
291: {"factor",0,1},
292:
293: {"allocatemem",0,0},
294:
295: {"isprime",0,2},
296: {"factorint",0,2},
1.1 noro 297: {0,0,0},
298: };
299:
300: void parif_init() {
301: int i;
302:
303: for ( i = 0, parif = 0; pariftab[i].name; i++ )
1.19 noro 304: appendparif(&parif,pariftab[i].name, 0,pariftab[i].type);
1.1 noro 305: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>