SciErr createMatrixOfBoolean(void* _pvCtx, int _iVar, int _iRows, int _iCols, const int* _piBool) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int* piBool = NULL; if(_iRows == 0 && _iCols == 0) { double dblReal = 0; sciErr = createMatrixOfDouble(_pvCtx, _iVar, 0, 0, &dblReal); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createEmptyMatrix"); } return sciErr; } sciErr = allocMatrixOfBoolean(_pvCtx, _iVar, _iRows, _iCols, &piBool); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_BOOLEAN, _("%s: Unable to create variable in Scilab memory"), "createMatrixOfBoolean"); return sciErr; } memcpy(piBool, _piBool, sizeof(int) * _iRows * _iCols); return sciErr; }
/** * The gateway function for soap_servers() * @param[in] fname the name of the file for the error messages * @return 0 if successful, a negative value otherwise */ int sci_empty_test(char *fname) { SciErr sciErr; // allocate memory for values double dOut = 0; char *cOut = "zero"; // this function does not take input arguments CheckInputArgument(pvApiCtx, 0, 0); // the number of output arguments must be 2 CheckOutputArgument(pvApiCtx, 2, 2); // create results on stack sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 0, 0, &dOut); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } sciErr = createMatrixOfString(pvApiCtx, nbInputArgument(pvApiCtx) + 2, 0, 0, &cOut); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; return 0; }
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) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int *piAddr = NULL; int iSize = _iRows * _iCols; int iNewPos = Top - Rhs + _iVar; int iAddr = *Lstk(iNewPos); int iTotalLen = 0; //return empty matrix if(_iRows == 0 && _iCols == 0) { double dblReal = 0; sciErr = createMatrixOfDouble(_pvCtx, _iVar, 0, 0, &dblReal); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createEmptyMatrix"); } return sciErr; } getNewVarAddressFromPosition(_pvCtx, iNewPos, &piAddr); sciErr = fillCommonMatrixOfPoly(_pvCtx, piAddr, _pstVarName, _iComplex, _iRows, _iCols, _piNbCoef, _pdblReal, _pdblImg, &iTotalLen); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_POLY, _("%s: Unable to create variable in Scilab memory"), _iComplex ? "createComplexMatrixOfPoly" : "createMatrixOfPoly"); return sciErr; } updateInterSCI(_iVar, '$', iAddr, iAddr + 4 + 4 + iSize + 1); updateLstk(iNewPos, iAddr + 4 + 4 + iSize + 1, iTotalLen); return sciErr; }
SciErr createBooleanSparseMatrix(void* _pvCtx, int _iVar, int _iRows, int _iCols, int _iNbItem, const int* _piNbItemRow, const int* _piColPos) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int* piNbItemRow = NULL; int* piColPos = NULL; if(_iRows == 0 && _iCols == 0) { double dblReal = 0; sciErr = createMatrixOfDouble(_pvCtx, _iVar, 0, 0, &dblReal); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createEmptyMatrix"); } return sciErr; } sciErr = allocBooleanSparseMatrix(_pvCtx, _iVar, _iRows, _iCols, _iNbItem, &piNbItemRow, &piColPos); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_BOOLEAN_SPARSE, _("%s: Unable to create variable in Scilab memory"), "createBooleanSparseMatrix"); return sciErr; } memcpy(piNbItemRow, _piNbItemRow, _iRows * sizeof(int)); memcpy(piColPos, _piColPos, _iNbItem * sizeof(int)); return sciErr; }
/*--------------------------------------------------------------------------*/ SciErr createMatrixOfWideString(void* _pvCtx, int _iVar, int _iRows, int _iCols, const wchar_t* const* _pstwStrings) { char **pStrings = NULL; //return empty matrix if (_iRows == 0 && _iCols == 0) { double dblReal = 0; SciErr sciErr = createMatrixOfDouble(_pvCtx, _iVar, 0, 0, &dblReal); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createEmptyMatrix"); } return sciErr; } pStrings = (char**)MALLOC( sizeof(char*) * (_iRows * _iCols) ); for (int i = 0; i < (_iRows * _iCols) ; i++) { pStrings[i] = wide_string_to_UTF8(_pstwStrings[i]); } SciErr sciErr = createMatrixOfString(_pvCtx, _iVar, _iRows, _iCols, pStrings); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_WIDE_STRING, _("%s: Unable to create variable in Scilab memory"), "createMatrixOfWideString"); } freeArrayOfString(pStrings, _iRows * _iCols); return sciErr; }
SciErr allocMatrixOfBoolean(void* _pvCtx, int _iVar, int _iRows, int _iCols, int** _piBool) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int *piAddr = NULL; int iNewPos = Top - Rhs + _iVar; int iAddr = *Lstk(iNewPos); //return empty matrix if(_iRows == 0 && _iCols == 0) { double dblReal = 0; sciErr = createMatrixOfDouble(_pvCtx, _iVar, 0, 0, &dblReal); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createEmptyMatrix"); } return sciErr; } int iMemSize = (int)(((double)(_iRows * _iCols) / 2) + 2); int iFreeSpace = iadr(*Lstk(Bot)) - (iadr(iAddr)); if (iMemSize > iFreeSpace) { addStackSizeError(&sciErr, ((StrCtx*)_pvCtx)->pstName, iMemSize); return sciErr; } getNewVarAddressFromPosition(_pvCtx, iNewPos, &piAddr); fillMatrixOfBoolean(_pvCtx, piAddr, _iRows, _iCols, _piBool); updateInterSCI(_iVar, '$', iAddr, sadr(iadr(iAddr) + 3)); updateLstk(iNewPos, sadr(iadr(iAddr) + 3), (_iRows * _iCols) / (sizeof(double) / sizeof(int))); return sciErr; }
/*--------------------------------------------------------------------------*/ int createEmptyMatrix(void *_pvCtx, int _iVar) { double dblReal = 0; SciErr sciErr = createMatrixOfDouble(_pvCtx, _iVar, 0, 0, &dblReal); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createEmptyMatrix"); printError(&sciErr, 0); return sciErr.iErr; } return 0; }
int sci_sym_getRowActivity(char *fname){ //error management variable SciErr sciErr; int iRet; //data declarations int numConstr; double *rowAct; //ensure that environment is active if(global_sym_env==NULL){ sciprint("Error: Symphony environment not initialized. Please run 'sym_open()' first.\n"); return 1; } //code to check arguments and get them CheckInputArgument(pvApiCtx,0,0) ; CheckOutputArgument(pvApiCtx,1,1) ; //code to process input iRet=sym_get_num_rows(global_sym_env,&numConstr); if(iRet==FUNCTION_TERMINATED_ABNORMALLY){ Scierror(999, "An error occured. Has the problem been solved? Is the problem feasible?\n"); return 1; } rowAct=new double[numConstr]; iRet=sym_get_row_activity(global_sym_env,rowAct); if(iRet==FUNCTION_TERMINATED_ABNORMALLY){ Scierror(999, "An error occured. Has the problem been solved? Is the problem feasible?\n"); delete[] rowAct; return 1; } //code to give output sciErr=createMatrixOfDouble(pvApiCtx,nbInputArgument(pvApiCtx)+1,numConstr,1,rowAct); if (sciErr.iErr) { printError(&sciErr, 0); delete[] rowAct; return 1; } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx)+1; //ReturnArguments(pvApiCtx); delete[] rowAct; return 0; }
int returnDoubleMatrixToScilab(int itemPos, int rows, int cols, double *dest) { SciErr sciErr; //same steps as above sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + itemPos, rows, cols, dest); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } AssignOutputVariable(pvApiCtx, itemPos) = nbInputArgument(pvApiCtx)+itemPos; return 0; }
SciErr allocCommonSparseMatrix(void* _pvCtx, int _iVar, int _iComplex, int _iRows, int _iCols, int _iNbItem, int** _piNbItemRow, int** _piColPos, double** _pdblReal, double** _pdblImg) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int iNewPos = Top - Rhs + _iVar; int iAddr = *Lstk(iNewPos); int iTotalSize = 0; int iOffset = 0; int* piAddr = NULL; //return empty matrix if(_iRows == 0 && _iCols == 0) { double dblReal = 0; sciErr = createMatrixOfDouble(_pvCtx, _iVar, 0, 0, &dblReal); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createEmptyMatrix"); } return sciErr; } //header + offset int iMemSize = (5 + _iRows + _iNbItem + !((_iRows + _iNbItem) % 2)) / 2; //+ items size iMemSize += _iNbItem * (_iComplex + 1); int iFreeSpace = iadr(*Lstk(Bot)) - (iadr(iAddr)); if (iMemSize > iFreeSpace) { addStackSizeError(&sciErr, ((StrCtx*)_pvCtx)->pstName, iMemSize); return sciErr; } getNewVarAddressFromPosition(_pvCtx, iNewPos, &piAddr); sciErr = fillCommonSparseMatrix(_pvCtx, piAddr, _iComplex, _iRows, _iCols, _iNbItem, _piNbItemRow, _piColPos, _pdblReal, _pdblImg, &iTotalSize); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_ALLOC_SPARSE, _("%s: Unable to create variable in Scilab memory"), _iComplex ? "allocComplexSparseMatrix" : "allocSparseMatrix"); return sciErr; } iOffset = 5;//4 for header + 1 for NbItem iOffset += _iRows + _iNbItem + !((_iRows + _iNbItem) % 2); updateInterSCI(_iVar, '$', iAddr, sadr(iadr(iAddr) + iOffset)); updateLstk(iNewPos, sadr(iadr(iAddr) + iOffset), iTotalSize); return sciErr; }
/*--------------------------------------------------------------------------*/ static int sci_format_norhs(char *fname) { SciErr sciErr; double dParamout[2]; getFormat(&dParamout[0], &dParamout[1]); sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, 1, 2, dParamout); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } LhsVar(1) = Rhs + 1; PutLhsVar(); return 0; }
SciErr allocBooleanSparseMatrix(void* _pvCtx, int _iVar, int _iRows, int _iCols, int _iNbItem, int** _piNbItemRow, int** _piColPos) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int iNewPos = Top - Rhs + _iVar; int iAddr = *Lstk(iNewPos); int iPos = 5 + _iRows + _iNbItem; int* piAddr = NULL; //return empty matrix if(_iRows == 0 && _iCols == 0) { double dblReal = 0; sciErr = createMatrixOfDouble(_pvCtx, _iVar, 0, 0, &dblReal); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createEmptyMatrix"); } return sciErr; } int iMemSize = (int)( ( (double)iPos / 2 ) + 0.5); int iFreeSpace = iadr(*Lstk(Bot)) - (iadr(iAddr)); if (iMemSize > iFreeSpace) { addStackSizeError(&sciErr, ((StrCtx*)_pvCtx)->pstName, iMemSize); return sciErr; } getNewVarAddressFromPosition(_pvCtx, iNewPos, &piAddr); sciErr = fillBooleanSparseMatrix(_pvCtx, piAddr, _iRows, _iCols, _iNbItem, _piNbItemRow, _piColPos); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_ALLOC_BOOLEAN_SPARSE, _("%s: Unable to create variable in Scilab memory"), "allocBooleanSparseMatrix"); return sciErr; } iPos += iAddr; updateInterSCI(_iVar, '$', iAddr, iPos); updateLstk(iNewPos, iPos, 0); return sciErr; }
SciErr createCommonSparseMatrix(void* _pvCtx, int _iVar, int _iComplex, int _iRows, int _iCols, int _iNbItem, const int* _piNbItemRow, const int* _piColPos, const double* _pdblReal, const double* _pdblImg) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int* piNbItemRow = NULL; int* piColPos = NULL; int iOne = 1; double* pdblReal = NULL; double* pdblImg = NULL; if(_iRows == 0 && _iCols == 0) { double dblReal = 0; sciErr = createMatrixOfDouble(_pvCtx, _iVar, 0, 0, &dblReal); if (sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_EMPTY_MATRIX, _("%s: Unable to create variable in Scilab memory"), "createEmptyMatrix"); } return sciErr; } sciErr = allocCommonSparseMatrix(_pvCtx, _iVar, _iComplex, _iRows, _iCols, _iNbItem, &piNbItemRow, &piColPos, &pdblReal, &pdblImg); if(sciErr.iErr) { addErrorMessage(&sciErr, API_ERROR_CREATE_SPARSE, _("%s: Unable to create variable in Scilab memory"), _iComplex ? "createComplexSparseMatrix" : "createSparseMatrix"); return sciErr; } memcpy(piNbItemRow, _piNbItemRow, _iRows * sizeof(int)); memcpy(piColPos, _piColPos, _iNbItem * sizeof(int)); C2F(dcopy)(&_iNbItem, const_cast<double*>(_pdblReal), &iOne, pdblReal, &iOne); if(_iComplex) { C2F(dcopy)(&_iNbItem, const_cast<double*>(_pdblImg), &iOne, pdblImg, &iOne); } return sciErr; }
/*--------------------------------------------------------------------------*/ int sci_uigetcolor(char *fname, void* pvApiCtx) { SciErr sciErr; //WARNING ALL NEW DECALRATIONS ARE HERE IF YOUR HAVE MANY FUNCTIONS //IN THE FILE YOU HAVE PROBABLY TO MOVE DECLARATIONS IN GOOD FUNCTIONS int* piAddrredAdr = NULL; double* redAdr = NULL; int* piAddrtitleAdr = NULL; char* titleAdr = NULL; int* piAddrgreenAdr = NULL; double* greenAdr = NULL; int* piAddrblueAdr = NULL; double* blueAdr = NULL; int colorChooserID = 0; int firstColorIndex = 0; int nbRow = 0, nbCol = 0; double *selectedRGB = NULL; CheckInputArgument(pvApiCtx, 0, 4); if ((nbOutputArgument(pvApiCtx) != 1) && (nbOutputArgument(pvApiCtx) != 3)) /* Bad use */ { Scierror(999, _("%s: Wrong number of output arguments: %d or %d expected.\n"), fname, 1, 3); return FALSE; } /* Rhs==1: title or [R, G, B] given */ if (nbInputArgument(pvApiCtx) == 1) { if ((checkInputArgumentType(pvApiCtx, 1, sci_matrix))) { sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrredAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position 1. sciErr = getMatrixOfDouble(pvApiCtx, piAddrredAdr, &nbRow, &nbCol, &redAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 1); return 1; } if ((nbRow != 1) || (nbCol != 3)) { Scierror(999, _("%s: Wrong size for input argument #%d: A 1 x %d real row vector expected.\n"), fname, 1, 3); return FALSE; } } else if ((checkInputArgumentType(pvApiCtx, 1, sci_strings))) { sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrtitleAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position 1. if (getAllocatedSingleString(pvApiCtx, piAddrtitleAdr, &titleAdr)) { Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 1); return 1; } } else { Scierror(999, _("%s: Wrong type for input argument #%d: A real or a string expected.\n"), fname, 1); return FALSE; } } /* Title and [R, G, B] given */ if (nbInputArgument(pvApiCtx) == 2) { /* Title */ if ((checkInputArgumentType(pvApiCtx, 1, sci_strings))) { sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrtitleAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position 1. if (getAllocatedSingleString(pvApiCtx, piAddrtitleAdr, &titleAdr)) { Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 1); return 1; } } else { Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), fname, 1); return FALSE; } /* [R, G, B] */ if ((checkInputArgumentType(pvApiCtx, 2, sci_matrix))) { sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrredAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position 2. sciErr = getMatrixOfDouble(pvApiCtx, piAddrredAdr, &nbRow, &nbCol, &redAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 2); return 1; } if (nbRow*nbCol != 3) { Scierror(999, _("%s: Wrong size for input argument #%d: A 1 x %d real row vector expected.\n"), fname, 2, 3); return FALSE; } } else { Scierror(999, _("%s: Wrong type for input argument #%d: A 1 x %d real row vector expected.\n"), fname, 2, 3); return FALSE; } } /* No title given but colors given with separate values */ if (nbInputArgument(pvApiCtx) == 3) { firstColorIndex = 1; } /* Title and colors given with separate values */ if (nbInputArgument(pvApiCtx) == 4) { firstColorIndex = 2; /* Title */ if ((checkInputArgumentType(pvApiCtx, 1, sci_strings))) { sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrtitleAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position 1. if (getAllocatedSingleString(pvApiCtx, piAddrtitleAdr, &titleAdr)) { Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 1); return 1; } } else { Scierror(999, _("%s: Wrong type for input argument #%d: A real or a string expected.\n"), fname, 1); return FALSE; } } /* R, G, B given */ if (nbInputArgument(pvApiCtx) >= 3) { /* Default red value */ if ((checkInputArgumentType(pvApiCtx, firstColorIndex, sci_matrix))) { sciErr = getVarAddressFromPosition(pvApiCtx, firstColorIndex, &piAddrredAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position firstColorIndex. sciErr = getMatrixOfDouble(pvApiCtx, piAddrredAdr, &nbRow, &nbCol, &redAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, firstColorIndex); return 1; } if (nbRow*nbCol != 1) { Scierror(999, _("%s: Wrong size for input argument #%d: A real expected.\n"), fname, firstColorIndex); return FALSE; } } else { Scierror(999, _("%s: Wrong type for input argument #%d: A real expected.\n"), fname, firstColorIndex); return FALSE; } /* Default green value */ if (checkInputArgumentType(pvApiCtx, firstColorIndex + 1, sci_matrix)) { sciErr = getVarAddressFromPosition(pvApiCtx, firstColorIndex + 1, &piAddrgreenAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position firstColorIndex + 1. sciErr = getMatrixOfDouble(pvApiCtx, piAddrgreenAdr, &nbRow, &nbCol, &greenAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, firstColorIndex + 1); return 1; } if (nbRow*nbCol != 1) { Scierror(999, _("%s: Wrong size for input argument #%d: A real expected.\n"), fname, firstColorIndex + 1); return FALSE; } } else { Scierror(999, _("%s: Wrong type for input argument #%d: A real expected.\n"), fname, firstColorIndex + 1); return FALSE; } /* Default blue value */ if (checkInputArgumentType(pvApiCtx, firstColorIndex + 2, sci_matrix)) { sciErr = getVarAddressFromPosition(pvApiCtx, firstColorIndex + 2, &piAddrblueAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position firstColorIndex + 2. sciErr = getMatrixOfDouble(pvApiCtx, piAddrblueAdr, &nbRow, &nbCol, &blueAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, firstColorIndex + 2); return 1; } if (nbRow*nbCol != 1) { Scierror(999, _("%s: Wrong size for input argument #%d: A real expected.\n"), fname, firstColorIndex + 2); return FALSE; } } else { Scierror(999, _("%s: Wrong type for input argument #%d: A real expected.\n"), fname, firstColorIndex + 2); return FALSE; } } /* Create the Java Object */ colorChooserID = createColorChooser(); /* Title */ if (titleAdr != 0) { setColorChooserTitle(colorChooserID, titleAdr); freeAllocatedSingleString(titleAdr); } /* Default red value */ if (redAdr != 0) { if (greenAdr != 0 ) /* All values given in first input argument */ { setColorChooserDefaultRGBSeparateValues(colorChooserID, (int)redAdr[0], (int)greenAdr[0], (int)blueAdr[0]); } else { setColorChooserDefaultRGB(colorChooserID, redAdr); } } /* Display it and wait for a user input */ colorChooserDisplayAndWait(colorChooserID); /* Return the selected color */ /* Read the user answer */ selectedRGB = getColorChooserSelectedRGB(colorChooserID); if (selectedRGB[0] >= 0) /* The user selected a color */ { nbRow = 1; if (nbOutputArgument(pvApiCtx) == 1) { nbCol = 3; sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, nbRow, nbCol, selectedRGB); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; } if (nbOutputArgument(pvApiCtx) >= 2) { nbCol = 1; sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, nbRow, nbCol, &redAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } redAdr[0] = selectedRGB[0]; sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, nbRow, nbCol, &greenAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } greenAdr[0] = selectedRGB[1]; sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 3, nbRow, nbCol, &blueAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } blueAdr[0] = selectedRGB[2]; AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; AssignOutputVariable(pvApiCtx, 3) = nbInputArgument(pvApiCtx) + 3; } } else /* The user canceled */ { nbRow = 0; nbCol = 0; if (nbOutputArgument(pvApiCtx) == 1) { /* Return [] */ sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, nbRow, nbCol, &redAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; } if (nbOutputArgument(pvApiCtx) >= 2) { sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, nbRow, nbCol, &redAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, nbRow, nbCol, &greenAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 3, nbRow, nbCol, &blueAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; AssignOutputVariable(pvApiCtx, 3) = nbInputArgument(pvApiCtx) + 3; } } ReturnArguments(pvApiCtx); return TRUE; }
int sci_umf_luget(char* fname, void* pvApiCtx) { /* * LU_ptr is (a pointer to) a factorization of A, we have: * -1 * P R A Q = L U * * A is n_row x n_col * L is n_row x n * U is n x n_col n = min(n_row, n_col) */ SciErr sciErr; void* Numeric = NULL; int lnz = 0, unz = 0, n_row = 0, n_col = 0, n = 0, nz_udiag = 0, i = 0, stat = 0, do_recip = 0, it_flag = 0; int *L_mnel = NULL, *L_icol = NULL, *L_ptrow = NULL, *U_mnel = NULL, *U_icol = NULL, *U_ptrow = NULL, *V_irow = NULL, *V_ptcol = NULL; double *L_R = NULL, *L_I = NULL, *U_R = NULL, *U_I = NULL, *V_R = NULL, *V_I = NULL, *Rs = NULL; int *p = NULL, *q = NULL, pl_miss = 0, error_flag = 0 ; int* piAddr1 = NULL; int iType1 = 0; /* Check numbers of input/output arguments */ CheckInputArgument(pvApiCtx, 1, 1); CheckOutputArgument(pvApiCtx, 1, 5); /* get the pointer to the LU factors */ sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } /* Check if the first argument is a pointer */ sciErr = getVarType(pvApiCtx, piAddr1, &iType1); if (sciErr.iErr || iType1 != sci_pointer) { printError(&sciErr, 0); Scierror(999, _("%s: Wrong type for input argument #%d: A pointer expected.\n"), fname, 1); return 1; } sciErr = getPointer(pvApiCtx, piAddr1, &Numeric); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } /* Check if the pointer is a valid ref to ... */ if ( IsAdrInList(Numeric, ListNumeric, &it_flag) ) { if (it_flag == 0 ) { umfpack_di_get_lunz(&lnz, &unz, &n_row, &n_col, &nz_udiag, Numeric); } else { umfpack_zi_get_lunz(&lnz, &unz, &n_row, &n_col, &nz_udiag, Numeric); } } else { Scierror(999, _("%s: Wrong value for input argument #%d: Must be a valid reference to (umf) LU factors.\n"), fname, 1); return 1; } if (n_row <= n_col) { n = n_row; } else { n = n_col; } L_mnel = (int*)MALLOC(n_row * sizeof(int)); L_icol = (int*)MALLOC(lnz * sizeof(int)); L_ptrow = (int*)MALLOC((n_row + 1) * sizeof(int)); L_R = (double*)MALLOC( lnz * sizeof(double)); U_mnel = (int*)MALLOC(n * sizeof(int)); U_icol = (int*)MALLOC(unz * sizeof(int)); U_ptrow = (int*)MALLOC((n + 1) * sizeof(int)); U_R = (double*)MALLOC( unz * sizeof(double)); V_irow = (int*)MALLOC(unz * sizeof(int)); V_ptcol = (int*)MALLOC((n_col + 1) * sizeof(int)); V_R = (double*)MALLOC( unz * sizeof(double)); p = (int*)MALLOC(n_row * sizeof(int)); q = (int*)MALLOC(n_col * sizeof(int)); Rs = (double*)MALLOC(n_row * sizeof(double)); if ( it_flag == 1 ) { L_I = (double*)MALLOC(lnz * sizeof(double)); U_I = (double*)MALLOC(unz * sizeof(double)); V_I = (double*)MALLOC(unz * sizeof(double)); } else { L_I = U_I = V_I = NULL; } if ( !(L_mnel && L_icol && L_R && L_ptrow && p && U_mnel && U_icol && U_R && U_ptrow && q && V_irow && V_R && V_ptcol && Rs) || (it_flag && !(L_I && U_I && V_I)) ) { error_flag = 1; goto the_end; } if ( it_flag == 0 ) { stat = umfpack_di_get_numeric(L_ptrow, L_icol, L_R, V_ptcol, V_irow, V_R, p, q, (double *)NULL, &do_recip, Rs, Numeric); } else { stat = umfpack_zi_get_numeric(L_ptrow, L_icol, L_R, L_I, V_ptcol, V_irow, V_R, V_I, p, q, (double *)NULL, (double *)NULL, &do_recip, Rs, Numeric); } if ( stat != UMFPACK_OK ) { error_flag = 2; goto the_end; }; if ( do_recip ) { for ( i = 0 ; i < n_row ; i++ ) { Rs[i] = 1.0 / Rs[i]; } } if ( it_flag == 0 ) { stat = umfpack_di_transpose(n, n_col, V_ptcol, V_irow, V_R, (int *) NULL, (int*) NULL, U_ptrow, U_icol, U_R); } else { stat = umfpack_zi_transpose(n, n_col, V_ptcol, V_irow, V_R, V_I, (int *) NULL, (int*) NULL, U_ptrow, U_icol, U_R, U_I, 0); } if ( stat != UMFPACK_OK ) { error_flag = 2; goto the_end; }; for ( i = 0 ; i < n_row ; i++ ) { L_mnel[i] = L_ptrow[i + 1] - L_ptrow[i]; } for ( i = 0 ; i < n ; i++ ) { U_mnel[i] = U_ptrow[i + 1] - U_ptrow[i]; } for ( i = 0 ; i < lnz ; i++ ) { L_icol[i]++; } for ( i = 0 ; i < unz ; i++ ) { U_icol[i]++; } for ( i = 0 ; i < n_row ; i++ ) { p[i]++; } for ( i = 0 ; i < n_col ; i++ ) { q[i]++; } /* output L */ if (it_flag) // complex { sciErr = createComplexSparseMatrix(pvApiCtx, 2, n_row, n, lnz, L_mnel, L_icol, L_R, L_I); } else { sciErr = createSparseMatrix(pvApiCtx, 2, n_row, n, lnz, L_mnel, L_icol, L_R); } if (sciErr.iErr) { printError(&sciErr, 0); FREE(L_mnel); FREE(U_mnel); return 1; } /* output U */ if (it_flag) // complex { sciErr = createComplexSparseMatrix(pvApiCtx, 3, n, n_col, unz, U_mnel, U_icol, U_R, U_I); } else { sciErr = createSparseMatrix(pvApiCtx, 3, n, n_col, unz, U_mnel, U_icol, U_R); } if (sciErr.iErr) { printError(&sciErr, 0); FREE(L_mnel); FREE(U_mnel); return 1; } /* output p */ sciErr = createMatrixOfDoubleAsInteger(pvApiCtx, 4, n_row, 1, p); if (sciErr.iErr) { printError(&sciErr, 0); FREE(L_mnel); FREE(U_mnel); return 1; } /* output q */ sciErr = createMatrixOfDoubleAsInteger(pvApiCtx, 5, n_col, 1, q); if (sciErr.iErr) { printError(&sciErr, 0); FREE(L_mnel); FREE(U_mnel); return 1; } /* output res */ sciErr = createMatrixOfDouble(pvApiCtx, 6, n_row, 1, Rs); if (sciErr.iErr) { printError(&sciErr, 0); FREE(L_mnel); FREE(U_mnel); return 1; } the_end: FREE(L_mnel); FREE(L_icol); FREE(L_R); FREE(L_ptrow); FREE(p); FREE(U_mnel); FREE(U_icol); FREE(U_R); FREE(U_ptrow); FREE(q); FREE(V_irow); FREE(V_R); FREE(V_ptcol); FREE(Rs); if ( it_flag == 1 ) { FREE(L_I); FREE(V_I); FREE(U_I); } switch (error_flag) { case 0: /* no error */ AssignOutputVariable(pvApiCtx, 1) = 2; AssignOutputVariable(pvApiCtx, 2) = 3; AssignOutputVariable(pvApiCtx, 3) = 4; AssignOutputVariable(pvApiCtx, 4) = 5; AssignOutputVariable(pvApiCtx, 5) = 6; ReturnArguments(pvApiCtx); return 0; case 1: /* enough memory (with malloc) */ Scierror(999, _("%s: No more memory.\n"), fname); break; case 2: /* a problem with one umfpack routine */ Scierror(999, "%s: %s\n", fname, UmfErrorMes(stat)); break; } return 1; }
/*--------------------------------------------------------------------------*/ int sci_basename(char *fname,unsigned long fname_len) { SciErr sciErr; BOOL flagexpand = TRUE; /* default */ int *piAddressVarOne = NULL; wchar_t **pStVarOne = NULL; int *lenStVarOne = NULL; int iType1 = 0; int m1 = 0, n1 = 0; wchar_t **pStResult = NULL; /* Check Input & Output parameters */ CheckRhs(1,3); CheckLhs(1,1); if (Rhs > 2) { int *piAddressVarThree = NULL; int *piData = NULL; int iType3 = 0; int m3 = 0, n3 = 0; sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType3); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); return 0; } if (iType3 != sci_boolean) { Scierror(999,_("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 3); return 0; } sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarThree, &m3, &n3, &piData); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); return 0; } sciErr = getVarDimension(pvApiCtx, piAddressVarThree, &m3, &n3); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); return 0; } if ( (m3 != n3) && (n3 != 1) ) { Scierror(999,_("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 3); return 0; } flagexpand = piData[0]; } if (Rhs > 1) { int *piAddressVarTwo = NULL; int *piData = NULL; int iType2 = 0; int m2 = 0, n2 = 0; sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 0; } if (iType2 != sci_boolean) { Scierror(999,_("%s: Wrong type for input argument #%d: A boolean expected.\n"), fname, 2); return 0; } sciErr = getVarDimension(pvApiCtx, piAddressVarTwo, &m2, &n2); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 0; } if ( (m2 != n2) && (n2 != 1) ) { Scierror(999,_("%s: Wrong size for input argument #%d: A boolean expected.\n"), fname, 2); return 0; } sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarTwo, &m2, &n2, &piData); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 0; } } sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (iType1 == sci_matrix) { sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if ( (m1 == n1) && (m1 == 0) ) { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m1, n1, NULL); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999,_("%s: Memory allocation error.\n"), fname); return 0; } LhsVar(1) = Rhs + 1; PutLhsVar(); } else { Scierror(999,_("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1); } } else if (iType1 == sci_strings) { int i = 0; sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } lenStVarOne = (int*)MALLOC(sizeof(int) * (m1 * n1)); if (lenStVarOne == NULL) { Scierror(999,_("%s: Memory allocation error.\n"),fname); return 0; } // get lenStVarOne value sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, NULL); if(sciErr.iErr) { freeArrayOfWideString(pStVarOne, m1 * n1); if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } pStVarOne = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1)); if (pStVarOne == NULL) { if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} Scierror(999,_("%s: Memory allocation error.\n"),fname); return 0; } for (i = 0; i < (m1 * n1); i++) { pStVarOne[i] = (wchar_t*)MALLOC(sizeof(wchar_t) * (lenStVarOne[i] + 1)); if (pStVarOne[i] == NULL) { freeArrayOfWideString(pStVarOne, m1 * n1); if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} Scierror(999,_("%s: Memory allocation error.\n"),fname); return 0; } } // get pStVarOne sciErr = getMatrixOfWideString(pvApiCtx, piAddressVarOne, &m1, &n1, lenStVarOne, pStVarOne); if(sciErr.iErr) { freeArrayOfWideString(pStVarOne, m1 * n1); if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } pStResult = (wchar_t**)MALLOC(sizeof(wchar_t*) * (m1 * n1)); if (pStResult == NULL) { if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} Scierror(999,_("%s: Memory allocation error.\n"),fname); return 0; } for (i=0;i< m1 * n1; i++) { pStResult[i] = basenameW(pStVarOne[i], flagexpand); } sciErr = createMatrixOfWideString(pvApiCtx, Rhs + 1, m1, n1, pStResult); if(sciErr.iErr) { printError(&sciErr, 0); Scierror(999,_("%s: Memory allocation error.\n"), fname); return 0; } LhsVar(1) = Rhs + 1; if (lenStVarOne) {FREE(lenStVarOne); lenStVarOne = NULL;} freeArrayOfWideString(pStResult, m1 * n1); freeArrayOfWideString(pStVarOne, m1 * n1); PutLhsVar(); } else { Scierror(999,_("%s: Wrong type for input argument #%d: String array expected.\n"), fname, 1); } return 0; }
// ============================================================================= int sci_csvStringToDouble(char *fname, unsigned long fname_len) { SciErr sciErr; int iErr = 0; int m1 = 0, n1 = 0; char **pStringValues = NULL; BOOL bConvertToNan = TRUE; complexArray *ptrComplexArray = NULL; stringToComplexError ierr = STRINGTOCOMPLEX_ERROR; CheckRhs(1, 2); CheckLhs(1, 1); if (Rhs == 1) { bConvertToNan = TRUE; } else /* Rhs == 2 */ { bConvertToNan = (BOOL)csv_getArgumentAsScalarBoolean(pvApiCtx, 2, fname, &iErr); if (iErr) { return 0; } } pStringValues = csv_getArgumentAsMatrixOfString(pvApiCtx, 1, fname, &m1, &n1, &iErr); if (iErr) { return 0; } ptrComplexArray = stringsToComplexArray((const char**)pStringValues, m1 * n1, getCsvDefaultDecimal(), bConvertToNan, &ierr); freeArrayOfString(pStringValues, m1 * n1); pStringValues = NULL; if (ptrComplexArray == NULL) { switch (ierr) { case STRINGTOCOMPLEX_NOT_A_NUMBER: case STRINGTOCOMPLEX_ERROR: Scierror(999, _("%s: can not convert data.\n"), fname); return 0; default: Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } switch (ierr) { case STRINGTOCOMPLEX_NOT_A_NUMBER: case STRINGTOCOMPLEX_NO_ERROR: { if (ptrComplexArray->isComplex) { sciErr = createComplexMatrixOfDouble(pvApiCtx, Rhs + 1, m1, n1, ptrComplexArray->realPart, ptrComplexArray->imagPart); } else { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m1, n1, ptrComplexArray->realPart); } freeComplexArray(ptrComplexArray); ptrComplexArray = NULL; } break; case STRINGTOCOMPLEX_MEMORY_ALLOCATION: { Scierror(999, _("%s: Memory allocation error.\n"), fname); } default: case STRINGTOCOMPLEX_ERROR: { Scierror(999, _("%s: can not convert data.\n"), fname); } } if (sciErr.iErr) { printError(&sciErr, 0); } else { LhsVar(1) = Rhs + 1; PutLhsVar(); } return 0; }
int sci_eigs(char *fname, void* pvApiCtx) { SciErr sciErr; int *piAddressVarOne = NULL; int iRowsOne = 0; int iColsOne = 0; double elemt1 = 0; double elemt2 = 0; double* Areal = NULL; doublecomplex* Acplx = NULL; int Asym = 1; int Acomplex = 0; int N = 0; int *piAddressVarTwo = NULL; int iTypeVarTwo = 0; int iRowsTwo = 0; int iColsTwo = 0; double* Breal = NULL; doublecomplex* Bcplx = NULL; int Bcomplex = 0; int matB = 0; int *piAddressVarThree = NULL; double dblNEV = 0; int iNEV = 0; int *piAddressVarFour = NULL; int iTypeVarFour = 0; int iRowsFour = 0; int iColsFour = 0; char* pstData = NULL; doublecomplex SIGMA; int *piAddressVarFive = NULL; double dblMAXITER = 0; int *piAddressVarSix = NULL; double dblTOL = 0; int *piAddressVarSeven = NULL; int TypeVarSeven = 0; int RowsSeven = 0; int ColsSeven = 0; double* dblNCV = NULL; int *piAddressVarEight = NULL; int iTypeVarEight = 0; double dblCHOLB = 0; int iCHOLB = 0; int *piAddressVarNine = NULL; int iTypeVarNine = 0; int iRowsNine = 0; int iColsNine = 0; double* RESID = NULL; doublecomplex* RESIDC = NULL; int *piAddressVarTen = NULL; int iINFO = 0; int RVEC = 0; // Output arguments double* eigenvalue = NULL; double* eigenvector = NULL; doublecomplex* eigenvalueC = NULL; doublecomplex* eigenvectorC = NULL; double* mat_eigenvalue = NULL; doublecomplex* mat_eigenvalueC = NULL; int INFO_EUPD = 0; int error = 0; int iErr = 0; int i = 0; int j = 0; CheckInputArgument(pvApiCtx, 1, 10); CheckOutputArgument(pvApiCtx, 0, 2); /**************************************** * First variable : A * *****************************************/ sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 1; } sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &iRowsOne, &iColsOne); //check if A is a square matrix if (iRowsOne * iColsOne == 1 || iRowsOne != iColsOne) { Scierror(999, _("%s: Wrong type for input argument #%d: A square matrix expected.\n"), "eigs", 1); return 1; } N = iRowsOne; //check if A is complex if (isVarComplex(pvApiCtx, piAddressVarOne)) { sciErr = getComplexZMatrixOfDouble(pvApiCtx, piAddressVarOne, &iRowsOne, &iColsOne, &Acplx); Acomplex = 1; } else { sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &iRowsOne, &iColsOne, &Areal); for (i = 0; i < iColsOne; i++) { for (j = 0; j < i; j++) { elemt1 = Areal[j + i * iColsOne]; elemt2 = Areal[j * iColsOne + i]; if (fabs(elemt1 - elemt2) > 0) { Asym = 0; break; } } if (Asym == 0) { break; } } } /**************************************** * Second variable : B * *****************************************/ sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 1; } sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iTypeVarTwo); if (sciErr.iErr || iTypeVarTwo != sci_matrix) { printError(&sciErr, 0); Scierror(999, _("%s: Wrong type for input argument #%d: An empty matrix or full or sparse square matrix expected.\n"), "eigs", 2); return 1; } sciErr = getVarDimension(pvApiCtx, piAddressVarTwo, &iRowsTwo, &iColsTwo); matB = iRowsTwo * iColsTwo; if (matB && (iRowsTwo != iRowsOne || iColsTwo != iColsOne)) { Scierror(999, _("%s: Wrong dimension for input argument #%d: B must have the same size as A.\n"), "eigs", 2); return 1; } if (isVarComplex(pvApiCtx, piAddressVarTwo)) { sciErr = getComplexZMatrixOfDouble(pvApiCtx, piAddressVarTwo, &iRowsTwo, &iColsTwo, &Bcplx); Bcomplex = 1; } else { sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarTwo, &iRowsTwo, &iColsTwo, &Breal); } if (matB != 0) { if (Acomplex && !Bcomplex) { Bcplx = (doublecomplex*)MALLOC(N * N * sizeof(doublecomplex)); memset(Bcplx, 0, N * N * sizeof(doublecomplex)); Bcomplex = 1; for (i = 0 ; i < N * N ; i++) { Bcplx[i].r = Breal[i]; } } if (!Acomplex && Bcomplex) { Acplx = (doublecomplex*)MALLOC(N * N * sizeof(doublecomplex)); memset(Acplx, 0, N * N * sizeof(doublecomplex)); Acomplex = 1; for (i = 0 ; i < N * N ; i++) { Acplx[i].r = Areal[i]; } } } /**************************************** * NEV * *****************************************/ sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); FREE_AB; return 1; } iErr = getScalarDouble(pvApiCtx, piAddressVarThree, &dblNEV); if (iErr) { Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "eigs", 3); FREE_AB; return 1; } if (isVarComplex(pvApiCtx, piAddressVarThree)) { Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "eigs", 3); FREE_AB; return 1; } if (dblNEV != floor(dblNEV) || (dblNEV <= 0)) { Scierror(999, _("%s: Wrong type for input argument #%d: k must be a positive integer.\n"), "eigs", 3); FREE_AB; return 1; } if (!finite(dblNEV)) { Scierror(999, _("%s: Wrong value for input argument #%d: k must be in the range 1 to N.\n"), "eigs", 3); FREE_AB; return 1; } iNEV = (int)dblNEV; /**************************************** * SIGMA AND WHICH * *****************************************/ sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddressVarFour); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4); FREE_AB; return 1; } sciErr = getVarType(pvApiCtx, piAddressVarFour, &iTypeVarFour); if (sciErr.iErr || (iTypeVarFour != sci_matrix && iTypeVarFour != sci_strings)) { Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "eigs", 4); FREE_AB; return 1; } if (iTypeVarFour == sci_strings) { int iErr = getAllocatedSingleString(pvApiCtx, piAddressVarFour, &pstData); if (iErr) { FREE_AB; return 1; } if (strcmp(pstData, "LM") != 0 && strcmp(pstData, "SM") != 0 && strcmp(pstData, "LR") != 0 && strcmp(pstData, "SR") != 0 && strcmp(pstData, "LI") != 0 && strcmp(pstData, "SI") != 0 && strcmp(pstData, "LA") != 0 && strcmp(pstData, "SA") != 0 && strcmp(pstData, "BE") != 0) { if (!Acomplex && Asym) { Scierror(999, _("%s: Wrong value for input argument #%d: Unrecognized sigma value.\n Sigma must be one of '%s', '%s', '%s', '%s' or '%s'.\n" ), "eigs", 4, "LM", "SM", "LA", "SA", "BE"); freeAllocatedSingleString(pstData); return 1; } else { Scierror(999, _("%s: Wrong value for input argument #%d: Unrecognized sigma value.\n Sigma must be one of '%s', '%s', '%s', '%s', '%s' or '%s'.\n " ), "eigs", 4, "LM", "SM", "LR", "SR", "LI", "SI"); FREE_AB; freeAllocatedSingleString(pstData); return 1; } } if ((Acomplex || !Asym) && (strcmp(pstData, "LA") == 0 || strcmp(pstData, "SA") == 0 || strcmp(pstData, "BE") == 0)) { Scierror(999, _("%s: Invalid sigma value for complex or non symmetric problem.\n"), "eigs", 4); FREE_AB; freeAllocatedSingleString(pstData); return 1; } if (!Acomplex && Asym && (strcmp(pstData, "LR") == 0 || strcmp(pstData, "SR") == 0 || strcmp(pstData, "LI") == 0 || strcmp(pstData, "SI") == 0)) { Scierror(999, _("%s: Invalid sigma value for real symmetric problem.\n"), "eigs", 4); freeAllocatedSingleString(pstData); return 1; } SIGMA.r = 0; SIGMA.i = 0; } if (iTypeVarFour == sci_matrix) { sciErr = getVarDimension(pvApiCtx, piAddressVarFour, &iRowsFour, &iColsFour); if (iRowsFour * iColsFour != 1) { Scierror(999, _("%s: Wrong type for input argument #%d: A scalar expected.\n"), "eigs", 4); FREE_AB; return 1; } if (getScalarComplexDouble(pvApiCtx, piAddressVarFour, &SIGMA.r, &SIGMA.i)) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 4); FREE_AB; return 1; } if (C2F(isanan)(&SIGMA.r) || C2F(isanan)(&SIGMA.i)) { Scierror(999, _("%s: Wrong type for input argument #%d: sigma must be a real.\n"), "eigs", 4); FREE_AB; return 1; } pstData = "LM"; } /**************************************** * MAXITER * *****************************************/ sciErr = getVarAddressFromPosition(pvApiCtx, 5, &piAddressVarFive); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 5); FREE_AB; FREE_PSTDATA; return 0; } iErr = getScalarDouble(pvApiCtx, piAddressVarFive, &dblMAXITER); if (iErr) { Scierror(999, _("%s: Wrong type for input argument #%d: %s must be a scalar.\n"), "eigs", 5, "opts.maxiter"); FREE_AB; FREE_PSTDATA; return 1; } if ((dblMAXITER != floor(dblMAXITER)) || (dblMAXITER <= 0)) { Scierror(999, _("%s: Wrong type for input argument #%d: %s must be an integer positive value.\n"), "eigs", 5, "opts.maxiter"); FREE_AB; FREE_PSTDATA; return 1; } /**************************************** * TOL * *****************************************/ sciErr = getVarAddressFromPosition(pvApiCtx, 6, &piAddressVarSix); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 6); FREE_AB; FREE_PSTDATA; return 1; } iErr = getScalarDouble(pvApiCtx, piAddressVarSix, &dblTOL); if (iErr) { Scierror(999, _("%s: Wrong type for input argument #%d: %s must be a real scalar.\n"), "eigs", 6, "opts.tol"); FREE_AB; FREE_PSTDATA; return 1; } if (C2F(isanan)(&dblTOL)) { Scierror(999, _("%s: Wrong type for input argument #%d: %s must be a real scalar.\n"), "eigs", 6, "opts.tol"); FREE_AB; FREE_PSTDATA; return 1; } /**************************************** * NCV * *****************************************/ sciErr = getVarAddressFromPosition(pvApiCtx, 7, &piAddressVarSeven); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 7); FREE_AB; FREE_PSTDATA; return 1; } sciErr = getVarType(pvApiCtx, piAddressVarSeven, &TypeVarSeven); if (sciErr.iErr || TypeVarSeven != sci_matrix) { printError(&sciErr, 0); Scierror(999, _("%s: Wrong type for input argument #%d: %s must be an integer scalar.\n"), "eigs", 7, "opts.ncv"); FREE_AB; FREE_PSTDATA; return 1; } else { if (isVarComplex(pvApiCtx, piAddressVarSeven)) { Scierror(999, _("%s: Wrong type for input argument #%d: %s must be an integer scalar.\n"), "eigs", 7, "opts.ncv"); } else { sciErr = getVarDimension(pvApiCtx, piAddressVarSeven, &RowsSeven, &ColsSeven); if (RowsSeven * ColsSeven > 1) { Scierror(999, _("%s: Wrong type for input argument #%d: %s must be an integer scalar.\n"), "eigs", 7, "opts.ncv"); FREE_AB; FREE_PSTDATA; return 1; } if (RowsSeven * ColsSeven == 1) { sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarSeven, &RowsSeven, &ColsSeven, &dblNCV); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 7); FREE_AB; FREE_PSTDATA; return 1; } if (dblNCV[0] != floor(dblNCV[0])) { Scierror(999, _("%s: Wrong type for input argument #%d: %s must be an integer scalar.\n"), "eigs", 7, "opts.ncv"); FREE_AB; FREE_PSTDATA; return 1; } } } } /**************************************** * CHOLB * *****************************************/ sciErr = getVarAddressFromPosition(pvApiCtx, 8, &piAddressVarEight); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 8); FREE_AB; FREE_PSTDATA; return 1; } sciErr = getVarType(pvApiCtx, piAddressVarEight, &iTypeVarEight); if (sciErr.iErr || (iTypeVarEight != sci_matrix && iTypeVarEight != sci_boolean)) { Scierror(999, _("%s: Wrong type for input argument #%d: %s must be an integer scalar or a boolean.\n"), "eigs", 8, "opts.cholB"); FREE_AB; FREE_PSTDATA; return 1; } if (iTypeVarEight == sci_boolean) { iErr = getScalarBoolean(pvApiCtx, piAddressVarEight, &iCHOLB); if (iErr) { Scierror(999, _("%s: Wrong type for input argument #%d: %s must be an integer scalar or a boolean.\n"), "eigs", 8, "opts.cholB"); FREE_AB; FREE_PSTDATA; return 1; } if (iCHOLB != 1 && iCHOLB != 0) { Scierror(999, _("%s: Wrong value for input argument #%d: %s must be %s or %s.\n"), "eigs", 8, "opts.cholB", "%f", "%t"); FREE_AB; FREE_PSTDATA; return 1; } dblCHOLB = (double) iCHOLB; } if (iTypeVarEight == sci_matrix) { iErr = getScalarDouble(pvApiCtx, piAddressVarEight, &dblCHOLB); if (iErr) { Scierror(999, _("%s: Wrong type for input argument #%d: %s must be an integer scalar or a boolean.\n"), "eigs", 8, "opts.cholB"); FREE_AB; FREE_PSTDATA; return 1; } if (dblCHOLB != 1 && dblCHOLB != 0) { Scierror(999, _("%s: Wrong value for input argument #%d: %s must be %s or %s.\n"), "eigs", 8, "opts.cholB", "%f", "%t"); FREE_AB; FREE_PSTDATA; return 1; } } if ( dblCHOLB ) // check that B is upper triangular with non zero element on the diagonal { if (!Bcomplex) { for (i = 0; i < N; i++) { for (j = 0; j <= i; j++) { if (i == j && Breal[i + j * N] == 0) { Scierror(999, _("%s: B is not positive definite. Try with sigma='SM' or sigma=scalar.\n"), "eigs"); FREE_PSTDATA; return 0; } else { if ( j < i && Breal[i + j * N] != 0 ) { Scierror(999, _("%s: If opts.cholB is true, B should be upper triangular.\n"), "eigs"); FREE_PSTDATA; return 0; } } } } } else { for (i = 0; i < N; i++) { for (j = 0; j <= i; j++) { if (i == j && Bcplx[i + i * N].r == 0 && Bcplx[i + i * N].i == 0) { Scierror(999, _("%s: B is not positive definite. Try with sigma='SM' or sigma=scalar.\n"), "eigs"); FREE_AB; FREE_PSTDATA; return 0; } else { if ( j < i && (Bcplx[i + j * N].r != 0 || Bcplx[i + j * N].i != 0) ) { Scierror(999, _("%s: If opts.cholB is true, B should be upper triangular.\n"), "eigs"); FREE_AB; FREE_PSTDATA; return 0; } } } } } } /**************************************** * RESID * *****************************************/ sciErr = getVarAddressFromPosition(pvApiCtx, 9, &piAddressVarNine); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 9); FREE_AB; FREE_PSTDATA; return 1; } sciErr = getVarType(pvApiCtx, piAddressVarNine, &iTypeVarNine); if (sciErr.iErr || iTypeVarNine != sci_matrix) { printError(&sciErr, 0); Scierror(999, _("%s: Wrong type for input argument #%d: A real or complex matrix expected.\n"), "eigs", 9); FREE_AB; FREE_PSTDATA; return 1; } else { sciErr = getVarDimension(pvApiCtx, piAddressVarNine, &iRowsNine, &iColsNine); if (iRowsNine * iColsNine == 1 || iRowsNine * iColsNine != N) { Scierror(999, _("%s: Wrong dimension for input argument #%d: Start vector %s must be N by 1.\n"), "eigs", 9, "opts.resid"); FREE_AB; FREE_PSTDATA; return 1; } } if (!Acomplex && !Bcomplex) { if (isVarComplex(pvApiCtx, piAddressVarNine)) { Scierror(999, _("%s: Wrong type for input argument #%d: Start vector %s must be real for real problems.\n"), "eigs", 9, "opts.resid"); FREE_PSTDATA; return 1; } else { sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarNine, &iRowsNine, &iColsNine, &RESID); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), "eigs", 9); FREE_PSTDATA; return 1; } } } else { sciErr = getComplexZMatrixOfDouble(pvApiCtx, piAddressVarNine, &iRowsNine, &iColsNine, &RESIDC); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), "eigs", 9); FREE_AB; FREE_PSTDATA; return 1; } } /**************************************** * INFO * *****************************************/ sciErr = getVarAddressFromPosition(pvApiCtx, 10, &piAddressVarTen); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), "eigs", 9); FREE_AB; FREE_PSTDATA; return 1; } iErr = getScalarInteger32(pvApiCtx, piAddressVarTen, &iINFO); if (iErr) { Scierror(999, _("%s: Wrong type for input argument #%d: An integer expected.\n"), "eigs", 1); FREE_AB; FREE_PSTDATA; return 1; } // Initialization output arguments if (nbOutputArgument(pvApiCtx) > 1) { RVEC = 1; } if (Acomplex || Bcomplex || !Asym) { eigenvalueC = (doublecomplex*)CALLOC((iNEV + 1), sizeof(doublecomplex)); if (RVEC) { eigenvectorC = (doublecomplex*)CALLOC(N * (iNEV + 1), sizeof(doublecomplex)); } } else { eigenvalue = (double*)CALLOC(iNEV, sizeof(double)); /* we should allocate eigenvector only if RVEC is true, but dseupd segfaults if Z is not allocated even when RVEC is false, contrary to the docs.*/ eigenvector = (double*)CALLOC(iNEV * N, sizeof(double)); } error = eigs(Areal, Acplx, N, Acomplex, Asym, Breal, Bcplx, Bcomplex, matB, iNEV, SIGMA, pstData, &dblMAXITER, &dblTOL, dblNCV, RESID, RESIDC, &iINFO, &dblCHOLB, INFO_EUPD, eigenvalue, eigenvector, eigenvalueC, eigenvectorC, RVEC); FREE_AB; FREE_PSTDATA; switch (error) { case -1 : if (Asym && !Acomplex && !Bcomplex) { Scierror(999, _("%s: Wrong value for input argument #%d: For real symmetric problems, NCV must be k < NCV <= N.\n"), "eigs", 7); } else { if (!Asym && !Acomplex && !Bcomplex) { Scierror(999, _("%s: Wrong value for input argument #%d: For real non symmetric problems, NCV must be k + 2 < NCV <= N.\n"), "eigs", 7); } else { Scierror(999, _("%s: Wrong value for input argument #%d: For complex problems, NCV must be k + 1 < NCV <= N.\n"), "eigs", 7); } } ReturnArguments(pvApiCtx); return 1; case -2 : if (Asym && !Acomplex && !Bcomplex) { Scierror(999, _("%s: Wrong value for input argument #%d: For real symmetric problems, k must be an integer in the range 1 to N - 1.\n"), "eigs", 3); } else { Scierror(999, _("%s: Wrong value for input argument #%d: For real non symmetric or complex problems, k must be an integer in the range 1 to N - 2.\n"), "eigs", 3); } ReturnArguments(pvApiCtx); return 1; case -3 : Scierror(999, _("%s: Error with input argument #%d: B is not positive definite. Try with sigma='SM' or sigma=scalar.\n"), "eigs", 2); ReturnArguments(pvApiCtx); return 0; case -4 : if (!Acomplex && !Bcomplex) { if (Asym) { Scierror(999, _("%s: Error with %s: info = %d \n"), "eigs", "DSAUPD", iINFO); } else { Scierror(999, _("%s: Error with %s: info = %d \n"), "eigs", "DNAUPD", iINFO); } } else { Scierror(999, _("%s: Error with %s: info = %d \n"), "eigs", "ZNAUPD", iINFO); } ReturnArguments(pvApiCtx); return 1; case -5 : if (!Acomplex && !Bcomplex) { if (Asym) { Scierror(999, _("%s: Error with %s: unknown mode returned.\n"), "eigs", "DSAUPD"); } else { Scierror(999, _("%s: Error with %s: unknown mode returned.\n"), "eigs", "DNAUPD"); } } else { Scierror(999, _("%s: Error with %s: unknown mode returned.\n"), "eigs", "ZNAUPD"); } ReturnArguments(pvApiCtx); return 1; case -6 : if (!Acomplex && !Bcomplex) { if (Asym) { Scierror(999, _("%s: Error with %s: info = %d \n"), "eigs", "DSEUPD", INFO_EUPD); } else { Scierror(999, _("%s: Error with %s: info = %d \n"), "eigs", "DNEUPD", INFO_EUPD); } } else { Scierror(999, _("%s: Error with %s: info = %d \n"), "eigs", "ZNEUPD", INFO_EUPD); } ReturnArguments(pvApiCtx); FREE(mat_eigenvalue); return 1; } if (nbOutputArgument(pvApiCtx) <= 1) { if (eigenvalue) { sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iNEV, 1, eigenvalue); FREE(eigenvalue); FREE(eigenvector); } else if (eigenvalueC) { sciErr = createComplexZMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iNEV, 1, eigenvalueC); FREE(eigenvalueC); } if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; } else { // create a matrix which contains the eigenvalues if (eigenvalue) { mat_eigenvalue = (double*)CALLOC(iNEV * iNEV, sizeof(double)); for (i = 0; i < iNEV; i++) { mat_eigenvalue[i * iNEV + i] = eigenvalue[i]; } sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iNEV, iNEV, mat_eigenvalue); FREE(eigenvalue); FREE(mat_eigenvalue); } else if (eigenvalueC) { mat_eigenvalueC = (doublecomplex*)CALLOC(iNEV * iNEV, sizeof(doublecomplex)); for (i = 0; i < iNEV; i++) { mat_eigenvalueC[i * iNEV + i] = eigenvalueC[i]; } sciErr = createComplexZMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iNEV, iNEV, mat_eigenvalueC); FREE(eigenvalueC); FREE(mat_eigenvalueC); } if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } if (eigenvector) { sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, N, iNEV, eigenvector); FREE(eigenvector); } else if (eigenvectorC) { sciErr = createComplexZMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, N, iNEV, eigenvectorC); FREE(eigenvectorC); } if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; } ReturnArguments(pvApiCtx); return 0; }
/* ==================================================================== */ int sci_foo(char *fname, void* pvApiCtx, unsigned long fname_len) { // Error management variable SciErr sciErr; ////////// Variables declaration ////////// int m1 = 0, n1 = 0; int *piAddressVarOne = NULL; double *matrixOfDouble = NULL; double *newMatrixOfDouble = NULL; int m2 = 0, n2 = 0; int *piAddressVarTwo = NULL; int *matrixOfBoolean = NULL; int *newMatrixOfBoolean = NULL; int i = 0; ////////// Check the number of input and output arguments ////////// /* --> [c, d] = foo(a, b) */ /* check that we have only 2 input arguments */ /* check that we have only 2 output argument */ CheckInputArgument(pvApiCtx, 2, 2) ; CheckOutputArgument(pvApiCtx, 2, 2) ; ////////// Manage the first input argument (double) ////////// /* get Address of inputs */ sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } /* Check that the first input argument is a real matrix (and not complex) */ if ( !isDoubleType(pvApiCtx, piAddressVarOne) || isVarComplex(pvApiCtx, piAddressVarOne) ) { Scierror(999, _("%s: Wrong type for input argument #%d: A real matrix expected.\n"), fname, 1); return 0; } /* get matrix */ sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m1, &n1, &matrixOfDouble); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } ////////// Manage the second input argument (boolean) ////////// /* get Address of inputs */ sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if ( !isBooleanType(pvApiCtx, piAddressVarTwo) ) { Scierror(999, _("%s: Wrong type for input argument #%d: A boolean matrix expected.\n"), fname, 2); return 0; } /* get matrix */ sciErr = getMatrixOfBoolean(pvApiCtx, piAddressVarTwo, &m2, &n2, &matrixOfBoolean); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } ////////// Check the consistency of the two input arguments ////////// if ((m1 != m2) | - (n1 != n2)) { Scierror(999, _("%s: Wrong size for input arguments: Same size expected.\n"), fname, 1); return 0; } newMatrixOfDouble = (double*)malloc(sizeof(double) * m1 * n1); ////////// Application code ////////// // Could be replaced by a call to a library for (i = 0; i < m1 * n1; i++) { /* For each element of the matrix, multiply by 2 */ newMatrixOfDouble[i] = matrixOfDouble[i] * 2; } newMatrixOfBoolean = (int*)malloc(sizeof(double) * m2 * n2); for (i = 0; i < m2 * n2; i++) { /* For each element of the matrix, invert the value */ newMatrixOfBoolean[i] = matrixOfBoolean[i] == TRUE ? FALSE : TRUE; } ////////// Create the output arguments ////////// /* Create the matrix as return of the function */ sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, m1, n1, newMatrixOfDouble); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } /* Create the matrix as return of the function */ sciErr = createMatrixOfBoolean(pvApiCtx, nbInputArgument(pvApiCtx) + 2, m2, n2, newMatrixOfBoolean); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } ////////// Return the output arguments to the Scilab engine ////////// AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; ReturnArguments(pvApiCtx); return 0; }
int write_double(char *fname, void* pvApiCtx) { SciErr sciErr; int i, j; //first variable info : real matrix of double 3 x 4 int iRows1 = 3; int iCols1 = 4; double* pdblReal1 = NULL; //second variable info : complex matrix of double 4 x 6 int iRows2 = 4; int iCols2 = 6; double* pdblReal2 = NULL; double* pdblImg2 = NULL; /************************ * First variable * ************************/ //alloc array of data in OS memory pdblReal1 = (double*)MALLOC(sizeof(double) * iRows1 * iCols1); //fill array with incremental values //[ 0 1 2 3 // 4 5 6 7 // 8 9 10 11] for (i = 0 ; i < iRows1 ; i++) { for (j = 0 ; j < iCols1 ; j++) { pdblReal1[i + iRows1 * j] = i * iCols1 + j; } } //can be written in a single loop //for(i = 0 ; i < iRows1 * iCols1; i++) //{ // pdblReal1[i] = i; //} //create a variable from a existing data array sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iRows1, iCols1, pdblReal1); //after creation, we can free memory. FREE(pdblReal1); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } /************************* * Second variable * *************************/ //reserve space in scilab memory and fill it sciErr = allocComplexMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 2, iRows2, iCols2, &pdblReal2, &pdblImg2); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } //fill array with incremental values for real part and decremental for imaginary part //[ 23i 1+22i 2+21i 3+20i 4+19i 5+18i // 6+17i 7+16i 8+15i 9+14i 10+13i 11+12i // 12+11i 13+10i 14+9i 15+8i 16+7i 17+6i // 18+5i 19+4i 20+3i 21+2i 22+1i 23 ] for (i = 0 ; i < iRows2 ; i++) { for (j = 0 ; j < iCols2 ; j++) { pdblReal2[i + iRows2 * j] = i * iCols2 + j; pdblImg2 [i + iRows2 * j] = (iRows2 * iCols2 - 1) - (i * iCols2 + j); } } //can be written in a single loop //for(i = 0 ; i < iRows2 * iCols2; i++) //{ // pdblReal2[i] = i; // pdblImg2 [i] = (iRows2 * iCols2 - 1) - i; //} // /!\ DO NOT FREE MEMORY, in this case, it's the Scilab memory //assign allocated variables to Lhs position AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; AssignOutputVariable(pvApiCtx, 2) = nbInputArgument(pvApiCtx) + 2; return 0; }
/*--------------------------------------------------------------------------*/ int sci_fscanfMat(char *fname, void* pvApiCtx) { SciErr sciErr; int *piAddressVarOne = NULL; int m1 = 0, n1 = 0; int iType1 = 0; char *filename = NULL; char *expandedFilename = NULL; char *Format = NULL; char *separator = NULL; BOOL bIsDefaultSeparator = TRUE; fscanfMatResult *results = NULL; CheckRhs(1, 3); CheckLhs(1, 2); if (Rhs == 3) { int *piAddressVarThree = NULL; int m3 = 0, n3 = 0; int iType3 = 0; sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3); return 0; } if (isStringType(pvApiCtx, piAddressVarThree) == 0 || isScalar(pvApiCtx, piAddressVarThree) == 0) { Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 3); return 0; } if (getAllocatedSingleString(pvApiCtx, piAddressVarThree, &separator)) { freeVar(&filename, &expandedFilename, &Format, &separator); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } bIsDefaultSeparator = FALSE; } if (Rhs >= 2) { int *piAddressVarTwo = NULL; int m2 = 0, n2 = 0; int iType2 = 0; sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo); if (sciErr.iErr) { freeVar(&filename, &expandedFilename, &Format, &separator); printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 0; } sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2); if (sciErr.iErr) { freeVar(&filename, &expandedFilename, &Format, &separator); printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2); return 0; } if (isStringType(pvApiCtx, piAddressVarTwo) == 0 || isScalar(pvApiCtx, piAddressVarTwo) == 0) { freeVar(&filename, &expandedFilename, &Format, &separator); Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 2); return 0; } if (getAllocatedSingleString(pvApiCtx, piAddressVarTwo, &Format)) { freeVar(&filename, &expandedFilename, &Format, &separator); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { Format = os_strdup(DEFAULT_FSCANFMAT_FORMAT); } sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne); if (sciErr.iErr) { freeVar(&filename, &expandedFilename, &Format, &separator); printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (isStringType(pvApiCtx, piAddressVarOne) == 0 || isScalar(pvApiCtx, piAddressVarOne) == 0) { freeVar(&filename, &expandedFilename, &Format, &separator); Scierror(999, _("%s: Wrong type for input argument #%d: string expected.\n"), fname, 1); return 0; } if (getAllocatedSingleString(pvApiCtx, piAddressVarOne, &filename)) { freeVar(&filename, &expandedFilename, &Format, &separator); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } expandedFilename = expandPathVariable(filename); if (bIsDefaultSeparator) { #define NB_DEFAULT_SUPPORTED_SEPARATORS 2 /* bug 8148 */ /* default separator can be a space or a tabulation */ char *supportedSeparators[NB_DEFAULT_SUPPORTED_SEPARATORS] = {DEFAULT_FSCANFMAT_SEPARATOR, "\t"}; int i = 0; for (i = 0; i < NB_DEFAULT_SUPPORTED_SEPARATORS; i++) { results = fscanfMat(expandedFilename, Format, supportedSeparators[i]); if (results && results->err == FSCANFMAT_NO_ERROR) { break; } freeFscanfMatResult(results); } } else { results = fscanfMat(expandedFilename, Format, separator); } if (results == NULL) { freeVar(&filename, &expandedFilename, &Format, &separator); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } freeVar(NULL, &expandedFilename, &Format, &separator); switch (results->err) { case FSCANFMAT_NO_ERROR: { if ( (results->values) && (results->m > 0) && (results->n > 0)) { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, results->m, results->n, results->values); if (sciErr.iErr) { FREE(filename); freeFscanfMatResult(results); printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { if (createEmptyMatrix(pvApiCtx, Rhs + 1)) { FREE(filename); freeFscanfMatResult(results); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } LhsVar(1) = Rhs + 1; if (Lhs == 2) { if (results->text) { sciErr = createMatrixOfString(pvApiCtx, Rhs + 2, results->sizeText, 1, (char const * const*) results->text); if (sciErr.iErr) { FREE(filename); freeFscanfMatResult(results); printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { if (createSingleString(pvApiCtx, Rhs + 2, "")) { FREE(filename); freeFscanfMatResult(results); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } LhsVar(2) = Rhs + 2; } freeFscanfMatResult(results); FREE(filename); PutLhsVar(); return 0; } case FSCANFMAT_MOPEN_ERROR: { Scierror(999, _("%s: can not open file %s.\n"), fname, filename); FREE(filename); return 0; } case FSCANFMAT_READLINES_ERROR: { Scierror(999, _("%s: can not read file %s.\n"), fname, filename); FREE(filename); return 0; } case FSCANFMAT_FORMAT_ERROR: { Scierror(999, _("%s: Invalid format.\n"), fname); FREE(filename); return 0; } case FSCANFMAT_MEMORY_ALLOCATION: { Scierror(999, _("%s: Memory allocation error.\n"), fname); FREE(filename); return 0; } default: case FSCANFMAT_ERROR: { Scierror(999, _("%s: error.\n"), fname); FREE(filename); return 0; } } FREE(filename); freeFscanfMatResult(results); return 0; }
int CreateIntegerVariable(void *pvApiCtx, int iVar, int integerType, matvar_t *matVariable, int * parent, int item_position) { int nbRow, nbCol, i; SciErr sciErr; char * tmp_int8 = NULL; short * tmp_int16 = NULL; int * tmp_int32 = NULL; int *piDims = NULL; unsigned char * tmp_uint8 = NULL; unsigned short * tmp_uint16 = NULL; unsigned int * tmp_uint32 = NULL; #ifdef __SCILAB_INT64__ long long * tmp_int64 = NULL; unsigned long long * tmp_uint64 = NULL; #endif int iSize = 0; // Matrix dimensions nbRow = (int)matVariable->dims[0]; nbCol = (int)matVariable->dims[1]; iSize = nbRow * nbCol; if (iSize == 0) { double dblReal = 0; SciErr sciErr = createMatrixOfDouble(pvApiCtx, iVar, 0, 0, &dblReal); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), "CreateIntegerVariable"); return FALSE; } return TRUE; } if (matVariable->rank == 2) /* 2-D array */ { switch (integerType) { case SCI_INT8: { tmp_int8 = (char *)MALLOC(iSize * sizeof(char)); if (tmp_int8 == NULL) { Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable"); return FALSE; } for (i = 0; i < iSize; i++) { tmp_int8[i] = ((char *)matVariable->data)[i]; } if (parent == NULL) { sciErr = createMatrixOfInteger8(pvApiCtx, iVar, nbRow, nbCol, tmp_int8); } else { sciErr = createMatrixOfInteger8InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int8); } if (sciErr.iErr) { printError(&sciErr, 0); return 0; } FREE(tmp_int8); } break; case SCI_INT16: { tmp_int16 = (short *)MALLOC(iSize * sizeof(short)); if (tmp_int16 == NULL) { Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable"); return FALSE; } for (i = 0; i < iSize; i++) { tmp_int16[i] = ((short *)matVariable->data)[i]; } if (parent == NULL) { sciErr = createMatrixOfInteger16(pvApiCtx, iVar, nbRow, nbCol, tmp_int16); } else { sciErr = createMatrixOfInteger16InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int16); } if (sciErr.iErr) { printError(&sciErr, 0); return 0; } FREE(tmp_int16); } break; case SCI_INT32: { tmp_int32 = (int *)MALLOC(iSize * sizeof(int)); if (tmp_int32 == NULL) { Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable"); return FALSE; } for (i = 0; i < iSize; i++) { tmp_int32[i] = ((int *)matVariable->data)[i]; } if (parent == NULL) { sciErr = createMatrixOfInteger32(pvApiCtx, iVar, nbRow, nbCol, tmp_int32); } else { sciErr = createMatrixOfInteger32InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int32); } if (sciErr.iErr) { printError(&sciErr, 0); return 0; } FREE(tmp_int32); } break; #ifdef __SCILAB_INT64__ case SCI_INT64: { tmp_int64 = (long long *)MALLOC(iSize * sizeof(long long)); if (tmp_int64 == NULL) { Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable"); return FALSE; } for (i = 0; i < iSize; i++) { tmp_int64[i] = ((long long *)matVariable->data)[i]; } if (parent == NULL) { sciErr = createMatrixOfInteger64(pvApiCtx, iVar, nbRow, nbCol, tmp_int64); } else { sciErr = createMatrixOfInteger64InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_int64); } if (sciErr.iErr) { printError(&sciErr, 0); return 0; } FREE(tmp_int64); } break; #endif case SCI_UINT8: { tmp_uint8 = (unsigned char *)MALLOC(iSize * sizeof(unsigned char)); if (tmp_uint8 == NULL) { Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable"); return FALSE; } for (i = 0; i < iSize; i++) { tmp_uint8[i] = ((unsigned char *)matVariable->data)[i]; } if (parent == NULL) { sciErr = createMatrixOfUnsignedInteger8(pvApiCtx, iVar, nbRow, nbCol, tmp_uint8); } else { sciErr = createMatrixOfUnsignedInteger8InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint8); } if (sciErr.iErr) { printError(&sciErr, 0); return 0; } FREE(tmp_uint8); } break; case SCI_UINT16: { tmp_uint16 = (unsigned short *)MALLOC(iSize * sizeof(unsigned short)); if (tmp_uint16 == NULL) { Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable"); return FALSE; } for (i = 0; i < iSize; i++) { tmp_uint16[i] = ((unsigned short *)matVariable->data)[i]; } if (parent == NULL) { sciErr = createMatrixOfUnsignedInteger16(pvApiCtx, iVar, nbRow, nbCol, tmp_uint16); } else { sciErr = createMatrixOfUnsignedInteger16InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint16); } if (sciErr.iErr) { printError(&sciErr, 0); return 0; } FREE(tmp_uint16); } break; case SCI_UINT32: { tmp_uint32 = (unsigned int *)MALLOC(iSize * sizeof(unsigned int)); if (tmp_uint32 == NULL) { Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable"); return FALSE; } for (i = 0; i < iSize; i++) { tmp_uint32[i] = ((unsigned int *)matVariable->data)[i]; } if (parent == NULL) { sciErr = createMatrixOfUnsignedInteger32(pvApiCtx, iVar, nbRow, nbCol, tmp_uint32); } else { sciErr = createMatrixOfUnsignedInteger32InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint32); } if (sciErr.iErr) { printError(&sciErr, 0); return 0; } FREE(tmp_uint32); } break; #ifdef __SCILAB_INT64__ case SCI_UINT64: { tmp_uint64 = (unsigned long long *)MALLOC(iSize * sizeof(unsigned long long)); if (tmp_uint64 == NULL) { Scierror(999, _("%s: No more memory.\n"), "CreateIntegerVariable"); return FALSE; } for (i = 0; i < iSize; i++) { tmp_uint64[i] = ((unsigned long long *)matVariable->data)[i]; } if (parent == NULL) { sciErr = createMatrixOfUnsignedInteger64(pvApiCtx, iVar, nbRow, nbCol, tmp_uint64); } else { sciErr = createMatrixOfUnsignedInteger64InList(pvApiCtx, iVar, parent, item_position, nbRow, nbCol, tmp_uint64); } if (sciErr.iErr) { printError(&sciErr, 0); return 0; } FREE(tmp_uint64); } break; #endif } } else /* Multi-dimension array -> Scilab HyperMatrix */ { piDims = (int*) MALLOC(matVariable->rank * sizeof(int)); if (piDims == NULL) { Scierror(999, _("%s: No more memory.\n"), "CreateBooleanVariable"); return FALSE; } for (i = 0; i < matVariable->rank; i++) { piDims[i] = (int)matVariable->dims[i]; } CreateHyperMatrixVariable(pvApiCtx, iVar, matVariable->class_type, &integerType, &matVariable->rank, piDims, matVariable , parent, item_position); FREE(piDims); } return TRUE; }
/*--------------------------------------------------------------------------*/ int sci_xget(char *fname, unsigned long fname_len) { SciErr sciErr; int* piAddrl1 = NULL; char* l1 = NULL; int* piAddrl2 = NULL; double* l2 = NULL; char* l3 = NULL; int m1 = 0, m2 = 0, n2 = 0, i = 0; int one = 1; BOOL keyFound = FALSE; if (nbInputArgument(pvApiCtx) <= 0) { sci_demo(fname, fname_len); return 0; } CheckInputArgument(pvApiCtx, 1, 2); CheckOutputArgument(pvApiCtx, 0, 1); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrl1); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position 1. if (getAllocatedSingleString(pvApiCtx, piAddrl1, &l1)) { Scierror(202, _("%s: Wrong type for argument #%d: A string expected.\n"), fname, 1); return 1; } /* check if key is valid */ for (i = 0; i < NUMSETFONC ; i++) { if (strcmp((l1), KeyTab_[i]) == 0) { keyFound = TRUE; break; } } if (!keyFound) { Scierror(999, _("%s: Unrecognized input argument: '%s'.\n"), fname, (l1)); freeAllocatedSingleString(l1); return -1; } if (nbInputArgument(pvApiCtx) == 2) { sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrl2); if (sciErr.iErr) { printError(&sciErr, 0); freeAllocatedSingleString(l1); return 1; } // Retrieve a matrix of double at position 2. sciErr = getMatrixOfDouble(pvApiCtx, piAddrl2, &m2, &n2, &l2); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 2); freeAllocatedSingleString(l1); return 1; } //CheckScalar if (m2 != 1 || n2 != 1) { Scierror(999, _("%s: Wrong size for input argument #%d: A real scalar expected.\n"), fname, 2); freeAllocatedSingleString(l1); return 1; } } if (strcmp(l1, "fpf") == 0 || strcmp(l1, "auto clear") == 0) { int bufl; char buf[4096]; /* special case for global variables set */ xgetg((l1), buf, &bufl, m1, bsiz); if (allocSingleString(pvApiCtx, nbInputArgument(pvApiCtx) + 1, bufl * one, (const char **)&l3)) { Scierror(999, _("%s: Memory allocation error.\n"), fname); freeAllocatedSingleString(l1); return 1; } strncpy((l3), buf, bufl); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "colormap") == 0) { int iObjUID = 0; // Force figure creation if none exists. getOrCreateDefaultSubwin(); iObjUID = getCurrentFigure(); get_color_map_property(pvApiCtx, iObjUID); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "mark") == 0) { int iObjUID = getOrCreateDefaultSubwin(); int iMarkStyle = 0; int* piMarkStyle = &iMarkStyle; int iMarkSize = 0; int* piMarkSize = &iMarkSize; double pdblResult[2]; getGraphicObjectProperty(iObjUID, __GO_MARK_STYLE__, jni_int, (void**)&piMarkStyle); getGraphicObjectProperty(iObjUID, __GO_MARK_SIZE__, jni_int, (void**)&piMarkSize); pdblResult[0] = iMarkStyle; pdblResult[1] = iMarkSize; createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 2, pdblResult); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "mark size") == 0) { int iObjUID = getOrCreateDefaultSubwin(); get_mark_size_property(pvApiCtx, iObjUID); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "line style") == 0) { get_line_style_property(pvApiCtx, getOrCreateDefaultSubwin()); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "clipping") == 0) { double *clipBox = NULL; int iObjUID = getOrCreateDefaultSubwin(); getGraphicObjectProperty(iObjUID, __GO_CLIP_BOX__, jni_double_vector, (void **)&clipBox); createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 4, clipBox); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "font") == 0) { int iObjUID = getOrCreateDefaultSubwin(); double dblFontSize = 0; double* pdblFontSize = &dblFontSize; int iFontStyle = 0; int* piFontStyle = &iFontStyle; double pdblResult[2]; getGraphicObjectProperty(iObjUID, __GO_FONT_SIZE__, jni_double, (void **)&pdblFontSize); getGraphicObjectProperty(iObjUID, __GO_FONT_STYLE__, jni_int, (void**)&piFontStyle); pdblResult[0] = iFontStyle; pdblResult[1] = dblFontSize; createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 2, pdblResult); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "font size") == 0) { double dblFontSize = 0; double* pdblFontSize = &dblFontSize; getGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_FONT_SIZE__, jni_double, (void **)&pdblFontSize); createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, dblFontSize); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "dashes") == 0) { int iLineStyle = 0; int* piLineStyle = &iLineStyle; getGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_LINE_STYLE__, jni_int, (void**)&piLineStyle); createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iLineStyle); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "hidden3d") == 0) { get_hidden_color_property(pvApiCtx, getOrCreateDefaultSubwin()); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "window") == 0 || strcmp(l1, "figure") == 0) { int iFigureId = 0; int* piFigureId = &iFigureId; getOrCreateDefaultSubwin(); getGraphicObjectProperty(getCurrentFigure(), __GO_ID__, jni_int, (void**)&piFigureId); createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iFigureId); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "thickness") == 0) { get_thickness_property(pvApiCtx, getOrCreateDefaultSubwin()); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "wdim") == 0 || strcmp(l1, "wpdim") == 0) { int *piFigureSize = NULL; double pdblFigureSize[2]; getOrCreateDefaultSubwin(); getGraphicObjectProperty(getCurrentFigure(), __GO_SIZE__, jni_int_vector, (void **) &piFigureSize); pdblFigureSize[0] = (double) piFigureSize[0]; pdblFigureSize[1] = (double) piFigureSize[1]; createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 2, pdblFigureSize); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "wpos") == 0) { int *piFigurePosition = NULL; double pdblFigurePosition[2]; getOrCreateDefaultSubwin(); getGraphicObjectProperty(getCurrentFigure(), __GO_POSITION__, jni_int_vector, (void **) &piFigurePosition); pdblFigurePosition[0] = piFigurePosition[0]; pdblFigurePosition[1] = piFigurePosition[1]; createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 2, pdblFigurePosition); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "viewport") == 0) { int* viewport = NULL; double pdblViewport[2]; getOrCreateDefaultSubwin(); getGraphicObjectProperty(getCurrentFigure(), __GO_VIEWPORT__, jni_int_vector, (void **)&viewport); pdblViewport[0] = viewport[0]; pdblViewport[1] = viewport[1]; createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1, 2, pdblViewport); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "background") == 0) { get_background_property(pvApiCtx, getOrCreateDefaultSubwin()); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if ( strcmp(l1, "color") == 0 || strcmp(l1, "foreground") == 0 || strcmp(l1, "pattern") == 0) { get_foreground_property(pvApiCtx, getOrCreateDefaultSubwin()); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "lastpattern") == 0) { int iNumColors = 0; int* piNumColors = &iNumColors; getOrCreateDefaultSubwin(); getGraphicObjectProperty(getCurrentFigure(), __GO_COLORMAP_SIZE__, jni_int, (void**)&piNumColors); createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iNumColors); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "line mode") == 0) { int iLineMode = 0; int* lineMode = &iLineMode; getGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_LINE_MODE__, jni_bool, (void **)&lineMode); createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iLineMode); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "white") == 0) { int iNumColors = 0; int* piNumColors = &iNumColors; getOrCreateDefaultSubwin(); getGraphicObjectProperty(getCurrentFigure(), __GO_COLORMAP_SIZE__, jni_int, (void**)&piNumColors); /* White is lqst colormap index + 2 */ createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iNumColors + 2); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "wresize") == 0) { // autoresize property int iAutoResize = 0; int* piAutoResize = &iAutoResize; getOrCreateDefaultSubwin(); getGraphicObjectProperty(getCurrentFigure(), __GO_AUTORESIZE__, jni_bool, (void **)&piAutoResize); createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iAutoResize); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "clipgrf") == 0) { /* clip_state : 0 = off, 1 = on */ int iClipState = 0; int* piClipState = &iClipState; getGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_CLIP_STATE__, jni_int, (void**)&piClipState); createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, iClipState); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else if (strcmp(l1, "clipoff") == 0) { int iClipState = 0; int* piClipState = &iClipState; getGraphicObjectProperty(getOrCreateDefaultSubwin(), __GO_CLIP_STATE__, jni_int, (void**)&piClipState); /* clip_state : 0 = off, 1 = on */ if (iClipState == 0) { createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 1); } else { createScalarDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, 0); } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); } else { Scierror(999, _("%s: Unrecognized input argument: '%s'.\n"), fname, (l1)); freeAllocatedSingleString(l1); return -1; } freeAllocatedSingleString(l1); return 0; }
/*--------------------------------------------------------------------------*/ int sci_x_choice(char *fname, void* pvApiCtx) { SciErr sciErr; int* piAddrdefaultValuesAdr = NULL; int* piAddrlabelsAdr = NULL; int* piAddrlineLabelsAdr = NULL; double* emptyMatrixAdr = NULL; int nbRow = 0, nbCol = 0; int nbRowDefaultValues = 0, nbColDefaultValues = 0; int nbRowLineLabels = 0, nbColLineLabels = 0; int messageBoxID = 0; char **labelsAdr = NULL; char **lineLabelsAdr = NULL; double *defaultValues = NULL; int *defaultValuesInt = NULL; int userValueSize = 0; int *userValue = NULL; double *userValueDouble = NULL; int K = 0; CheckInputArgument(pvApiCtx, 3, 3); CheckOutputArgument(pvApiCtx, 0, 1); /* READ THE DEFAULT VALUES */ if (checkInputArgumentType(pvApiCtx, 1, sci_matrix)) { sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddrdefaultValuesAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position 1. sciErr = getMatrixOfDouble(pvApiCtx, piAddrdefaultValuesAdr, &nbRowDefaultValues, &nbColDefaultValues, &defaultValues); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 1); return 1; } defaultValuesInt = (int *)MALLOC(nbRowDefaultValues * nbColDefaultValues * sizeof(int)); for (K = 0; K < nbRowDefaultValues * nbColDefaultValues; K++) { defaultValuesInt[K] = (int)defaultValues[K]; } } else { Scierror(999, _("%s: Wrong type for input argument #%d: Real or complex vector expected.\n"), fname, 1); return FALSE; } /* READ THE MESSAGE */ if ((checkInputArgumentType(pvApiCtx, 2, sci_strings))) { sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddrlabelsAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of string at position 2. if (getAllocatedMatrixOfString(pvApiCtx, piAddrlabelsAdr, &nbRow, &nbCol, &labelsAdr)) { Scierror(202, _("%s: Wrong type for argument #%d: String matrix expected.\n"), fname, 2); return 1; } } else { Scierror(999, _("%s: Wrong type for input argument #%d: Vector of strings expected.\n"), fname, 2); FREE(defaultValuesInt); return FALSE; } /* Create the Java Object */ messageBoxID = createMessageBox(); /* Title is a default title */ setMessageBoxTitle(messageBoxID, _("Scilab Choices Request")); /* Message */ setMessageBoxMultiLineMessage(messageBoxID, labelsAdr, nbCol * nbRow); freeAllocatedMatrixOfString(nbRow, nbCol, labelsAdr); /* READ THE LABELS */ if (checkInputArgumentType(pvApiCtx, 3, sci_strings)) { sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddrlineLabelsAdr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of string at position 3. if (getAllocatedMatrixOfString(pvApiCtx, piAddrlineLabelsAdr, &nbRowLineLabels, &nbColLineLabels, &lineLabelsAdr)) { Scierror(202, _("%s: Wrong type for argument #%d: String matrix expected.\n"), fname, 3); return 1; } if (nbRow != 1 && nbCol != 1) { freeAllocatedMatrixOfString(nbRowLineLabels, nbColLineLabels, lineLabelsAdr); Scierror(999, _("%s: Wrong size for input argument #%d: Vector of strings expected.\n"), fname, 3); return FALSE; } setMessageBoxLineLabels(messageBoxID, lineLabelsAdr, nbColLineLabels * nbRowLineLabels); freeAllocatedMatrixOfString(nbRowLineLabels, nbColLineLabels, lineLabelsAdr); } else { Scierror(999, _("%s: Wrong type for input argument #%d: Vector of strings expected.\n"), fname, 3); return FALSE; } /* Default selected buttons */ setMessageBoxDefaultSelectedButtons(messageBoxID, defaultValuesInt, nbRowDefaultValues * nbColDefaultValues); /* Display it and wait for a user input */ messageBoxDisplayAndWait(messageBoxID); /* Read the user answer */ userValueSize = getMessageBoxValueSize(messageBoxID); if (userValueSize == 0) { nbRow = 0; nbCol = 0; sciErr = allocMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, nbRow, nbCol, &emptyMatrixAdr); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } } else { userValue = (int*)getMessageBoxUserSelectedButtons(messageBoxID); userValueDouble = (double *)MALLOC(nbRowDefaultValues * nbColDefaultValues * sizeof(double)); for (K = 0; K < nbRowDefaultValues * nbColDefaultValues; K++) { userValueDouble[K] = userValue[K]; } sciErr = createMatrixOfDouble(pvApiCtx, nbInputArgument(pvApiCtx) + 1, nbRowDefaultValues, nbColDefaultValues, userValueDouble); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } /* TO DO : do a delete [] getMessageBoxUserSelectedButtons */ } FREE(defaultValuesInt); AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; ReturnArguments(pvApiCtx); return TRUE; }
// ============================================================================= int sci_csvTextScan(char *fname, unsigned long fname_len) { SciErr sciErr; int iErr = 0; int i = 0; int *piAddressVarOne = NULL; int m1 = 0, n1 = 0; int iType1 = 0; char **text = NULL; int *lengthText = NULL; int nbLines = 0; char *separator = NULL; char *decimal = NULL; char *conversion = NULL; double * dRealValues = NULL; int *iRange = NULL; int haveRange = 0; csvResult *result = NULL; CheckRhs(1, 5); CheckLhs(1, 1); if (Rhs == 5) { int m5 = 0, n5 = 0; iRange = csv_getArgumentAsMatrixofIntFromDouble(pvApiCtx, 5, fname, &m5, &n5, &iErr); if (iErr) { return 0; } if ((m5 * n5 != SIZE_RANGE_SUPPORTED) ) { if (iRange) { FREE(iRange); iRange = NULL; } Scierror(999, _("%s: Wrong size for input argument #%d: Four entries expected.\n"), fname, 5); return 0; } if ((m5 != 1) && (n5 != 1)) { if (iRange) { FREE(iRange); iRange = NULL; } Scierror(999, _("%s: Wrong size for input argument #%d: A column or row vector expected.\n"), fname, 5); return 0; } if (isValidRange(iRange, m5 * n5)) { haveRange = 1; } else { if (iRange) { FREE(iRange); iRange = NULL; } Scierror(999, _("%s: Wrong value for input argument #%d: Inconsistent range.\n"), fname, 5); return 0; } } if (Rhs >= 4) { conversion = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 4, fname, getCsvDefaultConversion(), &iErr); if (iErr) { if (iRange) { FREE(iRange); iRange = NULL; } return 0; } if (!((strcmp(conversion, CONVTOSTR) == 0) || (strcmp(conversion, CONVTODOUBLE) == 0))) { if (iRange) { FREE(iRange); iRange = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' string expected.\n"), fname, 4, "double", "string"); return 0; } } else { conversion = strdup(getCsvDefaultConversion()); } if (Rhs >= 3) { decimal = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 3, fname, getCsvDefaultDecimal(), &iErr); if (iErr) { if (iRange) { FREE(iRange); iRange = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } return 0; } if (decimal[0] != '.' && decimal[0] != ',') { if (iRange) { FREE(iRange); iRange = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' string expected.\n"), fname, 3, ",", "."); return 0; } } else { decimal = strdup(getCsvDefaultDecimal()); } if (Rhs >= 2) { separator = csv_getArgumentAsStringWithEmptyManagement(pvApiCtx, 2, fname, getCsvDefaultSeparator(), &iErr); if (iErr) { if (iRange) { FREE(iRange); iRange = NULL; } if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } return 0; } } else { separator = strdup(getCsvDefaultSeparator()); } if (!csv_isRowVector(pvApiCtx, 1) && !csv_isColumnVector(pvApiCtx, 1) && !csv_isScalar(pvApiCtx, 1)) { if (iRange) { FREE(iRange); iRange = NULL; } if (separator) { FREE(separator); separator = NULL; } if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } Scierror(999, _("%s: Wrong size for input argument #%d: Vector string expected.\n"), fname, 1); return 0; } text = csv_getArgumentAsMatrixOfString(pvApiCtx, 1, fname, &m1, &n1, &iErr); if (iErr) { if (iRange) { FREE(iRange); iRange = NULL; } if (separator) { FREE(separator); separator = NULL; } if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } return 0; } nbLines = m1 * n1; result = csvTextScan((const char**)text, nbLines, separator, decimal); if (text) { if (separator) { FREE(separator); separator = NULL; } freeArrayOfString(text, nbLines); text = NULL; } if (separator) { FREE(separator); separator = NULL; } if (result) { switch (result->err) { case CSV_READ_SEPARATOR_DECIMAL_EQUAL: { Scierror(999, _("%s: separator and decimal must have different values.\n"), fname); } break; case CSV_READ_NO_ERROR: { if (strcmp(conversion, CONVTOSTR) == 0) { if (haveRange) { int newM = 0; int newN = 0; char **pStrRange = getRangeAsString((const char**)result->pstrValues, result->m, result->n, iRange, &newM, &newN); if (pStrRange) { sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, newM, newN, pStrRange); freeArrayOfString(pStrRange, newM * newN); } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } } else { sciErr = createMatrixOfString(pvApiCtx, Rhs + 1, result->m, result->n, result->pstrValues); } } else /* to double */ { stringToComplexError ierr = STRINGTOCOMPLEX_ERROR; csv_complexArray *ptrCsvComplexArray = stringsToCsvComplexArray((const char**)result->pstrValues, result->m * result->n, decimal, TRUE, &ierr); if (ptrCsvComplexArray == NULL) { freeCsvResult(result); if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } if (iRange) { FREE(iRange); iRange = NULL; } if (ierr == STRINGTOCOMPLEX_ERROR) { Scierror(999, _("%s: can not convert data.\n"), fname); } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } return 0; } switch (ierr) { case STRINGTOCOMPLEX_NOT_A_NUMBER: case STRINGTOCOMPLEX_NO_ERROR: { if (haveRange) { int newM = 0; int newN = 0; csv_complexArray *csvComplexRange = getRangeAsCsvComplexArray(ptrCsvComplexArray, result->m, result->n, iRange, &newM, &newN); if (csvComplexRange) { if (csvComplexRange->isComplex) { sciErr = createComplexMatrixOfDouble(pvApiCtx, Rhs + 1, newM, newN, ptrCsvComplexArray->realPart, ptrCsvComplexArray->imagPart); } else { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, newM, newN, csvComplexRange->realPart); } freeCsvComplexArray(csvComplexRange); csvComplexRange = NULL; } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } } else { if (ptrCsvComplexArray->isComplex) { sciErr = createComplexMatrixOfDouble(pvApiCtx, Rhs + 1, result->m, result->n, ptrCsvComplexArray->realPart, ptrCsvComplexArray->imagPart); } else { sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, result->m, result->n, ptrCsvComplexArray->realPart); } } freeCsvComplexArray(ptrCsvComplexArray); ptrCsvComplexArray = NULL; } break; case STRINGTOCOMPLEX_MEMORY_ALLOCATION: { Scierror(999, _("%s: Memory allocation error.\n"), fname); } default: case STRINGTOCOMPLEX_ERROR: { Scierror(999, _("%s: can not convert data.\n"), fname); } } } if (sciErr.iErr) { freeCsvResult(result); if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } if (iRange) { FREE(iRange); iRange = NULL; } printError(&sciErr, 0); Scierror(17, _("%s: Memory allocation error.\n"), fname); return 0; } else { LhsVar(1) = Rhs + 1; PutLhsVar(); } } break; case CSV_READ_MEMORY_ALLOCATION: { Scierror(999, _("%s: Memory allocation error.\n"), fname); } break; case CSV_READ_COLUMNS_ERROR: { Scierror(999, _("%s: can not read text: Error in the column structure\n"), fname); } break; case CSV_READ_READLINES_ERROR: case CSV_READ_ERROR: { Scierror(999, _("%s: can not read text.\n"), fname); } break; } } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); } freeCsvResult(result); if (decimal) { FREE(decimal); decimal = NULL; } if (conversion) { FREE(conversion); conversion = NULL; } if (iRange) { FREE(iRange); iRange = NULL; } return 0; }
/*--------------------------------------------------------------------------*/ static int isasciiMatrix(char *fname, int *piAddressVarOne) { SciErr sciErr; int m1 = 0, n1 = 0; double *pdVarOne = NULL; sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } if (m1 * n1 > 0) { BOOL *bOutputMatrix = NULL; sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarOne, &m1, &n1, &pdVarOne); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1); return 0; } bOutputMatrix = (BOOL*)MALLOC(sizeof(BOOL) * (m1 * n1)); if (bOutputMatrix) { int nbElems = m1 * n1; int i = 0; for (i = 0; i < nbElems; i++) { int iVal = (int)pdVarOne[i]; if (isascii(iVal)) { bOutputMatrix[i] = (int)TRUE; } else { bOutputMatrix[i] = (int)FALSE; } } sciErr = createMatrixOfBoolean(pvApiCtx, Rhs + 1, m1, n1, bOutputMatrix); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } if (bOutputMatrix) { FREE(bOutputMatrix); bOutputMatrix = NULL; } LhsVar(1) = Rhs + 1; PutLhsVar(); } else { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { /* returns [] */ m1 = 0; n1 = 0; sciErr = createMatrixOfDouble(pvApiCtx, Rhs + 1, m1, n1, NULL); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } LhsVar(1) = Rhs + 1; PutLhsVar(); } return 0; }
/*--------------------------------------------------------------------------*/ static int returnMoveFileResultOnStack(int ierr, char *fname) { double dError = 0.; wchar_t **sciError = NULL; int m_out = 1, n_out = 1; sciError = (wchar_t **) MALLOC(sizeof(wchar_t *) * 1); if (sciError == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } #ifdef _MSC_VER if (ierr) { #define BUFFER_SIZE 1024 DWORD dw = GetLastError(); wchar_t buffer[BUFFER_SIZE]; if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buffer, BUFFER_SIZE, NULL) == 0) { wcscpy(buffer, L"Unknown Error"); } // for compatibilty with copyfile, we return 0 (error) //dError = (double) dw; dError = (double)0; sciError[0] = (wchar_t *) MALLOC(sizeof(wchar_t) * ((int)wcslen(buffer) + 1)); if (sciError[0] == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } wcscpy(sciError[0], buffer); } else { dError = 1.; sciError[0] = (wchar_t *) MALLOC(sizeof(wchar_t) * 1); if (sciError[0] == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } wcscpy(sciError[0], L""); } #else if (ierr) { // for compatibilty with copyfile, we return 0 (error) //dError = (double) ierr; dError = (double)0.; sciError[0] = to_wide_string(strerror(errno)); if (sciError[0] == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); return 0; } } else { dError = 1.; sciError[0] = (wchar_t *) MALLOC(sizeof(wchar_t) * 1); if (sciError[0] == NULL) { Scierror(999, _("%s: Memory allocation error.\n"), fname); freeArrayOfWideString(sciError, 1); return 0; } wcscpy(sciError[0], L""); } #endif createMatrixOfDouble(pvApiCtx, Rhs + 1, m_out, n_out, &dError); LhsVar(1) = Rhs + 1; if (Lhs == 2) { createMatrixOfWideString(pvApiCtx, Rhs + 2, m_out, n_out, sciError); LhsVar(2) = Rhs + 2; } freeArrayOfWideString(sciError, 1); PutLhsVar(); return 0; }
// This function is a wrapper which allows to call a Scilab script function like a "normal" C/C++/Fortran function void C2F(dense_glcpd_functions)(int * n, double * x, double * f, double * ws, int * lws, char * cws) { int n_x = 0; int * tmp_addr = NULL; int sci_obj, lhs_obj, rhs_obj; int stack_pos, i; int nbvars_old = Nbvars; double * tmp_var = NULL, tmp_val = 0.0; double f_out = 0.0; SciErr _SciErr; #ifdef DEBUG printf("DEBUG: Functions called\n"); #endif if (param_fobj.fobj_type==0) { sci_obj = param_fobj.sci_obj; lhs_obj = param_fobj.lhs_obj; rhs_obj = param_fobj.rhs_obj; stack_pos = param_fobj.stack_pos; n_x = param_fobj.n_x; Nbvars = stack_pos + MAX(rhs_obj,lhs_obj) + 1; #ifdef DEBUG printf("DEBUG: n = %d, m = %d, n_x = %d\n", *n, *m, n_x); for(i=0; i<n_x; i++) printf("DEBUG: x[%d] = %f\n", i, x[i]); #endif _SciErr = createMatrixOfDouble(pvApiCtx, stack_pos+0, n_x, 1, x); GLCPD_ERROR_NORETURN; // The scilab function return 1 output argument: f _SciErr = createMatrixOfDouble(pvApiCtx, stack_pos+1, 0, 0, &tmp_val); GLCPD_ERROR_NORETURN; if (!C2F(scifunction)(&stack_pos, &sci_obj, &lhs_obj, &rhs_obj)) { Scierror(999,"dense glcpd: error when calling objective function\n"); Nbvars = nbvars_old; return; } if (Err>0) { Scierror(999,"dense glcpd: error when calling objective function\n"); Nbvars = nbvars_old; return; } // Get F _SciErr = getVarAddressFromPosition(pvApiCtx, stack_pos+0, &tmp_addr); GLCPD_ERROR_NORETURN; getScalarDouble(pvApiCtx, tmp_addr, &f_out); *f = f_out; #ifdef DEBUG printf("DEBUG: f = %f\n", *f); #endif Nbvars = nbvars_old; } else { (*param_fobj.function)(n, x, f, NULL, NULL, NULL); return; } #ifdef DEBUG printf("DEBUG: End of Functions\n"); #endif }
/*--------------------------------------------------------------------------*/ int sci_contour2di(char * fname, void* pvApiCtx) { SciErr sciErr; int flagx = 0, nz = 10; /* default number of level curves : 10 */ int m1 = 0, n1 = 0, m2 = 0, n2 = 0, m3 = 0, n3 = 0, m4 = 0, n4 = 0; double* hl1 = NULL; double* hl2 = NULL; double* znz = NULL; int ix4, i = 0, un = 1; int* piAddr1 = NULL; int* piAddr2 = NULL; int* piAddr3 = NULL; int* piAddr4 = NULL; double* l1 = NULL; double* l2 = NULL; double* l3 = NULL; double* l4 = NULL; int* l5 = NULL; CheckInputArgument(pvApiCtx, 4, 4); CheckOutputArgument(pvApiCtx, 2, 2); //get variable address sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr1); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position 1. sciErr = getMatrixOfDouble(pvApiCtx, piAddr1, &m1, &n1, &l1); if (sciErr.iErr) { Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 1); printError(&sciErr, 0); return 1; } //CheckVector if (m1 != 1 && n1 != 1) { Scierror(999, _("%s: Wrong size for input argument #%d: Vector expected.\n"), fname, 1); return 1; } //get variable address sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddr2); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position 2. sciErr = getMatrixOfDouble(pvApiCtx, piAddr2, &m2, &n2, &l2); if (sciErr.iErr) { Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 2); printError(&sciErr, 0); return 1; } //CheckVector if (m2 != 1 && n2 != 1) { Scierror(999, _("%s: Wrong size for input argument #%d: Vector expected.\n"), fname, 2); return 1; } //get variable address sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddr3); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position 3. sciErr = getMatrixOfDouble(pvApiCtx, piAddr3, &m3, &n3, &l3); if (sciErr.iErr) { Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 3); printError(&sciErr, 0); return 1; } if (m3 * n3 == 0) { AssignOutputVariable(pvApiCtx, 1) = 0; ReturnArguments(pvApiCtx); return 0; } if (m3 == 1 || n3 == 1) { Scierror(999, _("%s: Wrong type for input argument #%d: Matrix expected.\n"), fname, 3); return 1; } //CheckDimProp if (m1 * n1 != m3) { Scierror(999, _("%s: Wrong size for input arguments: Incompatible sizes.\n"), fname); return 1; } //CheckDimProp if (m2 * n2 != n3) { Scierror(999, _("%s: Wrong size for input arguments: Incompatible sizes.\n"), fname); return 1; } /* number of level curves */ //get variable address sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddr4); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } // Retrieve a matrix of double at position 4. sciErr = getMatrixOfDouble(pvApiCtx, piAddr4, &m4, &n4, &l4); if (sciErr.iErr) { Scierror(202, _("%s: Wrong type for argument #%d: A real expected.\n"), fname, 4); printError(&sciErr, 0); return 1; } if (m4 * n4 == 1) { flagx = 0; nz = Max(1, (int) * (l4)); znz = (l4); } else { flagx = 1; nz = m4 * n4; znz = (l4); } ix4 = Max(nz, 2); sciErr = allocMatrixOfDoubleAsInteger(pvApiCtx, 5, un, ix4, &l5); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } for (i = 0 ; i < ix4 ; ++i) { l5[i] = i + 1; } if (nz == 1) { l5[1] = 1; } if (C2F(contourif)(l1, l2, l3, &m3, &n3, &flagx, &nz, znz, l5) != 0) { /* Something wrong happened */ return -1; } C2F(getconts)(&hl1, &hl2, &m1, &n1); if (n1 == 0) { sciErr = allocMatrixOfDouble(pvApiCtx, 6, n1, n1, &hl1); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } sciErr = allocMatrixOfDouble(pvApiCtx, 7, n1, n1, &hl2); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } } else { sciErr = createMatrixOfDouble(pvApiCtx, 6, m1, n1, hl1); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } sciErr = createMatrixOfDouble(pvApiCtx, 7, m1, n1, hl2); if (sciErr.iErr) { printError(&sciErr, 0); Scierror(999, _("%s: Memory allocation error.\n"), fname); return 1; } } AssignOutputVariable(pvApiCtx, 1) = 6; AssignOutputVariable(pvApiCtx, 2) = 7; ReturnArguments(pvApiCtx); return 0; }
// This function is a wrapper which allows to call a Scilab script function like a "normal" C/C++/Fortran function void C2F(dense_glcpd_gradients)(int * n, double * x, double * g, double * ws, int * lws, char * cws) { int n_x = 0; int n_tmp, m_tmp, * tmp_addr = NULL; int sci_obj, lhs_obj, rhs_obj; int stack_pos, i = 0, j = 0, k = 0; int nbvars_old = Nbvars; double * tmp_var = NULL; SciErr _SciErr; #ifdef DEBUG printf("DEBUG: Gradients called\n"); printf("DEBUG: n = %d, m = %d\n", *n, *m); #endif if (param_grad.fobj_type==0) { sci_obj = param_grad.sci_obj; lhs_obj = param_grad.lhs_obj; rhs_obj = param_grad.rhs_obj; stack_pos = param_grad.stack_pos; n_x = param_grad.n_x; Nbvars = stack_pos + MAX(rhs_obj,lhs_obj) + 1; #ifdef DEBUG for(i=0; i<n_x; i++) printf("DEBUG: x[%d] = %f\n", i, x[i]); #endif _SciErr = createMatrixOfDouble(pvApiCtx, stack_pos+0, n_x, 1, x); GLCPD_ERROR_NORETURN; if (!C2F(scifunction)(&stack_pos, &sci_obj, &lhs_obj, &rhs_obj)) { Scierror(999,"dense glcpd: error when calling gradient function\n"); Nbvars = nbvars_old; return; } if (Err>0) { Scierror(999,"dense glcpd: error when calling gradient function\n"); Nbvars = nbvars_old; return; } _SciErr = getVarAddressFromPosition(pvApiCtx, stack_pos+0, &tmp_addr); GLCPD_ERROR_NORETURN; _SciErr = getMatrixOfDouble(pvApiCtx, tmp_addr, &m_tmp, &n_tmp, &tmp_var); GLCPD_ERROR_NORETURN; if (m_tmp*n_tmp != n_x) { Scierror(999,"dense glcpd: gradients - a must be of size %d\n", n_x); Scierror(999," current size: %d\n", n_tmp*m_tmp); Nbvars = nbvars_old; return; } #ifdef DEBUG printf("DEBUG: gradient matrix is full\n"); #endif for(i=0;i<n_x;i++) { g[i] = tmp_var[i]; } Nbvars = nbvars_old; } else { (*param_grad.gradient)(n, x, g, NULL, NULL, NULL); } #ifdef DEBUG printf("DEBUG: End of Gradients\n"); #endif }