Spaces:
Paused
Paused
Add code/cube3d/training/check_rotation_flip.py
Browse files
code/cube3d/training/check_rotation_flip.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import itertools
|
| 3 |
+
|
| 4 |
+
def signed_perm_mats_det_plus_1():
|
| 5 |
+
"""生成所有 3x3 的 ±1 置换矩阵,且 det=+1(24 个)"""
|
| 6 |
+
mats = []
|
| 7 |
+
for perm in itertools.permutations(range(3)): # 6 种列置换
|
| 8 |
+
P = np.zeros((3,3), int)
|
| 9 |
+
for r, c in enumerate(perm):
|
| 10 |
+
P[r, c] = 1
|
| 11 |
+
for signs in itertools.product([1, -1], repeat=3): # 行符号
|
| 12 |
+
R = P * np.array(signs)[:, None] # 每行乘 ±1
|
| 13 |
+
if int(round(np.linalg.det(R))) == 1:
|
| 14 |
+
mats.append(R)
|
| 15 |
+
# 去重(以 tuple 做 key)
|
| 16 |
+
uniq = {}
|
| 17 |
+
for R in mats:
|
| 18 |
+
key = tuple(R.reshape(-1))
|
| 19 |
+
uniq[key] = R
|
| 20 |
+
return list(uniq.values())
|
| 21 |
+
|
| 22 |
+
def check_transformation_invariance():
|
| 23 |
+
"""严格验证变换后的矩阵仍在群中"""
|
| 24 |
+
R24 = signed_perm_mats_det_plus_1() # 使用您的可靠生成器
|
| 25 |
+
R_z180 = np.array([[-1, 0, 0],
|
| 26 |
+
[0, -1, 0],
|
| 27 |
+
[0, 0, 1]], dtype=int) # 确保整数类型
|
| 28 |
+
|
| 29 |
+
failed_cases = []
|
| 30 |
+
for i, R in enumerate(R24):
|
| 31 |
+
transformed = R_z180 @ R
|
| 32 |
+
# 精确匹配(因所有矩阵均为整数)
|
| 33 |
+
found = any(np.array_equal(transformed, M) for M in R24)
|
| 34 |
+
if not found:
|
| 35 |
+
failed_cases.append((i, R, transformed))
|
| 36 |
+
|
| 37 |
+
if not failed_cases:
|
| 38 |
+
print("✅ 所有24个矩阵经过变换后仍在集合中")
|
| 39 |
+
return True
|
| 40 |
+
else:
|
| 41 |
+
print(f"❌ 发现{len(failed_cases)}个不符合的案例:")
|
| 42 |
+
for idx, orig, trans in failed_cases:
|
| 43 |
+
print(f"\n案例 {idx}:")
|
| 44 |
+
print("原矩阵(行列式={}):\n{}".format(int(np.linalg.det(orig)), orig))
|
| 45 |
+
print("变换后(行列式={}):\n{}".format(int(np.linalg.det(transformed)), trans))
|
| 46 |
+
return False
|
| 47 |
+
|
| 48 |
+
# 执行验证
|
| 49 |
+
check_transformation_invariance()
|