Question

any body knows how to convert 2d dynamic array to static so that i can use it in lapacke. dgels function which only take static matrix in c? when i use malloc it does not give correct answer. how can i use malloc so that it works with it.thankyou

#include <stdio.h>
#include <lapacke.h>
#include <conio.h>

int main (int argc, const char * argv[])  
{
    /*double a[5][3] = {1,1,1,2,3,4,3,5,2,4,2,5,5,4,3};*/

    double b[5][2] = {-10,-3,12,14,14,12,16,16,18,16};
    lapack_int info,m,n,lda,ldb,nrhs;
    int i,j;
    double **a;

    a=(double**)malloc(5* sizeof(double*));
    for (i=0;i<5;i++)
    {
        a[i]=(double*)malloc(3* sizeof(double));
    }
    a[0][0]=1; 
    a[0][1]=1;
    a[0][2]=1;
    a[1][0]=2;
    a[1][1]=3;
    a[1][2]=4;
    a[2][0]=3;
    a[2][1]=5;
    a[2][2]=2;
    a[3][0]=4;
    a[3][1]=2;
    a[3][2]=5;
    a[4][0]=5; 
    a[4][1]=4;
    a[4][2]=3;

    m = 5;
    n = 3;
    nrhs = 2;
    lda = 3;
    ldb = 2;

    info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,*a,lda,*b,ldb);

    for(i=0;i<n;i++)
    {
        for(j=0;j<nrhs;j++)
        {
            printf("%lf ",b[i][j]);
        }
        printf("\n");
    }
    getch();
    return(info);
}
Was it helpful?

Solution 2

a is not a 2d array, it is an array of pointers to separate 1d arrays. Passing *a to LAPACKE_dgels only gives it a pointer to the first row. It will have no way to know where all of the other rows were allocated since they were allocated independently. It wants the entire array to be in a single contiguous block of memory. a must be of type double*, not double**, and you don't dereference it when passing it. You must flatten the 2d indexes into 1d indexes yourself, using either row or column major form (which you tell the function).

EDIT

The following code allocates a flat 1d array with room for m*n doubles. It then fills the array by converting the 2d indices to 1d row-major indices using the formula row * n + col. If we wanted column-major indices, we would use col * m + row.

#include <stdio.h>
#include <lapacke.h>
#include <conio.h>

int main (int argc, const char * argv[])  
{
    double b[5][2] = {-10,-3,12,14,14,12,16,16,18,16};
    lapack_int info,m,n,lda,ldb,nrhs;
    int i,j;
    double *a;

    m = 5;
    n = 3;
    nrhs = 2;
    lda = 3;
    ldb = 2;

    a = malloc(m * n * sizeof(double));
    a[0 * n + 0] = 1;
    a[0 * n + 1] = 1;
    a[0 * n + 2] = 1;
    a[1 * n + 0] = 2;
    a[1 * n + 1] = 3;
    a[1 * n + 2] = 4;
    a[2 * n + 0] = 3;
    a[2 * n + 1] = 5;
    a[2 * n + 2] = 2;
    a[3 * n + 0] = 4;
    a[3 * n + 1] = 2;
    a[3 * n + 2] = 5;
    a[4 * n + 0] = 5; 
    a[4 * n + 1] = 4;
    a[4 * n + 2] = 3;

    info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,a,lda,*b,ldb);

    for(i=0;i<n;i++)
    {
        for(j=0;j<nrhs;j++)
        {
            printf("%lf ",b[i][j]);
        }
        printf("\n");
    }
    getch();
    return(info);
}

OTHER TIPS

I do not know lapacke.dgels but try to change:

double **a;
a=(double**)malloc(5* sizeof(double*));
for (i=0;i<5;i++)
{
    a[i]=(double*)malloc(3* sizeof(double));
}

to:

double (*a)[3];
a = malloc(5 * 3 * sizeof(double));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top