void mpfc_abs(mpf_t r,mpfc_ptr x) { mpf_mul(mpfc_mpf_temp[0],x->Re,x->Re); mpf_mul(mpfc_mpf_temp[1],x->Im,x->Im); mpf_add(r,mpfc_mpf_temp[0],mpfc_mpf_temp[1]); mpf_sqrt(r,r); }
/*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; }
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 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; }
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); }
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 } }
void FractalTexture::updateWH(double zoom) { #ifdef ROE_FRACTAL_GMP_USE_C mpf_set_d(xPos, zoom); mpf_mul(m_width , m_width , xPos); mpf_mul(m_height, m_height, xPos); #else m_width *= zoom; m_height *= zoom; #endif }
/* r = sqrt(x) */ void my_sqrt_ui(mpf_t r, unsigned long x) { unsigned long prec, bits, prec0; prec0 = mpf_get_prec(r); if (prec0<=DOUBLE_PREC) { mpf_set_d(r, sqrt(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(x)); while (prec<prec0) { prec *=2; if (prec<prec0) { /* t1 = t1+t1*(1-x*t1*t1)/2; */ mpf_set_prec_raw(t2, prec); mpf_mul(t2, t1, t1); /* half x half -> full */ mpf_mul_ui(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); /* half x half -> half */ mpf_set_prec_raw(t1, prec); mpf_add(t1, t1, t2); } else { break; } prec -= (bits&1); bits /=2; } /* t2=x*t1, t1 = t2+t1*(x-t2*t2)/2; */ mpf_set_prec_raw(t2, prec0/2); mpf_mul_ui(t2, t1, x); mpf_mul(r, t2, t2); /* half x half -> full */ mpf_ui_sub(r, x, r); mpf_mul(t1, t1, r); /* half x half -> half */ mpf_div_2exp(t1, t1, 1); mpf_add(r, t1, t2); }
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(); } }
/* 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); }
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); }
// 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; } }
vanilla::object::ptr vanilla::int_object::mul(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_mul(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_mul(result.mpf(), lhs.mpf(), rhs->value().mpf()); return allocate_object<float_object>(std::move(result)); } default: { return object::mul(other); } } }
void mpf_mul_ui (mpf_ptr r, mpf_srcptr u, unsigned long int v) { mp_srcptr up; mp_size_t usize; mp_size_t size; mp_size_t prec, excess; mp_limb_t cy_limb, vl, cbit, cin; mp_ptr rp; usize = u->_mp_size; if (UNLIKELY (v == 0) || UNLIKELY (usize == 0)) { r->_mp_size = 0; r->_mp_exp = 0; return; } #if BITS_PER_ULONG > GMP_NUMB_BITS /* avoid warnings about shift amount */ if (v > GMP_NUMB_MAX) { mpf_t vf; mp_limb_t vp[2]; vp[0] = v & GMP_NUMB_MASK; vp[1] = v >> GMP_NUMB_BITS; PTR(vf) = vp; SIZ(vf) = 2; ASSERT_CODE (PREC(vf) = 2); EXP(vf) = 2; mpf_mul (r, u, vf); return; }
//------------------------------------------------------------------------------ // Name: //------------------------------------------------------------------------------ knumber_base *knumber_float::mul(knumber_base *rhs) { if(knumber_integer *const p = dynamic_cast<knumber_integer *>(rhs)) { knumber_float f(p); return mul(&f); } else if(knumber_float *const p = dynamic_cast<knumber_float *>(rhs)) { mpf_mul(mpf_, mpf_, p->mpf_); return this; } else if(knumber_fraction *const p = dynamic_cast<knumber_fraction *>(rhs)) { knumber_float f(p); return mul(&f); } else if(knumber_error *const p = dynamic_cast<knumber_error *>(rhs)) { if(is_zero()) { delete this; return new knumber_error(knumber_error::ERROR_UNDEFINED); } if(sign() < 0) { delete this; knumber_error *e = new knumber_error(p); return e->neg(); } else { delete this; return new knumber_error(p); } } Q_ASSERT(0); return 0; }
void FractalTexture::move(double x, double y) { x *= 0.5; y *= 0.5; #ifdef ROE_FRACTAL_GMP_USE_C mpf_set_d(xPos, x); mpf_set_d(yPos, y); mpf_mul(xPos, m_width, xPos); mpf_mul(yPos, m_width, yPos); mpf_add(m_xCenter, m_xCenter, xPos); mpf_add(m_yCenter, m_yCenter, yPos); #else m_xCenter += x*m_width; m_yCenter += y*m_height; #endif updateCorners(); }
void poly_eval_mpf(mpf_t val, mpf_t x, size_t nterms, mpf_t* coef, mpf_t *temp) { if (nterms<1 || coef==NULL) { mpf_set_d(val,1.0); return; } mpf_t* x_pow = GET_NEXT_TEMP(temp); mpf_set_d(*x_pow,1.0); mpf_set_d(val,0.0); for (size_t i=0; i<nterms; i++) { mpf_t* tmp = GET_NEXT_TEMP(temp); mpf_mul(*tmp,*x_pow, coef[i]); mpf_add(val,val,*tmp); mpf_mul(*x_pow,*x_pow,x); } return; }
static void mpf_exp(mpf_t res, mpf_t pwr) { mpf_t a; mpf_t f0; int i; mpf_init(a); mpf_set(a, pwr); mpf_init(f0); mpf_set(f0, a); mpf_add_ui(res, a, 1); for (i=2;;i++) { mpf_mul(f0, f0, a); mpf_div_ui(f0, f0, i); if (mpf_sgn(f0) > 0) { if (mpf_cmp(f0, epsilon) < 0) break; } else { if (mpf_cmp(f0, negepsilon) > 0) break; } mpf_add(res, res, f0); } mpf_clear(f0); mpf_clear(a); }
int sg_big_float_mul(sg_big_float_t *a, sg_big_float_t *b, sg_big_float_t *res) { if (!a || !b || !res) return -1; mpf_mul(res->mpf, a->mpf, b->mpf); 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]); }
void *thread2(void *param) { mpf_add(x, sqrtx, invsqrtx); mpf_div_ui(x, x, 2); mpf_add_ui(aux1, x, 1); mpf_mul(aux1 ,p, aux1); pthread_exit(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; }
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(); } }
void correction_factor_Q(mpf_t Q, int N,mpf_t rho,int k){ unsigned long int f; mpf_t tmp,tmp1,tmp2; /* if (PNK_N_1_rho_1_PN == 0) cout << "Algun factor es 0 N = " << N << " rho = " << rho << " k = " << k << " P__0 = " << P__0 << " P__N = " << P__N << endl; */ mpf_init(tmp); mpf_init_set_d(tmp1,pow(N,k-1)); // N^(k-1) f = 1; mpf_set_ui(Q,0); for (unsigned long int j = k;j < N;j++) { /*Q += (N - j) * pow(N,j) * pow(rho,j - k) * P__0 * N_k_1 / (factorial(j - k) * PNK_N_1_rho_1_PN); */ /* Q += [(N - j) * N^j * rho^(j - k) / (j - k)!] * P__0 * N_k_1 / PNK_N_1_rho_1_PN; */ mpf_mul_ui(tmp1,tmp1,N); // * N if (j - k > 1) mpf_div_ui(tmp1,tmp1,j - k); // / (j - k) mpf_mul_ui(tmp,tmp1,N - j); // * (N - j) mpf_add(Q,Q,tmp); // Q += (N - j) * N^j * rho^(j - k) / (j - k)! mpf_mul(tmp1,tmp1,rho); // * rho } mpf_init(tmp2); mpf_ui_sub(tmp2,1,P__N); // 1 - P__N mpf_pow_ui(tmp1,tmp2,k); // (1 - P__N)^k mpf_mul(tmp2,tmp2,rho); // rho * (1 - P__N) mpf_ui_sub(tmp2,1,tmp2); // 1 - rho * (1 - P__N) mpf_mul(Q,Q,P__0); // * P__0 mpf_mul_ui(Q,Q,factorial(N-k-1)); // * (N - k - 1)! /* pow(1 - P__N,k) * factorial(N) * (1 - rho * (1 - P__N)) */ mpf_mul(tmp2,tmp1,tmp2); // (1 - P__N)^k * (1 - rho * (1 - P__N)) mpf_mul_ui(tmp2,tmp2,factorial(N)); // (1 - P__N)^k * (1 - rho * (1 - P__N)) * N! mpf_div(Q,Q,tmp2); // / [(1 - P__N)^k * (1 - rho * (1 - P__N)) * N!] mpf_clear(tmp); mpf_clear(tmp1); mpf_clear(tmp2); }
void mpf_pow_ui (mpf_ptr r, mpf_srcptr b, unsigned long int e) { mpf_t b2; unsigned long int e2; mpf_init2 (b2, mpf_get_prec (r)); mpf_set (b2, b); mpf_set_ui (r, 1); if ((e & 1) != 0) mpf_set (r, b2); for (e2 = e >> 1; e2 != 0; e2 >>= 1) { mpf_mul (b2, b2, b2); if ((e2 & 1) != 0) mpf_mul (r, r, b2); } mpf_clear (b2); }
void poly_eval_mult_mpf(mpf_t val, mpf_t x, size_t nterms, mpf_t* coef, mpf_t* temp) { mpf_set_d(val,1.0); if (nterms==0 || coef==NULL) { return; } for (size_t i=0; i<nterms; i++) { mpf_t *tmp = GET_NEXT_TEMP(temp); mpf_add(*tmp, x, coef[i]); mpf_mul(val, val, *tmp); } return; }
void *thread1(void *param) { if (i != 0) { mpf_mul(aux2, y, sqrtx); mpf_add(aux2, aux2, invsqrtx); mpf_add_ui(y, y, 1); mpf_div(y, aux2, y); } mpf_add_ui(aux2, y, 1); pthread_exit(0); }
void w3j(mpf_t w, long j1, long j2, long j3, long m1, long m2, long m3) { mpq_t delta_sq,r; mpz_t i; mpf_t h; mpq_init(delta_sq); mpq_init(r); mpz_init(i); mpf_init(h); mpq_set_si(r,0,1); if(m1+m2+m3!=0) return; if((iabs(m1)>j1) || (iabs(m2)>j2) || (iabs(m3)>j3)) return; if((j3<iabs(j1-j2)) || ((j1+j2)<j3)) return; w3j_Delta_sq(delta_sq, j1, j2, j3); w3j_intterm(i, j1, j2, j3, m1, m2, m3); if(iabs(j1-j2-m3)%2 == 1) mpz_neg(i,i); w3j_sqrt_sq(r, j1, j2, j3, m1, m2, m3); mpq_mul(r,r,delta_sq); mpf_set_q(w,r); mpf_sqrt(w,w); mpf_set_z(h,i); mpf_mul(w,w,h); mpf_clear(h); mpz_clear(i); mpq_clear(r); mpq_clear(delta_sq); }