2023-04-26 16:33:20 +02:00
|
|
|
|
|
|
|
#include "mlpp_matrix_tests.h"
|
|
|
|
|
2023-04-26 21:50:36 +02:00
|
|
|
#include "core/log/logger.h"
|
|
|
|
|
2023-04-26 16:33:20 +02:00
|
|
|
#include "../mlpp/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();
|
|
|
|
}
|
|
|
|
|
2023-04-26 16:33:20 +02:00
|
|
|
void MLPPMatrixTests::test_mlpp_matrix() {
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t A[] = {
|
|
|
|
1, 0, 0, 0, //
|
|
|
|
0, 1, 0, 0, //
|
|
|
|
0, 0, 1, 0, //
|
|
|
|
0, 0, 0, 1, //
|
2023-04-26 16:33:20 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 02:06:16 +02:00
|
|
|
Ref<MLPPMatrix> rmat(memnew(MLPPMatrix(A, 4, 4)));
|
2023-04-26 16:33:20 +02:00
|
|
|
|
|
|
|
Ref<MLPPMatrix> rmat2;
|
|
|
|
rmat2.instance();
|
2023-10-25 00:54:01 +02:00
|
|
|
rmat2->set_from_ptr(A, 4, 4);
|
2023-04-26 16:33:20 +02:00
|
|
|
|
2023-10-25 00:54:01 +02:00
|
|
|
is_approx_equals_mat(rmat, rmat2, "set_from_ptr test.");
|
2023-04-26 16:33:20 +02:00
|
|
|
|
2023-10-25 00:54:01 +02:00
|
|
|
rmat2->set_from_ptr(A, 4, 4);
|
2023-04-26 16:33:20 +02:00
|
|
|
|
2023-10-25 00:54:01 +02:00
|
|
|
is_approx_equals_mat(rmat, rmat2, "re-set_from_ptr test.");
|
2023-04-26 16:33:20 +02:00
|
|
|
}
|
|
|
|
|
2023-04-29 15:07:30 +02:00
|
|
|
void MLPPMatrixTests::test_row_add() {
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t A[] = {
|
|
|
|
1, 2, 3, 4, //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t B[] = {
|
|
|
|
1, 2, 3, 4, //
|
|
|
|
1, 2, 3, 4, //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +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);
|
|
|
|
|
2023-10-25 02:06:16 +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->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() {
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t A[] = {
|
|
|
|
1, 2, 3, 4, //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t B[] = {
|
|
|
|
1, 2, 3, 4, //
|
|
|
|
1, 2, 3, 4, //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +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);
|
|
|
|
|
2023-10-25 02:06:16 +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->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() {
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t A[] = {
|
|
|
|
1, 2, 3, 4, //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t B[] = {
|
|
|
|
1, 2, 3, 4, //
|
|
|
|
1, 2, 3, 4, //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +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);
|
|
|
|
|
2023-10-25 02:06:16 +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->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() {
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t A[] = {
|
|
|
|
1, 2, 3, 4 //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t B[] = {
|
|
|
|
1, 2, 3, 4, //
|
|
|
|
1, 2, 3, 4, //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +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
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +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
|
|
|
|
2023-10-25 02:06:16 +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() {
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t A[] = {
|
|
|
|
1, 2, 3, 4, //
|
|
|
|
5, 6, 7, 8, //
|
|
|
|
9, 10, 11, 12, //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t B[] = {
|
|
|
|
1, 2, 3, 4, //
|
|
|
|
5, 6, 7, 8, //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t C[] = {
|
|
|
|
1, 2, 3, 4, //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +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
|
|
|
};
|
|
|
|
|
2023-10-25 02:06:16 +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();
|
2023-10-25 00:54:01 +02:00
|
|
|
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() {
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t A[] = {
|
|
|
|
1, 2, 3, 4, //
|
|
|
|
13, 14, 15, 16, //
|
|
|
|
9, 10, 11, 12, //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t B[] = {
|
|
|
|
9, 10, 11, 12, //
|
|
|
|
13, 14, 15, 16, //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t C[] = {
|
|
|
|
9, 10, 11, 12, //
|
2023-04-26 21:50:36 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +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
|
|
|
};
|
|
|
|
|
2023-10-25 02:06:16 +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
|
|
|
}
|
|
|
|
|
2023-04-26 16:33:20 +02:00
|
|
|
void MLPPMatrixTests::test_mlpp_matrix_mul() {
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t A[] = {
|
|
|
|
1, 2, //
|
|
|
|
3, 4, //
|
|
|
|
5, 6, //
|
|
|
|
7, 8, //
|
2023-04-26 16:33:20 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t B[] = {
|
|
|
|
1, 2, 3, 4, //
|
|
|
|
5, 6, 7, 8, //
|
2023-04-26 16:33:20 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 00:54:01 +02:00
|
|
|
const real_t C[] = {
|
|
|
|
11, 14, 17, 20, //
|
|
|
|
23, 30, 37, 44, //
|
|
|
|
35, 46, 57, 68, //
|
|
|
|
47, 62, 77, 92, //
|
2023-04-26 16:33:20 +02:00
|
|
|
};
|
|
|
|
|
2023-10-25 02:06:16 +02:00
|
|
|
Ref<MLPPMatrix> rmata(memnew(MLPPMatrix(A, 4, 2)));
|
|
|
|
Ref<MLPPMatrix> rmatb(memnew(MLPPMatrix(B, 2, 4)));
|
|
|
|
Ref<MLPPMatrix> rmatc(memnew(MLPPMatrix(C, 4, 4)));
|
2023-04-26 16:33:20 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2023-04-26 16:33:20 +02:00
|
|
|
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);
|
|
|
|
}
|