|
|
|
@ -1,9 +1,42 @@
@@ -1,9 +1,42 @@
|
|
|
|
|
#include <matrix/math.hpp> |
|
|
|
|
/****************************************************************************
|
|
|
|
|
* |
|
|
|
|
* 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 <gtest/gtest.h> |
|
|
|
|
#include <matrix/math.hpp> |
|
|
|
|
|
|
|
|
|
using namespace matrix; |
|
|
|
|
|
|
|
|
|
TEST(sparseVectorTest, defaultConstruction) |
|
|
|
|
TEST(MatrixSparseVectorTest, defaultConstruction) |
|
|
|
|
{ |
|
|
|
|
SparseVectorf<24, 4, 6> a; |
|
|
|
|
EXPECT_EQ(a.non_zeros(), 2); |
|
|
|
@ -13,7 +46,7 @@ TEST(sparseVectorTest, defaultConstruction)
@@ -13,7 +46,7 @@ TEST(sparseVectorTest, defaultConstruction)
|
|
|
|
|
a.at<6>() = 2.f; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST(sparseVectorTest, initializationWithData) |
|
|
|
|
TEST(MatrixSparseVectorTest, initializationWithData) |
|
|
|
|
{ |
|
|
|
|
const float data[3] = {1.f, 2.f, 3.f}; |
|
|
|
|
SparseVectorf<24, 4, 6, 22> a(data); |
|
|
|
@ -26,7 +59,7 @@ TEST(sparseVectorTest, initializationWithData)
@@ -26,7 +59,7 @@ TEST(sparseVectorTest, initializationWithData)
|
|
|
|
|
EXPECT_FLOAT_EQ(a.at<22>(), data[2]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST(sparseVectorTest, initialisationFromVector) |
|
|
|
|
TEST(MatrixSparseVectorTest, initialisationFromVector) |
|
|
|
|
{ |
|
|
|
|
const Vector3f vec(1.f, 2.f, 3.f); |
|
|
|
|
const SparseVectorf<3, 0, 2> a(vec); |
|
|
|
@ -34,7 +67,7 @@ TEST(sparseVectorTest, initialisationFromVector)
@@ -34,7 +67,7 @@ TEST(sparseVectorTest, initialisationFromVector)
|
|
|
|
|
EXPECT_FLOAT_EQ(a.at<2>(), vec(2)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST(sparseVectorTest, accessDataWithCompressedIndices) |
|
|
|
|
TEST(MatrixSparseVectorTest, accessDataWithCompressedIndices) |
|
|
|
|
{ |
|
|
|
|
const Vector3f vec(1.f, 2.f, 3.f); |
|
|
|
|
SparseVectorf<3, 0, 2> a(vec); |
|
|
|
@ -47,7 +80,7 @@ TEST(sparseVectorTest, accessDataWithCompressedIndices)
@@ -47,7 +80,7 @@ TEST(sparseVectorTest, accessDataWithCompressedIndices)
|
|
|
|
|
EXPECT_FLOAT_EQ(a.at<2>(), a.atCompressedIndex(1)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST(sparseVectorTest, setZero) |
|
|
|
|
TEST(MatrixSparseVectorTest, setZero) |
|
|
|
|
{ |
|
|
|
|
const float data[3] = {1.f, 2.f, 3.f}; |
|
|
|
|
SparseVectorf<24, 4, 6, 22> a(data); |
|
|
|
@ -57,7 +90,7 @@ TEST(sparseVectorTest, setZero)
@@ -57,7 +90,7 @@ TEST(sparseVectorTest, setZero)
|
|
|
|
|
EXPECT_FLOAT_EQ(a.at<22>(), 0.f); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST(sparseVectorTest, additionWithDenseVector) |
|
|
|
|
TEST(MatrixSparseVectorTest, additionWithDenseVector) |
|
|
|
|
{ |
|
|
|
|
Vector<float, 4> dense_vec; |
|
|
|
|
dense_vec.setAll(1.f); |
|
|
|
@ -70,7 +103,7 @@ TEST(sparseVectorTest, additionWithDenseVector)
@@ -70,7 +103,7 @@ TEST(sparseVectorTest, additionWithDenseVector)
|
|
|
|
|
EXPECT_FLOAT_EQ(res(3), 4.f); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST(sparseVectorTest, addScalar) |
|
|
|
|
TEST(MatrixSparseVectorTest, addScalar) |
|
|
|
|
{ |
|
|
|
|
const float data[3] = {1.f, 2.f, 3.f}; |
|
|
|
|
SparseVectorf<4, 1, 2, 3> sparse_vec(data); |
|
|
|
@ -80,7 +113,7 @@ TEST(sparseVectorTest, addScalar)
@@ -80,7 +113,7 @@ TEST(sparseVectorTest, addScalar)
|
|
|
|
|
EXPECT_FLOAT_EQ(sparse_vec.at<3>(), 5.f); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST(sparseVectorTest, dotProductWithDenseVector) |
|
|
|
|
TEST(MatrixSparseVectorTest, dotProductWithDenseVector) |
|
|
|
|
{ |
|
|
|
|
Vector<float, 4> dense_vec; |
|
|
|
|
dense_vec.setAll(3.f); |
|
|
|
@ -90,7 +123,7 @@ TEST(sparseVectorTest, dotProductWithDenseVector)
@@ -90,7 +123,7 @@ TEST(sparseVectorTest, dotProductWithDenseVector)
|
|
|
|
|
EXPECT_FLOAT_EQ(res, 18.f); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST(sparseVectorTest, multiplicationWithDenseMatrix) |
|
|
|
|
TEST(MatrixSparseVectorTest, multiplicationWithDenseMatrix) |
|
|
|
|
{ |
|
|
|
|
Matrix<float, 2, 3> dense_matrix; |
|
|
|
|
dense_matrix.setAll(2.f); |
|
|
|
@ -102,7 +135,7 @@ TEST(sparseVectorTest, multiplicationWithDenseMatrix)
@@ -102,7 +135,7 @@ TEST(sparseVectorTest, multiplicationWithDenseMatrix)
|
|
|
|
|
EXPECT_TRUE(isEqual(res_dense, res_sparse)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST(sparseVectorTest, quadraticForm) |
|
|
|
|
TEST(MatrixSparseVectorTest, quadraticForm) |
|
|
|
|
{ |
|
|
|
|
float matrix_data[9] = {1, 2, 3, |
|
|
|
|
2, 4, 5, |
|
|
|
@ -114,7 +147,7 @@ TEST(sparseVectorTest, quadraticForm)
@@ -114,7 +147,7 @@ TEST(sparseVectorTest, quadraticForm)
|
|
|
|
|
EXPECT_FLOAT_EQ(quadraticForm(dense_matrix, sparse_vec), 204.f); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TEST(sparseVectorTest, norms) |
|
|
|
|
TEST(MatrixSparseVectorTest, norms) |
|
|
|
|
{ |
|
|
|
|
const float data[2] = {3.f, 4.f}; |
|
|
|
|
const SparseVectorf<4, 1, 3> sparse_vec(data); |
|
|
|
@ -123,11 +156,3 @@ TEST(sparseVectorTest, norms)
@@ -123,11 +156,3 @@ TEST(sparseVectorTest, norms)
|
|
|
|
|
EXPECT_TRUE(sparse_vec.longerThan(4.5f)); |
|
|
|
|
EXPECT_FALSE(sparse_vec.longerThan(5.5f)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv) |
|
|
|
|
{ |
|
|
|
|
testing::InitGoogleTest(&argc, argv); |
|
|
|
|
std::cout << "Run SparseVector tests" << std::endl; |
|
|
|
|
return RUN_ALL_TESTS(); |
|
|
|
|
} |