matrix fertig
This commit is contained in:
parent
87fdfff437
commit
b756b8c648
60
matrix.c
60
matrix.c
@ -1,46 +1,10 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
// TODO Matrix-Funktionen implementieren
|
// TODO Matrix-Funktionen implementieren
|
||||||
|
|
||||||
/*Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
|
||||||
Matrix matrix;
|
|
||||||
matrix.rows = rows;
|
|
||||||
matrix.cols = cols;
|
|
||||||
matrix.buffer = NULL;
|
|
||||||
|
|
||||||
if(rows>0 && cols>0) {
|
|
||||||
matrix.buffer = (MatrixType **)malloc(rows * sizeof(MatrixType *)); // Speicher für Zeiger auf Zeilen
|
|
||||||
if(matrix.buffer == NULL) {
|
|
||||||
matrix.rows = 0;
|
|
||||||
matrix.cols = 0;
|
|
||||||
return matrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
for(int i=0;i<rows;i++) {
|
|
||||||
matrix.buffer[i] = (MatrixType *)malloc(cols * sizeof(MatrixType)); // Speicher für jede Zeile (Spalten)
|
|
||||||
if(matrix.buffer[i] == NULL) { // Wenn fehler, dann freigeben
|
|
||||||
for(int j=0;j<i;j++) {
|
|
||||||
free(matrix.buffer[j]);
|
|
||||||
}
|
|
||||||
free(matrix.buffer);
|
|
||||||
matrix.buffer = NULL;
|
|
||||||
matrix.rows = 0;
|
|
||||||
matrix.cols = 0;
|
|
||||||
return matrix;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for(int i;i<rows;i++){
|
|
||||||
for(int ii;ii<cols;ii++){
|
|
||||||
matrix.buffer[i][ii] = UNDEFINED_MATRIX_VALUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return matrix;
|
|
||||||
}*/
|
|
||||||
Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
Matrix createMatrix(unsigned int rows, unsigned int cols) {
|
||||||
Matrix matrix;
|
Matrix matrix;
|
||||||
matrix.rows = 0;
|
matrix.rows = 0;
|
||||||
@ -94,10 +58,31 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
|
|||||||
if(matrix1.rows==matrix2.rows && matrix1.cols==matrix2.cols){
|
if(matrix1.rows==matrix2.rows && matrix1.cols==matrix2.cols){
|
||||||
output.rows = matrix1.rows;
|
output.rows = matrix1.rows;
|
||||||
output.cols = matrix1.cols;
|
output.cols = matrix1.cols;
|
||||||
|
output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols);
|
||||||
for (int i=0;i<matrix1.rows*matrix1.cols;i++){
|
for (int i=0;i<matrix1.rows*matrix1.cols;i++){
|
||||||
output.buffer[i]= matrix1.buffer[i]+matrix2.buffer[i];
|
output.buffer[i]= matrix1.buffer[i]+matrix2.buffer[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if(matrix1.rows==matrix2.rows && (matrix1.cols>0&&matrix2.cols==1)){
|
||||||
|
output.rows = matrix1.rows;
|
||||||
|
output.cols = matrix1.cols;
|
||||||
|
output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols);
|
||||||
|
for (int i=0;i<matrix2.rows;i++){
|
||||||
|
for (int ii=0;ii<matrix1.cols;ii++){
|
||||||
|
output.buffer[(i*matrix1.cols)+ii]= matrix1.buffer[(i*matrix1.cols)+ii]+matrix2.buffer[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(matrix1.rows==matrix2.rows && (matrix1.cols==1&&matrix2.cols>0)){
|
||||||
|
output.rows = matrix2.rows;
|
||||||
|
output.cols = matrix2.cols;
|
||||||
|
output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols);
|
||||||
|
for (int i=0;i<matrix1.rows;i++){
|
||||||
|
for (int ii=0;ii<matrix2.cols;ii++){
|
||||||
|
output.buffer[(i*matrix2.cols)+ii]= matrix2.buffer[(i*matrix2.cols)+ii]+matrix1.buffer[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,6 +92,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
|
|||||||
if(matrix1.cols==matrix2.rows){
|
if(matrix1.cols==matrix2.rows){
|
||||||
output.rows = matrix1.rows;
|
output.rows = matrix1.rows;
|
||||||
output.cols = matrix2.cols;
|
output.cols = matrix2.cols;
|
||||||
|
output.buffer = malloc(sizeof(MatrixType)*output.rows*output.cols);
|
||||||
memset(output.buffer, 0 ,output.rows*output.cols);
|
memset(output.buffer, 0 ,output.rows*output.cols);
|
||||||
for (int spalte2=0;spalte2<output.cols;spalte2++){
|
for (int spalte2=0;spalte2<output.cols;spalte2++){
|
||||||
for (int zeile1=0;zeile1<matrix1.rows;zeile1++){
|
for (int zeile1=0;zeile1<matrix1.rows;zeile1++){
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user