kleinleinta105608 committed 2026-11-19 02:30:36 +01:00
commit 698079fc66
2 files changed
+106 -12

No files matched your search

+100 -8
View File
@@ -2,34 +2,126 @@
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix matrix = {0, 0, NULL};
if (rows == 0 || cols == 0) {
return matrix;
}
matrix.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
if (matrix.buffer != NULL) {
matrix.rows = rows;
matrix.cols = cols;
}
return matrix;
}
void clearMatrix(Matrix *matrix)
{
if (matrix != 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;
}
}
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 = {0, 0, NULL};
if (matrix1.buffer == NULL || matrix2.buffer == NULL) {
return result;
}
// Elementweise Addition
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) {
result = createMatrix(matrix1.rows, matrix1.cols);
if (result.buffer == NULL) {
return result;
}
for (unsigned int r = 0; r < result.rows; r++) {
for (unsigned int c = 0; c < result.cols; c++) {
MatrixType sum = getMatrixAt(matrix1, r, c) + getMatrixAt(matrix2, r, c);
setMatrixAt(sum, result, r, c);
}
}
}
// Broadcasting: matrix2 ist ein Spaltenvektor
else if (matrix1.rows == matrix2.rows && matrix2.cols == 1) {
result = createMatrix(matrix1.rows, matrix1.cols);
if (result.buffer == NULL) {
return result;
}
for (unsigned int r = 0; r < result.rows; r++) {
MatrixType vecVal = getMatrixAt(matrix2, r, 0);
for (unsigned int c = 0; c < result.cols; c++) {
MatrixType sum = getMatrixAt(matrix1, r, c) + vecVal;
setMatrixAt(sum, result, r, c);
}
}
}
// Broadcasting: matrix1 ist ein Spaltenvektor
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1) {
result = createMatrix(matrix2.rows, matrix2.cols);
if (result.buffer == NULL) {
return result;
}
for (unsigned int r = 0; r < result.rows; r++) {
MatrixType vecVal = getMatrixAt(matrix1, r, 0);
for (unsigned int c = 0; c < result.cols; c++) {
MatrixType sum = vecVal + getMatrixAt(matrix2, r, c);
setMatrixAt(sum, result, r, c);
}
}
}
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
Matrix result = {0, 0, NULL};
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows) {
return result;
}
result = createMatrix(matrix1.rows, matrix2.cols);
if (result.buffer == NULL) {
return result;
}
for (unsigned int r = 0; r < result.rows; r++) {
for (unsigned int c = 0; c < result.cols; c++) {
MatrixType sum = 0;
for (unsigned int k = 0; k < matrix1.cols; k++) {
sum += getMatrixAt(matrix1, r, k) * getMatrixAt(matrix2, k, c);
}
setMatrixAt(sum, result, r, c);
}
}
return result;
}
+6 -4
View File
@@ -5,8 +5,11 @@
typedef float MatrixType;
// TODO Matrixtyp definieren
typedef struct {
unsigned int rows;
unsigned int cols;
MatrixType *buffer;
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix);
@@ -15,5 +18,4 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
Matrix add(const Matrix matrix1, const Matrix matrix2);
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
#endif
#endif