squaternion


Namesquaternion JSON
Version 2023.9.2 PyPI version JSON
download
home_pagehttps://pypi.org/project/squaternion/
SummarySome simple functions for quaternion math
upload_time2023-09-02 17:15:26
maintainer
docs_urlNone
authorwalchko
requires_python>=3.8
licenseMIT
keywords rotations quaternion euler
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ![](https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/Inscription_on_Broom_Bridge_%28Dublin%29_regarding_the_discovery_of_Quaternions_multiplication_by_Sir_William_Rowan_Hamilton.jpg/800px-Inscription_on_Broom_Bridge_%28Dublin%29_regarding_the_discovery_of_Quaternions_multiplication_by_Sir_William_Rowan_Hamilton.jpg)

# Simple Quaternions (`squaternion`)

[![Actions Status](https://github.com/MomsFriendlyRobotCompany/squaternion/workflows/CheckPackage/badge.svg)](https://github.com/MomsFriendlyRobotCompany/squaternion/actions)
![GitHub](https://img.shields.io/github/license/MomsFriendlyRobotCompany/squaternion)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/squaternion)
![PyPI](https://img.shields.io/pypi/v/squaternion)
![PyPI - Downloads](https://img.shields.io/pypi/dm/squaternion?color=aqua)

Generally I don't need all of the capabilities (or complexity) of `quaternion`
math libraries. Basically I just need a way to convert between Euler and
Quaternion representations and have a nice way to print them out.

This has basically no imports outside of standard python 3.x libraries.
It should be easier to get on embedded python systems without having to build
`numpy`. Also, this tries to be *fast* by using a `frozen` `dataclass` available in python3.

## Install

```
pip install squaternion
```

## Usage

```python
from squaternion import Quaternion

# if you know the values you want Quaternion(w, x, y, z), note this is a
# attr frozen class so it is immutable once created
q = Quaternion(1,0,0,0)

# multiplication for scalar (int, double) and with another quaternion
q = Quaternion(1,2,3,4)
q*q => Quaternion(w=-28, x=4, y=6, z=8)
3*q => Quaternion(w=3, x=6, y=9, z=12)
q*3.0 => Quaternion(w=3.0, x=6.0, y=9.0, z=12.0)

# Addition and subtraction
q = Quaternion(1,2,3,4)
q+q => Quaternion(w=2, x=4, y=6, z=8)

# numpy can do some things, but it will change the tuple to an array, so you might
# need to transform it back to a quaternion
q = Quaternion(1,2,3,4)
np.dot(q,q) => 30
np.sqrt(np.dot(q,q)) => 5.477225575051661
q/np.sqrt(np.dot(q,q)) => array([0.18257419, 0.36514837, 0.54772256, 0.73029674])
Quaternion(*(q/np.sqrt(np.dot(q,q)))) => Quaternion(w=0.18257418583505536, x=0.3651483716701107, y=0.5477225575051661, z=0.7302967433402214)

# however you typically don't think in 4 dimensions, so create from
# euler angles from_eluer(roll, pitch, yaw), default is radians, but set
# degrees true if giving degrees
q = Quaternion.from_euler(0, -90, 100, degrees=True)

# can get the euler angles back out in degrees (set to True)
e = q.to_euler(degrees=True)
d = q.to_dict()
t = q.to_tuple()
r = q.to_rot() # returns a rotation matrix as tuple
r = np.array( q.to_rot() ) # rotation matrix as numpy array

# iterate through values
for i in q:
    print(f"{i}")

# indexing like a namedtuple
z = q[3]
z = q[-1]
v = q[-3:]
w = q[0]

# class properties
v = q.vector     # returns a tuple (x,y,z)
s = q.scalar     # returns a double (w)
n = q.normalize  # returns unit quaternion
m = q.magnitude  # returns the magnitude of the quaternion
a = q.angle      # returns angle of rotation in radians
a = q.axis       # returns axis of rotation

# useful attr functions
q == q    # compare will return True
q != q    # will return False

w = q.w
x = q.x
y = q.y
z = q.z

print(q)  # pretty print => Quaternion(w,x,y,z)
print(f"{q:.4f}") # print only 4 decimal places
```

## Alternatives

This is a basic library that converts between Euler angles and Quaternions.
There are other libraries that do so much more listed below ... but I don't
need all of that.

- [scipy.spatial.transform.Rotation](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.transform.Rotation.html#scipy.spatial.transform.Rotation): has everything you could want, with lots of imports
- [tinyquaternion](https://github.com/rezaahmadzadeh/tinyquaternion): appears to be more functional but needs `numpy`
- [quaternions](https://github.com/mjsobrep/quaternions): another good lightweight quaternion package
- [pyrr](https://github.com/adamlwgriffiths/Pyrr): seems good, integrated with `numpy`

## References

- [Wikipedia Convert Between Quaternions and Euler Angles](https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles)
- [Wikipedia Euler Angle Definitions](https://en.wikipedia.org/wiki/Euler_angles#Conventions_2)
- [Wikipedia Gimbal Lock](https://en.wikipedia.org/wiki/Gimbal_lock)

# MIT License

Copyright (c) 2014 Kevin Walchko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

            

Raw data

            {
    "_id": null,
    "home_page": "https://pypi.org/project/squaternion/",
    "name": "squaternion",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "rotations,quaternion,euler",
    "author": "walchko",
    "author_email": "walchko@users.noreply.github.com",
    "download_url": "https://files.pythonhosted.org/packages/8c/20/ca1fc28983e3311a8df3b45847eabb3c5e656d48450c9aaf91e7ece7d8da/squaternion-2023.9.2.tar.gz",
    "platform": null,
    "description": "![](https://upload.wikimedia.org/wikipedia/commons/thumb/d/d5/Inscription_on_Broom_Bridge_%28Dublin%29_regarding_the_discovery_of_Quaternions_multiplication_by_Sir_William_Rowan_Hamilton.jpg/800px-Inscription_on_Broom_Bridge_%28Dublin%29_regarding_the_discovery_of_Quaternions_multiplication_by_Sir_William_Rowan_Hamilton.jpg)\n\n# Simple Quaternions (`squaternion`)\n\n[![Actions Status](https://github.com/MomsFriendlyRobotCompany/squaternion/workflows/CheckPackage/badge.svg)](https://github.com/MomsFriendlyRobotCompany/squaternion/actions)\n![GitHub](https://img.shields.io/github/license/MomsFriendlyRobotCompany/squaternion)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/squaternion)\n![PyPI](https://img.shields.io/pypi/v/squaternion)\n![PyPI - Downloads](https://img.shields.io/pypi/dm/squaternion?color=aqua)\n\nGenerally I don't need all of the capabilities (or complexity) of `quaternion`\nmath libraries. Basically I just need a way to convert between Euler and\nQuaternion representations and have a nice way to print them out.\n\nThis has basically no imports outside of standard python 3.x libraries.\nIt should be easier to get on embedded python systems without having to build\n`numpy`. Also, this tries to be *fast* by using a `frozen` `dataclass` available in python3.\n\n## Install\n\n```\npip install squaternion\n```\n\n## Usage\n\n```python\nfrom squaternion import Quaternion\n\n# if you know the values you want Quaternion(w, x, y, z), note this is a\n# attr frozen class so it is immutable once created\nq = Quaternion(1,0,0,0)\n\n# multiplication for scalar (int, double) and with another quaternion\nq = Quaternion(1,2,3,4)\nq*q => Quaternion(w=-28, x=4, y=6, z=8)\n3*q => Quaternion(w=3, x=6, y=9, z=12)\nq*3.0 => Quaternion(w=3.0, x=6.0, y=9.0, z=12.0)\n\n# Addition and subtraction\nq = Quaternion(1,2,3,4)\nq+q => Quaternion(w=2, x=4, y=6, z=8)\n\n# numpy can do some things, but it will change the tuple to an array, so you might\n# need to transform it back to a quaternion\nq = Quaternion(1,2,3,4)\nnp.dot(q,q) => 30\nnp.sqrt(np.dot(q,q)) => 5.477225575051661\nq/np.sqrt(np.dot(q,q)) => array([0.18257419, 0.36514837, 0.54772256, 0.73029674])\nQuaternion(*(q/np.sqrt(np.dot(q,q)))) => Quaternion(w=0.18257418583505536, x=0.3651483716701107, y=0.5477225575051661, z=0.7302967433402214)\n\n# however you typically don't think in 4 dimensions, so create from\n# euler angles from_eluer(roll, pitch, yaw), default is radians, but set\n# degrees true if giving degrees\nq = Quaternion.from_euler(0, -90, 100, degrees=True)\n\n# can get the euler angles back out in degrees (set to True)\ne = q.to_euler(degrees=True)\nd = q.to_dict()\nt = q.to_tuple()\nr = q.to_rot() # returns a rotation matrix as tuple\nr = np.array( q.to_rot() ) # rotation matrix as numpy array\n\n# iterate through values\nfor i in q:\n    print(f\"{i}\")\n\n# indexing like a namedtuple\nz = q[3]\nz = q[-1]\nv = q[-3:]\nw = q[0]\n\n# class properties\nv = q.vector     # returns a tuple (x,y,z)\ns = q.scalar     # returns a double (w)\nn = q.normalize  # returns unit quaternion\nm = q.magnitude  # returns the magnitude of the quaternion\na = q.angle      # returns angle of rotation in radians\na = q.axis       # returns axis of rotation\n\n# useful attr functions\nq == q    # compare will return True\nq != q    # will return False\n\nw = q.w\nx = q.x\ny = q.y\nz = q.z\n\nprint(q)  # pretty print => Quaternion(w,x,y,z)\nprint(f\"{q:.4f}\") # print only 4 decimal places\n```\n\n## Alternatives\n\nThis is a basic library that converts between Euler angles and Quaternions.\nThere are other libraries that do so much more listed below ... but I don't\nneed all of that.\n\n- [scipy.spatial.transform.Rotation](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.transform.Rotation.html#scipy.spatial.transform.Rotation): has everything you could want, with lots of imports\n- [tinyquaternion](https://github.com/rezaahmadzadeh/tinyquaternion): appears to be more functional but needs `numpy`\n- [quaternions](https://github.com/mjsobrep/quaternions): another good lightweight quaternion package\n- [pyrr](https://github.com/adamlwgriffiths/Pyrr): seems good, integrated with `numpy`\n\n## References\n\n- [Wikipedia Convert Between Quaternions and Euler Angles](https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles)\n- [Wikipedia Euler Angle Definitions](https://en.wikipedia.org/wiki/Euler_angles#Conventions_2)\n- [Wikipedia Gimbal Lock](https://en.wikipedia.org/wiki/Gimbal_lock)\n\n# MIT License\n\nCopyright (c) 2014 Kevin Walchko\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Some simple functions for quaternion math",
    "version": "2023.9.2",
    "project_urls": {
        "Homepage": "https://pypi.org/project/squaternion/",
        "Repository": "https://github.com/the-guild-of-calamitous-intent/squaternion"
    },
    "split_keywords": [
        "rotations",
        "quaternion",
        "euler"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7644a8ae092bf357a0bde3356d4d6d98194429283c0a9273cb0883691f6bfa6b",
                "md5": "c19d2c8ff6572dd027b344cb2c4544a2",
                "sha256": "dfdf5020b7aba30bf1834e90ad4c201968200a4c039167da069fbfe7040793fc"
            },
            "downloads": -1,
            "filename": "squaternion-2023.9.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c19d2c8ff6572dd027b344cb2c4544a2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6794,
            "upload_time": "2023-09-02T17:15:24",
            "upload_time_iso_8601": "2023-09-02T17:15:24.567514Z",
            "url": "https://files.pythonhosted.org/packages/76/44/a8ae092bf357a0bde3356d4d6d98194429283c0a9273cb0883691f6bfa6b/squaternion-2023.9.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8c20ca1fc28983e3311a8df3b45847eabb3c5e656d48450c9aaf91e7ece7d8da",
                "md5": "f201c0ce34ed87a379b37d0488333f13",
                "sha256": "b3579fae68e8b48621fdf922e2f9fee166a1d868efd4091d29a9878e6cfe12ee"
            },
            "downloads": -1,
            "filename": "squaternion-2023.9.2.tar.gz",
            "has_sig": false,
            "md5_digest": "f201c0ce34ed87a379b37d0488333f13",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 6156,
            "upload_time": "2023-09-02T17:15:26",
            "upload_time_iso_8601": "2023-09-02T17:15:26.099483Z",
            "url": "https://files.pythonhosted.org/packages/8c/20/ca1fc28983e3311a8df3b45847eabb3c5e656d48450c9aaf91e7ece7d8da/squaternion-2023.9.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-02 17:15:26",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "the-guild-of-calamitous-intent",
    "github_project": "squaternion",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "squaternion"
}
        
Elapsed time: 0.12051s