Python矩阵乘法求解Ax <=b
Python Matrix multiplication solve Ax <=b
不确定如何用 Python
解决这个问题
有一个 3x3 矩阵,它是 A
3x1 矩阵(未知)是 X
和已知的 b
如何找到 Ax <= b 的所有解?提前致谢
让我们假设 A
是可逆的。然后你可以写条件
Ax <= b
作为
Ax = b + c | c <= 0
其中 c
是列向量。由于我们假设 A
是可逆的,我们可以求解 x
:
x = A^-1 (b + c)
给定 c
,如果 c
的所有分量都是 non-positive,x
将是一个精确的解。
让我们用 numpy 检查一下:
>>> import numpy as np
>>>
# create example A and b
>>> A = np.random.random((3, 3))
>>> b = np.random.random((3, 1))
>>>
# invert A and compute solution for c == 0
>>> invA = np.linalg.inv(A)
>>> x0 = invA @ b
>>>
# create 10,000 different vectors c
>>> c = np.random.uniform(-10, 10, (3, 10000))
# and mark those which have three non-pos components
>>> all_non_pos = np.all(c <= 0, axis=0)
# compute corresponding vectors x
>>> x = x0 + invA @ c
# and check which are solutions
>>> all_le_b = np.all(A @ x <= b, axis=0)
# finally, check whether solutions correspond to non-neg c
>>> np.all(all_non_pos == all_le_b)
True
不确定如何用 Python
解决这个问题有一个 3x3 矩阵,它是 A
3x1 矩阵(未知)是 X
和已知的 b
如何找到 Ax <= b 的所有解?提前致谢
让我们假设 A
是可逆的。然后你可以写条件
Ax <= b
作为
Ax = b + c | c <= 0
其中 c
是列向量。由于我们假设 A
是可逆的,我们可以求解 x
:
x = A^-1 (b + c)
给定 c
,如果 c
的所有分量都是 non-positive,x
将是一个精确的解。
让我们用 numpy 检查一下:
>>> import numpy as np
>>>
# create example A and b
>>> A = np.random.random((3, 3))
>>> b = np.random.random((3, 1))
>>>
# invert A and compute solution for c == 0
>>> invA = np.linalg.inv(A)
>>> x0 = invA @ b
>>>
# create 10,000 different vectors c
>>> c = np.random.uniform(-10, 10, (3, 10000))
# and mark those which have three non-pos components
>>> all_non_pos = np.all(c <= 0, axis=0)
# compute corresponding vectors x
>>> x = x0 + invA @ c
# and check which are solutions
>>> all_le_b = np.all(A @ x <= b, axis=0)
# finally, check whether solutions correspond to non-neg c
>>> np.all(all_non_pos == all_le_b)
True