#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "matrix.h"

/* Implementierung der Matrix-Funktionen */

Matrix createMatrix(unsigned int rows, unsigned int cols)
{
    Matrix m = {NULL,0,0};
    if (rows > 0 && cols > 0) {
        m.rows = rows;
        m.cols = cols;
        /* calloc initialisiert den Speicher mit 0 */
        m.buffer = (MatrixType*)calloc(rows * cols, sizeof(MatrixType));
    }
        return m;

}

void clearMatrix(Matrix *matrix)
{
    if (matrix != NULL) {
        if (matrix->buffer != NULL) {
            free(matrix->buffer);
            matrix->buffer = NULL;
        }
        matrix->rows = 0;
        matrix->cols = 0;
    }
}

void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
    if (matrix.buffer != NULL && rowIdx < matrix.rows && colIdx < matrix.cols) {
        matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
    }
}
/* um das eindimensionale arry umzurechenen rechnet um wo splaten und zeile ist */
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
    if (matrix.buffer != NULL && rowIdx < matrix.rows && colIdx < matrix.cols) {
        return matrix.buffer[rowIdx * matrix.cols + colIdx];
    }
    return UNDEFINED_MATRIX_VALUE;
}

Matrix add(const Matrix matrix1, const Matrix matrix2)
{
    Matrix result;
    unsigned int r, c;

    /* Fall 1: Normale Addition (Dimensionen identisch) */
    if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) {
        result = createMatrix(matrix1.rows, matrix1.cols);
        if (result.buffer != NULL) {
            unsigned int i;
            for (i = 0; i < matrix1.rows * matrix1.cols; i++) {
                result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
            }
        }
        return result;
    }
    /* Fall 2: Broadcasting (Matrix + Spaltenvektor) */
    else if (matrix1.rows == matrix2.rows && matrix2.cols == 1) {
        result = createMatrix(matrix1.rows, matrix1.cols);
        if (result.buffer != NULL) {
            for (r = 0; r < matrix1.rows; r++) {
                for (c = 0; c < matrix1.cols; c++) {
                    MatrixType sum = getMatrixAt(matrix1, r, c) + getMatrixAt(matrix2, r, 0);
                    setMatrixAt(sum, result, r, c);
                }
            }
        }
        return result;
    }
    /* Fall 3: Broadcasting (Spaltenvektor + Matrix) */
    else if (matrix2.rows == matrix1.rows && matrix1.cols == 1) {
        result = createMatrix(matrix2.rows, matrix2.cols);
        if (result.buffer != NULL) {
            for (r = 0; r < matrix2.rows; r++) {
                for (c = 0; c < matrix2.cols; c++) {
                    MatrixType sum = getMatrixAt(matrix1, r, 0) + getMatrixAt(matrix2, r, c);
                    setMatrixAt(sum, result, r, c);
                }
            }
        }
        return result;
    }

    /* Fehlerfall: Leere Matrix zurückgeben */
    return createMatrix(0, 0);
}

Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
    Matrix result;
    unsigned int r, c, k;

    /* Prüfen, ob Multiplikation erlaubt ist (Spalten von A == Zeilen von B) */
    if (matrix1.cols != matrix2.rows) {
        return createMatrix(0, 0);
    }

    result = createMatrix(matrix1.rows, matrix2.cols);

    if (result.buffer != NULL) {
        for (r = 0; r < result.rows; r++) {
            for (c = 0; c < result.cols; c++) {
                MatrixType sum = 0;
                /* Skalarprodukt berechnen */
                for (k = 0; k < matrix1.cols; k++) {
                    sum += getMatrixAt(matrix1, r, k) * getMatrixAt(matrix2, k, c);
                }
                setMatrixAt(sum, result, r, c);
            }
        }
    }
    return result;
}