Annotation of OpenXM_contrib2/asir2000/engine/pari.c, Revision 1.4
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.4 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/engine/pari.c,v 1.3 2000/08/22 05:04:06 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
51: #if PARI
52: #include "base.h"
53: #include <math.h>
54: #include "genpari.h"
55:
56: void patori(GEN,Obj *);
57: void patori_i(GEN,N *);
58: void ritopa(Obj,GEN *);
59: void ritopa_i(N,int,GEN *);
60:
61: extern long prec;
62: extern int paristack;
63:
64: void risa_pari_init() {
65: char buf[BUFSIZ];
66: int i;
67:
68: pari_init(paristack,2);
69: prec = 4;
70: }
71:
72: void create_pari_variable(index)
73: int index;
74: {
75: static int max_varn;
76: int i;
77: char name[BUFSIZ];
78:
79: if ( index > max_varn ) {
80: for ( i = max_varn+1; i <= index; i++ ) {
81: sprintf(name,"x%d",i);
82: fetch_named_var(name,0);
83: }
84: max_varn = index;
85: }
86: }
87:
88: int get_lg(a)
89: GEN a;
90: {
91: return lg(a);
92: }
93:
94: void ritopa(a,rp)
95: Obj a;
96: GEN *rp;
97: {
98: long ltop;
99: GEN pnm,z,w;
100: DCP dc;
101: int i,j,l,row,col;
102: VL vl;
103: V v;
104:
105: if ( !a ) {
106: *rp = gzero; return;
107: }
108: switch ( OID(a) ) {
109: case O_N:
110: switch ( NID(a) ) {
111: case N_Q:
112: ltop = avma; ritopa_i(NM((Q)a),SGN((Q)a),&pnm);
113: if ( INT((Q)a) )
114: *rp = pnm;
115: else {
116: *rp = z = cgetg(3,4); z[1] = (long)pnm;
117: ritopa_i(DN((Q)a),1,(GEN *)&z[2]);
118: }
119: break;
120: case N_R:
121: *rp = dbltor(BDY((Real)a)); break;
122: case N_B:
123: *rp = gcopy((GEN)BDY(((BF)a))); break;
124: case N_C:
125: z = cgetg(3,6);
126: ritopa((Obj)((C)a)->r,(GEN *)&z[1]); ritopa((Obj)((C)a)->i,(GEN *)&z[2]);
127: *rp = z;
128: break;
129: default:
130: error("ritopa : not implemented"); break;
131: }
132: break;
133: case O_P:
134: l = UDEG((P)a); *rp = z = cgetg(l+3,10);
135: setsigne(z,1);
136: for ( i = 0, vl = CO, v = VR((P)a); vl->v != v;
137: vl = NEXT(vl), i++ );
138: create_pari_variable(i);
139: setvarn(z,i);
140: setlgef(z,l+3);
141: for ( i = l+2; i >= 2; i-- )
142: z[i] = (long)gzero;
143: for ( dc = DC((P)a); dc; dc = NEXT(dc) )
144: ritopa((Obj)COEF(dc),(GEN *)&z[QTOS(DEG(dc))+2]);
145: break;
146: case O_VECT:
147: l = ((VECT)a)->len; z = cgetg(l+1,17);
148: for ( i = 0; i < l; i++ )
149: ritopa((Obj)BDY((VECT)a)[i],(GEN *)&z[i+1]);
150: *rp = z;
151: break;
152: case O_MAT:
153: row = ((MAT)a)->row; col = ((MAT)a)->col; z = cgetg(col+1,19);
154: for ( j = 0; j < col; j++ ) {
155: w = cgetg(row+1,18);
156: for ( i = 0; i < row; i++ )
157: ritopa((Obj)BDY((MAT)a)[i][j],(GEN *)&w[i+1]);
158: z[j+1] = (long)w;
159: }
160: *rp = z;
161: break;
162: default:
163: error("ritopa : not implemented"); break;
164: }
165: }
166:
167: void patori(a,rp)
168: GEN a;
169: Obj *rp;
170: {
171: Q q,qnm,qdn;
172: BF r;
173: C c;
174: VECT v;
175: MAT m;
176: N n,nm,dn;
177: DCP dc0,dc;
178: P t;
179: int s,i,j,l,row,col;
180: GEN b;
181: VL vl;
182:
183: if ( gcmp0(a) )
184: *rp = 0;
185: else {
186: switch ( typ(a) ) {
187: case 1: /* integer */
188: patori_i(a,&n); NTOQ(n,(char)signe(a),q); *rp = (Obj)q;
189: break;
190: case 2: /* real */
191: NEWBF(r,lg(a)); bcopy((char *)a,(char *)BDY(r),lg(a)*sizeof(long));
192: *rp = (Obj)r;
193: break;
194: case 4: /* rational; reduced */
195: patori_i((GEN)a[1],&nm); patori_i((GEN)a[2],&dn);
196: s = signe(a[1])*signe(a[2]);
197: if ( UNIN(dn) )
198: NTOQ(nm,s,q);
199: else
200: NDTOQ(nm,dn,s,q);
201: *rp = (Obj)q;
202: break;
203: case 5: /* rational; not necessarily reduced */
204: patori_i((GEN)a[1],&nm); patori_i((GEN)a[2],&dn);
205: s = signe(a[1])*signe(a[2]);
206: NTOQ(nm,s,qnm); NTOQ(dn,1,qdn); divq(qnm,qdn,(Q *)rp);
207: break;
208: case 6: /* complex */
209: if ( gcmp0((GEN)a[2]) )
210: patori((GEN)a[1],rp);
211: else {
212: NEWC(c); patori((GEN)a[1],(Obj *)&c->r); patori((GEN)a[2],(Obj *)&c->i);
213: *rp = (Obj)c;
214: }
215: break;
216: case 10: /* polynomial */
217: for ( i = lgef(a)-1, dc0 = 0; i >= 2; i-- )
218: if ( !gcmp0((GEN)a[i]) ) {
219: NEXTDC(dc0,dc);
220: patori((GEN)a[i],(Obj *)&COEF(dc)); STOQ(i-2,DEG(dc));
221: }
222: if ( !dc0 )
223: *rp = 0;
224: else {
225: /* assuming that CO has not changed. */
226: for ( s = varn(a), i = 0, vl = CO; i != s;
227: i++, vl = NEXT(vl) );
228: NEXT(dc) = 0; MKP(vl->v,dc0,t); *rp = (Obj)t;
229: }
230: break;
231: case 17: /* row vector */
232: case 18: /* column vector */
233: l = lg(a)-1; MKVECT(v,l);
234: for ( i = 0; i < l; i++ )
235: patori((GEN)a[i+1],(Obj *)&BDY(v)[i]);
236: *rp = (Obj)v;
237: break;
238: case 19: /* matrix */
239: col = lg(a)-1; row = lg(a[1])-1; MKMAT(m,row,col);
240: for ( j = 0; j < col; j++ )
241: for ( i = 0, b = (GEN)a[j+1]; i < row; i++ )
242: patori((GEN)b[i+1],(Obj *)&BDY(m)[i][j]);
243: *rp = (Obj)m;
244: break;
245: default:
246: error("patori : not implemented");
247: break;
248: }
249: }
250: }
251:
252: #if defined(LONG_IS_32BIT)
253: void ritopa_i(a,s,rp)
254: N a;
255: int s;
256: GEN *rp;
257: {
258: int j,l;
259: unsigned int *b;
260: GEN z;
261:
262: l = PL(a); b = (unsigned int *)BD(a);
263: z = cgeti(l+2);
264: bzero((char *)&z[2],l*sizeof(int));
265: for ( j = 0; j < l; j++ )
266: z[l-j+1] = b[j];
267: s = s>0?1:-1;
268: setsigne(z,s);
269: setlgefint(z,lg(z));
270: *rp = z;
271: }
272:
273: void patori_i(g,rp)
274: GEN g;
275: N *rp;
276: {
277: int j,l;
278: unsigned int *a,*b;
279: N z;
280:
281: l = lgef(g)-2;
282: a = (unsigned int *)g;
283: *rp = z = NALLOC(l); PL(z) = l;
284: for ( j = 0, b = (unsigned int *)BD(z); j < l; j++ )
285: b[l-j-1] = ((unsigned int *)g)[j+2];
286: }
287: #endif
288:
289: #if defined(LONG_IS_64BIT)
290: void ritopa_i(a,s,rp)
291: N a;
292: int s;
293: GEN *rp;
294: {
295: int j,l,words;
296: unsigned int *b;
297: GEN z;
298:
299: l = PL(a); b = BD(a);
300: words = (l+1)/2;
301: z = cgeti(words+2);
302: bzero((char *)&z[2],words*sizeof(long));
303: for ( j = 0; j < words; j++ )
304: z[words-j+1] = ((unsigned long)b[2*j])
305: |(((unsigned long)(2*j+1<l?b[2*j+1]:0))<<32);
306: s = s>0?1:-1;
307: setsigne(z,s);
308: setlgefint(z,lg(z));
309: *rp = z;
310: }
311:
312: void patori_i(g,rp)
313: GEN g;
314: N *rp;
315: {
316: int j,l,words;
317: unsigned long t;
318: unsigned long *a;
319: unsigned int *b;
320: N z;
321:
322: words = lgef(g)-2;
323: l = 2*words;
324: a = (unsigned long *)g;
325: *rp = z = NALLOC(l); PL(z) = l;
326: for ( j = 0, b = BD(z); j < words; j++ ) {
327: t = a[words+1-j];
328: b[2*j] = t&0xffffffff;
329: b[2*j+1] = t>>32;
330: }
331: PL(z) = b[l-1] ? l : l-1;
332: }
333: #endif
334:
335: void strtobf(s,rp)
336: char *s;
337: BF *rp;
338: {
339: GEN z;
340:
341: z = lisexpr(s); patori(z,(Obj *)rp); cgiv(z);
342: }
343: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>