QuaTorch - Quaternions in Pytorch

lucasegidio1: <p>Hi all,</p>
<p>Please allow me to advertise this project I started: <strong><a href="https://github.com/egidioln/QuaTorch" rel="noopener nofollow ugc">QuaTorch</a></strong></p>
<p>Try it out with</p>
<pre><code class="lang-auto">pip install quatorch
</code></pre>
<p>The idea is to have lightweight package providing a <strong>subclass</strong> of <code>torch.Tensor</code> , namely, <code>quatorch.Quaternion</code> that follows the <a href="https://en.wikipedia.org/wiki/Quaternion#Multiplication_of_basis_elements" rel="noopener nofollow ugc"> multiplication rules of quaternions</a>. It also contain convenience functions such as, converting from/to <em>rotations matrices</em>, <em>axis-angle representation</em>, <em><a href="https://en.wikipedia.org/wiki/Slerp" rel="noopener nofollow ugc">SLERP</a></em>, and, of course, <em>rotating a set of 3d vectors</em>.</p>
<p>It supports default <strong>broadcasting</strong> 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:</p>
<pre><code class="lang-auto">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])
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/c/ec2c6478122784529dfcfcfaa95521df14a992c1.png" data-download-href="https://discuss.pytorch.org/uploads/default/ec2c6478122784529dfcfcfaa95521df14a992c1" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/e/c/ec2c6478122784529dfcfcfaa95521df14a992c1.png" alt="image" data-base62-sha1="xHhNbVOddmdN2xlHRCysXIqCHTj" width="592" height="432"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">592×432 4.66 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I notice some other implementations of quaternions out there in some packages (<a href="https://kornia.readthedocs.io/en/stable/geometry.quaternion.html#kornia.geometry.quaternion.Quaternion" rel="noopener nofollow ugc">kornia</a>, <a href="https://pytorch3d.readthedocs.io/en/latest/_modules/pytorch3d/transforms/rotation_conversions.html#" rel="noopener nofollow ugc">PyTorch3D</a>, …) but the goal here is to have a lightweight dependency to work with quaternions in a more integrated way (i.e., as a <code>torch.Tensor</code> subclass). Ideally, these libraries would use QuaTorch as a dependency allowing user to operate seamlessly with quaternions between them.</p>
<p>Take a look:</p>
<ul>
<li><strong><a href="https://github.com/egidioln/QuaTorch" rel="noopener nofollow ugc">Main repo</a></strong></li>
<li><strong><a href="https://egidioln.github.io/QuaTorch/examples.html" rel="noopener nofollow ugc">Examples</a></strong></li>
<li><strong><a href="https://egidioln.github.io/QuaTorch/api.html" rel="noopener nofollow ugc">Full API</a></strong></li>
</ul>

ptrblck: <p>Thanks for sharing your repository!</p>