generated from freudenreichan/info2Praktikum-NeuronalesNetz
Fixed UnitTest Errorrs in matrix.c
This commit is contained in:
parent
05a29b50c6
commit
82c335ca89
17
matrix.c
17
matrix.c
@ -81,27 +81,33 @@ void clearMatrix(Matrix *matrix)
|
|||||||
free(matrix->buffer);
|
free(matrix->buffer);
|
||||||
matrix->rows = 0;
|
matrix->rows = 0;
|
||||||
matrix->cols = 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)
|
||||||
{
|
{
|
||||||
matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)] = value;
|
//checks, if the given incies are allowed
|
||||||
|
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) matrix.buffer[rowIdx + matrix.rows*(colIdx)] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
|
//checks, if the given indices are allowed
|
||||||
|
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) {
|
||||||
MatrixType returnVal;
|
MatrixType returnVal;
|
||||||
returnVal = matrix.buffer[rowIdx-1 + matrix.rows*(colIdx-1)];
|
returnVal = matrix.buffer[rowIdx + matrix.rows*(colIdx)];
|
||||||
return returnVal;
|
return returnVal;
|
||||||
}
|
}
|
||||||
|
else return 0;
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
//check if the matrices are able to be added (same size)
|
||||||
if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
|
if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
|
||||||
//size of the matrices should be the same, if the addition is supposed to happen
|
//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);
|
||||||
|
Matrix outputMatrix = createMatrix(matrix1.cols, matrix1.rows);
|
||||||
for (int i = 0; i < matrix1.rows;i++) {
|
for (int i = 0; i < matrix1.rows;i++) {
|
||||||
for (int j = 0; j < matrix1.cols; j++) {
|
for (int j = 0; j < matrix1.cols; j++) {
|
||||||
// how this should work in normal Matrix version:
|
// how this should work in normal Matrix version:
|
||||||
@ -125,7 +131,8 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
{
|
{
|
||||||
//check, if the matrices can be multiplied
|
//check, if the matrices can be multiplied
|
||||||
if (matrix1.rows == matrix2.cols) {
|
if (matrix1.rows == matrix2.cols) {
|
||||||
Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
|
// Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
|
Matrix outputMatrix = createMatrix(matrix2.cols, matrix1.rows);
|
||||||
for(int i = 0; i < matrix1.rows; i++) {
|
for(int i = 0; i < matrix1.rows; i++) {
|
||||||
for (int j = 0; j < matrix2.cols; j++) {
|
for (int j = 0; j < matrix2.cols; j++) {
|
||||||
for (int k = 0; k < matrix2.rows; k++) {
|
for (int k = 0; k < matrix2.rows; k++) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user