From 73b512b34462e96ef756a291f002151988f60d98 Mon Sep 17 00:00:00 2001 From: Pablo Alonso Date: Thu, 29 Jun 2023 08:52:09 +0200 Subject: [PATCH 1/2] Add unit test covering the bug described at #1345 Tests that the scale and shift steps are correctly applied after each non-linear operation --- .../unittests/standard/test_unaryoperator.py | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/test/src/unittests/standard/test_unaryoperator.py b/test/src/unittests/standard/test_unaryoperator.py index e37d19fe3..eb6a80779 100644 --- a/test/src/unittests/standard/test_unaryoperator.py +++ b/test/src/unittests/standard/test_unaryoperator.py @@ -46,7 +46,7 @@ def testLog10(self): def testLog10LowX(self): self.assertAlmostEqualVector( UnaryOperator(type="log10")(self.testInputLowX), - [-30., -30., -30., -30.]) + [-30., -30., -30., -30.]) def testLog(self): self.assertAlmostEqualVector( @@ -55,15 +55,15 @@ def testLog(self): def testLogLowX(self): self.assertAlmostEqualVector( - UnaryOperator(type="log")(self.testInputLowX),[-69.07755, -69.07755, -69.07755, -69.07755]) + UnaryOperator(type="log")(self.testInputLowX),[-69.07755, -69.07755, -69.07755, -69.07755]) def testLn(self): self.assertAlmostEqualVector(UnaryOperator(type="ln")(self.testInput), [0, 0.693147181, 1.098612289, 1.386294361, 1.223775432, -69.07755279, 11.513265407]) - def testLnLowX(self): + def testLnLowX(self): self.assertAlmostEqualVector(UnaryOperator(type="ln")(self.testInputLowX), - [-69.07755, -69.07755, -69.07755, -69.07755]) + [-69.07755, -69.07755, -69.07755, -69.07755]) def testLin2Db(self): self.assertAlmostEqualVector( @@ -107,6 +107,20 @@ def testSquare(self): def testInvalidParam(self): self.assertConfigureFails(UnaryOperator(), {'type':'exp'}) + def testOperatorScaleShift(self): + functions = ["identity", "abs", "log10", "log", "ln", "lin2db", "db2lin", "sin", "cos", "sqrt", "square"] + + shift = 1 + scale = 2 + test_data = [1] + + for type in functions: + with self.subTest(i=type): + + expected = UnaryOperator(type=type)(test_data) * scale + shift + found = UnaryOperator(type=type, shift=shift, scale=scale)(test_data) + + self.assertAlmostEqualVector(expected, found) suite = allTests(TestUnaryOperator) From 3e622e9a1bfb94122ba51bc22c710db4394793de Mon Sep 17 00:00:00 2001 From: Pablo Alonso Date: Thu, 29 Jun 2023 09:00:39 +0200 Subject: [PATCH 2/2] Fix UnaryOperator APPLY_FUNCTION should not return; but break; so that the scale and shift steps are correctly applied later. --- src/algorithms/standard/unaryoperator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/standard/unaryoperator.cpp b/src/algorithms/standard/unaryoperator.cpp index a5f436be0..5a41d15da 100644 --- a/src/algorithms/standard/unaryoperator.cpp +++ b/src/algorithms/standard/unaryoperator.cpp @@ -58,7 +58,7 @@ inline Real square_func(Real x) { for (int i=0; i