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

solving system of linear algebraic equations, dgesv

shapovalov
Beginner
926 Views

I want to solve system of linear algebraic equations with function dgesv. I want to understand how to use MKL functions from C++, but all examples are written forFortran.

double a1[16]={1.80, 2.88, 2.05, -0.89,
5.25, -2.95, -0.95, -3.80,
1.58, -2.69, -2.90, -1.04,
-1.11, -0.66, -0.59, 0.80};
double b1[4]={9.52, 24.35, 0.77, -6.22};

int N1=1,info,Size=4;
int *ipiv = new int[Size];
dgesv(&Size,&N1,a1,&Size,ipiv,b1,&Size,&info);

Arrayb1 must contain solution of system (1,-1,3,-5), but it contains other values(-1.37, -9.78, 10.53, -42.06).
What's my mistake? May be, because ofindexing? (in C++ from 0, in Fortran from 1)
And I shoulddefine arrays a1 and b1as
double a1[16]={ 0, 0, 0, 0, 0,
0, 1.80, 2.88, 2.05, -0.89,
0, 5.25, -2.95, -0.95, -3.80,
0, 1.58, -2.69, -2.90, -1.04,
0, -1.11, -0.66, -0.59, 0.80};
double b1[4]={0, 9.52, 24.35, 0.77, -6.22};

Or not?

0 Kudos
2 Replies
ArturGuzik
Valued Contributor I
926 Views
Quoting - shapovalov

I want to solve system of linear algebraic equations with function dgesv. I want to understand how to use MKL functions from C++, but all examples are written forFortran.

double a1[16]={1.80, 2.88, 2.05, -0.89,
5.25, -2.95, -0.95, -3.80,
1.58, -2.69, -2.90, -1.04,
-1.11, -0.66, -0.59, 0.80};
double b1[4]={9.52, 24.35, 0.77, -6.22};

int N1=1,info,Size=4;
int *ipiv = new int[Size];
dgesv(&Size,&N1,a1,&Size,ipiv,b1,&Size,&info);

Arrayb1 must contain solution of system (1,-1,3,-5), but it contains other values(-1.37, -9.78, 10.53, -42.06).
What's my mistake? May be, because ofindexing? (in C++ from 0, in Fortran from 1)
And I shoulddefine arrays a1 and b1as
double a1[16]={ 0, 0, 0, 0, 0,
0, 1.80, 2.88, 2.05, -0.89,
0, 5.25, -2.95, -0.95, -3.80,
0, 1.58, -2.69, -2.90, -1.04,
0, -1.11, -0.66, -0.59, 0.80};
double b1[4]={0, 9.52, 24.35, 0.77, -6.22};

Or not?


Hi,

I might be wrong but:

(1) dgesv is LAPACK function

(2) it takes a coefficient matrix, that is, A, and not A[m x m] (observe size/indexing issue)

(3) for LAPACK call consider matrix reordering (to Fortran style column-wise), passing first argument may go (may not?) into play.

(4) LAPACK also requires passing data by address and not value


All that is summarized, for example, here.


A.
0 Kudos
shapovalov
Beginner
926 Views
Thank you very much. I realized what my mistake. The matrixashould be transposed.
0 Kudos
Reply