This commit is contained in:
kai
2025-03-29 14:41:24 +01:00
parent 6f2286de60
commit 68e252c9ce
3 changed files with 44 additions and 47 deletions

View File

@@ -1,4 +1,5 @@
cmake_minimum_required(VERSION 3.14)
cmake_policy(SET CMP0135 NEW)
# Projektname
project(fib)
@@ -10,7 +11,10 @@ include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
DOWNLOAD_EXTRACT_TIMESTAMP true
)
FetchContent_MakeAvailable(googletest)
# add includes
include_directories(include)
# Quellverzeichnis angeben

View File

@@ -1,48 +1,6 @@
#include "matrix.h"
#include <cmath>
#include <iostream>
#include <vector>
int fib(int n) {
if (n < 2)
return n;
int a = 0;
int b = 1;
for (int i = 2; i <= n; ++i) {
int temp = a + b;
a = b;
b = temp;
}
return b;
}
std::vector<double> get_lookup(const std::vector<int> &v) {
std::vector<double> lookup = std::vector<double>(256);
for (int i = 0; i < 256; i++) {
lookup[i] = fib(i);
}
return lookup;
}
Matrix compute(const Matrix &s, const std::vector<int> &v) {
std::vector lookup = get_lookup(v);
Matrix m(s.dim());
const int n = v.size();
for (int j = 0; j < n; ++j) {
double val = static_cast<double>(lookup[v[j] % 256]);
double val2 = (sin(val) * tan(val) / sqrt(cos(val) + 2));
for (int i = 0; i < n; ++i) {
m(j, i) = s(j, i) * val2;
}
}
return m;
}
#include "matrix.h"
#include "compute.cpp"
int main() {
std::vector v = std::vector<int>(3);
@@ -74,3 +32,4 @@ int main() {
return 0;
}

View File

@@ -1,6 +1,40 @@
#include <gtest/gtest.h>
#include "matrix.h"
#include "compute.cpp"
TEST(first, test) {
EXPECT_STRNE("hello", "world");
EXPECT_EQ(7*6, 42);
TEST(MatrixVectorTest, ComputeTest) {
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 = compute(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
}
}
}