Skip to content

Commit

Permalink
migrate to XLINX coefficients
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed May 24, 2024
1 parent d79c733 commit 9a72bb9
Show file tree
Hide file tree
Showing 5 changed files with 391 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ai.hypergraph.kaliningraph.tensor.UTMatrix
import ai.hypergraph.kaliningraph.types.*
import com.ionspin.kotlin.bignum.integer.*
import kotlin.jvm.JvmName
import kotlin.random.Random
import kotlin.random.*
import kotlin.time.measureTimedValue


Expand Down Expand Up @@ -145,17 +145,8 @@ class PTree(val root: String = ".ε", val branches: List<Π2A<PTree>> = listOf()
}

fun sampleStrWithoutReplacement(stride: Int = 1, offset: Int = 0): Sequence<String> =
if (BigInteger(Int.MAX_VALUE) < totalTrees) // Uses LFSR to shuffle the sequence online
randomSequenceWithoutRepetition(0..totalTrees.intValue(false))
.mapIndexedNotNull { index, i -> if (index % stride == offset) newDecoder(i.toBigInteger()) else null }
// .map { newDecoder(it.toBigInteger() * stride + offset) }
else sequence {
var i = BigInteger.ZERO
while (i < totalTrees) {
if (i % stride.toBigInteger() == offset.toBigInteger()) yield(newDecoder(i))
i++
}
}
bigLFSRSequence(totalTrees)
.mapIndexedNotNull { index, i -> if (index % stride == offset) newDecoder(i) else null }

