【numpy】实现线代之矩阵
算法包下载链接:https://download.csdn.net/download/qq_42629529/79481514
import numpy as np
from numpy.linalg import eig
from scipy.linalg import svd
if __name__ == "__main__":
A = np.array([[1, 2], [3, 4]])
print(A)
print(A.shape)
print(A.T)
print(A[1, 1])
print(A[0])
print(A[:, 0])
print(A[1, :])
B = np.array([[5, 6], [7, 8]])
print(A + B)
print(A - B)
print(10 * A)
print(A * 10)
print(A * B)
print(A.dot(B))
p = np.array([10, 100])
print(A + p)
print(A + 1)
print(A.dot(p))
I = np.identity(2)
print(I)
print(A.dot(I))
print(I.dot(A))
invA = np.linalg.inv(A)
print(invA)
print(invA.dot(A))
print(A.dot(invA))
A1 = np.array([[4, -2],
[1, 1]]);
eigenvalues1, eigenvectors1 = eig(A1);
print(eigenvalues1)
print(eigenvectors1)
print()
A2 = np.array([[0, 1],
[1, 0]]);
eigenvalues2, eigenvectors2 = eig(A2);
print(eigenvalues2)
print(eigenvectors2)
print()
A3 = np.array([[0, -1],
[1, 0]]);
eigenvalues3, eigenvectors3 = eig(A3);
print(eigenvalues3)
print(eigenvectors3)
print()
A4 = np.array([[1, 0],
[0, 1]]);
eigenvalues4, eigenvectors4 = eig(A4);
print(eigenvalues4)
print(eigenvectors4)
print()
A5 = np.array([[3, 1],
[0, 3]]);
eigenvalues5, eigenvectors5 = eig(A5);
print(eigenvalues5)
print(eigenvectors5)
print()
A = np.array([[1, 2],
[3, 4],
[5, 6]])
U, s, VT = svd(A)
print(U)
print(s)
print(VT)
print()
Sigma = np.zeros(A.shape)
for i in range(len(s)):
Sigma[i][i] = s[i]
print(Sigma)
print(U.dot(Sigma).dot(VT))
|