- Code: Select all
#include <iostream>
using namespace std;
extern "C" void sgels_ ( ... );
int main(){
float mA[8][2];
float v[8];
float mWork[16];
mA[0][0]=100; mA[1][0]=90; v[0]=0.80;
mA[0][1]=97; mA[1][1]=90; v[1]=0.72;
mA[0][2]=104; mA[1][2]=99; v[2]=0.63;
mA[0][3]=96; mA[1][3]=96; v[3]=0.55;
mA[0][4]=99; mA[1][5]=99; v[4]=0.52;
mA[0][5]=100; mA[1][5]=97; v[5]=0.50;
mA[0][6]=96; mA[1][5]=96; v[6]=0.49;
mA[0][7]=98; mA[1][5]=100; v[7]=0.45;
char trans = 'N';
int m=8;
int n=2;
int nrhs = 2;
int lwork = 16;
int info = 0;
sgels_(&trans, &m, &n, &nrhs, mA, &m, v, &m, mWork, &lwork, &info);
if(info) printf("Error - sgels returned: %d\n", info);
for(int i=0; i<m; i++){
printf("%f\n", v[i]);
}
}
And I'm compiling with "g++ linear.cpp -llapack -lblas -lg". The output I get is:
- Code: Select all
0.005857
-0.011565
0.026700
-0.075284
-0.124824
-0.151338
-0.135284
-0.201338
As I understand it, the first two values should be the coefficients. But using those values gives extremely wrong results. Using Excel, the actual coefficients should be B1=0.03196 and B2=-0.02684 with no intercept, and B0=1.183, B1=0.0229, B2=-0.0299 with an intercept. What is my mistake?
Some other questions:
- What should NRHS be? It says, "The number of right hand sides, i.e., the number of columns of the matrices B and X. NRHS >=0." But I don't know if that means the number of columns each has (i.e. 1), or the sum (i.e. 2). It doesn't seem to matter here though, they both give the same values.
- For LWORK, what's NB, optimum block size?
- Is there any way one can set a gels routine to return an intercept? I didn't see anything about that on the man page, or at the netlib LLS page.

