/*
 * These functions are set for the assignment and cannot be changed.
 */

#include <string.h>
#include <stddef.h>
#include <stdlib.h>

// We include the header file for the definition of the Matrix struct.
#include "matrix_lib.h"

// Free the memory of a matrix.
void freeMatrix(Matrix m) {
    free(m.data);
    free(m.rows);
}

// Print a matrix
void printMatrix(Matrix m) {
    for (size_t row = 0; row < m.height; ++row) {
        for (size_t column = 0; column < m.width; ++column) {
            printf("%.3f", m.rows[row][column]);
            // Print a comma, except print a new line on the last value.
            if (column < m.width-1) {
                printf(", ");
            }
            else {
                printf("\n");
            }
        }
    }
}

// Add two matrices.
Matrix addMatrix(Matrix left, Matrix right) {
    // If the sizes don't match, return a matrix with NULL data and 0 size.
    Matrix sum = {.data = NULL, .rows = NULL, .width = 0, .height = 0};
    if (left.height != right.height || left.width != right.width) {
        return sum;
    }
    sum = newMatrix(left.height, left.width);
    memcpy(sum.data, left.data, sizeof(float)*left.height*left.width);
    for (size_t i = 0; i < sum.height * sum.width; ++i) {
        sum.data[i] += right.data[i];
    }
    return sum;
}
