From 719898f1e3aad35e19d2806954ba86241316eb41 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Wed, 23 Feb 2022 14:59:42 +0100 Subject: [PATCH] Matrix: convert least squares test to gtest --- src/lib/matrix/test/CMakeLists.txt | 2 +- .../matrix/test/MatrixLeastSquaresTest.cpp | 111 ++++++++++++++++++ src/lib/matrix/test/least_squares.cpp | 106 ----------------- 3 files changed, 112 insertions(+), 107 deletions(-) create mode 100644 src/lib/matrix/test/MatrixLeastSquaresTest.cpp delete mode 100644 src/lib/matrix/test/least_squares.cpp diff --git a/src/lib/matrix/test/CMakeLists.txt b/src/lib/matrix/test/CMakeLists.txt index d15c4df13b..04991b0266 100644 --- a/src/lib/matrix/test/CMakeLists.txt +++ b/src/lib/matrix/test/CMakeLists.txt @@ -16,7 +16,6 @@ set(tests vector2 vector3 squareMatrix - least_squares upperRightTriangle pseudoInverse ) @@ -43,5 +42,6 @@ px4_add_unit_gtest(SRC MatrixHatveeTest.cpp) px4_add_unit_gtest(SRC MatrixHelperTest.cpp) px4_add_unit_gtest(SRC MatrixIntegralTest.cpp) px4_add_unit_gtest(SRC MatrixInverseTest.cpp) +px4_add_unit_gtest(SRC MatrixLeastSquaresTest.cpp) px4_add_unit_gtest(SRC MatrixSparseVectorTest.cpp) px4_add_unit_gtest(SRC MatrixUnwrapTest.cpp) diff --git a/src/lib/matrix/test/MatrixLeastSquaresTest.cpp b/src/lib/matrix/test/MatrixLeastSquaresTest.cpp new file mode 100644 index 0000000000..b3742271ef --- /dev/null +++ b/src/lib/matrix/test/MatrixLeastSquaresTest.cpp @@ -0,0 +1,111 @@ +/**************************************************************************** + * + * Copyright (C) 2022 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#include +#include + +using namespace matrix; + +int test_4x3(void); +template int test_4x4(void); +int test_4x4_type_double(void); +int test_div_zero(void); + +TEST(MatrixLeastSquaresTest, 4x3) +{ + // Start with an (m x n) A matrix + float data[12] = {20.f, -10.f, -13.f, + 17.f, 16.f, -18.f, + 0.7f, -0.8f, 0.9f, + -1.f, -1.1f, -1.2f + }; + Matrix A(data); + + float b_data[4] = {2.0f, 3.0f, 4.0f, 5.0f}; + Vector b(b_data); + + float x_check_data[3] = {-0.69168233f, + -0.26227593f, + -1.03767522f + }; + Vector x_check(x_check_data); + + LeastSquaresSolver qrd = LeastSquaresSolver(A); + + Vector x = qrd.solve(b); + EXPECT_EQ(x, x_check); +} + +TEST(MatrixLeastSquaresTest, 4x4) +{ + // Start with an (m x n) A matrix + const float data[16] = { 20.f, -10.f, -13.f, 21.f, + 17.f, 16.f, -18.f, -14.f, + 0.7f, -0.8f, 0.9f, -0.5f, + -1.f, -1.1f, -1.2f, -1.3f + }; + Matrix A(data); + + float b_data[4] = {2.0f, 3.0f, 4.0f, 5.0f}; + Vector b(b_data); + + float x_check_data[4] = { 0.97893433f, + -2.80798701f, + -0.03175765f, + -2.19387649f + }; + Vector x_check(x_check_data); + + LeastSquaresSolver qrd = LeastSquaresSolver(A); + + Vector x = qrd.solve(b); + EXPECT_EQ(x, x_check); +} + +TEST(MatrixLeastSquaresTest, ZeroDivision) +{ + float data[4] = {0.0f, 0.0f, 0.0f, 0.0f}; + Matrix A(data); + + float b_data[2] = {1.0f, 1.0f}; + Vector b(b_data); + + // Implement such that x returns zeros if it reaches div by zero + float x_check_data[2] = {0.0f, 0.0f}; + Vector x_check(x_check_data); + + LeastSquaresSolver qrd = LeastSquaresSolver(A); + + Vector x = qrd.solve(b); + EXPECT_EQ(x, x_check); +} diff --git a/src/lib/matrix/test/least_squares.cpp b/src/lib/matrix/test/least_squares.cpp deleted file mode 100644 index 2009cc59a2..0000000000 --- a/src/lib/matrix/test/least_squares.cpp +++ /dev/null @@ -1,106 +0,0 @@ -#include "test_macros.hpp" -#include - -using namespace matrix; - -int test_4x3(void); -template int test_4x4(void); -int test_4x4_type_double(void); -int test_div_zero(void); - -int main() -{ - int ret; - - ret = test_4x4(); - - if (ret != 0) { return ret; } - - ret = test_4x4(); - - if (ret != 0) { return ret; } - - ret = test_4x3(); - - if (ret != 0) { return ret; } - - ret = test_div_zero(); - - if (ret != 0) { return ret; } - - return 0; -} - -int test_4x3() -{ - // Start with an (m x n) A matrix - float data[12] = {20.f, -10.f, -13.f, - 17.f, 16.f, -18.f, - 0.7f, -0.8f, 0.9f, - -1.f, -1.1f, -1.2f - }; - Matrix A(data); - - float b_data[4] = {2.0f, 3.0f, 4.0f, 5.0f}; - Vector b(b_data); - - float x_check_data[3] = {-0.69168233f, - -0.26227593f, - -1.03767522f - }; - Vector x_check(x_check_data); - - LeastSquaresSolver qrd = LeastSquaresSolver(A); - - Vector x = qrd.solve(b); - TEST(isEqual(x, x_check)); - return 0; -} - -template -int test_4x4() -{ - // Start with an (m x n) A matrix - const Type data[16] = { 20.f, -10.f, -13.f, 21.f, - 17.f, 16.f, -18.f, -14.f, - 0.7f, -0.8f, 0.9f, -0.5f, - -1.f, -1.1f, -1.2f, -1.3f - }; - Matrix A(data); - - Type b_data[4] = {2.0f, 3.0f, 4.0f, 5.0f}; - Vector b(b_data); - - Type x_check_data[4] = { 0.97893433f, - -2.80798701f, - -0.03175765f, - -2.19387649f - }; - Vector x_check(x_check_data); - - LeastSquaresSolver qrd = LeastSquaresSolver(A); - - Vector x = qrd.solve(b); - TEST(isEqual(x, x_check)); - return 0; -} - -int test_div_zero() -{ - float data[4] = {0.0f, 0.0f, 0.0f, 0.0f}; - Matrix A(data); - - float b_data[2] = {1.0f, 1.0f}; - Vector b(b_data); - - // Implement such that x returns zeros if it reaches div by zero - float x_check_data[2] = {0.0f, 0.0f}; - Vector x_check(x_check_data); - - LeastSquaresSolver qrd = LeastSquaresSolver(A); - - Vector x = qrd.solve(b); - TEST(isEqual(x, x_check)); - return 0; -} -