上传文件至 /
This commit is contained in:
32
yzz1.py
Normal file
32
yzz1.py
Normal 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)
|
||||
|
||||
# ======================
|
||||
# 方法2:NumPy库(工业界标准写法)
|
||||
# ======================
|
||||
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)) # 模长
|
||||
Reference in New Issue
Block a user