示例#1
0
static PyObject *
GMPy_MPZ_Function_Remove(PyObject *self, PyObject *args)
{
    MPZ_Object *result = NULL, *tempx = NULL, *tempf = NULL;
    PyObject *x, *f;
    size_t multiplicity;

    if (PyTuple_GET_SIZE(args) != 2) {
        TYPE_ERROR("remove() requires 'mpz','mpz' arguments");
        return NULL;
    }

    if (!(result = GMPy_MPZ_New(NULL))) {
        /* LCOV_EXCL_START */
        return NULL;
        /* LCOV_EXCL_STOP */
    }

    x = PyTuple_GET_ITEM(args, 0);
    f = PyTuple_GET_ITEM(args, 1);

    if (MPZ_Check(x) && MPZ_Check(f)) {
        if (mpz_cmp_si(MPZ(f), 2) < 0) {
            VALUE_ERROR("factor must be > 1");
            Py_DECREF((PyObject*)result);
            return NULL;
        }
        multiplicity = mpz_remove(result->z, MPZ(x), MPZ(f));
        return Py_BuildValue("(Nk)", result, multiplicity);
    }
    else {


        if (!(tempx = GMPy_MPZ_From_Integer(x, NULL)) ||
            !(tempf = GMPy_MPZ_From_Integer(f, NULL))) {

            TYPE_ERROR("remove() requires 'mpz','mpz' arguments");
            Py_XDECREF((PyObject*)tempx);
            Py_XDECREF((PyObject*)tempf);
            Py_DECREF((PyObject*)result);
            return NULL;
        }
        if (mpz_cmp_si(MPZ(tempf), 2) < 0) {
            VALUE_ERROR("factor must be > 1");
            Py_DECREF((PyObject*)tempx);
            Py_DECREF((PyObject*)tempf);
            Py_DECREF((PyObject*)result);
            return NULL;
        }
        multiplicity = mpz_remove(result->z, tempx->z, tempf->z);
        return Py_BuildValue("(Nk)", result, multiplicity);
    }
}
示例#2
0
文件: factor.c 项目: alexlibi/save
// Liefert die Primfaktorzerlegung einer Zahl als String.
char *factorize(mpz_t number)
{
	// Primtest (Miller-Rabin).
	if (mpz_probab_prime_p(number, 10) > 0)
		return mpz_get_str(NULL, 10, number);

	mpz_t factor, cofactor;
	mpz_init(factor);
	mpz_init(cofactor);
	char *str1, *str2, *result;
	int B1 = INITB1, B2 = INITB2, curves = INITCURVES;

	// Zunaechst eine einfache Probedivision.
	trial(number, factor, 3e3);
	if (mpz_cmp_si(factor, 1) == 0)
	{
		// Zweite Strategie: Pollard-Rho.
		do
		{
			rho(number, factor, 4e4);
		} while (mpz_cmp(factor, number) == 0);
		// Falls immer noch kein Faktor gefunden wurde, mit ECM fortfahren.
		while (mpz_cmp_si(factor, 1) == 0)
		{
			ecm(number, factor, B1, B2, curves);
			if (mpz_cmp(factor, number) == 0)
			{
				mpz_set_si(factor, 1);
				B1 = INITB1;
				B2 = INITB2;
				curves = INITCURVES;
				continue;
			}
			// Anpassung der Parameter.
			B1 *= 4;
			B2 *= 5;
			curves = (curves * 5) / 2;
		}
	}

	mpz_divexact(cofactor, number, factor);
	str1 = factorize(factor);
	str2 = factorize(cofactor);
	result = (char *) malloc(strlen(str1) + strlen(str2) + 4);
	strcpy(result, str1);
	strcat(result, " * ");
	strcat(result, str2);

	mpz_clear(factor);
	mpz_clear(cofactor);
	return result;
}
示例#3
0
文件: pexpr.c 项目: AllardJ/Tomato
/* Evaluate the expression E modulo MOD and put the result in R.  */
void
mpz_eval_mod_expr (mpz_ptr r, expr_t e, mpz_ptr mod)
{
  mpz_t lhs, rhs;

  switch (e->op)
    {
      case POW:
	mpz_init (lhs); mpz_init (rhs);
	mpz_eval_mod_expr (lhs, e->operands.ops.lhs, mod);
	mpz_eval_expr (rhs, e->operands.ops.rhs);
	mpz_powm (r, lhs, rhs, mod);
	mpz_clear (lhs); mpz_clear (rhs);
	return;
      case PLUS:
	mpz_init (lhs); mpz_init (rhs);
	mpz_eval_mod_expr (lhs, e->operands.ops.lhs, mod);
	mpz_eval_mod_expr (rhs, e->operands.ops.rhs, mod);
	mpz_add (r, lhs, rhs);
	if (mpz_cmp_si (r, 0L) < 0)
	  mpz_add (r, r, mod);
	else if (mpz_cmp (r, mod) >= 0)
	  mpz_sub (r, r, mod);
	mpz_clear (lhs); mpz_clear (rhs);
	return;
      case MINUS:
	mpz_init (lhs); mpz_init (rhs);
	mpz_eval_mod_expr (lhs, e->operands.ops.lhs, mod);
	mpz_eval_mod_expr (rhs, e->operands.ops.rhs, mod);
	mpz_sub (r, lhs, rhs);
	if (mpz_cmp_si (r, 0L) < 0)
	  mpz_add (r, r, mod);
	else if (mpz_cmp (r, mod) >= 0)
	  mpz_sub (r, r, mod);
	mpz_clear (lhs); mpz_clear (rhs);
	return;
      case MULT:
	mpz_init (lhs); mpz_init (rhs);
	mpz_eval_mod_expr (lhs, e->operands.ops.lhs, mod);
	mpz_eval_mod_expr (rhs, e->operands.ops.rhs, mod);
	mpz_mul (r, lhs, rhs);
	mpz_mod (r, r, mod);
	mpz_clear (lhs); mpz_clear (rhs);
	return;
      default:
	mpz_init (lhs);
	mpz_eval_expr (lhs, e);
	mpz_mod (r, lhs, mod);
	mpz_clear (lhs);
	return;
    }
}
示例#4
0
static ZSolveMatrix Matrix2zsolve(Matrix *M)
{
    int i, j;
    ZSolveMatrix zmatrix;

    zmatrix = createMatrix(M->NbColumns-2, M->NbRows);
    for (i = 0; i < M->NbRows; ++i)
	for (j = 0; j < M->NbColumns-2; ++j) {
	    assert(mpz_cmp_si(M->p[i][1+j], -MAXINT) > 0);
	    assert(mpz_cmp_si(M->p[i][1+j], MAXINT) < 0);
	    zmatrix->Data[i*zmatrix->Width+j] = mpz_get_si(M->p[i][1+j]);
	}

    return zmatrix;
}
示例#5
0
文件: ZZ.cpp 项目: DanGrayson/M2
bool RingZZ::from_rational(mpq_ptr q, ring_elem &result) const
{
  bool ok = mpz_cmp_si(mpq_denref(q), 1) == 0;
  if (not ok) return false;
  result = RingZZ::from_int(mpq_numref(q));
  return true;
}
static void
lst_linearized_niter (lst_p lst, mpz_t res)
{
  int i;
  lst_p l;
  mpz_t n;

  mpz_init (n);
  mpz_set_si (res, 0);

  FOR_EACH_VEC_ELT (lst_p, LST_SEQ (lst), i, l)
    if (LST_LOOP_P (l))
      {
	lst_linearized_niter (l, n);
	mpz_add (res, res, n);
      }

  if (LST_LOOP_P (lst))
    {
      lst_niter_for_loop (lst, n);

      if (mpz_cmp_si (res, 0) != 0)
	mpz_mul (res, res, n);
      else
	mpz_set (res, n);
    }

  mpz_clear (n);
}
示例#7
0
/**
 * Recombine les racines modulo mod1 et mod2 pour trouver les racines finales.
 * rac_modi[0]contient les valeurs en x des racines et rac_modi[1] celles en y. 
 * @param rac_mod1 tableau des couples (x, y) de racines modulo mod1
 * @param rac_mod2 tableau des couples (x, y) de racines modulo mod2
 * @param nb1 nombre de racines (x, y) dans rac_mod1
 * @param nb2 nombre de racines (x, y) dans rac_mod2 
 */
