Refractor Matrix add

This commit is contained in:
D2A62006 2025-11-26 18:04:17 +01:00
parent 57bf46bfc5
commit 04d9b35c36

View File

@ -101,6 +101,7 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
else return 0;
}
/*
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
//check if the matrices are able to be added (same size)
@ -126,7 +127,36 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
}
}
*/
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
//check if the matrices are able to be added (same size)
if (matrix1.cols != matrix2.cols && matrix1.rows != matrix2.rows){
Matrix m = {NULL, 0, 0};
return m;
}
//size of the matrices should be the same, if the addition is supposed to happen
// Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
if(!outputMatrix.buffer){
Matrix m = {NULL, 0, 0};
return m;
}
for (int i = 0; i < matrix1.rows;i++) {
for (int j = 0; j < matrix1.cols; j++) {
// how this should work in normal Matrix version:
// outputmatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j];
outputMatrix.buffer[i * outputMatrix.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
}
}
return outputMatrix;
}
/*
Matrix multiply(const Matrix matrix1, const Matrix matrix2)