Add Zig Matrix example
This commit is contained in:
parent
730666e26a
commit
1d70b482de
2
zig-matrix/.gitignore
vendored
Normal file
2
zig-matrix/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
zig-out/
|
||||
.zig-cache/
|
26
zig-matrix/build.zig
Normal file
26
zig-matrix/build.zig
Normal file
@ -0,0 +1,26 @@
|
||||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.Build) void {
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "zig-matrix",
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = b.host,
|
||||
});
|
||||
|
||||
b.installArtifact(exe);
|
||||
|
||||
const run_exe = b.addRunArtifact(exe);
|
||||
const run_step = b.step("run", "Run the application");
|
||||
|
||||
run_step.dependOn(&run_exe.step);
|
||||
|
||||
const unit_tests = b.addTest(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = b.host,
|
||||
});
|
||||
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
const run_unit_tests = b.addRunArtifact(unit_tests);
|
||||
|
||||
test_step.dependOn(&run_unit_tests.step);
|
||||
}
|
132
zig-matrix/src/main.zig
Normal file
132
zig-matrix/src/main.zig
Normal file
@ -0,0 +1,132 @@
|
||||
const std = @import("std");
|
||||
|
||||
const MatrixOpError = error{
|
||||
IncompatibleSizes,
|
||||
};
|
||||
|
||||
const Matrix = struct {
|
||||
allocator: std.mem.Allocator,
|
||||
numbers: []i64,
|
||||
dimensions: []const u64,
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, dimensions: []const u64) !Matrix {
|
||||
var size: u64 = 1;
|
||||
|
||||
for (dimensions) |dim| {
|
||||
size *= dim;
|
||||
}
|
||||
|
||||
return Matrix{
|
||||
.allocator = allocator,
|
||||
.numbers = try allocator.alloc(i64, size),
|
||||
.dimensions = dimensions,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn fill(self: Matrix, number: i64) void {
|
||||
var i: u64 = 0;
|
||||
while (i < self.numbers.len) {
|
||||
self.numbers[i] = number;
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(self: Matrix, allocator: std.mem.Allocator, other: Matrix) !Matrix {
|
||||
if (!std.mem.eql(u64, self.dimensions, other.dimensions)) {
|
||||
return error.IncompatibleSizes;
|
||||
}
|
||||
|
||||
var result: Matrix = try Matrix.init(allocator, self.dimensions);
|
||||
result.fill(0);
|
||||
|
||||
for (self.numbers, 0..) |num, idx| {
|
||||
result.numbers[idx] += num;
|
||||
}
|
||||
|
||||
for (other.numbers, 0..) |num, idx| {
|
||||
result.numbers[idx] += num;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn sub(self: Matrix, allocator: std.mem.Allocator, other: Matrix) !Matrix {
|
||||
if (!std.mem.eql(u64, self.dimensions, other.dimensions)) {
|
||||
return error.IncompatibleSizes;
|
||||
}
|
||||
|
||||
var result: Matrix = try Matrix.init(allocator, self.dimensions);
|
||||
result.fill(0);
|
||||
|
||||
for (self.numbers, 0..) |num, idx| {
|
||||
result.numbers[idx] += num;
|
||||
}
|
||||
|
||||
for (other.numbers, 0..) |num, idx| {
|
||||
result.numbers[idx] -= num;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
pub fn deinit(self: Matrix) void {
|
||||
self.allocator.free(self.numbers);
|
||||
}
|
||||
};
|
||||
|
||||
pub fn main() void {
|
||||
std.debug.print("Please use 'zig build test --summary all' to run tests. This example does not have any other runnable code.\n", .{});
|
||||
}
|
||||
|
||||
test "matrix init" {
|
||||
const mat = try Matrix.init(std.testing.allocator, &[_]u64{ 5, 5 });
|
||||
defer mat.deinit();
|
||||
|
||||
try std.testing.expect(mat.numbers.len == 25);
|
||||
}
|
||||
|
||||
test "matrix fill" {
|
||||
const mat = try Matrix.init(std.testing.allocator, &[_]u64{ 5, 5 });
|
||||
defer mat.deinit();
|
||||
mat.fill(123);
|
||||
|
||||
for (mat.numbers) |num| {
|
||||
try std.testing.expect(num == 123);
|
||||
}
|
||||
}
|
||||
|
||||
test "matrix add" {
|
||||
const mat1 = try Matrix.init(std.testing.allocator, &[_]u64{ 2, 2 });
|
||||
defer mat1.deinit();
|
||||
const mat2 = try Matrix.init(std.testing.allocator, &[_]u64{ 2, 2 });
|
||||
defer mat2.deinit();
|
||||
|
||||
mat1.fill(1);
|
||||
mat2.fill(2);
|
||||
|
||||
const mat3 = try mat1.add(std.testing.allocator, mat2);
|
||||
defer mat3.deinit();
|
||||
|
||||
try std.testing.expect(mat3.numbers[0] == 3);
|
||||
try std.testing.expect(mat3.numbers[1] == 3);
|
||||
try std.testing.expect(mat3.numbers[2] == 3);
|
||||
try std.testing.expect(mat3.numbers[3] == 3);
|
||||
}
|
||||
|
||||
test "matrix sub" {
|
||||
const mat1 = try Matrix.init(std.testing.allocator, &[_]u64{ 2, 2 });
|
||||
defer mat1.deinit();
|
||||
const mat2 = try Matrix.init(std.testing.allocator, &[_]u64{ 2, 2 });
|
||||
defer mat2.deinit();
|
||||
|
||||
mat1.fill(1);
|
||||
mat2.fill(2);
|
||||
|
||||
const mat3 = try mat1.sub(std.testing.allocator, mat2);
|
||||
defer mat3.deinit();
|
||||
|
||||
try std.testing.expect(mat3.numbers[0] == -1);
|
||||
try std.testing.expect(mat3.numbers[1] == -1);
|
||||
try std.testing.expect(mat3.numbers[2] == -1);
|
||||
try std.testing.expect(mat3.numbers[3] == -1);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user