Compare commits

..

2 Commits
main ... felix

Author SHA1 Message Date
c7150d2250 Dateien nach "/" hochladen 2025-11-27 19:23:34 +00:00
8240e7bab2 matrix.c gelöscht 2025-11-27 19:23:07 +00:00
6 changed files with 156 additions and 456 deletions

View File

@ -1,13 +0,0 @@
//neuralNetwork.c - BalckBox - Erkennung der Zahlen
//ImageInput.c - // TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
// Patric Viebcodet
// matrix.c - // TODO Matrix-Funktionen implementieren - Sven die letzen 3, Felix die ersten 3
//
//

302
matrix.c
View File

@ -1,151 +1,151 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "matrix.h" #include "matrix.h"
// TODO Matrix-Funktionen implementieren // TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix m; Matrix m;
if (rows == 0 || cols == 0) if (rows == 0 || cols == 0)
{ {
m.rows = 0; m.rows = 0;
m.cols = 0; m.cols = 0;
m.buffer = NULL; m.buffer = NULL;
return m; return m;
} }
m.rows = rows; m.rows = rows;
m.cols = cols; m.cols = cols;
m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType)); m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
return m; return m;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (matrix != NULL) if (matrix != NULL)
{ {
if (matrix->buffer != NULL) if (matrix->buffer != NULL)
{ {
free(matrix->buffer); free(matrix->buffer);
matrix->buffer = NULL; matrix->buffer = NULL;
} }
matrix->rows = 0; matrix->rows = 0;
matrix->cols = 0; matrix->cols = 0;
} }
} }
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (matrix.buffer != NULL) if (matrix.buffer != NULL)
{ {
if (rowIdx < matrix.rows && colIdx < matrix.cols) if (rowIdx < matrix.rows && colIdx < matrix.cols)
{ {
matrix.buffer[rowIdx * matrix.cols + colIdx] = value; matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
} }
} }
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols) if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
{ {
return UNDEFINED_MATRIX_VALUE; return UNDEFINED_MATRIX_VALUE;
} }
return matrix.buffer[rowIdx * matrix.cols + colIdx]; return matrix.buffer[rowIdx * matrix.cols + colIdx];
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result; Matrix result;
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows) if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows)
{ {
result.rows = 0; result.rows = 0;
result.cols = 0; result.cols = 0;
result.buffer = NULL; result.buffer = NULL;
return result; return result;
} }
if (matrix1.cols == matrix2.cols) if (matrix1.cols == matrix2.cols)
{ {
result = createMatrix(matrix1.rows, matrix1.cols); result = createMatrix(matrix1.rows, matrix1.cols);
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++)
{ {
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j); MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
setMatrixAt(value, result, i, j); setMatrixAt(value, result, i, j);
} }
} }
return result; return result;
} }
if (matrix1.cols == 1 && matrix2.cols > 1) if (matrix1.cols == 1 && matrix2.cols > 1)
{ {
result = createMatrix(matrix1.rows, matrix2.cols); result = createMatrix(matrix1.rows, matrix2.cols);
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++)
{ {
MatrixType value = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j); MatrixType value = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j);
setMatrixAt(value, result, i, j); setMatrixAt(value, result, i, j);
} }
} }
return result; return result;
} }
else if (matrix2.cols == 1 && matrix1.cols > 1) else if (matrix2.cols == 1 && matrix1.cols > 1)
{ {
result = createMatrix(matrix1.rows, matrix1.cols); result = createMatrix(matrix1.rows, matrix1.cols);
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++)
{ {
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0); MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0);
setMatrixAt(value, result, i, j); setMatrixAt(value, result, i, j);
} }
} }
return result; return result;
} }
//Fall: Unterschiedliche Spaltenanzahl, beide ungleich 1 //Fall: Unterschiedliche Spaltenanzahl, beide ungleich 1
result.rows = 0; result.rows = 0;
result.cols = 0; result.cols = 0;
result.buffer = NULL; result.buffer = NULL;
return result; return result;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result; Matrix result;
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows) if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
{ {
result.rows = 0; result.rows = 0;
result.cols = 0; result.cols = 0;
result.buffer = NULL; result.buffer = NULL;
return result; return result;
} }
result = createMatrix(matrix1.rows, matrix2.cols); result = createMatrix(matrix1.rows, matrix2.cols);
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++)
{ {
MatrixType sum = 0; MatrixType sum = 0;
for (int k = 0; k < matrix1.cols; k++) for (int k = 0; k < matrix1.cols; k++)
{ {
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j); sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
} }
setMatrixAt(sum, result, i, j); setMatrixAt(sum, result, i, j);
} }
} }
return result; return result;
} }

