add all methods + tests
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
25
lab02/fib/src/compute2.cpp
Normal file
25
lab02/fib/src/compute2.cpp
Normal 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;
|
||||
}
|
||||
22
lab02/fib/src/computeNew.cpp
Normal file
22
lab02/fib/src/computeNew.cpp
Normal file
File diff suppressed because one or more lines are too long
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user