psychopy.tools.mathtools.applyMatrix

psychopy.tools.mathtools.applyMatrix(m, points, out=None, dtype=None)[source]

Apply a matrix over a 2D array of points.

This function behaves similarly to the following Numpy statement:

points[:, :] = points.dot(m.T)

Transformation matrices specified to m must have dimensions 4x4, 3x4, 3x3 or 2x2. With the exception of 4x4 matrices, input points must have the same number of columns as the matrix has rows. 4x4 matrices can be used to transform both Nx4 and Nx3 arrays.

Parameters:
  • m (array_like) – Matrix with dimensions 2x2, 3x3, 3x4 or 4x4.

  • points (array_like) – 2D array of points/coordinates to transform. Each row should have length appropriate for the matrix being used.

  • 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:

Transformed coordinates.

Return type:

ndarray

Notes

  • Input (points) and output (out) arrays cannot be the same instance for this function.

  • In the case of 4x4 input matrices, this function performs optimizations based on whether the input matrix is affine, greatly improving performance when working with Nx3 arrays.

Examples

Construct a matrix and transform a point:

# identity 3x3 matrix for this example
M = [[1.0, 0.0, 0.0],
     [0.0, 1.0, 0.0],
     [0.0, 0.0, 1.0]]

pnt = [1.0, 0.0, 0.0]

pntNew = applyMatrix(M, pnt)

Construct an SRT matrix (scale, rotate, transform) and transform an array of points:

S = scaleMatrix([5.0, 5.0, 5.0])  # scale 5x
R = rotationMatrix(180., [0., 0., -1])  # rotate 180 degrees
T = translationMatrix([0., 1.5, -3.])  # translate point up and away
M = concatenate([S, R, T])  # create transform matrix

# points to transform
points = np.array([[0., 1., 0., 1.], [-1., 0., 0., 1.]]) # [x, y, z, w]
newPoints = applyMatrix(M, points)  # apply the transformation

Convert CIE-XYZ colors to sRGB:

sRGBMatrix = [[3.2404542, -1.5371385, -0.4985314],
              [-0.969266,  1.8760108,  0.041556 ],
              [0.0556434, -0.2040259,  1.0572252]]

colorsRGB = applyMatrix(sRGBMatrix, colorsXYZ)

Back to top