Skip to content

Examples

Kauê 地球の土星人 edited this page Sep 6, 2023 · 5 revisions

Generic

Batch Operations

const zilliam = @import("zilliam");

// you can change this to any algebra
const Alg = zilliam.Algebra(f32, 3, 0, 1);
const Blades = zilliam.blades.Blades(Alg, .{});
const Types = Blades.FormatTypes;

const Vector = Types[1];
const Bivector = Types[2];

pub fn main() !void {
    // This generates a batch type that does two operations at once.
    const BivectorBatch = Bivector.getBatchType(2);
    const VectorBatch = Vector.getBatchType(2);

    var buf: [2048]u8 = undefined;
    for (0..Bivector.Count) |a_i| {
        var c = Bivector{};
        c.val[a_i] = 1;

        for (0..Vector.Count) |b_i| {
            var a = BivectorBatch{};
            var b = VectorBatch{};

            a.val[a_i] = .{ 1, 2 };
            b.val[b_i] = .{ 1, -1 };

            // This is a Trivector, it gets properly dispatched
            const res = a.wedge(b);

            for (0..2) |i| {
                const r_w = res.get(i);

                var r_s = try a.get(i).print(&buf);
                std.debug.print("{s} ^ ", .{r_s});
                r_s = try b.get(i).print(&buf);
                std.debug.print("{s} = ", .{r_s});
                r_s = try r_w.print(&buf);
                std.debug.print("{s}\n", .{r_s});
            }

            std.debug.print("\n", .{});
        }
    }
}

// ...
// 1.0000e23 ^ 1.0000e1 = 1.0000e123
// 2.0000e23 ^ -1.0000e1 = -2.0000e123
// ...

Dual

Forward Automatic Differentiation

// function we want derivatives of
const Alg = Algebra(i32, 0, 0, 1);

const fn_str = "(x + off1) * (x + off2)";

// evaluate regular function
try std.testing.expectEqualSlices(
    i32,
    &.{ 20, 0 },
    &(try Alg.eval(fn_str, .{ .x = 3, .off1 = 2, .off2 = 1 })).val,
);

// evaluate derivative
try std.testing.expectEqualSlices(
    i32,
    &.{ 20, 9 },
    &(try Alg.eval(fn_str, .{ .x = try Alg.eval("3+e0", .{}), .off1 = 2, .off2 = 1 })).val,
);

PGA

2D

const zilliam = @import("zilliam");

const Pga = zilliam.PGA(f32, 2);

const Point = Pga.Point;
const A = Point.create(.{ -1, -1 });
const C = Point.create(.{ 1, 1 });

const L = Point.create(.{ 0, 0.5 }).regressive(Point.create(.{ 1, -0.5 }));

const AC = A.regressive(C);

// intersection of line L with line AC
const D = L.wedge(AC);

// Point.get(D) == .{0.25, 0.25}

3D

const zilliam = @import("zilliam");

// this can work in any dimension!
const Pga = zilliam.PGA(f32, 3);

const Point = Pga.Point;
const A = Point.create(.{ -1, -1, -1 });
const B = Point.create(.{ -1, 1, 1 });
const C = Point.create(.{ 1, 1, 1 });

const z = 1.0;
const P = Point.create(.{ 0, 0, z }).regressive(Point.create(.{ 1, 0, z })).regressive(Point.create(.{ 0, 1, z }));

const AC = A.regressive(C);

// the intersection of line AC with the plane P
const D = AC.wedge(P);

// Point.get(D) == .{ 1.0, 1.0, 1.0 }

Complex

Comptime Mandelbrot Fractal

const zilliam = @import("zilliam");
const Complex = zilliam.Algebra(f32, 0, 1, 0);

pub fn mandelbrot(comptime size: usize, comptime steps: usize) type {
    var data: [size][2 * size]bool = undefined;
    for (0..2 * size) |x| {
        for (0..size) |y| {
            const x_f: f32 = @floatFromInt(x);
            const y_f: f32 = @floatFromInt(y);
            const c: Complex = .{ .val = .{ (x_f / size - 1.0) * 2, (y_f / size - 0.5) * 2 } };
            var it: Complex = .{ .val = .{ 0, 0 } };
            data[y][x] = true;
            for (0..steps) |_| {
                it = it.mul(it).add(c);
                const vec: @Vector(2, f32) = it.val;
                if (@reduce(.Add, vec * vec) > steps) {
                    data[y][x] = false;
                    break;
                }
            }
        }
    }

    const data_cons = data;
    return struct {
        pub const Data = data_cons;
    };
}

pub fn main() !void {
    const Mandelbrot = mandelbrot(50, 80);
    std.debug.print("\n", .{});
    for (Mandelbrot.Data) |column| {
        for (column) |elem| {
            if (elem) {
                std.debug.print("#", .{});
            } else {
                std.debug.print(" ", .{});
            }
        }
        std.debug.print("\n", .{});
    }
}
Clone this wiki locally