無損離散場引擎演算法簡易認證
import unittest
import math
import time
import numpy as np
class TestLDFEEFormalAxioms(unittest.TestCase):
"""
LDFEE 公理系統自動化形式驗證套件 (100% 嚴格數學閉環版)
作者:張榮興 (Zhang Rong-Xing)
"""
# ================================================================
# 1. 公理 1 驗證:奇數尺度記憶體無衝突公理 (W = 990 * k, k 為正奇數)
# ================================================================
def test_axiom1_memory_alignment(self):
unit_w = 11 * 9 * 10 # 990 = 2 * 495
self.assertEqual(unit_w, 990)
# 當 k 為任意正奇數 (1, 3, 5, ..., 19999) 時,W 僅含單一因數 2
# 故 W mod 64 != 0 恆成立 (W mod 64 永遠為奇數倍的 2,絕不為 0)
for k in range(1, 20000, 2):
w = unit_w * k
self.assertNotEqual(
w % 64, 0,
f"公理 1 違反:奇數 k={k} 時 W={w} 不應被 64 整除"
)
# ================================================================
# 2. 公理 2 驗證:定點數最小能量量子與溢位安全值域公理
# ================================================================
def test_axiom2_quantum_domain_safety(self):
int64_min = -(1 << 63)
int64_max = (1 << 63) - 1
m_max = 1000.0
s_bits = 28
x_int = int(np.rint(m_max * (1 << s_bits)))
self.assertGreaterEqual(x_int, int64_min)
self.assertLessEqual(x_int, int64_max)
safety_bits = math.ceil(math.log2(m_max)) + s_bits + 6
self.assertLess(safety_bits, 63)
# ================================================================
# 3. 公理 3 驗證:零乘除計算等價閉包公理 O = {+, -, <<, >>, &}
# ================================================================
def test_axiom3_zero_mul_div_closure(self):
test_vals = np.array(
[-10**12, -99999, -11, -1, 0, 1, 11, 99999, 10**12],
dtype=np.int64
)
# 驗證 k = 10 拆解: ((X << 3) + (X << 1)) == 10 * X
shift_add_10 = (test_vals << np.int64(3)) + (test_vals << np.int64(1))
self.assertTrue(np.array_equal(shift_add_10, test_vals * np.int64(10)))
# 驗證中心權重 20 拆解: ((c << 4) + (c << 2)) == 20 * c
shift_add_20 = (test_vals << np.int64(4)) + (test_vals << np.int64(2))
self.assertTrue(np.array_equal(shift_add_20, test_vals * np.int64(20)))
# ================================================================
# 4. 引理 A 驗證:定點尺度擴展之零額外成本引理 C(28) = C(20)
# ================================================================
def test_lemma_a_zero_overhead_scaling(self):
n_nodes = 990 * 990
arr_s20 = np.arange(n_nodes, dtype=np.int64) << np.int64(20)
arr_s28 = np.arange(n_nodes, dtype=np.int64) << np.int64(28)
self.assertEqual(arr_s20.itemsize, 8)
self.assertEqual(arr_s28.itemsize, 8)
self.assertEqual(arr_s20.nbytes, arr_s28.nbytes)
t0 = time.perf_counter()
_ = (arr_s20 + np.int64(1 << 6) + (arr_s20 >> np.int64(63))) >> np.int64(7)
dt_20 = time.perf_counter() - t0
t1 = time.perf_counter()
_ = (arr_s28 + np.int64(1 << 6) + (arr_s28 >> np.int64(63))) >> np.int64(7)
dt_28 = time.perf_counter() - t1
self.assertLess(abs(dt_28 - dt_20), 0.05)
# ================================================================
# 5. 引理 B 驗證:無分支對稱無偏捨入誤差有界引理 (E[Error] = 0)
# ================================================================
def test_lemma_b_branchless_unbiased_rounding(self):
x = np.arange(-500000, 500001, dtype=np.int64)
for s in:
half = np.int64(1 << (s - 1))
y = (x + half + (x >> np.int64(63))) >> np.int64(s)
exact = x.astype(np.float64) / float(1 << s)
errors = y.astype(np.float64) - exact
self.assertLessEqual(float(np.max(np.abs(errors))), 0.5)
self.assertAlmostEqual(float(np.mean(errors)), 0.0, places=15)
# ================================================================
# 6. 2.3 節驗證:二維雙軸同動各向同性收斂 (E_45deg = 0)
# ================================================================
def test_isotropic_2d_dual_axis_e45deg_zero(self):
# 建立 3x3 局部離散網格座標 (-1, 0, 1)
coords = np.array([-1, 0, 1], dtype=np.int64)
y, x = np.meshgrid(coords, coords, indexing="ij")
# 採用 45 度方向性四次調和多項式 f(x,y) = x^4 + y^4 - 6*x^2*y^2
# 在連續空間中,其拉普拉斯與雙調和算子在 (0,0) 處均嚴格為 0
f = x**4 + y**4 - 6 * (x**2) * (y**2)
# 精準擷取中心點 (1, 1) 及其正交、對角鄰域
c = int(f[1, 1])
s_ortho = int(f[1, 0] + f[1, 2] + f[0, 1] + f[2, 1])
s_diag = int(f[0, 0] + f[0, 2] + f[2, 0] + f[2, 2])
# 1. 傳統五點算子在 (0,0) 處會產生非零方向性殘差 (4 != 0)
l5_center = s_ortho - (c << 2)
self.assertNotEqual(l5_center, 0, "傳統五點算子必然帶有 45 度方向性截斷偏差")
# 2. 本架構雙軸同動九點算子精準消滅 45 度方向性誤差 (E_45deg == 0)
l9_center = (s_ortho << 2) + s_diag - ((c << 4) + (c << 2))
self.assertEqual(
l9_center, 0,
f"各向同性定理違反:九點算子中心殘差應為 0,實測為 {l9_center}"
)
if __name__ == "__main__":
print("[*] 啟動 LDFEE 形式公理系統自動化驗證套件 v2 (Author: 張榮興)...")
unittest.main(verbosity=2)

