擅长:python、mysql、java
<p>您可以使用<code>cvxpy</code>来解决它:</p>
<pre><code>import cvxpy
def solve(A, B):
"""
Minimizes |AX - B|**2, assuming A and B are
square matrices for simplicity. If this optimized
error is zero, this corresponds to solving AX = B.
"""
n = A.shape[0]
X = cvxpy.Variable((n,n))
# Set objective
obj_fun = cvxpy.sum_squares(A*X - B)
objective = cvxpy.Minimize(obj_fun)
# Set constraints
constraints = [X >= 0]
prob = cvxpy.Problem(objective, constraints)
result = prob.solve(solver = "ECOS")
return X.value
</code></pre>
<p>编辑:我相信<a href="https://stackoverflow.com/a/56228106/2653663">Prune</a>的答案是正确的。您可以通过检查<code>results</code>来检查数值求解器中的错误是否为非零。在</p>