File: mul_KS.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 (100 lines) | stat: -rw-r--r-- 3,001 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
/*=============================================================================

    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 Fredrik Johansson
    Copyright (C) 2013 Mike Hansen

******************************************************************************/


#ifdef T

#include "templates.h"
#include "fmpz_mat.h"

void
TEMPLATE(T, mat_mul_KS) (TEMPLATE(T, mat_t) C,
                         const TEMPLATE(T, mat_t) A,
                         const TEMPLATE(T, mat_t) B,
                         const TEMPLATE(T, ctx_t) ctx)
{
    slong bits;
    slong ar, bc, br;
    slong i, j;
    fmpz_mat_t fa, fb, fc;
    fmpz_t beta;

    ar = A->r;
    br = B->r;
    bc = B->c;

    if (br == 0)
    {
        TEMPLATE(T, mat_zero) (C, ctx);
        return;
    }

    /* Compute the number of bits needed */

    /* TODO: Make this generic based on say an TEMPLATE(T,
     * bits_needed) and TEMPLATE(T, bit_pack) */
    fmpz_init(beta);
    fmpz_set(beta, TEMPLATE(T, ctx_prime) (ctx));
    fmpz_sub_ui(beta, beta, 1);
    fmpz_mul(beta, beta, beta);
    fmpz_mul_si(beta, beta, A->r);
    fmpz_mul_si(beta, beta, TEMPLATE(T, ctx_degree) (ctx));
    bits = fmpz_bits(beta) + 1;

    fmpz_mat_init(fa, A->r, A->c);
    fmpz_mat_init(fb, B->r, B->c);
    fmpz_mat_init(fc, A->r, B->c);

    for (i = 0; i < A->r; i++)
        for (j = 0; j < A->c; j++)
            TEMPLATE(T, bit_pack) (fmpz_mat_entry(fa, i, j),
                                   TEMPLATE(T, mat_entry) (A, i, j), bits,
                                   ctx);

    for (i = 0; i < B->r; i++)
        for (j = 0; j < B->c; j++)
            TEMPLATE(T, bit_pack) (fmpz_mat_entry(fb, i, j),
                                   TEMPLATE(T, mat_entry) (B, i, j), bits,
                                   ctx);

    fmpz_mat_mul(fc, fa, fb);

    for (i = 0; i < ar; i++)
    {
        for (j = 0; j < bc; j++)
        {
            TEMPLATE(T, bit_unpack) (TEMPLATE(T, mat_entry) (C, i, j),
                                     fmpz_mat_entry(fc, i, j), bits, ctx);
        }
    }

    fmpz_mat_clear(fa);
    fmpz_mat_clear(fb);
    fmpz_mat_clear(fc);
}


#endif