JAX study notes[13]
文章目录
- the relation of equivalence
- references
the relation of equivalence
- a relation of equivalence named with
R
is defined as follows:
let A is a nonempty set so that we can establish the relation between both elements from A.
- reflexivity: if a ∈ A a \in A a∈A,then a R a a R a aRa.
- symmetry: a R b a R b aRb,so that b R a b R a bRa.
- transitivity: if
a
R
b
,
b
R
c
a R b,b R c
aRb,bRc,then
a
R
c
a R c
aRc
if a R b a R b aRb,then it can be called that a ∼ b a \sim b a∼b.
- for a ∈ A a \in A a∈A, [ a ] = { b ∣ a ∼ b } [a]=\{b| a \sim b\} [a]={b∣a∼b} that [ a ] [a] [a] is defined with equivalence class.
- the set consisted of all equivalence class of A can be called the quotient set based on the quivalence relation of A ,which be can marked as A / ∼ A/\sim A/∼
- subset family:A is a nonempty set which is a union set of some mutually disjoint subsets of itself. all this sutbsets are called partition and everyone of them is called class
- integer modulo n equvialence class
let A is a set consisted of integer numbers between 0 and 99.
import jax
import jax.numpy as jnp
import numpy as np
def construct_mod_n_classes(n, max_num=100):
"""构造整数模n的等价类"""
numbers = jnp.arange(max_num)
equivalence_classes = dict()
for num in numbers:
equiv_class = np.array(numbers[num % n == numbers % n])
equiv_class_tuple = tuple(equiv_class) # 转换为元组
equivalence_classes[equiv_class_tuple] = equiv_class_tuple
return list(equivalence_classes.values())
# 示例:模11的等价类
mod5_classes = construct_mod_n_classes(5)
print("模11的等价类:")
for cls in mod5_classes:
print(cls)
模11的等价类:
(np.int32(0), np.int32(5), np.int32(10), np.int32(15), np.int32(20), np.int32(25), np.int32(30), np.int32(35), np.int32(40), np.int32(45), np.int32(50), np.int32(55), np.int32(60), np.int32(65), np.int32(70), np.int32(75), np.int32(80), np.int32(85), np.int32(90), np.int32(95))
(np.int32(1), np.int32(6), np.int32(11), np.int32(16), np.int32(21), np.int32(26), np.int32(31), np.int32(36), np.int32(41), np.int32(46), np.int32(51), np.int32(56), np.int32(61), np.int32(66), np.int32(71), np.int32(76), np.int32(81), np.int32(86), np.int32(91), np.int32(96))
(np.int32(2), np.int32(7), np.int32(12), np.int32(17), np.int32(22), np.int32(27), np.int32(32), np.int32(37), np.int32(42), np.int32(47), np.int32(52), np.int32(57), np.int32(62), np.int32(67), np.int32(72), np.int32(77), np.int32(82), np.int32(87), np.int32(92), np.int32(97))
(np.int32(3), np.int32(8), np.int32(13), np.int32(18), np.int32(23), np.int32(28), np.int32(33), np.int32(38), np.int32(43), np.int32(48), np.int32(53), np.int32(58), np.int32(63), np.int32(68), np.int32(73), np.int32(78), np.int32(83), np.int32(88), np.int32(93), np.int32(98))
(np.int32(4), np.int32(9), np.int32(14), np.int32(19), np.int32(24), np.int32(29), np.int32(34), np.int32(39), np.int32(44), np.int32(49), np.int32(54), np.int32(59), np.int32(64), np.int32(69), np.int32(74), np.int32(79), np.int32(84), np.int32(89), np.int32(94), np.int32(99))
- Matrix similarity equivalence class
to generate a similarity matrix is as follows:
import jax
import jax.numpy as jnp
# 初始化随机数生成器
key = jax.random.PRNGKey(0)
# 生成5个随机2×2矩阵
keys = jax.random.split(key, 7) # 5个原始矩阵 + 2个变换矩阵
matrices = [jax.random.normal(k, (2, 2)) for k in keys[:5]]
def generate_invertible_matrix(key, shape):
"""通过QR分解生成可逆矩阵"""
A = jax.random.normal(key, shape)
Q, R = jnp.linalg.qr(A)
return Q @ jnp.diag(jnp.sign(jnp.diag(R))) # 确保对角线元素为正
# 为前2个矩阵生成真正的相似矩阵
for i in range(2):
P = generate_invertible_matrix(keys[5+i], (2,2))
# 确保P可逆(实际使用时需要更严格的检查)
similar_mat = P @ matrices[i] @ jnp.linalg.inv(P)
matrices.append(similar_mat)
# 验证相似性
def is_similar(A, B, tol=1e-6):
return (jnp.allclose(jnp.linalg.eigvals(A), jnp.linalg.eigvals(B), tol) and
jnp.allclose(jnp.trace(A), jnp.trace(B), tol) and
jnp.allclose(jnp.linalg.det(A), jnp.linalg.det(B), tol))
# 检查构造是否正确
for i in range(2):
print(f"矩阵 {i} 和它的人工构造相似矩阵是否真正相似: "
f"{is_similar(matrices[i], matrices[5+i])}")
we can write following python code to build matrix similarity equivalence class
import jax
import jax.numpy as jnp
import numpy as np
def is_similar(A, B, tol=1e-6):
"""检查两个矩阵是否相似(存在可逆P使得PAP^-1 = B)"""
# 这里简化实现,实际相似性检查更复杂
eigenvalues_A = jnp.linalg.eigvals(A)
eigenvalues_B = jnp.linalg.eigvals(B)
return jnp.allclose(jnp.sort(eigenvalues_A), jnp.sort(eigenvalues_B), tol)
def construct_similarity_classes(matrices):
"""构造矩阵的相似等价类"""
classes = []
for mat in matrices:
in_existing_class = False
for cls in classes:
if is_similar(mat, cls[0]):
cls.append(mat)
in_existing_class = True
break
if not in_existing_class:
classes.append([mat])
return classes
def generate_invertible_matrix(key, shape):
"""通过QR分解生成可逆矩阵"""
A = jax.random.normal(key, shape)
Q, R = jnp.linalg.qr(A)
return Q @ jnp.diag(jnp.sign(jnp.diag(R))) # 确保对角线元素为正
# 示例矩阵
# 初始化随机数生成器
key = jax.random.PRNGKey(0)
# 生成5个随机2×2矩阵
keys = jax.random.split(key, 10) # 需要7个key:5个原始矩阵+2个变换矩阵
matrices = [jax.random.normal(k, (2, 2)) for k in keys[:5]]
# 为前5个矩阵生成真正的相似矩阵
i=1
for mat in matrices[:5]:
# 生成随机可逆矩阵P
P =generate_invertible_matrix(keys[5+i], (2,2))
keys = keys[1:] # 更新key
# 确保P是可逆的(实际应用中可能需要检查)
similar_mat = P @ mat @ jnp.linalg.inv(P)
matrices.append(similar_mat)
i+=1
similarity_classes = construct_similarity_classes(matrices)
print("\n矩阵相似等价类:")
for i, cls in enumerate(similarity_classes):
print(f"类 {i+1}: {len(cls)}个矩阵")
矩阵相似等价类:
类 1: 2个矩阵
类 2: 2个矩阵
类 3: 2个矩阵
类 4: 2个矩阵
类 5: 2个矩阵
references
- deepseek
- 《近世代数》
原文地址:https://blog.csdn.net/sakura_sea/article/details/149135302
免责声明:本站文章内容转载自网络资源,如侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!