add all methods + tests

This commit is contained in:
kai
2025-03-29 14:52:12 +01:00
parent 68e252c9ce
commit acbc42e3e6
4 changed files with 122 additions and 0 deletions

View File

@@ -3,6 +3,7 @@
#include <cmath>
#include <iostream>
#include <vector>
#include <array>
int fib(int n) {
if (n < 2)
@@ -44,3 +45,4 @@ Matrix compute(const Matrix &s, const std::vector<int> &v) {
}
return m;
}

View File

@@ -0,0 +1,25 @@
#include "matrix.h"
#include <cmath>
#include <iostream>
#include <vector>
int fib2(int i) {
if (i == 0) {
return 0;
} else if (i == 1) {
return 1;
} else {
return fib2(i - 1) + fib2(i - 2);
}
}
Matrix compute2(const Matrix& s, const std::vector<int>& v) {
Matrix m(s.dim());
const int n = v.size();
for (int j = 0; j < n; ++j) {
for (int i = 0; i < n; ++i) {
double val = static_cast<double>(fib2(v[j] % 256));
m(j, i) = s(j, i) * (sin(val) * tan(val) / sqrt(cos(val) + 2));
}
}
return m;
}

File diff suppressed because one or more lines are too long

View File

@@ -1,6 +1,8 @@
#include <gtest/gtest.h>
#include "matrix.h"
#include "compute.cpp"
#include "compute2.cpp"
#include "computeNew.cpp"
TEST(MatrixVectorTest, ComputeTest) {
std::vector<int> v = {10, 2, 20};
@@ -38,3 +40,74 @@ TEST(MatrixVectorTest, ComputeTest) {
}
}
TEST(MatrixVectorTest, Compute2Test) {
std::vector<int> v = {10, 2, 20};
Matrix m(3, 3);
for (int i = 0; i < 3; i++) {
for (int y = 0; y < 3; y++) {
m(i, y) = 1;
}
}
Matrix expected(3, 3);
for (int i = 0; i < 3; i++) {
expected(0, i) = 31.7662;
}
for (int i = 0; i < 3; i++) {
expected(1, i) = 0.82224;
}
for (int i = 0; i < 3; i++) {
expected(2, i) = -1.6214;
}
auto erg = compute2(m, v);
// Erwartete Ergebnisse für die Multiplikation von Matrix und Vektor
EXPECT_EQ(v[0], 10);
EXPECT_EQ(v[1], 2);
EXPECT_EQ(v[2], 20);
// Testen, ob das Ergebnis der Matrix korrekt ist
for (int i = 0; i < 3; i++) {
for (int y = 0; y < 3; y++) {
EXPECT_NEAR(erg(i,y), expected(i, y), 0.1); // Falls es sich um eine einfache Summation handelt
}
}
}
TEST(MatrixVectorTest, ComputeNewTest) {
std::vector<int> v = {10, 2, 20};
Matrix m(3, 3);
for (int i = 0; i < 3; i++) {
for (int y = 0; y < 3; y++) {
m(i, y) = 1;
}
}
Matrix expected(3, 3);
for (int i = 0; i < 3; i++) {
expected(0, i) = 31.7662;
}
for (int i = 0; i < 3; i++) {
expected(1, i) = 0.82224;
}
for (int i = 0; i < 3; i++) {
expected(2, i) = -1.6214;
}
auto erg = computeNew(m, v);
// Erwartete Ergebnisse für die Multiplikation von Matrix und Vektor
EXPECT_EQ(v[0], 10);
EXPECT_EQ(v[1], 2);
EXPECT_EQ(v[2], 20);
// Testen, ob das Ergebnis der Matrix korrekt ist
for (int i = 0; i < 3; i++) {
for (int y = 0; y < 3; y++) {
EXPECT_NEAR(erg(i,y), expected(i, y), 0.1); // Falls es sich um eine einfache Summation handelt
}
}
}