This notebook demonstrates how to use clifford
to implement rotations in three dimensions using euler angles, rotation matices and quaternions. All of these forms are derived from the more general rotor form, which is provided by GA. Conversion from the rotor form to a matrix reprentation is shown, and takes about three lines of code. We start with euler angles.
A common way to parameterize rotations in three dimensions is through Euler Angles.
"Any orientation can be achieved by composing three elemental rotations. The elemental rotations can either occur about the axes of the fixed coordinate system (extrinsic rotations) or about the axes of a rotating coordinate system, which is initially aligned with the fixed one, and modifies its orientation after each elemental rotation (intrinsic rotations). " -wikipedia
The animation below shows an intrinsic rotation model as each elemental rotation is applied. Label the left, right, and vertical blue-axes as e1,e2, and e3, respectively. The series of rotations can be described:
So the elemental rotations are about e3,e′1,e″3-axes, respectively.
from IPython.display import Image,SVG
Image(url='_static/Euler2a.gif') # taken from wikipedia
Following Sec. 2.7.5 from "Geometric Algebra for Physicists", we first rotate an angle ϕ about the e3-axis, which is equivalent to rotating in the e12-plane. This is done with the rotor
Rϕ=e−ϕ2e12Next we rotate about the rotated e1-axis, which we label e′1. To find where this is, we can rotate the axis,
e′1=Rϕe1~RϕThe plane corresponding to this axis is found by taking the dual of e′1
IRϕe1~Rϕ=Rϕe23~RϕWhere we have made use of the fact that the psuedo-scalar commutes in G3. Using this result, the second roation by angle θ about the e′1-axis is then ,
Rθ=eθ2Rϕe23~RϕHowever, noting that
eRϕe23~Rϕ=Rϕee23~RϕAllows us to write the second rotation by angle θ about the e′1-axis as
Rθ=Rϕeθ2e23~RϕSo, the combonation of the first two elemental rotations equals,
RθRϕ=Rϕeθ2e23~RϕRϕ=e−ϕ2e12e−θ2e23This pattern can be extended to the third elemental rotation of angle ψ in the twice-rotated e1-axis, creating the total rotor
R=e−ϕ2e12e−θ2e23e−ψ2e12First, we initialize the algbera and assign the variables
from numpy import e,pi
from clifford import Cl
layout, blades = Cl(3) # create a 3-dimensional clifford algebra
locals().update(blades) # lazy way to put entire basis in the namespace
Next we define a function to produce a rotor given euler angles
def R_euler(phi, theta,psi):
Rphi = e**(-phi/2.*e12)
Rtheta = e**(-theta/2.*e23)
Rpsi = e**(-psi/2.*e12)
return Rphi*Rtheta*Rpsi
For example, using this to create a rotation similar to that shown in the animation above,
R = R_euler(pi/4, pi/4, pi/4)
R
A Rotor in 3D space is a unit quaternion, and so we have essentially created a function that converts Euler angles to quaternions. All you need to do is interpret the bivectors as i,j, and k's. See the page "Interfacing Other Mathematical Systems", for more on quaternions.
The matrix representation for a rotation can defined as the result of rotating an ortho-normal frame. Rotating an ortho-normal frame can be done easily,
A = [e1,e2,e3] # initial ortho-normal frame
B = [R*a*~R for a in A] # resultant frame after rotation
B
The components of this frame are the rotation matrix, so we just enter the frame componenets into a matrix.
from numpy import array
M = [float(b|a) for b in B for a in A] # you need float() due to bug in clifford
M = array(M).reshape(3,3)
M
Thats a rotation matrix.
In 3 Dimenions, there is a simple formula which can be used to directly transform a rotations matrix into a rotor. For arbitrary dimensions you have to use a different algorithm (see orthoMat2Verser()
in clifford.tools
). Anyway, in 3 dimensions there is a closed form solution, as described in Sec. 4.3.3 of "Geometric Algebra for Physicists". Given a rotor R which transforms an orthonormal frame A=ak into B=bk as such,
R is given by
R=1+akbk|1+akbk|So, if you want to convert from a rotation matrix into a rotor, start by converting the matrix M
into a frame B. (You could do this with loop if you want.)
B = [M[0,0]*e1 + M[1,0]*e2 + M[2,0]*e3,
M[0,1]*e1 + M[1,1]*e2 + M[2,1]*e3,
M[0,2]*e1 + M[1,2]*e2 + M[2,2]*e3]
B
Then implement the formula
A = [e1,e2,e3]
R = 1+sum([A[k]*B[k] for k in range(3)])
R = R/abs(R)
R
blam.