Task: Geometric algebra







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()


VB: Option Strict On

Imports System.Text

Module Module1

    Structure Vector
        Private ReadOnly dims() As Double

        Public Sub New(da() As Double)
            dims = da
        End Sub

        Public Shared Operator -(v As Vector) As Vector
            Return v * -1.0
        End Operator

        Public Shared Operator +(lhs As Vector, rhs As Vector) As Vector
            Dim result(31) As Double
            Array.Copy(lhs.dims, 0, result, 0, lhs.Length)
            For i = 1 To result.Length
                Dim i2 = i - 1
                result(i2) = lhs(i2) + rhs(i2)
            Next
            Return New Vector(result)
        End Operator

        Public Shared Operator *(lhs As Vector, rhs As Vector) As Vector
            Dim result(31) As Double
            For i = 1 To lhs.Length
                Dim i2 = i - 1
                If lhs(i2) <> 0.0 Then
                    For j = 1 To lhs.Length
                        Dim j2 = j - 1
                        If rhs(j2) <> 0.0 Then
                            Dim s = ReorderingSign(i2, j2) * lhs(i2) * rhs(j2)
                            Dim k = i2 Xor j2
                            result(k) += s
                        End If
                    Next
                End If
            Next
            Return New Vector(result)
        End Operator

        Public Shared Operator *(v As Vector, scale As Double) As Vector
            Dim result = CType(v.dims.Clone, Double())
            For i = 1 To result.Length
                Dim i2 = i - 1
                result(i2) *= scale
            Next
            Return New Vector(result)
        End Operator

        Default Public Property Index(key As Integer) As Double
            Get
                Return dims(key)
            End Get
            Set(value As Double)
                dims(key) = value
            End Set
        End Property

        Public ReadOnly Property Length As Integer
            Get
                Return dims.Length
            End Get
        End Property

        Public Function Dot(rhs As Vector) As Vector
            Return (Me * rhs + rhs * Me) * 0.5
        End Function

        Private Shared Function BitCount(i As Integer) As Integer
            i -= ((i >> 1) And &H55555555)
            i = (i And &H33333333) + ((i >> 2) And &H33333333)
            i = (i + (i >> 4)) And &HF0F0F0F
            i += (i >> 8)
            i += (i >> 16)
            Return i And &H3F
        End Function

        Private Shared Function ReorderingSign(i As Integer, j As Integer) As Double
            Dim k = i >> 1
            Dim sum = 0
            While k <> 0
                sum += BitCount(k And j)
                k >>= 1
            End While
            Return If((sum And 1) = 0, 1.0, -1.0)
        End Function

        Public Overrides Function ToString() As String
            Dim it = dims.GetEnumerator

            Dim sb As New StringBuilder("[")
            If it.MoveNext() Then
                sb.Append(it.Current)
            End If
            While it.MoveNext
                sb.Append(", ")
                sb.Append(it.Current)
            End While
            sb.Append("]")
            Return sb.ToString
        End Function
    End Structure

    Function DoubleArray(size As Integer) As Double()
        Dim result(size - 1) As Double
        For i = 1 To size
            Dim i2 = i - 1
            result(i2) = 0.0
        Next
        Return result
    End Function

    Function E(n As Integer) As Vector
        If n > 4 Then
            Throw New ArgumentException("n must be less than 5")
        End If

        Dim result As New Vector(DoubleArray(32))
        result(1 << n) = 1.0
        Return result
    End Function

    ReadOnly r As New Random()

    Function RandomVector() As Vector
        Dim result As New Vector(DoubleArray(32))
        For i = 1 To 5
            Dim i2 = i - 1
            Dim singleton() As Double = {r.NextDouble()}
            result += New Vector(singleton) * E(i2)
        Next
        Return result
    End Function

    Function RandomMultiVector() As Vector
        Dim result As New Vector(DoubleArray(32))
        For i = 1 To result.Length
            Dim i2 = i - 1
            result(i2) = r.NextDouble()
        Next
        Return result
    End Function

    Sub Main()
        For i = 1 To 5
            Dim i2 = i - 1
            For j = 1 To 5
                Dim j2 = j - 1
                If i2 < j2 Then
                    If E(i2).Dot(E(j2))(0) <> 0.0 Then
                        Console.Error.WriteLine("Unexpected non-null scalar product")
                        Return
                    End If
                ElseIf i2 = j2 Then
                    If E(i2).Dot(E(j2))(0) = 0.0 Then
                        Console.Error.WriteLine("Unexpected null scalar product")
                        Return
                    End If
                End If
            Next
        Next

        Dim a = RandomMultiVector()
        Dim b = RandomMultiVector()
        Dim c = RandomMultiVector()
        Dim x = RandomVector()

        
        Console.WriteLine((a * b) * c)
        Console.WriteLine(a * (b * c))
        Console.WriteLine()

        
        Console.WriteLine(a * (b + c))
        Console.WriteLine(a * b + a * c)
        Console.WriteLine()

        
        Console.WriteLine((a + b) * c)
        Console.WriteLine(a * c + b * c)
        Console.WriteLine()

        
        Console.WriteLine(x * x)
    End Sub

End Module