Compare commits
9
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
689ae7bd56 | ||
|
|
b411ffa6c1 | ||
|
|
c1086b6be5 | ||
|
|
542d94eac8 | ||
|
|
d5466ae1fd | ||
|
|
b7de923066 | ||
|
|
09ada4e4d0 | ||
|
|
4aee809756 | ||
|
|
b661866a16 |
@@ -8,6 +8,18 @@
|
|||||||
|
|
||||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
||||||
|
|
||||||
|
static int checkDimensions(GrayScaleImage imageData){
|
||||||
|
|
||||||
|
int ok = 0;
|
||||||
|
|
||||||
|
if(imageData.width == 10 && imageData.height == 10 && imageData.buffer <= BUFFER_SIZE)
|
||||||
|
return ok = 1;
|
||||||
|
|
||||||
|
else
|
||||||
|
return ok = 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,200 +2,264 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
|
enum addModes{SAMEDIMENSIONS, COLVEC, ROWVEC};
|
||||||
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||||
{
|
{
|
||||||
Matrix m;
|
Matrix m;
|
||||||
m.rows = rows;
|
m.rows = rows;
|
||||||
m.cols = cols;
|
m.cols = cols;
|
||||||
m.data = NULL;
|
m.buffer = NULL;
|
||||||
|
|
||||||
if(rows == 0 || cols == 0)
|
if(rows == 0 || cols == 0)
|
||||||
{
|
{
|
||||||
printf("Error");
|
m.rows = 0;
|
||||||
exit(EXIT_FAILURE);
|
m.cols = 0;
|
||||||
|
m.buffer = NULL;
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
m.data = calloc(rows * cols, sizeof(MatrixType));
|
m.buffer = calloc(rows * cols, sizeof(MatrixType));
|
||||||
if (m.data == NULL)
|
|
||||||
|
if (m.buffer == NULL)
|
||||||
{
|
{
|
||||||
printf("Error");
|
m.rows = 0;
|
||||||
exit(EXIT_FAILURE);
|
m.cols = 0;
|
||||||
|
m.buffer = NULL;
|
||||||
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void clearMatrix(Matrix *matrix)
|
void clearMatrix(Matrix *matrix)
|
||||||
{
|
{
|
||||||
free(matrix->data);
|
|
||||||
|
if (matrix->buffer == NULL)
|
||||||
matrix->rows = 0;
|
{
|
||||||
matrix->cols = 0;
|
matrix->rows = 0;
|
||||||
matrix->data = NULL;
|
matrix->cols = 0;
|
||||||
|
matrix->buffer = NULL;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
free(matrix->buffer);
|
||||||
|
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
|
matrix->buffer = 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)
|
||||||
{
|
{
|
||||||
|
if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||||
matrix.data[(rowIdx * matrix.cols) + colIdx] = value; //wir setzen den data-Wert an der Stelle (rowIdx*Spalten + colIdx) auf den Wert von value
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
matrix.buffer[(rowIdx * matrix.cols) + colIdx] = value; //wir setzen den data-Wert an der Stelle (rowIdx*Spalten + colIdx) auf den Wert von value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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];
|
if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||||
|
{
|
||||||
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix.buffer[(rowIdx * matrix.cols) + colIdx];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int get_add_mode(Matrix matrix1, Matrix matrix2)
|
||||||
|
{
|
||||||
|
int get_add_mode = -1;
|
||||||
|
|
||||||
|
if(matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows)
|
||||||
|
{
|
||||||
|
get_add_mode = SAMEDIMENSIONS;
|
||||||
|
}
|
||||||
|
else if(matrix1.cols == 1 && matrix1.rows == matrix2.rows)
|
||||||
|
{
|
||||||
|
get_add_mode = COLVEC;
|
||||||
|
}
|
||||||
|
else if(matrix2.cols == 1 && matrix1.rows == matrix2.rows)
|
||||||
|
{
|
||||||
|
get_add_mode = COLVEC;
|
||||||
|
}
|
||||||
|
else if(matrix1.rows == 1 && matrix1.cols == matrix2.cols)
|
||||||
|
{
|
||||||
|
get_add_mode = ROWVEC;
|
||||||
|
}
|
||||||
|
else if(matrix2.rows == 1 && matrix1.cols == matrix2.cols)
|
||||||
|
{
|
||||||
|
get_add_mode = ROWVEC;
|
||||||
|
}
|
||||||
|
|
||||||
|
return get_add_mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Matrix addSameDim(Matrix matrix1, Matrix matrix2)
|
||||||
|
{
|
||||||
|
Matrix matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for(int i = 0; i < (matrix1.rows * matrix1.cols); i++)
|
||||||
|
{
|
||||||
|
matrix_erg.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix_erg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Matrix addColVec(Matrix matrix1, Matrix matrix2)
|
||||||
|
{
|
||||||
|
Matrix matrix_erg;
|
||||||
|
|
||||||
|
if(matrix1.cols == 1)
|
||||||
|
{
|
||||||
|
matrix_erg = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
|
|
||||||
|
for(int i = 0; i < matrix2.rows; i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < matrix2.cols; j++)
|
||||||
|
{
|
||||||
|
matrix_erg.buffer[i * matrix2.cols + j] = matrix1.buffer[i] + matrix2.buffer[i * matrix2.cols + j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(matrix2.cols == 1)
|
||||||
|
{
|
||||||
|
matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for(int i = 0; i < matrix1.rows; i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < matrix1.cols; j++)
|
||||||
|
{
|
||||||
|
matrix_erg.buffer[i * matrix1.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix_erg;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Matrix addRowVec(Matrix matrix1, Matrix matrix2)
|
||||||
|
{
|
||||||
|
Matrix matrix_erg;
|
||||||
|
|
||||||
|
if(matrix1.rows == 1)
|
||||||
|
{
|
||||||
|
matrix_erg = createMatrix(matrix2.rows, matrix2.cols);
|
||||||
|
|
||||||
|
for(int i = 0; i < matrix2.rows; i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < matrix2.cols; j++)
|
||||||
|
{
|
||||||
|
matrix_erg.buffer[i * matrix2.cols + j] = matrix1.buffer[j] + matrix2.buffer[i * matrix2.cols + j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(matrix2.rows == 1)
|
||||||
|
{
|
||||||
|
matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
|
||||||
|
|
||||||
|
for(int i = 0; i < matrix1.rows; i++)
|
||||||
|
{
|
||||||
|
for(int j = 0; j < matrix1.cols; j++)
|
||||||
|
{
|
||||||
|
matrix_erg.buffer[i * matrix1.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return matrix_erg;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int can_add(Matrix matrix1, Matrix matrix2) {
|
|
||||||
|
|
||||||
int can_add;
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
int ok = get_add_mode(matrix1,matrix2);
|
||||||
|
Matrix matrix_erg = createMatrix(0, 0);
|
||||||
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)
|
switch(ok)
|
||||||
{
|
{
|
||||||
case 1:
|
case SAMEDIMENSIONS:
|
||||||
|
matrix_erg = addSameDim(matrix1, matrix2);
|
||||||
for(int i = 0; i < (matrix1.rows * matrix1.cols); i++)
|
|
||||||
|
|
||||||
matrix_erg.data[i] = matrix1.data[i] + matrix2.data[i];
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case COLVEC:
|
||||||
|
matrix_erg = addColVec(matrix1, matrix2);
|
||||||
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;
|
break;
|
||||||
|
|
||||||
case 3:
|
case ROWVEC:
|
||||||
|
matrix_erg = addRowVec(matrix1, matrix2);
|
||||||
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;
|
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;
|
return matrix_erg;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static int can_multiply (Matrix matrix1, Matrix matrix2)
|
static int can_multiply (Matrix matrix1, Matrix matrix2)
|
||||||
{
|
{
|
||||||
int can_multiply = 0;
|
int can_multiply = 0;
|
||||||
|
|
||||||
if(matrix1.cols == matrix2.rows)
|
if(matrix1.cols == matrix2.rows)
|
||||||
|
{
|
||||||
can_multiply = 1;
|
can_multiply = 1;
|
||||||
|
}
|
||||||
|
|
||||||
return can_multiply;
|
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);
|
int ok = can_multiply(matrix1,matrix2);
|
||||||
unsigned int erg_rows = matrix1.rows;
|
unsigned int erg_rows = matrix1.rows;
|
||||||
unsigned int erg_cols = matrix2.cols;
|
unsigned int erg_cols = matrix2.cols;
|
||||||
Matrix matrix_erg = createMatrix(erg_rows, erg_cols);
|
Matrix matrix_erg = createMatrix(0, 0);
|
||||||
|
|
||||||
|
|
||||||
if (ok == 1)
|
if (ok == 1)
|
||||||
{
|
{
|
||||||
|
matrix_erg = createMatrix(erg_rows, erg_cols);
|
||||||
|
|
||||||
for (int i = 0; i < erg_rows; i++)
|
for (int i = 0; i < erg_rows; i++)
|
||||||
{
|
{
|
||||||
for (int j = 0; j < erg_cols; j++)
|
for (int j = 0; j < erg_cols; j++)
|
||||||
{
|
{
|
||||||
MatrixType sum = 0;
|
MatrixType sum = 0;
|
||||||
|
|
||||||
for (int k = 0; k < matrix1.cols; k++)
|
for (int k = 0; k < matrix1.cols; k++)
|
||||||
{
|
{
|
||||||
sum += matrix1.data[i * matrix1.cols + k] * matrix2.data[k * matrix2.cols + j];
|
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
||||||
}
|
}
|
||||||
|
|
||||||
matrix_erg.data [i * erg_cols + j] = sum;
|
setMatrixAt(sum, matrix_erg, i, j);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return matrix_erg;
|
return matrix_erg;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("Matrix multiplication not possible\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -11,7 +11,7 @@ typedef struct
|
|||||||
{
|
{
|
||||||
unsigned int rows;
|
unsigned int rows;
|
||||||
unsigned int cols;
|
unsigned int cols;
|
||||||
MatrixType *data;
|
MatrixType *buffer;
|
||||||
}Matrix;
|
}Matrix;
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
|
|||||||
Reference in New Issue
Block a user