void _fmpz_poly_pseudo_div(fmpz * Q, ulong * d, const fmpz * A, long lenA, const fmpz * B, long lenB) { fmpz * R = _fmpz_vec_init(lenA); _fmpz_poly_pseudo_divrem(Q, R, d, A, lenA, B, lenB); _fmpz_vec_clear(R, lenA); }
void _fmpq_poly_divrem(fmpz * Q, fmpz_t q, fmpz * R, fmpz_t r, const fmpz * A, const fmpz_t a, slong lenA, const fmpz * B, const fmpz_t b, slong lenB, const fmpz_preinvn_t inv) { slong lenQ = lenA - lenB + 1; slong lenR = lenB - 1; ulong d; const fmpz * lead = B + (lenB - 1); if (lenB == 1) { _fmpq_poly_scalar_div_fmpq(Q, q, A, a, lenA, B, b); fmpz_one(r); return; } /* From pseudo division over Z we have lead^d * A = Q * B + R and thus {A, a} = {b * Q, a * lead^d} * {B, b} + {R, a * lead^d}. */ _fmpz_poly_pseudo_divrem(Q, R, &d, A, lenA, B, lenB, inv); /* Determine the actual length of R */ for ( ; lenR != 0 && fmpz_is_zero(R + (lenR - 1)); lenR--) ; /* 1. lead^d == +-1. {Q, q} = {b Q, a}, {R, r} = {R, a} up to sign */ if (d == UWORD(0) || *lead == WORD(1) || *lead == WORD(-1)) { fmpz_one(q); _fmpq_poly_scalar_mul_fmpz(Q, q, Q, q, lenQ, b); _fmpq_poly_scalar_div_fmpz(Q, q, Q, q, lenQ, a); fmpz_one(r); if (lenR > 0) _fmpq_poly_scalar_div_fmpz(R, r, R, r, lenR, a); if (*lead == WORD(-1) && d % UWORD(2)) { _fmpz_vec_neg(Q, Q, lenQ); _fmpz_vec_neg(R, R, lenR); } } /* 2. lead^d != +-1. {Q, q} = {b Q, a lead^d}, {R, r} = {R, a lead^d} */ else { /* TODO: Improve this. Clearly we do not need to compute den = a lead^d in many cases, but can determine the GCD from lead alone already. */ fmpz_t den; fmpz_init(den); fmpz_pow_ui(den, lead, d); fmpz_mul(den, a, den); fmpz_one(q); _fmpq_poly_scalar_mul_fmpz(Q, q, Q, q, lenQ, b); _fmpq_poly_scalar_div_fmpz(Q, q, Q, q, lenQ, den); fmpz_one(r); if (lenR > 0) _fmpq_poly_scalar_div_fmpz(R, r, R, r, lenR, den); fmpz_clear(den); } }