Roll, Pitch, Yaw to a Rotation Matrix

~12 mincode completion

Implement rpy_to_matrix(roll, pitch, yaw) returning the 3x3 matrix . Angles are in radians.

Examples

All three angles zero: the identity

Input
rpy_to_matrix(0, 0, 0)
Output
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]

Yaw alone, 90 degrees: x turns onto y

Input
rpy_to_matrix(0, 0, 1.5707963267948966)
Output
[[0, -1, 0], [1, 0, 0], [0, 0, 1]]

Roll alone, 90 degrees: y tips onto z

Input
rpy_to_matrix(1.5707963267948966, 0, 0)
Output
[[1, 0, 0], [0, 0, -1], [0, 1, 0]]

Hints

Hint 1

Use a matrix product rather than nested loops, and check which operand transposes.

Hint 2

A common slip here: multiplied the three rotations in xyz order instead of zyx.

Requirements

  • roll: rotation about x, radians

  • pitch: rotation about y, radians

  • yaw: rotation about z, radians

  • Return (3, 3) rotation matrix R_z(yaw) @ R_y(pitch) @ R_x(roll)

Constraints

  • Allowed library: NumPy only

  • Time limit: 200 ms, Memory: 64 MB

Where this shows up

~12 min

6 employers weight this skill

4 autonomy companies, 2 defense companies. Top match scores 93.

Python
import numpy as np


def rpy_to_matrix(roll, pitch, yaw):
    """
    ZYX roll-pitch-yaw angles to a rotation matrix.

    Args:
        roll:  rotation about x, radians
        pitch: rotation about y, radians
        yaw:   rotation about z, radians

    Returns:
        (3, 3) rotation matrix R_z(yaw) @ R_y(pitch) @ R_x(roll)
    """
    # YOUR CODE HERE
    pass
Loading docs…

The AI Mentor needs an account

It reads your code and the failing tests and nudges you toward the fix without handing you the answer. Free accounts get it on every problem you're working on today.