I'm trying to code a function that will rotate a vector around an axis by an angle. Though I am running into some problems.I will show the trouble-shooting code I wrote.
import numpy as np
def unit_vector(vec):
vec = np.array(vec, float)
unit_vec = []
for i in range(len(vec)):
unit_vec.append(vec[i]/np.linalg.norm(vec))
return np.array(unit_vec, float)
def rotation(vector,axis, angle):
vector = np.array(vector, float)
axis = np.array(vector, float)
first = vector * np.cos(angle)
second = np.cross(vector, unit_vector(axis))
third = unit_vector(axis)
fourth = np.dot(vector, unit_vector(axis))
fifth = 1 - np.cos(angle)
print("first : {0} \nSecond : {1} \nThird : {2}\nFourth : {3}\nFifth : {4}\n ".format(list(first), list(second), list(third),fourth, fifth))
rotation([0,1,0],[0,0,1],np.pi/2)
Here is the output of my program :
first : [0.0, 6.123233995736766e-17, 0.0]
Second : [0.0, 0.0, 0.0]
Third : [0.0, 1.0, 0.0]
Fourth : 1.0
Fifth : 0.9999999999999999
I was wondering why the second and fourth values are [0,0,0] and 1 respectively. They should only output these values if the vector and the axis are both facing in the same direction (which they are not). Even when I print these variables outside of the function they output correctly. What is causing the program to get the second and fourth variables wrong?
In the second line of the
rotationprocedure. axis has been set tonp.array(vector,float)and notnp.array( axis, float.