【问题标题】:Python Matrix multiplication solve Ax <=bPython 矩阵乘法求解 Ax <=b
【发布时间】:2018-08-06 16:15:09
【问题描述】:

不知道如何用 Python 解决这个问题

有一个 3x3 矩阵,即 A

3x1 矩阵(未知),即 X

和已知的b

我怎样才能找到 Ax

【问题讨论】:

标签: python numpy matrix matrix-multiplication


【解决方案1】:

假设A 是可逆的。然后就可以写条件了

Ax <= b

作为

Ax = b + c   |   c <= 0

其中c 是列向量。由于我们假设A 是可逆的,我们可以求解x

x = A^-1 (b + c)

给定c,如果c 的所有组件都是非正数,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

【讨论】:

    猜你喜欢
    • 2014-04-05
    • 2018-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多