MyNixOS website logo
Description

Clifford Algebra of three dimensional space.

Haskell Library implementing standard functions for the Algebra of Physical Space Cl(3,0)

--          CCCCCCCCCC      llllllllll                     3333333333
--      CCCCCCCCCCCCCCCCCC   llllllllll                333333333333333333
--    CCCCCCC      CCCCCCCCCC llllllllll            3333333333       333333
--   CCCCC               CCC   llllllllll            333         ##   33333
--  CCCCCC                      llllllllll                       #    33333
--  CCCCC                        llllllllll                          33333
--  CCCCC                         llllllllll          /      @    333333
--  CCCCC                          llllllllll        /|  +         33333
--  CCCCC                          lllllllllll       \|  +         33333
--  CCCCC                         lllllllllllll       \      @    333333
--  CCCCC                        lllllll lllllll                     33333
--  CCCCCC                      lllllll   lllllll                #    33333
--   CCCCC               CCC   lllllll     lllllll   333         ##   33333
--    CCCCCCC      CCCCCCCCCC lllllll       lllllll 3333333333       333333
--      CCCCCCCCCCCCCCCCCC   lllllll         lllllll   333333333333333333
--          CCCCCCCCCC      lllllll           lllllll      3333333333

Cl3

Cl3 is a Haskell Library implementing standard functions for the Algebra of Physical Space Cl(3,0)

The goal of the Cl3 library is to provide a specialized, safe, high performance, Algebra of Physical Space implementation. This library is suitable for physics simulations. The library integrates into Haskell's standard prelude and has few dependencies. The library uses a ADT data type to specialize to specific graded elements in the Algebra of Physical Space.

ADT Interface

The constructors are specialized to single and double grade combinations and the general case of APS. Using the specialized constructors helps the compiler to compile to code similar to that you would hand write. The constructors follow the following conventions for basis.

scalar = R e0
vector = V3 e1 e2 e3
bivector = BV e23 e31 e12
trivectorPseudoScalar = I e123
paravector = PV e0 e1 e2 e3
quarternion = H e0 e23 e31 e12
complex = C e0 e123
biparavector = BPV e1 e2 e3 e23 e31 e12
oddparavector = ODD e1 e2 e3 e123
triparavector = TPV e23 e31 e12 e123
aps = APS e0 e1 e2 e3 e23 e31 e12 e123

Usage

In MATLAB or Octave one can write: sqrt(-25) and get 5.0i

In standard Haskell sqrt (-25) will produce NaN

But using the Cl3 library sqrt (-25) :: Cl3 will produce I 5.0, and likewise (I 5.0)^2 will produce R (-25)

If the unit imaginary is defined as i = I 1, expressions very similar to MATLAB can be formed 1.2 + 2.3*i will produce C 1.2 2.3

Vector addition is also natural, two arbitrary vectors v1 = V3 a1 a2 a3 and v2 = V3 b1 b2 b3 can be added v1 + v2 and scaled 2*(v1+v2)

The dot product (inner product) of two arbitrary vectors is toR $ v1 * v2, that is the scalar part of the geometric product of two vectors.

The cross product is the Hodge Dual of the wedge product (outer product) -i * toBV (v1*v2)

The multiplication of two unit vectors is related to the rotor rotating from u_from to u_to like so rot = sqrt $ u_to * u_from

Any arbitrary vector can be rotated by a rotor with the equation of v' = rot * v * dag rot

Rotors can also be formed with an axis unit vector u and real scalar angle theta in units of radians, it produces the versor (unit quaternion) rot = exp $ (-i/2) * theta * u

For special relativity with the velocity vector v and speed of light scalar c:

  • Beta is beta = v / c
  • Rapidity is rapidity = atanh beta
  • Gamma is gamma = cosh rapidity
  • Composition of velocities is simply adding the two rapidities and converting back to velocity
  • Proper Velocity is w = c * sinh rapidity or w = gamma * v
  • Four Velocity is a paravector u = exp rapidity where the real scalar part is gamma * c and the vector part is w / c
  • The Boost is boost = exp $ rapidity / 2

APS Basis

Where e0 is the scalar basis frequently refered to as "1", in other texts.

e1, e2, and e3 are the vector basis of 3 orthonormal vectors.

