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_mul_KS(fmpz_poly_mat_t C,const fmpz_poly_mat_t A,const fmpz_poly_mat_t B)19 fmpz_poly_mat_mul_KS(fmpz_poly_mat_t C, const fmpz_poly_mat_t A,
20     const fmpz_poly_mat_t B)
21 {
22     slong i, j;
23     slong A_len, B_len;
24     int signs;
25     slong A_bits, B_bits, bit_size;
26 
27     fmpz_mat_t AA, BB, CC;
28 
29     if (B->r == 0)
30     {
31         fmpz_poly_mat_zero(C);
32         return;
33     }
34 
35     A_len = fmpz_poly_mat_max_length(A);
36     B_len = fmpz_poly_mat_max_length(B);
37 
38     A_bits = fmpz_poly_mat_max_bits(A);
39     B_bits = fmpz_poly_mat_max_bits(B);
40 
41     signs = (A_bits < 0 || B_bits < 0);
42 
43     bit_size = FLINT_ABS(A_bits) + FLINT_ABS(B_bits) + signs;
44     bit_size += FLINT_BIT_COUNT(FLINT_MIN(A_len, B_len));
45     bit_size += FLINT_BIT_COUNT(B->r);
46 
47 /*
48     flint_printf("A: BITS %wd LEN %wd\n", A_bits, A_len);
49     flint_printf("B: BITS %wd LEN %wd\n", B_bits, B_len);
50     flint_printf("bit_size: %wd\n", bit_size);
51 */
52 
53     fmpz_mat_init(AA, A->r, A->c);
54     fmpz_mat_init(BB, B->r, B->c);
55     fmpz_mat_init(CC, C->r, C->c);
56 
57     for (i = 0; i < A->r; i++)
58         for (j = 0; j < A->c; j++)
59             fmpz_poly_bit_pack(fmpz_mat_entry(AA, i, j),
60                                fmpz_poly_mat_entry(A, i, j), bit_size);
61 
62     for (i = 0; i < B->r; i++)
63         for (j = 0; j < B->c; j++)
64             fmpz_poly_bit_pack(fmpz_mat_entry(BB, i, j),
65                                fmpz_poly_mat_entry(B, i, j), bit_size);
66 
67     fmpz_mat_mul(CC, AA, BB);
68 
69     for (i = 0; i < C->r; i++)
70         for (j = 0; j < C->c; j++)
71             if (signs)
72                 fmpz_poly_bit_unpack(fmpz_poly_mat_entry(C, i, j),
73                     fmpz_mat_entry(CC, i, j), bit_size);
74             else
75                 fmpz_poly_bit_unpack_unsigned(fmpz_poly_mat_entry(C, i, j),
76                     fmpz_mat_entry(CC, i, j), bit_size);
77 
78     fmpz_mat_clear(AA);
79     fmpz_mat_clear(BB);
80     fmpz_mat_clear(CC);
81 }
82