223 lines
5.2 KiB
C
223 lines
5.2 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include "matrix.h"
|
|
|
|
// TODO Matrix-Funktionen implementieren
|
|
|
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|
{
|
|
if (rows != 0 && cols != 0)
|
|
{
|
|
Matrix m = {rows, cols};
|
|
|
|
size_t total = (size_t)rows * (size_t)cols;
|
|
|
|
MatrixType *block = calloc(total, sizeof(MatrixType));
|
|
|
|
if (block == NULL)
|
|
{
|
|
printf("Fehler bei der Speicherreservierung!\n");
|
|
|
|
}
|
|
|
|
m.buffer = block;
|
|
return m;
|
|
|
|
}
|
|
|
|
else
|
|
{
|
|
printf("Fehler! Die Anzahl der Reihen und Spalten der Matrix darf nicht null sein!\n");
|
|
|
|
|
|
}
|
|
|
|
void clearMatrix(Matrix *matrix)
|
|
{
|
|
if (matrix != NULL)
|
|
{
|
|
if (matrix->buffer != NULL)
|
|
{
|
|
free(matrix->buffer);
|
|
matrix->buffer = NULL;
|
|
}
|
|
|
|
else
|
|
{
|
|
printf("Die Matrixgröße darf nicht NULL sein!\n");
|
|
|
|
}
|
|
|
|
|
|
matrix->rows = 0;
|
|
matrix->cols = 0;
|
|
}
|
|
|
|
else
|
|
{
|
|
printf("Keine gültige Matrix zum Leeren!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
if (matrix.buffer != NULL)
|
|
{
|
|
if (rowIdx < matrix.rows && colIdx < matrix.cols)
|
|
{
|
|
size_t index = rowIdx * matrix.cols + colIdx;
|
|
|
|
matrix.buffer[index] = value;
|
|
}
|
|
else
|
|
{
|
|
printf("Fehler, Zeile bzw Spalte außerhalb der Matrixdimensionen!\n");
|
|
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
printf("Fehler! Matrix nicht initialisier oder leer\n");
|
|
|
|
}
|
|
}
|
|
|
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
if (matrix.buffer != NULL)
|
|
{
|
|
if (rowIdx < matrix.rows && colIdx < matrix.cols)
|
|
{
|
|
size_t index = rowIdx * matrix.cols + colIdx;
|
|
|
|
MatrixType value = matrix.buffer[index];
|
|
|
|
return value;
|
|
}
|
|
else
|
|
{
|
|
printf("Fehler! Zeile oder Spalte ausserhalb der Matrixdimensionen!\n");
|
|
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
printf("Fehler! Matrix nicht initialisiert oder leer!\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows)
|
|
{
|
|
Matrix matrixAdd = createMatrix(matrix1.rows, matrix1.cols);
|
|
|
|
for (size_t i = 0; i < matrix1.rows; i++)
|
|
{
|
|
for (size_t j = 0; j < matrix1.cols; j++)
|
|
{
|
|
MatrixType value1 = getMatrixAt(matrix1, i, j);
|
|
MatrixType value2 = getMatrixAt(matrix2, i, j);
|
|
|
|
MatrixType sum = value1 + value2;
|
|
|
|
size_t index = i * matrix1.cols + j;
|
|
|
|
matrixAdd.buffer[index] = sum;
|
|
}
|
|
|
|
}
|
|
return matrixAdd;
|
|
}
|
|
|
|
else if (matrix1.rows == matrix2.rows && matrix2.cols == 1)
|
|
{
|
|
Matrix matrixAdd = createMatrix(matrix1.rows, matrix1.cols);
|
|
|
|
for (size_t i = 0; i < matrix1.rows; i++)
|
|
{
|
|
for (size_t j = 0; j < matrix1.cols; j++)
|
|
{
|
|
MatrixType value1 = getMatrixAt(matrix1, i, j);
|
|
MatrixType value2 = getMatrixAt(matrix2, i, 1);
|
|
|
|
MatrixType sum = value1 + value2;
|
|
size_t index = i * matrix1.cols + j;
|
|
|
|
matrixAdd.buffer[index] = sum;
|
|
}
|
|
|
|
}
|
|
return matrixAdd;
|
|
}
|
|
|
|
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
|
|
{
|
|
Matrix matrixAdd = createMatrix(matrix2.rows, matrix2.cols);
|
|
|
|
for (size_t i = 0; i < matrix2.rows; i++)
|
|
{
|
|
for (size_t j = 0; j < matrix2.cols; j++)
|
|
{
|
|
MatrixType value1 = getMatrixAt(matrix1, i, 1);
|
|
MatrixType value2 = getMatrixAt(matrix2, i, j);
|
|
|
|
MatrixType sum = value1 + value2;
|
|
size_t index = i * matrix1.cols + j;
|
|
|
|
matrixAdd.buffer[index] = sum;
|
|
}
|
|
|
|
}
|
|
return matrixAdd;
|
|
}
|
|
|
|
else
|
|
{
|
|
printf("Fehler bei der Matrixaddition! Die Matritzen sind nicht gleich gross!\n");
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
if (matrix1.cols == matrix2.rows)
|
|
{
|
|
Matrix matrixMult = createMatrix(matrix1.rows, matrix2.cols);
|
|
MatrixType sum = 0.0;
|
|
|
|
for (size_t i = 0; i < matrix1.rows; i++)
|
|
{
|
|
for (size_t j = 0; j < matrix2.cols; j++)
|
|
{
|
|
for (size_t k = 0; k < matrix1.cols; k++)
|
|
{
|
|
MatrixType value1 = getMatrixAt(matrix1, i, k);
|
|
MatrixType value2 = getMatrixAt(matrix2, k, j);
|
|
|
|
sum += value1 * value2;
|
|
}
|
|
size_t index = i * matrixMult.cols + j;
|
|
matrixMult.buffer[index] = sum;
|
|
}
|
|
|
|
}
|
|
return matrixMult;
|
|
}
|
|
else
|
|
{
|
|
printf("Fehler bei der Matrixmultiplikation! Die Anzahl an Zeilen der ersten Matrix entspricht nicht der Anzahl an Spalten der zweiten Matrix!\n");
|
|
|
|
}
|
|
|
|
|
|
} |