#Matrix Multiplication function parameters help

1 messages · Page 1 of 1 (latest)

fluid pier
#

i dont know how to declare the function for matrix multiplication.
i want to do matrix.mult(other_mat) .
i have this right now:

 pub fn mul(self: Self, comptime other_cols: usize, other: Matrix(T, cols, other_cols)) Matrix(T, rows, other_cols) {
            const p = other_cols;
            var out = Matrix(T, rows, p).init();
            for (0..rows) |i| {
                for (0..p) |j| {
                    var s: T = 0;
                    for (0..cols) |k| {
                        s += self.get(i, k) * other.get(k, j);
                    }
                    out.set(i, j, s);
                }
            }
            return out;
        }


but using this is very wierd:
mat.mult(2, other_mat); // 2 is the number of cols of other_mat

tropic flint
#

you can set other's type to anytype and check if the cols are equal to other's rows with an if

fluid pier
fluid pier
#

but then again, i dynamically get those matrix slices

#

actuallly no i think i might be able to do it at comptime itself

fluid pier
#

yeah nvm i cant

#

ugh

fluid pier
#

nah nvm i think i can

fluid pier
#

woo now it takes almost 3.5 seconds, idk if my cpu can do better though
Heres the code anyway:

   pub fn mul(self: *const Self, other: anytype, dst: anytype) !void {
            std.debug.assert(other.rows == self.cols);
            std.debug.assert(dst.rows == self.rows);
            std.debug.assert(dst.cols == other.cols);
    
        const V = @Vector(n,T);
        for(0..other.cols) |i| {
        var c = [_]T{0}**(other.rows); 
                other.getColDst(i,&c);
        const col_vec = @as(V,c);
        for(0..self.rows) |j| {
            const r = self.getRow(j)[0..self.rows].*;
            const row_vec = @as(V,r);
            dst.set(i,j,@reduce(.Add,row_vec*col_vec));
        }
        }
        
        }

#

sorry for bad formatting lol

#

whats nice is that i dont have to any heap allocations anymore

#

oo i kinda see the problem already

#

it means i cannot have different types of matrices in an array

#

damn it