/* TODO: move it to ANTIC? */ int _nf_equal_fmpq(const nf_elem_t a, const fmpq_t b, const nf_t nf) { if (nf->flag & NF_LINEAR) { return (fmpz_equal(LNF_ELEM_NUMREF(a), fmpq_numref(b)) && fmpz_equal(LNF_ELEM_DENREF(a), fmpq_denref(b))); } else if (nf->flag & NF_QUADRATIC) { const fmpz * const num = QNF_ELEM_NUMREF(a); const fmpz * const den = QNF_ELEM_DENREF(a); return (fmpz_is_zero(num + 1) && fmpz_equal(num, fmpq_numref(b)) && fmpz_equal(den, fmpq_denref(b))); } else { if (NF_ELEM(a)->length > 1) return 0; else if (NF_ELEM(a)->length == 0) return fmpq_is_zero(b); return (fmpz_equal(NF_ELEM_NUMREF(a), fmpq_numref(b)) && fmpz_equal(NF_ELEM_DENREF(a), fmpq_denref(b))); } return -1; }
bool renf_elem_class::is_zero() const noexcept { if (nf == nullptr) return fmpq_is_zero(b); else return renf_elem_is_zero(a, nf->renf_t()); }
int fmpq_mat_is_invertible(const fmpq_mat_t A) { int r; fmpq_t t; fmpq_init(t); fmpq_mat_det(t, A); r = !fmpq_is_zero(t); fmpq_clear(t); return r; }
void fmpq_randtest_not_zero(fmpq_t f, flint_rand_t state, mp_bitcnt_t bits) { if (bits == 0) { printf("Exception: 0 passed to fmpq_randtest_not_zero\n"); abort(); } do { fmpq_randtest(f, state, bits); } while (fmpq_is_zero(f)); }
void fmpq_div(fmpq_t res, const fmpq_t op1, const fmpq_t op2) { if (fmpq_is_zero(op2)) { printf("Exception: fmpq_div: division by zero"); abort(); } _fmpq_div(fmpq_numref(res), fmpq_denref(res), fmpq_numref(op1), fmpq_denref(op1), fmpq_numref(op2), fmpq_denref(op2)); }
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); } } }
void fmpq_poly_scalar_div_fmpq(fmpq_poly_t rop, const fmpq_poly_t op, const fmpq_t c) { if (fmpq_is_zero(c)) { flint_printf("Exception (fmpq_poly_scalar_div_fmpq). Division by zero.\n"); abort(); } if (fmpq_poly_is_zero(op)) { fmpq_poly_zero(rop); } else { fmpq_poly_fit_length(rop, op->length); _fmpq_poly_set_length(rop, op->length); _fmpq_poly_scalar_div_fmpq(rop->coeffs, rop->den, op->coeffs, op->den, op->length, fmpq_numref(c), fmpq_denref(c)); } }
void gmde_convert_soln_fmpq(mat_t A, const ctx_t ctxA, const fmpq_mat_struct *C, long N) { long i, j, k; assert(N > 0); assert(A->m == C->r && A->n == C->c); for (i = 0; i < A->m; i++) for (j = 0; j < A->n; j++) { ctxA->zero(ctxA, mat_entry(A, i, j, ctxA)); for (k = N - 1; k >= 0; k--) { if (!fmpq_is_zero(fmpq_mat_entry(C + k, i, j))) { fmpq_poly_set_coeff_fmpq( (fmpq_poly_struct *) mat_entry(A, i, j, ctxA), k, fmpq_mat_entry(C + k, i, j)); } } } }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("one...."); fflush(stdout); /* x == 1 * x */ for (i = 0; i < 10000; i++) { fmpq_t x, y, z; fmpq_init(x); fmpq_init(y); fmpq_init(z); fmpq_randtest(x, state, 200); fmpq_one(y); fmpq_mul(z, y, x); result = fmpq_is_canonical(z) && fmpq_equal(x, z); if (!result) { flint_printf("FAIL:\n"); flint_printf("x = "), fmpq_print(x), flint_printf("\n"); flint_printf("y = "), fmpq_print(y), flint_printf("\n"); flint_printf("z = "), fmpq_print(z), flint_printf("\n"); abort(); } fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); } /* x/x == 1 */ for (i = 0; i < 10000; i++) { fmpq_t x, y; fmpq_init(x); fmpq_init(y); while (fmpq_is_zero(x)) fmpq_randtest(x, state, 200); fmpq_div(y, x, x); result = fmpq_is_canonical(y) && fmpq_is_one(y); if (!result) { flint_printf("FAIL:\n"); flint_printf("x = "), fmpq_print(x), flint_printf("\n"); flint_printf("y = "), fmpq_print(y), flint_printf("\n"); abort(); } fmpq_clear(x); fmpq_clear(y); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int renf_elem_cmp_fmpq(renf_elem_t a, const fmpq_t b, renf_t nf) { int s; slong prec, cond; arb_t diffball; renf_elem_t diffnf; if (fmpq_is_zero(b)) return renf_elem_sgn(a, nf); if (nf_elem_is_rational(a->elem, nf->nf)) { if (nf->nf->flag & NF_LINEAR) return _fmpq_cmp(LNF_ELEM_NUMREF(a->elem), LNF_ELEM_DENREF(a->elem), fmpq_numref(b), fmpq_denref(b)); else if (nf->nf->flag & NF_QUADRATIC) return _fmpq_cmp(QNF_ELEM_NUMREF(a->elem), QNF_ELEM_DENREF(a->elem), fmpq_numref(b), fmpq_denref(b)); else return _fmpq_cmp(NF_ELEM_NUMREF(a->elem), NF_ELEM_DENREF(a->elem), fmpq_numref(b), fmpq_denref(b)); } arb_init(diffball); arb_set_fmpq(diffball, b, nf->prec); arb_sub(diffball, a->emb, diffball, nf->prec); if (!arb_contains_zero(diffball)) { s = arf_sgn(arb_midref(diffball)); arb_clear(diffball); return s; } renf_elem_relative_condition_number_2exp(&cond, a, nf); prec = FLINT_MAX(nf->prec, arb_rel_accuracy_bits(nf->emb)); renf_elem_set_evaluation(a, nf, prec + cond); arb_set_fmpq(diffball, b, prec); arb_sub(diffball, a->emb, diffball, prec); if (!arb_contains_zero(diffball)) { s = arf_sgn(arb_midref(diffball)); arb_clear(diffball); return s; } arb_clear(diffball); renf_elem_init(diffnf, nf); renf_elem_set(diffnf, a, nf); renf_elem_sub_fmpq(diffnf, diffnf, b, nf); s = renf_elem_sgn(diffnf, nf); renf_elem_clear(diffnf, nf); return s; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("resultant_div...."); fflush(stdout); for (i = 0; i < 50; i++) { fmpq_poly_t f, g, h, p; fmpq_t x, y, z, zz; fmpz_t den; slong nbits; fmpq_poly_init(f); fmpq_poly_init(g); fmpq_poly_init(p); fmpq_poly_init(h); fmpq_init(x); fmpq_init(y); fmpq_init(z); fmpq_init(zz); fmpz_init(den); fmpq_poly_randtest(f, state, n_randint(state, 50), 100); fmpq_poly_randtest(g, state, n_randint(state, 50), 100); fmpq_poly_randtest(h, state, n_randint(state, 50), 100); fmpz_set(den, fmpq_poly_denref(f)); fmpq_poly_scalar_mul_fmpz(f, f, den); fmpz_set(den, fmpq_poly_denref(g)); fmpq_poly_scalar_mul_fmpz(g, g, den); fmpz_set(den, fmpq_poly_denref(h)); fmpq_poly_scalar_mul_fmpz(h, h, den); fmpq_poly_mul(p, f, g); fmpq_poly_resultant(x, f, h); if (!fmpz_is_one(fmpq_denref(x))) { flint_printf("FAIL resultant not integral\n"); flint_printf("f = "), fmpq_poly_print(f), flint_printf("\n\n"); flint_printf("g = "), fmpq_poly_print(g), flint_printf("\n\n"); flint_printf("y = "), fmpq_print(y), flint_printf("\n\n"); abort(); } fmpq_poly_resultant(y, g, h); if (!fmpz_is_one(fmpq_denref(y))) { flint_printf("FAIL resultant not integral\n"); flint_printf("h = "), fmpq_poly_print(f), flint_printf("\n\n"); flint_printf("g = "), fmpq_poly_print(g), flint_printf("\n\n"); flint_printf("z = "), fmpq_print(y), flint_printf("\n\n"); abort(); } fmpq_poly_resultant(z, p, h); if (!fmpz_is_one(fmpq_denref(z))) { flint_printf("FAIL resultant not integral\n"); flint_printf("p = "), fmpq_poly_print(f), flint_printf("\n\n"); flint_printf("g = "), fmpq_poly_print(g), flint_printf("\n\n"); flint_printf("y = "), fmpq_print(y), flint_printf("\n\n"); abort(); } if (fmpq_is_zero(z)) { fmpq_poly_clear(f); fmpq_poly_clear(g); fmpq_poly_clear(h); fmpq_poly_clear(p); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); fmpq_clear(zz); fmpz_clear(den); continue; } nbits = (slong)fmpz_bits(fmpq_numref(y)) + 1; fmpq_poly_resultant_div(z, p, h, fmpq_numref(x), nbits); fmpq_poly_resultant(zz, p, h); result = fmpq_equal(z, y); if (!result) { flint_printf("FAIL (res(p, g)/div == res(p, g)/div:\n"); flint_printf("p = "), fmpq_poly_print_pretty(p, "x"), flint_printf("\n\n"); flint_printf("h = "), fmpq_poly_print_pretty(h, "x"), flint_printf("\n\n"); flint_printf("res(p, h) = "), fmpq_print(zz), flint_printf("\n\n"); flint_printf("res(p, h) = "), fmpq_print(x), flint_printf(" * "), fmpq_print(y), flint_printf("\n\n"); flint_printf("supplied divisor = "), fmpq_print(x), flint_printf("\n\n"); flint_printf("nbits = %wu\n\n", nbits); flint_printf("divisor found = "), fmpq_print(z), flint_printf("\n\n"); flint_printf("correct result = "), fmpq_print(y), flint_printf("\n\n"); abort(); } fmpq_poly_clear(f); fmpq_poly_clear(g); fmpq_poly_clear(h); fmpq_poly_clear(p); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); fmpq_clear(zz); fmpz_clear(den); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int main() { slong iter; flint_rand_t state; flint_printf("div...."); 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); do { 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)); } while (fmpq_is_zero(y)); arb_div(c, a, b, 2 + n_randint(state, 200)); fmpq_div(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); do { 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)); } while (fmpq_is_zero(y)); arb_div(a, a, b, 2 + n_randint(state, 200)); fmpq_div(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); do { 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)); } while (fmpq_is_zero(y)); arb_div(b, a, b, 2 + n_randint(state, 200)); fmpq_div(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); } /* test special values */ for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++) { arb_t a, b, c, d; arb_init(a); arb_init(b); arb_init(c); arb_init(d); arb_randtest_special(a, state, 1 + n_randint(state, 200), 1 + n_randint(state, 100)); arb_randtest_special(b, state, 1 + n_randint(state, 200), 1 + n_randint(state, 100)); arb_randtest_special(c, state, 1 + n_randint(state, 200), 1 + n_randint(state, 100)); arb_div(c, a, b, 2 + n_randint(state, 200)); arb_mul(d, c, b, 2 + n_randint(state, 200)); if (!arb_contains(d, a)) { flint_printf("FAIL: containment\n\n"); flint_printf("a = "); arb_printd(a, 15); flint_printf("\n\n"); flint_printf("b = "); arb_printd(b, 15); flint_printf("\n\n"); flint_printf("c = "); arb_printd(c, 15); flint_printf("\n\n"); flint_printf("d = "); arb_printd(d, 15); flint_printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); arb_clear(c); arb_clear(d); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
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; } }
bool is_zero(const ElementType& f) const {return fmpq_is_zero(&f);}
int main(void) { int i; FLINT_TEST_INIT(state); flint_printf("solve_dixon...."); fflush(stdout); /* Solve nonsingular systems */ for (i = 0; i < 100 * flint_test_multiplier(); i++) { fmpq_mat_t A, B, X, AX; fmpq_t d; int success; slong n, m, bits; n = n_randint(state, 10); m = n_randint(state, 10); bits = 1 + n_randint(state, 100); fmpq_mat_init(A, n, n); fmpq_mat_init(B, n, m); fmpq_mat_init(X, n, m); fmpq_mat_init(AX, n, m); fmpq_init(d); /* XXX: replace with a randtest function */ do { fmpq_mat_randtest(A, state, bits); fmpq_mat_det(d, A); } while (fmpq_is_zero(d)); fmpq_clear(d); fmpq_mat_randtest(B, state, bits); success = fmpq_mat_solve_dixon(X, A, B); fmpq_mat_mul(AX, A, X); if (!fmpq_mat_equal(AX, B) || !success) { flint_printf("FAIL!\n"); flint_printf("success: %d\n", success); flint_printf("A:\n"); fmpq_mat_print(A); flint_printf("B:\n"); fmpq_mat_print(B); flint_printf("X:\n"); fmpq_mat_print(X); flint_printf("AX:\n"); fmpq_mat_print(AX); abort(); } fmpq_mat_clear(A); fmpq_mat_clear(B); fmpq_mat_clear(X); fmpq_mat_clear(AX); } /* Check singular systems */ for (i = 0; i < 100 * flint_test_multiplier(); i++) { fmpq_mat_t A, B, X; fmpz_mat_t M; fmpz_t den; slong n, m, bits; int success; n = 1 + n_randint(state, 10); m = 1 + n_randint(state, 10); bits = 1 + n_randint(state, 100); fmpz_init(den); fmpz_mat_init(M, n, n); fmpz_mat_randrank(M, state, n_randint(state, n), bits); if (i % 2) fmpz_mat_randops(M, state, n_randint(state, 2*m*n + 1)); fmpz_randtest_not_zero(den, state, bits); fmpq_mat_init(A, n, n); fmpq_mat_set_fmpz_mat_div_fmpz(A, M, den); fmpq_mat_init(B, n, m); fmpq_mat_randtest(B, state, bits); fmpq_mat_init(X, n, m); success = fmpq_mat_solve_dixon(X, A, B); if (success != 0) { flint_printf("FAIL!\n"); flint_printf("Expected success = 0\n"); fmpq_mat_print(A); flint_printf("\n"); abort(); } fmpq_mat_clear(A); fmpq_mat_clear(B); fmpq_mat_clear(X); fmpz_mat_clear(M); fmpz_clear(den); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }