Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Hainan_W_
Beginner
104 Views

basic question using MKL data fitting

Hi all,

This is my first time using MKL data fitting function. The following code was adopted from the online example with only input data added (https://software.intel.com/en-us/node/522217). For data points of xi = 0 to 9 and yi = xi^3, I want to test if I used use these functions correctly. I was targeting to get the interpolation value for 20 points from 0 to 19 which should be stored in r array. "status" was negative after calling dfdInterpolate1D() and r array remained as its initial values. 

I suppose there is something basic that I did not get right. Can anyone advise?

Regards,

Hainan 

#include "mkl.h"

#define NX 10                     /* Size of partition, number of breakpoints */
#define NSITE 20                 /* Number of interpolation sites */
#define SPLINE_ORDER DF_PP_CUBIC   /* A cubic spline to construct */ 

int main()

{

        int status;          /* Status of a Data Fitting operation */
        DFTaskPtr task;      /* Data Fitting operations are task based */

        /* Parameters describing the partition */
        MKL_INT nx;          /* The size of partition x */
        double x[NX];         /* Partition x */
        MKL_INT xhint;       /* Additional information about the structure of breakpoints */

        /* Parameters describing the function */
        MKL_INT ny;          /* Function dimension */
        double y[NX];         /* Function values at the breakpoints */
        MKL_INT yhint;       /* Additional information about the function */

        /* Parameters describing the spline */
        MKL_INT  s_order;    /* Spline order */
        MKL_INT  s_type;     /* Spline type */
        MKL_INT  ic_type;    /* Type of internal conditions */
        double* ic;         /* Array of internal conditions */
        MKL_INT  bc_type;    /* Type of boundary conditions */
        double* bc;         /* Array of boundary conditions */

        double scoeff[(NX-1)* SPLINE_ORDER];   /* Array of spline coefficients */
        MKL_INT scoeffhint;            /* Additional information about the coefficients */

        /* Parameters describing interpolation computations */
        MKL_INT nsite;        /* Number of interpolation sites */
        double site[NSITE];   /* Array of interpolation sites */
        MKL_INT sitehint;     /* Additional information about the structure of 
                              interpolation sites */

        MKL_INT ndorder, dorder;    /* Parameters defining the type of interpolation */

        double* datahint;   /* Additional information on partition and interpolation sites */

        double r[NSITE];    /* Array of interpolation results */
        MKL_INT rhint;     /* Additional information on the structure of the results */
        MKL_INT* cell;      /* Array of cell indices */

        /* Initialize the partition */
        nx = NX;

        /* Set values of partition x */
        //...
        xhint = DF_NON_UNIFORM_PARTITION;  /* The partition is non-uniform. */

        /* Initialize the function */
        ny = 1;               /* The function is scalar. */

        /* Set function values */
        for (int i = 0; i < NX; i++)
        {
            x = i;
            y = pow(i, 3);
        }
        yhint = DF_NO_HINT;    /* No additional information about the function is provided. */

        /* Create a Data Fitting task */
        status = dfdNewTask1D( &task, nx, x, xhint, ny, y, yhint );

        /* Check the Data Fitting operation status */
        //...

        /* Initialize spline parameters */
        s_order = DF_PP_CUBIC;     /* Spline is of the fourth order (cubic spline). */ 
        s_type = DF_PP_BESSEL;     /* Spline is of the Bessel cubic type. */ 

        /* Define internal conditions for cubic spline construction (none in this example) */
        ic_type = DF_NO_IC; 
        ic = NULL;

        /* Use not-a-knot boundary conditions. In this case, the is first and the last 
        interior breakpoints are inactive, no additional values are provided. */
        bc_type = DF_BC_NOT_A_KNOT; 
        bc = NULL;
        scoeffhint = DF_NO_HINT;    /* No additional information about the spline. */ 

        /* Set spline parameters  in the Data Fitting task */
        status = dfdEditPPSpline1D( task, s_order, s_type, bc_type, bc, ic_type,
            ic, scoeff, scoeffhint );

        /* Check the Data Fitting operation status */
        //...

        /* Use a standard method to construct a cubic Bessel spline: */
        /* Pi(x) = ci,0 + ci,1(x - xi) + ci,2(x - xi)2 + ci,3(x - xi)3, */
        /* The library packs spline coefficients to array scoeff: */
        /* scoeff[4*i+0] = ci,0, scoef[4*i+1] = ci,1,         */   
        /* scoeff[4*i+2] = ci,2, scoef[4*i+1] = ci,3,         */   
        /* i=0,...,N-2  */
        status = dfdConstruct1D( task, DF_PP_SPLINE, DF_METHOD_STD );

        /* Check the Data Fitting operation status */
        //...

        /* Initialize interpolation parameters */
        nsite = NSITE;

        /* Set site values */
        //...
        for (int i = 0; i < NSITE; i++)
        {
            site = i;
        }

        sitehint = DF_NON_UNIFORM_PARTITION; /* Partition of sites is non-uniform */

        /* Request to compute spline values */
        ndorder = 1;
        dorder = 1;
        datahint = DF_NO_APRIORI_INFO;  /* No additional information about breakpoints or
                                        sites is provided. */
        rhint = DF_MATRIX_STORAGE_ROWS; /* The library packs interpolation results 
                                        in row-major format. */
        cell = NULL;                    /* Cell indices are not required. */

        /* Solve interpolation problem using the default method: compute the spline values
        at the points site(i), i=0,..., nsite-1 and place the results to array r */ 
        status = dfdInterpolate1D( task, DF_INTERP, DF_METHOD_STD, nsite, site,
            sitehint, ndorder, &dorder, datahint, r, rhint, cell );

        /* Check Data Fitting operation status */ 
        //...

        /* De-allocate Data Fitting task resources */
        status = dfDeleteTask( &task );
        /* Check Data Fitting operation status */ 
        //...
        return 0 ;

}

 

0 Kudos
4 Replies
mecej4
Black Belt
104 Views

Look in your .../mkl/examples/datafittingc/source directory, where you will find a complete example code (dfdcubicspline_interp.c) that generates data, calls the spline fitting routines, and checks the fitted values: 

104 Views

Hello Hainan,

Please change DF_METHOD_STD to DF_METHOD_PP in the line of code that calls dfdInterpolate1D function. The code will look like this:

status = dfdInterpolate1D( task, DF_INTERP, DF_METHOD_PP, nsite, site,
            sitehint, ndorder, &dorder, datahint, r, rhint, cell );

We have found that the example of a spline-based interpolation in our documentation is a bit incorrect, it passes incorrect value of method to dfdInterpolate1D function. We plan to fix this issue in the future releases of Intel® MKL.

Best regards,

Victoriya

Hainan_W_
Beginner
104 Views

Hi Victoriya,

Thank you for the reply. The code after applying the change you mentioned works. Then, I tried to test the code for yi = xi^2 with the following change in code: 

#define SPLINE_ORDER DF_PP_QUADRATIC

s_order = DF_PP_QUADRATIC;

But it did not work: status is -1011 after calling dfdConstruct1D( task, DF_PP_SPLINE, DF_METHOD_STD ). I tried to use s_type = DF_PP_DEFAULT which did not work, either. Do you know where the problem might be? I currently do not have mkl examples (I will ask for my admin for it), so I have difficulty in figuring out such possibly easy issue.

Regards,

Hainan

Victoriya Kardakova (Intel) wrote:

Hello Hainan,

Please change DF_METHOD_STD to DF_METHOD_PP in the line of code that calls dfdInterpolate1D function. The code will look like this:

status = dfdInterpolate1D( task, DF_INTERP, DF_METHOD_PP, nsite, site,
            sitehint, ndorder, &dorder, datahint, r, rhint, cell );

We have found that the example of a spline-based interpolation in our documentation is a bit incorrect, it passes incorrect value of method to dfdInterpolate1D function. We plan to fix this issue in the future releases of Intel® MKL.

Best regards,

Victoriya

104 Views

Hello Hainan,

The internal conditions and boundary conditions that you have used previously for the cubic spline are not valid for the quadratic spline. You can learn more about the boundary conditions here: https://software.intel.com/en-us/node/522222#TBL17-8

For s_type = DF_PP_DEFAULT you can use following values, for example:   

double bc;
/***** Parameters describing boundary conditions type *****/
bc_type    = DF_BC_Q_VAL;
bc         = 1.0;
   
double* ic;
/***** Parameters describing internal conditions type *****/
/* No internal conditions are provided for quadratic spline */
ic_type    = DF_NO_IC;
ic         = 0;

You can also find interpretation of error codes here: https://software.intel.com/en-us/node/522218. To map the value of the error code to the corresponding macro please use the file <MKLROOT>/include/mkl_df_defines.h.

Best regards,

Victoriya

 

Reply