Initial content commit
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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);
|
14
c_and_computergraphics/Einfuehrung_CG_8/matrix_aufgabe_rust/Cargo.lock
generated
Normal file
14
c_and_computergraphics/Einfuehrung_CG_8/matrix_aufgabe_rust/Cargo.lock
generated
Normal file
@ -0,0 +1,14 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "matrix"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "matrix_aufgabe_rust"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"matrix",
|
||||
]
|
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "matrix_aufgabe_rust"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies.matrix]
|
||||
path = "./src/matrix"
|
@ -0,0 +1,10 @@
|
||||
use matrix::Matrix;
|
||||
|
||||
fn main() {
|
||||
let m1 = Matrix::new_from_values(vec![vec![1f32, 2f32, 3f32], vec![4f32, 5f32, 6f32]]);
|
||||
let m2 = Matrix::new_from_values(vec![vec![1f32, 2f32], vec![3f32, 4f32], vec![5f32, 6f32]]);
|
||||
|
||||
let m = m1 * m2;
|
||||
|
||||
println!("{}", m);
|
||||
}
|
7
c_and_computergraphics/Einfuehrung_CG_8/matrix_aufgabe_rust/src/matrix/Cargo.lock
generated
Normal file
7
c_and_computergraphics/Einfuehrung_CG_8/matrix_aufgabe_rust/src/matrix/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "matrix"
|
||||
version = "0.1.0"
|
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "matrix"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
@ -0,0 +1,188 @@
|
||||
use std::fmt::{Display, Formatter};
|
||||
use std::ops::{Add, Mul};
|
||||
|
||||
pub struct Matrix {
|
||||
m_type: (usize, usize),
|
||||
values: Vec<Vec<f32>>
|
||||
}
|
||||
|
||||
impl Matrix {
|
||||
pub fn new((m, n): (usize, usize)) -> Self {
|
||||
let mut m_values = Vec::new();
|
||||
|
||||
for _i in 0..m {
|
||||
m_values.push(vec![0f32; n]);
|
||||
}
|
||||
|
||||
Self {
|
||||
m_type: (m, n),
|
||||
values: m_values
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_from_values(m_values: Vec<Vec<f32>>) -> Self {
|
||||
Self {
|
||||
m_type: (m_values.len(), m_values.get(0).unwrap().len()),
|
||||
values: m_values
|
||||
}
|
||||
}
|
||||
|
||||
fn add_matrices(&self, m: &Self) -> Self {
|
||||
let mut values = Vec::new();
|
||||
|
||||
for i in 0..self.m_type.0 {
|
||||
let mut row = vec![];
|
||||
|
||||
for j in 0..self.m_type.1 {
|
||||
let a = self.values.get(i).unwrap().get(j).unwrap();
|
||||
let b = m.values.get(i).unwrap().get(j).unwrap();
|
||||
|
||||
row.push(a + b);
|
||||
}
|
||||
|
||||
values.push(row);
|
||||
}
|
||||
|
||||
Self::new_from_values(values)
|
||||
}
|
||||
|
||||
pub fn multiply_with_scalar(&self, c: f32) -> Self {
|
||||
let mut values = Vec::new();
|
||||
|
||||
for i in 0..self.m_type.0 {
|
||||
let mut row = vec![];
|
||||
|
||||
for j in 0..self.m_type.1 {
|
||||
let a = self.values.get(i).unwrap().get(j).unwrap();
|
||||
|
||||
row.push(c * a);
|
||||
}
|
||||
|
||||
values.push(row);
|
||||
}
|
||||
|
||||
Self::new_from_values(values)
|
||||
}
|
||||
|
||||
fn multiply(&self, m: &Self) -> Self {
|
||||
let mut values = Vec::new();
|
||||
|
||||
for i in 0..self.m_type.0 {
|
||||
let mut row = vec![];
|
||||
|
||||
for j in 0..m.m_type.1 {
|
||||
let mut c = 0f32;
|
||||
|
||||
for k in 0..self.m_type.1 {
|
||||
let a = self.values.get(i).unwrap().get(k).unwrap();
|
||||
let b = m.values.get(k).unwrap().get(j).unwrap();
|
||||
|
||||
c += a * b;
|
||||
}
|
||||
|
||||
row.push(c);
|
||||
}
|
||||
|
||||
values.push(row);
|
||||
}
|
||||
|
||||
Self::new_from_values(values)
|
||||
}
|
||||
}
|
||||
|
||||
impl Add for Matrix {
|
||||
type Output = Matrix;
|
||||
|
||||
fn add(self, m2: Self) -> Self::Output {
|
||||
self.add_matrices(&m2)
|
||||
}
|
||||
}
|
||||
|
||||
impl Mul for Matrix {
|
||||
type Output = Matrix;
|
||||
|
||||
fn mul(self, m2: Self) -> Self::Output {
|
||||
self.multiply(&m2)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Matrix {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:?}", self.values)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::Matrix;
|
||||
|
||||
#[test]
|
||||
fn should_return_2_2_matrix_with_zeros() {
|
||||
let matrix = Matrix::new((2, 2));
|
||||
|
||||
assert_eq!(matrix.m_type, (2, 2));
|
||||
assert_eq!(matrix.values, vec![vec![0f32, 0f32], vec![0f32, 0f32]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_return_2_2_matrix_with_values() {
|
||||
let matrix = Matrix::new_from_values(vec![vec![0f32, 1f32], vec![2f32, 3f32]]);
|
||||
|
||||
assert_eq!(matrix.m_type, (2, 2));
|
||||
assert_eq!(matrix.values, vec![vec![0f32, 1f32], vec![2f32, 3f32]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_add_two_matrices() {
|
||||
let m1 = Matrix::new_from_values(vec![vec![0f32, 1f32], vec![2f32, 3f32]]);
|
||||
let m2 = Matrix::new_from_values(vec![vec![4f32, 5f32], vec![6f32, 7f32]]);
|
||||
|
||||
let m3 = m1.add_matrices(&m2);
|
||||
|
||||
assert_eq!(m3.m_type, (2, 2));
|
||||
assert_eq!(m3.values, vec![vec![4f32, 6f32], vec![8f32, 10f32]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_add_two_matrices_with_op() {
|
||||
let m1 = Matrix::new_from_values(vec![vec![0f32, 1f32], vec![2f32, 3f32]]);
|
||||
let m2 = Matrix::new_from_values(vec![vec![4f32, 5f32], vec![6f32, 7f32]]);
|
||||
|
||||
let m3 = m1 + m2;
|
||||
|
||||
assert_eq!(m3.m_type, (2, 2));
|
||||
assert_eq!(m3.values, vec![vec![4f32, 6f32], vec![8f32, 10f32]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_multiply_matrix_with_2() {
|
||||
let m1 = Matrix::new_from_values(vec![vec![0f32, 1f32], vec![2f32, 3f32]]);
|
||||
|
||||
let m = m1.multiply_with_scalar(2f32);
|
||||
|
||||
assert_eq!(m.m_type, (2, 2));
|
||||
assert_eq!(m.values, vec![vec![0f32, 2f32], vec![4f32, 6f32]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_multiply_matrices() {
|
||||
let m1 = Matrix::new_from_values(vec![vec![1f32, 2f32, 3f32], vec![4f32, 5f32, 6f32]]);
|
||||
let m2 = Matrix::new_from_values(vec![vec![1f32, 2f32], vec![3f32, 4f32], vec![5f32, 6f32]]);
|
||||
|
||||
let m = m1.multiply(&m2);
|
||||
|
||||
assert_eq!(m.m_type, (2, 2));
|
||||
assert_eq!(m.values, vec![vec![22f32, 28f32], vec![49f32, 64f32]]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_multiply_matrices_with_op() {
|
||||
let m1 = Matrix::new_from_values(vec![vec![1f32, 2f32, 3f32], vec![4f32, 5f32, 6f32]]);
|
||||
let m2 = Matrix::new_from_values(vec![vec![1f32, 2f32], vec![3f32, 4f32], vec![5f32, 6f32]]);
|
||||
|
||||
let m = m1 * m2;
|
||||
|
||||
assert_eq!(m.m_type, (2, 2));
|
||||
assert_eq!(m.values, vec![vec![22f32, 28f32], vec![49f32, 64f32]]);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user