Skip to main content

Clifford and Geometric Algebra with PyTorch

Project description

torch_ga - PyTorch Geometric Algebra

Build status

GitHub | Docs | Benchmarks

Python package for Geometric / Clifford Algebra with Pytorch.

This project is a work in progress. Its API may change and the examples aren't polished yet.

This project is based on the TF-GA library TGA

Pull requests and suggestions either by opening an issue or by sending me an email are welcome.

Installation

Install using pip: pip install torch_ga

Requirements:

  • Python 3
  • torch
  • numpy

Conda Environment

An example environment is provided, but please feel free to create your own custom environment

conda create -n torch_ga -f environment.yml

Basic usage

There are two ways to use this library. In both ways we first create a GeometricAlgebra instance given a metric. Then we can either work on torch.Tensor instances directly where the last axis is assumed to correspond to the algebra's blades.

import torch
from torch_ga import GeometricAlgebra

# Create an algebra with 3 basis vectors given their metric.
# Contains geometric algebra operations.
ga = GeometricAlgebra(metric=[1, 1, 1])

# Create geometric algebra torch.Tensor for vector blades (ie. e_0 + e_1 + e_2).
# Represented as torch.Tensor with shape [8] (one value for each blade of the algebra).
# torch.Tensor: [0, 1, 1, 1, 0, 0, 0, 0]
ordinary_vector = ga.from_tensor_with_kind(torch.ones(3), kind="vector")

# 5 + 5 e_01 + 5 e_02 + 5 e_12
quaternion = ga.from_tensor_with_kind(torch.fill(dims=4, value=5), kind="even")

# 5 + 1 e_0 + 1 e_1 + 1 e_2 + 5 e_01 + 5 e_02 + 5 e_12
multivector = ordinary_vector + quaternion

# Inner product e_0 | (e_0 + e_1 + e_2) = 1
# ga.print is like print, but has extra formatting for geometric algebra torch.Tensor instances.
ga.print(ga.inner_prod(ga.e0, ordinary_vector))

# Exterior product e_0 ^ e_1 = e_01.
ga.print(ga.ext_prod(ga.e0, ga.e1))

# Grade reversal ~(5 + 5 e_01 + 5 e_02 + 5 e_12)
# = 5 + 5 e_10 + 5 e_20 + 5 e_21
# = 5 - 5 e_01 - 5 e_02 - 5 e_12
ga.print(ga.reversion(quaternion))

# torch.Tensor 5
ga.print(quaternion[0])

# torch.Tensor of shape [1]: -5 (ie. reversed sign of e_01 component)
ga.print(ga.select_blades_with_name(quaternion, "10"))

# torch.Tensor of shape [8] with only e_01 component equal to 5
ga.print(ga.keep_blades_with_name(quaternion, "10"))

Alternatively we can convert the geometric algebra torch.Tensor instance to MultiVector instances which wrap the operations and provide operator overrides for convenience. This can be done by using the __call__ operator of the GeometricAlgebra instance.

# Create geometric algebra torch.Tensor instances
a = ga.e123
b = ga.e1

# Wrap them as `MultiVector` instances
mv_a = ga(a)
mv_b = ga(b)

# Reversion ((~mv_a).tensor equivalent to ga.reversion(a))
print(~mv_a)

# Geometric / inner / exterior product
print(mv_a * mv_b)
print(mv_a | mv_b)
print(mv_a ^ mv_b)

Keras layers

torch_ga also provides Keras-like layers which provide layers similar to the existing ones but using multivectors instead. For example the GeometricProductDense layer is exactly the same as the Dense layer but uses multivector-valued weights and biases instead of scalar ones. The exact kind of multivector-type can be passed too. Example:

import torch as tf
from torch_ga import GeometricAlgebra
from torch_ga.layers import TensorToGeometric, GeometricToTensor, GeometricProductDense

# 4 basis vectors (e0^2=+1, e1^2=-1, e2^2=-1, e3^2=-1)
sta = GeometricAlgebra([1, -1, -1, -1])

# We want our dense layer to perform a matrix multiply
# with a matrix that has vector-valued entries.
vector_blade_indices = sta.get_kind_blade_indices(BladeKind.VECTOR),

# Create our input of shape [Batch, Units, BladeValues]
tensor = torch.ones([20, 6, 4])

# The matrix-multiply will perform vector * vector
# so our result will be scalar + bivector.
# Use the resulting blade type for the bias too which is
# added to the result.
result_indices = torch.concat([
    sta.get_kind_blade_indices(BladeKind.SCALAR), # 1 index
    sta.get_kind_blade_indices(BladeKind.BIVECTOR) # 6 indices
], axis=0)

sequence = nn.Sequential([
    # Converts the last axis to a dense multivector
    # (so, 4 -> 16 (total number of blades in the algebra))
    TensorToGeometric(sta, blade_indices=vector_blade_indices),
    # Perform matrix multiply with vector-valued matrix
    GeometricProductDense(
        algebra=sta, units=8, # units is analagous to Keras' Dense layer
        blade_indices_kernel=vector_blade_indices,
        blade_indices_bias=result_indices
    ),
    # Extract our wanted blade indices (last axis 16 -> 7 (1+6))
    GeometricToTensor(sta, blade_indices=result_indices)
])

