首先,導(dǎo)入需要用到的庫(kù):
import numpy as np
創(chuàng)建一個(gè)矩陣和一個(gè)向量并輸出:
# 創(chuàng)建一個(gè)矩陣和一個(gè)向量
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
vector = np.array([1, 2, 3])
print(f"矩陣:{matrix}")
print(f"向量:{vector}")
計(jì)算矩陣的第一范數(shù):
# 計(jì)算矩陣的一范數(shù)
matrix_l1_norm = np.linalg.norm(matrix, ord=1)
print(f"矩陣的一范數(shù)為:{matrix_l1_norm}")
計(jì)算矩陣的第二范數(shù):
# 計(jì)算矩陣的二范數(shù)
matrix_l2_norm = np.linalg.norm(matrix, ord=2)
print(f"矩陣的二范數(shù)為:{matrix_l2_norm}")
計(jì)算矩陣的無(wú)窮范數(shù):
# 計(jì)算矩陣的無(wú)窮范數(shù)
matrix_inf_norm = np.linalg.norm(matrix, ord=np.inf)
print(f"矩陣的無(wú)窮范數(shù)為:{matrix_inf_norm}")
計(jì)算向量的第一范數(shù):
# 計(jì)算向量的一范數(shù)
vector_l1_norm = np.linalg.norm(vector, ord=1)
print(f"向量的一范數(shù)為:{vector_l1_norm}")
計(jì)算向量的第二范數(shù):
# 計(jì)算向量的二范數(shù)
vector_l2_norm = np.linalg.norm(vector, ord=2)
print(f"向量的二范數(shù)為:{vector_l2_norm}")
計(jì)算向量的無(wú)窮范數(shù):文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-855711.html
# 計(jì)算向量的無(wú)窮范數(shù)
vector_inf_norm = np.linalg.norm(vector, ord=np.inf)
print(f"向量的無(wú)窮范數(shù)為:{vector_inf_norm}")
完整代碼如下:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-855711.html
import numpy as np
# 創(chuàng)建一個(gè)矩陣和一個(gè)向量
matrix = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
vector = np.array([1, 2, 3])
print(f"矩陣:{matrix}")
print(f"向量:{vector}")
# 計(jì)算矩陣的一范數(shù)
matrix_l1_norm = np.linalg.norm(matrix, ord=1)
print(f"矩陣的一范數(shù)為:{matrix_l1_norm}")
# 計(jì)算矩陣的二范數(shù)
matrix_l2_norm = np.linalg.norm(matrix, ord=2)
print(f"矩陣的二范數(shù)為:{matrix_l2_norm}")
# 計(jì)算矩陣的無(wú)窮范數(shù)
matrix_inf_norm = np.linalg.norm(matrix, ord=np.inf)
print(f"矩陣的無(wú)窮范數(shù)為:{matrix_inf_norm}")
# 計(jì)算向量的一范數(shù)
vector_l1_norm = np.linalg.norm(vector, ord=1)
print(f"向量的一范數(shù)為:{vector_l1_norm}")
# 計(jì)算向量的二范數(shù)
vector_l2_norm = np.linalg.norm(vector, ord=2)
print(f"向量的二范數(shù)為:{vector_l2_norm}")
# 計(jì)算向量的無(wú)窮范數(shù)
vector_inf_norm = np.linalg.norm(vector, ord=np.inf)
print(f"向量的無(wú)窮范數(shù)為:{vector_inf_norm}")
到了這里,關(guān)于用python實(shí)現(xiàn)矩陣和向量的范數(shù)(包括一范數(shù),二范數(shù),無(wú)窮范數(shù))的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!