对于给定的点列表,我想检查它们是否位于(可能是低维)单形的内部。 我想用Python做到这一点。
编辑:我想(反复)回答这个问题,u
是否在A
的形象中(作为决策问题,所以只是是) /否)。
首先,我进行QR分解,然后解决系统,最后检查解决方案是否正确。
import scipy.linalg
import numpy as np
Q,R = np.linalg.qr(AA)
for u in points:
x = scipy.linalg.solve_triangular(R, np.dot(Q.T, u))
print(all(x <= 1-1e-6) and all(x >= 1e-6) and all(abs(np.dot(AA,x) - u) < 1e-6))
但是,我遇到了数值问题,准确性似乎太差了。我有一个点,它位于内部(根据先前的线性程序计算),但上面的代码无法识别这一点。
矩阵的条件大约为100,形状为(36,35)
,所以看起来并不那么可怕,但误差略高于1e-6
有没有办法提高准确度?
sympy
进行符号化,但不得不中断计算。花了太长时间。1e-6
。AA = np.array([
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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, 2, 2, 2, 2, 2, 4, 4, 6, 6, 6, 6, 6, 6, 6, 8],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 4, 6, 0, 0, 0, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 2, 6],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 4, 0, 0, 0, 4, 4, 4, 0, 0, 4, 0, 2, 0, 0, 0, 0, 0, 2, 2, 6, 0, 0],
[0, 0, 0, 0, 0, 2, 2, 2, 4, 4, 6, 6, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 6, 0, 2, 4, 0, 4],
[0, 0, 0, 4, 10, 0, 0, 6, 0, 0, 2, 2, 2, 0, 2, 2, 4, 2, 2, 0, 0, 2, 4, 2, 0, 0, 0, 0,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0, 0, 0, 0, 6, 0, 2, 0],
[0, 0, 2, 2, 0, 4, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 2, 0, 0, 2, 2, 0, 0, 0, 0, 2, 0, 2]])
v = np.array([1, 1, 1, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 1, 2, 1, 2, 2, 2, 1])
points = [v]
答案 0 :(得分:1)
您在上面发布的系统没有确切的解决方案。所以np.dot(AA,x) - u)
永远不会收敛到机器精度。事实上,您的代码正在做的是找到系统唯一最小二乘解的正确数值近似值。
有很多方法可以了解系统无法提供解决方案的原因。一种方法是recall
线性系统
Ax=b
是一致的,当且仅当A
的等级等于[A|b]
的等级时,矩阵A
增加b
}作为专栏。
您可以按如下方式在数字上估算排名:
# reshape the RHS to a column so we can combine it with AA
b = points[0].reshape((36,1))
# append the column to form the augmented matrix
AA_b = np.hstack((AA, b))
print("AA rank: %d" % np.linalg.matrix_rank(AA))
print("[AA|b] rank: %d" % np.linalg.matrix_rank(AA_b))
这表明AA
的排名为35,但[A|b]
的排名为36,因此系统无法提供解决方案。
你也可以通过将增强矩阵放入缩小的行梯形形式来说服自己。我能够在wxMaxima中很快完成这项工作,并验证增强矩阵是否等同于身份的RREF。同样这样做也是可能的,但似乎很慢:
AA_b.rref()