alles bis uf multiply
This commit is contained in:
parent
35a598a276
commit
4e2ee7078a
59
matrix.c
59
matrix.c
@ -28,9 +28,13 @@ void setMatrixAt(const MatrixType value, Matrix matrix,
|
||||
const unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
||||
const unsigned int colIdx) {
|
||||
|
||||
matrix.buffer[rowIdx * matrix.cols + colIdx] =
|
||||
value; // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
|
||||
// innerhalb der Zeile
|
||||
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) {
|
||||
// Speichergröße nicht überschreiten
|
||||
return;
|
||||
}
|
||||
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
||||
// rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
|
||||
// innerhalb der Zeile
|
||||
}
|
||||
MatrixType getMatrixAt(const Matrix matrix,
|
||||
unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
||||
@ -57,7 +61,7 @@ Matrix broadcastingCols(const Matrix matrix, const unsigned int cols){
|
||||
Matrix broadcastingRows(const Matrix matrix, const unsigned int rows){
|
||||
Matrix copy1 = createMatrix(rows, matrix.cols);
|
||||
for (int c= 0; c < matrix.cols; c++){
|
||||
MatrixType valueMatrix1 = getMatrixAt(matrix, c, 0);
|
||||
MatrixType valueMatrix1 = getMatrixAt(matrix, 0, c);
|
||||
for (int r=0; r < rows; r++){
|
||||
setMatrixAt(valueMatrix1, copy1,r,c);
|
||||
}
|
||||
@ -96,10 +100,30 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
||||
if (cols1==1){ //broadcasting von vektor 1 zu matrix 1, add
|
||||
Matrix newMatrix = broadcastingCols(matrix1, cols2);
|
||||
//add
|
||||
Matrix result = createMatrix(newMatrix.rows, newMatrix.cols);
|
||||
for (int i = 0; i< rows1; i++) {
|
||||
for (int j= 0; j< cols2; j++){
|
||||
int valueM1= getMatrixAt(newMatrix, i, j);
|
||||
int valueM2= getMatrixAt(matrix2, i, j);
|
||||
int sum = valueM1 + valueM2;
|
||||
setMatrixAt(sum, result, i, j);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else{
|
||||
Matrix newMatrix2 = broadcastingCols(matrix1, cols1);
|
||||
Matrix newMatrix2 = broadcastingCols(matrix2, cols1);
|
||||
//add
|
||||
Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols);
|
||||
for (int i = 0; i< rows1; i++) {
|
||||
for (int j= 0; j< cols1; j++){
|
||||
int valueM1= getMatrixAt(matrix1, i, j);
|
||||
int valueM2= getMatrixAt(newMatrix2, i, j);
|
||||
int sum = valueM1 + valueM2;
|
||||
setMatrixAt(sum, result, i, j);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
@ -107,15 +131,36 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
||||
if (rows1==1){
|
||||
Matrix newMatrix = broadcastingRows(matrix1, rows2);
|
||||
//add
|
||||
Matrix result = createMatrix(newMatrix.rows, newMatrix.cols);
|
||||
for (int i = 0; i< rows2; i++) {
|
||||
for (int j= 0; j< cols1; j++){
|
||||
int valueM1= getMatrixAt(newMatrix, i, j);
|
||||
int valueM2= getMatrixAt(matrix2, i, j);
|
||||
int sum = valueM1 + valueM2;
|
||||
setMatrixAt(sum, result, i, j);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
else{
|
||||
Matrix newMatrix2 = broadcastingRows(matrix1, rows1);
|
||||
Matrix newMatrix2 = broadcastingRows(matrix2, rows1);
|
||||
//add
|
||||
Matrix result = createMatrix(newMatrix2.rows, newMatrix2.cols);
|
||||
for (int i = 0; i< rows1; i++) {
|
||||
for (int j= 0; j< cols1; j++){
|
||||
int valueM1= getMatrixAt(matrix1, i, j);
|
||||
int valueM2= getMatrixAt(newMatrix2, i, j);
|
||||
int sum = valueM1 + valueM2;
|
||||
setMatrixAt(sum, result, i, j);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// kein add möglich
|
||||
|
||||
Matrix errorMatrix = {0, 0, NULL};
|
||||
return errorMatrix;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user