matrix: Typ definiert, Compilerfehler behoben, add/multiply korrigiert
This commit is contained in:
1 parent
b01d1febf4
commit
e4a750c649
2 files changed
+17
-12
No files matched your search
+11
-11
@@ -1,16 +1,13 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
|
||||||
#include "matrix'
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols)
|
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
|
if (rows == 0 || cols == 0) { //kein Speicher freigegeben, wenn die Matrix die 0. Dimension hat
|
||||||
matrix.rows = 0;
|
matrix.rows = 0;
|
||||||
matrix.cols = 0;
|
matrix.cols = 0;
|
||||||
@@ -45,7 +42,7 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned
|
|||||||
|
|
||||||
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
|
||||||
{
|
{
|
||||||
if (matrix.buffer == NULL) || rowIdx >= matrix.rows || colIdx >= matrix.cols
|
if (matrix.buffer == NULL || rowIdx >= matrix.rows || colIdx >= matrix.cols)
|
||||||
return UNDEFINED_MATRIX_VALUE;
|
return UNDEFINED_MATRIX_VALUE;
|
||||||
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
return matrix.buffer[rowIdx * matrix.cols + colIdx];
|
||||||
}
|
}
|
||||||
@@ -57,10 +54,10 @@ static int addKompatibel (const Matrix a, const Matrix b) {
|
|||||||
int gleicheZeilen = (a.rows == b.rows);
|
int gleicheZeilen = (a.rows == b.rows);
|
||||||
int gleicheSpalten = (a.cols == b.cols);
|
int gleicheSpalten = (a.cols == b.cols);
|
||||||
|
|
||||||
return (gleicheZeilen && gleicheSpalten);
|
return (gleicheZeilen && gleicheSpalten)
|
||||||
|| istSkalar(a) || istSkalar(b);
|
|| istSkalar(a) || istSkalar(b)
|
||||||
||(gleicheZeilen && (a.cols == b.cols));
|
|| (gleicheZeilen && (a.cols == 1 || b.cols == 1))
|
||||||
||(gleicheSpalten && (a.rows == b.rows));
|
|| (gleicheSpalten && (a.rows == 1 || b.rows == 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
static MatrixType wertErweitert(const Matrix m, unsigned int i, unsigned int j) {
|
static MatrixType wertErweitert(const Matrix m, unsigned int i, unsigned int j) {
|
||||||
@@ -90,19 +87,22 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
|
|
||||||
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
||||||
{
|
{
|
||||||
if (matrix1.cols != matrix2.cols)
|
if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.cols != matrix2.rows)
|
||||||
return createMatrix(0, 0);
|
return createMatrix(0, 0);
|
||||||
|
|
||||||
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
Matrix result = createMatrix(matrix1.rows, matrix2.cols);
|
||||||
if (result.buffer == NULL)
|
if (result.buffer == NULL)
|
||||||
return createMatrix(0, 0);
|
return createMatrix(0, 0);
|
||||||
|
|
||||||
for (unsigned int i = 0; i < matrix1.rows; i++) {
|
for (unsigned int i = 0; i < matrix1.rows; i++) {
|
||||||
for (unsigned int j = 0; j < matrix2.cols; j++) {
|
for (unsigned int j = 0; j < matrix2.cols; j++) {
|
||||||
MatrixType summe = 0;
|
MatrixType summe = 0;
|
||||||
for (unsigned int k = 0; k < matrix1.cols; k++) {
|
for (unsigned int k = 0; k < matrix1.cols; k++) {
|
||||||
summe += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j];
|
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;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,12 @@
|
|||||||
|
|
||||||
typedef float MatrixType;
|
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);
|
Matrix createMatrix(unsigned int rows, unsigned int cols);
|
||||||
|
|||||||
Reference in new issue
Block a user