Example #1
0
int putDoubleComplex(char* variableName, double *variable, int nbRow, int nbCol, double * imag, int nbRowI, int nbColI)
{
    SciErr sciErr;

    sciErr = createNamedComplexMatrixOfDouble(NULL, variableName, nbRow, nbCol, variable, imag);
    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return -1;
    }
    return 0;
}
/*------------------------------------------------------------*/
int main(void)
{
    void* pvApiCtx = NULL;
#ifdef _MSC_VER
    if ( StartScilab(NULL, NULL, 0) == FALSE )
#else
    if ( StartScilab(getenv("SCI"), NULL, 0) == FALSE )
#endif
    {
        fprintf(stderr, "Error while calling StartScilab\n");
        return -1;
    }

    /******************************** WRITE ****************************/
    /*
     * Write a line complex matrix into Scilab
     * A=[ 1+%i 3 3+7*%i 2-2*%i ];
     */
    {
        /* Declare the complex matrix */
        double A[] = {1, 3, 3, 2};
        double A_img[] = {1, 0, 7, -2};

        int rowA = 1, colA = 4; /* Size of the complex matrix
							* (note that colA = sizeof(A)/2
							*/
        char variableName[] = "A";
        SciErr sciErr;

        /* Write it into Scilab's memory */
        sciErr = createNamedComplexMatrixOfDouble(pvApiCtx, variableName, rowA, colA, A, A_img);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
        }
        printf("Display from Scilab of A:\n");
        SendScilabJob("disp(A);"); /* Display A */
    }

    /*
    * Write a matrix into Scilab
    * B=[1+%i 4-%i 2+1/2*%i 3;
    *    3 9 8+42*%i 2 ]
    * Note that it is done column by column
    */
    {

        double B[] = {1, 3, 4, 9, 2, 8, 3, 2};
        double B_img[] = {1, 0.233, -1, -0.2, 0.5, 42, -23, 123}; /* Declare the matrix */

        int rowB = 2, colB = 4; /* Size of the matrix */
        char variableNameB[] = "B";
        SciErr sciErr;

        /* Write it into Scilab's memory */
        sciErr = createNamedComplexMatrixOfDouble(pvApiCtx, variableNameB, rowB, colB, B, B_img);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
        }

        printf("\n");
        printf("Display from Scilab of B:\n");
        SendScilabJob("disp(B);"); /* Display B */
    }


    /******************************** READ ****************************/

    /* Load the previously set variable A */
    {
        int rowA_ = 0, colA_ = 0, lp = 0;
        int i = 0, j = 0;
        double *matrixOfComplex = NULL;
        double *matrixOfComplex_img = NULL;

        char variableToBeRetrieved[] = "A";
        SciErr sciErr;

        /* First, retrieve the size of the matrix */
        sciErr = readNamedComplexMatrixOfDouble(pvApiCtx, variableToBeRetrieved, &rowA_, &colA_, NULL, NULL);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
        }

        /* Alloc the memory */
        matrixOfComplex = (double*)malloc((rowA_ * colA_ * 2) * sizeof(double));
        matrixOfComplex_img = matrixOfComplex + (rowA_ * colA_);

        /* Load the matrix */
        sciErr = readNamedComplexMatrixOfDouble(pvApiCtx, variableToBeRetrieved, &rowA_, &colA_, matrixOfComplex, matrixOfComplex_img);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
        }


        printf("\n");
        printf("Display raw A (size: %d, %d):\n", rowA_, colA_);
        for (i = 0; i < rowA_ * colA_ * 2; i++) /* *2 is because complex part is store
										 * at the end
										 */
        {
            fprintf(stdout, "A[%d] = %5.2f\n", i, matrixOfComplex[i]);
        }

        printf("\n");
        printf("Display formated A (size: %d, %d):\n", rowA_, colA_);
        for (i = 0; i < rowA_ * colA_; i++)
        {
            fprintf(stdout, "%5.2f + %5.2f.i ", matrixOfComplex[i], matrixOfComplex[i + colA_]);
        }
        printf("\n");

        if (matrixOfComplex)
        {
            free(matrixOfComplex);
            matrixOfComplex = NULL;
        }
    }

    /* Load the previously set variable B */
    {
        int rowB_ = 0, colB_ = 0, lp_ = 0;
        int i = 0, j = 0;

        double *matrixOfComplexB = NULL;
        double *matrixOfComplexB_img = NULL;
        char variableToBeRetrievedB[] = "B";
        SciErr sciErr;
        /* First, retrieve the size of the matrix */

        sciErr = readNamedComplexMatrixOfDouble(pvApiCtx, variableToBeRetrievedB, &rowB_, &colB_, NULL, NULL);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
        }


        /* Alloc the memory */
        matrixOfComplexB = (double*)malloc((rowB_ * colB_) * sizeof(double));
        matrixOfComplexB_img = (double*)malloc((rowB_ * colB_) * sizeof(double));

        /* Load the matrix */
        sciErr = readNamedComplexMatrixOfDouble(pvApiCtx, variableToBeRetrievedB, &rowB_, &colB_, matrixOfComplexB, matrixOfComplexB_img);
        if (sciErr.iErr)
        {
            printError(&sciErr, 0);
        }


        printf("\n");
        printf("Display from B raw - real part (size: %d, %d):\n", rowB_, colB_);
        for (i = 0; i < rowB_ * colB_; i++) /* *2 is because complex part is store
										  * at the end
										  */
        {
            /* Display the raw matrix */
            fprintf(stdout, "B[%d] = %5.2f\n", i, matrixOfComplexB[i]);
        }
        printf("Display from B raw - imaginary part (size: %d, %d):\n", rowB_, colB_);
        for (i = 0; i < rowB_ * colB_; i++) /* *2 is because complex part is store
										  * at the end
										  */
        {
            /* Display the raw matrix */
            fprintf(stdout, "B[%d] = %5.2f\n", i, matrixOfComplexB_img[i]);
        }

        printf("\n");
        printf("Display from B formated (size: %d, %d):\n", rowB_, colB_);
        for (j = 0 ; j < rowB_ ; j++)
        {
            for (i = 0 ; i < colB_ ; i++)
            {
                /* Display the formated matrix ... the way the user
                 * expect */
                printf("%5.2f + %5.2f.i  ", matrixOfComplexB[i * rowB_ + j], matrixOfComplexB_img[i * rowB_ + j]);
            }
            printf("\n"); /* New row of the matrix */
        }

        if (matrixOfComplexB)
        {
            free(matrixOfComplexB);
            matrixOfComplexB = NULL;
        }
        if (matrixOfComplexB_img)
        {
            free(matrixOfComplexB_img);
            matrixOfComplexB_img = NULL;
        }
    }

    if ( TerminateScilab(NULL) == FALSE )
    {
        fprintf(stderr, "Error while calling TerminateScilab\n");
        return -2;
    }
    return 0;
}
static bool import_double(int* pvCtx, int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
{
    SciErr sciErr;
    int iRet = 0;
    double *pdblReal = NULL;
    double *pdblImg = NULL;
    int iDims = 0;
    int* piDims = NULL;
    int iComplex = 0;
    int iSize = 0;

    iRet = getDatasetInfo(_iDatasetId, &iComplex, &iDims, NULL);
    if (iRet < 0)
    {
        return false;
    }

    if (iDims)
    {
        if (iDims > 2)
        {
            //hypermatrix
            return false;
        }

        piDims = (int*)MALLOC(sizeof(int) * iDims);
        iSize = getDatasetInfo(_iDatasetId, &iComplex, &iDims, piDims);

        if (iSize > 0)
        {
            pdblReal = (double *)MALLOC(iSize * sizeof(double));

            if (iComplex)
            {
                pdblImg = (double *)MALLOC(iSize * sizeof(double));
                iRet = readDoubleComplexMatrix(_iDatasetId, pdblReal, pdblImg);
            }
            else
            {
                iRet = readDoubleMatrix(_iDatasetId, pdblReal);
            }

            //to be sure ti have 2 dims
            if (iDims == 1)
            {
                FREE(piDims);
                piDims = (int*)MALLOC(sizeof(int) * 2);
                piDims[0] = 1;
                piDims[1] = iSize;
            }
        }
    }

    if (iDims == 0 || iSize == 0) //empty matrix
    {
        if (piDims)
        {
            FREE(piDims);
        }

        /*bug 7224 : to close dataset */
        iRet = readEmptyMatrix(_iDatasetId);
        if (iRet)
        {
            return false;
        }

        // Hack to sure that piDims will not be null at line 372.
        iDims = 2;
        piDims = (int*)MALLOC(sizeof(int) * iDims);
        memset(piDims, 0, sizeof(int) * iDims);
        pdblReal = (double*)MALLOC(sizeof(double) * 1);
        pdblReal[0] = 0;
        iComplex = 0;
    }

    if (_piAddress == NULL)
    {
        if (iComplex)
        {
            sciErr = createNamedComplexMatrixOfDouble(pvCtx, _pstVarname, piDims[0], piDims[1], pdblReal, pdblImg);
        }
        else
        {
            sciErr = createNamedMatrixOfDouble(pvCtx, _pstVarname, piDims[0], piDims[1], pdblReal);
        }
    }
    else //if not null this variable is in a list
    {
        if (iComplex)
        {
            sciErr = createComplexMatrixOfDoubleInNamedList(pvCtx, _pstVarname, _piAddress, _iItemPos, piDims[0], piDims[1], pdblReal, pdblImg);
        }
        else
        {
            sciErr = createMatrixOfDoubleInNamedList(pvCtx, _pstVarname, _piAddress, _iItemPos, piDims[0], piDims[1], pdblReal);
        }
    }

    FREE(piDims);
    FREE(pdblReal);
    if (iComplex)
    {
        FREE(pdblImg);
    }

    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return false;
    }

    return true;
}
static bool import_double_v1(int* pvCtx, int _iDatasetId, int _iItemPos, int *_piAddress, char *_pstVarname)
{
    int iRet = 0;
    double *pdblReal = NULL;
    double *pdblImg = NULL;
    int iRows = 0;
    int iCols = 0;
    int iComplex = 0;
    SciErr sciErr;

#ifdef TIME_DEBUG
    LARGE_INTEGER iStart, iEnd, iFreq;

    QueryPerformanceFrequency(&iFreq);
    QueryPerformanceCounter(&iStart);
#endif

    iRet = getDatasetDims_v1(_iDatasetId, &iRows, &iCols);
    iComplex = isComplexData_v1(_iDatasetId);
    if (iRet)
    {
        return false;
    }

    if (iRows * iCols != 0)
    {
        if (iComplex)
        {
            pdblReal = (double *)MALLOC(iRows * iCols * sizeof(double));
            pdblImg = (double *)MALLOC(iRows * iCols * sizeof(double));
            iRet = readDoubleComplexMatrix_v1(_iDatasetId, iRows, iCols, pdblReal, pdblImg);
        }
        else
        {
            pdblReal = (double *)MALLOC(iRows * iCols * sizeof(double));
            iRet = readDoubleMatrix_v1(_iDatasetId, iRows, iCols, pdblReal);
        }

        if (iRet)
        {
            FREE(pdblReal);
            if (iComplex)
            {
                FREE(pdblImg);
            }

            return false;
        }
    }
    else
    {
        /*bug 7224 : to close dataset */
        iRet = readEmptyMatrix_v1(_iDatasetId);
        if (iRet)
        {
            return false;
        }

        pdblReal = (double*)MALLOC(sizeof(double) * 1);
        pdblReal[0] = 0;
        iComplex = 0;
    }

    if (_piAddress == NULL)
    {
        if (iComplex)
        {
            sciErr = createNamedComplexMatrixOfDouble(pvCtx, _pstVarname, iRows, iCols, pdblReal, pdblImg);
        }
        else
        {
            sciErr = createNamedMatrixOfDouble(pvCtx, _pstVarname, iRows, iCols, pdblReal);
        }
    }
    else                        //if not null this variable is in a list
    {
        if (iComplex)
        {
            sciErr = createComplexMatrixOfDoubleInNamedList(pvCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pdblReal, pdblImg);
        }
        else
        {
            sciErr = createMatrixOfDoubleInNamedList(pvCtx, _pstVarname, _piAddress, _iItemPos, iRows, iCols, pdblReal);
        }
    }

    FREE(pdblReal);
    if (iComplex)
    {
        FREE(pdblImg);
    }

    if (sciErr.iErr)
    {
        printError(&sciErr, 0);
        return false;
    }

#ifdef PRINT_DEBUG
    char pstMsg[512];

    sprintf(pstMsg, "double_%d (%d x %d)", _iItemPos, iRows, iCols);
    print_tree(pstMsg);
#endif

#ifdef TIME_DEBUG
    QueryPerformanceCounter(&iEnd);
    double dblTime = ((iEnd.QuadPart - iStart.QuadPart) * 1000.0) / iFreq.QuadPart;

    printf("Total Double : %0.3f ms\n\n", dblTime);
#endif

    return true;
}