pmlpp/test/mlpp_matrix_tests.cpp

370 lines
9.4 KiB
C++
Raw Normal View History

2023-12-30 00:41:59 +01:00
/*************************************************************************/
/* mlpp_matrix_tests.cpp */
/*************************************************************************/
/* This file is part of: */
/* PMLPP Machine Learning Library */
/* https://github.com/Relintai/pmlpp */
/*************************************************************************/
2023-12-30 00:43:39 +01:00
/* Copyright (c) 2023-present Péter Magyar. */
2023-12-30 00:41:59 +01:00
/* Copyright (c) 2022-2023 Marc Melikyan */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "mlpp_matrix_tests.h"
2023-04-26 21:50:36 +02:00
#include "core/log/logger.h"
#include "../lin_alg/mlpp_matrix.h"
2023-04-26 21:50:36 +02:00
void MLPPMatrixTests::run_tests() {
PLOG_MSG("RUNNIG MLPPMatrixTests!");
2023-10-25 01:47:44 +02:00
PLOG_TRACE("test_mlpp_matrix()");
2023-04-26 21:50:36 +02:00
test_mlpp_matrix();
2023-10-25 01:47:44 +02:00
PLOG_TRACE("test_row_add()");
2023-04-29 15:07:30 +02:00
test_row_add();
2023-10-25 01:47:44 +02:00
PLOG_TRACE("test_row_add_pool_vector()");
2023-04-29 15:07:30 +02:00
test_row_add_pool_vector();
2023-10-25 01:47:44 +02:00
PLOG_TRACE("test_row_add_mlpp_vector()");
2023-04-29 15:07:30 +02:00
test_row_add_mlpp_vector();
2023-10-25 01:47:44 +02:00
PLOG_TRACE("test_rows_add_mlpp_matrix()");
2023-04-29 15:07:30 +02:00
test_rows_add_mlpp_matrix();
2023-10-25 01:47:44 +02:00
PLOG_TRACE("test_row_remove()");
2023-04-29 15:07:30 +02:00
test_row_remove();
2023-10-25 01:47:44 +02:00
PLOG_TRACE("test_row_remove_unordered()");
2023-04-29 15:07:30 +02:00
test_row_remove_unordered();
2023-04-26 21:50:36 +02:00
2023-10-25 01:47:44 +02:00
PLOG_TRACE("test_mlpp_matrix_mul()");
2023-04-26 21:50:36 +02:00
test_mlpp_matrix_mul();
}
void MLPPMatrixTests::test_mlpp_matrix() {
const real_t A[] = {
1, 0, 0, 0, //
0, 1, 0, 0, //
0, 0, 1, 0, //
0, 0, 0, 1, //
};
Ref<MLPPMatrix> rmat(memnew(MLPPMatrix(A, 4, 4)));
Ref<MLPPMatrix> rmat2;
rmat2.instance();
rmat2->set_from_ptr(A, 4, 4);
is_approx_equals_mat(rmat, rmat2, "set_from_ptr test.");
rmat2->set_from_ptr(A, 4, 4);
is_approx_equals_mat(rmat, rmat2, "re-set_from_ptr test.");
}
2023-04-29 15:07:30 +02:00
void MLPPMatrixTests::test_row_add() {
const real_t A[] = {
1, 2, 3, 4, //
2023-04-26 21:50:36 +02:00
};
const real_t B[] = {
1, 2, 3, 4, //
1, 2, 3, 4, //
2023-04-26 21:50:36 +02:00
};
const real_t C[] = {
1, 2, 3, 4, //
1, 2, 3, 4, //
1, 2, 3, 4, //
2023-04-26 21:50:36 +02:00
};
Vector<real_t> rv;
rv.push_back(1);
rv.push_back(2);
rv.push_back(3);
rv.push_back(4);
Ref<MLPPMatrix> rmata(memnew(MLPPMatrix(A, 1, 4)));
Ref<MLPPMatrix> rmatb(memnew(MLPPMatrix(B, 2, 4)));
Ref<MLPPMatrix> rmatc(memnew(MLPPMatrix(C, 3, 4)));
2023-04-26 21:50:36 +02:00
Ref<MLPPMatrix> rmat;
rmat.instance();
2023-04-29 15:07:30 +02:00
rmat->row_add(rv);
is_approx_equals_mat(rmata, rmat, "rmat->row_add(rv);");
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->row_add(rv);
is_approx_equals_mat(rmatb, rmat, "rmat->row_add(rv);");
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->row_add(rv);
is_approx_equals_mat(rmatc, rmat, "rmat->row_add(rv);");
2023-04-26 21:50:36 +02:00
}
2023-04-29 15:07:30 +02:00
void MLPPMatrixTests::test_row_add_pool_vector() {
const real_t A[] = {
1, 2, 3, 4, //
2023-04-26 21:50:36 +02:00
};
const real_t B[] = {
1, 2, 3, 4, //
1, 2, 3, 4, //
2023-04-26 21:50:36 +02:00
};
const real_t C[] = {
1, 2, 3, 4, //
1, 2, 3, 4, //
1, 2, 3, 4, //
2023-04-26 21:50:36 +02:00
};
PoolVector<real_t> rv;
rv.push_back(1);
rv.push_back(2);
rv.push_back(3);
rv.push_back(4);
Ref<MLPPMatrix> rmata(memnew(MLPPMatrix(A, 1, 4)));
Ref<MLPPMatrix> rmatb(memnew(MLPPMatrix(B, 2, 4)));
Ref<MLPPMatrix> rmatc(memnew(MLPPMatrix(C, 3, 4)));
2023-04-26 21:50:36 +02:00
Ref<MLPPMatrix> rmat;
rmat.instance();
2023-04-29 15:07:30 +02:00
rmat->row_add_pool_vector(rv);
is_approx_equals_mat(rmata, rmat, "rmat->row_add_pool_vector(rv);");
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->row_add_pool_vector(rv);
is_approx_equals_mat(rmatb, rmat, "rmat->row_add_pool_vector(rv);");
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->row_add_pool_vector(rv);
is_approx_equals_mat(rmatc, rmat, "rmat->row_add_pool_vector(rv);");
2023-04-26 21:50:36 +02:00
}
2023-04-29 15:07:30 +02:00
void MLPPMatrixTests::test_row_add_mlpp_vector() {
const real_t A[] = {
1, 2, 3, 4, //
2023-04-26 21:50:36 +02:00
};
const real_t B[] = {
1, 2, 3, 4, //
1, 2, 3, 4, //
2023-04-26 21:50:36 +02:00
};
const real_t C[] = {
1, 2, 3, 4, //
1, 2, 3, 4, //
1, 2, 3, 4, //
2023-04-26 21:50:36 +02:00
};
Ref<MLPPVector> rv;
rv.instance();
rv->push_back(1);
rv->push_back(2);
rv->push_back(3);
rv->push_back(4);
Ref<MLPPMatrix> rmata(memnew(MLPPMatrix(A, 1, 4)));
Ref<MLPPMatrix> rmatb(memnew(MLPPMatrix(B, 2, 4)));
Ref<MLPPMatrix> rmatc(memnew(MLPPMatrix(C, 3, 4)));
2023-04-26 21:50:36 +02:00
Ref<MLPPMatrix> rmat;
rmat.instance();
2023-04-29 15:07:30 +02:00
rmat->row_add_mlpp_vector(rv);
is_approx_equals_mat(rmata, rmat, "rmat->row_add_mlpp_vector(rv);");
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->row_add_mlpp_vector(rv);
is_approx_equals_mat(rmatb, rmat, "rmat->row_add_mlpp_vector(rv);");
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->row_add_mlpp_vector(rv);
is_approx_equals_mat(rmatc, rmat, "rmat->row_add_mlpp_vector(rv);");
2023-04-26 21:50:36 +02:00
}
2023-04-29 15:07:30 +02:00
void MLPPMatrixTests::test_rows_add_mlpp_matrix() {
const real_t A[] = {
1, 2, 3, 4 //
2023-04-26 21:50:36 +02:00
};
const real_t B[] = {
1, 2, 3, 4, //
1, 2, 3, 4, //
2023-04-26 21:50:36 +02:00
};
const real_t C[] = {
1, 2, 3, 4, //
1, 2, 3, 4, //
1, 2, 3, 4, //
2023-04-26 21:50:36 +02:00
};
//const real_t r[] = {
// 1, 2, 3, 4
//};
2023-04-26 21:50:36 +02:00
PoolVector<real_t> rvp;
rvp.push_back(1);
rvp.push_back(2);
rvp.push_back(3);
rvp.push_back(4);
Ref<MLPPMatrix> rv;
rv.instance();
2023-04-29 15:07:30 +02:00
rv->row_add_pool_vector(rvp);
2023-04-26 21:50:36 +02:00
Ref<MLPPMatrix> rmata(memnew(MLPPMatrix(A, 1, 4)));
Ref<MLPPMatrix> rmatb(memnew(MLPPMatrix(B, 2, 4)));
Ref<MLPPMatrix> rmatc(memnew(MLPPMatrix(C, 3, 4)));
2023-04-26 21:50:36 +02:00
Ref<MLPPMatrix> rmat;
rmat.instance();
2023-04-29 15:07:30 +02:00
rmat->rows_add_mlpp_matrix(rv);
is_approx_equals_mat(rmata, rmat, "rmat->rows_add_mlpp_matrix(rv);");
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->rows_add_mlpp_matrix(rv);
is_approx_equals_mat(rmatb, rmat, "rmat->rows_add_mlpp_matrix(rv);");
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->rows_add_mlpp_matrix(rv);
is_approx_equals_mat(rmatc, rmat, "rmat->rows_add_mlpp_matrix(rv);");
2023-04-26 21:50:36 +02:00
}
2023-04-29 15:07:30 +02:00
void MLPPMatrixTests::test_row_remove() {
const real_t A[] = {
1, 2, 3, 4, //
5, 6, 7, 8, //
9, 10, 11, 12, //
2023-04-26 21:50:36 +02:00
};
const real_t B[] = {
1, 2, 3, 4, //
5, 6, 7, 8, //
2023-04-26 21:50:36 +02:00
};
const real_t C[] = {
1, 2, 3, 4, //
2023-04-26 21:50:36 +02:00
};
const real_t D[] = {
1, 2, 3, 4, //
5, 6, 7, 8, //
13, 14, 15, 16, //
9, 10, 11, 12, //
2023-04-26 21:50:36 +02:00
};
Ref<MLPPMatrix> rmata(memnew(MLPPMatrix(A, 3, 4)));
Ref<MLPPMatrix> rmatb(memnew(MLPPMatrix(B, 2, 4)));
Ref<MLPPMatrix> rmatc(memnew(MLPPMatrix(C, 1, 4)));
2023-04-26 21:50:36 +02:00
Ref<MLPPMatrix> rmat;
rmat.instance();
rmat->set_from_ptr(D, 4, 4);
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->row_remove(2);
is_approx_equals_mat(rmat, rmata, "rmat->row_remove(2);");
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->row_remove(2);
is_approx_equals_mat(rmat, rmatb, "rmat->row_remove(2);");
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->row_remove(1);
is_approx_equals_mat(rmat, rmatc, "rmat->row_remove(1);");
2023-04-26 21:50:36 +02:00
}
2023-04-29 15:07:30 +02:00
void MLPPMatrixTests::test_row_remove_unordered() {
const real_t A[] = {
1, 2, 3, 4, //
13, 14, 15, 16, //
9, 10, 11, 12, //
2023-04-26 21:50:36 +02:00
};
const real_t B[] = {
9, 10, 11, 12, //
13, 14, 15, 16, //
2023-04-26 21:50:36 +02:00
};
const real_t C[] = {
9, 10, 11, 12, //
2023-04-26 21:50:36 +02:00
};
const real_t D[] = {
1, 2, 3, 4, //
5, 6, 7, 8, //
9, 10, 11, 12, //
13, 14, 15, 16, //
2023-04-26 21:50:36 +02:00
};
Ref<MLPPMatrix> rmata(memnew(MLPPMatrix(A, 3, 4)));
Ref<MLPPMatrix> rmatb(memnew(MLPPMatrix(B, 2, 4)));
Ref<MLPPMatrix> rmatc(memnew(MLPPMatrix(C, 1, 4)));
Ref<MLPPMatrix> rmat(memnew(MLPPMatrix(D, 4, 4)));
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->row_remove_unordered(1);
is_approx_equals_mat(rmat, rmata, "rmat->row_remove_unordered(1);");
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->row_remove_unordered(0);
is_approx_equals_mat(rmat, rmatb, "rmat->row_remove(0);");
2023-04-26 21:50:36 +02:00
2023-04-29 15:07:30 +02:00
rmat->row_remove_unordered(1);
is_approx_equals_mat(rmat, rmatc, "rmat->row_remove_unordered(1);");
2023-04-26 21:50:36 +02:00
}
void MLPPMatrixTests::test_mlpp_matrix_mul() {
const real_t A[] = {
1, 2, //
3, 4, //
5, 6, //
7, 8, //
};
const real_t B[] = {
1, 2, 3, 4, //
5, 6, 7, 8, //
};
const real_t C[] = {
11, 14, 17, 20, //
23, 30, 37, 44, //
35, 46, 57, 68, //
47, 62, 77, 92, //
};
Ref<MLPPMatrix> rmata(memnew(MLPPMatrix(A, 4, 2)));
Ref<MLPPMatrix> rmatb(memnew(MLPPMatrix(B, 2, 4)));
Ref<MLPPMatrix> rmatc(memnew(MLPPMatrix(C, 4, 4)));
Ref<MLPPMatrix> rmatr1 = rmata->multn(rmatb);
is_approx_equals_mat(rmatr1, rmatc, "Ref<MLPPMatrix> rmatr1 = rmata->multn(rmatb);");
Ref<MLPPMatrix> rmatr2;
rmatr2.instance();
rmatr2->multb(rmata, rmatb);
is_approx_equals_mat(rmatr2, rmatc, "rmatr2->multb(rmata, rmatb);");
rmata->mult(rmatb);
is_approx_equals_mat(rmata, rmatc, "rmata->mult(rmatb);");
}
MLPPMatrixTests::MLPPMatrixTests() {
}
MLPPMatrixTests::~MLPPMatrixTests() {
}
void MLPPMatrixTests::_bind_methods() {
2023-04-26 21:50:36 +02:00
ClassDB::bind_method(D_METHOD("run_tests"), &MLPPMatrixTests::run_tests);
ClassDB::bind_method(D_METHOD("test_mlpp_matrix"), &MLPPMatrixTests::test_mlpp_matrix);
ClassDB::bind_method(D_METHOD("test_mlpp_matrix_mul"), &MLPPMatrixTests::test_mlpp_matrix_mul);
}