Annotation of OpenXM_contrib2/asir2018/engine/gfspn.c, Revision 1.1
1.1 ! noro 1: /* $OpenXM$ */
! 2:
! 3: #include "ca.h"
! 4: #include "base.h"
! 5:
! 6: UM current_mod_gfsn;
! 7:
! 8: void setmod_gfsn(UM p)
! 9: {
! 10: current_mod_gfsn = p;
! 11: }
! 12:
! 13: void getmod_gfsn(UM *up)
! 14: {
! 15: *up = current_mod_gfsn;
! 16: }
! 17:
! 18: void simpgfsn(GFSN n,GFSN *r)
! 19: {
! 20: UM t,q;
! 21:
! 22: if ( !n )
! 23: *r = 0;
! 24: else if ( NID(n) != N_GFSN )
! 25: *r = n;
! 26: else {
! 27: t = UMALLOC(DEG(BDY(n)));
! 28: q = W_UMALLOC(DEG(BDY(n)));
! 29: cpyum(BDY(n),t);
! 30: DEG(t) = divsfum(t,current_mod_gfsn,q);
! 31: MKGFSN(t,*r);
! 32: }
! 33: }
! 34:
! 35: #define NZGFSN(a) ((a)&&(OID(a)==O_N)&&(NID(a)==N_GFSN))
! 36:
! 37: void ntogfsn(Obj a,GFSN *b)
! 38: {
! 39: UM t;
! 40: GFS c;
! 41:
! 42: if ( !a || (OID(a)==O_N && NID(a) == N_GFSN) )
! 43: *b = (GFSN)a;
! 44: else if ( OID(a) == O_N && (NID(a) == N_GFS || NID(a) == N_Q) ) {
! 45: ntogfs((Obj)a,&c);
! 46: if ( !b )
! 47: *b = 0;
! 48: else {
! 49: t = UMALLOC(0); ptosfum((P)c,t);
! 50: MKGFSN(t,*b);
! 51: }
! 52: } else
! 53: error("ntogfsn : invalid argument");
! 54: }
! 55:
! 56: void addgfsn(GFSN a,GFSN b,GFSN *c)
! 57: {
! 58: UM t,q;
! 59: GFSN z;
! 60: int d;
! 61:
! 62: ntogfsn((Obj)a,&z); a = z; ntogfsn((Obj)b,&z); b = z;
! 63: if ( !a )
! 64: *c = b;
! 65: else if ( !b )
! 66: *c = a;
! 67: else {
! 68: d = MAX(DEG(BDY(a)),DEG(BDY(b)));
! 69: t = UMALLOC(d);
! 70: q = W_UMALLOC(d);
! 71: addsfum(BDY(a),BDY(b),t);
! 72: DEG(t) = divsfum(t,current_mod_gfsn,q);
! 73: MKGFSN(t,z);
! 74: *c = (GFSN)z;
! 75: }
! 76: }
! 77:
! 78: void subgfsn(GFSN a,GFSN b,GFSN *c)
! 79: {
! 80: UM t,q;
! 81: GFSN z;
! 82: int d;
! 83:
! 84: ntogfsn((Obj)a,&z); a = z; ntogfsn((Obj)b,&z); b = z;
! 85: if ( !a )
! 86: chsgngfsn(b,c);
! 87: else if ( !b )
! 88: *c = a;
! 89: else {
! 90: d = MAX(DEG(BDY(a)),DEG(BDY(b)));
! 91: t = UMALLOC(d);
! 92: q = W_UMALLOC(d);
! 93: subsfum(BDY(a),BDY(b),t);
! 94: DEG(t) = divsfum(t,current_mod_gfsn,q);
! 95: MKGFSN(t,*c);
! 96: }
! 97: }
! 98:
! 99: extern int up_lazy;
! 100:
! 101: void mulgfsn(GFSN a,GFSN b,GFSN *c)
! 102: {
! 103: UM t,q;
! 104: GFSN z;
! 105: int d;
! 106:
! 107: ntogfsn((Obj)a,&z); a = z; ntogfsn((Obj)b,&z); b = z;
! 108: if ( !a || !b )
! 109: *c = 0;
! 110: else {
! 111: d = DEG(BDY(a))+DEG(BDY(b));
! 112: t = UMALLOC(d);
! 113: q = W_UMALLOC(d);
! 114: mulsfum(BDY(a),BDY(b),t);
! 115: DEG(t) = divsfum(t,current_mod_gfsn,q);
! 116: MKGFSN(t,*c);
! 117: }
! 118: }
! 119:
! 120: void divgfsn(GFSN a,GFSN b,GFSN *c)
! 121: {
! 122: GFSN z;
! 123: int d;
! 124: UM wb,wc,wd,we,t,q;
! 125:
! 126: ntogfsn((Obj)a,&z); a = z; ntogfsn((Obj)b,&z); b = z;
! 127: if ( !b )
! 128: error("divgfsn: division by 0");
! 129: else if ( !a )
! 130: *c = 0;
! 131: else {
! 132: wb = W_UMALLOC(DEG(BDY(b))); cpyum(BDY(b),wb);
! 133: d = DEG(current_mod_gfsn);
! 134: wc = W_UMALLOC(d); cpyum(current_mod_gfsn,wc);
! 135: wd = W_UMALLOC(2*d); we = W_UMALLOC(2*d);
! 136: /* wd*wb+we*wc=1 */
! 137: eucsfum(wb,wc,wd,we);
! 138: d = DEG(BDY(a))+DEG(wd);
! 139: t = UMALLOC(d);
! 140: q = W_UMALLOC(d);
! 141: mulsfum(BDY(a),wd,t);
! 142: DEG(t) = divsfum(t,current_mod_gfsn,q);
! 143: MKGFSN(t,*c);
! 144: }
! 145: }
! 146:
! 147: void invgfsn(GFSN b,GFSN *c)
! 148: {
! 149: GFSN z;
! 150: int d;
! 151: UM wb,wc,wd,we,t;
! 152:
! 153: ntogfsn((Obj)b,&z); b = z;
! 154: if ( !b )
! 155: error("divgfsn: division by 0");
! 156: else {
! 157: wb = W_UMALLOC(DEG(BDY(b))); cpyum(BDY(b),wb);
! 158: d = DEG(current_mod_gfsn);
! 159: wc = W_UMALLOC(d); cpyum(current_mod_gfsn,wc);
! 160: wd = W_UMALLOC(2*d); we = W_UMALLOC(2*d);
! 161: /* wd*wb+we*wc=1 */
! 162: eucsfum(wb,wc,wd,we);
! 163: d = DEG(wd);
! 164: t = UMALLOC(d);
! 165: cpyum(wd,t);
! 166: MKGFSN(t,*c);
! 167: }
! 168: }
! 169:
! 170: void chsgngfsn(GFSN a,GFSN *c)
! 171: {
! 172: GFSN z;
! 173: int d;
! 174: struct oUM zero;
! 175: UM t;
! 176:
! 177: ntogfsn((Obj)a,&z); a = z;
! 178: if ( !a )
! 179: *c = 0;
! 180: else {
! 181: d = DEG(BDY(a));
! 182: t = UMALLOC(d);
! 183: DEG(&zero) = -1;
! 184: subsfum(&zero,BDY(a),t);
! 185: MKGFSN(t,*c);
! 186: }
! 187: }
! 188:
! 189: void pwrgfsn(GFSN a,Z b,GFSN *c)
! 190: {
! 191: GFSN z;
! 192: UM t,x,y,q;
! 193: int d,k,blen;
! 194:
! 195: ntogfsn((Obj)a,&z); a = z;
! 196: if ( !b ) {
! 197: t = UMALLOC(0); DEG(t) = 0; COEF(t)[0] = _onesf();
! 198: MKGFSN(t,*c);
! 199: } else if ( !a )
! 200: *c = 0;
! 201: else {
! 202: d = DEG(current_mod_gfsn);
! 203:
! 204: /* y = 1 */
! 205: y = UMALLOC(d); DEG(y) = 0; COEF(y)[0] = _onesf();
! 206:
! 207: t = W_UMALLOC(2*d); q = W_UMALLOC(2*d);
! 208:
! 209: /* x = simplify(a) */
! 210: x = W_UMALLOC(DEG(BDY(a))); cpyum(BDY(a),x);
! 211: DEG(x) = divsfum(x,current_mod_gfsn,q);
! 212: if ( DEG(x) < 0 ) {
! 213: *c = 0;
! 214: } else {
! 215: blen = z_bits((Q)b);
! 216: for ( k = blen-1; k >= 0; k-- ) {
! 217: mulsfum(y,y,t);
! 218: DEG(t) = divsfum(t,current_mod_gfsn,q);
! 219: cpyum(t,y);
! 220: if ( tstbitz(b,k) ) {
! 221: mulsfum(y,x,t);
! 222: DEG(t) = divsfum(t,current_mod_gfsn,q);
! 223: cpyum(t,y);
! 224: }
! 225: }
! 226: MKGFSN(y,*c);
! 227: }
! 228: }
! 229: }
! 230:
! 231: int cmpgfsn(GFSN a,GFSN b)
! 232: {
! 233: GFSN z;
! 234:
! 235: ntogfsn((Obj)a,&z); a = z; ntogfsn((Obj)b,&z); b = z;
! 236: if ( !a )
! 237: if ( !b )
! 238: return 0;
! 239: else
! 240: return -1;
! 241: else if ( !b )
! 242: return 1;
! 243: else
! 244: return compsfum(BDY(a),BDY(b));
! 245: }
! 246:
! 247: void randomgfsn(GFSN *r)
! 248: {
! 249: int d;
! 250: UM t;
! 251:
! 252: if ( !current_mod_gfsn )
! 253: error("randomgfsn : current_mod_gfsn is not set");
! 254: d = DEG(current_mod_gfsn);
! 255: t = UMALLOC(d-1);
! 256: randsfum(d,t);
! 257: degum(t,d-1);
! 258: if ( DEG(t) < 0 )
! 259: *r = 0;
! 260: else {
! 261: MKGFSN(t,*r);
! 262: }
! 263: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>