d5ab0e3f108744421caf8b469c2673b2ced8833e
[scilab.git] / scilab / modules / elementary_functions / sci_gateway / cpp / elem_func_gw.cpp
1 /*
2 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 *  Copyright (C) 2008-2008 - DIGITEO - Antoine ELIAS
4 *  Copyright (C) 2011-2011 - DIGITEO - Bruno JOFRET
5 *
6  * Copyright (C) 2012 - 2016 - Scilab Enterprises
7  *
8  * This file is hereby licensed under the terms of the GNU GPL v2.0,
9  * pursuant to article 5.3.4 of the CeCILL v.2.1.
10  * This file was originally licensed under the terms of the CeCILL v2.1,
11  * and continues to be available under such terms.
12  * For more information, see the COPYING file which you should have received
13  * along with this program.
14 *
15 */
16 //for Visual Leak Detector in debug compilation mode
17 //#define DEBUG_VLD
18 #if defined(DEBUG_VLD) && defined(_DEBUG)
19 #include <vld.h>
20 #endif
21
22 #include "elem_func_gw.hxx"
23 #include "context.hxx"
24 #include "overload.hxx"
25 #include "Scierror.h"
26 #include "localization.h"
27 #include "charEncoding.h"
28 #include "sparse.hxx"
29 #include "int.hxx"
30
31 #define MODULE_NAME L"elementary_functions"
32 extern "C"
33 {
34 #include "gw_elementary_functions.h"
35 }
36
37 int ElemFuncModule::Load()
38 {
39     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"abs", &sci_abs, MODULE_NAME));
40     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"acos", &sci_acos, MODULE_NAME));
41     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"asin", &sci_asin, MODULE_NAME));
42     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"atan", &sci_atan, MODULE_NAME));
43     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"bitstring", &sci_bitstring, MODULE_NAME));
44     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"ceil", &sci_ceil, MODULE_NAME));
45     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"clean", &sci_clean, MODULE_NAME));
46     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"conj", &sci_conj, MODULE_NAME));
47     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"cos", &sci_cos, MODULE_NAME));
48     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"cumprod", &sci_cumprod, MODULE_NAME));
49     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"cumsum", &sci_cumsum, MODULE_NAME));
50     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"diag", &sci_diag, MODULE_NAME));
51     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"dsearch", &sci_dsearch, MODULE_NAME));
52     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"exp", &sci_exp, MODULE_NAME));
53     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"expm", &sci_expm, MODULE_NAME));
54     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"eye", &sci_eye, MODULE_NAME));
55     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"floor", &sci_floor, MODULE_NAME));
56     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"frexp", &sci_frexp, MODULE_NAME));
57     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"gsort", &sci_gsort, MODULE_NAME));
58     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"imag", &sci_imag, MODULE_NAME));
59     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"imult", &sci_imult, MODULE_NAME));
60     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"int", &sci_int, MODULE_NAME));
61     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"isreal", &sci_isreal, MODULE_NAME));
62     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"isequal", &sci_isequal, MODULE_NAME));
63     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"kron", &sci_kron, MODULE_NAME));
64     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"log", &sci_log, MODULE_NAME));
65     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"log1p", &sci_log1p, MODULE_NAME));
66     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"matrix", &sci_matrix, MODULE_NAME));
67     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"max", &sci_max, MODULE_NAME));
68     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"min", &sci_min, MODULE_NAME));
69     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"nearfloat", &sci_nearfloat, MODULE_NAME));
70     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"number_properties", &sci_number_properties, MODULE_NAME));
71     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"ones", &sci_ones, MODULE_NAME));
72     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"prod", &sci_prod, MODULE_NAME));
73     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"permute", &sci_permute, MODULE_NAME));
74     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"rand", &sci_rand, MODULE_NAME));
75     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"real", &sci_real, MODULE_NAME));
76     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"round", &sci_round, MODULE_NAME));
77     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"sign", &sci_sign, MODULE_NAME));
78     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"sin", &sci_sin, MODULE_NAME));
79     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"size", &sci_size, MODULE_NAME));
80     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"sqrt", &sci_sqrt, MODULE_NAME));
81     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"sum", &sci_sum, MODULE_NAME));
82     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"tan", &sci_tan, MODULE_NAME));
83     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"testmatrix", &sci_testmatrix, MODULE_NAME));
84     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"tril", &sci_tril, MODULE_NAME));
85     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"triu", &sci_triu, MODULE_NAME));
86     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"zeros", &sci_zeros, MODULE_NAME));
87     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"rat", &sci_rat, MODULE_NAME));
88     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"base2dec", &sci_base2dec, MODULE_NAME));
89     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"dec2base", &sci_dec2base, MODULE_NAME));
90     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"log10", &sci_log10, MODULE_NAME));
91     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"cosh", &sci_cosh, MODULE_NAME));
92     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"sinh", &sci_sinh, MODULE_NAME));
93     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"tanh", &sci_tanh, MODULE_NAME));
94     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"acosh", &sci_acosh, MODULE_NAME));
95     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"asinh", &sci_asinh, MODULE_NAME));
96     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"atanh", &sci_atanh, MODULE_NAME));
97     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"isvector", &sci_isvector, MODULE_NAME));
98     symbol::Context::getInstance()->addFunction(types::Function::createFunction(L"issquare", &sci_issquare, MODULE_NAME));
99     return 1;
100 }
101
102
103 bool getDimsFromArguments(types::typed_list& in, const std::string& _pstName, int* _iDims, int** _piDims, bool* _alloc)
104 {
105     types::Double* pOut = 0;
106
107     *_alloc = false;
108     *_iDims = 0;
109     *_piDims = NULL;
110
111     if (in.size() == 0)
112     {
113         *_iDims = 2;
114         *_piDims = new int[*_iDims];
115         (*_piDims)[0] = 1;
116         (*_piDims)[1] = 1;
117         *_alloc = true;
118         return true;
119     }
120     else if (in.size() == 1)
121     {
122         *_iDims = 1;
123         // :
124         if (in[0]->isColon())
125         {
126             *_iDims = -1;
127             return false;
128         }
129
130         if (in[0]->isArrayOf() == false)
131         {
132             if (in[0]->isSparse())
133             {
134                 types::Sparse* sp = in[0]->getAs<types::Sparse>();
135                 *_iDims = sp->getDims();
136                 *_piDims = sp->getDimsArray();
137                 return true;
138             }
139             else if (in[0]->isSparseBool())
140             {
141                 types::SparseBool* sp = in[0]->getAs<types::SparseBool>();
142                 *_iDims = sp->getDims();
143                 *_piDims = sp->getDimsArray();
144                 return true;
145             }
146             return false;
147         }
148
149         types::GenericType* pIn = in[0]->getAs<types::GenericType>();
150         *_iDims = pIn->getDims();
151         *_piDims = pIn->getDimsArray();
152
153         return true;
154     }
155     else
156     {
157         *_iDims = static_cast<int>(in.size());
158         *_piDims = new int[*_iDims];
159         *_alloc = true;
160         for (int i = 0; i < *_iDims; i++)
161         {
162             if (in[i]->isArrayOf() == false)
163             {
164                 Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), _pstName.c_str(), i + 1);
165                 delete[] * _piDims;
166                 return false;
167             }
168
169             types::GenericType* pGTIn = in[i]->getAs<types::GenericType>();
170             if (pGTIn->isScalar() == false || pGTIn->isComplex())
171             {
172                 Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), _pstName.c_str(), i + 1);
173                 delete[] * _piDims;
174                 return false;
175             }
176
177             switch (in[i]->getType())
178             {
179                 case types::InternalType::ScilabDouble:
180                 {
181                     double dValue = in[i]->getAs<types::Double>()->get(0);
182                     if (dValue >= INT_MAX)
183                     {
184                         Scierror(999, _("%s: variable size exceeded : less than %d expected.\n"), _pstName.c_str(), INT_MAX);
185                         delete[] * _piDims;
186                         return false;
187                     }
188                     (*_piDims)[i] = static_cast<int>(dValue);
189                 }
190                 break;
191                 case types::InternalType::ScilabInt8:
192                     (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::Int8>()->get()[0]);
193                     break;
194                 case types::InternalType::ScilabUInt8:
195                     (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::UInt8>()->get()[0]);
196                     break;
197                 case types::InternalType::ScilabInt16:
198                     (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::Int16>()->get()[0]);
199                     break;
200                 case types::InternalType::ScilabUInt16:
201                     (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::UInt16>()->get()[0]);
202                     break;
203                 case types::InternalType::ScilabInt32:
204                     (*_piDims)[i] = in[i]->getAs<types::Int32>()->get()[0];
205                     break;
206                 case types::InternalType::ScilabUInt32:
207                     (*_piDims)[i] = static_cast<int>(in[i]->getAs<types::UInt32>()->get()[0]);
208                     break;
209                 case types::InternalType::ScilabInt64:
210                 {
211                     long long llValue = in[i]->getAs<types::Int64>()->get(0);
212                     if (llValue >= INT_MAX)
213                     {
214                         Scierror(999, _("%s: variable size exceeded : less than %d expected.\n"), _pstName.c_str(), INT_MAX);
215                         delete[] * _piDims;
216                         return false;
217                     }
218                     (*_piDims)[i] = static_cast<int>(llValue);
219                     break;
220                 }
221                 case types::InternalType::ScilabUInt64:
222                 {
223                     unsigned long long ullValue = in[i]->getAs<types::UInt64>()->get(0);
224                     if (ullValue >= INT_MAX)
225                     {
226                         Scierror(999, _("%s: variable size exceeded : less than %d expected.\n"), _pstName.c_str(), INT_MAX);
227                         delete[] * _piDims;
228                         return false;
229                     }
230                     (*_piDims)[i] = static_cast<int>(ullValue);
231                     break;
232                 }
233                 default:
234                     Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), _pstName.c_str(), i + 1);
235                     delete[] * _piDims;
236                     return false;
237             }
238         }
239
240         return true;
241     }
242
243     return false;
244 }
245
246 types::Double* trigo(types::Double* in, func_real func_r, func_complex func_c, bool forceComplex)
247 {
248     bool isComplex = in->isComplex() || forceComplex;
249     types::Double* out = new types::Double(in->getDims(), in->getDimsArray(), isComplex);
250
251     int size = in->getSize();
252     double* pInR = in->get();
253     double* pOutR = out->get();
254
255     if (isComplex)
256     {
257         double* pInI = in->getImg();
258         double* pOutI = out->getImg();
259         std::complex<double> d;
260         for (int i = 0; i < size; ++i)
261         {
262             d.real(pInR[i]);
263             d.imag(pInI[i]);
264             std::complex<double> res = func_c(d);
265             pOutR[i] = res.real();
266             pOutI[i] = res.imag();
267         }
268     }
269     else
270     {
271         for (int i = 0; i < size; ++i)
272         {
273             pOutR[i] = func_r(pInR[i]);
274         }
275     }
276
277     return out;
278 }