forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
No commits in common. "ede0bd8bd8b41c747ccde430f1add276a3312aa2" and "be07dcffcff20002e1da659304f3d1364ece292e" have entirely different histories.
ede0bd8bd8
...
be07dcffcf
13
makefile
13
makefile
@ -57,9 +57,12 @@ imageInputTests: imageInput.o imageInputTests.c $(unityfolder)/unity.c
|
|||||||
# --------------------------
|
# --------------------------
|
||||||
# Clean
|
# Clean
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
#clean:
|
||||||
|
#ifeq ($(OS),Windows_NT)
|
||||||
|
# del /f *.o *.exe
|
||||||
|
#else
|
||||||
|
# rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
|
||||||
|
#endif
|
||||||
|
# clean für windows
|
||||||
clean:
|
clean:
|
||||||
ifeq ($(OS),Windows_NT)
|
rm -f *.o *.exe
|
||||||
del /f *.o *.exe
|
|
||||||
else
|
|
||||||
rm -f *.o mnist runMatrixTests runNeuralNetworkTests runImageInputTests
|
|
||||||
endif
|
|
||||||
173
matrix.c
173
matrix.c
@ -1,8 +1,6 @@
|
|||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
/*typedef struct {
|
/*typedef struct {
|
||||||
unsigned int rows; //Zeilen
|
unsigned int rows; //Zeilen
|
||||||
@ -10,12 +8,6 @@
|
|||||||
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
|
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
|
||||||
} Matrix;*/
|
} Matrix;*/
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
||||||
|
|
||||||
Matrix errorMatrix = {0, 0, NULL};
|
|
||||||
if (rows == 0 || cols == 0) {
|
|
||||||
|
|
||||||
return errorMatrix;
|
|
||||||
}
|
|
||||||
MatrixType *buffer =
|
MatrixType *buffer =
|
||||||
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc
|
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc
|
||||||
// liefert Zeiger auf Speicher
|
// liefert Zeiger auf Speicher
|
||||||
@ -28,7 +20,6 @@ void clearMatrix(Matrix *matrix) {
|
|||||||
matrix->cols = UNDEFINED_MATRIX_VALUE;
|
matrix->cols = UNDEFINED_MATRIX_VALUE;
|
||||||
free((*matrix).buffer); // Speicher freigeben
|
free((*matrix).buffer); // Speicher freigeben
|
||||||
}
|
}
|
||||||
|
|
||||||
void setMatrixAt(const MatrixType value, Matrix matrix,
|
void setMatrixAt(const MatrixType value, Matrix matrix,
|
||||||
const unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
const unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
||||||
const unsigned int colIdx) {
|
const unsigned int colIdx) {
|
||||||
@ -49,168 +40,26 @@ MatrixType getMatrixAt(const Matrix matrix,
|
|||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix broadCastCols(const Matrix matrix, const unsigned int rows,
|
|
||||||
const unsigned int cols) {
|
|
||||||
|
|
||||||
Matrix copy = createMatrix(
|
|
||||||
rows, cols); // Matrix 1 Kopie erstellen mit Dimensionen von Matrix2
|
|
||||||
for (int r = 0; r < rows; r++) {
|
|
||||||
|
|
||||||
MatrixType value = getMatrixAt(matrix, r, 0);
|
|
||||||
for (int c = 0; c < cols; c++) {
|
|
||||||
|
|
||||||
setMatrixAt(value, copy, r, c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
Matrix broadCastRows(const Matrix matrix, const unsigned int rows,
|
|
||||||
const unsigned int cols) {
|
|
||||||
|
|
||||||
Matrix copy = createMatrix(rows, cols);
|
|
||||||
|
|
||||||
for (int c = 0; c < cols; c++) {
|
|
||||||
MatrixType value = getMatrixAt(matrix, 0, c);
|
|
||||||
|
|
||||||
for (int r = 0; r < rows; r++) {
|
|
||||||
|
|
||||||
setMatrixAt(value, copy, r, c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
||||||
|
|
||||||
// Broadcasting nur bei Vektor und Matrix, Fehlermeldung bei zwei unpassenden
|
// Ergebnismatrix
|
||||||
// Matrizen
|
Matrix result;
|
||||||
|
|
||||||
const unsigned int rows1 = matrix1.rows;
|
// Broadcasting nur bei Vektor und Matrix, Fehlermeldung bei zwei unpassender
|
||||||
const unsigned int rows2 = matrix2.rows;
|
// Matrix
|
||||||
const unsigned int cols1 = matrix1.cols;
|
if (matrix1.rows != matrix2.rows) {
|
||||||
const unsigned int cols2 = matrix2.cols;
|
|
||||||
|
|
||||||
const int rowsEqual = ((rows1 == rows2) ? 1 : 0);
|
// check, which one is smaller
|
||||||
|
// realloc
|
||||||
const int colsEqual = ((cols1 == cols2) ? 1 : 0);
|
|
||||||
|
|
||||||
if (rowsEqual && colsEqual) // addieren
|
|
||||||
|
|
||||||
{
|
|
||||||
Matrix result = createMatrix(rows1, cols1); // Speicher reservieren
|
|
||||||
|
|
||||||
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
|
|
||||||
|
|
||||||
result.buffer[i] =
|
|
||||||
(matrix1.buffer[i] +
|
|
||||||
matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
|
||||||
// Startadresse + (i * sizeof(MatrixType))
|
|
||||||
}
|
|
||||||
|
|
||||||
return result; // zurückgeben
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (rowsEqual && !colsEqual) {
|
if (matrix1.cols != matrix2.cols) {
|
||||||
|
|
||||||
if (cols1 == 1) {
|
|
||||||
|
|
||||||
Matrix result = createMatrix(rows2, cols2);
|
|
||||||
|
|
||||||
Matrix copy1 = broadCastCols(matrix1, rows2, cols2);
|
|
||||||
|
|
||||||
for (int i = 0; i < (rows2 * cols2); i++) { // addieren
|
|
||||||
|
|
||||||
result.buffer[i] =
|
|
||||||
(copy1.buffer[i] +
|
|
||||||
matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
|
||||||
// Startadresse + (i * sizeof(MatrixType))
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
|
|
||||||
// add und return
|
|
||||||
|
|
||||||
} else if (cols2 == 1) {
|
|
||||||
|
|
||||||
Matrix result = createMatrix(rows1, cols1);
|
|
||||||
|
|
||||||
Matrix copy2 = broadCastCols(matrix2, rows1, cols1);
|
|
||||||
|
|
||||||
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
|
|
||||||
|
|
||||||
result.buffer[i] =
|
|
||||||
(matrix1.buffer[i] +
|
|
||||||
copy2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
|
||||||
// Startadresse + (i * sizeof(MatrixType))
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
// add und return
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
|
|
||||||
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
|
|
||||||
// Programmabbruch
|
|
||||||
Matrix error = {0, 0, NULL};
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (!rowsEqual && colsEqual) {
|
// Speicher reservieren
|
||||||
|
|
||||||
if (rows1 == 1) {
|
// Matrix addieren
|
||||||
|
|
||||||
Matrix result = createMatrix(rows2, cols2);
|
return result;
|
||||||
|
|
||||||
Matrix copy1 = broadCastRows(matrix1, rows2, cols2);
|
|
||||||
|
|
||||||
for (int i = 0; i < (rows2 * cols2); i++) { // addieren
|
|
||||||
|
|
||||||
result.buffer[i] =
|
|
||||||
(copy1.buffer[i] +
|
|
||||||
matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
|
||||||
// Startadresse + (i * sizeof(MatrixType))
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
|
|
||||||
// add und return
|
|
||||||
|
|
||||||
} else if (rows2 == 1) {
|
|
||||||
|
|
||||||
Matrix result = createMatrix(rows1, cols1);
|
|
||||||
|
|
||||||
Matrix copy2 = broadCastCols(matrix2, rows1, cols1);
|
|
||||||
// add und return
|
|
||||||
|
|
||||||
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
|
|
||||||
|
|
||||||
result.buffer[i] =
|
|
||||||
(matrix1.buffer[i] +
|
|
||||||
copy2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
|
||||||
// Startadresse + (i * sizeof(MatrixType))
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
|
|
||||||
printf("Fehlermeldung"); // vielleicht Fehlermeldung ändern zu
|
|
||||||
// Programmabbruch
|
|
||||||
Matrix error = {0, 0, NULL};
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else {
|
|
||||||
printf(
|
|
||||||
"Fehlermeldung"); // vielleicht Fehlermeldung ändern zu Programmabbruch
|
|
||||||
Matrix error = {0, 0, NULL};
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2) { return matrix1; }
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2) { return matrix1; }
|
||||||
|
|||||||
5
matrix.h
5
matrix.h
@ -19,11 +19,6 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx,
|
|||||||
unsigned int colIdx);
|
unsigned int colIdx);
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx,
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx,
|
||||||
unsigned int colIdx);
|
unsigned int colIdx);
|
||||||
|
|
||||||
Matrix broadCastCols(const Matrix matrix, const unsigned int rows,
|
|
||||||
const unsigned int cols);
|
|
||||||
Matrix broadCastRows(const Matrix matrix, const unsigned int rows,
|
|
||||||
const unsigned int cols);
|
|
||||||
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);
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user