add supports broadcasting
This commit is contained in:
parent
bcf9926076
commit
cf19ece7a0
38
matrix.c
38
matrix.c
@ -68,16 +68,44 @@ 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) {
|
||||||
{
|
|
||||||
Matrix matrix;
|
Matrix matrix;
|
||||||
if(matrix1.cols != matrix2.cols || matrix1.rows != matrix2.rows)
|
if(matrix1.rows != matrix2.rows )
|
||||||
{
|
{
|
||||||
matrix.buffer=NULL;
|
matrix.buffer=NULL;
|
||||||
matrix.cols = 0;
|
matrix.cols = 0;
|
||||||
matrix.rows = 0;
|
matrix.rows = 0;
|
||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (matrix1.cols < matrix2.cols) {
|
||||||
|
matrix.cols = matrix2.cols;
|
||||||
|
matrix.rows = matrix2.rows;
|
||||||
|
matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * matrix.rows * matrix.cols);
|
||||||
|
for (int i = 0; i < matrix.rows; i++) {
|
||||||
|
for (int j = 0; j < matrix.cols; j++){
|
||||||
|
matrix.buffer[i * matrix.cols + j] = matrix2.buffer[i * matrix.cols + j]+matrix1.buffer[i*matrix1.cols];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
if (matrix1.cols > matrix2.cols)
|
||||||
|
{
|
||||||
|
matrix.cols = matrix1.cols;
|
||||||
|
matrix.rows = matrix1.rows;
|
||||||
|
matrix.buffer = (MatrixType*)malloc(sizeof(MatrixType) * matrix.rows * matrix.cols);
|
||||||
|
for (int i = 0; i < matrix.rows; i++) {
|
||||||
|
for (int j = 0; j < matrix.cols; j++){
|
||||||
|
matrix.buffer[i * matrix.cols + j] = matrix1.buffer[i * matrix.cols + j]+matrix2.buffer[i*matrix2.cols];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -86,13 +114,14 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType));
|
matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType));
|
||||||
for (int i = 0; i < matrix1.rows; i++) {
|
for (int i = 0; i < matrix1.rows; i++) {
|
||||||
for (int j = 0; j < matrix1.cols; j++) {
|
for (int j = 0; j < matrix1.cols; j++) {
|
||||||
(matrix.buffer[i*matrix.cols+j])= (matrix1.buffer[i*matrix.cols+j]) + (matrix2.buffer[i*matrix.cols+j]);
|
(matrix.buffer[i*matrix.cols+j])= (matrix1.buffer[i*matrix.cols+j]) + (matrix2.buffer[i*matrix2.cols+j]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
Matrix matrix;
|
Matrix matrix;
|
||||||
@ -122,4 +151,5 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
//Siehe I1-Pr Aufgabe
|
//Siehe I1-Pr Aufgabe
|
||||||
|
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user