Using Quaternion with Python ~ numpy-quaternion ~

Introduction

Quaternion and numpy-quaternion seem to be the top two libraries in the world that handle quaternions in Python, but probably because the person who made the reference page for pyquaternion was the first in Japan, the quaternion calculation in Python is overflowing in the streets. It's just pyquaternion (is there only?).

However, numpy-quaternion seems to have a lower calculation cost. Reference: https://www.theoj.org/joss-papers/joss.00787/10.21105.joss.00787.pdf

Also, comparing the number of stars on github, there are more numpy-quaternions (as of December 13, 2019, numpy-quaternion is 276 and pyquaternion is 138).

I feel that the affinity with numpy is also better than pyquaternion because of the ease of handling batch conversion. Would you like to use it?

github repository: https://github.com/moble/quaternion Documentation: https://quaternion.readthedocs.io/en/latest/ PyPI: https://pypi.org/project/numpy-quaternion/

Installation

pip install numpy-quaternion

You can easily enter with.

Also,

import quaternion

You can import the library with.

Quaternion notation

quat = (w, x, y, z) = \left(\underbrace{\cos\frac{\theta}{2}}_{Real part},\ \underbrace{\lambda_x\sin\frac{\theta}{2},\ \lambda_y\sin\frac{\theta}{2},\ \lambda_z\sin\frac{\theta}{2}}_{Imaginary number(vector)Department}\right)

How to use

Less than,

import numpy as np
import quaternion

will do.

Make a quaternion

quat = np.quaternion(w, x, y, z)

Quaternion is added to the attribute of numpy. For example

print(np.quaternion(1,0,0,0))
# -> quaternion(1, 0, 0, 0)

type(np.quaternion(1,0,0,0))
# -> quaternion.quaternion

It becomes.

Quaternion synthesis

q1 = np.quaternion(1, 2, 3, 4)
q2 = np.quaternion(4, 5, 6, 7)
print(q1*q2)
# -> quaternion(-52, 10, 24, 20)

Multiplication is defined. For the time being, other four arithmetic operations are also defined, but do you sometimes use sums ... It is not defined in Eigen of C ++.

methods of np.quaternion

Only a part will be taken up. It does not cover things related to spherical functions or linear interpolation. Reference: https://quaternion.readthedocs.io/en/latest/_autosummary/quaternion.html

Member variables

Member variables function
w Elements of the real part
x The first element of the imaginary part
y The second element of the imaginary part
z The third element of the imaginary part
components (w,x,y,z)Is numpy.Returned as an array
imag Imaginary part(x,y,z)Is numpy.Returned as an array
vec Imaginary part(x,y,z)Is numpy.Returned as an array
real Real part(w)Is returned

Member function

Member function function
abs() Absolute value of quaternion (Euclidean distance)
absolute() Absolute value of quaternion
angle() rotation angle
conj() Returns the complex conjugate of the quaternion
conjugate() Returns the complex conjugate of the quaternion
equal(quat) Is it equal to the quaternion in the argument content?
exp Returns exponential (e^q
inverse() Returns a reverse quaternion
isfinite() Are all elements finite
ininf() Is there even one inf element?
innan() Is there even one nan element?
log() Returns quaternion logs
nonzero() Are all elements 0
norm() Quaternion Cayley norm (absolute squared route)
normalized() Returns a normalized quaternion
notequal(quat) Is it not equal to the quaternion in the argument content?
sqrt() Quaternion square-root(quat=q*qMeetq)return it
square() Quaternion squared(quat*quat)return it

quaternion method

The point is that the methods in this area are not one-dimensional, but can also be used for multidimensional arrays. If the size of the last dimension requires a quaternion, it should be 4, if it is 3D, it should be 3D, and if it is 3x3, the last 2D should be 3x3.

Member function function
quaternion.as_quat_array(a) numpy.Convert array to quaternion. The size of the last dimension of a must be 4
quaternion.as_float_array(a) numpy.quaternion numpy.Convert to array. The dimension of the output is one larger than the input.
quaternion.from_float_array(a) as_quat_Same as array
quaternion.as_rotation_matrix(q) numpy.Convert quaternion to 3x3 rotation matrix.
quaternion.from_rotation_matrix(rot, nonorthogonal=True) Numpy a 3x3 rotation matrix.Convert to quaternion
quaternion.as_rotation_vector(q) Find the axis of rotation from the quaternion. The magnitude of the last dimension of the output is 3.
quaternion.from_rotation_vector(rot) Convert from a size 3 rotation axis to a quaternion.
quaternion.as_euler_angles(q) Convert from quaternion to Euler angles. Refer to the document for the conversion order of Euler angles.
quaternion.from_euler_angles(alpha_beta_gamma, beta=None, gamma=None) Convert Euler angles to quaternions. Refer to the document for the conversion order of Euler angles.
quaternion.rotate_vectors(R, v, axis=-1) R is a quaternion and v is a vector. Rotate the vector according to the quaternion.
quaternion.allclose(a, b, rtol=8.881784197001252e-16, atol=0.0, equal_nan=False, verbose=False) Compare two quaternions
quaternion.integrate_angular_velocity(Omega, t0, t1, R0=None, tolerance=1e-12) Rotate according to the angular velocity

Official documentation

https://quaternion.readthedocs.io/en/latest/index.html

By the way, the author of this library seems to have a big Euler angle. Stop if you see someone using it! Say that and walk away from the spot and mess with your mom! Is written

Finally

Where is the demand for quaternions in Python? Unity is C #, and I'm talking about using Unity's functions in the first place, and Python is too slow for robots, which is out of the question ...

Recommended Posts

Using Quaternion with Python ~ numpy-quaternion ~
[S3] CRUD with S3 using Python [Python]
[Python] Using OpenCV with Python (Basic)
Using OpenCV with Python @Mac
Send using Python with Gmail
Complement python with emacs using company-jedi
[Python] Using OpenCV with Python (Image Filtering)
Using Rstan from Python with PypeR
[Python] Using OpenCV with Python (Image transformation)
[Python] Using OpenCV with Python (Edge Detection)
Notes on using rstrip with python.
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
[AWS] Using ini files with Lambda [Python]
FizzBuzz with Python3
Scraping with Python
Statistics with python
Try mathematical formulas using Σ with python
Scraping with Python
Python with Go
Using Python and MeCab with Azure Databricks
Socket communication using socketserver with python now
Twilio with Python
Integrate with Python
Start using Python
Try using Python with Google Cloud Functions
Play with 2016-Python
AES256 with python
Check stock prices with slackbot using python
Working with OpenStack using the Python SDK
Tested with Python
python starts with ()
Tips for using python + caffe with TSUBAME
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
I'm using tox and Python 3.3 with Travis-CI
Scraping using Python
Excel with Python
Microcomputer with Python
Cast with python
[Personal memo] julia --Using Python library with julia using PyCall
Debug with VS Code using boost python numpy
I tried using mecab with python2.7, ruby2.3, php7
Recent ranking creation using Qiita API with Python
What are you using when testing with Python?
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Learning Python with ChemTHEATER 03
Sequential search with Python
Operate Redmine using Python Redmine
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python