hypercomplex-engine 0.6.0.1__py3-none-any.whl → 0.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hypercomplex_engine/__init__.py +12 -2
- hypercomplex_engine/core/__init__.py +13 -0
- hypercomplex_engine/core/bitplane/__init__.py +5 -0
- hypercomplex_engine/core/bitplane/bitplane_standard.py +162 -0
- hypercomplex_engine/core/graded/__init__.py +5 -0
- hypercomplex_engine/core/graded/graded_sparse_standard.py +125 -0
- hypercomplex_engine-0.7.dist-info/METADATA +1220 -0
- {hypercomplex_engine-0.6.0.1.dist-info → hypercomplex_engine-0.7.dist-info}/RECORD +12 -8
- hypercomplex_engine-0.6.0.1.dist-info/METADATA +0 -28
- {hypercomplex_engine-0.6.0.1.dist-info → hypercomplex_engine-0.7.dist-info}/WHEEL +0 -0
- {hypercomplex_engine-0.6.0.1.dist-info → hypercomplex_engine-0.7.dist-info}/licenses/LICENSE +0 -0
- {hypercomplex_engine-0.6.0.1.dist-info → hypercomplex_engine-0.7.dist-info}/licenses/NOTICE +0 -0
- {hypercomplex_engine-0.6.0.1.dist-info → hypercomplex_engine-0.7.dist-info}/top_level.txt +0 -0
hypercomplex_engine/__init__.py
CHANGED
|
@@ -30,7 +30,13 @@ from .core import (
|
|
|
30
30
|
FastSplit,
|
|
31
31
|
FastDual,
|
|
32
32
|
|
|
33
|
-
VectorizedStandard
|
|
33
|
+
VectorizedStandard,
|
|
34
|
+
|
|
35
|
+
BitPlaneStandard,
|
|
36
|
+
|
|
37
|
+
GradedSparseStandard,
|
|
38
|
+
|
|
39
|
+
|
|
34
40
|
)
|
|
35
41
|
|
|
36
42
|
from .printer import (
|
|
@@ -68,7 +74,11 @@ __all__ = [
|
|
|
68
74
|
"FastDual",
|
|
69
75
|
|
|
70
76
|
#VectorizedStandard
|
|
71
|
-
"VectorizedStandard"
|
|
77
|
+
"VectorizedStandard",
|
|
78
|
+
|
|
79
|
+
"BitPlaneStandard",
|
|
80
|
+
|
|
81
|
+
"GradedSparseStandard",
|
|
72
82
|
|
|
73
83
|
# Printer / formatter
|
|
74
84
|
"CDFormat",
|
|
@@ -24,6 +24,15 @@ from .vectorized import (
|
|
|
24
24
|
VectorizedStandard
|
|
25
25
|
)
|
|
26
26
|
|
|
27
|
+
from .bitplane import (
|
|
28
|
+
BitPlaneStandard
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
from .graded import (
|
|
33
|
+
GradedSparseStandard
|
|
34
|
+
)
|
|
35
|
+
|
|
27
36
|
__all__ = [
|
|
28
37
|
"BasisElement",
|
|
29
38
|
"BasisNotation",
|
|
@@ -42,4 +51,8 @@ __all__ = [
|
|
|
42
51
|
"FastDual",
|
|
43
52
|
|
|
44
53
|
"VectorizedStandard",
|
|
54
|
+
|
|
55
|
+
"BitPlaneStandard"
|
|
56
|
+
|
|
57
|
+
"GradedSparseStandard"
|
|
45
58
|
]
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class BitPlaneStandard:
|
|
5
|
+
"""
|
|
6
|
+
Arbitrary-width SIMD (bit-sliced) standard Cayley-Dickson O(1) multiplier.
|
|
7
|
+
|
|
8
|
+
Breaks the 64-bit SIMD-lane ceiling of VectorizedStandard by representing
|
|
9
|
+
each index as a column of bits (bit-slicing). Every word operation of the
|
|
10
|
+
O(1) evaluator (Definition def:o1) lifts to a lane-parallel boolean
|
|
11
|
+
operation over the bit-planes.
|
|
12
|
+
|
|
13
|
+
Representation:
|
|
14
|
+
A batch of N indices of width B is stored as a bit-plane matrix of
|
|
15
|
+
shape (N, B), dtype uint8, where entry (r, b) holds bit b of index r
|
|
16
|
+
(b = 0 is the LSB).
|
|
17
|
+
|
|
18
|
+
Complexity (paper Table tab:complexity):
|
|
19
|
+
Per-pair: O(B) bit-plane work
|
|
20
|
+
Batch of N: O(N*B / L) wall-clock, O(N*B) space, L = SIMD lanes
|
|
21
|
+
Bit-width: unbounded (no 64-bit ceiling)
|
|
22
|
+
|
|
23
|
+
Input / output convention:
|
|
24
|
+
multiply_indices(i_arr, j_arr) -> (signs_array, indices_list)
|
|
25
|
+
signs_array[k] in {-1, +1}, indices_list[k] = i_arr[k] ^ j_arr[k]
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
# ---------- representation: big-int <-> bit-plane ----------
|
|
29
|
+
@staticmethod
|
|
30
|
+
def ints_to_bitplane(vals, B):
|
|
31
|
+
"""
|
|
32
|
+
Convert a sequence of Python big-ints to an (N, B) uint8 bit-plane.
|
|
33
|
+
Entry (n, b) = bit b of vals[n]; b = 0 is the LSB.
|
|
34
|
+
"""
|
|
35
|
+
N = len(vals)
|
|
36
|
+
out = np.zeros((N, B), dtype=np.uint8)
|
|
37
|
+
for n, v in enumerate(vals):
|
|
38
|
+
v = int(v)
|
|
39
|
+
if v == 0:
|
|
40
|
+
continue
|
|
41
|
+
nbytes = (min(B, v.bit_length()) + 7) // 8
|
|
42
|
+
bits = np.unpackbits(
|
|
43
|
+
np.frombuffer(v.to_bytes(nbytes, 'little'), dtype=np.uint8),
|
|
44
|
+
bitorder='little')
|
|
45
|
+
m = min(B, len(bits))
|
|
46
|
+
out[n, :m] = bits[:m]
|
|
47
|
+
return out
|
|
48
|
+
|
|
49
|
+
@staticmethod
|
|
50
|
+
def bitplane_to_ints(bits):
|
|
51
|
+
"""
|
|
52
|
+
Convert an (N, B) bit-plane back to a list of Python big-ints.
|
|
53
|
+
"""
|
|
54
|
+
N, B = bits.shape
|
|
55
|
+
out = []
|
|
56
|
+
nbytes = (B + 7) // 8
|
|
57
|
+
for n in range(N):
|
|
58
|
+
packed = np.packbits(bits[n], bitorder='little')
|
|
59
|
+
out.append(int.from_bytes(packed.tobytes()[:nbytes], 'little'))
|
|
60
|
+
return out
|
|
61
|
+
|
|
62
|
+
@staticmethod
|
|
63
|
+
def _required_width(i_arr, j_arr):
|
|
64
|
+
"""Minimal bit-width B that holds every index in both batches."""
|
|
65
|
+
B = 1
|
|
66
|
+
for v in i_arr:
|
|
67
|
+
bl = int(v).bit_length()
|
|
68
|
+
if bl > B:
|
|
69
|
+
B = bl
|
|
70
|
+
for v in j_arr:
|
|
71
|
+
bl = int(v).bit_length()
|
|
72
|
+
if bl > B:
|
|
73
|
+
B = bl
|
|
74
|
+
return B
|
|
75
|
+
|
|
76
|
+
# ---------- core O(1) evaluator over bit-planes ----------
|
|
77
|
+
def multiply_indices_bitplane(self, i_bits, j_bits):
|
|
78
|
+
"""
|
|
79
|
+
Vectorized arbitrary-width O(1) multiplication on bit-planes.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
i_bits: (N, B) uint8 bit-plane for the first indices
|
|
83
|
+
j_bits: (N, B) uint8 bit-plane for the second indices
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
(signs, idx_bits):
|
|
87
|
+
signs : (N,) int8 array, +1 / -1
|
|
88
|
+
idx_bits: (N, B) uint8 bit-plane of the result indices i^j
|
|
89
|
+
"""
|
|
90
|
+
i_bits = np.asarray(i_bits, dtype=np.uint8)
|
|
91
|
+
j_bits = np.asarray(j_bits, dtype=np.uint8)
|
|
92
|
+
N, B = i_bits.shape
|
|
93
|
+
ar = np.arange(N)
|
|
94
|
+
|
|
95
|
+
idx = i_bits ^ j_bits # result index = XOR
|
|
96
|
+
|
|
97
|
+
# structural masks
|
|
98
|
+
mask_zero = (~i_bits.any(axis=1)) | (~j_bits.any(axis=1))
|
|
99
|
+
mask_diag = ((i_bits == j_bits).all(axis=1)) & ~mask_zero
|
|
100
|
+
|
|
101
|
+
# nu2 via first-set-bit (argmax returns the index of the first 1)
|
|
102
|
+
t = np.argmax(idx, axis=1)
|
|
103
|
+
ti = np.argmax(i_bits, axis=1)
|
|
104
|
+
tj = np.argmax(j_bits, axis=1)
|
|
105
|
+
|
|
106
|
+
# branch selection
|
|
107
|
+
cond1 = (t >= ti) & (t >= tj) # diagonal break
|
|
108
|
+
cond2 = (~cond1) & (ti >= tj) # i-axis break
|
|
109
|
+
cond3 = ~cond1 & ~cond2 # j-axis break
|
|
110
|
+
|
|
111
|
+
# branch signs
|
|
112
|
+
s1 = 2 * i_bits[ar, t].astype(np.int64) - 1
|
|
113
|
+
# i-axis: delta = -1 always here (Corollary cor:iaxis-collapse);
|
|
114
|
+
# kept in general form to mirror the scalar evaluator exactly.
|
|
115
|
+
delta = np.where(tj == ti, 1, -1)
|
|
116
|
+
s2 = np.where(j_bits[ar, ti] == 0, delta, -delta)
|
|
117
|
+
s3 = 1 - 2 * i_bits[ar, tj].astype(np.int64)
|
|
118
|
+
s = np.where(cond1, s1, np.where(cond2, s2, s3))
|
|
119
|
+
|
|
120
|
+
# higher-bit parity via reverse cumulative sum
|
|
121
|
+
k = np.where(cond1, t + 1, np.where(cond2, ti + 1, tj + 1))
|
|
122
|
+
or_bits = i_bits | j_bits
|
|
123
|
+
rev_cumsum = np.cumsum(or_bits[:, ::-1], axis=1)[:, ::-1]
|
|
124
|
+
parity_count = np.zeros(N, dtype=np.int64)
|
|
125
|
+
valid = k < B
|
|
126
|
+
parity_count[valid] = rev_cumsum[ar[valid], k[valid]]
|
|
127
|
+
s = np.where((parity_count & 1) == 1, -s, s)
|
|
128
|
+
|
|
129
|
+
# structural overrides
|
|
130
|
+
s = np.where(mask_zero, 1, s)
|
|
131
|
+
s = np.where(mask_diag, -1, s)
|
|
132
|
+
|
|
133
|
+
return s.astype(np.int8), idx
|
|
134
|
+
|
|
135
|
+
# ---------- convenience: Python ints in, ints out ----------
|
|
136
|
+
def multiply_indices(self, i_arr, j_arr):
|
|
137
|
+
"""
|
|
138
|
+
Convenience wrapper: sequences of Python big-ints in, integer results out.
|
|
139
|
+
Mirrors FastStandard.multiply_indices but vectorized and arbitrary-width.
|
|
140
|
+
|
|
141
|
+
Args:
|
|
142
|
+
i_arr: sequence of first indices (Python ints, any width)
|
|
143
|
+
j_arr: sequence of second indices (Python ints, any width)
|
|
144
|
+
|
|
145
|
+
Returns:
|
|
146
|
+
(signs, indices):
|
|
147
|
+
signs : (N,) int8 array, +1 / -1
|
|
148
|
+
indices : list of N Python ints, indices[k] = i_arr[k] ^ j_arr[k]
|
|
149
|
+
"""
|
|
150
|
+
i_arr = list(i_arr)
|
|
151
|
+
j_arr = list(j_arr)
|
|
152
|
+
if len(i_arr) != len(j_arr):
|
|
153
|
+
raise ValueError("i_arr and j_arr must have the same length")
|
|
154
|
+
if len(i_arr) == 0:
|
|
155
|
+
return np.zeros(0, dtype=np.int8), []
|
|
156
|
+
|
|
157
|
+
B = self._required_width(i_arr, j_arr)
|
|
158
|
+
i_bits = self.ints_to_bitplane(i_arr, B)
|
|
159
|
+
j_bits = self.ints_to_bitplane(j_arr, B)
|
|
160
|
+
signs, idx_bits = self.multiply_indices_bitplane(i_bits, j_bits)
|
|
161
|
+
indices = self.bitplane_to_ints(idx_bits)
|
|
162
|
+
return signs, indices
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import bisect
|
|
2
|
+
|
|
3
|
+
class GradedSparseStandard:
|
|
4
|
+
"""
|
|
5
|
+
Arbitrary-width O(1)-per-primitive multiplier on the GRADED (sparse
|
|
6
|
+
set-of-bit-positions) representation. Memory-optimal: O(p) not O(B).
|
|
7
|
+
|
|
8
|
+
Internal convention: an index m is a sorted tuple of 0-INDEXED bit
|
|
9
|
+
positions, m = sum(2**b for b in pos). The Wilmot generator level is
|
|
10
|
+
k = b + 1 (used only for display via graded_to_notation).
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
# ---------- representation: int <-> graded ----------
|
|
14
|
+
@staticmethod
|
|
15
|
+
def int_to_graded(m):
|
|
16
|
+
"""Python int -> sorted tuple of 0-indexed set-bit positions. O(p)."""
|
|
17
|
+
m = int(m)
|
|
18
|
+
pos = []
|
|
19
|
+
while m:
|
|
20
|
+
low = m & -m # isolate lowest set bit
|
|
21
|
+
pos.append(low.bit_length() - 1)
|
|
22
|
+
m ^= low # clear it
|
|
23
|
+
return tuple(pos) # already ascending
|
|
24
|
+
|
|
25
|
+
@staticmethod
|
|
26
|
+
def graded_to_int(pos):
|
|
27
|
+
"""Sorted tuple of bit positions -> Python int. O(p)."""
|
|
28
|
+
m = 0
|
|
29
|
+
for b in pos:
|
|
30
|
+
m |= (1 << b)
|
|
31
|
+
return m
|
|
32
|
+
|
|
33
|
+
@staticmethod
|
|
34
|
+
def graded_to_notation(pos):
|
|
35
|
+
"""Graded form -> Wilmot string 'o{k1},{k2},...' (1-indexed)."""
|
|
36
|
+
return "1" if not pos else "o" + ",".join(str(b + 1) for b in pos)
|
|
37
|
+
|
|
38
|
+
# ---------- faithful graded primitives ----------
|
|
39
|
+
@staticmethod
|
|
40
|
+
def _nu2_gr(pos):
|
|
41
|
+
"""nu2 = lowest set bit = first position. Requires pos non-empty."""
|
|
42
|
+
return pos[0]
|
|
43
|
+
|
|
44
|
+
@staticmethod
|
|
45
|
+
def _bit_gr(pos, t):
|
|
46
|
+
"""(m >> t) & 1 via membership test on sorted positions."""
|
|
47
|
+
idx = bisect.bisect_left(pos, t)
|
|
48
|
+
return 1 if (idx < len(pos) and pos[idx] == t) else 0
|
|
49
|
+
|
|
50
|
+
@staticmethod
|
|
51
|
+
def _nu2_xor_gr(pos_i, pos_j):
|
|
52
|
+
"""Lowest bit of i^j = lowest position in the symmetric difference."""
|
|
53
|
+
a = b = 0
|
|
54
|
+
while a < len(pos_i) and b < len(pos_j):
|
|
55
|
+
if pos_i[a] < pos_j[b]: return pos_i[a]
|
|
56
|
+
elif pos_j[b] < pos_i[a]: return pos_j[b]
|
|
57
|
+
else: a += 1; b += 1
|
|
58
|
+
if a < len(pos_i): return pos_i[a]
|
|
59
|
+
if b < len(pos_j): return pos_j[b]
|
|
60
|
+
return None # i == j (handled upstream)
|
|
61
|
+
|
|
62
|
+
@staticmethod
|
|
63
|
+
def _xor_gr(pos_i, pos_j):
|
|
64
|
+
"""Full symmetric difference = graded form of i^j."""
|
|
65
|
+
out = []; a = b = 0
|
|
66
|
+
while a < len(pos_i) and b < len(pos_j):
|
|
67
|
+
if pos_i[a] < pos_j[b]: out.append(pos_i[a]); a += 1
|
|
68
|
+
elif pos_j[b] < pos_i[a]: out.append(pos_j[b]); b += 1
|
|
69
|
+
else: a += 1; b += 1
|
|
70
|
+
out.extend(pos_i[a:]); out.extend(pos_j[b:])
|
|
71
|
+
return tuple(out)
|
|
72
|
+
|
|
73
|
+
@staticmethod
|
|
74
|
+
def _pop_or_shift_gr(pos_i, pos_j, k):
|
|
75
|
+
"""popcount((i|j) >> k) = #positions in union(pos_i,pos_j) that are >= k."""
|
|
76
|
+
count = 0; a = b = 0
|
|
77
|
+
while a < len(pos_i) and b < len(pos_j):
|
|
78
|
+
if pos_i[a] < pos_j[b]:
|
|
79
|
+
if pos_i[a] >= k: count += 1
|
|
80
|
+
a += 1
|
|
81
|
+
elif pos_j[b] < pos_i[a]:
|
|
82
|
+
if pos_j[b] >= k: count += 1
|
|
83
|
+
b += 1
|
|
84
|
+
else:
|
|
85
|
+
if pos_i[a] >= k: count += 1 # union: count once
|
|
86
|
+
a += 1; b += 1
|
|
87
|
+
while a < len(pos_i):
|
|
88
|
+
if pos_i[a] >= k: count += 1
|
|
89
|
+
a += 1
|
|
90
|
+
while b < len(pos_j):
|
|
91
|
+
if pos_j[b] >= k: count += 1
|
|
92
|
+
b += 1
|
|
93
|
+
return count
|
|
94
|
+
|
|
95
|
+
# ---------- the O(1) evaluator on graded forms ----------
|
|
96
|
+
def multiply_graded(self, pos_i, pos_j):
|
|
97
|
+
"""Core evaluator on graded forms -> (sign, result_pos).
|
|
98
|
+
Uses the i-axis collapse (Cor. cor:iaxis-collapse): delta = -1."""
|
|
99
|
+
if not pos_i or not pos_j: # identity
|
|
100
|
+
return (1, self._xor_gr(pos_i, pos_j))
|
|
101
|
+
if pos_i == pos_j: # main diagonal
|
|
102
|
+
return (-1, ())
|
|
103
|
+
|
|
104
|
+
t = self._nu2_xor_gr(pos_i, pos_j)
|
|
105
|
+
ti = self._nu2_gr(pos_i)
|
|
106
|
+
tj = self._nu2_gr(pos_j)
|
|
107
|
+
k = max(t, ti, tj) + 1
|
|
108
|
+
|
|
109
|
+
if t >= ti and t >= tj: # diagonal break
|
|
110
|
+
s = 2 * self._bit_gr(pos_i, t) - 1
|
|
111
|
+
elif ti >= tj: # i-axis break; delta=-1 always
|
|
112
|
+
b = self._bit_gr(pos_j, ti)
|
|
113
|
+
s = -((-1) ** b) # = 2b - 1
|
|
114
|
+
else: # j-axis break
|
|
115
|
+
s = 1 - 2 * self._bit_gr(pos_i, tj)
|
|
116
|
+
|
|
117
|
+
if self._pop_or_shift_gr(pos_i, pos_j, k) & 1:
|
|
118
|
+
s = -s
|
|
119
|
+
return (s, self._xor_gr(pos_i, pos_j))
|
|
120
|
+
|
|
121
|
+
def multiply_indices(self, i, j):
|
|
122
|
+
"""Convenience: ints in -> (sign, i^j) out. Mirrors FastStandard."""
|
|
123
|
+
sign, result_pos = self.multiply_graded(
|
|
124
|
+
self.int_to_graded(i), self.int_to_graded(j))
|
|
125
|
+
return (sign, self.graded_to_int(result_pos))
|