矩陣相乘
矩陣相乘
Example Code
- import numpy as np
- import math
- def rotationX(theta):
- Matrix=[[1, 0, 0], [0, math.cos(theta), math.sin(theta)], [0, -math.sin(theta), math.cos(theta)]]
- return Matrix
- def rotationY(theta):
- Matrix=[[math.cos(theta), 0, math.sin(theta)], [0, 1, 0], [-math.sin(theta), 0, math.cos(theta)]]
- return Matrix
- def rotationZ(theta):
- Matrix=[[math.cos(theta), math.sin(theta), 0], [-math.sin(theta), math.cos(theta), 0], [0, 0, 1]]
- return Matrix
- vectorA=[1,0,0]
- print(np.matmul(rotationX(30/180*math.pi), vectorA))
- print(np.matmul(rotationY(45/180*math.pi), vectorA))
- print(np.matmul(rotationZ(90/180*math.pi), vectorA))
Note and Comments
- import numpy as np: 讀入numpy函式庫並命名為np
- def rotationX(theta): 定義繞X軸旋轉的旋轉矩陣, 旋轉角度隨引數而變動
- Matrix=[[1, 0, 0], [0, math.cos(theta), math.sin(theta)], [0, -math.sin(theta), math.cos(theta)]]
- return Matrix
- vectorA=[1,0,0] 宣告一個單位矩陣vectorA
- np.matmul(rotationX(n/180*math.pi), vectorA) 將單位向量vectorA繞著X軸旋轉n度
result
向量[1,0,0]在繞X軸旋轉任何角度後仍是[1,0,0]
向量[1,0,0]在繞Y軸旋轉60度後是[0.707, 0, -0.707]
向量[1,0,0]在繞Z軸旋轉90度後是[6*10^17, -1, 0] 因為浮點數精度的緣故,X軸仍有少許分量