48 lines
1.7 KiB
C
48 lines
1.7 KiB
C
#include "matrix.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
// TODO Matrix-Funktionen implementieren
|
|
/*typedef struct {
|
|
unsigned int rows; //Zeilen
|
|
unsigned int cols; //Spalten
|
|
MatrixType *buffer; //Zeiger auf Speicherbereich Reihen*Spalten
|
|
} Matrix;*/
|
|
Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
|
MatrixType *buffer =
|
|
malloc(rows * cols * sizeof(MatrixType)); // Speicher reservieren, malloc
|
|
// liefert Zeiger auf Speicher
|
|
Matrix newMatrix = {rows, cols, buffer}; // neue Matrix nach struct
|
|
return newMatrix;
|
|
}
|
|
void clearMatrix(Matrix *matrix) {
|
|
matrix->buffer = UNDEFINED_MATRIX_VALUE;
|
|
matrix->rows = UNDEFINED_MATRIX_VALUE;
|
|
matrix->cols = UNDEFINED_MATRIX_VALUE;
|
|
free((*matrix).buffer); // Speicher freigeben
|
|
}
|
|
void setMatrixAt(MatrixType value, Matrix matrix,
|
|
unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
|
unsigned int colIdx) {
|
|
|
|
matrix.buffer[rowIdx * matrix.cols + colIdx] =
|
|
value; // rowIdx * matrix.cols -> Beginn der Zeile colIdx ->Spalte
|
|
// innerhalb der Zeile
|
|
}
|
|
MatrixType getMatrixAt(const Matrix matrix,
|
|
unsigned int rowIdx, // Kopie der Matrix wird übergeben
|
|
unsigned int colIdx) {
|
|
if (rowIdx >= matrix.rows ||
|
|
colIdx >= matrix.cols) { // Speichergröße nicht überschreiten
|
|
return 0;
|
|
}
|
|
|
|
MatrixType value = matrix.buffer[rowIdx * matrix.cols + colIdx];
|
|
|
|
return value;
|
|
}
|
|
Matrix add(const Matrix matrix1, const Matrix matrix2) {
|
|
// broadcasting
|
|
return matrix1;
|
|
}
|
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2) { return matrix1; }
|