# 陣列程式設計

``````nums = [1, 2, 3]
for i in range(len(nums)):
nums[i] += 10
``````

``````nums1 = [1, 2, 3]
nums2 = [n + 10 for n in nums1]
``````

``````nums1 = [1, 2, 3]
nums2 = list(map(lambda n: n + 10, nums1))
``````

``````import numpy as np

nums1 = np.array([1, 2, 3])
nums2 = nums1 + 10
``````

``````nums1 = [1, 2, 3]
nums2 = [4, 5, 6]
nums3 = [nums1[i] + nums2[i] for i in range(len(nums1))]
``````

``````import numpy as np

nums1 = np.array([1, 2, 3])
nums2 = np.array([4, 5, 6])

nums3 = nums1 + nums2
``````

``````import numpy as np

v1 = np.array([1, 2, 3])  # 以列（row）表示的向量
v2 = np.array([4, 5, 6])
v3 = v1 * 2               # 縮放
v4 = v1 + v2              # 相加
v5 = np.dot(v1, v2)       # 點積
v6 = np.cross(v1, v2)     # 叉積
``````

``````import numpy as np

m1 = np.array([
[ 1,  2, 3, 4],
[ 5,  6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]
])

m2 = np.array([
[13, 14, 15, 16],
[ 9, 10, 11, 12],
[ 5,  6, 7, 8],
[ 1,  2, 3, 4]
])

m3 = m1 @ m2  # 也可以寫 np.dot(m1, m2)
``````

``````import numpy as np

tx = 10
ty = 5
x = 10
y = 20

t = np.array([
[1, 0, tx],
[0, 1, ty],
[0, 0,  1]
])

v = t @ np.array([x, y, 1])
``````