Compare commits
10
Commits
set_matrix
...
add_matrix
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f3b964f17e | ||
|
|
782bcdc350 | ||
|
|
a1c582fcff | ||
|
|
bf27920a61 | ||
|
|
6885e104eb | ||
|
|
d15f3d1b9c | ||
|
|
84fb7219fb | ||
|
|
e2dbb0952f | ||
|
|
3a72794684 | ||
|
|
f0bc0ce06a |
@@ -6,12 +6,34 @@
|
|||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
|
Matrix m;
|
||||||
|
m.rows = rows;
|
||||||
|
m.cols = cols;
|
||||||
|
m.data = NULL;
|
||||||
|
|
||||||
|
if(rows == 0 || cols == 0)
|
||||||
|
{
|
||||||
|
printf("Error");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
m.data = calloc(rows * cols, sizeof(MatrixType));
|
||||||
|
if (m.data == NULL)
|
||||||
|
{
|
||||||
|
printf("Error");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
|
free(matrix->data);
|
||||||
|
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
|
matrix->data = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
@@ -22,11 +44,112 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
|
|||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
return matrix.data[(rowIdx * matrix.cols) + colIdx];
|
||||||
|
}
|
||||||
|
|
||||||
|
static int can_add(Matrix matrix1, Matrix matrix2) {
|
||||||
|
|
||||||
|
int can_add;
|
||||||
|
|
||||||
|
if(matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
|
||||||
|
|
||||||
|
can_add = 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(matrix1.cols == 1 && matrix1.rows == matrix2.rows){
|
||||||
|
|
||||||
|
can_add = 2;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(matrix2.cols == 1 && matrix1.rows == matrix2.rows){
|
||||||
|
|
||||||
|
can_add = 3;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(matrix1.rows == 1 && matrix1.cols == matrix2.cols){
|
||||||
|
|
||||||
|
can_add = 4;
|
||||||
|
|
||||||
|
}
|
||||||
|
else if(matrix2.rows == 1 && matrix1.cols == matrix2.cols){
|
||||||
|
|
||||||
|
can_add = 5;
|
||||||
|
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
|
||||||
|
can_add = 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
return can_add;
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix add(const Matrix matrix1, const Matrix matrix2) //wir addieren nur wenn beide Matrizen gleich groß sind oder eine von beiden eine Zeile oder eine Spalte besitzt
|
||||||
{
|
{
|
||||||
|
int ok = can_add(matrix1,matrix2);
|
||||||
|
|
||||||
|
unsigned int erg_rows = (matrix1.rows == matrix2.rows) ? matrix1.rows : (matrix1.rows == 1 ? matrix2.rows : matrix1.rows);
|
||||||
|
unsigned int erg_cols = (matrix1.cols == matrix2.cols) ? matrix1.cols : (matrix1.cols == 1 ? matrix2.cols : matrix1.cols);
|
||||||
|
|
||||||
|
Matrix matrix_erg = createMatrix(erg_rows, erg_cols);
|
||||||
|
|
||||||
|
switch(ok)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
|
||||||
|
for(int i = 0; i < (matrix1.rows * matrix1.cols); i++)
|
||||||
|
|
||||||
|
matrix_erg.data[i] = matrix1.data[i] + matrix2.data[i];
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
|
||||||
|
for(int i = 0; i < erg_rows; i++){
|
||||||
|
for(int j = 0; j < erg_cols; j++)
|
||||||
|
|
||||||
|
matrix_erg.data[i * erg_cols + j] = matrix1.data[i * erg_cols + j] + matrix2.data[i];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 3:
|
||||||
|
|
||||||
|
for(int i = 0; i < erg_rows; i++){
|
||||||
|
for(int j = 0; j < erg_cols; j++)
|
||||||
|
|
||||||
|
matrix_erg.data[i * erg_cols + j] = matrix1.data[i] + matrix2.data[i * erg_cols + j];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
|
||||||
|
for(int i = 0; i < erg_rows; i++){
|
||||||
|
for(int j = 0; j < erg_cols; j++)
|
||||||
|
|
||||||
|
matrix_erg.data[i * erg_cols + j] = matrix1.data[i * erg_cols + j] + matrix2.data[j];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 5:
|
||||||
|
|
||||||
|
for(int i = 0; i < erg_rows; i++){
|
||||||
|
for(int j = 0; j < erg_cols; j++)
|
||||||
|
|
||||||
|
matrix_erg.data[i * erg_cols + j] = matrix1.data[j] + matrix2.data[i * erg_cols + j];
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
printf("Matrix addition not possible\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix_erg;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user