Skip to content

Commit 5ccd08f

Browse files
fingolfincodex
andauthored
Fix ShortestVectors to return complete list (#6253)
Also simplify the code a bit. Co-authored-by: Codex <codex@openai.com>
1 parent e9ea6a4 commit 5ccd08f

2 files changed

Lines changed: 32 additions & 14 deletions

File tree

lib/zlattice.gi

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ end );
12441244
InstallGlobalFunction( ShortestVectors, function( arg )
12451245
local
12461246
# variables
1247-
n, checkpositiv, a, llg, nullv, m, c, anz, con, b, v,
1247+
n, checkpositiv, a, llg, nullv, m, c, con, b, v,
12481248
# procedures
12491249
srt, vschr;
12501250

@@ -1255,7 +1255,6 @@ InstallGlobalFunction( ShortestVectors, function( arg )
12551255
if v = nullv then
12561256
con := false;
12571257
else
1258-
anz := anz + 1;
12591258
vschr( dam );
12601259
fi;
12611260
else
@@ -1289,26 +1288,31 @@ InstallGlobalFunction( ShortestVectors, function( arg )
12891288

12901289
# output of vector
12911290
vschr := function( dam )
1292-
local i, j, w, neg;
1293-
c.vectors[anz] := [];
1294-
neg := false;
1291+
local newvec, i, j, w, haspos, hasneg;
1292+
newvec := [];
1293+
haspos := false;
1294+
hasneg := false;
12951295
for i in [1..n] do
12961296
w := 0;
12971297
for j in [1..n] do
12981298
w := w + v[j] * llg.transformation[j][i];
12991299
od;
13001300
if w < 0 then
1301-
neg := true;
1302-
#T better here check testpositiv and return!
1301+
hasneg := true;
1302+
elif w > 0 then
1303+
haspos := true;
13031304
fi;
1304-
c.vectors[anz][i] := w;
1305+
newvec[i] := w;
13051306
od;
1306-
if checkpositiv and neg then
1307-
Unbind(c.vectors[anz]);
1308-
anz := anz - 1;
1309-
else
1310-
c.norms[anz] := dam;
1307+
if checkpositiv then
1308+
if haspos and hasneg then
1309+
return;
1310+
elif hasneg then
1311+
newvec := -newvec;
1312+
fi;
13111313
fi;
1314+
Add(c.vectors, newvec);
1315+
Add(c.norms, dam);
13121316
end;
13131317

13141318
# main program
@@ -1347,7 +1351,6 @@ InstallGlobalFunction( ShortestVectors, function( arg )
13471351
#T here check that the matrix is really regular
13481352
#T (empty relations component)
13491353

1350-
anz := 0;
13511354
con := true;
13521355
srt( n, 0 );
13531356

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Fix: ShortestVectors(..., "positive") should also accept vectors
2+
# that are nonpositive by replacing them with their negation.
3+
4+
gap> E8 :=
5+
> [ [ 2, -1, 0, 0, 0, 0, 0, 0 ], [ -1, 2, -1, 0, 0, 0, 0, 0 ],
6+
> [ 0, -1, 2, -1, 0, 0, 0, 0 ], [ 0, 0, -1, 2, -1, 0, 0, 0 ],
7+
> [ 0, 0, 0, -1, 2, -1, 0, -1 ], [ 0, 0, 0, 0, -1, 2, -1, 0 ],
8+
> [ 0, 0, 0, 0, 0, -1, 2, 0 ], [ 0, 0, 0, 0, -1, 0, 0, 2 ] ];;
9+
gap> Length( ShortestVectors( E8, 2 ).vectors );
10+
120
11+
gap> sv := ShortestVectors( E8, 2, "positive" );;
12+
gap> Length( sv.vectors );
13+
120
14+
gap> ForAll( sv.vectors, x -> ForAll( x, y -> y >= 0 ) );
15+
true

0 commit comments

Comments
 (0)