Annotation of OpenXM_contrib2/asir2000/engine/Mgfs.c, Revision 1.2
1.2 ! noro 1: /* $OpenXM: OpenXM_contrib2/asir2000/engine/Mgfs.c,v 1.1 2001/06/20 09:32:13 noro Exp $ */
1.1 noro 2:
3: #include "ca.h"
4:
5: void mulssfum(UM,int,UM);
6:
7: void addsfum(p1,p2,pr)
8: UM p1,p2,pr;
9: {
10: int *c1,*c2,*cr,i,dmax,dmin;
11:
12: if ( DEG(p1) == -1 ) {
13: cpyum(p2,pr);
14: return;
15: }
16: if ( DEG(p2) == -1 ) {
17: cpyum(p1,pr);
18: return;
19: }
20: if ( DEG(p1) >= DEG(p2) ) {
21: c1 = COEF(p1); c2 = COEF(p2); dmax = DEG(p1); dmin = DEG(p2);
22: } else {
23: c1 = COEF(p2); c2 = COEF(p1); dmax = DEG(p2); dmin = DEG(p1);
24: }
25: for ( i = 0, cr = COEF(pr); i <= dmin; i++ )
26: cr[i] = _addsf(c1[i],c2[i]);
27: for ( ; i <= dmax; i++ )
28: cr[i] = c1[i];
29: if ( dmax == dmin )
30: degum(pr,dmax);
31: else
32: DEG(pr) = dmax;
33: }
34:
35: void subsfum(p1,p2,pr)
36: UM p1,p2,pr;
37: {
38: int *c1,*c2,*cr,i;
39: int dmax,dmin;
40:
41: if ( DEG(p1) == -1 ) {
42: for ( i = DEG(pr) = DEG(p2), c2 = COEF(p2), cr = COEF(pr);
43: i >= 0; i-- )
44: cr[i] = _chsgnsf(c2[i]);
45: return;
46: }
47: if ( DEG(p2) == -1 ) {
48: cpyum(p1,pr);
49: return;
50: }
51: c1 = COEF(p1); c2 = COEF(p2); cr = COEF(pr);
52: if ( DEG(p1) >= DEG(p2) ) {
53: dmax = DEG(p1); dmin = DEG(p2);
54: for ( i = 0; i <= dmin; i++ )
55: cr[i] = _subsf(c1[i],c2[i]);
56: for ( ; i <= dmax; i++ )
57: cr[i] = c1[i];
58: } else {
59: dmax = DEG(p2); dmin = DEG(p1);
60: for ( i = 0; i <= dmin; i++ )
61: cr[i] = _subsf(c1[i],c2[i]);
62: for ( ; i <= dmax; i++ )
63: cr[i] = _chsgnsf(c2[i]);
64: }
65: if ( dmax == dmin )
66: degum(pr,dmax);
67: else
68: DEG(pr) = dmax;
69: }
70:
71: void gcdsfum(p1,p2,pr)
72: UM p1,p2,pr;
73: {
74: int inv;
75: UM t1,t2,q,tum;
76: int drem;
77:
78: if ( DEG(p1) < 0 )
79: cpyum(p2,pr);
80: else if ( DEG(p2) < 0 )
81: cpyum(p1,pr);
82: else {
83: if ( DEG(p1) >= DEG(p2) ) {
84: t1 = p1; t2 = p2;
85: } else {
86: t1 = p2; t2 = p1;
87: }
88: q = W_UMALLOC(DEG(t1));
89: while ( ( drem = divsfum(t1,t2,q) ) >= 0 ) {
90: tum = t1; t1 = t2; t2 = tum; DEG(t2) = drem;
91: }
92: inv = _invsf(COEF(t2)[DEG(t2)]);
93: mulssfum(t2,inv,pr);
94: }
95: }
96: void mulsfum(p1,p2,pr)
97: UM p1,p2,pr;
98: {
99: int *pc1,*pcr;
100: int *c1,*c2,*cr;
101: int mul;
102: int i,j,d1,d2;
103:
104: if ( ( (d1 = DEG(p1)) < 0) || ( (d2 = DEG(p2)) < 0 ) ) {
105: DEG(pr) = -1;
106: return;
107: }
108: c1 = COEF(p1); c2 = COEF(p2); cr = COEF(pr);
109: bzero((char *)cr,(int)((d1+d2+1)*sizeof(int)));
110: for ( i = 0; i <= d2; i++, cr++ )
111: if ( mul = *c2++ )
112: for ( j = 0, pc1 = c1, pcr = cr; j <= d1; j++, pc1++, pcr++ )
113: *pcr = _addsf(_mulsf(*pc1,mul),*pcr);
114: DEG(pr) = d1 + d2;
115: }
116:
117: void mulssfum(p,n,pr)
118: int n;
119: UM p,pr;
120: {
121: int *sp,*dp;
122: int i;
123:
124: for ( i = DEG(pr) = DEG(p), sp = COEF(p)+i, dp = COEF(pr)+i;
125: i >= 0; i--, dp--, sp-- )
126: *dp = _mulsf(*sp,n);
127: }
128:
129: int divsfum(p1,p2,pq)
130: UM p1,p2,pq;
131: {
132: int *pc1,*pct;
133: int *c1,*c2,*ct;
134: int inv,hd,tmp;
135: int i,j, d1,d2,dd;
136:
137: if ( (d1 = DEG(p1)) < (d2 = DEG(p2)) ) {
138: DEG(pq) = -1;
139: return d1;
140: }
141: c1 = COEF(p1); c2 = COEF(p2); dd = d1-d2;
142: if ( ( hd = c2[d2] ) != _onesf() ) {
143: inv = _invsf(hd);
144: for ( pc1 = c2 + d2; pc1 >= c2; pc1-- )
145: *pc1 = _mulsf(*pc1,inv);
146: } else
147: inv = _onesf();
148: for ( i = dd, ct = c1+d1; i >= 0; i-- )
149: if ( tmp = *ct-- ) {
150: tmp = _chsgnsf(tmp);
151: for ( j = d2-1, pct = ct, pc1 = c2+j; j >= 0; j--, pct--, pc1-- )
152: *pct = _addsf(_mulsf(*pc1,tmp),*pct);
153: }
154: if ( inv != _onesf() ) {
155: for ( pc1 = c1+d2, pct = c1+d1; pc1 <= pct; pc1++ )
156: *pc1 = _mulsf(*pc1,inv);
157: for ( pc1 = c2, pct = c2+d2; pc1 <= pct; pc1++ )
158: *pc1 = _mulsf(*pc1,hd);
159: }
160: for ( i = d2-1, pc1 = c1+i; i >= 0 && !(*pc1); pc1--, i-- );
161: for ( DEG(pq) = j = dd, pc1 = c1+d1, pct = COEF(pq)+j; j >= 0; j-- )
162: *pct-- = *pc1--;
163: return i;
164: }
165:
166: void diffsfum(f,fd)
167: UM f,fd;
168: {
169: int *dp,*sp;
170: int i;
171:
172: for ( i = DEG(f), dp = COEF(fd)+i-1, sp = COEF(f)+i;
173: i >= 1; i--, dp--, sp-- ) {
174: *dp = _mulsf(*sp,_itosf(i));
175: }
176: degum(fd,DEG(f) - 1);
177: }
178:
179: void monicsfum(f)
180: UM f;
181: {
182: int *sp;
183: int i,inv;
184:
185: i = DEG(f); sp = COEF(f)+i;
186: inv = _invsf(*sp);
187: for ( ; i >= 0; i--, sp-- )
188: *sp = _mulsf(*sp,inv);
1.2 ! noro 189: }
! 190:
! 191: void addsf_trunc(int,int *,int *);
! 192: void mulsf_trunc(int,int *,int *,int *);
! 193:
! 194: void mulsflum(n,f1,f2,fr)
! 195: int n;
! 196: LUM f1,f2,fr;
! 197: {
! 198: int max;
! 199: int i,j,**p1,**p2,*px;
! 200: int *w,*w1,*w2;
! 201:
! 202: p1 = (int **)COEF(f1); p2 = (int **)COEF(f2);
! 203: w = W_ALLOC(2*(n+1)); w1 = W_ALLOC(DEG(f1)); w2 = W_ALLOC(DEG(f2));
! 204: for ( i = DEG(f1); i >= 0; i-- ) {
! 205: for ( j = n - 1, px = p1[i]; ( j >= 0 ) && ( px[j] == 0 ); j-- );
! 206: w1[i] = ( j == -1 ? 0 : 1 );
! 207: }
! 208: for ( i = DEG(f2); i >= 0; i-- ) {
! 209: for ( j = n - 1, px = p2[i]; ( j >= 0 ) && ( px[j] == 0 ); j-- );
! 210: w2[i] = ( j == -1 ? 0 : 1 );
! 211: }
! 212: for ( j = DEG(fr) = DEG(f1) + DEG(f2); j >= 0; j-- ) {
! 213: for ( i = n - 1, px = COEF(fr)[j]; i >= 0; i-- )
! 214: px[i] = 0;
! 215: for ( max = MIN(DEG(f1),j), i = MAX(0,j-DEG(f2)); i <= max; i++ )
! 216: if ( w1[i] != 0 && w2[j - i] != 0 ) {
! 217: mulsf_trunc(n,p1[i],p2[j - i],w); addsf_trunc(n,w,px);
! 218: }
! 219: }
! 220: }
! 221:
! 222: void addsf_trunc(n,a1,a2)
! 223: int n;
! 224: int *a1,*a2;
! 225: {
! 226: /* XXX */
! 227: }
! 228:
! 229: void mulsf_trunc(n,a1,a2,r)
! 230: int n;
! 231: int *a1,*a2,*r;
! 232: {
! 233: /* XXX */
! 234: }
! 235:
! 236: void eucsfum(f1,f2,a,b)
! 237: UM f1,f2,a,b;
! 238: {
! 239: UM g1,g2,a1,a2,a3,wm,q,tum;
! 240: int d,dr;
! 241:
! 242: /* XXX */
! 243: d = DEG(f1) + DEG(f2) + 10;
! 244: g1 = W_UMALLOC(d); g2 = W_UMALLOC(d); a1 = W_UMALLOC(d);
! 245: a2 = W_UMALLOC(d); a3 = W_UMALLOC(d); wm = W_UMALLOC(d);
! 246: q = W_UMALLOC(d);
! 247: DEG(a1) = 0; COEF(a1)[0] = 1; DEG(a2) = -1;
! 248: cpyum(f1,g1); cpyum(f2,g2);
! 249: while ( 1 ) {
! 250: dr = divsfum(g1,g2,q); tum = g1; g1 = g2; g2 = tum;
! 251: if ( ( DEG(g2) = dr ) == -1 )
! 252: break;
! 253: mulsfum(a2,q,wm); subsfum(a1,wm,a3); dr = divsfum(a3,f2,q);
! 254: tum = a1; a1 = a2; a2 = a3; a3 = tum; DEG(a3) = dr;
! 255: }
! 256: if ( COEF(g1)[0] != 1 )
! 257: mulssfum(a2,_invsf(COEF(g1)[0]),a);
! 258: else
! 259: cpyum(a2,a);
! 260: mulsfum(a,f1,wm);
! 261: if ( DEG(wm) >= 0 )
! 262: COEF(wm)[0] = _subsf(COEF(wm)[0],_onesf());
! 263: else {
! 264: DEG(wm) = 0; COEF(wm)[0] = _chsgnsf(_onesf());
! 265: }
! 266: divsfum(wm,f2,q); mulssfum(q,_chsgnsf(_onesf()),b);
1.1 noro 267: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>