# Result will have shape [20, 8, 7]
result = sequence(tensor)

Clifford Algebra

It is now available the Clifford Algebra, which has similar primitives to the Geometric Algebra class, but it is inspired by recent developments.

from torch_ga.clifford.algebra import CliffordAlgebra

signature = [1,1,-1]

cl = CliffordAlgebra(signature)

a = cl.embed(torch.tensor([3.,5.]),(3,4))
b = cl.embed(torch.tensor([2.,1.]),(3,4))

print(cl.product(a,b))
print(cl.inner_product(a,b),cl.outer_product(a,b))

# Converting to GA
ga = cl.to_ga()

a = ga(a)
b = ga(b)

print(a,b,a*b)
from torch_ga import GeometricAlgebra
signature = [1,1,-1]
# Converting to CL
ga = GeometricAlgebra(signature)
cl = ga.to_cl()

a = cl.embed(torch.tensor([3.,5.]),(3,4))
b = cl.embed(torch.tensor([2.,1.]),(3,4))

print(cl.product(a,b))
print(cl.inner_product(a,b),cl.outer_product(a,b))

Available layers

Class Description
[GeometricProductDense] Analagous to Keras' [Dense] with multivector-valued weights and biases. Each term in the matrix multiplication does the geometric product x * w.
[GeometricSandwichProductDense] Analagous to Keras' [Dense] with multivector-valued weights and biases. Each term in the matrix multiplication does the geometric product w *x * ~w.
[GeometricProductElementwise] Performs multivector-valued elementwise geometric product of the input units with a different weight for each unit.
[GeometricSandwichProductElementwise] Performs multivector-valued elementwise geometric sandwich product of the input units with a different weight for each unit.
[GeometricProductConv1D] Analagous to Keras' [Conv1D] with multivector-valued kernels and biases. Each term in the kernel multiplication does the geometric product x * k.
[TensorToGeometric] Converts from a [torch.Tensor] to the geometric algebra [torch.Tensor] with as many blades on the last axis as basis blades in the algebra where blade indices determine which basis blades the input's values belong to.
[GeometricToTensor] Converts from a geometric algebra [torch.Tensor] with as many blades on the last axis as basis blades in the algebra to a torch.Tensor where blade indices determine which basis blades we extract for the output.
[TensorWithKindToGeometric] Same as [TensorToGeometric] but using [BladeKind] (eg. "bivector", "even") instead of blade indices.
[GeometricToTensorWithKind] Same as [GeometricToTensor] but using [BladeKind] (eg. "bivector", "even") instead of blade indices.
[GeometricAlgebraExp] Calculates the exponential function of the input. Input must square to a scalar.

Notebooks

Generic examples

Using Keras layers to estimate triangle area

Classical Electromagnetism using Geometric Algebra

Quantum Electrodynamics using Geometric Algebra

Projective Geometric Algebra

1D Multivector-valued Convolution Example

Clifford class and its relationship with Geometric class

Tests

Tests using Python's built-in unittest module are available in the tests directory. All tests can be run by executing python -m unittest discover tests from the root directory of the repository.

Citing

For citing all versions the following BibTeX can be used

@software{torch_ga,
  author       = {Alesiani, Francesco},
  title        = {PyTorch Geometric Algebra},
  publisher    = {Github},
  url          = {https://github.com/falesiani/torch_ga}
}

Disclaimer

PyTorch, the PyTorch logo and any related marks are trademarks of Facebook, Inc.

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

torch_ga-0.0.6.tar.gz (45.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torch_ga-0.0.6-py3-none-any.whl (40.4 kB view details)

Uploaded Python 3

File details

Details for the file torch_ga-0.0.6.tar.gz.

File metadata

  • Download URL: torch_ga-0.0.6.tar.gz
  • Upload date:
  • Size: 45.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for torch_ga-0.0.6.tar.gz
Algorithm Hash digest
SHA256 719d910fa90a1c21bfe149cee9abb7b26e24de56f1343a7d0f7cd9d50c3df617
MD5 ec1cdf8fb4a7a3ff62205f38c4ea3ae8
BLAKE2b-256 ad5b2007899a62507e57b6b536cc6c11f68dc9c3e7c653178b9c5cded5eb9040

See more details on using hashes here.

File details

Details for the file torch_ga-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: torch_ga-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 40.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for torch_ga-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 8c4e4e042fcd2ed6b2ffde0f8fb6e53e868bab35fa35d88c858ad84f8b1276c9
MD5 611aa8fdfe9b45abd2fe0baa454fce7c
BLAKE2b-256 fe86e5f5213a8bc93e95db62c0a317b55e36d9d70bafa09100260317242cd554

See more details on using hashes here.

Supported by

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