diff options
author | Tom Barrett <tom@tombarrett.xyz> | 2021-06-30 17:19:47 +0200 |
---|---|---|
committer | Tom Barrett <tom@tombarrett.xyz> | 2021-06-30 17:19:47 +0200 |
commit | d79befe12a8d6b32a41beb88d6e75b12a75615a7 (patch) | |
tree | 365aa28165efa3e4727ce5ea0edbdf6c69135952 /src/matrix.zig |
rotating cube
Diffstat (limited to 'src/matrix.zig')
-rw-r--r-- | src/matrix.zig | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/matrix.zig b/src/matrix.zig new file mode 100644 index 0000000..eaf9685 --- /dev/null +++ b/src/matrix.zig @@ -0,0 +1,54 @@ +const std = @import("std"); +const ArrayList = std.ArrayList; + +const matrix = struct { + h: usize, + w: usize, + x: ArrayList(f32), +}; + +pub fn new(h: usize, w: usize, x: []f32) matrix { + var m = matrix{ + .h = h, + .w = w, + .x = ArrayList(f32).init(std.heap.page_allocator), + }; + + var i: usize = 0; + while (i < h * w) : (i += 1) { + if (m.x.append(x[i])) |val| {} else |err| {} + } + + return m; +} + +pub fn dot(a: []f32, b: []f32, len: usize, step: usize) f32 { + var x: f32 = 0; + var j: usize = 0; + var i: usize = len; + var k: usize = 0; + + while (i > 0) : (i -= 1) { + x += a[k] * b[j]; + k += 1; + j += step; + } + + return x; +} + +pub fn multiply(a: matrix, b: matrix) matrix { + var x = ArrayList(f32).init(std.heap.page_allocator); + var i: usize = 0; + var k: usize = 0; + + while (i < a.h) : (i += 1) { + var j: usize = 0; + while (j < b.w) : (j += 1) { + if (x.append(dot(a.x.items[k..], b.x.items[j..], a.w, b.w))) |val| {} else |err| {} + } + k += a.w; + } + + return new(a.h, b.w, x.items); +} |