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 tree_dbProverExo::baseline(const mpq_t* input_q, int num_inputs, mpq_t* output_recomputed, int num_outputs) { struct In input; struct Out output; for(int i = 0; i < num_inputs; i++) { input.hash.bit[i] = mpz_get_ui(mpq_numref(input_q[i])); } compute(&input, &output); mpq_set_si(output_recomputed[0], 0, 1); mpq_set_si(output_recomputed[1], output.rows, 1); for (int i = 0; i < 3; i++) { mpq_set_ui(output_recomputed[2 + i * 10 + 0], output.result[i].KEY, 1); mpq_set_ui(output_recomputed[2 + i * 10 + 1], output.result[i].FName, 1); mpq_set_ui(output_recomputed[2 + i * 10 + 2], output.result[i].LName, 1); mpq_set_ui(output_recomputed[2 + i * 10 + 3], output.result[i].Age, 1); mpq_set_ui(output_recomputed[2 + i * 10 + 4], output.result[i].Major, 1); mpq_set_ui(output_recomputed[2 + i * 10 + 5], output.result[i].State, 1); mpq_set_ui(output_recomputed[2 + i * 10 + 6], output.result[i].PhoneNum, 1); mpq_set_ui(output_recomputed[2 + i * 10 + 7], output.result[i].Class, 1); mpq_set_ui(output_recomputed[2 + i * 10 + 8], output.result[i].Credits, 1); mpq_set_ui(output_recomputed[2 + i * 10 + 9], output.result[i].Average, 1); } }
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); }
//Refer to apps_sfdl_gen/cql_rw_si_cons.h for constants to use when generating input. // this name stands for cql read/write select insert. void cql_rw_siVerifierInpGenHw::create_input(mpq_t* input_q, int num_inputs) { srand(time(NULL)); mpq_t* full_db_handle; int num_ints = sizeof(Student_handle_t) / sizeof(uint64_t); alloc_init_vec(&full_db_handle, num_ints); Student_handle_t handle; if (generate_states) { // SIZE should be a power of 2. int number_of_rows = SIZE - 1; // get the full handle of a DB. handle = create_db(number_of_rows, ("prover_1_" + shared_bstore_file_name).c_str()); uint64_t* input_ptr = (uint64_t*)&handle; for(int i = 0; i < num_ints; i++) { mpq_set_ui(full_db_handle[i], input_ptr[i], 1); } dump_vector(num_ints, full_db_handle, "db_handle", FOLDER_PERSIST_STATE); } else { // import the root hash from a place. load_vector(num_ints, full_db_handle, "db_handle", FOLDER_PERSIST_STATE); uint64_t* input_ptr = (uint64_t*)&handle; for(int i = 0; i < num_ints; i++) { input_ptr[i] = mpz_get_ui(mpq_numref(full_db_handle[i])); } } struct In input; Student_handle_t empty_handle; memset(&input, 0, sizeof(input)); // get a succinct handle of a DB using hashput. char db_file_path[BUFLEN]; snprintf(db_file_path, BUFLEN - 1, "%s/block_stores/prover_1_%s", FOLDER_STATE, shared_bstore_file_name.c_str()); HashBlockStore* bs = new ConfigurableBlockStore(db_file_path); hashput2(bs, &(input.db_handle), &handle); delete bs; // assign it to input_q uint64_t* input_ptr = (uint64_t*)&input.db_handle; int number_of_hash_elements = sizeof(hash_t) / sizeof(uint64_t); for(int i = 0; i < number_of_hash_elements; i++) { mpq_set_ui(input_q[i], input_ptr[i], 1); } for (int i = number_of_hash_elements; i < num_inputs; i++) { mpq_set_ui(input_q[i], rand(), 1); } clear_del_vec(full_db_handle, num_ints); }
static void test_cmp_q (mpfr_prec_t pmin, mpfr_prec_t pmax, int nmax) { mpfr_t x, z; mpq_t y; mpfr_prec_t p; int res1, res2; int n; mpfr_init (x); mpfr_init2 (z, MPFR_PREC_MIN); mpq_init (y); /* check the erange flag when x is NaN */ mpfr_set_nan (x); mpq_set_ui (y, 17, 1); mpfr_clear_erangeflag (); res1 = mpfr_cmp_q (x, y); if (res1 != 0 || mpfr_erangeflag_p () == 0) { printf ("Error for mpfr_cmp_q (NaN, 17)\n"); printf ("Return value: expected 0, got %d\n", res1); printf ("Erange flag: expected set, got %d\n", mpfr_erangeflag_p ()); exit (1); } for(p=pmin ; p < pmax ; p++) { mpfr_set_prec (x, p); for (n = 0 ; n < nmax ; n++) { mpfr_urandomb (x, RANDS); mpq_set_ui (y, randlimb (), randlimb() ); if (!MPFR_IS_SINGULAR (x)) { mpfr_sub_q (z, x, y, MPFR_RNDN); res1 = mpfr_sgn (z); res2 = mpfr_cmp_q (x, y); if (res1 != res2) { printf("Error for mpfr_cmp_q: res=%d sub_z gives %d\n", res2, res1); exit (1); } } } } mpq_clear (y); mpfr_clear (x); mpfr_clear (z); }
void test_gc_get_rational (void* data) { mpq_t* num = GC_NEW_RATIONAL(runtime); mpq_t* num2 = GC_NEW_RATIONAL(runtime); mpq_set_ui(*num, 2, 3); mpq_set_ui(*num2, 2, 3); mpq_add(*num, *num, *num2); tt_assert(mpq_cmp_ui(*num, 4, 3) == 0); end:; }
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 tollingProverExo::baseline(const mpq_t* input_q, int num_inputs, mpq_t* output_recomputed, int num_outputs) { struct In input; struct Out output; // Fill code here to prepare input from input_q. int inp = 0; for(int i = 0; i < NUM_CK_BITS/8; i++) { input.commitmentCK.bit[i] = mpz_get_ui(mpq_numref(input_q[inp++])); } for(int i = 0; i < NUM_COMMITMENT_CHUNKS; i++) { input.commitment.bit[i] = mpz_get_ui(mpq_numref(input_q[inp++])); } for(int i = 0; i < tolling_cons::MAX_SPOTCHECKS; i++) { tuple_t* tuple = &input.verifier_in.spotchecks[i]; tuple->time = mpz_get_si(mpq_numref(input_q[inp++])); tuple->toll_booth_id = mpz_get_ui(mpq_numref(input_q[inp++])); tuple->toll = mpz_get_ui(mpq_numref(input_q[inp++])); } input.verifier_in.time_threshold = mpz_get_si(mpq_numref(input_q[inp++])); // Do the computation compute(&input, &output); // Fill code here to dump output to output_recomputed. mpq_set_si(output_recomputed[0], 0, 1); mpq_set_si(output_recomputed[1], output.rejected, 1); mpq_set_ui(output_recomputed[2], output.cost, 1); }
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); }
//Refer to apps_sfdl_gen/ram_hybrid_micro_cons.h for constants to use when generating input. void ram_hybrid_microVerifierInpGenHw::create_input(mpq_t* input_q, int num_inputs) { #if IS_REDUCER == 0 //Default implementation is provided by compiler compiler_implementation.create_input(input_q, num_inputs); #endif // setup the Merkle root when the hybrid choose Merkle-tree based // solution if (num_inputs > 1) { ConfigurableBlockStore bs; RAMImpl ram(&bs); HashType* hash = ram.getRootHash(); int i = 0; for (HashType::HashVec::const_iterator itr = hash->GetFieldElts().begin(); itr != hash->GetFieldElts().end(); ++itr) { mpz_set(mpq_numref(input_q[i]), (*itr).get_mpz_t()); mpq_canonicalize(input_q[i]); i++; } } // set the address for RAM operation to be 0 mpq_set_ui(input_q[num_inputs-1], 0, 1); // states that should be persisted and may not be generated everytime should be created here. if (generate_states) { } }
static void test_specialq (mpfr_prec_t p0, mpfr_prec_t p1, unsigned int N, int (*mpfr_func)(mpfr_ptr, mpfr_srcptr, mpq_srcptr, mpfr_rnd_t), void (*mpq_func)(mpq_ptr, mpq_srcptr, mpq_srcptr), const char *op) { mpfr_t fra, frb, frq; mpq_t q1, q2, qr; unsigned int n; mpfr_prec_t prec; for (prec = p0 ; prec < p1 ; prec++) { mpfr_inits2 (prec, fra, frb, frq, (mpfr_ptr) 0); mpq_init (q1); mpq_init(q2); mpq_init (qr); for( n = 0 ; n < N ; n++) { mpq_set_ui(q1, randlimb(), randlimb() ); mpq_set_ui(q2, randlimb(), randlimb() ); mpq_canonicalize (q1); mpq_canonicalize (q2); mpq_func (qr, q1, q2); mpfr_set_q (fra, q1, MPFR_RNDD); mpfr_func (fra, fra, q2, MPFR_RNDD); mpfr_set_q (frb, q1, MPFR_RNDU); mpfr_func (frb, frb, q2, MPFR_RNDU); mpfr_set_q (frq, qr, MPFR_RNDN); /* We should have fra <= qr <= frb */ if ( (mpfr_cmp(fra, frq) > 0) || (mpfr_cmp (frq, frb) > 0)) { printf("Range error for prec=%lu and %s", (unsigned long) prec, op); printf ("\nq1="); mpq_out_str(stdout, 2, q1); printf ("\nq2="); mpq_out_str(stdout, 2, q2); printf ("\nfr_dn="); mpfr_print_binary (fra); printf ("\nfr_q ="); mpfr_print_binary (frq); printf ("\nfr_up="); mpfr_print_binary (frb); putchar('\n'); exit (1); } } mpq_clear (q1); mpq_clear (q2); mpq_clear (qr); mpfr_clears (fra, frb, frq, (mpfr_ptr) 0); } }
/* Check various values 2^n and 1/2^n. */ void check_onebit (void) { static const long data[] = { -3*GMP_NUMB_BITS-1, -3*GMP_NUMB_BITS, -3*GMP_NUMB_BITS+1, -2*GMP_NUMB_BITS-1, -2*GMP_NUMB_BITS, -2*GMP_NUMB_BITS+1, -GMP_NUMB_BITS-1, -GMP_NUMB_BITS, -GMP_NUMB_BITS+1, -5, -2, -1, 0, 1, 2, 5, GMP_NUMB_BITS-1, GMP_NUMB_BITS, GMP_NUMB_BITS+1, 2*GMP_NUMB_BITS-1, 2*GMP_NUMB_BITS, 2*GMP_NUMB_BITS+1, 3*GMP_NUMB_BITS-1, 3*GMP_NUMB_BITS, 3*GMP_NUMB_BITS+1, }; int i, neg; long exp, l; mpq_t q; double got, want; mpq_init (q); for (i = 0; i < numberof (data); i++) { exp = data[i]; mpq_set_ui (q, 1L, 1L); if (exp >= 0) mpq_mul_2exp (q, q, exp); else mpq_div_2exp (q, q, -exp); want = 1.0; for (l = 0; l < exp; l++) want *= 2.0; for (l = 0; l > exp; l--) want /= 2.0; for (neg = 0; neg <= 1; neg++) { if (neg) { mpq_neg (q, q); want = -want; } got = mpq_get_d (q); if (got != want) { printf ("mpq_get_d wrong on %s2**%ld\n", neg ? "-" : "", exp); mpq_trace (" q ", q); d_trace (" want ", want); d_trace (" got ", got); abort(); } } } mpq_clear (q); }
void ovm_q_mul(oregister_t *l, oregister_t *r) { switch (r->t) { case t_void: l->t = t_word; l->v.w = 0; break; case t_word: mpq_set_si(oqr(r), r->v.w, 1); mpq_mul(oqr(l), oqr(l), oqr(r)); check_mpq(l); break; case t_float: l->t = t_float; l->v.d = mpq_get_d(oqr(l)) * r->v.d; break; case t_mpz: mpz_set_ui(ozs(r), 1); mpq_mul(oqr(l), oqr(l), oqr(r)); check_mpq(l); break; case t_rat: mpq_set_si(oqr(r), rat_num(r->v.r), rat_den(r->v.r)); mpq_mul(oqr(l), oqr(l), oqr(r)); check_mpq(l); break; case t_mpq: mpq_mul(oqr(l), oqr(l), oqr(r)); check_mpq(l); break; case t_mpr: l->t = t_mpr; mpfr_set_q(orr(l), oqr(l), thr_rnd); mpfr_mul(orr(l), orr(l), orr(r), thr_rnd); break; case t_cdd: l->t = t_cdd; l->v.dd = mpq_get_d(oqr(l)) * r->v.dd; check_cdd(l); break; case t_cqq: l->t = t_cqq; mpq_set_ui(oqi(l), 0, 1); cqq_mul(oqq(l), oqq(l), oqq(r)); check_cqq(l); break; case t_mpc: l->t = t_mpc; mpc_set_q(occ(l), oqr(l), thr_rndc); mpc_mul(occ(l), occ(l), occ(r), thr_rndc); check_mpc(l); break; default: ovm_raise(except_not_a_number); } }
void generate_random_rational(rational_complex_number x) /***************************************************************\ * USAGE: generate a random rational number of unit modulus * \***************************************************************/ { int base = 10; char *str = NULL; mpq_t t, t_sqr; mpq_init(t); mpq_init(t_sqr); // generate a random number create_random_number_str(&str); mpq_set_str(t, str, base); mpq_canonicalize(t); // compute t_sqr = t^2 mpq_mul(t_sqr, t, t); // compute denominator mpq_set_ui(x->im, 1, 1); mpq_add(x->im, x->im, t_sqr); // 1 + t^2 // compute numerator mpq_set_ui(x->re, 1, 1); mpq_sub(x->re, x->re, t_sqr); // 1 - t^2 // divide to compute real part mpq_div(x->re, x->re, x->im); // compute imaginary part mpq_set_ui(x->im, 1, 1); mpq_add(x->im, x->re, x->im); // 1 + x->re mpq_mul(x->im, x->im, t); // t*(1+x->re) // clear memory mpq_clear(t); mpq_clear(t_sqr); free(str); return; }
static int mixed (void) { int n1; int n2; int i = 121; #ifndef NPRINTF_L long double d = 1. / 31.; #endif mpf_t mpf; mpq_t mpq; mpz_t mpz; mpfr_t x; mpfr_rnd_t rnd; mpf_init (mpf); mpf_set_ui (mpf, 40); mpf_div_ui (mpf, mpf, 31); /* mpf = 40.0 / 31.0 */ mpq_init (mpq); mpq_set_ui (mpq, 123456, 4567890); mpz_init (mpz); mpz_fib_ui (mpz, 64); mpfr_init (x); mpfr_set_str (x, "-12345678.875", 10, MPFR_RNDN); rnd = MPFR_RNDD; check_vsprintf ("121%", "%i%%", i); check_vsprintf ("121% -1.2345678875E+07", "%i%% %RNE", i, x); check_vsprintf ("121, -12345679", "%i, %.0Rf", i, x); check_vsprintf ("10610209857723, -1.2345678875e+07", "%Zi, %R*e", mpz, rnd, x); check_vsprintf ("-12345678.9, 121", "%.1Rf, %i", x, i); check_vsprintf ("-12345678, 1e240/45b352", "%.0R*f, %Qx", MPFR_RNDZ, x, mpq); n1 = check_vsprintf ("121, -12345678.875000000000, 1.290323", "%i, %.*Rf, %Ff%n", i, 12, x, mpf, &n2); if (n1 != n2) { printf ("error in number of characters written by mpfr_vsprintf\n"); printf ("expected: %d\n", n2); printf (" got: %d\n", n1); exit (1); } #ifndef NPRINTF_L check_vsprintf ("00000010610209857723, -1.2345678875e+07, 0.032258", "%.*Zi, %R*e, %Lf", 20, mpz, rnd, x, d); #endif mpf_clear (mpf); mpq_clear (mpq); mpz_clear (mpz); mpfr_clear (x); return 0; }
void pp_save_r(int n, int prime, int power) { pp_pp* pp = &pppp[power]; pp_value* v; int i, raise; mpq_t q; if (!pp->p) new_pp(pp, prime, power); QINIT(&q, "pp_save_r temp"); mpq_set_ui(q, 1, n); pp_save_any(pp, q, n, 1); QCLEAR(&q, "pp_save_r temp"); }
void dd_set_global_constants() { dd_init(dd_zero); dd_init(dd_minuszero); dd_init(dd_one); dd_init(dd_minusone); dd_init(dd_purezero); time(&dd_statStartTime); /* cddlib starting time */ dd_statBApivots=0; /* basis finding pivots */ dd_statCCpivots=0; /* criss-cross pivots */ dd_statDS1pivots=0; /* phase 1 pivots */ dd_statDS2pivots=0; /* phase 2 pivots */ dd_statACpivots=0; /* anticycling (cc) pivots */ dd_choiceLPSolverDefault=dd_DualSimplex; /* Default LP solver Algorithm */ dd_choiceRedcheckAlgorithm=dd_DualSimplex; /* Redundancy Checking Algorithm */ dd_choiceLexicoPivotQ=dd_TRUE; /* whether to use the lexicographic pivot */ #if defined GMPRATIONAL dd_statBSpivots=0; /* basis status checking pivots */ mpq_set_ui(dd_zero,0U,1U); mpq_set_ui(dd_purezero,0U,1U); mpq_set_ui(dd_one,1U,1U); mpq_set_si(dd_minusone,-1L,1U); ddf_set_global_constants(); #elif defined GMPFLOAT mpf_set_d(dd_zero,dd_almostzero); mpf_set_ui(dd_purezero,0U); mpf_set_ui(dd_one,1U); mpf_set_si(dd_minusone,-1L,1U); #else dd_zero[0]= dd_almostzero; /*real zero */ dd_purezero[0]= 0.0; dd_one[0]= 1L; dd_minusone[0]= -1L; #endif dd_neg(dd_minuszero,dd_zero); }
void check_overflow () { mpfr_t max; mpfi_t a; mpq_t q; int inexact; mpq_init (q); mpfi_init2 (a, 53); mpfr_init2 (max, 53); mpq_set_ui (q, 42, 17); mpfr_set_ui (&(a->left), 1, MPFI_RNDD); mpfr_set_inf (max, +1); mpfr_nextbelow (max); mpfr_set (&(a->right), max, MPFI_RNDU); inexact = mpfi_add_q (a, a, q); if (!mpfr_inf_p (&(a->right))) { printf ("Error: mpfi_add_q does not correctly handle overflow.\n"); exit (1); } if (!MPFI_RIGHT_IS_INEXACT (inexact)) { printf ("Error: mpfi_add_q does not return correct value " "when overflow.\n"); exit (1); } mpfr_set_inf (max, -1); mpfr_nextabove (max); mpfr_set (&(a->left), max, MPFI_RNDD); mpfr_set_ui (&(a->right), 1, MPFI_RNDU); mpq_set_si (q, -42, 17); inexact = mpfi_add_q (a, a, q); if (!mpfr_inf_p (&(a->left))) { printf ("Error: mpfi_add_q does not correctly handle negative " "overflow.\n"); exit (1); } if (!MPFI_LEFT_IS_INEXACT (inexact)) { printf ("Error: mpfi_add_q does not return correct value when negative " "overflow.\n"); exit (1); } mpfi_clear (a); mpfr_clear (max); mpq_clear (q); }
void gmp_init(Bool verbose, Bool with_management) { if (with_management) mp_set_memory_functions(gmp_alloc, gmp_realloc, gmp_free); mpq_init(const_zero); mpq_init(const_one); mpq_init(const_minus_one); mpq_set_ui(const_one, 1, 1); /* = 1 */ mpq_set_si(const_minus_one, -1, 1); /* = -1 */ if (verbose) printf("Using GMP Version %s %s\n", gmp_version, with_management ? "[memory management redirected]" : "[memory management unchanged]"); }
void refine_sqrt_upper(mpq_t sqrt_x, mpq_t x, int digits) /***************************************************************\ * USAGE: refine a rational upper bound on the sqrt(x) to the * * requested tolerance: 10^-digits * \***************************************************************/ { // assume (sqrt_x)^2 >= x > 0 && sqrt_x > 0 && digits >= 0 mpq_t new_sqrt_x, beta, tol; // initialize mpq_init(new_sqrt_x); mpq_init(beta); mpq_init(tol); // setup tol = (1/10)^digits mpq_set_ui(tol, 1, 10); exponentiate_mpq(tol, tol, digits); // loop until we have refined to the tol do { // compute new_sqrt_x & beta mpq_mul(beta, sqrt_x, sqrt_x); // (sqrt_x)^2 mpq_sub(beta, beta, x); // (sqrt_x)^2 - x mpq_add(new_sqrt_x, sqrt_x, sqrt_x); // 2*sqrt_x mpq_div(beta, beta, new_sqrt_x); // ((sqrt_x)^2 - x)/(2*sqrt_x) mpq_sub(new_sqrt_x, sqrt_x, beta); // sqrt_x - ((sqrt_x)^2 - x)/(2*sqrt_x) // determine if 2*beta <= tol mpq_add(beta, beta, beta); if (mpq_cmp(beta, tol) <= 0) { // success!! break; } else { // update sqrt_x & try again mpq_set(sqrt_x, new_sqrt_x); } } while (1); // clear mpq_clear(new_sqrt_x); mpq_clear(beta); mpq_clear(tol); return; }
void divide_int_constantProverExo::baseline(const mpq_t* input_q, int num_inputs, mpq_t* output_recomputed, int num_outputs) { uint32_t DIVISOR = divide_int_constant_cons::DIVISOR; uint32_t a; uint32_t b; a = mpz_get_ui(mpq_numref(input_q[0])); //void compute(struct In *input, struct Out *output){ b = a % DIVISOR; //} //Return value mpq_set_si(output_recomputed[0], 0, 1); //Output struct mpq_set_ui(output_recomputed[1], b, 1); }
static void bug_div_q_20100810 (void) { mpfr_t x; mpfr_t y; mpq_t q; int inexact; mpfr_init (x); mpfr_init (y); mpq_init (q); /* mpfr_div_q: the inexact value must be set in case of overflow */ mpq_set_ui (q, 3, 4096); mpfr_set_inf (x, +1); mpfr_nextbelow (x); inexact = mpfr_div_q (y, x, q, MPFR_RNDU); if (inexact <= 0) { printf ("Overflow error in mpfr_div_q. "); printf ("Wrong inexact flag: got %d, should be positive.\n", inexact); exit (1); } if (!mpfr_inf_p (y)) { printf ("Overflow error in mpfr_div_q (y, x, q, MPFR_RNDD). "); printf ("\nx = "); mpfr_out_str (stdout, 10, 0, x, MPFR_RNDD); printf ("\nq = "); mpq_out_str (stdout, 10, q); printf ("\ny = "); mpfr_out_str (stdout, 10, 0, y, MPFR_RNDD); printf (" (should be +infinity)\n"); exit (1); } mpq_clear (q); mpfr_clear (y); mpfr_clear (x); }
void mpq_mat_row_inner_product(mpq_t res, mpq_mat_t mat1, ulong r1, mpq_mat_t mat2, ulong r2){ if (mat2->c != mat1->c){ printf("FLINT exception: dimensions don't match\n"); abort(); } mpq_set_ui(res, 0L, 1L); mpq_t temp; mpq_init(temp); long i; for (i = 0; i< mat1->c; i++){ mpq_mul(temp, mat2->entries[r2*mat2->c + i], mat1->entries[r1*mat1->c + i]); mpq_add(res, res, temp); } mpq_clear(temp); return; }
void lps_scale(const Lps* lp) { Con* con; Nzo* nzo; mpq_t maxi; mpq_t v; assert(lps_valid(lp)); mpq_init(maxi); mpq_init(v); for(con = lp->con_root; con != NULL; con = con->next) { if ((con->flags & LP_FLAG_CON_SCALE) > 0) { mpq_set_ui(maxi, 0, 1); /* = 0 */ for(nzo = con->first; nzo != NULL; nzo = nzo->con_next) { mpq_abs(v, nzo->value); if (mpq_cmp(v, maxi) > 0) mpq_set(maxi, v); } mpq_inv(con->scale, maxi); /* scale = 1 / maxi */ if (HAS_RHS(con)) mpq_mul(con->rhs, con->rhs, con->scale); if (HAS_LHS(con)) mpq_mul(con->lhs, con->lhs, con->scale); for(nzo = con->first; nzo != NULL; nzo = nzo->con_next) mpq_mul(nzo->value, nzo->value, con->scale); } } mpq_clear(v); mpq_clear(maxi); }
void w3j_Delta_sq(mpq_t r, long j1, long j2, long j3) { // n = (j1+j2-j3)! (j1-j2+j3)! (-j1+j2+j3)! // d = (j1+j2+j3+1)! mpz_t h; mpz_init(h); mpq_set_ui(r,1,1); mpz_fac_ui(h,j1+j2-j3); mpz_mul(mpq_numref(r),mpq_numref(r),h); mpz_fac_ui(h,j1+j2+j3+1); mpz_mul(mpq_denref(r),mpq_denref(r),h); mpq_canonicalize(r); mpz_fac_ui(h,j1-j2+j3); mpz_mul(mpq_numref(r),mpq_numref(r),h); mpz_fac_ui(h,j2+j3-j1); mpz_mul(mpq_numref(r),mpq_numref(r),h); mpq_canonicalize(r); mpz_clear(h); }
static void test_cmp_q (mpfr_prec_t pmin, mpfr_prec_t pmax, int nmax) { mpfr_t x, z; mpq_t y; mpfr_prec_t p; int res1, res2; int n; mpfr_init (x); mpfr_init2 (z, MPFR_PREC_MIN); mpq_init (y); for(p=pmin ; p < pmax ; p++) { mpfr_set_prec (x, p); for (n = 0 ; n < nmax ; n++) { mpfr_urandomb (x, RANDS); mpq_set_ui (y, randlimb (), randlimb() ); if (!MPFR_IS_SINGULAR (x)) { mpfr_sub_q (z, x, y, MPFR_RNDN); res1 = mpfr_sgn (z); res2 = mpfr_cmp_q (x, y); if (res1 != res2) { printf("Error for mpfr_cmp_q: res=%d sub_z gives %d\n", res2, res1); exit (1); } } } } mpq_clear (y); mpfr_clear (x); mpfr_clear (z); }
void check_n (void) { int ret; /* %n suppressed */ { int n = 123; gmp_sscanf (" ", " %*n", &n); ASSERT_ALWAYS (n == 123); } { int n = 123; fromstring_gmp_fscanf (" ", " %*n", &n); ASSERT_ALWAYS (n == 123); } #define CHECK_N(type, string) \ do { \ type x[2]; \ char fmt[128]; \ int ret; \ \ x[0] = ~ (type) 0; \ x[1] = ~ (type) 0; \ sprintf (fmt, "abc%%%sn", string); \ ret = gmp_sscanf ("abc", fmt, &x[0]); \ \ ASSERT_ALWAYS (ret == 0); \ \ /* should write whole of x[0] and none of x[1] */ \ ASSERT_ALWAYS (x[0] == 3); \ ASSERT_ALWAYS (x[1] == (type) ~ (type) 0); \ \ } while (0) CHECK_N (char, "hh"); CHECK_N (long, "l"); #if HAVE_LONG_LONG CHECK_N (long long, "L"); #endif #if HAVE_INTMAX_T CHECK_N (intmax_t, "j"); #endif #if HAVE_PTRDIFF_T CHECK_N (ptrdiff_t, "t"); #endif CHECK_N (short, "h"); CHECK_N (size_t, "z"); /* %Zn */ { mpz_t x[2]; mpz_init_set_si (x[0], -987L); mpz_init_set_si (x[1], 654L); ret = gmp_sscanf ("xyz ", "xyz%Zn", x[0]); MPZ_CHECK_FORMAT (x[0]); MPZ_CHECK_FORMAT (x[1]); ASSERT_ALWAYS (ret == 0); ASSERT_ALWAYS (mpz_cmp_ui (x[0], 3L) == 0); ASSERT_ALWAYS (mpz_cmp_ui (x[1], 654L) == 0); mpz_clear (x[0]); mpz_clear (x[1]); } { mpz_t x; mpz_init (x); ret = fromstring_gmp_fscanf ("xyz ", "xyz%Zn", x); ASSERT_ALWAYS (ret == 0); ASSERT_ALWAYS (mpz_cmp_ui (x, 3L) == 0); mpz_clear (x); } /* %Qn */ { mpq_t x[2]; mpq_init (x[0]); mpq_init (x[1]); mpq_set_ui (x[0], -987L, 654L); mpq_set_ui (x[1], 4115L, 226L); ret = gmp_sscanf ("xyz ", "xyz%Qn", x[0]); MPQ_CHECK_FORMAT (x[0]); MPQ_CHECK_FORMAT (x[1]); ASSERT_ALWAYS (ret == 0); ASSERT_ALWAYS (mpq_cmp_ui (x[0], 3L, 1L) == 0); ASSERT_ALWAYS (mpq_cmp_ui (x[1], 4115L, 226L) == 0); mpq_clear (x[0]); mpq_clear (x[1]); } { mpq_t x; mpq_init (x); ret = fromstring_gmp_fscanf ("xyz ", "xyz%Qn", x); ASSERT_ALWAYS (ret == 0); ASSERT_ALWAYS (mpq_cmp_ui (x, 3L, 1L) == 0); mpq_clear (x); } /* %Fn */ { mpf_t x[2]; mpf_init (x[0]); mpf_init (x[1]); mpf_set_ui (x[0], -987L); mpf_set_ui (x[1], 654L); ret = gmp_sscanf ("xyz ", "xyz%Fn", x[0]); MPF_CHECK_FORMAT (x[0]); MPF_CHECK_FORMAT (x[1]); ASSERT_ALWAYS (ret == 0); ASSERT_ALWAYS (mpf_cmp_ui (x[0], 3L) == 0); ASSERT_ALWAYS (mpf_cmp_ui (x[1], 654L) == 0); mpf_clear (x[0]); mpf_clear (x[1]); } { mpf_t x; mpf_init (x); ret = fromstring_gmp_fscanf ("xyz ", "xyz%Fn", x); ASSERT_ALWAYS (ret == 0); ASSERT_ALWAYS (mpf_cmp_ui (x, 3L) == 0); mpf_clear (x); } }
void mpq_mat_GS(mpq_mat_t mu, mpq_mat_t GS, mpq_mat_t mat){ if ( ( GS->c != mat->c ) || ( GS->r != mat->r ) ){ printf("FLINT exception: dimensions don't match\n"); abort(); } if ( ( mu->r != mu->c) || (mu->r != mat->r) ){ printf("FLINT exception: mu dimensions don't match\n"); abort(); } //I'm going to use mu[0] to store <GS[i], GS[i]> until the end mpq_t temp; mpq_init(temp); long i, j, k; //setting GS[0] := mat[0] for (i = 0; i < mat->c; i++) mpq_set(GS->entries[i], mat->entries[i]); mpq_mat_row_inner_product(mu->entries[0], GS, 0, GS, 0); //mu[i,i] := 1 for (i = 1; i < mu->r; i++) mpq_set_ui(mu->entries[i*mu->c + i], 1L, 1L); for (i = 1; i < mat->r; i++){ //in this loop we want to find GS[i] := mat[i] - sum (mu[i,j]*mat[j]) //start with GS[i] = mat[i] then for each j < i compute mu and subtract for (k = 0; k < mat->c; k++) mpq_set(GS->entries[i*mat->c + k], mat->entries[i*mat->c + k]); for (j = 0; j < i; j++){ //temp will be the numerator of mu[i,j] which is <mat[i], GS[j]> mpq_mat_row_inner_product(temp, mat, i, GS, j); if (mpq_sgn(mu->entries[j]) != 0) mpq_div(mu->entries[i*mu->c + j], temp, mu->entries[j]); else mpq_set_ui(mu->entries[i*mu->c + j], 0L, 1L); //now need GS[i] := GS[i] - mu[i,j]*GS[j] for (k=0; k < mat->c; k++){ //temp = mu[i,j] * GS[j,k] mpq_mul(temp, mu->entries[i*mu->c + j], GS->entries[j*GS->c + k]); mpq_neg(temp, temp); mpq_add(GS->entries[i*GS->c + k], GS->entries[i*GS->c + k], temp); } } if (i+1 < mu->c) mpq_mat_row_inner_product(mu->entries[i], GS, i, GS, i); } mpq_set_ui(mu->entries[0], 1L, 1L); for (k = 1; k < mu->c; k++) mpq_set_ui(mu->entries[k], 0L, 1L); mpq_clear(temp); return; }
void bern_rat(mpq_t res, long k, int num_threads) { // special cases if (k == 0) { // B_0 = 1 mpq_set_ui(res, 1, 1); return; } if (k == 1) { // B_1 = -1/2 mpq_set_si(res, -1, 2); return; } if (k == 2) { // B_2 = 1/6 mpq_set_si(res, 1, 6); return; } if (k & 1) { // B_k = 0 if k is odd mpq_set_ui(res, 0, 1); return; } if (num_threads <= 0) num_threads = 1; mpz_t num, den; mpz_init(num); mpz_init(den); const double log2 = 0.69314718055994528622676; const double invlog2 = 1.44269504088896340735992; // = 1/log(2) // compute preliminary prime bound and build prime table long bound1 = (long) max(37.0, ceil((k + 0.5) * log(k) * invlog2)); PrimeTable table(bound1); // compute denominator of B_k bern_den(den, k, table); // compute number of bits we need to resolve the numerator long bits = (long) ceil((k + 0.5) * log(k) * invlog2 - 4.094 * k + 2.470 + log(mpz_get_d(den)) * invlog2); // compute tighter prime bound // (note: we can safely get away with double-precision here. It would // only start being insufficient around k = 10^13 or so, which is totally // impractical at present.) double prod = 1.0; long prod_bits = 0; long p; for (p = 5; prod_bits < bits + 1; p = table.next_prime(p)) { if (p >= NTL_SP_BOUND) abort(); // !!!!! not sure what else we can do here... if (k % (p-1) != 0) prod *= (double) p; int exp; prod = frexp(prod, &exp); prod_bits += exp; } long bound2 = p; State state(k, bound2, table); #ifdef USE_THREADS vector<pthread_t> threads(num_threads - 1); pthread_attr_t attr; pthread_attr_init(&attr); #ifdef THREAD_STACK_SIZE pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE * 1024); #endif // spawn worker threads to process blocks for (long i = 0; i < num_threads - 1; i++) pthread_create(&threads[i], &attr, worker, &state); #endif worker(&state); // make this thread a worker too #ifdef USE_THREADS for (long i = 0; i < num_threads - 1; i++) pthread_join(threads[i], NULL); #endif pthread_attr_destroy (&attr); // reconstruct B_k as a rational number Item* item = *(state.items.begin()); mpz_mul(num, item->residue, den); mpz_mod(num, num, item->modulus); if (k % 4 == 0) { // B_k is negative mpz_sub(num, item->modulus, num); mpz_neg(num, num); } delete item; mpz_swap(num, mpq_numref(res)); mpz_swap(den, mpq_denref(res)); mpz_clear(num); mpz_clear(den); }
static PyObject * GMPy_MPQ_Factory(PyObject *self, PyObject *args, PyObject *keywds) { MPQ_Object *result, *temp; PyObject *n, *m; int base = 10; Py_ssize_t argc, keywdc = 0; static char *kwlist[] = {"s", "base", NULL }; CTXT_Object *context = NULL; if (self && CTXT_Check(self)) { context = (CTXT_Object*)self; } else { CHECK_CONTEXT(context); } argc = PyTuple_Size(args); if (keywds) { keywdc = PyDict_Size(keywds); } if (argc + keywdc > 2) { TYPE_ERROR("mpq() takes at most 2 arguments"); return NULL; } if (argc + keywdc == 0) { if ((result = GMPy_MPQ_New(context))) { mpq_set_ui(result->q, 0, 1); } return (PyObject*)result; } if (argc == 0) { TYPE_ERROR("mpq() requires at least one non-keyword argument"); return NULL; } n = PyTuple_GetItem(args, 0); /* Handle the case where the first argument is a string. */ if (PyStrOrUnicode_Check(n)) { /* keyword base is legal */ if (keywdc || argc > 1) { if (!(PyArg_ParseTupleAndKeywords(args, keywds, "O|i", kwlist, &n, &base))) { return NULL; } } if ((base != 0) && ((base < 2) || (base > 62))) { VALUE_ERROR("base for mpq() must be 0 or in the interval [2, 62]"); return NULL; } return (PyObject*)GMPy_MPQ_From_PyStr(n, base, context); } /* Handle 1 argument. It must be non-complex number. */ if (argc == 1) { if (IS_REAL(n)) { return (PyObject*)GMPy_MPQ_From_Number(n, context); } } /* Handle 2 arguments. Both arguments must be integer or rational. */ if (argc == 2) { m = PyTuple_GetItem(args, 1); if (IS_RATIONAL(n) && IS_RATIONAL(m)) { result = GMPy_MPQ_From_Rational(n, context); temp = GMPy_MPQ_From_Rational(m, context); if (!result || !temp) { Py_XDECREF((PyObject*)result); Py_XDECREF((PyObject*)temp); return NULL; } if (mpq_sgn(temp->q) == 0) { ZERO_ERROR("zero denominator in mpq()"); Py_DECREF((PyObject*)result); Py_DECREF((PyObject*)temp); return NULL; } mpq_div(result->q, result->q, temp->q); Py_DECREF((PyObject*)temp); return (PyObject*)result; } } TYPE_ERROR("mpq() requires numeric or string argument"); return NULL; }