matrix: Typ definiert, Compilerfehler behoben, add/multiply korrigiert

This commit is contained in:
Simon committed 2026-11-12 10:34:14 +01:00
1 parent b01d1febf4
commit e4a750c649
2 files changed
+25 -20

No files matched your search

+19 -19
View File
@@ -1,16 +1,13 @@
#include <stdlib.h>
#include <string.h>
#include "matrix'
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "matrix.h"
// TODO Matrix-Funktionen implementieren
Matrix createMatrix(unsigned int rows, unsigned int cols)
{
Matrix matrix;
if (rows == 0 || cols == 0) { //kein Speicher freigegeben, wenn die Matrix die 0. Dimension hat
matrix.rows = 0;
matrix.cols = 0;
@@ -45,22 +42,22 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{
if (matrix.buffer == NULL) || rowIdx >= matrix.rows || colIdx >= matrix.cols
return UNDEFINED_MATRIX_VALUE;
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
return UNDEFINED_MATRIX_VALUE;
return matrix.buffer[rowIdx * matrix.cols + colIdx];
}
static int istSkalar(constMatrix m) {
static int istSkalar (const Matrix m) {
return m.rows == 1 && m.cols == 1;
}
static int addKompatibel (const Matrix a, const Matrix b) {
int gleicheZeilen = (a.rows == b.rows);
int gleicheSpalten = (a.cols == b.cols);
static int addKompatibel(const Matrix a, const Matrix b) {
int gleicheZeilen = (a.rows == b.rows);
int gleicheSpalten = (a.cols == b.cols);
return (gleicheZeilen && gleicheSpalten);
|| istSkalar(a) || istSkalar(b);
||(gleicheZeilen && (a.cols == b.cols));
||(gleicheSpalten && (a.rows == b.rows));
return (gleicheZeilen && gleicheSpalten)
|| istSkalar(a) || istSkalar(b)
|| (gleicheZeilen && (a.cols == 1 || b.cols == 1))
|| (gleicheSpalten && (a.rows == 1 || b.rows == 1));
}
static MatrixType wertErweitert(const Matrix m, unsigned int i, unsigned int j) {
@@ -88,21 +85,24 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
return result;
}
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.cols != matrix2.cols)
return createMatrix(0,0);
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
return createMatrix(0, 0);
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
if (result.buffer == NULL)
return createMatrix(0,0);
return createMatrix(0, 0);
for (unsigned int i = 0; i < matrix1.rows; i++) {
for (unsigned int j = 0; j < matrix2.cols; j++) {
MatrixType summe = 0;
for (unsigned int k = 0; k < matrix1.cols; k++) {
summe += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
result.buffer[i * result.cols + j] = summe;
}
result.buffer[i * result.cols + j] = summe;
}
}
return result;
}
+6 -1
View File
@@ -5,7 +5,12 @@
typedef float MatrixType;
// TODO Matrixtyp definieren
typedef struct
{
unsigned int rows;
unsigned int cols;
MatrixType *buffer;
} Matrix;
Matrix createMatrix(unsigned int rows, unsigned int cols);