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