void fmpq_poly_exp_series(fmpq_poly_t res, const fmpq_poly_t poly, long n) { fmpz *copy; int alloc; if (poly->length == 0) { fmpq_poly_set_ui(res, 1UL); return; } if (!fmpz_is_zero(poly->coeffs)) { printf("Exception: fmpq_poly_exp_series: constant term != 0"); abort(); } if (n < 2) { if (n == 0) fmpq_poly_zero(res); if (n == 1) fmpq_poly_set_ui(res, 1UL); return; } if (poly->length >= n) { copy = poly->coeffs; alloc = 0; } else { long i; copy = (fmpz *) flint_malloc(n * sizeof(fmpz)); for (i = 0; i < poly->length; i++) copy[i] = poly->coeffs[i]; for ( ; i < n; i++) copy[i] = 0; alloc = 1; } if (res != poly) { fmpq_poly_fit_length(res, n); _fmpq_poly_exp_series(res->coeffs, res->den, copy, poly->den, n); } else { fmpq_poly_t t; fmpq_poly_init2(t, n); _fmpq_poly_exp_series(t->coeffs, t->den, copy, poly->den, n); fmpq_poly_swap(res, t); fmpq_poly_clear(t); } _fmpq_poly_set_length(res, n); _fmpq_poly_normalise(res); if (alloc) flint_free(copy); }
void fmpq_poly_revert_series(fmpq_poly_t res, const fmpq_poly_t poly, long n) { fmpz *copy; int alloc; if (poly->length < 2 || !fmpz_is_zero(poly->coeffs) || fmpz_is_zero(poly->coeffs + 1)) { printf("exception: fmpq_poly_revert_series: input must have " "zero constant term and nonzero coefficient of x^1"); abort(); } if (n < 2) { fmpq_poly_zero(res); return; } if (poly->length >= n) { copy = poly->coeffs; alloc = 0; } else { long i; copy = (fmpz *) flint_malloc(n * sizeof(fmpz)); for (i = 0; i < poly->length; i++) copy[i] = poly->coeffs[i]; for ( ; i < n; i++) copy[i] = 0; alloc = 1; } if (res != poly) { fmpq_poly_fit_length(res, n); _fmpq_poly_revert_series(res->coeffs, res->den, copy, poly->den, n); } else { fmpq_poly_t t; fmpq_poly_init2(t, n); _fmpq_poly_revert_series(t->coeffs, t->den, copy, poly->den, n); fmpq_poly_swap(res, t); fmpq_poly_clear(t); } _fmpq_poly_set_length(res, n); _fmpq_poly_normalise(res); if (alloc) flint_free(copy); }
void fmpq_poly_compose(fmpq_poly_t res, const fmpq_poly_t poly1, const fmpq_poly_t poly2) { const long len1 = poly1->length; const long len2 = poly2->length; long lenr; if (len1 == 0L) { fmpq_poly_zero(res); return; } if (len1 == 1L || len2 == 0L) { fmpq_poly_fit_length(res, 1); fmpz_set(res->coeffs, poly1->coeffs); fmpz_set(res->den, poly1->den); { fmpz_t d; fmpz_init(d); fmpz_gcd(d, res->coeffs, res->den); if (*d != 1L) { fmpz_divexact(res->coeffs, res->coeffs, d); fmpz_divexact(res->den, res->den, d); } fmpz_clear(d); } _fmpq_poly_set_length(res, 1); _fmpq_poly_normalise(res); return; } lenr = (len1 - 1L) * (len2 - 1L) + 1L; if ((res != poly1) && (res != poly2)) { fmpq_poly_fit_length(res, lenr); _fmpq_poly_compose(res->coeffs, res->den, poly1->coeffs, poly1->den, len1, poly2->coeffs, poly2->den, len2); _fmpq_poly_set_length(res, lenr); _fmpq_poly_normalise(res); } else { fmpq_poly_t t; fmpq_poly_init2(t, lenr); _fmpq_poly_compose(t->coeffs, t->den, poly1->coeffs, poly1->den, len1, poly2->coeffs, poly2->den, len2); _fmpq_poly_set_length(t, lenr); _fmpq_poly_normalise(t); fmpq_poly_swap(res, t); fmpq_poly_clear(t); } }
void fmpq_poly_sqrt_series(fmpq_poly_t res, const fmpq_poly_t poly, slong n) { fmpz *copy; int alloc; if (poly->length < 1 || !fmpz_equal(poly->coeffs, poly->den)) { flint_printf("Exception (fmpq_poly_sqrt_series). Constant term != 1.\n"); abort(); } if (n < 1) { fmpq_poly_zero(res); return; } if (poly->length >= n) { copy = poly->coeffs; alloc = 0; } else { slong i; copy = (fmpz *) flint_malloc(n * sizeof(fmpz)); for (i = 0; i < poly->length; i++) copy[i] = poly->coeffs[i]; for ( ; i < n; i++) copy[i] = 0; alloc = 1; } if (res != poly) { fmpq_poly_fit_length(res, n); _fmpq_poly_sqrt_series(res->coeffs, res->den, copy, poly->den, n); } else { fmpq_poly_t t; fmpq_poly_init2(t, n); _fmpq_poly_sqrt_series(t->coeffs, t->den, copy, poly->den, n); fmpq_poly_swap(res, t); fmpq_poly_clear(t); } _fmpq_poly_set_length(res, n); fmpq_poly_canonicalise(res); if (alloc) flint_free(copy); }
void fmpq_poly_inv_series_newton(fmpq_poly_t Qinv, const fmpq_poly_t Q, long n) { fmpz *copy; int alloc; if (Q->length >= n) { copy = Q->coeffs; alloc = 0; } else { long i; copy = (fmpz *) flint_malloc(n * sizeof(fmpz)); for (i = 0; i < Q->length; i++) copy[i] = Q->coeffs[i]; for ( ; i < n; i++) copy[i] = 0; alloc = 1; } if (Qinv != Q) { fmpq_poly_fit_length(Qinv, n); _fmpq_poly_inv_series_newton(Qinv->coeffs, Qinv->den, copy, Q->den, n); } else { fmpq_poly_t t; fmpq_poly_init2(t, n); _fmpq_poly_inv_series_newton(t->coeffs, t->den, copy, Q->den, n); fmpq_poly_swap(Qinv, t); fmpq_poly_clear(t); } _fmpq_poly_set_length(Qinv, n); fmpq_poly_canonicalise(Qinv); if (alloc) flint_free(copy); }
void fmpq_poly_compose_series_horner(fmpq_poly_t res, const fmpq_poly_t poly1, const fmpq_poly_t poly2, long n) { long len1 = poly1->length; long len2 = poly2->length; long lenr; if (len2 != 0 && !fmpz_is_zero(poly2->coeffs)) { printf("exception: fmpq_poly_compose_series_horner: inner polynomial " "must have zero constant term\n"); abort(); } if (len1 == 0 || n == 0) { fmpq_poly_zero(res); return; } if (len2 == 0 || len1 == 1) { fmpq_poly_fit_length(res, 1); fmpz_set(res->coeffs, poly1->coeffs); fmpz_set(res->den, poly1->den); { fmpz_t d; fmpz_init(d); fmpz_gcd(d, res->coeffs, res->den); if (!fmpz_is_one(d)) { fmpz_divexact(res->coeffs, res->coeffs, d); fmpz_divexact(res->den, res->den, d); } fmpz_clear(d); } _fmpq_poly_set_length(res, 1); _fmpq_poly_normalise(res); return; } lenr = FLINT_MIN((len1 - 1) * (len2 - 1) + 1, n); len1 = FLINT_MIN(len1, lenr); len2 = FLINT_MIN(len2, lenr); if ((res != poly1) && (res != poly2)) { fmpq_poly_fit_length(res, lenr); _fmpq_poly_compose_series_horner(res->coeffs, res->den, poly1->coeffs, poly1->den, len1, poly2->coeffs, poly2->den, len2, lenr); _fmpq_poly_set_length(res, lenr); _fmpq_poly_normalise(res); } else { fmpq_poly_t t; fmpq_poly_init2(t, lenr); _fmpq_poly_compose_series_horner(t->coeffs, t->den, poly1->coeffs, poly1->den, len1, poly2->coeffs, poly2->den, len2, lenr); _fmpq_poly_set_length(t, lenr); _fmpq_poly_normalise(t); fmpq_poly_swap(res, t); fmpq_poly_clear(t); } }