/// @file /// @brief Task2: function definitions #include "task2.h" // struct Matrix, matrix_mult #include // size_t #include // printf #include // malloc, free /// @todo Include C standard library headers as needed /// @note This implementation is provided as declared and specified in task2.h struct Matrix matrix_init(size_t m, size_t n, const double* data) { struct Matrix res = {.data = malloc(sizeof(double) * m * n), .m = m, .n = n}; double* A = res.data; for (size_t i = 0; i != m * n; ++i) A[i] = data[i]; return res; } /// @note This implementation is provided as declared and specified in task2.h void matrix_print(const struct Matrix* mat) { size_t M = mat->m; size_t N = mat->n; const double* A = mat->data; for (size_t m = 0; m != M; ++m) { for (size_t n = 0; n != N; ++n) printf("%lf ", A[n + N * m]); printf("\n"); } printf("\n"); } /// @note This implementation is provided as declared and specified in task2.h void matrix_clear(struct Matrix* mat) { free(mat->data); mat->m = 0; mat->n = 0; } /// @brief Initializes an matrix with zeros /// @param m first dimension of the matrix /// @param n first dimension of the matrix struct Matrix matrix_zeros(size_t m, size_t n){ double* data = malloc(sizeof(double) * m * n); for (size_t i = 0; i != m * n; ++i) data[i] = 0; return matrix_init(m, n, data); } /// @brief Initializes a square identity matrix /// @param n dimension of the identity matrix struct Matrix matrix_identity(size_t n){ double* data = malloc(sizeof(double) * n * n); for (size_t i = 0; i != n; ++i) for (size_t j = 0; j != n; ++j) data[j + n * i] = i == j ? 1 : 0; return matrix_init(n, n, data); } /// @brief Transposes a matrix /// @param mat Matrix to be transposed /// @note this function might swap/replace the block of memory owned by the matrix void matrix_transpose(struct Matrix* mat){ size_t m = mat->m; size_t n = mat->n; double* data = malloc(sizeof(double) * m * n); for (size_t i = 0; i != m; ++i) for (size_t j = 0; j != n; ++j) data[i + m * j] = mat->data[j + n * i]; free(mat->data); mat->data = data; mat->m = n; mat->n = m; } /// @brief Performs a matrix-matrix mutliplication: a*b = c /// @a first matrix (left factor) /// @b second matrix (right factor) /// @c Result of the multiplication is stored in this Matrix: /// - the dimensions of this matrix must be 'a.m x b.n' when calling this function /// - the values will be overwritten with the result of the multiplication void matrix_mult(const struct Matrix* a, const struct Matrix* b, struct Matrix* c){ size_t m = a->m; size_t n = a->n; size_t p = b->n; double* data = malloc(sizeof(double) * m * p); for (size_t i = 0; i != m; ++i) for (size_t j = 0; j != p; ++j){ double sum = 0; for (size_t k = 0; k != n; ++k) sum += a->data[k + n * i] * b->data[j + p * k]; data[j + p * i] = sum; } free(c->data); c->data = data; c->m = m; c->n = p; }