/* GSoC12: Tobenna Peter, Igwe (Edited) */ Rat ratadd (Rat a, Rat b) { /* a.num = a.num * b.den + a.den * b.num; a.den *= b.den; return ratreduce(a); */ mp num, den, x, y, t; /*itomp (a.num, num) ;*/ copy(num, a.num); /*itomp (a.den, den) ;*/ copy(den, a.den); /*itomp (b.num, x) ;*/ copy(x, b.num); /*itomp (b.den, y) ;*/ copy(y, b.den); mulint (y, num, t); copy(num, t); mulint (den, x, x); linint (num, 1, x, 1); mulint (y, den, den); reduce(num, den) ; /*mptoi( num, &a.num, 1 ); mptoi( den, &a.den, 1 );*/ copy(a.num, num); copy(a.den, den); return a ; }
void mulrat ( lrs_mp Na, lrs_mp Da, lrs_mp Nb, lrs_mp Db, lrs_mp Nc, lrs_mp Dc ) /* computes Nc/Dc = Na/Da * Nb/Db and reduces by gcd(Nc,Dc) */ { mulint ( Na, Nb, Nc ); mulint ( Da, Db, Dc ); reduce ( Nc, Dc ); }
void divrat ( lrs_mp Na, lrs_mp Da, lrs_mp Nb, lrs_mp Db, lrs_mp Nc, lrs_mp Dc ) /* computes Nc/Dc = (Na/Da) / ( Nb/Db ) and reduces answer by gcd(Nc,Dc) */ { mulint ( Na, Db, Nc ); mulint ( Da, Nb, Dc ); reduce ( Nc, Dc ); }
void linrat ( lrs_mp Na, lrs_mp Da, long ka, lrs_mp Nb, lrs_mp Db, long kb, lrs_mp Nc, lrs_mp Dc ) /* computes Nc/Dc = ka*Na/Da +kb* Nb/Db and reduces answer by gcd(Nc,Dc) */ { lrs_mp c; mulint ( Na, Db, Nc ); mulint ( Da, Nb, c ); linint ( Nc, ka, c, kb ); /* Nc = (ka*Na*Db)+(kb*Da*Nb) */ mulint ( Da, Db, Dc ); /* Dc = Da*Db */ reduce ( Nc, Dc ); }
long comprod (lrs_mp Na, lrs_mp Nb, lrs_mp Nc, lrs_mp Nd) /* +1 if Na*Nb > Nc*Nd */ /* -1 if Na*Nb < Nc*Nd */ /* 0 if Na*Nb = Nc*Nd */ { lrs_mp mc, md; mulint (Na, Nb, mc); mulint (Nc, Nd, md); linint (mc, ONE, md, -ONE); if (positive (mc)) return (1); if (negative (mc)) return (-1); return (0); }
void getfactorial ( lrs_mp factorial, long k ) { /* compute k factorial in lrs_mp */ lrs_mp temp; long i; itomp ( ONE, factorial ); for ( i = 2; i <= k; i++ ) { itomp ( i, temp ); mulint ( temp, factorial, factorial ); } } /* end of getfactorial */
void lcm ( lrs_mp a, lrs_mp b ) /* a = least common multiple of a, b; b is preserved */ { lrs_mp u, v; copy ( u, a ); copy ( v, b ); gcd ( u, v ); exactdivint ( a, u, v ); /* v=a/u no remainder*/ mulint ( v, b, a ); } /* end of lcm */
void mixedtorealplan(int pl, Rat *mixed, Rat *rplan) { int r ; int i, len; int *list; mp num, den; /* numerator and denominator of sum */ mp x, y; list = TALLOC(nstrats[pl], int); /* for test purposes, we even compute the probability * of the empty sequence. Otherwise, we would start like this: * rplan[0] = ratfromi(1); * for (r = 1 .... */ for (r = 0; r < nseqs[pl]; r++) { itomp(0, num); itomp(1, den); len = seqtostratlist(firstmove[pl] + r, pl, list); for (i=0; i<len; i++) { itomp(mixed[list[i]].den, y); mulint (y, num, num); itomp(mixed[list[i]].num, x); mulint (den, x, x); linint (num, 1, x, 1); mulint (y, den, den); reduce(num, den) ; } /* mptoi( num, &(rplan[r].num), 1 ); */ if (mptoi( num, &(rplan[r].num), 1 )) printf(" numerator in mixed strategy probability %3d overflown\n", i); /* mptoi( den, &(rplan[r].den), 1 ); */ if (mptoi( den, &(rplan[r].den), 1 )) printf(" denominator in mixed strategy probability %3d overflown\n", i); } free(list); }
/* GSoC12: Tobenna Peter, Igwe (Edited) */ Rat ratmult (Rat a, Rat b) { mp x; /* avoid overflow in intermediate product by cross-cancelling first */ /*x = a.num ; */ copy(x, a.num); /*a.num = b.num ;*/ copy(a.num, b.num); /*b.num = x ;*/ copy(b.num, x); a = ratreduce(a); b = ratreduce(b); /*a.num *= b.num;*/ mulint(a.num, b.num, x); copy(a.num, x); /*a.den *= b.den;*/ mulint(a.den, b.den, x); copy(a.den, x); return ratreduce(a); /* a or b might be non-normalized s*/ }
Rat ratadd (Rat a, Rat b) { /* a.num = a.num * b.den + a.den * b.num; a.den *= b.den; return ratreduce(a); */ mp num, den, x, y; itomp (a.num, num) ; itomp (a.den, den) ; itomp (b.num, x) ; itomp (b.den, y) ; mulint (y, num, num); mulint (den, x, x); linint (num, 1, x, 1); mulint (y, den, den); reduce(num, den) ; mptoi( num, &a.num, 1 ); mptoi( den, &a.den, 1 ); return a ; }