-
Notifications
You must be signed in to change notification settings - Fork 0
/
F_mpzmod_mat.c
184 lines (147 loc) · 4.71 KB
/
F_mpzmod_mat.c
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
/*============================================================================
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
===============================================================================*/
/*****************************************************************************
zmod_mat.c: Matrices over F_mpz's mod p, for p a multiprecision
prime (FLINT 2.0).
Only used for test code at this point - not optimised.
Copyright (C) 2008, William Hart.
*****************************************************************************/
#include "F_mpzmod_mat.h"
#include "flint.h"
#include "F_mpz.h"
/****************************************************************************
Initialisation and memory management
****************************************************************************/
void F_mpzmod_mat_init(F_mpzmod_mat_t mat, F_mpz_t p, ulong rows, ulong cols)
{
mat->entries = (F_mpz *) flint_heap_alloc(rows*cols);
mat->rows = (F_mpz **) flint_heap_alloc(rows);
// Set up the rows
ulong i;
for (i = 0; i < rows; i++)
mat->rows[i] = mat->entries + i*cols;
ulong i;
for (i = 0; i < rows*cols; i++)
F_mpz_init(mat->entries + i);
F_mpz_set(mat->p, p);
mat->r = rows;
mat->c = cols;
}
void F_mpzmod_mat_clear(F_mpzmod_mat_t mat)
{
ulong i;
for (i = 0; i < mat->r*mat->c; i++)
F_mpz_clear(mat->entries + i);
flint_heap_free(mat->rows);
flint_heap_free(mat->entries);
}
/*******************************************************************************************
Arithmetic
*******************************************************************************************/
void F_mpzmod_mat_add(F_mpzmod_mat_t res, F_mpzmod_mat_t mat1, F_mpzmod_mat_t mat2)
{
ulong i;
for (i = 0; i < mat1->r; i++)
{
ulong j;
for (j = 0; j < mat1->c; j++)
{
F_mpz_add(res->rows[i] + j, mat1->rows[i] + j, mat2->rows[i] + j);
if (F_mpz_cmpabs(res->rows[i] + j, mat1->p) >= 0)
F_mpz_sub(res->rows[i] + j, res->rows[i] + j, mat1->p);
}
}
}
void F_mpzmod_mat_sub(F_mpzmod_mat_t res, F_mpzmod_mat_t mat1, F_mpzmod_mat_t mat2)
{
ulong i;
for (i = 0; i < mat1->r; i++)
{
ulong j;
for (j = 0; j < mat1->c; j++)
{
F_mpz_sub(res->rows[i] + j, mat1->rows[i] + j, mat2->rows[i] + j);
if (F_mpz_sgn(res->rows[i] + j) < 0)
F_mpz_add(res->rows[i] + j, res->rows[i] + j, mat1->p);
}
}
}
void F_mpzmod_mat_neg(F_mpzmod_mat_t res, F_mpzmod_mat_t mat1)
{
ulong i;
for (i = 0; i < mat1->r; i++)
{
ulong j;
for (j = 0; j < mat1->c; j++)
{
if (F_mpz_sgn(mat1->rows[i] + j))
F_mpz_sub(res->rows[i] + j, mat1->p, mat1->rows[i] + j);
else F_mpz_set_ui(res->rows[i] + j, 0L);
}
}
}
void F_mpzmod_mat_set(F_mpzmod_mat_t res, F_mpzmod_mat_t mat1)
{
ulong i;
for (i = 0; i < mat1->r; i++)
{
ulong j;
for (j = 0; j < mat1->c; j++)
F_mpz_set(res->rows[i] + j, mat1->rows[i] + j);
}
}
void F_mpzmod_mat_mul_classical(F_mpzmod_mat_t res, F_mpzmod_mat_t mat1, F_mpzmod_mat_t mat2)
{
ulong c1 = mat1->c;
ulong r2 = mat2->r;
if ((c1 != r2) || (c1 == 0))
{
printf("FLINT exception : invalid matrix multiplication!\n");
abort();
}
ulong r1 = mat1->r;
ulong c2 = mat2->c;
if ((r1 == 0) || (c2 == 0)) return; // no work to do
F_mpz * temp = (F_mpz *) flint_heap_alloc(c2);
ulong i;
for (i = 0; i < c2; i++)
F_mpz_init(temp + i);
ulong i;
for (i = 0; i < r1; i++) // for each row of mat1
{
F_mpz * c = mat1->rows[i];
ulong k;
for (k = 0; k < c2; k++) // do initial scalar product of row 1 of mat2 by c
F_mpz_mul2(temp + k, mat2->rows[0] + k, c);
ulong j;
for (j = 1; j < c1; j++) // compute scalar product for rows 1 to c1 of mat2
{
ulong k;
for (k = 0; k < c2; k++) // do scalar product of row j of mat2 by c
{
F_mpz_addmul(temp + k, mat2->rows[j] + k, c + j);
}
}
ulong k;
for (k = 0; k < c2; k++) // do reduction mod p and store in result
{
F_mpz_mod(res->rows[i] + k, temp + k, mat1->p);
}
}
ulong i;
for (i = 0; i < c2; i++)
F_mpz_clear(temp + i);
flint_heap_free(temp);
}