I’m trying to represent vectorNs but having a hard time enforcing the same length.
Closest I’ve come to it is like this:
pub fn add(comptime n: usize, vectors: [][n]f64) [n]f64 {
var res: [n]f64 = undefined;
@memset(&res, 0);
for (vectors) |v| {
for (0..n) |idx| {
res[idx] += v[idx];
}
}
return res;
}
But I don’t want to specify the length in every function.
Another path I’ve tried is to embed the length into the type:
fn Vector(comptime n: usize) type {
return struct {
values: [n]f64,
const Self = @This();
pub fn add(self: *Self, v: Self) void {
for (0..v.values.len) |idx| {
self.values[idx] += v.values[idx];
}
}
}
}
But I’d like to keep it as a function rather than a method, which resulted me being in square one with the Vector parameter requiring N to be defined.
In fact, a sidequestion here is: What’s the easiest way to provide any amount of vectors for my function? I thought `vectors: [][n]f64` would be let me do .{ v1, v2, v3 } I was mistaken: type '[][3]f64' does not support array initialization syntax