我想解决一个矩形系统(在解决方案中有任意参数)。如果没有,我想在矩阵中添加行,直到它为正方形。
print matrix_a
print vector_b
print len(matrix_a),len(matrix_a[0])
给出:
[[0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]]
[2, 0, 2, 1, 2, 1, 1, 1, 1, 1, 1]
11 26
我的完整代码位于http://codepad.org/tSgstpYe
如你所见,我有一个系统Ax = b。 我知道每个x值x1,x2 ..必须是1或0,我希望有这个限制,系统应该只有一个解决方案。
实际上我期望的答案是x = [0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0 ,0,0,1,0,0,0,0]
我看着 http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.solve.html#numpy.linalg.solve 但它似乎只采用方形矩阵。任何有关解决此系统的帮助都会很棒!
答案 0 :(得分:3)
这是一个简单的实现(具有硬编码阈值),但它提供了您正在寻找的测试数据解决方案。
它基于Iteratively Reweighted Least Squares。
from numpy import abs, diag, dot, zeros
from numpy.linalg import lstsq, inv, norm
def irls(A, b, p= 1):
"""Solve least squares problem min ||x||_p s.t. Ax= b."""
x, p, e= zeros((A.shape[1], 1)), p/ 2.- 1, 1.
xp= lstsq(A, b)[0]
for k in xrange(1000):
if e< 1e-6: break
Q= dot(diag(1./ (xp** 2+ e)** p), A.T)
x= dot(dot(Q, inv(dot(A, Q))), b)
x[abs(x)< 1e-1]= 0
if norm(x- xp)< 1e-2* e** .5: e*= 1e-1
xp= x
return k, x.round()
答案 1 :(得分:2)
根据您期望的输入,使用简单的树搜索算法可能会更好。您的结果向量包含相对较低的数字,允许尽早切断大多数树枝。我尝试实现此算法会在0.2秒后产生预期结果:
def testSolution(a, b, x):
result = 0
for i in range(len(b)):
n = 0
for j in range(len(a[i])):
n += a[i][j] * x[j]
if n < b[i]:
result = -1
elif n > b[i]:
return 1
return result
def solve(a, b):
def solveStep(a, b, result, step):
if step >= len(result):
return False
result[step] = 1
areWeThere = testSolution(a, b, result)
if areWeThere == 0:
return True
elif areWeThere < 0 and solveStep(a, b, result, step + 1):
return True
result[step] = 0
return solveStep(a, b, result, step + 1)
result = map(lambda x: 0, range(len(a[0])))
if solveStep(a, b, result, 0):
return result
else:
return None
matrix_a = [[0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0],
[1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]]
vector_b = [2, 0, 2, 1, 2, 1, 1, 1, 1, 1, 1]
print solve(matrix_a, vector_b)
这必须测试您输入的1325个可能的向量,这比所有可能的结果(6700万)少得多。最糟糕的情况当然是6700万次测试。
答案 2 :(得分:1)
让Ax = b
成为系统,然后A|b
成为增强矩阵
有三种可能性
rank(A) < rank(A|b)
rank(A) = rank(A|b) = n
rank(A) = rank(A|b) < n
其中n
是未知数。