Beispiel #1
0
int main (void) {
	mpf_set_default_prec (300000);
	mpf_t x0, y0, resA, resB, Z, var;

	mpf_init_set_ui (x0, 1);
	mpf_init_set_d (y0, 0.5);
	mpf_sqrt (y0, y0);
	mpf_init (resA);
	mpf_init (resB);
	mpf_init_set_d (Z, 0.25);
	mpf_init (var);

	int n = 1;
	for(int i=0; i<8; i++){
		agm(x0, y0, resA, resB);
		mpf_sub(var, resA, x0);
		mpf_mul(var, var, var);
		mpf_mul_ui(var, var, n);
		mpf_sub(Z, Z, var);
		n += n;
		agm(resA, resB, x0, y0);
		mpf_sub(var, x0, resA);
		mpf_mul(var, var, var);
		mpf_mul_ui(var, var, n);
		mpf_sub(Z, Z, var);
		n += n;
	}
	mpf_mul(x0, x0, x0);
	mpf_div(x0, x0, Z);
	gmp_printf ("%.100000Ff\n", x0);
	return 0;
}
/**
 * void calculate_t()
 * 
 * Descricao:
 * 	Calcula o valor da variavel t na n-esima iteracao.
 * 
 * Parametros de entrada:
 * 	-
 * 
 * Parametros de retorno:
 * 	-
 */