void find_roots(mpz_t *rac_mod1[2], mpz_t *rac_mod2[2], int nb1, int nb2,  mpz_t mod1, mpz_t mod2, mpz_t **PY, mpz_t **QY, int *degres_PY, int *degresQY, int deg_P, int deg_Q, mpz_t mod){
  
  int i, j, nb_racines=0;
  mpz_t rx, ry, isroot;
  mpz_inits(rx, ry, isroot, NULL);

  for(i=0; i<nb1; i++){
    for(j=0; j<nb2; j++){

      /* crt sur la i eme racine mod1 et la j eme mod2 */ 
      crt(rx, rac_mod1[0][i], rac_mod2[0][j], mod1, mod2);
      crt(ry, rac_mod1[1][i], rac_mod2[1][j], mod1, mod2);

      /* on evalue en le resultat (rx, ry) trouve */
      eval_bivXY(isroot, rx, ry, PY, degres_PY, deg_P, mod);

      /* on teste si le resultat est bien racine */
      if (!mpz_cmp_si(isroot, 0)){
	printf("(%ld, %ld) est racine\n", mpz_get_si(rx), mpz_get_si(ry));
	nb_racines++;
      }
    }
  }
  
  printf("%d racines au total\n", nb_racines);
  
  
  
}
/* Called when g is supposed to be gcd(a,b), and g = s a + t b, for some t.
   Uses temp1 and temp2 */
