[ast] update sparse extraction after 55ed9848 37/21237/5
St├ęphane Mottelet [Mon, 20 Jan 2020 11:05:07 +0000 (12:05 +0100)]
--> test_run ast sparse
   TMPDIR = /tmp/SCI_TMP_44818_TfHLNe

   001/001 - [ast] sparse.......................................passed

   --------------------------------------------------------------------------
   Summary

   tests              1 - 100 %
   passed             1 - 100 %
   failed             0 -   0 %
   skipped            0
   length             0.66 sec
   --------------------------------------------------------------------------
 ans  =

  T

--> test_run sparse
   TMPDIR = /tmp/SCI_TMP_10302_vHFecU

 Module  001/001 - [sparse] :

   001/053 - [sparse] adj2sp....................................passed
   002/053 - [sparse] conjgrad..................................passed
   003/053 - [sparse] conjgrad_function.........................passed
   004/053 - [sparse] conjgrad_list.............................passed
   005/053 - [sparse] conjgrad_numerical........................passed
   006/053 - [sparse] conjgrad_sparse...........................passed
   007/053 - [sparse] lsparse...................................passed
   008/053 - [sparse] nnz.......................................passed
   009/053 - [sparse] ordmmd....................................passed
   010/053 - [sparse] qmr.......................................passed
   011/053 - [sparse] sp2adj....................................passed
   012/053 - [sparse] sparse....................................passed
   013/053 - [sparse] sparse-extract............................passed
   014/053 - [sparse] sparse-insert.............................passed
   015/053 - [sparse] sparseBool................................passed
   016/053 - [sparse] spisp.....................................passed
   017/053 - [sparse] sprand....................................passed
   018/053 - [sparse] spzeros...................................passed
   019/053 - [sparse] bug_10126.................................passed
   020/053 - [sparse] bug_10271.................................passed
   021/053 - [sparse] bug_11612.................................passed
   022/053 - [sparse] bug_11618.................................passed
   023/053 - [sparse] bug_13838.................................passed
   024/053 - [sparse] bug_14271.................................passed
   025/053 - [sparse] bug_14330.................................passed
   026/053 - [sparse] bug_15246.................................passed
   027/053 - [sparse] bug_15647.................................passed
   028/053 - [sparse] bug_15648.................................passed
   029/053 - [sparse] bug_15750.................................passed
   030/053 - [sparse] bug_15753.................................passed
   031/053 - [sparse] bug_15758.................................passed
   032/053 - [sparse] bug_16007.................................passed
   033/053 - [sparse] bug_16260.................................passed
   034/053 - [sparse] bug_16272.................................passed
   035/053 - [sparse] bug_2003..................................passed
   036/053 - [sparse] bug_2277..................................passed
   037/053 - [sparse] bug_2430..................................passed
   038/053 - [sparse] bug_3006..................................passed
   039/053 - [sparse] bug_3025..................................passed
   040/053 - [sparse] bug_3263..................................passed
   041/053 - [sparse] bug_4654..................................passed
   042/053 - [sparse] bug_4783..................................passed
   043/053 - [sparse] bug_490...................................passed
   044/053 - [sparse] bug_6401..................................passed
   045/053 - [sparse] bug_6412..................................passed
   046/053 - [sparse] bug_6427..................................passed
   047/053 - [sparse] bug_6827..................................passed
   048/053 - [sparse] bug_7675..................................passed
   049/053 - [sparse] bug_8511..................................passed
   050/053 - [sparse] bug_8938..................................passed
   051/053 - [sparse] bug_9161..................................passed
   052/053 - [sparse] bug_9780..................................passed
   053/053 - [sparse] bug_9999..................................passed

   --------------------------------------------------------------------------
   Summary

   tests             53 - 100 %
   passed            53 - 100 %
   failed             0 -   0 %
   skipped            0
   length             41.33 sec
   --------------------------------------------------------------------------
 ans  =

  T

Change-Id: I43fdee90474ec78fdf89df039d20f9f8a9ee6058

scilab/modules/ast/src/cpp/types/sparse.cpp

index 06769c5..5e6cd64 100644 (file)
@@ -2058,8 +2058,9 @@ GenericType* Sparse::extract(typed_list* _pArgs)
             double* pIdx = pArg[0]->getAs<Double>()->get();
             if (isComplex())
             {
-                CplxSparse_t* cplx = new CplxSparse_t(iNewRows, iNewCols);
+                bool bIsReal = true;
                 std::vector<CplxTriplet_t> tripletList;
+                std::vector<RealTriplet_t> realTripletList;
                 int row = getRows();
                 for (int i = 0; i < iSeqCount; i++)
                 {
@@ -2073,11 +2074,25 @@ GenericType* Sparse::extract(typed_list* _pArgs)
                     {
                         //only non zero values
                         tripletList.emplace_back(iRowWrite, iColWrite, dbl);
+                        bIsReal &= (dbl.imag() == 0);
+                        if (bIsReal)
+                        {
+                            realTripletList.emplace_back(iRowWrite, iColWrite, dbl.real());
+                        }
                     }
                 }
-
-                cplx->setFromTriplets(tripletList.begin(), tripletList.end(), DupFunctor<std::complex<double>>());
-                pOut = new Sparse(nullptr, cplx);
+                if (bIsReal)
+                {
+                    RealSparse_t* real = new RealSparse_t(iNewRows, iNewCols);
+                    real->setFromTriplets(realTripletList.begin(), realTripletList.end(), DupFunctor<double>());
+                    pOut = new Sparse(real, nullptr);
+                }
+                else
+                {
+                    CplxSparse_t* cplx = new CplxSparse_t(iNewRows, iNewCols);
+                    cplx->setFromTriplets(tripletList.begin(), tripletList.end(), DupFunctor<std::complex<double>>());
+                    pOut = new Sparse(nullptr, cplx);
+                }
             }
             else
             {
@@ -2124,8 +2139,10 @@ GenericType* Sparse::extract(typed_list* _pArgs)
 
             if (isComplex())
             {
-                CplxSparse_t* cplx = new CplxSparse_t(iNewRows, iNewCols);
+                bool bIsReal = true;
                 std::vector<CplxTriplet_t> tripletList;
+                std::vector<RealTriplet_t> realTripletList;
+
                 for (int iRow = 0; iRow < iNewRows; iRow++)
                 {
                     for (int iCol = 0; iCol < iNewCols; iCol++)
@@ -2135,12 +2152,26 @@ GenericType* Sparse::extract(typed_list* _pArgs)
                         {
                             //only non zero values
                             tripletList.emplace_back(iRow, iCol, dbl);
+                            bIsReal &= (dbl.imag() == 0.);
+                            if (bIsReal)
+                            {
+                                realTripletList.emplace_back(iRow, iCol, dbl.real());
+                            }
                         }
                     }
                 }
-
-                cplx->setFromTriplets(tripletList.begin(), tripletList.end(), DupFunctor<std::complex<double>>());
-                pOut = new Sparse(nullptr, cplx);
+                if (bIsReal)
+                {
+                    RealSparse_t* real = new RealSparse_t(iNewRows, iNewCols);
+                    real->setFromTriplets(realTripletList.begin(), realTripletList.end(), DupFunctor<double>());
+                    pOut = new Sparse(real, nullptr);
+                }
+                else
+                {
+                    CplxSparse_t* cplx = new CplxSparse_t(iNewRows, iNewCols);
+                    cplx->setFromTriplets(tripletList.begin(), tripletList.end(), DupFunctor<std::complex<double>>());
+                    pOut = new Sparse(nullptr, cplx);
+                }
             }
             else
             {