# X4-DMSD P2：Shared-Edge Scale Coupling Engine

**Exact Shared-Edge Scale Coupling Engine for Indexed X4 Joint Search**

版本：v0.1  
系列：X4-DMSD  
階段：P2  
性質：Exact join engine／ratio indexing／bounded benchmark／Euler-brick regeneration  
Canonical source：UTF-8 Markdown

---

## 摘要

X4-DMSD P1 將三個 oriented primitive Pythagorean face joints 的 global compatibility壓成：

$$
ACB'=BC'A'.
$$

P2 的目標是把這條 identity從「判定公式」編譯成高效 exact join engine。

naive 方法若有：

$$
N
$$

個 oriented joints，會枚舉：

$$
N^3
$$

個：

$$
(J_{ab},J_{ac},J_{bc})
$$

組合。

P2 重新排列 coupling identity：

$$
ACB'=BC'A'
$$

為：

$$
\boxed{
\frac{B'}{C'}
=
\frac{BA'}{AC}.
}
$$

因此，每個 $J_{bc}$ 可以先以 exact reduced ratio：

$$
R_{bc}
=
\frac{B'}{C'}
$$

建立 hash index。

之後每一對：

$$
(J_{ab},J_{ac})
$$

只需計算 required key：

$$
\boxed{
R_{\mathrm{req}}
=
\frac{BA'}{AC}
}
$$

並直接查表。

所以顯式搜尋工作從：

$$
O(N^3)
$$

改為：

$$
\boxed{
O(N^2+\text{hash matches})
}
$$

在目前 implementation model 下。

P2 使用 primitive Euclid seeds：

$$
m\le80
$$

且保留兩種 ordered leg orientations，實際生成：

$$
2628
$$

個 X4 joints。

naive oriented triple space大小：

$$
\boxed{
18,149,977,152.
}
$$

P2 engine 實際只發出：

$$
\boxed{
6,906,384
}
$$

個 ordered pair queries，並經 exact ratio lookup只得到：

$$
\boxed{
288
}
$$

個 raw compatible oriented triples。

這些 canonicalize成：

$$
\boxed{
48
}
$$

個 unique Euler-brick edge worlds。

其中沒有 PerfectClick hit，這只是一個 bounded result。

P2 同時在 $m\le15$ 的 tractable range 中執行完整 $N^3$ brute-force cross-check，證明 indexed engine 的 raw compatible triple count與 unique Euler-brick set完全一致。

因此本輪已驗證：

$$
\boxed{
\text{ratio indexing加速沒有靠漏掉 compatible triples。}
}
$$

---

# 1. P1 Coupling Identity

三 joints：

$$
J_{ab}=(A,B,H_{ab}),
$$

$$
J_{ac}=(A',C,H_{ac}),
$$

$$
J_{bc}=(B',C',H_{bc}).
$$

compatible iff：

$$
\boxed{
ACB'=BC'A'.
}
$$

---

# 2. Solve for the Missing Joint Ratio

假設已知：

$$
J_{ab},
J_{ac}.
$$

則：

$$
ACB'=BC'A'
$$

可改寫：

$$
\boxed{
\frac{B'}{C'}
=
\frac{BA'}{AC}.
}
$$

右邊完全由前兩個 joints決定。

所以第三個 joint不需要 enumerate。

只需：

$$
\boxed{
\operatorname{lookup}
\left(
\frac{BA'}{AC}
\right).
}
$$

---

# 3. Exact Ratio Key

定義：

$$
K_{bc}(J)
=
\operatorname{Reduce}
(B',C').
$$

也就是：

$$
\frac{B'}{C'}
$$

的 reduced rational key。

implementation 可以使用 canonical pair：

$$
\left(
\frac{B'}{\gcd(B',C')},
\frac{C'}{\gcd(B',C')}
\right).
$$

對 primitive Pythagorean joints：

$$
\gcd(B',C')=1,
$$

但仍保留 general reduction semantics。

---

# 4. Required Ratio Key

給 pair：

$$
(J_{ab},J_{ac}),
$$

定義：

$$
\boxed{
K_{\mathrm{req}}
=
\operatorname{Reduce}
(
BA',
AC
).
}
$$

若 index 中沒有：

$$
K_{\mathrm{req}},
$$

整個：

$$
J_{bc}
$$

候補域立即為空。

---

# 5. 命題 P2-A：Indexed Join Completeness

若 ratio index包含所有 candidate：

$$
J_{bc},
$$

則：

$$
K_{bc}
=
K_{\mathrm{req}}
$$

iff：

$$
ACB'=BC'A'.
$$

所以 exact ratio hash lookup對 P1 coupling relation是 sound 且 complete。

---

# 6. 證明

$$
K_{bc}=K_{\mathrm{req}}
$$

等價於：

$$
\frac{B'}{C'}
=
\frac{BA'}{AC}.
$$

cross-multiply：

$$
ACB'=BC'A'.
$$

反方向同理。

因此 lookup沒有 heuristic loss。

---

# 7. Join Engine

P2 engine：

```text
index = map ratio(B',C') -> list[J_bc]

for J_ab in joints:
    for J_ac in joints:
        required = reduce(B*A', A*C)
        for J_bc in index[required]:
            emit exact compatible triple
```

---

# 8. Complexity

建立 index：

$$
O(N).
$$

pair emissions：

$$
N^2.
$$

若 total hash matches為：

$$
M,
$$

則：

$$
\boxed{
T_{\mathrm{join}}
=
O(N^2+M).
}
$$

相較 naive：

$$
O(N^3).
$$

這是 algorithmic work reduction，不代表完整 Perfect Cuboid search已具有相同 asymptotic bound；後續 DMSD、height growth與多層 indexing仍需另外分析。

---

# 9. Why Multiple Matches May Exist

若不同 local joints具有同一：

$$
B'/C'
$$

ratio，

同一 hash bucket可包含多個 states。

因此不能只存單一 joint。

index value必須是：

$$
\boxed{
\text{list / bucket}.
}
$$

---

# 10. Bucket Semantics

$$
I_R
:
R
\mapsto
\{J_1,\ldots,J_k\}.
$$

這讓 scale duplicates、不同 seed representations或未来 block summaries都能掛在同一 ratio class。

---

# 11. Role-Aware Index

不能只存 unlabelled triangle ratio。

對：

$$
J_{bc},
$$

key必須知道：

- numerator是 $b$-role；
- denominator是 $c$-role。

如果交換：

$$
B'\leftrightarrow C',
$$

key變成 reciprocal。

所以 P2 index是 oriented。

---

# 12. Why Orientation Is Necessary

P1 已用 Euler-brick role-swap negative control驗證：

local Pythagorean validity在 role swap後仍成立，

但 global coupling可能失敗。

所以：

$$
\boxed{
\text{shape key}
\neq
\text{role-aware coupling key}.
}
$$

---

# 13. Pair-First Join

P2 選：

$$
(J_{ab},J_{ac})
$$

作 pair emitter。

但 identity也可以解成：

$$
\frac{A'}{C}
=
\frac{AB'}{BC'},
$$

或其他等價形式。

所以 engine可動態選：

$$
\boxed{
\text{最小 bucket / 最稀疏 key orientation}.
}
$$

---

# 14. Adaptive Join Ordering

未來可估計三種 index的 bucket distribution：

- $A'/C$；
- $B'/C'$；
- $A/B$。

選擇 expected match count最小的方向。

這和 database query optimizer類似，但 equality semantics仍 exact。

---

# 15. Prime-Valuation Key

coupling identity：

$$
ACB'=BC'A'
$$

對 prime：

$$
p
$$

給：

$$
v_p(A)
+
v_p(C)
+
v_p(B')
=
v_p(B)
+
v_p(C')
+
v_p(A').
$$

因此除了 full rational ratio key，還可以保存：

$$
\boxed{
V_{\mathcal P}(J)
}
$$

作 coarse pre-index。

---

# 16. Modular Key

對 modulus：

$$
M,
$$

必要：

$$
ACB'
\equiv
BC'A'
\pmod M.
$$

可以在 expensive big-integer Fraction construction前先做 cheap modular screen。

但 P2 prototype目前直接使用 exact rational key，優先驗證 correctness。

---

# 17. Euler-Brick Regeneration

當 triple match後，

P1 explicit scales：

$$
k_{ab}=A'B',
$$

$$
k_{ac}=AB',
$$

$$
k_{bc}=BA'
$$

直接建立 raw Euler brick。

之後除：

$$
G
=
\gcd
(
a,b,c,d_{ab},d_{ac},d_{bc}
).
$$

得到 primitive representative。

---

# 18. Canonical World Key

P2 prototype以：

$$
\boxed{
\operatorname{sort}(a,b,c)
}
$$

作 unique Euler-brick edge key。

因為 face diagonals由 edges唯一決定。

這消除：

- local joint role permutations；
- repeated construction paths；
- common scale duplicates。

---

# 19. Body Gate

每個 unique Euler brick再 exact 檢查：

$$
a^2+b^2+c^2
\stackrel{?}{=}
g^2.
$$

只有 true 才：

$$
\operatorname{PerfectClick}=1.
$$

---

# 20. Search Layers

P2 實際 pipeline：

$$
\boxed{
\text{primitive seeds}
\rightarrow
\text{oriented joints}
\rightarrow
\text{ratio index}
\rightarrow
\text{pair emission}
\rightarrow
\text{hash lookup}
\rightarrow
\text{compatible triple}
\rightarrow
\text{Euler brick}
\rightarrow
\text{body gate}.
}
$$

---

# 21. Bounded Seed Domain

本輪定義：

$$
2\le m\le M,
$$

$$
1\le n<m,
$$

$$
\gcd(m,n)=1,
$$

且：

$$
m-n
$$

odd。

每個 primitive seed產生兩個 ordered orientations。

---

# 22. $M=80$ Joint Count

得到：

$$
\boxed{
N=2628
}
$$

個 oriented X4 joints。

---

# 23. Naive Triple Space

若真的做：

$$
N^3,
$$

則：

$$
2628^3
=
\boxed{
18,149,977,152.
}
$$

這只是 oriented local-joint combination count。

---

# 24. Indexed Pair Space

P2 只需要：

$$
N^2
=
2628^2
=
\boxed{
6,906,384.
}
$$

pair emissions。

與 naive triple count比例：

$$
\frac{N^2}{N^3}
=
\frac1N
\approx
3.805\times10^{-4}.
$$

---

# 25. Hash-Matched Compatible Triples

在：

$$
6,906,384
$$

pair queries中，exact ratio buckets總共回傳：

$$
\boxed{
288
}
$$

個 raw compatible oriented triples。

相對 naive triple space：

$$
\frac{288}{18,149,977,152}
\approx
1.5868\times10^{-8}.
$$

所以 coupling relation在此 bounded corpus中極稀疏。

---

# 26. Unique Euler Bricks

288 個 oriented construction paths canonicalize後只剩：

$$
\boxed{
48
}
$$

個 unique edge worlds。

---

# 27. Perfect Hits

本輪：

$$
\boxed{
0
}
$$

個 body-square closure。

這只對：

$$
m\le80
$$

的 local seed corpus成立。

不能外推為 Perfect Cuboid不存在。

---

# 28. Known Euler Brick Recovery

M80 corpus exact 重建：

$$
\boxed{
(44,117,240)
}
$$

以及其他 known Euler-brick worlds。

因此 indexed engine不是只產生 artificial coupling examples。

---

# 29. Brute-Force Completeness Cross-Check

為驗證 ratio index沒有漏掉 match，

P2 在：

$$
M=15
$$

另外執行完整：

$$
N^3
$$

triple enumeration。

該範圍：

$$
N=94.
$$

naive tests：

$$
94^3
=
830584.
$$

---

# 30. Cross-Check Result

naive與 indexed：

- raw compatible triple count完全相同；
- unique Euler-brick set完全相同。

所以：

$$
\boxed{
\text{P2 indexed join passes bounded completeness cross-check.}
}
$$

---

# 31. Benchmark Growth

P2 同時測：

$$
M
\in
\{15,20,30,40,60,80\}.
$$

所有詳細數據保存於：

`benchmark_summary.json`。

其目的不是建立硬體無關 performance theorem，

而是觀察：

- joint count；
- pair count；
- compatible density；
- Euler-brick yield。

---

# 32. Runtime Measurement Warning

實際秒數受：

- Python runtime；
- CPU；
- hashing；
- Fraction implementation；

影響。

所以 P2 不把 wall time寫成數學主張。

主要比較的是：

$$
N^3
$$

與：

$$
N^2+M.
$$

---

# 33. Query-Planning Interpretation

X4 coupling engine本質上可以看成 relational join：

```text
AB(A,B)
AC(A',C)
BC(B',C')
```

constraint：

```text
A*C*B' = B*C'*A'
```

P2 將它改寫成 equi-join：

```text
BC.ratio = required_ratio(AB,AC)
```

所以 X4 world search開始具備 database-like query optimization結構。

---

# 34. 為何這對 DMSD 很重要

DMSD block不需要保存所有 triples。

它可以保存：

$$
\boxed{
\text{ratio-key support}.
}
$$

若 two-block pair emitted required ratio不在第三 block的 support中，

整個 hyperblock直接 prune。

---

# 35. Block Ratio Support

對 block：

$$
B_{bc},
$$

定義：

$$
\mathcal R_{bc}(B)
=
\left\{
\frac{B'}{C'}
:
J_{bc}\in B
\right\}.
$$

pair blocks：

$$
B_{ab},
B_{ac}
$$

產生：

$$
\mathcal R_{\mathrm{req}}
=
\left\{
\frac{BA'}{AC}
\right\}.
$$

若：

$$
\mathcal R_{\mathrm{req}}
\cap
\mathcal R_{bc}
=
\varnothing,
$$

則：

$$
\boxed{
B_{ab}\bowtie B_{ac}\bowtie B_{bc}
=
\varnothing.
}
$$

---

# 36. 命題 P2-B：Ratio-Support Block Pruning

若 block summaries exact 包含所有 possible ratio keys，

且：

$$
\mathcal R_{\mathrm{req}}
\cap
\mathcal R_{bc}
=
\varnothing,
$$

則該 triple block中不存在 P1-compatible joint triple。

所以此 pruning sound。

---

# 37. Compression Challenge

若 block ratio support本身巨大，

直接列全部 ratios會失去 DMSD優勢。

所以後續需要：

- interval / Farey summaries；
- modular projections；
- valuation signatures；
- algebraic seed-range bounds；

作 compact support representation。

這是 P3–P5 的重點。

---

# 38. Pair Cache

同一：

$$
K_{\mathrm{req}}
$$

可能被很多 pair emissions重複產生。

因此可以 cache：

$$
K_{\mathrm{req}}
\mapsto
I_R[K_{\mathrm{req}}].
$$

後續 DMSD distributed runtime可共享。

---

# 39. Canonical Fraction vs Integer Pair

Python prototype使用：

$$
\operatorname{Fraction}.
$$

高效實作建議改成 reduced integer pair：

$$
(p,q),
$$

並自訂 hash，避免 generic Fraction overhead。

---

# 40. Overflow

因 X4-DMSD最終會跨巨大 height，

不能依賴 fixed-width：

$$
64\text{-bit}
$$

integer。

正式 runtime需要：

- big integer；
- modular prefilter；
- delayed exact multiplication。

---

# 41. Cross-Multiplication Overflow Avoidance

判定：

$$
ACB'=BC'A'
$$

若數字巨大，

可先：

1. reduce gcd factors；
2. compare valuations；
3. modular reject；
4. 最後 big-int exact equality。

這會成為 P3 engine optimization。

---

# 42. GCD Cancellation

required ratio：

$$
\frac{BA'}{AC}
$$

應在乘法前交叉 gcd-cancel。

例如先：

$$
g_1=\gcd(B,A),
$$

$$
g_2=\gcd(A',C),
$$

再縮減。

可顯著限制 intermediate bit-length。

---

# 43. PerfectClick Remains Exact

所有 join optimization只能改：

$$
\text{how fast candidates are reached}.
$$

不能改 final：

$$
\operatorname{PerfectClick}.
$$

最終仍需 exact body square test。

---

# 44. P2 Correctness Contract

任何 future optimization必須保證：

### Soundness

emitted triple必須真的滿足：

$$
ACB'=BC'A'.
$$

### Completeness

任何 corpus內 compatible triple必須被 emitted。

### Canonicalization Safety

scale / permutation duplicate removal不能刪除 distinct edge worlds。

---

# 45. P2 Validation Artifacts

bundle包含：

- `x4_coupling_engine.py`
- `benchmark_summary.json`
- `euler_bricks_m80.csv`

全部使用 exact integer / rational operations。

---

# 46. P2 核心結果整理

### P2-R1

coupling identity可重寫成 exact ratio equi-join。

### P2-R2

indexed join complexity在實作模型中由 explicit $N^3$ 降為：

$$
O(N^2+M).
$$

### P2-R3

$M=15$ brute cross-check與 indexed engine結果完全一致。

### P2-R4

$M=80$：

$$
N=2628,
$$

$$
N^3=18,149,977,152,
$$

$$
N^2=6,906,384,
$$

$$
288
$$

raw compatible triples，

$$
48
$$

unique Euler bricks，

$$
0
$$

Perfect hits。

---

# 47. P2 不宣稱

P2 不宣稱：

- complete Perfect Cuboid search複雜度已降為 $O(N^2)$；
- X4-DMSD 已跨巨大十進制尺度；
- ratio buckets在高 height保持同樣 sparsity；
- indexed join已優於所有現有 Euler-brick algorithms；
- Perfect Cuboid不存在。

---

# 48. 對原「卯榫」直覺的更新

P1：

$$
\text{能不能喀？}
$$

變成：

$$
ACB'=BC'A'.
$$

P2：

$$
\text{我要不要拿第三塊一塊一塊試？}
$$

答案變成：

$$
\boxed{
\text{不用。前兩塊直接算出第三塊必須具有的 exact 榫頭比例。}
}
$$

然後 hash lookup。

這就是第一次真正把「看接口」轉成搜尋加速。

---

# 49. 下一階段

**X4-DMSD P3 — Ratio/Valuation/Modular Signature Engine**

P3 應開始讓 exact ratio lookup進入 huge-scale-ready form：

1. reduced integer ratio key；
2. pre-multiplication gcd cancellation；
3. prime valuation vector；
4. modular ratio signature；
5. bucket intersection；
6. bit-length accounting；
7. false-negative-free prefilter；
8. comparison with plain Fraction index；
9. block-compatible summary format；
10. DMSD handoff interface。

P3 完成後，才真正開始把 P2 engine嵌進 DMSD blocks。
