上传文件至 /

This commit is contained in:
2026-04-21 11:29:52 +08:00
parent c7d06741f4
commit 0d833c7817
3 changed files with 73 additions and 0 deletions

32
yzz1.py Normal file
View File

@@ -0,0 +1,32 @@
# 定义向量 A 和 B
A = [3, 4]
B = [1, 2]
# ======================
# 方法1纯Python手动计算适合理解原理
# ======================
print("==== 纯Python计算结果 ====")
# 1. 向量加法 A + B
add_result = [A[0]+B[0], A[1]+B[1]]
print("A + B =", add_result)
# 2. 数乘 2×A
mul_result = [2*A[0], 2*A[1]]
print("2 × A =", mul_result)
# 3. 向量A的长度勾股定理 √(x²+y²)
import math
norm_A = math.sqrt(A[0]**2 + A[1]**2)
print("A 的长度(模)= %.2f" % norm_A)
# ======================
# 方法2NumPy库工业界标准写法
# ======================
print("\n==== NumPy计算结果 ====")
import numpy as np
A_np = np.array([3, 4])
B_np = np.array([1, 2])
print("A + B =", A_np + B_np) # 加法
print("2 × A =", 2 * A_np) # 数乘
print("A 的长度(模)= %.2f" % np.linalg.norm(A_np)) # 模长