Compare commits
2 Commits
f3b964f17e
...
5b32e1475c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5b32e1475c | ||
|
|
c96311347e |
42
matrix.c
42
matrix.c
@ -1,5 +1,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
@ -152,8 +153,49 @@ return matrix_erg;
|
|||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
static int can_multiply (Matrix matrix1, Matrix matrix2)
|
||||||
|
{
|
||||||
|
int can_multiply = 0;
|
||||||
|
|
||||||
|
if(matrix1.cols == matrix2.rows)
|
||||||
|
can_multiply = 1;
|
||||||
|
|
||||||
|
return can_multiply;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
|
int ok = can_multiply(matrix1,matrix2);
|
||||||
|
unsigned int erg_rows = matrix1.rows;
|
||||||
|
unsigned int erg_cols = matrix2.cols;
|
||||||
|
Matrix matrix_erg = createMatrix(erg_rows, erg_cols);
|
||||||
|
|
||||||
|
|
||||||
|
if (ok == 1)
|
||||||
|
{
|
||||||
|
|
||||||
|
for (int i = 0; i < erg_rows; i++)
|
||||||
|
{
|
||||||
|
for (int j = 0; j < erg_cols; j++)
|
||||||
|
{
|
||||||
|
MatrixType sum = 0;
|
||||||
|
|
||||||
|
for (int k = 0; k < matrix1.cols; k++)
|
||||||
|
{
|
||||||
|
sum += matrix1.data[i * matrix1.cols + k] * matrix2.data[k * matrix2.cols + j];
|
||||||
|
}
|
||||||
|
|
||||||
|
matrix_erg.data [i * erg_cols + j] = sum;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return matrix_erg;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Matrix multiplication not possible\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user