Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

GAP 4.8.9 installation with standard packages -- copy to your CoCalc project to get it

563603 views
1
#include"typedef.h"
2
/**************************************************************************\
3
@---------------------------------------------------------------------------
4
@---------------------------------------------------------------------------
5
@ FILE: short.c
6
@---------------------------------------------------------------------------
7
@---------------------------------------------------------------------------
8
@
9
\**************************************************************************/
10
11
#define EPS 0.001
12
13
static int n, max, min, *vec, anzahl, con;
14
static int **grn; /* Gram matrix */
15
static int **ba; /* basis transformation matrix */
16
static double **mo, *ge;
17
static matrix_TYP *SV;
18
static int SV_size, SV_ext;
19
static int break_opt, f_opt, c_opt;
20
21
22
static double scapr(i, j)
23
int i, j;
24
{
25
double r, *moi, *moj;
26
int l;
27
28
r = grn[i][j];
29
moi = mo[i];
30
moj = mo[j];
31
for (l = 0; l <= j-1; ++l)
32
r -= ge[l] * moi[l] * moj[l];
33
if (ge[j] == 0)
34
{
35
printf("Error: found norm 0 vector\n");
36
exit(3);
37
}
38
r = r / ge[j];
39
return r;
40
}
41
42
static double orth(i)
43
int i;
44
{
45
double r, *moi;
46
int l;
47
48
r = grn[i][i];
49
moi = mo[i];
50
for (l = 0; l <= i-1; ++l)
51
r -= ge[l] * moi[l] * moi[l];
52
return r;
53
}
54
55
/*---------------------------------------------------------*\
56
| makes model of the lattice (with same scalarproducts)
57
\*__________________________________________________________*/
58
59
static void modellmachen(a, e)
60
int a, e;
61
{
62
int i, j;
63
64
if (a == 0)
65
{
66
ge[0] = grn[0][0];
67
i = 1;
68
}
69
else
70
i = a;
71
while (i <= e)
72
{
73
for (j = 0; j < i; ++j)
74
mo[i][j] = scapr(i, j);
75
ge[i] = orth(i);
76
++i;
77
}
78
}
79
80
static int iround(r)
81
double r;
82
{
83
int i;
84
85
if (r >= 0)
86
i = (int)(2*r + 1) / 2;
87
else
88
i = -(int)(2*(-r) + 1) / 2;
89
return i;
90
}
91
92
static void red(k, l)
93
int k, l;
94
{
95
double r, *mok, *mol;
96
int i, ir, *bak, *bal, *grnk, *grnl;
97
98
r = mo[k][l];
99
if (2*r > 1.0 || 2*r < -1.0)
100
{
101
ir = iround(r);
102
mok = mo[k];
103
bak = ba[k];
104
grnk = grn[k];
105
mol = mo[l];
106
bal = ba[l];
107
grnl = grn[l];
108
for (i = 0; i < n; ++i)
109
{
110
mok[i] -= ir * mol[i];
111
bak[i] -= ir * bal[i];
112
grnk[i] -= ir * grnl[i];
113
}
114
for (i = 0; i < n; ++i)
115
grn[i][k] -= ir * grn[i][l];
116
}
117
}
118
119
static int interchange(k)
120
int k;
121
{
122
int i, z, *zp;
123
124
zp = ba[k];
125
ba[k] = ba[k-1];
126
ba[k-1] = zp;
127
128
zp = grn[k];
129
grn[k] = grn[k-1];
130
grn[k-1] = zp;
131
132
for (i = 0; i < n; ++i)
133
{
134
z = grn[i][k];
135
grn[i][k] = grn[i][k-1];
136
grn[i][k-1] = z;
137
}
138
if (k > 1)
139
return k-1;
140
else
141
return k;
142
}
143
144
static void reduzieren(k)
145
int k;
146
{
147
int l;
148
149
for (l = k-2; l >= 0; --l)
150
red(k, l);
151
}
152
153
154
/* prints the vector corresponding to vec */
155
/* in the model and its length */
156
157
static void vecschr(m, d)
158
int m;
159
double d;
160
{
161
int i, j, entry;
162
int l;
163
int *v;
164
165
l = iround(d);
166
if(l >= min)
167
{
168
anzahl++;
169
if(f_opt == TRUE)
170
break_opt = TRUE;
171
if(c_opt == TRUE)
172
return;
173
if(SV->rows == SV_size)
174
{
175
SV_size += SV_ext;
176
if((SV->array.SZ = (int **)realloc(SV->array.SZ, SV_size *sizeof(int *))) == 0)
177
exit(2);
178
}
179
if((v = (int *)malloc((n+1) *sizeof(int))) == 0)
180
exit(2);
181
for (i = 0; i < m; ++i)
182
{
183
v[i] = 0;
184
for (j = 0; j < m; ++j)
185
v[i] += vec[j] * ba[j][i];
186
}
187
v[m] = l;
188
SV->array.SZ[SV->rows] = v;
189
SV->rows++;
190
}
191
192
}
193
194
195
/* recursion for finding shortest vectors */
196
197
static void shrt(c, damage)
198
int c;
199
double damage;
200
{
201
double x, gec;
202
int i, j;
203
204
if (c == -1)
205
{
206
for (i = 0; i < n && vec[i] == 0; ++i);
207
if (i == n)
208
con = 1;
209
else
210
{
211
vecschr(n, damage);
212
if(break_opt == TRUE)
213
return;
214
}
215
}
216
else
217
{
218
x = 0.0;
219
for (j = c+1; j < n; ++j)
220
x += vec[j] * mo[j][c];
221
i = -iround(x);
222
gec = ge[c];
223
if (gec*(x+i)*(x+i) + damage < max + EPS)
224
{
225
while ((gec*(x+i)*(x+i) + damage <= max + EPS) ||
226
(x + i <= 0))
227
++i;
228
--i;
229
while ((gec*(x+i)*(x+i) + damage < max + EPS) &&
230
con == 0)
231
{
232
vec[c] = i;
233
if(break_opt == TRUE)
234
return;
235
shrt(c-1, gec*(x+i)*(x+i) + damage);
236
--i;
237
}
238
}
239
}
240
}
241
242
243
244
245
/**************************************************************************\
246
@---------------------------------------------------------------------------
247
@ matrix_TYP *short_vectors(mat, length, lengthmin, find_opt, count_opt, anz)
248
@ matrix_TYP *mat;
249
@ int length, lengthmin, find_opt, count_opt, *anz;
250
@
251
@ The matrix mat must be a symmetric, positiv definite nxn-Matrix.
252
@ The function calculates all x in Z^n ( up to multiplication with -1) with
253
@
254
@ lengthmin <= n(x) <= length
255
@ where
256
@ n(x) := x * mat * x^{tr}.
257
@
258
@ In the pointer 'anz' the number of all these vectors is returned.
259
@ The vectors x are the rows of the returned matrix,
260
@ called 'SV' in the following.
261
@ So 'SV' is an integral matrix with 'anz' rows and 'n' columns.
262
@ For 'SV' n+1 columns are allocated. The last column contains the norm n(x).
263
@ Although n+1 columns are allocated, SV->cols = n.
264
@
265
@ If 'findoption == 1' the Algorithm stops,
266
@ if a vector of desired norm is found.
267
@ In this case the Matrix 'SV' has only one row.
268
@
269
@ If 'count_option == 1', shortest_vectors returns a zero-pointer,
270
@ only the number of found vectors is returned in the pointer 'anz'.
271
@
272
@ Warning: if 'mat' is not positiv definite, the function runs into an
273
@ infinite loop.
274
@
275
@---------------------------------------------------------------------------
276
@
277
\**************************************************************************/
278
matrix_TYP *short_vectors(mat, length, lengthmin, find_opt, count_opt, anz)
279
matrix_TYP *mat;
280
int length, lengthmin, find_opt, count_opt, *anz;
281
{
282
int i, j, ak, bk;
283
double de, cst = 0.75;
284
char c;
285
matrix_TYP *erg;
286
287
extern matrix_TYP *init_mat();
288
289
anzahl = 0;
290
con = 0;
291
n = mat->rows;
292
SV_ext = n * (n+1)/2;
293
break_opt = FALSE;
294
f_opt = find_opt;
295
c_opt = count_opt;
296
297
SV = init_mat(1, n+1, "");
298
free(SV->array.SZ[0]);
299
SV->cols--;
300
SV->rows = 0;
301
if(f_opt == TRUE)
302
SV_ext = 1;
303
else
304
{
305
if((SV->array.SZ = (int **)realloc(SV->array.SZ, SV_ext *sizeof(int *))) == 0)
306
exit(2);
307
SV_size = SV_ext;
308
}
309
if ((grn = (int**)malloc(n * sizeof(int*))) == 0)
310
exit (2);
311
if ((ba = (int**)malloc(n * sizeof(int*))) == 0)
312
exit (2);
313
if ((mo = (double**)malloc(n * sizeof(double*))) == 0)
314
exit (2);
315
if ((ge = (double*)malloc(n * sizeof(double))) == 0)
316
exit (2);
317
if ((vec = (int*)malloc(n * sizeof(int))) == 0)
318
exit (2);
319
for (i = 0; i < n; ++i)
320
{
321
vec[i] = 0;
322
if ((grn[i] = (int*)malloc(n * sizeof(int))) == 0)
323
exit (2);
324
if ((ba[i] = (int*)malloc(n * sizeof(int))) == 0)
325
exit (2);
326
if ((mo[i] = (double*)malloc(n * sizeof(double))) == 0)
327
exit (2);
328
for (j = 0; j < n; ++j)
329
{
330
ba[i][j] = 0;
331
mo[i][j] = 0.0;
332
}
333
ba[i][i] = 1;
334
mo[i][i] = 1.0;
335
336
/* read the Gram matrix (can be square or lower triangular) */
337
for (j = 0; j <= i; ++j)
338
{
339
grn[i][j] = mat->array.SZ[i][j];
340
grn[j][i] = grn[i][j];
341
}
342
}
343
max = length; /* read the maximal length of the vectors */
344
max *= mat->kgv;
345
min = lengthmin;
346
min *= mat->kgv;
347
348
bk = 1;
349
while (bk < n)
350
{
351
ak = bk - 1;
352
modellmachen(ak, bk);
353
red(bk, bk-1);
354
if (ge[bk] < ((cst - mo[bk][bk-1]*mo[bk][bk-1]) * ge[bk-1]))
355
{
356
bk = interchange(bk);
357
modellmachen(bk-1, bk);
358
if (bk > 1)
359
--bk;
360
}
361
else
362
{
363
if (bk > 1)
364
reduzieren(bk);
365
++bk;
366
}
367
}
368
/* handel a special case, inserted 30/4/97 tilman */
369
if (n==1){
370
ge[0] = 1;
371
}
372
373
shrt(n-1, 0.0); /* recursively calculate the vectors up to length max */
374
*anz = anzahl;
375
376
for(i=0;i<n;i++)
377
{
378
free(grn[i]);
379
free(ba[i]);
380
free(mo[i]);
381
}
382
free(grn); free(ba); free(mo); free(ge); free(vec);
383
384
erg = SV;
385
SV = NULL;
386
return(erg);
387
}
388
389