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; }
int main() { long iter; flint_rand_t state; printf("exp_series_basecase...."); fflush(stdout); flint_randinit(state); /* compare with fmpq_poly */ for (iter = 0; iter < 10000; iter++) { long m, n, qbits, rbits1, rbits2; fmpq_poly_t A, B; fmprb_poly_t a, b; qbits = 2 + n_randint(state, 200); rbits1 = 2 + n_randint(state, 200); rbits2 = 2 + n_randint(state, 200); m = 1 + n_randint(state, 40); n = 1 + n_randint(state, 40); fmpq_poly_init(A); fmpq_poly_init(B); fmprb_poly_init(a); fmprb_poly_init(b); fmpq_poly_randtest(A, state, m, qbits); fmpq_poly_set_coeff_ui(A, 0, 0UL); fmpq_poly_exp_series(B, A, n); fmprb_poly_set_fmpq_poly(a, A, rbits1); fmprb_poly_exp_series_basecase(b, a, n, rbits2); if (!fmprb_poly_contains_fmpq_poly(b, B)) { printf("FAIL\n\n"); printf("bits2 = %ld\n", rbits2); printf("A = "); fmpq_poly_print(A); printf("\n\n"); printf("B = "); fmpq_poly_print(B); printf("\n\n"); printf("a = "); fmprb_poly_printd(a, 15); printf("\n\n"); printf("b = "); fmprb_poly_printd(b, 15); printf("\n\n"); abort(); } fmpq_poly_clear(A); fmpq_poly_clear(B); fmprb_poly_clear(a); fmprb_poly_clear(b); } /* test aliasing */ for (iter = 0; iter < 10000; iter++) { long m, n, qbits, rbits1, rbits2; fmpq_poly_t A; fmprb_poly_t a, b; qbits = 2 + n_randint(state, 200); rbits1 = 2 + n_randint(state, 200); rbits2 = 2 + n_randint(state, 200); m = 1 + n_randint(state, 40); n = 1 + n_randint(state, 40); fmpq_poly_init(A); fmprb_poly_init(a); fmprb_poly_init(b); fmpq_poly_randtest_small(A, state, m, qbits); fmprb_poly_set_fmpq_poly(a, A, rbits1); fmprb_poly_exp_series_basecase(b, a, n, rbits2); fmprb_poly_exp_series_basecase(a, a, n, rbits2); if (!fmprb_poly_equal(a, b)) { printf("FAIL\n\n"); printf("bits2 = %ld\n", rbits2); printf("A = "); fmpq_poly_print(A); printf("\n\n"); printf("a = "); fmprb_poly_printd(a, 15); printf("\n\n"); printf("b = "); fmprb_poly_printd(b, 15); printf("\n\n"); abort(); } fmpq_poly_clear(A); fmprb_poly_clear(a); fmprb_poly_clear(b); } /* test that log(exp(f)) contains f */ for (iter = 0; iter < 10000; iter++) { long m, n, qbits, rbits1, rbits2, rbits3; fmpq_poly_t A; fmprb_poly_t a, b, c; qbits = 2 + n_randint(state, 200); rbits1 = 2 + n_randint(state, 200); rbits2 = 2 + n_randint(state, 200); rbits3 = 2 + n_randint(state, 200); m = 1 + n_randint(state, 40); n = 1 + n_randint(state, 40); fmpq_poly_init(A); fmprb_poly_init(a); fmprb_poly_init(b); fmprb_poly_init(c); do { fmpq_poly_randtest_small(A, state, m, qbits); fmprb_poly_set_fmpq_poly(a, A, rbits1); fmprb_poly_exp_series_basecase(b, a, n, rbits3); } while (b->length == 0 || fmprb_contains_zero(b->coeffs)); fmprb_poly_log_series(c, b, n, rbits2); fmpq_poly_truncate(A, n); if (!fmprb_poly_contains_fmpq_poly(c, A)) { printf("FAIL\n\n"); printf("bits2 = %ld\n", rbits2); printf("bits3 = %ld\n", rbits3); printf("A = "); fmpq_poly_print(A); printf("\n\n"); printf("a = "); fmprb_poly_printd(a, 15); printf("\n\n"); printf("b = "); fmprb_poly_printd(b, 15); printf("\n\n"); printf("c = "); fmprb_poly_printd(c, 15); printf("\n\n"); abort(); } fmpq_poly_clear(A); fmprb_poly_clear(a); fmprb_poly_clear(b); fmprb_poly_clear(c); } flint_randclear(state); flint_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { fmpz_poly_t f, g; fmpz_poly_factor_t fac; fmpz_t t; slong compd, printd, i, j; if (argc < 2) { flint_printf("poly_roots [-refine d] [-print d] <poly>\n\n"); flint_printf("Isolates all the complex roots of a polynomial with integer coefficients.\n\n"); flint_printf("If -refine d is passed, the roots are refined to an absolute tolerance\n"); flint_printf("better than 10^(-d). By default, the roots are only computed to sufficient\n"); flint_printf("accuracy to isolate them. The refinement is not currently done efficiently.\n\n"); flint_printf("If -print d is passed, the computed roots are printed to d decimals.\n"); flint_printf("By default, the roots are not printed.\n\n"); flint_printf("The polynomial can be specified by passing the following as <poly>:\n\n"); flint_printf("a <n> Easy polynomial 1 + 2x + ... + (n+1)x^n\n"); flint_printf("t <n> Chebyshev polynomial T_n\n"); flint_printf("u <n> Chebyshev polynomial U_n\n"); flint_printf("p <n> Legendre polynomial P_n\n"); flint_printf("c <n> Cyclotomic polynomial Phi_n\n"); flint_printf("s <n> Swinnerton-Dyer polynomial S_n\n"); flint_printf("b <n> Bernoulli polynomial B_n\n"); flint_printf("w <n> Wilkinson polynomial W_n\n"); flint_printf("e <n> Taylor series of exp(x) truncated to degree n\n"); flint_printf("m <n> <m> The Mignotte-like polynomial x^n + (100x+1)^m, n > m\n"); flint_printf("coeffs <c0 c1 ... cn> c0 + c1 x + ... + cn x^n\n\n"); flint_printf("Concatenate to multiply polynomials, e.g.: p 5 t 6 coeffs 1 2 3\n"); flint_printf("for P_5(x)*T_6(x)*(1+2x+3x^2)\n\n"); return 1; } compd = 0; printd = 0; fmpz_poly_init(f); fmpz_poly_init(g); fmpz_init(t); fmpz_poly_one(f); for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-refine")) { compd = atol(argv[i+1]); i++; } else if (!strcmp(argv[i], "-print")) { printd = atol(argv[i+1]); i++; } else if (!strcmp(argv[i], "a")) { slong n = atol(argv[i+1]); fmpz_poly_zero(g); for (j = 0; j <= n; j++) fmpz_poly_set_coeff_ui(g, j, j+1); fmpz_poly_mul(f, f, g); i++; } else if (!strcmp(argv[i], "t")) { arith_chebyshev_t_polynomial(g, atol(argv[i+1])); fmpz_poly_mul(f, f, g); i++; } else if (!strcmp(argv[i], "u")) { arith_chebyshev_u_polynomial(g, atol(argv[i+1])); fmpz_poly_mul(f, f, g); i++; } else if (!strcmp(argv[i], "p")) { fmpq_poly_t h; fmpq_poly_init(h); arith_legendre_polynomial(h, atol(argv[i+1])); fmpq_poly_get_numerator(g, h); fmpz_poly_mul(f, f, g); fmpq_poly_clear(h); i++; } else if (!strcmp(argv[i], "c")) { arith_cyclotomic_polynomial(g, atol(argv[i+1])); fmpz_poly_mul(f, f, g); i++; } else if (!strcmp(argv[i], "s")) { arith_swinnerton_dyer_polynomial(g, atol(argv[i+1])); fmpz_poly_mul(f, f, g); i++; } else if (!strcmp(argv[i], "b")) { fmpq_poly_t h; fmpq_poly_init(h); arith_bernoulli_polynomial(h, atol(argv[i+1])); fmpq_poly_get_numerator(g, h); fmpz_poly_mul(f, f, g); fmpq_poly_clear(h); i++; } else if (!strcmp(argv[i], "w")) { slong n = atol(argv[i+1]); fmpz_poly_zero(g); fmpz_poly_fit_length(g, n+2); arith_stirling_number_1_vec(g->coeffs, n+1, n+2); _fmpz_poly_set_length(g, n+2); fmpz_poly_shift_right(g, g, 1); fmpz_poly_mul(f, f, g); i++; } else if (!strcmp(argv[i], "e")) { fmpq_poly_t h; fmpq_poly_init(h); fmpq_poly_set_coeff_si(h, 0, 0); fmpq_poly_set_coeff_si(h, 1, 1); fmpq_poly_exp_series(h, h, atol(argv[i+1]) + 1); fmpq_poly_get_numerator(g, h); fmpz_poly_mul(f, f, g); fmpq_poly_clear(h); i++; } else if (!strcmp(argv[i], "m")) { fmpz_poly_zero(g); fmpz_poly_set_coeff_ui(g, 0, 1); fmpz_poly_set_coeff_ui(g, 1, 100); fmpz_poly_pow(g, g, atol(argv[i+2])); fmpz_poly_set_coeff_ui(g, atol(argv[i+1]), 1); fmpz_poly_mul(f, f, g); i += 2; } else if (!strcmp(argv[i], "coeffs")) { fmpz_poly_zero(g); i++; j = 0; while (i < argc) { if (fmpz_set_str(t, argv[i], 10) != 0) { i--; break; } fmpz_poly_set_coeff_fmpz(g, j, t); i++; j++; } fmpz_poly_mul(f, f, g); } } fmpz_poly_factor_init(fac); flint_printf("computing squarefree factorization...\n"); TIMEIT_ONCE_START fmpz_poly_factor_squarefree(fac, f); TIMEIT_ONCE_STOP TIMEIT_ONCE_START for (i = 0; i < fac->num; i++) { flint_printf("roots with multiplicity %wd\n", fac->exp[i]); fmpz_poly_complex_roots_squarefree(fac->p + i, 32, compd * 3.32193 + 2, printd); } TIMEIT_ONCE_STOP fmpz_poly_factor_clear(fac); fmpz_poly_clear(f); fmpz_poly_clear(g); fmpz_clear(t); flint_cleanup(); return EXIT_SUCCESS; }