Code covered by the BSD License  

Highlights from
Quaternion Toolbox

3.6

3.6 | 5 ratings Rate this file 40 Downloads (last 30 days) File Size: 25.4 KB File ID: #1176

Quaternion Toolbox

by Jay St. Pierre

 

18 Dec 2001 (Updated 26 Jul 2009)

Vectorized quaternion functions

| Watch this File

File Information
Description

Version 1.3 (JASP) 26-Jul-2009

For purposes of these tools, a quaternion, q, is just a four element vector where q(1:3) is the "imaginary" or "vector" portion of the hypercomplex number, and q(4) is the "real" or "scalar" portion. Consequently, if q represents a rotation, then:

  q(1) = v1*sin(phi/2)
  q(2) = v2*sin(phi/2)
  q(3) = v3*sin(phi/2)
  q(4) = cos(phi/2)

where phi is the amount of rotation about the unit vector [v1 v2 v3].

All tools are vectorized, so "vectors" of quaternions (4xN or Nx4 matrices) can be handled as well. Since it is most common to work with normalized quaternions (also referred to as "unit quaternions" and "versors"), if a set of 4 quaternions, i.e., a 4x4 matrix, is input, the tools will attempt to determine the shape of the component quaternions (4x1 or 1x4) based on whether the rows or columns are normalized.

Of course, some of the tools, like QDECOMP, only make sense for normalized quaternions, and thus those tools enforce normality via QNORM.

  isq - determines whether or not input is a quaternion
  isnormq - determines whether or not input is a normalized quaternion

  qconj - quaternion conjugate
  qnorm - normalize quaternion
  qmult - multiply quaternions

  qdecomp - decompose quaternion into unit vector and rotation angle

  qcvq - operation on vector: qconj(q) v q
  qvqc - operation on vector: q v qconj(q)

Because the author uses the convention described in "Spacecraft Attitude Determination and Control" (Wertz, 1978), the following aliases exist:

  qvxform - quaternion/vector transform (alias for qcvq)
  qvrot - quaternion/vector rotation (alias for qvqc)

Likewise, the following operations assume the relationship between the DCM and the quaternion is: R*v = qvxform(q, v) = qcvq(q, v). That is, the q that performs the equivalent operation on v is the "right hand quaterion".

  q2dcm - quaternion to direction cosine matrix
  dcm2q - direction cosine matrix to quaternion

Note that many more recent applications, particularly computer graphics libraries, choose the opposite convention. That is, the equivalent "q" is the "left hand quaternion" and consequently the qvxform/qvrot aliases and q2dcm and dcm2q functions would be "backward".

See also qlib, the Quaternion block library for simulink.

MATLAB release MATLAB 7.5 (R2007b)
Other requirements If you want to run the unit tests, you will need the test_tools package found elsewhere in this archive.
Tags for This File  
Everyone's Tags
aerospace, linear algebra, mathematics, quaternion(4), quaternions(2)
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
23 Oct 2009 Brandon

Works well but not very quickly. I think I'll try to find a mex implementation.

02 Aug 2009 Jay St. Pierre

Lars Barring: It looks like you have a different "qnorm" in your search path prior to the quaternions toolbox. Do a "which qnorm" at the MATLAB command line.

-Jay

28 Jul 2009 Lars Barring

QNORM fails the unit test with the following output (R2008b):
============================================
Unit Test for: qnorm
============================================
----------------------------------------
Insufficient Arguments
----------------------------------------
Function Call: qnorm

Expected Error:
qnorm() requires one input argument

error ~= expected_error ***FAILED***

ans =
Input argument "p" is undefined.

err_string =
qnorm() requires one input argument

----------------------------------------
Invalid Input
----------------------------------------
Function Call: qnorm(1)

Expected Error:
Invalid input: must be a quaternion or a vector of quaternions

ans =
Inf

error ~= expected_error ***FAILED***

ans =
Input argument "p" is undefined.

err_string =
Invalid input: must be a quaternion or a vector of quaternions

----------------------------------------
Column of two quaternions
----------------------------------------

q1 =
1 2 3 4
q2 =
4 3 2 1

??? Error using ==> qnorm at 13
A probability should be 0<=p<=1, please!

Error in ==> check_value at 39
l_test_value = evalin('base', test_value);

Error in ==> test_qnorm at 36
failures=failures+check_value(truth_value, test_value);

Error in ==> unit_test at 25
test_qnorm; Failures=Failures+failures;

20 Aug 2007 Simon Child

NOTE:
A much better approach than testing for absolute equality is to calculate (q_calc - q_exp)/(q_calc + q_exp) and compare that value with eps('double')

09 Dec 2006 Martin Giacomelli

dcm2q([sqrt(.5) 0 -sqrt(.5) 0 ]) returns
[0.7071,0, 0.7071, 0] instead of
[-sqrt(.5) 0 sqrt(.5) 0 ]

25 Jan 2006 Hu Hansan

wrong answer for rotation. qvrot function is wrong.

04 Nov 2005 max scoo

nice one!

16 Jul 2003 Anuscheh Nawaz

nicely structured! makes life a lot easier!!
thank you.

Updates
26 Jul 2009

- Changed license from GPL to BSD.
- Fixed dcm2q algorithm to avoid numerical problems.
- Added qcvq and qvqc functions and elaborated on the choice of q when using quaternions to perform matrix-equivalent operations.

Contact us