求所有可逆方阵

2024-10-03 21:35:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想写一个函数,其中给定两个整数“n”和“p”,它生成所有n阶可逆矩阵,其中元素来自{0,1,…,p-1}。 我有以下代码:

import itertools 
import numpy as np 

def invertible_matrices(n, p):
    invertibleMatrices = set()
    # generates all the possible matrices
    x = [y for y in range(p)]
    a = [j for j in itertools.product(x, repeat=n)]
    b = {k for k in itertools.product(a, repeat=n)}
    for each in b:
        if np.linalg.det(each) != 0:
            invertibleMatrices.add(each)
    return invertibleMatrices

对于n=2p=2它可以正常工作,但是对于n=2p=3我得到50,而答案是48。 任何帮助都将不胜感激

如果你熟悉群论,我想找到GL(n, p)(有限域上p元素的一般线性群)的所有元素


Tags: 函数代码inimport元素fornp矩阵
2条回答

使用inspect_matrix()进行调试,获取_Inversible_matrix()使用集合理解来确定所有可逆矩阵,获取_Determinate_1_matrix()来获取行列式为1的矩阵:

import itertools
import numpy as np


def inspect_matrix(n, p):
    """Examine a single, square matrix."""
    a = list(itertools.product(list(range(p)), repeat=n))
    matrices = {k
                for k in itertools.product(a, repeat=n)
               }
    matrix = next(iter(matrices))  # Inspect one of the matrices
    determinant = np.linalg.det(matrix)
    print(f"{matrix = }\n{determinant = }")
    print(f"inverse = {(inverse:=np.linalg.inv(matrix))}") if determinant != 0.0 else print("Matrix is not invertible")
    return inverse 


def get_invertible_matrices(n, p):
    """Generates all the possible matrices."""
    a = list(itertools.product(list(range(p)), repeat=n))
    invertible_matrices = {k
                           for k in itertools.product(a, repeat=n)
                           if not np.isclose((np.linalg.det(k) + 1) % p, 1)
                           }
    print(f"{len(invertible_matrices) = }")
    return invertible_matrices


def get_determinant_1_matrices(n, p):
    """Generates all the square matrices with determinant 1."""
    a = list(itertools.product(list(range(p)), repeat=n))
    if p==2:
            det_1_matrices = {k
                              for k in itertools.product(a, repeat=n)
                              if np.isclose((np.linalg.det(k))%p,1)
                              }
    else:
            det_1_matrices = {k
                              for k in itertools.product(a, repeat=n)
                              if np.isclose((np.linalg.det(k)+1)%p,2)
                              }
    print(f"{len(det_1_matrices) = }")
    return det_1_matrices


def main():
    print(get_invertible_matrices(n=2, p=2))
    print(get_invertible_matrices(n=2, p=3))
    print(get_determinant_1_matrices(n=2, p=2))
    print(get_determinant_1_matrices(n=2, p=3))


if __name__ == '__main__':
    main()

返回:

len(invertible_matrices) = 6
{((1, 1), (0, 1)), ((1, 0), (0, 1)), ((1, 0), (1, 1)), ((0, 1), (1, 0)), ((0, 1), (1, 1)), ((1, 1), (1, 0))}
len(invertible_matrices) = 48
{((1, 0), (0, 1)), ((1, 2), (0, 2)), ((2, 1), (1, 0)), ((0, 2), (2, 0)), ((0, 1), (2, 0)), ((1, 1), (1, 0)), ((2, 1), (1, 1)), ((2, 2), (2, 0)), ((1, 1), (2, 1)), ((1, 2), (1, 0)), ((2, 1), (2, 2)), ((2, 0), (0, 2)), ((1, 2), (1, 1)), ((2, 2), (0, 2)), ((1, 0), (0, 2)), ((1, 1), (1, 2)), ((1, 2), (2, 2)), ((2, 1), (0, 1)), ((1, 1), (0, 1)), ((0, 2), (1, 0)), ((0, 1), (1, 0)), ((2, 0), (2, 1)), ((0, 2), (2, 1)), ((2, 2), (1, 0)), ((0, 1), (2, 1)), ((1, 2), (0, 1)), ((0, 2), (1, 1)), ((2, 0), (1, 1)), ((0, 1), (1, 1)), ((2, 2), (2, 1)), ((2, 0), (2, 2)), ((0, 2), (2, 2)), ((2, 1), (2, 0)), ((0, 1), (2, 2)), ((1, 0), (2, 1)), ((1, 0), (1, 1)), ((1, 1), (2, 0)), ((2, 0), (1, 2)), ((0, 2), (1, 2)), ((0, 1), (1, 2)), ((1, 0), (2, 2)), ((2, 0), (0, 1)), ((1, 2), (2, 0)), ((2, 2), (1, 2)), ((2, 1), (0, 2)), ((1, 0), (1, 2)), ((2, 2), (0, 1)), ((1, 1), (0, 2))}
len(det_1_matrices) = 6
{((0, 1), (1, 1)), ((0, 1), (1, 0)), ((1, 0), (0, 1)), ((1, 0), (1, 1)), ((1, 1), (0, 1)), ((1, 1), (1, 0))}
len(det_1_matrices) = 24
{((2, 2), (0, 2)), ((0, 2), (1, 2)), ((1, 1), (0, 1)), ((1, 2), (2, 2)), ((2, 1), (2, 0)), ((1, 0), (0, 1)), ((2, 0), (2, 2)), ((2, 1), (1, 1)), ((1, 1), (2, 0)), ((1, 0), (2, 1)), ((1, 2), (0, 1)), ((1, 2), (1, 0)), ((2, 0), (0, 2)), ((1, 0), (1, 1)), ((1, 1), (1, 2)), ((0, 2), (1, 0)), ((0, 1), (2, 2)), ((0, 2), (1, 1)), ((0, 1), (2, 1)), ((2, 0), (1, 2)), ((0, 1), (2, 0)), ((2, 2), (2, 1)), ((2, 2), (1, 0)), ((2, 1), (0, 2))}

我想你想要的是行列式模p(它是GL(n,p)中的行列式,也就是有限域上的p元素)

if not np.isclose((np.linalg.det(each)+1)%p,1):
    invertibleMatrices.add(each)

注:+1是为了避免小数值误差的缠绕

相关问题 更多 >