matrix.c is done and matrix.h too. End of Project

This commit is contained in:
regis37 2025-11-18 17:07:34 +01:00
parent 0fc70f982c
commit eefb9d362e
7 changed files with 99 additions and 21 deletions

View File

@ -2,7 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "unity.h" #include "C:\Git\Versuch 2\unity\unity.h"
#include "imageInput.h" #include "imageInput.h"

View File

@ -1,35 +1,102 @@
#include <stdlib.h>
#include <string.h>
#include "matrix.h" #include "matrix.h"
#include <stdlib.h>
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
if (rows == 0 || cols == 0) {
Matrix m = {0, 0, NULL};
return m;
}
Matrix m;
m.rows = rows;
m.cols = cols;
m.buffer = (MatrixType*)malloc(rows * cols * sizeof(MatrixType));
if (!m.buffer) {
m.rows = m.cols = 0;
}
return m;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (!matrix || !matrix->buffer)
} return;
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) free(matrix->buffer);
{ matrix->buffer = NULL;
matrix->rows = 0;
matrix->cols = 0;
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (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) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
if (rowIdx >= matrix.rows || colIdx >= matrix.cols)
return;
matrix.buffer[rowIdx * matrix.cols + colIdx] = value;
} }
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix m1, const Matrix m2)
{ {
// Broadcasting unterstützt
} if (m1.rows != m2.rows || (m1.cols != m2.cols && m1.cols != 1 && m2.cols != 1)) {
Matrix error = {0, 0, NULL};
return error;
}
unsigned int rows = m1.rows;
unsigned int cols = (m1.cols > m2.cols) ? m1.cols : m2.cols;
Matrix result = createMatrix(rows, cols);
if (!result.buffer)
return result;
for (unsigned int i = 0; i < rows; i++) {
for (unsigned int j = 0; j < cols; j++) {
MatrixType a = (m1.cols == 1) ? m1.buffer[i] : m1.buffer[i * m1.cols + j];
MatrixType b = (m2.cols == 1) ? m2.buffer[i] : m2.buffer[i * m2.cols + j];
result.buffer[i * cols + j] = a + b;
}
}
return result;
}
Matrix multiply(const Matrix m1, const Matrix m2)
{
if (m1.cols != m2.rows) {
Matrix error = {0, 0, NULL};
return error;
}
Matrix result = createMatrix(m1.rows, m2.cols);
if (!result.buffer)
return result;
for (unsigned int i = 0; i < m1.rows; i++) {
for (unsigned int j = 0; j < m2.cols; j++) {
MatrixType sum = 0;
for (unsigned int k = 0; k < m1.cols; k++)
sum += m1.buffer[i * m1.cols + k] * m2.buffer[k * m2.cols + j];
result.buffer[i * m2.cols + j] = sum;
}
}
return result;
}

View File

@ -1,18 +1,29 @@
#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;
// 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);
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx); void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx);
MatrixType getMatrixAt(const 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 add(const Matrix matrix1, const Matrix matrix2);
Matrix multiply(const Matrix matrix1, const Matrix matrix2); Matrix multiply(const Matrix matrix1, const Matrix matrix2);

View File

@ -2,7 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "matrix.h" #include "matrix.h"
#include "unity.h" #include "C:\Git\Versuch 2\unity\unity.h"
void test_createMatrixFailsOnZeroDimensions(void) void test_createMatrixFailsOnZeroDimensions(void)

View File

@ -2,7 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "mnistVisualization.h" #include "mnistVisualization.h"
#include "raylib.h" #include "C:\Git\Versuch 2\raylib\raylib.h"
#define MAX_TEXT_LEN 100 #define MAX_TEXT_LEN 100

View File

@ -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 = {NULL, 0, 0}; Matrix matrix = {0, 0, NULL};
if(count > 0 && images != NULL) if(count > 0 && images != NULL)
{ {

View File

@ -2,7 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include "unity.h" #include "C:\Git\Versuch 2\unity\unity.h"
#include "neuralNetwork.h" #include "neuralNetwork.h"