Merge remote-tracking branch 'origin/master' into windows
[scilab.git] / scilab / modules / elementary_functions / sci_gateway / cpp / sci_imag.cpp
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - DIGITEO - Cedric DELAMARRE
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 /*--------------------------------------------------------------------------*/
16
17 #include <complex>
18
19 #include "elem_func_gw.hxx"
20 #include "function.hxx"
21 #include "double.hxx"
22 #include "overload.hxx"
23 #include "sparse.hxx"
24 #include "polynom.hxx"
25
26 extern "C"
27 {
28 #include "Scierror.h"
29 #include "localization.h"
30 #include "elem_common.h"
31 }
32
33 /*--------------------------------------------------------------------------*/
34 types::Function::ReturnValue sci_imag(types::typed_list &in, int _iRetCount, types::typed_list &out)
35 {
36     if (in.size() != 1)
37     {
38         Scierror(77, _("%s: Wrong number of input argument(s): %d expected.\n"), "imag", 1);
39         return types::Function::Error;
40     }
41
42     if (_iRetCount > 1)
43     {
44         Scierror(78, _("%s: Wrong number of output argument(s): %d expected.\n"), "imag", 1);
45         return types::Function::Error;
46     }
47
48     if (in[0]->isDouble())
49     {
50         types::Double* pDblIn = in[0]->getAs<types::Double>();
51
52         int iSize = pDblIn->getSize();
53         int iOne = 1;
54
55         types::Double* pDblOut = new types::Double(pDblIn->getDims(), pDblIn->getDimsArray());
56
57         if (pDblIn->isComplex() == false)
58         {
59             memset(pDblOut->get(), 0x00, iSize * sizeof(double));
60         }
61         else
62         {
63             C2F(dcopy)(&iSize, pDblIn->getImg(), &iOne, pDblOut->getReal(), &iOne);
64         }
65
66
67         out.push_back(pDblOut);
68     }
69     else if (in[0]->isSparse())
70     {
71         types::Sparse* pSparseIn = in[0]->getAs<types::Sparse>();
72         types::Sparse* pSparseOut = new types::Sparse(pSparseIn->getRows(), pSparseIn->getCols());
73
74         if (pSparseIn->isComplex() == false)
75         {
76             out.push_back(pSparseOut);
77             return types::Function::OK;
78         }
79
80         int const nonZeros = static_cast<int>(pSparseIn->nonZeros());
81         int* pRows = new int[nonZeros * 2];
82         pSparseIn->outputRowCol(pRows);
83         int* pCols = pRows + nonZeros;
84
85         for (int i = 0 ; i < nonZeros ; i++)
86         {
87             std::complex<double> cplx = pSparseIn->getImg(pRows[i] - 1, pCols[i] - 1);
88             pSparseOut->set(pRows[i] - 1, pCols[i] - 1, cplx.imag(), false);
89         }
90
91         pSparseOut->finalize();
92
93         delete[] pRows;
94
95         out.push_back(pSparseOut);
96     }
97     else if (in[0]->isPoly())
98     {
99         types::Polynom* pPolyIn  = in[0]->getAs<types::Polynom>();
100         types::Polynom* pPolyOut = NULL;
101
102         if (pPolyIn->isComplex())
103         {
104             pPolyOut = new types::Polynom(pPolyIn->getVariableName(), pPolyIn->getDims(), pPolyIn->getDimsArray());
105             for (int i = 0; i < pPolyIn->getSize(); i++)
106             {
107                 int rank = pPolyIn->get(i)->getRank();
108                 int iNewRank = rank;
109
110                 // Reduce the rank of output polynom if the last ranks are null
111                 for (int j = rank ; j > 0 ; j--)
112                 {
113                     if (pPolyIn->get(i)->getImg()[j] == 0.0)
114                     {
115                         iNewRank--;
116                     }
117                     else
118                     {
119                         break;
120                     }
121                 }
122
123                 double* dataReal = NULL;
124                 types::SinglePoly* pSP = new types::SinglePoly(&dataReal, iNewRank);
125
126                 for (int j = 0; j < iNewRank + 1; j++)
127                 {
128                     dataReal[j] = pPolyIn->get(i)->getImg()[j];
129                 }
130
131                 pPolyOut->set(i, pSP);
132                 delete pSP;
133                 pSP = NULL;
134             }
135         }
136         else
137         {
138             int iSize = pPolyIn->getSize();
139             int* piRanks = new int[iSize];
140             memset(piRanks, 0x00, iSize * sizeof(int));
141             pPolyOut = new types::Polynom(pPolyIn->getVariableName(), pPolyIn->getDims(), pPolyIn->getDimsArray(), piRanks);
142             pPolyOut->setZeros();
143             delete[] piRanks;
144         }
145
146         out.push_back(pPolyOut);
147     }
148     else
149     {
150         std::string stFuncName = "%" + in[0]->getShortTypeStr() + "_imag";
151         return Overload::call(stFuncName, in, _iRetCount, out);
152     }
153
154     return types::Function::OK;
155 }
156 /*--------------------------------------------------------------------------*/