···11# mitochondria
2233-Zig utility library providing data structures, linear algebra, and helper functions
33+Zig utility library providing data structures, linear algebra, and helper functions.
+68-46
src/la.zig
···11const std = @import("std");
22const math = std.math;
33+const assert = std.debug.assert;
3445/// Create a `columns`x`rows` matrix of `T`
56pub fn Matrix(comptime T: type, rows: usize, columns: usize) type {
66- std.debug.assert(rows > 0);
77- std.debug.assert(columns > 0);
77+ assert(rows > 0);
88+ assert(columns > 0);
89910 {
1011 const t = @typeInfo(T);
···61626263 /// Add all elements of the matrix to another matrix
6364 pub fn add(self: *const Self, other: *const Self) Self {
6464- var mat = self.copy();
6565+ var out = self.copy();
65666666- for (mat.inner, 0..) |row, i| {
6767- mat.inner[i] = row + other.inner[i];
6767+ for (out.inner, 0..) |row, i| {
6868+ out.inner[i] = row + other.inner[i];
6869 }
69707070- return mat;
7171+ return out;
7172 }
72737374 /// Subtract all elements of the matrix from another matrix
7475 pub fn sub(self: *const Self, other: *const Self) Self {
7575- var mat = self.copy();
7676+ var out = self.copy();
76777777- for (mat.inner, 0..) |row, i| {
7878- mat.inner[i] = row - other.inner[i];
7878+ for (out.inner, 0..) |row, i| {
7979+ out.inner[i] = row - other.inner[i];
7980 }
80818181- return mat;
8282+ return out;
8283 }
83848485 /// Multiply all elements of the matrix by a scalar
8586 pub fn mul(self: *const Self, scalar: T) Self {
8686- var mat = self.copy();
8787+ var out = self.copy();
87888888- for (mat.inner, 0..) |row, i| {
8989- mat.inner[i] = row * @as(Row, @splat(scalar));
8989+ for (out.inner, 0..) |row, i| {
9090+ out.inner[i] = row * @as(Row, @splat(scalar));
9091 }
91929292- return mat;
9393+ return out;
9394 }
94959596 /// Multiply a matrix with another matrix
9697 /// Each element in the matrix is multiplied by the
9798 /// element at the same index of the other matrix
9899 pub fn mul2(self: *const Self, other: *const Self) Self {
9999- var mat = self.copy();
100100+ var out = self.copy();
100101101101- for (mat.inner, other.inner, 0..) |r1, r2, i| {
102102- mat.inner[i] = r1 * r2;
102102+ for (out.inner, other.inner, 0..) |r1, r2, i| {
103103+ out.inner[i] = r1 * r2;
103104 }
104105105105- return mat;
106106+ return out;
106107 }
107108108109 /// Divide all elements of the matrix by a scalar
109110 pub fn div(self: *const Self, scalar: T) Self {
110110- var mat = self.copy();
111111+ var out = self.copy();
111112112112- for (mat.inner, 0..) |row, i| {
113113- mat.inner[i] = row / @as(Row, @splat(scalar));
113113+ for (out.inner, 0..) |row, i| {
114114+ out.inner[i] = row / @as(Row, @splat(scalar));
114115 }
115116116116- return mat;
117117+ return out;
117118 }
118119119120 /// Divide a matrix by another matrix
120121 /// Each element in the matrix is divided by the
121122 /// element at the same index of the other matrix
122123 pub fn div2(self: *const Self, other: *const Self) Self {
123123- var mat = self.copy();
124124+ var out = self.copy();
124125125125- for (mat.inner, other.inner, 0..) |r1, r2, i| {
126126- mat.inner[i] = r1 / r2;
126126+ for (out.inner, other.inner, 0..) |r1, r2, i| {
127127+ out.inner[i] = r1 / r2;
127128 }
128129129129- return mat;
130130+ return out;
130131 }
131132132133 /// Get the magnitude of the vector
···203204204205 /// Call `func` on all the elements of the matrix
205206 pub fn map(self: *const Self, func: *const fn (T) T) Self {
206206- var mat = self.copy();
207207+ var out = self.copy();
207208208208- for (0..mat.inner.len) |i| {
209209- for (0..mat.inner[i].len) |j| {
210210- mat.inner[i][j] = func(mat.inner[i][j]);
209209+ for (0..out.inner.len) |i| {
210210+ for (0..out.inner[i].len) |j| {
211211+ out.inner[i][j] = func(out.inner[i][j]);
211212 }
212213 }
213214214214- return mat;
215215+ return out;
215216 }
216217217218 /// Return a X unit matrix
218219 pub fn unitX() Self {
219219- var mat = Self.zeroed();
220220+ var out = Self.zeroed();
220221221221- for (&mat.inner) |*row| {
222222+ for (&out.inner) |*row| {
222223 row[0] = 1;
223224 }
224225225225- return mat;
226226+ return out;
226227 }
227228228229 /// Return a Y unit matrix
229230 pub fn unitY() Self {
230230- std.debug.assert(rows >= 2);
231231+ assert(rows >= 2);
231232232232- var mat = Self.zeroed();
233233+ var out = Self.zeroed();
233234234234- for (&mat.inner) |*row| {
235235+ for (&out.inner) |*row| {
235236 row[1] = 1;
236237 }
237238238238- return mat;
239239+ return out;
239240 }
240241241242 /// Return a Z unit matrix
242243 pub fn unitZ() Self {
243243- std.debug.assert(rows >= 3);
244244+ assert(rows >= 3);
244245245245- var mat = Self.zeroed();
246246+ var out = Self.zeroed();
246247247247- for (&mat.inner) |*row| {
248248+ for (&out.inner) |*row| {
248249 row[2] = 1;
249250 }
250251251251- return mat;
252252+ return out;
252253 }
253254254255 /// Return a W unit matrix
255256 pub fn unitW() Self {
256256- std.debug.assert(rows >= 4);
257257+ assert(rows >= 4);
257258258258- var mat = Self.zeroed();
259259+ var out = Self.zeroed();
259260260260- for (&mat.inner) |*row| {
261261+ for (&out.inner) |*row| {
261262 row[3] = 1;
262263 }
263264264264- return mat;
265265+ return out;
265266 }
266267 };
267268}
···302303pub const Mat4D = Matrix(f64, 4, 4);
303304304305// TODO: idk how the fuck i will, but add quaternions eventually.
306306+307307+pub fn vec(comptime T: type, comptime components: usize, v: @Vector(components, T)) Vector(T, components) {
308308+ return Vector(T, components).init(.{v});
309309+}
310310+311311+pub fn mat(
312312+ comptime T: type,
313313+ comptime rows: usize,
314314+ comptime columns: usize,
315315+ rep: [columns]@Vector(rows, T),
316316+) Matrix(T, rows, columns) {
317317+ return Matrix(T, rows, columns).init(rep);
318318+}
305319306320test "mul" {
307321 const x = Matrix(f32, 3, 5).init(.{
···412426 try std.testing.expectEqual(rm.inner[0], .{ 27, 30 });
413427 try std.testing.expectEqual(rm.inner[1], .{ -35, 36 });
414428}
429429+430430+test "shorthand" {
431431+ _ = vec(f32, 2, .{ 3, 4 });
432432+ _ = mat(f32, 2, 2, .{
433433+ .{ 3, 4 },
434434+ .{ 5, 6 },
435435+ });
436436+}