forked from freudenreichan/info2Praktikum-NeuronalesNetz
lauffaehige version, noch haesslich
This commit is contained in:
parent
7aa57191da
commit
0f0f2f19c3
138
imageInput.c
138
imageInput.c
@ -1,29 +1,131 @@
|
|||||||
|
#include "imageInput.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "imageInput.h"
|
|
||||||
|
|
||||||
#define BUFFER_SIZE 100
|
|
||||||
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
#define FILE_HEADER_STRING "__info2_image_file_format__"
|
||||||
|
|
||||||
// TODO Implementieren Sie geeignete Hilfsfunktionen für das Lesen der Bildserie aus einer Datei
|
/* ----------------------------------------------------------
|
||||||
GrayScaleImage readImage()
|
1. Header prüfen
|
||||||
{
|
---------------------------------------------------------- */
|
||||||
|
static int readHeader(FILE *file) {
|
||||||
|
char header[sizeof(FILE_HEADER_STRING)];
|
||||||
|
if (fread(header, 1, sizeof(FILE_HEADER_STRING) - 1, file) !=
|
||||||
|
sizeof(FILE_HEADER_STRING) - 1)
|
||||||
|
return 0;
|
||||||
|
header[sizeof(FILE_HEADER_STRING) - 1] = '\0';
|
||||||
|
return strcmp(header, FILE_HEADER_STRING) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion readImages unter Benutzung Ihrer Hilfsfunktionen
|
/* ----------------------------------------------------------
|
||||||
GrayScaleImageSeries *readImages(const char *path)
|
2. Meta-Daten lesen (unsigned short)
|
||||||
{
|
---------------------------------------------------------- */
|
||||||
GrayScaleImageSeries *series = NULL;
|
static int readMeta(FILE *file, unsigned short *count, unsigned short *width,
|
||||||
FILE *file = fopen("mnist_test.info2","rb");
|
unsigned short *height) {
|
||||||
char headOfFile;
|
if (fread(count, sizeof(unsigned short), 1, file) != 1)
|
||||||
series = malloc();
|
return 0;
|
||||||
return series;
|
if (fread(width, sizeof(unsigned short), 1, file) != 1)
|
||||||
|
return 0;
|
||||||
|
if (fread(height, sizeof(unsigned short), 1, file) != 1)
|
||||||
|
return 0;
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Vervollständigen Sie die Funktion clearSeries, welche eine Bildserie vollständig aus dem Speicher freigibt
|
/* ----------------------------------------------------------
|
||||||
void clearSeries(GrayScaleImageSeries *series)
|
3. Einzelbild lesen
|
||||||
{
|
---------------------------------------------------------- */
|
||||||
|
static int readSingleImage(FILE *file, GrayScaleImage *img,
|
||||||
|
unsigned short width, unsigned short height) {
|
||||||
|
img->width = width;
|
||||||
|
img->height = height;
|
||||||
|
|
||||||
}
|
size_t numPixels = (size_t)width * (size_t)height;
|
||||||
|
img->buffer = malloc(numPixels);
|
||||||
|
if (!img->buffer)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (fread(img->buffer, 1, numPixels, file) != numPixels) {
|
||||||
|
free(img->buffer);
|
||||||
|
img->buffer = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------
|
||||||
|
4. Label lesen
|
||||||
|
---------------------------------------------------------- */
|
||||||
|
static int readLabel(FILE *file, unsigned char *label) {
|
||||||
|
return fread(label, 1, 1, file) == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------
|
||||||
|
5. Komplette Bildserie lesen
|
||||||
|
---------------------------------------------------------- */
|
||||||
|
GrayScaleImageSeries *readImages(const char *path) {
|
||||||
|
FILE *file = fopen(path, "rb");
|
||||||
|
if (!file)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (!readHeader(file)) {
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned short count, width, height;
|
||||||
|
if (!readMeta(file, &count, &width, &height)) {
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
GrayScaleImageSeries *series = malloc(sizeof(GrayScaleImageSeries));
|
||||||
|
if (!series) {
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
series->count = count;
|
||||||
|
series->images = malloc(count * sizeof(GrayScaleImage));
|
||||||
|
series->labels = malloc(count * sizeof(unsigned char));
|
||||||
|
if (!series->images || !series->labels) {
|
||||||
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < count; i++) {
|
||||||
|
if (!readSingleImage(file, &series->images[i], width, height) ||
|
||||||
|
!readLabel(file, &series->labels[i])) {
|
||||||
|
// Aufräumen bei Fehler
|
||||||
|
for (unsigned int j = 0; j < i; j++) {
|
||||||
|
free(series->images[j].buffer);
|
||||||
|
}
|
||||||
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
fclose(file);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose(file);
|
||||||
|
return series;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ----------------------------------------------------------
|
||||||
|
6. Speicher komplett freigeben
|
||||||
|
---------------------------------------------------------- */
|
||||||
|
void clearSeries(GrayScaleImageSeries *series) {
|
||||||
|
if (!series)
|
||||||
|
return;
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < series->count; i++) {
|
||||||
|
free(series->images[i].buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(series->images);
|
||||||
|
free(series->labels);
|
||||||
|
free(series);
|
||||||
|
}
|
||||||
|
|||||||
@ -1,143 +1,204 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include "unity.h"
|
|
||||||
#include "imageInput.h"
|
#include "imageInput.h"
|
||||||
|
#include "unity.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------
|
||||||
|
Hilfsfunktion: Testdatei vorbereiten
|
||||||
|
--------------------------------------------------------- */
|
||||||
|
static void prepareImageFile(const char *path, unsigned int width,
|
||||||
|
unsigned int height, unsigned int numberOfImages,
|
||||||
|
unsigned char label) {
|
||||||
|
FILE *file = fopen(path, "wb");
|
||||||
|
if (!file)
|
||||||
|
return;
|
||||||
|
|
||||||
static void prepareImageFile(const char *path, unsigned short int width, unsigned short int height, unsigned int short numberOfImages, unsigned char label)
|
// Header
|
||||||
{
|
const char *fileTag = "__info2_image_file_format__";
|
||||||
FILE *file = fopen(path, "wb");
|
fwrite(fileTag, 1, strlen(fileTag), file);
|
||||||
|
|
||||||
if(file != NULL)
|
// Meta-Daten als unsigned short
|
||||||
{
|
unsigned short n = (unsigned short)numberOfImages;
|
||||||
const char *fileTag = "__info2_image_file_format__";
|
unsigned short w = (unsigned short)width;
|
||||||
GrayScalePixelType *zeroBuffer = (GrayScalePixelType *)calloc(numberOfImages * width * height, sizeof(GrayScalePixelType));
|
unsigned short h = (unsigned short)height;
|
||||||
|
fwrite(&n, sizeof(unsigned short), 1, file);
|
||||||
|
fwrite(&w, sizeof(unsigned short), 1, file);
|
||||||
|
fwrite(&h, sizeof(unsigned short), 1, file);
|
||||||
|
|
||||||
if(zeroBuffer != NULL)
|
// Pixelbuffer
|
||||||
{
|
GrayScalePixelType *buffer =
|
||||||
fwrite(fileTag, sizeof(fileTag[0]), strlen(fileTag), file);
|
calloc(width * height, sizeof(GrayScalePixelType));
|
||||||
fwrite(&numberOfImages, sizeof(numberOfImages), 1, file);
|
if (!buffer) {
|
||||||
fwrite(&width, sizeof(width), 1, file);
|
fclose(file);
|
||||||
fwrite(&height, sizeof(height), 1, file);
|
return;
|
||||||
|
}
|
||||||
|
for (unsigned int i = 0; i < width * height; i++)
|
||||||
|
buffer[i] = (GrayScalePixelType)i;
|
||||||
|
|
||||||
for(int i = 0; i < numberOfImages; i++)
|
// Jedes Bild schreiben: Pixel + Label
|
||||||
{
|
for (unsigned int img = 0; img < numberOfImages; img++) {
|
||||||
fwrite(zeroBuffer, sizeof(GrayScalePixelType), width * height, file);
|
fwrite(buffer, sizeof(GrayScalePixelType), width * height, file);
|
||||||
fwrite(&label, sizeof(unsigned char), 1, file);
|
fwrite(&label, sizeof(unsigned char), 1, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(zeroBuffer);
|
free(buffer);
|
||||||
}
|
fclose(file);
|
||||||
|
|
||||||
fclose(file);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------
|
||||||
|
Unit Tests
|
||||||
|
--------------------------------------------------------- */
|
||||||
|
|
||||||
void test_readImagesReturnsCorrectNumberOfImages(void)
|
void test_readImagesReturnsCorrectNumberOfImages(void) {
|
||||||
{
|
GrayScaleImageSeries *series = NULL;
|
||||||
GrayScaleImageSeries *series = NULL;
|
const unsigned int expectedNumberOfImages = 2;
|
||||||
const unsigned short expectedNumberOfImages = 2;
|
const char *path = "testFile.info2";
|
||||||
const char *path = "testFile.info2";
|
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
|
||||||
prepareImageFile(path, 8, 8, expectedNumberOfImages, 1);
|
series = readImages(path);
|
||||||
series = readImages(path);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_EQUAL_UINT(expectedNumberOfImages, series->count);
|
||||||
TEST_ASSERT_EQUAL_UINT16(expectedNumberOfImages, series->count);
|
clearSeries(series);
|
||||||
clearSeries(series);
|
remove(path);
|
||||||
remove(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_readImagesReturnsCorrectImageWidth(void)
|
void test_readImagesReturnsCorrectImageWidth(void) {
|
||||||
{
|
GrayScaleImageSeries *series = NULL;
|
||||||
GrayScaleImageSeries *series = NULL;
|
const unsigned int expectedWidth = 10;
|
||||||
const unsigned short expectedWidth = 10;
|
const char *path = "testFile.info2";
|
||||||
const char *path = "testFile.info2";
|
prepareImageFile(path, expectedWidth, 8, 2, 1);
|
||||||
prepareImageFile(path, expectedWidth, 8, 2, 1);
|
series = readImages(path);
|
||||||
series = readImages(path);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
TEST_ASSERT_NOT_NULL(series->images);
|
TEST_ASSERT_EQUAL_UINT(2, series->count);
|
||||||
TEST_ASSERT_EQUAL_UINT16(2, series->count);
|
TEST_ASSERT_EQUAL_UINT(expectedWidth, series->images[0].width);
|
||||||
TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[0].width);
|
TEST_ASSERT_EQUAL_UINT(expectedWidth, series->images[1].width);
|
||||||
TEST_ASSERT_EQUAL_UINT16(expectedWidth, series->images[1].width);
|
clearSeries(series);
|
||||||
clearSeries(series);
|
remove(path);
|
||||||
remove(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_readImagesReturnsCorrectImageHeight(void)
|
void test_readImagesReturnsCorrectImageHeight(void) {
|
||||||
{
|
GrayScaleImageSeries *series = NULL;
|
||||||
GrayScaleImageSeries *series = NULL;
|
const unsigned int expectedHeight = 10;
|
||||||
const unsigned short expectedHeight = 10;
|
const char *path = "testFile.info2";
|
||||||
const char *path = "testFile.info2";
|
prepareImageFile(path, 8, expectedHeight, 2, 1);
|
||||||
prepareImageFile(path, 8, expectedHeight, 2, 1);
|
series = readImages(path);
|
||||||
series = readImages(path);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
TEST_ASSERT_NOT_NULL(series->images);
|
TEST_ASSERT_EQUAL_UINT(2, series->count);
|
||||||
TEST_ASSERT_EQUAL_UINT16(2, series->count);
|
TEST_ASSERT_EQUAL_UINT(expectedHeight, series->images[0].height);
|
||||||
TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[0].height);
|
TEST_ASSERT_EQUAL_UINT(expectedHeight, series->images[1].height);
|
||||||
TEST_ASSERT_EQUAL_UINT16(expectedHeight, series->images[1].height);
|
clearSeries(series);
|
||||||
clearSeries(series);
|
remove(path);
|
||||||
remove(path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_readImagesReturnsCorrectLabels(void)
|
void test_readImagesReturnsCorrectLabels(void) {
|
||||||
{
|
const unsigned char expectedLabel = 15;
|
||||||
const unsigned char expectedLabel = 15;
|
|
||||||
|
|
||||||
GrayScaleImageSeries *series = NULL;
|
GrayScaleImageSeries *series = NULL;
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
prepareImageFile(path, 8, 8, 2, expectedLabel);
|
prepareImageFile(path, 8, 8, 2, expectedLabel);
|
||||||
series = readImages(path);
|
series = readImages(path);
|
||||||
TEST_ASSERT_NOT_NULL(series);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NOT_NULL(series->labels);
|
TEST_ASSERT_NOT_NULL(series->labels);
|
||||||
TEST_ASSERT_EQUAL_UINT16(2, series->count);
|
TEST_ASSERT_EQUAL_UINT(2, series->count);
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
TEST_ASSERT_EQUAL_UINT8(expectedLabel, series->labels[i]);
|
TEST_ASSERT_EQUAL_UINT8(expectedLabel, series->labels[i]);
|
||||||
}
|
}
|
||||||
clearSeries(series);
|
clearSeries(series);
|
||||||
remove(path);
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_readImagesReturnsNullOnNotExistingPath(void)
|
void test_readImagesReturnsNullOnNotExistingPath(void) {
|
||||||
{
|
const char *path = "testFile.txt";
|
||||||
const char *path = "testFile.txt";
|
remove(path);
|
||||||
remove(path);
|
TEST_ASSERT_NULL(readImages(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_readImagesFailsOnWrongFileTag(void) {
|
||||||
|
const char *path = "testFile.info2";
|
||||||
|
FILE *file = fopen(path, "w");
|
||||||
|
if (file != NULL) {
|
||||||
|
fprintf(file, "some_tag ");
|
||||||
|
fclose(file);
|
||||||
TEST_ASSERT_NULL(readImages(path));
|
TEST_ASSERT_NULL(readImages(path));
|
||||||
|
}
|
||||||
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_readImagesFailsOnWrongFileTag(void)
|
void test_read_GrayScale_Pixel(void) {
|
||||||
{
|
GrayScaleImageSeries *series = NULL;
|
||||||
const char *path = "testFile.info2";
|
const char *path = "testFile.info2";
|
||||||
FILE *file = fopen(path, "w");
|
|
||||||
if(file != NULL)
|
prepareImageFile(path, 8, 8, 1, 1);
|
||||||
{
|
series = readImages(path);
|
||||||
fprintf(file, "some_tag ");
|
|
||||||
fclose(file);
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
TEST_ASSERT_NULL(readImages(path));
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
}
|
TEST_ASSERT_EQUAL_UINT(1, series->count);
|
||||||
remove(path);
|
|
||||||
|
for (int i = 0; i < (8 * 8); i++) {
|
||||||
|
TEST_ASSERT_EQUAL_UINT8((GrayScalePixelType)i, series->images[0].buffer[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
clearSeries(series);
|
||||||
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setUp(void) {
|
/* ---------------------------------------------------------
|
||||||
// Falls notwendig, kann hier Vorbereitungsarbeit gemacht werden
|
Optional: Mehrere Bilder gleichzeitig testen
|
||||||
|
--------------------------------------------------------- */
|
||||||
|
|
||||||
|
void test_readImagesMultipleImagesContent(void) {
|
||||||
|
GrayScaleImageSeries *series = NULL;
|
||||||
|
const char *path = "testFile.info2";
|
||||||
|
const unsigned int numberOfImages = 3;
|
||||||
|
const unsigned int width = 4;
|
||||||
|
const unsigned int height = 4;
|
||||||
|
const unsigned char label = 7;
|
||||||
|
|
||||||
|
prepareImageFile(path, width, height, numberOfImages, label);
|
||||||
|
|
||||||
|
series = readImages(path);
|
||||||
|
TEST_ASSERT_NOT_NULL(series);
|
||||||
|
TEST_ASSERT_NOT_NULL(series->images);
|
||||||
|
TEST_ASSERT_NOT_NULL(series->labels);
|
||||||
|
TEST_ASSERT_EQUAL_UINT(numberOfImages, series->count);
|
||||||
|
|
||||||
|
for (unsigned int img = 0; img < numberOfImages; img++) {
|
||||||
|
for (unsigned int i = 0; i < width * height; i++)
|
||||||
|
TEST_ASSERT_EQUAL_UINT8((GrayScalePixelType)i,
|
||||||
|
series->images[img].buffer[i]);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(label, series->labels[img]);
|
||||||
|
}
|
||||||
|
|
||||||
|
clearSeries(series);
|
||||||
|
remove(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tearDown(void) {
|
/* ---------------------------------------------------------
|
||||||
// Hier kann Bereinigungsarbeit nach jedem Test durchgeführt werden
|
Setup / Teardown
|
||||||
|
--------------------------------------------------------- */
|
||||||
|
void setUp(void) {}
|
||||||
|
void tearDown(void) {}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------
|
||||||
|
main()
|
||||||
|
--------------------------------------------------------- */
|
||||||
|
int main(void) {
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
printf("\n============================\nImage input "
|
||||||
|
"tests\n============================\n");
|
||||||
|
|
||||||
|
RUN_TEST(test_readImagesReturnsCorrectNumberOfImages);
|
||||||
|
RUN_TEST(test_readImagesReturnsCorrectImageWidth);
|
||||||
|
RUN_TEST(test_readImagesReturnsCorrectImageHeight);
|
||||||
|
RUN_TEST(test_readImagesReturnsCorrectLabels);
|
||||||
|
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
|
||||||
|
RUN_TEST(test_readImagesFailsOnWrongFileTag);
|
||||||
|
RUN_TEST(test_read_GrayScale_Pixel);
|
||||||
|
RUN_TEST(test_readImagesMultipleImagesContent);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
|
||||||
{
|
|
||||||
UNITY_BEGIN();
|
|
||||||
|
|
||||||
printf("\n============================\nImage input tests\n============================\n");
|
|
||||||
RUN_TEST(test_readImagesReturnsCorrectNumberOfImages);
|
|
||||||
RUN_TEST(test_readImagesReturnsCorrectImageWidth);
|
|
||||||
RUN_TEST(test_readImagesReturnsCorrectImageHeight);
|
|
||||||
RUN_TEST(test_readImagesReturnsCorrectLabels);
|
|
||||||
RUN_TEST(test_readImagesReturnsNullOnNotExistingPath);
|
|
||||||
RUN_TEST(test_readImagesFailsOnWrongFileTag);
|
|
||||||
|
|
||||||
return UNITY_END();
|
|
||||||
}
|
|
||||||
97
matrix.c
97
matrix.c
@ -10,31 +10,45 @@
|
|||||||
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 matrix;
|
||||||
Matrix errorMatrix = {0, 0, NULL};
|
Matrix errorMatrix = {0, 0, NULL};
|
||||||
if (rows == 0 || cols == 0) {
|
if (rows == 0 || cols == 0) {
|
||||||
|
|
||||||
return errorMatrix;
|
return errorMatrix;
|
||||||
}
|
}
|
||||||
MatrixType *buffer =
|
matrix.rows = rows;
|
||||||
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc
|
matrix.cols = cols;
|
||||||
// liefert Zeiger auf Speicher
|
|
||||||
Matrix newMatrix = {rows, cols, buffer}; // neue Matrix nach struct
|
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
|
||||||
return newMatrix;
|
if (matrix.buffer == NULL) {
|
||||||
|
matrix.rows = 0;
|
||||||
|
matrix.cols = 0;
|
||||||
|
return matrix;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < rows; i++) {
|
||||||
|
for (int j = 0; j < cols; j++) {
|
||||||
|
matrix.buffer[i * matrix.cols + j] = UNDEFINED_MATRIX_VALUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matrix;
|
||||||
}
|
}
|
||||||
void clearMatrix(Matrix *matrix) {
|
void clearMatrix(Matrix *matrix) {
|
||||||
matrix->buffer = UNDEFINED_MATRIX_VALUE;
|
|
||||||
matrix->rows = UNDEFINED_MATRIX_VALUE;
|
if (matrix->buffer != NULL) {
|
||||||
matrix->cols = UNDEFINED_MATRIX_VALUE;
|
free((*matrix).buffer);
|
||||||
free((*matrix).buffer); // Speicher freigeben
|
matrix->buffer = NULL;
|
||||||
|
}
|
||||||
|
matrix->rows = 0;
|
||||||
|
matrix->cols = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
|
||||||
if (rowIdx >= matrix.rows ||
|
if (rowIdx >= matrix.rows || colIdx >= matrix.cols ||
|
||||||
colIdx >= matrix.cols) { // Speichergröße nicht überschreiten
|
matrix.buffer == NULL) { // Speichergröße nicht überschreiten
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -45,9 +59,9 @@ void setMatrixAt(const MatrixType value, Matrix matrix,
|
|||||||
MatrixType getMatrixAt(const Matrix matrix,
|
MatrixType getMatrixAt(const Matrix matrix,
|
||||||
unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
||||||
unsigned int colIdx) {
|
unsigned int colIdx) {
|
||||||
if (rowIdx >= matrix.rows ||
|
if (rowIdx >= matrix.rows || colIdx >= matrix.cols ||
|
||||||
colIdx >= matrix.cols) { // Speichergröße nicht überschreiten
|
matrix.buffer == NULL) { // Speichergröße nicht überschreiten
|
||||||
return 0;
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
MatrixType value = matrix.buffer[rowIdx * matrix.cols + colIdx];
|
MatrixType value = matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
@ -103,6 +117,9 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
|||||||
|
|
||||||
{
|
{
|
||||||
Matrix result = createMatrix(rows1, cols1); // Speicher reservieren
|
Matrix result = createMatrix(rows1, cols1); // Speicher reservieren
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return (Matrix){0, 0, NULL};
|
||||||
|
}
|
||||||
|
|
||||||
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
|
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
|
||||||
|
|
||||||
@ -120,16 +137,22 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
|||||||
if (cols1 == 1) {
|
if (cols1 == 1) {
|
||||||
|
|
||||||
Matrix result = createMatrix(rows2, cols2);
|
Matrix result = createMatrix(rows2, cols2);
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return (Matrix){0, 0, NULL};
|
||||||
|
}
|
||||||
|
|
||||||
Matrix copy1 = broadCastCols(matrix1, rows2, cols2);
|
Matrix copy1 = broadCastCols(matrix1, rows2, cols2);
|
||||||
|
if (!copy1.buffer) {
|
||||||
for (int i = 0; i < (rows2 * cols2); i++) { // addieren
|
clearMatrix(&result);
|
||||||
|
return (Matrix){0, 0, NULL};
|
||||||
result.buffer[i] =
|
|
||||||
(copy1.buffer[i] +
|
|
||||||
matrix2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
|
||||||
// Startadresse + (i * sizeof(MatrixType))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < rows2 * cols2; i++) {
|
||||||
|
result.buffer[i] = copy1.buffer[i] + matrix2.buffer[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* freigeben, weil nicht mehr benötigt */
|
||||||
|
clearMatrix(©1);
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
// add und return
|
// add und return
|
||||||
@ -137,19 +160,22 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
|||||||
} else if (cols2 == 1) {
|
} else if (cols2 == 1) {
|
||||||
|
|
||||||
Matrix result = createMatrix(rows1, cols1);
|
Matrix result = createMatrix(rows1, cols1);
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
Matrix error = {0, 0, NULL};
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Matrix2 hat nur eine Spalte -> horizontal broadcasten
|
||||||
Matrix copy2 = broadCastCols(matrix2, rows1, cols1);
|
Matrix copy2 = broadCastCols(matrix2, rows1, cols1);
|
||||||
|
|
||||||
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
|
for (unsigned int i = 0; i < rows1 * cols1; i++) {
|
||||||
|
result.buffer[i] = matrix1.buffer[i] + copy2.buffer[i];
|
||||||
result.buffer[i] =
|
|
||||||
(matrix1.buffer[i] +
|
|
||||||
copy2.buffer[i]); // buffer[i] ⇔ *(buffer + i) Adresse =
|
|
||||||
// Startadresse + (i * sizeof(MatrixType))
|
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
// add und return
|
|
||||||
|
|
||||||
|
// Optional: Speicher von copy2 freigeben
|
||||||
|
clearMatrix(©2);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else {
|
||||||
@ -167,6 +193,9 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
|||||||
if (rows1 == 1) {
|
if (rows1 == 1) {
|
||||||
|
|
||||||
Matrix result = createMatrix(rows2, cols2);
|
Matrix result = createMatrix(rows2, cols2);
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return (Matrix){0, 0, NULL};
|
||||||
|
}
|
||||||
|
|
||||||
Matrix copy1 = broadCastRows(matrix1, rows2, cols2);
|
Matrix copy1 = broadCastRows(matrix1, rows2, cols2);
|
||||||
|
|
||||||
@ -184,8 +213,12 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
|||||||
} else if (rows2 == 1) {
|
} else if (rows2 == 1) {
|
||||||
|
|
||||||
Matrix result = createMatrix(rows1, cols1);
|
Matrix result = createMatrix(rows1, cols1);
|
||||||
|
if (result.buffer == NULL) {
|
||||||
|
return (Matrix){0, 0, NULL};
|
||||||
|
}
|
||||||
|
|
||||||
|
Matrix copy2 = broadCastRows(matrix2, rows1, cols1);
|
||||||
|
|
||||||
Matrix copy2 = broadCastCols(matrix2, rows1, cols1);
|
|
||||||
// add und return
|
// add und return
|
||||||
|
|
||||||
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
|
for (int i = 0; i < (rows1 * cols1); i++) { // addieren
|
||||||
@ -242,4 +275,4 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) {
|
|||||||
Matrix errorMatrix = {0, 0, NULL};
|
Matrix errorMatrix = {0, 0, NULL};
|
||||||
return errorMatrix;
|
return errorMatrix;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user