Пример #1
0
void 
cpx_confluent (cpx_t em, cpx_t a, cpx_t b, cpx_t z, unsigned int prec)
{
	mpf_t fact;
	cpx_t zee, z_n, term;
	cpx_t ay, be, poch_a, poch_b;

	mpf_init (fact);

	cpx_init (zee);
	cpx_init (z_n);
	cpx_init (term);
	cpx_init (ay);
	cpx_init (be);
	cpx_init (poch_a);
	cpx_init (poch_b);

	/* Make copy of arguments now! */
	cpx_set (zee, z);
	cpx_set (z_n, zee);
	
	cpx_set (ay, a);
	cpx_set (poch_a, a);
	cpx_set (be, b);
	cpx_set (poch_b, b);
	
	cpx_set_ui (em, 1, 0);
	mpf_set_ui (fact, 1);

	mpf_t mag;
	mpf_init (mag);
	
	/* Use 10^{-2 * prec} for smallest term in sum */
	mpf_t maxterm;
	mpf_init (maxterm);
	fp_epsilon (maxterm, prec);
	mpf_mul (maxterm, maxterm, maxterm);

	unsigned int n=1;
	while(1)
	{
		cpx_div_mpf (term, z_n, fact);
		cpx_mul (term, term, poch_a);
		cpx_div (term, term, poch_b);
		cpx_add (em, em, term);
		
		/* Don't go no farther than this */
		cpx_mod_sq (mag, term);
		if (mpf_cmp (mag, maxterm) < 0) break;
		
		n++;
		cpx_mul (z_n, z_n, zee);
		mpf_mul_ui (fact, fact, n);

		mpf_add_ui (ay[0].re, ay[0].re, 1);
		mpf_add_ui (be[0].re, be[0].re, 1);
		cpx_mul (poch_a, poch_a, ay);
		cpx_mul (poch_b, poch_b, be);
	}
	
	mpf_clear (fact);
	mpf_clear (mag);
	mpf_clear (maxterm);
	
	cpx_clear (zee);
	cpx_clear (z_n);
	cpx_clear (term);
	cpx_clear (ay);
	cpx_clear (be);
	cpx_clear (poch_a);
	cpx_clear (poch_b);
}
Пример #2
0
bool bbp_pi(bool const & abortTask,
            std::string const & digitIndex,
            uint32_t const digitStep,
            std::string & piSequence)
{
    unsigned long const mantissa_bits = 132;
    unsigned long const count_offset = 7;
    // settings above gives 32 hexadecimal digits
    unsigned long count = static_cast<unsigned long>(
        floor(log10(pow(2.0, static_cast<double>(mantissa_bits)))));
    count -= count_offset;

    // starting digit
    mpz_t digit;
    mpz_init(digit);
    if (mpz_set_str(digit, digitIndex.c_str(), 10) < 0)
        return false;
    mpz_sub_ui(digit, digit, 1); // subtract the 3 digit
    mpz_add_ui(digit, digit, digitStep);
    
    mpz_t tmpI[TEMPORARY_INTEGERS];
    for (size_t i = 0; i < (sizeof(tmpI) / sizeof(mpz_t)); ++i)
        mpz_init(tmpI[i]);
    mpf_t tmpF[TEMPORARY_FLOATS];
    for (size_t i = 0; i < (sizeof(tmpF) / sizeof(mpf_t)); ++i)
        mpf_init2(tmpF[i], mantissa_bits);
    
    // determine epsilon based on the number of digits required
    mpf_t epsilon;
    mpf_init2(epsilon, mantissa_bits);
    mpf_set_ui(epsilon, 10);
    mpf_pow_ui(epsilon, epsilon, count + count_offset);
    mpf_ui_div(epsilon, 1, epsilon);

    // integer constant
    mpz_t sixteen;
    mpz_init(sixteen);
    mpz_set_ui(sixteen, 16);

    // determine the series
    mpf_t s1, s2, s3, s4;
    mpf_init2(s1, mantissa_bits);
    mpf_init2(s2, mantissa_bits);
    mpf_init2(s3, mantissa_bits);
    mpf_init2(s4, mantissa_bits);
    series(abortTask, s1, 1, tmpI, tmpF, sixteen, digit, epsilon);
    if (abortTask)
        return false;
    series(abortTask, s2, 4, tmpI, tmpF, sixteen, digit, epsilon);
    if (abortTask)
        return false;
    series(abortTask, s3, 5, tmpI, tmpF, sixteen, digit, epsilon);
    if (abortTask)
        return false;
    series(abortTask, s4, 6, tmpI, tmpF, sixteen, digit, epsilon);
    if (abortTask)
        return false;

    // pid = 4. * s1 - 2. * s2 - s3 - s4;
    mpf_mul_ui(s1, s1, 4);
    mpf_mul_ui(s2, s2, 2);
    mpf_t result;
    mpf_init2(result, mantissa_bits);
    mpf_sub(result, s1, s2);
    mpf_sub(result, result, s3);
    mpf_sub(result, result, s4);

    // pid = pid - (int) pid + 1.;
    mpf_t & tmp1 = tmpF[0];
    mpf_floor(tmp1, result);
    mpf_sub(result, result, tmp1);
    mpf_add_ui(result, result, 1);
    mpf_abs(result, result);

    // output the result
    char resultStr[256];
    mp_exp_t exponent;
    mpf_get_str(resultStr, &exponent, 16, 254, result);
    resultStr[count + 1] = '\0'; // cut off any erroneous bits
    piSequence.assign(&resultStr[1]);

    // cleanup
    for (size_t i = 0; i < (sizeof(tmpI) / sizeof(mpz_t)); ++i)
        mpz_clear(tmpI[i]);
    for (size_t i = 0; i < (sizeof(tmpF) / sizeof(mpf_t)); ++i)
        mpf_clear(tmpF[i]);
    mpz_clear(digit);
    mpf_clear(epsilon);
    mpz_clear(sixteen);
    mpf_clear(s1);
    mpf_clear(s2);
    mpf_clear(s3);
    mpf_clear(s4);
    mpf_clear(result);
    return true;
}
Пример #3
0
void
mpf_ui_sub (mpf_ptr r, unsigned long int u, mpf_srcptr v)
{
  mp_srcptr up, vp;
  mp_ptr rp, tp;
  mp_size_t usize, vsize, rsize;
  mp_size_t prec;
  mp_exp_t uexp;
  mp_size_t ediff;
  int negate;
  mp_limb_t ulimb;
  TMP_DECL;

  vsize = v->_mp_size;

  /* Handle special cases that don't work in generic code below.  */
  if (u == 0)
    {
      mpf_neg (r, v);
      return;
    }
  if (vsize == 0)
    {
      mpf_set_ui (r, u);
      return;
    }

  /* If signs of U and V are different, perform addition.  */
  if (vsize < 0)
    {
      __mpf_struct v_negated;
      v_negated._mp_size = -vsize;
      v_negated._mp_exp = v->_mp_exp;
      v_negated._mp_d = v->_mp_d;
      mpf_add_ui (r, &v_negated, u);
      return;
    }

  TMP_MARK;

  /* Signs are now known to be the same.  */

  ulimb = u;
  /* Make U be the operand with the largest exponent.  */
  if (1 < v->_mp_exp)
    {
      negate = 1;
      usize = ABS (vsize);
      vsize = 1;
      up = v->_mp_d;
      vp = &ulimb;
      rp = r->_mp_d;
      prec = r->_mp_prec + 1;
      uexp = v->_mp_exp;
      ediff = uexp - 1;
    }
  else
    {
      negate = 0;
      usize = 1;
      vsize = ABS (vsize);
      up = &ulimb;
      vp = v->_mp_d;
      rp = r->_mp_d;
      prec = r->_mp_prec;
      uexp = 1;
      ediff = 1 - v->_mp_exp;
    }

  /* Ignore leading limbs in U and V that are equal.  Doing
     this helps increase the precision of the result.  */
  if (ediff == 0)
    {
      /* This loop normally exits immediately.  Optimize for that.  */
      for (;;)
	{
	  usize--;
	  vsize--;
	  if (up[usize] != vp[vsize])
	    break;
	  uexp--;
	  if (usize == 0)
	    goto Lu0;
	  if (vsize == 0)
	    goto Lv0;
	}
      usize++;
      vsize++;
      /* Note that either operand (but not both operands) might now have
	 leading zero limbs.  It matters only that U is unnormalized if
	 vsize is now zero, and vice versa.  And it is only in that case
	 that we have to adjust uexp.  */
      if (vsize == 0)
      Lv0:
	while (usize != 0 && up[usize - 1] == 0)
	  usize--, uexp--;
      if (usize == 0)
      Lu0:
	while (vsize != 0 && vp[vsize - 1] == 0)
	  vsize--, uexp--;
    }

  /* If U extends beyond PREC, ignore the part that does.  */
  if (usize > prec)
    {
      up += usize - prec;
      usize = prec;
    }

  /* If V extends beyond PREC, ignore the part that does.
     Note that this may make vsize negative.  */
  if (vsize + ediff > prec)
    {
      vp += vsize + ediff - prec;
      vsize = prec - ediff;
    }

  /* Allocate temp space for the result.  Allocate
     just vsize + ediff later???  */
  tp = (mp_ptr) TMP_ALLOC (prec * BYTES_PER_MP_LIMB);

  if (ediff >= prec)
    {
      /* V completely cancelled.  */
      if (tp != up)
	MPN_COPY (rp, up, usize);
      rsize = usize;
    }
  else
    {
      /* Locate the least significant non-zero limb in (the needed
	 parts of) U and V, to simplify the code below.  */
      for (;;)
	{
	  if (vsize == 0)
	    {
	      MPN_COPY (rp, up, usize);
	      rsize = usize;
	      goto done;
	    }
	  if (vp[0] != 0)
	    break;
	  vp++, vsize--;
	}
      for (;;)
	{
	  if (usize == 0)
	    {
	      MPN_COPY (rp, vp, vsize);
	      rsize = vsize;
	      negate ^= 1;
	      goto done;
	    }
	  if (up[0] != 0)
	    break;
	  up++, usize--;
	}

      /* uuuu     |  uuuu     |  uuuu     |  uuuu     |  uuuu    */
      /* vvvvvvv  |  vv       |    vvvvv  |    v      |       vv */

      if (usize > ediff)
	{
	  /* U and V partially overlaps.  */
	  if (ediff == 0)
	    {
	      /* Have to compare the leading limbs of u and v
		 to determine whether to compute u - v or v - u.  */
	      if (usize > vsize)
		{
		  /* uuuu     */
		  /* vv       */
		  int cmp;
		  cmp = mpn_cmp (up + usize - vsize, vp, vsize);
		  if (cmp >= 0)
		    {
		      mp_size_t size;
		      size = usize - vsize;
		      MPN_COPY (tp, up, size);
		      mpn_sub_n (tp + size, up + size, vp, vsize);
		      rsize = usize;
		    }
		  else
		    {
		      /* vv       */  /* Swap U and V. */
		      /* uuuu     */
		      mp_size_t size, i;
		      size = usize - vsize;
		      tp[0] = -up[0] & GMP_NUMB_MASK;
		      for (i = 1; i < size; i++)
			tp[i] = ~up[i] & GMP_NUMB_MASK;
		      mpn_sub_n (tp + size, vp, up + size, vsize);
		      mpn_sub_1 (tp + size, tp + size, vsize, (mp_limb_t) 1);
		      negate ^= 1;
		      rsize = usize;
		    }
		}
	      else if (usize < vsize)
		{
		  /* uuuu     */
		  /* vvvvvvv  */
		  int cmp;
		  cmp = mpn_cmp (up, vp + vsize - usize, usize);
		  if (cmp > 0)
		    {
		      mp_size_t size, i;
		      size = vsize - usize;
		      tp[0] = -vp[0] & GMP_NUMB_MASK;
		      for (i = 1; i < size; i++)
			tp[i] = ~vp[i] & GMP_NUMB_MASK;
		      mpn_sub_n (tp + size, up, vp + size, usize);
		      mpn_sub_1 (tp + size, tp + size, usize, (mp_limb_t) 1);
		      rsize = vsize;
		    }
		  else
		    {
		      /* vvvvvvv  */  /* Swap U and V. */
		      /* uuuu     */
		      /* This is the only place we can get 0.0.  */
		      mp_size_t size;
		      size = vsize - usize;
		      MPN_COPY (tp, vp, size);
		      mpn_sub_n (tp + size, vp + size, up, usize);
		      negate ^= 1;
		      rsize = vsize;
		    }
		}
	      else
		{
		  /* uuuu     */
		  /* vvvv     */
		  int cmp;
		  cmp = mpn_cmp (up, vp + vsize - usize, usize);
		  if (cmp > 0)
		    {
		      mpn_sub_n (tp, up, vp, usize);
		      rsize = usize;
		    }
		  else
		    {
		      mpn_sub_n (tp, vp, up, usize);
		      negate ^= 1;
		      rsize = usize;
		      /* can give zero */
		    }
		}
	    }
	  else
	    {
	      if (vsize + ediff <= usize)
		{
		  /* uuuu     */
		  /*   v      */
		  mp_size_t size;
		  size = usize - ediff - vsize;
		  MPN_COPY (tp, up, size);
		  mpn_sub (tp + size, up + size, usize - size, vp, vsize);
		  rsize = usize;
		}
	      else
		{
		  /* uuuu     */
		  /*   vvvvv  */
		  mp_size_t size, i;
		  size = vsize + ediff - usize;
		  tp[0] = -vp[0] & GMP_NUMB_MASK;
		  for (i = 1; i < size; i++)
		    tp[i] = ~vp[i] & GMP_NUMB_MASK;
		  mpn_sub (tp + size, up, usize, vp + size, usize - ediff);
		  mpn_sub_1 (tp + size, tp + size, usize, (mp_limb_t) 1);
		  rsize = vsize + ediff;
		}
	    }
	}
      else
	{
	  /* uuuu     */
	  /*      vv  */
	  mp_size_t size, i;
	  size = vsize + ediff - usize;
	  tp[0] = -vp[0] & GMP_NUMB_MASK;
	  for (i = 1; i < vsize; i++)
	    tp[i] = ~vp[i] & GMP_NUMB_MASK;
	  for (i = vsize; i < size; i++)
	    tp[i] = GMP_NUMB_MAX;
	  mpn_sub_1 (tp + size, up, usize, (mp_limb_t) 1);
	  rsize = size + usize;
	}

      /* Full normalize.  Optimize later.  */
      while (rsize != 0 && tp[rsize - 1] == 0)
	{
	  rsize--;
	  uexp--;
	}
      MPN_COPY (rp, tp, rsize);
    }

 done:
  r->_mp_size = negate ? -rsize : rsize;
  r->_mp_exp = uexp;
  TMP_FREE;
}
Пример #4
0
int
main (void)
{
  mpf_t       f, f0p5;
  int         got;
  const char  *expr;
  int         error = 0;

  tests_start ();
  mpf_init2 (f, 200L);
  mpf_init2 (f0p5, 200L);

  /* 0.5 */
  mpf_set_ui (f0p5, 1L);
  mpf_div_2exp (f0p5, f0p5, 1L);

  mpf_set_ui (f, 0L);
  expr = "0";
  EXPECT (mpf_fits_ulong_p, 1);
  EXPECT (mpf_fits_uint_p, 1);
  EXPECT (mpf_fits_ushort_p, 1);
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);
  EXPECT (mpf_fits_sshort_p, 1);

  mpf_set_ui (f, 1L);
  expr = "1";
  EXPECT (mpf_fits_ulong_p, 1);
  EXPECT (mpf_fits_uint_p, 1);
  EXPECT (mpf_fits_ushort_p, 1);
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);
  EXPECT (mpf_fits_sshort_p, 1);

  mpf_set_si (f, -1L);
  expr = "-1";
  EXPECT (mpf_fits_ulong_p, 0);
  EXPECT (mpf_fits_uint_p, 0);
  EXPECT (mpf_fits_ushort_p, 0);
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);
  EXPECT (mpf_fits_sshort_p, 1);


  mpf_set_ui (f, (unsigned long) USHRT_MAX);
  expr = "USHRT_MAX";
  EXPECT (mpf_fits_ulong_p, 1);
  EXPECT (mpf_fits_uint_p, 1);
  EXPECT (mpf_fits_ushort_p, 1);

  mpf_set_ui (f, (unsigned long) USHRT_MAX);
  mpf_add (f, f, f0p5);
  expr = "USHRT_MAX + 0.5";
  EXPECT (mpf_fits_ulong_p, 1);
  EXPECT (mpf_fits_uint_p, 1);
  EXPECT (mpf_fits_ushort_p, 1);

  mpf_set_ui (f, (unsigned long) USHRT_MAX);
  mpf_add_ui (f, f, 1L);
  expr = "USHRT_MAX + 1";
  EXPECT (mpf_fits_ushort_p, 0);


  mpf_set_ui (f, (unsigned long) UINT_MAX);
  expr = "UINT_MAX";
  EXPECT (mpf_fits_ulong_p, 1);
  EXPECT (mpf_fits_uint_p, 1);

  mpf_set_ui (f, (unsigned long) UINT_MAX);
  mpf_add (f, f, f0p5);
  expr = "UINT_MAX + 0.5";
  EXPECT (mpf_fits_ulong_p, 1);
  EXPECT (mpf_fits_uint_p, 1);

  mpf_set_ui (f, (unsigned long) UINT_MAX);
  mpf_add_ui (f, f, 1L);
  expr = "UINT_MAX + 1";
  EXPECT (mpf_fits_uint_p, 0);


  mpf_set_ui (f, ULONG_MAX);
  expr = "ULONG_MAX";
  EXPECT (mpf_fits_ulong_p, 1);

  mpf_set_ui (f, ULONG_MAX);
  mpf_add (f, f, f0p5);
  expr = "ULONG_MAX + 0.5";
  EXPECT (mpf_fits_ulong_p, 1);

  mpf_set_ui (f, ULONG_MAX);
  mpf_add_ui (f, f, 1L);
  expr = "ULONG_MAX + 1";
  EXPECT (mpf_fits_ulong_p, 0);


  mpf_set_si (f, (long) SHRT_MAX);
  expr = "SHRT_MAX";
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);
  EXPECT (mpf_fits_sshort_p, 1);

  mpf_set_si (f, (long) SHRT_MAX);
  expr = "SHRT_MAX + 0.5";
  mpf_add (f, f, f0p5);
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);
  EXPECT (mpf_fits_sshort_p, 1);

  mpf_set_si (f, (long) SHRT_MAX);
  mpf_add_ui (f, f, 1L);
  expr = "SHRT_MAX + 1";
  EXPECT (mpf_fits_sshort_p, 0);


  mpf_set_si (f, (long) INT_MAX);
  expr = "INT_MAX";
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);

  mpf_set_si (f, (long) INT_MAX);
  mpf_add (f, f, f0p5);
  expr = "INT_MAX + 0.5";
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);

  mpf_set_si (f, (long) INT_MAX);
  mpf_add_ui (f, f, 1L);
  expr = "INT_MAX + 1";
  EXPECT (mpf_fits_sint_p, 0);


  mpf_set_si (f, LONG_MAX);
  expr = "LONG_MAX";
  EXPECT (mpf_fits_slong_p, 1);

  mpf_set_si (f, LONG_MAX);
  mpf_add (f, f, f0p5);
  expr = "LONG_MAX + 0.5";
  EXPECT (mpf_fits_slong_p, 1);

  mpf_set_si (f, LONG_MAX);
  mpf_add_ui (f, f, 1L);
  expr = "LONG_MAX + 1";
  EXPECT (mpf_fits_slong_p, 0);


  mpf_set_si (f, (long) SHRT_MIN);
  expr = "SHRT_MIN";
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);
  EXPECT (mpf_fits_sshort_p, 1);

  mpf_set_si (f, (long) SHRT_MIN);
  mpf_sub (f, f, f0p5);
  expr = "SHRT_MIN - 0.5";
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);
  EXPECT (mpf_fits_sshort_p, 1);

  mpf_set_si (f, (long) SHRT_MIN);
  mpf_sub_ui (f, f, 1L);
  expr = "SHRT_MIN + 1";
  EXPECT (mpf_fits_sshort_p, 0);


  mpf_set_si (f, (long) INT_MIN);
  expr = "INT_MIN";
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);

  mpf_set_si (f, (long) INT_MIN);
  mpf_sub (f, f, f0p5);
  expr = "INT_MIN - 0.5";
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);

  mpf_set_si (f, (long) INT_MIN);
  mpf_sub_ui (f, f, 1L);
  expr = "INT_MIN + 1";
  EXPECT (mpf_fits_sint_p, 0);


  mpf_set_si (f, LONG_MIN);
  expr = "LONG_MIN";
  EXPECT (mpf_fits_slong_p, 1);

  mpf_set_si (f, LONG_MIN);
  mpf_sub (f, f, f0p5);
  expr = "LONG_MIN - 0.5";
  EXPECT (mpf_fits_slong_p, 1);

  mpf_set_si (f, LONG_MIN);
  mpf_sub_ui (f, f, 1L);
  expr = "LONG_MIN + 1";
  EXPECT (mpf_fits_slong_p, 0);


  mpf_set_str_or_abort (f, "0.5", 10);
  expr = "0.5";
  EXPECT (mpf_fits_ulong_p, 1);
  EXPECT (mpf_fits_uint_p, 1);
  EXPECT (mpf_fits_ushort_p, 1);
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);
  EXPECT (mpf_fits_sshort_p, 1);

  mpf_set_str_or_abort (f, "-0.5", 10);
  expr = "-0.5";
  EXPECT (mpf_fits_ulong_p, 0);
  EXPECT (mpf_fits_uint_p, 0);
  EXPECT (mpf_fits_ushort_p, 0);
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);
  EXPECT (mpf_fits_sshort_p, 1);


  mpf_set_str_or_abort (f, "1.000000000000000000000000000000000001", 16);
  expr = "1.000000000000000000000000000000000001 base 16";
  EXPECT (mpf_fits_ulong_p, 1);
  EXPECT (mpf_fits_uint_p, 1);
  EXPECT (mpf_fits_ushort_p, 1);
  EXPECT (mpf_fits_slong_p, 1);
  EXPECT (mpf_fits_sint_p, 1);
  EXPECT (mpf_fits_sshort_p, 1);

  mpf_set_str_or_abort (f, "1@1000", 16);
  expr = "1@1000 base 16";
  EXPECT (mpf_fits_ulong_p, 0);
  EXPECT (mpf_fits_uint_p, 0);
  EXPECT (mpf_fits_ushort_p, 0);
  EXPECT (mpf_fits_slong_p, 0);
  EXPECT (mpf_fits_sint_p, 0);
  EXPECT (mpf_fits_sshort_p, 0);


  mpf_set_ui (f, 1L);
  mpf_mul_2exp (f, f, BITS_PER_ULONG + 1);
  mpf_sub_ui (f, f, 1L);
  expr = "2^(BITS_PER_ULONG+1) - 1";
  EXPECT (mpf_fits_ulong_p, 0);
  EXPECT (mpf_fits_uint_p, 0);
  EXPECT (mpf_fits_ushort_p, 0);
  EXPECT (mpf_fits_slong_p, 0);
  EXPECT (mpf_fits_sint_p, 0);
  EXPECT (mpf_fits_sshort_p, 0);

  mpf_set_ui (f, 1L);
  mpf_mul_2exp (f, f, BITS_PER_ULONG + 1);
  mpf_sub_ui (f, f, 1L);
  mpf_neg (f, f);
  expr = "- (2^(BITS_PER_ULONG+1) - 1)";
  EXPECT (mpf_fits_ulong_p, 0);
  EXPECT (mpf_fits_uint_p, 0);
  EXPECT (mpf_fits_ushort_p, 0);
  EXPECT (mpf_fits_slong_p, 0);
  EXPECT (mpf_fits_sint_p, 0);
  EXPECT (mpf_fits_sshort_p, 0);

  mpf_set_ui (f, 1L);
  mpf_mul_2exp (f, f, BITS_PER_ULONG + 5);
  mpf_sub_ui (f, f, 1L);
  expr = "2^(BITS_PER_ULONG+5) - 1";
  EXPECT (mpf_fits_ulong_p, 0);
  EXPECT (mpf_fits_uint_p, 0);
  EXPECT (mpf_fits_ushort_p, 0);
  EXPECT (mpf_fits_slong_p, 0);
  EXPECT (mpf_fits_sint_p, 0);
  EXPECT (mpf_fits_sshort_p, 0);


  if (error)
    abort ();

  mpf_clear (f);
  mpf_clear (f0p5);
  tests_end ();
  exit (0);
}
Пример #5
0
long int julia(const mpf_t x, const mpf_t xr, long int xres, const mpf_t y, const mpf_t yr, long int yres, mpf_t *c, int flag, long int max_iteration,
	  float *iterations, int my_rank, int p, MPI_Comm comm)
{
	double t0 = MPI_Wtime();
	int i,j;

	// Find how many rows per process.
	int *rows;
	rows = (int*)malloc(sizeof(int)*p);
	for (i=0; i < p; i++)
		rows[i] = yres/p;
	for (i=0; i < yres % p; i++)
		rows[i]++;

	//allocate memory for each processor
	if(my_rank > 0){
		iterations = (float*)malloc( sizeof(float) * xres * rows[my_rank]);
		assert(iterations);
	}

	//------------julia gmp
	const double maxRadius = 4.0;

	mpf_t xi, yi, x_min, x_max, y_min, y_max, savex, savex2, savey, radius, xgap, ygap, savex_a, savex_b, savey_a, savey_b, tmp, tmp1;
	mpf_init(xi);
	mpf_init(yi);
	mpf_init(x_min);
	mpf_init(x_max);
	mpf_init(y_min);
	mpf_init(y_max);
	mpf_init(savex);
	mpf_init(savex2);
	mpf_init(savey);
	mpf_init(radius);
	mpf_init(xgap);
	mpf_init(ygap);
	mpf_init(savex_a);
	mpf_init(savex_b);
	mpf_init(savey_a);
	mpf_init(savey_b);

	mpf_init(tmp);
	mpf_init(tmp1);
	//double x_min = x - xr;
	mpf_sub(x_min, x, xr);
	//double x_max = x + xr;
	mpf_add(x_max, x, xr);
	//double y_min = y - yr;
	mpf_sub(y_min, y, yr);
	//double y_max = y + yr;
	mpf_add(y_max, y, yr);

	// spaceing between x and y points
	//double xgap = (x_max - x_min) / xres;
	mpf_sub(xgap, x_max, x_min);
	mpf_div_ui(xgap, xgap, xres);

	//double ygap = (y_max - y_min) / yres;
	mpf_sub(ygap, y_max, y_min);
	mpf_div_ui(ygap, ygap, yres);



	//----------------------------
	long long int iteration;
	long long int total_number_iterations = 0;	
	int k = 0;

	
	for (j = my_rank; j < yres; j+=p){
		if(my_rank==0)
			k = j;	//needed for root

		for (i = 0; i < xres; i++){
			//xi = x_min + i * xgap;
			mpf_mul_ui(tmp, xgap, i);
			mpf_add(xi, x_min, tmp);

			//yi = y_min + j * ygap;
			mpf_mul_ui(tmp, ygap, j);
			mpf_add(yi, y_min, tmp);

			//flag betwee[n julia or mandelbrot
			//savex = flag * c[0] + (1 - flag) * xi;
			mpf_mul_ui(savex_a, c[0], flag);
			mpf_mul_ui(savex_b, xi, (1-flag));
			mpf_add(savex, savex_a, savex_b);

			//savey = flag * c[1] + (1 - flag) * yi;
			mpf_mul_ui(savey_a, c[1], flag);
			mpf_mul_ui(savey_b, yi, (1-flag));
			mpf_add(savey, savey_a, savey_b);

			//radius = 0;
			mpf_set_ui(radius, 0);

			iteration = 0;
			
			//while ((radius <= maxRadius) && (iteration < max_iteration)){
			while ((mpf_cmp_d(radius, maxRadius)<=0) && (iteration < max_iteration)){
				//savex2 = xi;
				mpf_add_ui(savex2, xi, 0);

				//xi = xi * xi - yi * yi + savex;
				mpf_mul(xi, xi, xi);
				mpf_mul(tmp, yi, yi);
				mpf_sub(xi, xi, tmp);
				mpf_add(xi, xi, savex);

				//yi = 2.0f * savex2 * yi + savey;
				mpf_mul_ui(tmp, savex2, 2);
				mpf_mul(yi, yi, tmp);
				mpf_add(yi, yi, savey);

				//radius = xi * xi + yi * yi;
				mpf_mul(tmp, xi, xi);
				mpf_mul(tmp1, yi, yi);
				mpf_add(radius, tmp, tmp1);

				iteration++;
			}

			total_number_iterations += iteration;

			float *p = iterations + k*xres + i;

			//if (radius > maxRadius){
			if (mpf_cmp_d(radius, maxRadius)>0){
				//float zn = sqrt(xi*xi + yi*yi);
				mpf_t zn;
				mpf_init(zn);
				mpf_mul(tmp, xi, xi);
				mpf_mul(tmp1, yi, yi);
				mpf_add(zn, tmp, tmp1);
				mpf_sqrt(zn, zn);
				double n = mpf_get_d(zn);
				//float nu = log(log(zn) / log(2))/log(2);
				double nu = log(log(n) / log(2))/log(2);


				//the point has escaped at iteration at any of the iterations 0,1,2,3...
				*p = iteration + 1 - nu;
			}
			else
			// zij stays within the region up to max_iteration
			{
				assert(iteration==max_iteration);
				*p = -1;
			}
		}
		k++;
	}
	
	//collect various data
	MPI_Status status;
	if(my_rank == 0){
		int i,j;
		for(i = 1; i < p; i++){
			for(j = 0; j < rows[i]; j++){
				MPI_Recv((iterations + (i + j * p) * xres), xres, MPI_FLOAT, i, 0, comm, &status);
				//MPI_Irecv((iterations + (i + j * p) * xres), xres, MPI_FLOAT, i, 0, comm, NULL);
			}
		}
	}
	else{
		int i;
		for(i = 0; i < rows[my_rank]; i++)
			MPI_Send((iterations + i *xres), xres, MPI_FLOAT, 0, 0, comm);
	}
	

	//reduce max iteration count
	long long int total_reduced_iterations = -1;
	//printf("rank: %i, total_reduced_iterations: %i\n", my_rank, total_number_iterations);
	MPI_Reduce(&total_number_iterations, &total_reduced_iterations, 1, MPI_LONG_LONG_INT, MPI_SUM, 0, comm);

	double t4 = MPI_Wtime();
	
	double max_reduced_time = -1;
	double total_time = t4 - t0;
	MPI_Reduce(&total_time, &max_reduced_time, 1, MPI_DOUBLE, MPI_MAX, 0, comm);
	
	if(my_rank == 0){
		printf("np: %i, time: %f , iterations: %lld\n",p, max_reduced_time, total_reduced_iterations);
		//printf("%i\t%.2e\n", p, max_reduced_time);
	}
	//clear

	//printf("proc: %i, total time: %lf sec, init: %lf sec, calc: %lf sec, collect: %lf\n", my_rank, t4-t0, t1-t0, t2-t1, t3-t2);
  	return total_reduced_iterations;
}
Пример #6
0
void
check_rand (int argc, char **argv)
{
    mp_size_t size;
    mp_exp_t exp;
    int reps = 20000;
    int i;
    mpf_t u, v, w, wref;
    mp_size_t bprec = 100;
    mpf_t rerr, max_rerr, limit_rerr;
    gmp_randstate_t rands;

    if (argc > 1)
    {
        reps = strtol (argv[1], 0, 0);
        if (argc > 2)
            bprec = strtol (argv[2], 0, 0);
    }

    mpf_set_default_prec (bprec);
    gmp_randinit_default(rands);
    mpf_init_set_ui (limit_rerr, 1);
    mpf_div_2exp (limit_rerr, limit_rerr, bprec);
#if VERBOSE
    mpf_dump (limit_rerr);
#endif
    mpf_init (rerr);
    mpf_init_set_ui (max_rerr, 0);

    mpf_init (u);
    mpf_init (v);
    mpf_init (w);
    mpf_init (wref);
    for (i = 0; i < reps; i++)
    {
        size = urandom (rands) % (2 * SIZE) - SIZE;
        exp = urandom (rands) % SIZE;
        mpf_rrandomb (u, rands, size, exp);

        size = urandom (rands) % (2 * SIZE) - SIZE;
        exp = urandom (rands) % SIZE;
        mpf_rrandomb (v, rands, size, exp);

        if ((urandom (rands) & 1) != 0)
            mpf_add_ui (u, v, 1);
        else if ((urandom (rands) & 1) != 0)
            mpf_sub_ui (u, v, 1);

        mpf_sub (w, u, v);
        refmpf_sub (wref, u, v);

        mpf_reldiff (rerr, w, wref);
        if (mpf_cmp (rerr, max_rerr) > 0)
        {
            mpf_set (max_rerr, rerr);
#if VERBOSE
            mpf_dump (max_rerr);
#endif
            if (mpf_cmp (rerr, limit_rerr) > 0)
            {
                printf ("ERROR after %d tests\n", i);
                printf ("   u = ");
                mpf_dump (u);
                printf ("   v = ");
                mpf_dump (v);
                printf ("wref = ");
                mpf_dump (wref);
                printf ("   w = ");
                mpf_dump (w);
                abort ();
            }
        }
    }

    mpf_clear (limit_rerr);
    mpf_clear (rerr);
    mpf_clear (max_rerr);

    mpf_clear (u);
    mpf_clear (v);
    mpf_clear (w);
    mpf_clear (wref);
    gmp_randclear(rands);
}
int main() {
	pthread_t thread_a, thread_b; /* My threads*/
	int i;
	FILE *filePi, *fileTime;
	clock_t start, end;
	double cpu_time_used;
	mpf_set_default_prec(BITS_PER_DIGIT * 11000000);	
	
	/* Borwein Variable Initialization */   	
	for(i=0; i<2; i++)
    		for(j=0; j<2; j++)
    			mpf_init(params[i][j]);
    	
	mpf_init(real_pi);
	mpf_init(y0Aux);
	mpf_init(y0Aux2);
	mpf_init(a0Aux);
	mpf_init(a0Aux2);
	mpf_init(pi[0]);
    	mpf_init(pi[1]);	
	
	mpf_init_set_str(error, "1e-10000000", 10);

	/* Initial value setting */
	mpf_sqrt_ui(params[A][0], 2.0); /* a0 = sqrt(2)*/
	mpf_mul_ui(params[A][0], params[A][0], 4.0); /* a0 = 4 * sqrt(2) */
	mpf_ui_sub(params[A][0], 6.0, params[A][0]); /* a0 = 6 - 4 * sqrt(2) */ 
	
	mpf_sqrt_ui(params[Y][0], 2.0); /* y0 = sqrt(2) */
	mpf_sub_ui(params[Y][0], params[Y][0], 1.0); /* y0 = sqrt(2) - 1 */
	
	mpf_set_ui(pi[0], 0);
	mpf_set_ui(pi[1], 0);
	i = 1; 
	j = 1;
	iteracoes = 0;
	x = 0;

	/* Load the reals digits of pi */
	filePi = fopen("pi.txt", "r");
	gmp_fscanf(filePi, "%Ff", real_pi); 
	fclose(filePi);
	
	start = clock();

	while(1){
		/* y = ( 1 - (1 - y0 ^ 4) ^ 0.25 ) / ( 1 + ( 1 - y0 ^ 4) ^ 0.25 ) */
		mpf_pow_ui(y0Aux, params[Y][0], 4); 
		mpf_ui_sub(y0Aux, 1.0, y0Aux);
		mpf_sqrt(y0Aux, y0Aux);
                mpf_sqrt(y0Aux, y0Aux);

		mpf_add_ui(y0Aux2, y0Aux, 1.0);
		mpf_ui_sub(y0Aux, 1.0, y0Aux);
		
		mpf_div(params[Y][1], y0Aux, y0Aux2);

		/* a = a0 * ( 1 + params[Y][1] ) ^ 4 - 2 ^ ( 2 * i + 3 ) * params[Y][1] * ( 1 + params[Y][1] + params[Y][1] ^ 2 ) */
		/* Threads creation */                
		pthread_create(&thread_a, NULL, calc_a, NULL);
        	pthread_create(&thread_b, NULL, calc_b, NULL);  		

		pthread_join(thread_a, NULL);
                pthread_join(thread_b, NULL);
		
		/* 2 ^ ( 2 * i + 3 ) * params[Y][1] * ( 1 + params[Y][1] + params[Y][1] ^ 2 ) */	
		mpf_mul(a0Aux, a0Aux, a0Aux2);

		/*a0 * ( 1 + params[Y][1] ) ^ 4*/
                mpf_add_ui(a0Aux2, params[Y][1], 1);
                mpf_pow_ui(a0Aux2, a0Aux2, 4);
                mpf_mul(a0Aux2, params[A][0], a0Aux2);
	
		/* form the entire expression */
                mpf_sub(params[A][1], a0Aux2, a0Aux);
	
                mpf_set(params[A][0], params[A][1]);
                mpf_set(params[Y][0], params[Y][1]);

                mpf_ui_div(pi[j], 1, params[A][0]);
		gmp_printf("\nIteracao %d  | pi = %.25Ff", iteracoes, pi[j]);
		
		/* Calculate the error */
		mpf_sub(pi[(j+1)%2], real_pi, pi[j]);
		mpf_abs(pi[(j+1) % 2], pi[(j+1) % 2]);

		if(mpf_cmp(pi[(j+1)%2], error) < 0){
			printf("\n%d iteracoes para alcancar 10 milhoes de digitos de corretos.", iteracoes);			
			break;
		}

		j = (j+1) % 2;
		
		iteracoes++;
		i++;
	}

	end = clock();
    	cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

	fileTime = fopen("execution_time.txt", "w");
	fprintf(fileTime, "Execution time: %f\n", cpu_time_used);
	fclose(fileTime);
	
	/* Clean up*/
    	for(i=0; i<2; i++)
    		for(j=0; j<2; j++)
    			mpf_clear(params[i][j]);
    	
	mpf_clear(pi[0]);
    	mpf_clear(pi[1]);
	mpf_clear(real_pi);
	mpf_clear(error);

	return 0;
}
Пример #8
0
void
check_various (void)
{
  mpf_t  src, trunc, ceil, floor;
  int    n, i;

  mpf_init2 (src, 512L);
  mpf_init2 (trunc, 256L);
  mpf_init2 (ceil,  256L);
  mpf_init2 (floor, 256L);

  /* 0 */
  mpf_set_ui (src, 0L);
  mpf_set_ui (trunc, 0L);
  mpf_set_ui (ceil, 0L);
  mpf_set_ui (floor, 0L);
  check_all (src, trunc, ceil, floor);

  /* 1 */
  mpf_set_ui (src, 1L);
  mpf_set_ui (trunc, 1L);
  mpf_set_ui (ceil, 1L);
  mpf_set_ui (floor, 1L);
  check_all (src, trunc, ceil, floor);

  /* 2^1024 */
  mpf_set_ui (src, 1L);
  mpf_mul_2exp (src,   src,   1024L);
  mpf_set (trunc, src);
  mpf_set (ceil,  src);
  mpf_set (floor, src);
  check_all (src, trunc, ceil, floor);

  /* 1/2^1024, fraction only */
  mpf_set_ui (src, 1L);
  mpf_div_2exp (src,  src, 1024L);
  mpf_set_si (trunc, 0L);
  mpf_set_si (ceil, 1L);
  mpf_set_si (floor, 0L);
  check_all (src, trunc, ceil, floor);

  /* 1/2 */
  mpf_set_ui (src, 1L);
  mpf_div_2exp (src,  src, 1L);
  mpf_set_si (trunc, 0L);
  mpf_set_si (ceil, 1L);
  mpf_set_si (floor, 0L);
  check_all (src, trunc, ceil, floor);

  /* 123+1/2^64 */
  mpf_set_ui (src, 1L);
  mpf_div_2exp (src,  src, 64L);
  mpf_add_ui (src,  src, 123L);
  mpf_set_si (trunc, 123L);
  mpf_set_si (ceil, 124L);
  mpf_set_si (floor, 123L);
  check_all (src, trunc, ceil, floor);

  /* integer of full prec+1 limbs, unchanged */
  n = PREC(trunc)+1;
  ASSERT_ALWAYS (n <= PREC(src)+1);
  EXP(src) = n;
  SIZ(src) = n;
  for (i = 0; i < SIZ(src); i++)
    PTR(src)[i] = i+100;
  mpf_set (trunc, src);
  mpf_set (ceil, src);
  mpf_set (floor, src);
  check_all (src, trunc, ceil, floor);

  /* full prec+1 limbs, 1 trimmed for integer */
  n = PREC(trunc)+1;
  ASSERT_ALWAYS (n <= PREC(src)+1);
  EXP(src) = n-1;
  SIZ(src) = n;
  for (i = 0; i < SIZ(src); i++)
    PTR(src)[i] = i+200;
  EXP(trunc) = n-1;
  SIZ(trunc) = n-1;
  for (i = 0; i < SIZ(trunc); i++)
    PTR(trunc)[i] = i+201;
  mpf_set (floor, trunc);
  mpf_add_ui (ceil, trunc, 1L);
  check_all (src, trunc, ceil, floor);

  /* prec+3 limbs, 2 trimmed for size */
  n = PREC(trunc)+3;
  ASSERT_ALWAYS (n <= PREC(src)+1);
  EXP(src) = n;
  SIZ(src) = n;
  for (i = 0; i < SIZ(src); i++)
    PTR(src)[i] = i+300;
  EXP(trunc) = n;
  SIZ(trunc) = n-2;
  for (i = 0; i < SIZ(trunc); i++)
    PTR(trunc)[i] = i+302;
  mpf_set (floor, trunc);
  mpf_set (ceil, trunc);
  PTR(ceil)[0]++;
  check_all (src, trunc, ceil, floor);

  /* prec+4 limbs, 2 trimmed for size, 1 trimmed for integer */
  n = PREC(trunc)+4;
  ASSERT_ALWAYS (n <= PREC(src)+1);
  EXP(src) = n-1;
  SIZ(src) = n;
  for (i = 0; i < SIZ(src); i++)
    PTR(src)[i] = i+400;
  EXP(trunc) = n-1;
  SIZ(trunc) = n-3;
  for (i = 0; i < SIZ(trunc); i++)
    PTR(trunc)[i] = i+403;
  mpf_set (floor, trunc);
  mpf_set (ceil, trunc);
  PTR(ceil)[0]++;
  check_all (src, trunc, ceil, floor);

  /* F.F, carry out of ceil */
  EXP(src) = 1;
  SIZ(src) = 2;
  PTR(src)[0] = GMP_NUMB_MAX;
  PTR(src)[1] = GMP_NUMB_MAX;
  EXP(trunc) = 1;
  SIZ(trunc) = 1;
  PTR(trunc)[0] = GMP_NUMB_MAX;
  mpf_set (floor, trunc);
  EXP(ceil) = 2;
  SIZ(ceil) = 1;
  PTR(ceil)[0] = 1;
  check_all (src, trunc, ceil, floor);

  /* FF.F, carry out of ceil */
  EXP(src) = 2;
  SIZ(src) = 3;
  PTR(src)[0] = GMP_NUMB_MAX;
  PTR(src)[1] = GMP_NUMB_MAX;
  PTR(src)[2] = GMP_NUMB_MAX;
  EXP(trunc) = 2;
  SIZ(trunc) = 2;
  PTR(trunc)[0] = GMP_NUMB_MAX;
  PTR(trunc)[1] = GMP_NUMB_MAX;
  mpf_set (floor, trunc);
  EXP(ceil) = 3;
  SIZ(ceil) = 1;
  PTR(ceil)[0] = 1;
  check_all (src, trunc, ceil, floor);

  mpf_clear (src);
  mpf_clear (trunc);
  mpf_clear (ceil);
  mpf_clear (floor);
}
Пример #9
0
bool sum_test(double xf, int prec)
{
    bool fail = false;
    int k;
    mpf_t a_k, x, xn, term, sum, sino, low_bound;

    mpf_init(a_k);
    mpf_init(x);
    mpf_init(xn);
    mpf_init(term);
    mpf_init(sum);
    mpf_init(sino);
    mpf_init(low_bound);

    long nbits = (long) floor (3.321 * prec);
    mpf_set_ui(x, 1);
    mpf_div_2exp(low_bound, x, nbits+32);

    // Sum the series sum_k=1^\infty a_k x^k
    // It should equal sin(2pi/(1+x))
    mpf_set_d(x, xf);
    mpf_set_ui(sum, 0);
    mpf_set(xn, x);
    for (k=1; k<100000; k++)
    {
        topsin_series(a_k, k, prec);
        mpf_mul(term, a_k, xn);
        mpf_add(sum, sum, term);

        // If the term is small enough, we are done.
        mpf_abs(term, term);
        if (mpf_cmp(term, low_bound) < 0) break;

        mpf_mul(xn, xn, x);
    }

    // Now compute sin(2pi/(1+x))
    mpf_add_ui(term, x, 1);
    fp_two_pi(sino, prec);
    mpf_div(term, sino, term);

    fp_sine(sino, term, prec);

    // the sum and the sine should be equal
    mpf_sub(term, sino, sum);
    double zero = mpf_get_d(term);

    double lim = pow(10.0, -prec);
    if (fabs(zero) > lim)
    {
        printf("Error: Expecting precision 1.0e-%d got %g at x=%f\n", prec, zero, xf);
        fail = true;
    }

    mpf_clear(a_k);
    mpf_clear(x);
    mpf_clear(xn);
    mpf_clear(term);
    mpf_clear(sum);
    mpf_clear(sino);
    mpf_clear(low_bound);

    return fail;
}
Пример #10
0
void jarvis_hypercube_approximation
(int C, /* Number of types of customers*/
 int N, /* Number of servers */
 mpf_t *lambda, /* arrive rate according to a Poisson process per type */
 mpf_t **Tao, /* expected service time for service i and customer of node m */
 int **a, /* for customers of type m, the list of preferred servers */
 mpf_t **f) {

  mpf_t Lambda;
  mpf_t Rho;
  mpf_t tao;
  mpf_t *rho;
  mpf_t *new_rho;
  mpf_t *Q_N_rho;

  mpf_t max_change;
  mpf_t tmp;

  mpf_init(Lambda);
  rho = new mpf_t [N];
  for (int i = 0; i < N;i++)
    mpf_init(rho[i]);
  mpf_init(tao);
  mpf_init_set_ui(P__0,1);
  mpf_init_set_ui(P__N,1);
  Q_N_rho = new mpf_t [N];
  for (int i = 0;i < N;i++)
    mpf_init(Q_N_rho[i]);
  new_rho = new mpf_t [N];
  for (int i = 0;i < N;i++)
    mpf_init(new_rho[i]);
  mpf_init(Rho);
  mpf_init(max_change);
  mpf_init(tmp);

  /* INITIALIZE: */
  for (int m = 0;m < C;m++) 
    mpf_add(Lambda,Lambda,lambda[m]);

  /* Busy probability rho_i */
  for (int i = 0; i < N;i++) {
    for (int m = 0; m < C;m++) {
      if (a[m][0] == i) {
	mpf_mul(tmp,lambda[m],Tao[i][m]);
	mpf_add(rho[i],rho[i],tmp);
      }
    }
  }

  /* mean service time 'tao' */
  for (int m = 0;m < C;m++) {
    mpf_mul(tmp,lambda[m],Tao[a[m][0]][m]);
    mpf_add(tao,tao,tmp);
  }
  mpf_div(tao,tao,Lambda);

  /* Define intial values for P__0 and P__N */
  for (int i = 0;i < N;i++) {
    mpf_ui_sub(tmp,1,rho[i]);
    mpf_mul(P__0,P__0,tmp);
  }

  for (int i = 0;i < N;i++)
    mpf_mul(P__N,P__N,rho[i]);

  /* ITERATION: */
  do {

    /*
    cout << "'rho_i':";
    for (int i = 0;i < N;i++) cout << " " << mpf_get_d(rho[i]);
    cout << endl;
    */

    /* traffic intensity */
    mpf_mul(Rho,Lambda,tao);
    mpf_div_ui(Rho,Rho,N);

    /* Compute Q(N,'rho',k) */
    for (int k = 0;k < N;k++)
      correction_factor_Q(Q_N_rho[k],N,Rho,k);
  
    /* Compute f_{im} */
    mpf_t rho_a_ml;
    mpf_init(rho_a_ml);
    for (int i = 0;i < N;i++) {
      for (int m = 0;m < C;m++) {
	mpf_set_ui(rho_a_ml,1);
	for (int k = 0;k < N;k++) {
	  if (a[m][k] == i) {
	    mpf_ui_sub(tmp,1,rho[i]);
	    mpf_mul(tmp,tmp,rho_a_ml);
	    mpf_mul(f[i][m],tmp,Q_N_rho[k]);
	    break;
	  }
	  mpf_mul(rho_a_ml,rho_a_ml,rho[a[m][k]]);
	}
      }
    }
    mpf_clear(rho_a_ml);

    /* Aproximation of 'rho'_i */
    mpf_t Vi;
    mpf_init(Vi);
    mpf_init(rho_a_ml);
    for (int i = 0;i < N;i++) {
      mpf_set_ui(Vi,0);
      for (int m = 0;m < C;m++) {
	mpf_set_ui(rho_a_ml,1);
	for (int k = 0;k < N;k++) {
	  if (a[m][k] == i) {
	    mpf_mul(tmp,lambda[m],Tao[i][m]);
	    mpf_mul(tmp,tmp,Q_N_rho[k]);
	    mpf_mul(tmp,tmp,rho_a_ml);
	    mpf_add(Vi,Vi,tmp);
	    /* Vi += lambda[m] * Tao[i][m] * Q_N_rho[k] * rho_a_ml */
	    break;
	  }
	  mpf_mul(rho_a_ml,rho_a_ml,rho[a[m][k]]);
	}
      }
      mpf_add_ui(tmp,Vi,1);
      mpf_div(new_rho[i],Vi,tmp);
    }
    mpf_clear(rho_a_ml);
    mpf_clear(Vi);

    /* Convergence criterion */
    mpf_set_ui(max_change,0);
    for (int i = 0;i < N;i++) {
      mpf_sub(tmp,rho[i],new_rho[i]);
      mpf_abs(tmp,tmp);
      if (mpf_cmp(tmp,max_change) > 0)
	mpf_set(max_change,tmp);
    }
    
    /*
    cout << "max change = " << mpf_get_d(max_change);
    if (mpf_cmp_d(max_change,JARVIS_EPSILON) < 0)
      cout << " **STOP**" << endl;
    else 
      cout << "\r";
    */

    if (mpf_cmp_d(max_change,JARVIS_EPSILON) < 0) break; /* STOP */

    for (int i = 0;i < N;i++)
      mpf_set(rho[i],new_rho[i]);

    /* Compute P__0 */
    mpf_set_ui(P__0,1);
    for (int i = 0;i < N;i++) {
      mpf_ui_sub(tmp,1,rho[i]);
      mpf_mul(P__0,P__0,tmp);
    }

    /* Compute P__N */
    mpf_t s_rho;
    mpf_init(s_rho);
    for (int i = 0;i < N;i++) 
      mpf_add(s_rho,s_rho,rho[i]);
    mpf_div_ui(tmp,s_rho,N);
    mpf_div(tmp,tmp,Rho);
    mpf_ui_sub(P__N,1,tmp);

    /* Compute mean service time 'tao' */
    mpf_t aux;
    mpf_set_ui(tao,0);
    mpf_init(aux);
    for (int m = 0;m < C;m++) {
      mpf_set_ui(tmp,0);
      for (int i = 0;i < N;i++) {
	mpf_mul(aux,Tao[i][m],f[i][m]);
	mpf_add(tmp,tmp,aux);
      }
      mpf_mul(tmp,lambda[m],tmp);
      mpf_add(tao,tao,tmp);
    }
    mpf_div(tao,tao,Lambda); // / Lambda
    mpf_ui_sub(aux,1,P__N); // 1 - P__N
    mpf_div(tao,tao,aux); // / (1 - P__N)
    mpf_clear(aux);
 
  } while (1);

  /*
  cout << "finsh jarvis" << endl;
  for (int i = 0;i < N;i++) {
    for (int m = 0;m < C;m++) {
      cout << mpf_get_d(f[i][m]) << " ";
    }
    cout << endl;
  }
  */

  mpf_clear(tmp);
  mpf_clear(max_change);
  mpf_clear(Rho);
  for (int i = 0;i < N;i++)
    mpf_clear(new_rho[i]);
  delete [] new_rho;
  for (int i = 0;i < N;i++)
    mpf_clear(Q_N_rho[i]);
  delete [] Q_N_rho;
  mpf_clear(P__N);
  mpf_clear(P__0);
  mpf_clear(tao);
  for (int i = 0;i < N;i++)
    mpf_clear(rho[i]);
  delete [] rho;
  mpf_clear(Lambda);
}
Пример #11
0
void
check_random (long reps)
{
  unsigned long test;
  gmp_randstate_ptr rands = RANDS;
  mpf_t a, b, x;
  mpz_t ds;
  int hibits, lshift1, lshift2;
  int xtra;

#define HIBITS 10
#define LSHIFT1 10
#define LSHIFT2 10

  mpf_set_default_prec ((1 << HIBITS) + (1 << LSHIFT1) + (1 << LSHIFT2));

  mpz_init (ds);
  mpf_inits (a, b, x, NULL);

  for (test = 0; test < reps; test++)
    {
      mpz_urandomb (ds, rands, HIBITS);
      hibits = mpz_get_ui (ds) + 1;
      mpz_urandomb (ds, rands, hibits);
      mpz_setbit (ds, hibits  - 1);	/* make sure msb is set */
      mpf_set_z (a, ds);
      mpf_set_z (b, ds);

      mpz_urandomb (ds, rands, LSHIFT1);
      lshift1 = mpz_get_ui (ds);
      mpf_mul_2exp (a, a, lshift1 + 1);
      mpf_mul_2exp (b, b, lshift1 + 1);
      mpf_add_ui (a, a, 1);	/* make a one-bit difference */

      mpz_urandomb (ds, rands, LSHIFT2);
      lshift2 = mpz_get_ui (ds);
      mpf_mul_2exp (a, a, lshift2);
      mpf_mul_2exp (b, b, lshift2);
      mpz_urandomb (ds, rands, lshift2);
      mpf_set_z (x, ds);
      mpf_add (a, a, x);
      mpf_add (b, b, x);

      insert_random_low_zero_limbs (a, rands);
      insert_random_low_zero_limbs (b, rands);

      if (mpf_eq (a, b, lshift1 + hibits) == 0 ||
	  mpf_eq (b, a, lshift1 + hibits) == 0)
	{
	  dump_abort (a, b, lshift1 + hibits, lshift1, lshift2, hibits, 1, test);
	}
      for (xtra = 1; xtra < 100; xtra++)
	if (mpf_eq (a, b, lshift1 + hibits + xtra) != 0 ||
	    mpf_eq (b, a, lshift1 + hibits + xtra) != 0)
	  {
	    dump_abort (a, b, lshift1 + hibits + xtra, lshift1, lshift2, hibits, 0, test);
	  }
    }

  mpf_clears (a, b, x, NULL);
  mpz_clear (ds);
}
/**
  * Algoritmo de Borwein
  *
  * a1 se aproxima do valor de 1/PI.
  * Cada iteração quadruplica o número de dígitos corretos.
  *
  */
