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;
}




Java: import java.util.Arrays;
import java.util.Random;

public class GeometricAlgebra {
    private static 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;
    }

    private static 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;
    }

    static class Vector {
        private double[] dims;

        public Vector(double[] dims) {
            this.dims = dims;
        }

        public Vector dot(Vector rhs) {
            return times(rhs).plus(rhs.times(this)).times(0.5);
        }

        public Vector unaryMinus() {
            return times(-1.0);
        }

        public Vector plus(Vector rhs) {
            double[] result = Arrays.copyOf(dims, 32);
            for (int i = 0; i < rhs.dims.length; ++i) {
                result[i] += rhs.get(i);
            }
            return new Vector(result);
        }

        public Vector times(Vector rhs) {
            double[] result = new double[32];
            for (int i = 0; i < dims.length; ++i) {
                if (dims[i] != 0.0) {
                    for (int j = 0; j < rhs.dims.length; ++j) {
                        if (rhs.get(j) != 0.0) {
                            double s = reorderingSign(i, j) * dims[i] * rhs.dims[j];
                            int k = i ^ j;
                            result[k] += s;
                        }
                    }
                }
            }
            return new Vector(result);
        }

        public Vector times(double scale) {
            double[] result = dims.clone();
            for (int i = 0; i < 5; ++i) {
                dims[i] *= scale;
            }
            return new Vector(result);
        }

        double get(int index) {
            return dims[index];
        }

        void set(int index, double value) {
            dims[index] = value;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder("(");
            boolean first = true;
            for (double value : dims) {
                if (first) {
                    first = false;
                } else {
                    sb.append(", ");
                }
                sb.append(value);
            }
            return sb.append(")").toString();
        }
    }

    private static Vector e(int n) {
        if (n > 4) {
            throw new IllegalArgumentException("n must be less than 5");
        }
        Vector result = new Vector(new double[32]);
        result.set(1 << n, 1.0);
        return result;
    }

    private static final Random rand = new Random();

    private static Vector randomVector() {
        Vector result = new Vector(new double[32]);
        for (int i = 0; i < 5; ++i) {
            Vector temp = new Vector(new double[]{rand.nextDouble()});
            result = result.plus(temp.times(e(i)));
        }
        return result;
    }

    private static Vector randomMultiVector() {
        Vector result = new Vector(new double[32]);
        for (int i = 0; i < 32; ++i) {
            result.set(i, rand.nextDouble());
        }
        return result;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; ++i) {
            for (int j = 0; j < 5; ++j) {
                if (i < j) {
                    if (e(i).dot(e(j)).get(0) != 0.0) {
                        System.out.println("Unexpected non-null scalar product.");
                        return;
                    }
                }
            }
        }

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

        
        System.out.println(a.times(b).times(c));
        System.out.println(a.times(b.times(c)));
        System.out.println();

        
        System.out.println(a.times(b.plus(c)));
        System.out.println(a.times(b).plus(a.times(c)));
        System.out.println();

        
        System.out.println(a.plus(b).times(c));
        System.out.println(a.times(c).plus(b.times(c)));
        System.out.println();

        
        System.out.println(x.times(x));
    }
}