numpy - numpy - 在python中,最基本的线性代数方程

假设我有以下方程式系统:


x + y = 3


5x - y = 3



例如,类似于:


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


A'inverse = [3,3] # how to do this?



时间:

可以使用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)





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,这是我们期望的。

...