forked from freudenreichan/info2Praktikum-NeuronalesNetz
113 lines
2.8 KiB
C
113 lines
2.8 KiB
C
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include "matrix.h"
|
|
|
|
// TODO Matrix-Funktionen implementieren
|
|
|
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|
{
|
|
Matrix m;
|
|
m.rows = rows;
|
|
m.cols = cols;
|
|
|
|
m.buffer = (MatrixType*)malloc(sizeof(MatrixType) * rows * cols);
|
|
|
|
if (m.buffer == NULL){
|
|
fprintf(stderr, "Error: Memory allocation failed in createMatrix!.\n");
|
|
m.rows = 0;
|
|
m.cols = 0;
|
|
return m;
|
|
}
|
|
for (unsigned int i = 0; i < rows * cols; i++){
|
|
m.buffer[i] = 0.0f;
|
|
}
|
|
return m;
|
|
}
|
|
|
|
|
|
void clearMatrix(Matrix *matrix)
|
|
{
|
|
if (matrix == NULL || matrix->buffer == NULL) {
|
|
return;
|
|
}
|
|
|
|
// Alle Elemente auf 0 setzen
|
|
for (unsigned int i = 0; i < matrix->rows * matrix->cols; i++) {
|
|
matrix->buffer[i] = 0.0f;
|
|
}
|
|
}
|
|
|
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) {
|
|
fprintf(stderr, "Error: setMatrixAt index out of bounds.\n");
|
|
return;
|
|
}
|
|
|
|
matrix.buffer[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, "Error: getMatrixAt index out of bounds.\n");
|
|
return UNDEFINED_MATRIX_VALUE;
|
|
}
|
|
|
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
|
}
|
|
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) {
|
|
fprintf(stderr, "Error: Matrix dimensions do not match for addition.\n");
|
|
Matrix empty = {0, 0, NULL};
|
|
return empty;
|
|
}
|
|
|
|
Matrix result = createMatrix(matrix1.rows, matrix1.cols);
|
|
if (result.buffer == NULL) {
|
|
return result;
|
|
}
|
|
|
|
for (unsigned int i = 0; i < matrix1.rows; i++) {
|
|
for (unsigned int j = 0; j < matrix1.cols; j++) {
|
|
setMatrixAt(
|
|
getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j),
|
|
result,
|
|
i, j
|
|
);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
if (matrix1.cols != matrix2.rows) {
|
|
fprintf(stderr, "Error: Invalid matrix dimensions for multiplication.\n");
|
|
Matrix empty = {0, 0, NULL};
|
|
return empty;
|
|
}
|
|
|
|
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
|
if (result.buffer == NULL) {
|
|
return result;
|
|
}
|
|
|
|
for (unsigned int i = 0; i < matrix1.rows; i++) {
|
|
for (unsigned int j = 0; j < matrix2.cols; j++) {
|
|
MatrixType sum = 0.0f;
|
|
|
|
for (unsigned int k = 0; k < matrix1.cols; k++) {
|
|
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
|
|
}
|
|
|
|
setMatrixAt(sum, result, i, j);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
} |