### RTSX Help: quaternion math operations

qadd, qsubtract, qmult, qdivide, qinv, qpower, qnorm, qunit, qscale

Various math operations on quaternion.

### Syntax

- q = qadd(q1, q2 )
- q = qsubtract(q1, q2)
- q = qmult(q1, q2)
- q = qdivide(q1, q2)
- qi = qinv(q)
- qp = qpower(q, p)
- qmag = qnorm(q)
- qu = qunit(q)
- qs = qscale(q, r)

### Input Arguments

- q, q1, q2 — a quaternion s <v1,v2,v3>
- p — integer
- r — real number between 0 – 1

### Output Arguments

- q, qi, qp, qu, qs — a quaternion s <v1,v2,v3>
- qmag — a scalar representing norm of quaternion

### Description

A quaternion is a compact method of representing a 3D rotation that has computational advantages including speed and numerical robustness. A quaternion has 2 parts, a scalar s, and a vector v and is typically written: q = s <vx, vy, vz> The following math operations on quaternions are implemented.

- q = qadd(q1, q2 ) and q = qsubtract(q1, q2 ) add/subtract two quaternions (or arrays of quaternions).
- q = qmult(q1, q2) multiplies two quaternions. q = qmult(q1, v) rotates vector v by unit-quaternion q1. q = qmult(q1, s) mulitplies quaternion q1 by a scalar s.
- q = qdivide(q1, q2) returns a quaternion formed by Hamilton product of q1 and inv(q2). q = qdivide(q1, s) performs element-wise division of quaternion elements by scalar s.
- qi = qinv(q) inverts a unit quaternion.
- qp = qpower(q, p) raises quaternion to integer power.
- qmag = qnorm(q) computes scalar norm or magnitude of quaternion q. This is simply an Euclidean norm of vector representation of q. A unit quaternion has norm of 1.
- qu = qunit(q) unitizes a quaternion.
- qs = qscale(q, r) interpolates rotations expressed by quaternion. qs is a unit quaternion interpolated between identity for r = 0 to q for s = 1. This si a spherical linear interpolation (slerp) that can be interpreted as interpolation along a great circle arc on a sphere.

### See also

- Quaternion — create a quaternion data structure
- isquaternion, isqequal — test functions on quaternions
- q2str, q2tr, q2vec, tr2q — conversion between quaternion and other types of data
- qinterp — interpolate quaternions