static int
gcdext_valid_p (const mpz_t a, const mpz_t b, const mpz_t g, const mpz_t s)
{
  /* It's not clear that gcd(0,0) is well defined, but we allow it and require that
     allow gcd(0,0) = 0. */
  if (mpz_sgn (g) < 0)
    return 0;
  
  if (mpz_sgn (a) == 0)
    {
      /* Must have g == abs (b). Any value for s is in some sense "correct",
	 but it makes sense to require that s == 0. */
      return mpz_cmpabs (g, b) == 0 && mpz_sgn (s) == 0;
    }
  else if (mpz_sgn (b) == 0)
    {
      /* Must have g == abs (a), s == sign (a) */
      return mpz_cmpabs (g, a) == 0 && mpz_cmp_si (s, mpz_sgn (a)) == 0;
    }

  if (mpz_sgn (g) <= 0)
    return 0;

  if (! (mpz_divisible_p (a, g)
	 && mpz_divisible_p (b, g)
	 && mpz_cmpabs (s, b) <= 0))
    return 0;
      
  mpz_mul(temp1, s, a);
  mpz_sub(temp1, g, temp1);
  mpz_tdiv_qr(temp1, temp2, temp1, b);

  return mpz_sgn (temp2) == 0 && mpz_cmpabs (temp1, a) <= 0;
}
示例#9
0
/**
 *  Output the first n best rational approximations of rational q
 */
