Python 如何从生成集创建矩阵

Python 如何从生成集创建矩阵,python,matrix,sage,Python,Matrix,Sage,我试图使用一些向量的生成集来形成一个矩阵,其中每个元素表示二进制数据 我希望代码使用集合中的向量并创建一个矩阵,其中包含零向量、每行向量以及组合v1+v2、v1+v3、v2+v3和v1+v2+v3,即所有可能的线性组合,其中0和1作为系数 我尝试过使用for循环,但最终都是重复。我也可以通过做每一个操作来实现这一点,但这对于有很多向量的生成集是不可行的 import numpy as np A = np.matrix([[1,1,1,1,0,0,0,0],[0,0,1,1,1,1,0,0],[0

我试图使用一些向量的生成集来形成一个矩阵,其中每个元素表示二进制数据

我希望代码使用集合中的向量并创建一个矩阵,其中包含零向量、每行向量以及组合
v1+v2
v1+v3
v2+v3
v1+v2+v3
,即所有可能的线性组合,其中
0
1
作为系数

我尝试过使用for循环,但最终都是重复。我也可以通过做每一个操作来实现这一点,但这对于有很多向量的生成集是不可行的

import numpy as np
A = np.matrix([[1,1,1,1,0,0,0,0],[0,0,1,1,1,1,0,0],[0,0,0,0,1,1,1,1]])
我想创建一个新的矩阵,它由上述矩阵a中的行向量的所有可能的线性组合组成

输出应包含以下内容:

[0,0,0,0,0,0,0,0], 
[1,1,1,1,0,0,0,0], 
[0,0,1,1,1,1,0,0], 
[0,0,0,0,1,1,1,1], 
[1,1,0,0,1,1,0,0], 
[0,0,1,1,0,0,1,1],
[1,1,1,1,1,1,1,1], 
[1,1,0,0,0,0,1,1]

我想这就是你想要的

import numpy as np
from itertools import combinations

v = np.array([[1,1,1,1,0,0,0,0],[0,0,1,1,1,1,0,0],[0,0,0,0,1,1,1,1]]) # v1, v2, v3

l = [] # creating a list of possible combinations [(0, 1), (0, 2), (1, 2), (0, 1, 2)]
for j in range(2, v.shape[0]+1):
    comb = combinations(np.arange(v.shape[0]), j)  
    for i in list(comb): 
        l.append(i)

final = np.zeros((len(l), v.shape[1]))  # creating final matrix 

for i in range(len(l)): # filling final matrix based on combinations
    for j in (l[i]):
        final[i] += v[j]

final = np.concatenate((np.zeros((1,v.shape[1])), v, final%2), axis=0)

#array([[0., 0., 0., 0., 0., 0., 0., 0.],
#       [1., 1., 1., 1., 0., 0., 0., 0.],
#       [0., 0., 1., 1., 1., 1., 0., 0.],
#       [0., 0., 0., 0., 1., 1., 1., 1.],
#       [1., 1., 0., 0., 1., 1., 0., 0.],
#       [1., 1., 1., 1., 1., 1., 1., 1.],
#       [0., 0., 1., 1., 0., 0., 1., 1.],
#       [1., 1., 0., 0., 0., 0., 1., 1.]])

我看待这个问题的方式是,你有一组系数:

coeffs = [0, 1]
目标是将系数的所有组合乘以向量。从数学上讲,您希望将以下系数应用于向量:

from itertools import chain, combinations_with_replacement, permuations
import numpy as np

A = np.matrix([[1,1,1,1,0,0,0,0],[0,0,1,1,1,1,0,0],[0,0,0,0,1,1,1,1]])

N = len(A)
all_coeffs = (
    chain.from_iterable(
        list(set(permutations(x, N))) for x in combinations_with_replacement(coeffs, N)
    )
)
print(list(all_coeffs))
#[(0, 0, 0),
# (1, 0, 0),
# (0, 1, 0),
# (0, 0, 1),
# (0, 1, 1),
# (1, 1, 0),
# (1, 0, 1),
# (1, 1, 1)]
因此,您需要将每个系数的点积与
A
中的每一行相乘,然后对结果进行“求和”。由于您使用的是二进制数,因此可以使用
xor
运算符实现加法操作。最后,您可以将结果连接在一起

综上所述:

from functools import reduce
from operator import xor

N = len(A)
mat = np.concatenate(
    [
        reduce(xor, (a*b for a, b in zip(c, A)))
        for c in (
            chain.from_iterable(
                list(set(permutations(x, N))) for x in 
                combinations_with_replacement(coeffs, N)
            )
        )
    ]
)
print(mat)
#[[0 0 0 0 0 0 0 0]
# [1 1 1 1 0 0 0 0]
# [0 0 1 1 1 1 0 0]
# [0 0 0 0 1 1 1 1]
# [0 0 1 1 0 0 1 1]
# [1 1 0 0 1 1 0 0]
# [1 1 1 1 1 1 1 1]
# [1 1 0 0 0 0 1 1]]

如果包含一个包含输入和所需输出的具体示例的小示例,您将获得更好的响应。除了这个矩阵中不同行的操作之外,我真的没有别的东西,所以我不确定把什么作为我的最小可复制示例。我想知道是否有更好的方法来添加矩阵中的所有行向量,而不必手动键入。谢谢你的建议!只有1和0作为系数?那么,只有总数?就像你不想让v1-v2作为输出一样?是的,我只想求和的原因是因为我是用二进制的。你给出的矩阵的预期输出是什么?