generated from freudenreichan/info2Praktikum-NeuronalesNetz
126 lines
4.8 KiB
C
126 lines
4.8 KiB
C
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "matrix.h"
|
|
|
|
Matrix createMatrix(unsigned int rows, unsigned int cols){
|
|
/*
|
|
unsigned int memory_depth = 0;
|
|
memory_depth = rows * cols; //Meine Speichertiefe ist die Anzahl meiner Spalten mal die Reihen
|
|
|
|
unsigned int memory_reservation = 0;
|
|
memory_reservation = memory_depth * sizeof(MatrixType); //Nimmt die Speichertiefe mal die größe der einzelnen Speicherinhalte (float)
|
|
|
|
Matrix.space = malloc(memory_reservation); //Reserviert den Speicher und gibt den Zeiger auf die erste Speicheradresse dem zeiger space (malloc "merkt" sich die Speichergröße an der adresse space)
|
|
*/
|
|
Matrix matrix; //Matrix ist der Datentyp (im struct bestimmt). wir brauchen hier aber eine lokale variable (!NIX GLOBAL!) daher ist matrix eine Variable vom Typ Matrix.
|
|
|
|
matrix.rows = rows;
|
|
matrix.cols = cols;
|
|
matrix.space = malloc(rows * cols * sizeof(MatrixType));
|
|
|
|
if (matrix.space == NULL) {
|
|
fprintf(stderr, "Error, matrix could not be created...\n");
|
|
matrix.rows = 0;
|
|
matrix.cols = 0;
|
|
}
|
|
|
|
return matrix;
|
|
}
|
|
|
|
|
|
|
|
void clearMatrix(Matrix *matrix){ //*matrix greift auf die einträge im Struct Matrix zu
|
|
|
|
if (matrix == NULL) { //Wenn Matrix schon leer ist, mach nichts
|
|
return;
|
|
}
|
|
|
|
free(matrix->space); //Mit free wird die dynamische Speichergröße (die sich malloc gemerkt hat und mit der adresse von space verknüpft hat) wieder freigegeben
|
|
|
|
matrix->space = NULL; //matrix->space = NULL ist das gleiche wie (*matrix).space = NULL / Setzt den Zeiger auf 0
|
|
matrix->rows = 0; //Wir greifen auf einen zeiger in struct zu deswegen (*matrix)[zeiger auf] .space[variable im struct] !Klammer weil . stärker bindet als *!
|
|
matrix->cols = 0;
|
|
}
|
|
|
|
|
|
|
|
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx){
|
|
|
|
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) { //Gibt fehler bei falschen Idx werten aus / !Arraysyntax beachten, bei 5 rows ist im Array 0 bis 4 deviniert!
|
|
fprintf(stderr, "Error, the value could not be saved to the matrix...\n");
|
|
return;
|
|
}
|
|
|
|
unsigned int memoryspace = 0;
|
|
unsigned int columnlength = matrix.cols;
|
|
|
|
memoryspace = (rowIdx * columnlength)+colIdx; //Erklärung in "getMatrixAt"
|
|
matrix.space[memoryspace] = value;
|
|
}
|
|
|
|
|
|
|
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx){
|
|
|
|
if (rowIdx >= matrix.rows || colIdx >= matrix.cols) { //Gibt fehler bei falschen Idx werten aus / !Arraysyntax beachten, bei 5 rows ist im Array 0 bis 4 deviniert!
|
|
return UNDEFINED_MATRIX_VALUE;
|
|
}
|
|
|
|
unsigned int memoryspace = 0; //variable für den Eintrag an der gesuchten stelle
|
|
unsigned int columnlength = matrix.cols; //maximale länge einer Zeile
|
|
|
|
memoryspace = (rowIdx * columnlength)+colIdx; //Bsp. Matrix = 5 Spalten und 4 Zeilen. Gesucht ist Zeile 3, Spalte 2. In meinem Eindimensionalen Array ist das 12
|
|
//(2*4)+2 = 12 /Ich gehe erst die vollen Zeilen durch, dann die Position in der Zeile
|
|
//!Zeilen und Spalten beginnen bei 0 (Array syntax)!
|
|
return matrix.space[memoryspace]; //Gibt den Matrixwert an der ausgerechnenten Stelle zurück / ![] für Arraywert!
|
|
}
|
|
|
|
|
|
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2){
|
|
|
|
//Matrix erstellen, in der die additionsmatrix gespeichert wird
|
|
unsigned int maxrows = 0;
|
|
unsigned int maxcols = 0;
|
|
|
|
if(matrix1.rows >= matrix2.rows){maxrows = matrix1.rows;}
|
|
//Maximale Anzahlen der Reihen bestimmen
|
|
else {maxrows = matrix2.rows;}
|
|
|
|
if(matrix1.cols >= matrix2.cols){maxcols = matrix1.cols;}
|
|
//Maximale Anzahl der Zeilen bestimmen
|
|
else {maxcols = matrix2.cols;}
|
|
|
|
Matrix summatrix = createMatrix(maxrows, maxcols); //Neue Matrix erstellen
|
|
|
|
|
|
//Checken ob Matrizenen werte haben
|
|
if(matrix1.space == 0 || matrix2.space ==0){
|
|
fprintf(stderr, "Error, addition of matrices was not possible...\n");
|
|
summatrix.rows = 0;
|
|
summatrix.cols = 0;
|
|
}
|
|
|
|
|
|
//Prüfen welches Additionsverfahren genurtz werden soll
|
|
|
|
if((matrix1.cols == 1 || matrix2.cols == 1) && matrix1.cols == matrix2.cols){ //Erkennung ob eine der Matrizen ein Vektor ist -> Broadcasting addition
|
|
|
|
}
|
|
|
|
else{
|
|
for(int i = 0; i > summatrix.rows; i++){
|
|
summatrix.space[i]=
|
|
}
|
|
}
|
|
|
|
return summatrix;
|
|
|
|
}
|
|
|
|
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2){
|
|
|
|
}
|