void cpt_rat_approx(mpq_t *res, mpq_srcptr q, unsigned int *n)
{
	mpz_t pnm1,pnm2,qnm1,qnm2,pn,qn,a,num,den;
	mpz_init_set_si(pnm1,1);
	mpz_init_set_si(pnm2,0);
	mpz_init_set_si(qnm1,0);
	mpz_init_set_si(qnm2,1);
	mpz_init(pn);
	mpz_init(qn);
	mpz_init(a);
	mpz_init(num);
	mpz_init(den);
	mpq_get_num(num,q);
	mpq_get_den(den,q);
	for(unsigned int i = 0; i < *n; i++)
	{
		if(mpz_cmp_si(den,0) == 0)
		{
			*n = i;
			break;
		}
		__rat_approx_step(res[i], num, den, a, pnm2, pnm1, qnm2, qnm1, pn, qn);
	}
	mpz_clear(pnm1);
	mpz_clear(pnm2);
	mpz_clear(qnm1);
	mpz_clear(qnm2);
	mpz_clear(pn);
	mpz_clear(qn);
	mpz_clear(a);
	mpz_clear(num);
	mpz_clear(den);
}
示例#10
0
 void ARingZZGMP::syzygy(const ElementType& a, const ElementType& b,
                      ElementType& x, ElementType& y) const
 {
   M2_ASSERT(!is_zero(b));
   // First check the special cases a = 0, b = 1, -1.  Other cases: use gcd.
   if (is_zero(a))
     {
       set_from_long(x, 1);
       set_zero(y);
       return;
     }
   if (mpz_cmp_ui(&b,1) == 0)
     {
       set_from_long(x, 1);
       negate(y, a);
       return;
     }
   if (mpz_cmp_si(&b,-1) == 0)
     {
       set_from_long(x, 1);
       set(y, a);
       return;
     }
   elem g;
   init(g);
   mpz_gcd(&g,&a,&b);
   divide(y,a,g);
   divide(x,b,g);
   if (mpz_sgn(&x) > 0)
     negate(y,y);
   else
     negate(x,x);
   clear(g);
 }
示例#11
0
文件: factor.c 项目: alexlibi/save
// Das Hauptprogramm.
main(int argc, char *argv[])
{
	if (argc == 1)
	{
		printf("Wo ist die Eingabezahl?\n");
		return 1;
	}

	mpz_t number;
	mpz_init(number);

	if (mpz_set_str(number, argv[1], 10) == -1)
	{
		printf("Ungueltige Eingabe.\n");
		mpz_clear(number);
		return 1;
	}
	if (mpz_cmp_si(number, 2) < 0)
	{
		printf("Natuerliche Zahl > 1 erforderlich.\n");
		mpz_clear(number);
		return 1;
	}

	srand(time(0));
	printf("%s\n", factorize(number));
	mpz_clear(number);
	return 0;
}
示例#12
0
gfc_constructor *
gfc_constructor_lookup (gfc_constructor_base base, int offset)
{
  gfc_constructor *c;
  splay_tree_node node;

  if (!base)
    return NULL;

  node = splay_tree_lookup (base, (splay_tree_key) offset);
  if (node)
    return (gfc_constructor *) node->value;

  /* Check if the previous node has a repeat count big enough to
     cover the offset looked for.  */
  node = splay_tree_predecessor (base, (splay_tree_key) offset);
  if (!node)
    return NULL;

  c = (gfc_constructor *) node->value;
  if (mpz_cmp_si (c->repeat, 1) > 0)
    {
      if (mpz_get_si (c->offset) + mpz_get_si (c->repeat) <= offset)
	c = NULL;
    }
  else
    c = NULL;

  return c;
}
示例#13
0
/**
 *Affiche les racines de P
 */
void print_racines(mpz_t *P, int deg_P, mpz_t mod){
  int i, nb_racines=0;
  mpz_t rac[deg_P+1];
  for(i=0; i<deg_P+1; i++){
    mpz_init_set_str(rac[i], "-1", 10);
  }

  printf("\n\nEtude du Polynome :  ");
  print_P(P, deg_P);
  
  racines(rac, P, deg_P,&nb_racines, mod);

  for(i=0; i<deg_P+1; i++){
    if (!mpz_cmp_si(rac[i],-1)){
      printf("Polynome :  ");
      print_P(P, deg_P);
      printf("%d racine(s)\n", nb_racines);
      
      return;
    }
    printf("%ld est racine\n", mpz_get_si(rac[i]));
  }
  printf("Polynome :  ");
  print_P(P, deg_P);
  printf("%d racines\n", nb_racines);
  
}
示例#14
0
/**
 * Retourne le nombre de coefficient a 0 au debut de P
 */
