A symbolically optimized and pythonic Clifford (geometric) algebra library named after none other than William Kingdon Clifford.
MIT License
.. image:: https://img.shields.io/pypi/v/kingdon.svg :target: https://pypi.python.org/pypi/kingdon
.. image:: https://readthedocs.org/projects/kingdon/badge/?version=latest :target: https://kingdon.readthedocs.io/en/latest/?version=latest
.. image:: https://coveralls.io/repos/github/tBuLi/kingdon/badge.svg?branch=master :target: https://coveralls.io/github/tBuLi/kingdon?branch=master
Pythonic Geometric Algebra Package
✨ Try kingdon in your browser ✨ <https://tbuli.github.io/teahouse/>
_
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, :code:kingdon
uses :code:ganja.js
for visualization in notebooks,
making it an extremely well rounded GA package.
In bullet points:
ganja.js
enabled graphics in jupyter notebooks.numpy
arrays, :code:PyTorch
tensors, :code:sympy
expressions, etc. Any object that overloads addition, subtraction and multiplication makes for valid multivector coefficients in :code:kingdon
.numba
and other JIT compilers to speed-up numerical computations.In order to demonstrate the power of :code: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 :code:Algebra
. When calling :code:Algebra
we must provide the signature of the
algebra, in this case we shall go for 3DPGA, which is the algebra :math:\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 :code:locals().update(alg.blades)
:
.. code-block:: python
>>> 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 :code: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, :code:kingdon
knows that in this case only :code:e2
can be non-zero.
If only a name is provided for a multivector, :code:kingdon
will automatically populate all
relevant fields with symbols. This allows us to easily perform symbolic computations.
.. code-block:: python
>>> 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:
.. code-block:: python
>>> 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 :code:kingdon
MultiVector with symbols is callable. So in order to evaluate :code:w
from the previous example,
for a specific value of :code:b12
, simply call :code:w
:
.. code-block:: python
>>> w(b12=10)
3 𝐞₀ + -10 𝐞₂
.. list-table:: Operators :widths: 50 25 25 25 :header-rows: 1
a*b
a.gp(b)
a|b
a.ip(b)
a.sp(b)
a.lc(b)
a.rc(b)
a ^ b
a.op(b)
a & b
a.rp(b)
b
by :code:a
a >> b
a.sw(b)
a
onto :code:b
a @ b
a.proj(b)
a
and :code:b
a.cp(b)
a
and :code:b
a.acp(b)
a
and :code:b
a + b
a.add(b)
a
and :code:b
a - b
a.sub(b)
a
~a
a.reverse()
a
a.normsq()
a
a.norm()
a
a.normalized()
a
a.sqrt()
a
a.dual()
a
a.undual()
k
part of :code:a
a.grade(k)
This package was inspired by GAmphetamine.js.