hypercomplex-engine 0.7.1.2__py3-none-any.whl → 0.7.2__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/core/bitplane/bitplane_standard.py +60 -88
- hypercomplex_engine/core/fast/fast_standard.py +38 -89
- hypercomplex_engine/core/fast/fast_standard_old.py +121 -0
- hypercomplex_engine/core/graded/graded_sparse_standard.py +148 -72
- hypercomplex_engine/core/vectorized/vectorized_standard.py +101 -83
- {hypercomplex_engine-0.7.1.2.dist-info → hypercomplex_engine-0.7.2.dist-info}/METADATA +50 -2
- {hypercomplex_engine-0.7.1.2.dist-info → hypercomplex_engine-0.7.2.dist-info}/RECORD +11 -10
- {hypercomplex_engine-0.7.1.2.dist-info → hypercomplex_engine-0.7.2.dist-info}/WHEEL +0 -0
- {hypercomplex_engine-0.7.1.2.dist-info → hypercomplex_engine-0.7.2.dist-info}/licenses/LICENSE +0 -0
- {hypercomplex_engine-0.7.1.2.dist-info → hypercomplex_engine-0.7.2.dist-info}/licenses/NOTICE +0 -0
- {hypercomplex_engine-0.7.1.2.dist-info → hypercomplex_engine-0.7.2.dist-info}/top_level.txt +0 -0
|
@@ -1,39 +1,30 @@
|
|
|
1
|
+
"""
|
|
2
|
+
BitPlaneStandard — arbitrary-width bit-sliced O(1) Cayley-Dickson evaluator.
|
|
3
|
+
|
|
4
|
+
Breaks the 64-bit SIMD-lane ceiling by storing a batch of N indices of width B
|
|
5
|
+
as an (N, B) uint8 bit-plane, entry (r, b) = bit b of index r (b = 0 is LSB).
|
|
6
|
+
|
|
7
|
+
Optimizations vs. the first release (verified against FastStandard):
|
|
8
|
+
* i-axis branch collapsed to 2*b - 1 (Corollary cor:iaxis-collapse)
|
|
9
|
+
* parity via FORWARD XOR-accumulate instead of int64 cumsum:
|
|
10
|
+
- stays uint8 -> 8x less memory than the cumsum buffer
|
|
11
|
+
- no array reversal -> contiguous, cache-friendly access
|
|
12
|
+
- k == B handled for free (prefix == total -> parity 0), no mask needed
|
|
13
|
+
Identity used: XOR[k:B] = XOR[0:B] ^ XOR[0:k].
|
|
14
|
+
|
|
15
|
+
Measured (N=2000, B=4000): parity buffer 128 MB -> 8 MB; whole call 25.4 ms.
|
|
16
|
+
"""
|
|
1
17
|
import numpy as np
|
|
2
18
|
|
|
3
19
|
|
|
4
20
|
class BitPlaneStandard:
|
|
5
|
-
"""
|
|
6
|
-
|
|
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 ----------
|
|
21
|
+
"""Arbitrary-width bit-sliced standard Cayley-Dickson O(1) multiplier."""
|
|
22
|
+
|
|
23
|
+
# ---------- representation ----------
|
|
29
24
|
@staticmethod
|
|
30
25
|
def ints_to_bitplane(vals, B):
|
|
31
|
-
"""
|
|
32
|
-
|
|
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)
|
|
26
|
+
"""Python big-ints -> (N, B) uint8 bit-plane, b = 0 is LSB."""
|
|
27
|
+
out = np.zeros((len(vals), B), dtype=np.uint8)
|
|
37
28
|
for n, v in enumerate(vals):
|
|
38
29
|
v = int(v)
|
|
39
30
|
if v == 0:
|
|
@@ -48,12 +39,10 @@ class BitPlaneStandard:
|
|
|
48
39
|
|
|
49
40
|
@staticmethod
|
|
50
41
|
def bitplane_to_ints(bits):
|
|
51
|
-
"""
|
|
52
|
-
Convert an (N, B) bit-plane back to a list of Python big-ints.
|
|
53
|
-
"""
|
|
42
|
+
"""(N, B) bit-plane -> list of Python big-ints."""
|
|
54
43
|
N, B = bits.shape
|
|
55
|
-
out = []
|
|
56
44
|
nbytes = (B + 7) // 8
|
|
45
|
+
out = []
|
|
57
46
|
for n in range(N):
|
|
58
47
|
packed = np.packbits(bits[n], bitorder='little')
|
|
59
48
|
out.append(int.from_bytes(packed.tobytes()[:nbytes], 'little'))
|
|
@@ -61,7 +50,7 @@ class BitPlaneStandard:
|
|
|
61
50
|
|
|
62
51
|
@staticmethod
|
|
63
52
|
def _required_width(i_arr, j_arr):
|
|
64
|
-
"""Minimal
|
|
53
|
+
"""Minimal B holding every index in both batches (>= 1)."""
|
|
65
54
|
B = 1
|
|
66
55
|
for v in i_arr:
|
|
67
56
|
bl = int(v).bit_length()
|
|
@@ -73,90 +62,73 @@ class BitPlaneStandard:
|
|
|
73
62
|
B = bl
|
|
74
63
|
return B
|
|
75
64
|
|
|
76
|
-
# ---------- core
|
|
65
|
+
# ---------- core evaluator ----------
|
|
77
66
|
def multiply_indices_bitplane(self, i_bits, j_bits):
|
|
78
67
|
"""
|
|
79
68
|
Vectorized arbitrary-width O(1) multiplication on bit-planes.
|
|
80
69
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
|
70
|
+
Returns
|
|
71
|
+
-------
|
|
72
|
+
(signs, idx_bits) : (N,) int8, (N, B) uint8 bit-plane of i ^ j
|
|
89
73
|
"""
|
|
90
74
|
i_bits = np.asarray(i_bits, dtype=np.uint8)
|
|
91
75
|
j_bits = np.asarray(j_bits, dtype=np.uint8)
|
|
92
76
|
N, B = i_bits.shape
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
77
|
+
idx = i_bits ^ j_bits
|
|
78
|
+
if N == 0 or B == 0:
|
|
79
|
+
return np.zeros(N, dtype=np.int8), idx
|
|
96
80
|
|
|
97
|
-
|
|
81
|
+
ar = np.arange(N)
|
|
98
82
|
mask_zero = (~i_bits.any(axis=1)) | (~j_bits.any(axis=1))
|
|
99
83
|
mask_diag = ((i_bits == j_bits).all(axis=1)) & ~mask_zero
|
|
100
84
|
|
|
101
|
-
# nu2
|
|
85
|
+
# nu2 = first set bit; argmax on all-zero rows gives 0, overridden below
|
|
102
86
|
t = np.argmax(idx, axis=1)
|
|
103
87
|
ti = np.argmax(i_bits, axis=1)
|
|
104
88
|
tj = np.argmax(j_bits, axis=1)
|
|
105
89
|
|
|
106
|
-
#
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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)
|
|
90
|
+
cond1 = (t >= ti) & (t >= tj) # diagonal break
|
|
91
|
+
cond2 = (~cond1) & (ti >= tj) # i-axis break
|
|
92
|
+
|
|
93
|
+
s1 = 2 * i_bits[ar, t].astype(np.int8) - 1
|
|
94
|
+
# cond2 forces ti > tj, so delta == -1 unconditionally: s2 = 2*b - 1.
|
|
95
|
+
s2 = 2 * j_bits[ar, ti].astype(np.int8) - 1
|
|
96
|
+
s3 = 1 - 2 * i_bits[ar, tj].astype(np.int8)
|
|
118
97
|
s = np.where(cond1, s1, np.where(cond2, s2, s3))
|
|
119
98
|
|
|
120
|
-
# higher-bit parity via reverse cumulative sum
|
|
121
99
|
k = np.where(cond1, t + 1, np.where(cond2, ti + 1, tj + 1))
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
100
|
+
|
|
101
|
+
# parity of OR above k, via forward XOR-accumulate (uint8, contiguous)
|
|
102
|
+
inc = np.bitwise_xor.accumulate(i_bits | j_bits, axis=1)
|
|
103
|
+
total = inc[:, -1] # XOR of all B planes
|
|
104
|
+
prefix = np.zeros(N, dtype=np.uint8)
|
|
105
|
+
nz = k > 0
|
|
106
|
+
prefix[nz] = inc[ar[nz], k[nz] - 1] # k == B -> prefix == total
|
|
107
|
+
s = np.where((total ^ prefix) == 1, -s, s)
|
|
108
|
+
|
|
130
109
|
s = np.where(mask_zero, 1, s)
|
|
131
110
|
s = np.where(mask_diag, -1, s)
|
|
132
|
-
|
|
133
111
|
return s.astype(np.int8), idx
|
|
134
112
|
|
|
135
|
-
# ---------- convenience
|
|
113
|
+
# ---------- convenience ----------
|
|
136
114
|
def multiply_indices(self, i_arr, j_arr):
|
|
137
115
|
"""
|
|
138
|
-
|
|
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)
|
|
116
|
+
Python big-ints in, integer results out.
|
|
144
117
|
|
|
145
|
-
Returns
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
indices : list of N Python ints, indices[k] = i_arr[k] ^ j_arr[k]
|
|
118
|
+
Returns
|
|
119
|
+
-------
|
|
120
|
+
(signs, indices) : (N,) int8, list of N Python ints (i_arr[k] ^ j_arr[k])
|
|
149
121
|
"""
|
|
150
122
|
i_arr = list(i_arr)
|
|
151
123
|
j_arr = list(j_arr)
|
|
152
124
|
if len(i_arr) != len(j_arr):
|
|
153
125
|
raise ValueError("i_arr and j_arr must have the same length")
|
|
154
|
-
if
|
|
126
|
+
if not i_arr:
|
|
155
127
|
return np.zeros(0, dtype=np.int8), []
|
|
156
128
|
|
|
157
129
|
B = self._required_width(i_arr, j_arr)
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return signs,
|
|
130
|
+
signs, idx_bits = self.multiply_indices_bitplane(
|
|
131
|
+
self.ints_to_bitplane(i_arr, B),
|
|
132
|
+
self.ints_to_bitplane(j_arr, B),
|
|
133
|
+
)
|
|
134
|
+
return signs, self.bitplane_to_ints(idx_bits)
|
|
@@ -1,121 +1,70 @@
|
|
|
1
1
|
from ..validation import Validation
|
|
2
|
-
from .bit_utils import nu2, popcount
|
|
2
|
+
from .bit_utils import nu2, popcount # kept: public helpers, other modules use them
|
|
3
|
+
|
|
4
|
+
# Unbound builtins bound once at import: one module-global lookup instead of a
|
|
5
|
+
# per-call attribute lookup on the int object.
|
|
6
|
+
_bl = int.bit_length
|
|
7
|
+
_bc = int.bit_count
|
|
3
8
|
|
|
4
9
|
|
|
5
10
|
class FastStandard:
|
|
6
11
|
"""
|
|
7
12
|
Standard Cayley-Dickson O(1) multiplier.
|
|
8
13
|
|
|
9
|
-
Input / output convention:
|
|
10
|
-
(sign, index)
|
|
11
|
-
|
|
12
|
-
The sign is computed in O(1) Word-RAM time using:
|
|
13
|
-
t = nu2(i XOR j)
|
|
14
|
-
ti = nu2(i)
|
|
15
|
-
tj = nu2(j)
|
|
14
|
+
Input / output convention: (sign, index), index = i XOR j.
|
|
16
15
|
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
Optimized evaluation (verified exhaustively against the reference form):
|
|
17
|
+
* the break type is decided by (ti, tj) alone -- no comparison against t;
|
|
18
|
+
* t = nu2(i XOR j) is computed only on the tie ti == tj (~1/3 of pairs);
|
|
19
|
+
* the i-axis branch is collapsed to 2*b - 1 (Corollary cor:iaxis-collapse);
|
|
20
|
+
* nu2 / popcount are inlined (their zero-guards are provably dead here).
|
|
19
21
|
"""
|
|
20
22
|
|
|
21
23
|
def multiply(self, t1: tuple, t2: tuple) -> tuple:
|
|
22
|
-
"""
|
|
23
|
-
Multiply two standard basis element tuples.
|
|
24
|
-
|
|
25
|
-
t1 = (sign1, index1)
|
|
26
|
-
t2 = (sign2, index2)
|
|
27
|
-
|
|
28
|
-
Returns:
|
|
29
|
-
(final_sign, index1 XOR index2)
|
|
30
|
-
"""
|
|
24
|
+
"""Multiply two signed basis tuples (validated)."""
|
|
31
25
|
Validation.basis_tuple(t1, allow_zero=False, allow_eps=False)
|
|
32
26
|
Validation.basis_tuple(t2, allow_zero=False, allow_eps=False)
|
|
33
|
-
|
|
34
27
|
s1, i = int(t1[0]), int(t1[1])
|
|
35
28
|
s2, j = int(t2[0]), int(t2[1])
|
|
36
|
-
|
|
37
29
|
sign, idx = self.multiply_indices(i, j)
|
|
38
|
-
|
|
39
30
|
return (s1 * s2 * sign, idx)
|
|
40
31
|
|
|
41
32
|
def multiply_indices(self, i: int, j: int) -> tuple:
|
|
42
33
|
"""
|
|
43
34
|
Core O(1) multiplication for non-negative integer indices.
|
|
44
|
-
|
|
45
|
-
Returns:
|
|
46
|
-
(sign, i XOR j)
|
|
35
|
+
Returns (sign, i XOR j).
|
|
47
36
|
"""
|
|
48
|
-
i
|
|
49
|
-
|
|
50
|
-
|
|
37
|
+
if type(i) is not int:
|
|
38
|
+
i = int(i)
|
|
39
|
+
if type(j) is not int:
|
|
40
|
+
j = int(j)
|
|
41
|
+
# Explicit guard: do NOT fold into (i ^ j) < 0, which misses i == j < 0.
|
|
51
42
|
if i < 0 or j < 0:
|
|
52
43
|
raise ValueError("indices must be >= 0")
|
|
53
44
|
|
|
54
|
-
|
|
55
|
-
if i
|
|
56
|
-
return (1,
|
|
57
|
-
|
|
58
|
-
# Main diagonal
|
|
59
|
-
if i == j:
|
|
45
|
+
idx = i ^ j
|
|
46
|
+
if not (i and j): # identity: e_0 * e_j = e_j
|
|
47
|
+
return (1, idx)
|
|
48
|
+
if i == j: # e_i^2 = -e_0 for i > 0
|
|
60
49
|
return (-1, 0)
|
|
61
50
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
ti = nu2(i)
|
|
65
|
-
tj = nu2(j)
|
|
66
|
-
|
|
67
|
-
# --------------------------------------------------------------
|
|
68
|
-
# Diagonal break:
|
|
69
|
-
# t >= ti and t >= tj
|
|
70
|
-
# --------------------------------------------------------------
|
|
71
|
-
if t >= ti and t >= tj:
|
|
72
|
-
k = t + 1
|
|
73
|
-
s = 2 * ((i >> t) & 1) - 1
|
|
74
|
-
|
|
75
|
-
# --------------------------------------------------------------
|
|
76
|
-
# i-axis break:
|
|
77
|
-
# ti > t and ti >= tj
|
|
78
|
-
# --------------------------------------------------------------
|
|
79
|
-
elif ti >= tj:
|
|
80
|
-
k = ti + 1
|
|
81
|
-
|
|
82
|
-
# Quadrant bit:
|
|
83
|
-
# 0 -> quadrant c
|
|
84
|
-
# 1 -> quadrant d
|
|
85
|
-
b = (j >> ti) & 1
|
|
86
|
-
|
|
87
|
-
# Local j below the break level
|
|
88
|
-
j_loc = j & ((1 << ti) - 1)
|
|
89
|
-
|
|
90
|
-
# delta(j_loc):
|
|
91
|
-
# +1 if j_loc == 0
|
|
92
|
-
# -1 otherwise
|
|
93
|
-
delta = 1 if j_loc == 0 else -1
|
|
94
|
-
|
|
95
|
-
if b == 0:
|
|
96
|
-
# quadrant c
|
|
97
|
-
s = delta
|
|
98
|
-
else:
|
|
99
|
-
# quadrant d
|
|
100
|
-
s = -delta
|
|
51
|
+
ti = _bl(i & -i) - 1
|
|
52
|
+
tj = _bl(j & -j) - 1
|
|
101
53
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
# tj > t and tj > ti
|
|
105
|
-
# --------------------------------------------------------------
|
|
106
|
-
else:
|
|
54
|
+
if ti < tj:
|
|
55
|
+
# t = ti < tj -> j-axis break
|
|
107
56
|
k = tj + 1
|
|
108
|
-
|
|
109
|
-
# Quadrant bit:
|
|
110
|
-
# 0 -> quadrant b, sign +1
|
|
111
|
-
# 1 -> quadrant d, sign -1
|
|
112
57
|
s = 1 - 2 * ((i >> tj) & 1)
|
|
58
|
+
elif ti > tj:
|
|
59
|
+
# t = tj < ti -> i-axis break; delta == -1 unconditionally
|
|
60
|
+
k = ti + 1
|
|
61
|
+
s = 2 * ((j >> ti) & 1) - 1
|
|
62
|
+
else:
|
|
63
|
+
# ti == tj -> t > ti -> diagonal break (only case needing t)
|
|
64
|
+
t = _bl(idx & -idx) - 1
|
|
65
|
+
k = t + 1
|
|
66
|
+
s = 2 * ((i >> t) & 1) - 1
|
|
113
67
|
|
|
114
|
-
|
|
115
|
-
# Higher-bit parity correction:
|
|
116
|
-
# standard flips in quadrants b, c, d
|
|
117
|
-
# --------------------------------------------------------------
|
|
118
|
-
if popcount((i | j) >> k) & 1:
|
|
68
|
+
if _bc((i | j) >> k) & 1:
|
|
119
69
|
s = -s
|
|
120
|
-
|
|
121
|
-
return (s, i ^ j)
|
|
70
|
+
return (s, idx)
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
from ..validation import Validation
|
|
2
|
+
from .bit_utils import nu2, popcount
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class FastStandard:
|
|
6
|
+
"""
|
|
7
|
+
Standard Cayley-Dickson O(1) multiplier.
|
|
8
|
+
|
|
9
|
+
Input / output convention:
|
|
10
|
+
(sign, index)
|
|
11
|
+
|
|
12
|
+
The sign is computed in O(1) Word-RAM time using:
|
|
13
|
+
t = nu2(i XOR j)
|
|
14
|
+
ti = nu2(i)
|
|
15
|
+
tj = nu2(j)
|
|
16
|
+
|
|
17
|
+
The basis index is always:
|
|
18
|
+
i XOR j
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def multiply(self, t1: tuple, t2: tuple) -> tuple:
|
|
22
|
+
"""
|
|
23
|
+
Multiply two standard basis element tuples.
|
|
24
|
+
|
|
25
|
+
t1 = (sign1, index1)
|
|
26
|
+
t2 = (sign2, index2)
|
|
27
|
+
|
|
28
|
+
Returns:
|
|
29
|
+
(final_sign, index1 XOR index2)
|
|
30
|
+
"""
|
|
31
|
+
Validation.basis_tuple(t1, allow_zero=False, allow_eps=False)
|
|
32
|
+
Validation.basis_tuple(t2, allow_zero=False, allow_eps=False)
|
|
33
|
+
|
|
34
|
+
s1, i = int(t1[0]), int(t1[1])
|
|
35
|
+
s2, j = int(t2[0]), int(t2[1])
|
|
36
|
+
|
|
37
|
+
sign, idx = self.multiply_indices(i, j)
|
|
38
|
+
|
|
39
|
+
return (s1 * s2 * sign, idx)
|
|
40
|
+
|
|
41
|
+
def multiply_indices(self, i: int, j: int) -> tuple:
|
|
42
|
+
"""
|
|
43
|
+
Core O(1) multiplication for non-negative integer indices.
|
|
44
|
+
|
|
45
|
+
Returns:
|
|
46
|
+
(sign, i XOR j)
|
|
47
|
+
"""
|
|
48
|
+
i = int(i)
|
|
49
|
+
j = int(j)
|
|
50
|
+
|
|
51
|
+
if i < 0 or j < 0:
|
|
52
|
+
raise ValueError("indices must be >= 0")
|
|
53
|
+
|
|
54
|
+
# Identity
|
|
55
|
+
if i == 0 or j == 0:
|
|
56
|
+
return (1, i ^ j)
|
|
57
|
+
|
|
58
|
+
# Main diagonal
|
|
59
|
+
if i == j:
|
|
60
|
+
return (-1, 0)
|
|
61
|
+
|
|
62
|
+
# Structural break levels
|
|
63
|
+
t = nu2(i ^ j)
|
|
64
|
+
ti = nu2(i)
|
|
65
|
+
tj = nu2(j)
|
|
66
|
+
|
|
67
|
+
# --------------------------------------------------------------
|
|
68
|
+
# Diagonal break:
|
|
69
|
+
# t >= ti and t >= tj
|
|
70
|
+
# --------------------------------------------------------------
|
|
71
|
+
if t >= ti and t >= tj:
|
|
72
|
+
k = t + 1
|
|
73
|
+
s = 2 * ((i >> t) & 1) - 1
|
|
74
|
+
|
|
75
|
+
# --------------------------------------------------------------
|
|
76
|
+
# i-axis break:
|
|
77
|
+
# ti > t and ti >= tj
|
|
78
|
+
# --------------------------------------------------------------
|
|
79
|
+
elif ti >= tj:
|
|
80
|
+
k = ti + 1
|
|
81
|
+
|
|
82
|
+
# Quadrant bit:
|
|
83
|
+
# 0 -> quadrant c
|
|
84
|
+
# 1 -> quadrant d
|
|
85
|
+
b = (j >> ti) & 1
|
|
86
|
+
|
|
87
|
+
# Local j below the break level
|
|
88
|
+
j_loc = j & ((1 << ti) - 1)
|
|
89
|
+
|
|
90
|
+
# delta(j_loc):
|
|
91
|
+
# +1 if j_loc == 0
|
|
92
|
+
# -1 otherwise
|
|
93
|
+
delta = 1 if j_loc == 0 else -1
|
|
94
|
+
|
|
95
|
+
if b == 0:
|
|
96
|
+
# quadrant c
|
|
97
|
+
s = delta
|
|
98
|
+
else:
|
|
99
|
+
# quadrant d
|
|
100
|
+
s = -delta
|
|
101
|
+
|
|
102
|
+
# --------------------------------------------------------------
|
|
103
|
+
# j-axis break:
|
|
104
|
+
# tj > t and tj > ti
|
|
105
|
+
# --------------------------------------------------------------
|
|
106
|
+
else:
|
|
107
|
+
k = tj + 1
|
|
108
|
+
|
|
109
|
+
# Quadrant bit:
|
|
110
|
+
# 0 -> quadrant b, sign +1
|
|
111
|
+
# 1 -> quadrant d, sign -1
|
|
112
|
+
s = 1 - 2 * ((i >> tj) & 1)
|
|
113
|
+
|
|
114
|
+
# --------------------------------------------------------------
|
|
115
|
+
# Higher-bit parity correction:
|
|
116
|
+
# standard flips in quadrants b, c, d
|
|
117
|
+
# --------------------------------------------------------------
|
|
118
|
+
if popcount((i | j) >> k) & 1:
|
|
119
|
+
s = -s
|
|
120
|
+
|
|
121
|
+
return (s, i ^ j)
|
|
@@ -1,125 +1,201 @@
|
|
|
1
|
+
"""
|
|
2
|
+
GradedSparseStandard — arbitrary-width O(p) evaluator on the graded form.
|
|
3
|
+
|
|
4
|
+
An index m is a sorted tuple of 0-INDEXED set-bit positions, m = sum(2**b).
|
|
5
|
+
The Wilmot generator level is k = b + 1 (display only, graded_to_notation).
|
|
6
|
+
|
|
7
|
+
Optimizations vs. the first release (verified against FastStandard):
|
|
8
|
+
* SINGLE fused two-pointer merge replaces three separate O(p) scans
|
|
9
|
+
(_nu2_xor_gr + _pop_or_shift_gr + _xor_gr). Valid because every element
|
|
10
|
+
before the first difference is common to both lists and lies below t < k,
|
|
11
|
+
so it contributes nothing to the union count.
|
|
12
|
+
* the diagonal-break bit comes free from the merge (no bisect)
|
|
13
|
+
* i-axis branch uses 2*b - 1 (no integer exponentiation)
|
|
14
|
+
* hybrid int_to_graded: peel a few low bits with m & -m (wins for very
|
|
15
|
+
sparse indices), then a C-level byte scan for the remainder.
|
|
16
|
+
|
|
17
|
+
Measured: 1.8x-2.0x on multiply_graded; up to 9x on wide int_to_graded.
|
|
18
|
+
The legacy helpers are retained for reference and backward compatibility.
|
|
19
|
+
"""
|
|
1
20
|
import bisect
|
|
21
|
+
import numpy as np
|
|
22
|
+
|
|
23
|
+
# Peel budget for int_to_graded, scaled by width: peeling one bit costs
|
|
24
|
+
# O(B/w) big-int work, the byte scan costs O(B/8) C work plus O(p) Python.
|
|
25
|
+
_PEEL_NUM = 500_000
|
|
26
|
+
_PEEL_CAP = 24
|
|
2
27
|
|
|
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
28
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
k = b + 1 (used only for display via graded_to_notation).
|
|
11
|
-
"""
|
|
29
|
+
class GradedSparseStandard:
|
|
30
|
+
"""Arbitrary-width sparse (graded-form) standard Cayley-Dickson multiplier."""
|
|
12
31
|
|
|
13
|
-
# ---------- representation
|
|
32
|
+
# ---------- representation ----------
|
|
14
33
|
@staticmethod
|
|
15
34
|
def int_to_graded(m):
|
|
16
|
-
"""Python int -> sorted tuple of 0-indexed set-bit positions.
|
|
35
|
+
"""Python int -> sorted tuple of 0-indexed set-bit positions."""
|
|
17
36
|
m = int(m)
|
|
37
|
+
if m == 0:
|
|
38
|
+
return ()
|
|
18
39
|
pos = []
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
40
|
+
budget = max(1, min(_PEEL_CAP, _PEEL_NUM // max(1, m.bit_length())))
|
|
41
|
+
for _ in range(budget):
|
|
42
|
+
if not m:
|
|
43
|
+
return tuple(pos)
|
|
44
|
+
lo = m & -m
|
|
45
|
+
pos.append(lo.bit_length() - 1)
|
|
46
|
+
m ^= lo
|
|
47
|
+
if not m:
|
|
48
|
+
return tuple(pos)
|
|
49
|
+
# dense remainder: C-level scan, then per-nonzero-byte extraction
|
|
50
|
+
data = m.to_bytes((m.bit_length() + 7) // 8, 'little')
|
|
51
|
+
arr = np.frombuffer(data, dtype=np.uint8)
|
|
52
|
+
for bi in np.flatnonzero(arr):
|
|
53
|
+
v = int(arr[bi])
|
|
54
|
+
base = int(bi) * 8
|
|
55
|
+
while v:
|
|
56
|
+
lo = v & -v
|
|
57
|
+
pos.append(base + lo.bit_length() - 1)
|
|
58
|
+
v ^= lo
|
|
59
|
+
return tuple(pos)
|
|
24
60
|
|
|
25
61
|
@staticmethod
|
|
26
62
|
def graded_to_int(pos):
|
|
27
|
-
"""Sorted tuple of bit positions -> Python int.
|
|
63
|
+
"""Sorted tuple of bit positions -> Python int."""
|
|
28
64
|
m = 0
|
|
29
65
|
for b in pos:
|
|
30
|
-
m |=
|
|
66
|
+
m |= 1 << b
|
|
31
67
|
return m
|
|
32
68
|
|
|
33
69
|
@staticmethod
|
|
34
70
|
def graded_to_notation(pos):
|
|
35
|
-
"""Graded form -> Wilmot string 'o{k1},{k2},...' (1-indexed)."""
|
|
71
|
+
"""Graded form -> Wilmot string 'o{k1},{k2},...' (1-indexed levels)."""
|
|
36
72
|
return "1" if not pos else "o" + ",".join(str(b + 1) for b in pos)
|
|
37
73
|
|
|
38
|
-
# ----------
|
|
74
|
+
# ---------- primitives (kept for reference / external use) ----------
|
|
39
75
|
@staticmethod
|
|
40
76
|
def _nu2_gr(pos):
|
|
41
|
-
"""nu2 = lowest set bit = first position. Requires pos non-empty."""
|
|
42
77
|
return pos[0]
|
|
43
78
|
|
|
44
79
|
@staticmethod
|
|
45
80
|
def _bit_gr(pos, t):
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return 1 if (idx < len(pos) and pos[idx] == t) else 0
|
|
81
|
+
q = bisect.bisect_left(pos, t)
|
|
82
|
+
return 1 if (q < len(pos) and pos[q] == t) else 0
|
|
49
83
|
|
|
50
84
|
@staticmethod
|
|
51
85
|
def _nu2_xor_gr(pos_i, pos_j):
|
|
52
|
-
"""Lowest bit of i^j = lowest position in the symmetric difference."""
|
|
53
86
|
a = b = 0
|
|
54
87
|
while a < len(pos_i) and b < len(pos_j):
|
|
55
|
-
if pos_i[a] < pos_j[b]:
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
88
|
+
if pos_i[a] < pos_j[b]:
|
|
89
|
+
return pos_i[a]
|
|
90
|
+
if pos_j[b] < pos_i[a]:
|
|
91
|
+
return pos_j[b]
|
|
92
|
+
a += 1; b += 1
|
|
93
|
+
if a < len(pos_i):
|
|
94
|
+
return pos_i[a]
|
|
95
|
+
if b < len(pos_j):
|
|
96
|
+
return pos_j[b]
|
|
97
|
+
return None
|
|
61
98
|
|
|
62
99
|
@staticmethod
|
|
63
100
|
def _xor_gr(pos_i, pos_j):
|
|
64
|
-
"""Full symmetric difference = graded form of i^j."""
|
|
65
101
|
out = []; a = b = 0
|
|
66
102
|
while a < len(pos_i) and b < len(pos_j):
|
|
67
|
-
if pos_i[a] < pos_j[b]:
|
|
68
|
-
|
|
69
|
-
|
|
103
|
+
if pos_i[a] < pos_j[b]:
|
|
104
|
+
out.append(pos_i[a]); a += 1
|
|
105
|
+
elif pos_j[b] < pos_i[a]:
|
|
106
|
+
out.append(pos_j[b]); b += 1
|
|
107
|
+
else:
|
|
108
|
+
a += 1; b += 1
|
|
70
109
|
out.extend(pos_i[a:]); out.extend(pos_j[b:])
|
|
71
110
|
return tuple(out)
|
|
72
111
|
|
|
73
112
|
@staticmethod
|
|
74
113
|
def _pop_or_shift_gr(pos_i, pos_j, k):
|
|
75
|
-
|
|
76
|
-
count = 0; a = b = 0
|
|
114
|
+
cnt = 0; a = b = 0
|
|
77
115
|
while a < len(pos_i) and b < len(pos_j):
|
|
78
116
|
if pos_i[a] < pos_j[b]:
|
|
79
|
-
|
|
80
|
-
a += 1
|
|
117
|
+
cnt += (pos_i[a] >= k); a += 1
|
|
81
118
|
elif pos_j[b] < pos_i[a]:
|
|
82
|
-
|
|
83
|
-
b += 1
|
|
119
|
+
cnt += (pos_j[b] >= k); b += 1
|
|
84
120
|
else:
|
|
85
|
-
|
|
86
|
-
a += 1; b += 1
|
|
121
|
+
cnt += (pos_i[a] >= k); a += 1; b += 1
|
|
87
122
|
while a < len(pos_i):
|
|
88
|
-
|
|
89
|
-
a += 1
|
|
123
|
+
cnt += (pos_i[a] >= k); a += 1
|
|
90
124
|
while b < len(pos_j):
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
return count
|
|
125
|
+
cnt += (pos_j[b] >= k); b += 1
|
|
126
|
+
return cnt
|
|
94
127
|
|
|
95
|
-
# ----------
|
|
128
|
+
# ---------- fused evaluator ----------
|
|
96
129
|
def multiply_graded(self, pos_i, pos_j):
|
|
97
|
-
"""
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
130
|
+
"""
|
|
131
|
+
Core O(p) evaluator on graded forms, single fused merge.
|
|
132
|
+
|
|
133
|
+
Returns
|
|
134
|
+
-------
|
|
135
|
+
(sign, result_pos) : int in {-1,+1}, sorted tuple (graded form of i ^ j)
|
|
136
|
+
"""
|
|
137
|
+
ni, nj = len(pos_i), len(pos_j)
|
|
138
|
+
if ni == 0:
|
|
139
|
+
return (1, pos_j) # e_0 * e_j = e_j
|
|
140
|
+
if nj == 0:
|
|
141
|
+
return (1, pos_i)
|
|
142
|
+
if pos_i == pos_j:
|
|
143
|
+
return (-1, ()) # e_i^2 = -e_0 (i > 0)
|
|
144
|
+
|
|
145
|
+
ti, tj = pos_i[0], pos_j[0]
|
|
146
|
+
a = b = 0
|
|
147
|
+
# Common prefix: all these positions are shared and < t < k, so they
|
|
148
|
+
# contribute nothing to the union count. Skipping them yields t at once
|
|
149
|
+
# (zero iterations when ti != tj, since then t = min(ti, tj)).
|
|
150
|
+
while a < ni and b < nj and pos_i[a] == pos_j[b]:
|
|
151
|
+
a += 1; b += 1
|
|
152
|
+
if a < ni and b < nj:
|
|
153
|
+
if pos_i[a] < pos_j[b]:
|
|
154
|
+
t, t_from_i = pos_i[a], 1
|
|
155
|
+
else:
|
|
156
|
+
t, t_from_i = pos_j[b], 0
|
|
157
|
+
elif a < ni:
|
|
158
|
+
t, t_from_i = pos_i[a], 1
|
|
159
|
+
else:
|
|
160
|
+
t, t_from_i = pos_j[b], 0
|
|
161
|
+
|
|
162
|
+
k = (t if t > ti else ti)
|
|
163
|
+
k = (k if k > tj else tj)
|
|
164
|
+
k += 1
|
|
165
|
+
|
|
166
|
+
out = []
|
|
167
|
+
cnt = 0
|
|
168
|
+
while a < ni and b < nj:
|
|
169
|
+
x, y = pos_i[a], pos_j[b]
|
|
170
|
+
if x < y:
|
|
171
|
+
out.append(x); cnt += (x >= k); a += 1
|
|
172
|
+
elif y < x:
|
|
173
|
+
out.append(y); cnt += (y >= k); b += 1
|
|
174
|
+
else:
|
|
175
|
+
cnt += (x >= k); a += 1; b += 1 # union: count once
|
|
176
|
+
while a < ni:
|
|
177
|
+
x = pos_i[a]; out.append(x); cnt += (x >= k); a += 1
|
|
178
|
+
while b < nj:
|
|
179
|
+
y = pos_j[b]; out.append(y); cnt += (y >= k); b += 1
|
|
180
|
+
|
|
181
|
+
if t >= ti and t >= tj:
|
|
182
|
+
s = 2 * t_from_i - 1 # bit known from the merge
|
|
183
|
+
elif ti >= tj:
|
|
184
|
+
s = 2 * self._bit_gr(pos_j, ti) - 1 # delta == -1 (collapsed)
|
|
185
|
+
else:
|
|
115
186
|
s = 1 - 2 * self._bit_gr(pos_i, tj)
|
|
116
187
|
|
|
117
|
-
if
|
|
188
|
+
if cnt & 1:
|
|
118
189
|
s = -s
|
|
119
|
-
return (s,
|
|
190
|
+
return (s, tuple(out))
|
|
120
191
|
|
|
192
|
+
# ---------- convenience ----------
|
|
121
193
|
def multiply_indices(self, i, j):
|
|
122
|
-
"""
|
|
123
|
-
sign,
|
|
124
|
-
|
|
125
|
-
|
|
194
|
+
"""
|
|
195
|
+
Python ints in -> (sign, i ^ j) out. Mirrors FastStandard.
|
|
196
|
+
|
|
197
|
+
NOTE: this wrapper pays O(p * B/w) for the int -> graded conversion.
|
|
198
|
+
The O(p) claim applies to multiply_graded on graded-form inputs.
|
|
199
|
+
"""
|
|
200
|
+
sign, pos = self.multiply_graded(self.int_to_graded(i), self.int_to_graded(j))
|
|
201
|
+
return (sign, self.graded_to_int(pos))
|
|
@@ -1,118 +1,136 @@
|
|
|
1
|
+
"""
|
|
2
|
+
VectorizedStandard — uint64 SIMD lane-wise O(1) Cayley-Dickson evaluator.
|
|
3
|
+
|
|
4
|
+
Optimizations vs. the first release (all verified against FastStandard):
|
|
5
|
+
* single `idx = i ^ j`, reused for the trailing-zero count (was 3 XOR passes)
|
|
6
|
+
* nu2 guards zero internally, so the *_safe temporaries are gone
|
|
7
|
+
* i-axis branch collapsed to 2*b - 1 (Corollary cor:iaxis-collapse):
|
|
8
|
+
removes the variable shift `1 << ti`, the `j_loc` AND, and two np.where
|
|
9
|
+
* explicit wide-shift guard for k == 64 (see _GUARD_WIDE_SHIFT)
|
|
10
|
+
|
|
11
|
+
Measured: 71.2 -> 39.4 ns/pair (guard on, 1.81x) / 34.7 ns/pair (guard off, 2.05x).
|
|
12
|
+
"""
|
|
1
13
|
import numpy as np
|
|
2
|
-
from ..validation import Validation
|
|
3
14
|
|
|
4
|
-
|
|
15
|
+
_U64 = np.uint64
|
|
16
|
+
|
|
17
|
+
# NumPy returns 0 for `x >> width` (verified on 2.2.6 for uint64/int64/uint32/
|
|
18
|
+
# int32/uint8, both array and scalar paths). That behaviour is not formally
|
|
19
|
+
# documented, and `>> 64` on a 64-bit word is UB in C/C++. The guard makes the
|
|
20
|
+
# k == 64 lane explicit and portable; it costs ~4.7 ns/pair. Set False to skip.
|
|
21
|
+
_GUARD_WIDE_SHIFT = True
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _get_np_popcount():
|
|
25
|
+
"""Hardware population count, with a SWAR fallback for NumPy < 2.0."""
|
|
26
|
+
try:
|
|
27
|
+
return np.bitwise_count
|
|
28
|
+
except AttributeError:
|
|
29
|
+
def _swar(x):
|
|
30
|
+
x = np.asarray(x, dtype=np.uint64)
|
|
31
|
+
x = x - ((x >> _U64(1)) & _U64(0x5555555555555555))
|
|
32
|
+
x = (x & _U64(0x3333333333333333)) + ((x >> _U64(2)) & _U64(0x3333333333333333))
|
|
33
|
+
x = (x + (x >> _U64(4))) & _U64(0x0F0F0F0F0F0F0F0F)
|
|
34
|
+
return ((x * _U64(0x0101010101010101)) >> _U64(56)).astype(np.uint8)
|
|
35
|
+
return _swar
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
_popcount = _get_np_popcount()
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _nu2_vec(x):
|
|
5
42
|
"""
|
|
6
|
-
|
|
7
|
-
|
|
43
|
+
Lane-wise nu2 via popcount((x & -x) - 1).
|
|
44
|
+
x == 0 maps to 0; those lanes are overridden by mask_zero downstream.
|
|
8
45
|
"""
|
|
46
|
+
xs = np.where(x == 0, _U64(1), x)
|
|
47
|
+
return _popcount((xs & -xs) - _U64(1)).astype(_U64)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class VectorizedStandard:
|
|
51
|
+
"""Standard Cayley-Dickson O(1) multiplier on uint64 SIMD lanes (indices <= 64 bits)."""
|
|
9
52
|
|
|
10
53
|
@staticmethod
|
|
11
54
|
def _get_np_popcount():
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
except AttributeError:
|
|
16
|
-
def popcount_vec(x):
|
|
17
|
-
x = np.asarray(x, dtype=np.uint64)
|
|
18
|
-
x = x - ((x >> 1) & np.uint64(0x5555555555555555))
|
|
19
|
-
x = (x & np.uint64(0x3333333333333333)) + ((x >> 2) & np.uint64(0x3333333333333333))
|
|
20
|
-
x = (x + (x >> 4)) & np.uint64(0x0F0F0F0F0F0F0F0F)
|
|
21
|
-
return ((x * np.uint64(0x0101010101010101)) >> np.uint64(56)).astype(np.uint8)
|
|
22
|
-
return popcount_vec
|
|
23
|
-
|
|
24
|
-
def multiply_indices_vec(self, i_arr: np.ndarray, j_arr: np.ndarray) -> tuple:
|
|
55
|
+
return _popcount
|
|
56
|
+
|
|
57
|
+
def multiply_indices_vec(self, i_arr, j_arr):
|
|
25
58
|
"""
|
|
26
|
-
|
|
27
|
-
|
|
59
|
+
Lane-wise O(1) evaluation of Definition def:o1.
|
|
60
|
+
|
|
61
|
+
Returns
|
|
62
|
+
-------
|
|
63
|
+
(signs, idx) : (N,) int8 in {-1,+1}, (N,) uint64 with idx = i ^ j
|
|
28
64
|
"""
|
|
29
|
-
_popcount = self._get_np_popcount()
|
|
30
65
|
i = np.asarray(i_arr, dtype=np.uint64)
|
|
31
66
|
j = np.asarray(j_arr, dtype=np.uint64)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
idx = i ^ j
|
|
35
|
-
|
|
36
|
-
# Masks for structural cases
|
|
67
|
+
|
|
68
|
+
idx = i ^ j # single XOR, reused below
|
|
37
69
|
mask_zero = (i == 0) | (j == 0)
|
|
38
70
|
mask_diag = (i == j) & ~mask_zero
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
#
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
# Branch 2: i-axis break (ti > t and ti >= tj)
|
|
55
|
-
cond2 = (~cond1) & (ti >= tj)
|
|
56
|
-
b = (j >> ti) & np.uint64(1)
|
|
57
|
-
j_loc = j & ((np.uint64(1) << ti) - np.uint64(1))
|
|
58
|
-
delta = np.where(j_loc == 0, 1, -1).astype(np.int8)
|
|
59
|
-
s2 = np.where(b == 0, delta, -delta)
|
|
60
|
-
|
|
61
|
-
# Branch 3: j-axis break (tj > t and tj > ti)
|
|
62
|
-
cond3 = ~cond1 & ~cond2
|
|
63
|
-
s3 = 1 - 2 * ((i >> tj) & np.uint64(1)).astype(np.int8)
|
|
64
|
-
|
|
65
|
-
# Combine branches
|
|
71
|
+
|
|
72
|
+
t = _nu2_vec(idx)
|
|
73
|
+
ti = _nu2_vec(i)
|
|
74
|
+
tj = _nu2_vec(j)
|
|
75
|
+
k = np.maximum(np.maximum(t, ti), tj) + _U64(1)
|
|
76
|
+
|
|
77
|
+
cond1 = (t >= ti) & (t >= tj) # diagonal break
|
|
78
|
+
cond2 = (~cond1) & (ti >= tj) # i-axis break
|
|
79
|
+
|
|
80
|
+
s1 = 2 * ((i >> t) & _U64(1)).astype(np.int8) - 1
|
|
81
|
+
# cond2 forces ti > tj strictly (ti == tj would make cond1 fire), so
|
|
82
|
+
# j_loc > 0 and delta == -1 unconditionally: s2 = 2*b - 1.
|
|
83
|
+
s2 = 2 * ((j >> ti) & _U64(1)).astype(np.int8) - 1
|
|
84
|
+
s3 = 1 - 2 * ((i >> tj) & _U64(1)).astype(np.int8)
|
|
85
|
+
|
|
66
86
|
s = np.where(cond1, s1, np.where(cond2, s2, s3))
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
87
|
+
|
|
88
|
+
ij_or = i | j
|
|
89
|
+
if _GUARD_WIDE_SHIFT:
|
|
90
|
+
# k == 64 means no bits above the word: parity must be 0.
|
|
91
|
+
parity = np.where(
|
|
92
|
+
k < _U64(64),
|
|
93
|
+
_popcount(ij_or >> np.minimum(k, _U64(63))) & np.uint8(1),
|
|
94
|
+
np.uint8(0),
|
|
95
|
+
)
|
|
96
|
+
else:
|
|
97
|
+
parity = _popcount(ij_or >> k) & np.uint8(1)
|
|
98
|
+
|
|
71
99
|
s = np.where(parity == 1, -s, s)
|
|
72
|
-
|
|
73
|
-
# Apply structural overrides
|
|
74
100
|
s = np.where(mask_zero, 1, s)
|
|
75
101
|
s = np.where(mask_diag, -1, s)
|
|
76
|
-
|
|
77
102
|
return s.astype(np.int8), idx
|
|
78
103
|
|
|
79
|
-
def multiply_arrays(self, arr1
|
|
104
|
+
def multiply_arrays(self, arr1, arr2):
|
|
80
105
|
"""
|
|
81
|
-
|
|
82
|
-
|
|
106
|
+
Sparse twisted convolution of two real coefficient arrays
|
|
107
|
+
(Eq. eq:sparse-conv): support extraction -> active grid -> lane-wise
|
|
108
|
+
signs -> coefficient weighting -> scatter-add.
|
|
109
|
+
|
|
110
|
+
NOTE: only the sign kernel above was optimized; this combiner is
|
|
111
|
+
unchanged. The real-part factorization x0*y0 + x0*yt + y0*xt is a
|
|
112
|
+
separate future optimization.
|
|
83
113
|
"""
|
|
84
|
-
|
|
85
|
-
|
|
114
|
+
arr1 = np.asarray(arr1, dtype=np.float64)
|
|
115
|
+
arr2 = np.asarray(arr2, dtype=np.float64)
|
|
116
|
+
size1, size2 = len(arr1), len(arr2)
|
|
86
117
|
max_size = max(size1, size2)
|
|
87
|
-
|
|
88
118
|
if max_size <= 1:
|
|
89
|
-
|
|
119
|
+
if size1 and size2:
|
|
120
|
+
return np.array([arr1[0] * arr2[0]], dtype=np.float64)
|
|
121
|
+
return np.zeros(1, dtype=np.float64)
|
|
90
122
|
|
|
91
|
-
# Calculate required power-of-two dimension
|
|
92
|
-
# Equivalent to 1 << math.ceil(math.log2(max_size))
|
|
93
123
|
dim = 1 << (max_size - 1).bit_length()
|
|
94
|
-
|
|
95
|
-
# 1. SPARSE OPTIMIZATION: Isolate non-zero elements
|
|
96
124
|
nz_A = np.nonzero(arr1)[0]
|
|
97
125
|
nz_B = np.nonzero(arr2)[0]
|
|
98
|
-
|
|
99
126
|
if len(nz_A) == 0 or len(nz_B) == 0:
|
|
100
127
|
return np.zeros(dim, dtype=np.float64)
|
|
101
128
|
|
|
102
|
-
# 2. Create the 2D Grid of ONLY the active indices
|
|
103
129
|
I, J = np.meshgrid(nz_A, nz_B, indexing='ij')
|
|
104
|
-
|
|
105
|
-
# 3. Calculate the entire Sign and Index Matrix in one C-level SIMD sweep
|
|
106
130
|
S, V = self.multiply_indices_vec(I, J)
|
|
107
|
-
|
|
108
|
-
# 4. Calculate the Outer Product of ONLY the active coefficients
|
|
109
131
|
M = np.outer(arr1[nz_A], arr2[nz_B])
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
weighted_M = S.astype(np.float64) * M
|
|
113
|
-
|
|
114
|
-
# 6. C-Level Scatter-Add into the Result Array
|
|
132
|
+
weighted = S.astype(np.float64) * M
|
|
133
|
+
|
|
115
134
|
result = np.zeros(dim, dtype=np.float64)
|
|
116
|
-
np.add.at(result, V.ravel(),
|
|
117
|
-
|
|
135
|
+
np.add.at(result, V.ravel(), weighted.ravel()) # unbuffered scatter-add
|
|
118
136
|
return result
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: hypercomplex-engine
|
|
3
|
-
Version: 0.7.
|
|
3
|
+
Version: 0.7.2
|
|
4
4
|
Summary: Fast O(1)/O(n) multiplication engine for arbitrary dimensions and table generator for ordinary, split, and dual Cayley-Dickson algebras.
|
|
5
5
|
Author-email: Maher Ben Abdessalem <ba.maher94@gmail.com>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -73,7 +73,7 @@ If you build upon this package, please read fully [Go to Scope, Strengths, and L
|
|
|
73
73
|
This library serves as the formal verification substrate and computational engine for the following mathematical preprints:
|
|
74
74
|
|
|
75
75
|
**1. The Sign Structure of Cayley–Dickson and Split Algebras By Blocks**
|
|
76
|
-
*Proves the OPMT (
|
|
76
|
+
*Proves the OPMT (Observed Patterns in Multiplication Table) sign laws, block decomposition, and the O(1) closed-form sign evaluator implemented in the `fast` engine of this library.*
|
|
77
77
|
* 📊 **Figshare:** [10.6084/m9.figshare.33705022](https://doi.org/10.6084/m9.figshare.33705022)
|
|
78
78
|
* 📦 **Zenodo:** [10.5281/zenodo.22051873](https://doi.org/10.5281/zenodo.22051873)
|
|
79
79
|
|
|
@@ -90,6 +90,54 @@ This library serves as the formal verification substrate and computational engin
|
|
|
90
90
|
|
|
91
91
|
---
|
|
92
92
|
|
|
93
|
+
## Flowchart of the applied Construction
|
|
94
|
+
|
|
95
|
+
<p align="center">
|
|
96
|
+
<img src="https://raw.githubusercontent.com/maher1719/hypercomplex-engine/refs/heads/main/hypercomplex_engine/asset/flowchart_github_version.jpeg" alt="Flowchart of the construction" width="100%">
|
|
97
|
+
</p>
|
|
98
|
+
|
|
99
|
+
<details>
|
|
100
|
+
<summary>📊 <b>Click to view the Construction Flowchart (Vector SVG)</b></summary>
|
|
101
|
+
<br>
|
|
102
|
+
|
|
103
|
+
```mermaid
|
|
104
|
+
|
|
105
|
+
flowchart TD
|
|
106
|
+
Start([Input: Target N ≥ 0]) --> Init["A₀ Real Numbers (n = 0)"]
|
|
107
|
+
Init --> CheckN0{"Target N = 0?"}
|
|
108
|
+
|
|
109
|
+
CheckN0 -- Yes --> AddEps0{"Add ε?"}
|
|
110
|
+
CheckN0 -- No --> AddL["Add new imaginary unit 'l'"]
|
|
111
|
+
|
|
112
|
+
AddEps0 -- Yes --> DualReal["Dual Real Numbers ℝ[ε]"]
|
|
113
|
+
AddEps0 -- No --> MergeTop(( ))
|
|
114
|
+
DualReal --> MergeTop
|
|
115
|
+
MergeTop --> EndTop([End])
|
|
116
|
+
|
|
117
|
+
AddL --> IsSplit{"Is it split?"}
|
|
118
|
+
IsSplit -- Yes --> SplitHyp["Split Hypercomplex Aₙ₊₁<br/>(Set IsSplit = True)"]
|
|
119
|
+
IsSplit -- No --> OrdHyp["Ordinary Hypercomplex Aₙ₊₁"]
|
|
120
|
+
|
|
121
|
+
SplitHyp --> MergeMid(( ))
|
|
122
|
+
OrdHyp --> MergeMid
|
|
123
|
+
|
|
124
|
+
MergeMid --> AddEps{"Add ε?"}
|
|
125
|
+
AddEps -- Yes --> DualHyp["Dual Hypercomplex Numbers<br/>Aₙ₊₁[ε]"]
|
|
126
|
+
AddEps -- No --> CheckEnd{"IsSplit, or Reached<br/>Target Dimension (n + 1 = N)?"}
|
|
127
|
+
|
|
128
|
+
DualHyp --> MergeBot(( ))
|
|
129
|
+
CheckEnd -- Yes --> MergeBot
|
|
130
|
+
CheckEnd -- No --> IncLoop["Aₙ = Aₙ₊₁<br/>n = n + 1"]
|
|
131
|
+
IncLoop --> AddL
|
|
132
|
+
|
|
133
|
+
MergeBot --> EndBot([End])
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
</details>
|
|
139
|
+
|
|
140
|
+
|
|
93
141
|
## Installation
|
|
94
142
|
|
|
95
143
|
Clone the repository:
|
|
@@ -5,14 +5,15 @@ hypercomplex_engine/core/basis_element.py,sha256=TnPb1xLICv0TK0AfTW_I8_8T2TavktK
|
|
|
5
5
|
hypercomplex_engine/core/basis_notation.py,sha256=K17IUXEio7KmY3pvpFdDFI2MsLs11-E5Z5Cp10CS84Y,4887
|
|
6
6
|
hypercomplex_engine/core/validation.py,sha256=VThGdXzESoiOO1y6Ios-GcEyHGdbi8V9_fMmEEExkqw,3245
|
|
7
7
|
hypercomplex_engine/core/bitplane/__init__.py,sha256=YCMNGtmqMTFt8p6Kxyyh2cLSKaakmpK9SdoRRUm-hNE,84
|
|
8
|
-
hypercomplex_engine/core/bitplane/bitplane_standard.py,sha256=
|
|
8
|
+
hypercomplex_engine/core/bitplane/bitplane_standard.py,sha256=cORQzEc8784qqUk9_ZCmCFmIGIB24qJFDqU8rMEF2UM,4970
|
|
9
9
|
hypercomplex_engine/core/fast/__init__.py,sha256=6KsWo1dhNlT69jT_Bi8-khOrNFX9cDs98987TGTXo4w,173
|
|
10
10
|
hypercomplex_engine/core/fast/bit_utils.py,sha256=U8jmGRKB8tymSyyhCMMwEL5Ap2vUfrZTDyrTutVrB8k,448
|
|
11
11
|
hypercomplex_engine/core/fast/fast_dual.py,sha256=zQSMvXCNQgI_ZhmNEkds0_VbpNzFxS0476NfH2Qyfro,2098
|
|
12
12
|
hypercomplex_engine/core/fast/fast_split.py,sha256=Wdr9pd9zXA_mFzlMxVv8LpPwDZJFP-XPVa0uQIEs3jA,3037
|
|
13
|
-
hypercomplex_engine/core/fast/fast_standard.py,sha256=
|
|
13
|
+
hypercomplex_engine/core/fast/fast_standard.py,sha256=DWoQERE7UXrU0mPkU4MGBLCsZ1AKvak5ASg73DAwRHg,2514
|
|
14
|
+
hypercomplex_engine/core/fast/fast_standard_old.py,sha256=JQ7Mxb4qQTH6EuWQjpTtbxwb4rVI0Iu-jggHiwmEj1I,3250
|
|
14
15
|
hypercomplex_engine/core/graded/__init__.py,sha256=sGe4qKW_1qhc2RgTY4b3LBiqKyNdr1_M_CEVqXraQAo,98
|
|
15
|
-
hypercomplex_engine/core/graded/graded_sparse_standard.py,sha256=
|
|
16
|
+
hypercomplex_engine/core/graded/graded_sparse_standard.py,sha256=0-cUi9DHg2P7To0p5OxojeT3f72gpJFFCNfTIX1i3Ek,6976
|
|
16
17
|
hypercomplex_engine/core/holographic/__init__.py,sha256=TGMyPs6dyeaQ9I--rFooBOrKzkex73IbwzQtL7Q0i04,200
|
|
17
18
|
hypercomplex_engine/core/holographic/dual.py,sha256=LyxA4zpr6fUlD-ZckLYQGH_FAu9Xf8FOGyYo6c_5kyk,1731
|
|
18
19
|
hypercomplex_engine/core/holographic/split.py,sha256=kJXPXvw3rT8YieENor0hNTko4qBoXaKKb9n07UWhfQY,2864
|
|
@@ -23,13 +24,13 @@ hypercomplex_engine/core/table_builder/dual.py,sha256=9flbYGsCnZeKMirS3JlDg__EqS
|
|
|
23
24
|
hypercomplex_engine/core/table_builder/split.py,sha256=OUBNKT_-L6BMlCxiSYWzAOGSOdYGJZzLOR0Zo6R5Ubw,2923
|
|
24
25
|
hypercomplex_engine/core/table_builder/standard.py,sha256=3kyg4TLCG_LKhC8lT95hZBFmOee0Wg39T7CDIhayVUs,2820
|
|
25
26
|
hypercomplex_engine/core/vectorized/__init__.py,sha256=NfwXNqe9n8acZaMTquuXH5BLB-fbmCUDvKAB5rcWrOE,92
|
|
26
|
-
hypercomplex_engine/core/vectorized/vectorized_standard.py,sha256=
|
|
27
|
+
hypercomplex_engine/core/vectorized/vectorized_standard.py,sha256=tGEvhkTmdq3XfVKcBtyJLg7WcEGuqtSKJevsdvQ9qOs,5069
|
|
27
28
|
hypercomplex_engine/printer/__init__.py,sha256=6EZOUBYb9TDgfqrFpnIfPS1bbPFZ0dMX6RCPDZepL6M,129
|
|
28
29
|
hypercomplex_engine/printer/cd_format.py,sha256=X_cotGumJlRMuJO5cbsnI8FA9Py12TGYftPlUdx-9xg,5731
|
|
29
30
|
hypercomplex_engine/printer/cd_table_printer.py,sha256=Ny1g5osDwttI5I8-KV4DzhVhWp_7fP-R5lYyC9sEhKE,4973
|
|
30
|
-
hypercomplex_engine-0.7.
|
|
31
|
-
hypercomplex_engine-0.7.
|
|
32
|
-
hypercomplex_engine-0.7.
|
|
33
|
-
hypercomplex_engine-0.7.
|
|
34
|
-
hypercomplex_engine-0.7.
|
|
35
|
-
hypercomplex_engine-0.7.
|
|
31
|
+
hypercomplex_engine-0.7.2.dist-info/licenses/LICENSE,sha256=WNHhf_5RCaeuKWyq_K39vmp9F28LxKsB4SpomwSZ2L0,11357
|
|
32
|
+
hypercomplex_engine-0.7.2.dist-info/licenses/NOTICE,sha256=WRgJ9Hv74Vke3ByFbvR6sqdlinPx4rCWDc0-7Iyh9VI,745
|
|
33
|
+
hypercomplex_engine-0.7.2.dist-info/METADATA,sha256=AxkP5vgElnKF5LJOMeDNZtFLlHpd25J85fAwm6A-NFI,30452
|
|
34
|
+
hypercomplex_engine-0.7.2.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
35
|
+
hypercomplex_engine-0.7.2.dist-info/top_level.txt,sha256=654ACRoo9vk38vKW7kVBfgD-IjIADgVmh75smmXBZ88,20
|
|
36
|
+
hypercomplex_engine-0.7.2.dist-info/RECORD,,
|
|
File without changes
|
{hypercomplex_engine-0.7.1.2.dist-info → hypercomplex_engine-0.7.2.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|
{hypercomplex_engine-0.7.1.2.dist-info → hypercomplex_engine-0.7.2.dist-info}/licenses/NOTICE
RENAMED
|
File without changes
|
|
File without changes
|