Exemple #1
0
Fichier : rat.c Projet : ptigwe/gte
/* GSoC12: Tobenna Peter, Igwe */
    Rat parseRat(char* srat, const char* info, int j)
{
    char snum[MAXSTR], sden[MAXSTR];
    mp num, den;

    atoaa(srat, snum, sden);
    atomp(snum, num);
    if (sden[0]=='\0') 
        itomp(1, den);
    else
    {
        atomp(sden, den);
        if (negative(den) || zero(den))
        {
            char str[MAXSTR];
            mptoa(den, str);
            fprintf(stderr, "Warning: Denominator "); 
            fprintf(stderr, "%s of %s[%d] set to 1 since not positive\n", 
                str, info, j+1);
            itomp(1, den);  
        }
    }
    Rat r = mptorat(num, den);
    return r;
}
Exemple #2
0
void
atomp ( char s[], lrs_mp a ) { /*convert string to lrs_mp integer */
  lrs_mp mpone;
  long diff, ten, i, sig;
  itomp ( 1L, mpone );
  ten = 10L;

  for ( i = 0; s[i] == ' ' || s[i] == '\n' || s[i] == '\t'; i++ );

  /*skip white space */
  sig = POS;

  if ( s[i] == '+' || s[i] == '-' ) /* sign */
    sig = ( s[i++] == '+' ) ? POS : NEG;

  itomp ( 0L, a );

  while ( s[i] >= '0' && s[i] <= '9' ) {
    diff = s[i] - '0';
    linint ( a, ten, mpone, diff );
    i++;
  }

  storesign ( a, sig );

  if ( s[i] ) {
    fprintf ( stderr, "\nIllegal character in number: '%s'\n", s + i );
    exit ( 1 );
  }

}       /* end of atomp */
Exemple #3
0
Fichier : rat.c Projet : ptigwe/gte
/* Creates a rational number from two integers */
Rat itorat(int num, int den)
{
    Rat r;
    itomp(num, r.num);
    itomp(den, r.den);
    ratreduce(r);
    return r;
}
Exemple #4
0
Fichier : rat.c Projet : ptigwe/gte
/* GSoC12: Tobenna Peter, Igwe (Edited)*/
Rat ratfromi(int i)
{
    Rat tmp;
    /*tmp.num = i; */
    itomp(i, tmp.num);
    /*tmp.den = 1; */
    itomp(1, tmp.den);
    return tmp;
}
Exemple #5
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 */
Exemple #6
0
long
readrat ( lrs_mp Na, lrs_mp Da )
/* read a rational or integer and convert to lrs_mp with base BASE */
/* returns true if denominator is not one                      */
/* returns 999 if premature end of file                        */
{
  char in[MAXINPUT], num[MAXINPUT], den[MAXINPUT];

  if ( fscanf ( lrs_ifp, "%s", in ) == EOF ) {
    fprintf ( lrs_ofp, "\nInvalid rational input" );
    exit ( 1 );
  }

  if ( !strcmp ( in, "end" ) ) { /*premature end of input file */
    return ( 999L );
  }

  atoaa ( in, num, den ); /*convert rational to num/dem strings */
  atomp ( num, Na );

  if ( den[0] == '\0' ) {
    itomp ( 1L, Da );
    return ( FALSE );
  }

  atomp ( den, Da );
  return ( TRUE );
}
Exemple #7
0
Fichier : rat.c Projet : ptigwe/gte
/* GSoC12: Tobenna Peter, Igwe (Edited) */
Rat ratreduce (Rat a)
{
    if (zero(a.num))
    {
        /*a.den = 1;*/
        itomp(1, a.den);
    }
    else
    {
        mp div;
        mp c;
        if (negative(a.den))
        {
            /*a.den = -a.den;*/
            changesign(a.den);
            /*a.num = -a.num;*/
            changesign(a.num);
        }
        /*div = ratgcd(a.den, a.num);*/
        ratgcd(a.den, a.num, div);
        /*a.num = a.num/div;*/
        divint(a.num, div, c);
        copy(a.num, c);
        /*a.den = a.den/div;*/
        divint(a.den, div, c);
        copy(a.den, c);
    }
    return a;
}
Exemple #8
0
void
scalerat ( lrs_mp Na, lrs_mp Da, long ka ) { /* scales rational by ka */
  lrs_mp Nt;
  copy ( Nt, Na );
  itomp ( ZERO, Na );
  linint ( Na, ZERO, Nt, ka );
  reduce ( Na, Da );
}
Exemple #9
0
Fichier : rat.c Projet : ptigwe/gte
/* GSoC12: Tobenna Peter, Igwe (Edited) */
Bool ratiseq (Rat a, Rat b)
{
    /*return (a.num == b.num && a.den == b.den);*/
    mp c;
    itomp(1, c);
    int x = comprod(a.num, c, b.num, c);
    int y = comprod(a.den, c, b.den, c);
    return ((x == 0) && (y == 0));
}
Exemple #10
0
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);
}
Exemple #11
0
/* returns 999 if premature end of file                        */
long plrs_readrat (lrs_mp Na, lrs_mp Da, const char* rat)
{
  	char in[MAXINPUT], num[MAXINPUT], den[MAXINPUT];
 	strcpy(in, rat);
	atoaa (in, num, den);		/*convert rational to num/dem strings */
	atomp (num, Na);
	if (den[0] == '\0')
	{
		itomp (1L, Da);
		return (FALSE);
	}
	atomp (den, Da);
	return (TRUE);
}
Exemple #12
0
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 ; 
}
Exemple #13
0
RSApriv*
rsagen(int nlen, int elen, int rounds)
{
	mpint *p, *q, *e, *d, *phi, *n, *t1, *t2, *kp, *kq, *c2;
	RSApriv *rsa;

	p = mpnew(nlen/2);
	q = mpnew(nlen/2);
	n = mpnew(nlen);
	e = mpnew(elen);
	d = mpnew(0);
	phi = mpnew(nlen);

	// create the prime factors and euclid's function
	genprime(p, nlen/2, rounds);
	genprime(q, nlen - mpsignif(p) + 1, rounds);
	mpmul(p, q, n);
	mpsub(p, mpone, e);
	mpsub(q, mpone, d);
	mpmul(e, d, phi);

	// find an e relatively prime to phi
	t1 = mpnew(0);
	t2 = mpnew(0);
	mprand(elen, genrandom, e);
	if(mpcmp(e,mptwo) <= 0)
		itomp(3, e);
	// See Menezes et al. p.291 "8.8 Note (selecting primes)" for discussion
	// of the merits of various choices of primes and exponents.  e=3 is a
	// common and recommended exponent, but doesn't necessarily work here
	// because we chose strong rather than safe primes.
	for(;;){
		mpextendedgcd(e, phi, t1, d, t2);
		if(mpcmp(t1, mpone) == 0)
			break;
		mpadd(mpone, e, e);
	}
	mpfree(t1);
	mpfree(t2);

	// compute chinese remainder coefficient
	c2 = mpnew(0);
	mpinvert(p, q, c2);

	// for crt a**k mod p == (a**(k mod p-1)) mod p
	kq = mpnew(0);
	kp = mpnew(0);
	mpsub(p, mpone, phi);
	mpmod(d, phi, kp);
	mpsub(q, mpone, phi);
	mpmod(d, phi, kq);

	rsa = rsaprivalloc();
	rsa->pub.ek = e;
	rsa->pub.n = n;
	rsa->dk = d;
	rsa->kp = kp;
	rsa->kq = kq;
	rsa->p = p;
	rsa->q = q;
	rsa->c2 = c2;

	mpfree(phi);

	return rsa;
}
Exemple #14
0
Fichier : rat.c Projet : ptigwe/gte
/* GSoC12: Tobenna Peter, Igwe */
Rat ratfrommp(mp num)
{
    mp den;
    itomp(1, den);
    return mptorat(num, den);
}
Exemple #15
0
long 
lrs_getfirstbasis2 (lrs_dic ** D_p, lrs_dat * Q, lrs_dic * P2orig, lrs_mp_matrix * Lin, long no_output)
/* gets first basis, FALSE if none              */
/* P may get changed if lin. space Lin found    */
/* no_output is TRUE supresses output headers   */
{
  long i, j, k;

/* assign local variables to structures */

  lrs_mp_matrix A;
  long *B, *C, *Row, *Col;
  long *inequality;
  long *linearity;
  long hull = Q->hull;
  long m, d, lastdv, nlinearity, nredundcol;

  static long ocount=0;


  m = D->m;
  d = D->d;
  lastdv = Q->lastdv;

  nredundcol = 0L;		/* will be set after getabasis        */
  nlinearity = Q->nlinearity;	/* may be reset if new linearity read */
  linearity = Q->linearity;

  A = D->A;
  B = D->B;
  C = D->C;
  Row = D->Row;
  Col = D->Col;
  inequality = Q->inequality;

/* default is to look for starting cobasis using linearies first, then     */
/* filling in from last rows of input as necessary                         */
/* linearity array is assumed sorted here                                  */
/* note if restart/given start inequality indices already in place         */
/* from nlinearity..d-1                                                    */

  for (i = 0; i < nlinearity; i++)      /* put linearities first in the order */
    inequality[i] = linearity[i];


  k = 0;			/* index for linearity array   */

  if (Q->givenstart)
    k = d;
  else
    k = nlinearity;
  for (i = m; i >= 1; i--)
    {
      j = 0;
      while (j < k && inequality[j] != i)
	j++;			/* see if i is in inequality  */
      if (j == k)
	inequality[k++] = i;
    }
  if (Q->debug)
    {
      fprintf (lrs_ofp, "\n*Starting cobasis uses input row order");
      for (i = 0; i < m; i++)
	fprintf (lrs_ofp, " %ld", inequality[i]);
    }

  if (!Q->maximize && !Q->minimize)
    for (j = 0; j <= d; j++)
      itomp (ZERO, A[0][j]);

/* Now we pivot to standard form, and then find a primal feasible basis       */
/* Note these steps MUST be done, even if restarting, in order to get         */
/* the same index/inequality correspondance we had for the original prob.     */
/* The inequality array is used to give the insertion order                   */
/* and is defaulted to the last d rows when givenstart=FALSE                  */

  if (!getabasis2 (D, Q,P2orig, inequality))
          return FALSE;

  if(Q->debug)
  {
    fprintf(lrs_ofp,"\nafter getabasis2");
    printA(D, Q);
  }
  nredundcol = Q->nredundcol;
  lastdv = Q->lastdv;
  d = D->d;

/********************************************************************/
/* now we start printing the output file  unless no output requested */
/********************************************************************/
  if (!no_output || Q->debug)
    {
      fprintf (lrs_ofp, "\nV-representation");

/* Print linearity space                 */
/* Don't print linearity if first column zero in hull computation */

      k = 0;

     if (nredundcol > k)
	{
	  fprintf (lrs_ofp, "\nlinearity %ld ", nredundcol - k);	/*adjust nredundcol for homog. */
	  for (i = 1; i <= nredundcol - k; i++)
	    fprintf (lrs_ofp, " %ld", i);
	}			/* end print of linearity space */

      fprintf (lrs_ofp, "\nbegin");
      fprintf (lrs_ofp, "\n***** %ld rational", Q->n);

    }				/* end of if !no_output .......   */

/* Reset up the inequality array to remember which index is which input inequality */
/* inequality[B[i]-lastdv] is row number of the inequality with index B[i]              */
/* inequality[C[i]-lastdv] is row number of the inequality with index C[i]              */

  for (i = 1; i <= m; i++)
    inequality[i] = i;
  if (nlinearity > 0)		/* some cobasic indices will be removed */
    {
      for (i = 0; i < nlinearity; i++)	/* remove input linearity indices */
	inequality[linearity[i]] = 0;
      k = 1;			/* counter for linearities         */
      for (i = 1; i <= m - nlinearity; i++)
	{
	  while (k <= m && inequality[k] == 0)
	    k++;		/* skip zeroes in corr. to linearity */
	  inequality[i] = inequality[k++];
	}
    }				/* end if linearity */
  if (Q->debug)
    {
      fprintf (lrs_ofp, "\ninequality array initialization:");
      for (i = 1; i <= m - nlinearity; i++)
	fprintf (lrs_ofp, " %ld", inequality[i]);
    }
  if (nredundcol > 0)
    {
      *Lin = lrs_alloc_mp_matrix (nredundcol, Q->n);

      for (i = 0; i < nredundcol; i++)
	{
	  if (!(Q->homogeneous && Q->hull && i == 0))	/* skip redund col 1 for homog. hull */
	    {
	      lrs_getray (D, Q, Col[0], D->C[0] + i - hull, (*Lin)[i]);		/* adjust index for deletions */
	    }

	  if (!removecobasicindex (D, Q, 0L))
	    return FALSE;
	}
    }				/* end if nredundcol > 0 */

      if (Q->verbose)
      {
      fprintf (lrs_ofp, "\nNumber of pivots for starting dictionary: %ld",Q->count[3]);
      ocount=Q->count[3];
      }

/* Do dual pivots to get primal feasibility */
  if (!primalfeasible (D, Q))
    {
     if ( Q->verbose )
      {
          fprintf (lrs_ofp, "\nNumber of pivots for feasible solution: %ld",Q->count[3]);
          fprintf (lrs_ofp, " - No feasible solution");
          ocount=Q->count[3];
      }
      return FALSE;
    }

    if (Q->verbose)
     {
      fprintf (lrs_ofp, "\nNumber of pivots for feasible solution: %ld",Q->count[3]);
      ocount=Q->count[3];
     }


/* Now solve LP if objective function was given */
  if (Q->maximize || Q->minimize)
    {
      Q->unbounded = !lrs_solvelp (D, Q, Q->maximize);

      /* check to see if objective is dual degenerate */
      j = 1;
      while (j <= d && !zero (A[0][j]))
      j++;
      if (j <= d)
	    Q->dualdeg = TRUE;
    }
  else
/* re-initialize cost row to -det */
    {
      for (j = 1; j <= d; j++)
	{
	  copy (A[0][j], D->det);
	  storesign (A[0][j], NEG);
	}

      itomp (ZERO, A[0][0]);	/* zero optimum objective value */
    }


/* reindex basis to 0..m if necessary */
/* we use the fact that cobases are sorted by index value */
  if (Q->debug)
    printA (D, Q);
  while (C[0] <= m)
    {
      i = C[0];
      j = inequality[B[i] - lastdv];
      inequality[B[i] - lastdv] = inequality[C[0] - lastdv];
      inequality[C[0] - lastdv] = j;
      C[0] = B[i];
      B[i] = i;
      reorder1 (C, Col, ZERO, d);
    }

  if (Q->debug)
    {
      fprintf (lrs_ofp, "\n*Inequality numbers for indices %ld .. %ld : ", lastdv + 1, m + d);
      for (i = 1; i <= m - nlinearity; i++)
	fprintf (lrs_ofp, " %ld ", inequality[i]);
      printA (D, Q);
    }



  if (Q->restart)
    {
      if (Q->debug)
	fprintf (lrs_ofp, "\nPivoting to restart co-basis");
      if (!restartpivots (D, Q))
	return FALSE;
      D->lexflag = lexmin (D, Q, ZERO);		/* see if lexmin basis */
      if (Q->debug)
	printA (D, Q);
    }
/* Check to see if necessary to resize */
  if (Q->inputd > D->d)
    *D_p = resize (D, Q);

  return TRUE;
}