View File

@ -1,19 +1,13 @@
#ifndef MATRIX_H #ifndef MATRIX_H
#define MATRIX_H #define MATRIX_H
#define UNDEFINED_MATRIX_VALUE 0 #define UNDEFINED_MATRIX_VALUE 0
typedef float MatrixType; typedef float MatrixType;
// Definition des Matrix-Typs // TODO Matrixtyp definieren
typedef struct {
unsigned int rows;
unsigned int cols;
MatrixType *buffer; // dynamisch allokiertes Array
} Matrix;
// Funktionsdeklarationen
Matrix createMatrix(unsigned int rows, unsigned int cols); Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix); void clearMatrix(Matrix *matrix);
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx); void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
@ -21,4 +15,5 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
Matrix add(const Matrix matrix1, const Matrix matrix2); Matrix add(const Matrix matrix1, const Matrix matrix2);
Matrix multiply(const Matrix matrix1, const Matrix matrix2); Matrix multiply(const Matrix matrix1, const Matrix matrix2);
#endif
#endif

View File

@ -1,151 +0,0 @@
#include <stdlib.h>
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix m;
if (rows == 0 || cols == 0)
{
m.rows = 0;
m.cols = 0;
m.buffer = NULL;
return m;
}
m.rows = rows;
m.cols = cols;
m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType));
return m;
}
void clearMatrix(Matrix *matrix)
{
if (matrix != NULL)
{
if (matrix->buffer != NULL)
{
free(matrix->buffer);
matrix->buffer = NULL;
}
matrix->rows = 0;
matrix->cols = 0;
}
}
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
if (matrix.buffer != NULL)
{
if (rowIdx < matrix.rows && colIdx < matrix.cols)
{
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
}
}
}
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
{
return UNDEFINED_MATRIX_VALUE;
}
return matrix.buffer[rowIdx * matrix.cols + colIdx];
}
Matrix add(const Matrix matrix1, const Matrix matrix2)
{
Matrix result;
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows)
{
result.rows = 0;
result.cols = 0;
result.buffer = NULL;
return result;
}
if (matrix1.cols == matrix2.cols)
{
result = createMatrix(matrix1.rows, matrix1.cols);
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix1.cols; j++)
{
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j);
setMatrixAt(value, result, i, j);
}
}
return result;
}
if (matrix1.cols == 1 && matrix2.cols > 1)
{
result = createMatrix(matrix1.rows, matrix2.cols);
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix2.cols; j++)
{
MatrixType value = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j);
setMatrixAt(value, result, i, j);
}
}
return result;
}
else if (matrix2.cols == 1 && matrix1.cols > 1)
{
result = createMatrix(matrix1.rows, matrix1.cols);
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix1.cols; j++)
{
MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0);
setMatrixAt(value, result, i, j);
}
}
return result;
}
//Fall: Unterschiedliche Spaltenanzahl, beide ungleich 1
result.rows = 0;
result.cols = 0;
result.buffer = NULL;
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
Matrix result;
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
{
result.rows = 0;
result.cols = 0;
result.buffer = NULL;
return result;
}
result = createMatrix(matrix1.rows, matrix2.cols);
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix2.cols; j++)
{
MatrixType sum = 0;
for (int k = 0; k < matrix1.cols; k++)
{
sum += getMatrixAt(matrix1, i, k) * getMatrixAt(matrix2, k, j);
}
setMatrixAt(sum, result, i, j);
}
}
return result;
}

107
test.c
View File

@ -1,107 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include "matrix.h"
Matrix createMatrix(unsigned int rows, unsigned int cols) {
Matrix m;
if (rows == 0 || cols == 0) {
m.rows = 0;
m.cols = 0;
m.buffer = NULL;
return m;
}
m.rows = rows;
m.cols = cols;
m.buffer = (MatrixType *)malloc(rows * cols * sizeof(MatrixType));
if (m.buffer == NULL) {
fprintf(stderr, "Fehler: Speicher konnte nicht allokiert werden.\n");
m.rows = m.cols = 0;
} else {
for (unsigned int i = 0; i < rows * cols; i++) {
m.buffer[i] = UNDEFINED_MATRIX_VALUE;
}
}
return m;
}
void clearMatrix(Matrix *matrix) {
if (matrix->buffer != NULL) {
free(matrix->buffer);
matrix->buffer = 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) {
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) {
return matrix.buffer[rowIdx * matrix.cols + colIdx];
}
return UNDEFINED_MATRIX_VALUE;
}
Matrix add(const Matrix matrix1, const Matrix matrix2) {
Matrix result = {0, 0, NULL};
// Prüfe Dimensionen und Broadcasting
if ((matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) ||
(matrix1.rows == matrix2.rows && matrix2.cols == 1) ||
(matrix1.cols == matrix2.cols && matrix2.rows == 1) ||
(matrix2.rows == matrix1.rows && matrix1.cols == 1) ||
(matrix2.cols == matrix1.cols && matrix1.rows == 1)) {
result = createMatrix(matrix1.rows, matrix1.cols);
for (unsigned int i = 0; i < matrix1.rows; i++) {
for (unsigned int j = 0; j < matrix1.cols; j++) {
MatrixType val1 = getMatrixAt(matrix1, i, j);
MatrixType val2;
// Broadcasting
if (matrix2.rows == matrix1.rows && matrix2.cols == matrix1.cols) {
val2 = getMatrixAt(matrix2, i, j);
} else if (matrix2.rows == matrix1.rows && matrix2.cols == 1) {
val2 = getMatrixAt(matrix2, i, 0);
} else if (matrix2.rows == 1 && matrix2.cols == matrix1.cols) {
val2 = getMatrixAt(matrix2, 0, j);
} else if (matrix1.rows == matrix2.rows && matrix1.cols == 1) {
val2 = getMatrixAt(matrix2, i, 0);
} else if (matrix1.rows == 1 && matrix1.cols == matrix2.cols) {
val2 = getMatrixAt(matrix2, 0, j);
} else {
val2 = UNDEFINED_MATRIX_VALUE;
}
setMatrixAt(val1 + val2, result, i, j);
}
}
}
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2) {
Matrix result = {0, 0, NULL};
if (matrix1.cols != matrix2.rows) {
return result; // inkompatible Dimensionen
}
result = createMatrix(matrix1.rows, matrix2.cols);
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;
}

24
test.h
View File

@ -1,24 +0,0 @@
#ifndef MATRIX_H
#define MATRIX_H
#define UNDEFINED_MATRIX_VALUE 0
typedef float MatrixType;
// Definition des Matrix-Typs
typedef struct {
unsigned int rows;
unsigned int cols;
MatrixType *buffer; // dynamisch allokiertes Array
} Matrix;
// Funktionsdeklarationen
Matrix createMatrix(unsigned int rows, unsigned int cols);
void clearMatrix(Matrix *matrix);
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
Matrix add(const Matrix matrix1, const Matrix matrix2);
Matrix multiply(const Matrix matrix1, const Matrix matrix2);
#endif