diff --git a/src/lib/matrix/matrix/Matrix.hpp b/src/lib/matrix/matrix/Matrix.hpp index 8de2b0a400c..af6991cda76 100644 --- a/src/lib/matrix/matrix/Matrix.hpp +++ b/src/lib/matrix/matrix/Matrix.hpp @@ -604,6 +604,20 @@ public: } } + inline void nanToZero() + { + + Matrix &self = *this; + + for (size_t i = 0; i < M; i++) { + for (size_t j = 0; j < N; j++) { + if (std::isnan(self(i, j))) { + self(i, j) = static_cast(0.0f); + } + } + } + } + Matrix abs() const { Matrix r; diff --git a/src/lib/matrix/test/CMakeLists.txt b/src/lib/matrix/test/CMakeLists.txt index dbdf1d4c224..6de070a0411 100644 --- a/src/lib/matrix/test/CMakeLists.txt +++ b/src/lib/matrix/test/CMakeLists.txt @@ -28,3 +28,4 @@ px4_add_unit_gtest(SRC MatrixUpperRightTriangleTest.cpp) px4_add_unit_gtest(SRC MatrixVector2Test.cpp) px4_add_unit_gtest(SRC MatrixVector3Test.cpp) px4_add_unit_gtest(SRC MatrixVectorAssignmentTest.cpp) +px4_add_unit_gtest(SRC NanToZeroTest.cpp) diff --git a/src/lib/matrix/test/NanToZeroTest.cpp b/src/lib/matrix/test/NanToZeroTest.cpp new file mode 100644 index 00000000000..13ebde28324 --- /dev/null +++ b/src/lib/matrix/test/NanToZeroTest.cpp @@ -0,0 +1,70 @@ +/**************************************************************************** + * + * Copyright (C) 2026 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; + +TEST(NanToZeroTest, NanToZero) +{ + const float data[9] = {-1.f, 0.f, +INFINITY, NAN, 0.f, -INFINITY, 1.f, 0.f, -NAN}; + const float expected[9] = {-1.f, 0.f, +INFINITY, 0.f, 0.f, -INFINITY, 1.f, 0.f, 0.f}; + + Matrix mat(data); + mat.nanToZero(); + + const Matrix mat_expected(expected); + + for (size_t i = 0; i < 3; i++) { + for (size_t j = 0; j < 3; j++) { + EXPECT_FLOAT_EQ(mat(i, j), mat_expected(i, j)); + } + } +} + +TEST(NanToZeroTest, NoNanUnchanged) +{ + const float data[4] = {-2.5f, 0.f, 3.14f, +INFINITY}; + + Matrix mat(data); + mat.nanToZero(); + + const Matrix mat_orig(data); + + for (size_t i = 0; i < 2; i++) { + for (size_t j = 0; j < 2; j++) { + EXPECT_FLOAT_EQ(mat(i, j), mat_orig(i, j)); + } + } +}