void calculate_t(){
  
    mpf_sub(t_n[n_count+1], a_n[n_count], a_n[n_count+1]);
    mpf_pow_ui(t_n[n_count+1], t_n[n_count+1], 2);    
    mpf_mul(t_n[n_count+1], p_n[n_count], t_n[n_count+1]);    
    mpf_sub(t_n[n_count+1], t_n[n_count], t_n[n_count+1]); 

}
static void newton(mpf_t x, double seed,
                   fun_3term *fun, fun_3term *der, int n)
{
  DECLARE_3VARS(ox,f,df);
  mpf_set_d(x, seed);
  do {
    mpf_set(ox, x);
    fun(f, n,x), der(df, n,x), mpf_div(f, f,df), mpf_sub(x, x,f);
  } while(!is_small(f,x));
  fun( f, n,x), der(df, n,x), mpf_div(f, f,df), mpf_sub(x, x,f);
}
Beispiel #4
0
void recalcZoom() {
	int32_t diffX = selectZoomW - selectZoomX;
	int32_t diffY = selectZoomH - selectZoomY;
	if(abs(diffX) >= 10 || abs(diffY) >= 10) {
		long double x = sizeX;
		long double y = sizeY;
		long double factorLeft = ((selectZoomX*100.0)/x)/100.0;
		long double factorRight = 1.0-((selectZoomW*100.0)/x)/100.0;
		long double factorBottom = 1.0-((selectZoomH*100.0)/y)/100.0;
		
		mpf_t facLeft;
		mpf_init2(facLeft,gmpBit);
		mpf_set_d(facLeft,factorLeft);
		mpf_t facRight;
		mpf_init2(facRight,gmpBit);
		mpf_set_d(facRight,factorRight);
		mpf_t facBottom;
		mpf_init2(facBottom,gmpBit);
		mpf_set_d(facBottom,factorBottom);
		mpf_t tmpDiff;
		mpf_init2(tmpDiff,gmpBit);
		mpf_t tmp;
		mpf_init2(tmp,gmpBit);
		
		//(maxRe - minRe)
		mpf_sub(tmpDiff,mandelRange.maxRe,mandelRange.minRe);
		
		//minRe+=(maxRe - minRe)*factorLeft;
		mpf_mul(tmp,tmpDiff,facLeft);
		mpf_add(mandelRange.minRe,mandelRange.minRe,tmp);
		
		//maxRe-=(maxRe - minRe)*factorRight;
		mpf_mul(tmp,tmpDiff,facRight);
		mpf_sub(mandelRange.maxRe,mandelRange.maxRe,tmp);
		
		
		//minIm+=(maxIm - minIm)*factorBottom;
		mpf_sub(tmpDiff,mandelRange.maxIm,mandelRange.minIm);
		mpf_mul(tmp,tmpDiff,facBottom);
		mpf_add(mandelRange.minIm,mandelRange.minIm,tmp);
		
		
		mpf_clear(tmp);
		mpf_clear(tmpDiff);
		mpf_clear(facLeft);
		mpf_clear(facRight);
		mpf_clear(facBottom);
		
		restart();
	}
}
Beispiel #5
0
static void mpc_cis(mpc_t res, mpf_t theta) {
  mpf_t a;

  mpf_init(a); mpf_set(a, theta);
  //res = exp(i a)
  //  = cos a + i sin a
  //converges quickly near the origin
  mpf_t f0;
  mpf_ptr rx = mpc_re(res), ry = mpc_im(res);
  int i;
  int toggle = 1;

  mpf_init(f0);

  mpf_set(f0, a);
  mpf_set_ui(rx, 1);
  mpf_set(ry, f0);
  i = 1;
  for(;;) {
    toggle = !toggle;
    i++;
    mpf_div_ui(f0, f0, i);
    mpf_mul(f0, f0, a);
    if (toggle) {
      mpf_add(rx, rx, f0);
    } else {
      mpf_sub(rx, rx, f0);
    }

    i++;
    mpf_div_ui(f0, f0, i);
    mpf_mul(f0, f0, a);

    if (toggle) {
      mpf_add(ry, ry, f0);
    } else {
      mpf_sub(ry, ry, f0);
    }

    if (mpf_sgn(f0) > 0) {
      if (mpf_cmp(f0, epsilon) < 0) break;
    } else {
      if (mpf_cmp(f0, negepsilon) > 0) break;
    }
  }

  mpf_clear(f0);
  mpf_clear(a);
}
Beispiel #6
0
void DoUniaryOperation(number_t result, number_t number1, char *op)
{
	switch( op[0] )
	{
		case '!':
			mpf_set_si(result, mpf_cmp_ui(number1, 0));
			break;
		case '~':
			DO_INTEGER_OPERATION1_ON_FLOAT(mpz_com, result, number1);
			break;
		case '+':
			if (op[1] == '+') {
				mpf_add_ui(result, number1, 1);
			} else {
				mpf_set(result, number1);
			}
			break;
		case '-':
			if (op[1] == '-') {
				mpf_sub_ui(result, number1, 1);
			} else {
				mpf_sub(result, result, number1);
			}
			break;
	}
}
void extrapolate(index_t num_samples, mpf_t *samples, mpf_t ans) {
    // The Richardson extrapolation recursive formula is
    //
    // A_n+1(x) = (2^n A_n(2x) - A_n(x)) / (2^n - 1)

    mpf_t mult;  // mult = 2^n
    mpf_init_set_d(mult, 1);

    mpf_t denom;  // denom = 1 / (mult - 1)
    mp_bitcnt_t precision = mpf_get_prec(ans);
    mpf_init2(denom, precision);

    mpf_t *end = samples + num_samples;
    for (mpf_t *lim = samples; lim < end; lim++) {  // lim - samples = n
        mpf_mul_ui(mult, mult, 2);
        mpf_set(denom, mult);
        mpf_sub_ui(denom, denom, 1);
        mpf_ui_div(denom, 1, denom);
        // evaluate all extrapolations
        for (mpf_t *ptr = end; --ptr > lim; ) {
            // A_n+1(x) = (mult A_n(2x) - A_n(x)) * denom
            mpf_mul(*ptr, *ptr, mult);
            mpf_sub(*ptr, *ptr, *(ptr - 1));
            mpf_mul(*ptr, *ptr, denom);
        }
    }
    mpf_set(ans, *(end - 1));  // move to ans

    // Clean
    mpf_clear(mult);
    mpf_clear(denom);
}
depth_t frac_generalized_celtic_gmp(
                                depth_t depth,
                                mpf_t bail,
                                mpf_t wim,     mpf_t wre,
                                mpf_t c_im,    mpf_t c_re,
                                mpf_t wim2,    mpf_t wre2, mpf_t t1)
{
    depth_t wz;
    for (wz = 1; wz <= depth; wz++)
        {
        /* wim = 2.0 * wre * wim + c_im; */
        mpf_mul(   t1,     wre,    wim);
        mpf_mul_ui(t1,     t1,     2);
        mpf_add(   wim,    t1,     c_im);
        /* wre = wre2 - wim2 + c_re; */
        mpf_sub(   t1,     wre2,   wim2);
        mpf_abs(   t1,     t1);
        mpf_add(   wre,    t1,     c_re);
        /* wim2 = wim * wim; */
        mpf_mul(   wim2,   wim,    wim);
        /* wre2 = wre * wre; */
        mpf_mul(   wre2,   wre,    wre);
        /* if ((wim2 + wre2) > frs_bail) */
        mpf_add(   t1,     wim2,   wre2);
        if (mpf_cmp(t1, bail) > 0)
            return wz;
    }
    return 0;
}
/*TODO verificar se a melhor maneira de proceder com o cálculo abaixo*/
void *calc_a(thr_gmpf_t *vars)
{
    /*vars.a1 = vars.a0*pow(1 + vars.y1,4) - pow(2,2*k+3)*vars.y1*(1+vars.y1+pow(vars.y1,2));*/

    /*c = y1+1;*/
    mpf_add_ui(vars->aux_a1,vars->y_valoratual,1);

    mpf_pow_ui(vars->a1,vars->aux_a1,4); /*a1 = c^4*/
    mpf_mul(vars->a1,vars->a0,vars->a1); /*a1 = a0*a1*/

    mpf_pow_ui(vars->aux_a2,vars->y_valoratual,2); /*aux2 = pow(y_valoratual,2)*/
    mpf_add(vars->aux_a2,vars->aux_a2,vars->aux_a1); /*aux2 += c*/
    mpf_mul(vars->aux_a2,vars->aux_a2,vars->y_valoratual); /*vars->aux2 *= vars->y_valoratual*/


    /*continua os cálculos de a que não dependem de y*/
    /*o cálculo abaixo pode ser feito paralelamente*/
    mpf_set_ui(vars->aux_a1,2); /* aux1=2 */
    mpf_pow_ui(vars->aux_a1,vars->aux_a1,2*vars->k+3); /* aux_a1 = pow(aux_a1,2*k+3)*/

    mpf_mul(vars->aux_a1,vars->aux_a1,vars->aux_a2); /*vars->aux1 = vars->aux1*vars->aux2*/

    mpf_sub(vars->a1,vars->a1,vars->aux_a1);

    vars->k = vars->k+1;

    pthread_exit(NULL);
    return NULL;
}
Beispiel #10
0
void
mpf_reldiff (mpf_t rdiff, mpf_srcptr x, mpf_srcptr y)
{
  if (UNLIKELY (SIZ(x) == 0))
    {
      mpf_set_ui (rdiff, (unsigned long int) (mpf_sgn (y) != 0));
    }
  else
    {
      mp_size_t dprec;
      mpf_t d;
      TMP_DECL;

      TMP_MARK;
      dprec = PREC(rdiff) + ABSIZ(x);
      ASSERT (PREC(rdiff)+1 == dprec - ABSIZ(x) + 1);

      PREC(d) = dprec;
      PTR(d) = TMP_ALLOC_LIMBS (dprec + 1);

      mpf_sub (d, x, y);
      SIZ(d) = ABSIZ(d);
      mpf_div (rdiff, d, x);

      TMP_FREE;
    }
}
Beispiel #11
0
vanilla::object::ptr vanilla::int_object::sub(object::ptr const& other)
{
    switch(other->type_id())
    {
        case OBJECT_ID_INT:
        {
            int_object const* rhs = static_cast<int_object const*>(other.get());
            int_type result;
            mpz_sub(result.mpz(), _v.mpz(), rhs->value().mpz());
            return allocate_object<int_object>(std::move(result));
        }
        
        case OBJECT_ID_FLOAT:
        {
            float_object::float_type lhs( (_v.mpz()) );
            float_object const* rhs = static_cast<float_object const*>(other.get());
            float_object::float_type result;
            mpf_sub(result.mpf(), lhs.mpf(), rhs->value().mpf());
            return allocate_object<float_object>(std::move(result));
        }
        
        default:
        {
            return object::sub(other);
        }
    }
}
	void FractalTexture::create() {
		if(glIsTexture(texture.id)) glDeleteTextures(1, &texture.id); //cleaning gl memory up
		m_iMaxIterations = (long)((double)m_iStdMaxIterations * 1.0); //todo?
		m_dInvMaxIterations = 1.0 / (m_iMaxIterations+0.0001);
		const double winv = 1.0/(texture.width -1);
		const double hinv = 1.0/(texture.height-1);
		util::timeDiff();
		for (int x = 0; x < texture.width; ++x) {
			for (int y = 0; y < texture.height; ++y) {
#ifdef ROE_FRACTAL_GMP_USE_C
				mpf_set_d(m_xPos, x*winv);
				mpf_set_d(m_yPos, y*hinv);
				mpf_mul(m_xPos, m_xPos, m_width);
				mpf_mul(m_yPos, m_yPos, m_height);
				mpf_add(m_xPos, m_xUpLt, m_xPos);
				mpf_sub(m_yPos, m_yUpLt, m_yPos);
#else
				m_xPos = m_xUpLt + (x*winv)*m_width;
				m_yPos = m_yUpLt - (y*hinv)*m_height;
#endif
				computeColor(&(texture.data[((texture.height-1-y)*texture.width+x)*texture.bpp]));
			}
		}
		cout << "dt: " << util::timeDiff() << endl;
		cout << getCenterX() << endl;
		cout << getCenterY() << endl;
		cout << getZoomW() << endl;
		updateMipmaps();
		Texture::loadTextureIntoGL(&texture);
	}
