int read_poly(char *fname, void* pvApiCtx) { SciErr sciErr; int i, j; //variable info int iRows = 0; int iCols = 0; int iVarLen = 0; int* piAddr = NULL; int* piNbCoef = NULL; double** pdblReal = NULL; double** pdblImg = NULL; char* pstVarname = NULL; //check input and output arguments CheckInputArgument(pvApiCtx, 1, 1); CheckOutputArgument(pvApiCtx, 1, 1); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if (isVarComplex(pvApiCtx, piAddr) == FALSE) { //Error return 0; } //get variable name length sciErr = getPolyVariableName(pvApiCtx, piAddr, NULL, &iVarLen); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } //alloc buff to receive variable name pstVarname = (char*)MALLOC(sizeof(char) * (iVarLen + 1));//1 for null termination //get variable name sciErr = getPolyVariableName(pvApiCtx, piAddr, pstVarname, &iVarLen); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } //First call: retrieve dimmension sciErr = getComplexMatrixOfPoly(pvApiCtx, piAddr, &iRows, &iCols, NULL, NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } //alloc array of coefficient piNbCoef = (int*)MALLOC(sizeof(int) * iRows * iCols); //Second call: retrieve coefficient sciErr = getComplexMatrixOfPoly(pvApiCtx, piAddr, &iRows, &iCols, piNbCoef, NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } //alloc arrays of data pdblReal = (double**)MALLOC(sizeof(double*) * iRows * iCols); pdblImg = (double**)MALLOC(sizeof(double*) * iRows * iCols); for (i = 0 ; i < iRows * iCols ; i++) { pdblReal[i] = (double*)MALLOC(sizeof(double) * piNbCoef[i]); pdblImg[i] = (double*)MALLOC(sizeof(double) * piNbCoef[i]); } //Third call: retrieve data sciErr = getComplexMatrixOfPoly(pvApiCtx, piAddr, &iRows, &iCols, piNbCoef, pdblReal, pdblImg); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } //Do something with Data //Invert polynomials in the matrix and invert coefficients for (i = 0 ; i < (iRows * iCols) / 2 ; i++) { int iPos1 = iRows * iCols - 1 - i; double* pdblSave = NULL; int iNbCoefSave = 0; //switch array of coefficient pdblSave = pdblReal[i]; pdblReal[i] = pdblReal[iPos1]; pdblReal[iPos1] = pdblSave; pdblSave = pdblImg[i]; pdblImg[i] = pdblImg[iPos1]; pdblImg[iPos1] = pdblSave; //switch number of coefficient iNbCoefSave = piNbCoef[i]; piNbCoef[i] = piNbCoef[iPos1]; piNbCoef[iPos1] = iNbCoefSave; } //switch coefficient for (i = 0 ; i < iRows * iCols ; i++) { for (j = 0 ; j < piNbCoef[i] / 2 ; j++) { int iPos2 = piNbCoef[i] - 1 - j; double dblVal = pdblReal[i][j]; pdblReal[i][j] = pdblReal[i][iPos2]; pdblReal[i][iPos2] = dblVal; dblVal = pdblImg[i][j]; pdblImg[i][j] = pdblImg[i][iPos2]; pdblImg[i][iPos2] = dblVal; } } sciErr = createComplexMatrixOfPoly(pvApiCtx, nbInputArgument(pvApiCtx) + 1, pstVarname, iRows, iCols, piNbCoef, pdblReal, pdblImg); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } //free OS memory FREE(pstVarname); FREE(piNbCoef); for (i = 0 ; i < iRows * iCols ; i++) { FREE(pdblReal[i]); FREE(pdblImg[i]); } FREE(pdblReal); FREE(pdblImg); //assign allocated variables to Lhs position AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; return 0; }
static bool export_poly(int _iH5File, int *_piVar, char* _pstName) { int iRet = 0; int* piNbCoef = NULL; double** pdblReal = NULL; double** pdblImg = NULL; char pstVarName[64] = {0}; int iVarNameLen = 0; int piDims[2]; SciErr sciErr = getPolyVariableName(pvApiCtx, _piVar, pstVarName, &iVarNameLen); if (sciErr.iErr) { printError(&sciErr, 0); return false; } if (isVarComplex(pvApiCtx, _piVar)) { sciErr = getComplexMatrixOfPoly(pvApiCtx, _piVar, &piDims[0], &piDims[1], NULL, NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return false; } piNbCoef = (int*)MALLOC(piDims[0] * piDims[1] * sizeof(int)); sciErr = getComplexMatrixOfPoly(pvApiCtx, _piVar, &piDims[0], &piDims[1], piNbCoef, NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return false; } pdblReal = (double**)MALLOC(sizeof(double*) * piDims[0] * piDims[1]); pdblImg = (double**)MALLOC(sizeof(double*) * piDims[0] * piDims[1]); for (int i = 0 ; i < piDims[0] * piDims[1] ; i++) { pdblReal[i] = (double*)MALLOC(sizeof(double) * piNbCoef[i]);// for null termination pdblImg[i] = (double*)MALLOC(sizeof(double) * piNbCoef[i]);// for null termination } sciErr = getComplexMatrixOfPoly(pvApiCtx, _piVar, &piDims[0], &piDims[1], piNbCoef, pdblReal, pdblImg); if (sciErr.iErr) { printError(&sciErr, 0); return false; } iRet = writePolyComplexMatrix(_iH5File, _pstName, pstVarName, 2, piDims, piNbCoef, pdblReal, pdblImg); } else { sciErr = getMatrixOfPoly(pvApiCtx, _piVar, &piDims[0], &piDims[1], NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return false; } piNbCoef = (int*)MALLOC(piDims[0] * piDims[1] * sizeof(int)); sciErr = getMatrixOfPoly(pvApiCtx, _piVar, &piDims[0], &piDims[1], piNbCoef, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return false; } pdblReal = (double**)MALLOC(sizeof(double*) * piDims[0] * piDims[1]); for (int i = 0 ; i < piDims[0] * piDims[1] ; i++) { pdblReal[i] = (double*)MALLOC(sizeof(double) * piNbCoef[i]);// for null termination } sciErr = getMatrixOfPoly(pvApiCtx, _piVar, &piDims[0], &piDims[1], piNbCoef, pdblReal); if (sciErr.iErr) { printError(&sciErr, 0); return false; } iRet = writePolyMatrix(_iH5File, _pstName, pstVarName, 2, piDims, piNbCoef, pdblReal); } if (iRet) { return false; } char pstMsg[512]; sprintf(pstMsg, "poly (%d x %d)", piDims[0], piDims[1]); print_type(pstMsg); if (pdblReal) { for (int i = 0 ; i < piDims[0] * piDims[1] ; i++) { FREE(pdblReal[i]); } FREE(pdblReal); } if (pdblImg) { for (int i = 0 ; i < piDims[0] * piDims[1] ; i++) { FREE(pdblImg[i]); } FREE(pdblImg); } FREE(piNbCoef); return true; }
int polyExample(char *fname, void* pvApiCtx) { SciErr sciErr; int* piAddr = NULL; int iType = 0; int iRet = 0; CheckInputArgument(pvApiCtx, 1, 1); CheckOutputArgument(pvApiCtx, 0, 1); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if (isPolyType(pvApiCtx, piAddr)) { char pstVarName[64]; int iLen = 0; sciErr = getPolyVariableName(pvApiCtx, piAddr, pstVarName, &iLen); if (sciErr.iErr) { printError(&sciErr, 0); return sciErr.iErr; } if (isScalar(pvApiCtx, piAddr)) { int iNbCoef = 0; double* pdblReal = NULL; double* pdblImg = NULL; if (isVarComplex(pvApiCtx, piAddr)) { iRet = getAllocatedSingleComplexPoly(pvApiCtx, piAddr, &iNbCoef, &pdblReal, &pdblImg); if (iRet) { freeAllocatedSingleComplexPoly(pdblReal, pdblImg); return iRet; } sciErr = createComplexMatrixOfPoly(pvApiCtx, nbInputArgument(pvApiCtx) + 1, pstVarName, 1, 1, &iNbCoef, &pdblReal, &pdblImg); if (sciErr.iErr) { freeAllocatedSingleComplexPoly(pdblReal, pdblImg); printError(&sciErr, 0); return sciErr.iErr; } freeAllocatedSingleComplexPoly(pdblReal, pdblImg); } else { iRet = getAllocatedSinglePoly(pvApiCtx, piAddr, &iNbCoef, &pdblReal); if (iRet) { freeAllocatedSinglePoly(pdblReal); return iRet; } sciErr = createMatrixOfPoly(pvApiCtx, nbInputArgument(pvApiCtx) + 1, pstVarName, 1, 1, &iNbCoef, &pdblReal); if (sciErr.iErr) { freeAllocatedSinglePoly(pdblReal); printError(&sciErr, 0); return sciErr.iErr; } freeAllocatedSinglePoly(pdblReal); } } else { int iRows = 0; int iCols = 0; int* piNbCoef = NULL; double** pdblReal = NULL; double** pdblImg = NULL; if (isVarComplex(pvApiCtx, piAddr)) { iRet = getAllocatedMatrixOfComplexPoly(pvApiCtx, piAddr, &iRows, &iCols, &piNbCoef, &pdblReal, &pdblImg); if (iRet) { freeAllocatedMatrixOfComplexPoly(iRows, iCols, piNbCoef, pdblReal, pdblImg); return iRet; } sciErr = createComplexMatrixOfPoly(pvApiCtx, nbInputArgument(pvApiCtx) + 1, pstVarName, iRows, iCols, piNbCoef, pdblReal, pdblImg); if (sciErr.iErr) { freeAllocatedMatrixOfComplexPoly(iRows, iCols, piNbCoef, pdblReal, pdblImg); printError(&sciErr, 0); return sciErr.iErr; } freeAllocatedMatrixOfComplexPoly(iRows, iCols, piNbCoef, pdblReal, pdblImg); } else { iRet = getAllocatedMatrixOfPoly(pvApiCtx, piAddr, &iRows, &iCols, &piNbCoef, &pdblReal); if (iRet) { freeAllocatedMatrixOfPoly(iRows, iCols, piNbCoef, pdblReal); return iRet; } sciErr = createMatrixOfPoly(pvApiCtx, nbInputArgument(pvApiCtx) + 1, pstVarName, iRows, iCols, piNbCoef, pdblReal); if (sciErr.iErr) { freeAllocatedMatrixOfPoly(iRows, iCols, piNbCoef, pdblReal); printError(&sciErr, 0); return sciErr.iErr; } freeAllocatedMatrixOfPoly(iRows, iCols, piNbCoef, pdblReal); } } AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1; } else { AssignOutputVariable(pvApiCtx, 1) = 0; } return 0; }
int get_poly_info(void* _pvCtx, int _iRhs, int* _piParent, int *_piAddr, int _iItemPos) { SciErr sciErr; int i; int iLen = 0; int iRows = 0; int iCols = 0; char pstVar[16]; int* piCoeff = NULL; double** pdblReal = NULL; double** pdblImg = NULL; sciErr = getPolyVariableName(_pvCtx, _piAddr, pstVar, &iLen); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } if (_iItemPos == 0) { //not in list sciErr = getMatrixOfPoly(_pvCtx, _piAddr, &iRows, &iCols, NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } piCoeff = (int*)MALLOC(sizeof(int) * iRows * iCols); sciErr = getMatrixOfPoly(_pvCtx, _piAddr, &iRows, &iCols, piCoeff, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } pdblReal = (double**)MALLOC(sizeof(double*) * iRows * iCols); pdblImg = (double**)MALLOC(sizeof(double*) * iRows * iCols); for (i = 0 ; i < iRows * iCols ; i++) { pdblReal[i] = (double*)MALLOC(sizeof(double) * piCoeff[i]); pdblImg[i] = (double*)MALLOC(sizeof(double) * piCoeff[i]); } if (isVarComplex(_pvCtx, _piAddr)) { sciErr = getComplexMatrixOfPoly(_pvCtx, _piAddr, &iRows, &iCols, piCoeff, pdblReal, pdblImg); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } } else { sciErr = getMatrixOfPoly(_pvCtx, _piAddr, &iRows, &iCols, piCoeff, pdblReal); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } } } else { sciErr = getMatrixOfPolyInList(_pvCtx, _piParent, _iItemPos, &iRows, &iCols, NULL, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } piCoeff = (int*)MALLOC(sizeof(int) * iRows * iCols); sciErr = getMatrixOfPolyInList(_pvCtx, _piParent, _iItemPos, &iRows, &iCols, piCoeff, NULL); if (sciErr.iErr) { printError(&sciErr, 0); return 0; } pdblReal = (double**)MALLOC(sizeof(double*) * iRows * iCols); pdblImg = (double**)MALLOC(sizeof(double*) * iRows * iCols); for (i = 0 ; i < iRows * iCols ; i++) { pdblReal[i] = (double*)MALLOC(sizeof(double) * piCoeff[i]); pdblImg[i] = (double*)MALLOC(sizeof(double) * piCoeff[i]); } if (isVarComplex(_pvCtx, _piAddr)) { sciErr = getComplexMatrixOfPolyInList(_pvCtx, _piParent, _iItemPos, &iRows, &iCols, piCoeff, pdblReal, pdblImg); } else { sciErr = getMatrixOfPolyInList(_pvCtx, _piParent, _iItemPos, &iRows, &iCols, piCoeff, pdblReal); } } if (sciErr.iErr) { printError(&sciErr, 0); return 0; } insert_indent(); sciprint("Poly (%d x %d), varname : \'%s\'\n", iRows, iCols, pstVar); for (i = 0 ; i < iRows * iCols ; i++) { FREE(pdblReal[i]); FREE(pdblImg[i]); } FREE(pdblReal); FREE(pdblImg); FREE(piCoeff); return 0;; }