如何生成所有可能的 3x3 矩阵,其第一行和第一列固定,条目为 0,1,2

问题描述 投票:0回答:1

我想将矩阵的第一行和第一列固定为 0 1 2..只有这样的矩阵才会在所有可能的 3x3 矩阵中进行过滤,条目来自 0,1,2。稍后我必须检查那些过滤条件的一个条件。条件如下: i + j = a_ij (一个矩阵的对应条目) = m , m + k = a_mk j + k = a_jk(第 j 行第 k 列的条目)= n, i + n = a_in

我必须仅显示所有组合中满足“ a_mk = a_in ”的矩阵。它更像是检查关联性。谁能帮忙完成这个吗?

from itertools import product
import numpy as np
m=3
n=3

x = [[list(i[x:x+m]) for x in range(0, len(i), m)] for i in product("012", repeat=m*n)]
print(len(x))
A=[]
for j in x:
  j = np.array(j)
  for i in range(3):
    if j[0][i] == i:
      if j[i][0] == i:
        A.append(j)

print(len(A))

到目前为止我已经能写了。

python matrix combinations associativity
1个回答
0
投票

这是修改后的代码

从 itertools 导入产品 将 numpy 导入为 np

米 = 3 n = 3

x = [[list(i[x:x+m]) for x in range(0, len(i), m)] for i in Product("012",repeat=m*n)]

A = [] 对于 x 中的 j: j = np.array(j, dtype=int)

# Check conditions
condition_1 = np.all(j[0, :] == np.arange(m))
condition_2 = np.all(j[:, 0] == np.arange(n))
condition_3 = np.all(j[1:, :] + np.arange(m) == j[:-1, :])
condition_4 = np.all(j[:, 1:] + np.arange(n) == j[:, :-1])
condition_5 = np.all(j[0, :] + np.arange(n) == j[:, 0])

# Check associativity condition
associativity_condition = np.all(j[1:, 1:] == j[:-1, :-1])

if condition_1 and condition_2 and condition_3 and condition_4 and condition_5 and associativity_condition:
    A.append(j)

打印(长度(A)) 打印(A)

© www.soinside.com 2019 - 2024. All rights reserved.