inline Mtbdd readMPQAttribute(const TiXmlNode* node, const char* att) { const std::string numberString = readStringAttribute(node, att); mpq_t gmp_value; mpq_init(gmp_value); try { size_t pos = 0; if ((pos = numberString.find('.')) != std::string::npos) { mpf_t f_value; mpf_init(f_value); mpf_set_str(f_value, numberString.c_str(), 10); mpq_set_f(gmp_value, f_value); mpf_clear(f_value); } else { mpq_set_str(gmp_value, numberString.c_str(), 10); } if (mpq_sgn(gmp_value) == 0) { mpq_clear(gmp_value); return mtbdd_false; } MTBDD res = mtbdd_gmp(gmp_value); mpq_clear(gmp_value); return res; } catch(boost::bad_lexical_cast&) { throw ParseError("[ERROR] String " + numberString + " is not a number"); } }
void check_various (void) { mpq_t x, y; mpq_init (x); mpq_init (y); mpz_realloc (mpq_numref(x), (mp_size_t) 20); mpz_realloc (mpq_denref(x), (mp_size_t) 20); mpz_realloc (mpq_numref(y), (mp_size_t) 20); mpz_realloc (mpq_denref(y), (mp_size_t) 20); /* 0 == 0 */ SET4 (x, 0,13,12,11,10, 1,23,22,21,1); SET4 (y, 0,33,32,31,30, 1,43,42,41,1); check_all (x, y, 1); /* 83/99 == 83/99 */ SET4 (x, 1,13,12,11,83, 1,23,22,21,99); SET4 (y, 1,33,32,31,83, 1,43,42,41,99); check_all (x, y, 1); /* 1:2:3:4/5:6:7 == 1:2:3:4/5:6:7 */ SET4 (x, 4,1,2,3,4, 3,88,5,6,7); SET4 (y, 4,1,2,3,4, 3,99,5,6,7); check_all (x, y, 1); /* various individual changes making != */ SET4 (x, 4,1,2,3,667, 3,88,5,6,7); SET4 (y, 4,1,2,3,4, 3,99,5,6,7); check_all (x, y, 0); SET4 (x, 4,1,2,666,4, 3,88,5,6,7); SET4 (y, 4,1,2,3,4, 3,99,5,6,7); check_all (x, y, 0); SET4 (x, 4,1,666,3,4, 3,88,5,6,7); SET4 (y, 4,1,2,3,4, 3,99,5,6,7); check_all (x, y, 0); SET4 (x, 4,667,2,3,4, 3,88,5,6,7); SET4 (y, 4,1,2,3,4, 3,99,5,6,7); check_all (x, y, 0); SET4 (x, 4,1,2,3,4, 3,88,5,6,667); SET4 (y, 4,1,2,3,4, 3,99,5,6,7); check_all (x, y, 0); SET4 (x, 4,1,2,3,4, 3,88,5,667,7); SET4 (y, 4,1,2,3,4, 3,99,5,6,7); check_all (x, y, 0); SET4 (x, 4,1,2,3,4, 3,88,666,6,7); SET4 (y, 4,1,2,3,4, 3,99,5,6,7); check_all (x, y, 0); SET4 (x, -4,1,2,3,4, 3,88,5,6,7); SET4 (y, 4,1,2,3,4, 3,99,5,6,7); check_all (x, y, 0); SET4 (x, 1,1,2,3,4, 3,88,5,6,7); SET4 (y, 4,1,2,3,4, 3,99,5,6,7); check_all (x, y, 0); mpq_clear (x); mpq_clear (y); }
void ssx_update_pi(SSX *ssx) { int m = ssx->m; int n = ssx->n; mpq_t *pi = ssx->pi; mpq_t *cbar = ssx->cbar; int p = ssx->p; int q = ssx->q; mpq_t *aq = ssx->aq; mpq_t *rho = ssx->rho; int i; mpq_t new_dq, temp; mpq_init(new_dq); mpq_init(temp); xassert(1 <= p && p <= m); xassert(1 <= q && q <= n); /* compute d[q] in the adjacent basis */ mpq_div(new_dq, cbar[q], aq[p]); /* update the vector of simplex multipliers */ for (i = 1; i <= m; i++) { if (mpq_sgn(rho[i]) == 0) continue; mpq_mul(temp, new_dq, rho[i]); mpq_sub(pi[i], pi[i], temp); } mpq_clear(new_dq); mpq_clear(temp); return; }
SEXP qsign(SEXP foo) { if (! isString(foo)) error("argument must be character"); int n = LENGTH(foo); SEXP bar, bark; PROTECT(bar = allocVector(INTSXP, n)); PROTECT(bark = ATTRIB(foo)); if (bark != R_NilValue) SET_ATTRIB(bar, duplicate(bark)); UNPROTECT(1); mpq_t value; mpq_init(value); for (int k = 0; k < n; k++) { const char *zstr = CHAR(STRING_ELT(foo, k)); if (mpq_set_str(value, zstr, 10) == -1) { mpq_clear(value); error("error converting string to GMP rational"); } mpq_canonicalize(value); INTEGER(bar)[k] = mpq_sgn(value); } mpq_clear(value); UNPROTECT(1); return(bar); }
void bisect_sfdlProverExo::exogenous_fAtMidpt(mpq_t& f, mpq_t* a, mpq_t* b) { mpq_t t2; mpq_t t3; alloc_init_scalar(t2); alloc_init_scalar(t3); mpq_set_ui(f, 0, 1); int m = bisect_sfdl_cons::m; int rctr = 0; for(int i = 0; i < m; i++) { mpq_add(t2, a[i], b[i]); mpq_set_si(t3, bisect_sfdl_cons::fAtMidpt_FUNCTION_STATIC_RANDOM_INT[rctr++], 1); mpq_mul(t2, t3, t2); mpq_div_2exp(t2, t2, 1); mpq_add(f, f, t2); } for(int i = 0; i < m; i++) { for(int j = 0; j < m; j++) { mpq_add(t2, a[i], b[i]); mpq_add(t3, a[j], b[j]); mpq_mul(t2, t2, t3); mpq_set_si(t3, bisect_sfdl_cons::fAtMidpt_FUNCTION_STATIC_RANDOM_INT[rctr++], 1); mpq_mul(t2, t2, t3); mpq_div_2exp(t2, t2, 2); mpq_add(f, f, t2); } } mpq_clear(t2); mpq_clear(t3); }
void check_random () { gmp_randstate_ptr rands; mpz_t bs; unsigned long arg_size, size_range; mpq_t q, r; int i; mp_bitcnt_t shift; int reps = 10000; rands = RANDS; mpz_init (bs); mpq_init (q); mpq_init (r); for (i = 0; i < reps; i++) { mpz_urandomb (bs, rands, 32); size_range = mpz_get_ui (bs) % 11 + 2; /* 0..4096 bit operands */ mpz_urandomb (bs, rands, size_range); arg_size = mpz_get_ui (bs); mpz_rrandomb (mpq_numref (q), rands, arg_size); do { mpz_urandomb (bs, rands, size_range); arg_size = mpz_get_ui (bs); mpz_rrandomb (mpq_denref (q), rands, arg_size); } while (mpz_sgn (mpq_denref (q)) == 0); /* We now have a random rational in q, albeit an unnormalised one. The lack of normalisation should not matter here, so let's save the time a gcd would require. */ mpz_urandomb (bs, rands, 32); shift = mpz_get_ui (bs) % 4096; mpq_mul_2exp (r, q, shift); if (mpq_cmp (r, q) < 0) { printf ("mpq_mul_2exp wrong on random\n"); abort (); } mpq_div_2exp (r, r, shift); if (mpq_cmp (r, q) != 0) { printf ("mpq_mul_2exp or mpq_div_2exp wrong on random\n"); abort (); } } mpq_clear (q); mpq_clear (r); mpz_clear (bs); }
void check_rand (void) { mpq_t x, y, want_add, want_sub; int i; gmp_randstate_ptr rands = RANDS; mpq_init (x); mpq_init (y); mpq_init (want_add); mpq_init (want_sub); for (i = 0; i < 500; i++) { mpz_errandomb (mpq_numref(x), rands, 512L); mpz_errandomb_nonzero (mpq_denref(x), rands, 512L); mpq_canonicalize (x); mpz_errandomb (mpq_numref(y), rands, 512L); mpz_errandomb_nonzero (mpq_denref(y), rands, 512L); mpq_canonicalize (y); refmpq_add (want_add, x, y); refmpq_sub (want_sub, x, y); check_all (x, y, want_add, want_sub); } mpq_clear (x); mpq_clear (y); mpq_clear (want_add); mpq_clear (want_sub); }
void game_get_alpha(game_t *game, mpq_t alpha) { mpq_t max_t_1; mpq_t min_s_0; mpq_init(max_t_1); mpq_init(min_s_0); //MAX(game->t, 1) if(mpq_cmp_si(game->t, 1, 1) > 0) { mpq_set(max_t_1, game->t); } else { mpq_set_si(max_t_1, 1, 1); } //MIN(game->s, 0) if(mpq_cmp_si(game->s, 0, 1) < 0) { mpq_set(min_s_0, game->s); } else { mpq_set_si(min_s_0, 0, 1); } //return MAX(game->t, 1) - MIN(game->s, 0); mpq_sub(alpha, max_t_1, min_s_0); mpq_clear(max_t_1); mpq_clear(min_s_0); }
int main (int argc, char **argv) { mpfr_t x, y; mpq_t a, b; mpfi_t i; mpq_init (a); mpq_init (b); mpfi_init2 (i, 53); mpfr_init2 (x, 53); mpfr_init2 (y, 53); mpq_set_si (a, -1, 3); mpq_set_ui (b, +1, 1024); mpfr_set_q (x, a, MPFI_RNDD); mpfr_set_q (y, b, MPFI_RNDU); check (i, a, b, x, y, MPFI_FLAGS_LEFT_ENDPOINT_INEXACT); check (i, b, a, x, y, MPFI_FLAGS_LEFT_ENDPOINT_INEXACT); check (i, b, b, y, y, MPFI_FLAGS_BOTH_ENDPOINTS_EXACT); mpfr_set_q (y, a, MPFI_RNDU); check (i, a, a, x, y, MPFI_FLAGS_BOTH_ENDPOINTS_INEXACT); mpq_clear (a); mpq_clear (b); mpfi_clear (i); mpfr_clear (x); mpfr_clear (y); return 0; }
void ssx_eval_bbar(SSX *ssx) { int m = ssx->m; int n = ssx->n; mpq_t *coef = ssx->coef; int *A_ptr = ssx->A_ptr; int *A_ind = ssx->A_ind; mpq_t *A_val = ssx->A_val; int *Q_col = ssx->Q_col; mpq_t *bbar = ssx->bbar; int i, j, k, ptr; mpq_t x, temp; mpq_init(x); mpq_init(temp); /* bbar := 0 */ for (i = 1; i <= m; i++) mpq_set_si(bbar[i], 0, 1); /* bbar := - N * xN = - N[1] * xN[1] - ... - N[n] * xN[n] */ for (j = 1; j <= n; j++) { ssx_get_xNj(ssx, j, x); if (mpq_sgn(x) == 0) continue; k = Q_col[m+j]; /* x[k] = xN[j] */ if (k <= m) { /* N[j] is a column of the unity matrix I */ mpq_sub(bbar[k], bbar[k], x); } else { /* N[j] is a column of the original constraint matrix -A */ for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++) { mpq_mul(temp, A_val[ptr], x); mpq_add(bbar[A_ind[ptr]], bbar[A_ind[ptr]], temp); } } } /* bbar := inv(B) * bbar */ bfx_ftran(ssx->binv, bbar, 0); #if 1 /* compute value of the objective function */ /* bbar[0] := c[0] */ mpq_set(bbar[0], coef[0]); /* bbar[0] := bbar[0] + sum{i in B} cB[i] * xB[i] */ for (i = 1; i <= m; i++) { k = Q_col[i]; /* x[k] = xB[i] */ if (mpq_sgn(coef[k]) == 0) continue; mpq_mul(temp, coef[k], bbar[i]); mpq_add(bbar[0], bbar[0], temp); } /* bbar[0] := bbar[0] + sum{j in N} cN[j] * xN[j] */ for (j = 1; j <= n; j++) { k = Q_col[m+j]; /* x[k] = xN[j] */ if (mpq_sgn(coef[k]) == 0) continue; ssx_get_xNj(ssx, j, x); mpq_mul(temp, coef[k], x); mpq_add(bbar[0], bbar[0], temp); } #endif mpq_clear(x); mpq_clear(temp); return; }
void gmp_exit() { mpq_clear(const_zero); mpq_clear(const_one); mpq_clear(const_minus_one); pool_exit(); }
void lp_free(LP* lp) { int i; free(lp->name); free(lp->obj_name); if (lp->row_name != NULL) { for (i = 0; i < lp->rows; i ++) free(lp->row_name[i]); free(lp->row_name); hash_str_free(&lp->hash_str_row_name); } if (lp->var_name != NULL) { for (i = 0; i < lp->vars; i ++) free(lp->var_name[i]); free(lp->var_name); hash_str_free(&lp->hash_str_var_name); } free(lp->row_equality); matrix_free(lp->A); vector_free(&lp->b); vector_free(&lp->x); vector_free(&lp->xb); vector_free(&lp->c); vector_free(&lp->cb); vector_free(&lp->c_back); mpq_clear(lp->c_const); for (i = 0; i < lp->vars; i ++) { if (lp->upper.is_valid[i]) mpq_clear(lp->upper.bound[i]); if (lp->lower.is_valid[i]) mpq_clear(lp->lower.bound[i]); } free(lp->upper.is_valid); free(lp->lower.is_valid); free(lp->upper.bound); free(lp->lower.bound); free(lp->is_basis); free(lp->basis_column); free(lp->is_integer); if(lp->slackref != NULL) free(lp->slackref); mpq_clear(lp->q_work); free(lp->A_d); vector_free(&lp->b_back); vector_d_free(&lp->c_d); free(lp->var_type); if (lp->eta) eta_file_free(lp->eta); if(lp->hash_str_row_name != NULL) hash_str_free(&lp->hash_str_row_name); if(lp->hash_str_var_name != NULL) hash_str_free(&lp->hash_str_var_name); my_hash_mpq_free(); free(lp); }
gint random_integer_with_probability(mpq_t *p, gint n) { mpz_t *den; den = (mpz_t *)g_malloc(sizeof(mpz_t) * n); int i; for(i = 0; i < n; ++i) { mpz_init(den[i]); mpq_get_den(den[i], p[i]); } mpz_t l; mpz_init(l); lcm(l, den, n); mpq_t nlcm; mpq_init(nlcm); mpq_set_z(nlcm, l); int lc = mpz_get_ui(l); gint x = g_random_int_range(0, lc); mpq_t y; mpq_init(y); mpq_set_si(y, x, 1); mpq_t p_i; mpq_init(p_i); int j = n - 1; for(i = 0; i < n - 1; i++) { mpq_mul(p_i, nlcm, p[i]); if(mpq_cmp(y, p_i) < 0) { j = i; break; } mpq_sub(y, y, p_i); } for(i = 0; i < n; ++i) { mpz_clear(den[i]); } g_free(den); mpz_clear(l); mpq_clear(nlcm); mpq_clear(y); mpq_clear(p_i); return j; }
static void from_double(mpq_t n, double f, int level) { double fa; int nfa; mpq_t ni, nn; bool neg; //fprintf(stderr, "from_double: %.14g\n", f); if (level >= 10) goto __DEFAULT; fa = fabs(f); if (fa >= 1E8 || fa <= 1E-8) goto __DEFAULT; neg = (f < 0); nfa = (int)fa; if (nfa >= 1) fa -= nfa; //fprintf(stderr, "fa = %.14g %.14g\n", fa, (fa*1E8) - (int)(fa*1E8)); if (nfa && fa < 1E-8) { mpq_set_si(n, 0, 1); } else if (((fa*1E8) - (int)(fa*1E8)) < 1E-8) { mpq_set_si(n, (int)(fa*1E8), 100000000); } else { mpq_init(ni); from_double(ni, 1 / fa, level + 1); mpq_inv(n, ni); mpq_clear(ni); } mpq_init(nn); mpq_set_si(nn, nfa, 1); mpq_add(n, n, nn); mpq_clear(nn); if (neg) mpq_neg(n, n); mpq_canonicalize(n); return; __DEFAULT: mpq_set_d(n, f); }
/*lint -e{818} supress "Pointer parameter 'var' could be declared as pointing to const" */ static void remove_fixed_var( Lps* lp, Var* var, int verbose_level) { Nzo* nzo; mpq_t x; mpq_t temp; Bool is_zero; assert(lp != NULL); assert(var != NULL); assert(var->type == VAR_FIXED && mpq_equal(var->lower, var->upper)); if (verbose_level > 0) printf("Removing variable %s fixed to %g\n", var->name, mpq_get_d(var->lower)); mpq_init(x); mpq_init(temp); is_zero = mpq_equal(var->lower, x /* zero */); while(var->first != NULL) { nzo = var->first; /* Do we have to ajust the lhs/rhs ? */ if (!is_zero) { mpq_mul(x, var->lower, nzo->value); /* do we have a valid rhs ? */ if (HAS_RHS(nzo->con)) { mpq_sub(temp, nzo->con->rhs, x); lps_setrhs(nzo->con, temp); } /* do we have a valid lhs ? */ if (HAS_LHS(nzo->con)) { mpq_sub(temp, nzo->con->lhs, x); lps_setlhs(nzo->con, temp); } } lps_delnzo(lp, nzo); } mpq_clear(temp); mpq_clear(x); }
void game_free(game_t *game) { graph_free(game->graph); mpq_clear(game->s); mpq_clear(game->t); mpq_clear(game->p_c); g_free(game->initial_config); g_free(game->current_config); g_free(game->next_config); g_free(game); }
void check_various (void) { mpf_t got; mpq_t q; mpf_init (got); mpq_init (q); /* 1/1 == 1 */ mpf_set_prec (got, 20L); mpq_set_ui (q, 1L, 1L); mpf_set_q (got, q); MPF_CHECK_FORMAT (got); ASSERT_ALWAYS (mpf_cmp_ui (got, 1L) == 0); /* 1/(2^n+1), a case where truncating the divisor would be wrong */ mpf_set_prec (got, 500L); mpq_set_ui (q, 1L, 1L); mpz_mul_2exp (mpq_denref(q), mpq_denref(q), 800L); mpz_add_ui (mpq_denref(q), mpq_denref(q), 1L); check_one (got, q); mpf_clear (got); mpq_clear (q); }
RCP<const Number> bernoulli(unsigned long n) { #ifdef HAVE_SYMENGINE_ARB fmpq_t res; fmpq_init(res); bernoulli_fmpq_ui(res, n); mpq_t a; mpq_init(a); fmpq_get_mpq(a, res); rational_class b(a); fmpq_clear(res); mpq_clear(a); return Rational::from_mpq(std::move(b)); #else // TODO: implement a faster algorithm std::vector<rational_class> v(n + 1); for (unsigned m = 0; m <= n; ++m) { v[m] = rational_class(1, m + 1); for (unsigned j = m; j >= 1; --j) { v[j - 1] = j * (v[j - 1] - v[j]); } } return Rational::from_mpq(v[0]); #endif }
/* Shamelessly stolen, as it is, from libtommath */ int mpq_init_multi(mp_rat * mp, ...) { mp_err res = MP_OKAY; /* Assume ok until proven otherwise */ int n = 0; /* Number of ok inits */ mp_rat *cur_arg = mp; va_list args; va_start(args, mp); /* init args to next argument from caller */ while (cur_arg != NULL) { if (mpq_init(cur_arg) != MP_OKAY) { /* Oops - error! Back-track and mp_clear what we already * succeeded in init-ing, then return error. */ va_list clean_args; /* end the current list */ va_end(args); /* now start cleaning up */ cur_arg = mp; va_start(clean_args, mp); while (n--) { mpq_clear(cur_arg); cur_arg = va_arg(clean_args, mp_rat *); } va_end(clean_args); res = MP_MEM; break; } n++; cur_arg = va_arg(args, mp_rat *); }
void check_all (mpq_ptr x, mpq_ptr y, mpq_ptr want_add, mpq_ptr want_sub) { mpq_t got; int neg_x, neg_y, swap; mpq_init (got); MPQ_CHECK_FORMAT (want_add); MPQ_CHECK_FORMAT (want_sub); MPQ_CHECK_FORMAT (x); MPQ_CHECK_FORMAT (y); for (swap = 0; swap <= 1; swap++) { for (neg_x = 0; neg_x <= 1; neg_x++) { for (neg_y = 0; neg_y <= 1; neg_y++) { mpq_add (got, x, y); MPQ_CHECK_FORMAT (got); if (! mpq_equal (got, want_add)) { printf ("mpq_add wrong\n"); mpq_trace (" x ", x); mpq_trace (" y ", y); mpq_trace (" got ", got); mpq_trace (" want", want_add); abort (); } mpq_sub (got, x, y); MPQ_CHECK_FORMAT (got); if (! mpq_equal (got, want_sub)) { printf ("mpq_sub wrong\n"); mpq_trace (" x ", x); mpq_trace (" y ", y); mpq_trace (" got ", got); mpq_trace (" want", want_sub); abort (); } mpq_neg (y, y); mpq_swap (want_add, want_sub); } mpq_neg (x, x); mpq_swap (want_add, want_sub); mpq_neg (want_add, want_add); mpq_neg (want_sub, want_sub); } mpq_swap (x, y); mpq_neg (want_sub, want_sub); } mpq_clear (got); }
SEXP d2q(SEXP foo) { if (! isReal(foo)) error("argument must be real"); int n = LENGTH(foo); int i; for (i = 0; i < n; i++) if (! R_finite(REAL(foo)[i])) error("argument not finite-valued"); SEXP bar, bark; PROTECT(bar = allocVector(STRSXP, n)); PROTECT(bark = ATTRIB(foo)); if (bark != R_NilValue) SET_ATTRIB(bar, duplicate(bark)); UNPROTECT(1); mpq_t value; mpq_init(value); int k; for (k = 0; k < n; k++) { double z = REAL(foo)[k]; mpq_set_d(value, z); char *zstr = NULL; zstr = mpq_get_str(zstr, 10, value); SET_STRING_ELT(bar, k, mkChar(zstr)); free(zstr); } mpq_clear(value); UNPROTECT(1); return(bar); }
static void check0 (void) { mpq_t y; mpfr_t x; int inexact; int r; /* Check for +0 */ mpfr_init (x); mpq_init (y); mpq_set_si (y, 0, 1); for (r = 0; r < MPFR_RND_MAX; r++) { mpfr_clear_flags (); inexact = mpfr_set_q (x, y, (mpfr_rnd_t) r); if (!MPFR_IS_ZERO(x) || !MPFR_IS_POS(x) || inexact || __gmpfr_flags != 0) { printf("mpfr_set_q(x,0) failed for %s\n", mpfr_print_rnd_mode ((mpfr_rnd_t) r)); exit(1); } } mpfr_clear (x); mpq_clear (y); }
void game_update_with_proportional_imitate(game_t *game) { int n = game->graph->n; int i; for(i = 0; i < n; ++i) { int n_i = graph_number_of_neighbours_of(game->graph, i); int k = random_integer(n_i); mpq_t p_i; mpq_init(p_i); int j = GPOINTER_TO_INT(g_slist_nth_data(game->graph->edges[i], k)); game_compute_p_i(game, i, j, p_i); gboolean imitate = random_boolean_with_probability(p_i); //printf("%d copying %d\n", i, j); if(imitate) { //printf("%d copied %d\n", i, j); game->next_config[i] = game->current_config[j]; } else { game->next_config[i] = game->current_config[i]; } mpq_clear(p_i); } for(i = 0; i < n; ++i) { game->current_config[i] = game->next_config[i]; } }
void game_get_payoff_of_player(game_t *game, int i, mpq_t payoff) { GSList *list = graph_get_neighbours_of(game->graph, i); int n = g_slist_length(list) + 1; int delta = game_get_number_of_cooperators_in_list(game, list); delta += game->current_config[i]; //printf("n= %d, d= %d", n, delta); mpq_t tmp; mpq_init(tmp); //double payoff = 0; if(game->current_config[i] == COOPERATE) { //int pay = (delta - 1) + ((n - delta) * -1); //printf("pay = %d", pay); //payoff = (delta - 1) + (n - delta) * game->s; mpq_set_si(tmp, (n - delta), 1); mpq_mul(tmp, game->s, tmp); mpq_set(payoff, tmp); mpq_set_si(tmp, (delta - 1), 1); mpq_add(payoff, tmp, payoff); } else { //payoff = delta * game->t; mpq_set_si(tmp, delta, 1); mpq_mul(payoff, tmp, game->t); } mpq_clear(tmp); }
void check_data (void) { static const struct { int base; const char *num; const char *den; const char *want; } data[] = { { 10, "0", "1", "0" }, { 10, "1", "1", "1" }, { 16, "ffffffff", "1", "ffffffff" }, { 16, "ffffffffffffffff", "1", "ffffffffffffffff" }, { 16, "1", "ffffffff", "1/ffffffff" }, { 16, "1", "ffffffffffffffff", "1/ffffffffffffffff" }, { 16, "1", "10000000000000003", "1/10000000000000003" }, { 10, "12345678901234567890", "9876543210987654323", "12345678901234567890/9876543210987654323" }, }; mpq_t q; int i; mpq_init (q); for (i = 0; i < numberof (data); i++) { mpz_set_str_or_abort (mpq_numref(q), data[i].num, data[i].base); mpz_set_str_or_abort (mpq_denref(q), data[i].den, data[i].base); check_all (q, data[i].base, data[i].want); } mpq_clear (q); }
void subtract_multiple(ElementType& result, const ElementType& a, const ElementType& b) const { mpq_t tmp; mpq_init(tmp); mpq_mul(tmp, &a, &b); mpq_sub(&result, &result, tmp); mpq_clear(tmp); }
void fmpz_poly_disc_gauss_rounding(fmpz_poly_t rop, const fmpq_poly_t x, const mpfr_t r_f, gmp_randstate_t randstate) { mpfr_t xi; mpfr_init2(xi, mpfr_get_prec(r_f)); mpf_t xi_f; mpf_init2(xi_f, mpfr_get_prec(r_f)); mpq_t xi_q; mpq_init(xi_q); mpz_t s_z; mpz_init(s_z); const long n = fmpq_poly_length(x); const size_t tau = (ceil(2*sqrt(log2((double)n))) > 3) ? ceil(2*sqrt(log2((double)n))) : 3; fmpz_poly_zero(rop); for(int i=0; i<n; i++) { fmpq_poly_get_coeff_mpq(xi_q, x, i); mpf_set_q(xi_f, xi_q); mpfr_set_f(xi, xi_f, MPFR_RNDN); dgs_disc_gauss_mp_t *D = dgs_disc_gauss_mp_init(r_f, xi, tau, DGS_DISC_GAUSS_UNIFORM_ONLINE); D->call(s_z, D, randstate); dgs_disc_gauss_mp_clear(D); fmpz_poly_set_coeff_mpz(rop, i, s_z); } mpz_clear(s_z); mpq_clear(xi_q); mpf_clear(xi_f); mpfr_clear(xi); }
void check_one (mpq_srcptr want, int base, const char *str) { mpq_t got; MPQ_CHECK_FORMAT (want); mp_trace_base = base; mpq_init (got); if (mpq_set_str (got, str, base) != 0) { printf ("mpq_set_str unexpectedly failed\n"); printf (" base %d\n", base); printf (" str \"%s\"\n", str); abort (); } MPQ_CHECK_FORMAT (got); if (! mpq_equal (got, want)) { printf ("mpq_set_str wrong\n"); printf (" base %d\n", base); printf (" str \"%s\"\n", str); mpq_trace ("got ", got); mpq_trace ("want", want); abort (); } mpq_clear (got); }
void check_samples (void) { mpq_t q; mpq_init (q); mpq_set_ui (q, 0L, 1L); check_one (q, 10, "0"); check_one (q, 10, "0/1"); check_one (q, 10, "0 / 1"); check_one (q, 0, "0x0/ 1"); check_one (q, 0, "0x0/ 0x1"); check_one (q, 0, "0 / 0x1"); check_one (q, 10, "-0"); check_one (q, 10, "-0/1"); check_one (q, 10, "-0 / 1"); check_one (q, 0, "-0x0/ 1"); check_one (q, 0, "-0x0/ 0x1"); check_one (q, 0, "-0 / 0x1"); mpq_set_ui (q, 255L, 256L); check_one (q, 10, "255/256"); check_one (q, 0, "0xFF/0x100"); check_one (q, 16, "FF/100"); mpq_neg (q, q); check_one (q, 10, "-255/256"); check_one (q, 0, "-0xFF/0x100"); check_one (q, 16, "-FF/100"); mpq_clear (q); }
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); }