void gmp_borwein()
{
  /*variáveis para calculo com respectivos valores iniciais*/
  mpf_t a0, a1, y0, y1, aux1, aux2;
  /*contador de iterações*/
  long unsigned int k=0;


  //mpf_set_default_prec(33220); /*define a precisao do float em bits*/
  mpf_set_default_prec(33219280);//(33219280); /*define a precisao do float em bits*/
  mpf_init(a0),mpf_init(a1),mpf_init(y0),mpf_init(y1),mpf_init(aux1),mpf_init(aux2); /*Inicializa as variáveis em 0*/

  /*seta os valores inicias das váriaveis de precisão variável*/
  /*a0 = 6-4*sqrt(2)*/
  mpf_set_ui(aux1,2);
  mpf_sqrt(aux1,aux1); /*sqrt(2)*/
  mpf_mul_ui(a0,aux1,4);
  mpf_ui_sub(a0,6,a0);

  //mpf_set_d(a0, 6-4*sqrt(2));
  mpf_sub_ui(y0,aux1,1); /*y0 = sqrt(2)-1*/

  while(k<12)
  {
    /*y1 = (1-sqrt(sqrt((1-pow(y0,4))))) / (1+sqrt(sqrt(1-pow(y0,4))));*/
    mpf_pow_ui(y1,y0,4); /*y1 = pow(y0,4)*/
    mpf_ui_sub(y1,1,y1); /*y1 = 1 - y1*/
    mpf_sqrt(y1,y1);
    mpf_sqrt(y1,y1);
    mpf_ui_sub(aux1,1,y1);
    mpf_add_ui(aux2,y1,1);
    mpf_div(y1,aux1,aux2);

    /*a1 = a0*pow(1 + y1,4) - pow(2,2*k+3)*y1*(1+y1+pow(y1,2));*/
    mpf_add_ui(a1,y1,1); /*a1 = y1+1*/
    mpf_pow_ui(a1,a1,4); /*a1 = a1^4*/
    mpf_mul(a1,a0,a1); /*a1 = a0*a1*/ 

    mpf_pow_ui(aux2,y1,2); /*aux2 = pow(y1,2)*/
    mpf_add(aux2,aux2,y1); /*aux2 += y1*/
    mpf_add_ui(aux2,aux2,1); /*aux2++*/
    mpf_mul(aux2,aux2,y1); /*aux2 *= y1*/

    mpf_set_ui(aux1,2); /* aux1=2 */
    mpf_pow_ui(aux1,aux1,2*k+3); /* aux1 = pow(aux1,2*k+3)*/

    mpf_mul(aux1,aux1,aux2); /*aux1 = aux1*aux2*/

    mpf_sub(a1,a1,aux1);

    
    /*troca os valores das variáveis de maneira eficiente*/
    mpf_swap(a0,a1);
    mpf_swap(y0,y1);
    
    k++;
  
    /*gmp_printf("k=%ld, y1=%.*Ff, 1/PI=%.*Ff\n", k, 20, y1, 20, a1);
    gmp_printf("a0=%.*Ff, y0=%.*Ff\n", 20, a0, 20, y0);*/
  }

  mpf_ui_div(a1,1,a1); /*PI=1/a1*/

  gmp_printf("%.*Ff\n",10000000,a1);

  mpf_clear(a0),mpf_clear(a1),mpf_clear(y0),mpf_clear(y1),mpf_clear(aux1),mpf_clear(aux2); /*Limpa as variáveis da memória*/

  
}
Пример #13
0
int main ()
{
    
    int i = 0; /*Contador de iterações*/
    int k = 8; /*Fator de multiplicação*/
    mpf_t pi_pas, pi_novo;
    mpf_t a_pas, a_novo;
    mpf_t y_pas, y_novo;
    mpf_t temp1, temp2;
    mpf_t e;
    FILE *fileTime; /*Ponteiro do arquivo de saída*/
    clock_t start, end;
	double cpu_time_used;

    mpf_set_default_prec(BITS_PER_DIGIT * 11000000); /*Precisão default*/
    
    
    /*Inicialização das variáveis*/
    mpf_init(pi_pas);
    mpf_init(pi_novo);
    mpf_init(a_pas);
    mpf_init(y_pas);
    mpf_init(temp1);
    mpf_init(temp2);    
    mpf_init_set_d(a_novo, 32.0);
    mpf_sqrt(a_novo, a_novo);
    mpf_ui_sub(a_novo, 6, a_novo);
    mpf_init_set_d(y_novo, 2.0);
    mpf_sqrt(y_novo, y_novo);
    mpf_sub_ui(y_novo, y_novo, 1);    
    mpf_init_set_str(e, "1e-10000000", 10);    
    mpf_ui_div(pi_novo, 1, a_novo);
    
	start = clock();

    /*Calcula as iterações*/
    do
    {
        mpf_swap(pi_pas, pi_novo);
        mpf_swap(a_pas, a_novo);
        mpf_swap(y_pas, y_novo);
       
        mpf_pow_ui(y_pas, y_pas, 4);
        mpf_ui_sub(y_pas, 1, y_pas);
        mpf_sqrt(y_pas, y_pas);
        mpf_sqrt(y_pas, y_pas);
        mpf_add_ui(temp1, y_pas, 1);
        mpf_ui_sub(y_novo, 1, y_pas);
        mpf_div(y_novo, y_novo, temp1);
        
        mpf_add_ui(temp1, y_novo, 1);
        
        mpf_pow_ui(temp2, y_novo, 2);
        mpf_add(temp2, temp2, temp1);
        mpf_mul(temp2, temp2, y_novo);
        mpf_mul_ui(temp2, temp2, k);
        k *= 4;
        
        mpf_pow_ui(temp1, temp1, 4);
        mpf_mul(temp1, temp1, a_pas);
        mpf_sub(a_novo, temp1, temp2);
        
        mpf_ui_div(pi_novo, 1, a_novo);
        
        mpf_sub(pi_pas, pi_novo, pi_pas);
        mpf_abs(pi_pas, pi_pas);
        
	gmp_printf("\nIteracao %d  | pi = %.25Ff", i, pi_novo);
	i++;
    } while ( mpf_cmp(e, pi_pas) < 0 );
 	
	end = clock();
 	
	cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

	fileTime = fopen("execution_time.txt", "w");
	fprintf(fileTime, "Execution time: %f\n", cpu_time_used);
	fclose(fileTime);	
  
    /*Libera espaço de memória*/
    mpf_clear(pi_pas);
    mpf_clear(pi_novo);
    mpf_clear(a_pas);
    mpf_clear(a_novo);
    mpf_clear(y_pas);
    mpf_clear(y_novo);
    mpf_clear(temp1);
    mpf_clear(temp2);
    mpf_clear(e);
    
    return 0;
}