Skip to main content

Pythonic Geometric Algebra Package

Project description

Kingdon

https://img.shields.io/pypi/v/kingdon.svg https://readthedocs.org/projects/kingdon/badge/?version=latest https://coveralls.io/repos/github/tBuLi/kingdon/badge.svg?branch=master

Pythonic Geometric Algebra Package

โœจ Try kingdon in your browser โœจ

Features

Kingdon is a Geometric Algebra (GA) library which combines a Pythonic API with symbolic simplification and just-in-time compilation to achieve high-performance in a single package. It support both symbolic and numerical GA computations. Moreover, kingdon uses ganja.js for visualization in notebooks, making it an extremely well rounded GA package.

In bullet points:

  • Symbolically optimized.

  • Leverage sparseness of input.

  • ganja.js enabled graphics in jupyter notebooks.

  • Agnostic to the input types: work with GAโ€™s over numpy arrays, PyTorch tensors, sympy expressions, etc. Any object that overloads addition, subtraction and multiplication makes for valid multivector coefficients in kingdon.

  • Automatic broadcasting, such that transformations can be applied to e.g. point-clouds.

  • Compatible with numba and other JIT compilers to speed-up numerical computations.

Code Example

In order to demonstrate the power of Kingdon, let us first consider the common use-case of the commutator product between a bivector and vector.

In order to create an algebra, use Algebra. When calling Algebra we must provide the signature of the algebra, in this case we shall go for 3DPGA, which is the algebra \(\mathbb{R}_{3,0,1}\). There are a number of ways to make elements of the algebra. It can be convenient to work with the basis blades directly. We can add them to the local namespace by calling locals().update(alg.blades):

>>> from kingdon import Algebra
>>> alg = Algebra(3, 0, 1)
>>> locals().update(alg.blades)
>>> b = 2 * e12
>>> v = 3 * e1
>>> b * v
-6 ๐žโ‚‚

This example shows that only the e2 coefficient is calculated, despite the fact that there are 6 bivector and 4 vector coefficients in 3DPGA. But by exploiting the sparseness of the input and by performing symbolic optimization, kingdon knows that in this case only e2 can be non-zero.

Symbolic usage

If only a name is provided for a multivector, kingdon will automatically populate all relevant fields with symbols. This allows us to easily perform symbolic computations.

>>> from kingdon import Algebra
>>> alg = Algebra(3, 0, 1)
>>> b = alg.bivector(name='b')
>>> b
b01 ๐žโ‚€โ‚ + b02 ๐žโ‚€โ‚‚ + b03 ๐žโ‚€โ‚ƒ + b12 ๐žโ‚โ‚‚ + b13 ๐žโ‚โ‚ƒ + b23 ๐žโ‚‚โ‚ƒ
>>> v = alg.vector(name='v')
>>> v
v0 ๐žโ‚€ + v1 ๐žโ‚ + v2 ๐žโ‚‚ + v3 ๐žโ‚ƒ
>>> b.cp(v)
(b01*v1 + b02*v2 + b03*v3) ๐žโ‚€ + (b12*v2 + b13*v3) ๐žโ‚ + (-b12*v1 + b23*v3) ๐žโ‚‚ + (-b13*v1 - b23*v2) ๐žโ‚ƒ

It is also possible to define some coefficients to be symbolic by inputting a string, while others can be numeric:

>>> from kingdon import Algebra, symbols
>>> alg = Algebra(3, 0, 1)
>>> b = alg.bivector(e12='b12', e03=3)
>>> b
3 ๐žโ‚€โ‚ƒ + b12 ๐žโ‚โ‚‚
>>> v = alg.vector(e1=1, e3=1)
>>> v
1 ๐žโ‚ + 1 ๐žโ‚ƒ
>>> w = b.cp(v)
>>> w
3 ๐žโ‚€ + (-b12) ๐žโ‚‚

A kingdon MultiVector with symbols is callable. So in order to evaluate w from the previous example, for a specific value of b12, simply call w:

>>> w(b12=10)
3 ๐žโ‚€ + -10 ๐žโ‚‚

Overview of Operators

Operators

Operation

Expression

Infix

Inline

Geometric product

$ab$

a*b

a.gp(b)

Inner

$a \cdot b$

a|b

a.ip(b)

Scalar product

$\langle a \cdot b \rangle_0$

a.sp(b)

Left-contraction

$a \rfloor b$

a.lc(b)

Right-contraction

$a \lfloor b$

a.rc(b)