int nb_zeros(mpz_t *P, int deg_P ){
  int i=0;
  
  while(!mpz_cmp_si(P[i], 0)){
    i++;
  }
  return i;
}
示例#15
0
	int32_t Integer::operator < (const int64_t l) const
	{
#if GMP_LIMB_BITS != 64
		return mpz_cmp_si((mpz_srcptr)&gmp_rep, l) < 0;
#else
		return this->operator < (Integer(l));
#endif
	}
示例#16
0
文件: aring-qq-gmp.hpp 项目: pzinn/M2
 bool lift_to_mpz(mpz_ptr result, const ElementType& a) const {
     if (mpz_cmp_si(mpq_denref(&a), 1) == 0)
     {
         mpz_set(result, mpq_numref(&a));
         return true;
     }
     return false;
 }
示例#17
0
void SecretShare::getShares(mpz_t* shares, mpz_t secret){
	/*mpz_t coefficient, temp;
	mpz_init(coefficient);
	mpz_init(temp);
	int peer; 
	for(peer = 0; peer < peers; peer++)
		mpz_set_ui(shares[peer], 0); 

	for(int degree = 0; degree < threshold+1; degree++){
		if(degree == 0)
			mpz_set(coefficient,secret);

		else{
			mpz_urandomb(coefficient, rstate, bits); 
			if(degree == threshold && mpz_sgn(coefficient) == 0)
				mpz_add_ui(coefficient, coefficient, 1); 
		}

		for(int peer = 0; peer < peers; peer++){
			modMul(temp, sharingMatrix[peer][degree], coefficient);
			modAdd(shares[peer],shares[peer], temp);
		}
	}
	mpz_clear(temp); 
	mpz_clear(coefficient); */
        srand(time(NULL));
	mpz_t coefficient;
	mpz_init(coefficient);
	mpz_t temp;
	mpz_init(temp);
	mpz_set_ui(temp, 0);
	mpz_t random;
	mpz_init(random);
    
	for(int i = 0; i < peers; i++)
        mpz_set_ui(shares[i], 0);
	if(mpz_cmp_si(secret, 0) < 0){
		mpz_mod(secret, secret, fieldSize);
	}
	for(int degree = 0; degree < threshold+1; degree++){
		if(degree == 0)
			mpz_set(coefficient,secret);
		else{
			mpz_urandomm(coefficient, rstate, fieldSize); //
			if(degree == threshold && mpz_sgn(coefficient) == 0)
				mpz_add_ui(coefficient, coefficient, 1); 
			/*mpz_set_ui(temp,rand());
			mpz_set(temp, random);
			mpz_mod(coefficient, temp, fieldSize);
			mpz_add_ui(coefficient, coefficient, 1);*/
		}
		for(int peer = 0; peer < peers; peer++){
			modMul(temp, sharingMatrix[peer][degree], coefficient);
			modAdd(shares[peer],shares[peer], temp);
		}
	}

}
示例#18
0
int
check_si (mpz_t sz, mpz_t oz, long si, long oi, int c)
{
  mpz_t t;
  int fail;

  if (mpz_cmp_si (sz, oi) != c)
    {
      printf ("mpz_cmp_si (sz, %ld) != %i.\n", oi, c);
      printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n");
      abort ();
    }

  if ((si < oi ? -1 : si > oi) != c)
    return 1;

  mpz_init_set_si (t, si);

  if ((fail = mpz_cmp_si (sz, si)) != 0)
    printf ("mpz_cmp_si (sz, %ld) != 0.\n", si);
  if (mpz_cmp_si (oz, si) != -c)
    printf ("mpz_cmp_si (oz, %ld) != %i.\n", si, -c), fail = 1;
  if (! mpz_fits_slong_p (sz))
    printf ("mpz_fits_slong_p (sz) != 1.\n"), fail = 1;
  if (mpz_get_si (sz) != si)
    printf ("mpz_get_si (sz) != %ld.\n", si), fail = 1;
  if (mpz_cmp (t, sz) != 0)
    {
      printf ("mpz_init_set_si (%ld) failed.\n", si);
      printf (" got="); mpz_out_str (stdout, 10, t); printf ("\n");
      fail = 1;
    }

  mpz_clear (t);

  if (fail)
    {
      printf (" sz="); mpz_out_str (stdout, 10, sz); printf ("\n");
      printf (" oz="); mpz_out_str (stdout, 10, oz); printf ("\n");
      printf (" si=%ld\n", si);
      abort ();
    }

  return 0;
}
示例#19
0
 bool set_from_mpq(ElementType& result, const mpq_ptr a) const
 {
   if (mpz_cmp_si(mpq_denref(a), 1) == 0)
     {
       set_from_mpz(result, mpq_numref(a));
       return true;
     }
   return false;
 }