e23, e31, and e12 are the bivector basis, these are formed by the outer product of two vector basis. For instance in the case of e23, the outer product, or wedge product, is e2 /\ e3, but because this can be simplified to the geometric product of e2 * e3 because the scalar part is zero for orthoginal vector basis'. The geometric product of the two basis vectors is further shortened for brevity to e23.

e123 is the trivector basis, and is formed by the wedge product of e1 /\ e2 /\ e3, and likewise shortened to e123

Multiplication of the basis elements

The basis vectors multiply with the following multiplication table:

Multe0e1e2e3e23e31e12e123
e0e0e1e2e3e23e31e12e123
e1e1e0e12-e31e123-e3e2e23
e2e2-e12e0e23e3e123-e1e31
e3e3e31-e23e0-e2e1e123e12
e23e23e123-e3e2-e0-e12e31-e1
e31e31e3e123-e1e12-e0-e23-e2
e12e12-e2e1e123-e31e23-e0-e3
e123e123e23e31e12-e1-e2-e3-e0

Multiplication of the ADT Constructors

The grade specialized type constructors multiply with the following multiplication table:

MultRV3BVIPVHCBPVODDTPVAPS
RRV3BVIPVHCBPVODDTPVAPS
V3V3HODDBVAPSODDBPVAPSODDAPSAPS
BVBVODDHV3APSHBPVAPSODDAPSAPS
IIBVV3RTPVODDCBPVHPVAPS
PVPVAPSAPSTPVAPSAPSAPSAPSAPSAPSAPS
HHODDHODDAPSHAPSAPSODDAPSAPS
CCBPVBPVCAPSAPSCBPVAPSAPSAPS
BPVBPVAPSODDBPVAPSAPSBPVAPSAPSAPSAPS
ODDODDODDTPVHAPSODDAPSAPSHAPSAPS
TPVTPVAPSAPSPVAPSAPSAPSAPSAPSAPSAPS
APSAPSAPSAPSAPSAPSAPSAPSAPSAPSAPSAPS

Performace Benchmarking

A benchmark has been developed based on the Haskell entry for the N-Body Benchmark in the The Computer Language Benchmarks Game with some modifications to run with Criterion. On my machine with GHC-8.10.7 the current fastest implementation completes 50M steps with a mean time of 4.014 seconds. The benchmark uses a hand rolled implementation of vector math. The Cl3 implementation completes 50M steps with a mean time of 5.691 seconds. This 1.67 second difference amounts to a 33.5 ns difference in the inner loop. This performance has been degraded with GHC regressions in GHC-9.0.2 and GHC-9.2.2 by ~5x. In the 3.0 release a massiv benchmark was added in addition to a weigh based benchmark.

Saftey and Correctness

In the 3.0 release Liquid Haskell support was added, Liquid Haskell did prove its worth by finding a couple of bugs in the implementation. So far it is an initial release and not much has been done to fully integrate Liquid Haskell to the library.

Design Philosophy

The design space for Clifford Algebra libraries was explored quite a bit before the development of this library. Initially the isomorphism of APS with 2x2 Complex Matrices was used, this had the draw back that multiplying the scalar 2 * 2 would incur all of the computational cost of multiplying two 2x2 complex matrices. Then the design was changed to lists that contained the basis' values, but lists are computationally slow and do not produce well optimized code. Then a single constructor data type for APS was developed, but this had all of the drawbacks of 2x2 complex matrices. The specialized ADT Constructor version of the library was developed and it showed that it had some promise. More of the design space was explored, a version of the Cl3 library was developed using Multi-parameter Type Classes and Functional Dependencies, this didn't appear to have much gained over the specialized ADT Syntax interface and it didn't use the standard Prelude classes like Num, Float, etc. It was also difficult for me to figure out how to code a reduce function. So the specialized ADT Constructor design of the Cl3 library was finished and released.

How does this fit in with the existing Haskell ecosystem?

Cl3 is meant to be a Linear killer based on Geometric Algebra. The linear package consists of many different types that are not easily combinable using the Num Class, and require many specialized functions each to multiply a different combination of types.

The clifford package uses the Numeric Prelude, for a Clifford Algebra of arbitrary signature that stores multivector blades in a list data structure.

The clif is for symbolic computing using symbolic and numeric computations with finite and infinite-dimensional Clifford algebras arising from arbitrary bilinear forms. The libraries representation of a Cliffor also makes use of lists.

Metadata

Version

3.0.0.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows