forked from freudenreichan/info2Praktikum-NeuronalesNetz
174 lines
3.9 KiB
C
174 lines
3.9 KiB
C
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "matrix.h"
|
|
|
|
// Matrix erstellen
|
|
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
|
{
|
|
Matrix matrix;
|
|
|
|
matrix.rows = 0;
|
|
matrix.cols = 0;
|
|
matrix.buffer = NULL;
|
|
|
|
// Ungültige Dimensionen überprüfen damit calloc wie gewünscht arbeitet
|
|
if (rows == 0 || cols == 0)
|
|
{
|
|
return matrix;
|
|
}
|
|
|
|
matrix.buffer = calloc(rows * cols, sizeof(MatrixType));
|
|
|
|
if (matrix.buffer == NULL)
|
|
{
|
|
return matrix;
|
|
}
|
|
|
|
matrix.rows = rows;
|
|
matrix.cols = cols;
|
|
|
|
return matrix;
|
|
}
|
|
|
|
|
|
// Matrix-Speicher freigeben
|
|
void clearMatrix(Matrix* matrix)
|
|
{
|
|
if (matrix != NULL)
|
|
{
|
|
free(matrix->buffer);
|
|
|
|
matrix->buffer = NULL;
|
|
matrix->rows = 0;
|
|
matrix->cols = 0;
|
|
}
|
|
}
|
|
|
|
|
|
// Wert an einer bestimmten Position setzen
|
|
void setMatrixAt(MatrixType value, Matrix matrix,
|
|
unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
if (matrix.buffer != NULL &&
|
|
rowIdx < matrix.rows &&
|
|
colIdx < matrix.cols)
|
|
{
|
|
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
|
|
}
|
|
}
|
|
|
|
|
|
// Wert an einer bestimmten Position auslesen
|
|
MatrixType getMatrixAt(const Matrix matrix,
|
|
unsigned int rowIdx, unsigned int colIdx)
|
|
{
|
|
if (matrix.buffer != NULL &&
|
|
rowIdx < matrix.rows &&
|
|
colIdx < matrix.cols)
|
|
{
|
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
|
}
|
|
|
|
return UNDEFINED_MATRIX_VALUE;
|
|
}
|
|
|
|
|
|
// Zwei Matrizen addieren (mit Broadcasting)
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
// Buffer überprüfen
|
|
if (matrix1.buffer == NULL ||
|
|
matrix2.buffer == NULL)
|
|
{
|
|
return (Matrix){0};
|
|
}
|
|
|
|
// Überprüfen, ob Broadcasting möglich ist
|
|
if ((matrix1.rows != matrix2.rows &&
|
|
matrix1.rows != 1 && matrix2.rows != 1) ||
|
|
(matrix1.cols != matrix2.cols &&
|
|
matrix1.cols != 1 && matrix2.cols != 1))
|
|
{
|
|
return (Matrix){0};
|
|
}
|
|
|
|
// Ergebnisdimensionen bestimmen
|
|
unsigned int rows = matrix1.rows > matrix2.rows
|
|
? matrix1.rows : matrix2.rows;
|
|
|
|
unsigned int cols = matrix1.cols > matrix2.cols
|
|
? matrix1.cols : matrix2.cols;
|
|
|
|
// Ergebnismatrix erstellen
|
|
Matrix result = createMatrix(rows, cols);
|
|
|
|
if (result.buffer == NULL)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
// Elementweise Addition mit Broadcasting
|
|
for (unsigned int i = 0; i < rows; i++)
|
|
{
|
|
for (unsigned int j = 0; j < cols; j++)
|
|
{
|
|
unsigned int r1 = matrix1.rows == 1 ? 0 : i;
|
|
unsigned int c1 = matrix1.cols == 1 ? 0 : j;
|
|
|
|
unsigned int r2 = matrix2.rows == 1 ? 0 : i;
|
|
unsigned int c2 = matrix2.cols == 1 ? 0 : j;
|
|
|
|
MatrixType value =
|
|
getMatrixAt(matrix1, r1, c1) +
|
|
getMatrixAt(matrix2, r2, c2);
|
|
|
|
setMatrixAt(value, result, i, j);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
|
|
// Zwei Matrizen multiplizieren
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|
{
|
|
// Dimensionen und Buffer überprüfen
|
|
if (matrix1.cols != matrix2.rows ||
|
|
matrix1.rows == 0 ||
|
|
matrix2.cols == 0 ||
|
|
matrix1.buffer == NULL ||
|
|
matrix2.buffer == NULL)
|
|
{
|
|
return (Matrix){0};
|
|
}
|
|
|
|
// Ergebnismatrix erstellen
|
|
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
|
|
|
if (result.buffer == NULL)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
// Matrizenmultiplikation
|
|
for (unsigned int i = 0; i < matrix1.rows; i++)
|
|
{
|
|
for (unsigned int j = 0; j < matrix2.cols; j++)
|
|
{
|
|
MatrixType sum = 0;
|
|
|
|
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;
|
|
}
|