示例#20
0
文件: t-gcd.c 项目: AllardJ/Tomato
/* Called when g is supposed to be gcd(a,b), and g = s a + t b, for some t.
   Uses temp1, temp2 and temp3. */
static int
gcdext_valid_p (const mpz_t a, const mpz_t b, const mpz_t g, const mpz_t s)
{
  /* It's not clear that gcd(0,0) is well defined, but we allow it and require that
     gcd(0,0) = 0. */
  if (mpz_sgn (g) < 0)
    return 0;

  if (mpz_sgn (a) == 0)
    {
      /* Must have g == abs (b). Any value for s is in some sense "correct",
	 but it makes sense to require that s == 0. */
      return mpz_cmpabs (g, b) == 0 && mpz_sgn (s) == 0;
    }
  else if (mpz_sgn (b) == 0)
    {
      /* Must have g == abs (a), s == sign (a) */
      return mpz_cmpabs (g, a) == 0 && mpz_cmp_si (s, mpz_sgn (a)) == 0;
    }

  if (mpz_sgn (g) <= 0)
    return 0;

  mpz_tdiv_qr (temp1, temp3, a, g);
  if (mpz_sgn (temp3) != 0)
    return 0;

  mpz_tdiv_qr (temp2, temp3, b, g);
  if (mpz_sgn (temp3) != 0)
    return 0;

  /* Require that 2 |s| < |b/g|, or |s| == 1. */
  if (mpz_cmpabs_ui (s, 1) > 0)
    {
      mpz_mul_2exp (temp3, s, 1);
      if (mpz_cmpabs (temp3, temp2) >= 0)
	return 0;
    }

  /* Compute the other cofactor. */
  mpz_mul(temp2, s, a);
  mpz_sub(temp2, g, temp2);
  mpz_tdiv_qr(temp2, temp3, temp2, b);

  if (mpz_sgn (temp3) != 0)
    return 0;

  /* Require that 2 |t| < |a/g| or |t| == 1*/
  if (mpz_cmpabs_ui (temp2, 1) > 0)
    {
      mpz_mul_2exp (temp2, temp2, 1);
      if (mpz_cmpabs (temp2, temp1) >= 0)
	return 0;
    }
  return 1;
}
示例#21
0
int
mpc_cmp_si (mpc_t op1, signed long int op2)
{
  unsigned int count;
  
  for (count = 0; count < op1.precision; count++)
    op2 *= 10;

  return mpz_cmp_si (op1.object, op2);
}
示例#22
0
static CBIGINT *_div(CBIGINT *a, CBIGINT *b, bool invert)
{
	if (mpz_cmp_si(b->n, 0) == 0)
	{
		GB.Error(GB_ERR_ZERO);
		return NULL;
	}
	else
		return BIGINT_make(a, b, mpz_tdiv_q);
}
示例#23
0
文件: naif.c 项目: hippunk/Etudes
int first_test(mpz_t n){

	int test = 1;
	int sortie = 0;
	mpz_t nb, max,mod;

       	mpz_init (nb);
       	mpz_init (max);
	mpz_init (mod);

	mpz_root(max, n, 2); //need debug
	//gmp_printf ("\nDebug racine carré : %Zd\n", max);
	//mpz_out_str(stdout, 10, max);

	mpz_set_str(nb,"2",10);
	//gmp_printf ("\nDebug compteur : %Zd\n", nb);

	if(mpz_cmp_si(n,0)== 0 || mpz_cmp_si(n,1)== 0){
		test = 0;
		sortie = 1;
	}

	for(sortie = 0;sortie!=1 && mpz_cmp(nb,max)<=0;mpz_add_ui(nb,nb,1)){

		//printf("test : %i\n", test);
		mpz_mod(mod,n,nb);
		//gmp_printf("Debug tour num %Zd : n = %Zd, nb = %Zd, n mod nb = %Zd\n",nb,n,nb,mod);

		if(mpz_cmp_si(mod,0)== 0){
			//printf("Debug : Sortie\n");
			test = 0;
			sortie = 1;
		}
	}


	mpz_clear(nb);
	mpz_clear(max);
	mpz_clear(mod);
	//printf("test : %i\n", test);
	return test;

}
示例#24
0
文件: intinf.c 项目: Munksgaard/mosml
value largeint_cmp_si(value li, value si)		
{ 
  long res = mpz_cmp_si(Large_val(li), Long_val(si));
  if (res < 0)      
    return Val_long(-1);
  else if (res > 0) 
    return Val_long(1); 
  else 
    return Val_long(0);
}
示例#25
0
文件: number.c 项目: Hmaal/slash
SLVAL
sl_integer_parse(sl_vm_t* vm, uint8_t* str, size_t len)
{
    mpz_t mpz;
    char* buff = sl_alloc(vm->arena, len + 1);
    memcpy(buff, str, len);
    buff[len] = 0;
    char* dec = memchr(buff, '.', len);
    if(dec) {
        *dec = 0;
    }
    mpz_init_set_str(mpz, buff, 10);
    if(mpz_cmp_si(mpz, INT_MIN / 2) > 0 && mpz_cmp_si(mpz, INT_MAX / 2) < 0) {
        SLVAL retn = sl_make_int(vm, mpz_get_si(mpz));
        mpz_clear(mpz);
        return retn;
    } else {
        mpz_clear(mpz);
        return sl_make_bignum_s(vm, buff);
    }
}
示例#26
0
文件: qcn.c 项目: macssh/macssh
double
qcn_estimate (mpz_t d)
{
#define P_LIMIT  132000

  double  h;
  unsigned long  p;

  /* p=2 */
  h = sqrt (-mpz_get_d (d)) / M_PI
    * 2.0 / (2.0 - mpz_kronecker_ui (d, 2));

  if (mpz_cmp_si (d, -3) == 0)       h *= 3;
  else if (mpz_cmp_si (d, -4) == 0)  h *= 2;

  for (p = 3; p < P_LIMIT; p += 2)
    if (prime_p (p))
      h *= (double) p / (double) (p - mpz_kronecker_ui (d, p));

  return h;
}
示例#27
0
int
gfc_expr_is_one (gfc_expr *expr, int def)
{
  gcc_assert (expr != NULL);

  if (expr->expr_type != EXPR_CONSTANT)
    return def;

  if (expr->ts.type != BT_INTEGER)
    return def;

  return mpz_cmp_si (expr->value.integer, 1) == 0;
}
示例#28
0
/*-----------------------------------------------------------------------------*/
void tree_to_ops(tree_t tree, int var_res)
{
  int i;
  char* var[2];
  var[0] = label_var(1);
  var[1] = "tmp";
  
  if(!mpz_cmp_ui(tree->M_red, 1))
  {
    add_operation_str(SET, var[var_res], NULL, label_var(0));   
    if(tree->shift)    
      add_operation_int(SHF, var[var_res], var[var_res], tree->shift);            
  }
  else
  {

    for(i=3; i>0; i--)
      if(tree->node[i] && mpz_cmp_si(tree->node[i]->max_add, -1) != 0) break;
    switch(i)
    {
      case 0:
        tree_to_ops(tree->node[i], var_res);
        add_operation_str(ADD, var[var_res], var[var_res], label_var(0));   
        if(tree->shift)
          add_operation_int(SHF, var[var_res], var[var_res], tree->shift);            
      break;
      case 1:
        tree_to_ops(tree->node[i], var_res);
        add_operation_str(SUB, var[var_res], var[var_res], label_var(0));     
        if(tree->shift)         
          add_operation_int(SHF, var[var_res], var[var_res], tree->shift); 
      break;
      case 2:
        tree_to_ops(tree->node[i], 1-var_res);
        add_operation_str(SET, var[var_res], NULL, var[1-var_res]);
        add_operation_int(SHF, var[1-var_res], var[1-var_res], tree->k);
        add_operation_str(ADD, var[var_res], var[var_res], var[1-var_res]);                
        if(tree->shift)        
          add_operation_int(SHF, var[var_res], var[var_res], tree->shift);
      break;
      case 3:
        tree_to_ops(tree->node[i], 1-var_res);
        add_operation_str(SET, var[var_res], NULL, var[1-var_res]);
        add_operation_int(SHF, var[var_res], var[var_res], tree->k);
        add_operation_str(SUB, var[var_res], var[var_res], var[1-var_res]);                
        if(tree->shift)        
          add_operation_int(SHF, var[var_res], var[var_res], tree->shift); 
      break;    
    }
  }
}
示例#29
0
/**
 *  Compute exponential of x to e mod n
 */