Beispiel #13
0
int sg_big_float_sub(sg_big_float_t *a, sg_big_float_t *b, sg_big_float_t *res)
{
    if (!a || !b || !res)
        return -1;

    mpf_sub(res->mpf, a->mpf, b->mpf);
    return 0;
}
Beispiel #14
0
void mpfc_mul(mpfc_ptr r,mpfc_ptr x,mpfc_ptr y)
{
	mpf_mul(mpfc_mpf_temp[0],x->Re,y->Re);
	mpf_mul(mpfc_mpf_temp[1],x->Im,y->Im);
	mpf_sub(r->Re,mpfc_mpf_temp[0],mpfc_mpf_temp[1]);
	mpf_mul(mpfc_mpf_temp[0],x->Re,y->Im);
	mpf_mul(mpfc_mpf_temp[1],x->Im,y->Re);
	mpf_add(r->Im,mpfc_mpf_temp[0],mpfc_mpf_temp[1]);
}
	void FractalTexture::computeColor(unsigned char *pixel) {
#ifdef ROE_FRACTAL_GMP_USE_C
		mpf_set(xPos, m_xPos);
		mpf_set(yPos, m_yPos);
#else
		xPos = m_xPos;
		yPos = m_yPos;
#endif
		for(long iteration = 0; iteration < m_iMaxIterations; ++iteration) {
#ifdef ROE_FRACTAL_GMP_USE_C
			mpf_mul(xPos2, xPos, xPos);
			mpf_mul(yPos2, yPos, yPos);
			mpf_add(tmp, xPos2, yPos2); //save sum temporarily
			if(mpf_cmp_ui(tmp,4) >= 0) {
#else
				xPos2 = xPos*xPos;
				yPos2 = yPos*yPos;
				if(xPos2 + yPos2 > 4.0) {
#endif
					//(coloured) outer
					const double ratio = iteration*m_dInvMaxIterations;
					//const Color c = Color::WHITE;
					const Color c = colorInterpolation.interpolate(ratio);
					pixel[0] = c.byter();
					pixel[1] = c.byteg();
					pixel[2] = c.byteb();
					return;
				}
#ifdef ROE_FRACTAL_GMP_USE_C
				mpf_mul(yPos, yPos, xPos);
				mpf_mul_ui(yPos, yPos, 2);
				mpf_add(yPos, yPos, m_yPos);
				mpf_sub(xPos, xPos2, yPos2);
				mpf_add(xPos, xPos, m_xPos);
#else
				yPos *= xPos;
				yPos *= 2;
				yPos += m_yPos;
				xPos = xPos2;
				xPos -= yPos2;
				xPos += m_xPos;
#endif
			}
			//inner
			pixel[0] = m_innerR;
			pixel[1] = m_innerG;
			pixel[2] = m_innerB;
		}
		void FractalTexture::updateMipmaps() {
			S_Texture *subtex1 = &texture, *subtex2 = nullptr;
			while (subtex1->next_mipmap) {
				subtex2 = subtex1->next_mipmap;
				Texture::scaleImage2(subtex1->data, subtex1->width, subtex1->height, subtex2->data, subtex2->width, subtex2->height, subtex1->bpp); //scaling image to new size
				subtex1 = subtex2; //one level deeper
			}
		}
Beispiel #16
0
void recalcView() {
	long double x = sizeX;
	long double y = sizeY;
	int32_t diffX = selectMoveW - selectMoveX;
	int32_t diffY = selectMoveH - selectMoveY;
	if(abs(diffX) >= 1 || abs(diffY) >= 1) {
		long double factorX = ((diffX*100.0)/x)/100.0;
		long double factorY = ((diffY*100.0)/y)/100.0;
		
		mpf_t facX;
		mpf_init2(facX,gmpBit);
		mpf_set_d(facX,factorX);
		mpf_t facY;
		mpf_init2(facY,gmpBit);
		mpf_set_d(facY,factorY);		
		mpf_t tmpDiff;
		mpf_init2(tmpDiff,gmpBit);		
		mpf_t tmp;
		mpf_init2(tmp,gmpBit);
		
		
		mpf_sub(tmpDiff,mandelRange.maxRe,mandelRange.minRe);
		
		//minRe-=(maxRe - minRe)*factorX;
		mpf_mul(tmp,tmpDiff,facX);
		mpf_sub(mandelRange.maxRe,mandelRange.maxRe,tmp);
		//maxRe-=(maxRe - minRe)*factorX;
		mpf_sub(mandelRange.minRe,mandelRange.minRe,tmp);
		
		//minIm+=(maxIm - minIm)*factorY;
		mpf_sub(tmpDiff,mandelRange.maxIm,mandelRange.minIm);
		mpf_mul(tmp,tmpDiff,facY);
		mpf_add(mandelRange.minIm,mandelRange.minIm,tmp);
		
		mpf_clear(tmp);
		mpf_clear(tmpDiff);
		mpf_clear(facX);
		mpf_clear(facY);
		
		restart();
	}
}
Beispiel #17
0
// r = sqrt(x)
void my_sqrt(mpf_t r, mpf_t x)
{
    unsigned prec, bits, prec0;

    prec0 = mpf_get_prec(r);

    if (prec0 <= DOUBLE_PREC) {
        mpf_set_d(r, sqrt(mpf_get_d(x)));
        return;
    }

    bits = 0;
    for (prec = prec0; prec > DOUBLE_PREC;) {
        int bit = prec & 1;
        prec = (prec + bit) / 2;
        bits = bits * 2 + bit;
    }

    mpf_set_prec_raw(t1, DOUBLE_PREC);
    mpf_set_d(t1, 1 / sqrt(mpf_get_d(x)));

    while (prec < prec0) {
        prec *= 2;
        /*printf("prec=%d, prec0=%d\n", prec, prec0); */
        if (prec < prec0) {
            /* t1 = t1+t1*(1-x*t1*t1)/2; */
            mpf_set_prec_raw(t2, prec);
            mpf_mul(t2, t1, t1);
            mpf_set_prec_raw(x, prec/2);
            mpf_mul(t2, t2, x);
            mpf_ui_sub(t2, 1, t2);
            mpf_set_prec_raw(t2, prec/2);
            mpf_div_2exp(t2, t2, 1);
            mpf_mul(t2, t2, t1);
            mpf_set_prec_raw(t1, prec);
            mpf_add(t1, t1, t2);
        } else {
            prec = prec0;
            /* t2=x*t1, t1 = t2+t1*(x-t2*t2)/2; */
            mpf_set_prec_raw(t2, prec/2);
            mpf_set_prec_raw(x, prec/2);
            mpf_mul(t2, t1, x);
            mpf_mul(r, t2, t2);
            mpf_set_prec_raw(x, prec);
            mpf_sub(r, x, r);
            mpf_mul(t1, t1, r);
            mpf_div_2exp(t1, t1, 1);
            mpf_add(r, t1, t2);
            break;
        }
        prec -= (bits & 1);
        bits /= 2;
    }
}
		void FractalTexture::updateCorners() {
#ifdef ROE_FRACTAL_GMP_USE_C
			mpf_div_ui(xPos, m_width , 2);
			mpf_div_ui(yPos, m_height, 2);
			mpf_sub(m_xUpLt, m_xCenter, xPos);
			mpf_add(m_yUpLt, m_yCenter, yPos);
#else
			m_xUpLt = m_xCenter-0.5*m_width;
			m_yUpLt = m_yCenter+0.5*m_height;
#endif
		}
Beispiel #19
0
libmaus2::math::GmpFloat & libmaus2::math::GmpFloat::operator-=(
	GmpFloat const &
		#if defined(LIBMAUS2_HAVE_GMP)
		o
		#endif
)
{
	#if defined(LIBMAUS2_HAVE_GMP)
	mpf_sub(decode(v),decode(v),decode(o.v));
	#endif
	return *this;
}
Beispiel #20
0
/*
   Caclulates:
   |a11-b11  a12-b12|
   |a21-b21  a22-b22|
 */
void det22(mpf_t rop, double a11, double b11, double a12, double b12,
	   double a21, double b21, double a22, double b22)
{
    mpf_set_d(t1, a11);
    mpf_set_d(t2, b11);
    mpf_sub(p11, t1, t2);
    mpf_set_d(t1, a12);
    mpf_set_d(t2, b12);
    mpf_sub(p12, t1, t2);
    mpf_set_d(t1, a21);
    mpf_set_d(t2, b21);
    mpf_sub(p21, t1, t2);
    mpf_set_d(t1, a22);
    mpf_set_d(t2, b22);
    mpf_sub(p22, t1, t2);

    mpf_mul(t1, p11, p22);
    mpf_mul(t2, p12, p21);
    mpf_sub(rop, t1, t2);

    return;
}
/* New calculate function. */
ordinal_number_t cache_calculator(render_t* handle,const view_position_t render_position)
{
	/* Volatile data. */
	ordinal_number_t* help;
	complex_number_t  complex_position;
	view_position_t   shift;
	real_number_t     scaling_factor;
	mpf_t             help_mpf;
	mpf_t             help_two;

	mpf_set_default_prec(sizeof(char)*handle->prec);
	mpf_init(help_mpf);
	mpf_init(Re(complex_position));
	mpf_init(Im(complex_position));
	mpf_init(scaling_factor);
	mpf_init(help_two);

	/* Check if the point has been calculated already. */
	help=handle->points+render_position.y*handle->geometry.width+render_position.x;
	if(*help==0)
	{
		/* Has not been calculated till now, calculate the iteration. */

		/* Precalculate scaling factor and center shift for speed reasons. */
		mpf_div_ui(scaling_factor,handle->scale,handle->geometry.width);
		shift.x=handle->geometry.width/2;
		shift.y=handle->geometry.height/2;

		/* Calculate the iteration. */
		mpf_set_si(help_two,(render_position.x-shift.x));
		mpf_mul(help_mpf,scaling_factor,help_two);
		mpf_add(complex_position.real_part,help_mpf,handle->center.real_part);

		mpf_set_si(help_two,(render_position.y-shift.y));
		mpf_mul(help_mpf,scaling_factor,help_two);
		mpf_sub(Im(complex_position),Im(handle->center),help_mpf);

		*help=(*handle->fractal_facility->facility.fractal.calculate_function)(handle->fractal,&complex_position);
	}

	mpf_clear(help_mpf);
	mpf_clear(Re(complex_position));
	mpf_clear(Im(complex_position));
	mpf_clear(scaling_factor);
	mpf_clear(help_two);
	

	/* Return the iteration. */
	return(*help);
	//return(0);
}
Beispiel #22
0
void my_out_str_raw(FILE *fp, unsigned long digits, mpf_t f, unsigned long offset)
{
    unsigned long d;

    if (digits <= LINE_SIZE*NUM_BLOCKS) {
        unsigned long cursor = offset % LINE_SIZE;
        for (d = 0; d < digits; ) {
            mpf_set_prec_raw(f, (int)((digits-d)*BITS_PER_DIGIT+1));
            mpf_mul_ui(f, f, UNIT_MOD);
            unsigned long i = mpf_get_ui(f);
            mpf_sub_ui(f, f, i);

            utoa(i, UNIT_SIZE);
            *out_ptr++ = ' ';
            d += UNIT_SIZE;
            cursor += UNIT_SIZE;
            if (cursor == LINE_SIZE) {
                cursor = 0;
                *out_ptr++ = ':';
                *out_ptr++ = ' ';
                utoa(offset + d, 0);
                *out_ptr++ = '\n';
                if ((offset + d) % (LINE_SIZE*10) == 0)
                    flush_out(fp);
            }
        }
    } else {
        mpf_t block, mod;
        unsigned long num_units = (digits + UNIT_SIZE-1)/UNIT_SIZE;
        unsigned long block_size =  (num_units + NUM_BLOCKS-1)/NUM_BLOCKS*UNIT_SIZE;
        mpf_set_default_prec((int)(block_size*BITS_PER_DIGIT+1));
        mpf_init(block);
        mpf_init_set_ui(mod, 10);
        mpf_pow_ui(mod, mod, block_size);

        for (d = 0; d < digits; d += block_size) {
            unsigned long size = block_size < digits - d ? block_size : digits - d;
            mpf_set_prec_raw(block, (int)(size*BITS_PER_DIGIT+1));
            mpf_set(block, f);
            my_out_str_raw(fp, size, block, offset+d);
            if (block_size < digits - d) {
                mpf_set_prec_raw(f, (int)((digits-d)*BITS_PER_DIGIT+1));
                mpf_mul(f, f, mod);
                mpf_floor(trunk, f);
                mpf_sub(f, f, trunk);
            }
        }
        mpf_clear(block);
        mpf_clear(mod);
    }
}
Beispiel #23
0
void UniRootF_Newton()
{
	poly_f f,fd;
	mpf_t x,y,den,num;
	mpf_t prec;
	mpf_init2(den,DigitisToBits(FC_DEFAULT_PREC));
	mpf_init2(num,DigitisToBits(FC_DEFAULT_PREC));
	mpf_init2(y,DigitisToBits(FC_DEFAULT_PREC));
	mpf_init2(x,DigitisToBits(FC_DEFAULT_PREC));
	mpf_init2(prec,1);
	f.resize(3);
	mpf_set_str(prec,"1e-50",10);
	mpf_set_si(f[0],-2);
	mpf_set_si(f[1],0);
	mpf_set_si(f[2],1);
	mpf_set_str(x,"1",10);

	UniDFormF(fd,f);

	while(1)
	{
		UniEvalF(num,f,x);
		UniEvalF(den,fd,x);
		mpf_div(y,num,den);
		mpf_abs(num,y);
		if(mpf_cmp(num,prec)<0)break;
		mpf_sub(x,x,y);
	}
	mpf_sub(y,x,y);
	mpf_out_str(0,10,FC_DEFAULT_PREC,y);std::cout<<"\n";
	mpf_clear(prec);
	mpf_clear(den);
	mpf_clear(num);
	mpf_clear(y);
	mpf_clear(x);
	f.resize(0);
	fd.resize(0);
}
Beispiel #24
0
void mpfc_div(mpfc_ptr r,mpfc_ptr x,mpfc_ptr y)
{
	static mpfc_t z;
	mpf_mul(mpfc_mpf_temp[0],x->Re,y->Re);
	mpf_mul(mpfc_mpf_temp[1],x->Im,y->Im);
	mpf_add(mpfc_mpf_temp[2],mpfc_mpf_temp[0],mpfc_mpf_temp[1]);
	mpf_mul(mpfc_mpf_temp[0],x->Re,y->Im);
	mpf_mul(mpfc_mpf_temp[1],x->Im,y->Re);
	mpf_sub(mpfc_mpf_temp[3],mpfc_mpf_temp[1],mpfc_mpf_temp[0]);
	mpf_mul(mpfc_mpf_temp[0],x->Re,x->Re);
	mpf_mul(mpfc_mpf_temp[1],x->Im,x->Im);
	mpf_add(mpfc_mpf_temp[0],mpfc_mpf_temp[0],mpfc_mpf_temp[1]);
	mpf_div(r->Re,mpfc_mpf_temp[2],mpfc_mpf_temp[0]);
	mpf_div(r->Im,mpfc_mpf_temp[3],mpfc_mpf_temp[0]);
}
// r = y/x   WARNING: r cannot be the same as y.
void
my_div(mpf_t r, mpf_t y, mpf_t x)
{
  unsigned long prec, bits, prec0;

  prec0 = mpf_get_prec(r);

  if (prec0<=DOUBLE_PREC) {
    mpf_set_d(r, mpf_get_d(y)/mpf_get_d(x));
    return;
  }

  bits = 0;
  for (prec=prec0; prec>DOUBLE_PREC;) {
    int bit = prec&1;
    prec = (prec+bit)/2;
    bits = bits*2+bit;
  }

  mpf_set_prec_raw(t1, DOUBLE_PREC);
  mpf_ui_div(t1, 1, x);

  while (prec<prec0) {
    prec *=2;
    if (prec<prec0) {
      /* t1 = t1+t1*(1-x*t1); */
      mpf_set_prec_raw(t2, prec);
      mpf_mul(t2, x, t1);          // full x half -> full
      mpf_ui_sub(t2, 1, t2);
      mpf_set_prec_raw(t2, prec/2);
      mpf_mul(t2, t2, t1);         // half x half -> half
      mpf_set_prec_raw(t1, prec);
      mpf_add(t1, t1, t2);
    } else {
      prec = prec0;
      /* t2=y*t1, t1 = t2+t1*(y-x*t2); */
      mpf_set_prec_raw(t2, prec/2);
      mpf_mul(t2, t1, y);          // half x half -> half
      mpf_mul(r, x, t2);           // full x half -> full
      mpf_sub(r, y, r);
      mpf_mul(t1, t1, r);          // half x half -> half
      mpf_add(r, t1, t2);
      break;
    }
    prec -= (bits&1);
    bits /=2;
  }
}
Beispiel #26
0
// Computes q = exp(2 pi i tau).
static void compute_q(mpc_t q, mpc_t tau) {
  mpc_t z0;
  mpf_t f0, f1;
  mpf_ptr fp0;
  pbc_mpui pwr;

  mpc_init(z0);
  mpf_init(f0);
  mpf_init(f1);

  //compute z0 = 2 pi i tau
  mpc_set(z0, tau);
  //first remove integral part of Re(tau)
  //since exp(2 pi i)  = 1
  //it seems |Re(tau)| < 1 anyway?
  fp0 = mpc_re(z0);
  mpf_trunc(f1, fp0);
  mpf_sub(fp0, fp0, f1);

  mpc_mul_mpf(z0, z0, pi);
  mpc_mul_ui(z0, z0, 2);
  mpc_muli(z0, z0);

  //compute q = exp(z0);
  //first write z0 = A + a + b i
  //where A is a (negative) integer
  //and a, b are in [-1, 1]
  //compute e^A separately
  fp0 = mpc_re(z0);
  pwr = mpf_get_ui(fp0);
  mpf_pow_ui(f0, recipeulere, pwr);
  mpf_add_ui(fp0, fp0, pwr);

  mpf_exp(f1, mpc_re(z0));
  mpf_mul(f0, f1, f0);
  mpc_cis(q, mpc_im(z0));

  /*
  old_mpc_exp(q, z0);
  */
  mpc_mul_mpf(q, q, f0);

  mpc_clear(z0);
  mpf_clear(f0);
  mpf_clear(f1);
}
Beispiel #27
0
// The Brent-Salamin algorithm
int main(int argc, char* argv[]) {
  if (argc < 2) return -1;
  int n = (int)strtol(argv[1], NULL, 10);
  mpf_set_default_prec(1000);
  mpf_t a, b, t, c, sum;
  // a=1
  mpf_init_set_ui(a, 1);
  mpf_init_set_ui(sum, 0);
  mpf_init(b);
  mpf_init(t);
  mpf_init(c);
  mpf_init(sum);

  // b=1/sqrt(2)
  mpf_sqrt_ui(b, 2);
  mpf_ui_div(b, 1, b);

  // n次迭代的误差小于\frac{2^{n+9}}{20^{2n+1}}
  for (int i = 1; i <= n; ++i) {
    // t=(a+b)/2
    mpf_add(t, a, b);
    mpf_div_ui(t, t, 2);
    // b=sqrt(a*b);
    mpf_mul(b, a, b);
    mpf_sqrt(b, b);
    // a=t
    mpf_swap(t, a);
    mpf_mul(t, a, a);
    mpf_mul(c, b, b);
    mpf_sub(c, t, c);
    mpf_mul_2exp(c, c, i + 1);
    mpf_add(sum, sum, c);
  }
  mpf_mul(t, a, a);
  mpf_mul_ui(t, t, 4);
  mpf_ui_sub(sum, 1, sum);
  mpf_div(t, t, sum);
  mpf_out_str(stdout, 10, 0, t);
  printf("\n");
  mpf_clear(a);
  mpf_clear(b);
  mpf_clear(t);
  mpf_clear(c);
  mpf_clear(sum);
  return 0;
}
Beispiel #28
0
int main(void) {


  int i ;
  char buf[4096] ;
  mpf_t x, y , result ;
  mpf_set_default_prec(LIMIT);
  mpf_init ( x );
  mpf_init ( y );
  mpf_init (result) ;




   mpf_set_str(result, "1" , 10 );

   for ( i = 1 ; i < LIMIT ; i++ ) {
     sprintf ( buf , "%d" , (2*i + 1) ) ;
     mpf_set_str(x, buf , 10 );
     mpf_ui_div (y, 1, x) ;
     if ( i % 2 ) {
       mpf_sub ( x , result , y ) ;
       mpf_set (result , x) ;
     }
     else {
       mpf_add ( x , result , y ) ;
       mpf_set (result , x) ;
     }
   }

   mpf_mul_ui (x, result, 4 )  ;


   printf("\n pi = " ) ;
   mpf_out_str (stdout , 10, 0, x) ;
   printf ("\n") ;


   mpf_clear (x);
   mpf_clear (y);
   mpf_clear (result);

   return EXIT_SUCCESS;

}
Beispiel #29
0
void
mpf_sub_ui (mpf_ptr sum, mpf_srcptr u, unsigned long int v)
{
  __mpf_struct vv;
  mp_limb_t vl;

  if (v == 0)
    {
      mpf_set (sum, u);
      return;
    }

  vl = v;
  vv._mp_size = 1;
  vv._mp_d = &vl;
  vv._mp_exp = 1;
  mpf_sub (sum, u, &vv);
}
Beispiel #30
0
int main()
{
	char function;

  mpf_t number1, number2, output;

  mpf_init_set_ui(number1, 0);
  mpf_init_set_ui(number2, 0);
  mpf_init_set_ui(output, 0);

  while(true)
  {
      printf("enter a calculation using A +,-,*,/ B format.\n");
      gmp_scanf("%Ff%c%Ff", number1, &function, number2);

      if (function == '+')
      {
        mpf_add(output, number1, number2);
        gmp_printf("%Ff\n", output);
      }

      if (function == '-')
      {
        mpf_sub(output, number1, number2);
        gmp_printf("%Ff\n", output);
      }

      if (function == 'x' || function == '*')
      {
        mpf_mul(output, number1, number2);
        gmp_printf("%Ff\n", output);
      }

      if (function == '/')
      {
        mpf_div(output, number1, number2);
        gmp_printf("%Ff\n", output);
      }
  }

  mpf_clear(number1);
  mpf_clear(number2);
  mpf_clear(output);
}