【问题标题】:Most basic linear algebra equation in pythonpython中最基本的线性代数方程
【发布时间】:2020-05-24 04:00:40
【问题描述】:

假设我有以下方程组:

x  + y = 3
5x - y = 3

这里是x=1y=2。使用 scipy 或其他一些线性代数求解器,最基本的方法是什么。例如,类似:

A = np.array([1,1],[5,-1])
A'inverse = [3,3] # how to do this?

【问题讨论】:

  • 最好的方法是自己做基础研究。您可以使用一些历史上最强大的搜索工具。推荐任何一个更受欢迎的,我都会觉得很舒服
  • numpy.linalg.solve。文档字符串包含一个示例。
  • @WarrenWeckesser 感谢您的建议,是的,我已经使用与 numpy.linalg 类似的内容发布了这个问题的答案

标签: python numpy scipy linear-algebra


【解决方案1】:

你可以使用 Numpy 的线性代数部分:

import numpy as np

A = np.array(([1,1],[5,-1]))

# Calculating inverse of A 
B = np.linalg.inv(A)
# array([[ 0.16666667,  0.16666667],
#       [ 0.83333333, -0.16666667]])

# Calculating matrix multiplication of B and A: Expecting Identical Matrix
np.matmul(B,A)
# Very close to 0 and 1
# array([[ 1.00000000e+00,  2.77555756e-17],
#       [-5.55111512e-17,  1.00000000e+00]])

# Converting to integer
np.int32( np.matmul(B,A) ) 
# array([[1, 0],
#       [0, 1]], dtype=int32)

【讨论】:

    【解决方案2】:

    来自 aminrd 使用np.linalg 的建议:

    import numpy as np
    A = np.array(([1,1],[5,-1]))    # initialize 2x2 matrix
    B = np.array([3,3])             # initialize the (x,y) column vector 
    A_inv = np.linalg.inv(A)        # get A'
    np.matmul(A_inv,B)              # Ax = B --> x = A'B
    # array([1., 2.])
    

    在这里我们可以看到答案是X=1, Y=2,这是我们所期望的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      相关资源
      最近更新 更多