Fix after 55ed984 about extraction of real polynoms from complex matrix of poylnoms 86/21286/3
Antoine ELIAS [Thu, 6 Feb 2020 14:26:10 +0000 (15:26 +0100)]
Add some non regression tests to covert all extraction sub functions in ArrayOf::extract.

Change-Id: I533386dcd59b16ccc50eba22cc10726821aea71e

scilab/modules/ast/includes/types/arrayof.hxx
scilab/modules/ast/includes/types/polynom.hxx
scilab/modules/ast/includes/types/singlepoly.hxx
scilab/modules/ast/src/cpp/types/arrayof.cpp
scilab/modules/ast/src/cpp/types/polynom.cpp
scilab/modules/ast/src/cpp/types/singlepoly.cpp
scilab/modules/ast/tests/nonreg_tests/bug_16197.tst

index 1d686c9..26929aa 100644 (file)
@@ -192,6 +192,11 @@ public :
         return m_pImgData != NULL;
     }
 
+    virtual bool isComplexElement(int idx)
+    {
+        return isComplex();
+    }
+
     //type does not need to delete or clone ( int, double, ... )
     virtual bool isNativeType()
     {
index b548b17..968d4ca 100644 (file)
@@ -69,6 +69,7 @@ public :
     }
 
     bool isComplex(void) override;
+    bool isComplexElement(int i) override;
 
     bool isDollar() override;
     inline bool             isPoly() override
index 2000278..bdd1b3b 100644 (file)
@@ -33,7 +33,6 @@ public :
     virtual void            deleteAll() override;
     virtual void            deleteImg() override;
 
-    // FIXME : Should not return NULL;
     SinglePoly*             clone() override;
     SinglePoly*             conjugate();
 
@@ -41,6 +40,8 @@ public :
     {
         return true;
     }
+
+    bool                    hasComplexCoef();
     /*Config management*/
     void                    whoAmI() override;
 
index 87158b8..aed56fe 100644 (file)
@@ -1083,12 +1083,19 @@ GenericType* ArrayOf<T>::extract(typed_list* _pArgs)
         int dims[2] = {1, 1};
         pOut = createEmpty(2, dims, false);
         pOut->set(0, get(index));
-        if (isComplex() && getImg(index) !=0)
+        bool c = isNativeType() ? getImg(index) != 0 : isComplexElement(index);
+        if (isComplex() && c)
         {
             pOut->setComplex(true);
             pOut->setImg(0, getImg(index));
         }
+        else
+        {
+            pOut->setComplex(false);
+        }
+
         
+
         return pOut;
     }
 
@@ -1131,9 +1138,17 @@ GenericType* ArrayOf<T>::extract(typed_list* _pArgs)
                 T iValue = getImg(index);
                 pOut->set(i, get(index));
                 pOut->setImg(i, iValue);
-                bIsComplex |= (iValue !=0);
+                if (isNativeType())
+                {
+                    bIsComplex |= (iValue != 0);
+                }
+                else
+                {
+                    bIsComplex |= (isComplexElement(index) != 0);
+                }
                 idx += step;
             }
+
             pOut->setComplex(bIsComplex);
         }
         else
@@ -1182,7 +1197,15 @@ GenericType* ArrayOf<T>::extract(typed_list* _pArgs)
                 T iValue = getImg(i);
                 pOut->set(idx, get(i));
                 pOut->setImg(idx, iValue);
-                bIsComplex |= (iValue !=0);
+                if (isNativeType())
+                {
+                    bIsComplex |= (iValue != 0);
+                }
+                else
+                {
+                    bIsComplex |= (isComplexElement(i) != 0);
+                }
+
                 ++idx;
             }
             pOut->setComplex(bIsComplex);
@@ -1408,11 +1431,18 @@ GenericType* ArrayOf<T>::extract(typed_list* _pArgs)
         }
 
         pOut->set(i, get(iPos));
-        if (m_pImgData != NULL)
+        if (isComplex())
         {
             T iValue = getImg(iPos);
             pOut->setImg(i, iValue);
-            bIsComplex |= (iValue != 0);   
+            if (isNativeType())
+            {
+                bIsComplex |= (iValue != 0);
+            }
+            else
+            {
+                bIsComplex |= (isComplexElement(iPos) != 0);
+            }
         }
 
         piIndex[0]++;
index 2fadc71..e210e77 100644 (file)
@@ -244,6 +244,16 @@ bool Polynom::isComplex()
     return false;
 }
 
+bool Polynom::isComplexElement(int i)
+{
+    if (i < getSize())
+    {
+        return get(i)->hasComplexCoef();
+    }
+
+    return isComplex();
+}
+
 Polynom* Polynom::setComplex(bool _bComplex)
 {
     if (_bComplex == isComplex())
index 9a3b39a..db60925 100644 (file)
@@ -127,6 +127,22 @@ double* SinglePoly::allocData(int _iSize)
     return pDbl;
 }
 
+bool SinglePoly::hasComplexCoef()
+{
+    if (m_pImgData)
+    {
+        for (int i = 0; i < m_iSize; ++i)
+        {
+            if (m_pImgData[i] != 0)
+            {
+                return true;
+            }
+        }
+    }
+
+    return false;
+}
+
 double SinglePoly::copyValue(double _dblData)
 {
     return _dblData;
index c7d4bf6..3dfa749 100644 (file)
 // <-- Short Description -->
 // Extraction of real components of complex matrices is counterintuitive
 
-p=poly([1 1 1 1],"x","coeff"); 
-r=roots(p);
+x = [1+%i 1 2 3 1-%i];
+s = x * %s;
 
-assert_checktrue(isreal(r(1)) | isreal(r(2)) | isreal(r(3)))
+//single index
+//double
+assert_checkfalse(isreal(x(1)));
+assert_checktrue(isreal(x(2)));
+assert_checktrue(isreal(x(3)));
+assert_checktrue(isreal(x(4)));
+assert_checkfalse(isreal(x(5)));
 
-x = [1+%i 1 2 3 1-%i];
+//poly
+assert_checkfalse(isreal(s(1)));
+assert_checktrue(isreal(s(2)));
+assert_checktrue(isreal(s(3)));
+assert_checktrue(isreal(s(4)));
+assert_checkfalse(isreal(s(5)));
+
+//implicit list
+//double
+assert_checkfalse(isreal(x(1:4:5)));
+assert_checktrue(isreal(x(2:4)));
+
+//poly
+assert_checkfalse(isreal(s(1:4:5)));
+assert_checktrue(isreal(s(2:4)));
+
+//implicit list > 1 dim
+//double
+assert_checkfalse(isreal(x(1, 1:4:5)));
+assert_checktrue(isreal(x(1, 2:4)));
+
+//poly
+assert_checkfalse(isreal(s(1:4:$)));
+assert_checktrue(isreal(s(2:$-1)));
+
+//Matrix index
+//double
+assert_checkfalse(isreal(x([1 5])));
+assert_checktrue(isreal(x([2 3 4])));
+
+//poly
+assert_checkfalse(isreal(s([1 5])));
+assert_checktrue(isreal(s([2 3 4])));
 
-assert_checktrue(isreal(x(2:4)));
\ No newline at end of file