title: Understanding LAPACK calls in C++ with a simple example
body: <p>I am a beginner with LAPACK and C++/Fortran interfacing. I need to solve linear equations and eigenvalues problems using LAPACK/BLAS on Mac OS-X Lion. OS-X Lion provides optimized BLAS and LAPACK libraries (in /usr/lib) and I am linking these libraries instead of downloading them from netlib. </p>
  
  <p>My program (reproduced below) is compiling and running fine, but it is giving me wrong answers. I have researched in the web and Stackoverflow and the issue may have to deal with how C++ and Fortran store arrays in differing formats (row major vs Column major). However, as you will see in my example, the simple array for my example should look identical in C++ and fortran. Anyway here goes.</p>
  
  <p>Lets say we want to solve the following linear system:</p>
  
  <p>x + y = 2</p>
  
  <p>x - y = 0</p>
  
  <p>The solution is (x,y) = (1,1). Now I tried to solve this using Lapack as follows</p>
  
  <pre><code>// LAPACK test code
  
  #include&lt;iostream&gt;
  #include&lt;vector&gt;
  
  using namespace std;
  extern "C" void dgetrs(char *TRANS, int *N, int *NRHS, double *A, 
                        int *LDA, int *IPIV, double *B, int *LDB, int *INFO );
  
  int main()
  {
      char trans = 'N';
      int dim = 2;    
      int nrhs = 1;
      int LDA = dim;
      int LDB = dim;
      int info;
  
      vector&lt;double&gt; a, b;
  
      a.push_back(1);
      a.push_back(1);
      a.push_back(1);
      a.push_back(-1);
  
      b.push_back(2);
      b.push_back(0);
  
      int ipiv[3];
  
  
      dgetrs(&amp;trans, &amp;dim, &amp;nrhs, &amp; *a.begin(), &amp;LDA, ipiv, &amp; *b.begin(), &amp;LDB, &amp;info);
  
  
      std::cout &lt;&lt; "solution is:";    
      std::cout &lt;&lt; "[" &lt;&lt; b[0] &lt;&lt; ", " &lt;&lt; b[1] &lt;&lt; ", " &lt;&lt; "]" &lt;&lt; std::endl;
      std::cout &lt;&lt; "Info = " &lt;&lt; info &lt;&lt; std::endl; 
  
      return(0);
  }
  </code></pre>
  
  <p>This code was compiled as follows:</p>
  
  <p><code>g++ -Wall -llapack -lblas lapacktest.cpp</code></p>
  
  <p>On running this, however, I get the solution as (-2,2) which is obviously wrong. I have tried all combination of row/column re-arrangement of my matrix <code>a</code>. Also observe the matrix representation of <code>a</code> should be identical in row and column formats. I think getting this simple example to work will get me started with LAPACK and any help will be appreciated. </p>