-
Notifications
You must be signed in to change notification settings - Fork 2
/
read-localfields.mag
130 lines (120 loc) · 3.41 KB
/
read-localfields.mag
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
function replace_all(x, pat, repl)
while true do
i := Index(x, pat);
if i eq 0 then
break;
else
x := x[1..i-1] cat repl cat x[i+#pat..#x];
end if;
end while;
return x;
end function;
function template(tmpl, fields)
for x in fields do
tmpl := replace_all(tmpl, "{" cat x[1] cat "}", x[2]);
end for;
return tmpl;
end function;
function read_lines(filename)
F := Open(filename, "r");
ret := [];
while true do
line := Gets(F);
if IsEof(line) then
return ret;
else
Append(~ret, line);
end if;
end while;
end function;
function histogram(xs : bin_size:=1)
counts := AssociativeArray();
for x in xs do
bin := Floor(x / bin_size);
if IsDefined(counts, bin) then
counts[bin] +:= 1;
else
counts[bin] := 1;
end if;
end for;
return [<bin*bin_size, counts[bin]> : bin in Sort(SetToSequence(Keys(counts)))];
end function;
function local_fields_lookup_table(:prime:=false, degree:=false)
fields := LocalFields(:PrimeIs:=prime, DegreeIs:=degree);
A := AssociativeArray();
for x in fields do
A[<x`p, x`poly>] := x;
end for;
return A;
end function;
procedure show_histogram(xs : bin_size:=1, scale:=1, bin_fmt:="%6.2o", count_fmt:="%6o")
for row in histogram(xs : bin_size:=bin_size) do
printf "%o %o %o\n", Sprintf(bin_fmt, row[1]), Sprintf(count_fmt, row[2]), &cat[Strings() | "=" : i in [1..Round(row[2] * scale)]];
end for;
end procedure;
function mean(xs)
return &+xs / #xs;
end function;
function median(xs)
xs := Sort([x : x in xs]);
return xs[#xs div 2 + 1];
end function;
function extended_data(xs, p, d)
lookup := local_fields_lookup_table(:prime:=p, degree:=d);
FMT := recformat<poly, prec, success, err, cputime, G, t, correct_G, correct_t, is_correct, field>;
rs := [];
for x in xs do
r := rec<FMT | poly:=x[1], prec:=x[2], success:=x[3]>;
assert Degree(r`poly) eq d;
if r`success then
r`cputime := x[4];
r`G := x[5];
r`t := TransitiveGroupIdentification(r`G);
else
r`err := x[4];
end if;
r`field := lookup[<p, x[1]>];
ok, _, cs := Regexp("^t([0-9]+)n([0-9]+)$", r`field`Gname);
if ok then
assert StringToInteger(cs[1]) eq d;
r`correct_t := StringToInteger(cs[2]);
r`correct_G := TransitiveGroup(d, r`correct_t);
end if;
if assigned r`correct_t and assigned r`t then
r`is_correct := r`correct_t eq r`t;
end if;
Append(~rs, r);
end for;
return rs;
end function;
function read_data(
: prime := 2
, degree := 8
, algorithm := "ALG3"
, jump := 1
, starts := [1..jump]
, ofile_template := "test-localfields_p{prime}_d{degree}_{algorithm}_{start}of{jump}.out"
, ofiles := [template(ofile_template, [
<"prime", IntegerToString(prime)>,
<"degree", IntegerToString(degree)>,
<"algorithm", algorithm>,
<"jump", IntegerToString(jump)>,
<"start", IntegerToString(start)>
]) : start in starts]
, only_last := true
, extended := true
)
data := [eval line : line in read_lines(ofile), ofile in ofiles];
if only_last then
A := AssociativeArray();
for x in data do
A[x[1]] := x;
end for;
data := [A[k] : k in Keys(A)];
end if;
if extended then
return extended_data(data, prime, degree);
else
return data;
end if;
end function;