forked from freudenreichan/info2Praktikum-NeuronalesNetz
matrix.c weiter
This commit is contained in:
1 parent
ddea2d5d72
commit
85369b3bad
2 files changed
+38
-2
No files matched your search
+37
-2
@@ -63,8 +63,43 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
|||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
if(matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) {
|
||||||
}
|
Matrix resultMatrix = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows*matrix1.cols; i++) {
|
||||||
|
resultMatrix.buffer[i]=matrix1.buffer[i]+matrix2.buffer[i];
|
||||||
|
}
|
||||||
|
return resultMatrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
else if (matrix1.rows == matrix2.rows && (matrix1.cols == 1 || matrix2.cols == 1)) {
|
||||||
|
Matrix resultMatrix;
|
||||||
|
|
||||||
|
if (matrix2.cols == 1) {
|
||||||
|
resultMatrix = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
for (unsigned int i = 0; i < matrix1.rows*matrix1.cols; i++) {
|
||||||
|
unsigned int rowIdx = i / matrix1.cols;
|
||||||
|
resultMatrix.buffer[i] = matrix1.buffer[i] + matrix2.buffer[rowIdx];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
resultMatrix = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
|
for (unsigned int i = 0; i < matrix2.rows*matrix2.cols; i++) {
|
||||||
|
unsigned int rowIdx = i / matrix2.cols;
|
||||||
|
resultMatrix.buffer[i] = matrix2.buffer[i] + matrix1.buffer[rowIdx];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resultMatrix;
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Matrix result;
|
||||||
|
|
||||||
|
result.rows = 0;
|
||||||
|
result.cols = 0;
|
||||||
|
result.buffer = NULL;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -123,6 +123,7 @@ 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);
|
||||||
|
|
||||||
TEST_ASSERT_NULL(result.buffer);
|
TEST_ASSERT_NULL(result.buffer);
|
||||||
|
|||||||
Reference in new issue
Block a user