fun sampleStrWithPCFG5(pcfgTable: Map<Int, Int>): Sequence<String> =
sequence { while (true) yield(samplePCFG5(pcfgTable)) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,333 @@
package ai.hypergraph.kaliningraph.sampling

object Polynomials {
// Does not pass empirical tests?
val lecuyer = listOf(
1, 3, 7, 11, 13, 19, 25, 37, 59,
47, 61, 55, 41, 67, 97, 91, 109, 103,
115, 131, 193, 137, 145, 143, 241, 157, 185,
167, 229, 171, 213, 191, 253, 203, 211, 239,
247, 285, 369, 299, 425, 301, 361, 333, 357,
351, 501, 355, 397, 391, 451, 463, 487, 529,
545, 539, 865, 557, 721, 563, 817, 601, 617,
607, 1001, 623, 985, 631, 953, 637, 761, 647,
901, 661, 677, 675, 789, 687, 981, 695, 949,
701, 757, 719, 973, 731, 877, 787, 803, 799,
995, 827, 883, 847, 971, 859, 875, 895, 1019,
911, 967, 1033, 1153, 1051, 1729, 1063, 1825, 1069,
1441, 1125, 1329, 1135, 1969, 1163, 1673, 1221, 1305,
1239, 1881, 1255, 1849, 1267, 1657, 1279, 2041, 1293,
1413, 1315, 1573, 1341, 1509, 1347, 1557, 1367, 1877,
1387, 1717, 1423, 1933, 1431, 1869, 1479, 1821, 1527,
1917, 1531, 1789, 1555, 1603, 1591, 1891, 1615, 1939,
1627, 1747, 1663, 2035, 1759, 2011, 1815, 1863, 2053,
2561, 2071, 3713, 2091, 3393, 2093, 2881, 2119, 3617,
2147, 3169, 2149, 2657, 2161, 2273, 2171, 3553, 2189,
2833, 2197, 2705, 2207, 3985, 2217, 2385, 2225, 2257,
2255, 3889, 2279, 3697, 2283, 3441, 2293, 2801, 2317,
2825, 2323, 3209, 2341, 2633, 2345, 2377, 2363, 3529,
2365, 3017, 2373, 2601, 2395, 3497, 2419, 3305, 2421,
2793, 2431, 4073, 2435, 3097, 2447, 3865, 2475, 3417,
2477, 2905, 2489, 2521, 2503, 3641, 2533, 2681, 2551,
3833, 2567, 3589, 2579, 3205, 2581, 2693, 2669, 2917,
2687, 4069, 2717, 2965, 2727, 3669, 2731, 3413, 2739,
3285, 2741, 2773, 2783, 4021, 2799, 3957, 2811, 3573,
2819, 3085, 2867, 3277, 2879, 4045, 2891, 3373, 2911,
4013, 2927, 3949, 2941, 3053, 2951, 3613, 2955, 3357,
2963, 3229, 2991, 3933, 2999, 3805, 3005, 3037, 3035,
3517, 3047, 3709, 3083, 3331, 3103, 3971, 3159, 3747,
3179, 3427, 3187, 3299, 3223, 3731, 3227, 3475, 3251,
3283, 3263, 4051, 3271, 3635, 3319, 3827, 3343, 3851,
3367, 3659, 3399, 3627, 3439, 3947, 3487, 3995, 3515,
3547, 3543, 3771, 3559, 3707, 3623, 3655, 3679, 4007,
3743, 3991, 3791, 3895, 4179, 6465, 4201, 4801, 4219,
7105, 4221, 6081, 4249, 4897, 4305, 4449, 4331, 6881,
4359, 7185, 4383, 7953, 4387, 6289, 4411, 7057, 4431
).mapIndexed { i, it -> i to listOf(it.toLong()) }.toMap()

// http://www.xilinx.com/support/documentation/application_notes/xapp052.pdf
val xlinz = mapOf(
3 to listOf(3, 2),
4 to listOf(4, 3),
5 to listOf(5, 3),
6 to listOf(6, 5),
7 to listOf(7, 6),
8 to listOf(8, 6, 5, 4),
9 to listOf(9, 5),
10 to listOf(10, 7),
11 to listOf(11, 9),
12 to listOf(12, 6, 4, 1),
13 to listOf(13, 4, 3, 1),
14 to listOf(14, 5, 3, 1),
15 to listOf(15, 14),
16 to listOf(16, 15, 13, 4),
17 to listOf(17, 14),
18 to listOf(18, 11),
19 to listOf(19, 6, 2, 1),
20 to listOf(20, 17),
21 to listOf(21, 19),
22 to listOf(22, 21),
23 to listOf(23, 18),
24 to listOf(24, 23, 22, 17),
25 to listOf(25, 22),
26 to listOf(26, 6, 2, 1),
27 to listOf(27, 5, 2, 1),
28 to listOf(28, 25),
29 to listOf(29, 27),
30 to listOf(30, 6, 4, 1),
31 to listOf(31, 28),
32 to listOf(32, 22, 2, 1),
33 to listOf(33, 20),
34 to listOf(34, 27, 2, 1),
35 to listOf(35, 33),
36 to listOf(36, 25),
37 to listOf(37, 5, 4, 3, 2, 1),
38 to listOf(38, 6, 5, 1),
39 to listOf(39, 35),
40 to listOf(40, 38, 21, 19),
41 to listOf(41, 38),
42 to listOf(42, 41, 20, 19),
43 to listOf(43, 42, 38, 37),
44 to listOf(44, 43, 18, 17),
45 to listOf(45, 44, 42, 41),
46 to listOf(46, 45, 26, 25),
47 to listOf(47, 42),
48 to listOf(48, 47, 21, 20),
49 to listOf(49, 40),
50 to listOf(50, 49, 24, 23),
51 to listOf(51, 50, 36, 35),
52 to listOf(52, 49),
53 to listOf(53, 52, 38, 37),
54 to listOf(54, 53, 18, 17),
55 to listOf(55, 31),
56 to listOf(56, 55, 35, 34),
57 to listOf(57, 50),
58 to listOf(58, 39),
59 to listOf(59, 58, 38, 37),
60 to listOf(60, 59),
61 to listOf(61, 60, 46, 45),
62 to listOf(62, 61, 6, 5),
63 to listOf(63, 62),
64 to listOf(64, 63, 61, 60),
65 to listOf(65, 47),
66 to listOf(66, 65, 57, 56),
67 to listOf(67, 66, 58, 57),
68 to listOf(68, 59),
69 to listOf(69, 67, 42, 40),
70 to listOf(70, 69, 55, 54),
71 to listOf(71, 65),
72 to listOf(72, 66, 25, 19),
73 to listOf(73, 48),
74 to listOf(74, 73, 59, 58),
75 to listOf(75, 74, 65, 64),
76 to listOf(76, 75, 41, 40),
77 to listOf(77, 76, 47, 46),
78 to listOf(78, 77, 59, 58),
79 to listOf(79, 70),
80 to listOf(80, 79, 43, 42),
81 to listOf(81, 77),
82 to listOf(82, 79, 47, 44),
83 to listOf(83, 82, 38, 37),
84 to listOf(84, 71),
85 to listOf(85, 84, 58, 57),
86 to listOf(86, 85, 74, 73),
87 to listOf(87, 74),
88 to listOf(88, 87, 17, 16),
89 to listOf(89, 51),
90 to listOf(90, 89, 72, 71),
91 to listOf(91, 90, 8, 7),
92 to listOf(92, 91, 80, 79),
93 to listOf(93, 91),
94 to listOf(94, 73),
95 to listOf(95, 84),
96 to listOf(96, 94, 49, 47),
97 to listOf(97, 91),
98 to listOf(98, 87),
99 to listOf(99, 97, 54, 52),
100 to listOf(100, 63),
101 to listOf(101, 100, 95, 94),
102 to listOf(102, 101, 36, 35),
103 to listOf(103, 94),
104 to listOf(104, 103, 94, 93),
105 to listOf(105, 89),
106 to listOf(106, 91),
107 to listOf(107, 105, 44, 42),
108 to listOf(108, 77),
109 to listOf(109, 108, 103, 102),
110 to listOf(110, 109, 98, 97),
111 to listOf(111, 101),
112 to listOf(112, 110, 69, 67),
113 to listOf(113, 104),
114 to listOf(114, 113, 33, 32),
115 to listOf(115, 114, 101, 100),
116 to listOf(116, 115, 46, 45),
117 to listOf(117, 115, 99, 97),
118 to listOf(118, 85),
119 to listOf(119, 111),
120 to listOf(120, 113, 9, 2),
121 to listOf(121, 103),
122 to listOf(122, 121, 63, 62),
123 to listOf(123, 121),
124 to listOf(124, 87),
125 to listOf(125, 124, 18, 17),
126 to listOf(126, 125, 90, 89),
127 to listOf(127, 126),
128 to listOf(128, 126, 101, 99),
129 to listOf(129, 124),
130 to listOf(130, 127),
131 to listOf(131, 130, 84, 83),
132 to listOf(132, 103),
133 to listOf(133, 132, 82, 81),
134 to listOf(134, 77),
135 to listOf(135, 124),
136 to listOf(136, 135, 11, 10),
137 to listOf(137, 116),
138 to listOf(138, 137, 131, 130),
139 to listOf(139, 136, 134, 131),
140 to listOf(140, 111),
141 to listOf(141, 140, 110, 109),
142 to listOf(142, 121),
143 to listOf(143, 142, 123, 122),
144 to listOf(144, 143, 75, 74),
145 to listOf(145, 93),
146 to listOf(146, 145, 87, 86),
147 to listOf(147, 146, 110, 109),
148 to listOf(148, 121),
149 to listOf(149, 148, 40, 39),
150 to listOf(150, 97),
151 to listOf(151, 148),
152 to listOf(152, 151, 87, 86),
153 to listOf(153, 152),
154 to listOf(154, 152, 27, 25),
155 to listOf(155, 154, 124, 123),
156 to listOf(156, 155, 41, 40),
157 to listOf(157, 156, 131, 130),
158 to listOf(158, 157, 132, 131),
159 to listOf(159, 128),
160 to listOf(160, 159, 142, 141),
161 to listOf(161, 143),
162 to listOf(162, 161, 75, 74),
163 to listOf(163, 162, 104, 103),
164 to listOf(164, 163, 151, 150),
165 to listOf(165, 164, 135, 134),
166 to listOf(166, 165, 128, 127),
167 to listOf(167, 161),
168 to listOf(168, 166, 153, 151)
)

// TODO: Compute minimal elements of GF(p^e) dynamically
// http://www.seanerikoconnor.freeservers.com/Mathematics/AbstractAlgebra/PrimitivePolynomials/theory.html
// https://math.stackexchange.com/questions/2232179/how-to-find-minimal-polynomial-for-an-element-in-mboxgf2m
// http://crc.stanford.edu/crc_papers/CRC-TR-04-03.pdf#page=24
// https://link.springer.com/content/pdf/bbm%3A978-3-642-54649-5%2F1.pdf#page=5
val academic = mapOf(
// Degree to binary polynomial coefficients in decimal form
// https://link.springer.com/content/pdf/bbm%3A978-1-4615-1509-8%2F1.pdf
// https://sci-hub.se/https://doi.org/10.1023/A:1027422805851
// https://github.com/umontreal-simul/ssj/blob/f384e22adf08bd5202ea65bb7cd53fee192cb3ce/src/main/java/umontreal/ssj/hups/SobolSequence.java#L488
0 to listOf(0L),
1 to listOf(1L),
2 to listOf(7L),
3 to listOf(11L),
4 to listOf(19L, 25L),
5 to listOf(37L, 41L, 47L, 55L, 59L, 61L),
6 to listOf(67L, 91L, 97L, 103L, 109L, 115L),
7 to listOf(
131L, 137L, 143L, 145L, 157L, 167L, 171L, 185L, 191L, 193L, 203L, 211L, 213L, 229L,
239L, 241L, 247L, 253
),
8 to listOf(
285L, 299L, 301L, 333L, 351L, 355L, 357L, 361L, 369L, 391L, 397L, 425L, 451L, 463L,
487L, 501
),
9 to listOf(
529L, 539L, 545L, 557L, 563L, 601L, 607L, 617L, 623L, 631L, 637L, 647L, 661L, 675L,
677L, 687L, 695L, 701L, 719L, 721L, 731L, 757L, 761L, 787L, 789L, 799L, 803L, 817L,
827L, 847L, 859L, 865L, 875L, 877L, 883L, 895L, 901L, 911L, 949L, 953L, 967L, 971L,
973L, 981L, 985L, 995L, 1001L, 1019
),
10 to listOf(
1033L, 1051L, 1063L, 1069L, 1125L, 1135L, 1153L, 1163L, 1221L, 1239L, 1255L,
1267L, 1279L, 1293L, 1305L, 1315L, 1329L, 1341L, 1347L, 1367L, 1387L, 1413L,
1423L, 1431L, 1441L, 1479L, 1509L, 1527L, 1531L, 1555L, 1557L, 1573L, 1591L,
1603L, 1615L, 1627L, 1657L, 1663L, 1673L, 1717L, 1729L, 1747L, 1759L, 1789L,
1815L, 1821L, 1825L, 1849L, 1863L, 1869L, 1877L, 1881L, 1891L, 1917L, 1933L,
1939L, 1969L, 2011L, 2035L, 2041L
),
11 to listOf(
2053L, 2071L, 2091L, 2093L, 2119L, 2147L, 2149L, 2161L, 2171L, 2189L, 2197L,
2207L, 2217L, 2225L, 2255L, 2257L, 2273L, 2279L, 2283L, 2293L, 2317L, 2323L,
2341L, 2345L, 2363L, 2365L, 2373L, 2377L, 2385L, 2395L, 2419L, 2421L, 2431L,
2435L, 2447L, 2475L, 2477L, 2489L, 2503L, 2521L, 2533L, 2551L, 2561L, 2567L,
2579L, 2581L, 2601L, 2633L, 2657L, 2669L, 2681L, 2687L, 2693L, 2705L, 2717L,
2727L, 2731L, 2739L, 2741L, 2773L, 2783L, 2793L, 2799L, 2801L, 2811L, 2819L,
2825L, 2833L, 2867L, 2879L, 2881L, 2891L, 2905L, 2911L, 2917L, 2927L, 2941L,
2951L, 2955L, 2963L, 2965L, 2991L, 2999L, 3005L, 3017L, 3035L, 3037L, 3047L,
3053L, 3083L, 3085L, 3097L, 3103L, 3159L, 3169L, 3179L, 3187L, 3205L, 3209L,
3223L, 3227L, 3229L, 3251L, 3263L, 3271L, 3277L, 3283L, 3285L, 3299L, 3305L,
3319L, 3331L, 3343L, 3357L, 3367L, 3373L, 3393L, 3399L, 3413L, 3417L, 3427L,
3439L, 3441L, 3475L, 3487L, 3497L, 3515L, 3517L, 3529L, 3543L, 3547L, 3553L,
3559L, 3573L, 3589L, 3613L, 3617L, 3623L, 3627L, 3635L, 3641L, 3655L, 3659L,
3669L, 3679L, 3697L, 3707L, 3709L, 3713L, 3731L, 3743L, 3747L, 3771L, 3791L,
3805L, 3827L, 3833L, 3851L, 3865L, 3889L, 3895L, 3933L, 3947L, 3949L, 3957L,
3971L, 3985L, 3991L, 3995L, 4007L, 4013L, 4021L, 4045L, 4051L, 4069L, 4073
),
12 to listOf(
4179L, 4201L, 4219L, 4221L, 4249L, 4305L, 4331L, 4359L, 4383L, 4387L, 4411L,
4431L, 4439L, 4449L, 4459L, 4485L, 4531L, 4569L, 4575L, 4621L, 4663L, 4669L,
4711L, 4723L, 4735L, 4793L, 4801L, 4811L, 4879L, 4893L, 4897L, 4921L, 4927L,
4941L, 4977L, 5017L, 5027L, 5033L, 5127L, 5169L, 5175L, 5199L, 5213L, 5223L,
5237L, 5287L, 5293L, 5331L, 5391L, 5405L, 5453L, 5523L, 5573L, 5591L, 5597L,
5611L, 5641L, 5703L, 5717L, 5721L, 5797L, 5821L, 5909L, 5913L, 5955L, 5957L,
6005L, 6025L, 6061L, 6067L, 6079L, 6081L, 6231L, 6237L, 6289L, 6295L, 6329L,
6383L, 6427L, 6453L, 6465L, 6501L, 6523L, 6539L, 6577L, 6589L, 6601L, 6607L,
6631L, 6683L, 6699L, 6707L, 6761L, 6795L, 6865L, 6881L, 6901L, 6923L, 6931L,
6943L, 6999L, 7057L, 7079L, 7103L, 7105L, 7123L, 7173L, 7185L, 7191L, 7207L,
7245L, 7303L, 7327L, 7333L, 7355L, 7365L, 7369L, 7375L, 7411L, 7431L, 7459L,
7491L, 7505L, 7515L, 7541L, 7557L, 7561L, 7701L, 7705L, 7727L, 7749L, 7761L,
7783L, 7795L, 7823L, 7907L, 7953L, 7963L, 7975L, 8049L, 8089L, 8123L, 8125L,
8137
),
13 to listOf(
8219L, 8231L, 8245L, 8275L, 8293L, 8303L, 8331L, 8333L, 8351L, 8357L, 8367L,
8379L, 8381L, 8387L, 8393L, 8417L, 8435L, 8461L, 8469L, 8489L, 8495L, 8507L,
8515L, 8551L, 8555L, 8569L, 8585L, 8599L, 8605L, 8639L
),
14 to listOf(
16427L, 16441L, 16467L, 16479L, 16507L, 16553L, 16559L, 16571L, 16573L,
16591L, 16619L, 16627L, 16653L, 16659L, 16699L, 16707L, 16795L, 16797L,
16807L, 16813L, 16821L, 16853L, 16857L, 16881L
),
15 to listOf(
32771L, 32785L, 32791L, 32813L, 32821L, 32863L, 32887L, 32897L, 32903L,
32915L, 32933L, 32963L, 32975L, 32989L, 32999L, 33013L, 33025L, 33045L,
33061L, 33111L, 33117L, 33121L, 33133L, 33157L
),
16 to listOf(
65581L, 65593L, 65599L, 65619L, 65725L, 65751L, 65839L, 65853L, 65871L,
65885L, 65943L, 65953L, 65965L, 65983L, 65991L, 66069L, 66073L, 66085L,
66095L, 66141L, 66157L, 66181L, 66193L, 66209L
),
17 to listOf(
131081L, 131087L, 131105L, 131117L, 131123L, 131135L, 131137L,
131157L, 131177L, 131195L, 131213L, 131225L, 131235L, 131247L,
131259L, 131269L, 131317L
),
18 to listOf(262183L, 262207L, 262221L, 262267L, 262273L, 262363L, 262375L, 262381L, 262407L),
19 to listOf(524327L, 524351L, 524359L, 524371L, 524377L, 524387L, 524399L, 524413L, 524435L, 524463L),
20 to listOf(1048585L, 1048659L, 1048677L, 1048681L, 1048699L),
21 to listOf(2097157L, 2097191L, 2097215L, 2097253L),
22 to listOf(4194307L, 4194361L),
23 to listOf(8388641L, 8388651L, 8388653L, 8388659L, 8388671L, 8388685L, 8388709L),
24 to listOf(16777243L),
25 to listOf(33554441L),
26 to listOf(67108935L),
// https://oeis.org/A132453
27 to listOf(134217767L),
28 to listOf(268435465L),
29 to listOf(536870917L),
30 to listOf(1073741907L),
31 to listOf(2147483657L),
32 to listOf(4294967493L),
) + (33..64).associateWith { lecuyer[it]!! }
}
Loading

0 comments on commit 9a72bb9

Please sign in to comment.