* Bug 4953 fixed: now clean() works when the argument contains %inf 04/20204/12
St├ęphane Mottelet [Thu, 28 Jun 2018 13:21:17 +0000 (15:21 +0200)]
http://bugzilla.scilab.org/4953

Change-Id: I36f6dce1e0f81b5eb590c7b61c9d787128e7ff98

scilab/modules/elementary_functions/src/cpp/clean.cpp
scilab/modules/elementary_functions/tests/nonreg_tests/bug_4953.tst [new file with mode: 0644]

index 1933abb..a7f1eec 100644 (file)
@@ -14,7 +14,7 @@
  */
 /*--------------------------------------------------------------------------*/
 #include "clean.hxx"
-
+#include <cmath>
 extern "C"
 {
 #include "basic_functions.h"
@@ -25,7 +25,16 @@ void clean(double* pdblReal, double* pdblImg, int iSize, double dEpsA, double dE
 {
     if (pdblImg)
     {
-        double dNorm = wasums(iSize, pdblReal, pdblImg);
+        double dNorm = 0;
+        for (int i = 0 ; i < iSize ; i++)
+        {
+            double d = dabss(pdblReal[i])+dabss(pdblImg[i]);
+            if (std::isfinite(d))
+            {
+               dNorm += d;
+            }
+        }
+
         double dEps = Max(dEpsA, dEpsR * dNorm);
         for (int i = 0 ; i < iSize ; i++)
         {
@@ -43,7 +52,15 @@ void clean(double* pdblReal, double* pdblImg, int iSize, double dEpsA, double dE
     else
     {
         int iOne = 1;
-        double dNorm = C2F(dasum)(&iSize, pdblReal, &iOne);
+        double dNorm = 0;
+        for (int i = 0 ; i < iSize ; i++)
+        {
+            double d = dabss(pdblReal[i]);
+            if (std::isfinite(d))
+            {
+               dNorm += d;
+            }
+        }
         double dEps = Max(dEpsA, dEpsR * dNorm);
         for (int i = 0 ; i < iSize ; i++)
         {
diff --git a/scilab/modules/elementary_functions/tests/nonreg_tests/bug_4953.tst b/scilab/modules/elementary_functions/tests/nonreg_tests/bug_4953.tst
new file mode 100644 (file)
index 0000000..5df9a9d
--- /dev/null
@@ -0,0 +1,52 @@
+// =============================================================================
+// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
+// Copyright (C) 2018 - St├ęphane Mottelet
+//
+//  This file is distributed under the same license as the Scilab package.
+// =============================================================================
+//
+// <-- CLI SHELL MODE -->
+// <-- NO CHECK REF -->
+//
+// <-- Non-regression test for bug 4953 -->
+//
+// <-- Bugzilla URL -->
+// http://bugzilla.scilab.org/4953
+//
+// <-- Short Description -->
+// The clean function does not work when the matrix contains the %inf value.
+
+// with %inf
+big = %inf
+small = 1;
+a1 = [big small];
+a2 = [big complex(small,small)];
+a3 = [complex(0,big) small complex(big,small)];
+//double
+assert_checkequal(clean(a1),a1);
+assert_checkequal(clean(a2),a2);
+assert_checkequal(clean(a3),a3);
+//polynomial
+assert_checkequal(clean(poly(a1,"x","coeff")),poly(a1,"x","coeff"));
+assert_checkequal(clean(poly(a2,"x","coeff")),poly(a2,"x","coeff"));
+assert_checkequal(clean(poly(a3,"x","coeff")),poly(a3,"x","coeff"));
+//sparse
+assert_checkequal(clean(sparse(a1)),sparse(a1));
+assert_checkequal(clean(sparse(a2)),sparse(a2));
+assert_checkequal(clean(sparse(a3)),sparse(a3));
+
+// with %nan
+big = 1e30
+small = 1;
+a4 = [small big %nan];
+b4 = [0 big %nan];
+//double
+c4 = clean(a4);
+assert_checktrue(c4(1:2) == b4(1:2) && isnan(c4(3)))
+//poly
+c4 = clean(poly(a4,"x","coeff"));
+assert_checktrue(coeff(c4)(1:2) == b4(1:2) && isnan(coeff(c4)(3)))
+//sparse
+c4 = clean(sparse(a4));
+assert_checktrue(c4(1:2) == b4(1:2) && isnan(c4(3)))
+