Initial content commit
This commit is contained in:
8
c_and_computergraphics/Einfuehrung_CG_8/Matrix_Aufgabe/.idea/.gitignore
generated
vendored
Normal file
8
c_and_computergraphics/Einfuehrung_CG_8/Matrix_Aufgabe/.idea/.gitignore
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.21)
|
||||
project(Matrix_Aufgabe C)
|
||||
|
||||
set(CMAKE_C_STANDARD 17)
|
||||
|
||||
add_executable(Matrix_Aufgabe main.c matrix/matrix.h matrix/matrix.c)
|
@ -0,0 +1,87 @@
|
||||
#include <stdio.h>
|
||||
|
||||
#include "matrix/matrix.h"
|
||||
|
||||
void print_matrix(matrix *m);
|
||||
|
||||
void add_matrix();
|
||||
void multiply_matrix_with_scalar();
|
||||
void multiply_matrices();
|
||||
|
||||
int main()
|
||||
{
|
||||
add_matrix();
|
||||
printf("\n");
|
||||
multiply_matrix_with_scalar();
|
||||
printf("\n");
|
||||
multiply_matrices();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void add_matrix()
|
||||
{
|
||||
m_type type = {2, 2};
|
||||
float v[2][2] = {
|
||||
{1, 2},
|
||||
{3, 4}
|
||||
};
|
||||
matrix *m1 = new_matrix_with_values(type, v);
|
||||
matrix *m2 = new_matrix_with_values(type, v);
|
||||
|
||||
matrix *m = add(m1, m2);
|
||||
|
||||
print_matrix(m);
|
||||
|
||||
free_matrix(m1);
|
||||
free_matrix(m2);
|
||||
free_matrix(m);
|
||||
}
|
||||
|
||||
void multiply_matrix_with_scalar()
|
||||
{
|
||||
m_type type = {2, 2};
|
||||
float v[2][2] = {
|
||||
{1, 2},
|
||||
{3, 4}
|
||||
};
|
||||
matrix *m = new_matrix_with_values(type, v);
|
||||
|
||||
m = multiply_with_scalar(0.5f, m);
|
||||
|
||||
print_matrix(m);
|
||||
|
||||
free_matrix(m);
|
||||
}
|
||||
|
||||
void multiply_matrices()
|
||||
{
|
||||
m_type type = {2, 2};
|
||||
float v[2][2] = {
|
||||
{1, 2},
|
||||
{3, 4}
|
||||
};
|
||||
matrix *m1 = new_matrix_with_values(type, v);
|
||||
matrix *m2 = new_matrix_with_values(type, v);
|
||||
|
||||
matrix *m = multiply(m1, m2);
|
||||
|
||||
print_matrix(m);
|
||||
|
||||
free_matrix(m1);
|
||||
free_matrix(m2);
|
||||
free_matrix(m);
|
||||
}
|
||||
|
||||
void print_matrix(matrix *m)
|
||||
{
|
||||
for (int i = 0; i < m->type.n; i++)
|
||||
{
|
||||
printf("[");
|
||||
for (int j = 0; j < m->type.m; j++)
|
||||
{
|
||||
printf(" %6.3f ", m->values[i][j]);
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
#include "matrix.h"
|
||||
#include <stdio.h>
|
||||
|
||||
matrix *new_matrix(m_type type)
|
||||
{
|
||||
matrix *m = malloc(sizeof(matrix));
|
||||
|
||||
m->type = type;
|
||||
m->values = malloc(sizeof(float *) * type.n);
|
||||
|
||||
for (int i = 0; i < m->type.n; i++)
|
||||
{
|
||||
(m->values)[i] = malloc(sizeof(float) * type.m);
|
||||
}
|
||||
|
||||
for (int i = 0; i < m->type.n; i++)
|
||||
{
|
||||
for (int j = 0; j < m->type.m; j++)
|
||||
{
|
||||
m->values[i][j] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
matrix *new_matrix_with_values(m_type type, float v[type.n][type.m])
|
||||
{
|
||||
matrix *m = new_matrix(type);
|
||||
|
||||
for (int i = 0; i < m->type.n; i++)
|
||||
{
|
||||
for (int j = 0; j < m->type.m; j++)
|
||||
{
|
||||
m->values[i][j] = v[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
void free_matrix(matrix *m)
|
||||
{
|
||||
for (int i = 0; i < m->type.n; i++)
|
||||
{
|
||||
free((m->values)[i]);
|
||||
}
|
||||
|
||||
free(m->values);
|
||||
free(m);
|
||||
}
|
||||
|
||||
matrix *add(matrix *m1, matrix *m2)
|
||||
{
|
||||
matrix *m = new_matrix(m1->type);
|
||||
|
||||
for (int i = 0; i < m->type.n; i++)
|
||||
{
|
||||
for (int j = 0; j < m->type.m; j++)
|
||||
{
|
||||
m->values[i][j] = m1->values[i][j] + m2->values[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
matrix *multiply_with_scalar(float c, matrix *m)
|
||||
{
|
||||
for (int i = 0; i < m->type.n; i++)
|
||||
{
|
||||
for (int j = 0; j < m->type.m; j++)
|
||||
{
|
||||
m->values[i][j] = c * m->values[i][j] ;
|
||||
}
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
matrix *multiply(matrix *m1, matrix *m2)
|
||||
{
|
||||
if (m1->type.m != m2->type.n) return NULL;
|
||||
|
||||
m_type type = {m1->type.n, m2->type.m};
|
||||
matrix *m = new_matrix(type);
|
||||
|
||||
for (int i = 0; i < type.n; i++)
|
||||
{
|
||||
for (int j = 0; j < type.m; j++)
|
||||
{
|
||||
for (int k = 0; k < m1->type.m; k++)
|
||||
{
|
||||
m->values[i][j] += m1->values[i][k] * m2->values[k][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return m;
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct m_type {
|
||||
int n, m;
|
||||
} m_type;
|
||||
|
||||
typedef struct matrix {
|
||||
m_type type;
|
||||
float **values;
|
||||
} matrix;
|
||||
|
||||
matrix *new_matrix(m_type type);
|
||||
matrix *new_matrix_with_values(m_type type, float v[type.n][type.m]);
|
||||
|
||||
void free_matrix(matrix *m);
|
||||
|
||||
matrix *add(matrix *m1, matrix *m2);
|
||||
|
||||
matrix *multiply_with_scalar(float c, matrix *m);
|
||||
|
||||
matrix *multiply(matrix *m1, matrix *m2);
|
Reference in New Issue
Block a user