Author SHA1 Message Date
engelhardtda105083 ae2582cfbc Merge pull request 'Maxi matrix' (#1) from maxi-matrix into main
Reviewed-on: #1
2026-11-10 11:08:39 +00:00
thumannma105082 7f980922d0 maxi_matrix.c_fertiggestellt 2026-11-10 11:46:56 +01:00
thumannma105082 f807f8cc1a maxi_matirx.h fertiggestellt 2026-11-09 09:17:52 +01:00
2 changed files with 120 additions and 5 deletions

No files matched your search

+114 -3
View File
@@ -2,34 +2,145 @@
#include <string.h> #include <string.h>
#include "matrix.h" #include "matrix.h"
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
// ungueltiges ergebniss definieren
Matrix matrix = {NULL, 0, 0};
// fehlerfall wenn zeile oder spaltenanzahl = 0
if (rows == 0 || cols == 0)
{
return matrix;
}
// anfangsadresse fuer speicher ermittlen
matrix.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType)); // malloc liefert freie adresse oder NULL wenn kein speicher frei
// wenn speicher frei -> zeilen und spalten weitergeben
if (matrix.buffer != NULL)
{
matrix.rows = rows;
matrix.cols = cols;
}
return matrix;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
// pruefen ob gueltige adresse übergeben wurde
if (matrix != NULL)
{
free(matrix->buffer); // speicher freigeben (*matrix).buffer = matrix->buffer
matrix->buffer = NULL; // zeiger auf NULL setzen
matrix->rows = 0; // zeilen und spalten auf 0 setzen (sonst absturz in neuralNetwork.c z. 45)
matrix->cols = 0;
}
} }
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (rowIdx < matrix.rows && colIdx < matrix.cols)
{
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
}
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (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 add(const Matrix matrix1, const Matrix matrix2)
{ {
// laut matrixTests muessen 3 versch faelle funktionieren:
// 1. beide matrizen gleich groß
// 2. matrix1 ist spaltenvektor (durch broadcasting wird spaltenvektor verbreitert durch kopieren der spalte)
// 3. matirx2 ist spaltenvektor ''
// ungueltiges ergebnis definieren
Matrix result = {NULL, 0, 0};
// 1. beide matrizen gleich groß
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols)
{
result = createMatrix(matrix1.rows, matrix1.cols);
for (unsigned int i = 0; i < matrix1.rows; i++)
{
for (unsigned int j = 0; j < matrix1.cols; j++)
{
MatrixType summe = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
setMatrixAt(summe, result, i, j);
}
}
}
// 2. matrix1 ist spaltenvektor
else if (matrix1.rows == matrix2.rows && matrix1.cols == 1)
{
result = createMatrix(matrix2.rows, matrix2.cols);
for (unsigned int i = 0; i < matrix2.rows; i++)
{
for (unsigned int j = 0; j < matrix2.cols; j++)
{
MatrixType summe = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j);
setMatrixAt(summe, result, i, j);
}
}
}
// 3. matirx2 ist spaltenvektor
else if (matrix1.rows == matrix2.rows && matrix2.cols == 1)
{
result = createMatrix(matrix1.rows, matrix1.cols);
for (unsigned int i = 0; i < matrix1.rows; i++)
{
for (unsigned int j = 0; j < matrix1.cols; j++)
{
MatrixType summe = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0);
setMatrixAt(summe, result, i, j);
}
}
}
return result;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result = {NULL, 0, 0};
// #spalten matrix1 muss = #zeilen matirx2 sein
if (matrix1.cols != matrix2.rows)
{
return result;
}
result = createMatrix(matrix1.rows, matrix2.cols);
for (unsigned int i = 0; i < matrix1.rows; i++) // je unsigned weil rows und cols unsigned
{
for (unsigned int j = 0; j < matrix2.cols; j++)
{
MatrixType summe = 0;
for (unsigned int k = 0; k < matrix1.cols; k++)
{
summe += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
}
setMatrixAt(summe, result, i, j);
}
}
return result;
} }
+6 -2
View File
@@ -5,8 +5,12 @@
typedef float MatrixType; typedef float MatrixType;
// TODO Matrixtyp definieren typedef struct
{
MatrixType *buffer; // zeiger auf inhalt
unsigned int rows; // anzahl zeilen
unsigned int cols; // anzahl spalten
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols); Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix); void clearMatrix(Matrix *matrix);