A robot vision pipeline often holds a batch of rotation matrices and a batch of displacement vectors. Each vector needs the rotation for the sample that produced it. NumPy can calculate that operation with one expression, provided the batch dimension represents the same ordered samples in both arrays. A valid array shape can confirm that a multiplication is possible while leaving the sample association unchecked.

The fixture uses two planar rotations and two invented displacement vectors. It ran in NumPy 2.5.2 and uses integer values so the results can also be checked by hand. The rotations are mathematical examples. They describe no camera calibration, robot trajectory, or field measurement.

Define which sample owns each matrix

Suppose a vision service stores one rotation per observation. For this fixture, the first rotation is the identity. The second turns a column vector 90 degrees counterclockwise in a Cartesian plane with x to the right and y up. The two observations have local vectors [2, 1] and [1, 3], in that order.

import numpy as np

rotations = np.array([

[[1, 0], [0, 1]],

[[0, -1], [1, 0]],

])

vectors = np.array([[2, 1], [1, 3]])

The first dimension in each array indexes observations. The remaining matrix dimensions represent output and input coordinates. Each vector has one input-coordinate dimension. The operation uses column-vector multiplication even though the input table stores one vector per row. Record that convention along with the direction of the transform. An inverse calibration or an image coordinate system with y down can require a different matrix.

Use one shared batch label for pairwise multiplication

In the Einstein summation expression below, b is the shared observation index. Label j selects an input coordinate and i selects an output coordinate. Keeping b and i in the explicit output leaves one result vector per observation. The calculation sums over j.

paired = np.einsum("bij,bj->bi", rotations, vectors)

assert paired.tolist() == [[2, 1], [-3, 1]]

The identity keeps [2, 1] as it is. The second rotation maps [1, 3] to [-3, 1]. In NumPy's explicit syntax, the labels after the arrow determine which axes remain and the order in which they appear. An explicit batch label lets a reviewer check the data relationship before looking at the numbers.

Separate a pairwise batch from an all-pairs comparison

If the task is to test several candidate rotations against every vector, retain the two input batch dimensions separately. The following expression uses a for candidate rotations and b for vectors. The fixture returned shape (2, 2, 2), with the last dimension holding the two coordinates for each pair.

candidates = np.einsum("aij,bj->abi", rotations, vectors)

assert candidates.tolist() == [

[[2, 1], [1, 3]],

[[-1, 2], [-3, 1]],

]

This shape has a useful meaning in a calibration comparison. Its axes are candidate, observation, and coordinate. An inspection or replay stage that expects a single result per observation needs the pairwise array instead. Name the result axes in any export where the batch size and coordinate count might both happen to equal two.

Make vector rank explicit with matmul

The paired calculation can also use matmul. Add a trailing dimension to represent each input as a two-by-one column, perform the multiplication, and then remove the resulting singleton dimension.

columns = vectors[..., None]

paired_matmul = np.matmul(rotations, columns)[..., 0]

assert np.array_equal(paired_matmul, paired)

In this fixture, passing the two-by-two vectors table directly to matmul also succeeds. NumPy treats that table as one matrix and multiplies every rotation by it. The result has shape (2, 2, 2), where the final dimension contains columns of the table. It represents a different axis layout from the all-pairs einsum result above. A shape check can catch a mismatch when a caller expects (2, 2). A value and axis check explains what the result actually represents.

Verify the observation IDs before calculating

The expression pairs positions. It has no access to camera identifiers, timestamps, or frame metadata unless the application validates those first. Reversing the two input vectors while keeping the matrices fixed produces [[1, 3], [-1, 2]]. The output still has shape (2, 2), and both rotations remain mathematically valid. The example demonstrates how a misplaced observation can pass numeric checks on shape or matrix quality.

Before building the arrays, join matrices and vectors using the relevant observation key. Check that the key resolves to exactly one eligible transform under the data contract. Retain the frame names, transform direction, and calibration version. When a missing transform leaves an observation unresolved, preserve that outcome in the record before passing valid pairs to the array calculation.

A displacement vector is a difference between positions. Translating both endpoints by the same amount leaves the difference unchanged, so the rotation is sufficient for that calculation. A position in a new frame also needs the translation defined by the transform. Identify which quantity the array contains before reusing the expression. In a mixed telemetry export, storing a field such as displacement or position with the frame metadata gives the receiving job a way to distinguish those cases.

A compact regression test can assert both output vectors and both observation keys. Add a test with a permuted input table and verify that the join restores the correct pairing or reports a mismatch. Use a separate test for the intended all-pairs calculation. These checks cover the array algebra and the data association on which the resulting coordinates depend.

Sources checked November 28, 2026

NumPy, numpy.einsum

https://numpy.org/doc/stable/reference/generated/numpy.einsum.html

NumPy, numpy.matmul

https://numpy.org/doc/stable/reference/generated/numpy.matmul.html

Illustration. AI-generated concept showing two separate same-observation rotation and vector pairings from the synthetic fixture.