Task: Geometric algebra


C++: #include <algorithm>
#include <iostream>
#include <random>
#include <vector>

double uniform01() {
    static std::default_random_engine generator;
    static std::uniform_real_distribution<double> distribution(0.0, 1.0);
    return distribution(generator);
}

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

double reorderingSign(int i, int j) {
    int k = i >> 1;
    int sum = 0;
    while (k != 0) {
        sum += bitCount(k & j);
        k = k >> 1;
    }
    return ((sum & 1) == 0) ? 1.0 : -1.0;
}

struct MyVector {
public:
    MyVector(const std::vector<double> &da) : dims(da) {
        
    }

    double &operator[](size_t i) {
        return dims[i];
    }

    const double &operator[](size_t i) const {
        return dims[i];
    }

    MyVector operator+(const MyVector &rhs) const {
        std::vector<double> temp(dims);
        for (size_t i = 0; i < rhs.dims.size(); ++i) {
            temp[i] += rhs[i];
        }
        return MyVector(temp);
    }

    MyVector operator*(const MyVector &rhs) const {
        std::vector<double> temp(dims.size(), 0.0);
        for (size_t i = 0; i < dims.size(); i++) {
            if (dims[i] != 0.0) {
                for (size_t j = 0; j < dims.size(); j++) {
                    if (rhs[j] != 0.0) {
                        auto s = reorderingSign(i, j) * dims[i] * rhs[j];
                        auto k = i ^ j;
                        temp[k] += s;
                    }
                }
            }
        }
        return MyVector(temp);
    }

    MyVector operator*(double scale) const {
        std::vector<double> temp(dims);
        std::for_each(temp.begin(), temp.end(), [scale](double a) { return a * scale; });
        return MyVector(temp);
    }

    MyVector operator-() const {
        return *this * -1.0;
    }

    MyVector dot(const MyVector &rhs) const {
        return (*this * rhs + rhs * *this) * 0.5;
    }

    friend std::ostream &operator<<(std::ostream &, const MyVector &);

private:
    std::vector<double> dims;
};

std::ostream &operator<<(std::ostream &os, const MyVector &v) {
    auto it = v.dims.cbegin();
    auto end = v.dims.cend();

    os << '[';
    if (it != end) {
        os << *it;
        it = std::next(it);
    }
    while (it != end) {
        os << ", " << *it;
        it = std::next(it);
    }
    return os << ']';
}

MyVector e(int n) {
    if (n > 4) {
        throw new std::runtime_error("n must be less than 5");
    }

    auto result = MyVector(std::vector<double>(32, 0.0));
    result[1 << n] = 1.0;
    return result;
}

MyVector randomVector() {
    auto result = MyVector(std::vector<double>(32, 0.0));
    for (int i = 0; i < 5; i++) {
        result = result + MyVector(std::vector<double>(1, uniform01())) * e(i);
    }
    return result;
}

MyVector randomMultiVector() {
    auto result = MyVector(std::vector<double>(32, 0.0));
    for (int i = 0; i < 32; i++) {
        result[i] = uniform01();
    }
    return result;
}

int main() {
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (i < j) {
                if (e(i).dot(e(j))[0] != 0.0) {
                    std::cout << "Unexpected non-null scalar product.";
                    return 1;
                } else if (i == j) {
                    if (e(i).dot(e(j))[0] == 0.0) {
                        std::cout << "Unexpected null scalar product.";
                    }
                }
            }
        }
    }

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

    
    std::cout << ((a * b) * c) << '\n';
    std::cout << (a * (b * c)) << "\n\n";

    
    std::cout << (a * (b + c)) << '\n';
    std::cout << (a * b + a * c) << "\n\n";

    
    std::cout << ((a + b) * c) << '\n';
    std::cout << (a * c + b * c) << "\n\n";

    
    std::cout << (x * x) << '\n';

    return 0;
}







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