1 /*
2     Copyright (C) 2010 Sebastian Pancratz
3     Copyright (C) 2011-2014 Fredrik Johansson
4 
5     This file is part of FLINT.
6 
7     FLINT is free software: you can redistribute it and/or modify it under
8     the terms of the GNU Lesser General Public License (LGPL) as published
9     by the Free Software Foundation; either version 2.1 of the License, or
10     (at your option) any later version.  See <https://www.gnu.org/licenses/>.
11 */
12 
13 #include "fmpz_poly.h"
14 
15 #define FLINT_REVERSE_NEWTON_CUTOFF 10
16 
17 void
_fmpz_poly_revert_series_newton(fmpz * Qinv,const fmpz * Q,slong Qlen,slong n)18 _fmpz_poly_revert_series_newton(fmpz * Qinv,
19     const fmpz * Q, slong Qlen, slong n)
20 {
21     fmpz *T, *U, *V;
22     slong alloc = 3 * n;
23 
24     if (n <= 2)
25     {
26         _fmpz_vec_set(Qinv, Q, n);
27         return;
28     }
29 
30     T = _fmpz_vec_init(alloc);
31     U = T + n;
32     V = U + n;
33 
34     FLINT_NEWTON_INIT(FLINT_REVERSE_NEWTON_CUTOFF, n)
35 
36     FLINT_NEWTON_BASECASE(k)
37     _fmpz_poly_revert_series_lagrange(Qinv, Q, Qlen, k);
38     _fmpz_vec_zero(Qinv + k, n - k);
39     FLINT_NEWTON_END_BASECASE
40 
41     FLINT_NEWTON_LOOP(FLINT_UNUSED(k0), k)
42     _fmpz_poly_compose_series(T, Q, FLINT_MIN(Qlen, k), Qinv, k, k);
43     _fmpz_poly_derivative(U, T, k); fmpz_zero(U + k - 1);
44     fmpz_zero(T + 1);
45     _fmpz_poly_div_series(V, T, k, U, k, k);
46     _fmpz_poly_derivative(T, Qinv, k);
47     _fmpz_poly_mullow(U, V, k, T, k, k);
48     _fmpz_vec_sub(Qinv, Qinv, U, k);
49     FLINT_NEWTON_END_LOOP
50 
51     FLINT_NEWTON_END
52 
53     _fmpz_vec_clear(T, alloc);
54 }
55 
56 void
fmpz_poly_revert_series_newton(fmpz_poly_t Qinv,const fmpz_poly_t Q,slong n)57 fmpz_poly_revert_series_newton(fmpz_poly_t Qinv, const fmpz_poly_t Q, slong n)
58 {
59     slong Qlen = Q->length;
60 
61     if (Qlen < 2 || !fmpz_is_zero(Q->coeffs) || !fmpz_is_pm1(Q->coeffs + 1))
62     {
63         flint_printf("Exception (fmpz_poly_revert_series_newton). Input must have \n"
64                "zero constant term and +1 or -1 as coefficient of x^1\n.");
65         flint_abort();
66     }
67 
68     if (Qinv != Q)
69     {
70         fmpz_poly_fit_length(Qinv, n);
71         _fmpz_poly_revert_series_newton(Qinv->coeffs, Q->coeffs, Qlen, n);
72     }
73     else
74     {
75         fmpz_poly_t t;
76         fmpz_poly_init2(t, n);
77         _fmpz_poly_revert_series_newton(t->coeffs, Q->coeffs, Qlen, n);
78         fmpz_poly_swap(Qinv, t);
79         fmpz_poly_clear(t);
80     }
81 
82     _fmpz_poly_set_length(Qinv, n);
83     _fmpz_poly_normalise(Qinv);
84 }
85