Skip to content

pblischak/zig-ndarray

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

N-Dimensional Arrays in Zig

Installation

.{
    .name = "my_project",
    .version = "0.1.0",
    .paths = .{
        "build.zig",
        "build.zig.zon",
        "README.md",
        "LICENSE",
        "src",
    },
    .dependencies = .{
        // This will link to tagged v0.1.0 release.
        // Change the url and hash to link to a specific commit.
        .ndarray = {
            .url = "",
            .hash = "",
        }
    },
}

Then, in the build.zig file, add the following lines within the build function to include ndarray as a module:

pub fn build(b: *std.Build) void {
    // exe setup...

    const ndarray_dep = b.dependency("ndarray", .{
            .target = target,
            .optimize = optimize,
    });

    const ndarray_module = ndarray_dep.module("ndarray");
    exe.root_module.addImport("ndarray", ndarray_module);

    // additional build steps...
}

Check out the build files in the examples/ folder for some demos of complete sample code projects.

Getting Started

const std = @import("std");
const NDArray = @import("ndarray").NDArray;
const Allocator = std.mem.allocator;

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    const allocator = gpa.allocator();
    defer {
        const status = gpa.deinit();
        std.testing.expect(status == .ok) catch {
            @panic("Memory leak!");
        };
    }

    const arr = try NDArray(f32, 3).init(.{10, 10, 2}, allocator);
    defer arr.deinit();
}

Acknowledgements

The ndarray module modifies the code from this gist by AssortedFantasy by adding within-struct allocation and by making things more compatible with the Zig package management system. Many thanks to AssortedFantasy for the initial implementation of a multidimensional array structure in Zig.