bugfixes und Formatierung (in neuem Zweig)
This commit is contained in:
parent
542d94eac8
commit
c1086b6be5
241
matrix.c
241
matrix.c
@ -7,6 +7,7 @@
|
||||
|
||||
enum addModes{SAMEDIMENSIONS, COLVEC, ROWVEC};
|
||||
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
Matrix m;
|
||||
@ -16,7 +17,6 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
|
||||
if(rows == 0 || cols == 0)
|
||||
{
|
||||
|
||||
m.rows = 0;
|
||||
m.cols = 0;
|
||||
m.buffer = NULL;
|
||||
@ -27,7 +27,6 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
|
||||
if (m.buffer == NULL)
|
||||
{
|
||||
|
||||
m.rows = 0;
|
||||
m.cols = 0;
|
||||
m.buffer = NULL;
|
||||
@ -37,8 +36,10 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
|
||||
if (matrix->buffer == NULL)
|
||||
{
|
||||
matrix->rows = 0;
|
||||
@ -49,18 +50,18 @@ void clearMatrix(Matrix *matrix)
|
||||
else
|
||||
{
|
||||
|
||||
free(matrix->buffer);
|
||||
free(matrix->buffer);
|
||||
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
matrix->buffer = NULL;
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
matrix->buffer = NULL;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
|
||||
if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||
{
|
||||
return;
|
||||
@ -71,6 +72,7 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
if(matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||
@ -81,159 +83,150 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
||||
return matrix.buffer[(rowIdx * matrix.cols) + colIdx];
|
||||
}
|
||||
|
||||
static int get_add_mode(Matrix matrix1, Matrix matrix2) {
|
||||
|
||||
int get_add_mode = 0;
|
||||
|
||||
if(matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
|
||||
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){
|
||||
|
||||
else if(matrix1.cols == 1 && matrix1.rows == matrix2.rows)
|
||||
{
|
||||
get_add_mode = COLVEC;
|
||||
|
||||
}
|
||||
else if(matrix2.cols == 1 && matrix1.rows == matrix2.rows){
|
||||
|
||||
else if(matrix2.cols == 1 && matrix1.rows == matrix2.rows)
|
||||
{
|
||||
get_add_mode = COLVEC;
|
||||
|
||||
}
|
||||
else if(matrix1.rows == 1 && matrix1.cols == matrix2.cols){
|
||||
|
||||
else if(matrix1.rows == 1 && matrix1.cols == matrix2.cols)
|
||||
{
|
||||
get_add_mode = ROWVEC;
|
||||
|
||||
}
|
||||
else if(matrix2.rows == 1 && matrix1.cols == matrix2.cols){
|
||||
|
||||
else if(matrix2.rows == 1 && matrix1.cols == matrix2.cols)
|
||||
{
|
||||
get_add_mode = ROWVEC;
|
||||
|
||||
}
|
||||
|
||||
return get_add_mode;
|
||||
|
||||
}
|
||||
|
||||
|
||||
Matrix addSameDim(Matrix matrix1, Matrix matrix2)
|
||||
|
||||
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 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_erg.buffer[i] = matrix1.buffer[i] + matrix2.buffer[i];
|
||||
}
|
||||
|
||||
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.cols + j] + matrix2.buffer[i];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
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] + matrix2.buffer[i * matrix1.cols + j];
|
||||
}
|
||||
}
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Matrix addRowVec(Matrix matrix1, Matrix matrix2)
|
||||
if(matrix2.cols == 1)
|
||||
{
|
||||
Matrix matrix_erg;
|
||||
if(matrix1.rows == 1)
|
||||
matrix_erg = createMatrix(matrix1.rows, matrix1.cols);
|
||||
|
||||
for(int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
matrix_erg = createMatrix(matrix2.rows, matrix2.cols);
|
||||
|
||||
for(int i = 0; i < matrix2.rows; i++)
|
||||
for(int j = 0; j < matrix1.cols; j++)
|
||||
{
|
||||
for(int j = 0; j < matrix2.cols; j++)
|
||||
|
||||
matrix_erg.buffer[i * matrix2.cols + j] = matrix1.buffer[i * matrix2.cols + j] + matrix2.buffer[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[j] + matrix2.buffer[i * matrix1.cols + j];
|
||||
matrix_erg.buffer[i * matrix1.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i];
|
||||
}
|
||||
}
|
||||
return matrix_erg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
int ok = get_add_mode(matrix1,matrix2);
|
||||
|
||||
Matrix matrix_erg = createMatrix(0, 0);
|
||||
|
||||
|
||||
switch(ok)
|
||||
{
|
||||
|
||||
|
||||
case SAMEDIMENSIONS:
|
||||
|
||||
matrix_erg = addSameDim(matrix1, matrix2);
|
||||
|
||||
break;
|
||||
|
||||
case COLVEC:
|
||||
|
||||
matrix_erg = addColVec(matrix1, matrix2);
|
||||
|
||||
break;
|
||||
|
||||
case ROWVEC:
|
||||
|
||||
matrix_erg = addRowVec(matrix1, matrix2);
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
int ok = get_add_mode(matrix1,matrix2);
|
||||
Matrix matrix_erg = createMatrix(0, 0);
|
||||
|
||||
switch(ok)
|
||||
{
|
||||
case SAMEDIMENSIONS:
|
||||
matrix_erg = addSameDim(matrix1, matrix2);
|
||||
break;
|
||||
|
||||
case COLVEC:
|
||||
matrix_erg = addColVec(matrix1, matrix2);
|
||||
break;
|
||||
|
||||
case ROWVEC:
|
||||
matrix_erg = addRowVec(matrix1, matrix2);
|
||||
break;
|
||||
}
|
||||
|
||||
return matrix_erg;
|
||||
}
|
||||
|
||||
|
||||
static int can_multiply (Matrix matrix1, Matrix matrix2)
|
||||
{
|
||||
int can_multiply = 0;
|
||||
|
||||
if(matrix1.cols == matrix2.rows)
|
||||
{
|
||||
can_multiply = 1;
|
||||
}
|
||||
|
||||
return can_multiply;
|
||||
}
|
||||
@ -248,7 +241,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
|
||||
if (ok == 1)
|
||||
{
|
||||
Matrix matrix_erg = createMatrix(erg_rows, erg_cols);
|
||||
matrix_erg = createMatrix(erg_rows, erg_cols);
|
||||
|
||||
for (int i = 0; i < erg_rows; i++)
|
||||
{
|
||||
@ -265,10 +258,8 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return matrix_erg;
|
||||
|
||||
return matrix_erg;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user