# times, .*

Element-wise quaternion multiplication

## Syntax

``quatC = A.*B``

## Description

example

````quatC = A.*B` returns the element-by-element quaternion multiplication of quaternion arrays.You can use quaternion multiplication to compose rotation operators: To compose a sequence of frame rotations, multiply the quaternions in the same order as the desired sequence of rotations. For example, to apply a p quaternion followed by a q quaternion, multiply in the order pq. The rotation operator becomes ${\left(pq\right)}^{\ast }v\left(pq\right)$, where v represents the object to rotate in quaternion form. * represents conjugation.To compose a sequence of point rotations, multiply the quaternions in the reverse order of the desired sequence of rotations. For example, to apply a p quaternion followed by a q quaternion, multiply in the reverse order, qp. The rotation operator becomes $\left(qp\right)v{\left(qp\right)}^{\ast }$. ```

## Examples

collapse all

Create two vectors, `A` and `B`, and multiply them element by element.

```A = quaternion([1:4;5:8]); B = A; C = A.*B```
```C = 2x1 quaternion array -28 + 4i + 6j + 8k -124 + 60i + 70j + 80k ```

Create two 3-by-3 arrays, `A` and `B`, and multiply them element by element.

```A = reshape(quaternion(randn(9,4)),3,3); B = reshape(quaternion(randn(9,4)),3,3); C = A.*B```
```C = 3x3 quaternion array 0.60169 + 2.4332i - 2.5844j + 0.51646k -0.49513 + 1.1722i + 4.4401j - 1.217k 2.3126 + 0.16856i + 1.0474j - 1.0921k -4.2329 + 2.4547i + 3.7768j + 0.77484k -0.65232 - 0.43112i - 1.4645j - 0.90073k -1.8897 - 0.99593i + 3.8331j + 0.12013k -4.4159 + 2.1926i + 1.9037j - 4.0303k -2.0232 + 0.4205i - 0.17288j + 3.8529k -2.9137 - 5.5239i - 1.3676j + 3.0654k ```

Note that quaternion multiplication is not commutative:

`isequal(C,B.*A)`
```ans = logical 0 ```

Create a row vector `a` and a column vector `b`, then multiply them. The 1-by-3 row vector and 4-by-1 column vector combine to produce a 4-by-3 matrix with all combinations of elements multiplied.

`a = [zeros('quaternion'),ones('quaternion'),quaternion(randn(1,4))]`
```a = 1x3 quaternion array 0 + 0i + 0j + 0k 1 + 0i + 0j + 0k 0.53767 + 1.8339i - 2.2588j + 0.86217k ```
`b = quaternion(randn(4,4))`
```b = 4x1 quaternion array 0.31877 + 3.5784i + 0.7254j - 0.12414k -1.3077 + 2.7694i - 0.063055j + 1.4897k -0.43359 - 1.3499i + 0.71474j + 1.409k 0.34262 + 3.0349i - 0.20497j + 1.4172k ```
`a.*b`
```ans = 4x3 quaternion array 0 + 0i + 0j + 0k 0.31877 + 3.5784i + 0.7254j - 0.12414k -4.6454 + 2.1636i + 2.9828j + 9.6214k 0 + 0i + 0j + 0k -1.3077 + 2.7694i - 0.063055j + 1.4897k -7.2087 - 4.2197i + 2.5758j + 5.8136k 0 + 0i + 0j + 0k -0.43359 - 1.3499i + 0.71474j + 1.409k 2.6421 - 5.32i - 2.3841j - 1.3547k 0 + 0i + 0j + 0k 0.34262 + 3.0349i - 0.20497j + 1.4172k -7.0663 - 0.76439i - 0.86648j + 7.5369k ```

## Input Arguments

collapse all

Array to multiply, specified as a quaternion, an array of quaternions, a real scalar, or an array of real numbers.

`A` and `B` must have compatible sizes. In the simplest cases, they can be the same size or one can be a scalar. Two inputs have compatible sizes if, for every dimension, the dimension sizes of the inputs are the same or one of them is 1.

Data Types: `quaternion` | `single` | `double`

Array to multiply, specified as a quaternion, an array of quaternions, a real scalar, or an array of real numbers.

`A` and `B` must have compatible sizes. In the simplest cases, they can be the same size or one can be a scalar. Two inputs have compatible sizes if, for every dimension, the dimension sizes of the inputs are the same or one of them is 1.

Data Types: `quaternion` | `single` | `double`

## Output Arguments

collapse all

Quaternion product, returned as a scalar, vector, matrix, or multidimensional array.

Data Types: `quaternion`

## Algorithms

collapse all

### Quaternion Multiplication by a Real Scalar

