Skip to content

Commit

Permalink
Generate random rotation matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderFabisch committed Feb 19, 2024
1 parent dbdbe5b commit 26dfc2c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
38 changes: 37 additions & 1 deletion pytransform3d/rotations/_random.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np

from ._utils import norm_vector
from ._utils import norm_vector, check_matrix
from ._conversions import matrix_from_compact_axis_angle


def random_vector(rng=np.random.default_rng(0), n=3):
Expand Down Expand Up @@ -84,3 +85,38 @@ def random_quaternion(rng=np.random.default_rng(0)):
Unit quaternion to represent rotation: (w, x, y, z)
"""
return norm_vector(rng.standard_normal(size=4))


def random_matrix(rng=np.random.default_rng(0), mean=np.eye(3), cov=np.eye(3)):
"""Generate random rotation matrix.
Generate :math:`\Delta \boldsymbol{R}_{B_{i+1}{B_i}}
\boldsymbol{R}_{{B_i}A}`, with :math:`\Delta \boldsymbol{R}_{B_{i+1}{B_i}}
= Exp(\hat{\omega} \theta)` and :math:`\hat{\omega}\theta \sim
\mathcal{N}(\boldsymbol{0}_3, \boldsymbol{\Sigma}_{3 \times 3})`.
The mean :math:`\boldsymbol{R}_{{B_i}A}` and the covariance
:math:`\boldsymbol{\Sigma}_{3 \times 3}` are parameters of the function.
Note that uncertainty is defined in the global frame B, not in the
body frame A.
Parameters
----------
rng : np.random.Generator, optional (default: random seed 0)
Random number generator.
mean : array-like, shape (3, 3), optional (default: I)
Mean rotation matrix.
cov : array-like, shape (3, 3), optional (default: I)
Covariance of noise in exponential coordinate space.
Returns
-------
R : array, shape (3, 3)
Rotation matrix
"""
mean = check_matrix(mean)
a = rng.multivariate_normal(mean=np.zeros(3), cov=cov)
delta = matrix_from_compact_axis_angle(a)
return np.dot(delta, mean)
4 changes: 4 additions & 0 deletions pytransform3d/rotations/_random.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ def random_compact_axis_angle(rng: np.random.Generator = ...) -> np.ndarray: ...


def random_quaternion(rng: np.random.Generator = ...) -> np.ndarray: ...


def random_matrix(rng: np.random.Generator = ..., mean: npt.ArrayLike = ...,
cov: npt.ArrayLike = ...) -> np.ndarray: ...
2 changes: 1 addition & 1 deletion pytransform3d/transformations/_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def random_transform(
Returns
-------
A2B : array-like, shape (4, 4)
A2B : array, shape (4, 4)
Random transform from frame A to frame B
"""
mean = check_transform(mean)
Expand Down

0 comments on commit 26dfc2c

Please sign in to comment.