1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - Antoine ELIAS
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  * Please note that piece of code will be rewrited for the Scilab 6 family
15  * However, the API (profile of the functions in the header files) will be
16  * still available and supported in Scilab 6.
17  */
18 
19 #include "gatewaystruct.hxx"
20 #include "polynom.hxx"
21 #include "context.hxx"
22 
23 extern "C"
24 {
25 #include <string.h>
26 #include <stdlib.h>
27 #include "machine.h"
28 #include "call_scilab.h"
29 #include "api_scilab.h"
30 #include "api_internal_poly.h"
31 #include "api_internal_common.h"
32 #include "localization.h"
33 #include "sci_malloc.h"
34 #include "charEncoding.h"
35 }
36 
37 static int getCommonAllocatedSinglePoly(void* _pvCtx, int* _piAddress, int _iComplex, int* _piNbCoef, double** _pdblReal, double** _pdblImg);
38 static int getCommonAllocatedNamedSinglePoly(void* _pvCtx, const char* _pstName, int _iComplex, int* _piNbCoef, double** _pdblReal, double** _pdblImg);
39 static int getCommonAllocatedMatrixOfPoly(void* _pvCtx, int* _piAddress, int _iComplex, int* _piRows, int* _piCols, int** _piNbCoef, double*** _pdblReal, double*** _pdblImg);
40 static int getCommonAllocatedNamedMatrixOfPoly(void* _pvCtx, const char* _pstName, int _iComplex, int* _piRows, int* _piCols, int** _piNbCoef, double*** _pdblReal, double*** _pdblImg);
41 
42 
getPolyVariableName(void * _pvCtx,int * _piAddress,char * _pstVarName,int * _piVarNameLen)43 SciErr getPolyVariableName(void* _pvCtx, int* _piAddress, char* _pstVarName, int* _piVarNameLen)
44 {
45     SciErr sciErr = sciErrInit();
46 
47     if (_piAddress == NULL)
48     {
49         addErrorMessage(&sciErr, API_ERROR_INVALID_POINTER, _("%s: Invalid argument address"), "getPolyVariableName");
50         return sciErr;
51     }
52 
53     if (!((types::InternalType*)_piAddress)->isPoly())
54     {
55         addErrorMessage(&sciErr, API_ERROR_INVALID_TYPE, _("%s: Invalid argument type, %s expected"), "getPolyVariableName", _("polynomial matrix"));
56         return sciErr;
57     }
58 
59     if (*_piVarNameLen == 0)
60     {
61         *_piVarNameLen = (int)((types::InternalType*)_piAddress)->getAs<types::Polynom>()->getVariableName().size();
62         //No error
63     }
64 
65     if (_pstVarName == NULL)
66     {
67         return sciErr;
68     }
69 
70     char* pstTemp = wide_string_to_UTF8(((types::InternalType*)_piAddress)->getAs<types::Polynom>()->getVariableName().c_str());
71     strcpy(_pstVarName, pstTemp);
72     FREE(pstTemp);
73     *_piVarNameLen = static_cast<int>(strlen(_pstVarName));
74     return sciErr;
75 }
76 
getMatrixOfPoly(void * _pvCtx,int * _piAddress,int * _piRows,int * _piCols,int * _piNbCoef,double ** _pdblReal)77 SciErr getMatrixOfPoly(void* _pvCtx, int* _piAddress, int* _piRows, int* _piCols, int* _piNbCoef, double** _pdblReal)
78 {
79     return getCommonMatrixOfPoly(_pvCtx, _piAddress, 0, _piRows, _piCols, _piNbCoef, _pdblReal, NULL);
80 }
81 
getComplexMatrixOfPoly(void * _pvCtx,int * _piAddress,int * _piRows,int * _piCols,int * _piNbCoef,double ** _pdblReal,double ** _pdblImg)82 SciErr getComplexMatrixOfPoly(void* _pvCtx, int* _piAddress, int* _piRows, int* _piCols, int* _piNbCoef, double** _pdblReal, double** _pdblImg)
83 {
84     return getCommonMatrixOfPoly(_pvCtx, _piAddress, 1, _piRows, _piCols, _piNbCoef, _pdblReal, _pdblImg);
85 }
86 
getCommonMatrixOfPoly(void * _pvCtx,int * _piAddress,int _iComplex,int * _piRows,int * _piCols,int * _piNbCoef,double ** _pdblReal,double ** _pdblImg)87 SciErr getCommonMatrixOfPoly(void* _pvCtx, int* _piAddress, int _iComplex, int* _piRows, int* _piCols, int* _piNbCoef, double** _pdblReal, double** _pdblImg)
88 {
89     SciErr sciErr = sciErrInit();
90     int iType = 0;
91     int iSize = 0;
92     int *piOffset = NULL;
93     double *pdblReal = NULL;
94     double *pdblImg = NULL;
95 
96     if (_piAddress == NULL)
97     {
98         addErrorMessage(&sciErr, API_ERROR_INVALID_POINTER, _("%s: Invalid argument address"), _iComplex ? "getComplexMatrixOfPoly" : "getMatrixOfPoly");
99         return sciErr;
100     }
101 
102     sciErr = getVarType(_pvCtx, _piAddress, &iType);
103     if (sciErr.iErr)
104     {
105         addErrorMessage(&sciErr, API_ERROR_GET_POLY, _("%s: Unable to get argument #%d"), _iComplex ? "getComplexMatrixOfPoly" : "getMatrixOfPoly", getRhsFromAddress(_pvCtx, _piAddress));
106         return sciErr;
107     }
108 
109     if (iType != sci_poly)
110     {
111         addErrorMessage(&sciErr, API_ERROR_INVALID_TYPE, _("%s: Invalid argument type, %s expected"), _iComplex ? "getComplexMatrixOfPoly" : "getMatrixOfPoly", _("polynomial matrix"));
112         return sciErr;
113     }
114 
115     if (isVarComplex(_pvCtx, _piAddress) != _iComplex)
116     {
117         addErrorMessage(&sciErr, API_ERROR_INVALID_COMPLEXITY, _("%s: Bad call to get a non complex matrix"), _iComplex ? "getComplexMatrixOfPoly" : "getMatrixOfPoly");
118         return sciErr;
119     }
120 
121     sciErr = getVarDimension(_pvCtx, _piAddress, _piRows, _piCols);
122     if (sciErr.iErr)
123     {
124         addErrorMessage(&sciErr, API_ERROR_GET_POLY, _("%s: Unable to get argument #%d"), _iComplex ? "getComplexMatrixOfPoly" : "getMatrixOfPoly", getRhsFromAddress(_pvCtx, _piAddress));
125         return sciErr;
126     }
127 
128     iSize	= *_piRows **_piCols;
129 
130     if (_piNbCoef == NULL)
131     {
132         return sciErr;
133     }
134 
135     types::Polynom *pMP = ((types::InternalType*)_piAddress)->getAs<types::Polynom>();
136     pMP->getSizes(_piNbCoef);
137 
138     if (_pdblReal == NULL)
139     {
140         return sciErr;
141     }
142 
143     types::SinglePoly** pSP = pMP->get();
144     if (_iComplex == 1)
145     {
146         for (int i = 0 ; i < iSize ; i++)
147         {
148             memcpy(_pdblReal[i], pSP[i]->get(),    sizeof(double) * pSP[i]->getSize());
149             memcpy(_pdblImg[i],  pSP[i]->getImg(), sizeof(double) * _piNbCoef[i]);
150         }
151     }
152     else
153     {
154         for (int i = 0 ; i < iSize ; i++)
155         {
156             memcpy(_pdblReal[i], pSP[i]->get(), sizeof(double) * pSP[i]->getSize());
157         }
158     }
159 
160     return sciErr;
161 }
162 
createMatrixOfPoly(void * _pvCtx,int _iVar,char * _pstVarName,int _iRows,int _iCols,const int * _piNbCoef,const double * const * _pdblReal)163 SciErr createMatrixOfPoly(void* _pvCtx, int _iVar, char* _pstVarName, int _iRows, int _iCols, const int* _piNbCoef, const double* const* _pdblReal)
164 {
165     return createCommonMatrixOfPoly(_pvCtx, _iVar, 0, _pstVarName, _iRows, _iCols, _piNbCoef, _pdblReal, NULL);
166 }
167 
createComplexMatrixOfPoly(void * _pvCtx,int _iVar,char * _pstVarName,int _iRows,int _iCols,const int * _piNbCoef,const double * const * _pdblReal,const double * const * _pdblImg)168 SciErr createComplexMatrixOfPoly(void* _pvCtx, int _iVar, char* _pstVarName, int _iRows, int _iCols, const int* _piNbCoef, const double* const* _pdblReal, const double* const* _pdblImg)
169 {
170     return createCommonMatrixOfPoly(_pvCtx, _iVar, 1, _pstVarName, _iRows, _iCols, _piNbCoef, _pdblReal, _pdblImg);
171 }
172 
createCommonMatrixOfPoly(void * _pvCtx,int _iVar,int _iComplex,char * _pstVarName,int _iRows,int _iCols,const int * _piNbCoef,const double * const * _pdblReal,const double * const * _pdblImg)173 SciErr createCommonMatrixOfPoly(void* _pvCtx, int _iVar, int _iComplex, char* _pstVarName, int _iRows, int _iCols, const int* _piNbCoef, const double* const* _pdblReal, const double* const* _pdblImg)
174 {
175     SciErr sciErr = sciErrInit();
176     if (_pvCtx == NULL)
177     {
178         addErrorMessage(&sciErr, API_ERROR_INVALID_POINTER, _("%s: Invalid argument address"), _iComplex ? "createComplexMatrixOfPoly" : "createMatrixOfPoly");
179         return sciErr;
180     }
181 
182     types::GatewayStruct* pStr = (types::GatewayStruct*)_pvCtx;
183     types::InternalType** out = pStr->m_pOut;
184     int rhs = _iVar - *getNbInputArgument(_pvCtx);
185 
186     //return empty matrix
187     if (_iRows == 0 && _iCols == 0)
188     {
189         types::Double *pDbl = new types::Double(_iRows, _iCols);
190         if (pDbl == NULL)
191         {
192             addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createEmptyMatrix");
193             return sciErr;
194         }
195 
196         out[rhs - 1] = pDbl;
197         return sciErr;
198     }
199 
200     wchar_t* pstTemp = to_wide_string(_pstVarName);
201     std::wstring wstTemp(pstTemp);
202     types::Polynom* pP = new types::Polynom(wstTemp, _iRows, _iCols, _piNbCoef);
203     FREE(pstTemp);
204     if (pP == NULL)
205     {
206         addErrorMessage(&sciErr, API_ERROR_NO_MORE_MEMORY, _("%s: No more memory to allocated variable"), _iComplex ? "createComplexMatrixOfPoly" : "createMatrixOfPoly");
207         return sciErr;
208     }
209 
210     if (_iComplex)
211     {
212         pP->setComplex(true);
213     }
214 
215     out[rhs - 1] = pP;
216 
217     for (int i = 0 ; i < pP->getSize() ; i++)
218     {
219         types::Double* pD = new types::Double(_piNbCoef[i], 1, _iComplex == 1);
220         pD->set(_pdblReal[i]);
221         if (_iComplex)
222         {
223             pD->setImg(_pdblImg[i]);
224         }
225         pP->setCoef(i, pD);
226         delete pD;
227     }
228 
229     return sciErr;
230 }
231 
createNamedMatrixOfPoly(void * _pvCtx,const char * _pstName,char * _pstVarName,int _iRows,int _iCols,const int * _piNbCoef,const double * const * _pdblReal)232 SciErr createNamedMatrixOfPoly(void* _pvCtx, const char* _pstName, char* _pstVarName, int _iRows, int _iCols, const int* _piNbCoef, const double* const* _pdblReal)
233 {
234     return createCommonNamedMatrixOfPoly(_pvCtx, _pstName, _pstVarName, 0, _iRows, _iCols, _piNbCoef, _pdblReal, NULL);
235 }
236 
createNamedComplexMatrixOfPoly(void * _pvCtx,const char * _pstName,char * _pstVarName,int _iRows,int _iCols,const int * _piNbCoef,const double * const * _pdblReal,const double * const * _pdblImg)237 SciErr createNamedComplexMatrixOfPoly(void* _pvCtx, const char* _pstName, char* _pstVarName, int _iRows, int _iCols, const int* _piNbCoef, const double* const* _pdblReal, const double* const* _pdblImg)
238 {
239     return createCommonNamedMatrixOfPoly(_pvCtx, _pstName, _pstVarName, 1, _iRows, _iCols, _piNbCoef, _pdblReal, _pdblImg);
240 }
241 
createCommonNamedMatrixOfPoly(void * _pvCtx,const char * _pstName,char * _pstVarName,int _iComplex,int _iRows,int _iCols,const int * _piNbCoef,const double * const * _pdblReal,const double * const * _pdblImg)242 SciErr createCommonNamedMatrixOfPoly(void* _pvCtx, const char* _pstName, char* _pstVarName, int _iComplex, int _iRows, int _iCols, const int* _piNbCoef, const double* const* _pdblReal, const double* const* _pdblImg)
243 {
244     SciErr sciErr = sciErrInit();
245 
246     // check variable name
247     if (checkNamedVarFormat(_pvCtx, _pstName) == 0)
248     {
249         addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Invalid variable name: %s."), "createCommonNamedMatrixOfPoly", _pstName);
250         return sciErr;
251     }
252 
253     //return empty matrix
254     if (_iRows == 0 && _iCols == 0)
255     {
256         if (createNamedEmptyMatrix(_pvCtx, _pstName))
257         {
258             addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createNamedEmptyMatrix");
259             return sciErr;
260         }
261 
262         return sciErr;
263     }
264 
265     wchar_t* pstTemp = to_wide_string(_pstVarName);
266     std::wstring wstTemp(pstTemp);
267     types::Polynom* pP = new types::Polynom(wstTemp, _iRows, _iCols, _piNbCoef);
268     FREE(pstTemp);
269     if (pP == NULL)
270     {
271         addErrorMessage(&sciErr, API_ERROR_INVALID_NAME, _("%s: Invalid variable name: %s."), "createCommonNamedMatrixOfPoly", _pstName);
272         return sciErr;
273     }
274 
275     if (_iComplex)
276     {
277         pP->setComplex(true);
278     }
279 
280     for (int i = 0 ; i < pP->getSize() ; i++)
281     {
282         types::Double* pD = new types::Double(_piNbCoef[i], 1, _iComplex == 1);
283         pD->set(_pdblReal[i]);
284         if (_iComplex)
285         {
286             pD->setImg(_pdblImg[i]);
287         }
288         pP->setCoef(i, pD);
289         delete pD;
290     }
291 
292     wchar_t* pwstName = to_wide_string(_pstName);
293     symbol::Context* ctx = symbol::Context::getInstance();
294     symbol::Symbol sym = symbol::Symbol(pwstName);
295     FREE(pwstName);
296     if (ctx->isprotected(sym) == false)
297     {
298         ctx->put(sym, pP);
299     }
300     else
301     {
302         delete pP;
303         addErrorMessage(&sciErr, API_ERROR_REDEFINE_PERMANENT_VAR, _("Redefining permanent variable.\n"));
304     }
305     return sciErr;
306 }
307 
readNamedMatrixOfPoly(void * _pvCtx,const char * _pstName,int * _piRows,int * _piCols,int * _piNbCoef,double ** _pdblReal)308 SciErr readNamedMatrixOfPoly(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, int* _piNbCoef, double** _pdblReal)
309 {
310     return readCommonNamedMatrixOfPoly(_pvCtx, _pstName, 0, _piRows, _piCols, _piNbCoef, _pdblReal, NULL);
311 }
312 
readNamedComplexMatrixOfPoly(void * _pvCtx,const char * _pstName,int * _piRows,int * _piCols,int * _piNbCoef,double ** _pdblReal,double ** _pdblImg)313 SciErr readNamedComplexMatrixOfPoly(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, int* _piNbCoef, double** _pdblReal, double** _pdblImg)
314 {
315     return readCommonNamedMatrixOfPoly(_pvCtx, _pstName, 1, _piRows, _piCols, _piNbCoef, _pdblReal, _pdblImg);
316 }
317 
readCommonNamedMatrixOfPoly(void * _pvCtx,const char * _pstName,int _iComplex,int * _piRows,int * _piCols,int * _piNbCoef,double ** _pdblReal,double ** _pdblImg)318 SciErr readCommonNamedMatrixOfPoly(void* _pvCtx, const char* _pstName, int _iComplex, int* _piRows, int* _piCols, int* _piNbCoef, double** _pdblReal, double** _pdblImg)
319 {
320     int* piAddr = NULL;
321 
322     SciErr sciErr = getVarAddressFromName(_pvCtx, _pstName, &piAddr);
323     if (sciErr.iErr)
324     {
325         addErrorMessage(&sciErr, API_ERROR_READ_NAMED_POLY, _("%s: Unable to get variable \"%s\""), _iComplex ? "readNamedComplexMatrixOfPoly" : "readNamedMatrixOfPoly", _pstName);
326         return sciErr;
327     }
328 
329     if (_iComplex == 1)
330     {
331         sciErr = getComplexMatrixOfPoly(_pvCtx, piAddr, _piRows, _piCols, _piNbCoef, _pdblReal, _pdblImg);
332     }
333     else
334     {
335         sciErr = getMatrixOfPoly(_pvCtx, piAddr, _piRows, _piCols, _piNbCoef, _pdblReal);
336     }
337 
338     if (sciErr.iErr)
339     {
340         addErrorMessage(&sciErr, API_ERROR_READ_NAMED_POLY, _("%s: Unable to get variable \"%s\""), _iComplex ? "readNamedComplexMatrixOfPoly" : "readNamedMatrixOfPoly", _pstName);
341         return sciErr;
342     }
343 
344     return sciErr;
345 }
346 
347 /*shortcut functions */
348 
349 /*--------------------------------------------------------------------------*/
isPolyType(void * _pvCtx,int * _piAddress)350 int isPolyType(void* _pvCtx, int* _piAddress)
351 {
352     return checkVarType(_pvCtx, _piAddress, sci_poly);
353 }
354 /*--------------------------------------------------------------------------*/
isNamedPolyType(void * _pvCtx,const char * _pstName)355 int isNamedPolyType(void* _pvCtx, const char* _pstName)
356 {
357     return checkNamedVarType(_pvCtx, _pstName, sci_poly);
358 }
359 /*--------------------------------------------------------------------------*/
getAllocatedSinglePoly(void * _pvCtx,int * _piAddress,int * _piNbCoef,double ** _pdblReal)360 int getAllocatedSinglePoly(void* _pvCtx, int* _piAddress, int* _piNbCoef, double** _pdblReal)
361 {
362     return getCommonAllocatedSinglePoly(_pvCtx, _piAddress, 0, _piNbCoef, _pdblReal, NULL);
363 }
364 /*--------------------------------------------------------------------------*/
getAllocatedSingleComplexPoly(void * _pvCtx,int * _piAddress,int * _piNbCoef,double ** _pdblReal,double ** _pdblImg)365 int getAllocatedSingleComplexPoly(void* _pvCtx, int* _piAddress, int* _piNbCoef, double** _pdblReal, double** _pdblImg)
366 {
367     return getCommonAllocatedSinglePoly(_pvCtx, _piAddress, 1, _piNbCoef, _pdblReal, _pdblImg);
368 }
369 /*--------------------------------------------------------------------------*/
getCommonAllocatedSinglePoly(void * _pvCtx,int * _piAddress,int _iComplex,int * _piNbCoef,double ** _pdblReal,double ** _pdblImg)370 static int getCommonAllocatedSinglePoly(void* _pvCtx, int* _piAddress, int _iComplex, int* _piNbCoef, double** _pdblReal, double** _pdblImg)
371 {
372     SciErr sciErr = sciErrInit();
373     int iRows	= 0;
374     int iCols	= 0;
375 
376     double* pdblReal = NULL;
377     double* pdblImg	 = NULL;
378 
379     if (isScalar(_pvCtx, _piAddress) == 0)
380     {
381         addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_SINGLE_POLY, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), _iComplex ? "getAllocatedSingleComplexPoly" : "getAllocatedSinglePoly", getRhsFromAddress(_pvCtx, _piAddress));
382         printError(&sciErr, 0);
383         return sciErr.iErr;
384     }
385 
386     sciErr = getCommonMatrixOfPoly(_pvCtx, _piAddress, _iComplex, &iRows, &iCols, _piNbCoef, NULL, NULL);
387     if (sciErr.iErr)
388     {
389         addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_SINGLE_POLY, _("%s: Unable to get argument #%d"), _iComplex ? "getAllocatedSingleComplexPoly" : "getAllocatedSinglePoly", getRhsFromAddress(_pvCtx, _piAddress));
390         printError(&sciErr, 0);
391         return sciErr.iErr;
392     }
393 
394     *_pdblReal = (double*)MALLOC(sizeof(double) **_piNbCoef);
395 
396     if (_iComplex)
397     {
398         *_pdblImg	= (double*)MALLOC(sizeof(double) **_piNbCoef);
399     }
400 
401     sciErr = getCommonMatrixOfPoly(_pvCtx, _piAddress, _iComplex, &iRows, &iCols, _piNbCoef, _pdblReal, _pdblImg);
402     if (sciErr.iErr)
403     {
404         addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_SINGLE_POLY, _("%s: Unable to get argument #%d"), _iComplex ? "getAllocatedSingleComplexPoly" : "getAllocatedSinglePoly", getRhsFromAddress(_pvCtx, _piAddress));
405         printError(&sciErr, 0);
406         return sciErr.iErr;
407     }
408 
409     return 0;
410 }
411 /*--------------------------------------------------------------------------*/
getAllocatedNamedSinglePoly(void * _pvCtx,const char * _pstName,int * _piNbCoef,double ** _pdblReal)412 int getAllocatedNamedSinglePoly(void* _pvCtx, const char* _pstName, int* _piNbCoef, double** _pdblReal)
413 {
414     return getCommonAllocatedNamedSinglePoly(_pvCtx, _pstName, 0, _piNbCoef, _pdblReal, NULL);
415 }
416 /*--------------------------------------------------------------------------*/
getAllocatedNamedSingleComplexPoly(void * _pvCtx,const char * _pstName,int * _piNbCoef,double ** _pdblReal,double ** _pdblImg)417 int getAllocatedNamedSingleComplexPoly(void* _pvCtx, const char* _pstName, int* _piNbCoef, double** _pdblReal, double** _pdblImg)
418 {
419     return getCommonAllocatedNamedSinglePoly(_pvCtx, _pstName, 1, _piNbCoef, _pdblReal, _pdblImg);
420 }
421 /*--------------------------------------------------------------------------*/
getCommonAllocatedNamedSinglePoly(void * _pvCtx,const char * _pstName,int _iComplex,int * _piNbCoef,double ** _pdblReal,double ** _pdblImg)422 static int getCommonAllocatedNamedSinglePoly(void* _pvCtx, const char* _pstName, int _iComplex, int* _piNbCoef, double** _pdblReal, double** _pdblImg)
423 {
424     SciErr sciErr = sciErrInit();
425     int iRows	= 0;
426     int iCols	= 0;
427 
428     double* pdblReal = NULL;
429     double* pdblImg	 = NULL;
430 
431     if (isNamedScalar(_pvCtx, _pstName) == 0)
432     {
433         addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_SINGLE_POLY, _("%s: Wrong type for input argument \"%s\": A scalar expected.\n"), _iComplex ? "getAllocatedNamedSingleComplexPoly" : "getAllocatedNamedSinglePoly", _pstName);
434         printError(&sciErr, 0);
435         return sciErr.iErr;
436     }
437 
438     sciErr = readCommonNamedMatrixOfPoly(_pvCtx, _pstName, _iComplex, &iRows, &iCols, _piNbCoef, &pdblReal, &pdblImg);
439     if (sciErr.iErr)
440     {
441         addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_SINGLE_POLY, _("%s: Unable to get argument \"%s\""), _iComplex ? "getAllocatedNamedSingleComplexPoly" : "getAllocatedNamedSinglePoly", _pstName);
442         printError(&sciErr, 0);
443         return sciErr.iErr;
444     }
445 
446     *_pdblReal = (double*)MALLOC(sizeof(double) **_piNbCoef);
447     memcpy(*_pdblReal, pdblReal, sizeof(double) **_piNbCoef);
448 
449     if (_iComplex)
450     {
451         *_pdblImg	= (double*)MALLOC(sizeof(double) **_piNbCoef);
452         memcpy(*_pdblImg, pdblImg, sizeof(double) **_piNbCoef);
453     }
454     return 0;
455 }
456 /*--------------------------------------------------------------------------*/
getAllocatedMatrixOfPoly(void * _pvCtx,int * _piAddress,int * _piRows,int * _piCols,int ** _piNbCoef,double *** _pdblReal)457 int getAllocatedMatrixOfPoly(void* _pvCtx, int* _piAddress, int* _piRows, int* _piCols, int** _piNbCoef, double*** _pdblReal)
458 {
459     return getCommonAllocatedMatrixOfPoly(_pvCtx, _piAddress, 0, _piRows, _piCols, _piNbCoef, _pdblReal, NULL);
460 }
461 /*--------------------------------------------------------------------------*/
getAllocatedMatrixOfComplexPoly(void * _pvCtx,int * _piAddress,int * _piRows,int * _piCols,int ** _piNbCoef,double *** _pdblReal,double *** _pdblImg)462 int getAllocatedMatrixOfComplexPoly(void* _pvCtx, int* _piAddress, int* _piRows, int* _piCols, int** _piNbCoef, double*** _pdblReal, double*** _pdblImg)
463 {
464     return getCommonAllocatedMatrixOfPoly(_pvCtx, _piAddress, 1, _piRows, _piCols, _piNbCoef, _pdblReal, _pdblImg);
465 }
466 /*--------------------------------------------------------------------------*/
getCommonAllocatedMatrixOfPoly(void * _pvCtx,int * _piAddress,int _iComplex,int * _piRows,int * _piCols,int ** _piNbCoef,double *** _pdblReal,double *** _pdblImg)467 static int getCommonAllocatedMatrixOfPoly(void* _pvCtx, int* _piAddress, int _iComplex, int* _piRows, int* _piCols, int** _piNbCoef, double*** _pdblReal, double*** _pdblImg)
468 {
469     SciErr sciErr = sciErrInit();
470     double* pdblReal	= NULL;
471     double* pdblImg		= NULL;
472 
473     sciErr = getCommonMatrixOfPoly(_pvCtx, _piAddress, _iComplex, _piRows, _piCols, NULL, NULL, NULL);
474     if (sciErr.iErr)
475     {
476         addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_MATRIX_POLY, _("%s: Unable to get argument #%d"), _iComplex ? "getAllocatedMatrixOfComplexPoly" : "getAllocatedMatrixOfPoly", getRhsFromAddress(_pvCtx, _piAddress));
477         printError(&sciErr, 0);
478         return sciErr.iErr;
479     }
480 
481     *_piNbCoef = (int*)MALLOC(sizeof(int) **_piRows **_piCols);
482 
483     sciErr = getCommonMatrixOfPoly(_pvCtx, _piAddress, _iComplex, _piRows, _piCols, *_piNbCoef, NULL, NULL);
484     if (sciErr.iErr)
485     {
486         addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_MATRIX_POLY, _("%s: Unable to get argument #%d"), _iComplex ? "getAllocatedMatrixOfComplexPoly" : "getAllocatedMatrixOfPoly", getRhsFromAddress(_pvCtx, _piAddress));
487         printError(&sciErr, 0);
488         return sciErr.iErr;
489     }
490 
491     *_pdblReal = (double**)MALLOC(sizeof(double*) **_piRows **_piCols);
492     for (int i = 0 ; i < *_piRows **_piCols ; i++)
493     {
494         (*_pdblReal)[i] = (double*)MALLOC(sizeof(double) * (*_piNbCoef)[i]);
495     }
496 
497     if (_iComplex)
498     {
499         *_pdblImg	= (double**)MALLOC(sizeof(double*) **_piRows **_piCols);
500         for (int i = 0 ; i < *_piRows **_piCols ; i++)
501         {
502             (*_pdblImg)[i] = (double*)MALLOC(sizeof(double) * (*_piNbCoef)[i]);
503         }
504     }
505 
506     sciErr = getCommonMatrixOfPoly(_pvCtx, _piAddress, _iComplex, _piRows, _piCols, *_piNbCoef, *_pdblReal, _pdblImg == NULL ? NULL : *_pdblImg);
507     if (sciErr.iErr)
508     {
509         addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_MATRIX_POLY, _("%s: Unable to get argument #%d"), _iComplex ? "getAllocatedSingleComplexPoly" : "getAllocatedSinglePoly", getRhsFromAddress(_pvCtx, _piAddress));
510         printError(&sciErr, 0);
511         return sciErr.iErr;
512     }
513 
514     return 0;
515 }
516 /*--------------------------------------------------------------------------*/
getAllocatedNamedMatrixOfPoly(void * _pvCtx,const char * _pstName,int * _piRows,int * _piCols,int ** _piNbCoef,double *** _pdblReal)517 int getAllocatedNamedMatrixOfPoly(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, int** _piNbCoef, double*** _pdblReal)
518 {
519     return getCommonAllocatedNamedMatrixOfPoly(_pvCtx, _pstName, 0, _piRows, _piCols, _piNbCoef, _pdblReal, NULL);
520 }
521 /*--------------------------------------------------------------------------*/
getAllocatedNamedMatrixOfComplexPoly(void * _pvCtx,const char * _pstName,int * _piRows,int * _piCols,int ** _piNbCoef,double *** _pdblReal,double *** _pdblImg)522 int getAllocatedNamedMatrixOfComplexPoly(void* _pvCtx, const char* _pstName, int* _piRows, int* _piCols, int** _piNbCoef, double*** _pdblReal, double*** _pdblImg)
523 {
524     return getCommonAllocatedNamedMatrixOfPoly(_pvCtx, _pstName, 1, _piRows, _piCols, _piNbCoef, _pdblReal, _pdblImg);
525 }
526 /*--------------------------------------------------------------------------*/
getCommonAllocatedNamedMatrixOfPoly(void * _pvCtx,const char * _pstName,int _iComplex,int * _piRows,int * _piCols,int ** _piNbCoef,double *** _pdblReal,double *** _pdblImg)527 static int getCommonAllocatedNamedMatrixOfPoly(void* _pvCtx, const char* _pstName, int _iComplex, int* _piRows, int* _piCols, int** _piNbCoef, double*** _pdblReal, double*** _pdblImg)
528 {
529     SciErr sciErr = readCommonNamedMatrixOfPoly(_pvCtx, _pstName, _iComplex, _piRows, _piCols, NULL, NULL, NULL);
530     if (sciErr.iErr)
531     {
532         addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_MATRIX_POLY, _("%s: Unable to get argument \"%s\""), _iComplex ? "getAllocatedNamedMatrixOfComplexPoly" : "getAllocatedNamedMatrixOfPoly", _pstName);
533         printError(&sciErr, 0);
534         return sciErr.iErr;
535     }
536 
537     *_piNbCoef = (int*)MALLOC(sizeof(int) **_piRows **_piCols);
538 
539     sciErr = readCommonNamedMatrixOfPoly(_pvCtx, _pstName, _iComplex, _piRows, _piCols, *_piNbCoef, NULL, NULL);
540     if (sciErr.iErr)
541     {
542         addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_MATRIX_POLY, _("%s: Unable to get argument \"%s\""), _iComplex ? "getAllocatedNamedMatrixOfComplexPoly" : "getAllocatedNamedMatrixOfPoly", _pstName);
543         printError(&sciErr, 0);
544         return sciErr.iErr;
545     }
546 
547     *_pdblReal = (double**)MALLOC(sizeof(double*) **_piRows **_piCols);
548     for (int i = 0 ; i < *_piRows **_piCols ; i++)
549     {
550         (*_pdblReal)[i] = (double*)MALLOC(sizeof(double) * (*_piNbCoef)[i]);
551     }
552 
553     if (_iComplex)
554     {
555         *_pdblImg	= (double**)MALLOC(sizeof(double*) **_piRows **_piCols);
556         for (int i = 0 ; i < *_piRows **_piCols ; i++)
557         {
558             (*_pdblImg)[i] = (double*)MALLOC(sizeof(double) * (*_piNbCoef)[i]);
559         }
560     }
561 
562     sciErr = readCommonNamedMatrixOfPoly(_pvCtx, _pstName, _iComplex, _piRows, _piCols, *_piNbCoef, *_pdblReal, _pdblImg == NULL ? NULL : *_pdblImg);
563     if (sciErr.iErr)
564     {
565         addErrorMessage(&sciErr, API_ERROR_GET_ALLOC_NAMED_MATRIX_POLY, _("%s: Unable to get argument \"%s\""), _iComplex ? "getAllocatedNamedMatrixOfComplexPoly" : "getAllocatedNamedMatrixOfPoly", _pstName);
566         printError(&sciErr, 0);
567         return sciErr.iErr;
568     }
569 
570     return 0;
571 }
572 /*--------------------------------------------------------------------------*/
freeAllocatedSinglePoly(double * _pdblReal)573 void freeAllocatedSinglePoly(double* _pdblReal)
574 {
575     FREE(_pdblReal);
576 }
577 /*--------------------------------------------------------------------------*/
freeAllocatedSingleComplexPoly(double * _pdblReal,double * _pdblImg)578 void freeAllocatedSingleComplexPoly(double* _pdblReal, double* _pdblImg)
579 {
580     freeAllocatedSinglePoly(_pdblReal);
581     FREE(_pdblImg);
582 }
583 /*--------------------------------------------------------------------------*/
freeAllocatedMatrixOfPoly(int _iRows,int _iCols,int * _piNbCoef,double ** _pdblReal)584 void freeAllocatedMatrixOfPoly(int _iRows, int _iCols, int* _piNbCoef, double** _pdblReal)
585 {
586     FREE(_piNbCoef);
587 
588     for (int i = 0 ; i < _iRows * _iCols ; i++)
589     {
590         FREE(_pdblReal[i]);
591     }
592     FREE(_pdblReal);
593 }
594 /*--------------------------------------------------------------------------*/
freeAllocatedMatrixOfComplexPoly(int _iRows,int _iCols,int * _piNbCoef,double ** _pdblReal,double ** _pdblImg)595 void freeAllocatedMatrixOfComplexPoly(int _iRows, int _iCols, int* _piNbCoef, double** _pdblReal, double** _pdblImg)
596 {
597     freeAllocatedMatrixOfPoly(_iRows, _iCols, _piNbCoef, _pdblReal);
598 
599     for (int i = 0 ; i < _iRows * _iCols ; i++)
600     {
601         FREE(_pdblImg[i]);
602     }
603     FREE(_pdblImg);
604 }
605 /*--------------------------------------------------------------------------*/
606