renf_elem_class & renf_elem_class::operator=(const renf_elem_class & value) noexcept { if (value.nf == nullptr) { if (nf != nullptr) { renf_elem_clear(a, nf->renf_t()); nf = nullptr; fmpq_init(b); } } else { if (nf == nullptr) { fmpq_clear(b); renf_elem_init(a, value.nf->renf_t()); nf = value.nf; } else if (value.nf != nf) { renf_elem_clear(a, nf->renf_t()); renf_elem_init(a, value.nf->renf_t()); nf = value.nf; } } if (value.nf == nullptr) fmpq_set(b, value.b); else renf_elem_set(a, value.a, nf->renf_t()); return *this; }
void renf_elem_class::assign(const ::fmpq_t value) noexcept { if (nf == nullptr) fmpq_set(b, value); else renf_elem_set_fmpq(a, value, nf->renf_t()); }
void fmpq_mat_transpose(fmpq_mat_t rop, const fmpq_mat_t op) { slong i, j; if (rop == op) { for (i = 0; i < rop->r; i++) for (j = 0; j < i; j++) fmpq_swap(fmpq_mat_entry(rop, i, j), fmpq_mat_entry(rop, j, i)); } else { for (i = 0; i < rop->r; i++) for (j = 0; j < rop->c; j++) fmpq_set(fmpq_mat_entry(rop, i, j), fmpq_mat_entry(op, j, i)); } }
void fmpq_mul_2exp(fmpq_t res, const fmpq_t x, mp_bitcnt_t exp) { if (fmpq_is_zero(x) || exp == 0) { fmpq_set(res, x); } else { mp_bitcnt_t v = fmpz_val2(fmpq_denref(x)); if (exp <= v) { fmpz_set(fmpq_numref(res), fmpq_numref(x)); fmpz_fdiv_q_2exp(fmpq_denref(res), fmpq_denref(x), exp); } else { fmpz_mul_2exp(fmpq_numref(res), fmpq_numref(x), exp - v); fmpz_fdiv_q_2exp(fmpq_denref(res), fmpq_denref(x), v); } } }
int fmpq_mat_inv(fmpq_mat_t B, const fmpq_mat_t A) { long n = A->r; if (n == 0) { return 1; } else if (n == 1) { if (fmpq_is_zero(fmpq_mat_entry(A, 0, 0))) return 0; fmpq_inv(fmpq_mat_entry(B, 0, 0), fmpq_mat_entry(A, 0, 0)); return 1; } else if (n == 2) { fmpq_t d; int success; fmpq_init(d); fmpq_mul(d, fmpq_mat_entry(A, 0, 0), fmpq_mat_entry(A, 1, 1)); fmpq_submul(d, fmpq_mat_entry(A, 0, 1), fmpq_mat_entry(A, 1, 0)); success = !fmpq_is_zero(d); if (success) { fmpq_t t00, t01, t10, t11; fmpq_inv(d, d); fmpq_init(t00); fmpq_init(t01); fmpq_init(t10); fmpq_init(t11); fmpq_mul(t00, fmpq_mat_entry(A, 1, 1), d); fmpq_mul(t01, fmpq_mat_entry(A, 0, 1), d); fmpq_mul(t10, fmpq_mat_entry(A, 1, 0), d); fmpq_mul(t11, fmpq_mat_entry(A, 0, 0), d); fmpq_set(fmpq_mat_entry(B, 0, 0), t00); fmpq_neg(fmpq_mat_entry(B, 0, 1), t01); fmpq_neg(fmpq_mat_entry(B, 1, 0), t10); fmpq_set(fmpq_mat_entry(B, 1, 1), t11); fmpq_clear(t00); fmpq_clear(t01); fmpq_clear(t10); fmpq_clear(t11); } fmpq_clear(d); return success; } else { fmpz_mat_t Aclear, Bclear, I; fmpz * den; long i; int success; fmpz_mat_init(Aclear, n, n); fmpz_mat_init(Bclear, n, n); fmpz_mat_init(I, n, n); den = _fmpz_vec_init(n); fmpq_mat_get_fmpz_mat_rowwise(Aclear, den, A); for (i = 0; i < n; i++) fmpz_set(fmpz_mat_entry(I, i, i), den + i); success = fmpz_mat_solve(Bclear, den, Aclear, I); if (success) fmpq_mat_set_fmpz_mat_div_fmpz(B, Bclear, den); fmpz_mat_clear(Aclear); fmpz_mat_clear(Bclear); fmpz_mat_clear(I); _fmpz_vec_clear(den, A->r); return success; } }
int main() { slong iter, iter2; flint_rand_t state; flint_printf("submul...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++) { arb_t a, b, c; fmpq_t x, y, z; arb_init(a); arb_init(b); arb_init(c); fmpq_init(x); fmpq_init(y); fmpq_init(z); arb_randtest(a, state, 1 + n_randint(state, 200), 10); arb_randtest(b, state, 1 + n_randint(state, 200), 10); arb_randtest(c, state, 1 + n_randint(state, 200), 10); arb_get_rand_fmpq(x, state, a, 1 + n_randint(state, 200)); arb_get_rand_fmpq(y, state, b, 1 + n_randint(state, 200)); arb_get_rand_fmpq(z, state, c, 1 + n_randint(state, 200)); arb_submul(c, a, b, 2 + n_randint(state, 200)); fmpq_submul(z, x, y); if (!arb_contains_fmpq(c, z)) { flint_printf("FAIL: containment\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("y = "); fmpq_print(y); flint_printf("\n\n"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); flint_printf("z = "); fmpq_print(z); flint_printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); arb_clear(c); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); } /* aliasing of c and a */ for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arb_t a, b; fmpq_t x, y, z; arb_init(a); arb_init(b); fmpq_init(x); fmpq_init(y); fmpq_init(z); arb_randtest(a, state, 1 + n_randint(state, 200), 10); arb_randtest(b, state, 1 + n_randint(state, 200), 10); arb_get_rand_fmpq(x, state, a, 1 + n_randint(state, 200)); arb_get_rand_fmpq(y, state, b, 1 + n_randint(state, 200)); fmpq_set(z, x); arb_submul(a, a, b, 2 + n_randint(state, 200)); fmpq_submul(z, x, y); if (!arb_contains_fmpq(a, z)) { flint_printf("FAIL: aliasing (c, a)\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("y = "); fmpq_print(y); flint_printf("\n\n"); flint_printf("z = "); fmpq_print(z); flint_printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); } /* aliasing of c and b */ for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arb_t a, b; fmpq_t x, y, z; arb_init(a); arb_init(b); fmpq_init(x); fmpq_init(y); fmpq_init(z); arb_randtest(a, state, 1 + n_randint(state, 200), 10); arb_randtest(b, state, 1 + n_randint(state, 200), 10); arb_get_rand_fmpq(x, state, a, 1 + n_randint(state, 200)); arb_get_rand_fmpq(y, state, b, 1 + n_randint(state, 200)); fmpq_set(z, y); arb_submul(b, a, b, 2 + n_randint(state, 200)); fmpq_submul(z, x, y); if (!arb_contains_fmpq(b, z)) { flint_printf("FAIL: aliasing (c, b)\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("y = "); fmpq_print(y); flint_printf("\n\n"); flint_printf("z = "); fmpq_print(z); flint_printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); } /* main test */ for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arb_t x, y, z, v; slong prec; arb_init(x); arb_init(y); arb_init(z); arb_init(v); for (iter2 = 0; iter2 < 100; iter2++) { arb_randtest_special(x, state, n_randint(state,2) ? 2000 : 200, 200); arb_randtest_special(y, state, n_randint(state,2) ? 2000 : 200, 200); arb_randtest_special(z, state, n_randint(state,2) ? 2000 : 200, 200); prec = 2 + n_randint(state, 2000); switch (n_randint(state, 5)) { case 0: arb_set(v, z); arb_submul(z, x, y, prec); arb_submul_naive(v, x, y, prec); if (!arf_equal(arb_midref(z), arb_midref(v)) || !mag_close(arb_radref(z), arb_radref(v))) { flint_printf("FAIL!\n"); flint_printf("x = "); arb_print(x); flint_printf("\n\n"); flint_printf("y = "); arb_print(y); flint_printf("\n\n"); flint_printf("z = "); arb_print(z); flint_printf("\n\n"); flint_printf("v = "); arb_print(v); flint_printf("\n\n"); abort(); } break; case 1: arb_set(y, x); arb_set(z, v); arb_submul(z, x, y, prec); arb_submul(v, x, x, prec); if (!arf_equal(arb_midref(z), arb_midref(v)) || !mag_close(arb_radref(z), arb_radref(v))) { flint_printf("FAIL (aliasing 1)!\n"); flint_printf("x = "); arb_print(x); flint_printf("\n\n"); flint_printf("z = "); arb_print(z); flint_printf("\n\n"); flint_printf("v = "); arb_print(v); flint_printf("\n\n"); abort(); } break; case 2: arb_set(v, x); arb_submul(v, x, x, prec); arb_submul(x, x, x, prec); if (!arf_equal(arb_midref(x), arb_midref(v)) || !mag_close(arb_radref(x), arb_radref(v))) { flint_printf("FAIL (aliasing 2)!\n"); flint_printf("x = "); arb_print(x); flint_printf("\n\n"); flint_printf("v = "); arb_print(v); flint_printf("\n\n"); abort(); } break; case 3: arb_set(v, x); arb_submul(v, x, y, prec); arb_submul(x, x, y, prec); if (!arf_equal(arb_midref(x), arb_midref(v)) || !mag_close(arb_radref(x), arb_radref(v))) { flint_printf("FAIL (aliasing 3)!\n"); flint_printf("x = "); arb_print(x); flint_printf("\n\n"); flint_printf("y = "); arb_print(y); flint_printf("\n\n"); flint_printf("v = "); arb_print(v); flint_printf("\n\n"); abort(); } break; default: arb_set(v, x); arb_submul(v, x, y, prec); arb_submul(x, y, x, prec); if (!arf_equal(arb_midref(x), arb_midref(v)) || !mag_close(arb_radref(x), arb_radref(v))) { flint_printf("FAIL (aliasing 4)!\n"); flint_printf("x = "); arb_print(x); flint_printf("\n\n"); flint_printf("y = "); arb_print(y); flint_printf("\n\n"); flint_printf("v = "); arb_print(v); flint_printf("\n\n"); abort(); } break; } } arb_clear(x); arb_clear(y); arb_clear(z); arb_clear(v); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { long iter; flint_rand_t state; printf("addmul...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000; iter++) { fmprb_t a, b, c; fmpq_t x, y, z; fmprb_init(a); fmprb_init(b); fmprb_init(c); fmpq_init(x); fmpq_init(y); fmpq_init(z); fmprb_randtest(a, state, 1 + n_randint(state, 200), 10); fmprb_randtest(b, state, 1 + n_randint(state, 200), 10); fmprb_randtest(c, state, 1 + n_randint(state, 200), 10); fmprb_get_rand_fmpq(x, state, a, 1 + n_randint(state, 200)); fmprb_get_rand_fmpq(y, state, b, 1 + n_randint(state, 200)); fmprb_get_rand_fmpq(z, state, c, 1 + n_randint(state, 200)); fmprb_addmul(c, a, b, 2 + n_randint(state, 200)); fmpq_addmul(z, x, y); if (!fmprb_contains_fmpq(c, z)) { printf("FAIL: containment\n\n"); printf("a = "); fmprb_print(a); printf("\n\n"); printf("x = "); fmpq_print(x); printf("\n\n"); printf("b = "); fmprb_print(b); printf("\n\n"); printf("y = "); fmpq_print(y); printf("\n\n"); printf("c = "); fmprb_print(c); printf("\n\n"); printf("z = "); fmpq_print(z); printf("\n\n"); abort(); } fmprb_clear(a); fmprb_clear(b); fmprb_clear(c); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); } /* aliasing of c and a */ for (iter = 0; iter < 10000; iter++) { fmprb_t a, b; fmpq_t x, y, z; fmprb_init(a); fmprb_init(b); fmpq_init(x); fmpq_init(y); fmpq_init(z); fmprb_randtest(a, state, 1 + n_randint(state, 200), 10); fmprb_randtest(b, state, 1 + n_randint(state, 200), 10); fmprb_get_rand_fmpq(x, state, a, 1 + n_randint(state, 200)); fmprb_get_rand_fmpq(y, state, b, 1 + n_randint(state, 200)); fmpq_set(z, x); fmprb_addmul(a, a, b, 2 + n_randint(state, 200)); fmpq_addmul(z, x, y); if (!fmprb_contains_fmpq(a, z)) { printf("FAIL: aliasing (c, a)\n\n"); printf("a = "); fmprb_print(a); printf("\n\n"); printf("x = "); fmpq_print(x); printf("\n\n"); printf("b = "); fmprb_print(b); printf("\n\n"); printf("y = "); fmpq_print(y); printf("\n\n"); printf("z = "); fmpq_print(z); printf("\n\n"); abort(); } fmprb_clear(a); fmprb_clear(b); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); } /* aliasing of c and b */ for (iter = 0; iter < 10000; iter++) { fmprb_t a, b; fmpq_t x, y, z; fmprb_init(a); fmprb_init(b); fmpq_init(x); fmpq_init(y); fmpq_init(z); fmprb_randtest(a, state, 1 + n_randint(state, 200), 10); fmprb_randtest(b, state, 1 + n_randint(state, 200), 10); fmprb_get_rand_fmpq(x, state, a, 1 + n_randint(state, 200)); fmprb_get_rand_fmpq(y, state, b, 1 + n_randint(state, 200)); fmpq_set(z, y); fmprb_addmul(b, a, b, 2 + n_randint(state, 200)); fmpq_addmul(z, x, y); if (!fmprb_contains_fmpq(b, z)) { printf("FAIL: aliasing (c, b)\n\n"); printf("a = "); fmprb_print(a); printf("\n\n"); printf("x = "); fmpq_print(x); printf("\n\n"); printf("b = "); fmprb_print(b); printf("\n\n"); printf("y = "); fmpq_print(y); printf("\n\n"); printf("z = "); fmpq_print(z); printf("\n\n"); abort(); } fmprb_clear(a); fmprb_clear(b); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); } flint_randclear(state); flint_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
void set(ElementType& result, const ElementType& a) const {fmpq_set(&result, &a);}
void init_set(ElementType& result, const ElementType& a) const { fmpq_init(&result); fmpq_set(&result, &a); }
void fmpz_holonomic_get_nth_fmpq(fmpq_t res, const fmpz_holonomic_t op, const fmpq * initial, long n0, long n) { long r = fmpz_holonomic_order(op); if (r == 0) { fmpq_zero(res); return; } else if (n < n0) { printf("not implemented\n"); abort(); } else if (n - n0 < r) { fmpq_set(res, initial + n - n0); return; } else { fmpz_mat_t M; long i; fmpz_t Q; fmpz_mat_init(M, r, r); fmpz_init(Q); fmpz_holonomic_forward_fmpz_mat(M, Q, op, n0, n - n0 - r + 1); { fmpz_t g, t; fmpz_init(g); fmpz_init(t); fmpz_one(g); for (i = 0; i < r; i++) fmpz_lcm(g, g, fmpq_denref(initial + i)); fmpz_divexact(t, g, fmpq_denref(initial + 0)); fmpz_mul(t, t, fmpq_numref(initial + 0)); fmpz_mul(fmpz_mat_entry(M, r - 1, 0), fmpz_mat_entry(M, r - 1, 0), t); for (i = 1; i < r; i++) { fmpz_divexact(t, g, fmpq_denref(initial + i)); fmpz_mul(t, t, fmpq_numref(initial + i)); fmpz_addmul(fmpz_mat_entry(M, r - 1, 0), fmpz_mat_entry(M, r - 1, i), t); } fmpz_set(fmpq_numref(res), fmpz_mat_entry(M, r - 1, 0)); fmpz_mul(fmpq_denref(res), Q, g); fmpq_canonicalise(res); fmpz_clear(g); fmpz_clear(t); } fmpz_mat_clear(M); fmpz_clear(Q); } }