int main() { slong iter; flint_rand_t state; flint_printf("add_inline...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++) { fmpz_t a, b, c, d; fmpz_init(a); fmpz_init(b); fmpz_init(c); fmpz_init(d); fmpz_randtest(a, state, 1 + n_randint(state, 200)); fmpz_randtest(b, state, 1 + n_randint(state, 200)); fmpz_randtest(c, state, 1 + n_randint(state, 200)); fmpz_randtest(d, state, 1 + n_randint(state, 200)); fmpz_add(c, a, b); fmpz_add_inline(d, a, b); if (!fmpz_equal(c, d)) { flint_printf("FAIL\n"); fmpz_print(a); flint_printf("\n\n"); fmpz_print(b); flint_printf("\n\n"); fmpz_print(c); flint_printf("\n\n"); fmpz_print(d); flint_printf("\n\n"); abort(); } fmpz_add_inline(a, a, b); if (!fmpz_equal(c, a)) { flint_printf("FAIL (aliasing 1)\n"); fmpz_print(a); flint_printf("\n\n"); fmpz_print(b); flint_printf("\n\n"); fmpz_print(c); flint_printf("\n\n"); fmpz_print(d); flint_printf("\n\n"); abort(); } fmpz_randtest(a, state, 1 + n_randint(state, 200)); fmpz_add(c, a, b); fmpz_add_inline(b, a, b); if (!fmpz_equal(c, b)) { flint_printf("FAIL (aliasing 2)\n"); fmpz_print(a); flint_printf("\n\n"); fmpz_print(b); flint_printf("\n\n"); fmpz_print(c); flint_printf("\n\n"); fmpz_print(d); flint_printf("\n\n"); abort(); } fmpz_add(d, a, a); fmpz_add_inline(c, a, a); if (!fmpz_equal(c, d)) { flint_printf("FAIL (aliasing 3)\n"); fmpz_print(a); flint_printf("\n\n"); fmpz_print(b); flint_printf("\n\n"); fmpz_print(c); flint_printf("\n\n"); fmpz_print(d); flint_printf("\n\n"); abort(); } fmpz_add(d, a, a); fmpz_add_inline(a, a, a); if (!fmpz_equal(d, a)) { flint_printf("FAIL (aliasing 4)\n"); fmpz_print(a); flint_printf("\n\n"); fmpz_print(b); flint_printf("\n\n"); fmpz_print(c); flint_printf("\n\n"); fmpz_print(d); flint_printf("\n\n"); abort(); } fmpz_clear(a); fmpz_clear(b); fmpz_clear(c); fmpz_clear(d); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; flint_rand_t state; flint_printf("get_fmpz...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arf_t x, x2; fmpz_t z, z2, e; int ret1, ret2; arf_init(x); arf_init(x2); fmpz_init(z); fmpz_init(z2); fmpz_init(e); arf_randtest(x, state, 2 + n_randint(state, 1000), 10); fmpz_randtest(z, state, 1 + n_randint(state, 1000)); fmpz_randtest(z2, state, 1 + n_randint(state, 1000)); fmpz_randtest(e, state, 1 + n_randint(state, 200)); arf_mul_2exp_fmpz(x2, x, e); ret1 = arf_get_fmpz(z, x, ARF_RND_DOWN); ret2 = arf_get_fmpz_fixed_fmpz(z2, x2, e); if (!fmpz_equal(z, z2) || (ret1 != ret2)) { flint_printf("FAIL (fixed_fmpz)\n\n"); flint_printf("x = "); arf_print(x); flint_printf("\n\n"); flint_printf("x2 = "); arf_print(x2); flint_printf("\n\n"); flint_printf("z = "); fmpz_print(z); flint_printf("\n\n"); flint_printf("z2 = "); fmpz_print(z2); flint_printf("\n\n"); flint_printf("ret1 = %d, ret2 = %d\n\n", ret1, ret2); flint_abort(); } arf_clear(x); arf_clear(x2); fmpz_clear(z); fmpz_clear(z2); fmpz_clear(e); } for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arf_t x, x2; fmpz_t z, z2; slong e; int ret1, ret2; arf_init(x); arf_init(x2); fmpz_init(z); fmpz_init(z2); arf_randtest(x, state, 2 + n_randint(state, 1000), 10); fmpz_randtest(z, state, 1 + n_randint(state, 1000)); fmpz_randtest(z2, state, 1 + n_randint(state, 1000)); e = n_randtest(state); arf_mul_2exp_si(x2, x, e); ret1 = arf_get_fmpz(z, x, ARF_RND_DOWN); ret2 = arf_get_fmpz_fixed_si(z2, x2, e); if (!fmpz_equal(z, z2) || (ret1 != ret2)) { flint_printf("FAIL (fixed_si)\n\n"); flint_printf("x = "); arf_print(x); flint_printf("\n\n"); flint_printf("x2 = "); arf_print(x2); flint_printf("\n\n"); flint_printf("z = "); fmpz_print(z); flint_printf("\n\n"); flint_printf("z2 = "); fmpz_print(z2); flint_printf("\n\n"); flint_printf("ret1 = %d, ret2 = %d\n\n", ret1, ret2); flint_abort(); } arf_clear(x); arf_clear(x2); fmpz_clear(z); fmpz_clear(z2); } for (iter = 0; iter < 1000000 * arb_test_multiplier(); iter++) { slong bits; arf_t x; mpfr_t y; fmpz_t z, z2; mpz_t w; int ret1, ret2; bits = 2 + n_randint(state, 1000); arf_init(x); mpfr_init2(y, bits); fmpz_init(z); fmpz_init(z2); mpz_init(w); arf_randtest(x, state, bits, 10); fmpz_randtest(z, state, 1 + n_randint(state, 1000)); arf_get_mpfr(y, x, MPFR_RNDN); switch (n_randint(state, 5)) { case 0: ret1 = arf_get_fmpz(z, x, ARF_RND_FLOOR); ret2 = mpfr_get_z(w, y, MPFR_RNDD); break; case 1: ret1 = arf_get_fmpz(z, x, ARF_RND_CEIL); ret2 = mpfr_get_z(w, y, MPFR_RNDU); break; case 2: ret1 = arf_get_fmpz(z, x, ARF_RND_DOWN); ret2 = mpfr_get_z(w, y, MPFR_RNDZ); break; case 3: ret1 = arf_get_fmpz(z, x, ARF_RND_UP); ret2 = mpfr_get_z(w, y, MPFR_RNDA); break; default: ret1 = arf_get_fmpz(z, x, ARF_RND_NEAR); ret2 = mpfr_get_z(w, y, MPFR_RNDN); break; } fmpz_set_mpz(z2, w); if (!fmpz_equal(z, z2) || (ret1 != (ret2 != 0))) { flint_printf("FAIL\n\n"); flint_printf("x = "); arf_print(x); flint_printf("\n\n"); flint_printf("z = "); fmpz_print(z); flint_printf("\n\n"); flint_printf("z2 = "); fmpz_print(z2); flint_printf("\n\n"); flint_printf("ret1 = %d, ret2 = %d\n\n", ret1, ret2); flint_abort(); } arf_clear(x); mpfr_clear(y); fmpz_clear(z); fmpz_clear(z2); mpz_clear(w); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
void nf_elem_rep_mat_fmpz_mat_den(fmpz_mat_t res, fmpz_t den, const nf_elem_t a, const nf_t nf) { if (nf->flag & NF_LINEAR) { fmpz_set(fmpz_mat_entry(res, 0, 0), LNF_ELEM_NUMREF(a)); fmpz_set(den, LNF_ELEM_DENREF(a)); } else if (nf->flag & NF_QUADRATIC) { nf_elem_t t; const fmpz * const anum = QNF_ELEM_NUMREF(a); const fmpz * const aden = QNF_ELEM_DENREF(a); fmpz * const tnum = QNF_ELEM_NUMREF(t); fmpz * const tden = QNF_ELEM_DENREF(t); nf_elem_init(t, nf); nf_elem_mul_gen(t, a, nf); if (fmpz_equal(tden, aden)) { fmpz_set(fmpz_mat_entry(res, 0, 0), anum); fmpz_set(fmpz_mat_entry(res, 0, 1), anum + 1); fmpz_set(fmpz_mat_entry(res, 1, 0), tnum); fmpz_set(fmpz_mat_entry(res, 1, 1), tnum + 1); fmpz_set(den, tden); } else { fmpz_lcm(den, tden, aden); fmpz_divexact(fmpz_mat_entry(res, 0, 0), den, aden); fmpz_mul(fmpz_mat_entry(res, 0, 1), anum + 1, fmpz_mat_entry(res, 0, 0)); fmpz_mul(fmpz_mat_entry(res, 0, 0), anum, fmpz_mat_entry(res, 0, 0)); fmpz_divexact(fmpz_mat_entry(res, 1, 0), den, tden); fmpz_mul(fmpz_mat_entry(res, 1, 1), tnum + 1, fmpz_mat_entry(res, 1, 0)); fmpz_mul(fmpz_mat_entry(res, 1, 0), tnum, fmpz_mat_entry(res, 1, 0)); } nf_elem_clear(t, nf); } else { slong i, j; nf_elem_t t; slong d = fmpq_poly_degree(nf->pol); nf_elem_init(t, nf); nf_elem_set(t, a, nf); if (NF_ELEM(a)->length == 0) { fmpz_mat_zero(res); fmpz_one(den); } else if (NF_ELEM(a)->length == 1) { fmpz_mat_zero(res); for (i = 0; i <= d - 1; i++) { fmpz_set(fmpz_mat_entry(res, i, i), fmpq_poly_numref(NF_ELEM(a))); } fmpz_set(den, fmpq_poly_denref(NF_ELEM(a))); } else { /* Special case if defining polynomial is monic and integral and the element also has trivial denominator */ if (nf->flag & NF_MONIC && fmpz_is_one(fmpq_poly_denref(nf->pol)) && fmpz_is_one(fmpq_poly_denref(NF_ELEM(a)))) { fmpz_one(den); for (i = 0; i <= NF_ELEM(a)->length - 1; i++) fmpz_set(fmpz_mat_entry(res, 0, i), fmpq_poly_numref(NF_ELEM(a)) + i); for (i = NF_ELEM(a)->length; i <= d - 1; i++) fmpz_zero(fmpz_mat_entry(res, 0, i)); for (j = 1; j <= d - NF_ELEM(a)->length; j++) { nf_elem_mul_gen(t, t, nf); for (i = 0; i < j; i++) fmpz_zero(fmpz_mat_entry(res, j, i)); for (i = 0; i <= NF_ELEM(a)->length - 1; i++) fmpz_set(fmpz_mat_entry(res, j, j + i), fmpq_poly_numref(NF_ELEM(a)) + i); for (i = j + NF_ELEM(a)->length; i <= d - 1; i++) fmpz_zero(fmpz_mat_entry(res, j, i)); } for (j = d - NF_ELEM(a)->length + 1; j <= d - 1; j++) { nf_elem_mul_gen(t, t, nf); for (i = 0; i <= d - 1; i++) fmpz_set(fmpz_mat_entry(res, j, i), fmpq_poly_numref(NF_ELEM(t)) + i); } } else { /* Now the general case. For 0 <= j < d - 2 we store the * denominator for row j at res[d - 1, j]. At the end we * divide the lcm of all of them by the corresponding * denominator of the row to get the correct multiplier for * row. */ for (i = 0; i <= NF_ELEM(a)->length - 1; i++) fmpz_set(fmpz_mat_entry(res, 0, i), fmpq_poly_numref(NF_ELEM(a)) + i); for (i = NF_ELEM(a)->length; i <= d - 1; i++) fmpz_zero(fmpz_mat_entry(res, 0, i)); fmpz_set(fmpz_mat_entry(res, d - 1, 0), fmpq_poly_denref(NF_ELEM(a))); for (j = 1; j <= d - NF_ELEM(a)->length; j++) { nf_elem_mul_gen(t, t, nf); for (i = 0; i < j; i++) fmpz_zero(fmpz_mat_entry(res, j, i)); for (i = 0; i <= NF_ELEM(a)->length - 1; i++) fmpz_set(fmpz_mat_entry(res, j, j + i), fmpq_poly_numref(NF_ELEM(a)) + i); for (i = j + NF_ELEM(a)->length; i <= d - 1; i++) fmpz_zero(fmpz_mat_entry(res, j, i)); fmpz_set(fmpz_mat_entry(res, d - 1, j), fmpq_poly_denref(NF_ELEM(a))); } for (j = d - NF_ELEM(a)->length + 1; j <= d - 2; j++) { nf_elem_mul_gen(t, t, nf); for (i = 0; i <= d - 1; i++) fmpz_set(fmpz_mat_entry(res, j, i), fmpq_poly_numref(NF_ELEM(t)) + i); fmpz_set(fmpz_mat_entry(res, d - 1, j), fmpq_poly_denref(NF_ELEM(t))); } nf_elem_mul_gen(t, t, nf); /* Now compute the correct denominator */ fmpz_set(fmpz_mat_entry(res, d - 1, d - 1), fmpq_poly_denref(NF_ELEM(t))); fmpz_set(den, fmpq_poly_denref(NF_ELEM(t))); for (j = 0; j <= d - 2; j++) fmpz_lcm(den, den, fmpz_mat_entry(res, d - 1, j)); for (j = 0; j <= d - 2; j++) { if (!fmpz_equal(den, fmpz_mat_entry(res, d - 1, j))) { fmpz_divexact(fmpz_mat_entry(res, d - 1, j), den, fmpz_mat_entry(res, d - 1, j)); for (i = 0; i <= d - 1; i++) fmpz_mul(fmpz_mat_entry(res, j, i), fmpz_mat_entry(res, j, i), fmpz_mat_entry(res, d - 1, j)); } } if (fmpz_equal(den, fmpz_mat_entry(res, d - 1, d - 1))) { for (i = 0; i < d; i++) fmpz_set(fmpz_mat_entry(res, d - 1, i), fmpq_poly_numref(NF_ELEM(t)) + i); } else { fmpz_divexact(fmpz_mat_entry(res, d - 1, d - 1), den, fmpq_poly_denref(NF_ELEM(t))); for (i = 0; i < d; i++) fmpz_mul(fmpz_mat_entry(res, d - 1, i), fmpq_poly_numref(NF_ELEM(t)) + i, fmpz_mat_entry(res, d - 1, d - 1)); } } } nf_elem_clear(t, nf); } }
int main() { slong iter; flint_rand_t state; flint_printf("add_si_inline...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000; iter++) { fmpz_t a, c, d; slong b; fmpz_init(a); fmpz_init(c); fmpz_init(d); fmpz_randtest(a, state, 1 + n_randint(state, 200)); fmpz_randtest(c, state, 1 + n_randint(state, 200)); fmpz_randtest(d, state, 1 + n_randint(state, 200)); b = n_randtest(state); fmpz_add_si(c, a, b); fmpz_add_si_inline(d, a, b); if (!fmpz_equal(c, d)) { flint_printf("FAIL\n"); fmpz_print(a); flint_printf("\n\n"); flint_printf("%wd", b); flint_printf("\n\n"); fmpz_print(c); flint_printf("\n\n"); fmpz_print(d); flint_printf("\n\n"); abort(); } fmpz_add_si_inline(a, a, b); if (!fmpz_equal(c, a)) { flint_printf("FAIL (aliasing)\n"); fmpz_print(a); flint_printf("\n\n"); flint_printf("%wd", b); flint_printf("\n\n"); fmpz_print(c); flint_printf("\n\n"); fmpz_print(d); flint_printf("\n\n"); abort(); } fmpz_clear(a); fmpz_clear(c); fmpz_clear(d); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong i, j; int sign; fmpz_t input; fmpz_t result; fmpz_t r1; fmpz_t m1; fmpz_t mprod; ulong r2, m2; FLINT_TEST_INIT(state); flint_printf("CRT_ui...."); fflush(stdout); fmpz_init(input); fmpz_init(result); fmpz_init(r1); fmpz_init(m1); fmpz_init(mprod); for (i = 0; i < 1000 * flint_test_multiplier(); i++) { slong nprimes; m2 = n_randtest_prime(state, 0); nprimes = 1 + n_randint(state, 4); fmpz_set_ui(m1, UWORD(1)); for (j = 0; j < nprimes; ) { ulong t = n_randtest_prime(state, 0); if (t != m2) { fmpz_mul_ui(m1, m1, t); j++; } } fmpz_mul_ui(mprod, m1, m2); sign = n_randint(state, 2); if (sign) fmpz_randtest_mod_signed(input, state, mprod); else fmpz_randtest_mod(input, state, mprod); fmpz_mod(r1, input, m1); r2 = fmpz_fdiv_ui(input, m2); fmpz_CRT_ui(result, r1, m1, r2, m2, sign); if (!fmpz_equal(result, input)) { flint_printf("FAIL:\n"); flint_printf("m1: "); fmpz_print(m1); flint_printf("\n"); flint_printf("m2: %wu\n", m2); flint_printf("m1*m2: "); fmpz_print(mprod); flint_printf("\n"); flint_printf("input: "); fmpz_print(input); flint_printf("\n"); flint_printf("r1: "); fmpz_print(r1); flint_printf("\n"); flint_printf("r2: %wu\n", r2); flint_printf("result: "); fmpz_print(result); flint_printf("\n"); flint_printf("%wd Equalness: %d\n", i, fmpz_equal(result, input)); flint_printf("\n"); abort(); } } fmpz_clear(input); fmpz_clear(result); fmpz_clear(r1); fmpz_clear(m1); fmpz_clear(mprod); FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
slong fmpr_div(fmpr_t z, const fmpr_t x, const fmpr_t y, slong prec, fmpr_rnd_t rnd) { if (fmpr_is_special(x) || fmpr_is_special(y)) { _fmpr_div_special(z, x, y); return FMPR_RESULT_EXACT; } /* division by power of two <=> shift exponents */ if (fmpz_is_pm1(fmpr_manref(y))) { if (fmpz_is_one(fmpr_manref(y))) fmpz_set(fmpr_manref(z), fmpr_manref(x)); else fmpz_neg(fmpr_manref(z), fmpr_manref(x)); fmpz_sub(fmpr_expref(z), fmpr_expref(x), fmpr_expref(y)); return _fmpr_normalise(fmpr_manref(z), fmpr_expref(z), prec, rnd); } else { slong xbits, ybits, extra, extra_pad, extra_control; int negative; fmpz_t t, u; /* todo: work out exact needed shift */ xbits = fmpz_bits(fmpr_manref(x)); ybits = fmpz_bits(fmpr_manref(y)); extra = prec - xbits + ybits; extra = FLINT_MAX(extra, 0); extra_pad = 32; extra_control = 24; extra += extra_pad; fmpz_init(t); fmpz_init(u); fmpz_mul_2exp(t, fmpr_manref(x), extra); fmpz_tdiv_q(u, t, fmpr_manref(y)); if (low_bits_are_zero(u, extra_control)) { fmpz_t v; fmpz_init(v); fmpz_mul(v, u, fmpr_manref(y)); negative = fmpz_sgn(fmpr_manref(x)) != fmpz_sgn(fmpr_manref(y)); if (!fmpz_equal(t, v)) { if (negative) fmpz_sub_ui(u, u, 1); else fmpz_add_ui(u, u, 1); } fmpz_clear(v); } fmpz_swap(fmpr_manref(z), u); fmpz_clear(t); fmpz_clear(u); fmpz_sub(fmpr_expref(z), fmpr_expref(x), fmpr_expref(y)); fmpz_sub_ui(fmpr_expref(z), fmpr_expref(z), extra); return _fmpr_normalise(fmpr_manref(z), fmpr_expref(z), prec, rnd); } }