Skip to content

Commit 0e3a5c1

Browse files
committed
Don't hardcode XI_DEGREES and TAU_DEGREES
Generate them with const functions instead.
1 parent 30e41cf commit 0e3a5c1

File tree

2 files changed

+44
-37
lines changed

2 files changed

+44
-37
lines changed

ext/crates/algebra/src/algebra/combinatorics.rs

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,47 @@ use fp::vector::{FpVector, FpVectorT};
44

55
pub const MAX_XI_TAU : usize = fp::prime::MAX_MULTINOMIAL_LEN;
66

7-
// Generated by Mathematica:
8-
// "[\n " <> # <> "\n]" &[
9-
// StringJoin @@
10-
// StringReplace[
11-
// ToString /@
12-
// Riffle[Map[If[# > 2^31, 0, #] &,
13-
// Function[p, Function[k, (p^k - 1)/(p - 1)] /@ Range[10]] /@
14-
// Prime[Range[8]], {2}], ",\n "], {"{" -> "[", "}" -> "]"}]]
15-
static XI_DEGREES : [[i32; 10]; 8] = [
16-
[1, 3, 7, 15, 31, 63, 127, 255, 511, 1023],
17-
[1, 4, 13, 40, 121, 364, 1093, 3280, 9841, 29524],
18-
[1, 6, 31, 156, 781, 3906, 19531, 97656, 488281, 2441406],
19-
[1, 8, 57, 400, 2801, 19608, 137257, 960800, 6725601, 47079208],
20-
[1, 12, 133, 1464, 16105, 177156, 1948717, 21435888, 235794769, 0],
21-
[1, 14, 183, 2380, 30941, 402234, 5229043, 67977560, 883708281, 0],
22-
[1, 18, 307, 5220, 88741, 1508598, 25646167, 435984840, 0, 0],
23-
[1, 20, 381, 7240, 137561, 2613660, 49659541, 943531280, 0, 0]
24-
];
25-
26-
// Generated by Mathematica:
27-
// "[\n " <> # <> "\n]" &[
28-
// StringJoin @@
29-
// StringReplace[
30-
// ToString /@
31-
// Riffle[Map[If[# > 2^31, 0, #] &,
32-
// Function[p, Function[k, 2 p^k - 1] /@ Range[10]] /@ Prime[Range[8]], {2}], ",\n "], {"{" -> "[", "}" -> "]"}]]
33-
static TAU_DEGREES : [[i32; 10]; 8] = [
34-
[1, 3, 7, 15, 31, 63, 127, 255, 511, 1023],
35-
[1, 5, 17, 53, 161, 485, 1457, 4373, 13121, 39365],
36-
[1, 9, 49, 249, 1249, 6249, 31249, 156249, 781249, 3906249],
37-
[1, 13, 97, 685, 4801, 33613, 235297, 1647085, 11529601, 80707213],
38-
[1, 21, 241, 2661, 29281, 322101, 3543121, 38974341, 428717761, 0],
39-
[1, 25, 337, 4393, 57121, 742585, 9653617, 125497033, 1631461441, 0],
40-
[1, 33, 577, 9825, 167041, 2839713, 48275137, 820677345, 0, 0],
41-
[1, 37, 721, 13717, 260641, 4952197, 94091761, 1787743477, 0, 0]
42-
];
7+
macro_rules! const_for {
8+
($i:ident in $a:literal .. $b:ident $contents:block) => {
9+
let mut $i = 0;
10+
while $i < $b {
11+
$contents;
12+
$i += 1;
13+
}
14+
};
15+
}
16+
17+
/// If p is the nth prime, then XI_DEGREES[n][i - 1] is the degree of ξ_i at the prime p divided by
18+
/// q, where q = 2p - 2 if p != 2 and 1 if p = 2.
19+
const XI_DEGREES : [[i32; MAX_XI_TAU]; NUM_PRIMES] = {
20+
let mut res = [[0; 10]; 8];
21+
const_for! { p_idx in 0 .. NUM_PRIMES {
22+
let p = PRIMES[p_idx];
23+
let mut p_to_the_i = p;
24+
const_for! { x in 0 .. MAX_XI_TAU {
25+
res[p_idx][x] = ((p_to_the_i - 1) / (p - 1)) as i32;
26+
// At some point the powers overflow. The values are not going to be useful, so use
27+
// something replacement that is suitable for const evaluation.
28+
p_to_the_i = p_to_the_i.overflowing_mul(p).0;
29+
}}
30+
}}
31+
res
32+
};
33+
34+
/// If p is the nth prime, then TAU_DEGREES[n][i] is the degree of τ_i at the prime p. Its value is
35+
/// nonsense at the prime 2
36+
const TAU_DEGREES : [[i32; MAX_XI_TAU]; NUM_PRIMES] = {
37+
let mut res = [[0; 10]; 8];
38+
const_for! { p_idx in 0 .. NUM_PRIMES {
39+
let p = PRIMES[p_idx];
40+
let mut p_to_the_i: u32 = 1;
41+
const_for! { x in 0 .. MAX_XI_TAU {
42+
res[p_idx][x] = (2_u32.overflowing_mul(p_to_the_i).0 - 1) as i32;
43+
p_to_the_i = p_to_the_i.overflowing_mul(p).0;
44+
}}
45+
}}
46+
res
47+
};
4348

4449
pub fn adem_relation_coefficient(p : ValidPrime, x : u32, y : u32, j : u32, e1 : u32, e2 : u32) -> u32{
4550
let pi32 = *p as i32;
@@ -473,4 +478,4 @@ mod tests {
473478
assert_eq!(result, expected);
474479
}
475480

476-
}
481+
}

ext/crates/fp/src/prime.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,8 @@ mod tests {
472472

473473
}
474474

475+
pub const PRIMES: [u32; NUM_PRIMES] = [2, 3, 5, 7, 11, 13, 17, 19];
476+
475477
pub const PRIME_TO_INDEX_MAP : [usize; MAX_PRIME+1] = [
476478
!1, !1, 0, 1, !1, 2, !1, 3, !1, !1, !1, 4, !1, 5, !1, !1, !1, 6, !1, 7
477479
];

0 commit comments

Comments
 (0)