Commit 82640837 authored by Shawn Nithyan Stanley's avatar Shawn Nithyan Stanley
Browse files

Replace matrix.c

parent 845fa5f3
......@@ -52,6 +52,11 @@ matrix copy_matrix(matrix m)
{
matrix c = make_matrix(m.rows, m.cols);
// TODO: 1.1 - Fill in the new matrix
for (int i= 0; i < m.rows; i++) {
for (int j = 0; j < m.cols; j++) {
c.data[i * m.cols + j] = m.data[i * m.cols + j];
}
}
return c;
}
......@@ -61,7 +66,12 @@ matrix copy_matrix(matrix m)
matrix transpose_matrix(matrix m)
{
// TODO: 1.2 - Make a matrix the correct size, fill it in
matrix t = make_matrix(1,1);
matrix t = make_matrix(m.cols, m.rows);
for (int i= 0; i < m.rows; i++) {
for (int j = 0; j < m.cols; j++) {
t.data[j * m.rows + i] = m.data[i * m.cols + j];
}
}
return t;
}
......@@ -74,6 +84,13 @@ void axpy_matrix(float a, matrix x, matrix y)
assert(x.cols == y.cols);
assert(x.rows == y.rows);
// TODO: 1.3 - Perform the weighted sum, store result back in y
matrix x_copy = copy_matrix(x);
scal_matrix(a, x_copy);
for (int i = 0; i < x_copy.rows; i++) {
for (int j = 0; j < x_copy.cols; j++) {
y.data[i * x_copy.cols + j] += x_copy.data[i * x_copy.cols + j];
}
}
}
// Perform matrix multiplication a*b, return result
......@@ -84,7 +101,13 @@ matrix matmul(matrix a, matrix b)
assert(a.cols == b.rows);
matrix c = make_matrix(a.rows, b.cols);
// TODO: 1.4 - Implement matrix multiplication. Make sure it's fast!
for (int i = 0; i < a.rows; i++) {
for (int k = 0; k < b.rows; k++) {
for (int j = 0; j < b.cols; j++) {
c.data[i * b.cols + j] += a.data[i * a.cols + k] * b.data[j + b.cols * k];
}
}
}
return c;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment