QuaTorch - Quaternions in Pytorch

Hi all,

Please allow me to advertise this project I started: QuaTorch

Try it out with

pip install quatorch

The idea is to have lightweight package providing a subclass of torch.Tensor , namely, quatorch.Quaternion that follows the multiplication rules of quaternions. It also contain convenience functions such as, converting from/to rotations matrices, axis-angle representation, SLERP, and, of course, rotating a set of 3d vectors.

It supports default broadcasting in all operations as a quaternion is a tensor of shape (…, 4), i.e., with any number of leading dimensions. This let you do things like:

t = torch.linspace(0, 1, steps=10)[..., None]

r = q0.slerp(q_final, t).rotate_vector(torch.tensor([0.0, 1.0, -1.0]))
plt.scatter(r[:, 1], r[:, 2])

I notice some other implementations of quaternions out there in some packages (kornia, PyTorch3D, …) but the goal here is to have a lightweight dependency to work with quaternions in a more integrated way (i.e., as a torch.Tensor subclass). Ideally, these libraries would use QuaTorch as a dependency allowing user to operate seamlessly with quaternions between them.

Take a look:

5 Likes

Thanks for sharing your repository!

1 Like

Nice work.

Just one question. Your example seemed to produce different results on the 4th operation. Was that intended?

Thanks for you comment @J_Johnson

The small difference is because I defined the 45deg rotation as a quaternion with only 4 decimal digits. Therefore there is a tiny numerical different between turning 45 degrees 4 times clockwise and counterclockwise (notice that the difference is of order of $10^{-5}$).

Maybe I should define it through trigonometric functions to avoid this confusion, thanks for pointing it out :slight_smile:

Ah, I gotcha. Didn’t notice the size of the value until you now pointed it out.