elementary_functions module.
[scilab.git] / scilab / modules / elementary_functions / sci_gateway / cpp / sci_ceil.cpp
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - DIGITEO - Cedric DELAMARRE
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12 /*--------------------------------------------------------------------------*/
13 #include "elem_func_gw.hxx"
14 #include "function.hxx"
15 #include "double.hxx"
16 #include "overload.hxx"
17 #include "execvisitor.hxx"
18
19
20 extern "C"
21 {
22 #include "Scierror.h"
23 #include "localization.h"
24 #include "basic_functions.h"
25 }
26
27 /*--------------------------------------------------------------------------*/
28 types::Function::ReturnValue sci_ceil(types::typed_list &in, int _iRetCount, types::typed_list &out)
29 {
30     if (in.size() != 1)
31     {
32         ScierrorW(77, _W("%ls: Wrong number of input argument(s): %d expected.\n"), L"ceil", 1);
33         return types::Function::Error;
34     }
35
36     if (_iRetCount > 1)
37     {
38         ScierrorW(78, _W("%ls: Wrong number of output argument(s): %d expected.\n"), L"ceil", 1);
39         return types::Function::Error;
40     }
41
42     if (in[0]->isDouble())
43     {
44         types::Double* pDblIn = in[0]->getAs<types::Double>();
45         types::Double* pDblOut = new types::Double(pDblIn->getDims(), pDblIn->getDimsArray(), pDblIn->isComplex());
46
47         if (pDblIn->isComplex())
48         {
49             for (int i = 0 ; i < pDblIn->getSize() ; i++)
50             {
51                 pDblOut->setImg(i, dceils(pDblIn->getImg(i)));
52             }
53         }
54
55         for (int i = 0 ; i < pDblIn->getSize() ; i++)
56         {
57             pDblOut->set(i, dceils(pDblIn->get(i)));
58         }
59
60         out.push_back(pDblOut);
61     }
62     else if (in[0]->isSparse())
63     {
64         types::Sparse* pSparseIn = in[0]->getAs<types::Sparse>();
65         types::Sparse* pSparseOut = new types::Sparse(pSparseIn->getRows(), pSparseIn->getCols(), pSparseIn->isComplex());
66
67         int const nonZeros = static_cast<int>(pSparseIn->nonZeros());
68         double* pRows = new double[nonZeros * 2];
69         pSparseIn->outputRowCol(pRows);
70         double* pCols = pRows + nonZeros;
71
72         double* pNonZeroR = new double[nonZeros];
73         double* pNonZeroI = new double[nonZeros];
74         pSparseIn->outputValues(pNonZeroR, pNonZeroI);
75
76         if (pSparseIn->isComplex())
77         {
78             for (int i = 0; i < nonZeros; i++)
79             {
80                 std::complex<double> cplx(dceils(pNonZeroR[i]), dceils(pNonZeroI[i]));
81                 pSparseOut->set(pRows[i] - 1, pCols[i] - 1, cplx);
82             }
83         }
84         else
85         {
86             for (int i = 0; i < nonZeros; i++)
87             {
88                 pSparseOut->set(pRows[i] - 1, pCols[i] - 1, dceils(pNonZeroR[i]));
89             }
90         }
91
92         delete[] pRows;
93         delete[] pNonZeroR;
94         delete[] pNonZeroI;
95
96         out.push_back(pSparseOut);
97     }
98     else if (in[0]->isPoly())
99     {
100         types::Polynom* pPolyIn = in[0]->getAs<types::Polynom>();
101         types::Polynom* pPolyOut = new types::Polynom(pPolyIn->getVariableName(), pPolyIn->getDims(), pPolyIn->getDimsArray());
102         pPolyOut->setComplex(pPolyIn->isComplex());
103
104         double* dataImg  = NULL;
105         double* dataReal = NULL;
106
107         if (pPolyIn->isComplex())
108         {
109             for (int i = 0; i < pPolyIn->getSize(); i++)
110             {
111                 int rank = pPolyIn->get(i)->getRank();
112                 types::SinglePoly* pSP = new types::SinglePoly(&dataReal, &dataImg, rank);
113
114                 for (int j = 0; j < rank; j++)
115                 {
116                     dataReal[j] = dceils(pPolyIn->get(i)->getCoefReal()[j]);
117                     dataImg[j]  = dceils(pPolyIn->get(i)->getCoefImg()[j]);
118                 }
119
120                 pPolyOut->set(i, pSP);
121                 delete pSP;
122                 pSP = NULL;
123             }
124         }
125         else
126         {
127             for (int i = 0; i < pPolyIn->getSize(); i++)
128             {
129                 int rank = pPolyIn->get(i)->getRank();
130                 types::SinglePoly* pSP = new types::SinglePoly(&dataReal, rank);
131
132                 for (int j = 0; j < rank; j++)
133                 {
134                     dataReal[j] = dceils(pPolyIn->get(i)->getCoefReal()[j]);
135                 }
136
137                 pPolyOut->set(i, pSP);
138                 delete pSP;
139                 pSP = NULL;
140             }
141         }
142
143         out.push_back(pPolyOut);
144     }
145     else if (in[0]->isInt())
146     {
147         out.push_back(in[0]->getAs<types::InternalType>()->clone());
148         return types::Function::OK;
149     }
150     else
151     {
152         std::wstring wstFuncName = L"%"  + in[0]->getShortTypeStr() + L"_ceil";
153         return Overload::call(wstFuncName, in, _iRetCount, out, new ExecVisitor());
154     }
155
156     return types::Function::OK;
157 }
158 /*--------------------------------------------------------------------------*/