generated from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
6
Commits
a2f2be5592
...
devKob
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
15d74d972d | ||
|
|
e7373fe73a | ||
|
|
9bd18d3681 | ||
|
|
04d9b35c36 | ||
|
|
57bf46bfc5 | ||
|
|
2783663ab9 |
No files matched your search
@@ -42,7 +42,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
}
|
||||
*/
|
||||
|
||||
Matrix createMatrix(size_t cols, size_t rows)
|
||||
Matrix createMatrix(size_t rows, size_t cols)
|
||||
{
|
||||
Matrix m;
|
||||
m.rows = rows;
|
||||
@@ -75,7 +75,9 @@ void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
for (int i = 0; i < matrix->rows; i++) {
|
||||
for (int j = 0; j < matrix->cols;j++) {
|
||||
matrix->buffer[i + matrix->rows * j] = UNDEFINED_MATRIX_VALUE;
|
||||
// Normally one would expect to work matrices like this, but it is supposed to be the other way around.
|
||||
// matrix->buffer[i + matrix->rows * j] = UNDEFINED_MATRIX_VALUE;
|
||||
matrix->buffer[j + matrix->cols * i] = UNDEFINED_MATRIX_VALUE;
|
||||
}
|
||||
}
|
||||
free(matrix->buffer);
|
||||
@@ -87,7 +89,11 @@ void clearMatrix(Matrix *matrix)
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
//checks, if the given incies are allowed
|
||||
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) matrix.buffer[rowIdx + matrix.rows*(colIdx)] = value;
|
||||
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) {
|
||||
// Normally one would expect to work matrices like this, but it is supposed to be the other way around.
|
||||
// matrix.buffer[rowIdx + matrix.rows*(colIdx)] = value;
|
||||
matrix.buffer[colIdx + matrix.cols * rowIdx] = value;
|
||||
}
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
@@ -95,19 +101,22 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
|
||||
//checks, if the given indices are allowed
|
||||
if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) {
|
||||
MatrixType returnVal;
|
||||
returnVal = matrix.buffer[rowIdx + matrix.rows*(colIdx)];
|
||||
// Normally one would expect to work matrices like this, but it is supposed to be the other way around.
|
||||
// returnVal = matrix.buffer[rowIdx + matrix.rows*(colIdx)];
|
||||
returnVal = matrix.buffer[colIdx + rowIdx * matrix.cols];
|
||||
return returnVal;
|
||||
}
|
||||
else return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
//check if the matrices are able to be added (same size)
|
||||
if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){
|
||||
//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.cols, matrix1.rows);
|
||||
Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
|
||||
for (int i = 0; i < matrix1.rows;i++) {
|
||||
for (int j = 0; j < matrix1.cols; j++) {
|
||||
// how this should work in normal Matrix version:
|
||||
@@ -126,19 +135,50 @@ 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)
|
||||
if (matrix1.cols != matrix2.cols && matrix1.rows != matrix2.rows){
|
||||
Matrix m = {NULL, 0, 0};
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
//size of the matrices should be the same, if the addition is supposed to happen
|
||||
Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols);
|
||||
|
||||
if(!outputMatrix.buffer){
|
||||
Matrix m = {NULL, 0, 0};
|
||||
return m;
|
||||
}
|
||||
|
||||
for (int i = 0; i < matrix1.rows;i++) {
|
||||
for (int j = 0; j < matrix1.cols; j++) {
|
||||
// how this should work in normal Matrix version:
|
||||
// outputmatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j];
|
||||
outputMatrix.buffer[i * outputMatrix.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
|
||||
}
|
||||
}
|
||||
return outputMatrix;
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
|
||||
//check, if the matrices can be multiplied
|
||||
if (matrix1.rows == matrix2.cols) {
|
||||
// Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
|
||||
Matrix outputMatrix = createMatrix(matrix2.cols, matrix1.rows);
|
||||
Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
|
||||
//Matrix outputMatrix = createMatrix(matrix2.cols, matrix1.rows);
|
||||
for(int i = 0; i < matrix1.rows; i++) {
|
||||
for (int j = 0; j < matrix2.cols; j++) {
|
||||
for (int k = 0; k < matrix2.rows; k++) {
|
||||
// how this should work in normal Matrix version:
|
||||
// outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2.buffer[k][j];
|
||||
outputMatrix.buffer[i + outputMatrix.rows * j] = matrix1.buffer[i + matrix1.rows * k] * matrix2.buffer[k + matrix2.rows * j];
|
||||
outputMatrix.buffer[i + outputMatrix.rows * j] += matrix1.buffer[i + matrix1.rows * k] * matrix2.buffer[k + matrix2.rows * j];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -151,4 +191,33 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
m.buffer = NULL;
|
||||
return m;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
if(matrix1.cols != matrix2.rows){
|
||||
Matrix m = {NULL, 0, 0};
|
||||
return m;
|
||||
}
|
||||
|
||||
Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols);
|
||||
if(!outputMatrix.buffer){
|
||||
Matrix m = {NULL, 0, 0};
|
||||
return m;
|
||||
}
|
||||
|
||||
//Matrix outputMatrix = createMatrix(matrix2.cols, matrix1.rows);
|
||||
for(int i = 0; i < matrix1.rows; i++) {
|
||||
for (int j = 0; j < matrix2.cols; j++) {
|
||||
for (int k = 0; k < matrix1.cols; k++) {
|
||||
// how this should work in normal Matrix version:
|
||||
// outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2.buffer[k][j];
|
||||
//outputMatrix.buffer[i + outputMatrix.rows * j] += matrix1.buffer[i + matrix1.rows * k] * matrix2.buffer[k + matrix2.rows * j];
|
||||
outputMatrix.buffer[i * outputMatrix.cols + j] += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[j + matrix2.cols * k];
|
||||
}
|
||||
}
|
||||
}
|
||||
return outputMatrix;
|
||||
}
|
||||
@@ -7,9 +7,9 @@ typedef float MatrixType;
|
||||
// TODO Matrixtyp definieren
|
||||
|
||||
typedef struct Matrix {
|
||||
MatrixType *buffer;
|
||||
size_t rows; //X-Element
|
||||
size_t cols; //Y-Element
|
||||
MatrixType *buffer;
|
||||
} Matrix;
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
make clean && make matrixTests && ./runMatrixTests
|
||||
Reference in new issue
Block a user