logo back up home forward   further reading more topics »

Maths - Conversion Quaternion to Matrix - re: interesting method from Jay Ryness

By: thejaybird (thejaybird) - 2008-01-09 11:33
I've been using this method for many years but have never seen it published anywhere. It's not necessarily the fastest method, but is the most elegant IMO. On modern platforms with SIMD and shuffling, it's very fast. 
 
Matrix44 CreateRotation(const Quaternion& quat) 

Matrix44 m1( 
quat.w, quat.z, -quat.y, quat.x, 
-quat.z, quat.w, quat.x, quat.y, 
quat.y, -quat.x, quat.w, quat.z, 
-quat.x, -quat.y, -quat.z, quat.w ); 
 
Matrix44 m2( 
quat.w, quat.z, -quat.y, -quat.x, 
-quat.z, quat.w, quat.x, -quat.y, 
quat.y, -quat.x, quat.w, -quat.z, 
quat.x, quat.y, quat.z, quat.w ); 
 
return m1 * m2; 

 
It calculates the product of two simple matrices. The matrices have some very interesting features. Notice that they are identical and symmetric, except for the distribution of negations. The upper 3x3 is the same for both matrices, whereas the 4th row and column are transposed. I've put quite a bit of effort into finding ways to exploit that last bit of apparent symmetry/similarity, but haven't been able to simplify it any further. If anyone has any insight into this, I would be very keen to hear it. 
 
Note that it's assumed the quaternion is unit length. Otherwise, the resulting matrix will be scaled.
By: Martin Baker (martinbakerProject Admin) - 2008-01-10 01:48
This is very interesting, I don't know why this works, My first guess is that it might be related to either: 
 
* constructing a rotation from two reflections? 
* Pauli matricies? 
 
but I can't make either work at the moment, perhaps someone else can help? 
 
I would like to put this on the web page, It this alright with you? If so how would you like to be named: thejaybird? 
 
Thanks, 
 
Martin 
 
 
2 reflections 
------------- 
A single reflection in a line Px,Py,Pz is given by the symmetrical(including signs) matrix: 
 
-Px² + Pz²+ Py² - 2 * Px * Py - 2 * Px * Pz 
- 2 * Py * Px -Py² + Px² + Pz² - 2 * Py * Pz 
- 2 * Pz * Px -2 * Pz * Py -Pz² + Py² + Px² 
 
So could each of the 4x4 matricies be related to a reflection? I can't make this work. 
 
 
Pauli matricies 
--------------- 
The Pauli matricies usually represent i,j,k as: 
 
0 -i 
i 0 
 
0 1 
1 0 
 
1 0 
0 -1 
 
but we can also expand this out to a 4x4 version (I'm not sure I've got the signs correct): 
 
0 0 +1 0 | 0 0 0 1 | 0 +1 0 0 
0 0 0 -1 | 0 0 1 0 | +1 0 0 0 
-1 0 0 0 | 0 1 0 0 | 0 0 0 -1 
0 +1 0 0 | 1 0 0 0 | 0 0 -1 0 
 
We can take a linear combination of these (with the identity matrix) to give somthing like: 
 
+w x +y +z 
+x w +z -y 
-y z +w -x 
+z y -x +w 
 
So we can transform points by using the 'sandwich product' like: 
p' = [M][p][M]^t 
 
I was wondering if there is some rule we could use to reverse the order to give some variation of: 
[M][M]^t

By: thejaybird (thejaybird) - 2008-01-10 16:09
Yes, please go ahead and put it up on the website, by all means. You can use my real name, Jay Ryness. 
 
I don't have much to add, but I am reminded of some work I was doing years ago with geometric algebra. In GA, the product of two multivectors can be implemented as a 4x4 matrix multiplication, where the operands look basically like the multivector was scrambled into a 4x4 matrix not too unlike the operands in the quaternion product above, except that a multivector in GA has 8 scalar components rather than 4, and I think some of the expressions in the matrix may have been slightly more involved than a simple negation, but not much. Anyway, rotors (which are a specialized form of multivectors, where 4 of the 8 scalar components happen to be zero--I'm sure know all this stuff much better than I) are, IIRC, isomorphic to quaternions, or close enough. I haven't verified it, but I would venture to guess that if one were to write out the GA product of two rotors as a 4x4 matrix multiplication, it would simplify to something very similar if not identical to the quaternion product implementation above. 
 
It's also worth noting that rotors are logically constructed as the composition of two reflections, and all of this is tightly coupled with complex numbers, quaternions, Pauli matrices and the like. So, I think you're definitely on a good track with your approach. 
 
I should point out that I'm not a mathematician and it's been years since I delved into this stuff, and furthermore I'm shooting from the hip, so take my abstract mathematical observations for what they're worth. 
 
Anyway, thanks for taking an interest. I'll be keeping an eye out for developments. 
 
-Jay

Follow up message

Following this thread Tim Meehan has kindly sent me a message telling me that this article on Wikipedia has a section on isoclinic decomposition about how a quaternion can be converted to a 4x4 matrix by performing matrix multiplication on two simpler 4x4 matricies.

I think this sheds light on the subject and my intuition tells me this is important, I'll have to give this some thought.


metadata block
see also:

 

Correspondence about this page

Book Shop - Further reading.

Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.

cover us uk de jp fr ca Matrix Computations

Commercial Software Shop

Where I can, I have put links to Amazon for commercial software, not directly related to the software project, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.

 

cover Dark Basic Professional Edition - It is better to get this professional edition

cover This is a version of basic designed for building games, for example to rotate a cube you might do the following:
make object cube 1,100
for x=1 to 360
rotate object 1,x,x,0
next x

cover Game Programming with Darkbasic - book for above software

Can you help?

Please send me any improvements to here. I would appreciate ideas to make the pages more useful including error correction, ideas for new pages, improvements to wording. It helps if you quote the full URL of the page.

 

progam

I am working on a project which uses these principles, if you would like to help me with this you are welcome to join in, here:

http://sourceforge.net/projects/mjbworld/

This site may have errors. Don't use for critical systems.

Copyright (c) 1998-2008 Martin John Baker - All rights reserved.