# psychopy.tools.mathtools.multMatrix¶

psychopy.tools.mathtools.multMatrix(matrices, reverse=False, out=None, dtype=None)[source]

Chain multiplication of two or more matrices.

Multiply a sequence of matrices together, reducing to a single product matrix. For instance, specifying matrices the sequence of matrices (A, B, C, D) will return the product (((AB)C)D). If reverse=True, the product will be (A(B(CD))).

Alternatively, a 3D array can be specified to matrices as a stack, where an index along axis 0 references a 2D slice storing matrix values. The product of the matrices along the axis will be returned. This is a bit more efficient than specifying separate matrices in a sequence, but the difference is negligible when only a few matrices are being multiplied.

Parameters:
• matrices (list, tuple or ndarray) – Sequence or stack of matrices to multiply. All matrices must have the same dimensions.

• reverse (bool, optional) – Multiply matrices right-to-left. This is useful when dealing with transformation matrices, where the order of operations for transforms will appear the same as the order the matrices are specified. Default is ‘False’. When True, this function behaves similarly to `concatenate()`.

• out (ndarray, optional) – Optional output array. Must be same shape and dtype as the expected output if out was not specified.

• dtype (dtype or str, optional) – Data type for computations can either be ‘float32’ or ‘float64’. If out is specified, the data type of out is used and this argument is ignored. If out is not provided, ‘float64’ is used by default.

Returns:

Matrix product.

Return type:

ndarray

Notes

• You may use numpy.matmul when dealing with only two matrices instead of multMatrix.

• If a single matrix is specified, the returned product will have the same values.

Examples

Chain multiplication of SRT matrices:

```translate = translationMatrix((0.035, 0, -0.5))
rotate = rotationMatrix(90.0, (0, 1, 0))
scale = scaleMatrix(2.0)

SRT = multMatrix((translate, rotate, scale))
```

Same as above, but matrices are in a 3x4x4 array:

```matStack = np.array((translate, rotate, scale))

# or ...
# matStack = np.zeros((3, 4, 4))
# matStack[0, :, :] = translate
# matStack[1, :, :] = rotate
# matStack[2, :, :] = scale

SRT = multMatrix(matStack)
```

Using reverse=True allows you to specify transformation matrices in the order which they will be applied:

```SRT = multMatrix(np.array((scale, rotate, translate)), reverse=True)
```

Back to top