void fmpq_poly_get_slice(fmpq_poly_t rop, const fmpq_poly_t op, long i, long j) { i = FLINT_MAX(i, 0); j = FLINT_MIN(j, op->length); if (i < j) { long k; if (rop == op) { for (k = 0; k < i; k++) fmpz_zero(rop->coeffs + k); for (k = j; k < rop->length; k++) fmpz_zero(rop->coeffs + k); fmpq_poly_canonicalise(rop); } else { fmpq_poly_fit_length(rop, j); _fmpq_poly_set_length(rop, j); _fmpz_vec_set(rop->coeffs + i, op->coeffs + i, j - i); fmpz_set(rop->den, op->den); fmpq_poly_canonicalise(rop); } } else { fmpq_poly_zero(rop); } }
void fmpq_poly_set_coeff_mpz(fmpq_poly_t poly, long n, const mpz_t x) { long len = poly->length; const int replace = (n < len && !fmpz_is_zero(poly->coeffs + n)); if (!replace && mpz_sgn(x) == 0) return; if (n + 1 > len) { fmpq_poly_fit_length(poly, n + 1); _fmpq_poly_set_length(poly, n + 1); mpn_zero((mp_ptr) poly->coeffs + len, (n + 1) - len); } if (*poly->den == 1L) { fmpz_set_mpz(poly->coeffs + n, x); if (replace) _fmpq_poly_normalise(poly); } else { fmpz_set_mpz(poly->coeffs + n, x); fmpz_mul(poly->coeffs + n, poly->coeffs + n, poly->den); if (replace) fmpq_poly_canonicalise(poly); } }
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_reverse(fmpq_poly_t res, const fmpq_poly_t poly, slong n) { slong len = FLINT_MIN(n, poly->length); if (len == 0) { fmpq_poly_zero(res); return; } fmpq_poly_fit_length(res, n); _fmpz_poly_reverse(res->coeffs, poly->coeffs, len, n); fmpz_set(res->den, poly->den); _fmpq_poly_set_length(res, n); fmpq_poly_canonicalise(res); }
void random_fmpq_poly(fmpq_poly_t pol, flint_rand_t state, slong length) { fmpz * arr; slong i; fmpq_poly_fit_length(pol, length); arr = fmpq_poly_numref(pol); for (i = 0; i < length; i++) fmpz_randbits(arr + i, state, BITS); fmpz_randbits(fmpq_poly_denref(pol), state, BITS); _fmpq_poly_set_length(pol, length); _fmpq_poly_normalise(pol); fmpq_poly_canonicalise(pol); }
void fmpq_poly_interpolate_fmpz_vec(fmpq_poly_t poly, const fmpz * xs, const fmpz * ys, long n) { if (n == 0) { fmpq_poly_zero(poly); } else if (n == 1) { fmpq_poly_set_fmpz(poly, ys); } else { fmpq_poly_fit_length(poly, n); _fmpq_poly_interpolate_fmpz_vec(poly->coeffs, poly->den, xs, ys, n); _fmpq_poly_set_length(poly, n); fmpq_poly_canonicalise(poly); } }
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_randtest_unsigned(fmpq_poly_t poly, flint_rand_t state, long len, mp_bitcnt_t bits) { ulong m; m = n_randlimb(NULL); fmpq_poly_fit_length(poly, len); _fmpq_poly_set_length(poly, len); if (m & 1UL) { _fmpz_vec_randtest_unsigned(poly->coeffs, state, len, bits); } else { fmpz_t x; fmpz_init(x); fmpz_randtest_unsigned(x, state, bits / 2); _fmpz_vec_randtest_unsigned(poly->coeffs, state, len, (bits + 1) / 2); _fmpz_vec_scalar_mul_fmpz(poly->coeffs, poly->coeffs, len, x); fmpz_clear(x); } if (m & 2UL) { fmpz_randtest_not_zero(poly->den, state, FLINT_MAX(bits, 1)); fmpz_abs(poly->den, poly->den); fmpq_poly_canonicalise(poly); } else { fmpz_set_ui(poly->den, 1); _fmpq_poly_normalise(poly); } }
int main(void) { int i, result; ulong cflags = UWORD(0); FLINT_TEST_INIT(state); flint_printf("sqrt_series...."); fflush(stdout); /* Check aliasing of a and c */ for (i = 0; i < 50 * flint_test_multiplier(); i++) { fmpq_poly_t a, b; slong n = n_randint(state, 50) + 1; fmpq_poly_init(a); fmpq_poly_init(b); fmpq_poly_randtest_not_zero(a, state, n_randint(state, 50) + 1, 50); fmpq_poly_set_coeff_ui(a, 0, UWORD(1)); fmpq_poly_canonicalise(a); fmpq_poly_sqrt_series(b, a, n); fmpq_poly_sqrt_series(a, a, n); cflags |= fmpq_poly_is_canonical(a) ? 0 : 1; cflags |= fmpq_poly_is_canonical(b) ? 0 : 2; result = (fmpq_poly_equal(a, b) && !cflags); if (!result) { flint_printf("FAIL:\n"); fmpq_poly_debug(a), flint_printf("\n\n"); fmpq_poly_debug(b), flint_printf("\n\n"); flint_printf("cflags = %wu\n\n", cflags); abort(); } fmpq_poly_clear(a); fmpq_poly_clear(b); } /* Check sqrt(a)^2 = a */ for (i = 0; i < 50 * flint_test_multiplier(); i++) { fmpq_poly_t a, b, c; slong n = n_randint(state, 80) + 1; fmpq_poly_init(a); fmpq_poly_init(b); fmpq_poly_init(c); fmpq_poly_randtest_not_zero(a, state, n_randint(state, 80) + 1, 80); fmpq_poly_set_coeff_ui(a, 0, UWORD(1)); fmpq_poly_sqrt_series(b, a, n); fmpq_poly_mullow(c, b, b, n); fmpq_poly_truncate(a, n); cflags |= fmpq_poly_is_canonical(b) ? 0 : 1; result = (fmpq_poly_equal(c, a) && !cflags); if (!result) { flint_printf("FAIL:\n"); flint_printf("a = "), fmpq_poly_debug(a), flint_printf("\n\n"); flint_printf("b = "), fmpq_poly_debug(b), flint_printf("\n\n"); flint_printf("c = "), fmpq_poly_debug(c), flint_printf("\n\n"); flint_printf("cflags = %wu\n\n", cflags); abort(); } fmpq_poly_clear(a); fmpq_poly_clear(b); fmpq_poly_clear(c); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int main(void) { int i, result; ulong cflags = UWORD(0); FLINT_TEST_INIT(state); flint_printf("asinh_series...."); fflush(stdout); /* Check aliasing of a and c */ for (i = 0; i < 20 * flint_test_multiplier(); i++) { fmpq_poly_t a, b; slong n = n_randint(state, 50) + 1; fmpq_poly_init(a); fmpq_poly_init(b); fmpq_poly_randtest_not_zero(a, state, n_randint(state, 50) + 1, 50); fmpq_poly_set_coeff_ui(a, 0, UWORD(0)); fmpq_poly_canonicalise(a); fmpq_poly_asinh_series(b, a, n); fmpq_poly_asinh_series(a, a, n); cflags |= fmpq_poly_is_canonical(a) ? 0 : 1; cflags |= fmpq_poly_is_canonical(b) ? 0 : 2; result = (fmpq_poly_equal(a, b) && !cflags); if (!result) { flint_printf("FAIL:\n"); fmpq_poly_debug(a), flint_printf("\n\n"); fmpq_poly_debug(b), flint_printf("\n\n"); flint_printf("cflags = %wu\n\n", cflags); abort(); } fmpq_poly_clear(a); fmpq_poly_clear(b); } /* Check asinh(A) = atanh(A/sqrt(1+A^2)) */ for (i = 0; i < 20 * flint_test_multiplier(); i++) { fmpq_poly_t A, B, asinhA, atanhB; slong n = n_randint(state, 80) + 1; fmpq_poly_init(A); fmpq_poly_init(B); fmpq_poly_init(asinhA); fmpq_poly_init(atanhB); fmpq_poly_randtest_not_zero(A, state, n_randint(state, 80) + 1, 80); fmpq_poly_set_coeff_ui(A, 0, UWORD(0)); fmpq_poly_mullow(B, A, A, n); fmpq_poly_set_coeff_ui(B, 0, UWORD(1)); fmpq_poly_invsqrt_series(B, B, n); fmpq_poly_mullow(B, A, B, n); fmpq_poly_asinh_series(asinhA, A, n); fmpq_poly_atanh_series(atanhB, B, n); cflags |= fmpq_poly_is_canonical(asinhA) ? 0 : 1; cflags |= fmpq_poly_is_canonical(atanhB) ? 0 : 2; result = (fmpq_poly_equal(asinhA, atanhB) && !cflags); if (!result) { flint_printf("FAIL:\n"); flint_printf("A = "), fmpq_poly_debug(A), flint_printf("\n\n"); flint_printf("B = "), fmpq_poly_debug(B), flint_printf("\n\n"); flint_printf("asinh(A) = "), fmpq_poly_debug(asinhA), flint_printf("\n\n"); flint_printf("atanh(B) = "), fmpq_poly_debug(atanhB), flint_printf("\n\n"); flint_printf("cflags = %wu\n\n", cflags); abort(); } fmpq_poly_clear(A); fmpq_poly_clear(B); fmpq_poly_clear(asinhA); fmpq_poly_clear(atanhB); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int main(void) { int i, result; flint_rand_t state; ulong cflags = 0UL; printf("sinh_series...."); fflush(stdout); flint_randinit(state); /* Check aliasing of a and c */ for (i = 0; i < 200; i++) { fmpq_poly_t a, b; long n = n_randint(state, 50) + 1; fmpq_poly_init(a); fmpq_poly_init(b); fmpq_poly_randtest_not_zero(a, state, n_randint(state, 50) + 1, 50); fmpq_poly_set_coeff_ui(a, 0, 0UL); fmpq_poly_canonicalise(a); fmpq_poly_sinh_series(b, a, n); fmpq_poly_sinh_series(a, a, n); cflags |= fmpq_poly_is_canonical(a) ? 0 : 1; cflags |= fmpq_poly_is_canonical(b) ? 0 : 2; result = (fmpq_poly_equal(a, b) && !cflags); if (!result) { printf("FAIL:\n"); fmpq_poly_debug(a), printf("\n\n"); fmpq_poly_debug(b), printf("\n\n"); printf("cflags = %lu\n\n", cflags); abort(); } fmpq_poly_clear(a); fmpq_poly_clear(b); } /* Check asinh(sinh(a)) = a */ for (i = 0; i < 200; i++) { fmpq_poly_t a, sinha, asinhsinha; long n = n_randint(state, 80) + 1; fmpq_poly_init(a); fmpq_poly_init(sinha); fmpq_poly_init(asinhsinha); fmpq_poly_randtest_not_zero(a, state, n_randint(state, 60) + 1, 80); fmpq_poly_set_coeff_ui(a, 0, 0UL); fmpq_poly_sinh_series(sinha, a, n); fmpq_poly_asinh_series(asinhsinha, sinha, n); fmpq_poly_truncate(a, n); cflags |= fmpq_poly_is_canonical(sinha) ? 0 : 1; cflags |= fmpq_poly_is_canonical(asinhsinha) ? 0 : 2; result = (fmpq_poly_equal(asinhsinha, a) && !cflags); if (!result) { printf("FAIL:\n"); printf("a = "), fmpq_poly_debug(a), printf("\n\n"); printf("sinh(a) = "), fmpq_poly_debug(sinha), printf("\n\n"); printf("asinh(sinh(a)) = "), fmpq_poly_debug(asinhsinha), printf("\n\n"); printf("cflags = %lu\n\n", cflags); abort(); } fmpq_poly_clear(a); fmpq_poly_clear(sinha); fmpq_poly_clear(asinhsinha); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main(int argc, char* argv[]) { int i, j, k; int levels[argc-1]; fmpq_poly_t Pn, Ep; long deg; char *strf; acb_ptr nodes; acb_ptr weights; int solvable; int validate_extension, validate_weights; int valid; int comp_nodes, comp_weights; int target_prec; int nrprintdigits; int loglevel; if(argc <= 1) { printf("Compute a nested generalized Kronrod extension of a Gauss rule\n"); printf("Syntax: kes [-ve] [-vw] [-cn] [-cw] [-dc D] [-dp D] [-l L] n p1 p2 ... pk\n"); printf("Options:\n"); printf(" -ve Validate the polynomial extension by nodes\n"); printf(" -vw Validate the polynomial extension by weights\n"); printf(" -cn Compute the nodes\n"); printf(" -cw Compute the weights\n"); printf(" -dc Compute nodes and weights up to this number of decimal digits\n"); printf(" -dp Print this number of decimal digits\n"); printf(" -l Set the log level\n"); return EXIT_FAILURE; } target_prec = 53; nrprintdigits = 20; loglevel = 8; comp_nodes = 0; comp_weights = 0; validate_extension = 0; validate_weights = 0; k = 0; for(i = 1; i < argc; i++) { if (!strcmp(argv[i], "-ve")) { validate_extension = 1; } else if (!strcmp(argv[i], "-vw")) { validate_weights = 1; } else if (!strcmp(argv[i], "-cn")) { comp_nodes = 1; } else if (!strcmp(argv[i], "-cw")) { comp_weights = 1; } else if (!strcmp(argv[i], "-dc")) { /* 'digits' is in base 10 and log(10)/log(2) = 3.32193 */ target_prec = 3.32193 * atoi(argv[i+1]); i++; } else if (!strcmp(argv[i], "-dp")) { nrprintdigits = atoi(argv[i+1]); i++; } else if (!strcmp(argv[i], "-l")) { loglevel = atoi(argv[i+1]); i++; } else { levels[k] = atoi(argv[i]); k++; } } /* Compute extension */ fmpq_poly_init(Pn); polynomial(Pn, levels[0]); printf("Starting with polynomial:\n"); strf = fmpq_poly_get_str_pretty(Pn, "t"); flint_printf("P : %s\n", strf); printf("Extension levels are:"); for(i = 0; i < k; i++) { printf(" %i", levels[i]); } printf("\n"); fmpq_poly_init(Ep); solvable = find_multi_extension(Ep, Pn, k, levels, validate_extension, loglevel); fmpq_poly_mul(Pn, Pn, Ep); fmpq_poly_canonicalise(Pn); if(solvable) { if(! fmpq_poly_is_squarefree(Pn)) { printf("=====> FINAL POLYNOMIAL NOT SQF <====="); } if(loglevel >= 2) { printf("-------------------------------------------------\n"); printf("Ending with final polynomial:\n"); strf = fmpq_poly_get_str_pretty(Pn, "t"); flint_printf("P : %s\n", strf); } deg = fmpq_poly_degree(Pn); nodes = _acb_vec_init(deg); weights = _acb_vec_init(deg); if(comp_weights || validate_weights) { compute_nodes_and_weights(nodes, weights, Pn, target_prec, loglevel); } else if(comp_nodes) { compute_nodes(nodes, Pn, target_prec, loglevel); } valid = 1; if(validate_weights) { valid = validate_extension_by_weights(weights, deg, target_prec, loglevel); } if(! valid) { printf("**************************************\n"); printf("*** EXTENSION WITH INVALID WEIGHTS ***\n"); printf("**************************************\n"); } /* Print roots and weights */ if((validate_weights && valid) || ! validate_weights) { if(comp_nodes) { printf("-------------------------------------------------\n"); printf("The nodes are:\n"); for(j = 0; j < deg; j++) { printf("| "); acb_printd(nodes + j, nrprintdigits); printf("\n"); } } if(comp_weights) { printf("-------------------------------------------------\n"); printf("The weights are:\n"); for(j = 0; j < deg; j++) { printf("| "); acb_printd(weights + j, nrprintdigits); printf("\n"); } } } _acb_vec_clear(nodes, deg); _acb_vec_clear(weights, deg); } flint_free(strf); fmpq_poly_clear(Pn); fmpq_poly_clear(Ep); return EXIT_SUCCESS; }
int main(void) { int i, result; flint_rand_t state; ulong cflags = 0UL; printf("exp_series...."); fflush(stdout); flint_randinit(state); /* Check aliasing of a and c */ for (i = 0; i < 200; i++) { fmpq_poly_t a, b; long n = n_randint(state, 50) + 1; fmpq_poly_init(a); fmpq_poly_init(b); fmpq_poly_randtest_not_zero(a, state, n_randint(state, 50) + 1, 50); fmpq_poly_set_coeff_ui(a, 0, 0UL); fmpq_poly_canonicalise(a); fmpq_poly_exp_series(b, a, n); fmpq_poly_exp_series(a, a, n); cflags |= fmpq_poly_is_canonical(a) ? 0 : 1; cflags |= fmpq_poly_is_canonical(b) ? 0 : 2; result = (fmpq_poly_equal(a, b) && !cflags); if (!result) { printf("FAIL:\n"); fmpq_poly_debug(a), printf("\n\n"); fmpq_poly_debug(b), printf("\n\n"); printf("cflags = %lu\n\n", cflags); abort(); } fmpq_poly_clear(a); fmpq_poly_clear(b); } /* Check exp(a+b) = exp(a) * exp(b) */ for (i = 0; i < 500; i++) { fmpq_poly_t a, b, ab, expa, expb, expab, expa_expb; long n = n_randint(state, 80) + 1; fmpq_poly_init(a); fmpq_poly_init(b); fmpq_poly_init(ab); fmpq_poly_init(expa); fmpq_poly_init(expb); fmpq_poly_init(expab); fmpq_poly_init(expa_expb); fmpq_poly_randtest_not_zero(a, state, n_randint(state, 60) + 1, 80); fmpq_poly_set_coeff_ui(a, 0, 0UL); fmpq_poly_randtest_not_zero(b, state, n_randint(state, 60) + 1, 80); fmpq_poly_set_coeff_ui(b, 0, 0UL); fmpq_poly_add(ab, a, b); fmpq_poly_exp_series(expab, ab, n); fmpq_poly_exp_series(expa, a, n); fmpq_poly_exp_series(expb, b, n); fmpq_poly_mullow(expa_expb, expa, expb, n); cflags |= fmpq_poly_is_canonical(expa) ? 0 : 1; cflags |= fmpq_poly_is_canonical(expb) ? 0 : 2; cflags |= fmpq_poly_is_canonical(expab) ? 0 : 4; result = (fmpq_poly_equal(expab, expa_expb) && !cflags); if (!result) { printf("FAIL:\n"); printf("a = "), fmpq_poly_debug(a), printf("\n\n"); printf("b = "), fmpq_poly_debug(b), printf("\n\n"); printf("exp(a) = "), fmpq_poly_debug(expa), printf("\n\n"); printf("exp(b) = "), fmpq_poly_debug(expb), printf("\n\n"); printf("exp(ab) = "), fmpq_poly_debug(expab), printf("\n\n"); printf("cflags = %lu\n\n", cflags); abort(); } fmpq_poly_clear(a); fmpq_poly_clear(b); fmpq_poly_clear(ab); fmpq_poly_clear(expa); fmpq_poly_clear(expb); fmpq_poly_clear(expab); fmpq_poly_clear(expa_expb); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }