File: norm.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 (212 lines) | stat: -rw-r--r-- 5,415 bytes parent folder | download | duplicates (5)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*=============================================================================

    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
    Copyright (C) 2013 Mike Hansen
 
******************************************************************************/

#include "fq.h"

/*
    Computes the characteristic polynomial of the $n \times n$ matrix $M$ 
    modulo \code{pN} using a division-free algorithm in $O(n^4)$ ring 
    operations.

    Only returns the determinant.

    Assumes that $n$ is at least $2$.
 */

static void
_fmpz_mod_mat_det(fmpz_t rop, const fmpz * M, slong n, const fmpz_t pN)
{
    if (n == 1)
    {
        fmpz_set(rop, M);
    }
    else
    {
        fmpz *F;
        fmpz *a;
        fmpz *A;
        fmpz_t s;
        slong t, i, j, p, k;

        F = _fmpz_vec_init(n);
        a = _fmpz_vec_init((n - 1) * n);
        A = _fmpz_vec_init(n);

        fmpz_init(s);

        fmpz_neg(F + 0, M + 0 * n + 0);

        for (t = 1; t < n; t++)
        {
            for (i = 0; i <= t; i++)
                fmpz_set(a + 0 * n + i, M + i * n + t);

            fmpz_set(A + 0, M + t * n + t);

            for (p = 1; p < t; p++)
            {
                for (i = 0; i <= t; i++)
                {
                    fmpz_zero(s);
                    for (j = 0; j <= t; j++)
                        fmpz_addmul(s, M + i * n + j, a + (p - 1) * n + j);
                    fmpz_mod(a + p * n + i, s, pN);
                }

                fmpz_set(A + p, a + p * n + t);
            }

            fmpz_zero(s);
            for (j = 0; j <= t; j++)
                fmpz_addmul(s, M + t * n + j, a + (t - 1) * n + j);
            fmpz_mod(A + t, s, pN);

            for (p = 0; p <= t; p++)
            {
                fmpz_sub(F + p, F + p, A + p);
                for (k = 0; k < p; k++)
                    fmpz_submul(F + p, A + k, F + (p - k - 1));
                fmpz_mod(F + p, F + p, pN);
            }
        }

        /*
           Now [F{n-1}, F{n-2}, ..., F{0}, 1] is the 
           characteristic polynomial of the matrix M.
         */

        if (n % WORD(2) == 0)
        {
            fmpz_set(rop, F + (n - 1));
        }
        else
        {
            fmpz_neg(rop, F + (n - 1));
            fmpz_mod(rop, rop, pN);
        }

        _fmpz_vec_clear(F, n);
        _fmpz_vec_clear(a, (n - 1) * n);
        _fmpz_vec_clear(A, n);
        fmpz_clear(s);
    }
}

/*
    Computes the norm on $\mathbf{Q}_q$ to precision $N \geq 1$. 
    When $N = 1$, this computes the norm on $\mathbf{F}_q$.
 */

void
_fq_norm(fmpz_t rop, const fmpz * op, slong len, const fq_ctx_t ctx)
{
    const slong d = fq_ctx_degree(ctx);
    const slong N = 1;

    fmpz *pN;
    const fmpz *p = fq_ctx_prime(ctx);

    if (N == 1)
    {
        pN = (fmpz *) p;        /* XXX:  Read-only */
    }
    else
    {
        pN = flint_malloc(sizeof(fmpz));
        fmpz_init(pN);
        fmpz_pow_ui(pN, p, N);
    }

    if (len == 1)
    {
        fmpz_powm_ui(rop, op + 0, d, pN);
    }
    else
    {
        /* Construct an ad hoc matrix M and set rop to det(M) */
        {
            const slong n = d + len - 1;
            slong i, k;
            fmpz *M;

            M = flint_calloc(n * n, sizeof(fmpz));

            for (k = 0; k < len - 1; k++)
            {
                for (i = 0; i < ctx->len; i++)
                {
                    M[k * n + k + (d - ctx->j[i])] = ctx->a[i];
                }
            }
            for (k = 0; k < d; k++)
            {
                for (i = 0; i < len; i++)
                {
                    M[(len - 1 + k) * n + k + (len - 1 - i)] = op[i];
                }
            }

            _fmpz_mod_mat_det(rop, M, n, pN);

            flint_free(M);
        }

        /*
           XXX:  This part of the code is currently untested as the Conway 
           polynomials used for the extension Fq/Fp are monic.
         */
        if (!fmpz_is_one(ctx->a + (ctx->len - 1)))
        {
            fmpz_t f;

            fmpz_init(f);
            fmpz_powm_ui(f, ctx->a + (ctx->len - 1), len - 1, pN);
            fmpz_invmod(f, f, pN);
            fmpz_mul(rop, f, rop);
            fmpz_mod(rop, rop, pN);
            fmpz_clear(f);
        }
    }

    if (N > 1)
    {
        fmpz_clear(pN);
        flint_free(pN);
    }
}

void
fq_norm(fmpz_t rop, const fq_t op, const fq_ctx_t ctx)
{
    if (fq_is_zero(op, ctx))
    {
        fmpz_zero(rop);
    }
    else
    {
        _fq_norm(rop, op->coeffs, op->length, ctx);
    }
}