forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
No commits in common. "9dd4eff0d7f17012f14679605cb9e53e3dd7d053" and "96c6871ea38b16ea13214655c76b4d9037936c91" have entirely different histories.
9dd4eff0d7
...
96c6871ea3
118
matrix.c
118
matrix.c
@ -1,141 +1,35 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "matrix.h"
|
||||
#include <stdio.h>
|
||||
|
||||
// TODO Matrix-Funktionen implementieren
|
||||
|
||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
||||
{
|
||||
Matrix matrix;
|
||||
|
||||
if(rows == 0 || cols == 0)
|
||||
{
|
||||
matrix.rows = 0;
|
||||
matrix.cols = 0;
|
||||
matrix.data = NULL;
|
||||
return matrix;
|
||||
}
|
||||
|
||||
|
||||
matrix.rows = rows;
|
||||
matrix.cols = cols;
|
||||
|
||||
matrix.data = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
|
||||
|
||||
if (matrix.data == NULL)
|
||||
{
|
||||
|
||||
matrix.rows = 0;
|
||||
matrix.cols = 0;
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
for (int i = 0; i < rows; i++)
|
||||
{
|
||||
for (int j = 0; j < cols; j++)
|
||||
{
|
||||
matrix.data[i * matrix.cols + j] = UNDEFINED_MATRIX_VALUE;
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
|
||||
}
|
||||
|
||||
void clearMatrix(Matrix *matrix)
|
||||
{
|
||||
if (matrix->data != NULL)
|
||||
{
|
||||
free(matrix->data);
|
||||
matrix->data = NULL;
|
||||
}
|
||||
|
||||
matrix->rows = 0;
|
||||
matrix->cols = 0;
|
||||
|
||||
}
|
||||
|
||||
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
if (rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||
{
|
||||
fprintf(stderr, "Fehler: Ungültiger Index (%u, %u) bei Matrixgröße %u x %u\n", rowIdx, colIdx, matrix.rows, matrix.cols);
|
||||
return; // abbruch falls fehler
|
||||
}
|
||||
|
||||
matrix.data[rowIdx * matrix.cols + colIdx] = value;
|
||||
|
||||
}
|
||||
|
||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||
{
|
||||
if (rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||
{
|
||||
fprintf(stderr, "Fehler: Ungültiger Index (%u, %u) bei Matrixgröße %u x %u\n", rowIdx, colIdx, matrix.rows, matrix.cols);
|
||||
return UNDEFINED_MATRIX_VALUE;
|
||||
}
|
||||
|
||||
return matrix.data[rowIdx * matrix.cols + colIdx];
|
||||
|
||||
}
|
||||
|
||||
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
|
||||
{
|
||||
fprintf(stderr, "Fehler: Matrizen haben unterschiedliche Größen (%u x %u) und (%u x %u)\n",
|
||||
matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols);
|
||||
|
||||
Matrix empty = {0, 0, NULL};
|
||||
return empty;
|
||||
}
|
||||
|
||||
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
||||
|
||||
if (result.data == NULL)
|
||||
{
|
||||
fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n");
|
||||
return result;
|
||||
}
|
||||
|
||||
for (int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
for (int j = 0; j < matrix1.cols; j++)
|
||||
{
|
||||
result.data[i * result.cols + j] = matrix1.data[i * matrix1.cols + j] + matrix2.data[i * matrix2.cols + j];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||
{
|
||||
if (matrix1.cols != matrix2.rows)
|
||||
{
|
||||
fprintf(stderr, "Fehler: Matrizen der Dimension (%u x %u) und (%u x %u) koennen nicht multipliziert werden\n",
|
||||
matrix1.rows, matrix1.cols, matrix2.rows, matrix2.cols);
|
||||
|
||||
Matrix empty = {0, 0, NULL};
|
||||
return empty;
|
||||
}
|
||||
|
||||
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
||||
|
||||
if (result.data == NULL)
|
||||
{
|
||||
fprintf(stderr, "Fehler: Speicher konnte nicht reserviert werden!\n");
|
||||
return result;
|
||||
}
|
||||
|
||||
for (int i = 0; i < matrix1.rows; i++)
|
||||
{
|
||||
for (int j = 0; j < matrix2.cols; j++)
|
||||
{
|
||||
MatrixType sum = 0.0;
|
||||
|
||||
for (int k = 0; k < matrix1.cols; k++)
|
||||
{
|
||||
sum += matrix1.data[i * matrix1.cols + k] * matrix2.data[k * matrix2.cols + j];
|
||||
}
|
||||
result.data[i * result.cols +j] = sum;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user