1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/*=============================================================================
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2011, 2012 Sebastian Pancratz
******************************************************************************/
#include "fmpz_mod_poly.h"
#include "qadic.h"
void _qadic_inv(fmpz *rop, const fmpz *op, slong len,
const fmpz *a, const slong *j, slong lena,
const fmpz_t p, slong N)
{
const slong d = j[lena - 1];
if (len == 1)
{
_padic_inv(rop, op, p, N);
_fmpz_vec_zero(rop + 1, d - 1);
}
else if (N == 1)
{
fmpz *P = _fmpz_vec_init(d + 1);
slong k;
for (k = 0; k < lena; k++)
fmpz_set(P + j[k], a + k);
_fmpz_mod_poly_invmod(rop, op, len, P, d + 1, p);
_fmpz_vec_clear(P, d + 1);
}
else /* d, N >= 2 */
{
slong *e, i, n;
fmpz *pow, *u;
fmpz *s, *t;
n = FLINT_CLOG2(N) + 1;
/* Compute sequence of exponents */
e = flint_malloc(n * sizeof(slong));
for (e[i = 0] = N; e[i] > 1; i++)
e[i + 1] = (e[i] + 1) / 2;
pow = _fmpz_vec_init(n);
u = _fmpz_vec_init(len * n);
s = _fmpz_vec_init(2 * d - 1);
t = _fmpz_vec_init(2 * d - 1);
/* Compute powers of p */
{
fmpz_one(t);
fmpz_set(pow + i, p);
}
for (i--; i >= 1; i--)
{
if (e[i] & WORD(1))
{
fmpz_mul(pow + i, t, pow + (i + 1));
fmpz_mul(t, t, t);
}
else
{
fmpz_mul(t, t, pow + (i + 1));
fmpz_mul(pow + i, pow + (i + 1), pow + (i + 1));
}
}
{
if (e[i] & WORD(1))
fmpz_mul(pow + i, t, pow + (i + 1));
else
fmpz_mul(pow + i, pow + (i + 1), pow + (i + 1));
}
/* Compute reduced units */
{
_fmpz_vec_scalar_mod_fmpz(u + 0 * len, op, len, pow + 0);
}
for (i = 1; i < n; i++)
{
_fmpz_vec_scalar_mod_fmpz(u + i * len, u + (i - 1) * len, len, pow + i);
}
/* Run Newton iteration */
i = n - 1;
{
fmpz *P = _fmpz_vec_init(d + 1);
slong k;
for (k = 0; k < lena; k++)
fmpz_set(P + j[k], a + k);
_fmpz_mod_poly_invmod(rop, u + i * len, len, P, d + 1, pow + i);
_fmpz_vec_clear(P, d + 1);
}
for (i--; i >= 0; i--) /* z' := 2 z - a z^2 */
{
_fmpz_poly_sqr(s, rop, d);
_fmpz_poly_reduce(s, 2 * d - 1, a, j, lena);
_fmpz_poly_mul(t, s, d, u + i * len, len);
_fmpz_poly_reduce(t, d + len - 1, a, j, lena);
_fmpz_vec_scalar_mul_2exp(rop, rop, d, 1);
_fmpz_poly_sub(rop, rop, d, t, d);
_fmpz_vec_scalar_mod_fmpz(rop, rop, d, pow + i);
}
_fmpz_vec_clear(pow, n);
_fmpz_vec_clear(u, len * n);
_fmpz_vec_clear(s, 2 * d - 1);
_fmpz_vec_clear(t, 2 * d - 1);
flint_free(e);
}
}
void qadic_inv(qadic_t x, const qadic_t y, const qadic_ctx_t ctx)
{
const slong N = qadic_prec(x);
if (qadic_is_zero(y))
{
flint_printf("Exception (qadic_inv). Zero is not invertible.\n");
abort();
}
/*
If y = u p^v has negative valuation with N <= -v then the
exact inverse of y is zero when reduced modulo $p^N$
*/
if (N + y->val <= 0)
{
qadic_zero(x);
}
else
{
const slong d = qadic_ctx_degree(ctx);
fmpz *t;
if (x == y)
{
t = _fmpz_vec_init(d);
}
else
{
padic_poly_fit_length(x, d);
t = x->coeffs;
}
_qadic_inv(t, y->coeffs, y->length,
ctx->a, ctx->j, ctx->len, (&ctx->pctx)->p, N + y->val);
x->val = - y->val;
if (x == y)
{
_fmpz_vec_clear(x->coeffs, x->alloc);
x->coeffs = t;
x->alloc = d;
x->length = d;
}
else
{
_padic_poly_set_length(x, d);
}
_padic_poly_normalise(x);
}
}
|