Skip to content
/ kingdon Public

A symbolically optimized Geometric Algebra library with PyTorch/NumPy/SymPy/etc. compatibility and ganja.js visualization.

License

Notifications You must be signed in to change notification settings

tBuLi/kingdon

Repository files navigation

Kingdon

Documentation Status 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.

Teahouse Menu

If you are thirsty for some examples, please visit the teahouse. A small selection of our items:

docs/_static/pga2d_distances_and_angles.png

Land measurement 101

docs/_static/pga2d_inverse_kinematics.png

Dimension agnostic IK

docs/_static/pga2d_project_and_reject.png

2D projection and intersection

docs/_static/pga3d_distances_and_angles.png

Land measurement 420

docs/_static/pga2d_hypercube_on_string.png

Best-seller: Tesseract on a string!

docs/_static/pga3d_points_and_lines.png

3D projection and intersection

docs/_static/exercise_spider6.png

Build-A-Spider Workshop!

docs/_static/cga2d_points_and_circles.png

Project and intersect, but round

docs/_static/pga2d_fivebar.png

Fivebar mechanism

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)
"Divide" a by b $a b^{-1}$ a / b a.div(b)
Inverse of a $a^{-1}$   a.inv()
Reverse of a $\widetilde{a}$ ~a a.reverse()
Grade Involution of a $\hat{a}$   a.involute()
Clifford Conjugate of a $\bar{a} = \hat{\widetilde{a}}$   a.conjugate()
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.

About

A symbolically optimized Geometric Algebra library with PyTorch/NumPy/SymPy/etc. compatibility and ganja.js visualization.

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •