Annotation of OpenXM_contrib2/asir2000/engine/pari.c, Revision 1.10
1.2 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.10 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/engine/pari.c,v 1.9 2009/03/27 14:42:29 ohara Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
1.9 ohara 51:
1.6 ohara 52: #if defined(PARI)
1.1 noro 53: #include "base.h"
54: #include <math.h>
55: #include "genpari.h"
56:
57: void patori(GEN,Obj *);
58: void patori_i(GEN,N *);
59: void ritopa(Obj,GEN *);
60: void ritopa_i(N,int,GEN *);
61:
1.9 ohara 62: // PARI_VERSION(2,2,12) == 131596
63: #if PARI_VERSION_CODE >= 131596
64: #define prec precreal
65: #endif
1.1 noro 66: extern long prec;
67: extern int paristack;
68:
1.9 ohara 69: long get_pariprec() {
70: return prec;
71: }
72: void set_pariprec(long p) {
73: prec = p;
74: }
75:
1.1 noro 76: void risa_pari_init() {
77: pari_init(paristack,2);
1.9 ohara 78: set_pariprec(4);
1.1 noro 79: }
80:
81: void create_pari_variable(index)
82: int index;
83: {
84: static int max_varn;
85: int i;
86: char name[BUFSIZ];
87:
88: if ( index > max_varn ) {
89: for ( i = max_varn+1; i <= index; i++ ) {
90: sprintf(name,"x%d",i);
1.7 saito 91: #if (PARI_VERSION_CODE < 131594)
1.1 noro 92: fetch_named_var(name,0);
1.7 saito 93: #else
94: fetch_named_var(name);
95: #endif
1.1 noro 96: }
97: max_varn = index;
98: }
99: }
100:
101: int get_lg(a)
102: GEN a;
103: {
104: return lg(a);
105: }
106:
1.9 ohara 107: void gpui_ri(Obj a, Obj e, Obj *c)
108: {
109: GEN pa,pe,z;
1.10 ! noro 110: long ltop,lbot;
1.9 ohara 111:
112: ltop = avma; ritopa(a,&pa); ritopa(e,&pe); lbot = avma;
113: z = gerepile(ltop,lbot,gpui(pa,pe,get_pariprec()));
114: patori(z,c); cgiv(z);
115: }
116:
1.1 noro 117: void ritopa(a,rp)
118: Obj a;
119: GEN *rp;
120: {
121: long ltop;
122: GEN pnm,z,w;
123: DCP dc;
124: int i,j,l,row,col;
125: VL vl;
126: V v;
127:
128: if ( !a ) {
129: *rp = gzero; return;
130: }
131: switch ( OID(a) ) {
132: case O_N:
133: switch ( NID(a) ) {
134: case N_Q:
135: ltop = avma; ritopa_i(NM((Q)a),SGN((Q)a),&pnm);
136: if ( INT((Q)a) )
137: *rp = pnm;
138: else {
139: *rp = z = cgetg(3,4); z[1] = (long)pnm;
140: ritopa_i(DN((Q)a),1,(GEN *)&z[2]);
141: }
142: break;
143: case N_R:
144: *rp = dbltor(BDY((Real)a)); break;
145: case N_B:
146: *rp = gcopy((GEN)BDY(((BF)a))); break;
147: case N_C:
148: z = cgetg(3,6);
149: ritopa((Obj)((C)a)->r,(GEN *)&z[1]); ritopa((Obj)((C)a)->i,(GEN *)&z[2]);
150: *rp = z;
151: break;
152: default:
153: error("ritopa : not implemented"); break;
154: }
155: break;
156: case O_P:
157: l = UDEG((P)a); *rp = z = cgetg(l+3,10);
158: setsigne(z,1);
159: for ( i = 0, vl = CO, v = VR((P)a); vl->v != v;
160: vl = NEXT(vl), i++ );
161: create_pari_variable(i);
162: setvarn(z,i);
163: setlgef(z,l+3);
164: for ( i = l+2; i >= 2; i-- )
165: z[i] = (long)gzero;
166: for ( dc = DC((P)a); dc; dc = NEXT(dc) )
167: ritopa((Obj)COEF(dc),(GEN *)&z[QTOS(DEG(dc))+2]);
168: break;
169: case O_VECT:
170: l = ((VECT)a)->len; z = cgetg(l+1,17);
171: for ( i = 0; i < l; i++ )
172: ritopa((Obj)BDY((VECT)a)[i],(GEN *)&z[i+1]);
173: *rp = z;
174: break;
175: case O_MAT:
176: row = ((MAT)a)->row; col = ((MAT)a)->col; z = cgetg(col+1,19);
177: for ( j = 0; j < col; j++ ) {
178: w = cgetg(row+1,18);
179: for ( i = 0; i < row; i++ )
180: ritopa((Obj)BDY((MAT)a)[i][j],(GEN *)&w[i+1]);
181: z[j+1] = (long)w;
182: }
183: *rp = z;
184: break;
185: default:
186: error("ritopa : not implemented"); break;
187: }
188: }
189:
190: void patori(a,rp)
191: GEN a;
192: Obj *rp;
193: {
194: Q q,qnm,qdn;
195: BF r;
196: C c;
197: VECT v;
198: MAT m;
199: N n,nm,dn;
200: DCP dc0,dc;
201: P t;
202: int s,i,j,l,row,col;
203: GEN b;
204: VL vl;
205:
206: if ( gcmp0(a) )
207: *rp = 0;
208: else {
209: switch ( typ(a) ) {
210: case 1: /* integer */
211: patori_i(a,&n); NTOQ(n,(char)signe(a),q); *rp = (Obj)q;
212: break;
213: case 2: /* real */
214: NEWBF(r,lg(a)); bcopy((char *)a,(char *)BDY(r),lg(a)*sizeof(long));
215: *rp = (Obj)r;
216: break;
217: case 4: /* rational; reduced */
218: patori_i((GEN)a[1],&nm); patori_i((GEN)a[2],&dn);
219: s = signe(a[1])*signe(a[2]);
220: if ( UNIN(dn) )
221: NTOQ(nm,s,q);
222: else
223: NDTOQ(nm,dn,s,q);
224: *rp = (Obj)q;
225: break;
226: case 5: /* rational; not necessarily reduced */
227: patori_i((GEN)a[1],&nm); patori_i((GEN)a[2],&dn);
228: s = signe(a[1])*signe(a[2]);
229: NTOQ(nm,s,qnm); NTOQ(dn,1,qdn); divq(qnm,qdn,(Q *)rp);
230: break;
231: case 6: /* complex */
232: if ( gcmp0((GEN)a[2]) )
233: patori((GEN)a[1],rp);
234: else {
235: NEWC(c); patori((GEN)a[1],(Obj *)&c->r); patori((GEN)a[2],(Obj *)&c->i);
236: *rp = (Obj)c;
237: }
238: break;
239: case 10: /* polynomial */
240: for ( i = lgef(a)-1, dc0 = 0; i >= 2; i-- )
241: if ( !gcmp0((GEN)a[i]) ) {
242: NEXTDC(dc0,dc);
243: patori((GEN)a[i],(Obj *)&COEF(dc)); STOQ(i-2,DEG(dc));
244: }
245: if ( !dc0 )
246: *rp = 0;
247: else {
248: /* assuming that CO has not changed. */
249: for ( s = varn(a), i = 0, vl = CO; i != s;
250: i++, vl = NEXT(vl) );
251: NEXT(dc) = 0; MKP(vl->v,dc0,t); *rp = (Obj)t;
252: }
253: break;
254: case 17: /* row vector */
255: case 18: /* column vector */
256: l = lg(a)-1; MKVECT(v,l);
257: for ( i = 0; i < l; i++ )
258: patori((GEN)a[i+1],(Obj *)&BDY(v)[i]);
259: *rp = (Obj)v;
260: break;
261: case 19: /* matrix */
262: col = lg(a)-1; row = lg(a[1])-1; MKMAT(m,row,col);
263: for ( j = 0; j < col; j++ )
264: for ( i = 0, b = (GEN)a[j+1]; i < row; i++ )
265: patori((GEN)b[i+1],(Obj *)&BDY(m)[i][j]);
266: *rp = (Obj)m;
267: break;
268: default:
269: error("patori : not implemented");
270: break;
271: }
272: }
273: }
274:
1.8 ohara 275: #if SIZEOF_LONG == 4
1.1 noro 276: void ritopa_i(a,s,rp)
277: N a;
278: int s;
279: GEN *rp;
280: {
281: int j,l;
282: unsigned int *b;
283: GEN z;
284:
285: l = PL(a); b = (unsigned int *)BD(a);
286: z = cgeti(l+2);
287: bzero((char *)&z[2],l*sizeof(int));
288: for ( j = 0; j < l; j++ )
289: z[l-j+1] = b[j];
290: s = s>0?1:-1;
291: setsigne(z,s);
292: setlgefint(z,lg(z));
293: *rp = z;
294: }
295:
296: void patori_i(g,rp)
297: GEN g;
298: N *rp;
299: {
300: int j,l;
301: unsigned int *a,*b;
302: N z;
303:
304: l = lgef(g)-2;
305: a = (unsigned int *)g;
306: *rp = z = NALLOC(l); PL(z) = l;
307: for ( j = 0, b = (unsigned int *)BD(z); j < l; j++ )
308: b[l-j-1] = ((unsigned int *)g)[j+2];
309: }
310:
1.8 ohara 311: #elif SIZEOF_LONG == 8
1.1 noro 312: void ritopa_i(a,s,rp)
313: N a;
314: int s;
315: GEN *rp;
316: {
317: int j,l,words;
318: unsigned int *b;
319: GEN z;
320:
321: l = PL(a); b = BD(a);
322: words = (l+1)/2;
323: z = cgeti(words+2);
324: bzero((char *)&z[2],words*sizeof(long));
325: for ( j = 0; j < words; j++ )
326: z[words-j+1] = ((unsigned long)b[2*j])
327: |(((unsigned long)(2*j+1<l?b[2*j+1]:0))<<32);
328: s = s>0?1:-1;
329: setsigne(z,s);
330: setlgefint(z,lg(z));
331: *rp = z;
332: }
333:
334: void patori_i(g,rp)
335: GEN g;
336: N *rp;
337: {
338: int j,l,words;
339: unsigned long t;
340: unsigned long *a;
341: unsigned int *b;
342: N z;
343:
344: words = lgef(g)-2;
345: l = 2*words;
346: a = (unsigned long *)g;
347: *rp = z = NALLOC(l); PL(z) = l;
348: for ( j = 0, b = BD(z); j < words; j++ ) {
349: t = a[words+1-j];
350: b[2*j] = t&0xffffffff;
351: b[2*j+1] = t>>32;
352: }
353: PL(z) = b[l-1] ? l : l-1;
354: }
355: #endif
356:
357: void strtobf(s,rp)
358: char *s;
359: BF *rp;
360: {
361: GEN z;
362:
363: z = lisexpr(s); patori(z,(Obj *)rp); cgiv(z);
364: }
365: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>