mutiplyMatrix implementierung

This commit is contained in:
dehmfl103364 committed 2026-10-26 18:20:24 +01:00
1 parent d31214f974
commit e8eb227d44
2 files changed
+39 -7

No files matched your search

+37 -4
View File
@@ -2,6 +2,8 @@
#include <string.h> #include <string.h>
#include "matrix.h" #include "matrix.h"
#include <stdio.h>
// TODO Matrix-Funktionen implementieren // TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
@@ -38,8 +40,12 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
MatrixType testHelp = 0; if(matrix.rows > rowIdx && matrix.cols > colIdx){
return testHelp; return matrix.buffer[colIdx + rowIdx * matrix.cols];
}else{
return UNDEFINED_MATRIX_VALUE;
}
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
@@ -50,6 +56,33 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix testHelp = {NULL, 0,0}; Matrix resultMatrix = createMatrix(matrix1.rows,matrix2.cols);
return testHelp;
if(matrix1.cols == matrix2.rows){
unsigned int commonDimension = matrix1.cols;
for(int i = 0; i < resultMatrix.rows; i++){
for(int j = 0; j < resultMatrix.cols; j++){
MatrixType entry = 0;
for(int k = 0; k < commonDimension; k++){
entry += (matrix1.buffer[k + i * matrix1.cols] * matrix2.buffer[j + k * matrix2.cols]);
}
resultMatrix.buffer[j + i * resultMatrix.cols] = entry;
}
}
return resultMatrix;
}else{
resultMatrix.buffer = NULL;
resultMatrix.rows = 0;
resultMatrix.cols = 0;
return resultMatrix;
}
} }
+2 -3
View File
@@ -123,10 +123,9 @@ void test_multiplyFailsOnWrongInputDimensions(void)
Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1}; Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1};
Matrix matrix2 = {.rows=2, .cols=3, .buffer=buffer2}; Matrix matrix2 = {.rows=2, .cols=3, .buffer=buffer2};
Matrix result = multiply(matrix1, matrix2); Matrix result = multiply(matrix1, matrix2); // was würde passiern wenn der die Matrix fälschlicherweise doch allociert werden würde und jetzt der Speicher nicht mehr freigegeben wird
TEST_ASSERT_NULL(result.buffer); TEST_ASSERT_NULL(result.buffer);
TEST_ASSERT_EQUAL_UINT32(0, result.rows); TEST_ASSERT_EQUAL_UINT32(0, result.rows);
TEST_ASSERT_EQUAL_UINT32(0, result.cols); TEST_ASSERT_EQUAL_UINT32(0, result.cols);
} }