Given a quaternion,

`$q={a}_{\text{q}}+{b}_{\text{q}}\text{i}+{c}_{\text{q}}\text{j}+{d}_{\text{q}}\text{k,}$`

the product of q and a real scalar β is

`$\beta q=\beta {a}_{\text{q}}+\beta {b}_{\text{q}}\text{i}+\beta {c}_{\text{q}}\text{j}+\beta {d}_{\text{q}}\text{k}$`

### Quaternion Multiplication by a Quaternion Scalar

The definition of the basis elements for quaternions,

`${\text{i}}^{2}={\text{j}}^{2}={\text{k}}^{2}=\text{ijk}=-1\text{\hspace{0.17em}},$`

can be expanded to populate a table summarizing quaternion basis element multiplication:

 1 i j k 1 1 i j k i i −1 k −j j j −k −1 i k k j −i −1

When reading the table, the rows are read first, for example: ij = k and ji = −k.

Given two quaternions, $q={a}_{\text{q}}+{b}_{\text{q}}\text{i}+{c}_{\text{q}}\text{j}+{d}_{\text{q}}\text{k,}$ and $p={a}_{\text{p}}+{b}_{\text{p}}\text{i}+{c}_{\text{p}}\text{j}+{d}_{\text{p}}\text{k}$, the multiplication can be expanded as:

`$\begin{array}{l}z=pq=\left({a}_{\text{p}}+{b}_{\text{p}}\text{i}+{c}_{\text{p}}\text{j}+{d}_{\text{p}}\text{k}\right)\left({a}_{\text{q}}+{b}_{\text{q}}\text{i}+{c}_{\text{q}}\text{j}+{d}_{\text{q}}\text{k}\right)\\ \text{ }\text{\hspace{0.17em}}\text{\hspace{0.17em}}\text{\hspace{0.17em}}={a}_{\text{p}}{a}_{\text{q}}+{a}_{\text{p}}{b}_{\text{q}}\text{i}+{a}_{\text{p}}{c}_{\text{q}}\text{j}+{a}_{\text{p}}{d}_{\text{q}}\text{k}\\ \text{ }\text{ }+{b}_{\text{p}}{a}_{\text{q}}\text{i}+{b}_{\text{p}}{b}_{\text{q}}{\text{i}}^{2}+{b}_{\text{p}}{c}_{\text{q}}\text{ij}+{b}_{\text{p}}{d}_{\text{q}}\text{ik}\\ \text{ }\text{ }+{c}_{\text{p}}{a}_{\text{q}}\text{j}+{c}_{\text{p}}{b}_{\text{q}}\text{ji}+{c}_{\text{p}}{c}_{\text{q}}{\text{j}}^{2}+{c}_{\text{p}}{d}_{\text{q}}\text{jk}\\ \text{ }\text{ }+{d}_{\text{p}}{a}_{\text{q}}k+{d}_{\text{p}}{b}_{\text{q}}\text{ki}+{d}_{\text{p}}{c}_{\text{q}}\text{kj}+{d}_{\text{p}}{d}_{\text{q}}{\text{k}}^{2}\end{array}$`

You can simplify the equation using the quaternion multiplication table.

`$\begin{array}{l}z=pq\text{\hspace{0.17em}}={a}_{\text{p}}{a}_{\text{q}}+{a}_{\text{p}}{b}_{\text{q}}\text{i}+{a}_{\text{p}}{c}_{\text{q}}\text{j}+{a}_{\text{p}}{d}_{\text{q}}\text{k}\\ \text{ }\text{ }+{b}_{\text{p}}{a}_{\text{q}}\text{i}-{b}_{\text{p}}{b}_{\text{q}}+{b}_{\text{p}}{c}_{\text{q}}\text{k}-{b}_{\text{p}}{d}_{\text{q}}\text{j}\\ \text{ }\text{ }+{c}_{\text{p}}{a}_{\text{q}}\text{j}-{c}_{\text{p}}{b}_{\text{q}}\text{k}-{c}_{\text{p}}{c}_{\text{q}}+{c}_{\text{p}}{d}_{\text{q}}\text{i}\\ \text{ }\text{ }+{d}_{\text{p}}{a}_{\text{q}}k+{d}_{\text{p}}{b}_{\text{q}}\text{j}-{d}_{\text{p}}{c}_{\text{q}}\text{i}-{d}_{\text{p}}{d}_{\text{q}}\end{array}$`

## References

[1] Kuipers, Jack B. Quaternions and Rotation Sequences: A Primer with Applications to Orbits, Aerospace, and Virtual Reality. Princeton, NJ: Princeton University Press, 2007.

## Version History

Introduced in R2018b