Linalg tests.

This commit is contained in:
Relintai 2023-01-25 13:48:53 +01:00
parent 0ebe6f13e3
commit 3a67c5873b
3 changed files with 128 additions and 3 deletions

View File

@ -120,11 +120,11 @@ double f_mv(std::vector<double> x){
int main() { int main() {
// // OBJECTS // // OBJECTS
Stat stat; MLPPStat stat;
LinAlg alg; MLPPLinAlg alg;
MLPPActivation avn; MLPPActivation avn;
MLPPCost cost; MLPPCost cost;
Data data; MLPPData data;
MLPPConvolutions conv; MLPPConvolutions conv;
// DATA SETS // DATA SETS

View File

@ -55,6 +55,16 @@ Vector<double> dstd_vec_to_vec(const std::vector<double> &in) {
return r; return r;
} }
Vector<Vector<double>> dstd_mat_to_mat(const std::vector<std::vector<double>> &in) {
Vector<Vector<double>> r;
for (uint32_t i = 0; i < in.size(); ++i) {
r.push_back(dstd_vec_to_vec(in[i]));
}
return r;
}
void MLPPTests::test_statistics() { void MLPPTests::test_statistics() {
ERR_PRINT("MLPPTests::test_statistics() Started!"); ERR_PRINT("MLPPTests::test_statistics() Started!");
@ -101,6 +111,61 @@ void MLPPTests::test_statistics() {
ERR_PRINT("MLPPTests::test_statistics() Finished!"); ERR_PRINT("MLPPTests::test_statistics() Finished!");
} }
void MLPPTests::test_linear_algebra() {
MLPPLinAlg alg;
std::vector<std::vector<double>> square = { { 1, 1 }, { -1, 1 }, { 1, -1 }, { -1, -1 } };
std::vector<std::vector<double>> square_rot_res = { { 1.41421, 1.11022e-16 }, { -1.11022e-16, 1.41421 }, { 1.11022e-16, -1.41421 }, { -1.41421, -1.11022e-16 } };
is_approx_equals_dmat(dstd_mat_to_mat(alg.rotate(square, M_PI / 4)), dstd_mat_to_mat(square_rot_res), "alg.rotate(square, M_PI / 4)");
std::vector<std::vector<double>> A = {
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
};
std::vector<double> a = { 4, 3, 1, 3 };
std::vector<double> b = { 3, 5, 6, 1 };
std::vector<std::vector<double>> mmtr_res = {
{ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 },
{ 4, 8, 12, 16, 20, 24, 28, 32, 36, 40 },
{ 6, 12, 18, 24, 30, 36, 42, 48, 54, 60 },
{ 8, 16, 24, 32, 40, 48, 56, 64, 72, 80 },
{ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 },
{ 12, 24, 36, 48, 60, 72, 84, 96, 108, 120 },
{ 14, 28, 42, 56, 70, 84, 98, 112, 126, 140 },
{ 16, 32, 48, 64, 80, 96, 112, 128, 144, 160 },
{ 18, 36, 54, 72, 90, 108, 126, 144, 162, 180 },
{ 20, 40, 60, 80, 100, 120, 140, 160, 180, 200 }
};
is_approx_equals_dmat(dstd_mat_to_mat(alg.matmult(alg.transpose(A), A)), dstd_mat_to_mat(mmtr_res), "alg.matmult(alg.transpose(A), A)");
is_approx_equalsd(alg.dot(a, b), 36, "alg.dot(a, b)");
std::vector<std::vector<double>> had_prod_res = {
{ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 },
{ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 }
};
is_approx_equals_dmat(dstd_mat_to_mat(alg.hadamard_product(A, A)), dstd_mat_to_mat(had_prod_res), "alg.hadamard_product(A, A)");
std::vector<std::vector<double>> id_10_res = {
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
};
is_approx_equals_dmat(dstd_mat_to_mat(alg.identity(10)), dstd_mat_to_mat(id_10_res), "alg.identity(10)");
}
void MLPPTests::is_approx_equalsd(double a, double b, const String &str) { void MLPPTests::is_approx_equalsd(double a, double b, const String &str) {
if (!Math::is_equal_approx(a, b)) { if (!Math::is_equal_approx(a, b)) {
ERR_PRINT("TEST FAILED: " + str + " Got: " + String::num(a) + " Should be: " + String::num(b)); ERR_PRINT("TEST FAILED: " + str + " Got: " + String::num(a) + " Should be: " + String::num(b));
@ -143,6 +208,63 @@ IAEDVEC_FAILED:
ERR_PRINT(fail_str); ERR_PRINT(fail_str);
} }
String vmat_to_str(const Vector<Vector<double>> &a) {
String str;
str += "[ \n";
for (int i = 0; i < a.size(); ++i) {
str += " [ ";
const Vector<double> &aa = a[i];
for (int j = 0; j < aa.size(); ++j) {
str += String::num(aa[j]);
str += " ";
}
str += "]\n";
}
str += "]\n";
return str;
}
void MLPPTests::is_approx_equals_dmat(const Vector<Vector<double>> &a, const Vector<Vector<double>> &b, const String &str) {
if (a.size() != b.size()) {
goto IAEDMAT_FAILED;
}
for (int i = 0; i < a.size(); ++i) {
const Vector<double> &aa = a[i];
const Vector<double> &bb = b[i];
if (aa.size() != bb.size()) {
goto IAEDMAT_FAILED;
}
for (int j = 0; j < aa.size(); ++j) {
if (!Math::is_equal_approx(aa[j], bb[j])) {
goto IAEDMAT_FAILED;
}
}
}
return;
IAEDMAT_FAILED:
String fail_str = "TEST FAILED: ";
fail_str += str;
fail_str += "\nGot:\n";
fail_str += vmat_to_str(a);
fail_str += "Should be:\n";
fail_str += vmat_to_str(b);
ERR_PRINT(fail_str);
}
MLPPTests::MLPPTests() { MLPPTests::MLPPTests() {
} }
@ -151,4 +273,5 @@ MLPPTests::~MLPPTests() {
void MLPPTests::_bind_methods() { void MLPPTests::_bind_methods() {
ClassDB::bind_method(D_METHOD("test_statistics"), &MLPPTests::test_statistics); ClassDB::bind_method(D_METHOD("test_statistics"), &MLPPTests::test_statistics);
ClassDB::bind_method(D_METHOD("test_linear_algebra"), &MLPPTests::test_linear_algebra);
} }

View File

@ -15,9 +15,11 @@ class MLPPTests : public Reference {
public: public:
void test_statistics(); void test_statistics();
void test_linear_algebra();
void is_approx_equalsd(double a, double b, const String &str); void is_approx_equalsd(double a, double b, const String &str);
void is_approx_equals_dvec(const Vector<double> &a, const Vector<double> &b, const String &str); void is_approx_equals_dvec(const Vector<double> &a, const Vector<double> &b, const String &str);
void is_approx_equals_dmat(const Vector<Vector<double>> &a, const Vector<Vector<double>> &b, const String &str);
MLPPTests(); MLPPTests();
~MLPPTests(); ~MLPPTests();