File: evaluate_padic.c

package info (click to toggle)
flint 2.5.2-19
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 30,308 kB
  • sloc: ansic: 289,367; cpp: 11,210; python: 1,280; sh: 649; makefile: 283
file content (188 lines) | stat: -rw-r--r-- 5,426 bytes parent folder | download | duplicates (4)
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
/*============================================================================

    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) 2012 Sebastian Pancratz
 
******************************************************************************/

#include "fmpz_mod_poly.h"
#include "padic_poly.h"

/*
    TODO:  Move this bit of code into "padic".
 */
static void __padic_reduce(fmpz_t u, slong *v, slong N, const padic_ctx_t ctx)
{
    if (!fmpz_is_zero(u))
    {
        if (*v < N)
        {
            int alloc;
            fmpz_t pow;

            alloc = _padic_ctx_pow_ui(pow, N - *v, ctx);
            fmpz_mod(u, u, pow);
            if (alloc)
                fmpz_clear(pow);
        }
        else
        {
            fmpz_zero(u);
            *v = 0;
        }
    }
}

/*
    Evaluates the polynomial $F(x) = p^w f(x)$ at $x = p^b a$, setting 
    $y = p^v u$ to the result reduced modulo $p^N$.

    Suppose first that $b \geq 0$, in which case we can quickly relay 
    the call to the \code{fmpz_mod_poly} module.  Namely, we need to 
    compute $f(x) \bmod {p^{N-w}}$ where we know $f(x)$ to be integral.

    Otherwise, suppose now that $b < 0$ and we still wish to evaluate 
    $f(x) \bmod {p^{N-w}}$.
    \begin{align*}
    f(x) & = \sum_{i = 0}^{n} a_i x^i \\
         & = \sum_{i = 0}^{n} a_i p^{i b} a^i \\
    \intertext{Multiplying through by $p^{- n b} \in \mathbf{Z}$, }
    p^{-nb} f(x) & = \sum_{i = 0}^{n} a_i p^{-(n-i)b} a
    \end{align*}
    which leaves the right hand side integral.  As we want to 
    compute $f(x)$ to precision $N-w$, we have to compute 
    $p^{-nb} f(x)$ to precision $N-w-nb$.
 */

void _padic_poly_evaluate_padic(fmpz_t u, slong *v, slong N,  
                                const fmpz *poly, slong val, slong len, 
                                const fmpz_t a, slong b, const padic_ctx_t ctx)
{
    if (len == 0)
    {
        fmpz_zero(u);
        *v = 0;
    }
    else if (len == 1)
    {
        fmpz_set(u, poly);
        *v = val;

        __padic_reduce(u, v, N, ctx);
    }
    else if (b >= 0)
    {
        if (val >= N)
        {
            fmpz_zero(u);
            *v = 0;
        }
        else 
        {
            fmpz_t x;
            fmpz_t pow;
            int alloc;

            fmpz_init(x);
            alloc = _padic_ctx_pow_ui(pow, N - val, ctx);

            fmpz_pow_ui(x, ctx->p, b);
            fmpz_mul(x, x, a);

            _fmpz_mod_poly_evaluate_fmpz(u, poly, len, x, pow);
            if (!fmpz_is_zero(u))
                *v = val + _fmpz_remove(u, ctx->p, ctx->pinv);
            else
                *v = 0;

            fmpz_clear(x);
            if (alloc)
                fmpz_clear(pow);
        }
    }
    else  /* b < 0 */
    {
        const slong n = len - 1;

        if (val + n*b >= N)
        {
            fmpz_zero(u);
            *v = 0;
        }
        else
        {
            fmpz_t pow;
            int alloc;
            slong i;
            fmpz_t s, t;

            fmpz *vec = _fmpz_vec_init(len);
            fmpz_init(s);
            fmpz_init(t);

            alloc = _padic_ctx_pow_ui(pow, N - val - n*b, ctx);

            fmpz_pow_ui(s, ctx->p, -b);
            fmpz_one(t);
            fmpz_set(vec + (len - 1), poly + (len - 1));
            for (i = len - 2; i >= 0; i--)
            {
                fmpz_mul(t, t, s);
                fmpz_mul(vec + i, poly + i, t);
            }

            _fmpz_mod_poly_evaluate_fmpz(u, vec, len, a, pow);
            if (!fmpz_is_zero(u))
                *v = val + n*b + _fmpz_remove(u, ctx->p, ctx->pinv);
            else
                *v = 0;

            if (alloc)
                fmpz_clear(pow);
            fmpz_clear(s);
            fmpz_clear(t);
            _fmpz_vec_clear(vec, len);
        }
    }
}

void padic_poly_evaluate_padic(padic_t y, const padic_poly_t poly, 
                                          const padic_t x, const padic_ctx_t ctx)
{
    if (y == x)
    {
        padic_t t;

        padic_init2(t, padic_prec(y));
        _padic_poly_evaluate_padic(padic_unit(t), &padic_val(t), padic_prec(t), 
                                   poly->coeffs, poly->val, poly->length, 
                                   padic_unit(x), padic_val(x), ctx);
        padic_swap(y, t);
        padic_clear(t);
    }
    else
    {
        _padic_poly_evaluate_padic(padic_unit(y), &padic_val(y), padic_prec(y), 
                                   poly->coeffs, poly->val, poly->length, 
                                   padic_unit(x), padic_val(x), ctx);
    }
}