Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.

Invert Matrix from C

jmorgie
Beginner
1,046 Views

I know this is trivial but i cant find the answer anywhere else:

have in C, matrix A[16][16] [A is not symmetric.] that i want to invert. I have this code which is giving incorrect answer:

void inverseRD ( float* I, float* A, int N)

float work [WTDIM][WTDIM];
float *p;
p = &work[0][0];
int ipivot [256];
int lwork = 256;

sgetrf ( &N, &N, A, &N, &ipivot[0], &info);
sgetri ( &N, I, &N, &ipivot[0], p, &lwork, &info);

I assume i have made an error in passing parameters but I cant see it.

0 Kudos
1 Solution
Melvin_Robinson
New Contributor I
1,046 Views

I rewrote your code correcting some small errors, and making it a little smaller so that you can test. You might need to remove the underscores because I did not do this with MKL, but just LAPACK. My version destroys the original matrix as the sgetrf and sgetri will anyway, so you should make a copy of the original matrix to work with the routines.

1. info has not been declared as an integer.

2. ipivot can be passed as shown below, though your method is probably OK.

#include

void inverseRD (float *A, int N)
{
float work [2][2];
int ipivot [4];
int lwork = 4;
int info;

sgetrf_( &N, &N, A, &N, ipivot, &info);
sgetri_( &N, A, &N, ipivot, work, &lwork, &info);
}

int main()
{
float A[4]={2,1,1,2}, B[4];
inverseRD(A,2);
printf("%f %f %f %f\n",A[0],A[1],A[2],A[3]);
}

View solution in original post

0 Kudos
1 Reply
Melvin_Robinson
New Contributor I
1,047 Views

I rewrote your code correcting some small errors, and making it a little smaller so that you can test. You might need to remove the underscores because I did not do this with MKL, but just LAPACK. My version destroys the original matrix as the sgetrf and sgetri will anyway, so you should make a copy of the original matrix to work with the routines.

1. info has not been declared as an integer.

2. ipivot can be passed as shown below, though your method is probably OK.

#include

void inverseRD (float *A, int N)
{
float work [2][2];
int ipivot [4];
int lwork = 4;
int info;

sgetrf_( &N, &N, A, &N, ipivot, &info);
sgetri_( &N, A, &N, ipivot, work, &lwork, &info);
}

int main()
{
float A[4]={2,1,1,2}, B[4];
inverseRD(A,2);
printf("%f %f %f %f\n",A[0],A[1],A[2],A[3]);
}

0 Kudos
Reply