1 /*
2     Copyright (C) 2011 Fredrik Johansson
3 
4     This file is part of FLINT.
5 
6     FLINT is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
10 */
11 
12 #include <stdlib.h>
13 #include "flint.h"
14 #include "fmpz_poly.h"
15 #include "fmpz_poly_mat.h"
16 #include "fmpz_mat.h"
17 
18 void
fmpz_poly_mat_sqr_KS(fmpz_poly_mat_t B,const fmpz_poly_mat_t A)19 fmpz_poly_mat_sqr_KS(fmpz_poly_mat_t B, const fmpz_poly_mat_t A)
20 {
21     fmpz_mat_t AA, BB;
22     slong i, j, n;
23     slong A_len;
24     int signs;
25     slong A_bits, bit_size;
26 
27     n = A->r;
28 
29     if (n == 0)
30     {
31         fmpz_poly_mat_zero(B);
32         return;
33     }
34 
35     A_len = fmpz_poly_mat_max_length(A);
36     A_bits = fmpz_poly_mat_max_bits(A);
37 
38     signs = A_bits < 0;
39 
40     bit_size = 2 * FLINT_ABS(A_bits) + signs;
41     bit_size += FLINT_BIT_COUNT(A_len);
42     bit_size += FLINT_BIT_COUNT(n);
43 
44     fmpz_mat_init(AA, n, n);
45     fmpz_mat_init(BB, n, n);
46 
47     for (i = 0; i < n; i++)
48         for (j = 0; j < n; j++)
49             fmpz_poly_bit_pack(fmpz_mat_entry(AA, i, j),
50                                fmpz_poly_mat_entry(A, i, j), bit_size);
51 
52     /* Should use fmpz_mat_sqr */
53     fmpz_mat_mul(BB, AA, AA);
54 
55     for (i = 0; i < n; i++)
56         for (j = 0; j < n; j++)
57             if (signs)
58                 fmpz_poly_bit_unpack(fmpz_poly_mat_entry(B, i, j),
59                     fmpz_mat_entry(BB, i, j), bit_size);
60             else
61                 fmpz_poly_bit_unpack_unsigned(fmpz_poly_mat_entry(B, i, j),
62                     fmpz_mat_entry(BB, i, j), bit_size);
63 
64     fmpz_mat_clear(AA);
65     fmpz_mat_clear(BB);
66 }
67