Outer (Exterior)

$a \wedge b$

a ^ b

a.op(b)

Regressive

$a \vee b$

a & b

a.rp(b)

Conjugate b by a

$a b \widetilde{a}$

a >> b

a.sw(b)

Project a onto b

$(a \cdot b) \widetilde{b}$

a @ b

a.proj(b)

Commutator of a and b

$a \times b = \tfrac{1}{2} [a, b]$

a.cp(b)

Anti-commutator of a and b

$\tfrac{1}{2} \{a, b\}$

a.acp(b)

Sum of a and b

$a + b$

a + b

a.add(b)

Difference of a and b

$a - b$

a - b

a.sub(b)

Reverse of a

$\widetilde{a}$

~a

a.reverse()

Squared norm of a

$a \widetilde{a}$

a.normsq()

Norm of a

$\sqrt{a \widetilde{a}}$

a.norm()

Normalize a

$a / \sqrt{a \widetilde{a}}$

a.normalized()

Square root of a

$\sqrt{a}$

a.sqrt()

Dual of a

$a*$

a.dual()

Undual of a

a.undual()

Grade k part of a

$\langle a \rangle_k$

a.grade(k)

Credits

This package was inspired by GAmphetamine.js.

History

0.1.0 (2023-08-12)

  • First release on PyPI.

0.2.0 (2024-01-09)

  • Multivectors now have map and filter methods to apply element-wise operations to the coefficients.

  • Make matrix representations of expressions using expr_as_matrix.

  • Bugfixes.

0.3.0 (2024-03-11)

  • Much faster codegen by the introduction of a GAmphetamine.js inspired RationalPolynomial class, which now replaces SymPy for codegen. Particularly for inverses this is orders of magnitude faster.

  • Performed a numbotomy: numba is no longer a dependency since it actually didnโ€™t add much in most cases. Instead the user can now provide the Algebra with any wrapper function, which is applied to the generated functions. This can be numba.njit, but also any other decorator.

0.3.2 (2024-03-18)

  • Fixed a high priority bug in the graph function.

  • Fixed a bug that stopped multivectors from being callable.

1.0.0 (2024-04-17)

  • Kingdon now has proper support for ganja.js animations and the graphs are interactive!

  • Indexing a multivector will no longer access coefficients. The whole promise of GA is coordinate independence, so why would you need to access coefficients? Instead, slicing a multivector will pass on that information to the underlying datastructures (e.g. numpy array or pytorch tensor), and will return a new multivector. Moreover, you can use the new slicing syntax to set values as well. If you really still need access to the coefficients, there is always the getattr syntax or the .values() method.

1.0.5 (2024-06-26)

  • Blades by grade syntax: alg.blades.grade(2).

  • Fixed โ€œdefineโ€ error in ganja.js integration, kingdon now works with reveal.js voila template.

1.0.6 (2024-07-10)

Bugfixes to ganja.js integration: * Make sure camera is an object before checking for โ€˜mvโ€™ key. * Improved draggable points for PGA.

1.1.0 (2024-08-10)

  • Map and filter now support two argument functions. If such a funtion is provided, map/filter is applied on key, value pairs.

  • Added exponential function for simple objects.

  • Raising a mv to 0.5 is now correctly interpreted as a square root. This enables e.g. automatic differentiation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kingdon-1.1.1.tar.gz (131.9 kB view details)

Uploaded Source

Built Distribution

kingdon-1.1.1-py2.py3-none-any.whl (39.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file kingdon-1.1.1.tar.gz.

File metadata

  • Download URL: kingdon-1.1.1.tar.gz
  • Upload date:
  • Size: 131.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for kingdon-1.1.1.tar.gz
Algorithm Hash digest
SHA256 3feba3c85a99e3da89e0babdff41e2ec5ad54860a8349fc9bb202a09fb2aa7a8
MD5 9f9083b821dddd215d7ab06ecfce2582
BLAKE2b-256 538dd2abc147fbf5b23ad7cdf104a194914aa4fe49b72065c12be390c3d240d8

See more details on using hashes here.

File details

Details for the file kingdon-1.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: kingdon-1.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 39.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for kingdon-1.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a3af7d0c8e1fa8d390bfeb07898709138e6d0112dfafc228162abd4e0701c006
MD5 68eccd8a0c45c38ee1f916744ff838f6
BLAKE2b-256 f2d2df0c849f9bb6660be515705660f13bf9730c2bd3f3be791fe486d26e883d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page