forked from freudenreichan/info2Praktikum-NeuronalesNetz
Compare commits
2
Commits
7bffe1fdad
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
447e77a7bf | ||
|
|
a6749f07d1 |
+59
-2
@@ -1,4 +1,61 @@
|
|||||||
mnist
|
# ---> C
|
||||||
runTests
|
# Prerequisites
|
||||||
|
*.d
|
||||||
|
|
||||||
|
# Object files
|
||||||
*.o
|
*.o
|
||||||
|
*.ko
|
||||||
|
*.obj
|
||||||
|
*.elf
|
||||||
|
|
||||||
|
# Linker output
|
||||||
|
*.ilk
|
||||||
|
*.map
|
||||||
|
*.exp
|
||||||
|
|
||||||
|
# Precompiled Headers
|
||||||
|
*.gch
|
||||||
|
*.pch
|
||||||
|
|
||||||
|
# Libraries
|
||||||
|
*.lib
|
||||||
|
*.la
|
||||||
|
*.lo
|
||||||
|
|
||||||
|
# Shared objects (inc. Windows DLLs)
|
||||||
|
*.dll
|
||||||
|
*.so
|
||||||
|
*.so.*
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Executables
|
||||||
*.exe
|
*.exe
|
||||||
|
*.out
|
||||||
|
*.app
|
||||||
|
*.i*86
|
||||||
|
*.x86_64
|
||||||
|
*.hex
|
||||||
|
Startcode/mnist
|
||||||
|
Startcode/runTests
|
||||||
|
|
||||||
|
# Debug files
|
||||||
|
*.dSYM/
|
||||||
|
*.su
|
||||||
|
*.idb
|
||||||
|
*.pdb
|
||||||
|
|
||||||
|
# Kernel Module Compile Results
|
||||||
|
*.mod*
|
||||||
|
*.cmd
|
||||||
|
.tmp_versions/
|
||||||
|
modules.order
|
||||||
|
Module.symvers
|
||||||
|
Mkfile.old
|
||||||
|
dkms.conf
|
||||||
|
|
||||||
|
# IDE folders
|
||||||
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
|
||||||
|
# macOS
|
||||||
|
.DS_Store
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "imageInput.h"
|
||||||
|
|
||||||
|
#define BUFFER_SIZE 100
|
||||||
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
|
||||||
|
// 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
|
||||||
|
GrayScaleImageSeries *readImages(const char *path)
|
||||||
|
{
|
||||||
|
GrayScaleImageSeries *series = NULL;
|
||||||
|
|
||||||
|
return series;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
||||||
|
void clearSeries(GrayScaleImageSeries *series)
|
||||||
|
{
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef IMAGEINPUT_H
|
||||||
|
#define IMAGEINPUT_H
|
||||||
|
|
||||||
|
typedef unsigned char GrayScalePixelType;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
GrayScalePixelType *buffer;
|
||||||
|
unsigned int width;
|
||||||
|
unsigned int height;
|
||||||
|
} GrayScaleImage;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
GrayScaleImage *images;
|
||||||
|
unsigned char *labels;
|
||||||
|
unsigned int count;
|
||||||
|
} GrayScaleImageSeries;
|
||||||
|
|
||||||
|
GrayScaleImageSeries *readImages(const char *path);
|
||||||
|
void clearSeries(GrayScaleImageSeries *series);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "matrix.h"
|
||||||
|
|
||||||
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
@@ -6,12 +6,7 @@
|
|||||||
typedef float MatrixType;
|
typedef float MatrixType;
|
||||||
|
|
||||||
// TODO Matrixtyp definieren
|
// TODO Matrixtyp definieren
|
||||||
typedef struct{
|
|
||||||
unsigned int rows;
|
|
||||||
unsigned int cols;
|
|
||||||
MatrixType* buffer;
|
|
||||||
|
|
||||||
} Matrix;
|
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
void clearMatrix(Matrix *matrix);
|
void clearMatrix(Matrix *matrix);
|
||||||
@@ -170,7 +170,7 @@ NeuralNetwork loadModel(const char *path)
|
|||||||
|
|
||||||
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
|
static Matrix imageBatchToMatrixOfImageVectors(const GrayScaleImage images[], unsigned int count)
|
||||||
{
|
{
|
||||||
Matrix matrix = {0, 0, NULL}; //hier evtl Null auf int casten?
|
Matrix matrix = {NULL, 0, 0};
|
||||||
|
|
||||||
if(count > 0 && images != NULL)
|
if(count > 0 && images != NULL)
|
||||||
{
|
{
|
||||||
@@ -8,42 +8,7 @@
|
|||||||
|
|
||||||
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
static void prepareNeuralNetworkFile(const char *path, const NeuralNetwork nn)
|
||||||
{
|
{
|
||||||
FILE *file = fopen(path, "wb");
|
// TODO
|
||||||
|
|
||||||
if (file == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1. Header schreiben
|
|
||||||
const char *fileTag = "__info2_neural_network_file_format__";
|
|
||||||
fwrite(fileTag, sizeof(char), strlen(fileTag), file);
|
|
||||||
|
|
||||||
// 2. Alle Schichten schreiben
|
|
||||||
for (unsigned int i = 0; i < nn.numberOfLayers; i++) {
|
|
||||||
// NUR bei der ERSTEN Schicht: Input-Dimension schreiben
|
|
||||||
if (i == 0) {
|
|
||||||
int inputDim = nn.layers[i].weights.cols;
|
|
||||||
fwrite(&inputDim, sizeof(int), 1, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output-Dimension (= Anzahl Zeilen der Gewichtsmatrix)
|
|
||||||
int outputDim = nn.layers[i].weights.rows;
|
|
||||||
fwrite(&outputDim, sizeof(int), 1, file);
|
|
||||||
|
|
||||||
// Gewichtsmatrix schreiben (alle Werte)
|
|
||||||
int weightCount = nn.layers[i].weights.rows * nn.layers[i].weights.cols;
|
|
||||||
fwrite(nn.layers[i].weights.buffer, sizeof(MatrixType), weightCount, file);
|
|
||||||
|
|
||||||
// Bias-Matrix schreiben (alle Werte)
|
|
||||||
int biasCount = nn.layers[i].biases.rows * nn.layers[i].biases.cols;
|
|
||||||
fwrite(nn.layers[i].biases.buffer, sizeof(MatrixType), biasCount, file);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Terminator schreiben (outputDimension = 0 zum Stoppen)
|
|
||||||
int terminator = 0;
|
|
||||||
fwrite(&terminator, sizeof(int), 1, file);
|
|
||||||
|
|
||||||
fclose(file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
void test_loadModelReturnsCorrectNumberOfLayers(void)
|
||||||
-180
@@ -1,180 +0,0 @@
|
|||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "imageInput.h"
|
|
||||||
|
|
||||||
#define BUFFER_SIZE 100
|
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
|
||||||
|
|
||||||
// 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
|
|
||||||
|
|
||||||
|
|
||||||
static int checkFileHeader(FILE *file)
|
|
||||||
{
|
|
||||||
char buffer[BUFFER_SIZE];
|
|
||||||
int length = strlen(FILE_HEADER_STRING);
|
|
||||||
|
|
||||||
// Prüfen ob fread erfolgreich war
|
|
||||||
if (fread(buffer, sizeof(char), length, file) != length) {
|
|
||||||
return 0; // Lesefehler
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer[length] = '\0';
|
|
||||||
|
|
||||||
if (strcmp(buffer, FILE_HEADER_STRING) == 0) {
|
|
||||||
return 1;
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int readDimensions(FILE *file, unsigned short * count, unsigned short *width, unsigned short *height)
|
|
||||||
{
|
|
||||||
// Anzahl lesen
|
|
||||||
if (fread(count, sizeof(unsigned short), 1, file) != 1) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
// Breite lesen
|
|
||||||
if (fread(width, sizeof(unsigned short), 1, file) != 1) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
// Höhe lesen
|
|
||||||
if (fread(height, sizeof(unsigned short), 1, file) != 1) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1; // Alles ok
|
|
||||||
}
|
|
||||||
|
|
||||||
static int readSingleImage(FILE *file, GrayScaleImage *image, unsigned char *label, unsigned short width, unsigned short height)
|
|
||||||
{
|
|
||||||
// Schritt 1: Gesamtzahl Pixel berechnen
|
|
||||||
int totalPixels = width * height;
|
|
||||||
|
|
||||||
// Schritt 2: Speicher allokieren
|
|
||||||
image->buffer = (unsigned char *)malloc(totalPixels * sizeof(unsigned char));
|
|
||||||
if (image->buffer == NULL) {
|
|
||||||
return 0; // Fehler: kein Speicher verfügbar
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schritt 3: Breite und Höhe setzen
|
|
||||||
image->width = width;
|
|
||||||
image->height = height;
|
|
||||||
|
|
||||||
// Schritt 4: Pixel lesen
|
|
||||||
if (fread(image->buffer, sizeof(unsigned char), totalPixels, file) != totalPixels) {
|
|
||||||
free(image->buffer); // Aufräumen!
|
|
||||||
return 0; // Fehler beim Lesen
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schritt 5: Label lesen
|
|
||||||
if (fread(label, sizeof(unsigned char), 1, file) != 1) {
|
|
||||||
free(image->buffer); // Aufräumen!
|
|
||||||
return 0; // Fehler beim Lesen
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1; // Erfolg!
|
|
||||||
}
|
|
||||||
|
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
|
||||||
{
|
|
||||||
// Schritt 1: Datei öffnen
|
|
||||||
FILE *file = fopen(path, "rb");
|
|
||||||
if (file == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schritt 2: Header prüfen
|
|
||||||
if (!checkFileHeader(file)) {
|
|
||||||
fclose(file);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schritt 3: Dimensionen lesen
|
|
||||||
unsigned short count, width, height;
|
|
||||||
if (!readDimensions(file, &count, &width, &height)) {
|
|
||||||
fclose(file);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schritt 4: Speicher für die Serie allokieren
|
|
||||||
GrayScaleImageSeries *series = (GrayScaleImageSeries *)malloc(sizeof(GrayScaleImageSeries));
|
|
||||||
if (series == NULL) {
|
|
||||||
fclose(file);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schritt 5: Speicher für das images-Array allokieren
|
|
||||||
series->images = (GrayScaleImage *)malloc(count * sizeof(GrayScaleImage));
|
|
||||||
if (series->images == NULL) {
|
|
||||||
free(series);
|
|
||||||
fclose(file);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schritt 6: Speicher für das labels-Array allokieren
|
|
||||||
series->labels = (unsigned char *)malloc(count * sizeof(unsigned char));
|
|
||||||
if (series->labels == NULL) {
|
|
||||||
free(series->images);
|
|
||||||
free(series);
|
|
||||||
fclose(file);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schritt 7: count setzen
|
|
||||||
series->count = count;
|
|
||||||
|
|
||||||
// Schritt 8: Alle Bilder in einer Schleife einlesen
|
|
||||||
for (int i = 0; i < count; i++) {
|
|
||||||
if (!readSingleImage(file, &series->images[i], &series->labels[i], width, height)) {
|
|
||||||
// Bei Fehler: Aufräumen!
|
|
||||||
for (int j = 0; j < i; j++) {
|
|
||||||
free(series->images[j].buffer);
|
|
||||||
}
|
|
||||||
free(series->images);
|
|
||||||
free(series->labels);
|
|
||||||
free(series);
|
|
||||||
fclose(file);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schritt 9: Datei schließen
|
|
||||||
fclose(file);
|
|
||||||
|
|
||||||
// Schritt 10: Fertige Serie zurückgeben
|
|
||||||
return series;
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
|
||||||
{
|
|
||||||
// Schritt 0: Prüfen ob series überhaupt existiert
|
|
||||||
if (series == NULL) {
|
|
||||||
return; // Nichts zu tun
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schritt 1: Alle Pixel-Buffer freigeben (für jedes Bild)
|
|
||||||
if (series->images != NULL) {
|
|
||||||
for (int i = 0; i < series->count; i++) {
|
|
||||||
if (series->images[i].buffer != NULL) {
|
|
||||||
free(series->images[i].buffer); // ← Buffer von Bild i freigeben
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schritt 2: Das images-Array freigeben
|
|
||||||
if (series->images != NULL) {
|
|
||||||
free(series->images);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schritt 3: Das labels-Array freigeben
|
|
||||||
if (series->labels != NULL) {
|
|
||||||
free(series->labels);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schritt 4: Die Serie-Struktur selbst freigeben
|
|
||||||
free(series);
|
|
||||||
}
|
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
#ifndef IMAGEINPUT_H
|
|
||||||
#define IMAGEINPUT_H
|
|
||||||
|
|
||||||
typedef unsigned char GrayScalePixelType;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
GrayScalePixelType *buffer; // Breite in Pixeln
|
|
||||||
unsigned int width; // Höhe in Pixeln
|
|
||||||
unsigned int height; // Die Pixelwerte (0-255)
|
|
||||||
} GrayScaleImage; // EIN Bild
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
GrayScaleImage *images; // Array von Bildern
|
|
||||||
unsigned char *labels; // Array von Labels (welche Ziffer?)
|
|
||||||
unsigned int count; // Wie viele Bilder ?
|
|
||||||
} GrayScaleImageSeries; // Sammlung der Bilder
|
|
||||||
|
|
||||||
GrayScaleImageSeries *readImages(const char *path);
|
|
||||||
void clearSeries(GrayScaleImageSeries *series);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,173 +0,0 @@
|
|||||||
#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);
|
|
||||||
// Prüfe auf ungültige Dimensionen
|
|
||||||
if (rows == 0 || cols == 0) {
|
|
||||||
m.rows = 0;
|
|
||||||
m.cols = 0;
|
|
||||||
m.buffer = NULL;
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Speicher freigeben falls vorhanden
|
|
||||||
if (matrix->buffer != NULL) {
|
|
||||||
free(matrix->buffer);
|
|
||||||
matrix->buffer = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Dimensionen zurücksetzen
|
|
||||||
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, "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)
|
|
||||||
{
|
|
||||||
Matrix result;
|
|
||||||
|
|
||||||
// Fall 1: Normale elementweise Addition (gleiche Dimensionen)
|
|
||||||
if (matrix1.rows == matrix2.rows && matrix1.cols == matrix2.cols) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fall 2: Broadcasting - matrix2 ist Spaltenvektor (cols=1)
|
|
||||||
else if (matrix1.rows == matrix2.rows && matrix2.cols == 1) {
|
|
||||||
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++) {
|
|
||||||
// matrix2 hat nur 1 Spalte (Index 0), wird über alle Spalten verteilt
|
|
||||||
setMatrixAt(
|
|
||||||
getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0),
|
|
||||||
result,
|
|
||||||
i, j
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fall 3: Broadcasting - matrix1 ist Spaltenvektor (cols=1)
|
|
||||||
else if (matrix2.rows == matrix1.rows && matrix1.cols == 1) {
|
|
||||||
result = createMatrix(matrix2.rows, matrix2.cols);
|
|
||||||
if (result.buffer == NULL) {
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < matrix2.rows; i++) {
|
|
||||||
for (unsigned int j = 0; j < matrix2.cols; j++) {
|
|
||||||
// matrix1 hat nur 1 Spalte (Index 0), wird über alle Spalten verteilt
|
|
||||||
setMatrixAt(
|
|
||||||
getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j),
|
|
||||||
result,
|
|
||||||
i, j
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fall 4: Ungültige Dimensionen
|
|
||||||
else {
|
|
||||||
fprintf(stderr, "Error: Matrix dimensions do not match for addition.\n");
|
|
||||||
Matrix empty = {0, 0, NULL};
|
|
||||||
return empty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user