void cpt_exp_mod(mpz_t res, mpz_srcptr x, mpz_srcptr e, mpz_srcptr, n)
{
	if(mpz_cmp_si(e,0) < 0)
	{
		mpz_t me;
		mpz_init(me);
		mpz_neg(me,e);
		cpt_exp_mod(res,x,me,n);
		cpt_inv_mod(res,res,n);
		mpz_clear(me);
		return;
	}
	if(mpz_cmp_si(e,0) == 0)
	{
		mpz_set_si(res,1);
		return;
	}

	/* Get the binary expansion of e */
	const char * bin = mpz_get_str(NULL,2,e);
	unsigned int l = strlen(bin);
	mpz_t y;
	mpz_init_set_si(y,1);
	for(int i = 0; i < l; i++)
	{
		mpz_mul(y,y,2);
		mpz_mod(y,y,n);
		if(bin[i])
		{
			mpz_mul(y,x,y);
			mpz_mod(y,y,n);
		}
	}
	mpz_set(res,y);

	mpz_clear(y);
	free(bin);
}
示例#30
0
 void ARingZZGMP::elem_text_out(buffer &o, 
                             const ElementType& a,
                             bool p_one,
                             bool p_plus, 
                             bool p_parens) const
 {
   char *str;
   
   bool is_neg = (mpz_cmp_si(&a, 0) == -1);
   bool is_one = (mpz_cmp_si(&a, 1) == 0 || mpz_cmp_si(&a, -1) == 0);
   
   if (!is_neg && p_plus) o << '+';
   if (is_one)
     {
       if (is_neg) o << '-';
       if (p_one) o << '1';
     }
   else
     {
       str = mpz_get_str(static_cast<char*>(0), 10, &a);
       o << str;
     }
 }