140 lines
4.1 KiB
C
140 lines
4.1 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "matrix.h"
|
|
|
|
// TODO Matrix-Funktionen implementieren
|
|
|
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|
{
|
|
Matrix m;
|
|
// Default: leere Matrix bei Fehler/Null-Dimensionen
|
|
m.rows = 0;
|
|
m.cols = 0;
|
|
m.buffer = NULL;
|
|
|
|
if (rows == 0 || cols == 0) {
|
|
return m;
|
|
}
|
|
|
|
size_t count = (size_t)rows * (size_t)cols;
|
|
m.buffer = (MatrixType *)calloc(count, sizeof(MatrixType));
|
|
if (!m.buffer) {
|
|
// Allocation failed -> return empty matrix
|
|
return m;
|
|
}
|
|
|
|
// Nur bei erfolgreicher Allokation Dimensionen setzen
|
|
m.rows = rows;
|
|
m.cols = cols;
|
|
return m;
|
|
}
|
|
|
|
void clearMatrix(Matrix *matrix)
|
|
{
|
|
if (!matrix) return;
|
|
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) return;
|
|
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) return;
|
|
size_t idx = (size_t)rowIdx * matrix.cols + colIdx;
|
|
matrix.buffer[idx] = value;
|
|
}
|
|
|
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
if (matrix.buffer == NULL) return (MatrixType)0;
|
|
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) return (MatrixType)0;
|
|
size_t idx = (size_t)rowIdx * matrix.cols + colIdx;
|
|
return matrix.buffer[idx];
|
|
}
|
|
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
Matrix result;
|
|
result.rows = result.cols = 0;
|
|
result.buffer = NULL;
|
|
|
|
/* einfache Validierung */
|
|
if (matrix1.rows == 0 || matrix1.cols == 0 || matrix1.buffer == NULL) return result;
|
|
if (matrix2.rows == 0 || matrix2.cols == 0 || matrix2.buffer == NULL) return result;
|
|
|
|
/* Fall 1: gleiche Dimensionen -> elementweise Addition */
|
|
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols)
|
|
{
|
|
result = createMatrix(matrix1.rows, matrix1.cols);
|
|
if (!result.buffer) return result;
|
|
|
|
size_t count = (size_t)matrix1.rows * matrix1.cols;
|
|
for (size_t i = 0; i < count; ++i)
|
|
result.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
|
|
|
|
return result;
|
|
}
|
|
|
|
/* Fall 2: matrix2 ist Bias (rows x 1) -> broadcast über Spalten von matrix1 */
|
|
if (matrix1.rows == matrix2.rows && matrix2.cols == 1)
|
|
{
|
|
result = createMatrix(matrix1.rows, matrix1.cols);
|
|
if (!result.buffer) return result;
|
|
|
|
for (unsigned int c = 0; c < matrix1.cols; ++c)
|
|
{
|
|
for (unsigned int r = 0; r < matrix1.rows; ++r)
|
|
{
|
|
size_t idx = (size_t)r * matrix1.cols + c;
|
|
result.buffer[idx] = matrix1.buffer[idx] + matrix2.buffer[r];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/* Fall 3: matrix1 ist Bias (rows x 1) -> broadcast über Spalten von matrix2 */
|
|
if (matrix2.rows == matrix1.rows && matrix1.cols == 1)
|
|
{
|
|
result = createMatrix(matrix2.rows, matrix2.cols);
|
|
if (!result.buffer) return result;
|
|
|
|
for (unsigned int c = 0; c < matrix2.cols; ++c)
|
|
{
|
|
for (unsigned int r = 0; r < matrix2.rows; ++r)
|
|
{
|
|
size_t idx = (size_t)r * matrix2.cols + c;
|
|
result.buffer[idx] = matrix2.buffer[idx] + matrix1.buffer[r];
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/* keine kompatible Form -> leere Matrix zurückgeben */
|
|
return result;
|
|
}
|
|
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
Matrix result;
|
|
result.rows = result.cols = 0;
|
|
result.buffer = NULL;
|
|
|
|
if (matrix1.cols != matrix2.rows) return result;
|
|
if (matrix1.rows == 0 || matrix1.cols == 0 || matrix2.cols == 0) return result;
|
|
|
|
result = createMatrix(matrix1.rows, matrix2.cols);
|
|
if (!result.buffer) return result;
|
|
|
|
for (unsigned int i = 0; i < matrix1.rows; ++i) {
|
|
for (unsigned int j = 0; j < matrix2.cols; ++j) {
|
|
MatrixType sum = (MatrixType)0;
|
|
for (unsigned int k = 0; k < matrix1.cols; ++k) {
|
|
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
|
}
|
|
setMatrixAt(sum, result, i, j);
|
|
}
|
|
}
|
|
return result;
|
|
} |