I want to solve a linear equation system using the Lapack package in C++. I plan to implement it like this using the routines from here, namely dgesv.
This is my code:
unsigned short int n=profentries.size();
double **A, *b, *x;
A= new double*[n];
b=new double[n];
x=new double[2];
// Generate the matrices for my equation
for(int i = 0; i < n; ++i)
{
A[i] = new double[2];
}
for(int i=0; i<n; i++)
{
A[i][0]=5;
A[i][1]=1;
b[i]=3;
}
std::cout<< "printing matrix A:"<<std::endl;
for(int i=0; i<n; i++)
{
std::cout<< A[i][0]<<" " <<A[i][1]<<std::endl;
}
// Call the LAPACK solver
x = new double[n];//probably not necessary
dgesv(A, b, 2, x); //wrong result for x!
std::cout<< "printing vector x:"<<std::endl;
/*prints
3
3
but that's wrong, the solution is (0.6, 0)!
*/
for(int i=0; i<2; i++)
{
std::cout<< x[i]<<std::endl;
}
I have the following problem:
How can it be that dgesv calculates a vector x with the elements {3, 3}? The solution should be {0.6, 0} (checked it with matlab).
Greetings
Edit: dgesv might work for square matrices. My solution below shows how to solve overdetermined systems with dgels.
The problem was for once the mixup of row-major/column major but also the fact that apparently dgesv is not suitable for non-square matrices.
An overdetermined system of linear equations can be solved in the least-squares style, using dgels (#include "lapacke.h").
Noteworthy, to me not apparent at first sight: The solution vector (usually denoted by x) is then stored in b.
My example system consists of a matrix A holding the values 1-10 in its first column and a vector b with the value i^2 for {i|1<=i<=10}:
double a[10][2]={1,1,2,1,3,1,4,1,5,1,6,1,7,1,8,1,9,1,10,1};
double b[10][1]={1,4,9,16,25,36,49,64,81,100};
lapack_int info,m,n,lda,ldb,nrhs;
int i,j;
// description here: http://www.netlib.org/lapack/double/dgesv.f
m = 10;
n = 2;
nrhs = 1;
lda = 2;
ldb = 1;
for(int i=0; i<m; i++)
{
for(int k=0; k<lda; k++)
{
std::cout << a[i][k]<<" ";
}
std::cout <<"" << std::endl;
}
std::cout<< "printing vector b:"<<std::endl;
for(int i=0; i<m; i++)
{
std::cout<< *b[i]<<std::endl;
}
std::cout<< "\nStarting calculation..."<<std::endl;
info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,*a,lda,*b,ldb);
std::cout<< "Done."<<std::endl;
std::cout<< " "<<std::endl;
std::cout<< "printing vector b:"<<std::endl;
for(int i=0; i<2; i++)
{
std::cout<< *b[i]<<std::endl;
}
std::cout<< "Used values: "<< m << ", " << n << ", " << nrhs << ", " << lda << ", " << ldb << std::endl;