学习Numpy的必备知识
import numpy as np
# 定义一个矩阵
matrix = np.array([[1, 2], [3, 4]])
# 定义一个向量
vector = np.array([1, 2])
print("Matrix:\n", matrix)
print("Vector:\n", vector)
F = A * B
print("Element-wise Multiplication:\n", F)
#
G = np.dot(A, B)
print("Matrix Multiplication:\n", G)
# 逆矩阵
I = np.linalg.inv(A)
print("Inverse Matrix:\n", I)
-行列式
det = np.linalg.det(A)
print("Determinant:", det)
-其他运算
# 定义矩阵
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
# 矩阵加法
C = A + B
print("Matrix Addition:\n", C)
# 矩阵减法
D = A - B
print("Matrix Subtraction:\n", D)
# 标量乘法
scalar = 2
E = A * scalar
print("Scalar Multiplication:\n", E)
# 矩阵转置
H = A.T
print("Matrix Transpose:\n", H)
identity_matrix = np.eye(2)
print("Identity Matrix:\n", identity_matrix)
[ ]
[ ]
[ ]
[ ]
[ ]]
diagonal_matrix = np.diag([1, 2])
print("Diagonal Matrix:\n", diagonal_matrix)
[ ]
[ ]
[ ]]
eigenvalues, eigenvectors = np.linalg.eig(A)
print(eigenvalues)
# [-0.37228132 5.37228132]
print(eigenvectors)
# [[-0.82456484 -0.41597356]
# [ 0.56576746 -0.90937671]]
4、矩阵的实际应用案例
tcl调用Python实现两组节点最近距离计算 JinTian,公 众号:TodayCAEertcl调用Python实现两组节点最近距离计算