static void compare_mpc_pow (mpfr_prec_t pmax, int iter, unsigned long nbits) /* copied from tpow_ui.c and replaced unsigned by signed */ { mpfr_prec_t p; mpc_t x, y, z, t; long n; int i, inex_pow, inex_pow_si; mpc_rnd_t rnd; mpc_init3 (y, sizeof (unsigned long) * CHAR_BIT, MPFR_PREC_MIN); for (p = MPFR_PREC_MIN; p <= pmax; p++) for (i = 0; i < iter; i++) { mpc_init2 (x, p); mpc_init2 (z, p); mpc_init2 (t, p); mpc_urandom (x, rands); n = (signed long) gmp_urandomb_ui (rands, nbits); mpc_set_si (y, n, MPC_RNDNN); for (rnd = 0; rnd < 16; rnd ++) { inex_pow = mpc_pow (z, x, y, rnd); inex_pow_si = mpc_pow_si (t, x, n, rnd); if (mpc_cmp (z, t) != 0) { printf ("mpc_pow and mpc_pow_si differ for x="); mpc_out_str (stdout, 10, 0, x, MPC_RNDNN); printf (" n=%li\n", n); printf ("mpc_pow gives "); mpc_out_str (stdout, 10, 0, z, MPC_RNDNN); printf ("\nmpc_pow_si gives "); mpc_out_str (stdout, 10, 0, t, MPC_RNDNN); printf ("\n"); exit (1); } if (inex_pow != inex_pow_si) { printf ("mpc_pow and mpc_pow_si give different flags for x="); mpc_out_str (stdout, 10, 0, x, MPC_RNDNN); printf (" n=%li\n", n); printf ("mpc_pow gives %d\n", inex_pow); printf ("mpc_pow_si gives %d\n", inex_pow_si); exit (1); } } mpc_clear (x); mpc_clear (z); mpc_clear (t); } mpc_clear (y); }
SEXP R_mpc_urandom(SEXP sprec) { mpfr_prec_t prec = INTEGER(sprec)[0]; mpc_t *z = (mpc_t *)malloc(sizeof(mpc_t)); if (z == NULL) { Rf_error("Could not allocate memory for MPC type."); } mpc_init2(*z, prec); if (!R_mpc_seed_init) { gmp_randinit_default(R_mpc_seed_state); R_mpc_seed_init = 1; } mpc_urandom((mpc_ptr)z, R_mpc_seed_state); return(MakeMPC(z)); }
static PyObject * GMPy_MPC_random_Function(PyObject *self, PyObject *args) { MPC_Object *result; CTXT_Object *context = NULL; CHECK_CONTEXT(context); if (PyTuple_GET_SIZE(args) != 1) { TYPE_ERROR("mpfc_random() requires 1 argument"); return NULL; } if (!RandomState_Check(PyTuple_GET_ITEM(args, 0))) { TYPE_ERROR("mpc_random() requires 'random_state' argument"); return NULL; } if ((result = GMPy_MPC_New(0, 0, context))) { mpc_urandom(result->c, RANDOM_STATE(PyTuple_GET_ITEM(args, 0))); } return (PyObject*)result; }