Task: Geometric algebra




Go: package main

import (
    "fmt"
    "math/rand"
    "time"
)

type vector []float64

func e(n uint) vector {
    if n > 4 {
        panic("n must be less than 5")
    }
    result := make(vector, 32)
    result[1<<n] = 1.0
    return result
}

func cdot(a, b vector) vector {
    return mul(vector{0.5}, add(mul(a, b), mul(b, a)))
}

func neg(x vector) vector {
    return mul(vector{-1}, x)
}

func bitCount(i int) int {
    i = i - ((i >> 1) & 0x55555555)
    i = (i & 0x33333333) + ((i >> 2) & 0x33333333)
    i = (i + (i >> 4)) & 0x0F0F0F0F
    i = i + (i >> 8)
    i = i + (i >> 16)
    return i & 0x0000003F
}

func reorderingSign(i, j int) float64 {
    i >>= 1
    sum := 0
    for i != 0 {
        sum += bitCount(i & j)
        i >>= 1
    }
    cond := (sum & 1) == 0
    if cond {
        return 1.0
    }
    return -1.0
}

func add(a, b vector) vector {
    result := make(vector, 32)
    copy(result, a)
    for i, _ := range b {
        result[i] += b[i]
    }
    return result
}

func mul(a, b vector) vector {
    result := make(vector, 32)
    for i, _ := range a {
        if a[i] != 0 {
            for j, _ := range b {
                if b[j] != 0 {
                    s := reorderingSign(i, j) * a[i] * b[j]
                    k := i ^ j
                    result[k] += s
                }
            }
        }
    }
    return result
}

func randomVector() vector {
    result := make(vector, 32)
    for i := uint(0); i < 5; i++ {
        result = add(result, mul(vector{rand.Float64()}, e(i)))
    }
    return result
}

func randomMultiVector() vector {
    result := make(vector, 32)
    for i := 0; i < 32; i++ {
        result[i] = rand.Float64()
    }
    return result
}

func main() {
    rand.Seed(time.Now().UnixNano())
    for i := uint(0); i < 5; i++ {
        for j := uint(0); j < 5; j++ {
            if i < j {
                if cdot(e(i), e(j))[0] != 0 {
                    fmt.Println("Unexpected non-null scalar product.")
                    return
                }
            } else if i == j {
                if cdot(e(i), e(j))[0] == 0 {
                    fmt.Println("Unexpected null scalar product.")
                }
            }
        }
    }

    a := randomMultiVector()
    b := randomMultiVector()
    c := randomMultiVector()
    x := randomVector()

    
    fmt.Println(mul(mul(a, b), c))
    fmt.Println(mul(a, mul(b, c)))

    
    fmt.Println(mul(a, add(b, c)))
    fmt.Println(add(mul(a, b), mul(a, c)))

    
    fmt.Println(mul(add(a, b), c))
    fmt.Println(add(mul(a, c), mul(b, c)))

    
    fmt.Println(mul(x, x))
}




Python: import copy, random

def bitcount(n):
    return bin(n).count("1")

def reoderingSign(i, j):
    k = i >> 1
    sum = 0
    while k != 0:
        sum += bitcount(k & j)
        k = k >> 1
    return 1.0 if ((sum & 1) == 0) else -1.0

class Vector:
    def __init__(self, da):
        self.dims = da

    def dot(self, other):
        return (self * other + other * self) * 0.5

    def __getitem__(self, i):
        return self.dims[i]

    def __setitem__(self, i, v):
        self.dims[i] = v

    def __neg__(self):
        return self * -1.0

    def __add__(self, other):
        result = copy.copy(other.dims)
        for i in xrange(0, len(self.dims)):
            result[i] += self.dims[i]
        return Vector(result)

    def __mul__(self, other):
        if isinstance(other, Vector):
            result = [0.0] * 32
            for i in xrange(0, len(self.dims)):
                if self.dims[i] != 0.0:
                    for j in xrange(0, len(self.dims)):
                        if other.dims[j] != 0.0:
                            s = reoderingSign(i, j) * self.dims[i] * other.dims[j]
                            k = i ^ j
                            result[k] += s
            return Vector(result)
        else:
            result = copy.copy(self.dims)
            for i in xrange(0, len(self.dims)):
                self.dims[i] *= other
            return Vector(result)

    def __str__(self):
        return str(self.dims)

def e(n):
    assert n <= 4, "n must be less than 5"
    result = Vector([0.0] * 32)
    result[1 << n] = 1.0
    return result

def randomVector():
    result = Vector([0.0] * 32)
    for i in xrange(0, 5):
        result += Vector([random.uniform(0, 1)]) * e(i)
    return result

def randomMultiVector():
    result = Vector([0.0] * 32)
    for i in xrange(0, 32):
        result[i] = random.uniform(0, 1)
    return result

def main():
    for i in xrange(0, 5):
        for j in xrange(0, 5):
            if i < j:
                if e(i).dot(e(j))[0] != 0.0:
                    print "Unexpected non-null scalar product"
                    return
                elif i == j:
                    if e(i).dot(e(j))[0] == 0.0:
                        print "Unexpected non-null scalar product"

    a = randomMultiVector()
    b = randomMultiVector()
    c = randomMultiVector()
    x = randomVector()

    
    print (a * b) * c
    print a * (b * c)
    print

    
    print a * (b + c)
    print a * b + a * c
    print

    
    print (a + b) * c
    print a * c + b * c
    print

    
    print x * x

main()